about summary refs log tree commit diff
path: root/src/tools/miri/tests/pass/tls/tls_static.rs
blob: cc4eaca72ab6e6867090a792d96da4cdc754a828 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//@revisions: stack tree
//@[tree]compile-flags: -Zmiri-tree-borrows
//@compile-flags: -Zmiri-strict-provenance

//! The main purpose of this test is to check that if we take a pointer to
//! thread's `t1` thread-local `A` and send it to another thread `t2`,
//! dereferencing the pointer on `t2` resolves to `t1`'s thread-local. In this
//! test, we also check that thread-locals act as per-thread statics.

// FIXME(static_mut_refs): Do not allow `static_mut_refs` lint
#![allow(static_mut_refs)]
#![feature(thread_local)]

use std::ptr::addr_of_mut;
use std::thread;

#[thread_local]
static mut A: u8 = 0;
#[thread_local]
static mut B: u8 = 0;
static mut C: u8 = 0;

// Regression test for https://github.com/rust-lang/rust/issues/96191.
#[thread_local]
static READ_ONLY: u8 = 42;

unsafe fn get_a_ptr() -> *mut u8 {
    addr_of_mut!(A)
}

struct Sender(*mut u8);

unsafe impl Send for Sender {}

fn main() {
    let _val = READ_ONLY;

    let ptr = unsafe {
        let x = get_a_ptr();
        *x = 5;
        assert_eq!(A, 5);
        B = 15;
        C = 25;
        Sender(addr_of_mut!(A))
    };

    thread::spawn(move || unsafe {
        let ptr = ptr; // avoid field capturing
        assert_eq!(*ptr.0, 5);
        assert_eq!(A, 0);
        assert_eq!(B, 0);
        assert_eq!(C, 25);
        B = 14;
        C = 24;
        let y = get_a_ptr();
        assert_eq!(*y, 0);
        *y = 4;
        assert_eq!(*ptr.0, 5);
        assert_eq!(A, 4);
        assert_eq!(*get_a_ptr(), 4);
    })
    .join()
    .unwrap();

    unsafe {
        assert_eq!(*get_a_ptr(), 5);
        assert_eq!(A, 5);
        assert_eq!(B, 15);
        assert_eq!(C, 24);
    }
}