more log
This commit is contained in:
parent
9b2c4a2dd9
commit
90f97b9da2
1 changed files with 101 additions and 80 deletions
|
|
@ -8,6 +8,7 @@ import subprocess
|
||||||
import threading
|
import threading
|
||||||
import argparse
|
import argparse
|
||||||
import queue
|
import queue
|
||||||
|
import datetime
|
||||||
|
|
||||||
EMULATORS = [f"root34-{i}" for i in range(16)]
|
EMULATORS = [f"root34-{i}" for i in range(16)]
|
||||||
ANDROID_IMG = "system-images;android-34;default;x86_64"
|
ANDROID_IMG = "system-images;android-34;default;x86_64"
|
||||||
|
|
@ -239,96 +240,116 @@ def worker(emu: str, apklist: queue.Queue[str], out_folder: Path, script: Path):
|
||||||
script_env = os.environ.copy()
|
script_env = os.environ.copy()
|
||||||
script_env["ANDROID_HOME"] = str(ANDROID_HOME)
|
script_env["ANDROID_HOME"] = str(ANDROID_HOME)
|
||||||
proc_emu = restore_emu(emu, None)
|
proc_emu = restore_emu(emu, None)
|
||||||
while True:
|
marked_done = False
|
||||||
apk = apklist.get()
|
try:
|
||||||
folder_name = apk.split("/")[-1].removesuffix(".apk")
|
while True:
|
||||||
folder = out_folder / folder_name
|
apk = apklist.get()
|
||||||
|
marked_done = False
|
||||||
|
folder_name = apk.split("/")[-1].removesuffix(".apk")
|
||||||
|
folder = out_folder / folder_name
|
||||||
|
|
||||||
# Check if XP has already run without error or timeout
|
# Check if XP has already run without error or timeout
|
||||||
if folder.exists() and (folder / "data.json").exists():
|
if folder.exists() and (folder / "data.json").exists():
|
||||||
has_error = False
|
has_error = False
|
||||||
with (folder / "data.json").open() as fp:
|
with (folder / "data.json").open() as fp:
|
||||||
data = json.load(fp)
|
data = json.load(fp)
|
||||||
if "error" in data:
|
if "error" in data:
|
||||||
|
has_error = True
|
||||||
|
if (folder / "TIMEOUT").exists():
|
||||||
has_error = True
|
has_error = True
|
||||||
if (folder / "TIMEOUT").exists():
|
if has_error:
|
||||||
has_error = True
|
|
||||||
if has_error:
|
|
||||||
print(
|
|
||||||
f"Previous result for {apk=} found but with error of timeout, remove old result and rerun it"
|
|
||||||
)
|
|
||||||
shutil.rmtree(str(folder))
|
|
||||||
else:
|
|
||||||
# We already have a valid result, mark task done and skip xp
|
|
||||||
apklist.task_done()
|
|
||||||
continue
|
|
||||||
|
|
||||||
folder.mkdir(parents=True)
|
|
||||||
|
|
||||||
with (
|
|
||||||
(folder / "analysis.out").open("w") as fp_anly_stdout,
|
|
||||||
(folder / "analysis.err").open("w") as fp_anly_stderr,
|
|
||||||
):
|
|
||||||
|
|
||||||
print(f"START ANALYSIS: {apk=}, emulator-{console_port}")
|
|
||||||
|
|
||||||
# Reset the emulator and make sure it is runing
|
|
||||||
i = 0
|
|
||||||
started = False
|
|
||||||
while not started:
|
|
||||||
if i != 0 and i % 10 == 0:
|
|
||||||
print(
|
print(
|
||||||
f"Warning: tried to start emulator-{console_port} (avd {emu}) for the {i}th time without success"
|
f"Previous result for {apk=} found but with error of timeout, remove old result and rerun it"
|
||||||
)
|
)
|
||||||
proc_emu = restore_emu(emu, proc_emu)
|
shutil.rmtree(str(folder))
|
||||||
adb_run(
|
else:
|
||||||
f"emulator-{console_port}",
|
# We already have a valid result, mark task done and skip xp
|
||||||
["wait-for-device"],
|
apklist.task_done()
|
||||||
)
|
marked_done = True
|
||||||
j = 0
|
continue
|
||||||
|
|
||||||
|
folder.mkdir(parents=True)
|
||||||
|
|
||||||
|
with (
|
||||||
|
(folder / "analysis.out").open("w") as fp_anly_stdout,
|
||||||
|
(folder / "analysis.err").open("w") as fp_anly_stderr,
|
||||||
|
):
|
||||||
|
|
||||||
|
print(f"START ANALYSIS: {apk=}, emulator-{console_port}")
|
||||||
|
|
||||||
|
# Reset the emulator and make sure it is runing
|
||||||
|
i = 0
|
||||||
|
started = False
|
||||||
while not started:
|
while not started:
|
||||||
started = f"emulator-{console_port}\tdevice" in subprocess.run(
|
if i != 0 and i % 10 == 0:
|
||||||
[ADB, "devices"], stdout=subprocess.PIPE
|
print(
|
||||||
).stdout.decode("utf-8")
|
f"Warning: tried to start emulator-{console_port} (avd {emu}) for the {i}th time without success"
|
||||||
if not started:
|
)
|
||||||
time.sleep(1)
|
proc_emu = restore_emu(emu, proc_emu)
|
||||||
if j != 0 and j % 10 == 0:
|
adb_run(
|
||||||
print(
|
f"emulator-{console_port}",
|
||||||
f"emulator-{console_port} has been offline for 10s, restarting it now"
|
["wait-for-device"],
|
||||||
)
|
)
|
||||||
proc_emu.kill()
|
j = 0
|
||||||
break
|
while not started:
|
||||||
j += 1
|
started = f"emulator-{console_port}\tdevice" in subprocess.run(
|
||||||
i += 1
|
[ADB, "devices"], stdout=subprocess.PIPE
|
||||||
|
).stdout.decode("utf-8")
|
||||||
|
if not started:
|
||||||
|
time.sleep(1)
|
||||||
|
if j != 0 and j % 10 == 0:
|
||||||
|
print(
|
||||||
|
f"emulator-{console_port} has been offline for 10s, restarting it now"
|
||||||
|
)
|
||||||
|
proc_emu.kill()
|
||||||
|
break
|
||||||
|
j += 1
|
||||||
|
i += 1
|
||||||
|
|
||||||
print(f"emulator-{console_port} running")
|
print(f"emulator-{console_port} running")
|
||||||
fp_anly_stdout.write(f"START ANALYSIS: {apk=}, emulator-{console_port}\n")
|
fp_anly_stdout.write(
|
||||||
# should help debuging:
|
f"START ANALYSIS: {apk=}, emulator-{console_port}\n"
|
||||||
subprocess.run(
|
)
|
||||||
[ADB, "devices"],
|
# should help debuging:
|
||||||
stdout=fp_anly_stdout,
|
|
||||||
stderr=fp_anly_stderr,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Run script
|
|
||||||
try:
|
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
["bash", str(script), apk, f"emulator-{console_port}", str(folder)],
|
[ADB, "devices"],
|
||||||
env=script_env,
|
|
||||||
stdout=fp_anly_stdout,
|
stdout=fp_anly_stdout,
|
||||||
stderr=fp_anly_stderr,
|
stderr=fp_anly_stderr,
|
||||||
timeout=TIMEOUT,
|
|
||||||
)
|
)
|
||||||
print(f"FINISHED ANALYSIS: {apk=}, emulator-{console_port}")
|
|
||||||
# If timeout:
|
# Run script
|
||||||
except subprocess.TimeoutExpired:
|
try:
|
||||||
with (folder / "TIMEOUT").open("w") as fp:
|
subprocess.run(
|
||||||
fp.write("Process timedout")
|
[
|
||||||
print(f"TIMEOUT ANALYSIS: {apk=}, emulator-{console_port}")
|
"bash",
|
||||||
# again, for debuging:
|
str(script),
|
||||||
with (folder / "emu").open("w") as fp:
|
apk,
|
||||||
fp.write(f"Used emulator {emu}: emulator-{console_port}")
|
f"emulator-{console_port}",
|
||||||
apklist.task_done()
|
str(folder),
|
||||||
|
],
|
||||||
|
env=script_env,
|
||||||
|
stdout=fp_anly_stdout,
|
||||||
|
stderr=fp_anly_stderr,
|
||||||
|
timeout=TIMEOUT,
|
||||||
|
)
|
||||||
|
print(f"FINISHED ANALYSIS: {apk=}, emulator-{console_port}")
|
||||||
|
# If timeout:
|
||||||
|
except subprocess.TimeoutExpired:
|
||||||
|
with (folder / "TIMEOUT").open("w") as fp:
|
||||||
|
fp.write("Process timedout")
|
||||||
|
print(f"TIMEOUT ANALYSIS: {apk=}, emulator-{console_port}")
|
||||||
|
# again, for debuging:
|
||||||
|
with (folder / "emu").open("w") as fp:
|
||||||
|
fp.write(f"Used emulator {emu}: emulator-{console_port}")
|
||||||
|
apklist.task_done()
|
||||||
|
marked_done = True
|
||||||
|
except Exception as e:
|
||||||
|
msg = f"[{datetime.datetime.now()}] worker for {emu} (emulator-{console_port}) terminated after {e}"
|
||||||
|
print(msg)
|
||||||
|
with (out_folder / "worker_{emu}").open("w") as fp:
|
||||||
|
fp.write(msg)
|
||||||
|
if not marked_done:
|
||||||
|
apklist.task_done()
|
||||||
|
|
||||||
|
|
||||||
def run(apklist: list[str], out_folder: Path, script: Path):
|
def run(apklist: list[str], out_folder: Path, script: Path):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue