[clang] [clang][PAC] Sign and authenticate atomics on __ptrauth types (PR #240090)
JF Bastien via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 21:02:31 PST 2026
https://github.com/jfbastien created https://github.com/llvm/llvm-project/pull/240090
Atomic builtins on `__ptrauth`-qualified objects either rejected address
diversity or operated on raw signed bits. Stores could write unsigned
values that later failed authentication, and implicit `_Atomic` operations
could assert in CodeGen.
Use the object's schema for initialization, load, store, exchange and
compare-exchange. Preserve source authentication when copying between
qualified objects.
Compare-exchange never signs the caller's expected value: a signature of
a caller-chosen value is a signing oracle wherever the compiler keeps it
in memory, and at -O2 a hoisted signature lives in a callee-saved
register across calls. Instead, load the object's signed representation,
authenticate it, compare plain values, and use that snapshot as the
hardware comparand. A strong exchange retries after interference, and
it now succeeds against a signed-null representation. Volatile
compare-exchange is rejected because of the extra read, and misaligned
atomic builtins because the library call passes signed values through
memory.
Route qualified integer constants through schema-aware emission. Sema
rejects nonzero constant initializers of qualified integers in C static
storage and in C++ `constexpr` and `constinit` objects; other C++ objects
initialize dynamically.
Reject atomic arithmetic, `__sync_*`, GNU generic by-address forms,
`__atomic_test_and_set`, `__atomic_clear`, `_Atomic` aggregates with
address-diverse fields, and implicit increment, decrement and compound
assignment. Also reject authenticated OpenMP atomic targets and
compare-capture results. These can be supported in follow-ups if needed.
Ordinary OpenMP read and capture can still store to qualified results.
Support is limited to directly qualified pointers and pointer-sized
integers. No LLVM IR or backend changes are needed.
Validated with real arm64e programs at -O0 and -O2, including a probe
that recovers no signature of the expected value from the stack or from
callee-saved registers.
Assisted-by: AI
>From 88e6c10ffe8bd26f200049dbfba0bb3f123fb046 Mon Sep 17 00:00:00 2001
From: JF Bastien <github at jfbastien.com>
Date: Sun, 8 Nov 2026 09:11:08 +0900
Subject: [PATCH] [clang][PAC] Sign and authenticate atomics on __ptrauth types
Atomic builtins on __ptrauth-qualified objects either rejected address
diversity or operated on raw signed bits. Stores could write unsigned
values that later failed authentication, and implicit _Atomic operations
could assert in CodeGen.
Use the object's schema for initialization, load, store, exchange and
compare-exchange. Preserve source authentication when copying between
qualified objects.
Compare-exchange never signs the caller's expected value: a signature of
a caller-chosen value is a signing oracle wherever the compiler keeps it
in memory, and at -O2 a hoisted signature lives in a callee-saved
register across calls. Instead, load the object's signed representation,
authenticate it, compare plain values, and use that snapshot as the
hardware comparand. A strong exchange retries after interference, and
it now succeeds against a signed-null representation. Volatile compare-exchange is rejected because of the
extra read, and misaligned atomic builtins because the library call
passes signed values through memory.
Route qualified integer constants through schema-aware emission. Sema
rejects nonzero constant initializers of qualified integers in C static
storage and in C++ constexpr and constinit objects; other C++ objects
initialize dynamically.
Reject atomic arithmetic, __sync_*, GNU generic by-address forms,
__atomic_test_and_set, __atomic_clear, _Atomic aggregates with
address-diverse fields, and implicit increment, decrement and compound
assignment. Also reject authenticated OpenMP atomic targets and
compare-capture results. These can be supported in follow-ups if needed.
Ordinary OpenMP read and capture can still store to qualified results.
Support is limited to directly qualified pointers and pointer-sized
integers. No LLVM IR or backend changes are needed.
Validated with real arm64e programs at -O0 and -O2, including a probe
that recovers no signature of the expected value from the stack or from
callee-saved registers.
Assisted-by: AI
---
clang/docs/PointerAuthentication.md | 21 ++
clang/docs/ReleaseNotes.md | 19 ++
.../clang/Basic/DiagnosticFrontendKinds.td | 5 +
.../clang/Basic/DiagnosticSemaKinds.td | 21 +-
clang/include/clang/Sema/Sema.h | 7 +
clang/lib/AST/ASTContext.cpp | 2 +-
clang/lib/CodeGen/CGAtomic.cpp | 164 +++++++++++---
clang/lib/CodeGen/CGExprConstant.cpp | 20 +-
clang/lib/CodeGen/CGExprScalar.cpp | 5 +-
clang/lib/CodeGen/CGPointerAuth.cpp | 16 +-
clang/lib/Sema/SemaChecking.cpp | 47 +++-
clang/lib/Sema/SemaDecl.cpp | 86 ++++++++
clang/lib/Sema/SemaExpr.cpp | 25 ++-
clang/lib/Sema/SemaOpenMP.cpp | 20 ++
clang/lib/Sema/SemaType.cpp | 5 +
clang/test/CodeGen/ptrauth-atomic-builtins.c | 125 +++++++++++
.../test/CodeGen/ptrauth-atomic-misaligned.c | 22 ++
clang/test/CodeGen/ptrauth-qualifier-atomic.c | 203 ++++++++++++++++++
.../ptrauth-integer-dynamic-init.cpp | 40 ++++
clang/test/OpenMP/atomic_ptrauth.c | 53 +++++
.../OpenMP/atomic_ptrauth_result_codegen.c | 24 +++
clang/test/Sema/ptrauth-atomic-ops.c | 181 ++++++++++++----
.../test/Sema/ptrauth-integer-constant-init.c | 34 +++
.../SemaCXX/ptrauth-integer-constant-init.cpp | 28 +++
24 files changed, 1078 insertions(+), 95 deletions(-)
create mode 100644 clang/test/CodeGen/ptrauth-atomic-builtins.c
create mode 100644 clang/test/CodeGen/ptrauth-atomic-misaligned.c
create mode 100644 clang/test/CodeGen/ptrauth-qualifier-atomic.c
create mode 100644 clang/test/CodeGenCXX/ptrauth-integer-dynamic-init.cpp
create mode 100644 clang/test/OpenMP/atomic_ptrauth.c
create mode 100644 clang/test/OpenMP/atomic_ptrauth_result_codegen.c
create mode 100644 clang/test/Sema/ptrauth-integer-constant-init.c
create mode 100644 clang/test/SemaCXX/ptrauth-integer-constant-init.cpp
diff --git a/clang/docs/PointerAuthentication.md b/clang/docs/PointerAuthentication.md
index 8a77d0178430a3..72f5bc46403822 100644
--- a/clang/docs/PointerAuthentication.md
+++ b/clang/docs/PointerAuthentication.md
@@ -431,6 +431,27 @@ a discriminator determined as follows:
is `ptrauth_blend_discriminator(&x, discriminator)`; see
[`ptrauth_blend_discriminator`](#ptrauth-blend-discriminator).
+Atomic initialization, load, store, exchange, and compare-exchange use the
+object's authentication schema, including address diversity. This covers implicit
+`_Atomic` initialization, loads, and stores, and the C11, OpenCL, GNU by-value
+(`_n`, including scoped), and HIP atomic builtins on directly qualified pointers
+and pointer-sized integers. Compare-exchange reads the object before the
+exchange, so it is rejected on `volatile` objects. libc++ `atomic` and
+`atomic_ref` specializations remain unsupported.
+
+Atomic arithmetic, `__sync_*`, GNU generic by-address forms,
+`__atomic_test_and_set`, `__atomic_clear`, misaligned builtins, and implicit
+increment, decrement, and compound assignment are rejected, as are `_Atomic`
+aggregates with address-diverse fields. OpenMP atomic targets and
+compare-capture result destinations cannot be qualified either. Ordinary OpenMP
+read and capture can store to qualified results.
+
+Numeric integer constants requiring signing cannot currently initialize static
+storage in C. C++ can initialize such objects dynamically, except where
+`constexpr` or `constinit` requires constant initialization. This applies to
+atomic and non-atomic integers. Null-preserving zero and supported symbol-address
+relocations remain constant-initializable.
+
##### Non-triviality from address diversity
Address diversity must impose additional restrictions in order to allow the
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 110dc79b7a7ae8..e26241cbd0d662 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -48,8 +48,16 @@ The previous behavior can be restored with `-Wno-error=unicode-whitespace`.
Clang will stop accepting non-ascii whitespaces as token separators
in a future version of Clang.
+- Unsupported atomic operations on `__ptrauth`-qualified storage now diagnose.
+ C static numeric integer initializers requiring signing also diagnose instead
+ of storing unsigned bits. See [pointer authentication](PointerAuthentication.html).
+
### C++ Specific Potentially Breaking Changes
+- A `__ptrauth`-qualified integer with a nonzero numeric initializer is now
+ initialized dynamically, since no relocation can sign it. `constexpr` and
+ `constinit` declarations of such objects with static storage are rejected.
+
- The `[[carries_dependency]]` attribute is no longer recognized, in any language
mode, as it was removed from the standard by
[P3475R2](https://wg21.link/P3475R2).
@@ -1336,6 +1344,17 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
pointers stored in VTTs via the `-fptrauth-vtt-vtable-pointer-discrimination`
option.
+- Atomic initialization, load, store, exchange, and compare-exchange now use
+ the `__ptrauth` object's authentication schema. For example, these builtins
+ sign on store and authenticate on load, using `&p` as the discriminator:
+
+ ```c
+ typedef int *__ptrauth(2, 1, 0) AuthPtr;
+ _Atomic AuthPtr p;
+ void store(int *value) { __c11_atomic_store(&p, value, __ATOMIC_RELAXED); }
+ int *load(void) { return __c11_atomic_load(&p, __ATOMIC_RELAXED); }
+ ```
+
- Added support for the `aarch64-unknown-uefi` target triple. Like
`x86_64-unknown-uefi`, it follows the Microsoft ABI conventions (LLP64,
Microsoft C++ ABI, `char *` based `va_list`) and links with `lld-link`.
diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index b51aa3ff0a0cb4..7c6f65ac2ac19b 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -378,6 +378,11 @@ def warn_atomic_op_misaligned : Warning<
"; the expected alignment (%0 bytes) exceeds the actual alignment (%1 bytes)">,
InGroup<AtomicAlignment>;
+def err_atomic_op_misaligned_ptrauth : Error<
+ "misaligned atomic operation on a '__ptrauth'-qualified type is not "
+ "supported; the expected alignment (%0 bytes) exceeds the actual alignment "
+ "(%1 bytes)">;
+
def warn_atomic_op_oversized : Warning<
"large atomic operation may incur "
"significant performance penalty"
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 80de4627959c4b..e51a4c55b27d98 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -7557,8 +7557,9 @@ def err_func_def_incomplete_result : Error<
def err_atomic_specifier_bad_type
: Error<"_Atomic cannot be applied to "
"%select{incomplete |array |function |reference |atomic |qualified "
- "|sizeless ||integer ||overflow behavior }0type "
- "%1 %select{|||||||which is not trivially copyable||in C23|}0">;
+ "|sizeless ||integer ||overflow behavior |}0type "
+ "%1 %select{|||||||which is not trivially copyable||in C23||which "
+ "contains an address-discriminated field}0">;
def warn_atomic_member_access : Warning<
"accessing a member of an atomic structure or union is undefined behavior">,
InGroup<DiagGroup<"atomic-access">>, DefaultError;
@@ -9697,9 +9698,19 @@ def err_atomic_op_needs_non_const_atomic : Error<
def err_atomic_op_needs_non_const_pointer : Error<
"address argument to atomic operation must be a pointer to non-const "
"type (%0 invalid)">;
-def err_atomic_op_needs_non_address_discriminated_pointer : Error<
- "address argument to %select{atomic|__sync}0 operation must be a pointer to a non address discriminated "
- "type (%1 invalid)">;
+def err_atomic_op_needs_non_ptrauth_pointer : Error<
+ "address argument to %select{__sync|atomic arithmetic|generic atomic|"
+ "atomic test-and-set or clear|volatile compare-exchange}0 "
+ "operation must not be a pointer to a '__ptrauth'-qualified type (%1 "
+ "invalid)">;
+def err_ptrauth_integer_constant_init : Error<
+ "'__ptrauth'-qualified integer cannot be %select{statically|constant}0 "
+ "initialized to a nonzero integer constant">;
+def err_omp_atomic_ptrauth : Error<
+ "OpenMP atomic operation on '__ptrauth'-qualified type %0 is not supported">;
+def err_ptrauth_atomic_rmw : Error<
+ "atomic %select{increment|decrement|compound assignment}0 of "
+ "'__ptrauth'-qualified type %1 is not supported">;
def err_atomic_op_needs_trivial_copy : Error<
"address argument to atomic operation must be a pointer to a "
"trivially-copyable type (%0 invalid)">;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 3abbaf6b1941ec..49b44316823de1 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -4079,6 +4079,13 @@ class Sema final : public SemaBase {
bool checkPointerAuthDiscriminatorArg(Expr *Arg, PointerAuthDiscArgKind Kind,
unsigned &IntVal);
+ /// Diagnose a constant initializer that stores a nonzero integer into a
+ /// `__ptrauth`-qualified integer, which no relocation can sign. \p Evaluate
+ /// runs only when \p T contains one.
+ void checkPointerAuthIntegerConstantInit(
+ QualType T, llvm::function_ref<const APValue *()> Evaluate,
+ SourceLocation Loc, bool Constexpr);
+
/// Diagnose function specifiers on a declaration of an identifier that
/// does not identify a function.
void DiagnoseFunctionSpecifiers(const DeclSpec &DS);
diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index 338a0eaf8d9707..f88dab2a410c09 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -1741,7 +1741,7 @@ ASTContext::findPointerAuthContent(QualType T) const {
if (T.hasAddressDiscriminatedPointerAuth())
return PointerAuthContent::AddressDiscriminatedData;
- const RecordDecl *RD = T->getAsRecordDecl();
+ const RecordDecl *RD = getBaseElementType(T)->getAsRecordDecl();
if (!RD)
return PointerAuthContent::None;
diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp
index cd7b278eab15d9..04b2e808a24ccf 100644
--- a/clang/lib/CodeGen/CGAtomic.cpp
+++ b/clang/lib/CodeGen/CGAtomic.cpp
@@ -375,6 +375,78 @@ bool AtomicInfo::emitMemSetZeroIfNecessary() const {
return true;
}
+/// Compare-exchange on `__ptrauth`-qualified storage. The caller's expected
+/// value is never signed: that would mint a signature for a caller-chosen
+/// value, a signing oracle wherever the compiler spills it. The comparand is
+/// the object's own signed representation, authenticated and compared as a
+/// plain value. Leaves the builder in \p StoreExpectedBB and returns the plain
+/// value to write back there, with the block that reaches \p ContinueBB on
+/// success.
+static std::pair<llvm::Value *, llvm::BasicBlock *>
+emitAuthenticatedAtomicCmpXchg(
+ CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak, Address Ptr,
+ llvm::Value *Expected, llvm::Value *Desired, PointerAuthQualifier PtrAuth,
+ llvm::BasicBlock *StoreExpectedBB, llvm::BasicBlock *ContinueBB,
+ llvm::AtomicOrdering SuccessOrder, llvm::AtomicOrdering FailureOrder,
+ llvm::SyncScope::ID Scope) {
+ // The snapshot is a second access, which volatile rules out.
+ assert(!E->isVolatile() && "volatile authenticated compare-exchange");
+ QualType ValueTy = E->getValueType().getUnqualifiedType();
+ auto Unqualify = [&](llvm::Value *Signed) {
+ return CGF.EmitPointerAuthUnqualify(PtrAuth, Signed, ValueTy, Ptr,
+ /*IsKnownNonNull=*/false);
+ };
+ llvm::BasicBlock *AuthBB = CGF.createBasicBlock("cmpxchg.auth", CGF.CurFn);
+ llvm::BasicBlock *TryBB = CGF.createBasicBlock("cmpxchg.try", CGF.CurFn);
+ llvm::BasicBlock *FailBB = CGF.createBasicBlock("cmpxchg.fail", CGF.CurFn);
+
+ // A failed compare-exchange is a load with the failure ordering.
+ llvm::LoadInst *Snapshot = CGF.Builder.CreateLoad(Ptr, "cmpxchg.snapshot");
+ Snapshot->setAtomic(FailureOrder, Scope);
+ CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Snapshot, E);
+ llvm::BasicBlock *EntryBB = CGF.Builder.GetInsertBlock();
+ CGF.Builder.CreateBr(AuthBB);
+
+ CGF.Builder.SetInsertPoint(AuthBB);
+ llvm::PHINode *Signed =
+ CGF.Builder.CreatePHI(Snapshot->getType(), 2, "cmpxchg.signed");
+ Signed->addIncoming(Snapshot, EntryBB);
+ llvm::Value *Plain = Unqualify(Signed);
+ llvm::Value *Equal =
+ CGF.Builder.CreateICmpEQ(Plain, Expected, "cmpxchg.equal");
+ llvm::BasicBlock *CompareBB = CGF.Builder.GetInsertBlock();
+ CGF.Builder.CreateCondBr(Equal, TryBB, StoreExpectedBB);
+
+ CGF.Builder.SetInsertPoint(TryBB);
+ llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
+ Ptr, Signed, Desired, SuccessOrder, FailureOrder, Scope);
+ Pair->setWeak(IsWeak);
+ CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Pair, E);
+ llvm::Value *Observed = CGF.Builder.CreateExtractValue(Pair, 0);
+ llvm::Value *Success = CGF.Builder.CreateExtractValue(Pair, 1);
+ CGF.Builder.CreateCondBr(Success, ContinueBB, FailBB);
+
+ CGF.Builder.SetInsertPoint(FailBB);
+ llvm::Value *WeakOld = nullptr;
+ llvm::BasicBlock *WeakFailBB = nullptr;
+ if (IsWeak) {
+ WeakOld = Unqualify(Observed);
+ WeakFailBB = CGF.Builder.GetInsertBlock();
+ CGF.Builder.CreateBr(StoreExpectedBB);
+ } else {
+ Signed->addIncoming(Observed, FailBB);
+ CGF.Builder.CreateBr(AuthBB);
+ }
+
+ CGF.Builder.SetInsertPoint(StoreExpectedBB);
+ llvm::PHINode *Old =
+ CGF.Builder.CreatePHI(Plain->getType(), 2, "cmpxchg.old");
+ Old->addIncoming(Plain, CompareBB);
+ if (IsWeak)
+ Old->addIncoming(WeakOld, WeakFailBB);
+ return {Old, TryBB};
+}
+
static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
Address Dest, Address Ptr, Address Val1,
Address Val2, Address ExpectedResult,
@@ -385,17 +457,6 @@ static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
llvm::Value *Expected = CGF.Builder.CreateLoad(Val1);
llvm::Value *Desired = CGF.Builder.CreateLoad(Val2);
- llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
- Ptr, Expected, Desired, SuccessOrder, FailureOrder, Scope);
- Pair->setVolatile(E->isVolatile());
- Pair->setWeak(IsWeak);
- CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Pair, E);
-
- // Cmp holds the result of the compare-exchange operation: true on success,
- // false on failure.
- llvm::Value *Old = CGF.Builder.CreateExtractValue(Pair, 0);
- llvm::Value *Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
-
// This basic block is used to hold the store instruction if the operation
// failed.
llvm::BasicBlock *StoreExpectedBB =
@@ -406,11 +467,32 @@ static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
llvm::BasicBlock *ContinueBB =
CGF.createBasicBlock("cmpxchg.continue", CGF.CurFn);
- // Update Expected if Expected isn't equal to Old, otherwise branch to the
- // exit point.
- CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
+ // Old is the value written back to Expected on failure. Cmp holds the result
+ // of the compare-exchange operation: true on success, false on failure.
+ llvm::Value *Old = nullptr, *Cmp = nullptr;
+ llvm::BasicBlock *SuccessBB = nullptr;
+ if (PointerAuthQualifier PtrAuth =
+ E->getPtr()->getType()->getPointeeType().getPointerAuth()) {
+ auto [AuthOld, TryBB] = emitAuthenticatedAtomicCmpXchg(
+ CGF, E, IsWeak, Ptr, Expected, Desired, PtrAuth, StoreExpectedBB,
+ ContinueBB, SuccessOrder, FailureOrder, Scope);
+ Old = AuthOld;
+ SuccessBB = TryBB;
+ } else {
+ llvm::AtomicCmpXchgInst *Pair = CGF.Builder.CreateAtomicCmpXchg(
+ Ptr, Expected, Desired, SuccessOrder, FailureOrder, Scope);
+ Pair->setVolatile(E->isVolatile());
+ Pair->setWeak(IsWeak);
+ CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Pair, E);
+ Old = CGF.Builder.CreateExtractValue(Pair, 0);
+ Cmp = CGF.Builder.CreateExtractValue(Pair, 1);
+
+ // Update Expected if Expected isn't equal to Old, otherwise branch to the
+ // exit point.
+ CGF.Builder.CreateCondBr(Cmp, ContinueBB, StoreExpectedBB);
+ CGF.Builder.SetInsertPoint(StoreExpectedBB);
+ }
- CGF.Builder.SetInsertPoint(StoreExpectedBB);
// Update the memory at Expected with Old's value.
llvm::Type *ExpectedType = ExpectedResult.getElementType();
const llvm::DataLayout &DL = CGF.CGM.getDataLayout();
@@ -438,9 +520,17 @@ static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak,
}
// Finally, branch to the exit point.
+ llvm::BasicBlock *FailureBB = CGF.Builder.GetInsertBlock();
CGF.Builder.CreateBr(ContinueBB);
CGF.Builder.SetInsertPoint(ContinueBB);
+ if (SuccessBB) {
+ llvm::PHINode *Phi =
+ CGF.Builder.CreatePHI(CGF.Builder.getInt1Ty(), 2, "cmpxchg.success");
+ Phi->addIncoming(CGF.Builder.getTrue(), SuccessBB);
+ Phi->addIncoming(CGF.Builder.getFalse(), FailureBB);
+ Cmp = Phi;
+ }
// Update the memory at Dest with Cmp's value.
CGF.EmitStoreOfScalar(Cmp, CGF.MakeAddrLValue(Dest, E->getType()));
}
@@ -566,6 +656,9 @@ static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
llvm::AtomicRMWInst::BinOp Op = llvm::AtomicRMWInst::Add;
bool PostOpMinMax = false;
unsigned PostOp = 0;
+ PointerAuthQualifier PtrAuth =
+ E->getPtr()->getType()->getPointeeType().getPointerAuth();
+ QualType ValueTy = E->getValueType().getUnqualifiedType();
switch (E->getOp()) {
case AtomicExpr::AO__c11_atomic_init:
@@ -633,8 +726,12 @@ static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
Load->setVolatile(E->isVolatile());
CGF.getTargetHooks().setTargetAtomicMetadata(CGF, *Load, E);
CGF.maybeAttachRangeForLoad(Load, E->getValueType(), E->getExprLoc());
- auto *I = CGF.Builder.CreateStore(Load, Dest);
- CGF.addInstToCurrentSourceAtom(I, Load);
+ llvm::Value *LoadVal = Load;
+ if (PtrAuth)
+ LoadVal = CGF.EmitPointerAuthUnqualify(PtrAuth, LoadVal, ValueTy, Ptr,
+ /*IsKnownNonNull=*/false);
+ auto *I = CGF.Builder.CreateStore(LoadVal, Dest);
+ CGF.addInstToCurrentSourceAtom(I, LoadVal);
return;
}
@@ -838,6 +935,11 @@ static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
// For __atomic_*_fetch operations, perform the operation again to
// determine the value which was written.
llvm::Value *Result = RMWI;
+ if (PtrAuth) {
+ assert(Op == llvm::AtomicRMWInst::Xchg);
+ Result = CGF.EmitPointerAuthUnqualify(PtrAuth, Result, ValueTy, Ptr,
+ /*IsKnownNonNull=*/false);
+ }
if (PostOpMinMax)
Result = EmitPostAtomicMinMax(CGF.Builder, E->getOp(),
E->getValueType()->isSignedIntegerType(),
@@ -854,11 +956,15 @@ static void EmitAtomicOp(CodeGenFunction &CGF, AtomicExpr *E, Address Dest,
// This function emits any expression (scalar, complex, or aggregate)
// into a temporary alloca.
-static Address
-EmitValToTemp(CodeGenFunction &CGF, Expr *E) {
+static Address EmitValToTemp(CodeGenFunction &CGF, Expr *E,
+ PointerAuthQualifier PtrAuth, Address Ptr) {
Address DeclPtr = CGF.CreateMemTempWithoutCast(E->getType(), ".atomictmp");
- CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
- /*Init*/ true);
+ if (PtrAuth)
+ CGF.Builder.CreateStore(CGF.EmitPointerAuthQualify(PtrAuth, E, Ptr),
+ DeclPtr);
+ else
+ CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(),
+ /*Init*/ true);
return DeclPtr;
}
@@ -948,6 +1054,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
QualType MemTy = AtomicTy;
if (const AtomicType *AT = AtomicTy->getAs<AtomicType>())
MemTy = AT->getValueType();
+ PointerAuthQualifier PtrAuth = AtomicTy.getPointerAuth();
llvm::Value *IsWeak = nullptr, *OrderFail = nullptr;
Address Val1 = Address::invalid();
@@ -972,7 +1079,10 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
bool Misaligned = !Ptr.getAlignment().isMultipleOf(TInfo.Width);
bool Oversized = getContext().toBits(TInfo.Width) > MaxInlineWidthInBits;
if (Misaligned) {
- Diags.Report(E->getBeginLoc(), diag::warn_atomic_op_misaligned)
+ // The library call takes its operands through memory.
+ Diags.Report(E->getBeginLoc(), PtrAuth
+ ? diag::err_atomic_op_misaligned_ptrauth
+ : diag::warn_atomic_op_misaligned)
<< (int)TInfo.Width.getQuantity()
<< (int)Ptr.getAlignment().getQuantity();
}
@@ -1030,7 +1140,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
E->getOp() == AtomicExpr::AO__scoped_atomic_compare_exchange)
Val2 = EmitPointerWithAlignment(E->getVal2());
else
- Val2 = EmitValToTemp(*this, E->getVal2());
+ Val2 = EmitValToTemp(*this, E->getVal2(), PtrAuth, Ptr);
OrderFail = EmitScalarExpr(E->getOrderFail());
if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange_n ||
E->getOp() == AtomicExpr::AO__atomic_compare_exchange ||
@@ -1133,7 +1243,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) {
case AtomicExpr::AO__atomic_fetch_fmaximum:
case AtomicExpr::AO__atomic_fetch_fminimum_num:
case AtomicExpr::AO__atomic_fetch_fmaximum_num:
- Val1 = EmitValToTemp(*this, E->getVal1());
+ Val1 = EmitValToTemp(*this, E->getVal1(), PtrAuth, Ptr);
break;
}
@@ -2222,7 +2332,11 @@ void CodeGenFunction::EmitAtomicInit(Expr *init, LValue dest) {
switch (atomics.getEvaluationKind()) {
case TEK_Scalar: {
- llvm::Value *value = EmitScalarExpr(init);
+ llvm::Value *value;
+ if (PointerAuthQualifier PtrAuth = dest.getQuals().getPointerAuth())
+ value = EmitPointerAuthQualify(PtrAuth, init, dest.getAddress());
+ else
+ value = EmitScalarExpr(init);
atomics.emitCopyIntoMemory(RValue::get(value));
return;
}
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index e2ec07bbae711a..ef8d09f0975ce6 100644
--- a/clang/lib/CodeGen/CGExprConstant.cpp
+++ b/clang/lib/CodeGen/CGExprConstant.cpp
@@ -1947,8 +1947,13 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
if (!destType->isReferenceType()) {
QualType nonMemoryDestType = getNonMemoryType(CGM, destType);
- if (llvm::Constant *C = ConstExprEmitter(*this).Visit(E, nonMemoryDestType))
- return emitForMemory(C, destType);
+ // Integer expressions go through APValue emission: the destination
+ // authentication schema can prevent constant initialization.
+ if (!nonMemoryDestType.getPointerAuth() ||
+ !nonMemoryDestType->isIntegerType())
+ if (llvm::Constant *C =
+ ConstExprEmitter(*this).Visit(E, nonMemoryDestType))
+ return emitForMemory(C, destType);
}
// Try to emit the initializer. Note that this can allow some things that
@@ -1956,7 +1961,13 @@ llvm::Constant *ConstantEmitter::tryEmitPrivateForVarInit(const VarDecl &D) {
if (const APValue *value = D.evaluateValue()) {
assert(!value->allowConstexprUnknown() &&
"Constexpr unknown values are not allowed in CodeGen");
- return tryEmitPrivateForMemory(*value, destType);
+ llvm::Constant *C = tryEmitPrivateForMemory(*value, destType);
+ // A required constant initializer must not fall back to dynamic
+ // initialization silently.
+ if (!C && !isAbstract() && D.hasGlobalStorage() &&
+ (D.isConstexpr() || D.hasAttr<ConstInitAttr>()))
+ CGM.ErrorUnsupported(&D, "constant initializer");
+ return C;
}
return nullptr;
@@ -2115,7 +2126,8 @@ llvm::Constant *ConstantEmitter::tryEmitPrivate(const Expr *E,
QualType destType) {
assert(!destType->isVoidType() && "can't emit a void constant");
- if (!destType->isReferenceType())
+ if (!destType->isReferenceType() &&
+ (!destType.getPointerAuth() || !destType->isIntegerType()))
if (llvm::Constant *C = ConstExprEmitter(*this).Visit(E, destType))
return C;
diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp
index 0e2e29f0745d27..e18beeec11ecf4 100644
--- a/clang/lib/CodeGen/CGExprScalar.cpp
+++ b/clang/lib/CodeGen/CGExprScalar.cpp
@@ -2513,7 +2513,10 @@ static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E) {
}
bool CodeGenFunction::isPointerKnownNonNull(const Expr *E) {
- assert(E->getType()->isSignableType(getContext()));
+ QualType Ty = E->getType();
+ if (const auto *Atomic = Ty->getAs<AtomicType>())
+ Ty = Atomic->getValueType();
+ assert(Ty->isSignableType(getContext()));
E = E->IgnoreParens();
diff --git a/clang/lib/CodeGen/CGPointerAuth.cpp b/clang/lib/CodeGen/CGPointerAuth.cpp
index 438d08b53a742c..b44be4b455efa5 100644
--- a/clang/lib/CodeGen/CGPointerAuth.cpp
+++ b/clang/lib/CodeGen/CGPointerAuth.cpp
@@ -191,6 +191,8 @@ CGPointerAuthInfo CodeGenModule::getPointerAuthInfoForPointeeType(QualType T) {
/// pointer type.
static CGPointerAuthInfo getPointerAuthInfoForType(CodeGenModule &CGM,
QualType PointerType) {
+ if (const AtomicType *Atomic = PointerType->getAs<AtomicType>())
+ PointerType = Atomic->getValueType();
assert(PointerType->isSignableType(CGM.getContext()));
// Block pointers are currently not signed.
@@ -225,9 +227,12 @@ emitLoadOfOrigPointerRValue(CodeGenFunction &CGF, const LValue &LV,
/// needlessly resigning the pointer.
std::pair<llvm::Value *, CGPointerAuthInfo>
CodeGenFunction::EmitOrigPointerRValue(const Expr *E) {
- assert(E->getType()->isSignableType(getContext()));
-
E = E->IgnoreParens();
+ QualType Ty = E->getType();
+ if (const auto *Atomic = Ty->getAs<AtomicType>())
+ Ty = Atomic->getValueType();
+ assert(Ty->isSignableType(getContext()));
+
if (const auto *Load = dyn_cast<ImplicitCastExpr>(E)) {
if (Load->getCastKind() == CK_LValueToRValue) {
E = Load->getSubExpr()->IgnoreParens();
@@ -262,6 +267,13 @@ CodeGenFunction::EmitPointerAuthQualify(PointerAuthQualifier DestQualifier,
const Expr *E,
Address DestStorageAddress) {
assert(DestQualifier);
+ E = E->IgnoreParens();
+ while (const auto *Cast = dyn_cast<CastExpr>(E)) {
+ if (Cast->getCastKind() != CK_NonAtomicToAtomic &&
+ Cast->getCastKind() != CK_AtomicToNonAtomic)
+ break;
+ E = Cast->getSubExpr()->IgnoreParens();
+ }
auto [Value, CurAuthInfo] = EmitOrigPointerRValue(E);
CGPointerAuthInfo DestAuthInfo =
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 4b9c1d187dfeb9..31d684a119ff56 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -5461,6 +5461,7 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
}
}
+ PointerAuthQualifier PointerAuth = AtomTy.getPointerAuth();
if (Form != TestAndSetByte && Form != ClearByte) {
// Pointer to object of size zero is not allowed.
if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy,
@@ -5473,6 +5474,12 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
return ExprError();
}
} else {
+ // The byte forms would write raw bits into signed storage.
+ if (PointerAuth) {
+ Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_ptrauth_pointer)
+ << 3 << Ptr->getType() << Ptr->getSourceRange();
+ return ExprError();
+ }
// The __atomic_clear and __atomic_test_and_set intrinsics accept any
// non-const pointer type, including void* and pointers to incomplete
// structs, but only access the first byte.
@@ -5485,12 +5492,32 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange,
ValType = AtomTy;
}
- PointerAuthQualifier PointerAuth = AtomTy.getPointerAuth();
- if (PointerAuth && PointerAuth.isAddressDiscriminated()) {
- Diag(ExprRange.getBegin(),
- diag::err_atomic_op_needs_non_address_discriminated_pointer)
- << 0 << Ptr->getType() << Ptr->getSourceRange();
- return ExprError();
+ if (PointerAuth) {
+ if (Form == Arithmetic) {
+ Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_ptrauth_pointer)
+ << 1 << Ptr->getType() << Ptr->getSourceRange();
+ return ExprError();
+ }
+ if (!IsC11 && !IsHIP && !IsN) {
+ Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_ptrauth_pointer)
+ << 2 << Ptr->getType() << Ptr->getSourceRange();
+ return ExprError();
+ }
+ // Compare-exchange on signed storage reads the object first, which
+ // volatile does not allow.
+ if ((Form == C11CmpXchg || Form == GNUCmpXchg) &&
+ AtomTy.isVolatileQualified()) {
+ Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_ptrauth_pointer)
+ << 4 << Ptr->getType() << Ptr->getSourceRange();
+ return ExprError();
+ }
+ }
+
+ // The by-value forms yield the plain value, as the __c11 forms do.
+ if (!IsC11 && PointerAuth) {
+ Qualifiers Qs = ValType.getQualifiers();
+ Qs.removePointerAuth();
+ ValType = Context.getQualifiedType(ValType.getUnqualifiedType(), Qs);
}
// For an arithmetic operation, the implied arithmetic must be well-formed.
@@ -5872,11 +5899,9 @@ ExprResult Sema::BuiltinAtomicOverloaded(ExprResult TheCallResult) {
<< FirstArg->getType() << 0 << FirstArg->getSourceRange();
return ExprError();
}
- PointerAuthQualifier PointerAuth = ValType.getPointerAuth();
- if (PointerAuth && PointerAuth.isAddressDiscriminated()) {
- Diag(FirstArg->getBeginLoc(),
- diag::err_atomic_op_needs_non_address_discriminated_pointer)
- << 1 << ValType << FirstArg->getSourceRange();
+ if (ValType.getPointerAuth()) {
+ Diag(FirstArg->getBeginLoc(), diag::err_atomic_op_needs_non_ptrauth_pointer)
+ << 0 << FirstArg->getType() << FirstArg->getSourceRange();
return ExprError();
}
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d4fbf04ee284af..16fd02bf97cf83 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -13077,6 +13077,77 @@ void Sema::CheckMSVCRTEntryPoint(FunctionDecl *FD) {
}
}
+/// Whether \p Value stores a nonzero integer into a `__ptrauth`-qualified
+/// integer anywhere in \p T. Zero keeps its representation and a symbol
+/// address becomes a signed relocation; any other integer needs signing at
+/// run time.
+static bool storesUnsignableInteger(const ASTContext &Ctx, const APValue &Value,
+ QualType T) {
+ if (Value.isInt())
+ return T.getPointerAuth() && Value.getInt() != 0;
+ if (Value.isArray()) {
+ QualType EltTy = Ctx.getAsArrayType(T)->getElementType();
+ for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I)
+ if (storesUnsignableInteger(Ctx, Value.getArrayInitializedElt(I), EltTy))
+ return true;
+ return Value.hasArrayFiller() &&
+ storesUnsignableInteger(Ctx, Value.getArrayFiller(), EltTy);
+ }
+ if (Value.isUnion()) {
+ const FieldDecl *FD = Value.getUnionField();
+ return FD &&
+ storesUnsignableInteger(Ctx, Value.getUnionValue(), FD->getType());
+ }
+ if (Value.isStruct()) {
+ const RecordDecl *RD = T.getAtomicUnqualifiedType()->castAsRecordDecl();
+ if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD)) {
+ unsigned I = 0;
+ for (const CXXBaseSpecifier &Base : CXXRD->bases())
+ if (storesUnsignableInteger(Ctx, Value.getStructBase(I++),
+ Base.getType()))
+ return true;
+ }
+ for (const FieldDecl *FD : RD->fields())
+ if (!FD->isUnnamedBitField() &&
+ storesUnsignableInteger(
+ Ctx, Value.getStructField(FD->getFieldIndex()), FD->getType()))
+ return true;
+ }
+ return false;
+}
+
+/// Whether an object of type \p T contains a `__ptrauth`-qualified integer.
+static bool hasPointerAuthInteger(const ASTContext &Ctx, QualType T) {
+ T = Ctx.getBaseElementType(T);
+ if (T.getPointerAuth()) {
+ if (const auto *AT = T->getAs<AtomicType>())
+ return AT->getValueType()->isIntegerType();
+ return T->isIntegerType();
+ }
+ const RecordDecl *RD = T.getAtomicUnqualifiedType()->getAsRecordDecl();
+ if (!RD || !RD->isCompleteDefinition())
+ return false;
+ if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD))
+ for (const CXXBaseSpecifier &Base : CXXRD->bases())
+ if (hasPointerAuthInteger(Ctx, Base.getType()))
+ return true;
+ for (const FieldDecl *FD : RD->fields())
+ if (hasPointerAuthInteger(Ctx, FD->getType()))
+ return true;
+ return false;
+}
+
+void Sema::checkPointerAuthIntegerConstantInit(
+ QualType T, llvm::function_ref<const APValue *()> Evaluate,
+ SourceLocation Loc, bool Constexpr) {
+ if (!getLangOpts().PointerAuthIntrinsics ||
+ !hasPointerAuthInteger(Context, T))
+ return;
+ if (const APValue *Value = Evaluate())
+ if (storesUnsignableInteger(Context, *Value, T))
+ Diag(Loc, diag::err_ptrauth_integer_constant_init) << Constexpr;
+}
+
bool Sema::CheckForConstantInitializer(Expr *Init, unsigned DiagID) {
// FIXME: Need strict checking. In C89, we need to check for
// any assignment, increment, decrement, function-calls, or
@@ -15400,6 +15471,21 @@ void Sema::CheckCompleteVariableDeclaration(VarDecl *var) {
}
}
+ // C++ signs a nonzero `__ptrauth`-qualified integer dynamically unless
+ // constant initialization is required; C has no dynamic initialization. A
+ // static compound literal was already checked as its own object.
+ auto *Literal = dyn_cast_or_null<CompoundLiteralExpr>(
+ Init ? Init->IgnoreParenImpCasts() : nullptr);
+ if (GlobalStorage && Init && !var->isInvalidDecl() &&
+ !type->isDependentType() && !Init->isValueDependent() &&
+ !(Literal && Literal->isFileScope() && Literal->isLValue())) {
+ bool Constexpr = var->isConstexpr() || var->hasAttr<ConstInitAttr>();
+ if (!getLangOpts().CPlusPlus || Constexpr)
+ checkPointerAuthIntegerConstantInit(
+ type, [&] { return var->evaluateValue(); }, var->getLocation(),
+ Constexpr);
+ }
+
// Apply section attributes and pragmas to global variables.
if (GlobalStorage && var->isThisDeclarationADefinition() &&
!inTemplateInstantiation()) {
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 5c3541cfab0187..b47708fb882c64 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -7765,6 +7765,16 @@ Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo,
if (!LiteralExpr)
return ExprError();
}
+ // Only an lvalue literal has static storage.
+ Expr::EvalResult Eval;
+ if (VK == VK_LValue && !LiteralExpr->isValueDependent())
+ checkPointerAuthIntegerConstantInit(
+ literalType,
+ [&]() -> const APValue * {
+ return LiteralExpr->EvaluateAsRValue(Eval, Context) ? &Eval.Val
+ : nullptr;
+ },
+ LParenLoc, /*Constexpr=*/false);
}
auto *E = new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, VK,
@@ -15072,6 +15082,11 @@ static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op,
SourceLocation OpLoc, bool IsInc,
bool IsPrefix) {
QualType ResType = Op->getType();
+ if (ResType.getPointerAuth() && ResType->isAtomicType()) {
+ S.Diag(OpLoc, diag::err_ptrauth_atomic_rmw)
+ << (IsInc ? 0 : 1) << ResType << Op->getSourceRange();
+ return QualType();
+ }
// Atomic types can be used for increment / decrement where the non-atomic
// versions can, so ignore the _Atomic() specifier for the purpose of
// checking.
@@ -15900,7 +15915,15 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
}
}
- checkTypeSupport(LHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr);
+ QualType LHSType = LHSExpr->getType();
+ if (BinaryOperator::isCompoundAssignmentOp(Opc) && LHSType.getPointerAuth() &&
+ LHSType->isAtomicType()) {
+ Diag(OpLoc, diag::err_ptrauth_atomic_rmw)
+ << 2 << LHSType << LHSExpr->getSourceRange();
+ return ExprError();
+ }
+
+ checkTypeSupport(LHSType, OpLoc, /*ValueDecl*/ nullptr);
checkTypeSupport(RHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr);
switch (Opc) {
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 6f4c71b118d590..056bce6fa04d92 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -14369,6 +14369,26 @@ StmtResult SemaOpenMP::ActOnOpenMPAtomicDirective(ArrayRef<OMPClause *> Clauses,
}
}
+ // OpenMP atomic lowering neither signs nor authenticates qualified storage.
+ auto RejectAuthenticatedStorage = [&](Expr *Storage) {
+ if (!Storage || Storage->isTypeDependent())
+ return false;
+ QualType T = Storage->getType();
+ if (const auto *AT = T->getAs<AtomicType>())
+ T = Context.getQualifiedType(AT->getValueType(), T.getQualifiers());
+ if (!T.getPointerAuth())
+ return false;
+ Diag(Storage->getExprLoc(), diag::err_omp_atomic_ptrauth)
+ << T << Storage->getSourceRange();
+ return true;
+ };
+ // Compare capture writes its value and success results through raw
+ // addresses too.
+ if (RejectAuthenticatedStorage(X) ||
+ (AtomicKind == OMPC_compare &&
+ (RejectAuthenticatedStorage(V) || RejectAuthenticatedStorage(R))))
+ return StmtError();
+
SemaRef.setFunctionHasBranchProtectedScope();
return OMPAtomicDirective::Create(
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e16630ce9cd844..e4fba82c705054 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -10518,6 +10518,11 @@ QualType Sema::BuildAtomicType(QualType T, SourceLocation Loc) {
else if (T->isOverflowBehaviorType())
// Overflow behavior types do not compose with _Atomic
DisallowedKind = 10;
+ else if (T->isRecordType() &&
+ Context.containsAddressDiscriminatedPointerAuth(T))
+ // Atomic copies of the aggregate would move signatures between
+ // addresses without resigning them.
+ DisallowedKind = 11;
if (DisallowedKind != -1) {
Diag(Loc, diag::err_atomic_specifier_bad_type) << DisallowedKind << T;
diff --git a/clang/test/CodeGen/ptrauth-atomic-builtins.c b/clang/test/CodeGen/ptrauth-atomic-builtins.c
new file mode 100644
index 00000000000000..be54ebf16061e2
--- /dev/null
+++ b/clang/test/CodeGen/ptrauth-atomic-builtins.c
@@ -0,0 +1,125 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-calls -fptrauth-intrinsics -emit-llvm %s -o - | FileCheck %s
+
+typedef int *__ptrauth(2, 1, 100) AuthPtr;
+typedef long __ptrauth(2, 0, 200) AuthInt;
+typedef long __ptrauth(2, 1, 201) AddrAuthInt;
+typedef void Fn(void);
+typedef Fn *__ptrauth(2, 1, 300) AuthFn;
+AuthPtr pointer;
+AuthInt integer;
+_Atomic AuthInt atomic_integer;
+_Atomic AddrAuthInt diverse_integer;
+_Atomic AuthFn function;
+_Atomic AuthPtr destination;
+
+// CHECK-LABEL: define {{.*}}void @store_n(
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @pointer to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.sign(
+// CHECK: store atomic ptr {{.*}}, ptr @pointer release
+void store_n(int *p) { __atomic_store_n(&pointer, p, __ATOMIC_RELEASE); }
+
+// CHECK-LABEL: define {{.*}}ptr @exchange_n(
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @pointer to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.sign(
+// CHECK: atomicrmw xchg ptr @pointer, ptr {{.*}} acq_rel
+// CHECK: call i64 @llvm.ptrauth.auth(
+int *exchange_n(int *p) { return __atomic_exchange_n(&pointer, p, __ATOMIC_ACQ_REL); }
+
+// Only desired is signed. The expected value is compared against the
+// authenticated snapshot, which is also the cmpxchg comparand: signing a
+// caller-chosen value would be a signing oracle.
+// CHECK-LABEL: define {{.*}}i1 @compare_n(
+// CHECK: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @pointer to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK-NOT: call i64 @llvm.ptrauth.sign(
+// CHECK: [[EXPECTED:%.*]] = load ptr, ptr %{{.*}}, align 8
+// CHECK-NEXT: [[DESIRED:%.*]] = load ptr, ptr %.atomictmp, align 8
+// CHECK-NEXT: [[SNAPSHOT:%.*]] = load atomic ptr, ptr @pointer acquire, align 8
+// CHECK: cmpxchg.store_expected:
+// CHECK-NEXT: [[OLD:%.*]] = phi ptr [ [[PLAIN:%.*]], %{{.*}} ]
+// CHECK-NEXT: store ptr [[OLD]], ptr %{{.*}}
+// CHECK: cmpxchg.auth:
+// CHECK-NEXT: [[SIGNED:%.*]] = phi ptr [ [[SNAPSHOT]], %{{.*}} ], [ [[OBSERVED:%.*]], %cmpxchg.fail ]
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 {{.*}})
+// CHECK: [[PLAIN]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: icmp eq ptr [[PLAIN]], [[EXPECTED]]
+// CHECK: [[PAIR:%.*]] = cmpxchg ptr @pointer, ptr [[SIGNED]], ptr [[DESIRED]] acq_rel acquire
+// CHECK-NEXT: [[OBSERVED]] = extractvalue { ptr, i1 } [[PAIR]], 0
+_Bool compare_n(int **expected, int *desired) {
+ return __atomic_compare_exchange_n(&pointer, expected, desired, 0,
+ __ATOMIC_ACQ_REL, __ATOMIC_ACQUIRE);
+}
+
+// CHECK-LABEL: define {{.*}}i64 @integer_exchange(
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 200)
+// CHECK: atomicrmw xchg ptr @atomic_integer, i64 {{.*}} seq_cst
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 200)
+long integer_exchange(long value) {
+ return __c11_atomic_exchange(&atomic_integer, value, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}void @integer_store(
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 200)
+// CHECK: store atomic i64 {{.*}}, ptr @integer release
+void integer_store(long value) {
+ __atomic_store_n(&integer, value, __ATOMIC_RELEASE);
+}
+
+// CHECK-LABEL: define {{.*}}i64 @diverse_integer_exchange(
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @diverse_integer to i64), i64 201)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2,
+// CHECK: atomicrmw xchg ptr @diverse_integer, i64 {{.*}} seq_cst
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2,
+long diverse_integer_exchange(long value) {
+ return __c11_atomic_exchange(&diverse_integer, value, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}i1 @integer_compare(
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 200)
+// CHECK-NOT: call i64 @llvm.ptrauth.sign(
+// CHECK: [[SNAPSHOT:%.*]] = load atomic i64, ptr @integer seq_cst, align 8
+// CHECK: [[SIGNED:%.*]] = phi i64 [ [[SNAPSHOT]], %{{.*}} ], [ {{.*}}, %cmpxchg.fail ]
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 200)
+// CHECK: cmpxchg ptr @integer, i64 [[SIGNED]], i64 {{.*}} seq_cst seq_cst
+_Bool integer_compare(long *expected, long desired) {
+ return __atomic_compare_exchange_n(&integer, expected, desired, 0,
+ __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}ptr @function_exchange(
+// CHECK: call i64 @llvm.ptrauth.resign({{.*}}i32 0, i64 0, i32 2,
+// CHECK: atomicrmw xchg ptr @function, ptr {{.*}} seq_cst
+// CHECK: call i64 @llvm.ptrauth.resign({{.*}}i32 2, {{.*}}i32 0, i64 0)
+Fn *function_exchange(Fn *p) {
+ return __c11_atomic_exchange(&function, p, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}i1 @function_compare(
+// CHECK: call i64 @llvm.ptrauth.resign({{.*}}i32 0, i64 0, i32 2,
+// CHECK-NOT: call i64 @llvm.ptrauth.resign(
+// CHECK: [[SNAPSHOT:%.*]] = load atomic ptr, ptr @function seq_cst, align 8
+// CHECK: [[SIGNED:%.*]] = phi ptr [ [[SNAPSHOT]], %{{.*}} ], [ {{.*}}, %cmpxchg.fail ]
+// CHECK: call i64 @llvm.ptrauth.resign({{.*}}i32 2, {{.*}}i32 0, i64 0)
+// CHECK: cmpxchg ptr @function, ptr [[SIGNED]], ptr {{.*}} seq_cst seq_cst
+_Bool function_compare(Fn **expected, Fn *desired) {
+ return __c11_atomic_compare_exchange_strong(&function, expected, desired,
+ __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}void @init_qualified(
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @pointer to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @destination to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.resign(
+// CHECK-NOT: call i64 @llvm.ptrauth.auth(
+// CHECK-NOT: call i64 @llvm.ptrauth.sign(
+// CHECK: store ptr {{.*}}, ptr @destination
+// CHECK: ret void
+void init_qualified(void) { __c11_atomic_init(&destination, pointer); }
+
+// CHECK-LABEL: define {{.*}}void @integer_init(
+// CHECK: [[SIGNED:%.*]] = call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 200)
+// CHECK: [[RESULT:%.*]] = phi i64 [ 0, %{{.*}} ], [ [[SIGNED]], %{{.*}} ]
+// CHECK-NEXT: store i64 [[RESULT]], ptr @atomic_integer, align 8
+void integer_init(long value) {
+ __c11_atomic_init(&atomic_integer, value);
+}
diff --git a/clang/test/CodeGen/ptrauth-atomic-misaligned.c b/clang/test/CodeGen/ptrauth-atomic-misaligned.c
new file mode 100644
index 00000000000000..475b9f04c75a21
--- /dev/null
+++ b/clang/test/CodeGen/ptrauth-atomic-misaligned.c
@@ -0,0 +1,22 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -Wno-address-of-packed-member -DLOAD -emit-llvm -verify %s -o /dev/null
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -Wno-address-of-packed-member -DCOMPARE -emit-llvm -verify %s -o /dev/null
+
+typedef int *__ptrauth(2, 1, 100) AuthPtr;
+struct __attribute__((packed)) S {
+ char c;
+ _Atomic AuthPtr pointer;
+};
+struct S s;
+
+// A misaligned atomic becomes a library call, which passes the signed value
+// through memory.
+#ifdef LOAD
+int *load(void) {
+ return __c11_atomic_load(&s.pointer, __ATOMIC_SEQ_CST); // expected-error {{misaligned atomic operation on a '__ptrauth'-qualified type is not supported; the expected alignment (8 bytes) exceeds the actual alignment (1 bytes)}}
+}
+#endif
+#ifdef COMPARE
+_Bool compare(int **expected, int *desired) {
+ return __c11_atomic_compare_exchange_strong(&s.pointer, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); // expected-error {{misaligned atomic operation on a '__ptrauth'-qualified type is not supported; the expected alignment (8 bytes) exceeds the actual alignment (1 bytes)}}
+}
+#endif
diff --git a/clang/test/CodeGen/ptrauth-qualifier-atomic.c b/clang/test/CodeGen/ptrauth-qualifier-atomic.c
new file mode 100644
index 00000000000000..54c5fcb1eaa1fd
--- /dev/null
+++ b/clang/test/CodeGen/ptrauth-qualifier-atomic.c
@@ -0,0 +1,203 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-calls -fptrauth-intrinsics -emit-llvm %s -o - | FileCheck %s
+
+typedef int *__ptrauth(2, 1, 100) AuthPtr;
+typedef long __ptrauth(2, 0, 200) AuthInt;
+typedef void func_t(void);
+typedef func_t *__ptrauth(2, 1, 100) AuthFunc;
+
+_Atomic AuthPtr atomic_ptr;
+_Atomic AuthPtr atomic_src;
+AuthPtr auth_ptr;
+_Atomic AuthInt atomic_int;
+_Atomic AuthFunc atomic_func;
+
+// CHECK-LABEL: define {{.*}}ptr @load_implicit()
+// CHECK: [[LOAD:%.*]] = load atomic ptr, ptr @atomic_ptr seq_cst, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK-NEXT: [[CMP:%.*]] = icmp ne ptr [[LOAD]], null
+// CHECK-NEXT: br i1 [[CMP]], label %[[NONNULL:.*]], label %[[CONT:.*]]
+// CHECK: [[NONNULL]]:
+// CHECK-NEXT: [[INT:%.*]] = ptrtoint ptr [[LOAD]] to i64
+// CHECK-NEXT: [[AUTH:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 [[DISC]])
+// CHECK-NEXT: [[AUTHED:%.*]] = inttoptr i64 [[AUTH]] to ptr
+// CHECK: [[CONT]]:
+// CHECK-NEXT: [[RESULT:%.*]] = phi ptr [ null, %entry ], [ [[AUTHED]], %[[NONNULL]] ]
+// CHECK-NEXT: ret ptr [[RESULT]]
+int *load_implicit(void) { return atomic_ptr; }
+
+// CHECK-LABEL: define {{.*}}void @store_implicit(ptr noundef %p)
+// CHECK: [[VALUE:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[INT:%.*]] = ptrtoint ptr [[VALUE]] to i64
+// CHECK-NEXT: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 [[INT]], i32 2, i64 [[DISC]])
+// CHECK-NEXT: [[SIGNED:%.*]] = inttoptr i64 [[SIGN]] to ptr
+// CHECK: [[RESULT:%.*]] = phi ptr [ null, %{{.*}} ], [ [[SIGNED]], %{{.*}} ]
+// CHECK-NEXT: store atomic ptr [[RESULT]], ptr @atomic_ptr seq_cst, align 8
+void store_implicit(int *p) { atomic_ptr = p; }
+
+// CHECK-LABEL: define {{.*}}void @store_c11(ptr noundef %p)
+// CHECK: [[VALUE:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK: [[SIGNED:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[SIGNED]], ptr %.atomictmp, align 8
+// CHECK-NEXT: [[ATOMIC:%.*]] = load ptr, ptr %.atomictmp, align 8
+// CHECK-NOT: call i64 @llvm.ptrauth
+// CHECK-NEXT: store atomic ptr [[ATOMIC]], ptr @atomic_ptr seq_cst, align 8
+void store_c11(int *p) {
+ __c11_atomic_store(&atomic_ptr, p, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}void @store_c11_resign()
+// CHECK: [[VALUE:%.*]] = load atomic ptr, ptr @atomic_src seq_cst, align 8
+// CHECK-NEXT: [[SRCDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_src to i64), i64 100)
+// CHECK-NEXT: [[DSTDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[RESIGN:%.*]] = call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 2, i64 [[SRCDISC]], i32 2, i64 [[DSTDISC]])
+// CHECK: [[RESIGNED:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[RESIGNED]], ptr %.atomictmp, align 8
+// CHECK-NEXT: [[ATOMIC:%.*]] = load ptr, ptr %.atomictmp, align 8
+// CHECK-NOT: call i64 @llvm.ptrauth
+// CHECK-NEXT: store atomic ptr [[ATOMIC]], ptr @atomic_ptr seq_cst, align 8
+void store_c11_resign(void) {
+ __c11_atomic_store(&atomic_ptr, atomic_src, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}void @init_local(ptr noundef %p)
+// CHECK: [[VALUE:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[ADDR:%.*]] = ptrtoint ptr %l to i64
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 [[ADDR]], i64 100)
+// CHECK: [[INT:%.*]] = ptrtoint ptr [[VALUE]] to i64
+// CHECK-NEXT: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 [[INT]], i32 2, i64 [[DISC]])
+// CHECK-NEXT: [[SIGNED:%.*]] = inttoptr i64 [[SIGN]] to ptr
+// CHECK: [[RESULT:%.*]] = phi ptr [ null, %{{.*}} ], [ [[SIGNED]], %{{.*}} ]
+// CHECK-NEXT: store ptr [[RESULT]], ptr %l, align 8
+void init_local(int *p) { AuthPtr _Atomic l = p; }
+
+// CHECK-LABEL: define {{.*}}void @copy_implicit()
+// CHECK: [[LOAD:%.*]] = load atomic ptr, ptr @atomic_src seq_cst, align 8
+// CHECK: [[SRCDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_src to i64), i64 100)
+// CHECK: [[DSTDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 2, i64 [[SRCDISC]], i32 2, i64 [[DSTDISC]])
+// CHECK-NOT: call i64 @llvm.ptrauth.auth
+// CHECK-NOT: call i64 @llvm.ptrauth.sign
+// CHECK: store atomic ptr {{.*}}, ptr @atomic_ptr seq_cst, align 8
+// CHECK: ret void
+void copy_implicit(void) { atomic_ptr = atomic_src; }
+
+// CHECK-LABEL: define {{.*}}i64 @load_int()
+// CHECK: [[LOAD:%.*]] = load atomic i64, ptr @atomic_int seq_cst, align 8
+// CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[LOAD]], 0
+// CHECK: [[AUTH:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[LOAD]], i32 2, i64 200)
+// CHECK: [[RESULT:%.*]] = phi i64 [ 0, %entry ], [ [[AUTH]], %{{.*}} ]
+// CHECK-NEXT: ret i64 [[RESULT]]
+long load_int(void) { return atomic_int; }
+
+// CHECK-LABEL: define {{.*}}void @store_int(i64 noundef %v)
+// CHECK: [[VALUE:%.*]] = load i64, ptr %v.addr, align 8
+// CHECK-NEXT: [[CMP:%.*]] = icmp ne i64 [[VALUE]], 0
+// CHECK: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 [[VALUE]], i32 2, i64 200)
+// CHECK: [[RESULT:%.*]] = phi i64 [ 0, %entry ], [ [[SIGN]], %{{.*}} ]
+// CHECK-NEXT: store atomic i64 [[RESULT]], ptr @atomic_int seq_cst, align 8
+void store_int(long v) { atomic_int = v; }
+
+// CHECK-LABEL: define {{.*}}ptr @exchange_c11(ptr noundef %p)
+// CHECK: [[VALUE:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK: [[SIGNED:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[SIGNED]], ptr %.atomictmp, align 8
+// CHECK-NEXT: [[VAL:%.*]] = load ptr, ptr %.atomictmp, align 8
+// CHECK-NEXT: [[OLD:%.*]] = atomicrmw xchg ptr @atomic_ptr, ptr [[VAL]] seq_cst, align 8
+// CHECK-NEXT: [[OLDDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[OLDAUTH:%.*]] = call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 [[OLDDISC]])
+// CHECK: [[RESULT:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[RESULT]], ptr %atomic-temp, align 8
+int *exchange_c11(int *p) {
+ return __c11_atomic_exchange(&atomic_ptr, p, __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}i1 @cmpxchg_c11(ptr noundef %p)
+// CHECK: store ptr {{.*}}, ptr %expected, align 8
+// CHECK-NEXT: [[DESIRED:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[DESDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[DESSIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DESDISC]])
+// CHECK: [[DESSIGNED:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[DESSIGNED]], ptr %.atomictmp, align 8
+// CHECK-NEXT: [[EXPECTED:%.*]] = load ptr, ptr %expected, align 8
+// CHECK-NEXT: [[DES:%.*]] = load ptr, ptr %.atomictmp, align 8
+// CHECK-NEXT: [[SNAPSHOT:%.*]] = load atomic ptr, ptr @atomic_ptr seq_cst, align 8
+// CHECK-NEXT: br label %cmpxchg.auth
+// CHECK: cmpxchg.store_expected:
+// CHECK-NEXT: [[OLD:%.*]] = phi ptr [ [[PLAIN:%.*]], %[[COMPARE:.*]] ]
+// CHECK-NEXT: store ptr [[OLD]], ptr %expected, align 8
+// CHECK: cmpxchg.continue:
+// CHECK-NEXT: phi i1 [ true, %cmpxchg.try ], [ false, %cmpxchg.store_expected ]
+// CHECK: cmpxchg.auth:
+// CHECK-NEXT: [[SIGNED:%.*]] = phi ptr [ [[SNAPSHOT]], %{{.*}} ], [ [[OBSERVED:%.*]], %cmpxchg.fail ]
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK: [[COMPARE]]:
+// CHECK-NEXT: [[PLAIN]] = phi ptr [ null, %cmpxchg.auth ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: [[EQUAL:%.*]] = icmp eq ptr [[PLAIN]], [[EXPECTED]]
+// CHECK-NEXT: br i1 [[EQUAL]], label %cmpxchg.try, label %cmpxchg.store_expected
+// CHECK: cmpxchg.try:
+// CHECK-NEXT: [[PAIR:%.*]] = cmpxchg ptr @atomic_ptr, ptr [[SIGNED]], ptr [[DES]] seq_cst seq_cst, align 8
+// CHECK-NEXT: [[OBSERVED]] = extractvalue { ptr, i1 } [[PAIR]], 0
+// CHECK-NEXT: [[SUCCESS:%.*]] = extractvalue { ptr, i1 } [[PAIR]], 1
+// CHECK-NEXT: br i1 [[SUCCESS]], label %cmpxchg.continue, label %cmpxchg.fail
+// CHECK: cmpxchg.fail:
+// CHECK-NEXT: br label %cmpxchg.auth
+// The expected value is never signed. The snapshot of the object is the
+// comparand, so no signature of a caller-chosen value exists to leak.
+_Bool cmpxchg_c11(int *p) {
+ int *expected = p;
+ return __c11_atomic_compare_exchange_strong(&atomic_ptr, &expected, p,
+ __ATOMIC_SEQ_CST,
+ __ATOMIC_SEQ_CST);
+}
+
+// CHECK-LABEL: define {{.*}}i1 @cmpxchg_weak_c11(ptr noundef %p)
+// CHECK: [[SNAPSHOT:%.*]] = load atomic ptr, ptr @atomic_ptr monotonic, align 8
+// CHECK: cmpxchg.store_expected:
+// CHECK-NEXT: [[OLD:%.*]] = phi ptr [ [[PLAIN:%.*]], %{{.*}} ], [ [[WEAKOLD:%.*]], %{{.*}} ]
+// CHECK-NEXT: store ptr [[OLD]], ptr %expected, align 8
+// CHECK: cmpxchg.auth:
+// CHECK-NEXT: [[SIGNED:%.*]] = phi ptr [ [[SNAPSHOT]], %{{.*}} ]
+// CHECK: [[PLAIN]] = phi ptr [ null, %cmpxchg.auth ], [ {{.*}}, %{{.*}} ]
+// CHECK: cmpxchg.try:
+// CHECK-NEXT: [[PAIR:%.*]] = cmpxchg weak ptr @atomic_ptr, ptr [[SIGNED]], ptr {{.*}} release monotonic, align 8
+// CHECK-NEXT: [[OBSERVED:%.*]] = extractvalue { ptr, i1 } [[PAIR]], 0
+// CHECK: cmpxchg.fail:
+// CHECK-NEXT: [[FAILDISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 [[FAILDISC]])
+// CHECK: [[WEAKOLD]] = phi ptr [ null, %cmpxchg.fail ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: br label %cmpxchg.store_expected
+_Bool cmpxchg_weak_c11(int *p) {
+ int *expected = p;
+ return __c11_atomic_compare_exchange_weak(&atomic_ptr, &expected, p,
+ __ATOMIC_RELEASE,
+ __ATOMIC_RELAXED);
+}
+
+// CHECK-LABEL: define {{.*}}ptr @load_n()
+// CHECK: [[LOAD:%.*]] = load atomic ptr, ptr @auth_ptr seq_cst, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @auth_ptr to i64), i64 100)
+// CHECK: [[INT:%.*]] = ptrtoint ptr [[LOAD]] to i64
+// CHECK-NEXT: [[AUTH:%.*]] = call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 [[DISC]])
+int *load_n(void) { return __atomic_load_n(&auth_ptr, __ATOMIC_SEQ_CST); }
+
+// CHECK-LABEL: define {{.*}}void @init_c11(ptr noundef %p)
+// CHECK: [[VALUE:%.*]] = load ptr, ptr %p.addr, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_ptr to i64), i64 100)
+// CHECK: [[VALUEINT:%.*]] = ptrtoint ptr [[VALUE]] to i64
+// CHECK-NEXT: [[SIGN:%.*]] = call i64 @llvm.ptrauth.sign(i64 [[VALUEINT]], i32 2, i64 [[DISC]])
+// CHECK: [[SIGNED:%.*]] = phi ptr [ null, %{{.*}} ], [ {{.*}}, %{{.*}} ]
+// CHECK-NEXT: store ptr [[SIGNED]], ptr @atomic_ptr, align 8
+void init_c11(int *p) { __c11_atomic_init(&atomic_ptr, p); }
+
+// CHECK-LABEL: define {{.*}}ptr @load_func()
+// CHECK: [[LOAD:%.*]] = load atomic ptr, ptr @atomic_func seq_cst, align 8
+// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_func to i64), i64 100)
+// CHECK: [[INT:%.*]] = ptrtoint ptr [[LOAD]] to i64
+// CHECK-NEXT: {{.*}} = call i64 @llvm.ptrauth.resign(i64 [[INT]], i32 2, i64 [[DISC]], i32 0, i64 0)
+func_t *load_func(void) { return atomic_func; }
diff --git a/clang/test/CodeGenCXX/ptrauth-integer-dynamic-init.cpp b/clang/test/CodeGenCXX/ptrauth-integer-dynamic-init.cpp
new file mode 100644
index 00000000000000..b7753ebf3288e2
--- /dev/null
+++ b/clang/test/CodeGenCXX/ptrauth-integer-dynamic-init.cpp
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -std=c++20 -emit-llvm %s -o - | FileCheck %s
+
+typedef long __ptrauth(2, 0, 200) Auth;
+typedef long __ptrauth(2, 1, 201) AddrAuth;
+Auth plain = 0x1234;
+_Atomic Auth atomic = 0x1234;
+AddrAuth diverse = 0x1234;
+struct S { Auth value; } aggregate = {0x1234};
+Auth zero = 0;
+_Atomic Auth atomic_zero = 0;
+extern int object;
+Auth symbolic = (long)&object;
+
+// CHECK-DAG: @plain = global i64 0
+// CHECK-DAG: @atomic = global i64 0
+// CHECK-DAG: @diverse = global i64 0
+// CHECK-DAG: @zero = global i64 0
+// CHECK-DAG: @atomic_zero = global i64 0
+// CHECK-DAG: @symbolic = global i64 ptrtoint (ptr ptrauth (ptr @object, i32 2, i64 200) to i64)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 4660, i32 2, i64 200)
+// CHECK: store i64 {{.*}}, ptr @plain
+// CHECK: call i64 @llvm.ptrauth.sign(i64 4660, i32 2, i64 200)
+// CHECK: store i64 {{.*}}, ptr @atomic
+// CHECK: call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @diverse to i64), i64 201)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 4660, i32 2,
+// CHECK: store i64 {{.*}}, ptr @diverse
+
+long local() {
+ static Auth value = 0x1234;
+ return value;
+}
+
+// Failed speculative constant-emission probes must not reject these locals.
+// Taking both addresses forces storage for the scalar and aggregate cases.
+void automatic_constexpr() {
+ constexpr int *__ptrauth(2, 1, 100) p = &object;
+ struct Holder { int *__ptrauth(2, 1, 100) pointer; };
+ constexpr Holder h = {&object};
+ asm("" : : "r"(&p), "r"(&h));
+}
diff --git a/clang/test/OpenMP/atomic_ptrauth.c b/clang/test/OpenMP/atomic_ptrauth.c
new file mode 100644
index 00000000000000..2a3294e7cfb8d3
--- /dev/null
+++ b/clang/test/OpenMP/atomic_ptrauth.c
@@ -0,0 +1,53 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -fopenmp -verify %s
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -fopenmp-simd -verify %s
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -fopenmp -x c++ -std=c++20 -verify %s
+
+typedef long __ptrauth(2, 0, 200) Auth;
+Auth value;
+_Atomic Auth atomic_value;
+int *__ptrauth(2, 1, 100) pointer;
+
+void f(long raw, int *p) {
+#pragma omp atomic update
+ value = value + 1; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#ifndef __cplusplus
+#pragma omp atomic update
+ atomic_value = atomic_value + 1; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#endif
+#pragma omp atomic capture
+ { raw = value; value = value + 1; } // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic capture
+ raw = value++; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic read
+ raw = value; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic write
+ value = raw; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic compare
+ if (value == 1) value = 2; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic read
+ p = pointer; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic write
+ pointer = p; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic update
+ raw = raw + 1;
+#pragma omp atomic capture
+ { value = raw; raw++; }
+}
+
+void compare_capture(long raw) {
+#pragma omp atomic update
+ value = 1 + value; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic compare capture
+ { value = raw; if (raw == 1) raw = 2; } // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+#pragma omp atomic compare capture
+ { value = raw == 1; if (value) raw = 2; } // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+}
+
+#ifdef __cplusplus
+template<class T> void update(T &x) {
+#pragma omp atomic update
+ x = x + 1; // expected-error {{OpenMP atomic operation on '__ptrauth'-qualified type}}
+}
+void instantiate_plain(long &plain) { update(plain); }
+void instantiate() { update(value); } // expected-note {{in instantiation of function template specialization}}
+#endif
diff --git a/clang/test/OpenMP/atomic_ptrauth_result_codegen.c b/clang/test/OpenMP/atomic_ptrauth_result_codegen.c
new file mode 100644
index 00000000000000..0b778308684a5e
--- /dev/null
+++ b/clang/test/OpenMP/atomic_ptrauth_result_codegen.c
@@ -0,0 +1,24 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -fopenmp -emit-llvm %s -o - | FileCheck %s
+
+long raw;
+long __ptrauth(2, 1, 200) result;
+
+// CHECK-LABEL: define {{.*}}void @read_result(
+// CHECK: load atomic i64, ptr @raw monotonic
+// CHECK: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @result to i64), i64 200)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK: store i64 {{.*}}, ptr @result
+void read_result(void) {
+#pragma omp atomic read
+ result = raw;
+}
+
+// CHECK-LABEL: define {{.*}}void @capture_result(
+// CHECK: atomicrmw add ptr @raw, i64 1 monotonic
+// CHECK: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @result to i64), i64 200)
+// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]])
+// CHECK: store i64 {{.*}}, ptr @result
+void capture_result(void) {
+#pragma omp atomic capture
+ { result = raw; raw++; }
+}
diff --git a/clang/test/Sema/ptrauth-atomic-ops.c b/clang/test/Sema/ptrauth-atomic-ops.c
index a5411f2e197b3f..9b4398d8e50960 100644
--- a/clang/test/Sema/ptrauth-atomic-ops.c
+++ b/clang/test/Sema/ptrauth-atomic-ops.c
@@ -1,5 +1,5 @@
-// RUN: %clang_cc1 -triple arm64-apple-ios -fsyntax-only -verify -fptrauth-intrinsics %s
-// RUN: %clang_cc1 -triple aarch64-linux-gnu -fsyntax-only -verify -fptrauth-intrinsics %s
+// RUN: %clang_cc1 -triple arm64-apple-ios -fsyntax-only -verify -fptrauth-intrinsics -Wno-hip-deprecated-builtins %s
+// RUN: %clang_cc1 -triple aarch64-linux-gnu -fsyntax-only -verify -fptrauth-intrinsics -Wno-hip-deprecated-builtins %s
#include <stdatomic.h>
@@ -8,43 +8,118 @@ int *__ptrauth(2, 1, 100) authenticated_ptr = &i;
int *__ptrauth(2, 0, 200) non_addr_discriminatedauthenticated_ptr = &i;
int * wat = &i;
#define ATOMIZE(p) (__typeof__(p) volatile _Atomic *)(long)(&p)
+#define NONVOLATILE_ATOMIZE(p) (__typeof__(p) _Atomic *)(long)(&p)
void f() {
static int j = 1;
- __c11_atomic_init(ATOMIZE(authenticated_ptr), 5);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __c11_atomic_store(ATOMIZE(authenticated_ptr), 0, memory_order_relaxed);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __c11_atomic_load(ATOMIZE(authenticated_ptr), memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __c11_atomic_store(ATOMIZE(authenticated_ptr), 1, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __atomic_store_n(ATOMIZE(authenticated_ptr), 4, memory_order_release);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __atomic_store(ATOMIZE(authenticated_ptr), j, memory_order_release);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __c11_atomic_exchange(ATOMIZE(authenticated_ptr), 1, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
- __atomic_exchange(ATOMIZE(authenticated_ptr), &j, &j, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ int *raw = &i;
+ int *raw2 = &i;
+ int *__ptrauth(2, 1, 100) auth_result = &i;
+ _Bool res;
+
+ // Operations that do not compute on the value are supported, including with
+ // address discrimination.
+ __c11_atomic_init(ATOMIZE(authenticated_ptr), raw);
+ __c11_atomic_store(ATOMIZE(authenticated_ptr), raw, memory_order_relaxed);
+ raw = __c11_atomic_load(ATOMIZE(authenticated_ptr), memory_order_seq_cst);
+ raw = __c11_atomic_exchange(ATOMIZE(authenticated_ptr), raw2, memory_order_seq_cst);
+ res = __c11_atomic_compare_exchange_strong(NONVOLATILE_ATOMIZE(authenticated_ptr), &raw, raw2, memory_order_seq_cst, memory_order_seq_cst);
+ res = __c11_atomic_compare_exchange_weak(NONVOLATILE_ATOMIZE(authenticated_ptr), &raw, raw2, memory_order_seq_cst, memory_order_seq_cst);
+
+ // Compare-exchange reads the object before exchanging, so volatile objects
+ // are rejected.
+ res = __c11_atomic_compare_exchange_strong(ATOMIZE(authenticated_ptr), &raw, raw2, memory_order_seq_cst, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to volatile compare-exchange operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ res = __c11_atomic_compare_exchange_weak(ATOMIZE(authenticated_ptr), &raw, raw2, memory_order_seq_cst, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to volatile compare-exchange operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ res = __atomic_compare_exchange_n((int *volatile __ptrauth(2, 1, 100) *)&authenticated_ptr, &raw, raw2, 0, memory_order_seq_cst, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to volatile compare-exchange operation must not be a pointer to a '__ptrauth'-qualified type ('int *volatile __ptrauth(2,1,100) *' invalid)}}
+
+ // The byte forms would write raw bits into signed storage.
+ __atomic_test_and_set(&authenticated_ptr, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __atomic_clear(&authenticated_ptr, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __atomic_clear(ATOMIZE(non_addr_discriminatedauthenticated_ptr), memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,0,200) _Atomic(int *) *' invalid)}}
+
+ // Atomic copies of an aggregate would move address-discriminated signatures
+ // between addresses without resigning them.
+ struct Diverse { int *__ptrauth(2, 1, 100) pointer; };
+ _Atomic struct Diverse diverse;
+ // expected-error at -1 {{_Atomic cannot be applied to type 'struct Diverse' which contains an address-discriminated field}}
+ struct Nested { struct Diverse array[1]; };
+ _Atomic struct Nested nested;
+ // expected-error at -1 {{_Atomic cannot be applied to type 'struct Nested' which contains an address-discriminated field}}
+ struct Uniform { int *__ptrauth(2, 0, 200) pointer; };
+ _Atomic struct Uniform uniform;
+ (void)diverse;
+ (void)nested;
+ (void)uniform;
+
+ raw = __atomic_load_n(&authenticated_ptr, memory_order_seq_cst);
+ __atomic_store_n(&authenticated_ptr, raw, memory_order_seq_cst);
+ raw = __atomic_exchange_n(&authenticated_ptr, raw2, memory_order_seq_cst);
+ res = __atomic_compare_exchange_n(&authenticated_ptr, &raw, raw2, 0, memory_order_seq_cst, memory_order_seq_cst);
+ raw = __scoped_atomic_load_n(&authenticated_ptr, memory_order_seq_cst, __MEMORY_SCOPE_SYSTEM);
+
+ // The by-value forms yield the plain value type.
+ _Static_assert(
+ __builtin_types_compatible_p(
+ __typeof__(__atomic_load_n(&authenticated_ptr,
+ memory_order_seq_cst)) *,
+ int **),
+ "");
+ _Static_assert(
+ __builtin_types_compatible_p(
+ __typeof__(__hip_atomic_load(&authenticated_ptr,
+ memory_order_seq_cst, 1)) *,
+ int **),
+ "");
+
+ // The generic by-address forms are not supported.
+ __atomic_load(&authenticated_ptr, &raw, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to generic atomic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __atomic_store(&authenticated_ptr, &authenticated_ptr, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to generic atomic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __atomic_exchange(&authenticated_ptr, &raw2, &auth_result, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to generic atomic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ res = __atomic_compare_exchange(&authenticated_ptr, &raw, &authenticated_ptr, 0, memory_order_seq_cst, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to generic atomic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __scoped_atomic_load(&authenticated_ptr, &raw, memory_order_seq_cst, __MEMORY_SCOPE_SYSTEM);
+ // expected-error at -1 {{address argument to generic atomic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+
+ // Arithmetic atomic builtins compute on the signed value.
__c11_atomic_fetch_add(ATOMIZE(authenticated_ptr), 1, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_add(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_sub(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_min(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_max(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__c11_atomic_fetch_and(ATOMIZE(authenticated_ptr), 1, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_and(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_or(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
__atomic_fetch_xor(ATOMIZE(authenticated_ptr), 3, memory_order_seq_cst);
- // expected-error at -1 {{address argument to atomic operation must be a pointer to a non address discriminated type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}}
+ __atomic_fetch_add(&authenticated_ptr, 1, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+ __atomic_fetch_max(&authenticated_ptr, 1, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
+
+ // Implicit operations that compute on the value are not supported.
+ (*ATOMIZE(authenticated_ptr))++;
+ // expected-error at -1 {{atomic increment of '__ptrauth'-qualified type 'volatile __ptrauth(2,1,100) _Atomic(int *)' is not supported}}
+ --(*ATOMIZE(authenticated_ptr));
+ // expected-error at -1 {{atomic decrement of '__ptrauth'-qualified type 'volatile __ptrauth(2,1,100) _Atomic(int *)' is not supported}}
+ (*ATOMIZE(authenticated_ptr)) += 1;
+ // expected-error at -1 {{atomic compound assignment of '__ptrauth'-qualified type 'volatile __ptrauth(2,1,100) _Atomic(int *)' is not supported}}
__c11_atomic_init(ATOMIZE(non_addr_discriminatedauthenticated_ptr), &j);
__c11_atomic_store(ATOMIZE(non_addr_discriminatedauthenticated_ptr), 0, memory_order_relaxed);
@@ -54,65 +129,81 @@ void f() {
__c11_atomic_exchange(ATOMIZE(j), ATOMIZE(non_addr_discriminatedauthenticated_ptr), memory_order_seq_cst);
// expected-error at -1 {{incompatible pointer to integer conversion passing 'volatile __ptrauth(2,0,200) _Atomic(int *) *' to parameter of type 'typeof (j)' (aka 'int')}}
__c11_atomic_fetch_add(ATOMIZE(non_addr_discriminatedauthenticated_ptr), ATOMIZE(j), memory_order_seq_cst);
- // expected-error at -1 {{incompatible pointer to integer conversion passing 'volatile _Atomic(typeof (j)) *' to parameter of type '__ptrdiff_t'}}
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,0,200) _Atomic(int *) *' invalid)}}
__c11_atomic_fetch_and(ATOMIZE(j), ATOMIZE(non_addr_discriminatedauthenticated_ptr), memory_order_seq_cst);
// expected-error at -1 {{incompatible pointer to integer conversion passing 'volatile __ptrauth(2,0,200) _Atomic(int *) *' to parameter of type 'typeof (j)' (aka 'int')}}
+ __atomic_fetch_add(&non_addr_discriminatedauthenticated_ptr, 1, memory_order_seq_cst);
+ // expected-error at -1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
-
+ // __sync builtins are not supported.
__sync_fetch_and_add(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_fetch_and_sub(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_fetch_and_or(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_fetch_and_and(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_fetch_and_xor(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_fetch_and_nand(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_add_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_sub_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_or_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_and_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_xor_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_nand_and_fetch(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_bool_compare_and_swap(&authenticated_ptr, 1, 0);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_val_compare_and_swap(&authenticated_ptr, 1, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_lock_test_and_set(&authenticated_ptr, 1);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
__sync_lock_release(&authenticated_ptr);
- // expected-error at -1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}}
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,1,100) *' invalid)}}
int i = 0;
__sync_fetch_and_add(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_fetch_and_sub(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_fetch_and_or(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_fetch_and_and(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_fetch_and_xor(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_add_and_fetch(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_sub_and_fetch(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_or_and_fetch(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_and_and_fetch(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_xor_and_fetch(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_bool_compare_and_swap(&non_addr_discriminatedauthenticated_ptr, &i, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_val_compare_and_swap(&non_addr_discriminatedauthenticated_ptr, &i, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_lock_test_and_set(&non_addr_discriminatedauthenticated_ptr, &i);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
__sync_lock_release(&non_addr_discriminatedauthenticated_ptr);
+ // expected-error at -1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}}
}
diff --git a/clang/test/Sema/ptrauth-integer-constant-init.c b/clang/test/Sema/ptrauth-integer-constant-init.c
new file mode 100644
index 00000000000000..1063bb45ddcc08
--- /dev/null
+++ b/clang/test/Sema/ptrauth-integer-constant-init.c
@@ -0,0 +1,34 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -fsyntax-only -verify %s
+
+typedef long __ptrauth(2, 0, 200) Auth;
+typedef long __ptrauth(2, 1, 201) AddrAuth;
+struct S { Auth value; };
+union U { long plain; Auth value; };
+extern int object;
+
+// Static storage cannot sign at run time. Zero keeps its representation and a
+// symbol address becomes a signed relocation; any other constant is rejected.
+Auth plain = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+_Atomic Auth atomic = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+AddrAuth diverse = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+_Atomic AddrAuth atomic_diverse = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+Auth array[] = {0, 0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+struct S aggregate = {0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+union U member = {.value = 0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+Auth cast_absolute = (long)(void *)0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+struct S *literal = &(struct S){0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+struct S from_literal = (struct S){0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+
+long local(void) {
+ static Auth value = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+ Auth automatic = 0x1234;
+ struct S s = {0x1234};
+ return value + automatic + s.value + ((struct S){0x1234}).value;
+}
+
+Auth zero = 0;
+Auth cast_zero = (long)(void *)0;
+Auth symbol = (long)&object;
+struct S zero_aggregate = {0};
+union U plain_member = {.plain = 0x1234};
+struct S *zero_literal = &(struct S){0};
diff --git a/clang/test/SemaCXX/ptrauth-integer-constant-init.cpp b/clang/test/SemaCXX/ptrauth-integer-constant-init.cpp
new file mode 100644
index 00000000000000..e995bbe43992c3
--- /dev/null
+++ b/clang/test/SemaCXX/ptrauth-integer-constant-init.cpp
@@ -0,0 +1,28 @@
+// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-intrinsics -std=c++20 -fsyntax-only -verify %s
+
+typedef long __ptrauth(2, 0, 200) Auth;
+struct S { Auth value; };
+struct H { _Atomic(S) atomic; };
+extern int object;
+
+// Dynamic initialization signs at run time; constant initialization cannot.
+Auth dynamic = 0x1234;
+S aggregate = {0x1234};
+S from_literal = (S){0x1234};
+_Atomic(S) atomic_aggregate = S{0x1234};
+constinit Auth ci = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+constexpr Auth ce = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+constinit S constant_aggregate = {0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+constinit S constant_literal = (S){0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+constinit _Atomic(S) constant_atomic = S{0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+constinit H holder = {S{0x1234}}; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+Auth *array_literal = (Auth[]){0x1234}; // expected-error {{'__ptrauth'-qualified integer cannot be statically initialized to a nonzero integer constant}}
+constexpr Auth zero = 0;
+Auth symbol = (long)&object;
+
+long local() {
+ static constinit Auth value = 0x1234; // expected-error {{'__ptrauth'-qualified integer cannot be constant initialized to a nonzero integer constant}}
+ static Auth guarded = 0x1234;
+ constexpr Auth automatic = 0x1234;
+ return value + guarded + automatic;
+}
More information about the cfe-commits
mailing list