Skip to content

Commit

Permalink
完善 reverse 功能,添加对 libffi 相关的free
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFruity committed Feb 2, 2021
1 parent 820fefb commit 853950b
Show file tree
Hide file tree
Showing 12 changed files with 171 additions and 61 deletions.
38 changes: 28 additions & 10 deletions OCRunner/ORCoreImp/ORCoreFunction/ORCoreFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,34 @@ typedef struct {
unsigned nfixedargs; //可变参数需要的个数
} ffi_cif;

typedef enum {
FFI_OK = 0,
FFI_BAD_TYPEDEF,
FFI_BAD_ABI
} ffi_status;

typedef struct {
void *trampoline_table;
void *trampoline_table_entry;
ffi_cif *cif;
void (*fun)(ffi_cif*,void*,void**,void*);
void *user_data;
} ffi_closure;

NSUInteger floatPointFlagsWithTypeEncode(const char *typeEncode);
NSUInteger resultFlagsForTypeEncode(const char *retTypeEncode, char **argTypeEncodes, int narg);
void *core_register_function(void (*fun)(ffi_cif *,void *,void **, void*),
unsigned nargs,
char **argTypeEncodes,
char *retTypeEncode,
void *userdata);

void ffi_closure_free(void *ptr);
#endif /* __has_include */

typedef struct {
ffi_cif *cif;
ffi_closure *closure;
#ifdef __libffi__
ffi_type **arg_types;
#endif
void *function_imp;
}or_ffi_result;
void or_ffi_result_free(or_ffi_result *result);

@class NSArray;
@class MFValue;
Expand All @@ -66,20 +84,20 @@ void invoke_functionPointer(void *funptr, NSArray<MFValue *> *argValues, MFValue



void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable));

void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret,
void *userdata);

void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable));

void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret,
void *userdata);
Expand Down
65 changes: 40 additions & 25 deletions OCRunner/ORCoreImp/ORCoreFunction/ORCoreFunctionRegister.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,7 @@
#include <ptrauth.h>
#endif

typedef enum {
FFI_OK = 0,
FFI_BAD_TYPEDEF,
FFI_BAD_ABI
} ffi_status;

typedef struct {
void *trampoline_table;
void *trampoline_table_entry;
ffi_cif *cif;
void (*fun)(ffi_cif*,void*,void**,void*);
void *user_data;
} ffi_closure;

extern void *ffi_closure_trampoline_table_page;

Expand Down Expand Up @@ -260,7 +248,7 @@ void ffi_closure_free(void *ptr)
return FFI_OK;
}

void *core_register_function(void (*imp)(ffi_cif *,void *,void **, void*),
or_ffi_result *core_register_function(void (*func)(ffi_cif *,void *,void **, void*),
unsigned nargs,
char **argTypeEncodes,
char *retTypeEncode,
Expand All @@ -270,9 +258,14 @@ void ffi_closure_free(void *ptr)
cif->nargs = nargs;
cif->r_typeEncode = retTypeEncode;
cif->flags = (unsigned) resultFlagsForTypeEncode(retTypeEncode, argTypeEncodes, nargs);
void *result = NULL;
ffi_closure *closure = ffi_closure_alloc(sizeof(ffi_closure), &result);
ffi_prep_closure_loc(closure, cif, imp, userdata, result);
void *imp = NULL;
ffi_closure *closure = ffi_closure_alloc(sizeof(ffi_closure), &imp);
ffi_prep_closure_loc(closure, cif, func, userdata, imp);

or_ffi_result *result = malloc(sizeof(or_ffi_result));
result->cif = cif;
result->closure = closure;
result->function_imp = imp;
return result;
}

Expand Down Expand Up @@ -431,14 +424,14 @@ void ffi_closure_free(void *ptr)
return result;
}

void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable))
{
return register_function(fun, args, ret, NULL);
}

void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret,
void *userdata)
Expand All @@ -450,13 +443,13 @@ void ffi_closure_free(void *ptr)
char *retTyep = mallocCopyStr(ret.typeEncode);
return core_register_function(fun, (int)args.count, argTypes, retTyep, userdata);
}
void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable))
{
return register_method(fun, args, ret, NULL);
}
void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret, void *userdata)
{
Expand All @@ -477,13 +470,15 @@ void ffi_closure_free(void *ptr)
result[sLen] = '\0';
return result;
}
void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable))
{
return register_function(fun, args, ret, NULL);
}
void *register_function(void (*fun)(ffi_cif *,void *,void **, void*),


or_ffi_result *register_function(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret,
void *userdata)
Expand All @@ -500,16 +495,21 @@ void ffi_closure_free(void *ptr)
{
ffi_prep_closure_loc(closure, cif, fun, userdata, imp);
}
return imp;
or_ffi_result *result = malloc(sizeof(or_ffi_result));
result->cif = cif;
result->arg_types = arg_types;
result->closure = closure;
result->function_imp = imp;
return result;
}

void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret) __attribute__((overloadable))
{
return register_method(fun, args, ret, NULL);
}
void *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
or_ffi_result *register_method(void (*fun)(ffi_cif *,void *,void **, void*),
NSArray <ORTypeVarPair *>*args,
ORTypeVarPair *ret,
void *userdata)
Expand All @@ -521,3 +521,18 @@ void ffi_closure_free(void *ptr)
return register_function(fun, argTypes, ret, userdata);
}
#endif/* __libffi__ */

