Skip to content

Commit

Permalink
build: add build opts for enabling sanitizers
Browse files Browse the repository at this point in the history
  • Loading branch information
ripperi committed Oct 9, 2024
1 parent 98bee2a commit c6e23f8
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
20 changes: 20 additions & 0 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ pro hand -p true -s false -n false SIGBUS
pro hand -p true -s false -n false SIGSEGV
```

### Sanitizers

In order to use address sanitizer (`-Dasan`) and undefined behavior sanitizer (`-Dubsan`), you need to have version 18 of llvm and clang 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 +87,12 @@ Supported values:
Provide additional compiler flags. These propagate to all build artifacts and
dependencies.

#### `-Dasan`
Enable address sanitizer. Supported in native builds only. Requires llvm and clang 18, see [prerequisites](#sanitizers).

#### `-Dubsan`
Enable undefined behavior sanitizer. Supported in native builds only. Requires llvm and clang 18, see [prerequisites](#sanitizers).

<!-- ## LSP Integration -->

<!-- ```console -->
Expand Down
69 changes: 68 additions & 1 deletion build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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 +87,18 @@ pub fn build(b: *std.Build) !void {
"Binary name (Default: urbit)",
) orelse "urbit";

const asan = b.option(
bool,
"asan",
"Enable address sanitizer (native only, requires llvm & clang 18)",
) orelse false;

const ubsan = b.option(
bool,
"ubsan",
"Enable undefined behavior sanitizer (native only, requires llvm & clang 18)",
) orelse false;

// Parse short git rev
//
var file = try std.fs.cwd().openFile(".git/logs/HEAD", .{});
Expand Down Expand Up @@ -118,6 +132,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 +172,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 +903,31 @@ 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) {
urbit.addLibraryPath(.{ .cwd_relative = "/usr/lib/clang/18/lib/linux" });
// Requires llvm-18 and clang-18 installation
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

0 comments on commit c6e23f8

Please sign in to comment.