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

Benjamin Maxwell via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 07:42:41 PDT 2024


https://github.com/MacDue created https://github.com/llvm/llvm-project/pull/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.

>From a2f1bb60ecd31e8a52e29de60d7615abbe22160f Mon Sep 17 00:00:00 2001
From: Benjamin Maxwell <benjamin.maxwell at arm.com>
Date: Fri, 13 Sep 2024 14:06:37 +0000
Subject: [PATCH] [clang][codegen] Fix possible crash when setting TBAA
 metadata on FP math libcalls

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.
---
 clang/lib/CodeGen/CGBuiltin.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index 27abeba92999b3..d4c7eea3d20b24 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