llvm / llvm/llvm-project

lld/MachO + ASan + UBSan: thrown exception is uncatchable on macOS arm64

Open
#198,706 1 comment 0 reactions 0 assignees View on GitHub
lld lld:MachO
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

On macOS arm64, when linking a C++ program with both AddressSanitizer and UndefinedBehaviorSanitizer through ld.lld, a thrown exception derived from std::exception is not caught by an enclosing catch(...) and the program terminates with "uncaught exception of type X". Removing either sanitizer, or substituting Apple's ld64, makes the program behave correctly.

```
$ cat repro_lld_eh_simple.cpp
#include
#include

class MyError : public std::exception {
public:
explicit MyError(const char* msg) : m_msg(msg) {}
const char* what() const noexcept override { return m_msg; }
private:
const char* m_msg;
};

__attribute__((noinline))
static void thrower() {
throw MyError("hello");
}

int main() {
try {
thrower();
std::fprintf(stderr, "FAIL: thrower did not throw\n");
return 1;
} catch (...) {
std::fprintf(stderr, "PASS\n");
return 0;
}
}

$ /opt/homebrew/Cellar/llvm/22.1.4/bin/clang -std=c++20 -O0 -g -fuse-ld=lld -fsanitize=address,undefined repro_lld_eh_simple.cpp -o repro_lld_eh_simple -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -lstdc++
$ ./repro_lld_eh_simple
libc++abi: terminating due to uncaught exception of type MyError: hello
Abort trap: 6

$ /opt/homebrew/Cellar/llvm/22.1.4/bin/clang -std=c++20 -O0 -g -fsanitize=address,undefined repro_lld_eh_simple.cpp -o repro_lld_eh_simple -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -lstdc++
$ ./repro_lld_eh_simple
PASS

$ /opt/homebrew/Cellar/llvm/22.1.4/bin/clang -std=c++20 -O0 -g -fuse-ld=lld -fsanitize=undefined repro_lld_eh_simple.cpp -o repro_lld_eh_simple -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -lstdc++
$ ./repro_lld_eh_simple
PASS

$ /opt/homebrew/Cellar/llvm/22.1.4/bin/clang -std=c++20 -O0 -g -fsanitize=address,undefined repro_lld_eh_simple.cpp -o repro_lld_eh_simple -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk -lstdc++
$ ./repro_lld_eh_simple
PASS
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.