1// RUN: %clangxx_asan -O %s -o %t
2// RUN: not %run %t crash 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
3// RUN: not %run %t bad-bounds 2>&1 | FileCheck --check-prefix=CHECK-BAD-BOUNDS %s
4// RUN: not %run %t odd-alignment 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
5// RUN: not %run %t odd-alignment-end 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s
6// RUN: %env_asan_opts=detect_container_overflow=0 %run %t crash
7//
8// Test crash due to __sanitizer_annotate_contiguous_container.
9
10#include <assert.h>
11#include <string.h>
12
13extern "C" {
14void __sanitizer_annotate_contiguous_container(const void *beg, const void *end,
15 const void *old_mid,
16 const void *new_mid);
17} // extern "C"
18
19static volatile int one = 1;
20
21int TestCrash() {
22 long t[100];
23 t[60] = 0;
24 __sanitizer_annotate_contiguous_container(beg: &t[0], end: &t[0] + 100, old_mid: &t[0] + 100,
25 new_mid: &t[0] + 50);
26// CHECK-CRASH: AddressSanitizer: container-overflow
27// CHECK-CRASH: if you don't care about these errors you may set ASAN_OPTIONS=detect_container_overflow=0
28 return (int)t[60 * one]; // Touches the poisoned memory.
29}
30
31void BadBounds() {
32 long t[100];
33// CHECK-BAD-BOUNDS: ERROR: AddressSanitizer: bad parameters to __sanitizer_annotate_contiguous_container
34 __sanitizer_annotate_contiguous_container(beg: &t[0], end: &t[0] + 100, old_mid: &t[0] + 101,
35 new_mid: &t[0] + 50);
36}
37
38int OddAlignment() {
39 int t[100];
40 t[60] = 0;
41 __sanitizer_annotate_contiguous_container(beg: &t[1], end: &t[0] + 100, old_mid: &t[0] + 100,
42 new_mid: &t[1] + 50);
43 return (int)t[60 * one]; // Touches the poisoned memory.
44}
45
46int OddAlignmentEnd() {
47 int t[99];
48 t[60] = 0;
49 __sanitizer_annotate_contiguous_container(beg: &t[0], end: &t[0] + 98, old_mid: &t[0] + 98,
50 new_mid: &t[0] + 50);
51 return (int)t[60 * one]; // Touches the poisoned memory.
52}
53
54int main(int argc, char **argv) {
55 assert(argc == 2);
56 if (!strcmp(s1: argv[1], s2: "crash"))
57 return TestCrash();
58 else if (!strcmp(s1: argv[1], s2: "bad-bounds"))
59 BadBounds();
60 else if (!strcmp(s1: argv[1], s2: "odd-alignment"))
61 return OddAlignment();
62 else if (!strcmp(s1: argv[1], s2: "odd-alignment-end"))
63 return OddAlignmentEnd();
64 return 0;
65}
66

source code of compiler-rt/test/asan/TestCases/contiguous_container_crash.cpp