Skip to content

Commit

Permalink
暂时解决因为stream的问题导致fork read pipe混乱问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yao.mai committed Mar 26, 2020
1 parent e08272c commit 252bf5f
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 57 deletions.
32 changes: 20 additions & 12 deletions androidemu/cpu/syscall_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,23 @@ def __init__(self, mu, syscall_handler):
self._process_name = config.global_config_get("pkg_name")

#

def __do_fork(self, mu):
logger.info("vfork called")
r = os.fork()
if (r == 0):
pass
#实测这样改没效果
#logging.basicConfig(level=logging.DEBUG, format='%(process)d - %(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout)
#
else:
logger.info("-----here is parent process child pid=%d"%r)
#
return r
#

def __fork(self, mu):
logging.warning("syscall fork")
return os.fork()
return self.__do_fork(mu)
#

def __execve(self, mu, filename_ptr, argv_ptr, envp_ptr):
Expand Down Expand Up @@ -171,7 +185,8 @@ def _handle_sigaction(self, mu, sig, act, oact):
#

def _gettid(self, mu):
return 0x2211
#单线程直接用pid代替
return self._getpid(mu)
#

def _setsockopt(self, mu, fd, level, optname, optval, optlen):
Expand Down Expand Up @@ -210,6 +225,7 @@ def _handle_gettimeofday(self, uc, tv, tz):

def __wait4(self, mu, pid, wstatus, options, ru):
assert ru==0
#return pid
logger.warning("syscall wait4 pid %d"%pid)
t = os.wait4(pid, options)
logger.info("wait4 return %r"%(t,))
Expand Down Expand Up @@ -265,15 +281,7 @@ def _handle_sigprocmask(self, mu, how, set, oset):
#

def __vfork(self, mu):
logger.info("vfork called")
r = os.fork()
if (r == 0):
logger.info("-----here is child process")
#
else:
logger.info("-----here is parent process")
#
return r
return self.__do_fork(mu)
#

def _get_uid(self, mu):
Expand Down
6 changes: 5 additions & 1 deletion androidemu/emulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
from androidemu.vfs.file_system import VirtualFileSystem

from androidemu.java.java_class_def import JavaClassDef
logging.basicConfig(level=logging.DEBUG, format='%(process)d - %(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout)

sys.stdout = sys.stderr
#由于这里的stream只能改一次,为避免与fork之后的子进程写到stdout混合,将这些log写到stderr
#FIXME:解除这种特殊的依赖
logging.basicConfig(level=logging.DEBUG, format='%(process)d - %(asctime)s - %(levelname)s - %(message)s', stream=sys.stderr)

logger = logging.getLogger(__name__)

Expand Down
14 changes: 12 additions & 2 deletions androidemu/vfs/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,14 @@ def _open_file(self, filename, mode):
if (mode & 2000):
flags | os.O_APPEND
#
return self.__pcb.add_fd(filename, file_path, androidemu.utils.misc_utils.my_open(file_path, flags))
fd = androidemu.utils.misc_utils.my_open(file_path, flags)
self.__pcb.add_fd(filename, file_path, fd)
logger.info("openat return fd %d"%fd)
return fd
else:
logger.warning("File does not exist '%s'" % filename)
return -1
#

def _handle_read(self, mu, fd, buf_addr, count):
"""
Expand All @@ -139,6 +143,7 @@ def _handle_read(self, mu, fd, buf_addr, count):

buf = os.read(fd, count)

logger.info("read return %r"%buf)
result = len(buf)
mu.mem_write(buf_addr, buf)
return result
Expand Down Expand Up @@ -277,8 +282,12 @@ def __fcntl64(self, mu, fd, cmd, arg1, arg2, arg3, arg4):
raise NotImplementedError()
#

def __statfs64(self, mu, path, sz, buf):
def __statfs64(self, mu, path_ptr, sz, buf):
#TODO

path = memory_helpers.read_utf8(mu, path_ptr)
logger.info("statfs64 path %s"%path)
#raise NotImplementedError()
return -1
#

Expand All @@ -302,6 +311,7 @@ def _handle_openat(self, mu, dfd, filename_ptr, flags, mode):
raise NotImplementedError("Directory file descriptor has not been implemented yet.")

return self._open_file(filename, mode)
#

def _handle_fstatat64(self, mu, dirfd, pathname_ptr, buf, flags):
"""
Expand Down
7 changes: 0 additions & 7 deletions example_bb.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ def install(mu, obj, s):
#
#

# Configure logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

# Initialize emulator
Expand Down
6 changes: 0 additions & 6 deletions example_douyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ def hook_code(mu, address, size, user_data):
#
#

# Configure logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

Expand Down
22 changes: 12 additions & 10 deletions example_douyin8.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,19 @@ def hook_mem_read(uc, access, address, size, value, user_data):

def hook_mem_write(uc, access, address, size, value, user_data):
pc = uc.reg_read(UC_ARM_REG_PC)
if (address == 3419067861):
print("write")
base = address
end = address+size
'''
if (base <= 0x30001645 and end >= 0x30001645):
print("write!!! base=0x%08X end=0x%08X pc=0x%08X"%(base, end, pc))
#

if (base <= 0x30001646 and end >= 0x30001646):
print("write!!! base=0x%08X end=0x%08X pc=0x%08X"%(base, end, pc))
#
if (base <= 0x3000166B and end >= 0x3000166B):
print("write!!! base=0x%08X end=0x%08X pc=0x%08X"%(base, end, pc))
#
'''

#
g_cfd = ChainLogger(sys.stdout, "./ins-douyin.txt")
Expand All @@ -202,13 +211,6 @@ def hook_code(mu, address, size, user_data):

#

# Configure logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

# Initialize emulator
Expand Down
7 changes: 0 additions & 7 deletions example_jni.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,6 @@ def test(self):
pass


# Configure logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

# Initialize emulator
Expand Down
6 changes: 0 additions & 6 deletions example_sgmain.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ def getStackTrace(mu):
#
#

# Configure logging
logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

Expand Down
6 changes: 0 additions & 6 deletions tests/test_native.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
from androidemu.emulator import Emulator
from androidemu.java.classes.string import String

logging.basicConfig(
stream=sys.stdout,
level=logging.DEBUG,
format="%(asctime)s %(levelname)7s %(name)34s | %(message)s"
)

logger = logging.getLogger(__name__)

def hook_code(mu, address, size, user_data):
Expand Down

0 comments on commit 252bf5f

Please sign in to comment.