summaryrefslogtreecommitdiff
path: root/gcc/testsuite/c-c++-common/tsan/volatile.c
blob: 68379921685de7d390fdea73efdd3771c47592d1 (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
/* { dg-options "--param=tsan-distinguish-volatile=1 -fdump-tree-optimized" } */

#include <assert.h>
#include <stdint.h>
#include <stdio.h>

int32_t Global4;
volatile int32_t VolatileGlobal4;
volatile int64_t VolatileGlobal8;

static int nvolatile_reads;
static int nvolatile_writes;

#ifdef __cplusplus
extern "C" {
#endif

__attribute__((no_sanitize_thread))
void __tsan_volatile_read4(void *addr) {
  assert(addr == &VolatileGlobal4);
  nvolatile_reads++;
}
__attribute__((no_sanitize_thread))
void __tsan_volatile_write4(void *addr) {
  assert(addr == &VolatileGlobal4);
  nvolatile_writes++;
}
__attribute__((no_sanitize_thread))
void __tsan_volatile_read8(void *addr) {
  assert(addr == &VolatileGlobal8);
  nvolatile_reads++;
}
__attribute__((no_sanitize_thread))
void __tsan_volatile_write8(void *addr) {
  assert(addr == &VolatileGlobal8);
  nvolatile_writes++;
}

#ifdef __cplusplus
}
#endif

__attribute__((no_sanitize_thread))
static void check() {
  assert(nvolatile_reads == 4);
  assert(nvolatile_writes == 4);
}

int main() {
  Global4 = 1;

  VolatileGlobal4 = 1;
  Global4 = VolatileGlobal4;
  VolatileGlobal4 = 1 + VolatileGlobal4;

  VolatileGlobal8 = 1;
  Global4 = (int32_t)VolatileGlobal8;
  VolatileGlobal8 = 1 + VolatileGlobal8;

  check();
  return 0;
}

// { dg-final { scan-tree-dump-times "__tsan_volatile_read4 \\(&VolatileGlobal4" 2 "optimized" } }
// { dg-final { scan-tree-dump-times "__tsan_volatile_read8 \\(&VolatileGlobal8" 2 "optimized" } }
// { dg-final { scan-tree-dump-times "__tsan_volatile_write4 \\(&VolatileGlobal4" 2 "optimized" } }
// { dg-final { scan-tree-dump-times "__tsan_volatile_write8 \\(&VolatileGlobal8" 2 "optimized" } }