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

John McCall via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 21 09:20:19 PDT 2023


================
@@ -4869,6 +4869,73 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
   case Builtin::BI__iso_volatile_store64:
     return RValue::get(EmitISOVolatileStore(*this, E));
 
+  case Builtin::BI__builtin_ptrauth_auth:
+  case Builtin::BI__builtin_ptrauth_auth_and_resign:
+  case Builtin::BI__builtin_ptrauth_blend_discriminator:
+  case Builtin::BI__builtin_ptrauth_sign_generic_data:
+  case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
+  case Builtin::BI__builtin_ptrauth_strip: {
+    // Emit the arguments.
+    SmallVector<llvm::Value*, 5> args;
+    for (auto argExpr : E->arguments())
+      args.push_back(EmitScalarExpr(argExpr));
+
+    // Cast the value to intptr_t, saving its original type.
+    llvm::Type *origValueType = args[0]->getType();
+    if (origValueType->isPointerTy())
+      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_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;
+    }
+
+    // Call the intrinsic.
+    auto intrinsicID = [&]() -> unsigned {
+      switch (BuiltinID) {
+      case Builtin::BI__builtin_ptrauth_auth:
+        return llvm::Intrinsic::ptrauth_auth;
+      case Builtin::BI__builtin_ptrauth_auth_and_resign:
+        return llvm::Intrinsic::ptrauth_resign;
+      case Builtin::BI__builtin_ptrauth_blend_discriminator:
+        return llvm::Intrinsic::ptrauth_blend;
+      case Builtin::BI__builtin_ptrauth_sign_generic_data:
+        return llvm::Intrinsic::ptrauth_sign_generic;
+      case Builtin::BI__builtin_ptrauth_sign_unauthenticated:
+        return llvm::Intrinsic::ptrauth_sign;
+      case Builtin::BI__builtin_ptrauth_strip:
+        return llvm::Intrinsic::ptrauth_strip;
+      }
+      llvm_unreachable("bad ptrauth intrinsic");
+    }();
+    auto intrinsic = CGM.getIntrinsic(intrinsicID);
+    llvm::Value *result = EmitRuntimeCall(intrinsic, args);
+
+    if (BuiltinID != Builtin::BI__builtin_ptrauth_sign_generic_data &&
+        BuiltinID != Builtin::BI__builtin_ptrauth_blend_discriminator &&
+        origValueType->isPointerTy()) {
+      result = Builder.CreateIntToPtr(result, origValueType);
+    }
+    return RValue::get(result);
+  }
+
----------------
rjmccall wrote:

The codebase is generally inconsistent about this, but we've been moving towards lowercasing leading letters of methods in new code.

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


More information about the cfe-commits mailing list