2
2
//
3
3
// Test __sanitizer_annotate_contiguous_container.
4
4
5
+ #include < algorithm>
5
6
#include < assert.h>
6
7
#include < sanitizer/asan_interface.h>
7
8
#include < stdio.h>
@@ -23,10 +24,7 @@ void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
23
24
char *beg = buffer + off_begin;
24
25
char *end = beg + capacity;
25
26
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;
30
28
31
29
for (int i = 0 ; i < 1000 ; i++) {
32
30
size_t size = rand () % (capacity + 1 );
@@ -35,19 +33,21 @@ void TestContainer(size_t capacity, size_t off_begin, bool poison_buffer) {
35
33
mid = beg + size;
36
34
__sanitizer_annotate_contiguous_container (beg, end, old_mid, mid);
37
35
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);
51
51
assert (__sanitizer_verify_contiguous_container (beg, mid, end));
52
52
assert (NULL ==
53
53
__sanitizer_contiguous_container_find_bad_address (beg, mid, end));
0 commit comments