Skip to content

miri: enforce proper types for c-variadic arguments in shims - #161734

Merged
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
RalfJung:c-variadic-types
Sep 9, 2026
Merged

miri: enforce proper types for c-variadic arguments in shims#161734
rust-bors[bot] merged 2 commits into
rust-lang:mainfrom
RalfJung:c-variadic-types

Conversation

@RalfJung

@RalfJung RalfJung commented Aug 25, 2026

Copy link
Copy Markdown
Member

This adds proper type checks for c-variadic argument handling in Miri shims. So far we would have ICEd for many type mismatches since the innards of the interpreter generally assume that things are well-typed. The checks we are using are the same as for next_arg.

This uncovered some interesting points:

  • We should accept &mut as equivalent to *mut. We don't allow &mut as a type in next_arg but we do allow it for c-variadic calls, so this change also affects a bit more code in const-eval (if the caller passes an &mut and the callee expects a *mut). Even CFI is okay with treating &mut and *mut as equivalent so this should be fine. Cc @folkertdev
  • Some places in the standard library futex handling used types for their variadic arguments that are not quite correct according to these rules. I fixed those. Cc @m-ou-se
  • One variadic function takes pointers to libc::timespec. A program with a libc dependency has two copies of libc (one in the sysroot, and the normal dependency). When Miri looks up the expected argument type, it picks one of them, leading to Miri seeing a type mismatch when there is a call using the other copy. C generally considers two copies of the same type to be identical, so I implemented logic that does the same: two repr(C) types are "identical" if the names of the types, and the names and types of all fields, are identical. I then also used that logic for the normal ABI checks, where it will also help since we had similar trouble there in the past (that we had to find other work-arounds for).

@rustbot

rustbot commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

miri is developed in its own repository. If the Miri part of this change can be broken out, consider making this change to rust-lang/miri instead. However, if Miri needs adjusting for rustc changes, just ignore this message.

cc @rust-lang/miri

Some changes occurred to the CTFE machinery

cc @oli-obk, @lcnr

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri, @oli-obk, @lcnr

@rustbot rustbot added 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. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 25, 2026
@rustbot

rustbot commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

r? @mejrs

rustbot has assigned @mejrs.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler, mir
  • compiler, mir expanded to 75 candidates
  • Random selection from 16 candidates

@rustbot

This comment has been minimized.

@rustbot rustbot added has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 25, 2026
@RalfJung

Copy link
Copy Markdown
Member Author

r? @saethlin

@rustbot rustbot assigned saethlin and unassigned mejrs Aug 25, 2026
@RalfJung RalfJung changed the title C variadic types miri: enforce proper types for c-variadic arguments in shims Aug 25, 2026
@RalfJung
RalfJung force-pushed the c-variadic-types branch 2 times, most recently from 8cec627 to 888931c Compare August 25, 2026 10:17
@RalfJung

This comment was marked as resolved.

@RalfJung

Copy link
Copy Markdown
Member Author

@bors try jobs=aux

rust-bors Bot pushed a commit that referenced this pull request Aug 25, 2026
miri: enforce proper types for c-variadic arguments in shims


try-job: *aux*
@rust-bors

This comment has been minimized.

@rust-bors

rust-bors Bot commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

☀️ Try build successful (CI)
Build commit: 7980ab1 (7980ab1bb9a58e8bf9c45898865099a8d7564457)
Base parent: cc05892 (cc05892c8346313865afd91ca12ee1fde6d3603c)

@rustbot

This comment has been minimized.

@rustbot rustbot removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. has-merge-commits PR has merge commits, merge with caution. labels Aug 25, 2026
@rustbot

rustbot commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@RalfJung

RalfJung commented Sep 7, 2026

Copy link
Copy Markdown
Member Author

r? @oli-obk

@rustbot rustbot assigned oli-obk and unassigned saethlin Sep 7, 2026
@rustbot

rustbot commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

oli-obk is not on the review rotation at the moment.
They may take a while to respond.

@oli-obk

oli-obk commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@bors r+ rollup

@rust-bors

rust-bors Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 158f6f9 has been approved by oli-obk

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 9, 2026
Zalathar added a commit to Zalathar/rust that referenced this pull request Sep 9, 2026
miri: enforce proper types for c-variadic arguments in shims

