Skip to content

Commit

Permalink
add pointers being returned from C, add test_return_ptr
Browse files Browse the repository at this point in the history
  • Loading branch information
Strophox committed Nov 29, 2024
1 parent 9916c6a commit 1fb91a7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
12 changes: 10 additions & 2 deletions src/tools/miri/src/shims/native_lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ use std::ops::Deref;

use libffi::high::call as ffi;
use libffi::low::CodePtr;
use rustc_abi::{BackendRepr, HasDataLayout};
use rustc_middle::ty::{self as ty, IntTy, UintTy};
use rustc_abi::{BackendRepr, HasDataLayout, Size};
use rustc_middle::{
mir::interpret::Pointer,
ty::{self as ty, IntTy, UintTy},
};
use rustc_span::Symbol;

use crate::*;
Expand Down Expand Up @@ -75,6 +78,11 @@ trait EvalContextExtPriv<'tcx>: crate::MiriInterpCxExt<'tcx> {
unsafe { ffi::call::<()>(ptr, libffi_args.as_slice()) };
return interp_ok(ImmTy::uninit(dest.layout));
}
ty::RawPtr(..) => {
let x = unsafe { ffi::call::<*const ()>(ptr, libffi_args.as_slice()) };
let ptr = Pointer::new(Provenance::Wildcard, Size::from_bytes(x.addr()));
Scalar::from_pointer(ptr, this)
}
_ => throw_unsup_format!("unsupported return type for native call: {:?}", link_name),
};
interp_ok(ImmTy::from_scalar(scalar, dest.layout))
Expand Down
3 changes: 1 addition & 2 deletions src/tools/miri/tests/native-lib/pass/ptr_write_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn main() {
test_overwrite_dangling();
test_pass_dangling();
test_swap_ptr_triple_dangling();
test_return_ptr();
}

/// Test function that modifies an int.
Expand Down Expand Up @@ -207,7 +208,6 @@ fn test_swap_ptr_triple_dangling() {
}


/* TODO: Fix "unsupported return type"
/// Test function that directly returns its pointer argument.
fn test_return_ptr() {
extern "C" {
Expand All @@ -220,4 +220,3 @@ fn test_return_ptr() {
let ptr = unsafe { return_ptr(ptr) };
assert_eq!(unsafe { *ptr }, x);
}
*/
32 changes: 16 additions & 16 deletions src/tools/miri/tests/native-lib/ptr_write_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ EXPORT void init_array(int *array, size_t len, int val) {
}
}

/* Test: test_init_static_inner */

typedef struct SyncPtr {
int *ptr;
} SyncPtr;

EXPORT void init_static_inner(const SyncPtr *s_ptr, int val) {
*(s_ptr->ptr) = val;
}

/* Tests: test_exposed, test_pass_dangling */

EXPORT void ignore_ptr(__attribute__((unused)) const int *ptr) {
return;
}

/* Test: test_expose_int */
EXPORT void expose_int(const int *int_ptr, const int **pptr) {
*pptr = int_ptr;
Expand All @@ -49,28 +65,12 @@ EXPORT void swap_ptr_tuple(Tuple *t_ptr) {
t_ptr->ptr1 = tmp;
}

/* Test: test_init_static_inner */

typedef struct SyncPtr {
int *ptr;
} SyncPtr;

EXPORT void init_static_inner(const SyncPtr *s_ptr, int val) {
*(s_ptr->ptr) = val;
}

/* Test: test_overwrite_dangling */

EXPORT void overwrite_ptr(const int **pptr) {
*pptr = NULL;
}

/* Test: test_pass_dangling */

EXPORT void ignore_ptr(__attribute__((unused)) const int *ptr) {
return;
}

/* Test: test_swap_ptr_triple_dangling */

typedef struct Triple {
Expand Down

0 comments on commit 1fb91a7

Please sign in to comment.