Skip to content

Commit 2245703

Browse files
committed
Add llvm libunwind callback to suppress exceptions on apple silicon
1 parent 244d1ab commit 2245703

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/Interpreter/Compatibility.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
#include "llvm/ADT/SmallString.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/ADT/Twine.h"
16+
#include "llvm/BinaryFormat/MachO.h"
1617
#include "llvm/Config/llvm-config.h"
1718
#include "llvm/ExecutionEngine/JITSymbol.h"
1819
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
1920
#include "llvm/Support/Casting.h"
2021
#include "llvm/Support/Path.h"
22+
#include "llvm/Object/MachO.h"
2123

2224
#ifdef USE_CLING
2325

@@ -36,6 +38,43 @@ namespace compat {
3638

3739
using Interpreter = cling::Interpreter;
3840

41+
#ifdef __APPLE__
42+
// Define a minimal mach header for JIT'd code.
43+
static MachO::mach_header_64 fake_mach_header = {
44+
.magic = MachO::MH_MAGIC_64,
45+
.cputype = MachO::CPU_TYPE_ARM64,
46+
.cpusubtype = MachO::CPU_SUBTYPE_ARM64_ALL,
47+
.filetype = MachO::MH_DYLIB,
48+
.ncmds = 0,
49+
.sizeofcmds = 0,
50+
.flags = 0,
51+
.reserved = 0};
52+
53+
// Declare libunwind SPI types and functions.
54+
struct unw_dynamic_unwind_sections {
55+
uintptr_t dso_base;
56+
uintptr_t dwarf_section;
57+
size_t dwarf_section_length;
58+
uintptr_t compact_unwind_section;
59+
size_t compact_unwind_section_length;
60+
};
61+
62+
int find_dynamic_unwind_sections(uintptr_t addr,
63+
unw_dynamic_unwind_sections * info) {
64+
info->dso_base = (uintptr_t)&fake_mach_header;
65+
info->dwarf_section = 0;
66+
info->dwarf_section_length = 0;
67+
info->compact_unwind_section = 0;
68+
info->compact_unwind_section_length = 0;
69+
return 1;
70+
}
71+
72+
// Typedef for callback above.
73+
typedef int (*unw_find_dynamic_unwind_sections)(
74+
uintptr_t addr, struct unw_dynamic_unwind_sections * info);
75+
76+
#endif
77+
3978
inline void maybeMangleDeclName(const clang::GlobalDecl& GD,
4079
std::string& mangledName) {
4180
cling::utils::Analyze::maybeMangleDeclName(GD, mangledName);
@@ -151,6 +190,15 @@ createClangInterpreter(std::vector<const char*>& args) {
151190
return std::move(*innerOrErr);
152191
}
153192

193+
#ifdef __APPLE__
194+
inline void removeFindDynamicUnwindSections() {
195+
if (auto* unw_remove_find_dynamic_unwind_sections = (int (*)(
196+
unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
197+
dlsym(RTLD_DEFAULT, "__unw_remove_find_dynamic_unwind_sections"))
198+
unw_remove_find_dynamic_unwind_sections(find_dynamic_unwind_sections);
199+
}
200+
#endif
201+
154202
inline void maybeMangleDeclName(const clang::GlobalDecl& GD,
155203
std::string& mangledName) {
156204
// copied and adapted from CodeGen::CodeGenModule::getMangledName

lib/Interpreter/CppInterOp.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ namespace Cpp {
6262
// This might fix the issue https://reviews.llvm.org/D107087
6363
// FIXME: For now we just leak the Interpreter.
6464
struct InterpDeleter {
65-
~InterpDeleter() { sInterpreter.release(); }
65+
~InterpDeleter() {
66+
#ifdef __APPLE__
67+
compat::removeFindDynamicUnwindSections();
68+
#endif
69+
sInterpreter.release();
70+
}
6671
} Deleter;
6772

6873
static compat::Interpreter& getInterp() {

0 commit comments

Comments
 (0)