[llvm] [clang] [AArch64][PAC] Support ptrauth builtins and -fptrauth-intrinsics. (PR #65996)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 08:40:56 PST 2024


github-actions[bot] wrote:

<!--LLVM CODE FORMAT COMMENT: {clang-format}-->


:warning: C/C++ code formatter, clang-format found issues in your code. :warning:

<details>
<summary>
You can test this locally with the following command:
</summary>

``````````bash
git-clang-format --diff f9aba820eb6478a617145eb690ba8b06490d7ac5 c4e7c86eb967b4313e19587e33fdc6b50c8cd6a7 -- clang/lib/Headers/ptrauth.h clang/test/CodeGen/ptrauth-intrinsics.c clang/test/Preprocessor/ptrauth_feature.c clang/test/Sema/ptrauth-intrinsics-macro.c clang/test/Sema/ptrauth.c clang/include/clang/Basic/TargetInfo.h clang/include/clang/Sema/Sema.h clang/lib/Basic/TargetInfo.cpp clang/lib/Basic/Targets/AArch64.cpp clang/lib/Basic/Targets/AArch64.h clang/lib/CodeGen/CGBuiltin.cpp clang/lib/Driver/ToolChains/Clang.cpp clang/lib/Frontend/CompilerInvocation.cpp clang/lib/Sema/SemaChecking.cpp
``````````

</details>

<details>
<summary>
View the diff from clang-format here.
</summary>

``````````diff
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 2e1a687c39..5e7c5bf247 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4969,7 +4969,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
   case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
   case Builtin::BI__builtin_ptrauth_strip: {
     // Emit the arguments.
-    SmallVector<llvm::Value*, 5> args;
+    SmallVector<llvm::Value *, 5> args;
     for (auto argExpr : E->arguments())
       args.push_back(EmitScalarExpr(argExpr));
 
@@ -4979,25 +4979,25 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
       args[0] = Builder.CreatePtrToInt(args[0], IntPtrTy);
 
     switch (BuiltinID) {
-      case Builtin::BI__builtin_ptrauth_auth_and_resign:
-        if (args[4]->getType()->isPointerTy())
-          args[4] = Builder.CreatePtrToInt(args[4], IntPtrTy);
-        LLVM_FALLTHROUGH;
-
-      case Builtin::BI__builtin_ptrauth_auth:
-      case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
-        if (args[2]->getType()->isPointerTy())
-          args[2] = Builder.CreatePtrToInt(args[2], IntPtrTy);
-        break;
+    case Builtin::BI__builtin_ptrauth_auth_and_resign:
+      if (args[4]->getType()->isPointerTy())
+        args[4] = Builder.CreatePtrToInt(args[4], IntPtrTy);
+      LLVM_FALLTHROUGH;
+
+    case Builtin::BI__builtin_ptrauth_auth:
+    case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
+      if (args[2]->getType()->isPointerTy())
+        args[2] = Builder.CreatePtrToInt(args[2], IntPtrTy);
+      break;
 
-      case Builtin::BI__builtin_ptrauth_sign_generic_data:
-        if (args[1]->getType()->isPointerTy())
-          args[1] = Builder.CreatePtrToInt(args[1], IntPtrTy);
-        break;
+    case Builtin::BI__builtin_ptrauth_sign_generic_data:
+      if (args[1]->getType()->isPointerTy())
+        args[1] = Builder.CreatePtrToInt(args[1], IntPtrTy);
+      break;
 
-      case Builtin::BI__builtin_ptrauth_blend_discriminator:
-      case Builtin::BI__builtin_ptrauth_strip:
-        break;
+    case Builtin::BI__builtin_ptrauth_blend_discriminator:
+    case Builtin::BI__builtin_ptrauth_strip:
+      break;
     }
 
     // Call the intrinsic.
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 402f117976..8cd02a6430 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -7077,7 +7077,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
                    options::OPT_fno_ptrauth_intrinsics, false))
     CmdArgs.push_back("-fptrauth-intrinsics");
 
