Skip to content

null symbols cause unsoundness with extern static #139128

Open
@jasondyoungberg

Description

@jasondyoungberg

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team, which will review and decide on the PR/issue.T-specRelevant to the spec team.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions