| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // A class to make it easy to tag exception propagation boundaries and | |
| 17 // get crash reports of exceptions that pass over same. | |
| 18 #ifndef OMAHA_COMMON_EXCEPTION_BARRIER_H_ | |
| 19 #define OMAHA_COMMON_EXCEPTION_BARRIER_H_ | |
| 20 | |
| 21 #include <windows.h> | |
| 22 | |
| 23 /// This is the type dictated for an exception handler by the platform ABI | |
| 24 /// @see _except_handler in excpt.h | |
| 25 typedef EXCEPTION_DISPOSITION (__cdecl *ExceptionHandlerFunc)( | |
| 26 struct _EXCEPTION_RECORD *exception_record, | |
| 27 void * establisher_frame, | |
| 28 struct _CONTEXT *context, | |
| 29 void * reserved); | |
| 30 | |
| 31 /// The type of an exception record in the exception handler chain | |
| 32 struct EXCEPTION_REGISTRATION { | |
| 33 EXCEPTION_REGISTRATION *prev; | |
| 34 ExceptionHandlerFunc handler; | |
| 35 }; | |
| 36 | |
| 37 /// This is our raw exception handler, it must be declared extern "C" to | |
| 38 /// match up with the SAFESEH declaration in our corresponding ASM file | |
| 39 extern "C" EXCEPTION_DISPOSITION __cdecl | |
| 40 ExceptionBarrierHandler(struct _EXCEPTION_RECORD *exception_record, | |
| 41 void * establisher_frame, | |
| 42 struct _CONTEXT *context, | |
| 43 void * reserved); | |
| 44 | |
| 45 /// An exception barrier is used to report exceptions that pass through | |
| 46 /// a boundary where exceptions shouldn't pass, such as e.g. COM interface | |
| 47 /// boundaries. | |
| 48 /// This is handy for any kind of plugin code, where if the exception passes | |
| 49 /// through unhindered, it'll either be swallowed by an SEH exception handler | |
| 50 /// above us on the stack, or be reported as an unhandled exception for | |
| 51 /// the application hosting the plugin code. | |
| 52 /// | |
| 53 /// To use this class, simply instantiate an ExceptionBarrier just inside | |
| 54 /// the code boundary, like this: | |
| 55 /// @code | |
| 56 /// HRESULT SomeObject::SomeCOMMethod(...) { | |
| 57 /// ExceptionBarrier report_crashes; | |
| 58 /// | |
| 59 /// ... other code here ... | |
| 60 /// } | |
| 61 /// @endcode | |
| 62 class ExceptionBarrier { | |
| 63 public: | |
| 64 /// Register the barrier in the SEH chain | |
| 65 ExceptionBarrier(); | |
| 66 | |
| 67 /// And unregister on destruction | |
| 68 ~ExceptionBarrier(); | |
| 69 | |
| 70 /// Signature of the handler function which gets notified when | |
| 71 /// an exception propagates through a barrier. | |
| 72 typedef void (CALLBACK *ExceptionHandler)(EXCEPTION_POINTERS *ptrs); | |
| 73 | |
| 74 /// @name Accessors | |
| 75 /// @{ | |
| 76 static void set_handler(ExceptionHandler handler) { s_handler_ = handler; } | |
| 77 static ExceptionHandler handler() { return s_handler_; } | |
| 78 /// @} | |
| 79 | |
| 80 private: | |
| 81 /// Our SEH frame | |
| 82 EXCEPTION_REGISTRATION registration_; | |
| 83 | |
| 84 /// The function that gets invoked if an exception | |
| 85 /// propagates through a barrier | |
| 86 /// TODO(omaha): how can this be statically parametrized? | |
| 87 static ExceptionHandler s_handler_; | |
| 88 }; | |
| 89 | |
| 90 /// @name These are implemented in the associated .asm file | |
| 91 /// @{ | |
| 92 extern "C" void WINAPI RegisterExceptionRecord( | |
| 93 EXCEPTION_REGISTRATION *registration, | |
| 94 ExceptionHandlerFunc func); | |
| 95 extern "C" void WINAPI UnregisterExceptionRecord( | |
| 96 EXCEPTION_REGISTRATION *registration); | |
| 97 /// @} | |
| 98 | |
| 99 | |
| 100 inline ExceptionBarrier::ExceptionBarrier() { | |
| 101 RegisterExceptionRecord(®istration_, ExceptionBarrierHandler); | |
| 102 } | |
| 103 | |
| 104 inline ExceptionBarrier::~ExceptionBarrier() { | |
| 105 // TODO(omaha): I don't think it's safe to unregister after an exception | |
| 106 // has taken place??? | |
| 107 UnregisterExceptionRecord(®istration_); | |
| 108 } | |
| 109 | |
| 110 #endif // OMAHA_COMMON_EXCEPTION_BARRIER_H_ | |
| OLD | NEW |