Skip to content

Mark arguments of main as noundef - #166428

Open
mvanbem-goog wants to merge 1 commit into
rust-lang:mainfrom
mvanbem-goog:main-args-noundef
Open

mvanbem-goog wants to merge 1 commit into
rust-lang:mainfrom
mvanbem-goog:main-args-noundef

Conversation

@mvanbem-goog

Copy link
Copy Markdown

As far as I can tell, Clang applies the noundef attribute to the arguments of main because argc and argv are always initialized and have no forbidden bit patterns. The same would be true of any main function Rust generates, so we might as well apply it.

I don't have a deep understanding of the issue, but this seems to be needed to make MemorySanitizer work reliably with Google's internal aarch64 toolchain. Without the attribute, MemorySanitizer can instrument this main function to check whether the parameters were initialized, but the caller is an uninstrumented libc, so the shadow values it checks were not written and are stale. It's possible (and maybe even likely?) for other code linked into the binary like C++ static initializers to update those shadow values such that the checks at the beginning of main pass. Applying the attribute tells MSan to skip checking the parameters.

The checks for target.os being not UEFI and target.main_needs_argc_argv mirror the checks in create_entry_fn in compiler/rustc_codegen_ssa/src/base.rs.

I'm not sure that I've updated the 16bit-c-int test case correctly, since I don't have any compatible platforms on hand, but I suppose that's what a CI run is for.

LLM disclosure: As is strongly encouraged by my employer, I had an LLM agent debug the initially observed failure and draft a change. I reviewed the change and removed all LLM-generated prose; this is mine now. And I wrote the title and description.

As far as I can tell, Clang applies the `noundef` attribute to the arguments of `main` because `argc` and `argv` are always initialized and have no forbidden bit patterns. The same would be true of any `main` function Rust generates, so we might as well apply it.

I don't have a deep understanding of the issue, but this seems to be needed to make MemorySanitizer work reliably with Google's internal aarch64 toolchain. Without the attribute, MemorySanitizer can instrument this `main` function to check whether the parameters were initialized, but the caller is an uninstrumented libc, so the shadow values it checks were not written and are stale. It's possible (and maybe even likely?) for other code linked into the binary like C++ static initializers to update those shadow values such that the checks at the beginning of `main` pass. Applying the attribute tells MSan to skip checking the parameters.
@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 24, 2026
@rustbot

rustbot commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project has assigned @adwinwhite (or someone else) to review your changes, you should hear from them (or someone else) within the next two weeks.

Please see the contribution instructions and our LLM policy for more information.

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 79 candidates
  • Random selection from 21 candidates

@adwinwhite

Copy link
Copy Markdown
Contributor

@rustbot reroll

@rustbot rustbot assigned mati865 and unassigned adwinwhite Nov 24, 2026
@RalfJung

Copy link
Copy Markdown
Member

This seems to be an ad-hoc fix for one instance of #165040.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants