Skip to content

Commit 9916c6a

Browse files
committed
make Doc comments, add compile flag for tests (-Zmiri-permissive-provenance), test_exposed, test_pass_dangling, swap inside test_swap_ptr_triple_dangling, unused test_return_ptr, minor refactors
1 parent 724b0e4 commit 9916c6a

File tree

3 files changed

+95
-67
lines changed

3 files changed

+95
-67
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
fn main() {
66
test_access_pointer();
7-
87
test_access_simple();
9-
108
test_access_nested();
11-
129
test_access_static();
1310
}
1411

15-
// Test function that dereferences an int pointer and prints its contents from C.
12+
/// Test function that dereferences an int pointer and prints its contents from C.
1613
fn test_access_pointer() {
1714
extern "C" {
1815
fn print_pointer(ptr: *const i32);
@@ -23,7 +20,7 @@ fn test_access_pointer() {
2320
unsafe { print_pointer(&x) };
2421
}
2522

26-
// Test function that dereferences a simple struct pointer and accesses a field.
23+
/// Test function that dereferences a simple struct pointer and accesses a field.
2724
fn test_access_simple() {
2825
#[repr(C)]
2926
struct Simple {
@@ -39,7 +36,7 @@ fn test_access_simple() {
3936
assert_eq!(unsafe { access_simple(&simple) }, -42);
4037
}
4138

42-
// Test function that dereferences nested struct pointers and accesses fields.
39+
/// Test function that dereferences nested struct pointers and accesses fields.
4340
fn test_access_nested() {
4441
use std::ptr::NonNull;
4542

@@ -61,7 +58,7 @@ fn test_access_nested() {
6158
assert_eq!(unsafe { access_nested(&nested_2) }, 97);
6259
}
6360

64-
// Test function that dereferences a static struct pointer and accesses fields.
61+
/// Test function that dereferences a static struct pointer and accesses fields.
6562
fn test_access_static() {
6663
#[repr(C)]
6764
struct Static {
Lines changed: 74 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Only works on Unix targets
22
//@ignore-target: windows wasm
33
//@only-on-host
4+
//@compile-flags: -Zmiri-permissive-provenance
5+
46

57
#![feature(box_as_ptr)]
68

@@ -9,27 +11,19 @@ use std::ptr::null;
911

1012
fn main() {
1113
test_increment_int();
12-
1314
test_init_int();
14-
1515
test_init_array();
16-
1716
test_init_static_inner();
18-
17+
test_exposed();
1918
test_expose_int();
20-
2119
test_swap_ptr();
22-
23-
test_swap_nested_ptr();
24-
25-
test_swap_tuple();
26-
20+
test_swap_ptr_tuple();
2721
test_overwrite_dangling();
28-
29-
test_expose_triple();
22+
test_pass_dangling();
23+
test_swap_ptr_triple_dangling();
3024
}
3125

32-
// Test function that modifies an int.
26+
/// Test function that modifies an int.
3327
fn test_increment_int() {
3428
extern "C" {
3529
fn increment_int(ptr: *mut i32);
@@ -41,7 +35,7 @@ fn test_increment_int() {
4135
assert_eq!(x, 12);
4236
}
4337

44-
// Test function that initializes an int.
38+
/// Test function that initializes an int.
4539
fn test_init_int() {
4640
extern "C" {
4741
fn init_int(ptr: *mut i32, val: i32);
@@ -57,7 +51,7 @@ fn test_init_int() {
5751
assert_eq!(x, val);
5852
}
5953

60-
// Test function that initializes an array.
54+
/// Test function that initializes an array.
6155
fn test_init_array() {
6256
extern "C" {
6357
fn init_array(ptr: *mut i32, len: usize, val: i32);
@@ -74,7 +68,7 @@ fn test_init_array() {
7468
assert_eq!(array, [val; LEN]);
7569
}
7670

77-
// Test function that initializes an int pointed to by an immutable static.
71+
/// Test function that initializes an int pointed to by an immutable static.
7872
fn test_init_static_inner() {
7973
#[repr(C)]
8074
struct SyncPtr {
@@ -98,66 +92,66 @@ fn test_init_static_inner() {
9892
assert_eq!(inner, val);
9993
}
10094

101-
// Test function that writes a pointer and exposes the alloc of its int argument.
102-
fn test_expose_int() {
95+
// Test function that marks an allocation as exposed.
96+
fn test_exposed() {
10397
extern "C" {
104-
fn expose_int(int_ptr: *const i32, pptr: *mut *const i32);
98+
fn ignore_ptr(ptr: *const i32);
10599
}
106100

107101
let x = 51;
108-
let mut ptr = std::ptr::null();
102+
let ptr = &raw const x;
103+
let p = ptr.addr();
109104

110-
unsafe { expose_int(&x, &mut ptr) };
111-
assert_eq!(unsafe { *ptr }, x);
105+
unsafe { ignore_ptr(ptr) };
106+
assert_eq!(unsafe { *(p as *const i32) }, x);
112107
}
113108

114-
// Test function that swaps two pointers and exposes the alloc of an int.
115-
fn test_swap_ptr() {
109+
/// Test function that writes a pointer and exposes the alloc of its int argument.
110+
fn test_expose_int() {
116111
extern "C" {
117-
fn swap_ptr(pptr0: *mut *const i32, pptr1: *mut *const i32);
112+
fn expose_int(int_ptr: *const i32, pptr: *mut *const i32);
118113
}
119114

120115
let x = 61;
121-
let (mut ptr0, mut ptr1) = (&raw const x, null());
116+
let mut ptr = std::ptr::null();
122117

123-
unsafe { swap_ptr(&mut ptr0, &mut ptr1) };
124-
assert_eq!(unsafe { *ptr1 }, x);
118+
unsafe { expose_int(&x, &mut ptr) };
119+
assert_eq!(unsafe { *ptr }, x);
125120
}
126121

127-
// Test function that swaps two nested pointers and exposes the alloc of an int.
128-
fn test_swap_nested_ptr() {
122+
/// Test function that swaps two pointers and exposes the alloc of an int.
123+
fn test_swap_ptr() {
129124
extern "C" {
130-
fn swap_nested_ptr(ppptr0: *mut *mut *const i32, ppptr1: *mut *mut *const i32);
125+
fn swap_ptr(pptr0: *mut *const i32, pptr1: *mut *const i32);
131126
}
132127

133128
let x = 71;
134129
let (mut ptr0, mut ptr1) = (&raw const x, null());
135-
let (mut pptr0, mut pptr1) = (&raw mut ptr0, &raw mut ptr1);
136130

137-
unsafe { swap_nested_ptr(&mut pptr0, &mut pptr1) }
131+
unsafe { swap_ptr(&mut ptr0, &mut ptr1) };
138132
assert_eq!(unsafe { *ptr1 }, x);
139133
}
140134

141-
// Test function that swaps two pointers in a struct and exposes the alloc of an int.
142-
fn test_swap_tuple() {
135+
/// Test function that swaps two pointers in a struct and exposes the alloc of an int.
136+
fn test_swap_ptr_tuple() {
143137
#[repr(C)]
144138
struct Tuple {
145139
ptr0: *const i32,
146140
ptr1: *const i32,
147141
}
148142

149143
extern "C" {
150-
fn swap_tuple(t_ptr: *mut Tuple);
144+
fn swap_ptr_tuple(t_ptr: *mut Tuple);
151145
}
152146

153147
let x = 81;
154148
let mut tuple = Tuple { ptr0: &raw const x, ptr1: null() };
155149

156-
unsafe { swap_tuple(&mut tuple) }
150+
unsafe { swap_ptr_tuple(&mut tuple) }
157151
assert_eq!(unsafe { *tuple.ptr1 }, x);
158152
}
159153

160-
// Test function that interacts with a dangling pointer.
154+
/// Test function that interacts with a dangling pointer.
161155
fn test_overwrite_dangling() {
162156
extern "C" {
163157
fn overwrite_ptr(pptr: *mut *const i32);
@@ -166,13 +160,26 @@ fn test_overwrite_dangling() {
166160
let b = Box::new(91);
167161
let mut ptr = Box::as_ptr(&b);
168162
drop(b);
169-
unsafe { overwrite_ptr(&mut ptr) };
170163

164+
unsafe { overwrite_ptr(&mut ptr) };
171165
assert_eq!(ptr, null());
172166
}
173167

174-
// Test function that interacts with a struct storing a dangling pointer.
175-
fn test_expose_triple() {
168+
/// Test function that passes a dangling pointer.
169+
fn test_pass_dangling() {
170+
extern "C" {
171+
fn ignore_ptr(ptr: *const i32);
172+
}
173+
174+
let b = Box::new(101);
175+
let ptr = Box::as_ptr(&b);
176+
drop(b);
177+
178+
unsafe { ignore_ptr(ptr) };
179+
}
180+
181+
/// Test function that interacts with a struct storing a dangling pointer.
182+
fn test_swap_ptr_triple_dangling() {
176183
#[repr(C)]
177184
struct Triple {
178185
ptr0: *const i32,
@@ -181,16 +188,36 @@ fn test_expose_triple() {
181188
}
182189

183190
extern "C" {
184-
fn expose_triple(t_ptr: *const Triple);
191+
fn swap_ptr_triple_dangling(t_ptr: *const Triple);
185192
}
186193

187-
let x = 101;
188-
let y = 111;
194+
let x = 111;
189195
let b = Box::new(121);
190196
let ptr = Box::as_ptr(&b);
191197
drop(b);
192-
let triple = Triple { ptr0: &raw const x, ptr1: ptr, ptr2: &raw const y };
198+
let z = 131;
199+
let triple = Triple {
200+
ptr0: &raw const x,
201+
ptr1: ptr,
202+
ptr2: &raw const z
203+
};
204+
205+
unsafe { swap_ptr_triple_dangling(&triple) }
206+
assert_eq!(unsafe { *triple.ptr2 }, x);
207+
}
208+
193209

194-
unsafe { expose_triple(&triple) }
195-
assert_eq!(unsafe { *triple.ptr2 }, y);
210+
/* TODO: Fix "unsupported return type"
211+
/// Test function that directly returns its pointer argument.
212+
fn test_return_ptr() {
213+
extern "C" {
214+
fn return_ptr(ptr: *const i32) -> *const i32;
215+
}
216+
217+
let x = 141;
218+
let ptr = &raw const x;
219+
220+
let ptr = unsafe { return_ptr(ptr) };
221+
assert_eq!(unsafe { *ptr }, x);
196222
}
223+
*/

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,14 @@ EXPORT void swap_ptr(const int **pptr0, const int **pptr1) {
3636
*pptr1 = tmp;
3737
}
3838

39-
/* Test: test_swap_nested_ptr */
40-
41-
EXPORT void swap_nested_ptr(const int ***ppptr0, const int ***ppptr1) {
42-
const int *tmp = **ppptr0;
43-
**ppptr0 = **ppptr1;
44-
**ppptr1 = tmp;
45-
}
46-
47-
/* Test: test_swap_tuple */
39+
/* Test: test_swap_ptr_tuple */
4840

4941
typedef struct Tuple {
5042
int *ptr0;
5143
int *ptr1;
5244
} Tuple;
5345

54-
EXPORT void swap_tuple(Tuple *t_ptr) {
46+
EXPORT void swap_ptr_tuple(Tuple *t_ptr) {
5547
int *tmp = t_ptr->ptr0;
5648
t_ptr->ptr0 = t_ptr->ptr1;
5749
t_ptr->ptr1 = tmp;
@@ -73,14 +65,26 @@ EXPORT void overwrite_ptr(const int **pptr) {
7365
*pptr = NULL;
7466
}
7567

76-
/* Test: test_expose_triple */
68+
/* Test: test_pass_dangling */
69+
70+
EXPORT void ignore_ptr(__attribute__((unused)) const int *ptr) {
71+
return;
72+
}
73+
74+
/* Test: test_swap_ptr_triple_dangling */
7775

7876
typedef struct Triple {
7977
int *ptr0;
8078
int *ptr1;
8179
int *ptr2;
8280
} Triple;
8381

84-
EXPORT void expose_triple(__attribute__((unused)) const Triple *_t_ptr) {
85-
return;
82+
EXPORT void swap_ptr_triple_dangling(Triple *t_ptr) {
83+
int *tmp = t_ptr->ptr0;
84+
t_ptr->ptr0 = t_ptr->ptr2;
85+
t_ptr->ptr2 = tmp;
86+
}
87+
88+
EXPORT const int *return_ptr(const int *ptr) {
89+
return ptr;
8690
}

0 commit comments

Comments
 (0)