36
36
37
37
#include < set>
38
38
#include < sstream>
39
+ #include < stack>
39
40
#include < string>
40
41
41
42
// Stream redirect.
53
54
#include < unistd.h>
54
55
#endif // WIN32
55
56
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__
57
94
58
95
namespace Cpp {
59
96
@@ -70,7 +107,15 @@ namespace Cpp {
70
107
// This might fix the issue https://reviews.llvm.org/D107087
71
108
// FIXME: For now we just leak the Interpreter.
72
109
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
+ }
74
119
} Deleter;
75
120
76
121
static compat::Interpreter& getInterp () {
@@ -2711,6 +2756,14 @@ namespace Cpp {
2711
2756
// FIXME: Enable this assert once we figure out how to fix the multiple
2712
2757
// calls to CreateInterpreter.
2713
2758
// 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__
2714
2767
sInterpreter = I;
2715
2768
return I;
2716
2769
}
0 commit comments