Skip to content

Better documentation needed: reentrancy through the panic hook #505

Open
@RalfJung

Description

@RalfJung

The following function may seem sound:

fn definitely_safe(b: bool) {
    static mut S: i32 = 0;
    
    struct Bomb<'a>(&'a mut i32);
    impl<'a> Drop for Bomb<'a> {
        fn drop(&mut self) {
            *self.0 = 42;
        }
    }
    
    // SAEFTY: we're not creating a second reference while
    // this one is live.
    let _bomb = Bomb(unsafe { &mut *ptr::addr_of_mut!(S) });
    if b {
        panic!("Oopsie, something went wrong!");
    }
}

But actually it is not. The panic can call arbitrary user-defined code via a panic hook, and that can do a reentrant call do definitely_safe, and then we have UB.

I don't think there is any way to say that definitely_safe is sound, so the main question is -- how to we raise awareness of this reentrancy issue so that unsafe code authors don't stumble into it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions