authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 02:30:34+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-10-10 02:30:34+02:00
log2556cae4543b7f67d6547257f6684c8ea0e12ab7
treef6c2740a1ce87f8375e516f82e45623da1ecd97e
parent3f3e88e7b455bb458839d0d59eb4c50699aeadbf
parentaa8c774e0f15617137f56eb15c667e259b3be75a

Merge pull request 'default to LLVM and ReleaseSafe for fuzzing' (#36108) from gooncreeper/zig:fuzzing-default-llvm into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36108 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

8 files changed, 28 insertions(+), 16 deletions(-)

build.zig+1-1
......@@ -883,7 +883,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
883883}
884884
885885const AddCompilerModOptions = struct {
886 optimize: std.lang.Optimize,
886 optimize: ?std.lang.Optimize,
887887 target: std.Build.ResolvedTarget,
888888 strip: ?bool = null,
889889 valgrind: ?bool = null,
lib/init/src/main.zig+1-1
......@@ -45,7 +45,7 @@ test "fuzz example" {
4545
4646fn testOne(context: void, smith: *std.testing.Smith) !void {
4747 _ = context;
48 // Try command `zig build test --fuzz -Doptimize=ReleaseFast` to see if it manages to fail this test case!
48 // Try running `zig build test --fuzz` and see if it manages to fail this test case!
4949
5050 const gpa = std.testing.allocator;
5151 var list: std.ArrayList(u8) = .empty;
lib/std/Build.zig+3-3
......@@ -1190,14 +1190,14 @@ pub const StandardOptimizeOptionOptions = struct {
11901190 preferred_optimize_mode: ?std.builtin.Optimize = null,
11911191};
11921192
1193pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions) std.builtin.Optimize {
1193pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions) ?std.builtin.Optimize {
11941194 const graph = b.graph;
11951195
11961196 if (options.preferred_optimize_mode) |mode| {
11971197 if (b.option(bool, "release", "optimize for end users") orelse (graph.release_mode != .off)) {
11981198 return mode;
11991199 } else {
1200 return .debug;
1200 return null;
12011201 }
12021202 }
12031203
......@@ -1210,7 +1210,7 @@ pub fn standardOptimizeOption(b: *Build, options: StandardOptimizeOptionOptions)
12101210 }
12111211
12121212 return switch (graph.release_mode) {
1213 .off => .debug,
1213 .off => null,
12141214 .any => {
12151215 std.debug.print("the project does not declare a preferred optimization mode. choose: --release=fast, --release=safe, or --release=small\n", .{});
12161216 process.exit(1);
src/Compilation/Config.zig+9-1
......@@ -147,6 +147,7 @@ pub const ResolveError = error{
147147 TargetRequiresPie,
148148 SanitizeThreadRequiresPie,
149149 SanitizeThreadRequiresLlvmBackend,
150 FuzzRequiresLlvmBackend,
150151 BackendLacksErrorTracing,
151152 LlvmLibraryUnavailable,
152153 LldUnavailable,
......@@ -196,7 +197,8 @@ pub fn resolve(options: Options) ResolveError!Config {
196197 break :b options.use_lib_llvm orelse true;
197198 };
198199
199 const root_optimize_mode = options.root_optimize_mode orelse .debug;
200 const root_optimize_mode = options.root_optimize_mode orelse
201 @as(std.lang.OptimizeMode, if (options.any_fuzz) .safe else .debug);
200202
201203 // Make a decision on whether to use Clang or Aro for translate-c and compiling C files.
202204 const c_frontend: CFrontend = b: {
......@@ -349,6 +351,12 @@ pub fn resolve(options: Options) ResolveError!Config {
349351 break :b true;
350352 }
351353
354 if (options.any_fuzz) {
355 // Fuzzing instrumentation requires the LLVM backend.
356 if (options.use_llvm == false) return error.FuzzRequiresLlvmBackend;
357 break :b true;
358 }
359
352360 if (options.use_llvm) |x| break :b x;
353361
354362 // If we cannot use LLVM libraries, then our own backends will be a
src/main.zig+1
......@@ -4328,6 +4328,7 @@ fn createModule(
43284328 error.TargetRequiresPie => fatal("the specified target requires position independent executables", .{}),
43294329 error.SanitizeThreadRequiresPie => fatal("thread sanitization requires position independent executables", .{}),
43304330 error.SanitizeThreadRequiresLlvmBackend => fatal("thread sanitization requires the LLVM backend", .{}),
4331 error.FuzzRequiresLlvmBackend => fatal("fuzzing requires the LLVM backend", .{}),
43314332 error.BackendLacksErrorTracing => fatal("the selected backend has not yet implemented error return tracing", .{}),
43324333 error.LlvmLibraryUnavailable => fatal("zig was compiled without LLVM libraries", .{}),
43334334 error.LldUnavailable => fatal("zig was compiled without LLD libraries", .{}),
test/standalone/dependency_options/build.zig+3-3
......@@ -10,14 +10,14 @@ pub fn build(b: *std.Build) !void {
1010
1111 const none_specified_mod = none_specified.module("dummy");
1212 if (!none_specified_mod.resolved_target.?.query.eql(b.graph.host.query)) return error.TestFailed;
13 const expected_optimize: std.builtin.Optimize = switch (b.graph.release_mode) {
14 .off => .debug,
13 const expected_optimize: ?std.builtin.OptimizeMode = switch (b.graph.release_mode) {
14 .off => null,
1515 .any => unreachable,
1616 .fast => .fast,
1717 .safe => .safe,
1818 .small => .small,
1919 };
20 if (none_specified_mod.optimize.? != expected_optimize) return error.TestFailed;
20 if (none_specified_mod.optimize != expected_optimize) return error.TestFailed;
2121
2222 // Passing null is the same as not specifying the option,
2323 // so this should resolve to the same cached dependency instance.
test/standalone/posix/build.zig+1-1
......@@ -55,7 +55,7 @@ pub fn build(b: *std.Build) void {
5555 }
5656}
5757
58fn run_exe(b: *std.Build, optimize: std.builtin.Optimize, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
58fn run_exe(b: *std.Build, optimize: ?std.builtin.Optimize, case: *const Case, target: std.Build.ResolvedTarget, link_libc: bool) *std.Build.Step.Run {
5959 const exe_name = b.fmt("test-posix-{s}{s}{s}", .{
6060 std.fs.path.stem(case.src_path),
6161 if (link_libc) "-libc" else "",
test/tests.zig+9-6
......@@ -20,7 +20,7 @@ pub const LinkContext = @import("src/Link.zig");
2020const ModuleTestTarget = struct {
2121 linkage: ?std.builtin.LinkMode = null,
2222 target: std.Target.Query = .{},
23 optimize_mode: std.builtin.Optimize = .debug,
23 optimize_mode: ?std.builtin.Optimize = .debug,
2424 link_libc: ?bool = null,
2525 single_threaded: ?bool = null,
2626 use_llvm: ?bool = null,
......@@ -2795,7 +2795,7 @@ pub const ModuleTestOptions = struct {
27952795
27962796 pub const TestOnly = union(enum) {
27972797 default: void,
2798 fuzz: OptimizeMode,
2798 fuzz: ?OptimizeMode,
27992799 };
28002800};
28012801
......@@ -2896,6 +2896,9 @@ fn addOneModuleTest(
28962896 options: ModuleTestOptions,
28972897) void {
28982898 const target = &resolved_target.result;
2899 const optimize_suffix = if (test_target.optimize_mode) |mode| switch (mode) {
2900 inline else => |t| "-" ++ @tagName(t),
2901 } else "";
28992902 const libc_suffix = if (test_target.link_libc == true) "-libc" else "";
29002903 const model_txt = target.cpu.model.name;
29012904
......@@ -2948,11 +2951,11 @@ fn addOneModuleTest(
29482951
29492952 for (options.include_paths) |include_path| these_tests.root_module.addIncludePath(b.path(include_path));
29502953
2951 const qualified_name = b.fmt("{s}-{s}-{s}-{t}{s}{s}{s}{s}{s}{s}", .{
2954 const qualified_name = b.fmt("{s}-{s}-{s}{s}{s}{s}{s}{s}{s}{s}", .{
29522955 options.name,
29532956 triple_txt,
29542957 model_txt,
2955 test_target.optimize_mode,
2958 optimize_suffix,
29562959 libc_suffix,
29572960 single_threaded_suffix,
29582961 backend_suffix,
......@@ -3087,11 +3090,11 @@ fn addOneModuleTest(
30873090 }
30883091}
30893092
3090pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: OptimizeMode) bool {
3093pub fn wouldUseLlvm(use_llvm: ?bool, query: std.Target.Query, optimize_mode: ?OptimizeMode) bool {
30913094 if (comptime builtin.cpu.arch.endian() == .big) return true; // https://github.com/ziglang/zig/issues/25961
30923095 if (use_llvm) |x| return x;
30933096 if (query.ofmt == .c) return false;
3094 switch (optimize_mode) {
3097 switch (optimize_mode orelse .debug) {
30953098 .debug => {},
30963099 else => return true,
30973100 }