This adds proper type checks for c-variadic argument handling in Miri shims. So far we would have ICEd for many type mismatches since the innards of the interpreter generally assume that things are well-typed. The checks we are using are the same as for [`next_arg`](https://doc.rust-lang.org/nightly/std/ffi/struct.VaList.html#method.next_arg).

This uncovered some interesting points:
-  We should accept `&mut` as equivalent to `*mut`. We don't allow `&mut` as a type in `next_arg` but we do allow it for c-variadic calls, so this change also affects a bit more code in const-eval (if the caller passes an `&mut` and the callee expects a `*mut`). Even CFI is okay with treating `&mut` and `*mut` as equivalent so this should be fine. Cc @folkertdev
- Some places in the standard library futex handling used types for their variadic arguments that are not quite correct according to these rules. I fixed those. Cc @m-ou-se
- One variadic function takes pointers to `libc::timespec`. A program with a libc dependency has two copies of libc (one in the sysroot, and the normal dependency). When Miri looks up the expected argument type, it picks one of them, leading to Miri seeing a type mismatch when there is a call using the other copy. C generally considers two copies of the same type to be identical, so I implemented logic that does the same: two `repr(C)` types are "identical" if the names of the types, and the names and types of all fields, are identical. I then also used that logic for the normal ABI checks, where it will also help since we had similar trouble there in the past (that we had to find other work-arounds for).
rust-bors Bot pushed a commit that referenced this pull request Sep 9, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - #162470 (Subtree sync for rustc_codegen_cranelift)
 - #161734 (miri: enforce proper types for c-variadic arguments in shims)
 - #161821 (Add missing option to `-Zself-profile-event` help message as well as information about what the default options are)
 - #162453 (Minimize `DiagCtxt` methods)
 - #162528 (Add a mention to docs about promoting and demoting platform support)
@rust-bors
rust-bors Bot merged commit 8b9c125 into rust-lang:main Sep 9, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 9, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 9, 2026
Rollup merge of #161734 - RalfJung:c-variadic-types, r=oli-obk

miri: enforce proper types for c-variadic arguments in shims

This adds proper type checks for c-variadic argument handling in Miri shims. So far we would have ICEd for many type mismatches since the innards of the interpreter generally assume that things are well-typed. The checks we are using are the same as for [`next_arg`](https://doc.rust-lang.org/nightly/std/ffi/struct.VaList.html#method.next_arg).

This uncovered some interesting points:
-  We should accept `&mut` as equivalent to `*mut`. We don't allow `&mut` as a type in `next_arg` but we do allow it for c-variadic calls, so this change also affects a bit more code in const-eval (if the caller passes an `&mut` and the callee expects a `*mut`). Even CFI is okay with treating `&mut` and `*mut` as equivalent so this should be fine. Cc @folkertdev
- Some places in the standard library futex handling used types for their variadic arguments that are not quite correct according to these rules. I fixed those. Cc @m-ou-se
- One variadic function takes pointers to `libc::timespec`. A program with a libc dependency has two copies of libc (one in the sysroot, and the normal dependency). When Miri looks up the expected argument type, it picks one of them, leading to Miri seeing a type mismatch when there is a call using the other copy. C generally considers two copies of the same type to be identical, so I implemented logic that does the same: two `repr(C)` types are "identical" if the names of the types, and the names and types of all fields, are identical. I then also used that logic for the normal ABI checks, where it will also help since we had similar trouble there in the past (that we had to find other work-arounds for).
pull Bot pushed a commit to xtqqczze/rust-lang-miri that referenced this pull request Sep 10, 2026
…uwer

Rollup of 5 pull requests

Successful merges:

 - rust-lang/rust#162470 (Subtree sync for rustc_codegen_cranelift)
 - rust-lang/rust#161734 (miri: enforce proper types for c-variadic arguments in shims)
 - rust-lang/rust#161821 (Add missing option to `-Zself-profile-event` help message as well as information about what the default options are)
 - rust-lang/rust#162453 (Minimize `DiagCtxt` methods)
 - rust-lang/rust#162528 (Add a mention to docs about promoting and demoting platform support)
DRMacIver added a commit to hegeldev/hegel-rust that referenced this pull request Sep 10, 2026
The nightly Miri from 2026-09-09 (rust-lang/rust#161734) type-checks
c-variadic arguments in its shims, and rustix 1.1.4's libc-backend futex
wrapper passes the lock word as `*const AtomicU32` where the `syscall`
shim expects `*mut u32`. That fails the `c_abi_miri` test whenever an
engine mutex is contended. Real Linux builds keep the futex path; Miri
takes the yield-based park that every other Unix already uses.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library 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