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

Miri detects UB (SB error) when setting a thread name #1957

Closed
saethlin opened this issue Jan 4, 2022 · 5 comments
Closed

Miri detects UB (SB error) when setting a thread name #1957

saethlin opened this issue Jan 4, 2022 · 5 comments
Labels
A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows)

Comments

@saethlin
Copy link
Member

saethlin commented Jan 4, 2022

I tried this code:

fn main() {
    std::thread::Builder::new()
        .name("name".to_string())
        .spawn(|| {})
        .unwrap()
        .join()
        .unwrap();
}

MIRIFLAGS="-Zmiri-tag-raw-pointers" cargo miri run

I expected to see this happen: Nothing

Instead, this happened: Miri is upset 😢

error: Undefined Behavior: no item granting read access to tag <untagged> at alloc1582 found in borrow stack.
   --> /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/unix/thread.rs:125:13
    |
125 |             libc::prctl(PR_SET_NAME, name.as_ptr() as libc::c_ulong, 0, 0, 0);
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no item granting read access to tag <untagged> at alloc1582 found in borrow stack.
    |
    = help: this indicates a potential bug in the program: it performed an invalid operation, but the rules it violated are still experimental
    = help: see https:/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
            
    = note: inside `std::sys::unix::thread::Thread::set_name` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/unix/thread.rs:125:13
    = note: inside closure at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/thread/mod.rs:467:17
    = note: inside `<[closure@std::thread::Builder::spawn_unchecked<[closure@src/main.rs:4:16: 4:21], ()>::{closure#1}] as std::ops::FnOnce<()>>::call_once - shim(vtable)` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
    = note: inside `<std::boxed::Box<dyn std::ops::FnOnce()> as std::ops::FnOnce<()>>::call_once` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1811:9
    = note: inside `<std::boxed::Box<std::boxed::Box<dyn std::ops::FnOnce()>> as std::ops::FnOnce<()>>::call_once` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1811:9
    = note: inside `std::sys::unix::thread::Thread::new::thread_start` at /home/ben/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/sys/unix/thread.rs:108:17

I truly have no idea what's going on here. I've tried a number of things with moving data or pointers to data into threads, and none of them can trip up the Stacked Borrows tracking. cc @RalfJung ?

@5225225
Copy link
Contributor

5225225 commented Jan 4, 2022

I vaguely remember a previous issue where pointer -> int casts tripped up stacked borrows when used in a syscall like that.

I'd need to go find it, but IIRC the (proposed, i don't think tested) solution was to transmute if cfg!(miri)?

@RalfJung
Copy link
Member

RalfJung commented Jan 4, 2022

Yeah, the <untagged> is the smoking gun here -- good catch.

Int-to-ptr casts do not work with -Zmiri-tag-raw-pointers.

@saethlin
Copy link
Member Author

saethlin commented Jan 4, 2022

Wow that's awkward. So we're backed into a corner here by the combination of this miri peculiarity and the signature of this libc function, because it is trying to be generic.

Is this something better fixed in miri?

@RalfJung RalfJung transferred this issue from rust-lang/rust Jan 4, 2022
@RalfJung
Copy link
Member

RalfJung commented Jan 4, 2022

It seems at least better to track this in Miri, so I moved the issue.

@RalfJung RalfJung added the A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows) label Jan 4, 2022
@RalfJung
Copy link
Member

RalfJung commented Jan 4, 2022

Actually, we already have this issue on the Miri side: #1717

@RalfJung RalfJung closed this as completed Jan 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-aliasing Area: This affects the aliasing model (Stacked/Tree Borrows)
Projects
None yet
Development

No branches or pull requests

3 participants