Issue 240877
Summary [BOLT] An exception table with no call-site record is dropped: a `noexcept` function no longer calls `std::terminate`
Labels BOLT
Assignees
Reporter not4juu
    ## Description

GCC implements `noexcept` with an exception table (LSDA) whose call-site table has no record for the throwing call: the personality routine finds no record and calls `std::terminate`. BOLT emits no LSDA for a function, or a fragment of one, without call sites, so the rewritten FDE keeps the personality but has no LSDA. The personality then continues unwinding, and the exception reaches a handler in a caller that `noexcept` should have stopped, so the rewritten binary behaves differently from the original, on both AArch64 and x86-64.

**Environment:**

- **BOLT:** `llvm-bolt`, `llvm-dwarfdump` and `llvm-objdump` built from `72f18f704c03c8bdac29ba54d387e7d08fd1bad4`
- **Tools:** `aarch64-linux-gnu-g++ 14.1.1` with GNU `ld 2.41` (AArch64), `g++ 14.2.0` with GNU `ld 2.46` (x86-64)

## Reproducer

```cpp
// lib.cpp
void thrower(int);
__attribute__((noinline)) int guarded(int x) noexcept {
 thrower(x);
  return x + 1;
}
```

```cpp
// main.cpp
extern "C" int puts(const char *);
int guarded(int);  // defined noexcept in lib.cpp; declared without it so the catch is kept
void thrower(int x) { if (x > 0) throw x; }
int main(int argc, char **) {
  try {
    guarded(argc);
  } catch (int) {
    puts("caught: the noexcept of guarded() was ignored");
 return 1;
  }
  return 0;
}
```

```bash
aarch64-linux-gnu-g++ -O2 -fPIC -shared lib.cpp -o libt.so -Wl,--emit-relocs
aarch64-linux-gnu-g++ -O2 main.cpp -o main -L. -lt
mkdir -p bolt && llvm-bolt libt.so -o bolt/libt.so
```

### Result

```bash
# on an AArch64 machine, or under qemu-aarch64
LD_LIBRARY_PATH=.    ./main; echo "rc=$?"
LD_LIBRARY_PATH=bolt ./main; echo "rc=$?"
```

```
# libt.so
terminate called after throwing an instance of 'int'
rc=134

# bolt/libt.so
caught: the noexcept of guarded() was ignored
rc=1
```

In `.eh_frame`, the FDE of `guarded` loses its LSDA:

```bash
F='-e CIE -e FDE -e Augmentation: -e LSDA'
G1=$(llvm-objdump -t libt.so      | awk '/ _Z7guardedi$/ {print substr($1, 9)}')
G2=$(llvm-objdump -t bolt/libt.so | awk '/ _Z7guardedi$/ {print substr($1, 9)}')
llvm-dwarfdump --eh-frame libt.so      | grep $F | grep -B2 -A1 "pc=$G1" | grep -v 'CIE$'
llvm-objdump -s -j .gcc_except_table libt.so | tail -1
llvm-dwarfdump --eh-frame bolt/libt.so | grep $F | grep -B2 -A1 "pc=$G2" | grep -v 'CIE$'
```

```
# libt.so
  Augmentation: "zPLR"
00000094 00000024 00000020 FDE cie=00000078 pc=000006c0...000006e4
  LSDA Address: 00000000000007f0

# .gcc_except_table of libt.so
 07f0 ffff0100 ....

# bolt/libt.so: the FDE has no LSDA Address line
  Augmentation: "zPR"
00000090 00000024 00000020 FDE cie=00000074 pc=004000e8...0040010c
```

| `guarded` | `libt.so` | `bolt/libt.so` | expected |
|---|---|---|---|
| address | `0x6c0` | `0x4000e8` | `0x4000e8` |
| CIE augmentation | `zPLR` | `zPR` | `zPLR` |
| personality | `__gxx_personality_v0` | `__gxx_personality_v0` | `__gxx_personality_v0` |
| LSDA | `0x7f0`: `ff ff 01 00`, empty call-site table | none | `0x4002a8`: `ff ff 01 00`, empty call-site table |

On x86-64, building and running with `g++` gives the same `rc=134` / `rc=1`, and the FDE of `guarded` loses its LSDA the same way (`zPLR` becomes `zPR`).

## Cause

- `parseLSDA()` finds no call-site record, so no instruction gets a landing pad and `updateEHRanges()` builds no call sites for the function.
- `BinaryFunction::getLSDASymbol()` returns `nullptr` when `getCallSites(F)` is empty, and `BinaryEmitter::emitLSDA()` returns early for the same reason.
- The FDE is then emitted with `emitCFILsda(0, DW_EH_PE_omit)`. A missing LSDA means "no handlers, no cleanups" to the personality, while an empty call-site table means "terminate".

## Possible fix

