1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// RUN: %clangxx_asan -fexceptions -O %s -o %t && %env_asan_opts=detect_stack_use_after_return=0 %run %t
//
// Test __sanitizer_annotate_contiguous_container.
#include <algorithm>
#include <numeric>
#include <vector>
#include <assert.h>
#include <sanitizer/asan_interface.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static constexpr size_t kGranularity = 8;
template <class T> static constexpr T RoundDown(T x) {
return reinterpret_cast<T>(reinterpret_cast<uintptr_t>(x) &
~(kGranularity - 1));
}
static std::vector<int> GetPoisonedState(char *begin, char *end) {
std::vector<int> result;
for (; begin != end;) {
int poisoned = 0;
for (; begin != end && __asan_address_is_poisoned(begin); ++begin)
++poisoned;
result.push_back(poisoned);
int unpoisoned = 0;
for (; begin != end && !__asan_address_is_poisoned(begin); ++begin)
++unpoisoned;
result.push_back(unpoisoned);
}
return result;
}
static int GetFirstMismatch(const std::vector<int> &a,
const std::vector<int> &b) {
auto mismatch = std::mismatch(a.begin(), a.end(), b.begin(), b.end());
return std::accumulate(a.begin(), mismatch.first, 0) +
std::min(*mismatch.first, *mismatch.second);
}
void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
size_t buffer_size = capacity + off_begin + kGranularity * 2;
char *buffer = new char[buffer_size];
if (poison_buffer)
__asan_poison_memory_region(buffer, buffer_size);
char *st_beg = buffer + off_begin;
char *st_end = st_beg + capacity;
char *end = poison_buffer ? st_beg : st_end;
for (int i = 0; i < 1000; i++) {
size_t size = rand() % (capacity + 1);
assert(size <= capacity);
char *old_end = end;
end = st_beg + size;
__sanitizer_annotate_contiguous_container(st_beg, st_end, old_end, end);
char *cur = buffer;
for (; cur < RoundDown(st_beg); ++cur)
assert(__asan_address_is_poisoned(cur) == poison_buffer);
// The prefix of the first incomplete granule can switch from poisoned to
// unpoisoned but not otherwise.
for (; cur < st_beg; ++cur)
assert(poison_buffer || !__asan_address_is_poisoned(cur));
for (; cur < end; ++cur)
assert(!__asan_address_is_poisoned(cur));
for (; cur < RoundDown(st_end); ++cur)
assert(__asan_address_is_poisoned(cur));
// The suffix of the last incomplete granule must be poisoned the same as
// bytes after the end.
for (; cur != st_end + kGranularity; ++cur)
assert(__asan_address_is_poisoned(cur) == poison_buffer);
}
// Precalculate masks.
std::vector<std::vector<int>> masks(capacity + 1);
for (int i = 0; i <= capacity; i++) {
char *old_end = end;
end = st_beg + i;
__sanitizer_annotate_contiguous_container(st_beg, st_end, old_end, end);
masks[i] = GetPoisonedState(st_beg, st_end);
}
for (int i = 0; i <= capacity; i++) {
char *old_end = end;
end = st_beg + i;
__sanitizer_annotate_contiguous_container(st_beg, st_end, old_end, end);
char *cur_first = std::max(end - 2 * kGranularity, st_beg);
char *cur_last = std::min(end + 2 * kGranularity, st_end);
for (char *cur = cur_first; cur <= cur_last; ++cur) {
bool is_valid =
__sanitizer_verify_contiguous_container(st_beg, cur, st_end);
const void *bad_address =
__sanitizer_contiguous_container_find_bad_address(st_beg, cur,
st_end);
if (cur == end ||
// The last unaligned granule of the storage followed by unpoisoned
// bytes looks the same.
(!poison_buffer && RoundDown(st_end) <= std::min(cur, end))) {
assert(is_valid);
assert(!bad_address);
continue;
}
assert(!is_valid);
assert(bad_address == std::min(cur, end));
assert(bad_address ==
st_beg + GetFirstMismatch(masks[i], masks[cur - st_beg]));
}
}
__asan_unpoison_memory_region(buffer, buffer_size);
delete[] buffer;
}
void TestDoubleEndedContainer(size_t capacity, size_t off_begin,
bool poison_buffer) {
size_t buffer_size = capacity + off_begin + kGranularity * 2;
char *buffer = new char[buffer_size];
if (poison_buffer)
__asan_poison_memory_region(buffer, buffer_size);
char *st_beg = buffer + off_begin;
char *st_end = st_beg + capacity;
char *beg = st_beg;
char *end = poison_buffer ? st_beg : st_end;
for (int i = 0; i < 1000; i++) {
size_t size = rand() % (capacity + 1);
size_t skipped = rand() % (capacity - size + 1);
assert(size <= capacity);
char *old_beg = beg;
char *old_end = end;
beg = st_beg + skipped;
end = beg + size;
__sanitizer_annotate_double_ended_contiguous_container(
st_beg, st_end, old_beg, old_end, beg, end);
char *cur = buffer;
for (; cur < RoundDown(st_beg); ++cur)
assert(__asan_address_is_poisoned(cur) == poison_buffer);
// The prefix of the first incomplete granule can switch from poisoned to
// unpoisoned but not otherwise.
for (; cur < st_beg; ++cur)
assert(poison_buffer || !__asan_address_is_poisoned(cur));
if (beg != end) {
for (; cur < RoundDown(beg); ++cur)
assert(__asan_address_is_poisoned(cur));
for (; cur < end; ++cur)
assert(!__asan_address_is_poisoned(cur));
}
for (; cur < RoundDown(st_end); ++cur)
assert(__asan_address_is_poisoned(cur));
// The suffix of the last incomplete granule must be poisoned the same as
// bytes after the end.
for (; cur != st_end + kGranularity; ++cur)
assert(__asan_address_is_poisoned(cur) == poison_buffer);
}
if (capacity < 32) {
// Precalculate masks.
std::vector<std::vector<std::vector<int>>> masks(
capacity + 1, std::vector<std::vector<int>>(capacity + 1));
for (int i = 0; i <= capacity; i++) {
for (int j = i; j <= capacity; j++) {
char *old_beg = beg;
char *old_end = end;
beg = st_beg + i;
end = st_beg + j;
__sanitizer_annotate_double_ended_contiguous_container(
st_beg, st_end, old_beg, old_end, beg, end);
masks[i][j] = GetPoisonedState(st_beg, st_end);
}
}
for (int i = 0; i <= capacity; i++) {
for (int j = i; j <= capacity; j++) {
char *old_beg = beg;
char *old_end = end;
beg = st_beg + i;
end = st_beg + j;
__sanitizer_annotate_double_ended_contiguous_container(
st_beg, st_end, old_beg, old_end, beg, end);
// Try to mismatch the end of the container.
char *cur_first = std::max(end - 2 * kGranularity, beg);
char *cur_last = std::min(end + 2 * kGranularity, st_end);
for (char *cur = cur_first; cur <= cur_last; ++cur) {
bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
st_beg, beg, cur, st_end);
const void *bad_address =
__sanitizer_double_ended_contiguous_container_find_bad_address(
st_beg, beg, cur, st_end);
if (cur == end ||
// The last unaligned granule of the storage followed by unpoisoned
// bytes looks the same.
(!poison_buffer && RoundDown(st_end) <= std::min(cur, end))) {
assert(is_valid);
assert(!bad_address);
continue;
}
assert(!is_valid);
assert(bad_address);
assert(bad_address ==
st_beg +
GetFirstMismatch(masks[i][j], masks[i][cur - st_beg]));
}
// Try to mismatch the begin of the container.
cur_first = std::max(beg - 2 * kGranularity, st_beg);
cur_last = std::min(beg + 2 * kGranularity, end);
for (char *cur = cur_first; cur <= cur_last; ++cur) {
bool is_valid = __sanitizer_verify_double_ended_contiguous_container(
st_beg, cur, end, st_end);
const void *bad_address =
__sanitizer_double_ended_contiguous_container_find_bad_address(
st_beg, cur, end, st_end);
if (cur == beg ||
// The last unaligned granule of the storage followed by unpoisoned
// bytes looks the same.
(!poison_buffer && RoundDown(st_end) <= std::min(cur, beg) ||
// The first unaligned granule of non-empty container looks the
// same.
(std::max(beg, cur) < end &&
RoundDown(beg) == RoundDown(cur)))) {
assert(is_valid);
assert(!bad_address);
continue;
}
assert(!is_valid);
assert(bad_address);
assert(bad_address ==
st_beg +
GetFirstMismatch(masks[i][j], masks[cur - st_beg][j]));
}
}
}
}
__asan_unpoison_memory_region(buffer, buffer_size);
delete[] buffer;
}
__attribute__((noinline)) void Throw() { throw 1; }
__attribute__((noinline)) void ThrowAndCatch() {
try {
Throw();
} catch (...) {
}
}
void TestThrow() {
char x[32];
__sanitizer_annotate_contiguous_container(x, x + 32, x + 32, x + 14);
assert(!__asan_address_is_poisoned(x + 13));
assert(__asan_address_is_poisoned(x + 14));
ThrowAndCatch();
assert(!__asan_address_is_poisoned(x + 13));
assert(!__asan_address_is_poisoned(x + 14));
__sanitizer_annotate_contiguous_container(x, x + 32, x + 14, x + 32);
assert(!__asan_address_is_poisoned(x + 13));
assert(!__asan_address_is_poisoned(x + 14));
}
int main(int argc, char **argv) {
int n = argc == 1 ? 64 : atoi(argv[1]);
for (int i = 0; i <= n; i++) {
for (int j = 0; j < kGranularity * 2; j++) {
for (int poison = 0; poison < 2; ++poison) {
TestContainer(i, j, poison);
TestDoubleEndedContainer(i, j, poison);
}
}
}
TestThrow();
}
|