Skip to content

Commit 1fc77f1

Browse files
committed
changes after code review
1 parent 5afb38e commit 1fc77f1

5 files changed

Lines changed: 20 additions & 19 deletions

File tree

compiler/rustc_const_eval/src/interpret/call.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
371371

372372
(fixed_count, self.tcx.mk_type_list_from_iter(extra_tys))
373373
} else {
374-
(caller_fn_abi.args.len(), ty::List::empty())
374+
(caller_fn_abi.fixed_count.try_into().unwrap(), ty::List::empty())
375375
};
376376

377377
let callee_fn_abi = self.fn_abi_of_instance(instance, callee_c_variadic_args)?;
@@ -393,7 +393,7 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
393393
});
394394
}
395395

396-
if caller_fn_abi.fixed_count != callee_fn_abi.fixed_count {
396+
if caller_fn_abi.c_variadic && caller_fn_abi.fixed_count != callee_fn_abi.fixed_count {
397397
throw_ub!(CVariadicFixedCountMismatch {
398398
caller: caller_fn_abi.fixed_count,
399399
callee: callee_fn_abi.fixed_count,
@@ -472,12 +472,16 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
472472
// `pass_argument` would be the loop body. It takes care to
473473
// not advance `caller_iter` for ignored arguments.
474474
let mut callee_args_abis = if caller_fn_abi.c_variadic {
475-
// Only the fixed arguments are passed normally.
475+
// Only the fixed arguments are passed normally. C-variadic functions cannot be
476+
// `extern "Rust"` and `#[track_caller]` can only be applied to `extern "Rust"`, to
477+
// the extra caller location argument is not relevant here.
478+
assert!(!instance.def.requires_caller_location(*self.tcx));
476479
callee_fn_abi.args[..fixed_count].iter().enumerate()
477480
} else {
478-
// NOTE: this handles the extra caller location argument
479-
// when `#[track_caller]` is used. This attribute is only allowed on `extern "Rust"`
480-
// functions, so the c-variadic case does not need to handle the extra argument.
481+
// NOTE: this handles the extra caller location argument that is passed when
482+
// `#[track_caller]` is used. The `fixed_count` does not account for this argument.
483+
// This attribute is only allowed on `extern "Rust"` functions, so the c-variadic
484+
// case does not need to handle the extra argument.
481485
callee_fn_abi.args.iter().enumerate()
482486
};
483487

@@ -501,7 +505,9 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
501505
// Consume the remaining arguments by putting them into the variable argument
502506
// list.
503507
let varargs = self.allocate_varargs(&mut caller_args)?;
504-
let key = self.va_list_ptr(varargs);
508+
// When the frame is dropped, these variable arguments are deallocated.
509+
self.frame_mut().va_list = varargs.clone();
510+
let key = self.va_list_ptr(varargs.into());
505511

506512
// Zero the VaList, so it is fully initialized.
507513
self.write_bytes_ptr(

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,13 +1287,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
12871287
let va_list_inner = self.project_field(va_list, FieldIdx::ZERO)?;
12881288

12891289
// Find the first pointer field in this struct. The exact index is target-specific.
1290-
let ty::Adt(adt, _substs) = va_list_inner.layout().ty.kind() else {
1290+
let ty::Adt(adt, substs) = va_list_inner.layout().ty.kind() else {
12911291
bug!("invalid VaListImpl layout");
12921292
};
12931293

12941294
for (i, field) in adt.non_enum_variant().fields.iter().enumerate() {
1295-
let field_ty = self.tcx.type_of(field.did);
1296-
if field_ty.skip_binder().is_raw_ptr() {
1295+
if field.ty(*self.tcx, substs).is_raw_ptr() {
12971296
return self.project_field(&va_list_inner, FieldIdx::from_usize(i));
12981297
}
12991298
}

compiler/rustc_const_eval/src/interpret/stack.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Manages the low-level pushing and popping of stack frames and the (de)allocation of local variables.
22
//! For handling of argument passing and return values, see the `call` module.
33
use std::cell::Cell;
4-
use std::collections::VecDeque;
54
use std::{fmt, mem};
65

76
use either::{Either, Left, Right};
@@ -640,7 +639,7 @@ impl<'a, 'tcx: 'a, M: Machine<'tcx>> InterpCx<'tcx, M> {
640639
pub(crate) fn allocate_varargs<I>(
641640
&mut self,
642641
caller_args: &mut I,
643-
) -> InterpResult<'tcx, VecDeque<MPlaceTy<'tcx, M::Provenance>>>
642+
) -> InterpResult<'tcx, Vec<MPlaceTy<'tcx, M::Provenance>>>
644643
where
645644
I: Iterator<Item = (&'a FnArg<'tcx, M::Provenance>, &'a ArgAbi<'tcx, Ty<'tcx>>)>,
646645
{
@@ -655,10 +654,7 @@ impl<'a, 'tcx: 'a, M: Machine<'tcx>> InterpCx<'tcx, M> {
655654
varargs.push(mplace);
656655
}
657656

658-
// When the frame is dropped, these variable arguments are deallocated.
659-
self.frame_mut().va_list = varargs.clone();
660-
661-
interp_ok(varargs.into())
657+
interp_ok(varargs)
662658
}
663659

664660
fn deallocate_varargs(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn main() {
77
let f = helper as *const ();
88
let f = std::mem::transmute::<_, unsafe extern "C" fn(...)>(f);
99

10-
f();
10+
f(1);
1111
//~^ ERROR: Undefined Behavior
1212
}
1313
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error: Undefined Behavior: calling a C-variadic function with 0 fixed arguments, but the function expects 1
22
--> tests/fail/c-variadic-mismatch-count.rs:LL:CC
33
|
4-
LL | f();
5-
| ^^^ Undefined Behavior occurred here
4+
LL | f(1);
5+
| ^^^^ Undefined Behavior occurred here
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)