1 // RUN: %clangxx_asan -fsized-deallocation -O0 %s -o %t
2 // RUN: not %run %t scalar 2>&1 | FileCheck %s -check-prefix=SCALAR
3 // RUN: %env_asan_opts=new_delete_type_mismatch=1 not %run %t scalar 2>&1 | FileCheck %s -check-prefix=SCALAR
4 // RUN: not %run %t array 2>&1 | FileCheck %s -check-prefix=ARRAY
5 // RUN: %env_asan_opts=new_delete_type_mismatch=1 not %run %t array 2>&1 | FileCheck %s -check-prefix=ARRAY
6 // RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t scalar
7 // RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t array
13 inline void break_optimization(void *arg
) {
14 __asm__
__volatile__("" : : "r" (arg
) : "memory");
27 ~D1() { fprintf(stderr
, "D1::~D1\n"); }
32 ~D2() { fprintf(stderr
, "D2::~D2\n"); }
36 break_optimization(x
);
39 void Del12NoThrow(S12
*x
) {
40 break_optimization(x
);
41 operator delete(x
, std::nothrow
);
43 void Del12Ar(S12
*x
) {
44 break_optimization(x
);
47 void Del12ArNoThrow(S12
*x
) {
48 break_optimization(x
);
49 operator delete[](x
, std::nothrow
);
52 int main(int argc
, char **argv
) {
53 if (argc
!= 2) return 1;
54 std::string flag
= argv
[1];
57 Del12NoThrow(new S12
);
58 Del12Ar(new S12
[100]);
59 Del12ArNoThrow(new S12
[100]);
61 // Here we pass wrong type of pointer to delete,
62 // but [] and nothrow variants of delete are not sized.
63 Del12Ar(reinterpret_cast<S12
*>(new S20
[100]));
64 Del12NoThrow(reinterpret_cast<S12
*>(new S20
));
65 Del12ArNoThrow(reinterpret_cast<S12
*>(new S20
[100]));
66 fprintf(stderr
, "OK SO FAR\n");
69 if (flag
== "scalar") {
70 // Here asan should bark as we are passing a wrong type of pointer
72 Del12(reinterpret_cast<S12
*>(new S20
));
73 // SCALAR: AddressSanitizer: new-delete-type-mismatch
74 // SCALAR: object passed to delete has wrong type:
75 // SCALAR: size of the allocated type: 20 bytes;
76 // SCALAR: size of the deallocated type: 12 bytes.
77 // SCALAR: is located 0 bytes inside of 20-byte region
78 // SCALAR: SUMMARY: AddressSanitizer: new-delete-type-mismatch
79 } else if (flag
== "array") {
80 D1
*d1
= reinterpret_cast<D1
*>(new D2
[10]);
81 break_optimization(d1
);
85 // ARRAY: AddressSanitizer: new-delete-type-mismatch
86 // ARRAY: size of the allocated type: 20{{4|8}} bytes;
87 // ARRAY: size of the deallocated type: 12{{4|8}} bytes.