exception handling - arizona state universityrts.lab.asu.edu/web_438/project_final/talk 4...

30
Exception Handling Nikhil Kulkarni Dipal Saluja

Upload: others

Post on 14-Mar-2020

10 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Exception HandlingNikhil Kulkarni

Dipal Saluja

Page 2: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Introduction x86 Exceptions Exception handling Unix/Linux EHM Fast EHM

Agenda

Page 3: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Introduction An Exception is a unusual or an anomalous condition occurring at runtime. 

Types of Exceptions Processor detected exceptions

Faults ‐ Ex: page fault Traps‐ Ex: debugging Aborts ‐ Ex: inconsistent values in system tables

Programmed exceptions

Page 4: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

x86 Exceptions

Page 5: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Exception Handling

Save contents of registers in kernel mode stack

Handle the exception by means of a C‐function

Exit from handler: ret_from_exception() 

Page 6: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

• Entering the handler

do_trap(){

…current->thread.error_code = error_code; current->thread.trap_no = vector; force_sig(sig_number, current);…

}

• User mode process is delivered the signal

• If in kernel mode, checks whether its an invalid parameter to a system call else it is a kernel bug.

Exception Handling contd..

Page 7: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Segmentation Fault

Page 8: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Segmentation Fault

• regs is a pointer to pt_regs(processor regs)

• 3 bit error code0 – access to page not 

present

1‐ invalid access right

0 – read/exec access

1‐ write access 

0 – kernel mode

1‐ user mode

Page 9: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Unix/Linux EHM

Trampoline code: “A trampoline is a small piece of code that is created at run time when the address of a nested function is taken”.

Page 10: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Fast EHM

Page 11: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

User processes must register all the exceptions to be handled and in addition also specify a memory region on pinned memory, where the PC and condition register will be stored

Ex: Unaligned accesses, protection faults

Fast EHM

Page 12: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

EXCEPTION HANDLING IN C++

Function(){

try{throw new exception();}catch(exception &e){ … }}}

Page 13: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

A few concepts

• Stack Frame: A call stack is composed of stack frames (also called activation records or activation frames). These are machine dependent and ABI‐dependent data structures containing subroutine state

Page 14: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

A few concepts

• Stack Unwinding: When an exception is thrown and control passes from a try block to a handler, the C++ run time calls destructors for all automatic objects constructed since the beginning of the try block. This process is called stack unwinding.

Page 15: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

A Few Concepts

• Landing Pad:A section of user code intended to catch, or otherwise clean up after, an exception. It gains control from the exception runtime via the personality routine, and after doing the appropriate processing either merges into the normal user code or returns to the runtime by resuming or raising a new exception.

Page 16: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

C++ ABI: C++ Exception objects• The exceptionType field encodes the type of the 

thrown exception.

• The fields unexpectedHandler and terminateHandler contain pointers to the unexpected and terminate handlers at the point where the exception is thrown.

• The nextException field is used to create a linked list of exceptions (per thread).

• The handlerCount field contains a count of how many handlers have caught this exception object

• The handlerSwitchValue, actionRecord, languageSpecificData, catchTemp, and adjustedPtr fields cache information that is best computed during pass 1, but useful during pass 2.

• The unwindHeader structure is used to allow correct operation of exception in the presence of multiple languages or multiple runtimes for the same language.

