Skip to content

Technically a soundness bug: test_main_static_abort clears the environment #158602

Description

@Manishearth

pub fn test_main_static_abort(tests: &[&TestDescAndFn]) {
// If we're being run in SpawnedSecondary mode, run the test here. run_test
// will then exit the process.
if let Ok(name) = env::var(SECONDARY_TEST_INVOKER_VAR) {
unsafe {
env::remove_var(SECONDARY_TEST_INVOKER_VAR);
}

https://doc.rust-lang.org/stable/std/env/fn.remove_var.html

AIUI since libtest is multithreaded, this is not a safe invocation of remove_var from the public safe test_main_static_abort function.

In practice, this doesn't matter: Rust libraries aren't manually calling this function, and if you can cause UB in a custom test harness I don't think it's that big a deal.

Filing for completeness' sake. It would probably make sense to at least document why this is okay here.

(This issue was found during an agentic safety audit using Gemini)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-libtestArea: `#[test]` / the `test` libraryC-bugCategory: This is a bug.T-libsRelevant to the library team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions