Skip to content

Commit

Permalink
Rename mu to uc for disambiguation (AeonLucid#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
hzyitc authored Mar 13, 2022
1 parent 36b4a60 commit 40b89c8
Show file tree
Hide file tree
Showing 18 changed files with 464 additions and 464 deletions.
2 changes: 1 addition & 1 deletion README_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class XGorgen(metaclass=JavaClassDef, jvm_name='com/ss/sys/ces/a'):
pass

@java_method_def(name='leviathan', signature='(I[B)[B', native=True)
def leviathan(self, mu):
def leviathan(self, uc):
pass

def test(self):
Expand Down
16 changes: 8 additions & 8 deletions examples/debug_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
logger = logging.getLogger(__name__)


def hook_code(mu, address, size, user_data):
instruction = mu.mem_read(address, size)
def hook_code(uc, address, size, user_data):
instruction = uc.mem_read(address, size)
instruction_str = ''.join('{:02x} '.format(x) for x in instruction)

logger.debug('# Tracing instruction at 0x%x, instruction size = 0x%x, instruction = %s' % (address, size, instruction_str))

if instruction == b"\x00\x00\x00\x00":
logger.error("Uh oh, we messed up.")
mu.emu_stop()
uc.emu_stop()


def hook_block(mu, address, size, user_data):
instruction = mu.mem_read(address, size)
def hook_block(uc, address, size, user_data):
instruction = uc.mem_read(address, size)
instruction_str = ''.join('{:02x} '.format(x) for x in instruction)

logger.debug('# Block at 0x%x, instruction size = 0x%x, instruction = %s' % (address, size, instruction_str))


def hook_unmapped(mu, access, address, length, value, context):
pc = mu.reg_read(UC_ARM_REG_PC)
def hook_unmapped(uc, access, address, length, value, context):
pc = uc.reg_read(UC_ARM_REG_PC)

logger.debug("mem unmapped: pc: %x access: %x address: %x length: %x value: %x" % (pc, access, address, length, value))
mu.emu_stop()
uc.emu_stop()
return True


Expand Down
8 changes: 4 additions & 4 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@


# Add debugging.
def hook_code(mu, address, size, user_data):
instruction = mu.mem_read(address, size)
def hook_code(uc, address, size, user_data):
instruction = uc.mem_read(address, size)
instruction_str = ''.join('{:02x} '.format(x) for x in instruction)

print('# Tracing instruction at 0x%x, instruction size = 0x%x, instruction = %s' % (address, size, instruction_str))


emulator.mu.hook_add(UC_HOOK_CODE, hook_code)
emulator.uc.hook_add(UC_HOOK_CODE, hook_code)

# Runs a method of "libnative-lib.so" that calls an imported function "strlen" from "libc.so".
emulator.call_symbol(lib_module, '_Z4testv')

print("String length is: %i" % emulator.mu.reg_read(UC_ARM_REG_R0))
print("String length is: %i" % emulator.uc.reg_read(UC_ARM_REG_R0))
20 changes: 10 additions & 10 deletions examples/example_douyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def __init__(self):
pass

@java_method_def(name='leviathan', signature='(I[B)[B', native=True)
def leviathan(self, mu):
def leviathan(self, uc):
pass

def test(self):
Expand All @@ -30,11 +30,11 @@ def __init__(self):
pass

@java_method_def(name='n0', signature='(Landroid/content/Context;)[B', native=True)
def n0(self, mu):
def n0(self, uc):
pass

@java_method_def(name='n1', signature='(Landroid/content/Context;Ljava/lang/String;)I', native=True)
def n1(self, mu):
def n1(self, uc):
pass


Expand Down Expand Up @@ -131,10 +131,10 @@ def getStackTrace(self, *args, **kwargs):
logger.info("=> 0x%08x - %s" % (module.base, module.filename))

# Debug
# emulator.mu.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
emulator.mu.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.mu.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.mu.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)
# emulator.uc.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
emulator.uc.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.uc.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.uc.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)

try:
# Run JNI_OnLoad.
Expand All @@ -145,8 +145,8 @@ def getStackTrace(self, *args, **kwargs):
with open("./misc/app_process32", 'rb') as ap:
data = ap.read()
len1 = len(data) + 1024 - (len(data) % 1024)
emulator.mu.mem_map(0xab006000, len1)
emulator.mu.mem_write(0xab006000, data)
emulator.uc.mem_map(0xab006000, len1)
emulator.uc.mem_write(0xab006000, data)

x = XGorgen()
data = 'acde74a94e6b493a3399fac83c7c08b35D58B21D9582AF77647FC9902E36AE70f9c001e9334e6e94916682224fbe4e5f00000000000000000000000000000000'
Expand All @@ -167,5 +167,5 @@ def getStackTrace(self, *args, **kwargs):
# if method.native:
# logger.info("- [0x%08x] %s - %s" % (method.native_addr, method.name, method.signature))
except UcError as e:
print("Exit at %x" % emulator.mu.reg_read(UC_ARM_REG_PC))
print("Exit at %x" % emulator.uc.reg_read(UC_ARM_REG_PC))
raise
16 changes: 8 additions & 8 deletions examples/example_jiagu.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):
pass

@java_method_def(name='stringFromJNI', signature='()Ljava/lang/String;', native=True)
def string_from_jni(self, mu):
def string_from_jni(self, uc):
pass

def test(self):
Expand Down Expand Up @@ -58,19 +58,19 @@ def test(self):
logger.info("=> 0x%08x - %s" % (module.base, module.filename))

