Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006, Google Inc. |
michael@0 | 2 | // All rights reserved. |
michael@0 | 3 | // |
michael@0 | 4 | // Redistribution and use in source and binary forms, with or without |
michael@0 | 5 | // modification, are permitted provided that the following conditions are |
michael@0 | 6 | // met: |
michael@0 | 7 | // |
michael@0 | 8 | // * Redistributions of source code must retain the above copyright |
michael@0 | 9 | // notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | // * Redistributions in binary form must reproduce the above |
michael@0 | 11 | // copyright notice, this list of conditions and the following disclaimer |
michael@0 | 12 | // in the documentation and/or other materials provided with the |
michael@0 | 13 | // distribution. |
michael@0 | 14 | // * Neither the name of Google Inc. nor the names of its |
michael@0 | 15 | // contributors may be used to endorse or promote products derived from |
michael@0 | 16 | // this software without specific prior written permission. |
michael@0 | 17 | // |
michael@0 | 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
michael@0 | 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
michael@0 | 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
michael@0 | 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
michael@0 | 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
michael@0 | 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 29 | |
michael@0 | 30 | #import <Breakpad/Breakpad.h> |
michael@0 | 31 | |
michael@0 | 32 | #import "Controller.h" |
michael@0 | 33 | #import "TestClass.h" |
michael@0 | 34 | #import "GTMDefines.h" |
michael@0 | 35 | #include <unistd.h> |
michael@0 | 36 | #include <mach/mach.h> |
michael@0 | 37 | |
michael@0 | 38 | @implementation Controller |
michael@0 | 39 | |
michael@0 | 40 | - (void)causeCrash { |
michael@0 | 41 | float *aPtr = nil; |
michael@0 | 42 | NSLog(@"Crash!"); |
michael@0 | 43 | NSLog(@"Bad programmer: %f", *aPtr); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | - (void)generateReportWithoutCrash:(id)sender { |
michael@0 | 47 | BreakpadGenerateAndSendReport(breakpad_); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | - (IBAction)showForkTestWindow:(id) sender { |
michael@0 | 51 | [forkTestOptions_ setIsVisible:YES]; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | - (IBAction)forkTestOptions:(id)sender { |
michael@0 | 55 | NSInteger tag = [[sender selectedCell] tag]; |
michael@0 | 56 | NSLog(@"sender tag: %d", tag); |
michael@0 | 57 | if (tag <= 2) { |
michael@0 | 58 | bpForkOption = tag; |
michael@0 | 59 | } |
michael@0 | 60 | |
michael@0 | 61 | if (tag == 3) { |
michael@0 | 62 | useVFork = NO; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | if (tag == 4) { |
michael@0 | 66 | useVFork = YES; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | if (tag >= 5 && tag <= 7) { |
michael@0 | 70 | progCrashPoint = tag; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | - (IBAction)forkTestGo:(id)sender { |
michael@0 | 76 | |
michael@0 | 77 | NSString *resourcePath = [[NSBundle bundleForClass: |
michael@0 | 78 | [self class]] resourcePath]; |
michael@0 | 79 | NSString *execProgname = nil; |
michael@0 | 80 | if (progCrashPoint == DURINGLAUNCH) { |
michael@0 | 81 | execProgname = [resourcePath stringByAppendingString:@"/crashduringload"]; |
michael@0 | 82 | } else if (progCrashPoint == AFTERLAUNCH) { |
michael@0 | 83 | execProgname = [resourcePath stringByAppendingString:@"/crashInMain"]; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | const char *progName = NULL; |
michael@0 | 87 | if (progCrashPoint != BETWEENFORKEXEC) { |
michael@0 | 88 | progName = [execProgname UTF8String]; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | int pid; |
michael@0 | 92 | |
michael@0 | 93 | if (bpForkOption == UNINSTALL) { |
michael@0 | 94 | BreakpadRelease(breakpad_); |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | if (useVFork) { |
michael@0 | 98 | pid = vfork(); |
michael@0 | 99 | } else { |
michael@0 | 100 | pid = fork(); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | if (pid == 0) { |
michael@0 | 104 | sleep(3); |
michael@0 | 105 | NSLog(@"Child continuing"); |
michael@0 | 106 | FILE *fd = fopen("/tmp/childlog.txt","wt"); |
michael@0 | 107 | kern_return_t kr; |
michael@0 | 108 | if (bpForkOption == RESETEXCEPTIONPORT) { |
michael@0 | 109 | kr = task_set_exception_ports(mach_task_self(), |
michael@0 | 110 | EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | |
michael@0 | 111 | EXC_MASK_ARITHMETIC | EXC_MASK_BREAKPOINT, |
michael@0 | 112 | MACH_PORT_NULL, |
michael@0 | 113 | EXCEPTION_DEFAULT, |
michael@0 | 114 | THREAD_STATE_NONE); |
michael@0 | 115 | fprintf(fd,"task_set_exception_ports returned %d\n", kr); |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | if (progCrashPoint == BETWEENFORKEXEC) { |
michael@0 | 119 | fprintf(fd,"crashing post-fork\n"); |
michael@0 | 120 | int *a = NULL; |
michael@0 | 121 | printf("%d\n",*a++); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | fprintf(fd,"about to call exec with %s\n", progName); |
michael@0 | 125 | fclose(fd); |
michael@0 | 126 | int i = execl(progName, progName, NULL); |
michael@0 | 127 | fprintf(fd, "exec returned! %d\n", i); |
michael@0 | 128 | fclose(fd); |
michael@0 | 129 | } |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | - (IBAction)crash:(id)sender { |
michael@0 | 133 | NSInteger tag = [sender tag]; |
michael@0 | 134 | |
michael@0 | 135 | if (tag == 1) { |
michael@0 | 136 | [NSObject cancelPreviousPerformRequestsWithTarget:self]; |
michael@0 | 137 | [self performSelector:@selector(causeCrash) withObject:nil afterDelay:10.0]; |
michael@0 | 138 | [sender setState:NSOnState]; |
michael@0 | 139 | return; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | if (tag == 2 && breakpad_) { |
michael@0 | 143 | BreakpadRelease(breakpad_); |
michael@0 | 144 | breakpad_ = NULL; |
michael@0 | 145 | return; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | [self causeCrash]; |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | - (void)anotherThread { |
michael@0 | 152 | NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; |
michael@0 | 153 | TestClass *tc = [[TestClass alloc] init]; |
michael@0 | 154 | |
michael@0 | 155 | [tc wait]; |
michael@0 | 156 | |
michael@0 | 157 | [pool release]; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | - (void)awakeFromNib { |
michael@0 | 161 | NSBundle *bundle = [NSBundle mainBundle]; |
michael@0 | 162 | NSDictionary *info = [bundle infoDictionary]; |
michael@0 | 163 | |
michael@0 | 164 | |
michael@0 | 165 | breakpad_ = BreakpadCreate(info); |
michael@0 | 166 | |
michael@0 | 167 | // Do some unit tests with keys |
michael@0 | 168 | // first a series of bogus values |
michael@0 | 169 | BreakpadSetKeyValue(breakpad_, nil, @"bad2"); |
michael@0 | 170 | BreakpadSetKeyValue(nil, @"bad3", @"bad3"); |
michael@0 | 171 | |
michael@0 | 172 | // Now some good ones |
michael@0 | 173 | BreakpadSetKeyValue(breakpad_,@"key1", @"value1"); |
michael@0 | 174 | BreakpadSetKeyValue(breakpad_,@"key2", @"value2"); |
michael@0 | 175 | BreakpadSetKeyValue(breakpad_,@"key3", @"value3"); |
michael@0 | 176 | |
michael@0 | 177 | // Look for a bogus one that we didn't try to set |
michael@0 | 178 | NSString *test = BreakpadKeyValue(breakpad_, @"bad4"); |
michael@0 | 179 | if (test) { |
michael@0 | 180 | NSLog(@"Bad BreakpadKeyValue (bad4)"); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | // Look for a bogus one we did try to set |
michael@0 | 184 | test = BreakpadKeyValue(breakpad_, @"bad1"); |
michael@0 | 185 | if (test) { |
michael@0 | 186 | NSLog(@"Bad BreakpadKeyValue (bad1)"); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | // Test some bad args for BreakpadKeyValue |
michael@0 | 190 | test = BreakpadKeyValue(nil, @"bad5"); |
michael@0 | 191 | if (test) { |
michael@0 | 192 | NSLog(@"Bad BreakpadKeyValue (bad5)"); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | test = BreakpadKeyValue(breakpad_, nil); |
michael@0 | 196 | if (test) { |
michael@0 | 197 | NSLog(@"Bad BreakpadKeyValue (nil)"); |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | // Find some we did set |
michael@0 | 201 | test = BreakpadKeyValue(breakpad_, @"key1"); |
michael@0 | 202 | if (![test isEqualToString:@"value1"]) { |
michael@0 | 203 | NSLog(@"Can't find BreakpadKeyValue (key1)"); |
michael@0 | 204 | } |
michael@0 | 205 | test = BreakpadKeyValue(breakpad_, @"key2"); |
michael@0 | 206 | if (![test isEqualToString:@"value2"]) { |
michael@0 | 207 | NSLog(@"Can't find BreakpadKeyValue (key2)"); |
michael@0 | 208 | } |
michael@0 | 209 | test = BreakpadKeyValue(breakpad_, @"key3"); |
michael@0 | 210 | if (![test isEqualToString:@"value3"]) { |
michael@0 | 211 | NSLog(@"Can't find BreakpadKeyValue (key3)"); |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | // Bad args for BreakpadRemoveKeyValue |
michael@0 | 215 | BreakpadRemoveKeyValue(nil, @"bad6"); |
michael@0 | 216 | BreakpadRemoveKeyValue(breakpad_, nil); |
michael@0 | 217 | |
michael@0 | 218 | // Remove one that is valid |
michael@0 | 219 | BreakpadRemoveKeyValue(breakpad_, @"key3"); |
michael@0 | 220 | |
michael@0 | 221 | // Try and find it |
michael@0 | 222 | test = BreakpadKeyValue(breakpad_, @"key3"); |
michael@0 | 223 | if (test) { |
michael@0 | 224 | NSLog(@"Shouldn't find BreakpadKeyValue (key3)"); |
michael@0 | 225 | } |
michael@0 | 226 | |
michael@0 | 227 | // Try and remove it again |
michael@0 | 228 | BreakpadRemoveKeyValue(breakpad_, @"key3"); |
michael@0 | 229 | |
michael@0 | 230 | // Try removal by setting to nil |
michael@0 | 231 | BreakpadSetKeyValue(breakpad_,@"key2", nil); |
michael@0 | 232 | // Try and find it |
michael@0 | 233 | test = BreakpadKeyValue(breakpad_, @"key2"); |
michael@0 | 234 | if (test) { |
michael@0 | 235 | NSLog(@"Shouldn't find BreakpadKeyValue (key2)"); |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | BreakpadAddUploadParameter(breakpad_, |
michael@0 | 239 | @"MeaningOfLife", |
michael@0 | 240 | @"42"); |
michael@0 | 241 | [NSThread detachNewThreadSelector:@selector(anotherThread) |
michael@0 | 242 | toTarget:self withObject:nil]; |
michael@0 | 243 | |
michael@0 | 244 | NSUserDefaults *args = [NSUserDefaults standardUserDefaults]; |
michael@0 | 245 | |
michael@0 | 246 | // If the user specified autocrash on the command line, toggle |
michael@0 | 247 | // Breakpad to not confirm and crash immediately. This is for |
michael@0 | 248 | // automated testing. |
michael@0 | 249 | if ([args boolForKey:@"autocrash"]) { |
michael@0 | 250 | BreakpadSetKeyValue(breakpad_, |
michael@0 | 251 | @BREAKPAD_SKIP_CONFIRM, |
michael@0 | 252 | @"YES"); |
michael@0 | 253 | [self causeCrash]; |
michael@0 | 254 | } |
michael@0 | 255 | |
michael@0 | 256 | progCrashPoint = DURINGLAUNCH; |
michael@0 | 257 | [window_ center]; |
michael@0 | 258 | [window_ makeKeyAndOrderFront:self]; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | @end |