Skip to content

Commit 7711eb9

Browse files
committed
lazy_sync: ensure the cookie fits inside the primitive
1 parent 6b394d4 commit 7711eb9

File tree

1 file changed

+4
-5
lines changed
  • src/tools/miri/src/concurrency

1 file changed

+4
-5
lines changed

src/tools/miri/src/concurrency/sync.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
246246
let (alloc_extra, _machine) = this.get_alloc_extra_mut(alloc)?;
247247
alloc_extra.sync.insert(offset, Box::new(data));
248248
// Mark this as "initialized".
249+
let init_cookie = Scalar::from_u32(LAZY_INIT_COOKIE);
250+
assert!(init_offset + init_cookie.size() <= primitive.layout.size);
249251
let init_field = primitive.offset(init_offset, this.machine.layouts.u32, this)?;
250-
this.write_scalar_atomic(
251-
Scalar::from_u32(LAZY_INIT_COOKIE),
252-
&init_field,
253-
AtomicWriteOrd::Relaxed,
254-
)?;
252+
this.write_scalar_atomic(init_cookie, &init_field, AtomicWriteOrd::Relaxed)?;
255253
interp_ok(this.get_alloc_extra(alloc)?.get_sync::<T>(offset).unwrap())
256254
}
257255

@@ -278,6 +276,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
278276
// thread initializing. Needs to be an RMW operation to ensure we read the *latest* value.
279277
// So we just try to replace MUTEX_INIT_COOKIE with itself.
280278
let init_cookie = Scalar::from_u32(LAZY_INIT_COOKIE);
279+
assert!(init_offset + init_cookie.size() <= primitive.layout.size);
281280
let init_field = primitive.offset(init_offset, this.machine.layouts.u32, this)?;
282281
let (_init, success) = this
283282
.atomic_compare_exchange_scalar(

0 commit comments

Comments
 (0)