Skip to content

Commit 96297ba

Browse files
authored
merge main into amd-staging (llvm#3096)
2 parents 11ba8dc + d8eba45 commit 96297ba

File tree

236 files changed

+6010
-921
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

236 files changed

+6010
-921
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
=========================
2+
C++ Type Aware Allocators
3+
=========================
4+
5+
.. contents::
6+
:local:
7+
8+
Introduction
9+
============
10+
11+
Clang includes an implementation of P2719 "Type-aware allocation and deallocation
12+
functions".
13+
14+
This is a feature that extends the semantics of `new`, `new[]`, `delete` and
15+
`delete[]` operators to expose the type being allocated to the operator. This
16+
can be used to customize allocation of types without needing to modify the
17+
type declaration, or via template definitions fully generic type aware
18+
allocators.
19+
20+
P2719 introduces a type-identity tag as valid parameter type for all allocation
21+
operators. This tag is a default initialized value of type `std::type_identity<T>`
22+
where T is the type being allocated or deallocated. Unlike the other placement
23+
arguments this tag is passed as the first parameter to the operator.
24+
25+
The most basic use case is as follows
26+
27+
.. code-block:: c++
28+
29+
#include <new>
30+
#include <type_traits>
31+
32+
struct S {
33+
// ...
34+
};
35+
36+
void *operator new(std::type_identity<S>, size_t, std::align_val_t);
37+
void operator delete(std::type_identity<S>, void *, size_t, std::align_val_t);
38+
39+
void f() {
40+
S *s = new S; // calls ::operator new(std::type_identity<S>(), sizeof(S), alignof(S))
41+
delete s; // calls ::operator delete(std::type_identity<S>(), s, sizeof(S), alignof(S))
42+
}
43+
44+
While this functionality alone is powerful and useful, the true power comes
45+
by using templates. In addition to adding the type-identity tag, P2719 allows
46+
the tag parameter to be a dependent specialization of `std::type_identity`,
47+
updates the overload resolution rules to support full template deduction and
48+
constraint semantics, and updates the definition of usual deallocation functions
49+
to include `operator delete` definitions that are templatized on the
50+
type-identity tag.
51+
52+
This allows arbitrarily constrained definitions of the operators that resolve
53+
as would be expected for any other template function resolution, e.g (only
54+
showing `operator new` for brevity)
55+
56+
.. code-block:: c++
57+
58+
template <typename T, unsigned Size> struct Array {
59+
T buffer[Size];
60+
};
61+
62+
// Starting with a concrete type
63+
void *operator new(std::type_identity<Array<int, 5>>, size_t, std::align_val_t);
64+
65+
// Only care about five element arrays
66+
template <typename T>
67+
void *operator new(std::type_identity<Array<T, 5>>, size_t, std::align_val_t);
68+
69+
// An array of N floats
70+
template <unsigned N>
71+
void *operator new(std::type_identity<Array<float, N>>, size_t, std::align_val_t);
72+
73+
// Any array
74+
template <typename T, unsigned N>
75+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t);
76+
77+
// A handy concept
78+
template <typename T> concept Polymorphic = std::is_polymorphic_v<T>;
79+
80+
// Only applies is T is Polymorphic
81+
template <Polymorphic T, unsigned N>
82+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t);
83+
84+
// Any even length array
85+
template <typename T, unsigned N>
86+
void *operator new(std::type_identity<Array<T, N>>, size_t, std::align_val_t)
87+
requires(N%2 == 0);
88+
89+
Operator selection then proceeds according to the usual rules for choosing
90+
the best/most constrained match.
91+
92+
Any declaration of a type aware operator new or operator delete must include a
93+
matching complimentary operator defined in the same scope.
94+
95+
Notes
96+
=====
97+
98+
Unconstrained Global Operators
99+
------------------------------
100+
101+
Declaring an unconstrained type aware global operator `new` or `delete` (or
102+
`[]` variants) creates numerous hazards, similar to, but different from, those
103+
created by attempting to replace the non-type aware global operators. For that
104+
reason unconstrained operators are strongly discouraged.
105+
106+
Mismatching Constraints
107+
-----------------------
108+
109+
When declaring global type aware operators you should ensure the constraints
110+
applied to new and delete match exactly, and declare them together. This
111+
limits the risk of having mismatching operators selected due to differing
112+
constraints resulting in changes to prioritization when determining the most
113+
viable candidate.
114+
115+
Declarations Across Libraries
116+
-----------------------------
117+
118+
Declaring a typed allocator for a type in a separate TU or library creates
119+
similar hazards as different libraries and TUs may see (or select) different
120+
definitions.
121+
122+
Under this model something like this would be risky
123+
124+
.. code-block:: c++
125+
126+
template<typename T>
127+
void *operator new(std::type_identity<std::vector<T>>, size_t, std::align_val_t);
128+
129+
However this hazard is not present simply due to the use of the a type from
130+
another library:
131+
132+
.. code-block:: c++
133+
134+
template<typename T>
135+
struct MyType {
136+
T thing;
137+
};
138+
template<typename T>
139+
void *operator new(std::type_identity<MyType<std::vector<T>>>, size_t, std::align_val_t);
140+
141+
Here we see `std::vector` being used, but that is not the actual type being
142+
allocated.
143+
144+
Implicit and Placement Parameters
145+
---------------------------------
146+
147+
Type aware allocators are always passed both the implicit alignment and size
148+
parameters in all cases. Explicit placement parameters are supported after the
149+
mandatory implicit parameters.
150+
151+
Publication
152+
===========
153+
154+
`Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
155+
Louis Dionne, Oliver Hunt.

clang/docs/LanguageExtensions.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Clang Language Extensions
1515
AutomaticReferenceCounting
1616
PointerAuthentication
1717
MatrixTypes
18+
CXXTypeAwareAllocators
1819

1920
Introduction
2021
============
@@ -1540,6 +1541,13 @@ Use ``__has_feature(cxx_variable_templates)`` or
15401541
``__has_extension(cxx_variable_templates)`` to determine if support for
15411542
templated variable declarations is enabled.
15421543

1544+
C++ type aware allocators
1545+
^^^^^^^^^^^^^^^^^^^^^^^^^
1546+
1547+
Use ``__has_extension(cxx_type_aware_allocators)`` to determine the existence of
1548+
support for the future C++2d type aware allocator feature. For full details see
1549+
:doc:`C++ Type Aware Allocators <CXXTypeAwareAllocators>` for additional details.
1550+
15431551
C11
15441552
---
15451553

clang/docs/ReleaseNotes.rst

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ C++2c Feature Support
133133

134134
- Implemented `P0963R3 Structured binding declaration as a condition <https://wg21.link/P0963R3>`_.
135135

