Skip to content

Commit

Permalink
feat: enable LTO mode by default, even for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
tensorush committed Jul 22, 2024
1 parent 30f7aa5 commit 3072b37
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
uses: mlugg/setup-zig@v1

- name: Run `build`
run: zig build -Doptimize=ReleaseFast
run: zig build
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
```sh
git clone https://github.com/allyourcodebase/AFLplusplus.git
cd AFLplusplus/
zig build -Doptimize=ReleaseFast
zig build
```

### Building instrumented executables
For help building instrumented executables, see [kristoff-it/zig-afl-kit](https://github.com/kristoff-it/zig-afl-kit).

For help building instrumented executables, see [kristoff-it/zig-afl-kit](https://github.com/kristoff-it/zig-afl-kit).

<!-- MARKDOWN LINKS -->

Expand Down
27 changes: 13 additions & 14 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ pub fn build(b: *std.Build) !void {

// Custom options
const use_z = b.option(bool, "use-z", "Use system zlib") orelse true;
const enable_lto = b.option(bool, "enable-lto", "Enable LTO mode") orelse true;
const build_nyx = b.option(bool, "build-nyx", "Build Nyx mode on Linux") orelse true;
const enable_wafl = b.option(bool, "enable-wafl", "Enable WAFL mode on WASI") orelse false;
const build_coresight = b.option(bool, "build-coresight", "Build CoreSight mode on ARM64 Linux") orelse true;
const build_unicorn_aarch64 = b.option(bool, "build-unicorn-aarch64", "Build Unicorn mode on ARM64") orelse true;
const enable_lto = b.option(bool, "enable-lto", "Enable LTO mode") orelse if (target.result.isDarwin()) false else true;

// Dependencies
const AFLplusplus_dep = b.dependency("AFLplusplus", .{});
Expand Down Expand Up @@ -259,13 +259,9 @@ pub fn build(b: *std.Build) !void {
b.fmt("-DCLANGPP_BIN=\"{s}/clang++\"", .{llvm_bin_dir}),
});
if (enable_lto) {
llvm_c_flags.appendSliceAssumeCapacity(&.{
"-DAFL_CLANG_FLTO=\"-flto\"",
});
llvm_c_flags.appendAssumeCapacity("-DAFL_CLANG_FLTO=\"-flto\"");
} else {
llvm_c_flags.appendSliceAssumeCapacity(&.{
"-DAFL_CLANG_FLTO=\"\"",
});
llvm_c_flags.appendAssumeCapacity("-DAFL_CLANG_FLTO=\"\"");
}
if (target.query.isNative()) {
llvm_c_flags.appendAssumeCapacity("-march=native");
Expand All @@ -281,11 +277,6 @@ pub fn build(b: *std.Build) !void {
if (enable_wafl and target.result.isWasm()) {
llvm_cpp_flags.appendSliceAssumeCapacity(&.{ "-DNDEBUG", "-DNO_TLS" });
}
if (target.result.isDarwin()) {
llvm_cpp_flags.appendSliceAssumeCapacity(&.{ "-Wl,-flat_namespace", "-Wl,-undefined,suppress" });
} else {
llvm_cpp_flags.appendAssumeCapacity("-Wl,-znodelete");
}

// LLVM instrumentation object suite
const llvm_objs_step = b.step("llvm_objs", "Install LLVM instrumentation object suite");
Expand Down Expand Up @@ -336,9 +327,16 @@ pub fn build(b: *std.Build) !void {
const llvm_libs_step = b.step("llvm_libs", "Install LLVM instrumentation library suite");

const llvm_inc_dir = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--includedir" }), "\n");
const llvm_name = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--libs" }), "\n")[2..];
const llvm_inc_path = std.Build.LazyPath{ .cwd_relative = llvm_inc_dir };

const llvm_libs = std.mem.trimRight(u8, b.run(&.{ "llvm-config", "--libs" }), "\n");
const llvm_name: []const u8 = blk: {
if (std.mem.indexOf(u8, llvm_libs, "-lLLVM-1")) |llvm_lib_name_idx| {
break :blk llvm_libs[llvm_lib_name_idx + 2 .. llvm_lib_name_idx + 9];
}
unreachable;
};

const llvm_common_obj = b.addObject(.{
.name = "afl-llvm-common",
.pic = true,
Expand Down Expand Up @@ -418,7 +416,7 @@ pub fn build(b: *std.Build) !void {
cc_exe_install.step.dependOn(llvm_libs_step);
llvm_exes_step.dependOn(&cc_exe_install.step);

if (!target.result.isDarwin()) {
if (enable_lto) {
const ld_lto_exe = b.addExecutable(.{
.name = "afl-ld-lto",
.target = target,
Expand Down Expand Up @@ -600,6 +598,7 @@ const LLVM_EXE_C_FLAGS = .{

const LLVM_EXE_CPP_FLAGS = .{
"-fno-rtti",
"-Wl,-znodelete",
"-fno-exceptions",
"-Wno-deprecated-declarations",
};
Expand Down

0 comments on commit 3072b37

Please sign in to comment.