Skip to content

Commit

Permalink
修复字符串内存泄漏
Browse files Browse the repository at this point in the history
  • Loading branch information
雍鹏亮 committed May 7, 2020
1 parent cbc8d6e commit fcdf9aa
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 63 deletions.
16 changes: 8 additions & 8 deletions MangoFix/Compiler/create.m
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,14 @@ Class mf_expression_class_of_kind(MFExpressionKind kind){


MFExpression *mf_create_expression(MFExpressionKind kind){
Class clazz = mf_expression_class_of_kind(kind);
MFExpression *expr = [[clazz alloc] init];
expr.lineNumber = mf_get_current_compile_util().currentLineNumber;
expr.expressionKind = kind;
if (mf_get_current_compile_util().currentClassDefinition) {
expr.currentClassName = mf_get_current_compile_util().currentClassDefinition.name;
}
return expr;
Class clazz = mf_expression_class_of_kind(kind);
MFExpression *expr = [[clazz alloc] init];
expr.lineNumber = mf_get_current_compile_util().currentLineNumber;
expr.expressionKind = kind;
if (mf_get_current_compile_util().currentClassDefinition) {
expr.currentClassName = mf_get_current_compile_util().currentClassDefinition.name;
}
return expr;
}


Expand Down
51 changes: 29 additions & 22 deletions MangoFix/Execute/MFContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,31 @@ - (instancetype)initWithRASPrivateKey:(NSString *)privateKey{
}

- (void)evalMangoScriptWithURL:(NSURL *)url{
NSError *error;
NSString *rsaEncryptedBase64String = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"MangoFix: %@",error);
return;
@autoreleasepool {
NSError *error;
NSString *rsaEncryptedBase64String = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"MangoFix: %@",error);
return;
}
[self evalMangoScriptWithRASEncryptedBase64String:rsaEncryptedBase64String];

}
[self evalMangoScriptWithRASEncryptedBase64String:rsaEncryptedBase64String];
}