136-
- Implemented `P2719R4 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_.
137-
138136
- Implemented `P3618R0 Allow attaching main to the global module <https://wg21.link/P3618>`_.
139137

140138
C++23 Feature Support
@@ -1044,15 +1042,29 @@ Arm and AArch64 Support
10441042
`as specified here <https://github.com/ARM-software/acle/blob/main/main/acle.md#modal-8-bit-floating-point-extensions>`_
10451043
is now available.
10461044
- Support has been added for the following processors (command-line identifiers in parentheses):
1045+
10471046
- Arm Cortex-A320 (``cortex-a320``)
1047+
10481048
- For ARM targets, cc1as now considers the FPU's features for the selected CPU or Architecture.
10491049
- The ``+nosimd`` attribute is now fully supported for ARM. Previously, this had no effect when being used with
10501050
ARM targets, however this will now disable NEON instructions being generated. The ``simd`` option is
10511051
also now printed when the ``--print-supported-extensions`` option is used.
10521052
- When a feature that depends on NEON (``simd``) is used, NEON is now automatically enabled.
10531053
- When NEON is disabled (``+nosimd``), all features that depend on NEON will now be disabled.
10541054

1055-
- Support for __ptrauth type qualifier has been added.
1055+
- Pointer authentication
1056+
1057+
- Support for __ptrauth type qualifier has been added.
1058+
- Objective-C adoption of pointer authentication
1059+
1060+
- ``isa`` and ``super`` pointers are protected with address diversity and separate
1061+
usage specific discriminators.
1062+
- methodlist pointers and content are protected with address diversity and methodlist
1063+
pointers have a usage specific discriminator.
1064+
- ``class_ro_t`` pointers are protected with address diversity and usage specific
1065+
discriminators.
1066+
- ``SEL`` typed ivars are protected with address diversity and usage specific
1067+
discriminators.
10561068

