1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // UNSUPPORTED: no-exceptions 10 11 // PR41395 isn't fixed until the dylib shipped with macOS 10.15 12 // XFAIL: with_system_cxx_lib=macosx10.14 13 // XFAIL: with_system_cxx_lib=macosx10.13 14 // XFAIL: with_system_cxx_lib=macosx10.12 15 // XFAIL: with_system_cxx_lib=macosx10.11 16 // XFAIL: with_system_cxx_lib=macosx10.10 17 // XFAIL: with_system_cxx_lib=macosx10.9 18 19 #include "cxxabi.h" 20 #include <new> 21 #include <cassert> 22 23 void dummy_ctor(void*) { assert(false && "should not be called"); } 24 void dummy_dtor(void*) { assert(false && "should not be called"); } 25 26 void *dummy_alloc(size_t) { assert(false && "should not be called"); } 27 void dummy_dealloc(void*) { assert(false && "should not be called"); } 28 void dummy_dealloc_sized(void*, size_t) { assert(false && "should not be called"); } 29 30 31 bool check_mul_overflows(size_t x, size_t y) { 32 size_t tmp = x * y; 33 if (tmp / x != y) 34 return true; 35 return false; 36 } 37 38 bool check_add_overflows(size_t x, size_t y) { 39 size_t tmp = x + y; 40 if (tmp < x) 41 return true; 42 43 return false; 44 } 45 46 void test_overflow_in_multiplication() { 47 const size_t elem_count = std::size_t(1) << (sizeof(std::size_t) * 8 - 2); 48 const size_t elem_size = 8; 49 const size_t padding = 0; 50 assert(check_mul_overflows(elem_count, elem_size)); 51 52 try { 53 __cxxabiv1::__cxa_vec_new(elem_count, elem_size, padding, dummy_ctor, 54 dummy_dtor); 55 assert(false && "allocation should fail"); 56 } catch (std::bad_array_new_length const&) { 57 // OK 58 } catch (...) { 59 assert(false && "unexpected exception"); 60 } 61 62 try { 63 __cxxabiv1::__cxa_vec_new2(elem_count, elem_size, padding, dummy_ctor, 64 dummy_dtor, &dummy_alloc, &dummy_dealloc); 65 assert(false && "allocation should fail"); 66 } catch (std::bad_array_new_length const&) { 67 // OK 68 } catch (...) { 69 assert(false && "unexpected exception"); 70 } 71 72 try { 73 __cxxabiv1::__cxa_vec_new3(elem_count, elem_size, padding, dummy_ctor, 74 dummy_dtor, &dummy_alloc, &dummy_dealloc_sized); 75 assert(false && "allocation should fail"); 76 } catch (std::bad_array_new_length const&) { 77 // OK 78 } catch (...) { 79 assert(false && "unexpected exception"); 80 } 81 } 82 83 void test_overflow_in_addition() { 84 const size_t elem_size = 4; 85 const size_t elem_count = static_cast<size_t>(-1) / 4u; 86 #if defined(_LIBCXXABI_ARM_EHABI) 87 const size_t padding = 8; 88 #else 89 const size_t padding = sizeof(std::size_t); 90 #endif 91 assert(!check_mul_overflows(elem_count, elem_size)); 92 assert(check_add_overflows(elem_count * elem_size, padding)); 93 try { 94 __cxxabiv1::__cxa_vec_new(elem_count, elem_size, padding, dummy_ctor, 95 dummy_dtor); 96 assert(false && "allocation should fail"); 97 } catch (std::bad_array_new_length const&) { 98 // OK 99 } catch (...) { 100 assert(false && "unexpected exception"); 101 } 102 103 104 try { 105 __cxxabiv1::__cxa_vec_new2(elem_count, elem_size, padding, dummy_ctor, 106 dummy_dtor, &dummy_alloc, &dummy_dealloc); 107 assert(false && "allocation should fail"); 108 } catch (std::bad_array_new_length const&) { 109 // OK 110 } catch (...) { 111 assert(false && "unexpected exception"); 112 } 113 114 try { 115 __cxxabiv1::__cxa_vec_new3(elem_count, elem_size, padding, dummy_ctor, 116 dummy_dtor, &dummy_alloc, &dummy_dealloc_sized); 117 assert(false && "allocation should fail"); 118 } catch (std::bad_array_new_length const&) { 119 // OK 120 } catch (...) { 121 assert(false && "unexpected exception"); 122 } 123 } 124 125 int main(int, char**) { 126 test_overflow_in_multiplication(); 127 test_overflow_in_addition(); 128 129 return 0; 130 } 131