Skip to content

Commit 86d7dff

Browse files
committed
factor some pthread offset into constants
1 parent 98bb8ac commit 86d7dff

File tree

1 file changed

+16
-8
lines changed
  • src/tools/miri/src/shims/unix

1 file changed

+16
-8
lines changed

src/tools/miri/src/shims/unix/sync.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ fn mutexattr_set_kind<'mir, 'tcx: 'mir>(
6565
// (need to avoid this because it is set by static initializer macros)
6666
// bytes 4-7: mutex id as u32 or 0 if id is not assigned yet.
6767
// bytes 12-15 or 16-19 (depending on platform): mutex kind, as an i32
68-
// (the kind has to be at its offset for compatibility with static initializer macros)
68+
// (the kind has to be at this particular offset for compatibility with Linux's static initializer
69+
// macros, e.g. PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP.)
6970

7071
fn mutex_get_id<'mir, 'tcx: 'mir>(
7172
ecx: &mut MiriInterpCx<'mir, 'tcx>,
@@ -123,11 +124,13 @@ fn mutex_set_kind<'mir, 'tcx: 'mir>(
123124
// (need to avoid this because it is set by static initializer macros)
124125
// bytes 4-7: rwlock id as u32 or 0 if id is not assigned yet.
125126

127+
const RWLOCK_ID_OFFSET: u64 = 4;
128+
126129
fn rwlock_get_id<'mir, 'tcx: 'mir>(
127130
ecx: &mut MiriInterpCx<'mir, 'tcx>,
128131
rwlock_op: &OpTy<'tcx, Provenance>,
129132
) -> InterpResult<'tcx, RwLockId> {
130-
ecx.rwlock_get_or_create_id(rwlock_op, ecx.libc_ty_layout("pthread_rwlock_t"), 4)
133+
ecx.rwlock_get_or_create_id(rwlock_op, ecx.libc_ty_layout("pthread_rwlock_t"), RWLOCK_ID_OFFSET)
131134
}
132135

133136
// pthread_condattr_t
@@ -136,13 +139,15 @@ fn rwlock_get_id<'mir, 'tcx: 'mir>(
136139
// store an i32 in the first four bytes equal to the corresponding libc clock id constant
137140
// (e.g. CLOCK_REALTIME).
138141

142+
const CONDATTR_CLOCK_OFFSET: u64 = 0;
143+
139144
fn condattr_get_clock_id<'mir, 'tcx: 'mir>(
140145
ecx: &MiriInterpCx<'mir, 'tcx>,
141146
attr_op: &OpTy<'tcx, Provenance>,
142147
) -> InterpResult<'tcx, i32> {
143148
ecx.deref_pointer_and_read(
144149
attr_op,
145-
0,
150+
CONDATTR_CLOCK_OFFSET,
146151
ecx.libc_ty_layout("pthread_condattr_t"),
147152
ecx.machine.layouts.i32,
148153
)?
@@ -156,7 +161,7 @@ fn condattr_set_clock_id<'mir, 'tcx: 'mir>(
156161
) -> InterpResult<'tcx, ()> {
157162
ecx.deref_pointer_and_write(
158163
attr_op,
159-
0,
164+
CONDATTR_CLOCK_OFFSET,
160165
Scalar::from_i32(clock_id),
161166
ecx.libc_ty_layout("pthread_condattr_t"),
162167
ecx.machine.layouts.i32,
@@ -172,11 +177,14 @@ fn condattr_set_clock_id<'mir, 'tcx: 'mir>(
172177
// bytes 4-7: the conditional variable id as u32 or 0 if id is not assigned yet.
173178
// bytes 8-11: the clock id constant as i32
174179

180+
const CONDVAR_ID_OFFSET: u64 = 4;
181+
const CONDVAR_CLOCK_OFFSET: u64 = 8;
182+
175183
fn cond_get_id<'mir, 'tcx: 'mir>(
176184
ecx: &mut MiriInterpCx<'mir, 'tcx>,
177185
cond_op: &OpTy<'tcx, Provenance>,
178186
) -> InterpResult<'tcx, CondvarId> {
179-
ecx.condvar_get_or_create_id(cond_op, ecx.libc_ty_layout("pthread_cond_t"), 4)
187+
ecx.condvar_get_or_create_id(cond_op, ecx.libc_ty_layout("pthread_cond_t"), CONDVAR_ID_OFFSET)
180188
}
181189

182190
fn cond_reset_id<'mir, 'tcx: 'mir>(
@@ -185,7 +193,7 @@ fn cond_reset_id<'mir, 'tcx: 'mir>(
185193
) -> InterpResult<'tcx, ()> {
186194
ecx.deref_pointer_and_write(
187195
cond_op,
188-
4,
196+
CONDVAR_ID_OFFSET,
189197
Scalar::from_i32(0),
190198
ecx.libc_ty_layout("pthread_cond_t"),
191199
ecx.machine.layouts.u32,
@@ -198,7 +206,7 @@ fn cond_get_clock_id<'mir, 'tcx: 'mir>(
198206
) -> InterpResult<'tcx, i32> {
199207
ecx.deref_pointer_and_read(
200208
cond_op,
201-
8,
209+
CONDVAR_CLOCK_OFFSET,
202210
ecx.libc_ty_layout("pthread_cond_t"),
203211
ecx.machine.layouts.i32,
204212
)?
@@ -212,7 +220,7 @@ fn cond_set_clock_id<'mir, 'tcx: 'mir>(
212220
) -> InterpResult<'tcx, ()> {
213221
ecx.deref_pointer_and_write(
214222
cond_op,
215-
8,
223+
CONDVAR_CLOCK_OFFSET,
216224
Scalar::from_i32(clock_id),
217225
ecx.libc_ty_layout("pthread_cond_t"),
218226
ecx.machine.layouts.i32,

0 commit comments

Comments
 (0)