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