1 // RUN: %clangxx_scudo %s -o %t
2 // RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t mallocdel       2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
3 // RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t mallocdel       2>&1
4 // RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t newfree         2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
5 // RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t newfree         2>&1
6 // RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memaligndel     2>&1 | FileCheck --check-prefix=CHECK-dealloc %s
7 // RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t memaligndel     2>&1
8 // RUN: %env_scudo_opts=DeallocationTypeMismatch=1 not %run %t memalignrealloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s
9 // RUN: %env_scudo_opts=DeallocationTypeMismatch=0     %run %t memalignrealloc 2>&1
10 
11 // Tests that type mismatches between allocation and deallocation functions are
12 // caught when the related option is set.
13 
14 #include <assert.h>
15 #include <malloc.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 int main(int argc, char **argv)
20 {
21   assert(argc == 2);
22   if (!strcmp(argv[1], "mallocdel")) {
23     int *p = (int *)malloc(16);
24     assert(p);
25     delete p;
26   }
27   if (!strcmp(argv[1], "newfree")) {
28     int *p = new int;
29     assert(p);
30     free((void *)p);
31   }
32   if (!strcmp(argv[1], "memaligndel")) {
33     int *p = (int *)memalign(16, 16);
34     assert(p);
35     delete p;
36   }
37   if (!strcmp(argv[1], "memalignrealloc")) {
38     void *p = memalign(16, 16);
39     assert(p);
40     p = realloc(p, 32);
41     free(p);
42   }
43   return 0;
44 }
45 
46 // CHECK-dealloc: ERROR: allocation type mismatch when deallocating address
47 // CHECK-realloc: ERROR: allocation type mismatch when reallocating address
48