From 3944c72a148096e3933745a9fda0397c0dd67cbf Mon Sep 17 00:00:00 2001 From: Kendall Condon Date: Mon, 29 Jun 2026 21:11:46 -0400 Subject: [PATCH] default to LLVM and ReleaseSafe for fuzzing Currently, only the LLVM backend is capable of emitting fuzzing instrumentation, so `-ffuzz -fno-llvm` now results in an error. --- lib/init/src/main.zig | 2 +- src/Compilation/Config.zig | 10 +++++++++- src/main.zig | 1 + 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/init/src/main.zig b/lib/init/src/main.zig index d77848ab63f0afb0d963ace4577ac1ee701ed548..f4bc7d488da391a64071c17851dbc7bd43b7436d 100644 --- a/lib/init/src/main.zig +++ b/lib/init/src/main.zig @@ -45,7 +45,7 @@ test "fuzz example" { fn testOne(context: void, smith: *std.testing.Smith) !void { _ = context; - // Try command `zig build test --fuzz -Doptimize=ReleaseFast` to see if it manages to fail this test case! + // Try running `zig build test --fuzz` and see if it manages to fail this test case! const gpa = std.testing.allocator; var list: std.ArrayList(u8) = .empty; diff --git a/src/Compilation/Config.zig b/src/Compilation/Config.zig index 3691b1eb423b72130b0fd8e962137a08a1da1d61..fdfd8cfe741e1069aa886213940e8d50a88c7677 100644 --- a/src/Compilation/Config.zig +++ b/src/Compilation/Config.zig @@ -147,6 +147,7 @@ pub const ResolveError = error{ TargetRequiresPie, SanitizeThreadRequiresPie, SanitizeThreadRequiresLlvmBackend, + FuzzRequiresLlvmBackend, BackendLacksErrorTracing, LlvmLibraryUnavailable, LldUnavailable, @@ -196,7 +197,8 @@ pub fn resolve(options: Options) ResolveError!Config { break :b options.use_lib_llvm orelse true; }; - const root_optimize_mode = options.root_optimize_mode orelse .debug; + const root_optimize_mode = options.root_optimize_mode orelse + @as(std.lang.OptimizeMode, if (options.any_fuzz) .safe else .debug); // Make a decision on whether to use Clang or Aro for translate-c and compiling C files. const c_frontend: CFrontend = b: { @@ -349,6 +351,12 @@ pub fn resolve(options: Options) ResolveError!Config { break :b true; } + if (options.any_fuzz) { + // Fuzzing instrumentation requires the LLVM backend. + if (options.use_llvm == false) return error.FuzzRequiresLlvmBackend; + break :b true; + } + if (options.use_llvm) |x| break :b x; // If we cannot use LLVM libraries, then our own backends will be a diff --git a/src/main.zig b/src/main.zig index 68b38d043071c3ca48e2bd3aab68648165c13530..607cd7b643a5a94d06d710dc25feea16c55568c7 100644 --- a/src/main.zig +++ b/src/main.zig @@ -4328,6 +4328,7 @@ fn createModule( error.TargetRequiresPie => fatal("the specified target requires position independent executables", .{}), error.SanitizeThreadRequiresPie => fatal("thread sanitization requires position independent executables", .{}), error.SanitizeThreadRequiresLlvmBackend => fatal("thread sanitization requires the LLVM backend", .{}), + error.FuzzRequiresLlvmBackend => fatal("fuzzing requires the LLVM backend", .{}), error.BackendLacksErrorTracing => fatal("the selected backend has not yet implemented error return tracing", .{}), error.LlvmLibraryUnavailable => fatal("zig was compiled without LLVM libraries", .{}), error.LldUnavailable => fatal("zig was compiled without LLD libraries", .{}), -- 2.54.0