Overview of Exception Handling Implementation in Open64 Hucheng Zhou Wenguang Chen Tsinghua University 10/15/2019
Outline Introduction Three-Level C++ EH ABI Overview of component of EH Mechanism EH Implementation in Open64 LSDA structure Call Frame Dwarf Information How to debug EH implementation 10/15/2019
Introduction Exceptions Handling provides a systematic and robust approach to coping with errors that cannot be recovered from locally at the point where they are detected There are two main strategies to EH implementation: “Code” based approach “Table” based approach C++ EH ABI defines the convention of “Table” based approach Most of the existed compiler adopt the “Table” based approach, including gcc, open64, LLVM Since it is “zero-cost” way that it will not add the burden on the exception-free execution We will describe the detailed C++ EH ABI and the specific implementation in Open64 10/15/2019
Three Level C++ EH ABI According to the bottom-up order Level 1: 2019/10/15 Three Level C++ EH ABI According to the bottom-up order Level 1: Unwind Library Level 2: Runtime Library, i.e., libstdc++.so Level 3: Unwind descriptor and EH table generated by compiler This EH ABI is part of C++ ABI for Itanium, but as the de fact ABI for EH. 10/15/2019
Three Level EH ABI 10/15/2019
Component of EH FLOW 10/15/2019
EH Implementation in Open64 10/15/2019
EH Implementation in Open64 The implantation in FE, IPA, OPT and CGEMIT is straightforward Here we mainly focus on CG part: LSDA (Language Specific Data Area) generation, including call site table, activation record table, type table and type specification table Unwind Descriptor generation Call Frame Dwarf Information .eh_frame section 10/15/2019
2019/10/15 This graph shows the internal data structure for EH table, which is not documented well. These data structures were dumped through reversing engineering and debug the personality_routine in libstdc++. 10/15/2019
Unwind Table 10/15/2019
LSDA Structure 10/15/2019
LSDA Illustration 10/15/2019
Call Frame Dwarf Information The unwinding operation needs CIE and FDE information to know how to recover the saved registers and how to compute the predecessor’s CFA (Canonical Frame Address) and code location 10/15/2019
Example 10/15/2019 2019/10/15 There are 8 4-byte registers: R0 always 0; R1 hold the return address; R2~R3 caller saved registers; R4~R6 callee saved registers; R7 stack register. 10/15/2019
2019/10/15 Example Cont’ The first column indicates an address for every location. The remaining columns contain virtual unwinding rules that are associated with the indicated location given in the first column. CFA(Canonical Frame Address) is used to identify the current call frame. Typically, CFA is defined to be the values of the stack pointer at the call site in the previous frame. S = same_value; u = undefined value; r = register(N) rule; c = offset(N) rule; a = architecture rule 10/15/2019
10/15/2019
Call Frame Dwarf Information Prologue and epilogue code is not always in distinct blocks at the beginning and end of a program There are different ways to manage the call frame, sometimes using frame pointer CFA’s computation will be different at different PC Some PU hasn’t call frame Callee saved register can be saved in another register which is a scratch register 10/15/2019
Impact on Optimization 2019/10/15 Impact on Optimization Optimization to exception free application Generally, there is no runtime overhead. However, EH has a few impacts on compiler optimization More complex CFG (e.g., There should be an edge from throw point to its landing pad) Conservative optimization across EH region Optimization to exception intensive application Unnecessary EH ranges removable IPA can help to statically analyze the thrown point and its related landing pad Profile the exception caught path, and inline the calls during thrown point and its handler function Generally, table based EH implementation does not add any runtime overhead to exception-free application 10/15/2019
How to Debug EH Implementation 1. Add EH related trace mechanism 2. Instrument the personality routine in libstdc++.so 3. Even instrument the functions in unwind library 10/15/2019
Thanks Thank you very much And any questions? 10/15/2019