Skip to content

Commit 89688bb

Browse files
author
Ellen Arteca
committed
no more yolo pointer typing
1 parent 94dacfd commit 89688bb

File tree

1 file changed

+63
-25
lines changed

1 file changed

+63
-25
lines changed

src/shims/ffi_support.rs

Lines changed: 63 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,10 @@ use rustc_target::abi::HasDataLayout;
77

88
use crate::*;
99

10-
// pub trait HasUnderlyingPointer {
11-
// fn get_underlying_raw_ptr(&self) -> *const u8;
12-
// }
13-
14-
// impl HasUnderlyingPointer for Allocation {
15-
// fn get_underlying_raw_ptr(&self) -> *const u8 {
16-
17-
// }
18-
// }
19-
2010
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
2111

2212
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
2313

24-
// fn get_allocation_for_id(&self, id: AllocId) -> InterpResult<'tcx, &Allocation<Provenance, AllocExtra>> {
25-
// let this = self.eval_context_ref();
26-
// this.get_alloc_raw(id)
27-
// }
28-
2914
/// Extract the scalar value from the result of reading a scalar from the machine,
3015
/// and convert it to a `CArg`.
3116
fn scalar_to_carg(
@@ -69,16 +54,71 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6954
return Ok(CArg::USize(k.to_machine_usize(cx)?.try_into().unwrap()));
7055
}
7156
// pointers
72-
TyKind::RawPtr(TypeAndMut{..} ) => {
57+
TyKind::RawPtr(TypeAndMut{ ty: some_ty, mutbl: some_mut } ) => {
7358
match k {
74-
ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, sz)) => {
59+
ScalarMaybeUninit::Scalar(Scalar::Ptr(ptr, _)) => {
7560
let qq = ptr.into_parts().1.bytes_usize();
76-
// TODO select correct types rather than yoloing
77-
return Ok(CArg::RecConstPtrCarg(Box::new(CArg::ConstPtrInt32(qq as *const i32))))
61+
match (some_ty.kind(), some_mut) {
62+
// int
63+
(TyKind::Int(IntTy::I8), rustc_hir::Mutability::Mut) => {
64+
return Ok(CArg::MutPtrInt8(qq as *mut i8));
65+
},
66+
(TyKind::Int(IntTy::I8), rustc_hir::Mutability::Not) => {
67+
return Ok(CArg::ConstPtrInt8(qq as *const i8));
68+
},
69+
(TyKind::Int(IntTy::I16), rustc_hir::Mutability::Mut) => {
70+
return Ok(CArg::MutPtrInt16(qq as *mut i16));
71+
},
72+
(TyKind::Int(IntTy::I16), rustc_hir::Mutability::Not) => {
73+
return Ok(CArg::ConstPtrInt16(qq as *const i16));
74+
},
75+
(TyKind::Int(IntTy::I32), rustc_hir::Mutability::Mut) => {
76+
return Ok(CArg::MutPtrInt32(qq as *mut i32));
77+
},
78+
(TyKind::Int(IntTy::I32), rustc_hir::Mutability::Not) => {
79+
return Ok(CArg::ConstPtrInt32(qq as *const i32));
80+
},
81+
(TyKind::Int(IntTy::I64), rustc_hir::Mutability::Mut) => {
82+
return Ok(CArg::MutPtrInt64(qq as *mut i64));
83+
},
84+
(TyKind::Int(IntTy::I64), rustc_hir::Mutability::Not) => {
85+
return Ok(CArg::ConstPtrInt64(qq as *const i64));
86+
},
87+
// uints
88+
(TyKind::Uint(UintTy::U8), rustc_hir::Mutability::Mut) => {
89+
return Ok(CArg::MutPtrUInt8(qq as *mut u8));
90+
},
91+
(TyKind::Uint(UintTy::U8), rustc_hir::Mutability::Not) => {
92+
return Ok(CArg::ConstPtrUInt8(qq as *const u8));
93+
},
94+
(TyKind::Uint(UintTy::U16), rustc_hir::Mutability::Mut) => {
95+
return Ok(CArg::MutPtrUInt16(qq as *mut u16));
96+
},
97+
(TyKind::Uint(UintTy::U16), rustc_hir::Mutability::Not) => {
98+
return Ok(CArg::ConstPtrUInt16(qq as *const u16));
99+
},
100+
(TyKind::Uint(UintTy::U32), rustc_hir::Mutability::Mut) => {
101+
return Ok(CArg::MutPtrUInt32(qq as *mut u32));
102+
},
103+
(TyKind::Uint(UintTy::U32), rustc_hir::Mutability::Not) => {
104+
return Ok(CArg::ConstPtrUInt32(qq as *const u32));
105+
},
106+
(TyKind::Uint(UintTy::U64), rustc_hir::Mutability::Mut) => {
107+
return Ok(CArg::MutPtrUInt64(qq as *mut u64));
108+
},
109+
(TyKind::Uint(UintTy::U64), rustc_hir::Mutability::Not) => {
110+
return Ok(CArg::ConstPtrUInt64(qq as *const u64));
111+
},
112+
// recursive case
113+
(TyKind::RawPtr(..), _) => {
114+
return Ok(CArg::RecCarg(Box::new(Self::scalar_to_carg(k, some_ty, cx)?)));
115+
}
116+
_ => {}
117+
}
78118
}
79119
_ => {}
80120
}
81-
}
121+
},
82122
_ => {}
83123
}
84124
// If no primitives were returned then we have an unsupported type.
@@ -321,9 +361,8 @@ pub enum CArg {
321361
ConstPtrUInt16(*const u16),
322362
ConstPtrUInt32(*const u32),
323363
ConstPtrUInt64(*const u64),
324-
// recursive
325-
RecConstPtrCarg(Box<CArg>),
326-
RecMutPtrCarg(Box<CArg>),
364+
/// Recursive `CArg` (for nested pointers).
365+
RecCarg(Box<CArg>),
327366
}
328367

329368
impl<'a> CArg {
@@ -356,8 +395,7 @@ impl<'a> CArg {
356395
CArg::ConstPtrUInt16(i) => arg(i),
357396
CArg::ConstPtrUInt32(i) => arg(i),
358397
CArg::ConstPtrUInt64(i) => arg(i),
359-
CArg::RecConstPtrCarg(box_carg) => (*box_carg).arg_downcast(),
360-
CArg::RecMutPtrCarg(box_carg) => (*box_carg).arg_downcast(),
398+
CArg::RecCarg(box_carg) => (*box_carg).arg_downcast(),
361399
}
362400
}
363401
}

0 commit comments

Comments
 (0)