1 // Copyright (c) 2006, Google Inc.
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 are
6 // met:
7 //
8 //     * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //     * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 //     * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 
30 #include <objbase.h>
31 
32 #include <algorithm>
33 #include <cassert>
34 #include <cstdio>
35 
36 #include "common/windows/string_utils-inl.h"
37 
38 #include "client/windows/common/ipc_protocol.h"
39 #include "client/windows/handler/exception_handler.h"
40 #include "common/windows/guid_string.h"
41 
42 namespace google_breakpad {
43 
44 // This is passed as the context to the MinidumpWriteDump callback.
45 typedef struct {
46   AppMemoryList::const_iterator iter;
47   AppMemoryList::const_iterator end;
48 } MinidumpCallbackContext;
49 
50 // This define is new to Windows 10.
51 #ifndef DBG_PRINTEXCEPTION_WIDE_C
52 #define DBG_PRINTEXCEPTION_WIDE_C ((DWORD)0x4001000A)
53 #endif
54 
55 vector<ExceptionHandler*>* ExceptionHandler::handler_stack_ = NULL;
56 LONG ExceptionHandler::handler_stack_index_ = 0;
57 CRITICAL_SECTION ExceptionHandler::handler_stack_critical_section_;
58 volatile LONG ExceptionHandler::instance_count_ = 0;
59 
ExceptionHandler(const wstring & dump_path,FilterCallback filter,MinidumpCallback callback,void * callback_context,int handler_types,MINIDUMP_TYPE dump_type,const wchar_t * pipe_name,const CustomClientInfo * custom_info)60 ExceptionHandler::ExceptionHandler(const wstring& dump_path,
61                                    FilterCallback filter,
62                                    MinidumpCallback callback,
63                                    void* callback_context,
64                                    int handler_types,
65                                    MINIDUMP_TYPE dump_type,
66                                    const wchar_t* pipe_name,
67                                    const CustomClientInfo* custom_info) {
68   Initialize(dump_path,
69              filter,
70              callback,
71              callback_context,
72              handler_types,
73              dump_type,
74              pipe_name,
75              NULL,  // pipe_handle
76              NULL,  // crash_generation_client
77              custom_info);
78 }
79 
ExceptionHandler(const wstring & dump_path,FilterCallback filter,MinidumpCallback callback,void * callback_context,int handler_types,MINIDUMP_TYPE dump_type,HANDLE pipe_handle,const CustomClientInfo * custom_info)80 ExceptionHandler::ExceptionHandler(const wstring& dump_path,
81                                    FilterCallback filter,
82                                    MinidumpCallback callback,
83                                    void* callback_context,
84                                    int handler_types,
85                                    MINIDUMP_TYPE dump_type,
86                                    HANDLE pipe_handle,
87                                    const CustomClientInfo* custom_info) {
88   Initialize(dump_path,
89              filter,
90              callback,
91              callback_context,
92              handler_types,
93              dump_type,
94              NULL,  // pipe_name
95              pipe_handle,
96              NULL,  // crash_generation_client
97              custom_info);
98 }
99 
ExceptionHandler(const wstring & dump_path,FilterCallback filter,MinidumpCallback callback,void * callback_context,int handler_types,CrashGenerationClient * crash_generation_client)100 ExceptionHandler::ExceptionHandler(
101     const wstring& dump_path,
102     FilterCallback filter,
103     MinidumpCallback callback,
104     void* callback_context,
105     int handler_types,
106     CrashGenerationClient* crash_generation_client) {
107   // The dump_type, pipe_name and custom_info that are passed in to Initialize()
108   // are not used.  The ones set in crash_generation_client are used instead.
109   Initialize(dump_path,
110              filter,
111              callback,
112              callback_context,
113              handler_types,
114              MiniDumpNormal,           // dump_type - not used
115              NULL,                     // pipe_name - not used
116              NULL,                     // pipe_handle
117              crash_generation_client,
118              NULL);                    // custom_info - not used
119 }
120 
ExceptionHandler(const wstring & dump_path,FilterCallback filter,MinidumpCallback callback,void * callback_context,int handler_types)121 ExceptionHandler::ExceptionHandler(const wstring& dump_path,
122                                    FilterCallback filter,
123                                    MinidumpCallback callback,
124                                    void* callback_context,
125                                    int handler_types) {
126   Initialize(dump_path,
127              filter,
128              callback,
129              callback_context,
130              handler_types,
131              MiniDumpNormal,
132              NULL,   // pipe_name
133              NULL,   // pipe_handle
134              NULL,   // crash_generation_client
135              NULL);  // custom_info
136 }
137 
Initialize(const wstring & dump_path,FilterCallback filter,MinidumpCallback callback,void * callback_context,int handler_types,MINIDUMP_TYPE dump_type,const wchar_t * pipe_name,HANDLE pipe_handle,CrashGenerationClient * crash_generation_client,const CustomClientInfo * custom_info)138 void ExceptionHandler::Initialize(
139     const wstring& dump_path,
140     FilterCallback filter,
141     MinidumpCallback callback,
142     void* callback_context,
143     int handler_types,
144     MINIDUMP_TYPE dump_type,
145     const wchar_t* pipe_name,
146     HANDLE pipe_handle,
147     CrashGenerationClient* crash_generation_client,
148     const CustomClientInfo* custom_info) {
149   LONG instance_count = InterlockedIncrement(&instance_count_);
150   filter_ = filter;
151   callback_ = callback;
152   callback_context_ = callback_context;
153   dump_path_c_ = NULL;
154   next_minidump_id_c_ = NULL;
155   next_minidump_path_c_ = NULL;
156   dbghelp_module_ = NULL;
157   minidump_write_dump_ = NULL;
158   dump_type_ = dump_type;
159   rpcrt4_module_ = NULL;
160   uuid_create_ = NULL;
161   handler_types_ = handler_types;
162   previous_filter_ = NULL;
163 #if _MSC_VER >= 1400  // MSVC 2005/8
164   previous_iph_ = NULL;
165 #endif  // _MSC_VER >= 1400
166   previous_pch_ = NULL;
167   handler_thread_ = NULL;
168   is_shutdown_ = false;
169   handler_start_semaphore_ = NULL;
170   handler_finish_semaphore_ = NULL;
171   requesting_thread_id_ = 0;
172   exception_info_ = NULL;
173   assertion_ = NULL;
174   handler_return_value_ = false;
175   handle_debug_exceptions_ = false;
176   consume_invalid_handle_exceptions_ = false;
177 
178   // Attempt to use out-of-process if user has specified a pipe or a
179   // crash generation client.
180   scoped_ptr<CrashGenerationClient> client;
181   if (crash_generation_client) {
182     client.reset(crash_generation_client);
183   } else if (pipe_name) {
184     client.reset(
185       new CrashGenerationClient(pipe_name, dump_type_, custom_info));
186   } else if (pipe_handle) {
187     client.reset(
188       new CrashGenerationClient(pipe_handle, dump_type_, custom_info));
189   }
190 
191   if (client.get() != NULL) {
192     // If successful in registering with the monitoring process,
193     // there is no need to setup in-process crash generation.
194     if (client->Register()) {
195       crash_generation_client_.reset(client.release());
196     }
197   }
198 
199   if (!IsOutOfProcess()) {
200     // Either client did not ask for out-of-process crash generation
201     // or registration with the server process failed. In either case,
202     // setup to do in-process crash generation.
203 
204     // Set synchronization primitives and the handler thread.  Each
205     // ExceptionHandler object gets its own handler thread because that's the
206     // only way to reliably guarantee sufficient stack space in an exception,
207     // and it allows an easy way to get a snapshot of the requesting thread's
208     // context outside of an exception.
209     InitializeCriticalSection(&handler_critical_section_);
210     handler_start_semaphore_ = CreateSemaphore(NULL, 0, 1, NULL);
211     assert(handler_start_semaphore_ != NULL);
212 
213     handler_finish_semaphore_ = CreateSemaphore(NULL, 0, 1, NULL);
214     assert(handler_finish_semaphore_ != NULL);
215 
216     // Don't attempt to create the thread if we could not create the semaphores.
217     if (handler_finish_semaphore_ != NULL && handler_start_semaphore_ != NULL) {
218       DWORD thread_id;
219       const int kExceptionHandlerThreadInitialStackSize = 64 * 1024;
220       handler_thread_ = CreateThread(NULL,         // lpThreadAttributes
221                                      kExceptionHandlerThreadInitialStackSize,
222                                      ExceptionHandlerThreadMain,
223                                      this,         // lpParameter
224                                      0,            // dwCreationFlags
225                                      &thread_id);
226       assert(handler_thread_ != NULL);
227     }
228 
229     dbghelp_module_ = LoadLibrary(L"dbghelp.dll");
230     if (dbghelp_module_) {
231       minidump_write_dump_ = reinterpret_cast<MiniDumpWriteDump_type>(
232           GetProcAddress(dbghelp_module_, "MiniDumpWriteDump"));
233     }
234 
235     // Load this library dynamically to not affect existing projects.  Most
236     // projects don't link against this directly, it's usually dynamically
237     // loaded by dependent code.
238     rpcrt4_module_ = LoadLibrary(L"rpcrt4.dll");
239     if (rpcrt4_module_) {
240       uuid_create_ = reinterpret_cast<UuidCreate_type>(
241           GetProcAddress(rpcrt4_module_, "UuidCreate"));
242     }
243 
244     // set_dump_path calls UpdateNextID.  This sets up all of the path and id
245     // strings, and their equivalent c_str pointers.
246     set_dump_path(dump_path);
247   }
248 
249   // Reserve one element for the instruction memory
250   AppMemory instruction_memory;
251   instruction_memory.ptr = NULL;
252   instruction_memory.length = 0;
253   app_memory_info_.push_back(instruction_memory);
254 
255   // There is a race condition here. If the first instance has not yet
256   // initialized the critical section, the second (and later) instances may
257   // try to use uninitialized critical section object. The feature of multiple
258   // instances in one module is not used much, so leave it as is for now.
259   // One way to solve this in the current design (that is, keeping the static
260   // handler stack) is to use spin locks with volatile bools to synchronize
261   // the handler stack. This works only if the compiler guarantees to generate
262   // cache coherent code for volatile.
263   // TODO(munjal): Fix this in a better way by changing the design if possible.
264 
265   // Lazy initialization of the handler_stack_critical_section_
266   if (instance_count == 1) {
267     InitializeCriticalSection(&handler_stack_critical_section_);
268   }
269 
270   if (handler_types != HANDLER_NONE) {
271     EnterCriticalSection(&handler_stack_critical_section_);
272 
273     // The first time an ExceptionHandler that installs a handler is
274     // created, set up the handler stack.
275     if (!handler_stack_) {
276       handler_stack_ = new vector<ExceptionHandler*>();
277     }
278     handler_stack_->push_back(this);
279 
280     if (handler_types & HANDLER_EXCEPTION)
281       previous_filter_ = SetUnhandledExceptionFilter(HandleException);
282 
283 #if _MSC_VER >= 1400  // MSVC 2005/8
284     if (handler_types & HANDLER_INVALID_PARAMETER)
285       previous_iph_ = _set_invalid_parameter_handler(HandleInvalidParameter);
286 #endif  // _MSC_VER >= 1400
287 
288     if (handler_types & HANDLER_PURECALL)
289       previous_pch_ = _set_purecall_handler(HandlePureVirtualCall);
290 
291     LeaveCriticalSection(&handler_stack_critical_section_);
292   }
293 }
294 
~ExceptionHandler()295 ExceptionHandler::~ExceptionHandler() {
296   if (dbghelp_module_) {
297     FreeLibrary(dbghelp_module_);
298   }
299 
300   if (rpcrt4_module_) {
301     FreeLibrary(rpcrt4_module_);
302   }
303 
304   if (handler_types_ != HANDLER_NONE) {
305     EnterCriticalSection(&handler_stack_critical_section_);
306 
307     if (handler_types_ & HANDLER_EXCEPTION)
308       SetUnhandledExceptionFilter(previous_filter_);
309 
310 #if _MSC_VER >= 1400  // MSVC 2005/8
311     if (handler_types_ & HANDLER_INVALID_PARAMETER)
312       _set_invalid_parameter_handler(previous_iph_);
313 #endif  // _MSC_VER >= 1400
314 
315     if (handler_types_ & HANDLER_PURECALL)
316       _set_purecall_handler(previous_pch_);
317 
318     if (handler_stack_->back() == this) {
319       handler_stack_->pop_back();
320     } else {
321       // TODO(mmentovai): use advapi32!ReportEvent to log the warning to the
322       // system's application event log.
323       fprintf(stderr, "warning: removing Breakpad handler out of order\n");
324       vector<ExceptionHandler*>::iterator iterator = handler_stack_->begin();
325       while (iterator != handler_stack_->end()) {
326         if (*iterator == this) {
327           iterator = handler_stack_->erase(iterator);
328         } else {
329           ++iterator;
330         }
331       }
332     }
333 
334     if (handler_stack_->empty()) {
335       // When destroying the last ExceptionHandler that installed a handler,
336       // clean up the handler stack.
337       delete handler_stack_;
338       handler_stack_ = NULL;
339     }
340 
341     LeaveCriticalSection(&handler_stack_critical_section_);
342   }
343 
344   // Some of the objects were only initialized if out of process
345   // registration was not done.
346   if (!IsOutOfProcess()) {
347 #ifdef BREAKPAD_NO_TERMINATE_THREAD
348     // Clean up the handler thread and synchronization primitives. The handler
349     // thread is either waiting on the semaphore to handle a crash or it is
350     // handling a crash. Coming out of the wait is fast but wait more in the
351     // eventuality a crash is handled.  This compilation option results in a
352     // deadlock if the exception handler is destroyed while executing code
353     // inside DllMain.
354     is_shutdown_ = true;
355     ReleaseSemaphore(handler_start_semaphore_, 1, NULL);
356     const int kWaitForHandlerThreadMs = 60000;
357     WaitForSingleObject(handler_thread_, kWaitForHandlerThreadMs);
358 #else
359     TerminateThread(handler_thread_, 1);
360 #endif  // BREAKPAD_NO_TERMINATE_THREAD
361 
362     CloseHandle(handler_thread_);
363     handler_thread_ = NULL;
364     DeleteCriticalSection(&handler_critical_section_);
365     CloseHandle(handler_start_semaphore_);
366     CloseHandle(handler_finish_semaphore_);
367   }
368 
369   // There is a race condition in the code below: if this instance is
370   // deleting the static critical section and a new instance of the class
371   // is created, then there is a possibility that the critical section be
372   // initialized while the same critical section is being deleted. Given the
373   // usage pattern for the code, this race condition is unlikely to hit, but it
374   // is a race condition nonetheless.
375   if (InterlockedDecrement(&instance_count_) == 0) {
376     DeleteCriticalSection(&handler_stack_critical_section_);
377   }
378 }
379 
RequestUpload(DWORD crash_id)380 bool ExceptionHandler::RequestUpload(DWORD crash_id) {
381   return crash_generation_client_->RequestUpload(crash_id);
382 }
383 
384 // static
ExceptionHandlerThreadMain(void * lpParameter)385 DWORD ExceptionHandler::ExceptionHandlerThreadMain(void* lpParameter) {
386   ExceptionHandler* self = reinterpret_cast<ExceptionHandler*>(lpParameter);
387   assert(self);
388   assert(self->handler_start_semaphore_ != NULL);
389   assert(self->handler_finish_semaphore_ != NULL);
390 
391   for (;;) {
392     if (WaitForSingleObject(self->handler_start_semaphore_, INFINITE) ==
393         WAIT_OBJECT_0) {
394       // Perform the requested action.
395       if (self->is_shutdown_) {
396         // The instance of the exception handler is being destroyed.
397         break;
398       } else {
399         self->handler_return_value_ =
400             self->WriteMinidumpWithException(self->requesting_thread_id_,
401                                              self->exception_info_,
402                                              self->assertion_);
403       }
404 
405       // Allow the requesting thread to proceed.
406       ReleaseSemaphore(self->handler_finish_semaphore_, 1, NULL);
407     }
408   }
409 
410   // This statement is not reached when the thread is unconditionally
411   // terminated by the ExceptionHandler destructor.
412   return 0;
413 }
414 
415 // HandleException and HandleInvalidParameter must create an
416 // AutoExceptionHandler object to maintain static state and to determine which
417 // ExceptionHandler instance to use.  The constructor locates the correct
418 // instance, and makes it available through get_handler().  The destructor
419 // restores the state in effect prior to allocating the AutoExceptionHandler.
420 class AutoExceptionHandler {
421  public:
AutoExceptionHandler()422   AutoExceptionHandler() {
423     // Increment handler_stack_index_ so that if another Breakpad handler is
424     // registered using this same HandleException function, and it needs to be
425     // called while this handler is running (either because this handler
426     // declines to handle the exception, or an exception occurs during
427     // handling), HandleException will find the appropriate ExceptionHandler
428     // object in handler_stack_ to deliver the exception to.
429     //
430     // Because handler_stack_ is addressed in reverse (as |size - index|),
431     // preincrementing handler_stack_index_ avoids needing to subtract 1 from
432     // the argument to |at|.
433     //
434     // The index is maintained instead of popping elements off of the handler
435     // stack and pushing them at the end of this method.  This avoids ruining
436     // the order of elements in the stack in the event that some other thread
437     // decides to manipulate the handler stack (such as creating a new
438     // ExceptionHandler object) while an exception is being handled.
439     EnterCriticalSection(&ExceptionHandler::handler_stack_critical_section_);
440     handler_ = ExceptionHandler::handler_stack_->at(
441         ExceptionHandler::handler_stack_->size() -
442         ++ExceptionHandler::handler_stack_index_);
443 
444     // In case another exception occurs while this handler is doing its thing,
445     // it should be delivered to the previous filter.
446     SetUnhandledExceptionFilter(handler_->previous_filter_);
447 #if _MSC_VER >= 1400  // MSVC 2005/8
448     _set_invalid_parameter_handler(handler_->previous_iph_);
449 #endif  // _MSC_VER >= 1400
450     _set_purecall_handler(handler_->previous_pch_);
451   }
452 
~AutoExceptionHandler()453   ~AutoExceptionHandler() {
454     // Put things back the way they were before entering this handler.
455     SetUnhandledExceptionFilter(ExceptionHandler::HandleException);
456 #if _MSC_VER >= 1400  // MSVC 2005/8
457     _set_invalid_parameter_handler(ExceptionHandler::HandleInvalidParameter);
458 #endif  // _MSC_VER >= 1400
459     _set_purecall_handler(ExceptionHandler::HandlePureVirtualCall);
460 
461     --ExceptionHandler::handler_stack_index_;
462     LeaveCriticalSection(&ExceptionHandler::handler_stack_critical_section_);
463   }
464 
get_handler() const465   ExceptionHandler* get_handler() const { return handler_; }
466 
467  private:
468   ExceptionHandler* handler_;
469 };
470 
471 // static
HandleException(EXCEPTION_POINTERS * exinfo)472 LONG ExceptionHandler::HandleException(EXCEPTION_POINTERS* exinfo) {
473   AutoExceptionHandler auto_exception_handler;
474   ExceptionHandler* current_handler = auto_exception_handler.get_handler();
475 
476   // Ignore EXCEPTION_BREAKPOINT and EXCEPTION_SINGLE_STEP exceptions.  This
477   // logic will short-circuit before calling WriteMinidumpOnHandlerThread,
478   // allowing something else to handle the breakpoint without incurring the
479   // overhead transitioning to and from the handler thread.  This behavior
480   // can be overridden by calling ExceptionHandler::set_handle_debug_exceptions.
481   DWORD code = exinfo->ExceptionRecord->ExceptionCode;
482   LONG action;
483   bool is_debug_exception = (code == EXCEPTION_BREAKPOINT) ||
484                             (code == EXCEPTION_SINGLE_STEP) ||
485                             (code == DBG_PRINTEXCEPTION_C) ||
486                             (code == DBG_PRINTEXCEPTION_WIDE_C);
487 
488   if (code == EXCEPTION_INVALID_HANDLE &&
489       current_handler->consume_invalid_handle_exceptions_) {
490     return EXCEPTION_CONTINUE_EXECUTION;
491   }
492 
493   bool success = false;
494 
495   if (!is_debug_exception ||
496       current_handler->get_handle_debug_exceptions()) {
497     // If out-of-proc crash handler client is available, we have to use that
498     // to generate dump and we cannot fall back on in-proc dump generation
499     // because we never prepared for an in-proc dump generation
500 
501     // In case of out-of-process dump generation, directly call
502     // WriteMinidumpWithException since there is no separate thread running.
503     if (current_handler->IsOutOfProcess()) {
504       success = current_handler->WriteMinidumpWithException(
505           GetCurrentThreadId(),
506           exinfo,
507           NULL);
508     } else {
509       success = current_handler->WriteMinidumpOnHandlerThread(exinfo, NULL);
510     }
511   }
512 
513   // The handler fully handled the exception.  Returning
514   // EXCEPTION_EXECUTE_HANDLER indicates this to the system, and usually
515   // results in the application being terminated.
516   //
517   // Note: If the application was launched from within the Cygwin
518   // environment, returning EXCEPTION_EXECUTE_HANDLER seems to cause the
519   // application to be restarted.
520   if (success) {
521     action = EXCEPTION_EXECUTE_HANDLER;
522   } else {
523     // There was an exception, it was a breakpoint or something else ignored
524     // above, or it was passed to the handler, which decided not to handle it.
525     // This could be because the filter callback didn't want it, because
526     // minidump writing failed for some reason, or because the post-minidump
527     // callback function indicated failure.  Give the previous handler a
528     // chance to do something with the exception.  If there is no previous
529     // handler, return EXCEPTION_CONTINUE_SEARCH, which will allow a debugger
530     // or native "crashed" dialog to handle the exception.
531     if (current_handler->previous_filter_) {
532       action = current_handler->previous_filter_(exinfo);
533     } else {
534       action = EXCEPTION_CONTINUE_SEARCH;
535     }
536   }
537 
538   return action;
539 }
540 
541 #if _MSC_VER >= 1400  // MSVC 2005/8
542 // static
HandleInvalidParameter(const wchar_t * expression,const wchar_t * function,const wchar_t * file,unsigned int line,uintptr_t reserved)543 void ExceptionHandler::HandleInvalidParameter(const wchar_t* expression,
544                                               const wchar_t* function,
545                                               const wchar_t* file,
546                                               unsigned int line,
547                                               uintptr_t reserved) {
548   // This is an invalid parameter, not an exception.  It's safe to play with
549   // sprintf here.
550   AutoExceptionHandler auto_exception_handler;
551   ExceptionHandler* current_handler = auto_exception_handler.get_handler();
552 
553   MDRawAssertionInfo assertion;
554   memset(&assertion, 0, sizeof(assertion));
555   _snwprintf_s(reinterpret_cast<wchar_t*>(assertion.expression),
556                sizeof(assertion.expression) / sizeof(assertion.expression[0]),
557                _TRUNCATE, L"%s", expression);
558   _snwprintf_s(reinterpret_cast<wchar_t*>(assertion.function),
559                sizeof(assertion.function) / sizeof(assertion.function[0]),
560                _TRUNCATE, L"%s", function);
561   _snwprintf_s(reinterpret_cast<wchar_t*>(assertion.file),
562                sizeof(assertion.file) / sizeof(assertion.file[0]),
563                _TRUNCATE, L"%s", file);
564   assertion.line = line;
565   assertion.type = MD_ASSERTION_INFO_TYPE_INVALID_PARAMETER;
566 
567   // Make up an exception record for the current thread and CPU context
568   // to make it possible for the crash processor to classify these
569   // as do regular crashes, and to make it humane for developers to
570   // analyze them.
571   EXCEPTION_RECORD exception_record = {};
572   CONTEXT exception_context = {};
573   EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };
574 
575   ::RtlCaptureContext(&exception_context);
576 
577   exception_record.ExceptionCode = STATUS_INVALID_PARAMETER;
578 
579   // We store pointers to the the expression and function strings,
580   // and the line as exception parameters to make them easy to
581   // access by the developer on the far side.
582   exception_record.NumberParameters = 3;
583   exception_record.ExceptionInformation[0] =
584       reinterpret_cast<ULONG_PTR>(&assertion.expression);
585   exception_record.ExceptionInformation[1] =
586       reinterpret_cast<ULONG_PTR>(&assertion.file);
587   exception_record.ExceptionInformation[2] = assertion.line;
588 
589   bool success = false;
590   // In case of out-of-process dump generation, directly call
591   // WriteMinidumpWithException since there is no separate thread running.
592   if (current_handler->IsOutOfProcess()) {
593     success = current_handler->WriteMinidumpWithException(
594         GetCurrentThreadId(),
595         &exception_ptrs,
596         &assertion);
597   } else {
598     success = current_handler->WriteMinidumpOnHandlerThread(&exception_ptrs,
599                                                             &assertion);
600   }
601 
602   if (!success) {
603     if (current_handler->previous_iph_) {
604       // The handler didn't fully handle the exception.  Give it to the
605       // previous invalid parameter handler.
606       current_handler->previous_iph_(expression,
607                                      function,
608                                      file,
609                                      line,
610                                      reserved);
611     } else {
612       // If there's no previous handler, pass the exception back in to the
613       // invalid parameter handler's core.  That's the routine that called this
614       // function, but now, since this function is no longer registered (and in
615       // fact, no function at all is registered), this will result in the
616       // default code path being taken: _CRT_DEBUGGER_HOOK and _invoke_watson.
617       // Use _invalid_parameter where it exists (in _DEBUG builds) as it passes
618       // more information through.  In non-debug builds, it is not available,
619       // so fall back to using _invalid_parameter_noinfo.  See invarg.c in the
620       // CRT source.
621 #ifdef _DEBUG
622       _invalid_parameter(expression, function, file, line, reserved);
623 #else  // _DEBUG
624       _invalid_parameter_noinfo();
625 #endif  // _DEBUG
626     }
627   }
628 
629   // The handler either took care of the invalid parameter problem itself,
630   // or passed it on to another handler.  "Swallow" it by exiting, paralleling
631   // the behavior of "swallowing" exceptions.
632   exit(0);
633 }
634 #endif  // _MSC_VER >= 1400
635 
636 // static
HandlePureVirtualCall()637 void ExceptionHandler::HandlePureVirtualCall() {
638   // This is an pure virtual function call, not an exception.  It's safe to
639   // play with sprintf here.
640   AutoExceptionHandler auto_exception_handler;
641   ExceptionHandler* current_handler = auto_exception_handler.get_handler();
642 
643   MDRawAssertionInfo assertion;
644   memset(&assertion, 0, sizeof(assertion));
645   assertion.type = MD_ASSERTION_INFO_TYPE_PURE_VIRTUAL_CALL;
646 
647   // Make up an exception record for the current thread and CPU context
648   // to make it possible for the crash processor to classify these
649   // as do regular crashes, and to make it humane for developers to
650   // analyze them.
651   EXCEPTION_RECORD exception_record = {};
652   CONTEXT exception_context = {};
653   EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };
654 
655   ::RtlCaptureContext(&exception_context);
656 
657   exception_record.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
658 
659   // We store pointers to the the expression and function strings,
660   // and the line as exception parameters to make them easy to
661   // access by the developer on the far side.
662   exception_record.NumberParameters = 3;
663   exception_record.ExceptionInformation[0] =
664       reinterpret_cast<ULONG_PTR>(&assertion.expression);
665   exception_record.ExceptionInformation[1] =
666       reinterpret_cast<ULONG_PTR>(&assertion.file);
667   exception_record.ExceptionInformation[2] = assertion.line;
668 
669   bool success = false;
670   // In case of out-of-process dump generation, directly call
671   // WriteMinidumpWithException since there is no separate thread running.
672 
673   if (current_handler->IsOutOfProcess()) {
674     success = current_handler->WriteMinidumpWithException(
675         GetCurrentThreadId(),
676         &exception_ptrs,
677         &assertion);
678   } else {
679     success = current_handler->WriteMinidumpOnHandlerThread(&exception_ptrs,
680                                                             &assertion);
681   }
682 
683   if (!success) {
684     if (current_handler->previous_pch_) {
685       // The handler didn't fully handle the exception.  Give it to the
686       // previous purecall handler.
687       current_handler->previous_pch_();
688     } else {
689       // If there's no previous handler, return and let _purecall handle it.
690       // This will just put up an assertion dialog.
691       return;
692     }
693   }
694 
695   // The handler either took care of the invalid parameter problem itself,
696   // or passed it on to another handler.  "Swallow" it by exiting, paralleling
697   // the behavior of "swallowing" exceptions.
698   exit(0);
699 }
700 
WriteMinidumpOnHandlerThread(EXCEPTION_POINTERS * exinfo,MDRawAssertionInfo * assertion)701 bool ExceptionHandler::WriteMinidumpOnHandlerThread(
702     EXCEPTION_POINTERS* exinfo, MDRawAssertionInfo* assertion) {
703   EnterCriticalSection(&handler_critical_section_);
704 
705   // There isn't much we can do if the handler thread
706   // was not successfully created.
707   if (handler_thread_ == NULL) {
708     LeaveCriticalSection(&handler_critical_section_);
709     return false;
710   }
711 
712   // The handler thread should only be created when the semaphores are valid.
713   assert(handler_start_semaphore_ != NULL);
714   assert(handler_finish_semaphore_ != NULL);
715 
716   // Set up data to be passed in to the handler thread.
717   requesting_thread_id_ = GetCurrentThreadId();
718   exception_info_ = exinfo;
719   assertion_ = assertion;
720 
721   // This causes the handler thread to call WriteMinidumpWithException.
722   ReleaseSemaphore(handler_start_semaphore_, 1, NULL);
723 
724   // Wait until WriteMinidumpWithException is done and collect its return value.
725   WaitForSingleObject(handler_finish_semaphore_, INFINITE);
726   bool status = handler_return_value_;
727 
728   // Clean up.
729   requesting_thread_id_ = 0;
730   exception_info_ = NULL;
731   assertion_ = NULL;
732 
733   LeaveCriticalSection(&handler_critical_section_);
734 
735   return status;
736 }
737 
WriteMinidump()738 bool ExceptionHandler::WriteMinidump() {
739   // Make up an exception record for the current thread and CPU context
740   // to make it possible for the crash processor to classify these
741   // as do regular crashes, and to make it humane for developers to
742   // analyze them.
743   EXCEPTION_RECORD exception_record = {};
744   CONTEXT exception_context = {};
745   EXCEPTION_POINTERS exception_ptrs = { &exception_record, &exception_context };
746 
747   ::RtlCaptureContext(&exception_context);
748   exception_record.ExceptionCode = STATUS_NONCONTINUABLE_EXCEPTION;
749 
750   return WriteMinidumpForException(&exception_ptrs);
751 }
752 
WriteMinidumpForException(EXCEPTION_POINTERS * exinfo)753 bool ExceptionHandler::WriteMinidumpForException(EXCEPTION_POINTERS* exinfo) {
754   // In case of out-of-process dump generation, directly call
755   // WriteMinidumpWithException since there is no separate thread running.
756   if (IsOutOfProcess()) {
757     return WriteMinidumpWithException(GetCurrentThreadId(),
758                                       exinfo,
759                                       NULL);
760   }
761 
762   bool success = WriteMinidumpOnHandlerThread(exinfo, NULL);
763   UpdateNextID();
764   return success;
765 }
766 
767 // static
WriteMinidump(const wstring & dump_path,MinidumpCallback callback,void * callback_context,MINIDUMP_TYPE dump_type)768 bool ExceptionHandler::WriteMinidump(const wstring& dump_path,
769                                      MinidumpCallback callback,
770                                      void* callback_context,
771                                      MINIDUMP_TYPE dump_type) {
772   ExceptionHandler handler(dump_path, NULL, callback, callback_context,
773                            HANDLER_NONE, dump_type, (HANDLE)NULL, NULL);
774   return handler.WriteMinidump();
775 }
776 
777 // static
WriteMinidumpForChild(HANDLE child,DWORD child_blamed_thread,const wstring & dump_path,MinidumpCallback callback,void * callback_context,MINIDUMP_TYPE dump_type)778 bool ExceptionHandler::WriteMinidumpForChild(HANDLE child,
779                                              DWORD child_blamed_thread,
780                                              const wstring& dump_path,
781                                              MinidumpCallback callback,
782                                              void* callback_context,
783                                              MINIDUMP_TYPE dump_type) {
784   EXCEPTION_RECORD ex;
785   CONTEXT ctx;
786   EXCEPTION_POINTERS exinfo = { NULL, NULL };
787   // As documented on MSDN, on failure SuspendThread returns (DWORD) -1
788   const DWORD kFailedToSuspendThread = static_cast<DWORD>(-1);
789   DWORD last_suspend_count = kFailedToSuspendThread;
790   HANDLE child_thread_handle = OpenThread(THREAD_GET_CONTEXT |
791                                           THREAD_QUERY_INFORMATION |
792                                           THREAD_SUSPEND_RESUME,
793                                           FALSE,
794                                           child_blamed_thread);
795   // This thread may have died already, so not opening the handle is a
796   // non-fatal error.
797   if (child_thread_handle != NULL) {
798     last_suspend_count = SuspendThread(child_thread_handle);
799     if (last_suspend_count != kFailedToSuspendThread) {
800       ctx.ContextFlags = CONTEXT_ALL;
801       if (GetThreadContext(child_thread_handle, &ctx)) {
802         memset(&ex, 0, sizeof(ex));
803         ex.ExceptionCode = EXCEPTION_BREAKPOINT;
804 #if defined(_M_IX86)
805         ex.ExceptionAddress = reinterpret_cast<PVOID>(ctx.Eip);
806 #elif defined(_M_X64)
807         ex.ExceptionAddress = reinterpret_cast<PVOID>(ctx.Rip);
808 #endif
809         exinfo.ExceptionRecord = &ex;
810         exinfo.ContextRecord = &ctx;
811       }
812     }
813   }
814 
815   ExceptionHandler handler(dump_path, NULL, callback, callback_context,
816                            HANDLER_NONE, dump_type, (HANDLE)NULL, NULL);
817   bool success = handler.WriteMinidumpWithExceptionForProcess(
818       child_blamed_thread,
819       exinfo.ExceptionRecord ? &exinfo : NULL,
820       NULL, child, false);
821 
822   if (last_suspend_count != kFailedToSuspendThread) {
823     ResumeThread(child_thread_handle);
824   }
825 
826   CloseHandle(child_thread_handle);
827 
828   if (callback) {
829     success = callback(handler.dump_path_c_, handler.next_minidump_id_c_,
830                        callback_context, NULL, NULL, success);
831   }
832 
833   return success;
834 }
835 
WriteMinidumpWithException(DWORD requesting_thread_id,EXCEPTION_POINTERS * exinfo,MDRawAssertionInfo * assertion)836 bool ExceptionHandler::WriteMinidumpWithException(
837     DWORD requesting_thread_id,
838     EXCEPTION_POINTERS* exinfo,
839     MDRawAssertionInfo* assertion) {
840   // Give user code a chance to approve or prevent writing a minidump.  If the
841   // filter returns false, don't handle the exception at all.  If this method
842   // was called as a result of an exception, returning false will cause
843   // HandleException to call any previous handler or return
844   // EXCEPTION_CONTINUE_SEARCH on the exception thread, allowing it to appear
845   // as though this handler were not present at all.
846   if (filter_ && !filter_(callback_context_, exinfo, assertion)) {
847     return false;
848   }
849 
850   bool success = false;
851   if (IsOutOfProcess()) {
852     success = crash_generation_client_->RequestDump(exinfo, assertion);
853   } else {
854     success = WriteMinidumpWithExceptionForProcess(requesting_thread_id,
855                                                    exinfo,
856                                                    assertion,
857                                                    GetCurrentProcess(),
858                                                    true);
859   }
860 
861   if (callback_) {
862     // TODO(munjal): In case of out-of-process dump generation, both
863     // dump_path_c_ and next_minidump_id_ will be NULL. For out-of-process
864     // scenario, the server process ends up creating the dump path and dump
865     // id so they are not known to the client.
866     success = callback_(dump_path_c_, next_minidump_id_c_, callback_context_,
867                         exinfo, assertion, success);
868   }
869 
870   return success;
871 }
872 
873 // static
MinidumpWriteDumpCallback(PVOID context,const PMINIDUMP_CALLBACK_INPUT callback_input,PMINIDUMP_CALLBACK_OUTPUT callback_output)874 BOOL CALLBACK ExceptionHandler::MinidumpWriteDumpCallback(
875     PVOID context,
876     const PMINIDUMP_CALLBACK_INPUT callback_input,
877     PMINIDUMP_CALLBACK_OUTPUT callback_output) {
878   switch (callback_input->CallbackType) {
879   case MemoryCallback: {
880     MinidumpCallbackContext* callback_context =
881         reinterpret_cast<MinidumpCallbackContext*>(context);
882     if (callback_context->iter == callback_context->end)
883       return FALSE;
884 
885     // Include the specified memory region.
886     callback_output->MemoryBase = callback_context->iter->ptr;
887     callback_output->MemorySize = callback_context->iter->length;
888     callback_context->iter++;
889     return TRUE;
890   }
891 
892     // Include all modules.
893   case IncludeModuleCallback:
894   case ModuleCallback:
895     return TRUE;
896 
897     // Include all threads.
898   case IncludeThreadCallback:
899   case ThreadCallback:
900     return TRUE;
901 
902     // Stop receiving cancel callbacks.
903   case CancelCallback:
904     callback_output->CheckCancel = FALSE;
905     callback_output->Cancel = FALSE;
906     return TRUE;
907   }
908   // Ignore other callback types.
909   return FALSE;
910 }
911 
WriteMinidumpWithExceptionForProcess(DWORD requesting_thread_id,EXCEPTION_POINTERS * exinfo,MDRawAssertionInfo * assertion,HANDLE process,bool write_requester_stream)912 bool ExceptionHandler::WriteMinidumpWithExceptionForProcess(
913     DWORD requesting_thread_id,
914     EXCEPTION_POINTERS* exinfo,
915     MDRawAssertionInfo* assertion,
916     HANDLE process,
917     bool write_requester_stream) {
918   bool success = false;
919   if (minidump_write_dump_) {
920     HANDLE dump_file = CreateFile(next_minidump_path_c_,
921                                   GENERIC_WRITE,
922                                   0,  // no sharing
923                                   NULL,
924                                   CREATE_NEW,  // fail if exists
925                                   FILE_ATTRIBUTE_NORMAL,
926                                   NULL);
927     if (dump_file != INVALID_HANDLE_VALUE) {
928       MINIDUMP_EXCEPTION_INFORMATION except_info;
929       except_info.ThreadId = requesting_thread_id;
930       except_info.ExceptionPointers = exinfo;
931       except_info.ClientPointers = FALSE;
932 
933       // Leave room in user_stream_array for possible breakpad and
934       // assertion info streams.
935       MINIDUMP_USER_STREAM user_stream_array[2];
936       MINIDUMP_USER_STREAM_INFORMATION user_streams;
937       user_streams.UserStreamCount = 0;
938       user_streams.UserStreamArray = user_stream_array;
939 
940       if (write_requester_stream) {
941         // Add an MDRawBreakpadInfo stream to the minidump, to provide
942         // additional information about the exception handler to the Breakpad
943         // processor. The information will help the processor determine which
944         // threads are relevant.  The Breakpad processor does not require this
945         // information but can function better with Breakpad-generated dumps
946         // when it is present. The native debugger is not harmed by the
947         // presence of this information.
948         MDRawBreakpadInfo breakpad_info;
949         breakpad_info.validity = MD_BREAKPAD_INFO_VALID_DUMP_THREAD_ID |
950                                  MD_BREAKPAD_INFO_VALID_REQUESTING_THREAD_ID;
951         breakpad_info.dump_thread_id = GetCurrentThreadId();
952         breakpad_info.requesting_thread_id = requesting_thread_id;
953 
954         int index = user_streams.UserStreamCount;
955         user_stream_array[index].Type = MD_BREAKPAD_INFO_STREAM;
956         user_stream_array[index].BufferSize = sizeof(breakpad_info);
957         user_stream_array[index].Buffer = &breakpad_info;
958         ++user_streams.UserStreamCount;
959       }
960 
961       if (assertion) {
962         int index = user_streams.UserStreamCount;
963         user_stream_array[index].Type = MD_ASSERTION_INFO_STREAM;
964         user_stream_array[index].BufferSize = sizeof(MDRawAssertionInfo);
965         user_stream_array[index].Buffer = assertion;
966         ++user_streams.UserStreamCount;
967       }
968 
969       // Older versions of DbgHelp.dll don't correctly put the memory around
970       // the faulting instruction pointer into the minidump. This
971       // callback will ensure that it gets included.
972       if (exinfo) {
973         // Find a memory region of 256 bytes centered on the
974         // faulting instruction pointer.
975         const ULONG64 instruction_pointer =
976 #if defined(_M_IX86)
977           exinfo->ContextRecord->Eip;
978 #elif defined(_M_AMD64)
979           exinfo->ContextRecord->Rip;
980 #elif defined(_M_ARM64)
981           exinfo->ContextRecord->Pc;
982 #else
983 #error Unsupported platform
984 #endif
985 
986         MEMORY_BASIC_INFORMATION info;
987         if (VirtualQueryEx(process,
988                            reinterpret_cast<LPCVOID>(instruction_pointer),
989                            &info,
990                            sizeof(MEMORY_BASIC_INFORMATION)) != 0 &&
991             info.State == MEM_COMMIT) {
992           // Attempt to get 128 bytes before and after the instruction
993           // pointer, but settle for whatever's available up to the
994           // boundaries of the memory region.
995           const ULONG64 kIPMemorySize = 256;
996           ULONG64 base =
997             (std::max)(reinterpret_cast<ULONG64>(info.BaseAddress),
998                        instruction_pointer - (kIPMemorySize / 2));
999           ULONG64 end_of_range =
1000             (std::min)(instruction_pointer + (kIPMemorySize / 2),
1001                        reinterpret_cast<ULONG64>(info.BaseAddress)
1002                        + info.RegionSize);
1003           ULONG size = static_cast<ULONG>(end_of_range - base);
1004 
1005           AppMemory& elt = app_memory_info_.front();
1006           elt.ptr = base;
1007           elt.length = size;
1008         }
1009       }
1010 
1011       MinidumpCallbackContext context;
1012       context.iter = app_memory_info_.begin();
1013       context.end = app_memory_info_.end();
1014 
1015       // Skip the reserved element if there was no instruction memory
1016       if (context.iter->ptr == 0) {
1017         context.iter++;
1018       }
1019 
1020       MINIDUMP_CALLBACK_INFORMATION callback;
1021       callback.CallbackRoutine = MinidumpWriteDumpCallback;
1022       callback.CallbackParam = reinterpret_cast<void*>(&context);
1023 
1024       // The explicit comparison to TRUE avoids a warning (C4800).
1025       success = (minidump_write_dump_(process,
1026                                       GetProcessId(process),
1027                                       dump_file,
1028                                       dump_type_,
1029                                       exinfo ? &except_info : NULL,
1030                                       &user_streams,
1031                                       &callback) == TRUE);
1032 
1033       CloseHandle(dump_file);
1034     }
1035   }
1036 
1037   return success;
1038 }
1039 
UpdateNextID()1040 void ExceptionHandler::UpdateNextID() {
1041   assert(uuid_create_);
1042   UUID id = {0};
1043   if (uuid_create_) {
1044     uuid_create_(&id);
1045   }
1046   next_minidump_id_ = GUIDString::GUIDToWString(&id);
1047   next_minidump_id_c_ = next_minidump_id_.c_str();
1048 
1049   wchar_t minidump_path[MAX_PATH];
1050   swprintf(minidump_path, MAX_PATH, L"%s\\%s.dmp",
1051            dump_path_c_, next_minidump_id_c_);
1052 
1053   // remove when VC++7.1 is no longer supported
1054   minidump_path[MAX_PATH - 1] = L'\0';
1055 
1056   next_minidump_path_ = minidump_path;
1057   next_minidump_path_c_ = next_minidump_path_.c_str();
1058 }
1059 
RegisterAppMemory(void * ptr,size_t length)1060 void ExceptionHandler::RegisterAppMemory(void* ptr, size_t length) {
1061   AppMemoryList::iterator iter =
1062     std::find(app_memory_info_.begin(), app_memory_info_.end(), ptr);
1063   if (iter != app_memory_info_.end()) {
1064     // Don't allow registering the same pointer twice.
1065     return;
1066   }
1067 
1068   AppMemory app_memory;
1069   app_memory.ptr = reinterpret_cast<ULONG64>(ptr);
1070   app_memory.length = static_cast<ULONG>(length);
1071   app_memory_info_.push_back(app_memory);
1072 }
1073 
UnregisterAppMemory(void * ptr)1074 void ExceptionHandler::UnregisterAppMemory(void* ptr) {
1075   AppMemoryList::iterator iter =
1076     std::find(app_memory_info_.begin(), app_memory_info_.end(), ptr);
1077   if (iter != app_memory_info_.end()) {
1078     app_memory_info_.erase(iter);
1079   }
1080 }
1081 
1082 }  // namespace google_breakpad
1083