toolkit/crashreporter/test/nsTestCrasher.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:c389e1bdb72c
1 #include "mozilla/Assertions.h"
2
3 #include <stdio.h>
4
5 #include "nscore.h"
6 #include "nsXULAppAPI.h"
7 #include "nsExceptionHandler.h"
8 #include "mozilla/unused.h"
9
10 /*
11 * This pure virtual call example is from MSDN
12 */
13 class A;
14
15 void fcn( A* );
16
17 class A
18 {
19 public:
20 virtual void f() = 0;
21 A() { fcn( this ); }
22 };
23
24 class B : A
25 {
26 void f() { }
27 public:
28 void use() { }
29 };
30
31 void fcn( A* p )
32 {
33 p->f();
34 }
35
36 void PureVirtualCall()
37 {
38 // generates a pure virtual function call
39 B b;
40 b.use(); // make sure b's actually used
41 }
42
43 // Keep these in sync with CrashTestUtils.jsm!
44 const int16_t CRASH_INVALID_POINTER_DEREF = 0;
45 const int16_t CRASH_PURE_VIRTUAL_CALL = 1;
46 const int16_t CRASH_RUNTIMEABORT = 2;
47 const int16_t CRASH_OOM = 3;
48 const int16_t CRASH_MOZ_CRASH = 4;
49 const int16_t CRASH_ABORT = 5;
50
51 extern "C" NS_EXPORT
52 void Crash(int16_t how)
53 {
54 switch (how) {
55 case CRASH_INVALID_POINTER_DEREF: {
56 volatile int* foo = (int*)0x42;
57 *foo = 0;
58 // not reached
59 break;
60 }
61 case CRASH_PURE_VIRTUAL_CALL: {
62 PureVirtualCall();
63 // not reached
64 break;
65 }
66 case CRASH_RUNTIMEABORT: {
67 NS_RUNTIMEABORT("Intentional crash");
68 break;
69 }
70 case CRASH_OOM: {
71 mozilla::unused << moz_xmalloc((size_t) -1);
72 mozilla::unused << moz_xmalloc((size_t) -1);
73 mozilla::unused << moz_xmalloc((size_t) -1);
74 break;
75 }
76 case CRASH_MOZ_CRASH: {
77 MOZ_CRASH();
78 break;
79 }
80 case CRASH_ABORT: {
81 abort();
82 break;
83 }
84 default:
85 break;
86 }
87 }
88
89 extern "C" NS_EXPORT
90 nsISupports* LockDir(nsIFile *directory)
91 {
92 nsISupports* lockfile = nullptr;
93 XRE_LockProfileDirectory(directory, &lockfile);
94 return lockfile;
95 }
96
97 char testData[32];
98
99 extern "C" NS_EXPORT
100 uint64_t SaveAppMemory()
101 {
102 for (size_t i=0; i<sizeof(testData); i++)
103 testData[i] = i;
104
105 FILE *fp = fopen("crash-addr", "w");
106 if (!fp)
107 return 0;
108 fprintf(fp, "%p\n", (void *)testData);
109 fclose(fp);
110
111 return (int64_t)testData;
112 }
113
114 #ifdef XP_WIN32
115 static LONG WINAPI HandleException(EXCEPTION_POINTERS* exinfo)
116 {
117 TerminateProcess(GetCurrentProcess(), 0);
118 return 0;
119 }
120
121 extern "C" NS_EXPORT
122 void TryOverrideExceptionHandler()
123 {
124 SetUnhandledExceptionFilter(HandleException);
125 }
126 #endif

mercurial