Skip to content

Seg-fault using ArrayList in thread local storage #11364

@A1Liu

Description

@A1Liu

Zig Version

0.9.1

Steps to Reproduce

Platform: M1 Macbook Air (2020), MacOS Monterey 12.3

const std = @import("std");
const print = std.debug.print;
const mem = std.mem;
const Allocator = mem.Allocator;
const ArrayList = std.ArrayList;

pub const Pages = std.heap.page_allocator;

const Storage = struct {
    data: ArrayList(u8),
};

threadlocal var temporary_storage: Storage = .{
    .data = ArrayList(u8).init(Pages),
};

pub fn main() anyerror!void {
    // the workaround I'm using right now, which causes expected behavior
    // temporary_storage.ranges.allocator = Pages;

    try temporary_storage.data.append(1);
    print("done!\n", .{});
}

Expected Behavior

I expect the program to print:

done!

Actual Behavior

Segmentation fault at address 0x100044478
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/mem/Allocator.zig:154:23: 0x10499a17c in std.mem.Allocator.allocAdvancedWithRetAddr (tls_allocators)
    return self.vtable.alloc(self.ptr, len, ptr_align, len_align, ret_addr);
                      ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/mem/Allocator.zig:284:62: 0x104973d77 in std.mem.Allocator.allocAdvancedWithRetAddr (tls_allocators)
        if (a == @alignOf(T)) return allocAdvancedWithRetAddr(self, T, null, n, exact, return_address);
                                                             ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/mem/Allocator.zig:382:45: 0x104971f43 in std.mem.Allocator.reallocAdvancedWithRetAddr (tls_allocators)
        return self.allocAdvancedWithRetAddr(T, new_alignment, new_n, exact, return_address);
                                            ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/mem/Allocator.zig:355:43: 0x104971e87 in std.mem.Allocator.reallocAtLeast (tls_allocators)
    return self.reallocAdvancedWithRetAddr(old_mem, old_alignment, new_n, .at_least, @returnAddress());
                                          ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/array_list.zig:325:69: 0x104971d3b in std.array_list.ArrayListAligned(u8,null).ensureTotalCapacityPrecise (tls_allocators)
                const new_memory = try self.allocator.reallocAtLeast(self.allocatedSlice(), new_capacity);
                                                                    ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/array_list.zig:310:55: 0x104971b0b in std.array_list.ArrayListAligned(u8,null).ensureTotalCapacity (tls_allocators)
                return self.ensureTotalCapacityPrecise(better_capacity);
                                                      ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/array_list.zig:349:41: 0x104971a77 in std.array_list.ArrayListAligned(u8,null).addOne (tls_allocators)
            try self.ensureTotalCapacity(newlen);
                                        ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/array_list.zig:161:49: 0x104970823 in std.array_list.ArrayListAligned(u8,null).append (tls_allocators)
            const new_item_ptr = try self.addOne();
                                                ^
/Users/a1liu/code/idk/repro/tls_allocators.zig:22:38: 0x1049707a7 in main (tls_allocators)
    try temporary_storage.data.append(1);
                                     ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/start.zig:561:37: 0x10497448f in std.start.callMain (tls_allocators)
            const result = root.main() catch |err| {
                                    ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/start.zig:495:12: 0x104970bc7 in std.start.callMainWithArgs (tls_allocators)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/opt/homebrew/Cellar/zig/0.9.1/lib/zig/std/start.zig:460:12: 0x104970af3 in std.start.main (tls_allocators)
    return @call(.{ .modifier = .always_inline }, callMainWithArgs, .{ @intCast(usize, c_argc), c_argv, envp });
           ^
???:?:?: 0x104dd5087 in ??? (???)
???:?:?: 0x42707fffffffffff in ??? (???)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavioros-macosmacOS

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions