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// This issue got fixed in MSVC 2022 17.14; with vcruntime from that version,
25// this test does pass. (We could try to detect it and set a feature for when
26// this is known broken, but that detection would essentially a copy of this
27// test.)
28// UNSUPPORTED: target={{.+}}-windows-msvc
29
30#include <new>
31#include <cstddef>
32#include <cstdlib>
33#include <cassert>
34
35#include "test_macros.h"
36
37int new_called = 0;
38int delete_called = 0;
39
40TEST_WORKAROUND_BUG_109234844_WEAK
41void* operator new[](std::size_t s) TEST_THROW_SPEC(std::bad_alloc) {
42 ++new_called;
43 void* ret = std::malloc(s);
44 if (!ret) {
45 std::abort(); // placate MSVC's unchecked malloc warning (assert() won't silence it)
46 }
47 return ret;
48}
49
50void operator delete(void* p) TEST_NOEXCEPT {
51 ++delete_called;
52 std::free(p);
53}
54
55int main(int, char**) {
56 new_called = delete_called = 0;
57 int* x = DoNotOptimize(new (std::nothrow) int[3]);
58 assert(x != nullptr);
59 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
60
61 delete[] x;
62 ASSERT_WITH_OPERATOR_NEW_FALLBACKS(delete_called == 1);
63
64 return 0;
65}
66

source code of libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_nothrow.replace.indirect.pass.cpp