3636
3737#include < set>
3838#include < sstream>
39+ #include < stack>
3940#include < string>
4041
4142// Stream redirect.
5354#include < unistd.h>
5455#endif // WIN32
5556
56- #include < stack>
57+ #ifdef __APPLE__
58+ // Define a minimal mach header for JIT'd code, to support exceptions on osx 14
59+ // and later. See llvm/llvm-project#49036
60+ static llvm::MachO::mach_header_64 fake_mach_header = {
61+ .magic = llvm::MachO::MH_MAGIC_64 ,
62+ .cputype = llvm::MachO::CPU_TYPE_ARM64 ,
63+ .cpusubtype = llvm::MachO::CPU_SUBTYPE_ARM64_ALL ,
64+ .filetype = llvm::MachO::MH_DYLIB ,
65+ .ncmds = 0 ,
66+ .sizeofcmds = 0 ,
67+ .flags = 0 ,
68+ .reserved = 0 };
69+
70+ // Declare libunwind SPI types and functions.
71+ struct unw_dynamic_unwind_sections {
72+ uintptr_t dso_base;
73+ uintptr_t dwarf_section;
74+ size_t dwarf_section_length;
75+ uintptr_t compact_unwind_section;
76+ size_t compact_unwind_section_length;
77+ };
78+
79+ int find_dynamic_unwind_sections (uintptr_t addr,
80+ unw_dynamic_unwind_sections* info) {
81+ info->dso_base = (uintptr_t )&fake_mach_header;
82+ info->dwarf_section = 0 ;
83+ info->dwarf_section_length = 0 ;
84+ info->compact_unwind_section = 0 ;
85+ info->compact_unwind_section_length = 0 ;
86+ return 1 ;
87+ }
88+
89+ // Typedef for callback above.
90+ typedef int (*unw_find_dynamic_unwind_sections)(
91+ uintptr_t addr, struct unw_dynamic_unwind_sections * info);
92+
93+ #endif // __APPLE__
5794
5895namespace Cpp {
5996
@@ -70,7 +107,15 @@ namespace Cpp {
70107 // This might fix the issue https://reviews.llvm.org/D107087
71108 // FIXME: For now we just leak the Interpreter.
72109 struct InterpDeleter {
73- ~InterpDeleter () = default ;
110+ ~InterpDeleter () {
111+ #ifdef __APPLE__
112+ if (auto * unw_remove_find_dynamic_unwind_sections = (int (*)(
113+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
114+ dlsym (RTLD_DEFAULT , " __unw_remove_find_dynamic_unwind_sections" ))
115+ unw_remove_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
116+ #endif
117+ // sInterpreter.release();
118+ }
74119 } Deleter;
75120
76121 static compat::Interpreter& getInterp () {
@@ -2711,6 +2756,14 @@ namespace Cpp {
27112756 // FIXME: Enable this assert once we figure out how to fix the multiple
27122757 // calls to CreateInterpreter.
27132758 // assert(!sInterpreter && "Interpreter already set.");
2759+ #ifdef __APPLE__
2760+ // Add a handler to support exceptions from interpreted code.
2761+ // See llvm/llvm-project#49036
2762+ if (auto * unw_add_find_dynamic_unwind_sections = (int (*)(
2763+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
2764+ dlsym (RTLD_DEFAULT , " __unw_add_find_dynamic_unwind_sections" ))
2765+ unw_add_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
2766+ #endif // __APPLE__
27142767 sInterpreter = I;
27152768 return I;
27162769 }
0 commit comments