]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/asan/TestCases/contiguous_container_crash.cc
Vendor import of compiler-rt trunk r256633:
[FreeBSD/FreeBSD.git] / test / asan / TestCases / contiguous_container_crash.cc
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
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   return (int)t[60 * one];  // Touches the poisoned memory.
27 }
28
29 void BadBounds() {
30   long t[100];
31 // CHECK-BAD-BOUNDS: ERROR: AddressSanitizer: bad parameters to __sanitizer_annotate_contiguous_container
32   __sanitizer_annotate_contiguous_container(&t[0], &t[0] + 100, &t[0] + 101,
33                                             &t[0] + 50);
34 }
35
36 void BadAlignment() {
37   int t[100];
38 // CHECK-BAD-ALIGNMENT: ERROR: AddressSanitizer: bad parameters to __sanitizer_annotate_contiguous_container
39 // CHECK-BAD-ALIGNMENT: ERROR: beg is not aligned by 8
40   __sanitizer_annotate_contiguous_container(&t[1], &t[0] + 100, &t[1] + 10,
41                                             &t[0] + 50);
42 }
43
44 int main(int argc, char **argv) {
45   assert(argc == 2);
46   if (!strcmp(argv[1], "crash"))
47     return TestCrash();
48   else if (!strcmp(argv[1], "bad-bounds"))
49     BadBounds();
50   else if (!strcmp(argv[1], "bad-alignment"))
51     BadAlignment();
52 }