Skip to content

Commit 77208bc

Browse files
authored
Merge pull request #4626 from RalfJung/native-call-args
native-lib args: also reject wide pointers
2 parents 46c5f0c + 18a468e commit 77208bc

File tree

1 file changed

+14
-10
lines changed
  • src/tools/miri/src/shims/native_lib

1 file changed

+14
-10
lines changed

src/tools/miri/src/shims/native_lib/mod.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use std::sync::atomic::AtomicBool;
66
use libffi::low::CodePtr;
77
use libffi::middle::Type as FfiType;
88
use rustc_abi::{HasDataLayout, Size};
9-
use rustc_middle::ty::{self as ty, IntTy, Ty, UintTy};
9+
use rustc_middle::ty::layout::HasTypingEnv;
10+
use rustc_middle::ty::{self, IntTy, Ty, UintTy};
1011
use rustc_span::Symbol;
1112
use serde::{Deserialize, Serialize};
1213

@@ -373,15 +374,13 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
373374
adt_def: ty::AdtDef<'tcx>,
374375
args: &'tcx ty::List<ty::GenericArg<'tcx>>,
375376
) -> InterpResult<'tcx, FfiType> {
376-
// TODO: Certain non-C reprs should be okay also.
377-
if !adt_def.repr().c() {
378-
throw_unsup_format!("passing a non-#[repr(C)] struct over FFI: {orig_ty}")
379-
}
380377
// TODO: unions, etc.
381378
if !adt_def.is_struct() {
382-
throw_unsup_format!(
383-
"unsupported argument type for native call: {orig_ty} is an enum or union"
384-
);
379+
throw_unsup_format!("passing an enum or union over FFI: {orig_ty}");
380+
}
381+
// TODO: Certain non-C reprs should be okay also.
382+
if !adt_def.repr().c() {
383+
throw_unsup_format!("passing a non-#[repr(C)] {} over FFI: {orig_ty}", adt_def.descr())
385384
}
386385

387386
let this = self.eval_context_ref();
@@ -395,19 +394,24 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
395394

396395
/// Gets the matching libffi type for a given Ty.
397396
fn ty_to_ffitype(&self, ty: Ty<'tcx>) -> InterpResult<'tcx, FfiType> {
397+
let this = self.eval_context_ref();
398398
interp_ok(match ty.kind() {
399399
ty::Int(IntTy::I8) => FfiType::i8(),
400400
ty::Int(IntTy::I16) => FfiType::i16(),
401401
ty::Int(IntTy::I32) => FfiType::i32(),
402402
ty::Int(IntTy::I64) => FfiType::i64(),
403403
ty::Int(IntTy::Isize) => FfiType::isize(),
404-
// the uints
405404
ty::Uint(UintTy::U8) => FfiType::u8(),
406405
ty::Uint(UintTy::U16) => FfiType::u16(),
407406
ty::Uint(UintTy::U32) => FfiType::u32(),
408407
ty::Uint(UintTy::U64) => FfiType::u64(),
409408
ty::Uint(UintTy::Usize) => FfiType::usize(),
410-
ty::RawPtr(..) => FfiType::pointer(),
409+
ty::RawPtr(pointee_ty, _mut) => {
410+
if !pointee_ty.is_sized(*this.tcx, this.typing_env()) {
411+
throw_unsup_format!("passing a pointer to an unsized type over FFI: {}", ty);
412+
}
413+
FfiType::pointer()
414+
}
411415
ty::Adt(adt_def, args) => self.adt_to_ffitype(ty, *adt_def, args)?,
412416
_ => throw_unsup_format!("unsupported argument type for native call: {}", ty),
413417
})

0 commit comments

Comments
 (0)