Skip to content

Commit 5c27b83

Browse files
committed
fix/tweak tests
1 parent 26602eb commit 5c27b83

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

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

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ fn main() {
1313

1414
test_swap_ptr();
1515

16+
test_init_interior_mutable();
17+
1618
test_dangling();
1719
}
1820

@@ -43,56 +45,61 @@ fn test_init_int() {
4345

4446
fn test_init_array() {
4547
extern "C" {
46-
fn init_array(ptr: *mut i32, len: usize, value: i32);
48+
fn init_array(ptr: *mut i32, len: usize);
4749
}
4850

49-
const LEN: usize = 4;
50-
let init_value = 41;
51+
const LEN: usize = 3;
5152

5253
let mut array = MaybeUninit::<[i32; LEN]>::uninit();
5354
let array = unsafe {
54-
init_array(array.as_mut_ptr().cast::<i32>(), LEN, init_value);
55+
init_array(array.as_mut_ptr().cast::<i32>(), LEN);
5556
array.assume_init()
5657
};
5758

58-
assert_eq!(array, [init_value; LEN]);
59+
assert_eq!(array, [31; LEN]);
5960
}
6061

6162
fn test_swap_ptr() {
6263
extern "C" {
6364
fn swap_ptr(pptr0: *mut *const i32, pptr1: *mut *const i32);
6465
}
6566

66-
let x = 51;
67-
let mut ptr0 = &x;
67+
let x = 41;
68+
let mut ptr0 = &raw const x;
6869
let mut ptr1 = std::ptr::null();
6970
unsafe { swap_ptr(&mut ptr0, &mut ptr1) };
7071

7172
assert_eq!(unsafe { *ptr1 }, x);
7273
}
7374

74-
fn test_init_static_inner() {
75+
fn test_init_interior_mutable() {
7576
extern "C" {
76-
fn init_static_inner(pptr: *const *mut MaybeUninit<i32>);
77+
fn init_interior_mutable(pptr: *const UnsafeInterior);
7778
}
7879

79-
static mut INNER: MaybeUninit<i32> = MaybeUninit::uninit();
80-
static STATIC: *mut MaybeUninit<i32> = &raw mut INNER;
81-
unsafe { init_static_inner(&STATIC) }
80+
#[repr(C)]
81+
struct UnsafeInterior {
82+
mut_ptr: *mut i32
83+
}
84+
unsafe impl Sync for UnsafeInterior {}
85+
86+
let mut x = MaybeUninit::<i32>::uninit();
87+
let unsafe_interior = UnsafeInterior { mut_ptr: x.as_mut_ptr() };
88+
unsafe { init_interior_mutable(&unsafe_interior) };
8289

83-
assert_eq!(unsafe { INNER.assume_init() }, 61);
90+
assert_eq!(unsafe { x.assume_init() }, 51);
8491
}
8592

8693
fn test_dangling() {
8794
extern "C" {
88-
fn write_nullptr(pptr: *mut *const i32);
95+
fn overwrite_ptr(pptr: *mut *const i32);
8996
}
9097

91-
let x = vec![71];
98+
let x = vec![61];
9299
let mut ptr = x.as_ptr();
93100
drop(x);
94-
unsafe { write_nullptr(&mut ptr) };
101+
unsafe { overwrite_ptr(&mut ptr) };
95102
assert_eq!(ptr, std::ptr::null());
96103
}
97104

98-
// TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.
105+
// TODO: Write tests for (forgetting to) expose: -initial allocation -recursively all allocations -unexposed pointers.

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ EXPORT void modify_int(int *ptr) {
1212
/* Test: test_init_int */
1313

1414
EXPORT void init_int(int *ptr) {
15-
*ptr = 12;
15+
*ptr = 21;
1616
}
1717

1818
/* Test: test_init_array */
1919

20-
EXPORT void init_array(int *array, size_t len, int value) {
20+
EXPORT void init_array(int *array, size_t len) {
2121
for (size_t i = 0; i < len; i++) {
22-
array[i] = value;
22+
array[i] = 31;
2323
}
2424
}
2525

@@ -31,14 +31,18 @@ EXPORT void swap_ptr(const int **pptr0, const int **pptr1) {
3131
*pptr1 = tmp;
3232
}
3333

34-
/* Test: test_init_static_inner */
34+
/* Test: test_init_interior_mutable */
3535

36-
EXPORT void init_static_inner(int **const pptr) {
37-
**pptr = 1234;
36+
typedef struct UnsafeInterior {
37+
int *mut_ptr;
38+
} UnsafeInterior;
39+
40+
EXPORT void init_interior_mutable(const UnsafeInterior *u_ptr) {
41+
*(u_ptr->mut_ptr) = 51;
3842
}
3943

4044
/* Test: test_dangling */
4145

42-
EXPORT void write_nullptr(const int **pptr) {
46+
EXPORT void overwrite_ptr(const int **pptr) {
4347
*pptr = NULL;
44-
}
48+
}

0 commit comments

Comments
 (0)