1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
"""Exercise the native Windows crash monitor with an entirely synthetic child."""
import os
from pathlib import Path
import shutil
import struct
import subprocess
import sys
import tempfile
import unittest
REPO = Path(__file__).resolve().parents[2]
@unittest.skipUnless(__name__ == "__main__", "run through the windows_crash_monitor CTest target")
class CrashMonitorTest(unittest.TestCase):
def setUp(self):
tmp = tempfile.TemporaryDirectory(prefix="leonida crash test ")
self.addCleanup(tmp.cleanup)
self.report = Path(tmp.name) / "private run"
def run_fixture(self, mode, *args, seconds=10):
env = dict(os.environ, LEONIDA_TEST_CRASH_MODE=mode)
result = subprocess.run(
[MONITOR, REPO, self.report, str(seconds), FIXTURE,
subprocess.list2cmdline([str(FIXTURE), *args])],
env=env, capture_output=True, timeout=30,
)
return result.returncode & 0xffffffff
def check_artifacts(self, code, exception):
data = (self.report / "crash.dmp").read_bytes()
self.assertEqual(data[:4], b"MDMP")
count, offset = struct.unpack_from("<II", data, 8)
streams = dict((kind, pos) for kind, size, pos in
(struct.unpack_from("<III", data, offset + 12*i) for i in range(count)))
self.assertIn(3, streams) # ThreadListStream
trace = (self.report / "stack.txt").read_text(encoding="utf-8", errors="replace") # module names may be in the ANSI code page
self.assertIn(f"code=0x{code:08x}", trace)
if exception:
pos = streams[6] # ExceptionStream: thread ID, padding, exception record
thread_id, _, actual_code = struct.unpack_from("<III", data, pos)
self.assertEqual(actual_code, code)
self.assertIn(f"thread={thread_id}\n", trace)
self.assertIn("kind=unhandled_exception", trace)
self.assertIn("#0 ", trace)
else:
self.assertNotIn(6, streams)
self.assertIn("kind=abnormal_exit", trace)
self.assertFalse((self.report / "crash.dmp.partial").exists())
return trace
def test_worker(self):
code = 0xe0421234
self.assertEqual(self.run_fixture("worker"), code)
trace = self.check_artifacts(code, True)
if FIXTURE.with_suffix(".pdb").exists():
self.assertIn("crash_worker", trace)
def test_breakpoint(self):
self.assertEqual(self.run_fixture("break"), 0x80000003)
self.check_artifacts(0x80000003, True)
def test_explicit_exit(self):
self.assertEqual(self.run_fixture("exit"), 23)
trace = self.check_artifacts(23, False)
self.assertIn("#0 ", trace)
def test_exit_with_exception_number(self):
self.assertEqual(self.run_fixture("numeric"), 0xc0000005)
self.check_artifacts(0xc0000005, False)
def test_handled(self):
self.assertEqual(self.run_fixture("handled"), 0)
self.assertFalse(any(self.report.iterdir()))
def test_arguments_and_clean_exit(self):
self.assertEqual(self.run_fixture("argv", "a b\\", 'a"b'), 0)
self.assertFalse(any(self.report.iterdir()))
def test_timeout(self):
self.assertEqual(self.run_fixture("timer", seconds=1), 0)
self.assertEqual([p.name for p in self.report.iterdir()], ["timed-out"])
def test_existing_directory(self):
self.report.mkdir()
sentinel = self.report / "keep"
sentinel.write_bytes(b"original")
self.assertNotEqual(self.run_fixture("worker"), 0)
self.assertEqual(list(self.report.iterdir()), [sentinel])
self.assertEqual(sentinel.read_text(encoding="utf-8"), "original")
def test_repo_rejected(self):
self.report = REPO / (self.report.parent.name + "-forbidden")
self.assertNotEqual(self.run_fixture("worker"), 0)
self.assertFalse(self.report.exists())
def test_boot_entry_point(self):
root = self.report.parent
(root / "MicrosoftGame.Config").touch()
(root / "index.json").write_bytes(b"{}")
(root / "exe").mkdir()
(root / "exe" / "index.json").write_bytes(b"{}")
(root / "gta6rt_tls_host.dll").touch()
fixture = shutil.copy2(FIXTURE, root)
shutil.copy2(MONITOR, root)
out = root / "output"
script = REPO / "scripts" / "windows-boot" / "boot.ps1"
# Isolate the fixture from a developer's normal boot settings and paths.
env = {k: v for k, v in os.environ.items() if not k.upper().startswith("LEONIDA_")}
for mode, expected in (("exit", 23), ("timer", 0)):
with self.subTest(mode=mode):
result = subprocess.run(
["powershell.exe", "-NoProfile", "-ExecutionPolicy", "Bypass", "-File", script,
"-CrashDump", "-Name", mode, "-Runtime", fixture, "-Exe", fixture, "-GameDir", root,
"-Shaders", root, "-OutDir", out, "-Python", sys.executable, "-Seconds",
"1" if mode == "timer" else "10", "-LockWaitSeconds", "1", "-NoInput", "-NoCapture"],
env=dict(env, LEONIDA_TEST_CRASH_MODE=mode), capture_output=True, timeout=30,
)
self.assertEqual(result.returncode, expected, (result.stdout, result.stderr))
self.assertTrue((out / f"{mode}.digest.txt").is_file())
self.assertIn(b"fixture stdout", (out / f"{mode}.stdout.log").read_bytes())
self.assertIn(b"fixture stderr", (out / f"{mode}.log").read_bytes())
report, = out.glob(f"{mode}-crash-*")
if mode == "exit":
self.assertTrue((report / "crash.dmp").is_file())
else:
self.assertTrue((report / "timed-out").is_file())
self.assertFalse((report / "crash.dmp").exists())
if __name__ == "__main__":
MONITOR, FIXTURE = map(Path, sys.argv[1:3])
unittest.main(argv=sys.argv[:1], verbosity=2)
|