Skip to content

Commit f0fbf51

Browse files
committed
[test][asan] Simplify test
We don't need to iterate off_end, just need to check a granule after the end.
1 parent 80f4446 commit f0fbf51

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

compiler-rt/test/asan/TestCases/contiguous_container.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,18 @@ template <class T> static constexpr T RoundDown(T x) {
1515
~(kGranularity - 1));
1616
}
1717

18-
void TestContainer(size_t capacity, size_t off_begin, size_t off_end,
19-
bool poison_buffer) {
20-
char *buffer = new char[capacity + off_begin + off_end];
21-
char *buffer_end = buffer + capacity + off_begin + off_end;
18+
void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
19+
size_t buffer_size = capacity + off_begin + kGranularity * 2;
20+
char *buffer = new char[buffer_size];
2221
if (poison_buffer)
23-
__asan_poison_memory_region(buffer, buffer_end - buffer);
24-
else
25-
__asan_unpoison_memory_region(buffer, buffer_end - buffer);
22+
__asan_poison_memory_region(buffer, buffer_size);
2623
char *beg = buffer + off_begin;
2724
char *end = beg + capacity;
2825
char *mid = poison_buffer ? beg : beg + capacity;
2926
char *old_mid = 0;
3027
// If after the container, there is another object, last granule
3128
// cannot be poisoned.
32-
char *cannot_poison = (off_end == 0) ? end : RoundDown(end);
29+
char *cannot_poison = (poison_buffer) ? end : RoundDown(end);
3330

3431
for (int i = 0; i < 1000; i++) {
3532
size_t size = rand() % (capacity + 1);
@@ -48,13 +45,13 @@ void TestContainer(size_t capacity, size_t off_begin, size_t off_end,
4845
assert(!__asan_address_is_poisoned(beg + idx));
4946
for (size_t idx = size; beg + idx < cannot_poison; idx++)
5047
assert(__asan_address_is_poisoned(beg + idx));
51-
for (size_t idx = 0; idx < off_end; idx++)
48+
for (size_t idx = 0; idx < kGranularity; idx++)
5249
assert(__asan_address_is_poisoned(end + idx) == poison_buffer);
5350

5451
assert(__sanitizer_verify_contiguous_container(beg, mid, end));
5552
assert(NULL ==
5653
__sanitizer_contiguous_container_find_bad_address(beg, mid, end));
57-
size_t distance = (off_end > 0) ? kGranularity + 1 : 1;
54+
size_t distance = (end > RoundDown(end)) ? kGranularity + 1 : 1;
5855
if (mid >= beg + distance) {
5956
assert(
6057
!__sanitizer_verify_contiguous_container(beg, mid - distance, end));
@@ -71,7 +68,7 @@ void TestContainer(size_t capacity, size_t off_begin, size_t off_end,
7168
}
7269
}
7370

74-
__asan_unpoison_memory_region(buffer, buffer_end - buffer);
71+
__asan_unpoison_memory_region(buffer, buffer_size);
7572
delete[] buffer;
7673
}
7774

@@ -100,9 +97,8 @@ void TestThrow() {
10097
int main(int argc, char **argv) {
10198
int n = argc == 1 ? 64 : atoi(argv[1]);
10299
for (int i = 0; i <= n; i++)
103-
for (int j = 0; j < 8; j++)
104-
for (int k = 0; k < 8; k++)
105-
for (int poison = 0; poison < 2; ++poison)
106-
TestContainer(i, j, k, poison);
100+
for (int j = 0; j < kGranularity * 2; j++)
101+
for (int poison = 0; poison < 2; ++poison)
102+
TestContainer(i, j, poison);
107103
TestThrow();
108104
}

0 commit comments

Comments
 (0)