Skip to content

stack trace SEGV when linking libc and using debug_allocator within a thread #25025

@blblack

Description

@blblack

Zig Version

0.16.0-dev.46+d4df65e35

Steps to Reproduce and Observed Behavior

This is the simplest test code I got this down to:

const std = @import("std");

fn sub_thread() !void {
    var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
    const alloc = debug_allocator.allocator();
    const thing: []u8 = try alloc.alloc(u8, 1);
    defer alloc.free(thing);
}

pub fn main() !void {
    const thr = try std.Thread.spawn(.{}, sub_thread, .{});
    thr.join();
}

For me, on x86_64 Linux, if I run the above via plain zig run it executes just fine. But zig run -lc (link libc, which means threads are pthreads) causes this sort of thing:

Segmentation fault at address 0xffffffffffffff40
/home/bblack/repos/zig/lib/std/debug.zig:953:52: 0x1081a04 in next_internal (std.zig)
        const new_fp = math.add(usize, @as(*usize, @ptrFromInt(fp)).*, fp_bias) catch
                                                   ^
/home/bblack/repos/zig/lib/std/debug.zig:876:39: 0x104cc66 in next (std.zig)
        var address = it.next_internal() orelse return null;
                                      ^
/home/bblack/repos/zig/lib/std/debug.zig:510:29: 0x113fbd2 in captureStackTrace (std.zig)
            addr.* = it.next() orelse {
                            ^
/home/bblack/repos/zig/lib/std/heap/debug_allocator.zig:515:40: 0x11401d4 in collectStackTrace (std.zig)
            std.debug.captureStackTrace(first_trace_addr, &stack_trace);
                                       ^
/home/bblack/repos/zig/lib/std/heap/debug_allocator.zig:333:34: 0x113caf0 in captureStackTrace (std.zig)
                collectStackTrace(ret_addr, stack_addresses);
                                 ^
/home/bblack/repos/zig/lib/std/heap/debug_allocator.zig:801:41: 0x113a356 in alloc (std.zig)
                bucket.captureStackTrace(ret_addr, slot_count, 0, .alloc);
                                        ^
/home/bblack/repos/zig/lib/std/mem/Allocator.zig:129:26: 0x10ba2ce in allocBytesWithAlignment__anon_14058 (std.zig)
    return a.vtable.alloc(a.ptr, len, alignment, ret_addr);
                         ^
/home/bblack/repos/zig/lib/std/mem/Allocator.zig:269:40: 0x1095789 in allocWithSizeAndAlignment__anon_10662 (std.zig)
    return self.allocBytesWithAlignment(alignment, byte_count, return_address);
                                       ^
/home/bblack/repos/zig/lib/std/mem/Allocator.zig:257:89: 0x10682bc in alloc__anon_5687 (std.zig)
    const ptr: [*]align(a.toByteUnits()) T = @ptrCast(try self.allocWithSizeAndAlignment(@sizeOf(T), a, n, return_address));
                                                                                        ^
/home/bblack/repos/gdnsd/direct.zig:6:40: 0x11373ac in sub_thread (direct.zig)
    const thing: []u8 = try alloc.alloc(u8, 1);
                                       ^
/home/bblack/repos/zig/lib/std/Thread.zig:529:21: 0x1137533 in callFn__anon_22584 (std.zig)
                    @call(.auto, f, args) catch |err| {
                    ^
/home/bblack/repos/zig/lib/std/Thread.zig:782:30: 0x1136e56 in entryFn (std.zig)
                return callFn(f, args_ptr.*);
                             ^
v./nptl/pthread_create.c:448:8: 0x7f1049559b7a in start_thread (pthread_create.c)
../sysdeps/unix/sysv/linux/x86_64/clone3.S:78:0: 0x7f10495d77b7 in __clone3 (../sysdeps/unix/sysv/linux/x86_64/clone3.S)
???:?:?: 0x0 in ??? (???)
Aborted

Note this is an extremely similar trace to #24970 , but that is flagged as ppc-only. This happens on x86_64 Linux, when linking libc and spinning up a debug allocator inside a thread.

Also note: this problem goes away if I revert the deletion of MemoryAccessor and related followup commits.

Expected Behavior

Not crashing

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorregressionIt worked in a previous version of Zig, but stopped working.standard libraryThis issue involves writing Zig code for the standard library.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions