54
54
#include < map>
55
55
#include < set>
56
56
#include < sstream>
57
+ #include < stack>
57
58
#include < string>
58
59
59
60
// Stream redirect.
71
72
#include < unistd.h>
72
73
#endif // WIN32
73
74
74
- #include < stack>
75
+ #ifdef __APPLE__
76
+ // Define a minimal mach header for JIT'd code, to support exceptions on osx 14
77
+ // and later. See llvm/llvm-project#49036
78
+ static llvm::MachO::mach_header_64 fake_mach_header = {
79
+ .magic = llvm::MachO::MH_MAGIC_64,
80
+ .cputype = llvm::MachO::CPU_TYPE_ARM64,
81
+ .cpusubtype = llvm::MachO::CPU_SUBTYPE_ARM64_ALL,
82
+ .filetype = llvm::MachO::MH_DYLIB,
83
+ .ncmds = 0 ,
84
+ .sizeofcmds = 0 ,
85
+ .flags = 0 ,
86
+ .reserved = 0 };
87
+
88
+ // Declare libunwind SPI types and functions.
89
+ struct unw_dynamic_unwind_sections {
90
+ uintptr_t dso_base;
91
+ uintptr_t dwarf_section;
92
+ size_t dwarf_section_length;
93
+ uintptr_t compact_unwind_section;
94
+ size_t compact_unwind_section_length;
95
+ };
96
+
97
+ int find_dynamic_unwind_sections (uintptr_t addr,
98
+ unw_dynamic_unwind_sections* info) {
99
+ info->dso_base = (uintptr_t )&fake_mach_header;
100
+ info->dwarf_section = 0 ;
101
+ info->dwarf_section_length = 0 ;
102
+ info->compact_unwind_section = 0 ;
103
+ info->compact_unwind_section_length = 0 ;
104
+ return 1 ;
105
+ }
106
+
107
+ // Typedef for callback above.
108
+ typedef int (*unw_find_dynamic_unwind_sections)(
109
+ uintptr_t addr, struct unw_dynamic_unwind_sections * info);
110
+
111
+ #endif // __APPLE__
75
112
76
113
namespace Cpp {
77
114
@@ -88,7 +125,15 @@ namespace Cpp {
88
125
// This might fix the issue https://reviews.llvm.org/D107087
89
126
// FIXME: For now we just leak the Interpreter.
90
127
struct InterpDeleter {
91
- ~InterpDeleter () = default ;
128
+ ~InterpDeleter () {
129
+ #ifdef __APPLE__
130
+ if (auto * unw_remove_find_dynamic_unwind_sections = (int (*)(
131
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
132
+ dlsym (RTLD_DEFAULT, " __unw_remove_find_dynamic_unwind_sections" ))
133
+ unw_remove_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
134
+ #endif
135
+ // sInterpreter.release();
136
+ }
92
137
} Deleter;
93
138
94
139
static compat::Interpreter& getInterp () {
@@ -2977,6 +3022,14 @@ namespace Cpp {
2977
3022
// FIXME: Enable this assert once we figure out how to fix the multiple
2978
3023
// calls to CreateInterpreter.
2979
3024
// assert(!sInterpreter && "Interpreter already set.");
3025
+ #ifdef __APPLE__
3026
+ // Add a handler to support exceptions from interpreted code.
3027
+ // See llvm/llvm-project#49036
3028
+ if (auto * unw_add_find_dynamic_unwind_sections = (int (*)(
3029
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
3030
+ dlsym (RTLD_DEFAULT, " __unw_add_find_dynamic_unwind_sections" ))
3031
+ unw_add_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
3032
+ #endif // __APPLE__
2980
3033
sInterpreter = I;
2981
3034
return I;
2982
3035
}
0 commit comments