rust-lang / rust-lang/rust

`thread::current` may indirectly call `Arc::new_uninit_in(System)` which may call an untrusted alloc error hook

Open
#160,794 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-allocators A-global-allocator-reentry A-thread C-bug I-unsound needs-triage requires-nightly T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Related to #160793, but a platform-independent reentrancy issue with alloc_error_hook that potentially impacts more code.

The implementation of thread::current may require allocating a new Thread object.

Because GlobalAlloc guarantees that std::thread::current will not invoke the global allocator reentrantly, Thread uses an Arc<_, System> internally. However, the call to Arc::new_uninit_in(System) may involve a call to handle_alloc_error. Using feature(alloc_error_hook), we can thus call the global allocator reentrantly from thread::current. In particular, a hook just calling panic! (or even just resume_unwind) does so.

I can't give a demo, because it would require getting System to fail at the right moment.

Relevant code from std:
https://github.com/rust-lang/rust/blob/4667d75565e47ba5df36c0df598c556b543e8624/library/std/src/thread/current.rs#L289-L301
https://github.com/rust-lang/rust/blob/4667d75565e47ba5df36c0df598c556b543e8624/library/std/src/thread/thread.rs#L92-L102

@rustbot label T-libs A-thread requires-nightly A-allocators I-unsound A-global-allocator-reentry

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read library/std/src/thread/current.rs around lines 289-301 and library/std/src/thread/thread.rs around lines 92-102, then trace Arc::new_uninit_in(System) through allocation failure handling. Confirm whether thread::current can invoke an alloc_error_hook reentrantly and identify a design that preserves the GlobalAlloc guarantee; done means the reentrancy issue is resolved and covered by an appropriate regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
operating-systems
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.