Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add build opts for enabling sanitizers #731

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ pro hand -p true -s false -n false SIGBUS
pro hand -p true -s false -n false SIGSEGV
```

### Sanitizers

Musl does not have proper sanitizer support, so until we support native (GNU) linux builds, these are limited to mac only.

In order to use address sanitizer (`-Dasan`) and undefined behavior sanitizer (`-Dubsan`), you need to have version 18 of llvm installed on your machine. You also have to build natively, since cross-compilation is not supported.

macOS:
```terminal
brew install llvm@18
```

<!-- linux: -->
<!-- ```terminal -->
<!-- apt-get install llvm-18 clang-18 -->
<!-- ``` -->

## Build

Once you install `zig`, you're ready to build:
Expand Down Expand Up @@ -73,6 +89,12 @@ Supported values:
Provide additional compiler flags. These propagate to all build artifacts and
dependencies.

#### `-Dasan`
Enable address sanitizer. Only supported in native macos builds. Requires llvm 18, see [prerequisites](#sanitizers).

#### `-Dubsan`
Enable undefined behavior sanitizer. Only supported in native macos builds. Requires llvm 18, see [prerequisites](#sanitizers).

<!-- ## LSP Integration -->

<!-- ```console -->
Expand Down
81 changes: 80 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const std = @import("std");

const VERSION = "3.1";

// TODO: x86_64-linux-gnu & aarch64-linux-gnu
const targets: []const std.Target.Query = &.{
.{ .cpu_arch = .aarch64, .os_tag = .macos, .abi = null },
.{ .cpu_arch = .x86_64, .os_tag = .macos, .abi = null },
Expand All @@ -19,6 +20,8 @@ const BuildCfg = struct {
mem_dbg: bool = false,
c3dbg: bool = false,
snapshot_validation: bool = false,
ubsan: bool = false,
asan: bool = false,
};

pub fn build(b: *std.Build) !void {
Expand Down Expand Up @@ -85,6 +88,26 @@ pub fn build(b: *std.Build) !void {
"Binary name (Default: urbit)",
) orelse "urbit";

// Sanitizers are not properly supported in musl => mac only

const asan = if (target.query.isNative() and target.result.isDarwin())
b.option(
bool,
"asan",
"Enable address sanitizer (native only, requires llvm@18)",
) orelse false
else
false;

const ubsan = if (target.query.isNative() and target.result.isDarwin())
b.option(
bool,
"ubsan",
"Enable undefined behavior sanitizer (native only, requires llvm@18)",
) orelse false
else
false;

// Parse short git rev
//
var file = try std.fs.cwd().openFile(".git/logs/HEAD", .{});
Expand Down Expand Up @@ -118,6 +141,8 @@ pub fn build(b: *std.Build) !void {
.mem_dbg = mem_dbg,
.c3dbg = c3dbg,
.snapshot_validation = snapshot_validation,
.asan = asan,
.ubsan = ubsan,
.include_test_steps = !all,
};

Expand Down Expand Up @@ -156,12 +181,38 @@ fn build_single(

try global_flags.appendSlice(cfg.flags);
try global_flags.appendSlice(&.{
"-fno-sanitize=all",
"-g",
"-Wall",
"-Werror",
});

if (!cfg.asan and !cfg.ubsan)
try global_flags.appendSlice(&.{
"-fno-sanitize=all",
});

if (cfg.asan and !cfg.ubsan)
try global_flags.appendSlice(&.{
"-Wno-deprecated",
"-fsanitize=address",
"-fno-sanitize=undefined",
"-fno-sanitize-trap=undefined",
});

if (!cfg.asan and cfg.ubsan)
try global_flags.appendSlice(&.{
"-fsanitize=undefined",
"-fno-sanitize-trap=undefined",
});

if (cfg.asan and cfg.ubsan)
try global_flags.appendSlice(&.{
"-Wno-deprecated",
"-fsanitize=address",
"-fsanitize=undefined",
"-fno-sanitize-trap=undefined",
});

//
// CFLAGS for Urbit Libs and Binaries
//
Expand Down Expand Up @@ -861,6 +912,34 @@ fn build_single(

urbit.linkLibC();

if (t.isDarwin()) {
// Requires llvm@18 homebrew installation
if (cfg.asan or cfg.ubsan)
urbit.addLibraryPath(.{
.cwd_relative = "/opt/homebrew/opt/llvm/lib/clang/18/lib/darwin",
});
if (cfg.asan) urbit.linkSystemLibrary("clang_rt.asan_osx_dynamic");
if (cfg.ubsan) urbit.linkSystemLibrary("clang_rt.ubsan_osx_dynamic");
}

if (t.os.tag == .linux) {
// Requires llvm-18 and clang-18 installation
if (cfg.asan or cfg.ubsan)
urbit.addLibraryPath(.{
.cwd_relative = "/usr/lib/clang/18/lib/linux",
});
if (t.cpu.arch == .x86_64) {
if (cfg.asan) urbit.linkSystemLibrary("clang_rt.asan-x86_64");
if (cfg.ubsan)
urbit.linkSystemLibrary("clang_rt.ubsan_standalone-x86_64");
}
if (t.cpu.arch == .aarch64) {
if (cfg.asan) urbit.linkSystemLibrary("clang_rt.asan-aarch64");
if (cfg.ubsan)
urbit.linkSystemLibrary("clang_rt.ubsan_standalone-aarch64");
}
}

urbit.linkLibrary(vere);
urbit.linkLibrary(pkg_noun);
urbit.linkLibrary(pkg_c3);
Expand Down
6 changes: 5 additions & 1 deletion pkg/c3/portable.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,11 @@
# define U3_OS_LoomBits 30
# elif defined(U3_OS_osx)
# ifdef __LP64__
# define U3_OS_LoomBase 0x28000000000
# ifdef ASAN_ENABLED
# define U3_OS_LoomBase 0x728000000000
# else
# define U3_OS_LoomBase 0x28000000000
# endif
# else
# define U3_OS_LoomBase 0x4000000
# endif
Expand Down
Loading