47 lines · cpp
1// RUN: %clangxx_nsan -O2 %s -o %t2// RUN: %run %t 2>&1 | FileCheck %s3 4// RUN: %clangxx_nsan -fno-builtin -O2 %s -o %t5// RUN: %run %t 2>&1 | FileCheck %s6 7// This verifies that shadow memory is tracked correcty across typed and8// bitcasted swaps.9 10#include <cassert>11#include <cstddef>12#include <cstdint>13#include <utility>14 15extern "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 27int main() {28 double a = 1.0, b = 2.0;29 __nsan_dump_shadow_mem((const char *)&a, sizeof(a), sizeof(a), 0);30 __nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 0);31 SwapFT(a, b);32 __nsan_dump_shadow_mem((const char *)&a, sizeof(a), sizeof(a), 0);33 __nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 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((const char *)&a, sizeof(a), sizeof(a), 0);38 __nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 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