-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Enable Miri to pass pointers through FFI #129684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.