1 // RUN: %clangxx_asan -std=c++1z -faligned-allocation -fsanitize-recover=address -O0 %s -o %t 2 // RUN: %env_asan_opts=new_delete_type_mismatch=1:halt_on_error=false:detect_leaks=false %run %t 2>&1 | FileCheck %s 3 // RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t 4 5 // RUN: %clangxx_asan -std=c++1z -faligned-allocation -fsized-deallocation -fsanitize-recover=address -O0 %s -o %t 6 // RUN: %env_asan_opts=new_delete_type_mismatch=1:halt_on_error=false:detect_leaks=false %run %t 2>&1 | FileCheck %s 7 // RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t 8 9 #include <stdio.h> 10 11 // Define all new/delete to do not depend on the version provided by the 12 // platform. The implementation is provided by ASan anyway. 13 14 namespace std { 15 struct nothrow_t {}; 16 static const nothrow_t nothrow; 17 enum class align_val_t : size_t {}; 18 } // namespace std 19 20 void *operator new(size_t); 21 void *operator new[](size_t); 22 void *operator new(size_t, std::nothrow_t const&); 23 void *operator new[](size_t, std::nothrow_t const&); 24 void *operator new(size_t, std::align_val_t); 25 void *operator new[](size_t, std::align_val_t); 26 void *operator new(size_t, std::align_val_t, std::nothrow_t const&); 27 void *operator new[](size_t, std::align_val_t, std::nothrow_t const&); 28 29 void operator delete(void*) throw(); 30 void operator delete[](void*) throw(); 31 void operator delete(void*, std::nothrow_t const&); 32 void operator delete[](void*, std::nothrow_t const&); 33 void operator delete(void*, size_t) throw(); 34 void operator delete[](void*, size_t) throw(); 35 void operator delete(void*, std::align_val_t) throw(); 36 void operator delete[](void*, std::align_val_t) throw(); 37 void operator delete(void*, std::align_val_t, std::nothrow_t const&); 38 void operator delete[](void*, std::align_val_t, std::nothrow_t const&); 39 void operator delete(void*, size_t, std::align_val_t) throw(); 40 void operator delete[](void*, size_t, std::align_val_t) throw(); 41 42 43 template<typename T> 44 inline T* break_optimization(T *arg) { 45 __asm__ __volatile__("" : : "r" (arg) : "memory"); 46 return arg; 47 } 48 49 50 struct S12 { int a, b, c; }; 51 struct alignas(128) S12_128 { int a, b, c; }; 52 struct alignas(256) S12_256 { int a, b, c; }; 53 struct alignas(512) S1024_512 { char a[1024]; }; 54 struct alignas(1024) S1024_1024 { char a[1024]; }; 55 56 57 int main(int argc, char **argv) { 58 // Check the mismatched calls only, all the valid cases are verified in 59 // test/sanitizer_common/TestCases/Linux/new_delete_test.cpp. 60 61 operator delete(break_optimization(new S12_128), std::nothrow); 62 // CHECK: AddressSanitizer: new-delete-type-mismatch 63 // CHECK: object passed to delete has wrong type: 64 // CHECK: alignment of the allocated type: 128 bytes; 65 // CHECK: alignment of the deallocated type: default-aligned. 66 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 67 68 operator delete(break_optimization(new S12_128), sizeof(S12_128)); 69 // CHECK: AddressSanitizer: new-delete-type-mismatch 70 // CHECK: object passed to delete has wrong type: 71 // CHECK: alignment of the allocated type: 128 bytes; 72 // CHECK: alignment of the deallocated type: default-aligned. 73 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 74 75 operator delete[](break_optimization(new S12_128[100]), std::nothrow); 76 // CHECK: AddressSanitizer: new-delete-type-mismatch 77 // CHECK: object passed to delete has wrong type: 78 // CHECK: alignment of the allocated type: 128 bytes; 79 // CHECK: alignment of the deallocated type: default-aligned. 80 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 81 82 operator delete[](break_optimization(new S12_128[100]), sizeof(S12_128[100])); 83 // CHECK: AddressSanitizer: new-delete-type-mismatch 84 // CHECK: object passed to delete has wrong type: 85 // CHECK: alignment of the allocated type: 128 bytes; 86 // CHECK: alignment of the deallocated type: default-aligned. 87 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 88 89 // Various mismatched alignments. 90 91 delete break_optimization(reinterpret_cast<S12*>(new S12_256)); 92 // CHECK: AddressSanitizer: new-delete-type-mismatch 93 // CHECK: object passed to delete has wrong type: 94 // CHECK: alignment of the allocated type: 256 bytes; 95 // CHECK: alignment of the deallocated type: default-aligned. 96 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 97 98 delete break_optimization(reinterpret_cast<S12_256*>(new S12)); 99 // CHECK: AddressSanitizer: new-delete-type-mismatch 100 // CHECK: object passed to delete has wrong type: 101 // CHECK: alignment of the allocated type: default-aligned; 102 // CHECK: alignment of the deallocated type: 256 bytes. 103 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 104 105 delete break_optimization(reinterpret_cast<S12_128*>(new S12_256)); 106 // CHECK: AddressSanitizer: new-delete-type-mismatch 107 // CHECK: object passed to delete has wrong type: 108 // CHECK: alignment of the allocated type: 256 bytes; 109 // CHECK: alignment of the deallocated type: 128 bytes. 110 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 111 112 delete [] break_optimization(reinterpret_cast<S12*>(new S12_256[100])); 113 // CHECK: AddressSanitizer: new-delete-type-mismatch 114 // CHECK: object passed to delete has wrong type: 115 // CHECK: alignment of the allocated type: 256 bytes; 116 // CHECK: alignment of the deallocated type: default-aligned. 117 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 118 119 delete [] break_optimization(reinterpret_cast<S12_256*>(new S12[100])); 120 // CHECK: AddressSanitizer: new-delete-type-mismatch 121 // CHECK: object passed to delete has wrong type: 122 // CHECK: alignment of the allocated type: default-aligned; 123 // CHECK: alignment of the deallocated type: 256 bytes. 124 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 125 126 delete [] break_optimization(reinterpret_cast<S12_128*>(new S12_256[100])); 127 // CHECK: AddressSanitizer: new-delete-type-mismatch 128 // CHECK: object passed to delete has wrong type: 129 // CHECK: alignment of the allocated type: 256 bytes; 130 // CHECK: alignment of the deallocated type: 128 bytes. 131 // CHECK: SUMMARY: AddressSanitizer: new-delete-type-mismatch 132 133 // Push ASan limits, the current limitation is that it cannot differentiate 134 // alignments above 512 bytes. 135 fprintf(stderr, "Checking alignments >= 512 bytes\n"); 136 delete break_optimization(reinterpret_cast<S1024_512*>(new S1024_1024)); 137 fprintf(stderr, "Done\n"); 138 // CHECK: Checking alignments >= 512 bytes 139 // CHECK-NEXT: Done 140 } 141