authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2024-03-01 16:46:48+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-03-01 17:42:54-08:00
logb60fc16b4f6b973ce2207fb28b77606d45961972
treeeb90eb7d6cfcfa8d90f821b34c4e6d17e7159e1a
parent155f5274ff4db3bc6e75ae5660cabab5bab22f42

compiler: audit debug mode checks

* Introduce `-Ddebug-extensions` for enabling compiler debug helpers * Replace safety mode checks with `std.debug.runtime_safety` * Replace debugger helper checks with `!builtin.strip_debug_info` Sometimes, you just have to debug optimized compilers...

19 files changed, 70 insertions(+), 81 deletions(-)

bootstrap.c+4-4
......@@ -131,15 +131,15 @@ int main(int argc, char **argv) {
131131 "pub const llvm_has_xtensa = false;\n"
132132 "pub const version: [:0]const u8 = \"%s\";\n"
133133 "pub const semver = @import(\"std\").SemanticVersion.parse(version) catch unreachable;\n"
134 "pub const enable_logging: bool = false;\n"
135 "pub const enable_link_snapshots: bool = false;\n"
134 "pub const enable_debug_extensions = false;\n"
135 "pub const enable_logging = false;\n"
136 "pub const enable_link_snapshots = false;\n"
136137 "pub const enable_tracy = false;\n"
137138 "pub const value_tracing = false;\n"
138139 "pub const skip_non_native = false;\n"
139 "pub const only_c = false;\n"
140140 "pub const force_gpa = false;\n"
141 "pub const only_c = false;\n"
141142 "pub const only_core_functionality = true;\n"
142 "pub const only_reduce = false;\n"
143143 , zig_version);
144144 if (written < 100)
145145 panic("unable to write to config.zig file");
build.zig+4
......@@ -250,6 +250,7 @@ pub fn build(b: *std.Build) !void {
250250 }
251251
252252 const is_debug = optimize == .Debug;
253 const enable_debug_extensions = b.option(bool, "debug-extensions", "Enable commands and options useful for debugging the compiler") orelse is_debug;
253254 const enable_logging = b.option(bool, "log", "Enable debug logging with --debug-log") orelse is_debug;
254255 const enable_link_snapshots = b.option(bool, "link-snapshot", "Whether to enable linker state snapshots") orelse false;
255256
......@@ -357,6 +358,7 @@ pub fn build(b: *std.Build) !void {
357358 const semver = try std.SemanticVersion.parse(version);
358359 exe_options.addOption(std.SemanticVersion, "semver", semver);
359360
361 exe_options.addOption(bool, "enable_debug_extensions", enable_debug_extensions);
360362 exe_options.addOption(bool, "enable_logging", enable_logging);
361363 exe_options.addOption(bool, "enable_link_snapshots", enable_link_snapshots);
362364 exe_options.addOption(bool, "enable_tracy", tracy != null);
......@@ -393,6 +395,7 @@ pub fn build(b: *std.Build) !void {
393395 check_case_exe.root_module.addOptions("build_options", test_cases_options);
394396
395397 test_cases_options.addOption(bool, "enable_tracy", false);
398 test_cases_options.addOption(bool, "enable_debug_extensions", enable_debug_extensions);
396399 test_cases_options.addOption(bool, "enable_logging", enable_logging);
397400 test_cases_options.addOption(bool, "enable_link_snapshots", enable_link_snapshots);
398401 test_cases_options.addOption(bool, "skip_non_native", skip_non_native);
......@@ -588,6 +591,7 @@ fn addWasiUpdateStep(b: *std.Build, version: [:0]const u8) !void {
588591 exe_options.addOption(bool, "only_c", true);
589592 exe_options.addOption([:0]const u8, "version", version);
590593 exe_options.addOption(std.SemanticVersion, "semver", semver);
594 exe_options.addOption(bool, "enable_debug_extensions", false);
591595 exe_options.addOption(bool, "enable_logging", false);
592596 exe_options.addOption(bool, "enable_link_snapshots", false);
593597 exe_options.addOption(bool, "enable_tracy", false);
lib/std/hash_map.zig+1-1
......@@ -1601,7 +1601,7 @@ pub fn HashMapUnmanaged(
16011601 }
16021602
16031603 comptime {
1604 if (builtin.mode == .Debug) {
1604 if (!builtin.strip_debug_info) {
16051605 _ = &dbHelper;
16061606 }
16071607 }
lib/std/multi_array_list.zig+1-1
......@@ -574,7 +574,7 @@ pub fn MultiArrayList(comptime T: type) type {
574574 }
575575
576576 comptime {
577 if (builtin.mode == .Debug) {
577 if (!builtin.strip_debug_info) {
578578 _ = &dbHelper;
579579 _ = &Slice.dbHelper;
580580 }
src/Air.zig+2-2
......@@ -1102,10 +1102,10 @@ pub const Inst = struct {
11021102 };
11031103
11041104 // Make sure we don't accidentally add a field to make this union
1105 // bigger than expected. Note that in Debug builds, Zig is allowed
1105 // bigger than expected. Note that in safety builds, Zig is allowed
11061106 // to insert a secret field for safety checks.
11071107 comptime {
1108 if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
1108 if (!std.debug.runtime_safety) {
11091109 assert(@sizeOf(Data) == 8);
11101110 }
11111111 }
src/Compilation.zig+2-2
......@@ -2191,14 +2191,14 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
21912191 try comp.performAllTheWork(main_progress_node);
21922192
21932193 if (comp.module) |module| {
2194 if (builtin.mode == .Debug and comp.verbose_intern_pool) {
2194 if (build_options.enable_debug_extensions and comp.verbose_intern_pool) {
21952195 std.debug.print("intern pool stats for '{s}':\n", .{
21962196 comp.root_name,
21972197 });
21982198 module.intern_pool.dump();
21992199 }
22002200
2201 if (builtin.mode == .Debug and comp.verbose_generic_instances) {
2201 if (build_options.enable_debug_extensions and comp.verbose_generic_instances) {
22022202 std.debug.print("generic instances for '{s}:0x{x}':\n", .{
22032203 comp.root_name,
22042204 @as(usize, @intFromPtr(module)),
src/InternPool.zig+1-1
......@@ -2593,7 +2593,7 @@ pub const Index = enum(u32) {
25932593 }
25942594
25952595 comptime {
2596 if (builtin.mode == .Debug) {
2596 if (!builtin.strip_debug_info) {
25972597 _ = &dbHelper;
25982598 }
25992599 }
src/Module.zig+2-2
......@@ -3203,8 +3203,8 @@ pub fn ensureFuncBodyAnalyzed(zcu: *Zcu, func_index: InternPool.Index) SemaError
32033203
32043204 const comp = zcu.comp;
32053205
3206 const dump_air = builtin.mode == .Debug and comp.verbose_air;
3207 const dump_llvm_ir = builtin.mode == .Debug and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null);
3206 const dump_air = build_options.enable_debug_extensions and comp.verbose_air;
3207 const dump_llvm_ir = build_options.enable_debug_extensions and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null);
32083208
32093209 if (comp.bin_file == null and zcu.llvm_object == null and !dump_air and !dump_llvm_ir) {
32103210 return;
src/Sema.zig+2-2
......@@ -2505,7 +2505,7 @@ fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Module.ErrorMsg)
25052505 ref: {
25062506 errdefer err_msg.destroy(gpa);
25072507
2508 if (crash_report.is_enabled and mod.comp.debug_compile_errors) {
2508 if (build_options.enable_debug_extensions and mod.comp.debug_compile_errors) {
25092509 var wip_errors: std.zig.ErrorBundle.Wip = undefined;
25102510 wip_errors.init(gpa) catch unreachable;
25112511 Compilation.addModuleErrorMsg(mod, &wip_errors, err_msg.*) catch unreachable;
......@@ -5758,7 +5758,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr
57585758 const body = sema.code.bodySlice(extra.end, extra.data.body_len);
57595759
57605760 // we check this here to avoid undefined symbols
5761 if (!@import("build_options").have_llvm)
5761 if (!build_options.have_llvm)
57625762 return sema.fail(parent_block, src, "C import unavailable; Zig compiler built without LLVM extensions", .{});
57635763
57645764 var c_import_buf = std.ArrayList(u8).init(gpa);
src/Value.zig+1-1
......@@ -4069,7 +4069,7 @@ fn dbHelper(self: *Value, tag_to_payload_map: *map: {
40694069}
40704070
40714071comptime {
4072 if (builtin.mode == .Debug) {
4072 if (!builtin.strip_debug_info) {
40734073 _ = &dbHelper;
40744074 }
40754075}
src/arch/aarch64/Mir.zig+2-2
......@@ -484,9 +484,9 @@ pub const Inst = struct {
484484 };
485485
486486 // Make sure we don't accidentally make instructions bigger than expected.
487 // Note that in Debug builds, Zig is allowed to insert a secret field for safety checks.
487 // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
488488 comptime {
489 if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
489 if (!std.debug.runtime_safety) {
490490 assert(@sizeOf(Data) == 8);
491491 }
492492 }
src/arch/arm/Mir.zig+2-2
......@@ -264,9 +264,9 @@ pub const Inst = struct {
264264 };
265265
266266 // Make sure we don't accidentally make instructions bigger than expected.
267 // Note that in Debug builds, Zig is allowed to insert a secret field for safety checks.
267 // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
268268 comptime {
269 if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
269 if (!std.debug.runtime_safety) {
270270 assert(@sizeOf(Data) == 8);
271271 }
272272 }
src/arch/riscv64/Mir.zig+2-2
......@@ -112,9 +112,9 @@ pub const Inst = struct {
112112 };
113113
114114 // Make sure we don't accidentally make instructions bigger than expected.
115 // Note that in Debug builds, Zig is allowed to insert a secret field for safety checks.
115 // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
116116 // comptime {
117 // if (builtin.mode != .Debug) {
117 // if (!std.debug.runtime_safety) {
118118 // assert(@sizeOf(Inst) == 8);
119119 // }
120120 // }
src/arch/sparc64/Mir.zig+2-2
......@@ -356,9 +356,9 @@ pub const Inst = struct {
356356 };
357357
358358 // Make sure we don't accidentally make instructions bigger than expected.
359 // Note that in Debug builds, Zig is allowed to insert a secret field for safety checks.
359 // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
360360 comptime {
361 if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
361 if (!std.debug.runtime_safety) {
362362 assert(@sizeOf(Data) == 8);
363363 }
364364 }
src/arch/wasm/CodeGen.zig+4-4
......@@ -735,7 +735,7 @@ free_locals_v128: std.ArrayListUnmanaged(u32) = .{},
735735/// stored in our `values` map and therefore cause bugs.
736736air_bookkeeping: @TypeOf(bookkeeping_init) = bookkeeping_init,
737737
738const bookkeeping_init = if (builtin.mode == .Debug) @as(usize, 0) else {};
738const bookkeeping_init = if (std.debug.runtime_safety) @as(usize, 0) else {};
739739
740740const InnerError = error{
741741 OutOfMemory,
......@@ -830,7 +830,7 @@ fn finishAir(func: *CodeGen, inst: Air.Inst.Index, result: WValue, operands: []c
830830 branch.values.putAssumeCapacityNoClobber(inst.toRef(), result);
831831 }
832832
833 if (builtin.mode == .Debug) {
833 if (std.debug.runtime_safety) {
834834 func.air_bookkeeping += 1;
835835 }
836836}
......@@ -866,7 +866,7 @@ const BigTomb = struct {
866866 bt.gen.currentBranch().values.putAssumeCapacityNoClobber(bt.inst.toRef(), result);
867867 }
868868
869 if (builtin.mode == .Debug) {
869 if (std.debug.runtime_safety) {
870870 bt.gen.air_bookkeeping += 1;
871871 }
872872 }
......@@ -2079,7 +2079,7 @@ fn genBody(func: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
20792079 try func.currentBranch().values.ensureUnusedCapacity(func.gpa, Liveness.bpi);
20802080 try func.genInst(inst);
20812081
2082 if (builtin.mode == .Debug and func.air_bookkeeping < old_bookkeeping_value + 1) {
2082 if (std.debug.runtime_safety and func.air_bookkeeping < old_bookkeeping_value + 1) {
20832083 std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{}')", .{
20842084 inst,
20852085 func.air.instructions.items(.tag)[@intFromEnum(inst)],
src/arch/x86_64/Mir.zig+2-2
......@@ -1012,9 +1012,9 @@ pub const Inst = struct {
10121012 };
10131013
10141014 // Make sure we don't accidentally make instructions bigger than expected.
1015 // Note that in Debug builds, Zig is allowed to insert a secret field for safety checks.
1015 // Note that in safety builds, Zig is allowed to insert a secret field for safety checks.
10161016 comptime {
1017 if (builtin.mode != .Debug and builtin.mode != .ReleaseSafe) {
1017 if (!std.debug.runtime_safety) {
10181018 assert(@sizeOf(Data) == 8);
10191019 }
10201020 }
src/crash_report.zig+21-35
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const builtin = @import("builtin");
3const build_options = @import("build_options");
34const debug = std.debug;
45const os = std.os;
56const io = std.io;
......@@ -11,37 +12,26 @@ const Sema = @import("Sema.zig");
1112const Zir = std.zig.Zir;
1213const Decl = Module.Decl;
1314
14pub const is_enabled = builtin.mode == .Debug;
15
1615/// To use these crash report diagnostics, publish this panic in your main file
1716/// and add `pub const enable_segfault_handler = false;` to your `std_options`.
1817/// You will also need to call initialize() on startup, preferably as the very first operation in your program.
19pub const panic = if (is_enabled) compilerPanic else std.builtin.default_panic;
18pub const panic = if (build_options.enable_debug_extensions) compilerPanic else std.builtin.default_panic;
2019
2120/// Install signal handlers to identify crashes and report diagnostics.
2221pub fn initialize() void {
23 if (is_enabled and debug.have_segfault_handling_support) {
22 if (build_options.enable_debug_extensions and debug.have_segfault_handling_support) {
2423 attachSegfaultHandler();
2524 }
2625}
2726
28fn En(comptime T: type) type {
29 return if (is_enabled) T else void;
30}
31
32fn en(val: anytype) En(@TypeOf(val)) {
33 return if (is_enabled) val else {};
34}
35
36pub const AnalyzeBody = struct {
37 parent: if (is_enabled) ?*AnalyzeBody else void,
38 sema: En(*Sema),
39 block: En(*Sema.Block),
40 body: En([]const Zir.Inst.Index),
41 body_index: En(usize),
27pub const AnalyzeBody = if (build_options.enable_debug_extensions) struct {
28 parent: ?*AnalyzeBody,
29 sema: *Sema,
30 block: *Sema.Block,
31 body: []const Zir.Inst.Index,
32 body_index: usize,
4233
4334 pub fn push(self: *@This()) void {
44 if (!is_enabled) return;
4535 const head = &zir_state;
4636 debug.assert(self.parent == null);
4737 self.parent = head.*;
......@@ -49,7 +39,6 @@ pub const AnalyzeBody = struct {
4939 }
5040
5141 pub fn pop(self: *@This()) void {
52 if (!is_enabled) return;
5342 const head = &zir_state;
5443 const old = head.*.?;
5544 debug.assert(old == self);
......@@ -57,27 +46,24 @@ pub const AnalyzeBody = struct {
5746 }
5847
5948 pub fn setBodyIndex(self: *@This(), index: usize) void {
60 if (!is_enabled) return;
6149 self.body_index = index;
6250 }
51} else struct {
52 pub inline fn push(_: @This()) void {}
53 pub inline fn pop(_: @This()) void {}
54 pub inline fn setBodyIndex(_: @This(), _: usize) void {}
6355};
6456
65threadlocal var zir_state: ?*AnalyzeBody = if (is_enabled) null else @compileError("Cannot use zir_state if crash_report is disabled.");
57threadlocal var zir_state: ?*AnalyzeBody = if (build_options.enable_debug_extensions) null else @compileError("Cannot use zir_state without debug extensions.");
6658
6759pub fn prepAnalyzeBody(sema: *Sema, block: *Sema.Block, body: []const Zir.Inst.Index) AnalyzeBody {
68 if (is_enabled) {
69 return .{
70 .parent = null,
71 .sema = sema,
72 .block = block,
73 .body = body,
74 .body_index = 0,
75 };
76 } else {
77 if (@sizeOf(AnalyzeBody) != 0)
78 @compileError("AnalyzeBody must have zero size when crash reports are disabled");
79 return undefined;
80 }
60 return if (build_options.enable_debug_extensions) .{
61 .parent = null,
62 .sema = sema,
63 .block = block,
64 .body = body,
65 .body_index = 0,
66 } else .{};
8167}
8268
8369fn dumpStatusReport() !void {
src/main.zig+11-13
......@@ -67,8 +67,6 @@ pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
6767 process.exit(1);
6868}
6969
70const debug_extensions_enabled = builtin.mode == .Debug;
71
7270const normal_usage =
7371 \\Usage: zig [command] [options]
7472 \\
......@@ -120,7 +118,7 @@ const debug_usage = normal_usage ++
120118 \\
121119;
122120
123const usage = if (debug_extensions_enabled) debug_usage else normal_usage;
121const usage = if (build_options.enable_debug_extensions) debug_usage else normal_usage;
124122
125123var log_scopes: std.ArrayListUnmanaged([]const u8) = .{};
126124
......@@ -334,9 +332,9 @@ fn mainArgs(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
334332 return io.getStdOut().writeAll(usage);
335333 } else if (mem.eql(u8, cmd, "ast-check")) {
336334 return cmdAstCheck(gpa, arena, cmd_args);
337 } else if (debug_extensions_enabled and mem.eql(u8, cmd, "changelist")) {
335 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "changelist")) {
338336 return cmdChangelist(gpa, arena, cmd_args);
339 } else if (debug_extensions_enabled and mem.eql(u8, cmd, "dump-zir")) {
337 } else if (build_options.enable_debug_extensions and mem.eql(u8, cmd, "dump-zir")) {
340338 return cmdDumpZir(gpa, arena, cmd_args);
341339 } else {
342340 std.log.info("{s}", .{usage});
......@@ -1591,10 +1589,10 @@ fn buildOutputType(
15911589 });
15921590 };
15931591 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
1594 if (!crash_report.is_enabled) {
1595 warn("Zig was compiled in a release mode. --debug-compile-errors has no effect.", .{});
1596 } else {
1592 if (build_options.enable_debug_extensions) {
15971593 debug_compile_errors = true;
1594 } else {
1595 warn("Zig was compiled without debug extensions. --debug-compile-errors has no effect.", .{});
15981596 }
15991597 } else if (mem.eql(u8, arg, "--verbose-link")) {
16001598 verbose_link = true;
......@@ -5076,10 +5074,10 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void {
50765074 }
50775075 continue;
50785076 } else if (mem.eql(u8, arg, "--debug-compile-errors")) {
5079 if (!crash_report.is_enabled) {
5080 warn("Zig was compiled in a release mode. --debug-compile-errors has no effect.", .{});
5081 } else {
5077 if (build_options.enable_debug_extensions) {
50825078 debug_compile_errors = true;
5079 } else {
5080 warn("Zig was compiled without debug extensions. --debug-compile-errors has no effect.", .{});
50835081 }
50845082 } else if (mem.eql(u8, arg, "--verbose-link")) {
50855083 verbose_link = true;
......@@ -6317,8 +6315,8 @@ fn cmdAstCheck(
63176315 if (!want_output_text) {
63186316 return cleanExit();
63196317 }
6320 if (!debug_extensions_enabled) {
6321 fatal("-t option only available in debug builds of zig", .{});
6318 if (!build_options.enable_debug_extensions) {
6319 fatal("-t option only available in builds of zig with debug extensions", .{});
63226320 }
63236321
63246322 {
stage1/config.zig.in+4-3
......@@ -5,11 +5,12 @@ pub const llvm_has_arc = false;
55pub const llvm_has_xtensa = false;
66pub const version: [:0]const u8 = "@RESOLVED_ZIG_VERSION@";
77pub const semver = @import("std").SemanticVersion.parse(version) catch unreachable;
8pub const enable_logging: bool = false;
9pub const enable_link_snapshots: bool = false;
8pub const enable_debug_extensions = false;
9pub const enable_logging = false;
10pub const enable_link_snapshots = false;
1011pub const enable_tracy = false;
1112pub const value_tracing = false;
1213pub const skip_non_native = false;
13pub const only_c = false;
1414pub const force_gpa = false;
15pub const only_c = false;
1516pub const only_core_functionality = true;