Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
// Copyright 2019 Google
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
 
15
#include "Crashlytics/Crashlytics/Helpers/FIRCLSDefines.h"
16
#include "Crashlytics/Crashlytics/Helpers/FIRCLSFeatures.h"
17
 
18
#if CLS_MACH_EXCEPTION_SUPPORTED
19
 
20
#include "Crashlytics/Crashlytics/Components/FIRCLSGlobals.h"
21
#include "Crashlytics/Crashlytics/Handlers/FIRCLSHandler.h"
22
#include "Crashlytics/Crashlytics/Handlers/FIRCLSMachException.h"
23
#include "Crashlytics/Crashlytics/Components/FIRCLSProcess.h"
24
#include "Crashlytics/Crashlytics/Handlers/FIRCLSSignal.h"
25
#include "Crashlytics/Crashlytics/Helpers/FIRCLSUtility.h"
26
 
27
#include <errno.h>
28
#include <mach/mach.h>
29
#include <pthread.h>
30
#include <unistd.h>
31
 
32
#pragma mark Prototypes
33
static void* FIRCLSMachExceptionServer(void* argument);
34
static bool FIRCLSMachExceptionThreadStart(FIRCLSMachExceptionReadContext* context);
35
static bool FIRCLSMachExceptionReadMessage(FIRCLSMachExceptionReadContext* context,
36
                                           MachExceptionMessage* message);
37
static kern_return_t FIRCLSMachExceptionDispatchMessage(FIRCLSMachExceptionReadContext* context,
38
                                                        MachExceptionMessage* message);
39
static bool FIRCLSMachExceptionReply(FIRCLSMachExceptionReadContext* context,
40
                                     MachExceptionMessage* message,
41
                                     kern_return_t result);
42
static bool FIRCLSMachExceptionRegister(FIRCLSMachExceptionReadContext* context);
43
static bool FIRCLSMachExceptionUnregister(FIRCLSMachExceptionOriginalPorts* originalPorts,
44
                                          exception_mask_t mask);
45
static bool FIRCLSMachExceptionRecord(FIRCLSMachExceptionReadContext* context,
46
                                      MachExceptionMessage* message);
47
 
48
#pragma mark - Initialization
49
void FIRCLSMachExceptionInit(FIRCLSMachExceptionReadContext* context) {
50
  if (!FIRCLSUnlinkIfExists(context->path)) {
51
    FIRCLSSDKLog("Unable to reset the mach exception file %s\n", strerror(errno));
52
  }
53
 
54
  if (!FIRCLSMachExceptionRegister(context)) {
55
    FIRCLSSDKLog("Unable to register mach exception handler\n");
56
    return;
57
  }
58
 
59
  if (!FIRCLSMachExceptionThreadStart(context)) {
60
    FIRCLSSDKLog("Unable to start thread\n");
61
    FIRCLSMachExceptionUnregister(&context->originalPorts, context->mask);
62
  }
63
}
64
 
65
void FIRCLSMachExceptionCheckHandlers(void) {
66
  if (_firclsContext.readonly->debuggerAttached) {
67
    return;
68
  }
69
 
70
  // It isn't really critical that this be done, as its extremely uncommon to run into
71
  // preexisting handlers.
72
  // Can use task_get_exception_ports for this.
73
}
74
 
75
static exception_mask_t FIRCLSMachExceptionMask(void) {
76
  exception_mask_t mask;
77
 
78
  // EXC_BAD_ACCESS
79
  // EXC_BAD_INSTRUCTION
80
  // EXC_ARITHMETIC
81
  // EXC_EMULATION - non-failure
82
  // EXC_SOFTWARE - non-failure
83
  // EXC_BREAKPOINT - trap instructions, from the debugger and code. Needs special treatment.
84
  // EXC_SYSCALL - non-failure
85
  // EXC_MACH_SYSCALL - non-failure
86
  // EXC_RPC_ALERT - non-failure
87
  // EXC_CRASH - see below
88
  // EXC_RESOURCE - non-failure, happens when a process exceeds a resource limit
89
  // EXC_GUARD - see below
90
  //
91
  // EXC_CRASH is a special kind of exception.  It is handled by launchd, and treated special by
92
  // the kernel.  Seems that we cannot safely catch it - our handler will never be called.  This
93
  // is a confirmed kernel bug.  Lacking access to EXC_CRASH means we must use signal handlers to
94
  // cover all types of crashes.
95
  // EXC_GUARD is relatively new, and isn't available on all OS versions. You have to be careful,
96
  // becuase you cannot succesfully register hanlders if there are any unrecognized masks. We've
97
  // dropped support for old OS versions that didn't have EXC_GUARD (iOS 5 and below, macOS 10.6 and
98
  // below) so we always add it now
99
 
100
  mask = EXC_MASK_BAD_ACCESS | EXC_MASK_BAD_INSTRUCTION | EXC_MASK_ARITHMETIC |
101
         EXC_MASK_BREAKPOINT | EXC_MASK_GUARD;
102
 
103
  return mask;
104
}
105
 
