Skip to content

Commit c21c80e

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

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

lib/Interpreter/Compatibility.h

+46
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,43 @@ createClangInterpreter(std::vector<const char*>& args) {
103103
bool CudaEnabled = !gpu_args.empty();
104104
#endif
105105

106+
#ifdef __APPLE__
107+
// Define a minimal mach header for JIT'd code.
108+
static MachO::mach_header_64 fake_mach_header = {
109+
.magic = MachO::MH_MAGIC_64,
110+
.cputype = MachO::CPU_TYPE_ARM64,
111+
.cpusubtype = MachO::CPU_SUBTYPE_ARM64_ALL,
112+
.filetype = MachO::MH_DYLIB,
113+
.ncmds = 0,
114+
.sizeofcmds = 0,
115+
.flags = 0,
116+
.reserved = 0};
117+
118+
// Declare libunwind SPI types and functions.
119+
struct unw_dynamic_unwind_sections {
120+
uintptr_t dso_base;
121+
uintptr_t dwarf_section;
122+
size_t dwarf_section_length;
123+
uintptr_t compact_unwind_section;
124+
size_t compact_unwind_section_length;
125+
};
126+
127+
int find_dynamic_unwind_sections(uintptr_t addr,
128+
unw_dynamic_unwind_sections * info) {
129+
info->dso_base = (uintptr_t)&fake_mach_header;
130+
info->dwarf_section = 0;
131+
info->dwarf_section_length = 0;
132+
info->compact_unwind_section = 0;
133+
info->compact_unwind_section_length = 0;
134+
return 1;
135+
}
136+
137+
// Typedef for callback above.
138+
typedef int (*unw_find_dynamic_unwind_sections)(
139+
uintptr_t addr, struct unw_dynamic_unwind_sections * info);
140+
141+
#endif
142+
106143
clang::IncrementalCompilerBuilder CB;
107144
CB.SetCompilerArgs({args.begin(), it});
108145

@@ -151,6 +188,15 @@ createClangInterpreter(std::vector<const char*>& args) {
151188
return std::move(*innerOrErr);
152189
}
153190

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

lib/Interpreter/CppInterOp.cpp

+6-1
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+
sInterpreter.removeFindDynamicUnwindSections();
68+
#endif
69+
sInterpreter.release();
70+
}
6671
} Deleter;
6772

6873
static compat::Interpreter& getInterp() {

0 commit comments

Comments
 (0)