Skip to content

Commit

Permalink
Merge pull request #1 from pilaf-king/Swift5
Browse files Browse the repository at this point in the history
Swift5
  • Loading branch information
hbulixu committed Jan 18, 2021
2 parents 0b3ae52 + e52df5d commit 9be7aeb
Show file tree
Hide file tree
Showing 5 changed files with 426 additions and 231 deletions.
89 changes: 77 additions & 12 deletions WBBlades/Scan/WBBladesScanManager+CrashSymbol.mm
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ + (NSDictionary *)scanSwift5Types:(section_64)swift5Types fileData:(NSData *)fil
+ (NSDictionary *)scanSwiftClassMethodSymbol:(uintptr_t)typeOffset swiftType:(SwiftType)swiftType vm:(uintptr_t)vm fileData:(NSData *)fileData crashAddress:(NSArray *)crashAddress{
NSMutableDictionary *crashSymbolRst = [NSMutableDictionary dictionary];

NSString *className = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:typeOffset fileData:fileData];
NSString *className = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:typeOffset vm:vm fileData:fileData];

SwiftClassType classType = {0};
NSRange range = NSMakeRange(typeOffset, 0);
Expand Down Expand Up @@ -516,7 +516,7 @@ + (NSDictionary *)scanSwiftClassMethodSymbol:(uintptr_t)typeOffset swiftType:(Sw
memSqu++;
}

NSString *methodName = [self swiftClassMethod:method memberOffset:memberOffset member:record squ:j memSqu:memSqu fileData:fileData];
NSString *methodName = [self swiftClassMethod:method memberOffset:memberOffset member:record vm:vm squ:j memSqu:memSqu fileData:fileData];
uintptr_t imp = methodLocation + 4 + method.Offset;
NSLog(@"%@.%@",className,methodName);

Expand Down Expand Up @@ -582,17 +582,17 @@ + (NSDictionary *)scanSwiftClassOverrideMethodSymbol:(uintptr_t)methodLocation
data = [WBBladesTool readBytes:range length:sizeof(SwiftType) fromFile:fileData];
[data getBytes:&classType length:sizeof(SwiftType)];

NSString *overrideClassName = [WBBladesTool getSwiftTypeNameWithSwiftType:classType Offset:overrideClassOffset fileData:fileData];
NSString *overrideClassName = [WBBladesTool getSwiftTypeNameWithSwiftType:classType Offset:overrideClassOffset vm:vm fileData:fileData];

NSString *methodName = [self swiftClassMethod:overrideMethod memberOffset:overrideMethodOffset member:{0} squ:0 memSqu:0 fileData:fileData];
uintptr_t imp = overrideMethodOffset + 4 + overrideMethod.Offset;
NSString *methodName = [self swiftClassMethod:overrideMethod memberOffset:overrideMethodOffset member:{0} vm:vm squ:0 memSqu:0 fileData:fileData];
uintptr_t imp = methodLocation + 4*2 + method.Method;//函数地址
NSLog(@"%@重写%@.%@",className,overrideClassName,methodName);
[crashAddress enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
unsigned long long crash = [(NSString *)obj longLongValue];
if ([self scanFuncBinaryCode:crash begin:imp vm:vm fileData:fileData]) {
NSString *key = [NSString stringWithFormat:@"%lld",crash];
if (!crashSymbolRst[key] || [crashSymbolRst[key][IMP_KEY] longLongValue] < imp) {
NSMutableDictionary *dic = @{IMP_KEY:@(imp),SYMBOL_KEY:[NSString stringWithFormat:@"%@.%@",className,methodName]}.mutableCopy;
NSMutableDictionary *dic = @{IMP_KEY:@(imp),SYMBOL_KEY:[NSString stringWithFormat:@"%@.重写%@.%@",className,overrideClassName,methodName]}.mutableCopy;
[crashSymbolRst setObject:dic forKey:key];
}
}
Expand All @@ -604,14 +604,14 @@ + (NSDictionary *)scanSwiftClassOverrideMethodSymbol:(uintptr_t)methodLocation
return [crashSymbolRst copy];
}

