Skip to content

Commit

Permalink
Fix initialization of large-sized NSData
Browse files Browse the repository at this point in the history
  • Loading branch information
sledgeh4w committed Jul 16, 2024
1 parent d36272e commit 4d559bf
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/chomper/os/ios/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,3 +528,26 @@ def hook_sec_item_copy_matching(uc, address, size, user_data):
emu.write_u64(a2, result)

return 0


@register_hook("_mach_vm_allocate")
def hook_mach_vm_allocate(uc, address, size, user_data):
emu = user_data["emu"]

addr = emu.get_arg(1)
size = emu.get_arg(2)

mem = emu.memory_manager.alloc(size)
emu.write_pointer(addr, mem)

return 0


@register_hook("_mach_vm_deallocate")
def hook_mach_vm_deallocate(uc, address, size, user_data):
emu = user_data["emu"]

mem = emu.get_arg(1)
emu.memory_manager.free(mem)

return 0
13 changes: 13 additions & 0 deletions tests/test_objc.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def test_ns_data(emu_ios, objc):
assert data


def test_ns_data_with_large_size(emu_ios, objc):
"""When the size of `NSData` exceeds 64k, `vm_allocate` will be called."""
with objc.autorelease_pool():
data_bytes = bytes(1024 * 64)

buffer = emu_ios.create_buffer(len(data_bytes))
emu_ios.write_bytes(buffer, data_bytes)

data = objc.msg_send("NSData", "dataWithBytes:length:", buffer, len(data_bytes))

assert data


def test_ns_url(emu_ios, objc):
with objc.autorelease_pool():
string = objc.msg_send(
Expand Down

0 comments on commit 4d559bf

Please sign in to comment.