1
1
const std = @import ("std" );
2
2
3
- pub fn build (b : * std.Build ) void {
3
+ pub fn build (b : * std.Build ) ! void {
4
4
const target = b .standardTargetOptions (.{});
5
5
const optimize = b .standardOptimizeOption (.{});
6
6
const lib_path_flag = b .fmt ("-DAFL_PATH=\" {s}\" " , .{b .lib_dir });
@@ -12,6 +12,7 @@ pub fn build(b: *std.Build) void {
12
12
const use_deflate = b .option (bool , "use_deflate" , "Use system libdeflate" ) orelse true ;
13
13
14
14
const build_nyx = b .option (bool , "build_nyx" , "Build Nyx mode on Linux" ) orelse true ;
15
+ const enable_wafl = b .option (bool , "enable_wafl" , "Enable WAFL mode on WASI" ) orelse true ;
15
16
const build_coresight = b .option (bool , "build_coresight" , "Build CoreSight mode on ARM64 Linux" ) orelse true ;
16
17
const build_unicorn_aarch64 = b .option (bool , "build_unicorn_aarch64" , "Build Unicorn mode on ARM64" ) orelse true ;
17
18
@@ -236,13 +237,115 @@ pub fn build(b: *std.Build) void {
236
237
237
238
b .default_step .dependOn (exes_step );
238
239
239
- // LLVM instrumentation flags
240
- var llvm_flags = std .BoundedArray ([]const u8 , 16 ){};
241
- llvm_flags .appendSliceAssumeCapacity (& EXE_LLVM_FLAGS );
242
- llvm_flags .appendSliceAssumeCapacity (&.{ lib_path_flag , bin_path_flag });
240
+ // LLVM instrumentation C flags
241
+ var llvm_c_flags = std .BoundedArray ([]const u8 , 32 ){};
242
+ llvm_c_flags .appendSliceAssumeCapacity (& EXE_LLVM_C_FLAGS );
243
+ const llvm_version = std .mem .trimRight (u8 , b .run (&.{ "llvm-config" , "--version" }), "\n " );
244
+ var llvm_version_iter = std .mem .tokenizeScalar (u8 , llvm_version , '.' );
245
+ const llvm_major = try std .fmt .parseUnsigned (u8 , llvm_version_iter .next ().? , 10 );
246
+ const llvm_minor = try std .fmt .parseUnsigned (u8 , llvm_version_iter .next ().? , 10 );
247
+ const llvm_bin_dir = std .mem .trimRight (u8 , b .run (&.{ "llvm-config" , "--bindir" }), "\n " );
248
+ const llvm_lib_dir = std .mem .trimRight (u8 , b .run (&.{ "llvm-config" , "--libdir" }), "\n " );
249
+ llvm_c_flags .appendSliceAssumeCapacity (&.{
250
+ lib_path_flag ,
251
+ bin_path_flag ,
252
+ b .fmt ("-DLLVM_MAJOR={}" , .{llvm_major }),
253
+ b .fmt ("-DLLVM_MINOR={}" , .{llvm_minor }),
254
+ b .fmt ("-DLLVM_VER=\" {s}\" " , .{llvm_version }),
255
+ b .fmt ("-DLLVM_BINDIR=\" {s}\" " , .{llvm_bin_dir }),
256
+ b .fmt ("-DLLVM_LIBDIR=\" {s}\" " , .{llvm_lib_dir }),
257
+ b .fmt ("-DCLANG_BIN=\" {s}/clang\" " , .{llvm_bin_dir }),
258
+ b .fmt ("-DCLANGPP_BIN=\" {s}/clang++\" " , .{llvm_bin_dir }),
259
+ });
260
+ if (target .query .isNative ()) {
261
+ flags .appendAssumeCapacity ("-march=native" );
262
+ }
263
+
264
+ // LLVM instrumentation C++ flags
265
+ var llvm_cpp_flags = std .BoundedArray ([]const u8 , 32 ){};
266
+ llvm_cpp_flags .appendSliceAssumeCapacity (llvm_c_flags .constSlice ());
267
+ llvm_cpp_flags .appendSliceAssumeCapacity (& EXE_LLVM_CPP_FLAGS );
268
+ llvm_cpp_flags .appendSliceAssumeCapacity (&.{
269
+ b .fmt ("-std={s}" , .{if (llvm_major < 10 ) "gnu++11" else if (llvm_major < 16 ) "c++14" else "c++17" }),
270
+ });
271
+ if (enable_wafl and target .result .os .tag == .wasi ) {
272
+ llvm_cpp_flags .appendSliceAssumeCapacity (&.{ "-DNDEBUG" , "-DNO_TLS" });
273
+ }
274
+
275
+ // LLVM instrumentation objects
276
+ const llvm_inc_dir = std .mem .trimRight (u8 , b .run (&.{ "llvm-config" , "--includedir" }), "\n " );
277
+ const llvm_inc_path = std.Build.LazyPath { .cwd_relative = llvm_inc_dir };
278
+
279
+ const llvm_common_obj = b .addObject (.{
280
+ .name = "afl-llvm-common" ,
281
+ .pic = true ,
282
+ .target = target ,
283
+ .optimize = optimize ,
284
+ });
285
+ llvm_common_obj .addCSourceFile (.{
286
+ .file = AFLplusplus_ins_path .path (b , "afl-llvm-common.cc" ),
287
+ .flags = llvm_cpp_flags .constSlice (),
288
+ });
289
+ llvm_common_obj .addIncludePath (AFLplusplus_inc_path );
290
+ llvm_common_obj .addIncludePath (llvm_inc_path );
291
+ llvm_common_obj .linkLibCpp ();
292
+
293
+ const compiler_rt_obj = b .addObject (.{
294
+ .name = "afl-compiler-rt" ,
295
+ .pic = true ,
296
+ .target = target ,
297
+ .optimize = optimize ,
298
+ });
299
+ compiler_rt_obj .addCSourceFile (.{
300
+ .file = AFLplusplus_ins_path .path (b , "afl-compiler-rt.o.c" ),
301
+ .flags = llvm_c_flags .constSlice (),
302
+ });
303
+ compiler_rt_obj .addIncludePath (AFLplusplus_inc_path );
304
+ compiler_rt_obj .linkLibC ();
305
+
306
+ // Library LLVM instrumentation suite
307
+ const lib_llvm_step = b .step ("lib_llvm" , "Install library LLVM instrumentation suite" );
308
+
309
+ const llvm_pass_lib = b .addSharedLibrary (.{
310
+ .name = "afl-llvm-pass" ,
311
+ .pic = true ,
312
+ .target = target ,
313
+ .version = version ,
314
+ .optimize = optimize ,
315
+ });
316
+ llvm_pass_lib .addCSourceFile (.{
317
+ .file = AFLplusplus_ins_path .path (b , "afl-llvm-pass.so.cc" ),
318
+ .flags = llvm_cpp_flags .constSlice (),
319
+ });
320
+ llvm_pass_lib .addIncludePath (AFLplusplus_inc_path );
321
+ llvm_pass_lib .addIncludePath (llvm_inc_path );
322
+ llvm_pass_lib .addObject (llvm_common_obj );
323
+ llvm_pass_lib .linkLibCpp ();
324
+
325
+ const llvm_pass_lib_install = b .addInstallArtifact (llvm_pass_lib , .{});
326
+ lib_llvm_step .dependOn (& llvm_pass_lib_install .step );
327
+
328
+ const llvm_lto_lib = b .addSharedLibrary (.{
329
+ .name = "afl-llvm-lto-instrumentlist" ,
330
+ .pic = true ,
331
+ .target = target ,
332
+ .version = version ,
333
+ .optimize = optimize ,
334
+ });
335
+ llvm_lto_lib .addCSourceFile (.{
336
+ .file = AFLplusplus_ins_path .path (b , "afl-llvm-lto-instrumentlist.so.cc" ),
337
+ .flags = llvm_cpp_flags .constSlice (),
338
+ });
339
+ llvm_lto_lib .addIncludePath (AFLplusplus_inc_path );
340
+ llvm_lto_lib .addIncludePath (llvm_inc_path );
341
+ llvm_lto_lib .addObject (llvm_common_obj );
342
+ llvm_lto_lib .linkLibCpp ();
343
+
344
+ const llvm_lto_lib_install = b .addInstallArtifact (llvm_lto_lib , .{});
345
+ lib_llvm_step .dependOn (& llvm_lto_lib_install .step );
243
346
244
347
// Executable LLVM instrumentation suite
245
- const exe_llvm_step = b .step ("exe_llvm" , "Install executable instrumentation suite" );
348
+ const exe_llvm_step = b .step ("exe_llvm" , "Install executable LLVM instrumentation suite" );
246
349
247
350
const cc_exe = b .addExecutable (.{
248
351
.name = "afl-cc" ,
@@ -252,7 +355,7 @@ pub fn build(b: *std.Build) void {
252
355
});
253
356
cc_exe .addCSourceFile (.{
254
357
.file = AFLplusplus_src_path .path (b , "afl-cc.c" ),
255
- .flags = llvm_flags .constSlice (),
358
+ .flags = llvm_c_flags .constSlice (),
256
359
});
257
360
cc_exe .addIncludePath (AFLplusplus_inc_path );
258
361
cc_exe .addIncludePath (AFLplusplus_ins_path );
@@ -262,8 +365,23 @@ pub fn build(b: *std.Build) void {
262
365
const cc_exe_install = b .addInstallArtifact (cc_exe , .{});
263
366
exe_llvm_step .dependOn (& cc_exe_install .step );
264
367
265
- // TODO: finish implementing LLVM instrumentation
266
- // b.default_step.dependOn(exe_llvm_step);
368
+ const ld_lto_exe = b .addExecutable (.{
369
+ .name = "afl-ld-lto" ,
370
+ .target = target ,
371
+ .version = version ,
372
+ .optimize = optimize ,
373
+ });
374
+ ld_lto_exe .addCSourceFile (.{
375
+ .file = AFLplusplus_src_path .path (b , "afl-ld-lto.c" ),
376
+ .flags = llvm_c_flags .constSlice (),
377
+ });
378
+ ld_lto_exe .addIncludePath (AFLplusplus_inc_path );
379
+ ld_lto_exe .linkLibC ();
380
+
381
+ const ld_lto_exe_install = b .addInstallArtifact (ld_lto_exe , .{});
382
+ exe_llvm_step .dependOn (& ld_lto_exe_install .step );
383
+
384
+ b .default_step .dependOn (exe_llvm_step );
267
385
268
386
// Executable utility suite
269
387
const exe_utils_step = b .step ("exe_utils" , "Install executable utility suite" );
@@ -398,7 +516,7 @@ pub fn build(b: *std.Build) void {
398
516
// TODO: Nyx mode (nyx_mode/build_nyx_support.sh)
399
517
}
400
518
401
- if (! target .result .cpu .arch .isAARCH64 () or build_unicorn_aarch64 ) {
519
+ if (build_unicorn_aarch64 or ! target .result .cpu .arch .isAARCH64 ()) {
402
520
// TODO: Unicorn mode (unicorn_mode/build_unicorn_support.sh)
403
521
}
404
522
@@ -438,41 +556,37 @@ const EXE_FUZZ_SOURCES = .{
438
556
const EXE_FLAGS = .{
439
557
"-O2" ,
440
558
"-g" ,
441
- "-Wno-pointer-sign" ,
442
- "-Wno-variadic-macros" ,
443
559
"-Wall" ,
444
560
"-Wextra" ,
561
+ "-Wno-pointer-sign" ,
445
562
"-Wno-pointer-arith" ,
446
- "-D_AFL_SPECIAL_PERFORMANCE " ,
563
+ "-Wno-variadic-macros " ,
447
564
"-DDOC_PATH=\"\" " ,
565
+ "-D_AFL_SPECIAL_PERFORMANCE" ,
448
566
};
449
567
450
- const EXE_LLVM_FLAGS = .{
568
+ const EXE_LLVM_C_FLAGS = .{
451
569
"-O3" ,
570
+ "-g" ,
452
571
"-funroll-loops" ,
453
572
"-Wall" ,
454
- "-g" ,
455
573
"-Wno-cast-qual" ,
456
- "-Wno-variadic-macros " ,
574
+ "-Wno-deprecated " ,
457
575
"-Wno-pointer-sign" ,
576
+ "-Wno-unused-result" ,
458
577
"-Wno-unused-function" ,
578
+ "-Wno-variadic-macros" ,
459
579
"-Wno-deprecated-copy-with-dtor" ,
460
- "-DAFL_CLANG_FLTO=\" -flto=full\" " ,
461
580
"-DUSE_BINDIR=1" ,
462
- // TODO: properly set these by using system `llvm-config`
463
- // "-DLLVM_BINDIR=\"$(LLVM_BINDIR)\"",
464
- // "-DVERSION=\"$(VERSION)\"",
465
- // "-DLLVM_LIBDIR=\"$(LLVM_LIBDIR)\"",
466
- // "-DLLVM_VERSION=\"$(LLVMVER)\"",
467
- // "-DAFL_REAL_LD=\"$(AFL_REAL_LD)\"",
468
- // "-DAFL_CLANG_LDPATH=\"$(AFL_CLANG_LDPATH)\"",
469
- // "-DAFL_CLANG_FUSELD=\"$(AFL_CLANG_FUSELD)\"",
470
- // "-DCLANG_BIN=\"$(CLANG_BIN)\"",
471
- // "-DCLANGPP_BIN=\"$(CLANGPP_BIN)\"",
472
- // "$(AFL_CLANG_DEBUG_PREFIX)",
473
- // "-DLLVM_MINOR=$(LLVM_MINOR)",
474
- // "-DLLVM_MAJOR=$(LLVM_MAJOR)",
475
- // "-DCFLAGS_OPT=\"$(CFLAGS_OPT)",
581
+ "-DAFL_CLANG_LDPATH=1" ,
582
+ "-DAFL_REAL_LD=\" lld\" " ,
583
+ "-DAFL_CLANG_FLTO=\" -flto=full\" " ,
584
+ };
585
+
586
+ const EXE_LLVM_CPP_FLAGS = .{
587
+ "-fno-rtti" ,
588
+ "-fno-exceptions" ,
589
+ "-Wno-deprecated-declarations" ,
476
590
};
477
591
478
592
const LIB_FLAGS = .{
0 commit comments