Skip to content

Commit 4b4250c

Browse files
committed
[test][asan] Simplify loops in test
1 parent f0fbf51 commit 4b4250c

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Test __sanitizer_annotate_contiguous_container.
44

5+
#include <algorithm>
56
#include <assert.h>
67
#include <sanitizer/asan_interface.h>
78
#include <stdio.h>
@@ -23,10 +24,7 @@ void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
2324
char *beg = buffer + off_begin;
2425
char *end = beg + capacity;
2526
char *mid = poison_buffer ? beg : beg + capacity;
26-
char *old_mid = 0;
27-
// If after the container, there is another object, last granule
28-
// cannot be poisoned.
29-
char *cannot_poison = (poison_buffer) ? end : RoundDown(end);
27+
char *old_mid;
3028

3129
for (int i = 0; i < 1000; i++) {
3230
size_t size = rand() % (capacity + 1);
@@ -35,19 +33,21 @@ void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
3533
mid = beg + size;
3634
__sanitizer_annotate_contiguous_container(beg, end, old_mid, mid);
3735

38-
// If off buffer before the container was poisoned and we had to
39-
// unpoison it, we won't poison it again as we don't have information,
40-
// if it was poisoned.
41-
if (!poison_buffer)
42-
for (size_t idx = 0; idx < off_begin; idx++)
43-
assert(!__asan_address_is_poisoned(buffer + idx));
44-
for (size_t idx = 0; idx < size; idx++)
45-
assert(!__asan_address_is_poisoned(beg + idx));
46-
for (size_t idx = size; beg + idx < cannot_poison; idx++)
47-
assert(__asan_address_is_poisoned(beg + idx));
48-
for (size_t idx = 0; idx < kGranularity; idx++)
49-
assert(__asan_address_is_poisoned(end + idx) == poison_buffer);
50-
36+
char *cur = buffer;
37+
for (; cur < buffer + RoundDown(off_begin); ++cur)
38+
assert(__asan_address_is_poisoned(cur) == poison_buffer);
39+
// The prefix of the first incomplete granule can switch from poisoned to
40+
// unpoisoned but not otherwise.
41+
for (; cur < buffer + off_begin; ++cur)
42+
assert(poison_buffer || !__asan_address_is_poisoned(cur));
43+
for (; cur < mid; ++cur)
44+
assert(!__asan_address_is_poisoned(cur));
45+
for (; cur < RoundDown(end); ++cur)
46+
assert(__asan_address_is_poisoned(cur));
47+
// The suffix of the last incomplete granule must be poisoned the same as
48+
// bytes after the end.
49+
for (; cur != end + kGranularity; ++cur)
50+
assert(__asan_address_is_poisoned(cur) == poison_buffer);
5151
assert(__sanitizer_verify_contiguous_container(beg, mid, end));
5252
assert(NULL ==
5353
__sanitizer_contiguous_container_find_bad_address(beg, mid, end));

0 commit comments

Comments
 (0)