aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/stack_iterator
diff options
context:
space:
mode:
authorJakub Konka <kubkon@jakubkonka.com>2023-07-21 17:37:22 +0200
committerGitHub <noreply@github.com>2023-07-21 17:37:22 +0200
commit61d5b7c957e63239f1ebbdbfb94105d53f2dbaa4 (patch)
treea49da9a773faff32301383f9bffaed99852c26f6 /test/standalone/stack_iterator
parentc43ee5bb22298eefc3fae919807f5da8f7be70f1 (diff)
parentb1d86db7b45c57b2a9d48655738bce8d77327438 (diff)
downloadzig-61d5b7c957e63239f1ebbdbfb94105d53f2dbaa4.tar.gz
zig-61d5b7c957e63239f1ebbdbfb94105d53f2dbaa4.zip
Merge pull request #15823 from kcbanner/dwarf_unwind
Add DWARF unwinding, and an external debug info loader for ELF
Diffstat (limited to 'test/standalone/stack_iterator')
-rw-r--r--test/standalone/stack_iterator/build.zig94
-rw-r--r--test/standalone/stack_iterator/shared_lib.c22
-rw-r--r--test/standalone/stack_iterator/shared_lib_unwind.zig47
-rw-r--r--test/standalone/stack_iterator/unwind.zig99
4 files changed, 262 insertions, 0 deletions
diff --git a/test/standalone/stack_iterator/build.zig b/test/standalone/stack_iterator/build.zig
new file mode 100644
index 0000000000..1c5a9673ce
--- /dev/null
+++ b/test/standalone/stack_iterator/build.zig
@@ -0,0 +1,94 @@
+const std = @import("std");
+
+pub fn build(b: *std.Build) void {
+ const test_step = b.step("test", "Test it");
+ b.default_step = test_step;
+
+ const target = b.standardTargetOptions(.{});
+ const optimize = b.standardOptimizeOption(.{});
+
+ // Unwinding with a frame pointer
+ //
+ // getcontext version: zig std
+ //
+ // Unwind info type:
+ // - ELF: DWARF .debug_frame
+ // - MachO: __unwind_info encodings:
+ // - x86_64: RBP_FRAME
+ // - aarch64: FRAME, DWARF
+ {
+ const exe = b.addExecutable(.{
+ .name = "unwind_fp",
+ .root_source_file = .{ .path = "unwind.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+
+ if (target.isDarwin()) exe.unwind_tables = true;
+ exe.omit_frame_pointer = false;
+
+ const run_cmd = b.addRunArtifact(exe);
+ test_step.dependOn(&run_cmd.step);
+ }
+
+ // Unwinding without a frame pointer
+ //
+ // getcontext version: zig std
+ //
+ // Unwind info type:
+ // - ELF: DWARF .eh_frame_hdr + .eh_frame
+ // - MachO: __unwind_info encodings:
+ // - x86_64: STACK_IMMD, STACK_IND
+ // - aarch64: FRAMELESS, DWARF
+ {
+ const exe = b.addExecutable(.{
+ .name = "unwind_nofp",
+ .root_source_file = .{ .path = "unwind.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+
+ exe.omit_frame_pointer = true;
+ exe.unwind_tables = true;
+
+ const run_cmd = b.addRunArtifact(exe);
+ test_step.dependOn(&run_cmd.step);
+ }
+
+ // Unwinding through a C shared library without a frame pointer (libc)
+ //
+ // getcontext version: libc
+ //
+ // Unwind info type:
+ // - ELF: DWARF .eh_frame + .debug_frame
+ // - MachO: __unwind_info encodings:
+ // - x86_64: STACK_IMMD, STACK_IND
+ // - aarch64: FRAMELESS, DWARF
+ {
+ const c_shared_lib = b.addSharedLibrary(.{
+ .name = "c_shared_lib",
+ .target = target,
+ .optimize = optimize,
+ });
+
+ if (target.isWindows()) c_shared_lib.defineCMacro("LIB_API", "__declspec(dllexport)");
+
+ c_shared_lib.strip = false;
+ c_shared_lib.addCSourceFile("shared_lib.c", &.{"-fomit-frame-pointer"});
+ c_shared_lib.linkLibC();
+
+ const exe = b.addExecutable(.{
+ .name = "shared_lib_unwind",
+ .root_source_file = .{ .path = "shared_lib_unwind.zig" },
+ .target = target,
+ .optimize = optimize,
+ });
+
+ if (target.isDarwin()) exe.unwind_tables = true;
+ exe.omit_frame_pointer = true;
+ exe.linkLibrary(c_shared_lib);
+
+ const run_cmd = b.addRunArtifact(exe);
+ test_step.dependOn(&run_cmd.step);
+ }
+}
diff --git a/test/standalone/stack_iterator/shared_lib.c b/test/standalone/stack_iterator/shared_lib.c
new file mode 100644
index 0000000000..c3170f2dc0
--- /dev/null
+++ b/test/standalone/stack_iterator/shared_lib.c
@@ -0,0 +1,22 @@
+#include <stdint.h>
+
+#ifndef LIB_API
+#define LIB_API
+#endif
+
+__attribute__((noinline)) void frame1(
+ void** expected,
+ void** unwound,
+ void (*frame2)(void** expected, void** unwound)) {
+ expected[3] = __builtin_extract_return_addr(__builtin_return_address(0));
+ frame2(expected, unwound);
+}
+
+LIB_API void frame0(
+ void** expected,
+ void** unwound,
+ void (*frame2)(void** expected, void** unwound)) {
+ expected[4] = __builtin_extract_return_addr(__builtin_return_address(0));
+ frame1(expected, unwound, frame2);
+}
+
diff --git a/test/standalone/stack_iterator/shared_lib_unwind.zig b/test/standalone/stack_iterator/shared_lib_unwind.zig
new file mode 100644
index 0000000000..50e0421e2a
--- /dev/null
+++ b/test/standalone/stack_iterator/shared_lib_unwind.zig
@@ -0,0 +1,47 @@
+const std = @import("std");
+const builtin = @import("builtin");
+const debug = std.debug;
+const testing = std.testing;
+
+noinline fn frame4(expected: *[5]usize, unwound: *[5]usize) void {
+ expected[0] = @returnAddress();
+
+ var context: debug.ThreadContext = undefined;
+ testing.expect(debug.getContext(&context)) catch @panic("failed to getContext");
+
+ var debug_info = debug.getSelfDebugInfo() catch @panic("failed to openSelfDebugInfo");
+ var it = debug.StackIterator.initWithContext(expected[0], debug_info, &context) catch @panic("failed to initWithContext");
+ defer it.deinit();
+
+ for (unwound) |*addr| {
+ if (it.next()) |return_address| addr.* = return_address;
+ }
+}
+
+noinline fn frame3(expected: *[5]usize, unwound: *[5]usize) void {
+ expected[1] = @returnAddress();
+ frame4(expected, unwound);
+}
+
+fn frame2(expected: *[5]usize, unwound: *[5]usize) callconv(.C) void {
+ expected[2] = @returnAddress();
+ frame3(expected, unwound);
+}
+
+extern fn frame0(
+ expected: *[5]usize,
+ unwound: *[5]usize,
+ frame_2: *const fn (expected: *[5]usize, unwound: *[5]usize) callconv(.C) void,
+) void;
+
+pub fn main() !void {
+ // Disabled until the DWARF unwinder bugs on .aarch64 are solved
+ if (builtin.omit_frame_pointer and comptime builtin.target.isDarwin() and builtin.cpu.arch == .aarch64) return;
+
+ if (!std.debug.have_ucontext or !std.debug.have_getcontext) return;
+
+ var expected: [5]usize = undefined;
+ var unwound: [5]usize = undefined;
+ frame0(&expected, &unwound, &frame2);
+ try testing.expectEqual(expected, unwound);
+}
diff --git a/test/standalone/stack_iterator/unwind.zig b/test/standalone/stack_iterator/unwind.zig
new file mode 100644
index 0000000000..1280118173
--- /dev/null
+++ b/test/standalone/stack_iterator/unwind.zig
@@ -0,0 +1,99 @@
+const std = @import("std");
+const builtin = @import("builtin");
+const debug = std.debug;
+const testing = std.testing;
+
+noinline fn frame3(expected: *[4]usize, unwound: *[4]usize) void {
+ expected[0] = @returnAddress();
+
+ var context: debug.ThreadContext = undefined;
+ testing.expect(debug.getContext(&context)) catch @panic("failed to getContext");
+
+ var debug_info = debug.getSelfDebugInfo() catch @panic("failed to openSelfDebugInfo");
+ var it = debug.StackIterator.initWithContext(expected[0], debug_info, &context) catch @panic("failed to initWithContext");
+ defer it.deinit();
+
+ for (unwound) |*addr| {
+ if (it.next()) |return_address| addr.* = return_address;
+ }
+}
+
+noinline fn frame2(expected: *[4]usize, unwound: *[4]usize) void {
+ // Excercise different __unwind_info / DWARF CFI encodings by forcing some registers to be restored
+ if (builtin.target.ofmt != .c) {
+ switch (builtin.cpu.arch) {
+ .x86 => {
+ if (builtin.omit_frame_pointer) {
+ asm volatile (
+ \\movl $3, %%ebx
+ \\movl $1, %%ecx
+ \\movl $2, %%edx
+ \\movl $7, %%edi
+ \\movl $6, %%esi
+ \\movl $5, %%ebp
+ ::: "ebx", "ecx", "edx", "edi", "esi", "ebp");
+ } else {
+ asm volatile (
+ \\movl $3, %%ebx
+ \\movl $1, %%ecx
+ \\movl $2, %%edx
+ \\movl $7, %%edi
+ \\movl $6, %%esi
+ ::: "ebx", "ecx", "edx", "edi", "esi");
+ }
+ },
+ .x86_64 => {
+ if (builtin.omit_frame_pointer) {
+ asm volatile (
+ \\movq $3, %%rbx
+ \\movq $12, %%r12
+ \\movq $13, %%r13
+ \\movq $14, %%r14
+ \\movq $15, %%r15
+ \\movq $6, %%rbp
+ ::: "rbx", "r12", "r13", "r14", "r15", "rbp");
+ } else {
+ asm volatile (
+ \\movq $3, %%rbx
+ \\movq $12, %%r12
+ \\movq $13, %%r13
+ \\movq $14, %%r14
+ \\movq $15, %%r15
+ ::: "rbx", "r12", "r13", "r14", "r15");
+ }
+ },
+ else => {},
+ }
+ }
+
+ expected[1] = @returnAddress();
+ frame3(expected, unwound);
+}
+
+noinline fn frame1(expected: *[4]usize, unwound: *[4]usize) void {
+ expected[2] = @returnAddress();
+
+ // Use a stack frame that is too big to encode in __unwind_info's stack-immediate encoding
+ // to exercise the stack-indirect encoding path
+ var pad: [std.math.maxInt(u8) * @sizeOf(usize) + 1]u8 = undefined;
+ _ = pad;
+
+ frame2(expected, unwound);
+}
+
+noinline fn frame0(expected: *[4]usize, unwound: *[4]usize) void {
+ expected[3] = @returnAddress();
+ frame1(expected, unwound);
+}
+
+pub fn main() !void {
+ // Disabled until the DWARF unwinder bugs on .aarch64 are solved
+ if (builtin.omit_frame_pointer and comptime builtin.target.isDarwin() and builtin.cpu.arch == .aarch64) return;
+
+ if (!std.debug.have_ucontext or !std.debug.have_getcontext) return;
+
+ var expected: [4]usize = undefined;
+ var unwound: [4]usize = undefined;
+ frame0(&expected, &unwound);
+ try testing.expectEqual(expected, unwound);
+}