Skip to content

Commit 7ebf623

Browse files
author
Vytautas Astrauskas
committed
Fix the problem of sending pointed to thread local statics. Add a regression test.
1 parent 588c233 commit 7ebf623

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/machine.rs

+23
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,29 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
351351
Ok(())
352352
}
353353

354+
fn access_local(
355+
ecx: &InterpCx<'mir, 'tcx, Self>,
356+
frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
357+
local: mir::Local,
358+
) -> InterpResult<'tcx, Operand<Self::PointerTag>> {
359+
match frame.body.local_decls[local].local_info {
360+
mir::LocalInfo::StaticRef { def_id, is_thread_local: true } => {
361+
let static_alloc_id = ecx.tcx.alloc_map.lock().create_static_alloc(def_id);
362+
let alloc_id = ecx.memory.extra.tls.get_or_register_allocation(*ecx.memory.tcx, static_alloc_id);
363+
let tag = Self::tag_global_base_pointer(&ecx.memory.extra, alloc_id);
364+
let pointer: Pointer = alloc_id.into();
365+
let pointer = pointer.with_tag(tag);
366+
let scalar: Scalar<_> = pointer.into();
367+
let scalar: ScalarMaybeUndef<_> = scalar.into();
368+
let immediate: Immediate<_> = scalar.into();
369+
Ok(
370+
Operand::Immediate(immediate)
371+
)
372+
},
373+
_ => frame.locals[local].access(),
374+
}
375+
}
376+
354377
fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
355378
let tcx = mem.tcx;
356379
let alloc = tcx.alloc_map.lock().get(id);
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
2+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
3+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
4+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
5+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
6+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
7+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
8+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
9+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.
10+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.

tests/run-pass/concurrency/thread_locals.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,24 @@ unsafe fn get_a_ref() -> *mut u8 {
1212
&mut A
1313
}
1414

15+
struct Sender(*mut u8);
16+
17+
unsafe impl Send for Sender {}
18+
1519
fn main() {
1620

17-
unsafe {
21+
let ptr = unsafe {
1822
let x = get_a_ref();
1923
*x = 5;
2024
assert_eq!(A, 5);
2125
B = 15;
2226
C = 25;
23-
}
27+
Sender(&mut A)
28+
};
2429

25-
thread::spawn(|| {
30+
thread::spawn(move || {
2631
unsafe {
32+
assert_eq!(*ptr.0, 5);
2733
assert_eq!(A, 0);
2834
assert_eq!(B, 0);
2935
assert_eq!(C, 25);
@@ -32,6 +38,7 @@ fn main() {
3238
let y = get_a_ref();
3339
assert_eq!(*y, 0);
3440
*y = 4;
41+
assert_eq!(*ptr.0, 5);
3542
assert_eq!(A, 4);
3643
assert_eq!(*get_a_ref(), 4);
3744

@@ -45,4 +52,5 @@ fn main() {
4552
assert_eq!(C, 24);
4653
}
4754

48-
}
55+
}
56+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
WARNING: The thread support is experimental. For example, Miri does not detect data races yet.

0 commit comments

Comments
 (0)