Commit c9b6f1bf90

Veikka Tuominen <git@vexu.eu>
2022-03-19 13:05:57
std: enable default panic handler for stage2 LLVM on Linux
1 parent 2757237
lib/std/heap/general_purpose_allocator.zig
@@ -341,7 +341,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
                             const slot_index = @intCast(SlotIndex, used_bits_byte * 8 + bit_index);
                             const stack_trace = bucketStackTrace(bucket, size_class, slot_index, .alloc);
                             const addr = bucket.page + slot_index * size_class;
-                            if (builtin.zig_backend == .stage1) {
+                            if (builtin.zig_backend == .stage1 or builtin.os.tag == .linux) {
                                 log.err("memory address 0x{x} leaked: {s}", .{
                                     @ptrToInt(addr), stack_trace,
                                 });
@@ -379,7 +379,7 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
             while (it.next()) |large_alloc| {
                 if (config.retain_metadata and large_alloc.freed) continue;
                 const stack_trace = large_alloc.getStackTrace(.alloc);
-                if (builtin.zig_backend == .stage1) {
+                if (builtin.zig_backend == .stage1 or builtin.os.tag == .linux) {
                     log.err("memory address 0x{x} leaked: {s}", .{
                         @ptrToInt(large_alloc.bytes.ptr), stack_trace,
                     });
lib/std/os/linux.zig
@@ -1080,12 +1080,19 @@ pub fn sigaction(sig: u6, noalias act: ?*const Sigaction, noalias oact: ?*Sigact
     const mask_size = @sizeOf(@TypeOf(ksa.mask));
 
     if (act) |new| {
-        const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) restore_rt else restore;
+        const restore_rt_ptr = if (builtin.zig_backend == .stage1) restore_rt else &syscall_bits.restore_rt;
+        // TODO https://github.com/ziglang/zig/issues/11227
+        const restore_ptr = if (builtin.zig_backend == .stage1) restore else switch (native_arch) {
+            .arm, .thumb, .mips, .mipsel, .i386 => &syscall_bits.restore,
+            .x86_64, .aarch64, .riscv64, .sparcv9, .powerpc, .powerpc64, .powerpc64le => &syscall_bits.restore_rt,
+            else => unreachable,
+        };
+        const restorer_fn = if ((new.flags & SA.SIGINFO) != 0) restore_rt_ptr else restore_ptr;
         ksa = k_sigaction{
             .handler = new.handler.handler,
             .flags = new.flags | SA.RESTORER,
             .mask = undefined,
-            .restorer = @ptrCast(fn () callconv(.C) void, restorer_fn),
+            .restorer = @ptrCast(k_sigaction_funcs.restorer, restorer_fn),
         };
         @memcpy(@ptrCast([*]u8, &ksa.mask), @ptrCast([*]const u8, &new.mask), mask_size);
     }
@@ -3047,39 +3054,55 @@ pub const sigset_t = [1024 / 32]u32;
 pub const all_mask: sigset_t = [_]u32{0xffffffff} ** sigset_t.len;
 pub const app_mask: sigset_t = [2]u32{ 0xfffffffc, 0x7fffffff } ++ [_]u32{0xffffffff} ** 30;
 
+const k_sigaction_funcs = if (builtin.zig_backend == .stage1) struct {
+    const handler = ?fn (c_int) callconv(.C) void;
+    const restorer = fn () callconv(.C) void;
+} else struct {
+    const handler = ?*const fn (c_int) callconv(.C) void;
+    const restorer = *const fn () callconv(.C) void;
+};
+
 pub const k_sigaction = switch (native_arch) {
     .mips, .mipsel => extern struct {
         flags: c_uint,
-        handler: ?fn (c_int) callconv(.C) void,
+        handler: k_sigaction_funcs.handler,
         mask: [4]c_ulong,
-        restorer: fn () callconv(.C) void,
+        restorer: k_sigaction_funcs.restorer,
     },
     .mips64, .mips64el => extern struct {
         flags: c_uint,
-        handler: ?fn (c_int) callconv(.C) void,
+        handler: k_sigaction_funcs.handler,
         mask: [2]c_ulong,
-        restorer: fn () callconv(.C) void,
+        restorer: k_sigaction_funcs.restorer,
     },
     else => extern struct {
-        handler: ?fn (c_int) callconv(.C) void,
+        handler: k_sigaction_funcs.handler,
         flags: c_ulong,
-        restorer: fn () callconv(.C) void,
+        restorer: k_sigaction_funcs.restorer,
         mask: [2]c_uint,
     },
 };
 
 /// Renamed from `sigaction` to `Sigaction` to avoid conflict with the syscall.
 pub const Sigaction = extern struct {
-    pub const handler_fn = fn (c_int) callconv(.C) void;
-    pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
+    pub usingnamespace if (builtin.zig_backend == .stage1) struct {
+        pub const handler_fn = fn (c_int) callconv(.C) void;
+        pub const sigaction_fn = fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
+    } else struct {
+        pub const handler_fn = *const fn (c_int) callconv(.C) void;
+        pub const sigaction_fn = *const fn (c_int, *const siginfo_t, ?*const anyopaque) callconv(.C) void;
+    };
 
     handler: extern union {
-        handler: ?handler_fn,
-        sigaction: ?sigaction_fn,
+        handler: ?Sigaction.handler_fn,
+        sigaction: ?Sigaction.sigaction_fn,
     },
     mask: sigset_t,
     flags: c_uint,
-    restorer: ?fn () callconv(.C) void = null,
+    restorer: ?if (builtin.zig_backend == .stage1)
+        fn () callconv(.C) void
+    else
+        *const fn () callconv(.C) void = null,
 };
 
 pub const empty_sigset = [_]u32{0} ** @typeInfo(sigset_t).Array.len;
lib/std/builtin.zig
@@ -753,7 +753,9 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn
     @setCold(true);
     // Until self-hosted catches up with stage1 language features, we have a simpler
     // default panic function:
-    if (builtin.zig_backend != .stage1) {
+    const panic_works = builtin.zig_backend == .stage1 or
+        (builtin.zig_backend == .stage2_llvm and builtin.os.tag == .linux);
+    if (!panic_works) {
         while (true) {
             @breakpoint();
         }
lib/std/debug.zig
@@ -1541,7 +1541,6 @@ pub const ModuleDebugInfo = switch (native_os) {
                         .symbol_name = o_file_di.getSymbolName(relocated_address_o) orelse "???",
                         .compile_unit_name = compile_unit.die.getAttrString(o_file_di, DW.AT.name) catch |err| switch (err) {
                             error.MissingDebugInfo, error.InvalidDebugInfo => "???",
-                            else => return err,
                         },
                         .line_info = o_file_di.getLineNumberInfo(compile_unit.*, relocated_address_o + addr_off) catch |err| switch (err) {
                             error.MissingDebugInfo, error.InvalidDebugInfo => null,