-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add exception and signal handling for iOS #453
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1be1e5a
Add exception and signal handling for iOS
LeoNatan 2e738f2
Move exception handling to lower level C++ API to catch C++ exception…
LeoNatan 44300a7
Update DetoxCrashHandler.mm
LeoNatan 86e7951
Merge branch 'master' into CrashHandling
rotemmiz fb9fea8
Handling native crashes gracefully
rotemmiz 567ccea
Merge remote-tracking branch 'origin/master' into CrashHandling
LeoNatan 8dc950d
fixing tests
rotemmiz 64018df
Fixed e2e
rotemmiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// | ||
// DetoxCrashHandler.m | ||
// Detox | ||
// | ||
// Created by Leo Natan (Wix) on 12/5/17. | ||
// Copyright © 2017 Wix. All rights reserved. | ||
// | ||
|
||
#include <fishhook.h> | ||
#import <dlfcn.h> | ||
#import <Foundation/Foundation.h> | ||
#import "DetoxManager.h" | ||
#import <Detox/Detox-Swift.h> | ||
|
||
#include <cstdlib> | ||
#include <exception> | ||
#include <typeinfo> | ||
#include <cxxabi.h> | ||
|
||
static void __DTXHandleCrash(NSException* exception, NSNumber* signal, NSString* other) | ||
{ | ||
NSNumber* threadNumber = [[NSThread currentThread] valueForKeyPath:@"private.seqNum"]; | ||
NSString* queueName = @""; | ||
#pragma clang diagnostic push | ||
#pragma clang diagnostic ignored "-Wdeprecated-declarations" | ||
dispatch_queue_t currentQueue = dispatch_get_current_queue(); | ||
#pragma clang diagnostic pop | ||
if(currentQueue) | ||
{ | ||
queueName = [NSString stringWithUTF8String:dispatch_queue_get_label(currentQueue)]; | ||
} | ||
|
||
NSMutableDictionary* report = [@{@"threadNumber": threadNumber, @"queueName": queueName} mutableCopy]; | ||
if(exception) | ||
{ | ||
report[@"errorDetails"] = exception.debugDescription; | ||
} | ||
else if(signal) | ||
{ | ||
report[@"errorDetails"] = [NSString stringWithFormat:@"Signal %@ was raised\n%@", signal, [NSThread callStackSymbols]]; | ||
} | ||
else if(other) | ||
{ | ||
report[@"errorDetails"] = other; | ||
} | ||
|
||
[DetoxManager.sharedManager notifyOnCrashWithDetails:report]; | ||
|
||
[NSThread sleepForTimeInterval:5]; | ||
} | ||
|
||
static NSSet<NSNumber*>* __supportedSignals; | ||
|
||
static int (*__orig_sigaction)(int, const struct sigaction * __restrict, struct sigaction * __restrict); | ||
static int __dtx_sigaction(int signal, const struct sigaction * __restrict newaction, struct sigaction * __restrict oldaction) | ||
{ | ||
if([__supportedSignals containsObject:@(signal)] == NO) | ||
{ | ||
return __orig_sigaction(signal, newaction, oldaction); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
static void __DTXHandleSignal(int signal) | ||
{ | ||
__DTXHandleCrash(nil, @(signal), nil); | ||
|
||
exit(1); | ||
} | ||
|
||
OBJC_EXTERN std::type_info *__cxa_current_exception_type(void); | ||
OBJC_EXTERN void __cxa_rethrow(void); | ||
|
||
static void (*__old_terminate)(void) = nil; | ||
static void __dtx_terminate(void) | ||
{ | ||
std::type_info* exceptionType = __cxa_current_exception_type(); | ||
if (exceptionType == nullptr) | ||
{ | ||
// No current exception. | ||
__DTXHandleCrash(nil, nil, @"Unknown error"); | ||
(*__old_terminate)(); | ||
} | ||
else | ||
{ | ||
// There is a current exception. Check if it's an objc exception. | ||
@try | ||
{ | ||
__cxa_rethrow(); | ||
} | ||
@catch (id e) | ||
{ | ||
__DTXHandleCrash(e, nil, nil); | ||
// It's an objc object. Call Foundation's handler, if any. | ||
void (*handler)(NSException*) = NSGetUncaughtExceptionHandler(); | ||
if(handler != nullptr) | ||
{ | ||
handler(e); | ||
} | ||
} | ||
@catch (...) | ||
{ | ||
const char* exceptionTypeMangledName = exceptionType->name(); | ||
|
||
int status = -1; | ||
const char* demangled = abi::__cxa_demangle(exceptionTypeMangledName, NULL, NULL, &status); | ||
NSString* exceptionTypeName = nil; | ||
if(demangled) | ||
{ | ||
exceptionTypeName = [NSString stringWithUTF8String:demangled]; | ||
free((void*)demangled); | ||
} | ||
else | ||
{ | ||
exceptionTypeName = [NSString stringWithUTF8String:exceptionTypeMangledName]; | ||
} | ||
|
||
__DTXHandleCrash(nil, nil, [NSString stringWithFormat:@"C++ exception of type \"%@\" was thrown", exceptionTypeName]); | ||
// It's not an objc object. Continue to C++ terminate. | ||
(*__old_terminate)(); | ||
} | ||
} | ||
} | ||
|
||
__attribute__((constructor)) | ||
static void __DTXInstallCrashHandlers() | ||
{ | ||
__old_terminate = std::set_terminate(__dtx_terminate); | ||
|
||
__supportedSignals = [NSSet setWithArray:@[@(SIGQUIT), @(SIGILL), @(SIGTRAP), @(SIGABRT), @(SIGFPE), @(SIGBUS), @(SIGSEGV), @(SIGSYS)]]; | ||
|
||
__orig_sigaction = (int (*)(int, const struct sigaction * __restrict, struct sigaction * __restrict))dlsym(RTLD_DEFAULT, "sigaction"); | ||
|
||
{ | ||
struct rebinding rebindings[] = { | ||
{"sigaction", (void*)__dtx_sigaction, nullptr} | ||
}; | ||
|
||
rebind_symbols(rebindings, 1); | ||
} | ||
|
||
struct sigaction signalAction; | ||
memset(&signalAction, 0, sizeof(signalAction)); | ||
sigemptyset(&signalAction.sa_mask); | ||
signalAction.sa_handler = &__DTXHandleSignal; | ||
|
||
[__supportedSignals enumerateObjectsUsingBlock:^(NSNumber * _Nonnull obj, BOOL * _Nonnull stop) { | ||
int signum = obj.intValue; | ||
|
||
__orig_sigaction(signum, &signalAction, nullptr); | ||
}]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably You forgot about one
m
in extension 😄There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was an Objective C file before, but now Objective C++.