Skip to content

Commit c841ffb

Browse files
authored
Rollup merge of #164796 - RalfJung:variadic-ice, r=oli-obk
miri: fix ICE when caller signature is c-variadic but callee is not It's entirely unnecessary to pass the `extra_args` to the *callee* signature. We only need them on the caller side. This also simplifies c-variadic passing a bit. Fixes rust-lang/miri#5465 r? @oli-obk
2 parents 3be2d85 + 7594d47 commit c841ffb

12 files changed

Lines changed: 47 additions & 30 deletions

‎compiler/rustc_const_eval/src/interpret/call.rs‎

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -478,17 +478,11 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
478478
let caller_location = with_caller_location.then(|| self.caller_location());
479479

480480
// The first order of business is to figure out the callee signature.
481-
// However, that requires the list of variadic arguments.
482-
// We use the *caller* information to determine where to split the list of arguments,
483-
// and then later check that the callee indeed has the same number of fixed arguments.
484-
let extra_tys = if caller_fn_abi.c_variadic {
485-
let fixed_count = usize::try_from(caller_fn_abi.fixed_count).unwrap();
486-
let extra_tys = args[fixed_count..].iter().map(|arg| arg.layout().ty);
487-
self.tcx.mk_type_list_from_iter(extra_tys)
488-
} else {
489-
ty::List::empty()
490-
};
491-
let callee_fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, extra_tys)?;
481+
// We leave the list of extra c-variadic args empty because the callee cannot know about
482+
// them, and we don't need them in the `FnAbi`. This also avoids ICEs if the callee is
483+
// not actually c-variadic.
484+
let extra_args = ty::List::empty();
485+
let callee_fn_abi = self.fn_abi_of_instance_no_deduced_attrs(instance, extra_args)?;
492486

