Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | // RUN: %clangxx_asan -O %s -o %t |
| 2 | // RUN: not %run %t crash 2>&1 | FileCheck --check-prefix=CHECK-CRASH %s |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 5 | // RUN: %env_asan_opts=detect_container_overflow=0 %run %t crash |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 25 | // CHECK-CRASH: AddressSanitizer: container-overflow |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 26 | return (int)t[60 * one]; // Touches the poisoned memory. |
| 27 | } |
| 28 | |
| 29 | void BadBounds() { |
| 30 | long t[100]; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 31 | // CHECK-BAD-BOUNDS: ERROR: AddressSanitizer: bad parameters to __sanitizer_annotate_contiguous_container |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 32 | __sanitizer_annotate_contiguous_container(&t[0], &t[0] + 100, &t[0] + 101, |
| 33 | &t[0] + 50); |
| 34 | } |
| 35 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 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(); |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 50 | else if (!strcmp(argv[1], "bad-alignment")) |
| 51 | BadAlignment(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 52 | } |