Skip to content

Copy byval argument to local stackslot if alignment is insufficient#1641

Open
0xmuon wants to merge 3 commits intorust-lang:mainfrom
0xmuon:fix1
Open

Copy byval argument to local stackslot if alignment is insufficient#1641
0xmuon wants to merge 3 commits intorust-lang:mainfrom
0xmuon:fix1

Conversation

@0xmuon
Copy link
Copy Markdown

@0xmuon 0xmuon commented Apr 13, 2026

fix: #1465

Copy the underaligned byval (indirect) arguments into a local stackslot when the incoming pointer alignment is less than the Rust ABI alignment. This avoids miscompiles from assuming stronger alignment than the ABI guarantees.

Comment thread src/abi/mod.rs Outdated
Comment thread src/abi/mod.rs
Comment thread src/abi/pass_mode.rs
Comment on lines +332 to +333
let len = ty::Const::from_target_usize(fx.tcx, arg_abi.layout.size.bytes());
let bytes_ty = fx.tcx.mk_ty_from_kind(ty::Array(fx.tcx.types.u8, len));
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can use Ty::new_array().

View changes since the review

Comment thread src/abi/mod.rs

pub(super) struct ArgValue<'tcx> {
pub(super) value: Option<CValue<'tcx>>,
pub(super) underaligned_pointee_align: Option<Align>,
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can now be a bool is_underaligned_pointee.

View changes since the review

Comment thread src/abi/mod.rs
Comment on lines +252 to +253
Normal(ArgValue<'tcx>),
Spread(Vec<ArgValue<'tcx>>),
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe pull out the Option around the field ty and have:

Suggested change
Normal(ArgValue<'tcx>),
Spread(Vec<ArgValue<'tcx>>),
Normal(Option<ArgValue<'tcx>>),
Spread(Vec<Option<ArgValue<'tcx>>>),

? The align field is unused when the argument is not passed at all. I don't know how much that would complicate cvalue_for_param though.

View changes since the review

Comment thread src/abi/mod.rs
let arg_abi = arg_abis_iter.next().unwrap();
fx.caller_location =
Some(cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter).unwrap());
let arg = cvalue_for_param(fx, None, None, arg_abi, &mut block_params_iter);
Copy link
Copy Markdown
Member

@bjorn3 bjorn3 Apr 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe assert that arg is not underaligned?

View changes since the review

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Copy byval argument to local stackslot if alignment is insufficient

2 participants