493487
if caller_fn_abi.conv != callee_fn_abi.conv {
494488
throw_ub_format!(
@@ -641,11 +635,13 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
641635
ecx.eval_place(dest, /* skip_validity_for_simple_deref */ false)?;
642636
let mplace = ecx.force_allocation(&place)?;
643637

638+
// There should be no more callee ABI arguments.
639+
assert!(callee_args_abis.next().is_none());
644640
// Consume the remaining arguments by putting them into the variable argument
645641
// list. We disable retagging to avoid creating protected tags. Protection should
646642
// only use callee-side information, and the varargs have no static callee-side type.
647643
let varargs = M::with_retag_mode(ecx, RetagMode::None, |ecx| {
648-
ecx.allocate_varargs(&mut caller_args, &mut callee_args_abis)
644+
ecx.allocate_varargs(&mut caller_args)
649645
})?;
650646

651647
// When the frame is dropped, these variable arguments are deallocated.

‎compiler/rustc_const_eval/src/interpret/stack.rs‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -656,30 +656,23 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
656656
impl<'a, 'tcx: 'a, M: Machine<'tcx>> InterpCx<'tcx, M> {
657657
/// Consume the arguments provided by the iterator and store them as a list
658658
/// of variadic arguments. Return a list of the places that hold those arguments.
659-
pub(crate) fn allocate_varargs<I, J>(
659+
pub(crate) fn allocate_varargs<I>(
660660
&mut self,
661661
caller_args: I,
662-
mut callee_abis: J,
663662
) -> InterpResult<'tcx, Vec<MPlaceTy<'tcx, M::Provenance>>>
664663
where
665664
I: Iterator<Item = (&'a FnArg<'tcx, M::Provenance>, &'a ArgAbi<'tcx, Ty<'tcx>>)>,
666-
J: Iterator<Item = (usize, &'a ArgAbi<'tcx, Ty<'tcx>>)>,
667665
{
668666
// Consume the remaining arguments and store them in fresh allocations.
669667
let mut varargs = Vec::new();
670-
for (fn_arg, caller_abi) in caller_args {
671-
// The callee ABI is entirely computed based on which arguments the caller has
672-
// provided so it should not be possible to get a mismatch here.
673-
let (_idx, callee_abi) = callee_abis.next().unwrap();
674-
assert!(self.check_argument_compat(caller_abi, callee_abi)?);
668+
for (fn_arg, _caller_abi) in caller_args {
675669
// FIXME: do we have to worry about in-place argument passing?
676670
let op = fn_arg.copy_fn_arg();
677671
let mplace = self.allocate(op.layout, MemoryKind::Stack)?;
678672
self.copy_op(&op, &mplace)?;
679673

680674
varargs.push(mplace);
681675
}
682-
assert!(callee_abis.next().is_none());
683676

684677
interp_ok(varargs)
685678
}

src/tools/miri/tests/fail/c-variadic-ignored-argument.rs renamed to src/tools/miri/tests/fail/function_calls/c-variadic-ignored-argument.rs

File renamed without changes.

src/tools/miri/tests/fail/c-variadic-ignored-argument.stderr renamed to src/tools/miri/tests/fail/function_calls/c-variadic-ignored-argument.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Undefined Behavior: va_arg type mismatch: requested `i32` is incompatible with next argument of type `()`
2-
--> tests/fail/c-variadic-ignored-argument.rs:LL:CC
2+
--> tests/fail/function_calls/c-variadic-ignored-argument.rs:LL:CC
33
|
44
LL | ap.next_arg::<i32>();
55
| ^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
@@ -8,9 +8,9 @@ LL | ap.next_arg::<i32>();
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
= note: stack backtrace:
1010
0: main::variadic
11-
at tests/fail/c-variadic-ignored-argument.rs:LL:CC
11+
at tests/fail/function_calls/c-variadic-ignored-argument.rs:LL:CC
1212
1: main
13-
at tests/fail/c-variadic-ignored-argument.rs:LL:CC
13+
at tests/fail/function_calls/c-variadic-ignored-argument.rs:LL:CC
1414

1515
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
1616

src/tools/miri/tests/fail/c-variadic-mismatch-count.rs renamed to src/tools/miri/tests/fail/function_calls/c-variadic-mismatch-count.rs

File renamed without changes.

src/tools/miri/tests/fail/c-variadic-mismatch-count.stderr renamed to src/tools/miri/tests/fail/function_calls/c-variadic-mismatch-count.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Undefined Behavior: calling a C-variadic function with 0 fixed arguments, but the function expects 1
2-
--> tests/fail/c-variadic-mismatch-count.rs:LL:CC
2+
--> tests/fail/function_calls/c-variadic-mismatch-count.rs:LL:CC
33
|
44
LL | f(1);
55
| ^^^^ Undefined Behavior occurred here

src/tools/miri/tests/fail/c-variadic-mismatch.rs renamed to src/tools/miri/tests/fail/function_calls/c-variadic-mismatch.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Callee is variadic, caller not.
12
unsafe extern "C" fn helper(_: i32, _: ...) {}
23

34
fn main() {

src/tools/miri/tests/fail/c-variadic-mismatch.stderr renamed to src/tools/miri/tests/fail/function_calls/c-variadic-mismatch.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: Undefined Behavior: calling a function where the caller and callee disagree on whether the function is C-variadic
2-
--> tests/fail/c-variadic-mismatch.rs:LL:CC
2+
--> tests/fail/function_calls/c-variadic-mismatch.rs:LL:CC
33
|
44
LL | f(1i32, 1i64);
55
| ^^^^^^^^^^^^^ Undefined Behavior occurred here
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Caller is variadic, callee not.
2+
mod module {
3+
#[unsafe(no_mangle)]
4+
extern "C" fn weird() {}
5+
}
6+
7+
unsafe extern "C" {
8+
fn weird(_: ...);
9+
}
10+
11+
fn main() {
12+
unsafe { weird(1_i32) };
13+
//~^ ERROR: Undefined Behavior: calling a function where the caller and callee disagree on whether the function is C-variadic
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
error: Undefined Behavior: calling a function where the caller and callee disagree on whether the function is C-variadic
2+
--> tests/fail/function_calls/c-variadic-mismatch2.rs:LL:CC
3+
|
4+
LL | unsafe { weird(1_i32) };
5+
| ^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
10+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
11+
12+
error: aborting due to 1 previous error
13+

0 commit comments

Comments
 (0)