Skip to content

Commit

Permalink
Free memory after tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sledgeh4w committed Dec 15, 2024
1 parent 5c286c1 commit b115e9b
Show file tree
Hide file tree
Showing 5 changed files with 300 additions and 132 deletions.
43 changes: 28 additions & 15 deletions tests/test_emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def hook_code(uc, address, size, user_data):
a2 = emu_arm64.create_buffer(32)
a3 = emu_arm64.create_buffer(32)

emu_arm64.call_address(libtiny_v73021_arm64.base + 0x289A4, a1, a2, a3)
try:
emu_arm64.call_address(libtiny_v73021_arm64.base + 0x289A4, a1, a2, a3)
finally:
emu_arm64.free(a1)
emu_arm64.free(a2)
emu_arm64.free(a3)


@pytest.mark.usefixtures("libz_arm64")
Expand All @@ -46,14 +51,16 @@ def test_set_and_get_retval(emu_arm64):

def test_create_buffer(emu_arm64):
result = emu_arm64.create_buffer(1024)
assert result

assert result is not None
emu_arm64.free(result)


def test_create_string(emu_arm64):
result = emu_arm64.create_string("chomper")
assert result

assert result is not None
emu_arm64.free(result)


def test_free(emu_arm64):
Expand All @@ -65,32 +72,38 @@ def test_read_and_write_int(emu_arm64):
addr = emu_arm64.create_buffer(1024)
value = 105

emu_arm64.write_int(addr, value, size=4)
result = emu_arm64.read_int(addr, size=4)

assert result == value
try:
emu_arm64.write_int(addr, value, size=4)
result = emu_arm64.read_int(addr, size=4)
assert result == value
finally:
emu_arm64.free(addr)


def test_read_and_write_bytes(emu_arm64):
sample_bytes = b"chomper"

addr = emu_arm64.create_buffer(1024)

emu_arm64.write_bytes(addr, sample_bytes)
result = emu_arm64.read_bytes(addr, len(sample_bytes))

assert result == sample_bytes
try:
emu_arm64.write_bytes(addr, sample_bytes)
result = emu_arm64.read_bytes(addr, len(sample_bytes))
assert result == sample_bytes
finally:
emu_arm64.free(addr)


def test_read_and_write_string(emu_arm64):
sample_str = "chomper"

addr = emu_arm64.create_buffer(1024)

emu_arm64.write_string(addr, sample_str)
result = emu_arm64.read_string(addr)

assert result == sample_str
try:
emu_arm64.write_string(addr, sample_str)
result = emu_arm64.read_string(addr)
assert result == sample_str
finally:
emu_arm64.free(addr)


@pytest.mark.usefixtures("libz_arm64")
Expand Down
39 changes: 25 additions & 14 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ def test_libdusanwa_v4856_arm(emu_arm, libdusanwa_v4856_arm):
emu_arm.write_bytes(a1, sample_bytes)
emu_arm.write_bytes(a4, sample_bytes)

emu_arm.call_address((libdusanwa_v4856_arm.base + 0xA588) | 1, a1, a2, a3, a4)
result = emu_arm.read_bytes(a3, a2)

assert zlib.crc32(result) == 1148403178
try:
emu_arm.call_address((libdusanwa_v4856_arm.base + 0xA588) | 1, a1, a2, a3, a4)
result = emu_arm.read_bytes(a3, a2)
assert zlib.crc32(result) == 1148403178
finally:
emu_arm.free(a1)
emu_arm.free(a3)
emu_arm.free(a4)


@pytest.mark.usefixtures("libc_arm64", "libz_arm64")
Expand All @@ -31,12 +35,15 @@ def test_libszstone_v4945_arm64(emu_arm64, libszstone_v4945_arm64):

emu_arm64.write_bytes(a1, sample_bytes)

result_size = emu_arm64.call_address(
libszstone_v4945_arm64.base + 0x2F1C8, a1, a2, a3
)
result = emu_arm64.read_bytes(a3, result_size)

assert zlib.crc32(result) == 3884391316
try:
result_size = emu_arm64.call_address(
libszstone_v4945_arm64.base + 0x2F1C8, a1, a2, a3
)
result = emu_arm64.read_bytes(a3, result_size)
assert zlib.crc32(result) == 3884391316
finally:
emu_arm64.free(a1)
emu_arm64.free(a3)


@pytest.mark.usefixtures("libc_arm64", "libz_arm64")
Expand All @@ -50,10 +57,14 @@ def test_libtiny_v73021_arm64(emu_arm64, libtiny_v73021_arm64):
emu_arm64.write_bytes(a1, sample_bytes * 4)
emu_arm64.write_bytes(a2, sample_bytes * 4)

emu_arm64.call_address(libtiny_v73021_arm64.base + 0x289A4, a1, a2, a3)
result = emu_arm64.read_bytes(a3, 32)

assert zlib.crc32(result) == 4192995551
try:
emu_arm64.call_address(libtiny_v73021_arm64.base + 0x289A4, a1, a2, a3)
result = emu_arm64.read_bytes(a3, 32)
assert zlib.crc32(result) == 4192995551
finally:
emu_arm64.free(a1)
emu_arm64.free(a2)
emu_arm64.free(a3)


# @staticmethod
Expand Down
25 changes: 23 additions & 2 deletions tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from chomper import Chomper
from chomper.const import ARCH_ARM64
from chomper.exceptions import EmulatorCrashed
from chomper.exceptions import EmulatorCrashed, SymbolMissing, ObjCUnrecognizedSelector


def test_unhandled_system_call_exception():
Expand Down Expand Up @@ -35,4 +35,25 @@ def test_missing_symbol_required_exception():
a3 = emu.create_buffer(1024)

emu.write_bytes(a1, sample_bytes)
emu.call_address(libszstone.base + 0x289A4, a1, a2, a3)

try:
emu.call_address(libszstone.base + 0x289A4, a1, a2, a3)
finally:
emu.free(a1)
emu.free(a3)


def test_symbol_missing_exception(emu_ios):
with pytest.raises(SymbolMissing):
emu_ios.find_symbol("_undefined_symbol")


def test_objc_unrecognized_selector_exception(emu_ios, objc):
with pytest.raises(ObjCUnrecognizedSelector):
with objc.autorelease_pool():
objc.msg_send("NSString", "undefinedSelector")

with pytest.raises(ObjCUnrecognizedSelector):
with objc.autorelease_pool():
string = objc.msg_send("NSString", "stringWithUTF8String:", "")
objc.msg_send(string, "undefinedSelector")
Loading

0 comments on commit b115e9b

Please sign in to comment.