Home | History | Annotate | Download | only in src
      1 // Copyright (C) 2012 The Android Open Source Project
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions
      6 // are met:
      7 // 1. Redistributions of source code must retain the above copyright
      8 //    notice, this list of conditions and the following disclaimer.
      9 // 2. Redistributions in binary form must reproduce the above copyright
     10 //    notice, this list of conditions and the following disclaimer in the
     11 //    documentation and/or other materials provided with the distribution.
     12 // 3. Neither the name of the project nor the names of its contributors
     13 //    may be used to endorse or promote products derived from this software
     14 //    without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19 // ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22 // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26 // SUCH DAMAGE.
     27 //===----------------------------------------------------------------------===//
     28 //
     29 //                     The LLVM Compiler Infrastructure
     30 //
     31 // This file is dual licensed under the MIT and the University of Illinois Open
     32 // Source Licenses. See LICENSE.TXT for details.
     33 //
     34 //
     35 //  This file implements the "Exception Handling APIs"
     36 //  http://www.codesourcery.com/public/cxx-abi/abi-eh.html
     37 //  http://www.intel.com/design/itanium/downloads/245358.htm
     38 //
     39 //===----------------------------------------------------------------------===//
     40 
     41 #include <cassert>
     42 #include <cstdlib>
     43 #include <exception>
     44 #include <unwind.h>
     45 #include <typeinfo>
     46 #include "cxxabi_defines.h"
     47 #include "dwarf_helper.h"
     48 #include "helper_func_internal.h"
     49 
     50 
     51 namespace __cxxabiv1 {
     52 
     53   /*
     54    * Personality Routine
     55    */
     56   extern "C" _Unwind_Reason_Code BEGIN_DEFINE_PERSONALITY_FUNC
     57     if (version != 1 || unwind_exception == 0 || context == 0) {
     58       return _URC_FATAL_PHASE1_ERROR;
     59     }
     60 
     61     bool native_exception = exceptionClass == __gxx_exception_class;
     62     ScanResultInternal results;
     63 
     64     /*
     65      * Phase 1: Search
     66      */
     67     if (actions & _UA_SEARCH_PHASE) {
     68       scanEHTable(results, actions, native_exception, unwind_exception, context);
     69       if (results.reason == _URC_HANDLER_FOUND) {
     70         // Found one.  Cache result for phase 2.
     71         if (native_exception) {
     72           __cxa_exception* exception_header = reinterpret_cast<__cxa_exception*>(unwind_exception+1)-1;
     73           exception_header->handlerSwitchValue = static_cast<int>(results.ttypeIndex);
     74           exception_header->actionRecord = results.actionRecord;
     75           exception_header->languageSpecificData = results.languageSpecificData;
     76           exception_header->catchTemp = reinterpret_cast<void*>(results.landingPad);
     77           exception_header->adjustedPtr = results.adjustedPtr;
     78           saveDataToBarrierCache(unwind_exception, context, results);
     79         }
     80         return _URC_HANDLER_FOUND;
     81       }
     82       return continueUnwinding(unwind_exception, context);
     83     }
     84 
     85     /*
     86      * Phase 2: Cleanup
     87      */
     88     if (actions & _UA_CLEANUP_PHASE) {
     89       if (actions & _UA_HANDLER_FRAME) {
     90         if (native_exception) {
     91           // We have cached on phase 1. Just recall that.
     92           __cxa_exception* exception_header = reinterpret_cast<__cxa_exception*>(unwind_exception+1)-1;
     93           results.ttypeIndex = exception_header->handlerSwitchValue;
     94           results.actionRecord = exception_header->actionRecord;
     95           results.languageSpecificData = exception_header->languageSpecificData;
     96           results.landingPad = reinterpret_cast<uintptr_t>(exception_header->catchTemp);
     97           results.adjustedPtr = exception_header->adjustedPtr;
     98           loadDataFromBarrierCache(unwind_exception, results);
     99         } else {
    100           scanEHTable(results, actions, native_exception, unwind_exception, context);
    101 
    102           if (results.reason != _URC_HANDLER_FOUND)
    103             call_terminate(unwind_exception);
    104         }
    105 
    106         setRegisters(unwind_exception, context, results);
    107         saveUnexpectedDataToBarrierCache(unwind_exception,
    108                                          context,
    109                                          results);
    110         return _URC_INSTALL_CONTEXT;
    111       }
    112 
    113       // Force unwinding or no handler found
    114       scanEHTable(results, actions, native_exception, unwind_exception, context);
    115       if (results.reason == _URC_HANDLER_FOUND) {
    116         // Found a non-catching handler.  Jump to it.
    117         setRegisters(unwind_exception, context, results);
    118         prepareBeginCleanup(unwind_exception);
    119         return _URC_INSTALL_CONTEXT;
    120       }
    121       return continueUnwinding(unwind_exception, context);
    122     }
    123 
    124     // Why here? Neither phase 1 nor 2.
    125     return _URC_FATAL_PHASE1_ERROR;
    126   }
    127 
    128 }  // namespace __cxxabiv1
    129