struct __cxa_exception { 

std::type_info *exceptionType;

void (*exceptionDestructor) (void *); 

unexpected_handlerunexpectedHandler;

terminate_handler

Page 17: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

C++ ABI: Caught Exceptions Stack

• Each thread in a C++ program has access to an object of the following class:

struct __cxa_eh_globals {

__cxa_exception *caughtExceptions;

unsigned int uncaughtExceptions;

};

• The caughtExceptions field is a list of the active exceptions, organized as a stack with the most recent first, linked through the nextException field of the exception header.

Page 18: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

C++ ABI: Throwing an Exception

• Allocating the Exception ObjectThis storage must persist while stack is being unwound, since it will be used by the handler, and must be thread‐safe. Exception object storage will therefore normally be allocated in the heap

• Memory will be allocated by the __cxa_allocate_exception runtime library routine.

void *__cxa_allocate_exception(size_t 

Page 19: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

C++ ABI: Throwing an Exception

• Throwing the Exception object

void __cxa_throw (void *thrown_exception, std::type_info *tinfo, void (*dest) (void *) );

• This routine never returns

• Arguments• The address of the thrown exception object 

• A std::type_info pointer, giving the static type of the throw argument as a std::type_info pointer, used for 

t hi t ti l

Page 20: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

_Cxa_throw continued

• The __cxa_throw routine will do the following:

• Obtain the __cxa_exception header from the thrown exception object address, which can be computed as follows:__cxa_exception *header = ((__cxa_exception *) thrown_exception ‐ 1);

• Save the current unexpected_handler and terminate_handler in the __cxa_exception header.

Page 21: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

_Cxa_throw continued

• __Unwind_RaiseException begins the process of stack unwinding. 

• In special cases, such as an inability to find a handler, _Unwind_RaiseException may return. In that case, __cxa_throw will call terminate, assuming that there was no handler for the exception.

Page 22: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Unwind Process

• This ABI requires a two‐phase unwind process. During the first phase, i.e. with actions including the bit _UA_SEARCH_PHASE, the personality routine should do nothing to update state, simply searching for a handler and returning _URC_HANDLER_FOUND when it finds one.

• During the second phase, i.e. with actions including the bit _UA_CLEANUP_PHASE, the personality routine may perform cleanup actions at intermediate frames and must

Page 23: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Overview of Throw Processing

• Call __cxa_allocate_exception to create an exception object.

• Evaluate the thrown expression, and copy it into the buffer returned by __cxa_allocate_exception, possibly using a copy constructor

• Call __cxa_throw to pass the exception to the runtime library. __cxa_throw never returns.

Page 24: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Catching an Exception

Personality routine• Unwind_Reason_Code (*__personality_routine)(int version,_Unwind_Action actions,uint64 exceptionClass,

• A language and vendor specific personality routine will be stored by the compiler in the unwind descriptor for the stack frames requiring exception processing. The personality routine is called by the

Page 25: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Exception Handlers

• Upon entry, a handler must call:void *__cxa_get_exception_ptr ( void *exceptionObject );

This routine returns the adjusted pointer to the exception object. (The adjusted pointer is typically computed by the personality routine during phase 1 and saved in the exception object.)

Page 26: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Exception Handlers

• Following initialization of the catch parameter, a handler must call:

void *__cxa_begin_catch ( void *exceptionObject );

This routine:• Increment's the exception's handler count.• Places the exception on the stack of currently‐caught exceptions if it is not already there, linking the exception to the previous top of the stack.

Page 27: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Exception Handlers

• Upon exit for any reason, a handler must call:void __cxa_end_catch ();This routine:• Locates the most recently caught exception and decrements its handler count.

• Removes the exception from the caught exception stack, if the handler count goes to zero.

• Destroys the exception if the handler count goes to zero, and the exception was not re‐

Page 28: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Finishing and Destroying the Exception

• An exception is considered handled:

1. Immediately after initializing the parameter of the corresponding catch clause (or upon entry to a catch(...) clause).

• An exception is considered finished:

1. When the corresponding catch clause exits (normally, by another throw, or by rethrow).

2. When unexpected() 

Page 29: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

References

1. “Hardware and Software Support for Efficient Exception Handling”Chandramohan A. Thekkath and Henry M. Levy

2. “Advanced Exception Handling Mechanisms” Peter A. Buhr and W. Y. Russell Mok

3. “Understanding the Linux Kernel” 3rd edition, Daniel P. Bovet, Marco Cesat

4. Itanium C++ ABI: Exception Handling,- http://mentorembedded.github.com/cxx-abi/abi-eh.html

5. http://www.airs.com/blog/archives/464

Page 30: Exception Handling - Arizona State Universityrts.lab.asu.edu/web_438/project_final/Talk 4 exception_handling.pdf · Exception Handlers • Upon exit for any reason, a handler must

Q & A