1 | // RUN: %clangxx_nsan -O2 -g %s -o %t |
2 | // RUN: %run %t 2>&1 | FileCheck %s |
3 | |
4 | // Check compilation mode that is required to call memcpy/memmove. |
5 | // RUN: %clangxx_nsan -fno-builtin -O2 -g %s -o %t |
6 | // RUN: %run %t 2>&1 | FileCheck %s |
7 | |
8 | // This tests a particularaly hard case of memory tracking. stable_sort does |
9 | // conditional swaps of pairs of elements with mixed types (int/double). |
10 | |
11 | #include <algorithm> |
12 | #include <cstddef> |
13 | #include <cstdio> |
14 | #include <utility> |
15 | #include <vector> |
16 | |
17 | extern "C" void __nsan_dump_shadow_mem(const char *addr, size_t size_bytes, |
18 | size_t bytes_per_line, size_t reserved); |
19 | |
20 | __attribute__((noinline)) void Run(std::vector<int> &indices, |
21 | std::vector<double> &values) { |
22 | const auto num_entries = indices.size(); |
23 | std::vector<std::pair<int, double>> entries; |
24 | entries.reserve(num_entries); |
25 | for (size_t i = 0; i < num_entries; ++i) |
26 | entries.emplace_back(indices[i], values[i]); |
27 | |
28 | __nsan_dump_shadow_mem((const char *)&entries[0].second, sizeof(double), |
29 | sizeof(double), 0); |
30 | __nsan_dump_shadow_mem((const char *)&entries[1].second, sizeof(double), |
31 | sizeof(double), 0); |
32 | // CHECK: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.02800000000000002487) |
33 | // CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (7.95099999999999962341) |
34 | std::stable_sort( |
35 | entries.begin(), entries.end(), |
36 | [](const std::pair<int, double> &a, const std::pair<int, double> &b) { |
37 | return a.first < b.first; |
38 | }); |
39 | __nsan_dump_shadow_mem((const char *)&entries[0].second, sizeof(double), |
40 | sizeof(double), 0); |
41 | __nsan_dump_shadow_mem((const char *)&entries[1].second, sizeof(double), |
42 | sizeof(double), 0); |
43 | // We make sure that the shadow values have been swapped correctly. |
44 | // CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (7.95099999999999962341) |
45 | // CHECK-NEXT: {{.*}}: d0 d1 d2 d3 d4 d5 d6 d7 (1.02800000000000002487) |
46 | } |
47 | |
48 | int main() { |
49 | std::vector<int> indices; |
50 | std::vector<double> values; |
51 | indices.push_back(75); |
52 | values.push_back(1.028); |
53 | indices.push_back(74); |
54 | values.push_back(7.951); |
55 | Run(indices, values); |
56 | } |
57 | |