-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
alexander-shaposhnikov
merged 1 commit into
llvm:main
from
alexander-shaposhnikov:add_shadow_memory_tests
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{{.*}} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.