@@ -2,57 +2,11 @@ use std::convert::TryInto;
2
2
use std:: time:: { Duration , SystemTime } ;
3
3
use std:: ops:: Not ;
4
4
5
- use rustc_middle:: ty:: { layout:: TyAndLayout , TyKind , TypeAndMut } ;
6
- use rustc_target:: abi:: { LayoutOf , Size } ;
7
-
8
5
use crate :: * ;
9
6
use stacked_borrows:: Tag ;
10
7
use thread:: Time ;
11
8
12
9
13
- fn assert_ptr_target_min_size < ' mir , ' tcx : ' mir > (
14
- ecx : & MiriEvalContext < ' mir , ' tcx > ,
15
- operand : OpTy < ' tcx , Tag > ,
16
- min_size : u64 ,
17
- ) -> InterpResult < ' tcx , ( ) > {
18
- let target_ty = match operand. layout . ty . kind {
19
- TyKind :: RawPtr ( TypeAndMut { ty, mutbl : _ } ) => ty,
20
- _ => panic ! ( "Argument to pthread function was not a raw pointer" ) ,
21
- } ;
22
- let target_layout = ecx. layout_of ( target_ty) ?;
23
- assert ! ( target_layout. size. bytes( ) >= min_size) ;
24
- Ok ( ( ) )
25
- }
26
-
27
- fn get_at_offset < ' mir , ' tcx : ' mir > (
28
- ecx : & MiriEvalContext < ' mir , ' tcx > ,
29
- op : OpTy < ' tcx , Tag > ,
30
- offset : u64 ,
31
- layout : TyAndLayout < ' tcx > ,
32
- min_size : u64 ,
33
- ) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
34
- // Ensure that the following read at an offset to the attr pointer is within bounds
35
- assert_ptr_target_min_size ( ecx, op, min_size) ?;
36
- let op_place = ecx. deref_operand ( op) ?;
37
- let value_place = op_place. offset ( Size :: from_bytes ( offset) , MemPlaceMeta :: None , layout, ecx) ?;
38
- ecx. read_scalar ( value_place. into ( ) )
39
- }
40
-
41
- fn set_at_offset < ' mir , ' tcx : ' mir > (
42
- ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
43
- op : OpTy < ' tcx , Tag > ,
44
- offset : u64 ,
45
- value : impl Into < ScalarMaybeUninit < Tag > > ,
46
- layout : TyAndLayout < ' tcx > ,
47
- min_size : u64 ,
48
- ) -> InterpResult < ' tcx , ( ) > {
49
- // Ensure that the following write at an offset to the attr pointer is within bounds
50
- assert_ptr_target_min_size ( ecx, op, min_size) ?;
51
- let op_place = ecx. deref_operand ( op) ?;
52
- let value_place = op_place. offset ( Size :: from_bytes ( offset) , MemPlaceMeta :: None , layout, ecx) ?;
53
- ecx. write_scalar ( value. into ( ) , value_place. into ( ) )
54
- }
55
-
56
10
// pthread_mutexattr_t is either 4 or 8 bytes, depending on the platform.
57
11
58
12
// Our chosen memory layout for emulation (does not have to match the platform layout!):
@@ -66,8 +20,6 @@ fn set_at_offset<'mir, 'tcx: 'mir>(
66
20
/// in `pthread_mutexattr_settype` function.
67
21
const PTHREAD_MUTEX_NORMAL_FLAG : i32 = 0x8000000 ;
68
22
69
- const PTHREAD_MUTEXATTR_T_MIN_SIZE : u64 = 4 ;
70
-
71
23
fn is_mutex_kind_default < ' mir , ' tcx : ' mir > (
72
24
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
73
25
kind : Scalar < Tag > ,
@@ -88,15 +40,15 @@ fn mutexattr_get_kind<'mir, 'tcx: 'mir>(
88
40
ecx : & MiriEvalContext < ' mir , ' tcx > ,
89
41
attr_op : OpTy < ' tcx , Tag > ,
90
42
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
91
- get_at_offset ( ecx, attr_op, 0 , ecx. machine . layouts . i32 , PTHREAD_MUTEXATTR_T_MIN_SIZE )
43
+ ecx. read_scalar_at_offset ( attr_op, 0 , ecx. machine . layouts . i32 )
92
44
}
93
45
94
46
fn mutexattr_set_kind < ' mir , ' tcx : ' mir > (
95
47
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
96
48
attr_op : OpTy < ' tcx , Tag > ,
97
49
kind : impl Into < ScalarMaybeUninit < Tag > > ,
98
50
) -> InterpResult < ' tcx , ( ) > {
99
- set_at_offset ( ecx, attr_op, 0 , kind, ecx. machine . layouts . i32 , PTHREAD_MUTEXATTR_T_MIN_SIZE )
51
+ ecx. write_scalar_at_offset ( attr_op, 0 , kind, ecx. machine . layouts . i32 )
100
52
}
101
53
102
54
// pthread_mutex_t is between 24 and 48 bytes, depending on the platform.
@@ -108,14 +60,12 @@ fn mutexattr_set_kind<'mir, 'tcx: 'mir>(
108
60
// bytes 12-15 or 16-19 (depending on platform): mutex kind, as an i32
109
61
// (the kind has to be at its offset for compatibility with static initializer macros)
110
62
111
- const PTHREAD_MUTEX_T_MIN_SIZE : u64 = 24 ;
112
-
113
63
fn mutex_get_kind < ' mir , ' tcx : ' mir > (
114
64
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
115
65
mutex_op : OpTy < ' tcx , Tag > ,
116
66
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
117
67
let offset = if ecx. pointer_size ( ) . bytes ( ) == 8 { 16 } else { 12 } ;
118
- get_at_offset ( ecx, mutex_op, offset, ecx. machine . layouts . i32 , PTHREAD_MUTEX_T_MIN_SIZE )
68
+ ecx. read_scalar_at_offset ( mutex_op, offset, ecx. machine . layouts . i32 )
119
69
}
120
70
121
71
fn mutex_set_kind < ' mir , ' tcx : ' mir > (
@@ -124,22 +74,22 @@ fn mutex_set_kind<'mir, 'tcx: 'mir>(
124
74
kind : impl Into < ScalarMaybeUninit < Tag > > ,
125
75
) -> InterpResult < ' tcx , ( ) > {
126
76
let offset = if ecx. pointer_size ( ) . bytes ( ) == 8 { 16 } else { 12 } ;
127
- set_at_offset ( ecx, mutex_op, offset, kind, ecx. machine . layouts . i32 , PTHREAD_MUTEX_T_MIN_SIZE )
77
+ ecx. write_scalar_at_offset ( mutex_op, offset, kind, ecx. machine . layouts . i32 )
128
78
}
129
79
130
80
fn mutex_get_id < ' mir , ' tcx : ' mir > (
131
81
ecx : & MiriEvalContext < ' mir , ' tcx > ,
132
82
mutex_op : OpTy < ' tcx , Tag > ,
133
83
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
134
- get_at_offset ( ecx, mutex_op, 4 , ecx. machine . layouts . u32 , PTHREAD_MUTEX_T_MIN_SIZE )
84
+ ecx. read_scalar_at_offset ( mutex_op, 4 , ecx. machine . layouts . u32 )
135
85
}
136
86
137
87
fn mutex_set_id < ' mir , ' tcx : ' mir > (
138
88
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
139
89
mutex_op : OpTy < ' tcx , Tag > ,
140
90
id : impl Into < ScalarMaybeUninit < Tag > > ,
141
91
) -> InterpResult < ' tcx , ( ) > {
142
- set_at_offset ( ecx, mutex_op, 4 , id, ecx. machine . layouts . u32 , PTHREAD_MUTEX_T_MIN_SIZE )
92
+ ecx. write_scalar_at_offset ( mutex_op, 4 , id, ecx. machine . layouts . u32 )
143
93
}
144
94
145
95
fn mutex_get_or_create_id < ' mir , ' tcx : ' mir > (
@@ -165,21 +115,19 @@ fn mutex_get_or_create_id<'mir, 'tcx: 'mir>(
165
115
// (need to avoid this because it is set by static initializer macros)
166
116
// bytes 4-7: rwlock id as u32 or 0 if id is not assigned yet.
167
117
168
- const PTHREAD_RWLOCK_T_MIN_SIZE : u64 = 32 ;
169
-
170
118
fn rwlock_get_id < ' mir , ' tcx : ' mir > (
171
119
ecx : & MiriEvalContext < ' mir , ' tcx > ,
172
120
rwlock_op : OpTy < ' tcx , Tag > ,
173
121
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
174
- get_at_offset ( ecx, rwlock_op, 4 , ecx. machine . layouts . u32 , PTHREAD_RWLOCK_T_MIN_SIZE )
122
+ ecx. read_scalar_at_offset ( rwlock_op, 4 , ecx. machine . layouts . u32 )
175
123
}
176
124
177
125
fn rwlock_set_id < ' mir , ' tcx : ' mir > (
178
126
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
179
127
rwlock_op : OpTy < ' tcx , Tag > ,
180
128
id : impl Into < ScalarMaybeUninit < Tag > > ,
181
129
) -> InterpResult < ' tcx , ( ) > {
182
- set_at_offset ( ecx, rwlock_op, 4 , id, ecx. machine . layouts . u32 , PTHREAD_RWLOCK_T_MIN_SIZE )
130
+ ecx. write_scalar_at_offset ( rwlock_op, 4 , id, ecx. machine . layouts . u32 )
183
131
}
184
132
185
133
fn rwlock_get_or_create_id < ' mir , ' tcx : ' mir > (
@@ -204,21 +152,19 @@ fn rwlock_get_or_create_id<'mir, 'tcx: 'mir>(
204
152
// store an i32 in the first four bytes equal to the corresponding libc clock id constant
205
153
// (e.g. CLOCK_REALTIME).
206
154
207
- const PTHREAD_CONDATTR_T_MIN_SIZE : u64 = 4 ;
208
-
209
155
fn condattr_get_clock_id < ' mir , ' tcx : ' mir > (
210
156
ecx : & MiriEvalContext < ' mir , ' tcx > ,
211
157
attr_op : OpTy < ' tcx , Tag > ,
212
158
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
213
- get_at_offset ( ecx, attr_op, 0 , ecx. machine . layouts . i32 , PTHREAD_CONDATTR_T_MIN_SIZE )
159
+ ecx. read_scalar_at_offset ( attr_op, 0 , ecx. machine . layouts . i32 )
214
160
}
215
161
216
162
fn condattr_set_clock_id < ' mir , ' tcx : ' mir > (
217
163
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
218
164
attr_op : OpTy < ' tcx , Tag > ,
219
165
clock_id : impl Into < ScalarMaybeUninit < Tag > > ,
220
166
) -> InterpResult < ' tcx , ( ) > {
221
- set_at_offset ( ecx, attr_op, 0 , clock_id, ecx. machine . layouts . i32 , PTHREAD_CONDATTR_T_MIN_SIZE )
167
+ ecx. write_scalar_at_offset ( attr_op, 0 , clock_id, ecx. machine . layouts . i32 )
222
168
}
223
169
224
170
// pthread_cond_t
@@ -230,21 +176,19 @@ fn condattr_set_clock_id<'mir, 'tcx: 'mir>(
230
176
// bytes 4-7: the conditional variable id as u32 or 0 if id is not assigned yet.
231
177
// bytes 8-11: the clock id constant as i32
232
178
233
- const PTHREAD_COND_T_MIN_SIZE : u64 = 12 ;
234
-
235
179
fn cond_get_id < ' mir , ' tcx : ' mir > (
236
180
ecx : & MiriEvalContext < ' mir , ' tcx > ,
237
181
cond_op : OpTy < ' tcx , Tag > ,
238
182
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
239
- get_at_offset ( ecx, cond_op, 4 , ecx. machine . layouts . u32 , PTHREAD_COND_T_MIN_SIZE )
183
+ ecx. read_scalar_at_offset ( cond_op, 4 , ecx. machine . layouts . u32 )
240
184
}
241
185
242
186
fn cond_set_id < ' mir , ' tcx : ' mir > (
243
187
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
244
188
cond_op : OpTy < ' tcx , Tag > ,
245
189
id : impl Into < ScalarMaybeUninit < Tag > > ,
246
190
) -> InterpResult < ' tcx , ( ) > {
247
- set_at_offset ( ecx, cond_op, 4 , id, ecx. machine . layouts . u32 , PTHREAD_COND_T_MIN_SIZE )
191
+ ecx. write_scalar_at_offset ( cond_op, 4 , id, ecx. machine . layouts . u32 )
248
192
}
249
193
250
194
fn cond_get_or_create_id < ' mir , ' tcx : ' mir > (
@@ -267,15 +211,15 @@ fn cond_get_clock_id<'mir, 'tcx: 'mir>(
267
211
ecx : & MiriEvalContext < ' mir , ' tcx > ,
268
212
cond_op : OpTy < ' tcx , Tag > ,
269
213
) -> InterpResult < ' tcx , ScalarMaybeUninit < Tag > > {
270
- get_at_offset ( ecx, cond_op, 8 , ecx. machine . layouts . i32 , PTHREAD_COND_T_MIN_SIZE )
214
+ ecx. read_scalar_at_offset ( cond_op, 8 , ecx. machine . layouts . i32 )
271
215
}
272
216
273
217
fn cond_set_clock_id < ' mir , ' tcx : ' mir > (
274
218
ecx : & mut MiriEvalContext < ' mir , ' tcx > ,
275
219
cond_op : OpTy < ' tcx , Tag > ,
276
220
clock_id : impl Into < ScalarMaybeUninit < Tag > > ,
277
221
) -> InterpResult < ' tcx , ( ) > {
278
- set_at_offset ( ecx, cond_op, 8 , clock_id, ecx. machine . layouts . i32 , PTHREAD_COND_T_MIN_SIZE )
222
+ ecx. write_scalar_at_offset ( cond_op, 8 , clock_id, ecx. machine . layouts . i32 )
279
223
}
280
224
281
225
/// Try to reacquire the mutex associated with the condition variable after we
0 commit comments