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 | // void* operator new[](std::size_t, std::nothrow_t const&); |
10 | |
11 | // Test that we can replace the operator by replacing `operator new[](std::size_t)` (the throwing version). |
12 | |
13 | // This doesn't work when the shared library was built with exceptions disabled, because |
14 | // we can't implement the non-throwing new from the throwing new in that case. |
15 | // XFAIL: no-exceptions |
16 | |
17 | // UNSUPPORTED: sanitizer-new-delete |
18 | // XFAIL: libcpp-no-vcruntime |
19 | // XFAIL: LIBCXX-AIX-FIXME |
20 | |
21 | // MSVC/vcruntime falls back from the nothrow array new to the nothrow |
22 | // scalar new, instead of falling back on the throwing array new. |
23 | // https://developercommunity.visualstudio.com/t/vcruntime-nothrow-array-operator-new-fal/10373274 |
24 | // XFAIL: target={{.+}}-windows-msvc |
25 | |
26 | #include <new> |
27 | #include <cstddef> |
28 | #include <cstdlib> |
29 | #include <cassert> |
30 | |
31 | #include "test_macros.h" |
32 | |
33 | int new_called = 0; |
34 | int delete_called = 0; |
35 | |
36 | TEST_WORKAROUND_BUG_109234844_WEAK |
37 | void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) { |
38 | ++new_called; |
39 | void* ret = std::malloc(s); |
40 | if (!ret) { |
41 | std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it) |
42 | } |
43 | return ret; |
44 | } |
45 | |
46 | void operator delete(void* p) TEST_NOEXCEPT { |
47 | ++delete_called; |
48 | std::free(p); |
49 | } |
50 | |
51 | int main(int, char**) { |
52 | new_called = delete_called = 0; |
53 | int* x = DoNotOptimize(new (std::nothrow) int[3]); |
54 | assert(x != nullptr); |
55 | ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1); |
56 | |
57 | delete[] x; |
58 | ASSERT_WITH_OPERATOR_NEW_FALLBACKS(delete_called == 1); |
59 | |
60 | return 0; |
61 | } |
62 | |