Skip to content

Commit 6006ef8

Browse files
committed
init previous work on passing pointers through miri FFI (causes ice)
1 parent 748c548 commit 6006ef8

File tree

6 files changed

+46
-1
lines changed

6 files changed

+46
-1
lines changed

src/tools/miri/src/alloc_addresses/mod.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,28 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
166166
assert!(!matches!(kind, AllocKind::Dead));
167167

168168
// This allocation does not have a base address yet, pick or reuse one.
169-
let base_addr = if let Some((reuse_addr, clock)) = global_state.reuse.take_addr(
169+
let base_addr = if ecx.machine.native_lib.is_some() { // V1: memory.rs:212 InterpError(UseAfterFree...
170+
// let base_addr = if ecx.machine.native_lib.is_some() && kind != AllocKind::Function { // V2: /home/lwerner/.rustup/toolchains/miri/lib/rustlib/src/rust/library/std/src/rt.rs:159:9 UB???...
171+
// let base_addr = if ecx.machine.native_lib.is_some() && kind == AllocKind::LiveData { // V3: alloc_addresses/mod.rs:151 BorrowMutError...
172+
// Work with the real underlying machine address directly TODO https://doc.rust-lang.org/std/primitive.pointer.html
173+
match kind {
174+
AllocKind::LiveData => {
175+
let the = ecx.get_alloc_bytes_unchecked_raw(alloc_id);
176+
dbg!((1, &alloc_id, &kind, &the)); // TODO
177+
let the2 = the?.addr().try_into().unwrap();
178+
dbg!((2, &the2)); // TODO
179+
the2
180+
}
181+
AllocKind::Function | AllocKind::VTable => {
182+
let alloc_bytes = MiriAllocBytes::from_bytes(&[0u8; 1], Align::from_bytes(1).unwrap());
183+
let addr = alloc_bytes.as_ptr().addr().try_into().unwrap();
184+
// Leak this.
185+
std::mem::forget(alloc_bytes);
186+
addr
187+
}
188+
AllocKind::Dead => unreachable!()
189+
}
190+
} else if let Some((reuse_addr, clock)) = global_state.reuse.take_addr(
170191
&mut *rng,
171192
size,
172193
align,

src/tools/miri/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#![feature(let_chains)]
1313
#![feature(trait_upcasting)]
1414
#![feature(strict_overflow_ops)]
15+
#![feature(strict_provenance)]
1516
// Configure clippy and other lints
1617
#![allow(
1718
clippy::collapsible_else_if,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ enum CArg {
194194
UInt64(u64),
195195
/// usize.
196196
USize(usize),
197+
/// Raw pointer, stored as C's `void*`.
198+
RawPtr(*mut std::ffi::c_void),
197199
}
198200

199201
impl<'a> CArg {
@@ -210,6 +212,7 @@ impl<'a> CArg {
210212
CArg::UInt32(i) => ffi::arg(i),
211213
CArg::UInt64(i) => ffi::arg(i),
212214
CArg::USize(i) => ffi::arg(i),
215+
CArg::RawPtr(i) => ffi::arg(i),
213216
}
214217
}
215218
}
@@ -234,6 +237,10 @@ fn imm_to_carg<'tcx>(v: ImmTy<'tcx>, cx: &impl HasDataLayout) -> InterpResult<'t
234237
ty::Uint(UintTy::U64) => CArg::UInt64(v.to_scalar().to_u64()?),
235238
ty::Uint(UintTy::Usize) =>
236239
CArg::USize(v.to_scalar().to_target_usize(cx)?.try_into().unwrap()),
240+
ty::RawPtr(..) => {
241+
let s = v.to_scalar().to_pointer(cx)?.addr();
242+
CArg::RawPtr(s.bytes() as *mut std::ffi::c_void) // TODO
243+
},
237244
_ => throw_unsup_format!("unsupported argument type for native call: {}", v.layout.ty),
238245
})
239246
}

src/tools/miri/tests/native-lib/libtest.map

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ CODEABI_1.0 {
33
global:
44
add_one_int;
55
printer;
6+
ptr_printer;
67
test_stack_spill;
78
get_unsigned_int;
89
add_int16;

src/tools/miri/tests/native-lib/pass/call_extern_c_fn.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ extern "C" {
2121
fn add_short_to_long(x: i16, y: i64) -> i64;
2222
fn get_unsigned_int() -> u32;
2323
fn printer();
24+
fn ptr_printer(ptr: *mut i32);
2425
}
2526

2627
fn main() {
@@ -42,5 +43,13 @@ fn main() {
4243

4344
// test void function that prints from C
4445
printer();
46+
47+
// test void function that dereferences a pointer and prints from C
48+
let mut x = 42i32;
49+
let ptr = &mut x as *mut i32;
50+
//println!("pointer address from Rust: {:?}", ptr);
51+
//println!("printing dereference from Rust: {}", *ptr);
52+
ptr_printer(ptr);
53+
4554
}
4655
}

src/tools/miri/tests/native-lib/test.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ void printer() {
88
printf("printing from C\n");
99
}
1010

11+
void ptr_printer(int *ptr) {
12+
printf("printing address from C: %p\n", ptr);
13+
// TODO: Get *this* to work.
14+
//printf("printing dereference from C: %d\n", *ptr);
15+
}
16+
1117
// function with many arguments, to test functionality when some args are stored
1218
// on the stack
1319
int test_stack_spill(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l) {

0 commit comments

Comments
 (0)