| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 1 | // RUN: %clang_scudo %s -o %t |
| Kostya Kortchinsky | 4391772 | 2017-08-16 16:40:48 +0000 | [diff] [blame^] | 2 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=1 not %run %t mallocdel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s |
| 3 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=0 %run %t mallocdel 2>&1 |
| 4 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=1 not %run %t newfree 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s |
| 5 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=0 %run %t newfree 2>&1 |
| 6 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=1 not %run %t memaligndel 2>&1 | FileCheck --check-prefix=CHECK-dealloc %s |
| 7 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=0 %run %t memaligndel 2>&1 |
| 8 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=1 not %run %t memalignrealloc 2>&1 | FileCheck --check-prefix=CHECK-realloc %s |
| 9 | // RUN: SCUDO_OPTIONS=DeallocationTypeMismatch=0 %run %t memalignrealloc 2>&1 |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 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> |
| Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 15 | #include <malloc.h> |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 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); |
| Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 24 | assert(p); |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 25 | delete p; |
| 26 | } |
| 27 | if (!strcmp(argv[1], "newfree")) { |
| 28 | int *p = new int; |
| Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 29 | assert(p); |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 30 | free((void *)p); |
| 31 | } |
| 32 | if (!strcmp(argv[1], "memaligndel")) { |
| Kostya Kortchinsky | 1148dc5 | 2016-11-30 17:32:20 +0000 | [diff] [blame] | 33 | int *p = (int *)memalign(16, 16); |
| Kostya Kortchinsky | 8d6257b | 2017-02-03 20:49:42 +0000 | [diff] [blame] | 34 | assert(p); |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 35 | delete p; |
| 36 | } |
| Kostya Kortchinsky | 4391772 | 2017-08-16 16:40:48 +0000 | [diff] [blame^] | 37 | if (!strcmp(argv[1], "memalignrealloc")) { |
| 38 | void *p = memalign(16, 16); |
| 39 | assert(p); |
| 40 | p = realloc(p, 32); |
| 41 | free(p); |
| 42 | } |
| Kostya Serebryany | 712fc98 | 2016-06-07 01:20:26 +0000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| Kostya Kortchinsky | 4391772 | 2017-08-16 16:40:48 +0000 | [diff] [blame^] | 46 | // CHECK-dealloc: ERROR: allocation type mismatch when deallocating address |
| 47 | // CHECK-realloc: ERROR: allocation type mismatch when reallocating address |