-
   // -fsigned-bitfields is default, and clang doesn't yet support
   // -funsigned-bitfields.
   if (!Args.hasFlag(options::OPT_fsigned_bitfields,
diff --git a/clang/lib/Headers/ptrauth.h b/clang/lib/Headers/ptrauth.h
index 5061c8298f..de656abddf 100644
--- a/clang/lib/Headers/ptrauth.h
+++ b/clang/lib/Headers/ptrauth.h
@@ -67,8 +67,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
    The value must be an expression of pointer type.
    The key must be a constant expression of type ptrauth_key.
    The result will have the same type as the original value. */
-#define ptrauth_strip(__value, __key) \
-  __builtin_ptrauth_strip(__value, __key)
+#define ptrauth_strip(__value, __key) __builtin_ptrauth_strip(__value, __key)
 
 /* Blend a constant discriminator into the given pointer-like value
    to form a new discriminator.  Not all bits of the inputs are
@@ -80,7 +79,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
    The first argument must be an expression of pointer type.
    The second argument must be an expression of integer type.
    The result will have type uintptr_t. */
-#define ptrauth_blend_discriminator(__pointer, __integer) \
+#define ptrauth_blend_discriminator(__pointer, __integer)                      \
   __builtin_ptrauth_blend_discriminator(__pointer, __integer)
 
 /* Add a signature to the given pointer value using a specific key,
@@ -95,7 +94,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
    The extra data must be an expression of pointer or integer type;
    if an integer, it will be coerced to ptrauth_extra_data_t.
    The result will have the same type as the original value. */
-#define ptrauth_sign_unauthenticated(__value, __key, __data) \
+#define ptrauth_sign_unauthenticated(__value, __key, __data)                   \
   __builtin_ptrauth_sign_unauthenticated(__value, __key, __data)
 
 /* Authenticate a pointer using one scheme and resign it using another.
@@ -115,8 +114,10 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
 
    Do not pass a null pointer to this function. A null pointer
    will not successfully authenticate. */
-#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data) \
-  __builtin_ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data)
+#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key,     \
+                                __new_data)                                    \
+  __builtin_ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, \
+                                    __new_data)
 
 /* Authenticate a data pointer.
 
@@ -128,7 +129,7 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
 
    If the authentication fails, dereferencing the resulting pointer
    will fail. */
-#define ptrauth_auth_data(__value, __old_key, __old_data) \
+#define ptrauth_auth_data(__value, __old_key, __old_data)                      \
   __builtin_ptrauth_auth(__value, __old_key, __old_data)
 
 /* Compute a signature for the given pair of pointer-sized values.
@@ -150,17 +151,49 @@ typedef __UINTPTR_TYPE__ ptrauth_generic_signature_t;
 
    The arguments must be either pointers or integers; if integers, they
    will be coerce to uintptr_t. */
-#define ptrauth_sign_generic_data(__value, __data) \
+#define ptrauth_sign_generic_data(__value, __data)                             \
   __builtin_ptrauth_sign_generic_data(__value, __data)
 
 #else
 
