Skip to content

Commit f5bc6ee

Browse files
author
Kim Barrett
committed
8369187: Add wrapper for <new> that forbids use of global allocation and deallocation functions
Reviewed-by: stefank, erikj, jrose
1 parent 6f1c573 commit f5bc6ee

File tree

15 files changed

+187
-20
lines changed

15 files changed

+187
-20
lines changed

make/hotspot/lib/CompileGtest.gmk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ $(eval $(call SetupJdkLibrary, BUILD_GTEST_LIBJVM, \
9595
EXTRA_OBJECT_FILES := $(BUILD_LIBJVM_ALL_OBJS), \
9696
DEFAULT_CFLAGS := false, \
9797
CFLAGS := $(JVM_CFLAGS) \
98+
-DHOTSPOT_GTEST \
9899
-I$(GTEST_FRAMEWORK_SRC)/googletest/include \
99100
-I$(GTEST_FRAMEWORK_SRC)/googlemock/include \
100101
$(addprefix -I, $(GTEST_TEST_SRC)), \

src/hotspot/share/code/relocInfo.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "code/compiledIC.hpp"
2727
#include "code/nmethod.hpp"
2828
#include "code/relocInfo.hpp"
29+
#include "cppstdlib/new.hpp"
2930
#include "cppstdlib/type_traits.hpp"
3031
#include "memory/resourceArea.hpp"
3132
#include "memory/universe.hpp"
@@ -37,8 +38,6 @@
3738
#include "utilities/checkedCast.hpp"
3839
#include "utilities/copy.hpp"
3940

40-
#include <new>
41-
4241
const RelocationHolder RelocationHolder::none; // its type is relocInfo::none
4342

4443

src/hotspot/share/code/relocInfo.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@
2525
#ifndef SHARE_CODE_RELOCINFO_HPP
2626
#define SHARE_CODE_RELOCINFO_HPP
2727

28+
#include "cppstdlib/new.hpp"
2829
#include "memory/allocation.hpp"
2930
#include "oops/oopsHierarchy.hpp"
3031
#include "runtime/osInfo.hpp"
3132
#include "utilities/checkedCast.hpp"
3233
#include "utilities/globalDefinitions.hpp"
3334
#include "utilities/macros.hpp"
3435

35-
#include <new>
36-
3736
class CodeBlob;
3837
class Metadata;
3938
class NativeMovConstReg;
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/*
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#ifndef SHARE_CPPSTDLIB_NEW_HPP
26+
#define SHARE_CPPSTDLIB_NEW_HPP
27+
28+
#include "utilities/compilerWarnings.hpp"
29+
30+
// HotSpot usage:
31+
// Only the following may be used:
32+
// * std::nothrow_t, std::nothrow
33+
// * std::align_val_t
34+
// * The non-allocating forms of `operator new` and `operator new[]` are
35+
// implicitly used by the corresponding `new` and `new[]` expressions.
36+
// - operator new(size_t, void*) noexcept
37+
// - operator new[](size_t, void*) noexcept
38+
// Note that the non-allocating forms of `operator delete` and `operator
39+
// delete[]` are not used, since they are only invoked by a placement new
40+
// expression that fails by throwing an exception. But they might still
41+
// end up being referenced in such a situation.
42+
43+
BEGIN_ALLOW_FORBIDDEN_FUNCTIONS
44+
#include "utilities/vmassert_uninstall.hpp"
45+
46+
#include <new>
47+
48+
#include "utilities/vmassert_reinstall.hpp" // don't reorder
49+
END_ALLOW_FORBIDDEN_FUNCTIONS
50+
51+
// Deprecation declarations to forbid use of the default global allocator.
52+
// See C++17 21.6.1 Header <new> synopsis.
53+
54+
namespace std {
55+
56+
#if 0
57+
// We could deprecate exception types, for completeness, but don't bother. We
58+
// already have exceptions disabled, and run into compiler bugs when we try.
59+
//
60+
// gcc -Wattributes => type attributes ignored after type is already defined
61+
// See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122167
62+
//
63+
// clang -Wignored-attributes => attribute declaration must precede definition
64+
// The clang warning is https://github.com/llvm/llvm-project/issues/135481,
65+
// which should be fixed in clang 21.
66+
class [[deprecated]] bad_alloc;
67+
class [[deprecated]] bad_array_new_length;
68+
#endif // #if 0
69+
70+
// Forbid new_handler manipulation by HotSpot code, leaving it untouched for
71+
// use by application code.
72+
[[deprecated]] new_handler get_new_handler() noexcept;
73+
[[deprecated]] new_handler set_new_handler(new_handler) noexcept;
74+
75+
// Prefer HotSpot mechanisms for padding.
76+
//
77+
// The syntax for redeclaring these for deprecation is tricky, and not
78+
// supported by some versions of some compilers. Dispatch on compiler and
79+
// version to decide whether to redeclare deprecated.
80+
81+
#if defined(__clang__)
82+
#if __clang_major__ >= 19
83+
// clang18 and earlier may accept the declaration but go wrong with uses.
84+
// Different warnings and link-time failures are both possible.
85+
#define CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES 1
86+
#endif // restrict clang version
87+
88+
#elif defined(__GNUC__)
89+
#if (__GNUC__ > 13) || (__GNUC__ == 13 && __GNUC_MINOR__ >= 2)
90+
// g++11.5 accepts the declaration and reports deprecation for uses, but also
91+
// has link-time failure for uses. Haven't tested intermediate versions.
92+
#define CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES 1
93+
#endif // restrict gcc version
94+
95+
#elif defined(_MSVC)
96+
// VS2022-17.13.2 => error C2370: '...': redefinition; different storage class
97+
98+
#endif // Compiler dispatch
99+
100+
// Redeclare deprecated if such is supported.
101+
#ifdef CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES
102+
[[deprecated]] extern const size_t hardware_destructive_interference_size;
103+
[[deprecated]] extern const size_t hardware_constructive_interference_size;
104+
#undef CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES
105+
#endif // CAN_DEPRECATE_HARDWARE_INTERFERENCE_SIZES
106+
107+
} // namespace std
108+
109+
// Forbid using the global allocator by HotSpot code.
110+
// This doesn't provide complete coverage. Some global allocation and
111+
// deallocation functions are implicitly declared in all translation units,
112+
// without needing to include <new>; see C++17 6.7.4. So this doesn't remove
113+
// the need for the link-time verification that these functions aren't used.
114+
//
115+
// But don't poison them when compiling gtests. The gtest framework, the
116+
// HotSpot wrapper around it (gtestMain.cpp), and even some tests, all have
117+
// new/new[] and delete/delete[] expressions that use the default global
118+
// allocator. We also don't apply the link-time check for gtests, for the
119+
// same reason.
120+
#ifndef HOTSPOT_GTEST
121+
122+
[[deprecated]] void* operator new(std::size_t);
123+
[[deprecated]] void* operator new(std::size_t, std::align_val_t);
124+
[[deprecated]] void* operator new(std::size_t, const std::nothrow_t&) noexcept;
125+
[[deprecated]] void* operator new(std::size_t, std::align_val_t,
126+
const std::nothrow_t&) noexcept;
127+
128+
[[deprecated]] void operator delete(void*) noexcept;
129+
[[deprecated]] void operator delete(void*, std::size_t) noexcept;
130+
[[deprecated]] void operator delete(void*, std::align_val_t) noexcept;
131+
[[deprecated]] void operator delete(void*, std::size_t, std::align_val_t) noexcept;
132+
[[deprecated]] void operator delete(void*, const std::nothrow_t&) noexcept;
133+
[[deprecated]] void operator delete(void*, std::align_val_t,
134+
const std::nothrow_t&) noexcept;
135+
136+
[[deprecated]] void* operator new[](std::size_t);
137+
[[deprecated]] void* operator new[](std::size_t, std::align_val_t);
138+
[[deprecated]] void* operator new[](std::size_t, const std::nothrow_t&) noexcept;
139+
[[deprecated]] void* operator new[](std::size_t, std::align_val_t,
140+
const std::nothrow_t&) noexcept;
141+
142+
[[deprecated]] void operator delete[](void*) noexcept;
143+
[[deprecated]] void operator delete[](void*, std::size_t) noexcept;
144+
[[deprecated]] void operator delete[](void*, std::align_val_t) noexcept;
145+
[[deprecated]] void operator delete[](void*, std::size_t, std::align_val_t) noexcept;
146+
[[deprecated]] void operator delete[](void*, const std::nothrow_t&) noexcept;
147+
[[deprecated]] void operator delete[](void*, std::align_val_t,
148+
const std::nothrow_t&) noexcept;
149+
150+
#endif // HOTSPOT_GTEST
151+
152+
// Allow (don't poison) the non-allocating forms from [new.delete.placement].
153+
154+
#endif // SHARE_CPPSTDLIB_NEW_HPP

src/hotspot/share/gc/shared/bufferNode.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
*
2323
*/
2424

25+
#include "cppstdlib/new.hpp"
2526
#include "gc/shared/bufferNode.hpp"
2627
#include "memory/allocation.inline.hpp"
2728
#include "utilities/debug.hpp"
2829

29-
#include <new>
30-
3130
BufferNode::AllocatorConfig::AllocatorConfig(size_t size)
3231
: _buffer_capacity(size)
3332
{

src/hotspot/share/gc/shared/partialArrayState.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*
2323
*/
2424

25+
#include "cppstdlib/new.hpp"
2526
#include "gc/shared/partialArrayState.hpp"
2627
#include "memory/allocation.inline.hpp"
2728
#include "memory/arena.hpp"
@@ -33,8 +34,6 @@
3334
#include "utilities/globalDefinitions.hpp"
3435
#include "utilities/macros.hpp"
3536

36-
#include <new>
37-
3837
PartialArrayState::PartialArrayState(oop src, oop dst,
3938
size_t index, size_t length,
4039
size_t initial_refcount)

src/hotspot/share/gc/z/zDeferredConstructed.inline.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727

2828
#include "gc/z/zDeferredConstructed.hpp"
2929

30+
#include "cppstdlib/new.hpp"
3031
#include "cppstdlib/type_traits.hpp"
3132

32-
#include <new>
33-
3433
template <typename T>
3534
inline ZDeferredConstructed<T>::ZDeferredConstructed()
3635
DEBUG_ONLY(: _initialized(false)) {

src/hotspot/share/memory/allocation.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,13 @@
2525
#ifndef SHARE_MEMORY_ALLOCATION_HPP
2626
#define SHARE_MEMORY_ALLOCATION_HPP
2727

28+
#include "cppstdlib/new.hpp"
2829
#include "memory/allStatic.hpp"
2930
#include "nmt/memTag.hpp"
3031
#include "utilities/debug.hpp"
3132
#include "utilities/globalDefinitions.hpp"
3233
#include "utilities/macros.hpp"
3334

34-
#include <new>
35-
3635
class outputStream;
3736
class Thread;
3837
class JavaThread;

src/hotspot/share/memory/arena.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include "compiler/compilationMemoryStatistic.hpp"
27+
#include "cppstdlib/new.hpp"
2728
#include "memory/allocation.inline.hpp"
2829
#include "memory/arena.hpp"
2930
#include "memory/resourceArea.hpp"

src/hotspot/share/memory/arena.hpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
#include "utilities/globalDefinitions.hpp"
3232
#include "utilities/powerOfTwo.hpp"
3333

34-
#include <new>
35-
3634
// The byte alignment to be used by Arena::Amalloc.
3735
#define ARENA_AMALLOC_ALIGNMENT BytesPerLong
3836
#define ARENA_ALIGN(x) (align_up((x), ARENA_AMALLOC_ALIGNMENT))

0 commit comments

Comments
 (0)