When the input function had an LSDA, emit one for every fragment that contains a call, with an empty call-site table if the fragment has no call sites. This also covers `.cold` fragments emitted by the compiler and fragments split by BOLT, which lose their LSDA for the same reason. A fragment without calls cannot throw, so it needs none. The empty table does not reference a landing-pad fragment, since such a fragment may have none. ICF must then not fold a function that has an LSDA with one that has none: whichever body is kept, the other function's behaviour changes.

```diff
--- a/bolt/include/bolt/Core/BinaryFunction.h
+++ b/bolt/include/bolt/Core/BinaryFunction.h
@@ -2109,11 +2109,16 @@ public:
 /// Return original LSDA address for the function or NULL.
   uint64_t getLSDAAddress() const { return LSDAAddress; }
 
+  /// Return true if fragment \p F has no call sites but needs an LSDA anyway:
+  /// the input function had one, so a throw from a call in \p F must call
+  /// std::terminate() instead of unwinding further.
+  bool needsEmptyLSDA(const FragmentNum F) const;
+
   /// Return symbol pointing to function's LSDA.
 MCSymbol *getLSDASymbol(const FragmentNum F) {
     if (F.get() < LSDASymbols.size() && LSDASymbols[F.get()] != nullptr)
       return LSDASymbols[F.get()];
-    if (getCallSites(F).empty())
+    if (getCallSites(F).empty() && !needsEmptyLSDA(F))
       return nullptr;
 
 if (F.get() >= LSDASymbols.size())
--- a/bolt/lib/Core/BinaryEmitter.cpp
+++ b/bolt/lib/Core/BinaryEmitter.cpp
@@ -940,7 +940,7 @@ void BinaryEmitter::emitCFIInstruction(const MCCFIInstruction &Inst) const {
 void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) {
   const BinaryFunction::CallSitesRange Sites =
       BF.getCallSites(FF.getFragmentNum());
-  if (Sites.empty())
+  if (Sites.empty() && !BF.needsEmptyLSDA(FF.getFragmentNum()))
     return;
 
 Streamer.switchSection(BC.MOFI->getLSDASection());
@@ -957,6 +957,16 @@ void BinaryEmitter::emitLSDA(BinaryFunction &BF, const FunctionFragment &FF) {
 assert(LSDASymbol && "no LSDA symbol set");
 Streamer.emitLabel(LSDASymbol);
 
+  // No call sites: any throw from this fragment terminates. Such a fragment
+  // may have no landing pad fragment, so do not reference one.
+  if (Sites.empty()) {
+ Streamer.emitIntValue(dwarf::DW_EH_PE_omit, 1);    // LPStart format
+ Streamer.emitIntValue(dwarf::DW_EH_PE_omit, 1);    // TType format
+ Streamer.emitIntValue(dwarf::DW_EH_PE_uleb128, 1); // Call site format
+ Streamer.emitULEB128IntValue(0);                   // Call site table size
+ return;
+  }
+
   // Corresponding FDE start.
   const MCSymbol *StartSymbol = BF.getSymbol(FF.getFragmentNum());
 
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -3258,6 +3258,16 @@ uint64_t BinaryFunction::getInstructionCount() const {
   return Count;
 }
 
+bool BinaryFunction::needsEmptyLSDA(const FragmentNum F) const {
+  if (!getLSDAAddress() || !getCallSites(F).empty())
+    return false;
+  for (const BinaryBasicBlock *BB : getLayout().getFragment(F))
+    for (const MCInst &Inst : *BB)
+      if (BC.MIB->isCall(Inst))
+        return true;
+  return false;
+}
+
 void BinaryFunction::clearDisasmState() {
 clearList(Instructions);
   clearList(IgnoredBranches);
--- a/bolt/lib/Passes/IdenticalCodeFolding.cpp
+++ b/bolt/lib/Passes/IdenticalCodeFolding.cpp
@@ -332,6 +332,10 @@ static bool isIdenticalWith(const BinaryFunction &A, const BinaryFunction &B,
 ++BBI;
   }
 
+  // A call without a call site terminates only when the function has an LSDA.
+  if (!A.getLSDAAddress() != !B.getLSDAAddress())
+ return false;
+
   // Compare exceptions action tables.
   if (A.getLSDAActionTable() != B.getLSDAActionTable() ||
 A.getLSDATypeTable() != B.getLSDATypeTable() ||
```

With this change, on AArch64 and x86-64:

- the reproducer calls `std::terminate` again (`rc=134`), and `guarded` has an LSDA `ff ff 01 00`;
- every fragment of a split `noexcept` function that contains a call gets an LSDA, including `.cold` fragments emitted by the compiler and fragments split by BOLT;
- with `--icf`, a `noexcept` function and an identical one without `noexcept` are no longer folded;
- `check-bolt`, `check-large-bolt` and `check-large-bolt-arm` give the same results as without the change.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to