https://github.com/jfbastien updated https://github.com/llvm/llvm-project/pull/240090
>From 72e68330f1f812608a57e33e3dd9b1ef3aa0e26f Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Mon, 9 Nov 2026 15:03:07 +0900 Subject: [PATCH 1/9] [clang][PAC] Fix crashes on _Atomic __ptrauth-qualified objects Implicit _Atomic operations on a __ptrauth-qualified object assert in CodeGen, because the pointer-authentication helpers do not look through AtomicType: https://godbolt.org/z/6KE7Ez5P4 (a trunk build, so the link stops crashing once this lands). Fix the crashes. In a build without assertions the same code miscompiled an _Atomic function pointer under -fptrauth-calls, signing it twice or leaving it unsigned. Nothing else changes: the tests simply document current behavior, with a FIXME on what I propose fixing in follow-ups, for example https://godbolt.org/z/hGosq14ea. Assisted-by: AI --- clang/lib/CodeGen/CGExprScalar.cpp | 2 +- clang/lib/CodeGen/CGPointerAuth.cpp | 3 +- clang/test/CodeGen/ptrauth-qualifier-atomic.c | 277 ++++++++++++++++++ 3 files changed, 280 insertions(+), 2 deletions(-) create mode 100644 clang/test/CodeGen/ptrauth-qualifier-atomic.c diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 0e2e29f0745d27..57f8367d367892 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2513,7 +2513,7 @@ static bool isLValueKnownNonNull(CodeGenFunction &CGF, const Expr *E) { } bool CodeGenFunction::isPointerKnownNonNull(const Expr *E) { - assert(E->getType()->isSignableType(getContext())); + assert(E->getType().getAtomicUnqualifiedType()->isSignableType(getContext())); E = E->IgnoreParens(); diff --git a/clang/lib/CodeGen/CGPointerAuth.cpp b/clang/lib/CodeGen/CGPointerAuth.cpp index 438d08b53a742c..62697b317f9126 100644 --- a/clang/lib/CodeGen/CGPointerAuth.cpp +++ b/clang/lib/CodeGen/CGPointerAuth.cpp @@ -191,6 +191,7 @@ CGPointerAuthInfo CodeGenModule::getPointerAuthInfoForPointeeType(QualType T) { /// pointer type. static CGPointerAuthInfo getPointerAuthInfoForType(CodeGenModule &CGM, QualType PointerType) { + PointerType = PointerType.getAtomicUnqualifiedType(); assert(PointerType->isSignableType(CGM.getContext())); // Block pointers are currently not signed. @@ -225,7 +226,7 @@ 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())); + assert(E->getType().getAtomicUnqualifiedType()->isSignableType(getContext())); E = E->IgnoreParens(); if (const auto *Load = dyn_cast<ImplicitCastExpr>(E)) { diff --git a/clang/test/CodeGen/ptrauth-qualifier-atomic.c b/clang/test/CodeGen/ptrauth-qualifier-atomic.c new file mode 100644 index 00000000000000..dc939dfd710684 --- /dev/null +++ b/clang/test/CodeGen/ptrauth-qualifier-atomic.c @@ -0,0 +1,277 @@ +// RUN: %clang_cc1 -triple arm64e-apple-ios -fptrauth-calls -fptrauth-intrinsics -emit-llvm %s -o - | FileCheck %s + +typedef int *__ptrauth(2, 0, 50) UniformPtr; +typedef int *__ptrauth(2, 1, 100) AuthPtr; +typedef long __ptrauth(2, 0, 200) UniformInt; +typedef void Fn(void); +typedef Fn *__ptrauth(0, 0, 300) UniformFn; + +_Atomic UniformPtr atomic_ptr, atomic_src; +_Atomic AuthPtr atomic_auth, atomic_auth_src; +UniformPtr qualified_ptr; +_Atomic UniformInt atomic_int; +UniformInt qualified_int; +_Atomic(Fn *) plain_fn; +_Atomic UniformFn atomic_fn; + +// CHECK-LABEL: define {{.*}}ptr @load_implicit() +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @atomic_ptr +// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) +int *load_implicit(void) { return atomic_ptr; } + +// CHECK-LABEL: define {{.*}}void @store_implicit( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_ptr +void store_implicit(int *p) { atomic_ptr = p; } + +// FIXME: authenticates and re-signs under the same schema. +// CHECK-LABEL: define {{.*}}void @copy_implicit() +// CHECK: load atomic ptr, ptr @atomic_src +// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_ptr +void copy_implicit(void) { atomic_ptr = atomic_src; } + +// CHECK-LABEL: define {{.*}}ptr @load_implicit_auth() +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @atomic_auth +// CHECK-NEXT: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth to i64), i64 100) +// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 [[DISC]]) +int *load_implicit_auth(void) { return atomic_auth; } + +// CHECK-LABEL: define {{.*}}void @store_implicit_auth( +// CHECK: [[DISC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth to i64), i64 100) +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DISC]]) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_auth +void store_implicit_auth(int *p) { atomic_auth = p; } + +// FIXME: authenticates and re-signs instead of one resign. +// CHECK-LABEL: define {{.*}}void @copy_implicit_auth() +// CHECK: [[SRC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth_src to i64), i64 100) +// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 [[SRC]]) +// CHECK: [[DST:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth to i64), i64 100) +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DST]]) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_auth +void copy_implicit_auth(void) { atomic_auth = atomic_auth_src; } + +// CHECK-LABEL: define {{.*}}void @store_fn_implicit( +// CHECK: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 0, i64 0, i32 0, i64 300) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_fn +void store_fn_implicit(Fn *p) { atomic_fn = p; } + +// CHECK-LABEL: define {{.*}}ptr @load_fn_implicit() +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @atomic_fn +// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.resign(i64 [[INT]], i32 0, i64 300, i32 0, i64 0) +Fn *load_fn_implicit(void) { return atomic_fn; } + +// CHECK-LABEL: define {{.*}}void @init_decl( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store ptr [[SIGNED]], ptr %l +void init_decl(int *p) { _Atomic UniformPtr l = p; } + +// FIXME: stores the raw pointer. +// CHECK-LABEL: define {{.*}}void @init_c11( +// CHECK-NOT: @llvm.ptrauth +// CHECK: store ptr {{.*}}, ptr @atomic_ptr +void init_c11(int *p) { __c11_atomic_init(&atomic_ptr, p); } + +// FIXME: returns the signed pointer. +// CHECK-LABEL: define {{.*}}ptr @load_c11() +// CHECK: load atomic ptr, ptr @atomic_ptr +// CHECK-NOT: @llvm.ptrauth +int *load_c11(void) { return __c11_atomic_load(&atomic_ptr, __ATOMIC_SEQ_CST); } + +// FIXME: stores the raw pointer. +// CHECK-LABEL: define {{.*}}void @store_c11( +// CHECK-NOT: @llvm.ptrauth +// CHECK: store atomic ptr {{.*}}, ptr @atomic_ptr +void store_c11(int *p) { __c11_atomic_store(&atomic_ptr, p, __ATOMIC_SEQ_CST); } + +// FIXME: stores the raw pointer and returns the signed one. +// CHECK-LABEL: define {{.*}}ptr @exchange_c11( +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw xchg ptr @atomic_ptr, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +int *exchange_c11(int *p) { + return __c11_atomic_exchange(&atomic_ptr, p, __ATOMIC_SEQ_CST); +} + +// FIXME: compares the raw expected and stores the raw desired. +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_c11( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg ptr @atomic_ptr, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_c11(int **expected, int *desired) { + return __c11_atomic_compare_exchange_strong(&atomic_ptr, expected, desired, + __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); +} + +// FIXME: compares the raw expected and stores the raw desired. +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_weak_c11( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg weak ptr @atomic_ptr, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_weak_c11(int **expected, int *desired) { + return __c11_atomic_compare_exchange_weak(&atomic_ptr, expected, desired, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); +} + +// FIXME: returns the signed pointer. +// CHECK-LABEL: define {{.*}}ptr @load_n() +// CHECK: load atomic ptr, ptr @qualified_ptr +// CHECK-NOT: @llvm.ptrauth +int *load_n(void) { return __atomic_load_n(&qualified_ptr, __ATOMIC_SEQ_CST); } + +// FIXME: stores the raw pointer. +// CHECK-LABEL: define {{.*}}void @store_n( +// CHECK-NOT: @llvm.ptrauth +// CHECK: store atomic ptr {{.*}}, ptr @qualified_ptr +void store_n(int *p) { __atomic_store_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); } + +// FIXME: stores the raw pointer and returns the signed one. +// CHECK-LABEL: define {{.*}}ptr @exchange_n( +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +int *exchange_n(int *p) { + return __atomic_exchange_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); +} + +// FIXME: compares the raw expected and stores the raw desired. +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_n( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg ptr @qualified_ptr, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_n(int **expected, int *desired) { + return __atomic_compare_exchange_n(&qualified_ptr, expected, desired, 0, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); +} + +// FIXME: compares the raw expected. +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_generic( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg ptr @qualified_ptr, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_generic(int **expected, UniformPtr *desired) { + return __atomic_compare_exchange(&qualified_ptr, expected, desired, 0, + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); +} + +// FIXME: stores the signed bits into a raw pointer. +// CHECK-LABEL: define {{.*}}void @load_generic( +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @qualified_ptr +// CHECK-NEXT: store ptr [[SIGNED]], ptr +void load_generic(int **out) { + __atomic_load(&qualified_ptr, out, __ATOMIC_SEQ_CST); +} + +// CHECK-LABEL: define {{.*}}void @store_generic( +// CHECK-NOT: @llvm.ptrauth +// CHECK: store atomic ptr {{.*}}, ptr @qualified_ptr +void store_generic(UniformPtr *in) { + __atomic_store(&qualified_ptr, in, __ATOMIC_SEQ_CST); +} + +// FIXME: stores the raw pointer. +// CHECK-LABEL: define {{.*}}void @exchange_generic( +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +void exchange_generic(int **in, UniformPtr *out) { + __atomic_exchange(&qualified_ptr, in, out, __ATOMIC_SEQ_CST); +} + +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_plain_fn( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg ptr @plain_fn, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_plain_fn(Fn **expected, Fn *desired) { + return __c11_atomic_compare_exchange_strong(&plain_fn, expected, desired, + __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); +} + +// FIXME: compares and stores pointers signed under the function pointer schema. +// CHECK-LABEL: define {{.*}}i1 @cmpxchg_fn( +// CHECK-NOT: @llvm.ptrauth +// CHECK: cmpxchg ptr @atomic_fn, ptr {{.*}}, ptr {{.*}} +// CHECK-NOT: @llvm.ptrauth +_Bool cmpxchg_fn(Fn **expected, Fn *desired) { + return __c11_atomic_compare_exchange_strong(&atomic_fn, expected, desired, + __ATOMIC_SEQ_CST, + __ATOMIC_SEQ_CST); +} + +// FIXME: returns the signed integer. +// CHECK-LABEL: define {{.*}}i64 @load_int() +// CHECK: load atomic i64, ptr @atomic_int +// CHECK-NOT: @llvm.ptrauth +long load_int(void) { return __c11_atomic_load(&atomic_int, __ATOMIC_SEQ_CST); } + +// FIXME: stores the raw integer. +// CHECK-LABEL: define {{.*}}void @store_int( +// CHECK-NOT: @llvm.ptrauth +// CHECK: store atomic i64 {{.*}}, ptr @atomic_int +void store_int(long v) { __c11_atomic_store(&atomic_int, v, __ATOMIC_SEQ_CST); } + +// FIXME: adds to the signed bits. +// CHECK-LABEL: define {{.*}}i64 @fetch_add_int() +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw add ptr @atomic_int, i64 {{.*}} +// CHECK-NOT: @llvm.ptrauth +long fetch_add_int(void) { + return __c11_atomic_fetch_add(&atomic_int, 1, __ATOMIC_SEQ_CST); +} + +// FIXME: adds to the signed bits. +// CHECK-LABEL: define {{.*}}void @add_assign_int() +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw add ptr @atomic_int, i64 1 +// CHECK-NOT: @llvm.ptrauth +void add_assign_int(void) { atomic_int += 1; } + +// FIXME: adds to the signed bits. +// CHECK-LABEL: define {{.*}}void @inc_int() +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw add ptr @atomic_int, i64 1 +// CHECK-NOT: @llvm.ptrauth +void inc_int(void) { atomic_int++; } + +// FIXME: adds to the signed bits. +// CHECK-LABEL: define {{.*}}i64 @sync_fetch_add_int() +// CHECK-NOT: @llvm.ptrauth +// CHECK: atomicrmw add ptr @qualified_int, i64 1 +// CHECK-NOT: @llvm.ptrauth +long sync_fetch_add_int(void) { return __sync_fetch_and_add(&qualified_int, 1); } + +// FIXME: writes a raw byte into the signed representation. +// CHECK-LABEL: define {{.*}}i1 @test_and_set() +// CHECK: atomicrmw xchg ptr @qualified_ptr, i8 1 +_Bool test_and_set(void) { + return __atomic_test_and_set(&qualified_ptr, __ATOMIC_SEQ_CST); +} + +// FIXME: writes a raw byte into the signed representation. +// CHECK-LABEL: define {{.*}}void @clear() +// CHECK: store atomic i8 0, ptr @qualified_ptr +void clear(void) { __atomic_clear(&qualified_ptr, __ATOMIC_SEQ_CST); } + +// FIXME: authenticates once, then compares and increments the signed bits without re-signing. +// CHECK-LABEL: define {{.*}}void @inc_ptr() +// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) +// CHECK: [[AUTHED:%.*]] = phi ptr [ null, {{.*}} ] +// CHECK: [[CUR:%.*]] = phi ptr [ [[AUTHED]], {{.*}} ], [ [[OBSERVED:%.*]], {{.*}} ] +// CHECK-NEXT: [[NEW:%.*]] = getelementptr {{.*}}, ptr [[CUR]], i32 1 +// CHECK-NEXT: [[PAIR:%.*]] = cmpxchg ptr @atomic_ptr, ptr [[CUR]], ptr [[NEW]] +// CHECK-NEXT: [[OBSERVED]] = extractvalue { ptr, i1 } [[PAIR]], 0 +// CHECK-NOT: @llvm.ptrauth +void inc_ptr(void) { atomic_ptr++; } >From 1403fce9cf7d082653021aac97b61f5142f6269c Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Mon, 9 Nov 2026 15:10:30 +0900 Subject: [PATCH 2/9] [clang][PAC] Sign and authenticate atomics on __ptrauth types typedef int *__ptrauth(2, 0, 50) Ptr; _Atomic Ptr p; int *load(void) { return __c11_atomic_load(&p, __ATOMIC_SEQ_CST); } returned the signed bits as if they were the pointer, and __c11_atomic_store wrote the raw pointer, which the next read failed to authenticate. Loads now authenticate and stores sign, as the implicit _Atomic operations already do. Same for __c11_atomic_init, exchange, compare-exchange and the GNU _n forms. A copy between two _Atomic qualified objects resigns at most once instead of authenticating and signing. Operands passed by address, such as the expected of a compare-exchange, must point to the object's qualified type. __sync_*, atomic arithmetic, __atomic_test_and_set, __atomic_clear, and implicit ++, -- and compound assignment of an _Atomic object are rejected. Assisted-by: AI --- clang/docs/PointerAuthentication.md | 7 + clang/docs/ReleaseNotes.md | 3 + .../clang/Basic/DiagnosticSemaKinds.td | 7 + clang/lib/CodeGen/CGAtomic.cpp | 49 ++++++- clang/lib/CodeGen/CGPointerAuth.cpp | 7 + clang/lib/Sema/SemaChecking.cpp | 29 +++- clang/lib/Sema/SemaExpr.cpp | 13 ++ clang/test/CodeGen/ptrauth-qualifier-atomic.c | 137 +++++------------- clang/test/Sema/ptrauth-atomic-ops.c | 137 ++++++++++++++---- 9 files changed, 250 insertions(+), 139 deletions(-) diff --git a/clang/docs/PointerAuthentication.md b/clang/docs/PointerAuthentication.md index 8a77d0178430a3..84bff23dfc8ada 100644 --- a/clang/docs/PointerAuthentication.md +++ b/clang/docs/PointerAuthentication.md @@ -431,6 +431,13 @@ 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 on a +`__ptrauth`-qualified object use its schema. An operand passed by address, +such as the `expected` of a compare-exchange, must point to the object's +`__ptrauth`-qualified type. Atomic arithmetic, `__sync_*`, +`__atomic_test_and_set`, `__atomic_clear`, and implicit increment, decrement +and compound assignment of an `_Atomic` object are rejected. + ##### 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 603b3096259cb5..80eb391f545256 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -1336,6 +1336,9 @@ 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 load, store, exchange and compare-exchange on a `__ptrauth`-qualified + object now sign and authenticate. + - 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/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 80de4627959c4b..f9046207551f7f 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -9700,6 +9700,13 @@ def err_atomic_op_needs_non_const_pointer : Error< 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|atomic test-and-set or " + "clear}0 operation must not be a pointer to a '__ptrauth'-qualified type (%1 " + "invalid)">; +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/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp index cd7b278eab15d9..d1f9592a0bf163 100644 --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -375,6 +375,20 @@ bool AtomicInfo::emitMemSetZeroIfNecessary() const { return true; } +/// The '__ptrauth' qualifier of the object of \p E when the result of \p E is +/// a plain value. +static PointerAuthQualifier getResultPointerAuth(const AtomicExpr *E) { + switch (E->getOp()) { + case AtomicExpr::AO__atomic_load: + case AtomicExpr::AO__atomic_exchange: + case AtomicExpr::AO__scoped_atomic_load: + case AtomicExpr::AO__scoped_atomic_exchange: + return PointerAuthQualifier(); + default: + return E->getPtr()->getType()->getPointeeType().getPointerAuth(); + } +} + static void emitAtomicCmpXchg(CodeGenFunction &CGF, AtomicExpr *E, bool IsWeak, Address Dest, Address Ptr, Address Val1, Address Val2, Address ExpectedResult, @@ -633,7 +647,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); + llvm::Value *Result = Load; + if (PointerAuthQualifier PtrAuth = getResultPointerAuth(E)) + Result = CGF.EmitPointerAuthUnqualify( + PtrAuth, Load, E->getValueType().getUnqualifiedType(), Ptr, + /*IsKnownNonNull=*/false); + auto *I = CGF.Builder.CreateStore(Result, Dest); CGF.addInstToCurrentSourceAtom(I, Load); return; } @@ -838,6 +857,12 @@ 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 (PointerAuthQualifier PtrAuth = getResultPointerAuth(E)) { + assert(Op == llvm::AtomicRMWInst::Xchg && !PostOp && !PostOpMinMax); + Result = CGF.EmitPointerAuthUnqualify( + PtrAuth, RMWI, E->getValueType().getUnqualifiedType(), Ptr, + /*IsKnownNonNull=*/false); + } if (PostOpMinMax) Result = EmitPostAtomicMinMax(CGF.Builder, E->getOp(), E->getValueType()->isSignedIntegerType(), @@ -854,11 +879,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; } @@ -1030,7 +1059,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(), AtomicTy.getPointerAuth(), Ptr); OrderFail = EmitScalarExpr(E->getOrderFail()); if (E->getOp() == AtomicExpr::AO__atomic_compare_exchange_n || E->getOp() == AtomicExpr::AO__atomic_compare_exchange || @@ -1133,7 +1162,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(), AtomicTy.getPointerAuth(), Ptr); break; } @@ -2222,7 +2251,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/CGPointerAuth.cpp b/clang/lib/CodeGen/CGPointerAuth.cpp index 62697b317f9126..6072f87bd0ad22 100644 --- a/clang/lib/CodeGen/CGPointerAuth.cpp +++ b/clang/lib/CodeGen/CGPointerAuth.cpp @@ -263,6 +263,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..22c6c54853f872 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5461,6 +5461,15 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, } } + if (AtomTy.getPointerAuth()) { + bool IsByte = Form == TestAndSetByte || Form == ClearByte; + if (IsByte || Form == Arithmetic) { + Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_ptrauth_pointer) + << (IsByte ? 2 : 1) << Ptr->getType() << Ptr->getSourceRange(); + return ExprError(); + } + } + if (Form != TestAndSetByte && Form != ClearByte) { // Pointer to object of size zero is not allowed. if (RequireCompleteType(Ptr->getBeginLoc(), AtomTy, @@ -5572,6 +5581,11 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, // pointer to a const 'A'. ValType.removeLocalVolatile(); ValType.removeLocalConst(); + if (ValType.getPointerAuth()) { + Qualifiers Quals = ValType.getQualifiers(); + Quals.removePointerAuth(); + ValType = Context.getQualifiedType(ValType.getUnqualifiedType(), Quals); + } QualType ResultType = ValType; if (Form == Copy || Form == LoadCopy || Form == GNUXchg || Form == Init || Form == ClearByte) @@ -5671,8 +5685,11 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, ValArg->getType()->getAs<PointerType>()) { AS = PtrTy->getPointeeType().getAddressSpace(); } - Ty = Context.getPointerType( - Context.getAddrSpaceQualType(ValType.getUnqualifiedType(), AS)); + QualType Pointee = ValType.getUnqualifiedType(); + if (PointerAuth) + Pointee = Context.getPointerAuthType(Pointee, PointerAuth); + Ty = + Context.getPointerType(Context.getAddrSpaceQualType(Pointee, AS)); } break; case 2: @@ -5872,11 +5889,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/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp index 5c3541cfab0187..81daeeb82a7db7 100644 --- a/clang/lib/Sema/SemaExpr.cpp +++ b/clang/lib/Sema/SemaExpr.cpp @@ -15072,6 +15072,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,6 +15905,14 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, } } + if (BinaryOperator::isCompoundAssignmentOp(Opc) && + LHSExpr->getType().getPointerAuth() && + LHSExpr->getType()->isAtomicType()) { + Diag(OpLoc, diag::err_ptrauth_atomic_rmw) + << 2 << LHSExpr->getType() << LHSExpr->getSourceRange(); + return ExprError(); + } + checkTypeSupport(LHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr); checkTypeSupport(RHSExpr->getType(), OpLoc, /*ValueDecl*/ nullptr); diff --git a/clang/test/CodeGen/ptrauth-qualifier-atomic.c b/clang/test/CodeGen/ptrauth-qualifier-atomic.c index dc939dfd710684..a7b135344fa9ad 100644 --- a/clang/test/CodeGen/ptrauth-qualifier-atomic.c +++ b/clang/test/CodeGen/ptrauth-qualifier-atomic.c @@ -10,7 +10,6 @@ _Atomic UniformPtr atomic_ptr, atomic_src; _Atomic AuthPtr atomic_auth, atomic_auth_src; UniformPtr qualified_ptr; _Atomic UniformInt atomic_int; -UniformInt qualified_int; _Atomic(Fn *) plain_fn; _Atomic UniformFn atomic_fn; @@ -26,12 +25,8 @@ int *load_implicit(void) { return atomic_ptr; } // CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_ptr void store_implicit(int *p) { atomic_ptr = p; } -// FIXME: authenticates and re-signs under the same schema. // CHECK-LABEL: define {{.*}}void @copy_implicit() -// CHECK: load atomic ptr, ptr @atomic_src -// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) -// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) -// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @atomic_src // CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_ptr void copy_implicit(void) { atomic_ptr = atomic_src; } @@ -49,12 +44,12 @@ int *load_implicit_auth(void) { return atomic_auth; } // CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_auth void store_implicit_auth(int *p) { atomic_auth = p; } -// FIXME: authenticates and re-signs instead of one resign. // CHECK-LABEL: define {{.*}}void @copy_implicit_auth() // CHECK: [[SRC:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth_src to i64), i64 100) -// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 [[SRC]]) -// CHECK: [[DST:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth to i64), i64 100) -// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 [[DST]]) +// CHECK-NEXT: [[DST:%.*]] = call i64 @llvm.ptrauth.blend(i64 ptrtoint (ptr @atomic_auth to i64), i64 100) +// CHECK-NOT: @llvm.ptrauth +// CHECK: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 2, i64 [[SRC]], i32 2, i64 [[DST]]) +// CHECK-NOT: @llvm.ptrauth // CHECK: [[SIGNED:%.*]] = phi ptr // CHECK-NEXT: store atomic ptr [[SIGNED]], ptr @atomic_auth void copy_implicit_auth(void) { atomic_auth = atomic_auth_src; } @@ -77,100 +72,100 @@ Fn *load_fn_implicit(void) { return atomic_fn; } // CHECK-NEXT: store ptr [[SIGNED]], ptr %l void init_decl(int *p) { _Atomic UniformPtr l = p; } -// FIXME: stores the raw pointer. // CHECK-LABEL: define {{.*}}void @init_c11( -// CHECK-NOT: @llvm.ptrauth -// CHECK: store ptr {{.*}}, ptr @atomic_ptr +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) +// CHECK: [[SIGNED:%.*]] = phi ptr +// CHECK-NEXT: store ptr [[SIGNED]], ptr @atomic_ptr void init_c11(int *p) { __c11_atomic_init(&atomic_ptr, p); } -// FIXME: returns the signed pointer. // CHECK-LABEL: define {{.*}}ptr @load_c11() -// CHECK: load atomic ptr, ptr @atomic_ptr -// CHECK-NOT: @llvm.ptrauth +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @atomic_ptr +// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) int *load_c11(void) { return __c11_atomic_load(&atomic_ptr, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw pointer. // CHECK-LABEL: define {{.*}}void @store_c11( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth // CHECK: store atomic ptr {{.*}}, ptr @atomic_ptr void store_c11(int *p) { __c11_atomic_store(&atomic_ptr, p, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw pointer and returns the signed one. // CHECK-LABEL: define {{.*}}ptr @exchange_c11( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw xchg ptr @atomic_ptr, ptr {{.*}} -// CHECK-NOT: @llvm.ptrauth +// CHECK: [[OLD:%.*]] = atomicrmw xchg ptr @atomic_ptr, ptr {{.*}} +// CHECK: [[INT:%.*]] = ptrtoint ptr [[OLD]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) int *exchange_c11(int *p) { return __c11_atomic_exchange(&atomic_ptr, p, __ATOMIC_SEQ_CST); } -// FIXME: compares the raw expected and stores the raw desired. // CHECK-LABEL: define {{.*}}i1 @cmpxchg_c11( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth // CHECK: cmpxchg ptr @atomic_ptr, ptr {{.*}}, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -_Bool cmpxchg_c11(int **expected, int *desired) { +_Bool cmpxchg_c11(UniformPtr *expected, int *desired) { return __c11_atomic_compare_exchange_strong(&atomic_ptr, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } -// FIXME: compares the raw expected and stores the raw desired. // CHECK-LABEL: define {{.*}}i1 @cmpxchg_weak_c11( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth // CHECK: cmpxchg weak ptr @atomic_ptr, ptr {{.*}}, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -_Bool cmpxchg_weak_c11(int **expected, int *desired) { +_Bool cmpxchg_weak_c11(UniformPtr *expected, int *desired) { return __c11_atomic_compare_exchange_weak(&atomic_ptr, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } -// FIXME: returns the signed pointer. // CHECK-LABEL: define {{.*}}ptr @load_n() -// CHECK: load atomic ptr, ptr @qualified_ptr -// CHECK-NOT: @llvm.ptrauth +// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @qualified_ptr +// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) int *load_n(void) { return __atomic_load_n(&qualified_ptr, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw pointer. // CHECK-LABEL: define {{.*}}void @store_n( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth // CHECK: store atomic ptr {{.*}}, ptr @qualified_ptr void store_n(int *p) { __atomic_store_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw pointer and returns the signed one. // CHECK-LABEL: define {{.*}}ptr @exchange_n( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} -// CHECK-NOT: @llvm.ptrauth +// CHECK: [[OLD:%.*]] = atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} +// CHECK: [[INT:%.*]] = ptrtoint ptr [[OLD]] to i64 +// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) int *exchange_n(int *p) { return __atomic_exchange_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); } -// FIXME: compares the raw expected and stores the raw desired. // CHECK-LABEL: define {{.*}}i1 @cmpxchg_n( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth // CHECK: cmpxchg ptr @qualified_ptr, ptr {{.*}}, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -_Bool cmpxchg_n(int **expected, int *desired) { +_Bool cmpxchg_n(UniformPtr *expected, int *desired) { return __atomic_compare_exchange_n(&qualified_ptr, expected, desired, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } -// FIXME: compares the raw expected. // CHECK-LABEL: define {{.*}}i1 @cmpxchg_generic( // CHECK-NOT: @llvm.ptrauth // CHECK: cmpxchg ptr @qualified_ptr, ptr {{.*}}, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -_Bool cmpxchg_generic(int **expected, UniformPtr *desired) { +_Bool cmpxchg_generic(UniformPtr *expected, UniformPtr *desired) { return __atomic_compare_exchange(&qualified_ptr, expected, desired, 0, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } -// FIXME: stores the signed bits into a raw pointer. // CHECK-LABEL: define {{.*}}void @load_generic( // CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @qualified_ptr // CHECK-NEXT: store ptr [[SIGNED]], ptr -void load_generic(int **out) { +void load_generic(UniformPtr *out) { __atomic_load(&qualified_ptr, out, __ATOMIC_SEQ_CST); } @@ -181,12 +176,11 @@ void store_generic(UniformPtr *in) { __atomic_store(&qualified_ptr, in, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw pointer. // CHECK-LABEL: define {{.*}}void @exchange_generic( // CHECK-NOT: @llvm.ptrauth // CHECK: atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -void exchange_generic(int **in, UniformPtr *out) { +void exchange_generic(UniformPtr *in, UniformPtr *out) { __atomic_exchange(&qualified_ptr, in, out, __ATOMIC_SEQ_CST); } @@ -200,78 +194,25 @@ _Bool cmpxchg_plain_fn(Fn **expected, Fn *desired) { __ATOMIC_SEQ_CST); } -// FIXME: compares and stores pointers signed under the function pointer schema. // CHECK-LABEL: define {{.*}}i1 @cmpxchg_fn( // CHECK-NOT: @llvm.ptrauth +// CHECK: call i64 @llvm.ptrauth.resign(i64 {{.*}}, i32 0, i64 0, i32 0, i64 300) +// CHECK-NOT: @llvm.ptrauth // CHECK: cmpxchg ptr @atomic_fn, ptr {{.*}}, ptr {{.*}} // CHECK-NOT: @llvm.ptrauth -_Bool cmpxchg_fn(Fn **expected, Fn *desired) { +_Bool cmpxchg_fn(UniformFn *expected, Fn *desired) { return __c11_atomic_compare_exchange_strong(&atomic_fn, expected, desired, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); } -// FIXME: returns the signed integer. // CHECK-LABEL: define {{.*}}i64 @load_int() -// CHECK: load atomic i64, ptr @atomic_int -// CHECK-NOT: @llvm.ptrauth +// CHECK: [[SIGNED:%.*]] = load atomic i64, ptr @atomic_int +// CHECK: call i64 @llvm.ptrauth.auth(i64 [[SIGNED]], i32 2, i64 200) long load_int(void) { return __c11_atomic_load(&atomic_int, __ATOMIC_SEQ_CST); } -// FIXME: stores the raw integer. // CHECK-LABEL: define {{.*}}void @store_int( +// CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 200) // CHECK-NOT: @llvm.ptrauth // CHECK: store atomic i64 {{.*}}, ptr @atomic_int void store_int(long v) { __c11_atomic_store(&atomic_int, v, __ATOMIC_SEQ_CST); } - -// FIXME: adds to the signed bits. -// CHECK-LABEL: define {{.*}}i64 @fetch_add_int() -// CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw add ptr @atomic_int, i64 {{.*}} -// CHECK-NOT: @llvm.ptrauth -long fetch_add_int(void) { - return __c11_atomic_fetch_add(&atomic_int, 1, __ATOMIC_SEQ_CST); -} - -// FIXME: adds to the signed bits. -// CHECK-LABEL: define {{.*}}void @add_assign_int() -// CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw add ptr @atomic_int, i64 1 -// CHECK-NOT: @llvm.ptrauth -void add_assign_int(void) { atomic_int += 1; } - -// FIXME: adds to the signed bits. -// CHECK-LABEL: define {{.*}}void @inc_int() -// CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw add ptr @atomic_int, i64 1 -// CHECK-NOT: @llvm.ptrauth -void inc_int(void) { atomic_int++; } - -// FIXME: adds to the signed bits. -// CHECK-LABEL: define {{.*}}i64 @sync_fetch_add_int() -// CHECK-NOT: @llvm.ptrauth -// CHECK: atomicrmw add ptr @qualified_int, i64 1 -// CHECK-NOT: @llvm.ptrauth -long sync_fetch_add_int(void) { return __sync_fetch_and_add(&qualified_int, 1); } - -// FIXME: writes a raw byte into the signed representation. -// CHECK-LABEL: define {{.*}}i1 @test_and_set() -// CHECK: atomicrmw xchg ptr @qualified_ptr, i8 1 -_Bool test_and_set(void) { - return __atomic_test_and_set(&qualified_ptr, __ATOMIC_SEQ_CST); -} - -// FIXME: writes a raw byte into the signed representation. -// CHECK-LABEL: define {{.*}}void @clear() -// CHECK: store atomic i8 0, ptr @qualified_ptr -void clear(void) { __atomic_clear(&qualified_ptr, __ATOMIC_SEQ_CST); } - -// FIXME: authenticates once, then compares and increments the signed bits without re-signing. -// CHECK-LABEL: define {{.*}}void @inc_ptr() -// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) -// CHECK: [[AUTHED:%.*]] = phi ptr [ null, {{.*}} ] -// CHECK: [[CUR:%.*]] = phi ptr [ [[AUTHED]], {{.*}} ], [ [[OBSERVED:%.*]], {{.*}} ] -// CHECK-NEXT: [[NEW:%.*]] = getelementptr {{.*}}, ptr [[CUR]], i32 1 -// CHECK-NEXT: [[PAIR:%.*]] = cmpxchg ptr @atomic_ptr, ptr [[CUR]], ptr [[NEW]] -// CHECK-NEXT: [[OBSERVED]] = extractvalue { ptr, i1 } [[PAIR]], 0 -// CHECK-NOT: @llvm.ptrauth -void inc_ptr(void) { atomic_ptr++; } diff --git a/clang/test/Sema/ptrauth-atomic-ops.c b/clang/test/Sema/ptrauth-atomic-ops.c index a5411f2e197b3f..ec0580efaf5a8b 100644 --- a/clang/test/Sema/ptrauth-atomic-ops.c +++ b/clang/test/Sema/ptrauth-atomic-ops.c @@ -7,6 +7,8 @@ int i; int *__ptrauth(2, 1, 100) authenticated_ptr = &i; int *__ptrauth(2, 0, 200) non_addr_discriminatedauthenticated_ptr = &i; int * wat = &i; +long __ptrauth(2, 0, 200) qualified_int; +_Atomic long __ptrauth(2, 0, 200) atomic_qualified_int; #define ATOMIZE(p) (__typeof__(p) volatile _Atomic *)(long)(&p) void f() { @@ -28,23 +30,23 @@ void f() { __atomic_exchange(ATOMIZE(authenticated_ptr), &j, &j, memory_order_seq_cst); // expected-error@-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_fetch_add(ATOMIZE(authenticated_ptr), 1, memory_order_seq_cst); - // expected-error@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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_init(ATOMIZE(non_addr_discriminatedauthenticated_ptr), &j); __c11_atomic_store(ATOMIZE(non_addr_discriminatedauthenticated_ptr), 0, memory_order_relaxed); @@ -54,65 +56,148 @@ void f() { __c11_atomic_exchange(ATOMIZE(j), ATOMIZE(non_addr_discriminatedauthenticated_ptr), memory_order_seq_cst); // expected-error@-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@-1 {{incompatible pointer to integer conversion passing 'volatile _Atomic(typeof (j)) *' to parameter of type '__ptrdiff_t'}} + // expected-error@-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@-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), 1, memory_order_seq_cst); + // expected-error@-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)}} + __atomic_fetch_add(&non_addr_discriminatedauthenticated_ptr, 1, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} + __c11_atomic_fetch_add(&atomic_qualified_int, 1, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) _Atomic(long) *' invalid)}} + __atomic_fetch_add(&qualified_int, 1, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} + __atomic_fetch_or(&atomic_qualified_int, 1, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) _Atomic(long) *' invalid)}} + + __atomic_test_and_set(&non_addr_discriminatedauthenticated_ptr, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} + __atomic_clear(&non_addr_discriminatedauthenticated_ptr, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} + __atomic_test_and_set(ATOMIZE(authenticated_ptr), memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}} + __atomic_clear(&qualified_int, memory_order_seq_cst); + // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} + + (*ATOMIZE(non_addr_discriminatedauthenticated_ptr))++; + // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} + --(*ATOMIZE(non_addr_discriminatedauthenticated_ptr)); + // expected-error@-1 {{atomic decrement of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} + (*ATOMIZE(non_addr_discriminatedauthenticated_ptr)) += 1; + // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} + (*ATOMIZE(authenticated_ptr))--; + // expected-error@-1 {{atomic decrement of '__ptrauth'-qualified type 'volatile __ptrauth(2,1,100) _Atomic(int *)' is not supported}} + atomic_qualified_int++; + // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} + ++atomic_qualified_int; + // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} + atomic_qualified_int -= 2; + // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} + atomic_qualified_int |= 2; + // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} + qualified_int++; + qualified_int += 2; + atomic_qualified_int = 3; __sync_fetch_and_add(&authenticated_ptr, 1); - // expected-error@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-1 {{address argument to __sync operation must be a pointer to a non address discriminated type ('int *__ptrauth(2,1,100)' invalid)}} + // expected-error@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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@-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_add(&qualified_int, 1); + // expected-error@-1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} +} + +typedef int *__ptrauth(2, 0, 200) UniformPtr; +UniformPtr uniform; +_Atomic UniformPtr atomic_uniform; +_Atomic(int *) atomic_plain; + +void by_address(int *p, int **raw, UniformPtr *same) { + __c11_atomic_compare_exchange_strong(&atomic_uniform, same, p, memory_order_seq_cst, memory_order_seq_cst); + __atomic_compare_exchange_n(&uniform, same, p, 0, memory_order_seq_cst, memory_order_seq_cst); + __atomic_compare_exchange(&uniform, same, same, 0, memory_order_seq_cst, memory_order_seq_cst); + __atomic_load(&uniform, same, memory_order_seq_cst); + __atomic_store(&uniform, same, memory_order_seq_cst); + __atomic_exchange(&uniform, same, same, memory_order_seq_cst); + + __c11_atomic_compare_exchange_strong(&atomic_uniform, raw, p, memory_order_seq_cst, memory_order_seq_cst); + // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} + __atomic_compare_exchange_n(&uniform, raw, p, 0, memory_order_seq_cst, memory_order_seq_cst); + // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} + __atomic_compare_exchange(&uniform, raw, same, 0, memory_order_seq_cst, memory_order_seq_cst); + // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} + __atomic_load(&uniform, raw, memory_order_seq_cst); + // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} + __atomic_exchange(&uniform, raw, same, memory_order_seq_cst); + // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} + __c11_atomic_compare_exchange_strong(&atomic_plain, same, p, memory_order_seq_cst, memory_order_seq_cst); + // expected-error@-1 {{passing 'UniformPtr *' (aka 'int *__ptrauth(2,0,200) *') to parameter of type 'int **' changes pointer authentication of pointee type}} } >From 8b65fe6c8eef2fdbc16344ea09475887b9634f82 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:14:38 +0900 Subject: [PATCH 3/9] Address review: emit the operand temporary without qualifiers Assisted-by: AI --- clang/lib/CodeGen/CGAtomic.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp index d1f9592a0bf163..de79d4257259ab 100644 --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -886,8 +886,7 @@ static Address EmitValToTemp(CodeGenFunction &CGF, Expr *E, CGF.Builder.CreateStore(CGF.EmitPointerAuthQualify(PtrAuth, E, Ptr), DeclPtr); else - CGF.EmitAnyExprToMem(E, DeclPtr, E->getType().getQualifiers(), - /*Init*/ true); + CGF.EmitAnyExprToMem(E, DeclPtr, Qualifiers(), /*Init*/ true); return DeclPtr; } >From 376735af54631aad2ac2cfac1a0434d977dcd8e9 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:14:50 +0900 Subject: [PATCH 4/9] Address review: look through implicit atomic conversions only Assisted-by: AI --- clang/lib/CodeGen/CGPointerAuth.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/lib/CodeGen/CGPointerAuth.cpp b/clang/lib/CodeGen/CGPointerAuth.cpp index 6072f87bd0ad22..46ef0270d52302 100644 --- a/clang/lib/CodeGen/CGPointerAuth.cpp +++ b/clang/lib/CodeGen/CGPointerAuth.cpp @@ -264,7 +264,7 @@ CodeGenFunction::EmitPointerAuthQualify(PointerAuthQualifier DestQualifier, Address DestStorageAddress) { assert(DestQualifier); E = E->IgnoreParens(); - while (const auto *Cast = dyn_cast<CastExpr>(E)) { + while (const auto *Cast = dyn_cast<ImplicitCastExpr>(E)) { if (Cast->getCastKind() != CK_NonAtomicToAtomic && Cast->getCastKind() != CK_AtomicToNonAtomic) break; >From b84306d5d79bbae36d122e5c5bffb6b363b3f2a5 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:54:54 +0900 Subject: [PATCH 5/9] Address review: drop all qualifiers from the atomic value type Assisted-by: AI --- clang/lib/Sema/SemaChecking.cpp | 8 +------- clang/test/Sema/atomic-ops.c | 8 ++++++++ clang/test/Sema/ptrauth-atomic-ops.c | 2 ++ clang/test/SemaCXX/atomic-ops.cpp | 12 ++++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 22c6c54853f872..79153685558750 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5579,13 +5579,7 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, // 'A'. We shouldn't let the volatile-ness of the pointee-type inject itself // into the result or the other operands. Similarly atomic_load takes a // pointer to a const 'A'. - ValType.removeLocalVolatile(); - ValType.removeLocalConst(); - if (ValType.getPointerAuth()) { - Qualifiers Quals = ValType.getQualifiers(); - Quals.removePointerAuth(); - ValType = Context.getQualifiedType(ValType.getUnqualifiedType(), Quals); - } + ValType = ValType.getUnqualifiedType(); QualType ResultType = ValType; if (Form == Copy || Form == LoadCopy || Form == GNUXchg || Form == Init || Form == ClearByte) diff --git a/clang/test/Sema/atomic-ops.c b/clang/test/Sema/atomic-ops.c index cebbc78ea22624..890c81265decca 100644 --- a/clang/test/Sema/atomic-ops.c +++ b/clang/test/Sema/atomic-ops.c @@ -976,3 +976,11 @@ void nullPointerWarning(void) { (void)__atomic_fetch_max(&vi, 0, memory_order_relaxed); (void)__atomic_fetch_max(&i, 0, memory_order_relaxed); } + +typedef const int ConstInt; +extern ConstInt cint; + +void unqualified_result(void) { + __typeof__(__atomic_load_n(&cint, memory_order_relaxed)) r; + r = 1; +} diff --git a/clang/test/Sema/ptrauth-atomic-ops.c b/clang/test/Sema/ptrauth-atomic-ops.c index ec0580efaf5a8b..d4cd9664ae7f51 100644 --- a/clang/test/Sema/ptrauth-atomic-ops.c +++ b/clang/test/Sema/ptrauth-atomic-ops.c @@ -187,6 +187,8 @@ void by_address(int *p, int **raw, UniformPtr *same) { __atomic_load(&uniform, same, memory_order_seq_cst); __atomic_store(&uniform, same, memory_order_seq_cst); __atomic_exchange(&uniform, same, same, memory_order_seq_cst); + __typeof__(__atomic_load_n(&uniform, memory_order_seq_cst)) loaded; + int **unqualified = &loaded; __c11_atomic_compare_exchange_strong(&atomic_uniform, raw, p, memory_order_seq_cst, memory_order_seq_cst); // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} diff --git a/clang/test/SemaCXX/atomic-ops.cpp b/clang/test/SemaCXX/atomic-ops.cpp index 30d829d1e97d79..d4182c3f61e28c 100644 --- a/clang/test/SemaCXX/atomic-ops.cpp +++ b/clang/test/SemaCXX/atomic-ops.cpp @@ -22,3 +22,15 @@ void f() { __atomic_store(p, p, __ATOMIC_RELAXED); __atomic_compare_exchange(p, p, q, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED); } + +typedef volatile int VI; +typedef const int CI; +extern VI vi; +extern CI ci; +void sink(int &&); + +void unqualified_result() { + sink(__atomic_load_n(&vi, __ATOMIC_RELAXED)); + sink(__atomic_load_n(&ci, __ATOMIC_RELAXED)); + sink(__atomic_exchange_n(&vi, 0, __ATOMIC_RELAXED)); +} >From d8daccb00df95fc1008803ba031285cde9c226e5 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:55:03 +0900 Subject: [PATCH 6/9] Address review: drop a redundant comment Assisted-by: AI --- clang/lib/CodeGen/CGAtomic.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp index de79d4257259ab..45c7a62d87a090 100644 --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -375,8 +375,6 @@ bool AtomicInfo::emitMemSetZeroIfNecessary() const { return true; } -/// The '__ptrauth' qualifier of the object of \p E when the result of \p E is -/// a plain value. static PointerAuthQualifier getResultPointerAuth(const AtomicExpr *E) { switch (E->getOp()) { case AtomicExpr::AO__atomic_load: >From 583dd3c0ff98aa77660466f878a159f65cd33370 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:15:37 +0900 Subject: [PATCH 7/9] Copy arrays of __ptrauth pointers as bits The first commit of this PR asserted on __atomic_load of an array of __ptrauth pointers, which upstream copies as bits. Assisted-by: AI --- clang/lib/Sema/SemaChecking.cpp | 3 ++- clang/test/CodeGen/ptrauth-qualifier-atomic.c | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp index 79153685558750..5ce94fcf1d7ad9 100644 --- a/clang/lib/Sema/SemaChecking.cpp +++ b/clang/lib/Sema/SemaChecking.cpp @@ -5494,7 +5494,8 @@ ExprResult Sema::BuildAtomicExpr(SourceRange CallRange, SourceRange ExprRange, ValType = AtomTy; } - PointerAuthQualifier PointerAuth = AtomTy.getPointerAuth(); + PointerAuthQualifier PointerAuth = + AtomTy->isArrayType() ? PointerAuthQualifier() : AtomTy.getPointerAuth(); if (PointerAuth && PointerAuth.isAddressDiscriminated()) { Diag(ExprRange.getBegin(), diag::err_atomic_op_needs_non_address_discriminated_pointer) diff --git a/clang/test/CodeGen/ptrauth-qualifier-atomic.c b/clang/test/CodeGen/ptrauth-qualifier-atomic.c index a7b135344fa9ad..9779858b8b6a44 100644 --- a/clang/test/CodeGen/ptrauth-qualifier-atomic.c +++ b/clang/test/CodeGen/ptrauth-qualifier-atomic.c @@ -216,3 +216,14 @@ long load_int(void) { return __c11_atomic_load(&atomic_int, __ATOMIC_SEQ_CST); } // CHECK-NOT: @llvm.ptrauth // CHECK: store atomic i64 {{.*}}, ptr @atomic_int void store_int(long v) { __c11_atomic_store(&atomic_int, v, __ATOMIC_SEQ_CST); } + +UniformPtr uniform_array[1], uniform_array_out[1]; + +// CHECK-LABEL: define {{.*}}void @load_array() +// CHECK-NOT: @llvm.ptrauth +// CHECK: load atomic i64, ptr @uniform_array +// CHECK-NOT: @llvm.ptrauth +// CHECK: store i64 {{.*}}, ptr @uniform_array_out +void load_array(void) { + __atomic_load(&uniform_array, &uniform_array_out, __ATOMIC_SEQ_CST); +} >From 2110b3122ec6ac859b5b8d0fa64531b93afd6c49 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:55:40 +0900 Subject: [PATCH 8/9] Trim tests Assisted-by: AI --- clang/test/CodeGen/ptrauth-qualifier-atomic.c | 10 +++---- clang/test/Sema/ptrauth-atomic-ops.c | 27 ------------------- 2 files changed, 4 insertions(+), 33 deletions(-) diff --git a/clang/test/CodeGen/ptrauth-qualifier-atomic.c b/clang/test/CodeGen/ptrauth-qualifier-atomic.c index 9779858b8b6a44..0e175b61b6e07d 100644 --- a/clang/test/CodeGen/ptrauth-qualifier-atomic.c +++ b/clang/test/CodeGen/ptrauth-qualifier-atomic.c @@ -122,9 +122,8 @@ _Bool cmpxchg_weak_c11(UniformPtr *expected, int *desired) { } // CHECK-LABEL: define {{.*}}ptr @load_n() -// CHECK: [[SIGNED:%.*]] = load atomic ptr, ptr @qualified_ptr -// CHECK: [[INT:%.*]] = ptrtoint ptr [[SIGNED]] to i64 -// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) +// CHECK: load atomic ptr, ptr @qualified_ptr +// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) int *load_n(void) { return __atomic_load_n(&qualified_ptr, __ATOMIC_SEQ_CST); } // CHECK-LABEL: define {{.*}}void @store_n( @@ -136,9 +135,8 @@ void store_n(int *p) { __atomic_store_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); } // CHECK-LABEL: define {{.*}}ptr @exchange_n( // CHECK: call i64 @llvm.ptrauth.sign(i64 {{.*}}, i32 2, i64 50) // CHECK-NOT: @llvm.ptrauth -// CHECK: [[OLD:%.*]] = atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} -// CHECK: [[INT:%.*]] = ptrtoint ptr [[OLD]] to i64 -// CHECK-NEXT: call i64 @llvm.ptrauth.auth(i64 [[INT]], i32 2, i64 50) +// CHECK: atomicrmw xchg ptr @qualified_ptr, ptr {{.*}} +// CHECK: call i64 @llvm.ptrauth.auth(i64 {{.*}}, i32 2, i64 50) int *exchange_n(int *p) { return __atomic_exchange_n(&qualified_ptr, p, __ATOMIC_SEQ_CST); } diff --git a/clang/test/Sema/ptrauth-atomic-ops.c b/clang/test/Sema/ptrauth-atomic-ops.c index d4cd9664ae7f51..fb3bc8e268716a 100644 --- a/clang/test/Sema/ptrauth-atomic-ops.c +++ b/clang/test/Sema/ptrauth-atomic-ops.c @@ -59,14 +59,8 @@ void f() { // expected-error@-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@-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), 1, memory_order_seq_cst); - // expected-error@-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)}} __atomic_fetch_add(&non_addr_discriminatedauthenticated_ptr, 1, memory_order_seq_cst); // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} - __c11_atomic_fetch_add(&atomic_qualified_int, 1, memory_order_seq_cst); - // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) _Atomic(long) *' invalid)}} - __atomic_fetch_add(&qualified_int, 1, memory_order_seq_cst); - // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} __atomic_fetch_or(&atomic_qualified_int, 1, memory_order_seq_cst); // expected-error@-1 {{address argument to atomic arithmetic operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) _Atomic(long) *' invalid)}} @@ -74,10 +68,6 @@ void f() { // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} __atomic_clear(&non_addr_discriminatedauthenticated_ptr, memory_order_seq_cst); // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('int *__ptrauth(2,0,200) *' invalid)}} - __atomic_test_and_set(ATOMIZE(authenticated_ptr), memory_order_seq_cst); - // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('volatile __ptrauth(2,1,100) _Atomic(int *) *' invalid)}} - __atomic_clear(&qualified_int, memory_order_seq_cst); - // expected-error@-1 {{address argument to atomic test-and-set or clear operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} (*ATOMIZE(non_addr_discriminatedauthenticated_ptr))++; // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} @@ -85,16 +75,8 @@ void f() { // expected-error@-1 {{atomic decrement of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} (*ATOMIZE(non_addr_discriminatedauthenticated_ptr)) += 1; // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type 'volatile __ptrauth(2,0,200) _Atomic(int *)' is not supported}} - (*ATOMIZE(authenticated_ptr))--; - // expected-error@-1 {{atomic decrement of '__ptrauth'-qualified type 'volatile __ptrauth(2,1,100) _Atomic(int *)' is not supported}} atomic_qualified_int++; // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} - ++atomic_qualified_int; - // expected-error@-1 {{atomic increment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} - atomic_qualified_int -= 2; - // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} - atomic_qualified_int |= 2; - // expected-error@-1 {{atomic compound assignment of '__ptrauth'-qualified type '__ptrauth(2,0,200) _Atomic(long)' is not supported}} qualified_int++; qualified_int += 2; atomic_qualified_int = 3; @@ -170,9 +152,6 @@ int i = 0; // expected-error@-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@-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_add(&qualified_int, 1); - // expected-error@-1 {{address argument to __sync operation must not be a pointer to a '__ptrauth'-qualified type ('__ptrauth(2,0,200) long *' invalid)}} } typedef int *__ptrauth(2, 0, 200) UniformPtr; @@ -192,14 +171,8 @@ void by_address(int *p, int **raw, UniformPtr *same) { __c11_atomic_compare_exchange_strong(&atomic_uniform, raw, p, memory_order_seq_cst, memory_order_seq_cst); // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} - __atomic_compare_exchange_n(&uniform, raw, p, 0, memory_order_seq_cst, memory_order_seq_cst); - // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} - __atomic_compare_exchange(&uniform, raw, same, 0, memory_order_seq_cst, memory_order_seq_cst); - // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} __atomic_load(&uniform, raw, memory_order_seq_cst); // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} - __atomic_exchange(&uniform, raw, same, memory_order_seq_cst); - // expected-error@-1 {{passing 'int **' to parameter of type 'int *__ptrauth(2,0,200) *' changes pointer authentication of pointee type}} __c11_atomic_compare_exchange_strong(&atomic_plain, same, p, memory_order_seq_cst, memory_order_seq_cst); // expected-error@-1 {{passing 'UniformPtr *' (aka 'int *__ptrauth(2,0,200) *') to parameter of type 'int **' changes pointer authentication of pointee type}} } >From 3a50604fb3e5a974ee235f42a16d3dcce7053192 Mon Sep 17 00:00:00 2001 From: JF Bastien <[email protected]> Date: Fri, 13 Nov 2026 08:55:54 +0900 Subject: [PATCH 9/9] Release notes for the new errors and for code built by older Clang Assisted-by: AI --- clang/docs/ReleaseNotes.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 80eb391f545256..1834ff4f728996 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -48,6 +48,12 @@ 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. +- Atomic arithmetic, `__sync_*`, `__atomic_test_and_set` and `__atomic_clear` + on a `__ptrauth`-qualified object, and `++`, `--` and compound assignment on + an `_Atomic` one, are now errors. An operand passed by address, such as the + `expected` of a compare-exchange, must now point to the object's + `__ptrauth`-qualified type. + ### C++ Specific Potentially Breaking Changes - The `[[carries_dependency]]` attribute is no longer recognized, in any language @@ -168,6 +174,10 @@ features cannot lower the translation-unit ABI level; This affects the ABI of `std::strong_ordering` in some versions of the Microsoft STL. (#GH237188) +- Code built with an older Clang reads and writes the raw bits of a + `__ptrauth`-qualified object through the atomic builtins, so it disagrees + with this release about the same object. + ### AST Dumping Potentially Breaking Changes - The AST printer (`-ast-print`) now prints a floating-point literal as the _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