# Debug
# emulator.mu.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.mu.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.mu.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.mu.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)
# emulator.uc.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.uc.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.uc.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.uc.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)

try:
# Run JNI_OnLoad.
# JNI_OnLoad will call 'RegisterNatives'.
emulator.call_symbol(lib_module, 'JNI_OnLoad', emulator.java_vm.address_ptr, 0x00)
emulator.mu.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
emulator.uc.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)

# Do native stuff.
emulator.mu.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
emulator.uc.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
main_activity = MainActivity()
logger.info("Response from JNI call: %s" % main_activity.string_from_jni(emulator))

Expand All @@ -82,5 +82,5 @@ def test(self):
if method.native:
logger.info("- [0x%08x] %s - %s" % (method.native_addr, method.name, method.signature))
except UcError as e:
print("Exit at %x" % emulator.mu.reg_read(UC_ARM_REG_PC))
print("Exit at %x" % emulator.uc.reg_read(UC_ARM_REG_PC))
raise
16 changes: 8 additions & 8 deletions examples/example_jni.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):
pass

@java_method_def(name='stringFromJNI', signature='()Ljava/lang/String;', native=True)
def string_from_jni(self, mu):
def string_from_jni(self, uc):
pass

def test(self):
Expand All @@ -41,8 +41,8 @@ def test(self):
vfs_root=posixpath.join(posixpath.dirname(__file__), "vfs")
)

# emulator.mu.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.mu.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.uc.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.uc.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)

# Register Java class.
emulator.java_classloader.add_class(MainActivity)
Expand All @@ -61,10 +61,10 @@ def test(self):
logger.info("=> 0x%08x - %s" % (module.base, module.filename))

# Debug
# emulator.mu.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.mu.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.mu.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.mu.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)
# emulator.uc.hook_add(UC_HOOK_CODE, debug_utils.hook_code)
# emulator.uc.hook_add(UC_HOOK_MEM_UNMAPPED, debug_utils.hook_unmapped)
# emulator.uc.hook_add(UC_HOOK_MEM_WRITE, debug_utils.hook_mem_write)
# emulator.uc.hook_add(UC_HOOK_MEM_READ, debug_utils.hook_mem_read)

try:
# Run JNI_OnLoad.
Expand All @@ -83,5 +83,5 @@ def test(self):
if method.native:
logger.info("- [0x%08x] %s - %s" % (method.native_addr, method.name, method.signature))
except UcError as e:
print("Exit at %x" % emulator.mu.reg_read(UC_ARM_REG_PC))
print("Exit at %x" % emulator.uc.reg_read(UC_ARM_REG_PC))
raise
12 changes: 6 additions & 6 deletions src/androidemu/cpu/interrupt_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@
class InterruptHandler:

"""
:type mu Uc
:type uc Uc
"""
def __init__(self, mu):
self._mu = mu
self._mu.hook_add(UC_HOOK_INTR, self._hook_interrupt)
def __init__(self, uc):
self._uc = uc
self._uc.hook_add(UC_HOOK_INTR, self._hook_interrupt)
self._handlers = dict()

def _hook_interrupt(self, uc, intno, data):
if intno in self._handlers:
self._handlers[intno](uc)
else:
logger.error("Unhandled interrupt %d at %x, stopping emulation" % (intno, self._mu.reg_read(UC_ARM_REG_PC)))
self._mu.emu_stop()
logger.error("Unhandled interrupt %d at %x, stopping emulation" % (intno, self._uc.reg_read(UC_ARM_REG_PC)))
self._uc.emu_stop()

def set_handler(self, intno, handler):
self._handlers[intno] = handler
18 changes: 9 additions & 9 deletions src/androidemu/cpu/syscall_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ def __init__(self, interrupt_handler):
def set_handler(self, idx, name, arg_count, callback):
self._handlers[idx] = SyscallHandler(idx, name, arg_count, callback)

def _handle_syscall(self, mu):
idx = mu.reg_read(UC_ARM_REG_R7)
args = [mu.reg_read(reg_idx) for reg_idx in range(UC_ARM_REG_R0, UC_ARM_REG_R6 + 1)]
def _handle_syscall(self, uc):
idx = uc.reg_read(UC_ARM_REG_R7)
args = [uc.reg_read(reg_idx) for reg_idx in range(UC_ARM_REG_R0, UC_ARM_REG_R6 + 1)]

if idx in self._handlers:
handler = self._handlers[idx]
args = args[:handler.arg_count]
args_formatted = ", ".join(["%08x" % arg for arg in args])
logger.debug("Executing syscall %s(%s) at 0x%x" % (handler.name, args_formatted,
mu.reg_read(UC_ARM_REG_PC)))
uc.reg_read(UC_ARM_REG_PC)))

try:
result = handler.callback(mu, *args)
result = handler.callback(uc, *args)
except:
logger.error("An error occured during in %x syscall hander, stopping emulation" % idx)
mu.emu_stop()
uc.emu_stop()
raise

if result is not None:
mu.reg_write(UC_ARM_REG_R0, result)
uc.reg_write(UC_ARM_REG_R0, result)
else:

error = "Unhandled syscall 0x%x (%u) at 0x%x, stopping emulation" % (idx, idx,
mu.reg_read(UC_ARM_REG_PC))
mu.emu_stop()
uc.reg_read(UC_ARM_REG_PC))
uc.emu_stop()
raise RuntimeError(error)
Loading

0 comments on commit 40b89c8

Please sign in to comment.