10571069
- For AArch64, added support for generating executable-only code sections by using the
10581070
``-mexecute-only`` or ``-mpure-code`` compiler flags. (#GH125688)
@@ -1216,6 +1228,9 @@ New features
12161228
so frequent 'not yet implemented' diagnostics should be expected. Also, the
12171229
ACC MLIR dialect does not currently implement any lowering to LLVM-IR, so no
12181230
code generation is possible for OpenACC.
1231+
- Implemented `P2719R5 Type-aware allocation and deallocation functions <https://wg21.link/P2719>`_
1232+
as an extension in all C++ language modes.
1233+
12191234

12201235
Crash and bug fixes
12211236
^^^^^^^^^^^^^^^^^^^

clang/docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ Using Clang as a Compiler
6161
APINotes
6262
DebuggingCoroutines
6363
AMDGPUSupport
64+
CXXTypeAwareAllocators
6465
CommandGuide/index
6566
FAQ
6667

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2300,6 +2300,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
23002300
return getTypeDeclType(getObjCSelDecl());
23012301
}
23022302

2303+
PointerAuthQualifier getObjCMemberSelTypePtrAuth();
2304+
23032305
/// Retrieve the typedef declaration corresponding to the predefined
23042306
/// Objective-C 'Class' type.
23052307
TypedefDecl *getObjCClassDecl() const;

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10081,11 +10081,8 @@ def err_destroying_operator_delete_not_usual : Error<
1008110081
def err_type_aware_destroying_operator_delete : Error<
1008210082
"destroying delete is not permitted to be type aware">;
1008310083

10084-
def ext_cxx26_type_aware_allocators : ExtWarn<
10085-
"type aware allocators are a C++2c extension">, InGroup<CXX26>;
10086-
def warn_cxx26_type_aware_allocators : Warning<
10087-
"type aware allocators are incompatible with C++ standards before C++2c">,
10088-
DefaultIgnore, InGroup<CXXPre26Compat>;
10084+
def warn_ext_type_aware_allocators : ExtWarn<
10085+
"type aware allocators are a Clang extension">, InGroup<DiagGroup<"ext-cxx-type-aware-allocators">>;
1008910086
def err_type_aware_allocator_missing_matching_operator : Error<
1009010087
"declaration of type aware %0 in %1 must have matching type aware %2"
1009110088
>;

clang/include/clang/Basic/Features.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ FEATURE(ptrauth_indirect_gotos, LangOpts.PointerAuthIndirectGotos)
160160
FEATURE(ptrauth_init_fini, LangOpts.PointerAuthInitFini)
161161
FEATURE(ptrauth_init_fini_address_discrimination, LangOpts.PointerAuthInitFiniAddressDiscrimination)
162162
FEATURE(ptrauth_elf_got, LangOpts.PointerAuthELFGOT)
163+
164+
FEATURE(ptrauth_objc_isa, LangOpts.PointerAuthObjcIsa)
165+
FEATURE(ptrauth_objc_interface_sel, LangOpts.PointerAuthObjcInterfaceSel)
166+
FEATURE(ptrauth_objc_signable_class, true)
167+
FEATURE(ptrauth_objc_method_list_pointer, LangOpts.PointerAuthCalls)
168+
163169
EXTENSION(swiftcc,
164170
PP.getTargetInfo().checkCallingConvention(CC_Swift) ==
165171
clang::TargetInfo::CCCR_OK)
@@ -366,5 +372,8 @@ FEATURE(clang_atomic_attributes, true)
366372
FEATURE(cuda_noinline_keyword, LangOpts.CUDA)
367373
EXTENSION(cuda_implicit_host_device_templates, LangOpts.CUDA && LangOpts.OffloadImplicitHostDeviceTemplates)
368374

375+
// C++2d type-aware allocators
376+
EXTENSION(cxx_type_aware_allocators, true)
377+
369378
#undef EXTENSION
370379
#undef FEATURE