-#define ptrauth_strip(__value, __key) ({(void)__key; __value;})
-#define ptrauth_blend_discriminator(__pointer, __integer) ({(void)__pointer; (void)__integer; ((ptrauth_extra_data_t)0);})
-#define ptrauth_sign_unauthenticated(__value, __key, __data) ({(void)__key; (void)__data; __value;})
-#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key, __new_data) ({(void)__old_key; (void)__old_data; (void)__new_key; (void)__new_data; __value;})
-#define ptrauth_auth_data(__value, __old_key, __old_data) ({(void)__old_key;(void)__old_data;__value;})
-#define ptrauth_sign_generic_data(__value, __data) ({(void)__value;(void)__data;((ptrauth_generic_signature_t)0);})
+#define ptrauth_strip(__value, __key)                                          \
+  ({                                                                           \
+    (void)__key;                                                               \
+    __value;                                                                   \
+  })
+#define ptrauth_blend_discriminator(__pointer, __integer)                      \
+  ({                                                                           \
+    (void)__pointer;                                                           \
+    (void)__integer;                                                           \
+    ((ptrauth_extra_data_t)0);                                                 \
+  })
+#define ptrauth_sign_unauthenticated(__value, __key, __data)                   \
+  ({                                                                           \
+    (void)__key;                                                               \
+    (void)__data;                                                              \
+    __value;                                                                   \
+  })
+#define ptrauth_auth_and_resign(__value, __old_key, __old_data, __new_key,     \
+                                __new_data)                                    \
+  ({                                                                           \
+    (void)__old_key;                                                           \
+    (void)__old_data;                                                          \
+    (void)__new_key;                                                           \
+    (void)__new_data;                                                          \
+    __value;                                                                   \
+  })
+#define ptrauth_auth_data(__value, __old_key, __old_data)                      \
+  ({                                                                           \
+    (void)__old_key;                                                           \
+    (void)__old_data;                                                          \
+    __value;                                                                   \
+  })
+#define ptrauth_sign_generic_data(__value, __data)                             \
+  ({                                                                           \
+    (void)__value;                                                             \
+    (void)__data;                                                              \
+    ((ptrauth_generic_signature_t)0);                                          \
+  })
 
 #endif /* __has_feature(ptrauth_intrinsics) */
 
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index a8403f2df9..f49da19d0f 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -1972,10 +1972,15 @@ static bool SemaOpenCLBuiltinToAddr(Sema &S, unsigned BuiltinID,
 }
 
 namespace {
-  enum PointerAuthOpKind {
-    PAO_Strip, PAO_Sign, PAO_Auth, PAO_SignGeneric, PAO_Discriminator,
-    PAO_BlendPointer, PAO_BlendInteger
-  };
+enum PointerAuthOpKind {
+  PAO_Strip,
+  PAO_Sign,
+  PAO_Auth,
+  PAO_SignGeneric,
+  PAO_Discriminator,
+  PAO_BlendPointer,
+  PAO_BlendInteger
+};
 }
 
 static bool checkPointerAuthEnabled(Sema &S, Expr *E) {
@@ -2003,20 +2008,21 @@ bool Sema::checkConstantPointerAuthKey(Expr *Arg, unsigned &Result) {
   // Attempt to constant-evaluate the expression.
   std::optional<llvm::APSInt> KeyValue = Arg->getIntegerConstantExpr(Context);
   if (!KeyValue) {
-    Diag(Arg->getExprLoc(), diag::err_expr_not_ice) << 0
-      << Arg->getSourceRange();
+    Diag(Arg->getExprLoc(), diag::err_expr_not_ice)
+        << 0 << Arg->getSourceRange();
     return true;
   }
 
   // Ask the target to validate the key parameter.
   if (!Context.getTargetInfo().validatePointerAuthKey(*KeyValue)) {
-    llvm::SmallString<32> Value; {
+    llvm::SmallString<32> Value;
+    {
       llvm::raw_svector_ostream Str(Value);
       Str << *KeyValue;
     }
 
     Diag(Arg->getExprLoc(), diag::err_ptrauth_invalid_key)
-      << Value << Arg->getSourceRange();
+        << Value << Arg->getSourceRange();
     return true;
   }
 
@@ -2028,7 +2034,8 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
                                   PointerAuthOpKind OpKind) {
   if (Arg->hasPlaceholderType()) {
     ExprResult R = S.CheckPlaceholderExpr(Arg);
-    if (R.isInvalid()) return true;
+    if (R.isInvalid())
+      return true;
     Arg = R.get();
   }
 
@@ -2036,8 +2043,7 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
     return OpKind != PAO_BlendInteger;
   };
   auto allowsInteger = [](PointerAuthOpKind OpKind) {
-    return OpKind == PAO_Discriminator ||
-           OpKind == PAO_BlendInteger ||
+    return OpKind == PAO_Discriminator || OpKind == PAO_BlendInteger ||
            OpKind == PAO_SignGeneric;
   };
 
@@ -2051,16 +2057,15 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
              Arg->getType()->isIntegralOrUnscopedEnumerationType()) {
     ExpectedTy = S.Context.getUIntPtrType();
 
-  // Diagnose the failures.
+    // Diagnose the failures.
   } else {
     S.Diag(Arg->getExprLoc(), diag::err_ptrauth_value_bad_type)
-      << unsigned(OpKind == PAO_Discriminator ? 1 :
-                  OpKind == PAO_BlendPointer ? 2 :
-                  OpKind == PAO_BlendInteger ? 3 : 0)
-      << unsigned(allowsInteger(OpKind) ?
-                    (allowsPointer(OpKind) ? 2 : 1) : 0)
-      << Arg->getType()
-      << Arg->getSourceRange();
+        << unsigned(OpKind == PAO_Discriminator  ? 1
+                    : OpKind == PAO_BlendPointer ? 2
+                    : OpKind == PAO_BlendInteger ? 3
+                                                 : 0)
+        << unsigned(allowsInteger(OpKind) ? (allowsPointer(OpKind) ? 2 : 1) : 0)
+        << Arg->getType() << Arg->getSourceRange();
     return true;
   }
 
@@ -2072,18 +2077,20 @@ static bool checkPointerAuthValue(Sema &S, Expr *&Arg,
   // Warn about null pointers for non-generic sign and auth operations.
   if ((OpKind == PAO_Sign || OpKind == PAO_Auth) &&
       Arg->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) {
-    S.Diag(Arg->getExprLoc(),
-           OpKind == PAO_Sign ? diag::warn_ptrauth_sign_null_pointer
-                              : diag::warn_ptrauth_auth_null_pointer)
-      << Arg->getSourceRange();
+    S.Diag(Arg->getExprLoc(), OpKind == PAO_Sign
+                                  ? diag::warn_ptrauth_sign_null_pointer
+                                  : diag::warn_ptrauth_auth_null_pointer)
+        << Arg->getSourceRange();
   }
 
   return false;
 }
 
 static ExprResult SemaPointerAuthStrip(Sema &S, CallExpr *Call) {
-  if (checkArgCount(S, Call, 2)) return ExprError();
-  if (checkPointerAuthEnabled(S, Call)) return ExprError();
+  if (checkArgCount(S, Call, 2))
+    return ExprError();
+  if (checkPointerAuthEnabled(S, Call))
+    return ExprError();
   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Strip) ||
       checkPointerAuthKey(S, Call->getArgs()[1]))
     return ExprError();
