1*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -fexceptions -O %s -o %t && %run %t
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot // Test __sanitizer_annotate_contiguous_container.
4*7c3d14c8STreehugger Robot
5*7c3d14c8STreehugger Robot #include <stdlib.h>
6*7c3d14c8STreehugger Robot #include <stdio.h>
7*7c3d14c8STreehugger Robot #include <string.h>
8*7c3d14c8STreehugger Robot #include <assert.h>
9*7c3d14c8STreehugger Robot #include <sanitizer/asan_interface.h>
10*7c3d14c8STreehugger Robot
TestContainer(size_t capacity)11*7c3d14c8STreehugger Robot void TestContainer(size_t capacity) {
12*7c3d14c8STreehugger Robot char *beg = new char[capacity];
13*7c3d14c8STreehugger Robot char *end = beg + capacity;
14*7c3d14c8STreehugger Robot char *mid = beg + capacity;
15*7c3d14c8STreehugger Robot char *old_mid = 0;
16*7c3d14c8STreehugger Robot
17*7c3d14c8STreehugger Robot for (int i = 0; i < 10000; i++) {
18*7c3d14c8STreehugger Robot size_t size = rand() % (capacity + 1);
19*7c3d14c8STreehugger Robot assert(size <= capacity);
20*7c3d14c8STreehugger Robot old_mid = mid;
21*7c3d14c8STreehugger Robot mid = beg + size;
22*7c3d14c8STreehugger Robot __sanitizer_annotate_contiguous_container(beg, end, old_mid, mid);
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot for (size_t idx = 0; idx < size; idx++)
25*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(beg + idx));
26*7c3d14c8STreehugger Robot for (size_t idx = size; idx < capacity; idx++)
27*7c3d14c8STreehugger Robot assert(__asan_address_is_poisoned(beg + idx));
28*7c3d14c8STreehugger Robot assert(__sanitizer_verify_contiguous_container(beg, mid, end));
29*7c3d14c8STreehugger Robot assert(NULL ==
30*7c3d14c8STreehugger Robot __sanitizer_contiguous_container_find_bad_address(beg, mid, end));
31*7c3d14c8STreehugger Robot if (mid != beg) {
32*7c3d14c8STreehugger Robot assert(!__sanitizer_verify_contiguous_container(beg, mid - 1, end));
33*7c3d14c8STreehugger Robot assert(mid - 1 == __sanitizer_contiguous_container_find_bad_address(
34*7c3d14c8STreehugger Robot beg, mid - 1, end));
35*7c3d14c8STreehugger Robot }
36*7c3d14c8STreehugger Robot if (mid != end) {
37*7c3d14c8STreehugger Robot assert(!__sanitizer_verify_contiguous_container(beg, mid + 1, end));
38*7c3d14c8STreehugger Robot assert(mid == __sanitizer_contiguous_container_find_bad_address(
39*7c3d14c8STreehugger Robot beg, mid + 1, end));
40*7c3d14c8STreehugger Robot }
41*7c3d14c8STreehugger Robot }
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot // Don't forget to unpoison the whole thing before destroing/reallocating.
44*7c3d14c8STreehugger Robot __sanitizer_annotate_contiguous_container(beg, end, mid, end);
45*7c3d14c8STreehugger Robot for (size_t idx = 0; idx < capacity; idx++)
46*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(beg + idx));
47*7c3d14c8STreehugger Robot delete[] beg;
48*7c3d14c8STreehugger Robot }
49*7c3d14c8STreehugger Robot
50*7c3d14c8STreehugger Robot __attribute__((noinline))
Throw()51*7c3d14c8STreehugger Robot void Throw() { throw 1; }
52*7c3d14c8STreehugger Robot
53*7c3d14c8STreehugger Robot __attribute__((noinline))
ThrowAndCatch()54*7c3d14c8STreehugger Robot void ThrowAndCatch() {
55*7c3d14c8STreehugger Robot try {
56*7c3d14c8STreehugger Robot Throw();
57*7c3d14c8STreehugger Robot } catch(...) {
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot }
60*7c3d14c8STreehugger Robot
TestThrow()61*7c3d14c8STreehugger Robot void TestThrow() {
62*7c3d14c8STreehugger Robot char x[32];
63*7c3d14c8STreehugger Robot __sanitizer_annotate_contiguous_container(x, x + 32, x + 32, x + 14);
64*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(x + 13));
65*7c3d14c8STreehugger Robot assert(__asan_address_is_poisoned(x + 14));
66*7c3d14c8STreehugger Robot ThrowAndCatch();
67*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(x + 13));
68*7c3d14c8STreehugger Robot // FIXME: invert the assertion below once we fix
69*7c3d14c8STreehugger Robot // https://code.google.com/p/address-sanitizer/issues/detail?id=258
70*7c3d14c8STreehugger Robot // This assertion works only w/o UAR.
71*7c3d14c8STreehugger Robot if (!__asan_get_current_fake_stack())
72*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(x + 14));
73*7c3d14c8STreehugger Robot __sanitizer_annotate_contiguous_container(x, x + 32, x + 14, x + 32);
74*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(x + 13));
75*7c3d14c8STreehugger Robot assert(!__asan_address_is_poisoned(x + 14));
76*7c3d14c8STreehugger Robot }
77*7c3d14c8STreehugger Robot
main(int argc,char ** argv)78*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
79*7c3d14c8STreehugger Robot int n = argc == 1 ? 128 : atoi(argv[1]);
80*7c3d14c8STreehugger Robot for (int i = 0; i <= n; i++)
81*7c3d14c8STreehugger Robot TestContainer(i);
82*7c3d14c8STreehugger Robot TestThrow();
83*7c3d14c8STreehugger Robot }
84