1*a1e7e401SKazuaki Ishizaki // Check that we detect malloc/delete mismatch only if the appropriate flag
2673dc3d4SNico Weber // is set.
3673dc3d4SNico Weber 
4673dc3d4SNico Weber // RUN: %clangxx_asan -g %s -o %t 2>&1
5673dc3d4SNico Weber 
6673dc3d4SNico Weber // Find error and provide malloc context.
7673dc3d4SNico Weber // RUN: %env_asan_opts=alloc_dealloc_mismatch=1 not %run %t 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=ALLOC-STACK
8673dc3d4SNico Weber 
9673dc3d4SNico Weber // No error here.
10673dc3d4SNico Weber // RUN: %env_asan_opts=alloc_dealloc_mismatch=0 %run %t
11673dc3d4SNico Weber 
12673dc3d4SNico Weber // Also works if no malloc context is available.
13673dc3d4SNico Weber // RUN: %env_asan_opts=alloc_dealloc_mismatch=1:malloc_context_size=0:fast_unwind_on_malloc=0 not %run %t 2>&1 | FileCheck %s
14673dc3d4SNico Weber // RUN: %env_asan_opts=alloc_dealloc_mismatch=1:malloc_context_size=0:fast_unwind_on_malloc=1 not %run %t 2>&1 | FileCheck %s
15673dc3d4SNico Weber // REQUIRES: stable-runtime
16673dc3d4SNico Weber #include <stdlib.h>
17673dc3d4SNico Weber 
18673dc3d4SNico Weber static volatile char *x;
19673dc3d4SNico Weber 
main()20673dc3d4SNico Weber int main() {
21673dc3d4SNico Weber   x = (char*)malloc(10);
22673dc3d4SNico Weber   x[0] = 0;
23673dc3d4SNico Weber   delete x;
24673dc3d4SNico Weber }
25673dc3d4SNico Weber // CHECK: ERROR: AddressSanitizer: alloc-dealloc-mismatch (malloc vs operator delete) on 0x
26673dc3d4SNico Weber // CHECK-NEXT: #0{{.*}}operator delete
27673dc3d4SNico Weber // CHECK: #{{.*}}main
28673dc3d4SNico Weber // CHECK: is located 0 bytes inside of 10-byte region
29673dc3d4SNico Weber // CHECK-NEXT: allocated by thread T0 here:
30673dc3d4SNico Weber // ALLOC-STACK-NEXT: #0{{.*}}malloc
31673dc3d4SNico Weber // ALLOC-STACK: #{{.*}}main
32673dc3d4SNico Weber // CHECK: HINT: {{.*}} you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0
33