blob: 0e51da4a5e7419ee4b7108d2505b5e492b819de0 [file] [log] [blame]
Kostya Serebryany712fc982016-06-07 01:20:26 +00001// RUN: %clang_scudo %s -o %t
Kostya Kortchinsky43917722017-08-16 16:40:48 +00002// 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 Serebryany712fc982016-06-07 01:20:26 +000010
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 Kortchinsky8d6257b2017-02-03 20:49:42 +000015#include <malloc.h>
Kostya Serebryany712fc982016-06-07 01:20:26 +000016#include <stdlib.h>
17#include <string.h>
Kostya Serebryany712fc982016-06-07 01:20:26 +000018
19int main(int argc, char **argv)
20{
21 assert(argc == 2);
22 if (!strcmp(argv[1], "mallocdel")) {
23 int *p = (int *)malloc(16);
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000024 assert(p);
Kostya Serebryany712fc982016-06-07 01:20:26 +000025 delete p;
26 }
27 if (!strcmp(argv[1], "newfree")) {
28 int *p = new int;
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000029 assert(p);
Kostya Serebryany712fc982016-06-07 01:20:26 +000030 free((void *)p);
31 }
32 if (!strcmp(argv[1], "memaligndel")) {
Kostya Kortchinsky1148dc52016-11-30 17:32:20 +000033 int *p = (int *)memalign(16, 16);
Kostya Kortchinsky8d6257b2017-02-03 20:49:42 +000034 assert(p);
Kostya Serebryany712fc982016-06-07 01:20:26 +000035 delete p;
36 }
Kostya Kortchinsky43917722017-08-16 16:40:48 +000037 if (!strcmp(argv[1], "memalignrealloc")) {
38 void *p = memalign(16, 16);
39 assert(p);
40 p = realloc(p, 32);
41 free(p);
42 }
Kostya Serebryany712fc982016-06-07 01:20:26 +000043 return 0;
44}
45
Kostya Kortchinsky43917722017-08-16 16:40:48 +000046// CHECK-dealloc: ERROR: allocation type mismatch when deallocating address
47// CHECK-realloc: ERROR: allocation type mismatch when reallocating address