Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add stack traces to pointer stability safety locks #19328

Open
Tracked by #17719
andrewrk opened this issue Mar 16, 2024 · 0 comments
Open
Tracked by #17719

add stack traces to pointer stability safety locks #19328

andrewrk opened this issue Mar 16, 2024 · 0 comments
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Milestone

Comments

@andrewrk
Copy link
Member

Extracted from #17719.

Based on this comment by @GethDW: #17719 (comment)

Comment reproduced here:

I think it would be good to add a ConfigurableTrace and panic with it to quickly identify where it was locked. I'm not too familiar with StackTrace but I played around with it and got:

pub const SafetyLock = struct {
    state: State = .unlocked,
    trace: std.debug.ConfigurableTrace(2, 1, runtime_safety) = .{},

    pub const State = if (runtime_safety) enum { unlocked, locked } else enum { unlocked };

    pub fn lock(l: *SafetyLock) void {
        if (!runtime_safety) return;
        l.trace.addAddr(@returnAddress(), "locked");
        if (l.state == .locked) l.panic("locked while already locked");
        l.state = .locked;
    }

    pub fn unlock(l: *SafetyLock) void {
        if (!runtime_safety) return;
        l.trace.addAddr(@returnAddress(), "unlock");
        if (l.state == .unlocked) l.panic("unlocked while already unlocked");
        l.trace = .{};
        l.state = .unlocked;
    }

    pub fn assertUnlocked(l: *SafetyLock, comptime msg: []const u8, ret_addr: usize) void {
        if (!runtime_safety) return;
        l.trace.addAddr(ret_addr, msg);
        if (l.state != .unlocked) l.panic(msg);
    }

    fn panic(l: SafetyLock, comptime msg: []const u8) noreturn {
        var insts = [2]usize{ l.trace.addrs[0][0], l.trace.addrs[1][0] };
        const stack_trace = std.builtin.StackTrace{
            .index = l.trace.index,
            .instruction_addresses = &insts,
        };
        std.debug.panicImpl(&stack_trace, null, msg);
    }
};

The trace can then look like this:

thread 343239 panic: modified while locked
/home/gethin/random/test/src/main.zig:53:14: 0x21d788 in main (test)
    lock.lock(); // locked
             ^
/home/gethin/random/test/src/main.zig:54:11: 0x21d791 in main (test)
    modify(&lock); // modified
...

This issue is to evaluate whether enabling this unconditionally in debug mode is prohibitively expensive in terms of performance and memory, or not. And depending on the results of this investigation, potentially create a std lib configuration option for setting the number of stack trace frames to collect for safety locks.

Related:

@andrewrk andrewrk added enhancement Solving this issue will likely involve adding new logic or components to the codebase. contributor friendly This issue is limited in scope and/or knowledge of Zig internals. standard library This issue involves writing Zig code for the standard library. labels Mar 16, 2024
@andrewrk andrewrk added this to the 0.15.0 milestone Mar 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly This issue is limited in scope and/or knowledge of Zig internals. enhancement Solving this issue will likely involve adding new logic or components to the codebase. standard library This issue involves writing Zig code for the standard library.
Projects
None yet
Development

No branches or pull requests

1 participant