Skip to content

Commit

Permalink
implement call_static_boolean_method / vfprintf (AeonLucid#57)
Browse files Browse the repository at this point in the history
* implement call_static_boolean_method

* implement hooks vfprintf
  • Loading branch information
Ch3nYe authored Feb 19, 2021
1 parent 33cd61f commit b9fd5f8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
52 changes: 46 additions & 6 deletions androidemu/java/jni_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,9 +590,13 @@ def new_object_a(self, mu, env):
raise NotImplementedError()

@native_method
def get_object_class(self, mu, env, jobj):
def get_object_class(self, mu, env, jobj): # when you call this function 0xFA(defined in androidemu.java.java_method_def.java_method_def)
# always cast to jobj, this is a bug right?
# logger.debug("JNIEnv->GetObjectClass(%s) was called" % jobj)
ref = self.get_reference(jobj)
if isinstance(ref, jobject):
if isinstance(ref, jclass):
return ref.value
elif isinstance(ref, jobject):
return ref.value.__class__
raise NotImplementedError()

Expand Down Expand Up @@ -1174,12 +1178,48 @@ def call_static_object_method_a(self, mu, env):
raise NotImplementedError()

@native_method
def call_static_boolean_method(self, mu, env):
raise NotImplementedError()
def call_static_boolean_method(self, mu, env, clazz_idx, method_id):
params_count = len(locals())
clazz = self.get_reference(clazz_idx)

if not isinstance(clazz, jclass):
raise ValueError('Expected a jclass.')

method = clazz.value.find_method_by_id(method_id)

if method is None:
raise RuntimeError("Could not find method %d in class %s by id." % (method_id, clazz.value.jvm_name))

logger.debug("JNIEnv->CallStaticBooleanMethod(%s, %s <%s>) was called" % (
clazz.value.jvm_name,
method.name,
method.signature))

# Parse arguments.
constructor_args = self.read_args(mu, params_count - 2, method.args_list)
return method.func(self._emu, *constructor_args)

@native_method
def call_static_boolean_method_v(self, mu, env):
raise NotImplementedError()
def call_static_boolean_method_v(self, mu, env, clazz_idx, method_id, args):
clazz = self.get_reference(clazz_idx)

if not isinstance(clazz, jclass):
raise ValueError('Expected a jclass.')

method = clazz.value.find_method_by_id(method_id)

if method is None:
raise RuntimeError("Could not find method %d in class %s by id." % (method_id, clazz.value.jvm_name))

logger.debug("JNIEnv->CallStaticBooleanMethodV(%s, %s <%s>, 0x%x) was called" % (
clazz.value.jvm_name,
method.name,
method.signature, args))

# Parse arguments.
constructor_args = self.read_args_v(mu, args, method.args_list)

return method.func(self._emu, *constructor_args)

@native_method
def call_static_boolean_method_a(self, mu, env):
Expand Down
34 changes: 33 additions & 1 deletion androidemu/native/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ def __init__(self, emu, memory, modules, hooker):
modules.add_symbol_hook('dlclose', hooker.write_function(self.dlclose) + 1)
modules.add_symbol_hook('dladdr', hooker.write_function(self.dladdr) + 1)
modules.add_symbol_hook('dlsym', hooker.write_function(self.dlsym) + 1)
modules.add_symbol_hook('vfprintf', hooker.write_function(self.vfprintf) + 1)
modules.add_symbol_hook('pthread_create', hooker.write_function(self.nop('pthread_create')) + 1)
modules.add_symbol_hook('pthread_join', hooker.write_function(self.nop('pthread_join')) + 1)
modules.add_symbol_hook('vfprintf', hooker.write_function(self.nop('vfprintf')) + 1)
modules.add_symbol_hook('fprintf', hooker.write_function(self.nop('fprintf')) + 1)
modules.add_symbol_hook('dlerror', hooker.write_function(self.nop('dlerror')) + 1)

Expand Down Expand Up @@ -137,6 +137,38 @@ def dlsym(self, uc, handle, symbol):

raise NotImplementedError

@native_method
def vfprintf(self, uc, FILE, format, va_list):
# int vfprintf ( FILE * stream, const char * format, va_list arg );
struct_FILE = memory_helpers.read_byte_array(uc, FILE, 18)
c_string = memory_helpers.read_utf8(uc, format)

args = []
result_string = ""
for i in range(0,len(c_string)):
if c_string[i] == '%':
if c_string[i+1] == "d":
args.append(memory_helpers.read_uints(uc,va_list,1)[0])
elif c_string[i+1] == "c":
args.append(chr(memory_helpers.read_byte_array(uc,va_list,1)[0]))
elif c_string[i+1] == "s":
s_addr = memory_helpers.read_ptr(uc, va_list)
args.append(memory_helpers.read_cString(uc, s_addr)[0])
else:
result_string += c_string[i:i+2]
# TODO more format support
va_list += 4
result_string += "{0["+str(len(args)-1)+"]}"
continue
if i>=1:
if c_string[i-1] == '%' or c_string[i] == '%':
continue
result_string += c_string[i]

result_string = result_string.format(args)
logger.debug("Called vfprintf(%r)" % result_string)


def nop(self, name):
@native_method
def nop_inside(emu):
Expand Down
17 changes: 17 additions & 0 deletions androidemu/utils/memory_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ def read_utf8(mu, address):
return buffer[:null_pos].decode("utf-8")


def read_cString(mu, address):
# read string null-terminated, return string and length
buffer_address = address
buffer_read_size = 1
buffer = b""
null_pos = None

while null_pos is None:
buf_read = mu.mem_read(buffer_address, buffer_read_size)
if b'\x00' in buf_read:
null_pos = len(buffer) + buf_read.index(b'\x00')
buffer += buf_read
buffer_address += buffer_read_size

return buffer[:null_pos].decode("utf-8"),null_pos


def read_uints(mu, address, num=1):
data = mu.mem_read(address, num * 4)
return struct.unpack("I" * num, data)
Expand Down
4 changes: 2 additions & 2 deletions androidemu/vfs/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ def _handle_read(self, mu, fd, buf_addr, count):
raise NotImplementedError("Unsupported read operation for file descriptor %d." % fd)

if fd not in self._file_descriptors:
# TODO: Return valid error.
raise NotImplementedError()
logger.warning("No such file descriptor index %s in VirtualFileSystem" % fd)
mu.emu_stop()

file = self._file_descriptors[fd]

Expand Down

0 comments on commit b9fd5f8

Please sign in to comment.