106
static bool FIRCLSMachExceptionThreadStart(FIRCLSMachExceptionReadContext* context) {
107
  pthread_attr_t attr;
108
 
109
  if (pthread_attr_init(&attr) != 0) {
110
    FIRCLSSDKLog("pthread_attr_init %s\n", strerror(errno));
111
    return false;
112
  }
113
 
114
  if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
115
    FIRCLSSDKLog("pthread_attr_setdetachstate %s\n", strerror(errno));
116
    return false;
117
  }
118
 
119
  // Use to pre-allocate a stack for this thread
120
  // The stack must be page-aligned
121
  if (pthread_attr_setstack(&attr, _firclsContext.readonly->machStack,
122
                            CLS_MACH_EXCEPTION_HANDLER_STACK_SIZE) != 0) {
123
    FIRCLSSDKLog("pthread_attr_setstack %s\n", strerror(errno));
124
    return false;
125
  }
126
 
127
  if (pthread_create(&context->thread, &attr, FIRCLSMachExceptionServer, context) != 0) {
128
    FIRCLSSDKLog("pthread_create %s\n", strerror(errno));
129
    return false;
130
  }
131
 
132
  pthread_attr_destroy(&attr);
133
 
134
  return true;
135
}
136
 
137
exception_mask_t FIRCLSMachExceptionMaskForSignal(int signal) {
138
  switch (signal) {
139
    case SIGTRAP:
140
      return EXC_MASK_BREAKPOINT;
141
    case SIGSEGV:
142
      return EXC_MASK_BAD_ACCESS;
143
    case SIGBUS:
144
      return EXC_MASK_BAD_ACCESS;
145
    case SIGILL:
146
      return EXC_MASK_BAD_INSTRUCTION;
147
    case SIGABRT:
148
      return EXC_MASK_CRASH;
149
    case SIGSYS:
150
      return EXC_MASK_CRASH;
151
    case SIGFPE:
152
      return EXC_MASK_ARITHMETIC;
153
  }
154
 
155
  return 0;
156
}
157
 
158
#pragma mark - Message Handling
159
static void* FIRCLSMachExceptionServer(void* argument) {
160
  FIRCLSMachExceptionReadContext* context = argument;
161
 
162
  pthread_setname_np("com.google.firebase.crashlytics.MachExceptionServer");
163
 
164
  while (1) {
165
    MachExceptionMessage message;
166
 
167
    // read the exception message
168
    if (!FIRCLSMachExceptionReadMessage(context, &message)) {
169
      break;
170
    }
171
 
172
    // handle it, and possibly forward
173
    kern_return_t result = FIRCLSMachExceptionDispatchMessage(context, &message);
174
 
175
    // and now, reply
176
    if (!FIRCLSMachExceptionReply(context, &message, result)) {
177
      break;
178
    }
179
  }
180
 
181
  FIRCLSSDKLog("Mach exception server thread exiting\n");
182
 
183
  return NULL;
184
}
185
 
