aboutsummaryrefslogtreecommitdiff
path: root/src/client/windows/unittests/exception_handler_nesting_test.cc
blob: e24bd18b6e9908579450dab222d39a1612f53318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2012, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <windows.h>

#include <string>

#include "breakpad_googletest_includes.h"
#include "client/windows/handler/exception_handler.h"
#include "client/windows/unittests/exception_handler_test.h"

namespace {

const char kFoo[] = "foo";
const char kBar[] = "bar";

const char kStartOfLine[] = "^";
const char kEndOfLine[] = "$";

const char kFilterReturnsTrue[] = "filter_returns_true";
const char kFilterReturnsFalse[] = "filter_returns_false";

const char kCallbackReturnsTrue[] = "callback_returns_true";
const char kCallbackReturnsFalse[] = "callback_returns_false";

bool DoesPathExist(const wchar_t* path_name) {
  DWORD flags = GetFileAttributes(path_name);
  if (flags == INVALID_FILE_ATTRIBUTES) {
    return false;
  }
  return true;
}

// A callback function to run before Breakpad performs any substantial
// processing of an exception.  A FilterCallback is called before writing
// a minidump.  context is the parameter supplied by the user as
// callback_context when the handler was created.  exinfo points to the
// exception record, if any; assertion points to assertion information,
// if any.
//
// If a FilterCallback returns true, Breakpad will continue processing,
// attempting to write a minidump.  If a FilterCallback returns false,
// Breakpad will immediately report the exception as unhandled without
// writing a minidump, allowing another handler the opportunity to handle it.
template <bool filter_return_value>
bool CrashHandlerFilter(void* context,
                        EXCEPTION_POINTERS* exinfo,
                        MDRawAssertionInfo* assertion) {
  if (filter_return_value) {
    fprintf(stderr, kFilterReturnsTrue);
  } else {
    fprintf(stderr, kFilterReturnsFalse);
  }
  fflush(stderr);

  return filter_return_value;
}

// A callback function to run after the minidump has been written.
// minidump_id is a unique id for the dump, so the minidump
// file is <dump_path>\<minidump_id>.dmp.  context is the parameter supplied
// by the user as callback_context when the handler was created.  exinfo
// points to the exception record, or NULL if no exception occurred.
// succeeded indicates whether a minidump file was successfully written.
// assertion points to information about an assertion if the handler was
// invoked by an assertion.
//
// If an exception occurred and the callback returns true, Breakpad will treat
// the exception as fully-handled, suppressing any other handlers from being
// notified of the exception.  If the callback returns false, Breakpad will
// treat the exception as unhandled, and allow another handler to handle it.
// If there are no other handlers, Breakpad will report the exception to the
// system as unhandled, allowing a debugger or native crash dialog the
// opportunity to handle the exception.  Most callback implementations
// should normally return the value of |succeeded|, or when they wish to
// not report an exception of handled, false.  Callbacks will rarely want to
// return true directly (unless |succeeded| is true).
//
// For out-of-process dump generation, dump path and minidump ID will always
// be NULL. In case of out-of-process dump generation, the dump path and
// minidump id are controlled by the server process and are not communicated
// back to the crashing process.
template <bool callback_return_value>
bool MinidumpWrittenCallback(const wchar_t* dump_path,
                             const wchar_t* minidump_id,
                             void* context,
                             EXCEPTION_POINTERS* exinfo,
                             MDRawAssertionInfo* assertion,
                             bool succeeded) {
  bool rv = false;
  if (callback_return_value &&
      succeeded &&
      DoesPathExist(dump_path)) {
    rv = true;
    fprintf(stderr, kCallbackReturnsTrue);
  } else {
    fprintf(stderr, kCallbackReturnsFalse);
  }
  fflush(stderr);

  return rv;
}


void DoCrash(const char* message) {
  if (message) {
    fprintf(stderr, "%s", message);
    fflush(stderr);
  }
  int* i = NULL;
  (*i)++;

  ASSERT_TRUE(false);
}

void InstallExceptionHandlerAndCrash(bool install_filter,
                                     bool filter_return_value,
                                     bool install_callback,
                                     bool callback_return_value) {
  wchar_t temp_path[MAX_PATH] = { '\0' };
  GetTempPath(MAX_PATH, temp_path);

  ASSERT_TRUE(DoesPathExist(temp_path));
  google_breakpad::ExceptionHandler exc(
      temp_path,
      install_filter ?
        (filter_return_value ?
          &CrashHandlerFilter<true> :
          &CrashHandlerFilter<false>) :
        NULL,
      install_callback ?
        (callback_return_value ?
          &MinidumpWrittenCallback<true> :
          &MinidumpWrittenCallback<false>) :
        NULL,
      NULL,  // callback_context
      google_breakpad::ExceptionHandler::HANDLER_EXCEPTION);

  // Disable GTest SEH handler
  testing::DisableExceptionHandlerInScope disable_exception_handler;

  DoCrash(NULL);
}

TEST(AssertDeathSanity, Simple) {
  ASSERT_DEATH(DoCrash(NULL), "");
}

TEST(AssertDeathSanity, Regex) {
  ASSERT_DEATH(DoCrash(kFoo),
    std::string(kStartOfLine) +
      std::string(kFoo) +
      std::string(kEndOfLine));

  ASSERT_DEATH(DoCrash(kBar),
    std::string(kStartOfLine) +
      std::string(kBar) +
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerCallbacks, FilterTrue_No_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,    // install_filter
                                    true,    // filter_return_value
                                    false,   // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsTrue) +
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerCallbacks, FilterTrue_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,    // install_filter
                                    true,    // filter_return_value
                                    true,    // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsTrue) +
      std::string(kCallbackReturnsFalse) +
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerCallbacks, FilterFalse_No_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,    // install_filter
                                    false,   // filter_return_value
                                    false,   // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsFalse) +
      std::string(kEndOfLine));
}

// Callback shouldn't be executed when filter returns false
TEST(ExceptionHandlerCallbacks, FilterFalse_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,    // install_filter
                                    false,   // filter_return_value
                                    true,    // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsFalse) +
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerCallbacks, No_Filter_No_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(false,   // install_filter
                                    true,    // filter_return_value
                                    false,   // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerCallbacks, No_Filter_Callback) {
  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(false,   // install_filter
                                    true,    // filter_return_value
                                    true,    // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kCallbackReturnsFalse) +
      std::string(kEndOfLine));
}


TEST(ExceptionHandlerNesting, Skip_From_Inner_Filter) {
  wchar_t temp_path[MAX_PATH] = { '\0' };
  GetTempPath(MAX_PATH, temp_path);

  ASSERT_TRUE(DoesPathExist(temp_path));
  google_breakpad::ExceptionHandler exc(
      temp_path,
      &CrashHandlerFilter<true>,
      &MinidumpWrittenCallback<false>,
      NULL,  // callback_context
      google_breakpad::ExceptionHandler::HANDLER_EXCEPTION);

  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,   // install_filter
                                    false,  // filter_return_value
                                    true,   // install_callback
                                    true),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsFalse) +    // inner filter
      std::string(kFilterReturnsTrue) +     // outer filter
      std::string(kCallbackReturnsFalse) +  // outer callback
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerNesting, Skip_From_Inner_Callback) {
  wchar_t temp_path[MAX_PATH] = { '\0' };
  GetTempPath(MAX_PATH, temp_path);

  ASSERT_TRUE(DoesPathExist(temp_path));
  google_breakpad::ExceptionHandler exc(
      temp_path,
      &CrashHandlerFilter<true>,
      &MinidumpWrittenCallback<false>,
      NULL,  // callback_context
      google_breakpad::ExceptionHandler::HANDLER_EXCEPTION);

  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,    // install_filter
                                    true,    // filter_return_value
                                    true,    // install_callback
                                    false),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsTrue) +      // inner filter
      std::string(kCallbackReturnsFalse) +   // inner callback
      std::string(kFilterReturnsTrue) +      // outer filter
      std::string(kCallbackReturnsFalse) +   // outer callback
      std::string(kEndOfLine));
}

TEST(ExceptionHandlerNesting, Handled_By_Inner_Handler) {
  wchar_t temp_path[MAX_PATH] = { '\0' };
  GetTempPath(MAX_PATH, temp_path);

  ASSERT_TRUE(DoesPathExist(temp_path));
  google_breakpad::ExceptionHandler exc(
      temp_path,
      &CrashHandlerFilter<true>,
      &MinidumpWrittenCallback<true>,
      NULL,  // callback_context
      google_breakpad::ExceptionHandler::HANDLER_EXCEPTION);

  ASSERT_DEATH(
    InstallExceptionHandlerAndCrash(true,   // install_filter
                                    true,   // filter_return_value
                                    true,   // install_callback
                                    true),  // callback_return_value
    std::string(kStartOfLine) +
      std::string(kFilterReturnsTrue) +    // inner filter
      std::string(kCallbackReturnsTrue) +  // inner callback
      std::string(kEndOfLine));
}

}  // namespace