Exception Handling. Nikhil Kulkarni Dipal Saluja

Exception Handling Nikhil Kulkarni Dipal Saluja Agenda      Introduction x86 Exceptions Exception handling Unix/Linux EHM Fast EHM Introduct...
Author: Felicity Snow
1 downloads 1 Views 607KB Size
Exception Handling Nikhil Kulkarni Dipal Saluja

Agenda     

Introduction x86 Exceptions Exception handling Unix/Linux EHM Fast EHM

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

x86 Exceptions

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() 

Exception Handling contd.. • 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.

Segmentation Fault

Segmentation Fault • regs is a pointer to pt_regs(processor regs) • 3 bit error code 0 – access to page not  present 1‐ invalid access right

0 – read/exec access

0 – kernel mode

1‐ write access 

1‐ user mode

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”.

Fast EHM

Fast EHM 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

EXCEPTION HANDLING IN C++ Function() { try{ throw new exception(); } catch(exception &e) { … } } }

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

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.

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.

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  (*exceptionDestruct or) (void *);  unexpected_handler unexpectedHandler; terminate_handler

C++ ABI: Caught Exceptions Stack • Each thread in a C++  • The  program has access  caughtExceptions  to an object of the  field is a list of the  following class: active exceptions,  struct __cxa_eh_globals { organized as a stack  __cxa_exception * with the most  caughtExceptions; recent first, linked  unsigned int uncaughtExceptions; }; through the  nextException field  of the exception  header.

C++ ABI: Throwing an Exception • Allocating the Exception Object This 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 

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 

_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.

_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.

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 

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.

Catching an Exception Personality routine • Unwind_Reason_Co de  (*__personality_rou tine) (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

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.)

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.

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‐

Finishing and Destroying the  Exception • An exception is  considered handled:

• An exception is  considered finished:

1. Immediately after  1. When the  initializing the  corresponding catch  parameter of the  clause exits  corresponding catch  (normally, by  clause (or upon  another throw, or  entry to a catch(...)  by rethrow). clause). 2. When unexpected() 

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

Q & A