Structured Exception Handling for EM/NT

Download Report

Transcript Structured Exception Handling for EM/NT

C++ Exception Handling for
IA-64 Unix
By
Priti Shrivastav
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
1
C++ EH Overview




C++ EH uses static model to avoid runtime overhead for
functions with try/throw/catch statements.
Destructor thunks are separate functions.
Try and Catch blocks are generated inline within the function’s
body.
Three tables are generated by the compiler backend for each
routine and for each catch routine.
– Function Table
– Try region Table ( C++ EH tables )
– Unwind descriptors table
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
2
Compiler and C++ EH Runtime

Catch blocks stay mainline code.
– They need to be separate entry points.
– Runtime will allocate exception objects on the regular heap.


At start-up runtime will pre-allocate enough space to process
one out-of-memory exception.
Runtime allocates the object on heap before branching to catch
block. Runtime will pass in r8 the destructor thunk which frees
the object to catch. Catch will call the destructor thunk before
leaving the catch block. If the object to catch is passed by value,
two copies of the objects will be allocated by the runtime and
the destructor thunk will have to know to free both.
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
3
Function,Try Region and Unwind Tables

Runtime function table is created for every executable image.
– A function table entry is created for every function.

typedef struct _RUNTIME_FUNCTION {
ULONGLONG BeginAddress;
ULONGLONG EndAddress;
ULONGLONG UnwindInfo;
// unwind information block
} RUNTIME_FUNCTION, *PRUNTIME_FUNCTION;
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
4
Function,Try Region and Unwind Tables
(Continued)

The unwindinfo field points to a structure which contains the
unwind information for the function
– The Unwind table

typedef struct _UNWIND_INFO {
USHORT version;
USHORT Flags;
// try/except or try/finally indicator
UINT DataLength;
// length of the unwind descriptors
UCHAR Descriptors[];
// unwind descriptors
PEXCEPTION_ROUTINE ExceptionHandler; // personality routine
TRY_REGION_TABLE TryRegionInfo;
// Try Region Table
CLEANUP_TABLE ObjectCleanupInfo;
// Object Cleanup Table
} UNWIND_INFO, *PUNWIND_INFO;
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
5
Function,Try Region and Unwind Tables
(Continued)

The TryRegionInfo points to the try region table whose entries
define the try regions in the function.
– The table is used by the C++ exception handler to determine the active
regions in the function at the time of exception.
– The entries for nested scopes are PC-mapped and are ordered from inner
to outer scopes.
– The try region table
 typedef struct _TRY_REGION_TABLE {
UINT NumberOfTryEntries;
struct {
UINT BeginAddress;
UINT EndAddress;
UINT CatchHandlerInfo;
} TryRegionTableEntry[ ];
// begin of a try region
// next bundle after the end of try
// catch address table
} TRY_REGION_TABLE, *PTRY_REGION_TABLE;
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
6
Function,Try Region and Unwind Tables
(Continued)

The ObjectCleanupInfo points to the object cleanup table
whose entries define the cleanup action required in the function.
– The table is used by the C++ exception handler to determine the active
cleanup regions in the function at the time of exception.
– The entries for nested scopes are PC-mapped and are in reverse order of
creation.
– The object cleanup table
 typedef struct _OBJ_CLEANUP_TABLE {
UINT NumberOfCleanupEntries;
struct {
UINT BeginAddress;
// begin of a cleanup region
UINT EndAddress;
// next bundle after the end of region
UINT CleanupFunctionAddress;
// destructor ’s address
} ObjectCleanupEntry[ ];
} CLEANUP_TABLE, *PCLEAUP_TABLE;
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
7
Unwind support in EM compiler






Unwind descriptor region header records are created for prolog,body
and epilog regions
Descriptors for updating special and preserved registers in prolog
regions are generated.
Unwind mechanism handles shrink-wrap regions
– In case of multiple prologs, either a matching epilog region header
record for each prolog is created or epilog region header records
specify the number of prolog regions to pop.
Each region header record specifies region length which helps
determine which are the active regions for a given IP.
All the spill code for saving the preserved registers must be in the
prolog region. For this post-pass scheduling must be prevented for the
prolog.
Previous sp value is saved in a stacked register for variable size
frames.
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
8
IA-64 Unwind Scheme
Call unwind
(target sp, target
bsp, target IP)
Lookup Funtion
Table Entry
Unwinder
Personality
Routine
Process
descriptors to set
the call frame
(sp,bsp)
Cleanup the
objects
Unwind to the
previous call
frame
Unwinder
No
Target
Frame?
Yes
Resume
execution at
the target IP
R
7/7/2015
intel
Microcomputer Software Labs
Intel Confidential
9