Skip to content

Commit

Permalink
objective_c_class: Add set_implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
netanelc305 committed Feb 7, 2024
1 parent c642def commit 260069d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
6 changes: 4 additions & 2 deletions hilda/objective_c/get_objectivec_class_description.m
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
methodReturnType = method_copyReturnType(methods[i]);
[classDescription[@"methods"] addObject:@{
@"name": [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding],
@"address": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"address": [NSNumber numberWithLong:STRIP_PAC((uintptr_t)(methods[i]))],
@"imp": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"is_class": @YES,
@"type": [NSString stringWithCString:method_getTypeEncoding(methods[i]) encoding:NSUTF8StringEncoding],
@"return_type": [NSString stringWithCString:methodReturnType encoding:NSUTF8StringEncoding],
Expand Down Expand Up @@ -110,7 +111,8 @@
methodReturnType = method_copyReturnType(methods[i]);
[classDescription[@"methods"] addObject:@{
@"name": [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding],
@"address": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"address": [NSNumber numberWithLong:STRIP_PAC((uintptr_t)(methods[i]))],
@"imp": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"is_class": @NO,
@"type": [NSString stringWithCString:method_getTypeEncoding(methods[i]) encoding:NSUTF8StringEncoding],
@"return_type": [NSString stringWithCString:methodReturnType encoding:NSUTF8StringEncoding],
Expand Down
6 changes: 4 additions & 2 deletions hilda/objective_c/get_objectivec_symbol_data.m
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@
methodReturnType = method_copyReturnType(methods[i]);
[objectData[@"methods"] addObject:@{
@"name": [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding],
@"address": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"address": [NSNumber numberWithLong:STRIP_PAC((uintptr_t)(methods[i]))],
@"imp": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"is_class": @YES,
@"type": [NSString stringWithCString:method_getTypeEncoding(methods[i]) encoding:NSUTF8StringEncoding],
@"return_type": [NSString stringWithCString:methodReturnType encoding:NSUTF8StringEncoding],
Expand Down Expand Up @@ -149,7 +150,8 @@
methodReturnType = method_copyReturnType(methods[i]);
[objectData[@"methods"] addObject:@{
@"name": [NSString stringWithCString:sel_getName(method_getName(methods[i])) encoding:NSUTF8StringEncoding],
@"address": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"address": [NSNumber numberWithLong:STRIP_PAC((uintptr_t)(methods[i]))],
@"imp": [NSNumber numberWithLong:STRIP_PAC(method_getImplementation(methods[i]))],
@"is_class": @NO,
@"type": [NSString stringWithCString:method_getTypeEncoding(methods[i]) encoding:NSUTF8StringEncoding],
@"return_type": [NSString stringWithCString:methodReturnType encoding:NSUTF8StringEncoding],
Expand Down
14 changes: 11 additions & 3 deletions hilda/objective_c_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ def convert_encoded_property_attributes(encoded):
@dataclass
class Method:
name: str
client: 'HildaClient' = field(compare=False)
address: int = field(compare=False)
imp: int = field(compare=False)
type_: str = field(compare=False)
return_type: str = field(compare=False)
is_class: bool = field(compare=False)
Expand All @@ -64,13 +66,19 @@ def from_data(data: dict, client):
"""
return Method(
name=data['name'],
client=client,
address=client.symbol(data['address']),
imp=client.symbol(data['imp']),
type_=data['type'],
return_type=decode_type(data['return_type']),
is_class=data['is_class'],
args_types=list(map(decode_type, data['args_types']))
)

def set_implementation(self, new_imp: int):
self.client.symbols.method_setImplementation(self.address, new_imp)
self.imp = self.client.symbol(new_imp)

def __str__(self):
if ':' in self.name:
args_names = self.name.split(':')
Expand Down Expand Up @@ -191,8 +199,8 @@ def hook(hilda, frame, bp_loc, options):
for method in self.methods:
if not method.is_class:
# only instance methods are relevant for capturing self
method.address.bp(hook, group_uuid=group_uuid,
name=f'-[{class_name} {method.name}]')
method.imp.bp(hook, group_uuid=group_uuid,
name=f'-[{class_name} {method.name}]')

if sync:
self._client.cont()
Expand All @@ -214,7 +222,7 @@ def bp(self, callback=None, **kwargs):
"""
for method in self.methods:
kwargs['name'] = f'[{self.name} {method.name}]'
method.address.bp(callback, **kwargs)
method.imp.bp(callback, **kwargs)

def iter_supers(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_symbols/test_objective_c_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,11 @@ def test_symbol_str_recursive(hilda_client):
:param hilda.hilda_client.HildaClient hilda_client: Hilda client.
"""
assert '+ alloc;' in hilda_client.ns({1: 2, 3: 4}).objc_symbol._to_str(True)


def test_set_implementation(hilda_client):
pid = hilda_client.symbols.getpid()

hilda_client.objc_get_class('NSJSONSerialization').get_method('isValidJSONObject:').set_implementation(
hilda_client.symbols.getpid)
assert hilda_client.objc_get_class('NSJSONSerialization').isValidJSONObject_() == pid

0 comments on commit 260069d

Please sign in to comment.