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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

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