+ (NSString *)swiftClassMethod:(SwiftMethod)method memberOffset:(uintptr_t)memberOffset member:(FieldRecord)member squ:(NSInteger)squ memSqu:(NSInteger)memSqu fileData:(NSData *)fileData{
+ (NSString *)swiftClassMethod:(SwiftMethod)method memberOffset:(uintptr_t)memberOffset member:(FieldRecord)member vm:(uintptr_t)vm squ:(NSInteger)squ memSqu:(NSInteger)memSqu fileData:(NSData *)fileData{
SwiftMethodKind kind = [WBBladesTool getSwiftMethodKind:method];
SwiftMethodType type = [WBBladesTool getSwiftMethodType:method];

NSString *methodName = @"";
NSString *memName = @"";
if (member.FieldName > 0 && (kind == SwiftMethodKindGetter || kind == SwiftMethodKindSetter || kind == SwiftMethodKindModify)) {
uintptr_t memNameOffset = memberOffset + (memSqu-1)*sizeof(FieldRecord) + 4*2 + member.FieldName;
uintptr_t memNameOffset = memberOffset + (memSqu-1)*sizeof(FieldRecord) + 4*2 + member.FieldName - vm;
uint8_t *buffer = (uint8_t *)malloc(CLASSNAME_MAX_LEN + 1); buffer[CLASSNAME_MAX_LEN] = '\0';
[fileData getBytes:buffer range:NSMakeRange(memNameOffset, CLASSNAME_MAX_LEN)];
memName = NSSTRING(buffer);
Expand Down Expand Up @@ -648,7 +648,7 @@ + (NSString *)swiftClassMethod:(SwiftMethod)method memberOffset:(uintptr_t)membe
+ (NSDictionary *)scanSwiftStructMethodSymbol:(uintptr_t)typeOffset swiftType:(SwiftType)swiftType vm:(uintptr_t)vm fileData:(NSData *)fileData crashAddress:(NSArray *)crashAddress{
NSMutableDictionary *crashSymbolRst = [NSMutableDictionary dictionary];

NSString *structName = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:typeOffset fileData:fileData];
NSString *structName = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:typeOffset vm:vm fileData:fileData];

SwiftStructType structType = {0};
NSRange range = NSMakeRange(typeOffset, 0);
Expand Down Expand Up @@ -711,16 +711,81 @@ + (NSDictionary *)scanSwift5Protos:(section_64)swift5Protos fileData:(NSData *)f

SwiftKind kindType = [WBBladesTool getSwiftType:swiftType];
if (kindType == SwiftKindProtocol) {
NSString *protosName = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:protosOffset fileData:fileData];

NSString *protosName = [WBBladesTool getSwiftTypeNameWithSwiftType:swiftType Offset:protosOffset vm:linkBase fileData:fileData];
NSLog(@"Protocol %@",protosName);

NSInteger requirementsCount = 0;
range = NSMakeRange(protosOffset + 4*4, 0);
data = [WBBladesTool readBytes:range length:sizeof(4) fromFile:fileData];
[data getBytes:&requirementsCount range:NSMakeRange(0, sizeof(4))];

if (requirementsCount > 0) {
NSLog(@"Protocol Requirements %lu",requirementsCount);
uintptr_t methodOffset = protosOffset + 4*6;

for (NSInteger r = 0; r< requirementsCount; r++) {
SwiftMethod method = {0};
range = NSMakeRange(methodOffset + r*sizeof(SwiftMethod), 0);
data = [WBBladesTool readBytes:range length:sizeof(SwiftMethod) fromFile:fileData];
[data getBytes:&method range:NSMakeRange(0, sizeof(SwiftMethod))];

NSString *methodName = [self swiftProtocolMethod:method methodOffset:methodOffset fileData:fileData];
NSLog(@"%@",methodName);
}
}
}

}

return crashSymbolRst.copy;
}

+ (NSString *)swiftProtocolMethod:(SwiftMethod)method methodOffset:(uintptr_t)methodOffset fileData:(NSData *)fileData{
SwiftProtocolTableKind kind = [WBBladesTool getSwiftProtocolTableKind:method];
SwiftProtocolTableType type = [WBBladesTool getSwiftProtocolTableType:method];

NSString *methodName = @"";
switch (kind) {
case SwiftProtocolTableKindBaseProtocol:
methodName = @"base protocol";
break;
case SwiftProtocolTableKindMethod:
if (type == SwiftProtocolTableTypeKind) {
methodName = @"class method";
}else if (type == SwiftProtocolTableTypeInstance){
methodName = @"instance method";
}else if (type == SwiftProtocolTableTypeExtraDiscriminatorShift){
methodName = @"extra discriminator shift";
}else if (type == SwiftProtocolTableTypeExtraDiscriminator){
methodName = @"extra discriminator";
}
break;
case SwiftProtocolTableKindInit:
methodName = @"init";
break;
case SwiftProtocolTableKindGetter:
methodName = @"getter";
break;
case SwiftProtocolTableKindSetter:
methodName = @"setter";
break;
case SwiftProtocolTableKindReadCoroutine:
methodName = @"read";
break;
case SwiftProtocolTableKindModifyCoroutine:
methodName = @"modify";
break;
case SwiftProtocolTableKindAssociatedTypeAccessFunction:
methodName = @"associated type";
break;
case SwiftProtocolTableKindAssociatedConformanceAccessFunction:
methodName = @"assosiated conformance";
break;
default:
break;
}
return methodName;
}

#pragma mark Tools
+ (BOOL)hasDWARF:(symtab_command)symTabCommand fileData:(NSData *)fileData{
BOOL has = YES;
Expand Down
Loading

0 comments on commit 9be7aeb

Please sign in to comment.