Skip to content

[compiler-rt][nsan] Add more tests for shadow memory #100906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions compiler-rt/test/nsan/stable_sort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// RUN: %clangxx_nsan -O2 -g %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// Check compilation mode that is required to call memcpy/memmove.
// RUN: %clangxx_nsan -fno-builtin -O2 -g %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// This tests a particularaly hard case of memory tracking. stable_sort does
// conditional swaps of pairs of elements with mixed types (int/double).

#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <utility>
#include <vector>

extern "C" void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes,
size_t bytes_per_line, size_t reserved);

__attribute__((noinline)) void Run(std::vector<int> &indices,
std::vector<double> &values) {
const auto num_entries = indices.size();
std::vector<std::pair<int, double>> entries;
entries.reserve(num_entries);
for (size_t i = 0; i < num_entries; ++i)
entries.emplace_back(indices[i], values[i]);

__nsan_dump_shadow_mem((const char *)&entries[0].second, sizeof(double),
sizeof(double), 0);
__nsan_dump_shadow_mem((const char *)&entries[1].second, sizeof(double),
sizeof(double), 0);
// CHECK: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.02800000000000002487)
// CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (7.95099999999999962341)
std::stable_sort(
entries.begin(), entries.end(),
[](const std::pair<int, double> &a, const std::pair<int, double> &b) {
return a.first < b.first;
});
__nsan_dump_shadow_mem((const char *)&entries[0].second, sizeof(double),
sizeof(double), 0);
__nsan_dump_shadow_mem((const char *)&entries[1].second, sizeof(double),
sizeof(double), 0);
// We make sure that the shadow values have been swapped correctly.
// CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (7.95099999999999962341)
// CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.02800000000000002487)
}

int main() {
std::vector<int> indices;
std::vector<double> values;
indices.push_back(75);
values.push_back(1.028);
indices.push_back(74);
values.push_back(7.951);
Run(indices, values);
}
46 changes: 46 additions & 0 deletions compiler-rt/test/nsan/swap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %clangxx_nsan -O2 %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// RUN: %clangxx_nsan -fno-builtin -O2 %s -o %t
// RUN: %run %t 2>&1 | FileCheck %s

// This verifies that shadow memory is tracked correcty across typed and
// bitcasted swaps.

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <utility>

extern "C" void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes,
size_t bytes_per_line, size_t reserved);

__attribute__((noinline)) void SwapFT(double &a, double &b) {
// LLVM typically optimizes this to an untyped swap (through i64) anyway.
std::swap(a, b);
}

__attribute__((noinline)) void SwapBitcasted(uint64_t &a, uint64_t &b) {
std::swap(a, b);
}

int main() {
double a = 1.0, b = 2.0;
__nsan_dump_shadow_mem((const char *)&a, sizeof(a), sizeof(a), 0);
__nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 0);
SwapFT(a, b);
__nsan_dump_shadow_mem((const char *)&a, sizeof(a), sizeof(a), 0);
__nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 0);
assert(a == 2.0 && b == 1.0);
SwapBitcasted(*reinterpret_cast<uint64_t *>(&a),
*reinterpret_cast<uint64_t *>(&b));
__nsan_dump_shadow_mem((const char *)&a, sizeof(a), sizeof(a), 0);
__nsan_dump_shadow_mem((const char *)&b, sizeof(b), sizeof(b), 0);
assert(a == 1.0 && b == 2.0);
// CHECK: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}}
// CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}}
// CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}}
// CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}}
// CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.0{{.*}}
// CHECK-NEXT: 0x{{[a-f0-9]*}}: d0 d1 d2 d3 d4 d5 d6 d7 (2.0{{.*}}
}
Loading