Skip to content

Unsoundness when a panic Rust code is caught by separetely compiled Rust code through FFI-unwind #102715

Closed
@nbdd0121

Description

@nbdd0121

Rust code might be able to catch foreign Rust code through FFI unwind.

a.rs:

#![crate_type = "cdylib"]
#![feature(c_unwind)]

#[no_mangle]
extern "C-unwind" fn panic() {
    panic!();
}

b.rs:

#![feature(c_unwind)]

#[link(name = "a")]
extern "C-unwind" {
    fn panic();
}

fn main() {
    let err = std::panic::catch_unwind(|| {
        unsafe { panic() };
    });
    match err {
        Err(v) => {
            // Able to access `Box<dyn Any>` generated by another
            // compiler; we can't guarantee that typeid does not conflict
            // across Rust versions, nor that the vtable format is
            // stable.
			// EDIT: Also this will result a `Box` allocated in one allocator
			// from being deallocated in another, which is more obviously unsound.
        }
        _ => (),
    }
}

These two crates could be compiled with different Rust versions, or same version with different flags (e.g. struct layout randomisation), and this will create unsoundness because we couldn't guarantee the ABI for separate compilations.

Currently we just use the exception class in the unwind runtime ("MOZ\0RUST") to tell apart Rust exceptions from foreign exceptions, but for soundness we need to treat Rust exception from another compilation as foreign exception as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-FFIArea: Foreign function interface (FFI)A-runtimeArea: std's runtime and "pre-main" init for handling backtraces, unwinds, stack overflowsC-bugCategory: This is a bug.F-c_unwind`#![feature(c_unwind)]`I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessWG-ffi-unwindWorking group: FFI unwindrequires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions