rust-lang / rust-lang/rust

null symbols cause unsoundness with extern static

Open
#139,128 12 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs C-bug T-compiler T-lang T-spec
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Pointers to external statics are incorrectly assumed to be non-null and well-aligned. Edition 2024 marking extern as unsafe does help, but there is no documentation mentioning this, and this still works in 2021 despite forbidding unsafe_code.

#![forbid(unsafe_code)]

extern "C" {
    static mut GLIBC_PRIVATE: u8;
    static perror: u64;
}

fn main() {
    let x = std::ptr::NonNull::new(&raw mut GLIBC_PRIVATE).unwrap();
    println!("{x:?}"); // 0x0

    let y = &raw const perror;
    println!("{y:?} {}", y.is_aligned()); // 0x7c5aa9e5ea93 true
}

https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=4d1319d8ff14e454c3e391474ef299e6

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

Start by reproducing the linked Rust Playground example in release mode under Edition 2021, then compare the behavior with Edition 2024 and #![forbid(unsafe_code)]. The report names no source files or tests, and completion is not defined; maintainers first need to decide whether the fix belongs in compiler behavior, language rules, or documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
28/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.