Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 1 | // RUN: %clangxx -O2 %s -o %t && not %run %t 2>&1 | FileCheck %s |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 2 | |
| 3 | // REQUIRES: stable-runtime |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 4 | |
| 5 | #include <sanitizer/common_interface_defs.h> |
| 6 | #include <stdio.h> |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 7 | |
| 8 | volatile char *zero = 0; |
| 9 | |
| 10 | void Death() { |
| 11 | fprintf(stderr, "DEATH CALLBACK EXECUTED\n"); |
| 12 | } |
| 13 | // CHECK: DEATH CALLBACK EXECUTED |
| 14 | |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 15 | char global; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 16 | volatile char *sink; |
| 17 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 18 | __attribute__((noinline)) |
| 19 | void MaybeInit(int *uninitialized) { |
| 20 | if (zero) |
| 21 | *uninitialized = 1; |
| 22 | } |
| 23 | |
| 24 | __attribute__((noinline)) |
| 25 | void Leak() { |
| 26 | sink = new char[100]; // trigger lsan report. |
| 27 | } |
| 28 | |
| 29 | int main(int argc, char **argv) { |
| 30 | int uninitialized; |
| 31 | __sanitizer_set_death_callback(Death); |
| 32 | MaybeInit(&uninitialized); |
| 33 | if (uninitialized) // trigger msan report. |
Pirama Arumuga Nainar | 799172d | 2016-03-03 15:50:30 -0800 | [diff] [blame^] | 34 | global = 77; |
| 35 | sink = new char[100]; |
| 36 | delete[] sink; |
| 37 | global = sink[0]; // use-after-free: trigger asan/tsan report. |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 38 | Leak(); |
| 39 | sink = 0; |
| 40 | } |