clang/include/clang/Basic/LangOptions.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ LANGOPT(PointerAuthInitFiniAddressDiscrimination, 1, 0, NotCompatible,
133133
LANGOPT(PointerAuthELFGOT, 1, 0, NotCompatible, "authenticate pointers from GOT")
134134
LANGOPT(AArch64JumpTableHardening, 1, 0, NotCompatible, "use hardened lowering for jump-table dispatch")
135135

136+
LANGOPT(PointerAuthObjcIsa, 1, 0, NotCompatible, "authentication of isa and super pointers in ObjC instances")
137+
LANGOPT(PointerAuthObjcInterfaceSel, 1, 0, NotCompatible, "authentication of SEL fields of ObjC interfaces")
138+
LANGOPT(PointerAuthObjcInterfaceSelKey, 16, 0, NotCompatible, "authentication key for SEL fields of ObjC interfaces")
139+
LANGOPT(PointerAuthObjcClassROPointers, 1, 0, Benign, "class_ro_t pointer authentication")
140+
136141
LANGOPT(DoubleSquareBracketAttributes, 1, 0, NotCompatible, "'[[]]' attributes extension for all language standard modes")
137142
LANGOPT(ExperimentalLateParseAttributes, 1, 0, NotCompatible, "experimental late parsing of attributes")
138143

clang/include/clang/Basic/PointerAuthOptions.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,26 @@ namespace clang {
2727
/// .fini_array. The value is ptrauth_string_discriminator("init_fini")
2828
constexpr uint16_t InitFiniPointerConstantDiscriminator = 0xD9D4;
2929

30+
/// Constant discriminator to be used with method list pointers. The value is
31+
/// ptrauth_string_discriminator("method_list_t")
32+
constexpr uint16_t MethodListPointerConstantDiscriminator = 0xC310;
33+
34+
/// Constant discriminator to be used with objective-c isa pointers. The value
35+
/// is ptrauth_string_discriminator("isa")
36+
constexpr uint16_t IsaPointerConstantDiscriminator = 0x6AE1;
37+
38+
/// Constant discriminator to be used with objective-c superclass pointers.
39+
/// The value is ptrauth_string_discriminator("objc_class:superclass")
40+
constexpr uint16_t SuperPointerConstantDiscriminator = 0xB5AB;
41+
42+
/// Constant discriminator to be used with objective-c sel pointers. The value
43+
/// is ptrauth_string_discriminator("sel")
44+
constexpr uint16_t SelPointerConstantDiscriminator = 0x57c2;
45+
46+
/// Constant discriminator to be used with objective-c class_ro_t pointers.
47+
/// The value is ptrauth_string_discriminator("class_data_bits")
48+
constexpr uint16_t ClassROConstantDiscriminator = 0x61F8;
49+
3050
constexpr unsigned PointerAuthKeyNone = -1;
3151

3252
/// Constant discriminator for std::type_info vtable pointers: 0xB1EA/45546
@@ -202,6 +222,21 @@ struct PointerAuthOptions {
202222

203223
/// The ABI for function addresses in .init_array and .fini_array
204224
PointerAuthSchema InitFiniPointers;
225+
226+
/// The ABI for Objective-C method lists.
227+
PointerAuthSchema ObjCMethodListFunctionPointers;
228+
229+
/// The ABI for a reference to an Objective-C method list in _class_ro_t.
230+
PointerAuthSchema ObjCMethodListPointer;
231+
232+
/// The ABI for Objective-C isa pointers.
233+
PointerAuthSchema ObjCIsaPointers;
234+
235+
/// The ABI for Objective-C superclass pointers.
236+
PointerAuthSchema ObjCSuperPointers;
237+
238+
/// The ABI for Objective-C class_ro_t pointers.
239+
PointerAuthSchema ObjCClassROPointers;
205240
};
206241

207242
} // end namespace clang

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4623,6 +4623,9 @@ defm ptrauth_init_fini_address_discrimination : OptInCC1FFlag<"ptrauth-init-fini
46234623
"Enable address discrimination of function pointers in init/fini arrays">;
46244624
defm ptrauth_elf_got : OptInCC1FFlag<"ptrauth-elf-got", "Enable authentication of pointers from GOT (ELF only)">;
46254625
defm aarch64_jump_table_hardening: OptInCC1FFlag<"aarch64-jump-table-hardening", "Use hardened lowering for jump-table dispatch">;
4626+
defm ptrauth_objc_isa : OptInCC1FFlag<"ptrauth-objc-isa", "Enable signing and authentication of Objective-C object's 'isa' field">;
4627+
defm ptrauth_objc_interface_sel : OptInCC1FFlag<"ptrauth-objc-interface-sel", "Enable signing and authentication of Objective-C object's 'SEL' fields">;
4628+
defm ptrauth_objc_class_ro : OptInCC1FFlag<"ptrauth-objc-class-ro", "Enable signing and authentication for ObjC class_ro pointers">;
46264629
}
46274630

46284631
def fenable_matrix : Flag<["-"], "fenable-matrix">, Group<f_Group>,

0 commit comments

Comments
 (0)