186
static bool FIRCLSMachExceptionReadMessage(FIRCLSMachExceptionReadContext* context,
187
                                           MachExceptionMessage* message) {
188
  mach_msg_return_t r;
189
 
190
  memset(message, 0, sizeof(MachExceptionMessage));
191
 
192
  r = mach_msg(&message->head, MACH_RCV_MSG | MACH_RCV_LARGE, 0, sizeof(MachExceptionMessage),
193
               context->port, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
194
  if (r != MACH_MSG_SUCCESS) {
195
    FIRCLSSDKLog("Error receving mach_msg (%d)\n", r);
196
    return false;
197
  }
198
 
199
  FIRCLSSDKLog("Accepted mach exception message\n");
200
 
201
  return true;
202
}
203
 
204
static kern_return_t FIRCLSMachExceptionDispatchMessage(FIRCLSMachExceptionReadContext* context,
205
                                                        MachExceptionMessage* message) {
206
  FIRCLSSDKLog("Mach exception: 0x%x, count: %d, code: 0x%llx 0x%llx\n", message->exception,
207
               message->codeCnt, message->codeCnt > 0 ? message->code[0] : -1,
208
               message->codeCnt > 1 ? message->code[1] : -1);
209
 
210
  // This will happen if a child process raises an exception, as the exception ports are
211
  // inherited.
212
  if (message->task.name != mach_task_self()) {
213
    FIRCLSSDKLog("Mach exception task mis-match, returning failure\n");
214
    return KERN_FAILURE;
215
  }
216
 
217
  FIRCLSSDKLog("Unregistering handler\n");
218
  if (!FIRCLSMachExceptionUnregister(&context->originalPorts, context->mask)) {
219
    FIRCLSSDKLog("Failed to unregister\n");
220
    return KERN_FAILURE;
221
  }
222
 
223
  FIRCLSSDKLog("Restoring original signal handlers\n");
224
  if (!FIRCLSSignalSafeInstallPreexistingHandlers(& _firclsContext.readonly->signal, -1, NULL, NULL)) {
225
    FIRCLSSDKLog("Failed to restore signal handlers\n");
226
    return KERN_FAILURE;
227
  }
228
 
229
  FIRCLSSDKLog("Recording mach exception\n");
230
  if (!FIRCLSMachExceptionRecord(context, message)) {
231
    FIRCLSSDKLog("Failed to record mach exception\n");
232
    return KERN_FAILURE;
233
  }
234
 
235
  return KERN_SUCCESS;
236
}
237
 
238
static bool FIRCLSMachExceptionReply(FIRCLSMachExceptionReadContext* context,
239
                                     MachExceptionMessage* message,
240
                                     kern_return_t result) {
241
  MachExceptionReply reply;
242
  mach_msg_return_t r;
243
 
244
  // prepare the reply
245
  reply.head.msgh_bits = MACH_MSGH_BITS(MACH_MSGH_BITS_REMOTE(message->head.msgh_bits), 0);
246
  reply.head.msgh_remote_port = message->head.msgh_remote_port;
247
  reply.head.msgh_size = (mach_msg_size_t)sizeof(MachExceptionReply);
248
  reply.head.msgh_local_port = MACH_PORT_NULL;
249
  reply.head.msgh_id = message->head.msgh_id + 100;
250
 
251
  reply.NDR = NDR_record;
252
 
253
  reply.retCode = result;
254
 
255
  FIRCLSSDKLog("Sending exception reply\n");
256
 
257
  // send it
258
  r = mach_msg(&reply.head, MACH_SEND_MSG, reply.head.msgh_size, 0, MACH_PORT_NULL,
259
               MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
260
  if (r != MACH_MSG_SUCCESS) {
261
    FIRCLSSDKLog("mach_msg reply failed (%d)\n", r);
262
    return false;
263
  }
264
 
265
  FIRCLSSDKLog("Exception reply delivered\n");
266
 
267
  return true;
268
}
269
 
270
#pragma mark - Registration
271
static bool FIRCLSMachExceptionRegister(FIRCLSMachExceptionReadContext* context) {
272
  mach_port_t task = mach_task_self();
273
 
274
  kern_return_t kr = mach_port_allocate(task, MACH_PORT_RIGHT_RECEIVE, &context->port);
275
  if (kr != KERN_SUCCESS) {
276
    FIRCLSSDKLog("Error: mach_port_allocate failed %d\n", kr);
277
    return false;
278
  }
279
 
280
  kr = mach_port_insert_right(task, context->port, context->port, MACH_MSG_TYPE_MAKE_SEND);
281
  if (kr != KERN_SUCCESS) {
282
    FIRCLSSDKLog("Error: mach_port_insert_right failed %d\n", kr);
283
    mach_port_deallocate(task, context->port);
284
    return false;
285
  }
286
 
287
  // Get the desired mask, which covers all the mach exceptions we are capable of handling,
288
  // but clear out any that are in our ignore list.  We do this by ANDing with the bitwise
289
  // negation.  Because we are only clearing bits, there's no way to set an incorrect mask
290
  // using ignoreMask.
291
  context->mask = FIRCLSMachExceptionMask();
292
 
293
  // ORing with MACH_EXCEPTION_CODES will produce 64-bit exception data
294
  kr = task_swap_exception_ports(task, context->mask, context->port,
295
                                 EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE,
296
                                 context->originalPorts.masks, &context->originalPorts.count,
297
                                 context->originalPorts.ports, context->originalPorts.behaviors,
298
                                 context->originalPorts.flavors);
299
  if (kr != KERN_SUCCESS) {
300
    FIRCLSSDKLog("Error: task_swap_exception_ports %d\n", kr);
301
    return false;
302
  }
303
 
304
  for (int i = 0; i < context->originalPorts.count; ++i) {
305
    FIRCLSSDKLog("original 0x%x 0x%x 0x%x 0x%x\n", context->originalPorts.ports[i],
306
                 context->originalPorts.masks[i], context->originalPorts.behaviors[i],
307
                 context->originalPorts.flavors[i]);
308
  }
309
 
310
  return true;
311
}
312
 
313
static bool FIRCLSMachExceptionUnregister(FIRCLSMachExceptionOriginalPorts* originalPorts,
314
                                          exception_mask_t mask) {
315
  kern_return_t kr;
316
 
317
  // Re-register all the old ports.
318
  for (mach_msg_type_number_t i = 0; i < originalPorts->count; ++i) {
319
    // clear the bits from this original mask
320
    mask &= ~originalPorts->masks[i];
321
 
322
    kr =
323
        task_set_exception_ports(mach_task_self(), originalPorts->masks[i], originalPorts->ports[i],
324
                                 originalPorts->behaviors[i], originalPorts->flavors[i]);
325
    if (kr != KERN_SUCCESS) {
326
      FIRCLSSDKLog("unable to restore original port: %d", originalPorts->ports[i]);
327
    }
328
  }
329
 
330
  // Finally, mark any masks we registered for that do not have an original port as unused.
331
  kr = task_set_exception_ports(mach_task_self(), mask, MACH_PORT_NULL,
332
                                EXCEPTION_DEFAULT | MACH_EXCEPTION_CODES, THREAD_STATE_NONE);
333
  if (kr != KERN_SUCCESS) {
334
    FIRCLSSDKLog("unable to unset unregistered mask: 0x%x", mask);
335
    return false;
336
  }
337
 
338
  return true;
339
}
340
 
341
#pragma mark - Recording
342
void FIRCLSMachExceptionNameLookup(exception_type_t number,
343
                                          mach_exception_data_type_t code,
344
                                          const char** name,
345
                                          const char** codeName) {
346
  if (!name || !codeName) {
347
    return;
348
  }
349
 
350
  *name = NULL;
351
  *codeName = NULL;
352
 
353
  switch (number) {
354
    case EXC_BAD_ACCESS:
355
      *name = "EXC_BAD_ACCESS";
356
      switch (code) {
357
        case KERN_INVALID_ADDRESS:
358
          *codeName = "KERN_INVALID_ADDRESS";
359
          break;
360
        case KERN_PROTECTION_FAILURE:
361
          *codeName = "KERN_PROTECTION_FAILURE";
362
          break;
363
      }
364
 
365
      break;
366
    case EXC_BAD_INSTRUCTION:
367
      *name = "EXC_BAD_INSTRUCTION";
368
#if CLS_CPU_X86
369
      *codeName = "EXC_I386_INVOP";
370
#endif
371
      break;
372
    case EXC_ARITHMETIC:
373
      *name = "EXC_ARITHMETIC";
374
#if CLS_CPU_X86
375
      switch (code) {
376
        case EXC_I386_DIV:
377
          *codeName = "EXC_I386_DIV";
378
          break;
379
        case EXC_I386_INTO:
380
          *codeName = "EXC_I386_INTO";
381
          break;
382
        case EXC_I386_NOEXT:
383
          *codeName = "EXC_I386_NOEXT";
384
          break;
385
        case EXC_I386_EXTOVR:
386
          *codeName = "EXC_I386_EXTOVR";
387
          break;
388
        case EXC_I386_EXTERR:
389
          *codeName = "EXC_I386_EXTERR";
390
          break;
391
        case EXC_I386_EMERR:
392
          *codeName = "EXC_I386_EMERR";
393
          break;
394
        case EXC_I386_BOUND:
395
          *codeName = "EXC_I386_BOUND";
396
          break;
397
        case EXC_I386_SSEEXTERR:
398
          *codeName = "EXC_I386_SSEEXTERR";
399
          break;
400
      }
401
#endif
402
      break;
403
    case EXC_BREAKPOINT:
404
      *name = "EXC_BREAKPOINT";
405
#if CLS_CPU_X86
406
      switch (code) {
407
        case EXC_I386_DIVERR:
408
          *codeName = "EXC_I386_DIVERR";
409
          break;
410
        case EXC_I386_SGLSTP:
411
          *codeName = "EXC_I386_SGLSTP";
412
          break;
413
        case EXC_I386_NMIFLT:
414
          *codeName = "EXC_I386_NMIFLT";
415
          break;
416
        case EXC_I386_BPTFLT:
417
          *codeName = "EXC_I386_BPTFLT";
418
          break;
419
        case EXC_I386_INTOFLT:
420
          *codeName = "EXC_I386_INTOFLT";
421
          break;
422
        case EXC_I386_BOUNDFLT:
423
          *codeName = "EXC_I386_BOUNDFLT";
424
          break;
425
        case EXC_I386_INVOPFLT:
426
          *codeName = "EXC_I386_INVOPFLT";
427
          break;
428
        case EXC_I386_NOEXTFLT:
429
          *codeName = "EXC_I386_NOEXTFLT";
430
          break;
431
        case EXC_I386_EXTOVRFLT:
432
          *codeName = "EXC_I386_EXTOVRFLT";
433
          break;
434
        case EXC_I386_INVTSSFLT:
435
          *codeName = "EXC_I386_INVTSSFLT";
436
          break;
437
        case EXC_I386_SEGNPFLT:
438
          *codeName = "EXC_I386_SEGNPFLT";
439
          break;
440
        case EXC_I386_STKFLT:
441
          *codeName = "EXC_I386_STKFLT";
442
          break;
443
        case EXC_I386_GPFLT:
444
          *codeName = "EXC_I386_GPFLT";
445
          break;
446
        case EXC_I386_PGFLT:
447
          *codeName = "EXC_I386_PGFLT";
448
          break;
449
        case EXC_I386_EXTERRFLT:
450
          *codeName = "EXC_I386_EXTERRFLT";
451
          break;
452
        case EXC_I386_ALIGNFLT:
453
          *codeName = "EXC_I386_ALIGNFLT";
454
          break;
455
        case EXC_I386_ENDPERR:
456
          *codeName = "EXC_I386_ENDPERR";
457
          break;
458
        case EXC_I386_ENOEXTFLT:
459
          *codeName = "EXC_I386_ENOEXTFLT";
460
          break;
461
      }
462
#endif
463
      break;
464
    case EXC_GUARD:
465
      *name = "EXC_GUARD";
466
      break;
467
  }
468
}
469
 
470
static bool FIRCLSMachExceptionRecord(FIRCLSMachExceptionReadContext* context,
471
                                      MachExceptionMessage* message) {
472
  if (!context || !message) {
473
    return false;
474
  }
475
 
476
  if (FIRCLSContextMarkAndCheckIfCrashed()) {
477
    FIRCLSSDKLog("Error: aborting mach exception handler because crash has already occurred\n");
478
    exit(1);
479
    return false;
480
  }
481
 
482
  FIRCLSFile file;
483
 
484
  if (!FIRCLSFileInitWithPath(&file, context->path, false)) {
485
    FIRCLSSDKLog("Unable to open mach exception file\n");
486
    return false;
487
  }
488
 
489
  FIRCLSFileWriteSectionStart(&file, "mach_exception");
490
 
491
  FIRCLSFileWriteHashStart(&file);
492
 
493
  FIRCLSFileWriteHashEntryUint64(&file, "exception", message->exception);
494
 
495
  // record the codes
496
  FIRCLSFileWriteHashKey(&file, "codes");
497
  FIRCLSFileWriteArrayStart(&file);
498
  for (mach_msg_type_number_t i = 0; i < message->codeCnt; ++i) {
499
    FIRCLSFileWriteArrayEntryUint64(&file, message->code[i]);
500
  }
501
  FIRCLSFileWriteArrayEnd(&file);
502
 
503
  const char* name = NULL;
504
  const char* codeName = NULL;
505
 
506
  FIRCLSMachExceptionNameLookup(message->exception, message->codeCnt > 0 ? message->code[0] : 0,
507
                                &name, &codeName);
508
 
509
  FIRCLSFileWriteHashEntryString(&file, "name", name);
510
  FIRCLSFileWriteHashEntryString(&file, "code_name", codeName);
511
 
512
  FIRCLSFileWriteHashEntryUint64(&file, "original_ports", context->originalPorts.count);
513
  FIRCLSFileWriteHashEntryUint64(&file, "time", time(NULL));
514
 
515
  FIRCLSFileWriteHashEnd(&file);
516
 
517
  FIRCLSFileWriteSectionEnd(&file);
518
 
519
  FIRCLSHandler(&file, message->thread.name, NULL);
520
 
521
  FIRCLSFileClose(&file);
522
 
523
  return true;
524
}
525
 
526
#else
527
 
528
INJECT_STRIP_SYMBOL(cls_mach_exception)
529
 
530
#endif