When I compile the following code targeting arm64 and execute it, it terminates with an uncaught exception: "libc++abi.dylib: terminating with uncaught exception of type int":
int foo1() noexcept {
try { throw 0; } catch (int i) { return 0; } return 1;
}
int main() {
return foo1();
}
This happens because function foo1 has attribute nounwind but doesn't have attribute uwtable on it, in which case Funcion::needsUnwindTableEntry, which is the function that determines whether an unwind table is needed, returns false.
bool needsUnwindTableEntry() const { return hasUWTable() || !doesNotThrow(); }
This patch changes MachO::IsUnwindTablesDefault to return true when !UseSjLjExceptions. When the function returns true, -munwind-table is passed to the frontend, which causes IRGen to annotate functions with attribute uwtable.
One question: instead of adding uwtable in SetLLVMFunctionAttributesForDefinition whenever CodeGenOpts.UnwindTables is true, is it possible to limit it to functions that actually need unwind tables (for example, in TryMarkNoThrow in CodeGenFunction.cpp)?
rdar://problem/32411865