authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-01-24 05:11:26+00:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-24 05:11:26+00:00
logf77e1b86225cd49c2d04dfa6ca4a7ede315dc0b1
treec30cddc29ae1f1162f3ec38fcaf89b94ecb4e6dd
parentd916954bee0f477bcada0693d4aa952197cf1eef
parent180db2bf23f05a02876d4567cac3b04842c11acb
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22578 from mlugg/stack-trace-tests-x86_64

tests: enable stack trace tests for x86_64-selfhosted

13 files changed, 200 insertions(+), 125 deletions(-)

lib/std/debug.zig+10-2
......@@ -732,11 +732,12 @@ pub const StackIterator = struct {
732732 // via DWARF before attempting to use the compact unwind info will produce incorrect results.
733733 if (module.unwind_info) |unwind_info| {
734734 if (SelfInfo.unwindFrameMachO(
735 unwind_state.debug_info.allocator,
736 module.base_address,
735737 &unwind_state.dwarf_context,
736738 &it.ma,
737739 unwind_info,
738740 module.eh_frame,
739 module.base_address,
740741 )) |return_address| {
741742 return return_address;
742743 } else |err| {
......@@ -748,7 +749,14 @@ pub const StackIterator = struct {
748749 }
749750
750751 if (try module.getDwarfInfoForAddress(unwind_state.debug_info.allocator, unwind_state.dwarf_context.pc)) |di| {
751 return SelfInfo.unwindFrameDwarf(di, &unwind_state.dwarf_context, &it.ma, null);
752 return SelfInfo.unwindFrameDwarf(
753 unwind_state.debug_info.allocator,
754 di,
755 module.base_address,
756 &unwind_state.dwarf_context,
757 &it.ma,
758 null,
759 );
752760 } else return error.MissingDebugInfo;
753761 }
754762
lib/std/debug/Dwarf.zig+14-4
......@@ -48,6 +48,8 @@ compile_unit_list: std.ArrayListUnmanaged(CompileUnit) = .empty,
4848/// Filled later by the initializer
4949func_list: std.ArrayListUnmanaged(Func) = .empty,
5050
51/// Starts out non-`null` if the `.eh_frame_hdr` section is present. May become `null` later if we
52/// find that `.eh_frame_hdr` is incomplete.
5153eh_frame_hdr: ?ExceptionFrameHeader = null,
5254/// These lookup tables are only used if `eh_frame_hdr` is null
5355cie_map: std.AutoArrayHashMapUnmanaged(u64, CommonInformationEntry) = .empty,
......@@ -1754,10 +1756,12 @@ fn readDebugAddr(di: Dwarf, compile_unit: CompileUnit, index: u64) !u64 {
17541756 };
17551757}
17561758
1757/// If .eh_frame_hdr is present, then only the header needs to be parsed.
1759/// If `.eh_frame_hdr` is present, then only the header needs to be parsed. Otherwise, `.eh_frame`
1760/// and `.debug_frame` are scanned and a sorted list of FDEs is built for binary searching during
1761/// unwinding. Even if `.eh_frame_hdr` is used, we may find during unwinding that it's incomplete,
1762/// in which case we build the sorted list of FDEs at that point.
17581763///
1759/// Otherwise, .eh_frame and .debug_frame are scanned and a sorted list
1760/// of FDEs is built for binary searching during unwinding.
1764/// See also `scanCieFdeInfo`.
17611765pub fn scanAllUnwindInfo(di: *Dwarf, allocator: Allocator, base_address: usize) !void {
17621766 if (di.section(.eh_frame_hdr)) |eh_frame_hdr| blk: {
17631767 var fbr: FixedBufferReader = .{ .buf = eh_frame_hdr, .endian = native_endian };
......@@ -1797,6 +1801,12 @@ pub fn scanAllUnwindInfo(di: *Dwarf, allocator: Allocator, base_address: usize)
17971801 return;
17981802 }
17991803
1804 try di.scanCieFdeInfo(allocator, base_address);
1805}
1806
1807/// Scan `.eh_frame` and `.debug_frame` and build a sorted list of FDEs for binary searching during
1808/// unwinding.
1809pub fn scanCieFdeInfo(di: *Dwarf, allocator: Allocator, base_address: usize) !void {
18001810 const frame_sections = [2]Section.Id{ .eh_frame, .debug_frame };
18011811 for (frame_sections) |frame_section| {
18021812 if (di.section(frame_section)) |section_data| {
......@@ -2125,7 +2135,7 @@ pub const ElfModule = struct {
21252135 return self.dwarf.getSymbol(allocator, relocated_address);
21262136 }
21272137
2128 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*const Dwarf {
2138 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf {
21292139 _ = allocator;
21302140 _ = address;
21312141 return &self.dwarf;
lib/std/debug/SelfInfo.zig+64-33
......@@ -707,7 +707,7 @@ pub const Module = switch (native_os) {
707707 }
708708 }
709709
710 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*const Dwarf {
710 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf {
711711 return if ((try self.getOFileInfoForAddress(allocator, address)).o_file_info) |o_file_info| &o_file_info.di else null;
712712 }
713713 },
......@@ -784,7 +784,7 @@ pub const Module = switch (native_os) {
784784 return .{};
785785 }
786786
787 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*const Dwarf {
787 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf {
788788 _ = allocator;
789789 _ = address;
790790
......@@ -808,7 +808,7 @@ pub const Module = switch (native_os) {
808808 return .{};
809809 }
810810
811 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*const Dwarf {
811 pub fn getDwarfInfoForAddress(self: *@This(), allocator: Allocator, address: usize) !?*Dwarf {
812812 _ = self;
813813 _ = allocator;
814814 _ = address;
......@@ -1156,11 +1156,12 @@ test machoSearchSymbols {
11561156/// If the compact encoding can't encode a way to unwind a frame, it will
11571157/// defer unwinding to DWARF, in which case `.eh_frame` will be used if available.
11581158pub fn unwindFrameMachO(
1159 allocator: Allocator,
1160 base_address: usize,
11591161 context: *UnwindContext,
11601162 ma: *std.debug.MemoryAccessor,
11611163 unwind_info: []const u8,
11621164 eh_frame: ?[]const u8,
1163 module_base_address: usize,
11641165) !usize {
11651166 const header = std.mem.bytesAsValue(
11661167 macho.unwind_info_section_header,
......@@ -1172,7 +1173,7 @@ pub fn unwindFrameMachO(
11721173 );
11731174 if (indices.len == 0) return error.MissingUnwindInfo;
11741175
1175 const mapped_pc = context.pc - module_base_address;
1176 const mapped_pc = context.pc - base_address;
11761177 const second_level_index = blk: {
11771178 var left: usize = 0;
11781179 var len: usize = indices.len;
......@@ -1351,7 +1352,7 @@ pub fn unwindFrameMachO(
13511352 else stack_size: {
13521353 // In .STACK_IND, the stack size is inferred from the subq instruction at the beginning of the function.
13531354 const sub_offset_addr =
1354 module_base_address +
1355 base_address +
13551356 entry.function_offset +
13561357 encoding.value.x86_64.frameless.stack.indirect.sub_offset;
13571358 if (ma.load(usize, sub_offset_addr) == null) return error.InvalidUnwindInfo;
......@@ -1416,7 +1417,7 @@ pub fn unwindFrameMachO(
14161417 break :blk new_ip;
14171418 },
14181419 .DWARF => {
1419 return unwindFrameMachODwarf(context, ma, eh_frame orelse return error.MissingEhFrame, @intCast(encoding.value.x86_64.dwarf));
1420 return unwindFrameMachODwarf(allocator, base_address, context, ma, eh_frame orelse return error.MissingEhFrame, @intCast(encoding.value.x86_64.dwarf));
14201421 },
14211422 },
14221423 .aarch64, .aarch64_be => switch (encoding.mode.arm64) {
......@@ -1430,7 +1431,7 @@ pub fn unwindFrameMachO(
14301431 break :blk new_ip;
14311432 },
14321433 .DWARF => {
1433 return unwindFrameMachODwarf(context, ma, eh_frame orelse return error.MissingEhFrame, @intCast(encoding.value.arm64.dwarf));
1434 return unwindFrameMachODwarf(allocator, base_address, context, ma, eh_frame orelse return error.MissingEhFrame, @intCast(encoding.value.arm64.dwarf));
14341435 },
14351436 .FRAME => blk: {
14361437 const fp = (try regValueNative(context.thread_context, fpRegNum(reg_context), reg_context)).*;
......@@ -1555,13 +1556,16 @@ pub inline fn stripInstructionPtrAuthCode(ptr: usize) usize {
15551556
15561557/// Unwind a stack frame using DWARF unwinding info, updating the register context.
15571558///
1558/// If `.eh_frame_hdr` is available, it will be used to binary search for the FDE.
1559/// Otherwise, a linear scan of `.eh_frame` and `.debug_frame` is done to find the FDE.
1559/// If `.eh_frame_hdr` is available and complete, it will be used to binary search for the FDE.
1560/// Otherwise, a linear scan of `.eh_frame` and `.debug_frame` is done to find the FDE. The latter
1561/// may require lazily loading the data in those sections.
15601562///
15611563/// `explicit_fde_offset` is for cases where the FDE offset is known, such as when __unwind_info
15621564/// defers unwinding to DWARF. This is an offset into the `.eh_frame` section.
15631565pub fn unwindFrameDwarf(
1564 di: *const Dwarf,
1566 allocator: Allocator,
1567 di: *Dwarf,
1568 base_address: usize,
15651569 context: *UnwindContext,
15661570 ma: *std.debug.MemoryAccessor,
15671571 explicit_fde_offset: ?usize,
......@@ -1570,10 +1574,7 @@ pub fn unwindFrameDwarf(
15701574 if (context.pc == 0) return 0;
15711575
15721576 // Find the FDE and CIE
1573 var cie: Dwarf.CommonInformationEntry = undefined;
1574 var fde: Dwarf.FrameDescriptionEntry = undefined;
1575
1576 if (explicit_fde_offset) |fde_offset| {
1577 const cie, const fde = if (explicit_fde_offset) |fde_offset| blk: {
15771578 const dwarf_section: Dwarf.Section.Id = .eh_frame;
15781579 const frame_section = di.section(dwarf_section) orelse return error.MissingFDE;
15791580 if (fde_offset >= frame_section.len) return error.MissingFDE;
......@@ -1594,7 +1595,7 @@ pub fn unwindFrameDwarf(
15941595 const cie_entry_header = try Dwarf.EntryHeader.read(&fbr, null, dwarf_section);
15951596 if (cie_entry_header.type != .cie) return Dwarf.bad();
15961597
1597 cie = try Dwarf.CommonInformationEntry.parse(
1598 const cie = try Dwarf.CommonInformationEntry.parse(
15981599 cie_entry_header.entry_bytes,
15991600 0,
16001601 true,
......@@ -1604,8 +1605,7 @@ pub fn unwindFrameDwarf(
16041605 @sizeOf(usize),
16051606 native_endian,
16061607 );
1607
1608 fde = try Dwarf.FrameDescriptionEntry.parse(
1608 const fde = try Dwarf.FrameDescriptionEntry.parse(
16091609 fde_entry_header.entry_bytes,
16101610 0,
16111611 true,
......@@ -1613,17 +1613,44 @@ pub fn unwindFrameDwarf(
16131613 @sizeOf(usize),
16141614 native_endian,
16151615 );
1616 } else if (di.eh_frame_hdr) |header| {
1617 const eh_frame_len = if (di.section(.eh_frame)) |eh_frame| eh_frame.len else null;
1618 try header.findEntry(
1619 ma,
1620 eh_frame_len,
1621 @intFromPtr(di.section(.eh_frame_hdr).?.ptr),
1622 context.pc,
1623 &cie,
1624 &fde,
1625 );
1626 } else {
1616
1617 break :blk .{ cie, fde };
1618 } else blk: {
1619 // `.eh_frame_hdr` may be incomplete. We'll try it first, but if the lookup fails, we fall
1620 // back to loading `.eh_frame`/`.debug_frame` and using those from that point on.
1621
1622 if (di.eh_frame_hdr) |header| hdr: {
1623 const eh_frame_len = if (di.section(.eh_frame)) |eh_frame| eh_frame.len else null;
1624
1625 var cie: Dwarf.CommonInformationEntry = undefined;
1626 var fde: Dwarf.FrameDescriptionEntry = undefined;
1627
1628 header.findEntry(
1629 ma,
1630 eh_frame_len,
1631 @intFromPtr(di.section(.eh_frame_hdr).?.ptr),
1632 context.pc,
1633 &cie,
1634 &fde,
1635 ) catch |err| switch (err) {
1636 error.InvalidDebugInfo => {
1637 // `.eh_frame_hdr` appears to be incomplete, so go ahead and populate `cie_map`
1638 // and `fde_list`, and fall back to the binary search logic below.
1639 try di.scanCieFdeInfo(allocator, base_address);
1640
1641 // Since `.eh_frame_hdr` is incomplete, we're very likely to get more lookup
1642 // failures using it, and we've just built a complete, sorted list of FDEs
1643 // anyway, so just stop using `.eh_frame_hdr` altogether.
1644 di.eh_frame_hdr = null;
1645
1646 break :hdr;
1647 },
1648 else => return err,
1649 };
1650
1651 break :blk .{ cie, fde };
1652 }
1653
16271654 const index = std.sort.binarySearch(Dwarf.FrameDescriptionEntry, di.fde_list.items, context.pc, struct {
16281655 pub fn compareFn(pc: usize, item: Dwarf.FrameDescriptionEntry) std.math.Order {
16291656 if (pc < item.pc_begin) return .lt;
......@@ -1635,9 +1662,11 @@ pub fn unwindFrameDwarf(
16351662 }
16361663 }.compareFn);
16371664
1638 fde = if (index) |i| di.fde_list.items[i] else return error.MissingFDE;
1639 cie = di.cie_map.get(fde.cie_length_offset) orelse return error.MissingCIE;
1640 }
1665 const fde = if (index) |i| di.fde_list.items[i] else return error.MissingFDE;
1666 const cie = di.cie_map.get(fde.cie_length_offset) orelse return error.MissingCIE;
1667
1668 break :blk .{ cie, fde };
1669 };
16411670
16421671 var expression_context: Dwarf.expression.Context = .{
16431672 .format = cie.format,
......@@ -1802,6 +1831,8 @@ pub fn supportsUnwinding(target: std.Target) bool {
18021831}
18031832
18041833fn unwindFrameMachODwarf(
1834 allocator: Allocator,
1835 base_address: usize,
18051836 context: *UnwindContext,
18061837 ma: *std.debug.MemoryAccessor,
18071838 eh_frame: []const u8,
......@@ -1818,7 +1849,7 @@ fn unwindFrameMachODwarf(
18181849 .owned = false,
18191850 };
18201851
1821 return unwindFrameDwarf(&di, context, ma, fde_offset);
1852 return unwindFrameDwarf(allocator, &di, base_address, context, ma, fde_offset);
18221853}
18231854
18241855/// This is a virtual machine that runs DWARF call frame instructions.
src/Compilation.zig+4-5
......@@ -1274,15 +1274,12 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
12741274 // The "any" values provided by resolved config only account for
12751275 // explicitly-provided settings. We now make them additionally account
12761276 // for default setting resolution.
1277 const any_unwind_tables = switch (options.config.any_unwind_tables) {
1278 .none => options.root_mod.unwind_tables,
1279 .sync, .@"async" => |uwt| uwt,
1280 };
1277 const any_unwind_tables = options.config.any_unwind_tables or options.root_mod.unwind_tables != .none;
12811278 const any_non_single_threaded = options.config.any_non_single_threaded or !options.root_mod.single_threaded;
12821279 const any_sanitize_thread = options.config.any_sanitize_thread or options.root_mod.sanitize_thread;
12831280 const any_fuzz = options.config.any_fuzz or options.root_mod.fuzz;
12841281
1285 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables != .none;
1282 const link_eh_frame_hdr = options.link_eh_frame_hdr or any_unwind_tables;
12861283 const build_id = options.build_id orelse .none;
12871284
12881285 const link_libc = options.config.link_libc;
......@@ -6459,6 +6456,7 @@ fn buildOutputFromZig(
64596456 .root_optimize_mode = optimize_mode,
64606457 .root_strip = strip,
64616458 .link_libc = comp.config.link_libc,
6459 .any_unwind_tables = comp.root_mod.unwind_tables != .none,
64626460 });
64636461
64646462 const root_mod = try Package.Module.create(arena, .{
......@@ -6595,6 +6593,7 @@ pub fn build_crt_file(
65956593 .root_optimize_mode = comp.compilerRtOptMode(),
65966594 .root_strip = comp.compilerRtStrip(),
65976595 .link_libc = false,
6596 .any_unwind_tables = options.unwind_tables != .none,
65986597 .lto = switch (output_mode) {
65996598 .Lib => comp.config.lto,
66006599 .Obj, .Exe => .none,
src/Compilation/Config.zig+8-13
......@@ -12,14 +12,13 @@ link_libunwind: bool,
1212/// True if and only if the c_source_files field will have nonzero length when
1313/// calling Compilation.create.
1414any_c_source_files: bool,
15/// This is not `.none` if any `Module` has `unwind_tables` set explicitly to a
15/// This is `true` if any `Module` has `unwind_tables` set explicitly to a
1616/// value other than `.none`. Until `Compilation.create()` is called, it is
17/// possible for this to be `.none` while in fact all `Module` instances have
17/// possible for this to be `false` while in fact all `Module` instances have
1818/// `unwind_tables != .none` due to the default. After `Compilation.create()` is
1919/// called, this will also take into account the default setting, making this
20/// value `.sync` or `.@"async"` if and only if any `Module` has
21/// `unwind_tables != .none`.
22any_unwind_tables: std.builtin.UnwindTables,
20/// value `true` if and only if any `Module` has `unwind_tables != .none`.
21any_unwind_tables: bool,
2322/// This is true if any Module has single_threaded set explicitly to false. Until
2423/// Compilation.create is called, it is possible for this to be false while in
2524/// fact all Module instances have single_threaded=false due to the default
......@@ -57,6 +56,7 @@ export_memory: bool,
5756shared_memory: bool,
5857is_test: bool,
5958debug_format: DebugFormat,
59root_optimize_mode: std.builtin.OptimizeMode,
6060root_strip: bool,
6161root_error_tracing: bool,
6262dll_export_fns: bool,
......@@ -88,7 +88,7 @@ pub const Options = struct {
8888 any_non_single_threaded: bool = false,
8989 any_sanitize_thread: bool = false,
9090 any_fuzz: bool = false,
91 any_unwind_tables: std.builtin.UnwindTables = .none,
91 any_unwind_tables: bool = false,
9292 any_dyn_libs: bool = false,
9393 any_c_source_files: bool = false,
9494 any_non_stripped: bool = false,
......@@ -359,12 +359,6 @@ pub fn resolve(options: Options) ResolveError!Config {
359359 break :b false;
360360 };
361361
362 const any_unwind_tables = b: {
363 if (options.any_unwind_tables != .none) break :b options.any_unwind_tables;
364
365 break :b target_util.needUnwindTables(target, link_libunwind, options.any_sanitize_thread);
366 };
367
368362 const link_mode = b: {
369363 const explicitly_exe_or_dyn_lib = switch (options.output_mode) {
370364 .Obj => false,
......@@ -496,7 +490,7 @@ pub fn resolve(options: Options) ResolveError!Config {
496490 .link_libc = link_libc,
497491 .link_libcpp = link_libcpp,
498492 .link_libunwind = link_libunwind,
499 .any_unwind_tables = any_unwind_tables,
493 .any_unwind_tables = options.any_unwind_tables,
500494 .any_c_source_files = options.any_c_source_files,
501495 .any_non_single_threaded = options.any_non_single_threaded,
502496 .any_error_tracing = any_error_tracing,
......@@ -515,6 +509,7 @@ pub fn resolve(options: Options) ResolveError!Config {
515509 .use_lld = use_lld,
516510 .wasi_exec_model = wasi_exec_model,
517511 .debug_format = debug_format,
512 .root_optimize_mode = root_optimize_mode,
518513 .root_strip = root_strip,
519514 .dll_export_fns = dll_export_fns,
520515 .rdynamic = rdynamic,
src/Package/Module.zig+13-5
......@@ -112,17 +112,14 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
112112 if (options.inherited.sanitize_thread == true) assert(options.global.any_sanitize_thread);
113113 if (options.inherited.fuzz == true) assert(options.global.any_fuzz);
114114 if (options.inherited.single_threaded == false) assert(options.global.any_non_single_threaded);
115 if (options.inherited.unwind_tables) |uwt| if (uwt != .none) assert(options.global.any_unwind_tables != .none);
115 if (options.inherited.unwind_tables) |uwt| if (uwt != .none) assert(options.global.any_unwind_tables);
116116 if (options.inherited.error_tracing == true) assert(options.global.any_error_tracing);
117117
118118 const resolved_target = options.inherited.resolved_target orelse options.parent.?.resolved_target;
119119 const target = resolved_target.result;
120120
121121 const optimize_mode = options.inherited.optimize_mode orelse
122 if (options.parent) |p| p.optimize_mode else .Debug;
123
124 const unwind_tables = options.inherited.unwind_tables orelse
125 if (options.parent) |p| p.unwind_tables else options.global.any_unwind_tables;
122 if (options.parent) |p| p.optimize_mode else options.global.root_optimize_mode;
126123
127124 const strip = b: {
128125 if (options.inherited.strip) |x| break :b x;
......@@ -220,6 +217,17 @@ pub fn create(arena: Allocator, options: CreateOptions) !*Package.Module {
220217 break :b false;
221218 };
222219
220 const unwind_tables = b: {
221 if (options.inherited.unwind_tables) |x| break :b x;
222 if (options.parent) |p| break :b p.unwind_tables;
223
224 break :b target_util.defaultUnwindTables(
225 target,
226 options.global.link_libunwind,
227 sanitize_thread or options.global.any_sanitize_thread,
228 );
229 };
230
223231 const fuzz = b: {
224232 if (options.inherited.fuzz) |x| break :b x;
225233 if (options.parent) |p| break :b p.fuzz;
src/libcxx.zig+6-6
......@@ -397,6 +397,10 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
397397
398398 const optimize_mode = comp.compilerRtOptMode();
399399 const strip = comp.compilerRtStrip();
400 // See the `-fno-exceptions` logic for WASI.
401 // The old 32-bit x86 variant of SEH doesn't use tables.
402 const unwind_tables: std.builtin.UnwindTables =
403 if (target.os.tag == .wasi or (target.cpu.arch == .x86 and target.os.tag == .windows)) .none else .@"async";
400404
401405 const config = Compilation.Config.resolve(.{
402406 .output_mode = output_mode,
......@@ -408,6 +412,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
408412 .root_optimize_mode = optimize_mode,
409413 .root_strip = strip,
410414 .link_libc = true,
415 .any_unwind_tables = unwind_tables != .none,
411416 .lto = comp.config.lto,
412417 .any_sanitize_thread = comp.config.any_sanitize_thread,
413418 }) catch |err| {
......@@ -438,12 +443,7 @@ pub fn buildLibCxxAbi(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
438443 .valgrind = false,
439444 .optimize_mode = optimize_mode,
440445 .structured_cfg = comp.root_mod.structured_cfg,
441 // See the `-fno-exceptions` logic for WASI.
442 // The old 32-bit x86 variant of SEH doesn't use tables.
443 .unwind_tables = if (target.os.tag == .wasi or (target.cpu.arch == .x86 and target.os.tag == .windows))
444 .none
445 else
446 .@"async",
446 .unwind_tables = unwind_tables,
447447 .pic = comp.root_mod.pic,
448448 },
449449 .global = config,
src/libunwind.zig+5-2
......@@ -27,6 +27,9 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
2727 const arena = arena_allocator.allocator();
2828
2929 const output_mode = .Lib;
30 const target = comp.root_mod.resolved_target.result;
31 const unwind_tables: std.builtin.UnwindTables =
32 if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .@"async";
3033 const config = Compilation.Config.resolve(.{
3134 .output_mode = .Lib,
3235 .resolved_target = comp.root_mod.resolved_target,
......@@ -36,6 +39,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
3639 .root_optimize_mode = comp.compilerRtOptMode(),
3740 .root_strip = comp.compilerRtStrip(),
3841 .link_libc = true,
42 .any_unwind_tables = unwind_tables != .none,
3943 .lto = comp.config.lto,
4044 }) catch |err| {
4145 comp.setMiscFailure(
......@@ -45,7 +49,6 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
4549 );
4650 return error.SubCompilationFailed;
4751 };
48 const target = comp.root_mod.resolved_target.result;
4952 const root_mod = Module.create(arena, .{
5053 .global_cache_directory = comp.global_cache_directory,
5154 .paths = .{
......@@ -65,7 +68,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: std.Progress.Node) BuildErr
6568 .sanitize_thread = false,
6669 // necessary so that libunwind can unwind through its own stack frames
6770 // The old 32-bit x86 variant of SEH doesn't use tables.
68 .unwind_tables = if (target.cpu.arch == .x86 and target.os.tag == .windows) .none else .@"async",
71 .unwind_tables = unwind_tables,
6972 .pic = if (target_util.supports_fpic(target)) true else null,
7073 .optimize_mode = comp.compilerRtOptMode(),
7174 },
src/main.zig+5-12
......@@ -575,6 +575,7 @@ const usage_build_generic =
575575 \\ 0x[hexstring] Maximum 32 bytes
576576 \\ none (default) Disable build-id
577577 \\ --eh-frame-hdr Enable C++ exception handling by passing --eh-frame-hdr to linker
578 \\ --no-eh-frame-hdr Disable C++ exception handling by passing --no-eh-frame-hdr to linker
578579 \\ --emit-relocs Enable output of relocation sections for post build tools
579580 \\ -z [arg] Set linker extension flags
580581 \\ nodelete Indicate that the object cannot be deleted from a process
......@@ -1582,6 +1583,8 @@ fn buildOutputType(
15821583 fatal("unable to parse '{s}': {s}", .{ arg, @errorName(err) });
15831584 } else if (mem.eql(u8, arg, "--eh-frame-hdr")) {
15841585 link_eh_frame_hdr = true;
1586 } else if (mem.eql(u8, arg, "--no-eh-frame-hdr")) {
1587 link_eh_frame_hdr = false;
15851588 } else if (mem.eql(u8, arg, "--dynamicbase")) {
15861589 linker_dynamicbase = true;
15871590 } else if (mem.eql(u8, arg, "--no-dynamicbase")) {
......@@ -2846,12 +2849,7 @@ fn buildOutputType(
28462849 create_module.opts.any_fuzz = true;
28472850 if (mod_opts.unwind_tables) |uwt| switch (uwt) {
28482851 .none => {},
2849 .sync => if (create_module.opts.any_unwind_tables == .none) {
2850 create_module.opts.any_unwind_tables = .sync;
2851 },
2852 .@"async" => {
2853 create_module.opts.any_unwind_tables = .@"async";
2854 },
2852 .sync, .@"async" => create_module.opts.any_unwind_tables = true,
28552853 };
28562854 if (mod_opts.strip == false)
28572855 create_module.opts.any_non_stripped = true;
......@@ -7563,12 +7561,7 @@ fn handleModArg(
75637561 create_module.opts.any_fuzz = true;
75647562 if (mod_opts.unwind_tables) |uwt| switch (uwt) {
75657563 .none => {},
7566 .sync => if (create_module.opts.any_unwind_tables == .none) {
7567 create_module.opts.any_unwind_tables = .sync;
7568 },
7569 .@"async" => {
7570 create_module.opts.any_unwind_tables = .@"async";
7571 },
7564 .sync, .@"async" => create_module.opts.any_unwind_tables = true,
75727565 };
75737566 if (mod_opts.strip == false)
75747567 create_module.opts.any_non_stripped = true;
src/target.zig+1-1
......@@ -407,7 +407,7 @@ pub fn clangSupportsNoImplicitFloatArg(target: std.Target) bool {
407407 };
408408}
409409
410pub fn needUnwindTables(target: std.Target, libunwind: bool, libtsan: bool) std.builtin.UnwindTables {
410pub fn defaultUnwindTables(target: std.Target, libunwind: bool, libtsan: bool) std.builtin.UnwindTables {
411411 if (target.os.tag == .windows) {
412412 // The old 32-bit x86 variant of SEH doesn't use tables.
413413 return if (target.cpu.arch != .x86) .@"async" else .none;
test/src/StackTrace.zig+25-6
......@@ -21,17 +21,34 @@ const Config = struct {
2121};
2222
2323pub fn addCase(self: *StackTrace, config: Config) void {
24 self.addCaseInner(config, true);
25 if (shouldTestNonLlvm(self.b.graph.host.result)) {
26 self.addCaseInner(config, false);
27 }
28}
29
30fn addCaseInner(self: *StackTrace, config: Config, use_llvm: bool) void {
2431 if (config.Debug) |per_mode|
25 self.addExpect(config.name, config.source, .Debug, per_mode);
32 self.addExpect(config.name, config.source, .Debug, use_llvm, per_mode);
2633
2734 if (config.ReleaseSmall) |per_mode|
28 self.addExpect(config.name, config.source, .ReleaseSmall, per_mode);
35 self.addExpect(config.name, config.source, .ReleaseSmall, use_llvm, per_mode);
2936
3037 if (config.ReleaseFast) |per_mode|
31 self.addExpect(config.name, config.source, .ReleaseFast, per_mode);
38 self.addExpect(config.name, config.source, .ReleaseFast, use_llvm, per_mode);
3239
3340 if (config.ReleaseSafe) |per_mode|
34 self.addExpect(config.name, config.source, .ReleaseSafe, per_mode);
41 self.addExpect(config.name, config.source, .ReleaseSafe, use_llvm, per_mode);
42}
43
44fn shouldTestNonLlvm(target: std.Target) bool {
45 return switch (target.cpu.arch) {
46 .x86_64 => switch (target.ofmt) {
47 .elf => true,
48 else => false,
49 },
50 else => false,
51 };
3552}
3653
3754fn addExpect(
......@@ -39,13 +56,14 @@ fn addExpect(
3956 name: []const u8,
4057 source: []const u8,
4158 optimize_mode: OptimizeMode,
59 use_llvm: bool,
4260 mode_config: Config.PerMode,
4361) void {
4462 for (mode_config.exclude_os) |tag| if (tag == builtin.os.tag) return;
4563
4664 const b = self.b;
47 const annotated_case_name = b.fmt("check {s} ({s})", .{
48 name, @tagName(optimize_mode),
65 const annotated_case_name = b.fmt("check {s} ({s} {s})", .{
66 name, @tagName(optimize_mode), if (use_llvm) "llvm" else "selfhosted",
4967 });
5068 for (self.test_filters) |test_filter| {
5169 if (mem.indexOf(u8, annotated_case_name, test_filter)) |_| break;
......@@ -61,6 +79,7 @@ fn addExpect(
6179 .target = b.graph.host,
6280 .error_tracing = mode_config.error_tracing,
6381 }),
82 .use_llvm = use_llvm,
6483 });
6584
6685 const run = b.addRunArtifact(exe);
test/src/check-stack-trace.zig+15-6
......@@ -58,14 +58,23 @@ pub fn main() !void {
5858 try buf.appendSlice(line[pos + 1 .. marks[2] + delims[2].len]);
5959 try buf.appendSlice(" [address]");
6060 if (optimize_mode == .Debug) {
61 // On certain platforms (windows) or possibly depending on how we choose to link main
62 // the object file extension may be present so we simply strip any extension.
63 if (mem.indexOfScalar(u8, line[marks[4]..marks[5]], '.')) |idot| {
64 try buf.appendSlice(line[marks[3] .. marks[4] + idot]);
65 try buf.appendSlice(line[marks[5]..]);
61 try buf.appendSlice(line[marks[3] .. marks[4] + delims[4].len]);
62
63 const file_name = line[marks[4] + delims[4].len .. marks[5]];
64 // The LLVM backend currently uses the object file name in the debug info here.
65 // This actually violates the DWARF specification (DWARF5 § 3.1.1, lines 24-27).
66 // The self-hosted backend uses the root Zig source file of the module (in compilance with the spec).
67 if (std.mem.eql(u8, file_name, "test") or
68 std.mem.eql(u8, file_name, "test.exe.obj") or
69 std.mem.endsWith(u8, file_name, ".zig"))
70 {
71 try buf.appendSlice("[main_file]");
6672 } else {
67 try buf.appendSlice(line[marks[3]..]);
73 // Something unexpected; include it verbatim.
74 try buf.appendSlice(file_name);
6875 }
76
77 try buf.appendSlice(line[marks[5]..]);
6978 } else {
7079 try buf.appendSlice(line[marks[3] .. marks[3] + delims[3].len]);
7180 try buf.appendSlice("[function]");
test/stack_traces.zig+30-30
......@@ -13,7 +13,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
1313 .Debug = .{
1414 .expect =
1515 \\error: TheSkyIsFalling
16 \\source.zig:2:5: [address] in main (test)
16 \\source.zig:2:5: [address] in main ([main_file])
1717 \\ return error.TheSkyIsFalling;
1818 \\ ^
1919 \\
......@@ -61,10 +61,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
6161 .Debug = .{
6262 .expect =
6363 \\error: TheSkyIsFalling
64 \\source.zig:2:5: [address] in foo (test)
64 \\source.zig:2:5: [address] in foo ([main_file])
6565 \\ return error.TheSkyIsFalling;
6666 \\ ^
67 \\source.zig:6:5: [address] in main (test)
67 \\source.zig:6:5: [address] in main ([main_file])
6868 \\ try foo();
6969 \\ ^
7070 \\
......@@ -120,7 +120,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
120120 .Debug = .{
121121 .expect =
122122 \\error: UnrelatedError
123 \\source.zig:13:5: [address] in main (test)
123 \\source.zig:13:5: [address] in main ([main_file])
124124 \\ return error.UnrelatedError;
125125 \\ ^
126126 \\
......@@ -172,7 +172,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
172172 .Debug = .{
173173 .expect =
174174 \\error: UnrelatedError
175 \\source.zig:10:5: [address] in main (test)
175 \\source.zig:10:5: [address] in main ([main_file])
176176 \\ return error.UnrelatedError;
177177 \\ ^
178178 \\
......@@ -224,10 +224,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
224224 .Debug = .{
225225 .expect =
226226 \\error: TheSkyIsFalling
227 \\source.zig:2:5: [address] in foo (test)
227 \\source.zig:2:5: [address] in foo ([main_file])
228228 \\ return error.TheSkyIsFalling;
229229 \\ ^
230 \\source.zig:10:5: [address] in main (test)
230 \\source.zig:10:5: [address] in main ([main_file])
231231 \\ try foo();
232232 \\ ^
233233 \\
......@@ -284,7 +284,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
284284 .Debug = .{
285285 .expect =
286286 \\error: BadTime
287 \\source.zig:12:5: [address] in main (test)
287 \\source.zig:12:5: [address] in main ([main_file])
288288 \\ return error.BadTime;
289289 \\ ^
290290 \\
......@@ -332,10 +332,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
332332 .Debug = .{
333333 .expect =
334334 \\error: AndMyCarIsOutOfGas
335 \\source.zig:2:5: [address] in foo (test)
335 \\source.zig:2:5: [address] in foo ([main_file])
336336 \\ return error.TheSkyIsFalling;
337337 \\ ^
338 \\source.zig:6:5: [address] in main (test)
338 \\source.zig:6:5: [address] in main ([main_file])
339339 \\ return foo() catch error.AndMyCarIsOutOfGas;
340340 \\ ^
341341 \\
......@@ -391,7 +391,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
391391 .Debug = .{
392392 .expect =
393393 \\error: SomethingUnrelatedWentWrong
394 \\source.zig:11:5: [address] in main (test)
394 \\source.zig:11:5: [address] in main ([main_file])
395395 \\ return error.SomethingUnrelatedWentWrong;
396396 \\ ^
397397 \\
......@@ -456,13 +456,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
456456 .Debug = .{
457457 .expect =
458458 \\error: StillUnresolved
459 \\source.zig:1:18: [address] in foo (test)
459 \\source.zig:1:18: [address] in foo ([main_file])
460460 \\fn foo() !void { return error.TheSkyIsFalling; }
461461 \\ ^
462 \\source.zig:2:18: [address] in bar (test)
462 \\source.zig:2:18: [address] in bar ([main_file])
463463 \\fn bar() !void { return error.InternalError; }
464464 \\ ^
465 \\source.zig:23:5: [address] in main (test)
465 \\source.zig:23:5: [address] in main ([main_file])
466466 \\ return error.StillUnresolved;
467467 \\ ^
468468 \\
......@@ -527,13 +527,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
527527 .Debug = .{
528528 .expect =
529529 \\error: TestExpectedError
530 \\source.zig:9:18: [address] in foo (test)
530 \\source.zig:9:18: [address] in foo ([main_file])
531531 \\fn foo() !void { return error.Foo; }
532532 \\ ^
533 \\source.zig:5:5: [address] in expectError (test)
533 \\source.zig:5:5: [address] in expectError ([main_file])
534534 \\ return error.TestExpectedError;
535535 \\ ^
536 \\source.zig:17:5: [address] in main (test)
536 \\source.zig:17:5: [address] in main ([main_file])
537537 \\ try expectError(error.Bar, foo());
538538 \\ ^
539539 \\
......@@ -592,13 +592,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
592592 .Debug = .{
593593 .expect =
594594 \\error: AndMyCarIsOutOfGas
595 \\source.zig:2:5: [address] in foo (test)
595 \\source.zig:2:5: [address] in foo ([main_file])
596596 \\ return error.TheSkyIsFalling;
597597 \\ ^
598 \\source.zig:6:5: [address] in bar (test)
598 \\source.zig:6:5: [address] in bar ([main_file])
599599 \\ return error.AndMyCarIsOutOfGas;
600600 \\ ^
601 \\source.zig:11:9: [address] in main (test)
601 \\source.zig:11:9: [address] in main ([main_file])
602602 \\ try bar();
603603 \\ ^
604604 \\
......@@ -657,13 +657,13 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
657657 .Debug = .{
658658 .expect =
659659 \\error: AndMyCarIsOutOfGas
660 \\source.zig:2:5: [address] in foo (test)
660 \\source.zig:2:5: [address] in foo ([main_file])
661661 \\ return error.TheSkyIsFalling;
662662 \\ ^
663 \\source.zig:6:5: [address] in bar (test)
663 \\source.zig:6:5: [address] in bar ([main_file])
664664 \\ return error.AndMyCarIsOutOfGas;
665665 \\ ^
666 \\source.zig:11:9: [address] in main (test)
666 \\source.zig:11:9: [address] in main ([main_file])
667667 \\ try bar();
668668 \\ ^
669669 \\
......@@ -724,16 +724,16 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
724724 .Debug = .{
725725 .expect =
726726 \\error: TheSkyIsFalling
727 \\source.zig:10:5: [address] in make_error (test)
727 \\source.zig:10:5: [address] in make_error ([main_file])
728728 \\ return error.TheSkyIsFalling;
729729 \\ ^
730 \\source.zig:6:5: [address] in bar (test)
730 \\source.zig:6:5: [address] in bar ([main_file])
731731 \\ return make_error();
732732 \\ ^
733 \\source.zig:2:5: [address] in foo (test)
733 \\source.zig:2:5: [address] in foo ([main_file])
734734 \\ try bar();
735735 \\ ^
736 \\source.zig:14:5: [address] in main (test)
736 \\source.zig:14:5: [address] in main ([main_file])
737737 \\ try foo();
738738 \\ ^
739739 \\
......@@ -797,10 +797,10 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
797797 .windows, // TODO intermittent failures
798798 },
799799 .expect =
800 \\source.zig:7:8: [address] in foo (test)
800 \\source.zig:7:8: [address] in foo ([main_file])
801801 \\ bar();
802802 \\ ^
803 \\source.zig:10:8: [address] in main (test)
803 \\source.zig:10:8: [address] in main ([main_file])
804804 \\ foo();
805805 \\ ^
806806 \\
......@@ -829,7 +829,7 @@ pub fn addCases(cases: *tests.StackTracesContext) void {
829829 .Debug = .{
830830 .expect =
831831 \\error: TheSkyIsFalling
832 \\source.zig:3:5: [address] in main (test)
832 \\source.zig:3:5: [address] in main ([main_file])
833833 \\ return error.TheSkyIsFalling;
834834 \\ ^
835835 \\