[Lldb-commits] [lldb] [lldb] Handle ConstantExpr constants in InjectPointerSigningFixupCode (PR #194476)

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 27 15:52:24 PDT 2026


================
@@ -126,37 +147,50 @@ Error InjectPointerSigningFixupCode(llvm::Module &M,
       continue;
     }
 
-    // Build a GEP to the location of the ConstantPtrAuth within the global.
+    // Build a GEP to the location of the ConstantPtrAuth (or the expression
+    // path to the ConstantPtrAuth) within the global.
     Value *Loc;
-    if (Fixup.Indices.empty()) {
+    if (Fixup.GEPPath.empty()) {
       Loc = GV;
     } else {
-      SmallVector<Value *> GEPIndices;
-      GEPIndices.push_back(ConstantInt::get(Int32Ty, 0));
-      for (unsigned Idx : Fixup.Indices)
-        GEPIndices.push_back(ConstantInt::get(Int32Ty, Idx));
-      Loc = B.CreateGEP(GV->getValueType(), GV, GEPIndices);
+      SmallVector<Value *> GEPValues;
+      GEPValues.push_back(ConstantInt::get(Int32Ty, 0));
+      for (unsigned Idx : Fixup.GEPPath)
+        GEPValues.push_back(ConstantInt::get(Int32Ty, Idx));
+      Loc = B.CreateGEP(GV->getValueType(), GV, GEPValues);
     }
 
     Type *PtrTy = CPA->getType();
 
-    // Load the raw (unsigned) pointer.
-    Value *RawPtr = B.CreateLoad(PtrTy, Loc);
-
     // Compute the discriminator, blending with the address if needed.
     Value *Disc = CPA->getDiscriminator();
     if (CPA->hasAddressDiscriminator())
       Disc = B.CreateCall(BlendIntrinsic,
                           {B.CreatePointerCast(Loc, IntPtrTy), Disc});
 
-    // Sign the pointer.
-    Value *SignedPtr =
-        B.CreateCall(SignIntrinsic, {B.CreatePointerCast(RawPtr, IntPtrTy),
-                                     CPA->getKey(), Disc});
-
-    // Store the signed pointer back.
-    B.CreateStore(B.CreateBitOrPointerCast(SignedPtr, PtrTy), Loc);
-
+    // If the CPA is wrapped in a ConstantExpr chain, we'll need to sign the
+    // CPA's pointer value directly and re-evaluate the expr chain. If there is
+    // no expression chain, load and sign the pointer directly.
+    if (Fixup.ExprPath.empty()) {
+      Value *RawPtr = B.CreateLoad(PtrTy, Loc);
+      Value *SignedPtr =
+          B.CreateCall(SignIntrinsic, {B.CreatePointerCast(RawPtr, IntPtrTy),
+                                       CPA->getKey(), Disc});
+      B.CreateStore(B.CreateBitOrPointerCast(SignedPtr, PtrTy), Loc);
+    } else {
+      Value *SignedPtr = B.CreateCall(
+          SignIntrinsic, {B.CreatePointerCast(CPA->getPointer(), IntPtrTy),
+                          CPA->getKey(), Disc});
+      Value *Result = B.CreateIntToPtr(SignedPtr, PtrTy);
+
+      for (auto &Step : llvm::reverse(Fixup.ExprPath)) {
+        Instruction *I = Step.CE->getAsInstruction();
+        I->setOperand(Step.OperandIdx, Result);
+        B.Insert(I);
+        Result = I;
+      }
+      B.CreateStore(Result, Loc);
+    }
----------------
JDevlieghere wrote:

Nit: move the comments inside the block to describe the situation. I would probably even inverse them to handle the chain scenario first (matching the original order of the comment):

```suggestion
    if (!Fixup.ExprPath.empty()) {
      // The CPA is wrapped in a ConstantExpr chain: sign the CPA's pointer 
      // value directly and re-evaluate the expr chain. 
      Value *SignedPtr = B.CreateCall(
          SignIntrinsic, {B.CreatePointerCast(CPA->getPointer(), IntPtrTy),
                          CPA->getKey(), Disc});
      Value *Result = B.CreateIntToPtr(SignedPtr, PtrTy);

      for (auto &Step : llvm::reverse(Fixup.ExprPath)) {
        Instruction *I = Step.CE->getAsInstruction();
        I->setOperand(Step.OperandIdx, Result);
        B.Insert(I);
        Result = I;
      }
      B.CreateStore(Result, Loc);
    } else {
      // There is no expression chain: load and sign the pointer directly.
      Value *RawPtr = B.CreateLoad(PtrTy, Loc);
      Value *SignedPtr =
          B.CreateCall(SignIntrinsic, {B.CreatePointerCast(RawPtr, IntPtrTy),
                                       CPA->getKey(), Disc});
      B.CreateStore(B.CreateBitOrPointerCast(SignedPtr, PtrTy), Loc);
    }
```

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


More information about the lldb-commits mailing list