[clang] [ObjC] Fix jmp_buf sizing for ObjC exceptions (PR #85481)

via cfe-commits cfe-commits at lists.llvm.org
Sun Mar 17 08:46:35 PDT 2024


https://github.com/AtariDreams updated https://github.com/llvm/llvm-project/pull/85481

>From 26f0da257911cfe8998f6daf84a61557acc9d0bd Mon Sep 17 00:00:00 2001
From: Rose <gfunni234 at gmail.com>
Date: Fri, 15 Mar 2024 18:45:48 -0400
Subject: [PATCH] [ObjC] Fix jmp_buf sizing for ObjC exceptions

The size of 18 only works for x86, but does not work for the other architectures. This works for Darwin and was retrieved via reading the setjmp.h header. It would be nice if we could do the equivalent of sizeof() that would obtain the size as though it were being run on the target, not the host, but this is the best I could do for now.
---
 clang/lib/CodeGen/CGObjCMac.cpp | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/clang/lib/CodeGen/CGObjCMac.cpp b/clang/lib/CodeGen/CGObjCMac.cpp
index e815e097e1fb48..0f43c19ca8fb07 100644
--- a/clang/lib/CodeGen/CGObjCMac.cpp
+++ b/clang/lib/CodeGen/CGObjCMac.cpp
@@ -587,9 +587,9 @@ class ObjCTypesHelper : public ObjCCommonTypesHelper {
   /// SetJmpFn - LLVM _setjmp function.
   llvm::FunctionCallee getSetJmpFn() {
     // This is specifically the prototype for x86.
-    llvm::Type *params[] = { CGM.Int32Ty->getPointerTo() };
+    llvm::Type *params[] = {IntTy->getPointerTo()};
     return CGM.CreateRuntimeFunction(
-        llvm::FunctionType::get(CGM.Int32Ty, params, false), "_setjmp",
+        llvm::FunctionType::get(IntTy, params, false), "_setjmp",
         llvm::AttributeList::get(CGM.getLLVMContext(),
                                  llvm::AttributeList::FunctionIndex,
                                  llvm::Attribute::NonLazyBind));
@@ -5946,16 +5946,37 @@ ObjCTypesHelper::ObjCTypesHelper(CodeGen::CodeGenModule &cgm)
   ModuleTy = llvm::StructType::create("struct._objc_module", LongTy, LongTy,
                                       Int8PtrTy, SymtabPtrTy);
 
-  // FIXME: This is the size of the setjmp buffer and should be target
-  // specific. 18 is what's used on 32-bit X86.
-  uint64_t SetJmpBufferSize = 18;
+  // FIXME: Not a perfect solution, but one that better fits the other
+  // architectures Values are derived from setjmp.h on Darwin
+  uint64_t SetJmpBufferSize = 0;
+  switch (CGM.getTarget().getTriple().getArch()) {
+  case llvm::Triple::arm:
+    if (CGM.getTarget().getTriple().getSubArch() !=
+        llvm::Triple::ARMSubArch_v7k) {
+      SetJmpBufferSize = (10 + 16 + 2);
+      break;
+    }
+    [[fallthrough]];
+  case llvm::Triple::aarch64:
+    SetJmpBufferSize = ((14 + 8 + 2) * 2);
+    break;
+  case llvm::Triple::x86_64:
+    SetJmpBufferSize = ((9 * 2) + 3 + 16);
+    break;
+  case llvm::Triple::x86:
+  default:
+    // 18 is what's used on 32-bit X86 and on all architectures on prior
+    // versions of clang.
+    SetJmpBufferSize = 18;
+    break;
+  }
 
   // Exceptions
   llvm::Type *StackPtrTy = llvm::ArrayType::get(CGM.Int8PtrTy, 4);
 
   ExceptionDataTy = llvm::StructType::create(
       "struct._objc_exception_data",
-      llvm::ArrayType::get(CGM.Int32Ty, SetJmpBufferSize), StackPtrTy);
+      llvm::ArrayType::get(CGM.IntTy, SetJmpBufferSize), StackPtrTy);
 }
 
 ObjCNonFragileABITypesHelper::ObjCNonFragileABITypesHelper(CodeGen::CodeGenModule &cgm)



More information about the cfe-commits mailing list