Skip to content

Commit 1fb91a7

Browse files
committed
add pointers being returned from C, add test_return_ptr
1 parent 9916c6a commit 1fb91a7

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ use std::ops::Deref;
33

44
use libffi::high::call as ffi;
55
use libffi::low::CodePtr;
6-
use rustc_abi::{BackendRepr, HasDataLayout};
7-
use rustc_middle::ty::{self as ty, IntTy, UintTy};
6+
use rustc_abi::{BackendRepr, HasDataLayout, Size};
7+
use rustc_middle::{
8+
mir::interpret::Pointer,
9+
ty::{self as ty, IntTy, UintTy},
10+
};
811
use rustc_span::Symbol;
912

1013
use crate::*;
@@ -75,6 +78,11 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
7578
unsafe { ffi::call::<()>(ptr, libffi_args.as_slice()) };
7679
return interp_ok(ImmTy::uninit(dest.layout));
7780
}
81+
ty::RawPtr(..) => {
82+
let x = unsafe { ffi::call::<*const ()>(ptr, libffi_args.as_slice()) };
83+
let ptr = Pointer::new(Provenance::Wildcard, Size::from_bytes(x.addr()));
84+
Scalar::from_pointer(ptr, this)
85+
}
7886
_ => throw_unsup_format!("unsupported return type for native call: {:?}", link_name),
7987
};
8088
interp_ok(ImmTy::from_scalar(scalar, dest.layout))

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn main() {
2121
test_overwrite_dangling();
2222
test_pass_dangling();
2323
test_swap_ptr_triple_dangling();
24+
test_return_ptr();
2425
}
2526

2627
/// Test function that modifies an int.
@@ -207,7 +208,6 @@ fn test_swap_ptr_triple_dangling() {
207208
}
208209

209210

210-
/* TODO: Fix "unsupported return type"
211211
/// Test function that directly returns its pointer argument.
212212
fn test_return_ptr() {
213213
extern "C" {
@@ -220,4 +220,3 @@ fn test_return_ptr() {
220220
let ptr = unsafe { return_ptr(ptr) };
221221
assert_eq!(unsafe { *ptr }, x);
222222
}
223-
*/

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

+16-16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ EXPORT void init_array(int *array, size_t len, int val) {
2323
}
2424
}
2525

26+
/* Test: test_init_static_inner */
27+
28+
typedef struct SyncPtr {
29+
int *ptr;
30+
} SyncPtr;
31+
32+
EXPORT void init_static_inner(const SyncPtr *s_ptr, int val) {
33+
*(s_ptr->ptr) = val;
34+
}
35+
36+
/* Tests: test_exposed, test_pass_dangling */
37+
38+
EXPORT void ignore_ptr(__attribute__((unused)) const int *ptr) {
39+
return;
40+
}
41+
2642
/* Test: test_expose_int */
2743
EXPORT void expose_int(const int *int_ptr, const int **pptr) {
2844
*pptr = int_ptr;
@@ -49,28 +65,12 @@ EXPORT void swap_ptr_tuple(Tuple *t_ptr) {
4965
t_ptr->ptr1 = tmp;
5066
}
5167

52-
/* Test: test_init_static_inner */
53-
54-
typedef struct SyncPtr {
55-
int *ptr;
56-
} SyncPtr;
57-
58-
EXPORT void init_static_inner(const SyncPtr *s_ptr, int val) {
59-
*(s_ptr->ptr) = val;
60-
}
61-
6268
/* Test: test_overwrite_dangling */
6369

6470
EXPORT void overwrite_ptr(const int **pptr) {
6571
*pptr = NULL;
6672
}
6773

68-
/* Test: test_pass_dangling */
69-
70-
EXPORT void ignore_ptr(__attribute__((unused)) const int *ptr) {
71-
return;
72-
}
73-
7474
/* Test: test_swap_ptr_triple_dangling */
7575

7676
typedef struct Triple {

0 commit comments

Comments
 (0)