Skip to content

Commit a7ae7ef

Browse files
aaronj0vgvassilev
authored andcommitted
Add llvm libunwind callback to suppress exceptions on apple silicon
See llvm/llvm-project#49036
1 parent 244d1ab commit a7ae7ef

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

lib/Interpreter/Compatibility.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
#include "llvm/ADT/SmallString.h"
1414
#include "llvm/ADT/StringRef.h"
1515
#include "llvm/ADT/Twine.h"
16+
#include "llvm/BinaryFormat/MachO.h"
1617
#include "llvm/Config/llvm-config.h"
1718
#include "llvm/ExecutionEngine/JITSymbol.h"
1819
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
20+
#include "llvm/Object/MachO.h"
1921
#include "llvm/Support/Casting.h"
2022
#include "llvm/Support/Path.h"
2123

lib/Interpreter/CppInterOp.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,51 @@
4949
#include <unistd.h>
5050
#endif // WIN32
5151

52+
#ifdef __APPLE__
53+
// Define a minimal mach header for JIT'd code, to support exceptions on osx 14
54+
// and later. See llvm/llvm-project#49036
55+
static llvm::MachO::mach_header_64 fake_mach_header = {
56+
.magic = llvm::MachO::MH_MAGIC_64,
57+
.cputype = llvm::MachO::CPU_TYPE_ARM64,
58+
.cpusubtype = llvm::MachO::CPU_SUBTYPE_ARM64_ALL,
59+
.filetype = llvm::MachO::MH_DYLIB,
60+
.ncmds = 0,
61+
.sizeofcmds = 0,
62+
.flags = 0,
63+
.reserved = 0};
64+
65+
// Declare libunwind SPI types and functions.
66+
struct unw_dynamic_unwind_sections {
67+
uintptr_t dso_base;
68+
uintptr_t dwarf_section;
69+
size_t dwarf_section_length;
70+
uintptr_t compact_unwind_section;
71+
size_t compact_unwind_section_length;
72+
};
73+
74+
int find_dynamic_unwind_sections(uintptr_t addr,
75+
unw_dynamic_unwind_sections* info) {
76+
info->dso_base = (uintptr_t)&fake_mach_header;
77+
info->dwarf_section = 0;
78+
info->dwarf_section_length = 0;
79+
info->compact_unwind_section = 0;
80+
info->compact_unwind_section_length = 0;
81+
return 1;
82+
}
83+
84+
// Typedef for callback above.
85+
typedef int (*unw_find_dynamic_unwind_sections)(
86+
uintptr_t addr, struct unw_dynamic_unwind_sections* info);
87+
88+
void removeFindDynamicUnwindSections() {
89+
if (auto* unw_remove_find_dynamic_unwind_sections = (int (*)(
90+
unw_find_dynamic_unwind_sections find_dynamic_unwind_sections))
91+
dlsym(RTLD_DEFAULT, "__unw_remove_find_dynamic_unwind_sections"))
92+
unw_remove_find_dynamic_unwind_sections(find_dynamic_unwind_sections);
93+
}
94+
95+
#endif // __APPLE__
96+
5297
namespace Cpp {
5398

5499
using namespace clang;
@@ -62,7 +107,12 @@ namespace Cpp {
62107
// This might fix the issue https://reviews.llvm.org/D107087
63108
// FIXME: For now we just leak the Interpreter.
64109
struct InterpDeleter {
65-
~InterpDeleter() { sInterpreter.release(); }
110+
~InterpDeleter() {
111+
#ifdef __APPLE__
112+
removeFindDynamicUnwindSections();
113+
#endif
114+
sInterpreter.release();
115+
}
66116
} Deleter;
67117

68118
static compat::Interpreter& getInterp() {

unittests/CppInterOp/InterpreterTest.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,15 @@ TEST(InterpreterTest, DetectSystemCompilerIncludePaths) {
103103
Cpp::DetectSystemCompilerIncludePaths(includes);
104104
EXPECT_FALSE(includes.empty());
105105
}
106+
107+
TEST(InterpreterTest, InterpreterExceptions) {
108+
Cpp::CreateInterpreter();
109+
bool caught = false;
110+
try {
111+
EXPECT_TRUE(Cpp::Declare("int f() { throw; return 1; }") == 0);
112+
EXPECT_TRUE(Cpp::Process("int res = f();") == 0);
113+
} catch (...) {
114+
caught = true;
115+
}
116+
EXPECT_TRUE(caught) << "Unable to catch exception coming from interpreter";
117+
}

0 commit comments

Comments
 (0)