-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor and expand native-lib tests, comment fixes
- Loading branch information
Showing
9 changed files
with
160 additions
and
20 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
11 changes: 9 additions & 2 deletions
11
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,13 +1,20 @@ | ||
CODEABI_1.0 { | ||
# Define which symbols to export. | ||
global: | ||
# scalar_arguments.c | ||
add_one_int; | ||
printer; | ||
ptr_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,89 @@ | ||
//@only-target-linux | ||
//@only-on-host | ||
|
||
fn main() { | ||
test_pointer(); | ||
|
||
test_simple(); | ||
|
||
test_nested(); | ||
|
||
test_static(); | ||
} | ||
|
||
// Test function that dereferences a pointer and prints its contents from C. | ||
fn test_pointer() { | ||
extern "C" { | ||
fn print_pointer(ptr: *mut i32); | ||
} | ||
|
||
let mut x = 42; | ||
let ptr = &mut x as *mut i32; | ||
|
||
unsafe { print_pointer(ptr) }; | ||
} | ||
|
||
// 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: *mut Simple) -> i32; | ||
} | ||
|
||
let mut simple = Simple { field: -42 }; | ||
let s_ptr = &mut simple as *mut Simple; | ||
|
||
let result = unsafe { access_simple(s_ptr) }; | ||
assert_eq!(result, -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: *mut Nested) -> i32; | ||
} | ||
|
||
let mut nested_0 = Nested { value: 0, next: None }; | ||
let mut nested_1 = Nested { value: 1, next: NonNull::new(&mut nested_0) }; | ||
let mut nested_2 = Nested { value: 2, next: NonNull::new(&mut nested_1) }; | ||
let mut nested_3 = Nested { value: 3, next: NonNull::new(&mut nested_2) }; | ||
let n_ptr = &mut nested_3 as *mut Nested; | ||
|
||
let result = unsafe { access_nested(n_ptr) }; | ||
assert_eq!(result, 0); | ||
} | ||
|
||
// 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, | ||
}; | ||
|
||
let result = unsafe { access_static(&STATIC) }; | ||
assert_eq!(result, 9001); | ||
} |
1 change: 0 additions & 1 deletion
1
...s/native-lib/pass/call_extern_c_fn.stdout → ...ts/native-lib/pass/ptr_read_access.stdout
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,2 +1 @@ | ||
printing from C | ||
printing pointer dereference from C: 42 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
printing from C |
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. */ | ||
|
||
void print_pointer(int *ptr) { | ||
printf("printing pointer dereference from C: %d\n", *ptr); | ||
} | ||
|
||
/* Test. */ | ||
|
||
typedef struct Simple { | ||
int field; | ||
} Simple; | ||
|
||
int access_simple(Simple *s_ptr) { | ||
return s_ptr->field; | ||
} | ||
|
||
/* Test. */ | ||
|
||
typedef struct Nested { | ||
int value; | ||
struct Nested *next; | ||
} Nested; | ||
|
||
// Returns the innermost/last `value` of the `Nested` pointer chain. | ||
int access_nested(Nested *n_ptr) { | ||
// Edge case: `n_ptr == NULL`, first Nested is None). | ||
if (!n_ptr) { return 0; } | ||
|
||
while (n_ptr->next) { | ||
n_ptr = n_ptr->next; | ||
} | ||
|
||
return n_ptr->value; | ||
} | ||
|
||
/* Test. */ | ||
|
||
typedef struct Static { | ||
int value; | ||
struct Static *recurse; | ||
} Static; | ||
|
||
int access_static(Static *s_ptr) { | ||
return s_ptr->recurse->recurse->value; | ||
} |
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