141 lines · cpp
1// RUN: %clangxx_asan -fno-sized-deallocation -fsanitize-recover=address -O0 %s -o %t2// RUN: %env_asan_opts=new_delete_type_mismatch=1:halt_on_error=false:detect_leaks=false %run %t 2>&1 | FileCheck %s3// RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t4 5// RUN: %clangxx_asan -fsized-deallocation -fsanitize-recover=address -O0 %s -o %t6// RUN: %env_asan_opts=new_delete_type_mismatch=1:halt_on_error=false:detect_leaks=false %run %t 2>&1 | FileCheck %s7// RUN: %env_asan_opts=new_delete_type_mismatch=0 %run %t8 9#include <stdio.h>10 11// Define all new/delete to do not depend on the version provided by the12// platform. The implementation is provided by ASan anyway.13 14namespace std {15struct nothrow_t {};16static const nothrow_t nothrow;17enum class align_val_t : size_t {};18} // namespace std19 20void *operator new(size_t);21void *operator new[](size_t);22void *operator new(size_t, std::nothrow_t const&);23void *operator new[](size_t, std::nothrow_t const&);24void *operator new(size_t, std::align_val_t);25void *operator new[](size_t, std::align_val_t);26void *operator new(size_t, std::align_val_t, std::nothrow_t const&);27void *operator new[](size_t, std::align_val_t, std::nothrow_t const&);28 29void operator delete(void*) throw();30void operator delete[](void*) throw();31void operator delete(void*, std::nothrow_t const&);32void operator delete[](void*, std::nothrow_t const&);33void operator delete(void*, size_t) throw();34void operator delete[](void*, size_t) throw();35void operator delete(void*, std::align_val_t) throw();36void operator delete[](void*, std::align_val_t) throw();37void operator delete(void*, std::align_val_t, std::nothrow_t const&);38void operator delete[](void*, std::align_val_t, std::nothrow_t const&);39void operator delete(void*, size_t, std::align_val_t) throw();40void operator delete[](void*, size_t, std::align_val_t) throw();41 42 43template<typename T>44inline T* break_optimization(T *arg) {45 __asm__ __volatile__("" : : "r" (arg) : "memory");46 return arg;47}48 49 50struct S12 { int a, b, c; };51struct alignas(128) S12_128 { int a, b, c; };52struct alignas(256) S12_256 { int a, b, c; };53struct alignas(512) S1024_512 { char a[1024]; };54struct alignas(1024) S1024_1024 { char a[1024]; };55 56 57int main(int argc, char **argv) {58 // Check the mismatched calls only, all the valid cases are verified in59 // 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-mismatch63 // 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-mismatch67 68 operator delete(break_optimization(new S12_128), sizeof(S12_128));69 // CHECK: AddressSanitizer: new-delete-type-mismatch70 // 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-mismatch74 75 operator delete[](break_optimization(new S12_128[100]), std::nothrow);76 // CHECK: AddressSanitizer: new-delete-type-mismatch77 // 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-mismatch81 82 operator delete[](break_optimization(new S12_128[100]), sizeof(S12_128[100]));83 // CHECK: AddressSanitizer: new-delete-type-mismatch84 // 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-mismatch88 89 // Various mismatched alignments.90 91 delete break_optimization(reinterpret_cast<S12*>(new S12_256));92 // CHECK: AddressSanitizer: new-delete-type-mismatch93 // 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-mismatch97 98 delete break_optimization(reinterpret_cast<S12_256*>(new S12));99 // CHECK: AddressSanitizer: new-delete-type-mismatch100 // 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-mismatch104 105 delete break_optimization(reinterpret_cast<S12_128*>(new S12_256));106 // CHECK: AddressSanitizer: new-delete-type-mismatch107 // 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-mismatch111 112 delete [] break_optimization(reinterpret_cast<S12*>(new S12_256[100]));113 // CHECK: AddressSanitizer: new-delete-type-mismatch114 // 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-mismatch118 119 delete [] break_optimization(reinterpret_cast<S12_256*>(new S12[100]));120 // CHECK: AddressSanitizer: new-delete-type-mismatch121 // 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-mismatch125 126 delete [] break_optimization(reinterpret_cast<S12_128*>(new S12_256[100]));127 // CHECK: AddressSanitizer: new-delete-type-mismatch128 // 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-mismatch132 133 // Push ASan limits, the current limitation is that it cannot differentiate134 // 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 bytes139 // CHECK-NEXT: Done140}141