diff --git a/src/chomper/os/ios/hooks.py b/src/chomper/os/ios/hooks.py index 0b5f6dc..d53d218 100644 --- a/src/chomper/os/ios/hooks.py +++ b/src/chomper/os/ios/hooks.py @@ -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 diff --git a/tests/test_objc.py b/tests/test_objc.py index 549e5aa..6d6ff54 100644 --- a/tests/test_objc.py +++ b/tests/test_objc.py @@ -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(