1 | #include <assert.h> |
2 | #include <stdint.h> |
3 | #include <stdio.h> |
4 | #include <string.h> |
5 | |
6 | int main() { printf(format: "main\n" ); } |
7 | |
8 | namespace { |
9 | #define FN(X) \ |
10 | if (pc == reinterpret_cast<uintptr_t>(X)) \ |
11 | return #X |
12 | |
13 | const char *symbolize(uintptr_t pc) { |
14 | FUNCTIONS; |
15 | return nullptr; |
16 | } |
17 | |
18 | template <typename T> T consume(const char *&pos, const char *end) { |
19 | T v; |
20 | // We need to memcpy from pos, because it's not guaranteed that every entry |
21 | // has the required alignment of T. |
22 | memcpy(&v, pos, sizeof(T)); |
23 | pos += sizeof(T); |
24 | assert(pos <= end); |
25 | return v; |
26 | } |
27 | |
28 | uint64_t consume_uleb128(const char *&pos, const char *end) { |
29 | uint64_t val = 0; |
30 | int shift = 0; |
31 | uint8_t cur; |
32 | do { |
33 | cur = *pos++; |
34 | val |= uint64_t{cur & 0x7fu} << shift; |
35 | shift += 7; |
36 | } while (cur & 0x80); |
37 | assert(shift < 64); |
38 | assert(pos <= end); |
39 | return val; |
40 | } |
41 | |
42 | constexpr uint32_t kSanitizerBinaryMetadataUARHasSize = 1 << 2; |
43 | |
44 | uint32_t meta_version; |
45 | const char *meta_start; |
46 | const char *meta_end; |
47 | const char *atomics_start; |
48 | const char *atomics_end; |
49 | }; // namespace |
50 | |
51 | extern "C" { |
52 | void __sanitizer_metadata_covered_add(uint32_t version, const char *start, |
53 | const char *end) { |
54 | const uint32_t version_base = version & 0xffff; |
55 | const bool offset_ptr_sized = version & (1 << 16); |
56 | assert(version_base == 2); |
57 | printf(format: "metadata add version %u\n" , version_base); |
58 | for (const char *pos = start; pos < end;) { |
59 | const auto base = reinterpret_cast<uintptr_t>(pos); |
60 | const intptr_t offset = offset_ptr_sized ? consume<intptr_t>(pos, end) |
61 | : consume<int32_t>(pos, end); |
62 | [[maybe_unused]] const uint64_t size = consume_uleb128(pos, end); |
63 | const uint64_t features = consume_uleb128(pos, end); |
64 | uint64_t stack_args = 0; |
65 | if (features & kSanitizerBinaryMetadataUARHasSize) |
66 | stack_args = consume_uleb128(pos, end); |
67 | if (const char *name = symbolize(pc: base + offset)) |
68 | printf(format: "%s: features=%lx stack_args=%lu\n" , name, features, stack_args); |
69 | } |
70 | meta_version = version; |
71 | meta_start = start; |
72 | meta_end = end; |
73 | } |
74 | |
75 | void __sanitizer_metadata_covered_del(uint32_t version, const char *start, |
76 | const char *end) { |
77 | assert(version == meta_version); |
78 | assert(start == meta_start); |
79 | assert(end == meta_end); |
80 | } |
81 | |
82 | void __sanitizer_metadata_atomics_add(uint32_t version, const char *start, |
83 | const char *end) { |
84 | assert(version == meta_version); |
85 | assert(start); |
86 | assert(end >= end); |
87 | atomics_start = start; |
88 | atomics_end = end; |
89 | } |
90 | |
91 | void __sanitizer_metadata_atomics_del(uint32_t version, const char *start, |
92 | const char *end) { |
93 | assert(version == meta_version); |
94 | assert(atomics_start == start); |
95 | assert(atomics_end == end); |
96 | } |
97 | } |
98 | |