forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#129684 - Strophox:miri-pass-pointer-to-ffi,…
… r=RalfJung Enable Miri to pass pointers through FFI Following rust-lang#126787, the purpose of this PR is to now enable Miri to execute native calls that make use of pointers. > <details> > > <summary> Simple example </summary> > > ```rust > extern "C" { > fn ptr_printer(ptr: *mut i32); > } > > fn main() { > let ptr = &mut 42 as *mut i32; > unsafe { > ptr_printer(ptr); > } > } > ``` > ```c > void ptr_printer(int *ptr) { > printf("printing pointer dereference from C: %d\n", *ptr); > } > ``` > should now show `printing pointer dereference from C: 42`. > > </details> Note that this PR does not yet implement any logic involved in updating Miri's "analysis" state (byte initialization, provenance) upon such a native call. r? `@RalfJung`
- Loading branch information
Showing
15 changed files
with
260 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/tools/miri/tests/native-lib/libtest.map → ...ools/miri/tests/native-lib/native-lib.map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,20 @@ | ||
CODEABI_1.0 { | ||
# Define which symbols to export. | ||
global: | ||
# scalar_arguments.c | ||
add_one_int; | ||
printer; | ||
test_stack_spill; | ||
get_unsigned_int; | ||
add_int16; | ||
add_short_to_long; | ||
|
||
# ptr_read_access.c | ||
print_pointer; | ||
access_simple; | ||
access_nested; | ||
access_static; | ||
|
||
# The rest remains private. | ||
local: *; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//@only-target-linux | ||
//@only-on-host | ||
|
||
fn main() { | ||
test_pointer(); | ||
|
||
test_simple(); | ||
|
||
test_nested(); | ||
|
||
test_static(); | ||
} | ||
|
||
// Test void function that dereferences a pointer and prints its contents from C. | ||
fn test_pointer() { | ||
extern "C" { | ||
fn print_pointer(ptr: *const i32); | ||
} | ||
|
||
let x = 42; | ||
|
||
unsafe { print_pointer(&x) }; | ||
} | ||
|
||
// Test function that dereferences a simple struct pointer and accesses a field. | ||
fn test_simple() { | ||
#[repr(C)] | ||
struct Simple { | ||
field: i32 | ||
} | ||
|
||
extern "C" { | ||
fn access_simple(s_ptr: *const Simple) -> i32; | ||
} | ||
|
||
let simple = Simple { field: -42 }; | ||
|
||
assert_eq!(unsafe { access_simple(&simple) }, -42); | ||
} | ||
|
||
// Test function that dereferences nested struct pointers and accesses fields. | ||
fn test_nested() { | ||
use std::ptr::NonNull; | ||
|
||
#[derive(Debug, PartialEq, Eq)] | ||
#[repr(C)] | ||
struct Nested { | ||
value: i32, | ||
next: Option<NonNull<Nested>>, | ||
} | ||
|
||
extern "C" { | ||
fn access_nested(n_ptr: *const Nested) -> i32; | ||
} | ||
|
||
let mut nested_0 = Nested { value: 97, next: None }; | ||
let mut nested_1 = Nested { value: 98, next: NonNull::new(&mut nested_0) }; | ||
let nested_2 = Nested { value: 99, next: NonNull::new(&mut nested_1) }; | ||
|
||
assert_eq!(unsafe { access_nested(&nested_2) }, 97); | ||
} | ||
|
||
// Test function that dereferences static struct pointers and accesses fields. | ||
fn test_static() { | ||
|
||
#[repr(C)] | ||
struct Static { | ||
value: i32, | ||
recurse: &'static Static, | ||
} | ||
|
||
extern "C" { | ||
fn access_static(n_ptr: *const Static) -> i32; | ||
} | ||
|
||
static STATIC: Static = Static { | ||
value: 9001, | ||
recurse: &STATIC, | ||
}; | ||
|
||
assert_eq!(unsafe { access_static(&STATIC) }, 9001); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
printing pointer dereference from C: 42 |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include <stdio.h> | ||
|
||
/* Test: test_pointer */ | ||
|
||
void print_pointer(const int *ptr) { | ||
printf("printing pointer dereference from C: %d\n", *ptr); | ||
} | ||
|
||
/* Test: test_simple */ | ||
|
||
typedef struct Simple { | ||
int field; | ||
} Simple; | ||
|
||
int access_simple(const Simple *s_ptr) { | ||
return s_ptr->field; | ||
} | ||
|
||
/* Test: test_nested */ | ||
|
||
typedef struct Nested { | ||
int value; | ||
struct Nested *next; | ||
} Nested; | ||
|
||
// Returns the innermost/last value of a Nested pointer chain. | ||
int access_nested(const Nested *n_ptr) { | ||
// Edge case: `n_ptr == NULL` (i.e. first Nested is None). | ||
if (!n_ptr) { return 0; } | ||
|
||
while (n_ptr->next) { | ||
n_ptr = n_ptr->next; | ||
} | ||
|
||
return n_ptr->value; | ||
} | ||
|
||
/* Test: test_static */ | ||
|
||
typedef struct Static { | ||
int value; | ||
struct Static *recurse; | ||
} Static; | ||
|
||
int access_static(const Static *s_ptr) { | ||
return s_ptr->recurse->recurse->value; | ||
} |
File renamed without changes.
Oops, something went wrong.