[clang] a56ca1a - [clang][codegen] Fix possible crash when setting TBAA metadata on FP math libcalls (#108575)

via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 15 05:41:30 PDT 2024


Author: Benjamin Maxwell
Date: 2024-09-15T13:41:26+01:00
New Revision: a56ca1a0fb248c6f38b5841323a74673748f43ea

URL: https://github.com/llvm/llvm-project/commit/a56ca1a0fb248c6f38b5841323a74673748f43ea
DIFF: https://github.com/llvm/llvm-project/commit/a56ca1a0fb248c6f38b5841323a74673748f43ea.diff

LOG: [clang][codegen] Fix possible crash when setting TBAA metadata on FP math libcalls (#108575)

There's currently no code path that can reach this crash, but:

```
Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
```

fails if the call returns `void`. This could happen if a builtin for
something like `void sincos(double, double*, double*)` is added to
clang.

Instead, use the `llvm::CallBase` returned from `EmitCall()` to set the
TBAA metadata, which should exist no matter the return type.

Added: 
    

Modified: 
    clang/lib/CodeGen/CGBuiltin.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index a52e880a764252..a76cd5f9a6f47d 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -690,8 +690,10 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
                               const CallExpr *E, llvm::Constant *calleeValue) {
   CodeGenFunction::CGFPOptionsRAII FPOptsRAII(CGF, E);
   CGCallee callee = CGCallee::forDirect(calleeValue, GlobalDecl(FD));
+  llvm::CallBase *callOrInvoke = nullptr;
   RValue Call =
-      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot());
+      CGF.EmitCall(E->getCallee()->getType(), callee, E, ReturnValueSlot(),
+                   /*Chain=*/nullptr, &callOrInvoke);
 
   if (unsigned BuiltinID = FD->getBuiltinID()) {
     // Check whether a FP math builtin function, such as BI__builtin_expf
@@ -705,8 +707,7 @@ static RValue emitLibraryCall(CodeGenFunction &CGF, const FunctionDecl *FD,
       // Emit "int" TBAA metadata on FP math libcalls.
       clang::QualType IntTy = Context.IntTy;
       TBAAAccessInfo TBAAInfo = CGF.CGM.getTBAAAccessInfo(IntTy);
-      Instruction *Inst = cast<llvm::Instruction>(Call.getScalarVal());
-      CGF.CGM.DecorateInstructionWithTBAA(Inst, TBAAInfo);
+      CGF.CGM.DecorateInstructionWithTBAA(callOrInvoke, TBAAInfo);
     }
   }
   return Call;


        


More information about the cfe-commits mailing list