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