Skip to content

Commit 29929d8

Browse files
author
Vytautas Astrauskas
committed
Fix the problem of sending pointed to thread local statics. Add a regression test.
1 parent eba471d commit 29929d8

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
@@ -386,6 +386,29 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
386386
Ok(())
387387
}
388388

389+
fn access_local(
390+
ecx: &InterpCx<'mir, 'tcx, Self>,
391+
frame: &Frame<'mir, 'tcx, Self::PointerTag, Self::FrameExtra>,
392+
local: mir::Local,
393+
) -> InterpResult<'tcx, Operand<Self::PointerTag>> {
394+
match frame.body.local_decls[local].local_info {
395+
mir::LocalInfo::StaticRef { def_id, is_thread_local: true } => {
396+
let static_alloc_id = ecx.tcx.alloc_map.lock().create_static_alloc(def_id);
397+
let alloc_id = ecx.memory.extra.tls.get_or_register_allocation(*ecx.memory.tcx, static_alloc_id);
398+
let tag = Self::tag_global_base_pointer(&ecx.memory.extra, alloc_id);
399+
let pointer: Pointer = alloc_id.into();
400+
let pointer = pointer.with_tag(tag);
401+
let scalar: Scalar<_> = pointer.into();
402+
let scalar: ScalarMaybeUndef<_> = scalar.into();
403+
let immediate: Immediate<_> = scalar.into();
404+
Ok(
405+
Operand::Immediate(immediate)
406+
)
407+
},
408+
_ => frame.locals[local].access(),
409+
}
410+
}
411+
389412
fn canonical_alloc_id(mem: &Memory<'mir, 'tcx, Self>, id: AllocId) -> AllocId {
390413
let tcx = mem.tcx;
391414
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)