void or_ffi_result_free(or_ffi_result *result){
#ifdef __libffi__
free(result->arg_types);
#else
for (int i = 0; i < result->cif->nargs; i++) {
free(result->cif->arg_typeEncodes[i]);
}
free(result->cif->arg_typeEncodes);
free(result->cif->r_typeEncode);
#endif
free(result->cif);
ffi_closure_free(result->closure);
free(result);
}
2 changes: 2 additions & 0 deletions OCRunner/ORInterpreter.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "ORStructDeclare.h"
#import "ORSystemFunctionPointerTable.h"
#import "MFStaticVarTable.h"
#import "ORffiResultCache.h"

@interface ORInterpreter()
@property (nonatomic, copy)NSArray *currentNodes;
Expand Down Expand Up @@ -135,6 +136,7 @@ + (void)reverse{
[[MFStaticVarTable shareInstance] clear];
[[ORStructDeclareTable shareInstance] clear];
[[ORTypeSymbolTable shareInstance] clear];
[[ORffiResultCache shared] clear];
ORInterpreter.shared.currentNodes = [NSArray array];

}
Expand Down
21 changes: 21 additions & 0 deletions OCRunner/ORffiResultCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ORffiResultCache.h
// OCRunner
//
// Created by Jiang on 2021/2/2.
//

#import <Foundation/Foundation.h>
#import "ORCoreFunction.h"
NS_ASSUME_NONNULL_BEGIN

@interface ORffiResultCache : NSObject
@property (nonatomic, strong)NSMutableDictionary <NSValue *,NSValue *>*cache;
+ (instancetype)shared;
- (void)saveffiResult:(or_ffi_result *)result WithKey:(NSValue *)key;
- (or_ffi_result *)ffiResultForKey:(NSValue *)key;
- (void)removeForKey:(NSValue *)key;
- (void)clear;
@end

NS_ASSUME_NONNULL_END
39 changes: 39 additions & 0 deletions OCRunner/ORffiResultCache.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ORffiResultCache.m
// OCRunner
//
// Created by Jiang on 2021/2/2.
//

#import "ORffiResultCache.h"

@implementation ORffiResultCache
+ (instancetype)shared{
static dispatch_once_t onceToken;
static ORffiResultCache *_instance;
dispatch_once(&onceToken, ^{
_instance = [ORffiResultCache new];
});
return _instance;
}
- (instancetype)init
{
self = [super init];
if (self) {
self.cache = [NSMutableDictionary dictionary];
}
return self;
}
- (void)saveffiResult:(or_ffi_result *)result WithKey:(NSValue *)key{
self.cache[key] = [NSValue valueWithPointer:result];
}
- (or_ffi_result *)ffiResultForKey:(NSValue *)key{
return self.cache[key].pointerValue;
}
- (void)removeForKey:(NSValue *)key{
[self.cache removeObjectForKey:key];
}
- (void)clear{
self.cache = [NSMutableDictionary dictionary];
}
@end
6 changes: 4 additions & 2 deletions OCRunner/RunEnv/MFBlock.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void dispose_helper(struct MFSimulateBlock *src)
@implementation MFBlock{
BOOL _generatedPtr;
void *_blockPtr;
or_ffi_result *_ffi_result;
struct MFGOSimulateBlockDescriptor *_descriptor;
}

Expand Down Expand Up @@ -77,14 +78,14 @@ - (void *)blockPtr{
(void (*)(const void *src))dispose_helper,
typeEncoding
};
void *blockImp = register_function(&blockInter, self.paramTypes, self.retType);
_ffi_result = register_function(&blockInter, self.paramTypes, self.retType);
_descriptor = malloc(sizeof(struct MFGOSimulateBlockDescriptor));
memcpy(_descriptor, &descriptor, sizeof(struct MFGOSimulateBlockDescriptor));
struct MFSimulateBlock simulateBlock = {
&_NSConcreteStackBlock,
(BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_SIGNATURE | BLOCK_CREATED_FROM_MFGO),
0,
blockImp,
_ffi_result->function_imp,
_descriptor,
(__bridge void*)self
};
Expand All @@ -94,6 +95,7 @@ - (void *)blockPtr{

-(void)dealloc{
free(_descriptor);
or_ffi_result_free(_ffi_result);
return;
}

Expand Down
1 change: 1 addition & 0 deletions OCRunner/RunEnv/MFScopeChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern const void *mf_propKey(NSString *propName);
- (nullable MFValue *)getValueWithIdentifier:(NSString *)identifer;
- (void)setValue:(MFValue *)value withIndentifier:(NSString *)identier;
- (void)assignWithIdentifer:(NSString *)identifier value:(MFValue *)value;
- (void)removeForIdentifier:(NSString *)key;
- (void)clear;
@end
NS_ASSUME_NONNULL_END
4 changes: 3 additions & 1 deletion OCRunner/RunEnv/MFScopeChain.m
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ - (MFValue *)getValueWithIdentifier:(NSString *)identifier endScope:(MFScopeChai
- (MFValue *)recursiveGetValueWithIdentifier:(NSString *)identifier{
return [self getValueWithIdentifier:identifier endScope:nil];
}

- (void)removeForIdentifier:(NSString *)key{
[_vars removeObjectForKey:key];
}
- (void)clear{
_vars = [NSMutableDictionary dictionary];
}
Expand Down
Loading

0 comments on commit 853950b

Please sign in to comment.