- (void)evalMangoScriptWithRASEncryptedBase64String:(NSString *)rsaEncryptedBase64String{
NSString *mangoFixString = [MFRSA decryptString:rsaEncryptedBase64String privateKey:self.privateKey];
if (!mangoFixString.length) {
NSLog(@"MangoFix: RAS decrypt error!");
return;
@autoreleasepool {
NSString *mangoFixString = [MFRSA decryptString:rsaEncryptedBase64String privateKey:self.privateKey];
if (!mangoFixString.length) {
NSLog(@"MangoFix: RAS decrypt error!");
return;
}
mf_set_current_compile_util(self.interpreter);
mf_add_built_in(self.interpreter);
[self.interpreter compileSoruceWithString:mangoFixString];
mf_set_current_compile_util(nil);
mf_interpret(self.interpreter);
}
mf_set_current_compile_util(self.interpreter);
mf_add_built_in(self.interpreter);
[self.interpreter compileSoruceWithString:mangoFixString];
mf_set_current_compile_util(nil);
mf_interpret(self.interpreter);
}

- (MFValue *)objectForKeyedSubscript:(id)key{
Expand All @@ -63,13 +68,15 @@ - (void)setObject:(MFValue *)value forKeyedSubscript:(NSString *)key{

#ifdef DEBUG
- (void)evalMangoScriptWithDebugURL:(NSURL *)url{
NSError *error;
NSString *mangoFixString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
mf_set_current_compile_util(self.interpreter);
mf_add_built_in(self.interpreter);
[self.interpreter compileSoruceWithString:mangoFixString];
mf_set_current_compile_util(nil);
mf_interpret(self.interpreter);
@autoreleasepool {
NSError *error;
NSString *mangoFixString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
mf_set_current_compile_util(self.interpreter);
mf_add_built_in(self.interpreter);
[self.interpreter compileSoruceWithString:mangoFixString];
mf_set_current_compile_util(nil);
mf_interpret(self.interpreter);
}
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions MangoFix/Execute/eval.m
Original file line number Diff line number Diff line change
Expand Up @@ -692,13 +692,13 @@ static void eval_block_expression(MFInterpreter *inter, MFScopeChain *outScope,

manBlock.inter = inter;

const char *typeEncoding = [manBlock.func.returnTypeSpecifier typeEncoding];
typeEncoding = mf_str_append(typeEncoding, "@?");
NSMutableString *typeEncoding = [NSMutableString stringWithUTF8String:[manBlock.func.returnTypeSpecifier typeEncoding]];
[typeEncoding appendString:@"@?"];
for (MFParameter *param in manBlock.func.params) {
const char *paramTypeEncoding = [param.type typeEncoding];
typeEncoding = mf_str_append(typeEncoding, paramTypeEncoding);
[typeEncoding appendString:@(paramTypeEncoding)];
}
manBlock.typeEncoding = typeEncoding;
manBlock.typeEncoding = strdup(typeEncoding.UTF8String);
__autoreleasing id ocBlock = [manBlock ocBlock];
value.objectValue = ocBlock;
CFRelease((__bridge void *)ocBlock);
Expand Down
31 changes: 12 additions & 19 deletions MangoFix/Execute/execute.m
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,8 @@ static void replace_getter_method(NSUInteger lineNumber, MFInterpreter *inter ,C
ffi_prep_cif(cifPtr, FFI_DEFAULT_ABI, argCount, returnType, argTypes);
ffi_closure *closure = ffi_closure_alloc(sizeof(ffi_closure), (void **)&imp);
ffi_prep_closure_loc(closure, cifPtr, getterInter, (__bridge void *)prop, imp);
const char * typeEncoding = mf_str_append(retTypeEncoding, "@:");
class_replaceMethod(clazz, getterSEL, (IMP)imp, typeEncoding);
free((void *)typeEncoding);
NSString * typeEncoding = [NSString stringWithFormat:@"%s%s",retTypeEncoding, "@:"];
class_replaceMethod(clazz, getterSEL, (IMP)imp, typeEncoding.UTF8String);
}

static void replace_setter_method(NSUInteger lineNumber ,MFInterpreter *inter ,Class clazz, MFPropertyDefinition *prop){
Expand All @@ -526,9 +525,8 @@ static void replace_setter_method(NSUInteger lineNumber ,MFInterpreter *inter ,C
ffi_prep_cif(cifPtr, FFI_DEFAULT_ABI, argCount, returnType, argTypes);
ffi_closure *closure = ffi_closure_alloc(sizeof(ffi_closure), (void **)&imp);
ffi_prep_closure_loc(closure, cifPtr, setterInter, (__bridge void *)prop, imp);
const char * typeEncoding = mf_str_append("v@:", prtTypeEncoding);
class_replaceMethod(clazz, setterSEL, (IMP)imp, typeEncoding);
free((void *)typeEncoding);
NSString * typeEncoding = [NSString stringWithFormat:@"%s%s","v@:", prtTypeEncoding];
class_replaceMethod(clazz, setterSEL, (IMP)imp, typeEncoding.UTF8String);
}


Expand Down Expand Up @@ -630,8 +628,7 @@ static void replace_method(MFInterpreter *interpreter,Class clazz, MFMethodDefin
MFMethodMapTableItem *item = [[MFMethodMapTableItem alloc] initWithClass:clazz inter:interpreter method:method];
[[MFMethodMapTable shareInstance] addMethodMapTableItem:item];

BOOL needFreeTypeEncoding = NO;
const char *typeEncoding;
NSMutableString *typeEncoding = [NSMutableString string];
Method ocMethod;
if (method.classMethod) {
ocMethod = class_getClassMethod(clazz, sel);
Expand All @@ -640,25 +637,24 @@ static void replace_method(MFInterpreter *interpreter,Class clazz, MFMethodDefin
}

if (ocMethod) {
typeEncoding = method_getTypeEncoding(ocMethod);
[typeEncoding appendString:@(method_getTypeEncoding(ocMethod))];
}else{
typeEncoding =[func.returnTypeSpecifier typeEncoding];
[typeEncoding appendString:@([func.returnTypeSpecifier typeEncoding])];

for (MFParameter *param in func.params) {
const char *paramTypeEncoding = [param.type typeEncoding];
typeEncoding = mf_str_append(typeEncoding, paramTypeEncoding);
needFreeTypeEncoding = YES;
[typeEncoding appendString:@(paramTypeEncoding)];
}
}
Class c2 = method.classMethod ? objc_getMetaClass(class_getName(clazz)) : clazz;
if (class_respondsToSelector(c2, sel)) {
NSString *orgSelName = [NSString stringWithFormat:@"ORG%@",func.name];
SEL orgSel = NSSelectorFromString(orgSelName);
if (!class_respondsToSelector(c2, orgSel)) {
class_addMethod(c2, orgSel, method_getImplementation(ocMethod), typeEncoding);
class_addMethod(c2, orgSel, method_getImplementation(ocMethod), typeEncoding.UTF8String);
}
}
NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:typeEncoding];
NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:typeEncoding.UTF8String];
unsigned int argCount = (unsigned int)[sig numberOfArguments];
void *imp = NULL;
ffi_cif *cif = malloc(sizeof(ffi_cif));//不可以free
Expand All @@ -671,14 +667,11 @@ static void replace_method(MFInterpreter *interpreter,Class clazz, MFMethodDefin

if(ffi_prep_cif(cif, FFI_DEFAULT_ABI, argCount, returnType, args) == FFI_OK)
{
NSDictionary *userInfo = @{@"class":c2,@"typeEncoding":@(typeEncoding)};
NSDictionary *userInfo = @{@"class":c2,@"typeEncoding":[typeEncoding copy]};
CFTypeRef cfuserInfo = (__bridge_retained CFTypeRef)userInfo;
ffi_prep_closure_loc(closure, cif, replaceIMP, (void *)cfuserInfo, imp);
}
class_replaceMethod(c2, sel, imp, typeEncoding);
if (needFreeTypeEncoding) {
free((void *)typeEncoding);
}
class_replaceMethod(c2, sel, imp, typeEncoding.UTF8String);
}


Expand Down
2 changes: 0 additions & 2 deletions MangoFix/Util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ inline static char *removeTypeEncodingPrefix(char *typeEncoding){
return typeEncoding;
}

const char * mf_str_append(const char *str1, const char *str2);

ffi_type *mf_ffi_type_with_type_encoding(const char *typeEncoding);

size_t mf_size_with_encoding(const char *typeEncoding);
Expand Down
7 changes: 0 additions & 7 deletions MangoFix/Util/util.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@
#import "runenv.h"


const char * mf_str_append(const char *str1, const char *str2){
size_t len = strlen(str1) + strlen(str2);
char *ret = malloc(sizeof(char) * (len + 1));
strcpy(ret, str1);
strcat(ret, str2);
return ret;
}

static ffi_type *_ffi_type_with_type_encoding(NSString *typeEncoding){
char *code = (char *)typeEncoding.UTF8String;
Expand Down
1 change: 1 addition & 0 deletions MangoFixDemo/MangoFixDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_ENTITLEMENTS = "";
CODE_SIGN_STYLE = Automatic;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
DEVELOPMENT_TEAM = 4FV3KS7L29;
INFOPLIST_FILE = MangoFixDemo/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 10.0;
Expand Down
1 change: 0 additions & 1 deletion MangoFixDemo/MangoFixDemo/SuperMyController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ - (void)viewDidLoad {
}

- (void)dealloc{
NSLog(@"SuperMyController::%@", NSStringFromSelector(_cmd));
}


Expand Down

0 comments on commit fcdf9aa

Please sign in to comment.