@@ -2093,8 +2100,10 @@ static ExprResult SemaPointerAuthStrip(Sema &S, CallExpr *Call) {
 }
 
 static ExprResult SemaPointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) {
-  if (checkArgCount(S, Call, 2)) return ExprError();
-  if (checkPointerAuthEnabled(S, Call)) return ExprError();
+  if (checkArgCount(S, Call, 2))
+    return ExprError();
+  if (checkPointerAuthEnabled(S, Call))
+    return ExprError();
   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_BlendPointer) ||
       checkPointerAuthValue(S, Call->getArgs()[1], PAO_BlendInteger))
     return ExprError();
@@ -2104,8 +2113,10 @@ static ExprResult SemaPointerAuthBlendDiscriminator(Sema &S, CallExpr *Call) {
 }
 
 static ExprResult SemaPointerAuthSignGenericData(Sema &S, CallExpr *Call) {
-  if (checkArgCount(S, Call, 2)) return ExprError();
-  if (checkPointerAuthEnabled(S, Call)) return ExprError();
+  if (checkArgCount(S, Call, 2))
+    return ExprError();
+  if (checkPointerAuthEnabled(S, Call))
+    return ExprError();
   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_SignGeneric) ||
       checkPointerAuthValue(S, Call->getArgs()[1], PAO_Discriminator))
     return ExprError();
@@ -2116,8 +2127,10 @@ static ExprResult SemaPointerAuthSignGenericData(Sema &S, CallExpr *Call) {
 
 static ExprResult SemaPointerAuthSignOrAuth(Sema &S, CallExpr *Call,
                                             PointerAuthOpKind OpKind) {
-  if (checkArgCount(S, Call, 3)) return ExprError();
-  if (checkPointerAuthEnabled(S, Call)) return ExprError();
+  if (checkArgCount(S, Call, 3))
+    return ExprError();
+  if (checkPointerAuthEnabled(S, Call))
+    return ExprError();
   if (checkPointerAuthValue(S, Call->getArgs()[0], OpKind) ||
       checkPointerAuthKey(S, Call->getArgs()[1]) ||
       checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator))
@@ -2128,8 +2141,10 @@ static ExprResult SemaPointerAuthSignOrAuth(Sema &S, CallExpr *Call,
 }
 
 static ExprResult SemaPointerAuthAuthAndResign(Sema &S, CallExpr *Call) {
-  if (checkArgCount(S, Call, 5)) return ExprError();
-  if (checkPointerAuthEnabled(S, Call)) return ExprError();
+  if (checkArgCount(S, Call, 5))
+    return ExprError();
+  if (checkPointerAuthEnabled(S, Call))
+    return ExprError();
   if (checkPointerAuthValue(S, Call->getArgs()[0], PAO_Auth) ||
       checkPointerAuthKey(S, Call->getArgs()[1]) ||
       checkPointerAuthValue(S, Call->getArgs()[2], PAO_Discriminator) ||

``````````

</details>


https://github.com/llvm/llvm-project/pull/65996


More information about the cfe-commits mailing list