1 | // RUN: %clangxx_nsan -O2 %s -o %t |
2 | // RUN: %run %t 2>&1 | FileCheck %s |
3 | |
4 | // RUN: %clangxx_nsan -fno-builtin -O2 %s -o %t |
5 | // RUN: %run %t 2>&1 | FileCheck %s |
6 | |
7 | // This verifies that shadow memory is tracked correcty across typed and |
8 | // bitcasted swaps. |
9 | |
10 | #include <cassert> |
11 | #include <cstddef> |
12 | #include <cstdint> |
13 | #include <utility> |
14 | |
15 | extern "C" void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes, |
16 | size_t bytes_per_line, size_t reserved); |
17 | |
18 | __attribute__((noinline)) void SwapFT(double &a, double &b) { |
19 | // LLVM typically optimizes this to an untyped swap (through i64) anyway. |
20 | std::swap(a, b); |
21 | } |
22 | |
23 | __attribute__((noinline)) void SwapBitcasted(uint64_t &a, uint64_t &b) { |
24 | std::swap(a, b); |
25 | } |
26 | |
27 | int main() { |
28 | double a = 1.0, b = 2.0; |
29 | __nsan_dump_shadow_mem(addr: (const char *)&a, size_bytes: sizeof(a), bytes_per_line: sizeof(a), reserved: 0); |
30 | __nsan_dump_shadow_mem(addr: (const char *)&b, size_bytes: sizeof(b), bytes_per_line: sizeof(b), reserved: 0); |
31 | SwapFT(a, b); |
32 | __nsan_dump_shadow_mem(addr: (const char *)&a, size_bytes: sizeof(a), bytes_per_line: sizeof(a), reserved: 0); |
33 | __nsan_dump_shadow_mem(addr: (const char *)&b, size_bytes: sizeof(b), bytes_per_line: sizeof(b), reserved: 0); |
34 | assert(a == 2.0 && b == 1.0); |
35 | SwapBitcasted(*reinterpret_cast<uint64_t *>(&a), |
36 | *reinterpret_cast<uint64_t *>(&b)); |
37 | __nsan_dump_shadow_mem(addr: (const char *)&a, size_bytes: sizeof(a), bytes_per_line: sizeof(a), reserved: 0); |
38 | __nsan_dump_shadow_mem(addr: (const char *)&b, size_bytes: sizeof(b), bytes_per_line: sizeof(b), reserved: 0); |
39 | assert(a == 1.0 && b == 2.0); |
40 | // CHECK: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}} |
41 | // CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}} |
42 | // CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}} |
43 | // CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}} |
44 | // CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}} |
45 | // CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}} |
46 | } |
47 | |