37
37
38
38
#include < set>
39
39
#include < sstream>
40
+ #include < stack>
40
41
#include < string>
41
42
42
43
// Stream redirect.
54
55
#include < unistd.h>
55
56
#endif // WIN32
56
57
57
- #include < stack>
58
+ #ifdef __APPLE__
59
+ // Define a minimal mach header for JIT'd code, to support exceptions on osx 14
60
+ // and later. See llvm/llvm-project#49036
61
+ static llvm::MachO::mach_header_64 fake_mach_header = {
62
+ .magic = llvm::MachO::MH_MAGIC_64,
63
+ .cputype = llvm::MachO::CPU_TYPE_ARM64,
64
+ .cpusubtype = llvm::MachO::CPU_SUBTYPE_ARM64_ALL,
65
+ .filetype = llvm::MachO::MH_DYLIB,
66
+ .ncmds = 0 ,
67
+ .sizeofcmds = 0 ,
68
+ .flags = 0 ,
69
+ .reserved = 0 };
70
+
71
+ // Declare libunwind SPI types and functions.
72
+ struct unw_dynamic_unwind_sections {
73
+ uintptr_t dso_base;
74
+ uintptr_t dwarf_section;
75
+ size_t dwarf_section_length;
76
+ uintptr_t compact_unwind_section;
77
+ size_t compact_unwind_section_length;
78
+ };
79
+
80
+ int find_dynamic_unwind_sections (uintptr_t addr,
81
+ unw_dynamic_unwind_sections* info) {
82
+ info->dso_base = (uintptr_t )&fake_mach_header;
83
+ info->dwarf_section = 0 ;
84
+ info->dwarf_section_length = 0 ;
85
+ info->compact_unwind_section = 0 ;
86
+ info->compact_unwind_section_length = 0 ;
87
+ return 1 ;
88
+ }
89
+
90
+ // Typedef for callback above.
91
+ typedef int (*unw_find_dynamic_unwind_sections)(
92
+ uintptr_t addr, struct unw_dynamic_unwind_sections * info);
93
+
94
+ #endif // __APPLE__
58
95
59
96
namespace Cpp {
60
97
@@ -71,7 +108,15 @@ namespace Cpp {
71
108
// This might fix the issue https://reviews.llvm.org/D107087
72
109
// FIXME: For now we just leak the Interpreter.
73
110
struct InterpDeleter {
74
- ~InterpDeleter () = default ;
111
+ ~InterpDeleter () {
112
+ #ifdef __APPLE__
113
+ if (auto * unw_remove_find_dynamic_unwind_sections = (int (*)(
114
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
115
+ dlsym (RTLD_DEFAULT, " __unw_remove_find_dynamic_unwind_sections" ))
116
+ unw_remove_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
117
+ #endif
118
+ // sInterpreter.release();
119
+ }
75
120
} Deleter;
76
121
77
122
static compat::Interpreter& getInterp () {
@@ -2716,6 +2761,14 @@ namespace Cpp {
2716
2761
// FIXME: Enable this assert once we figure out how to fix the multiple
2717
2762
// calls to CreateInterpreter.
2718
2763
// assert(!sInterpreter && "Interpreter already set.");
2764
+ #ifdef __APPLE__
2765
+ // Add a handler to support exceptions from interpreted code.
2766
+ // See llvm/llvm-project#49036
2767
+ if (auto * unw_add_find_dynamic_unwind_sections = (int (*)(
2768
+ unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
2769
+ dlsym (RTLD_DEFAULT, " __unw_add_find_dynamic_unwind_sections" ))
2770
+ unw_add_find_dynamic_unwind_sections (find_dynamic_unwind_sections);
2771
+ #endif // __APPLE__
2719
2772
sInterpreter = I;
2720
2773
return I;
2721
2774
}
0 commit comments