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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
#!/usr/bin/env python3
"""Executable/data lenses, scroll bounds, display formats and native glass rendering."""
import argparse
from pathlib import Path
import re
import subprocess
import tempfile
ROOT = Path(__file__).resolve().parents[2]
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('--gui', type=Path, default=ROOT / 'gui/zig-out/bin/ztrace-gui')
p.add_argument('--native', action='store_true')
p.add_argument('--output', type=Path)
a = p.parse_args()
a.gui = a.gui.resolve()
with tempfile.TemporaryDirectory(prefix='ztrace-lenses-') as directory:
out = a.output.resolve() if a.output else Path(directory)
out.mkdir(parents=True, exist_ok=True)
target = out / 'segments'
subprocess.run(['cc', '-g', '-O0', '-no-pie', str(ROOT / 'gui/targets/segments.c'), '-o', str(target)], check=True)
symbols = {name: int(address, 16) for address, name in re.findall(r'^([0-9a-f]+) \w (\w+)$', subprocess.check_output(['nm', '-n', str(target)], text=True), re.M)}
head, code = symbols['head'], symbols['checkpoint']
script = out / 'check.script'
commands = f'''console launch {target}
await stops 1
console break checkpoint
console continue
await execution 1
frames 40
console lens {code}
expect lens_count 1
expect lenses[0].first {code}
frames 40
screenshot {out / 'code-glass.png'}
console set glass false
frames 20
screenshot {out / 'code-opaque.png'}
expect-different-screenshot {out / 'code-glass.png'} {out / 'code-opaque.png'} 1120 175 1060 140
console set glass true
wheel 1600 220 -1
frames 40
print lenses[0].first
expect execution 1
# Right-clicking an executable lens address opens memory, not code.
console mouse-button 3 1230 161
expect focus memory
expect nav[2].place.address {code}
key escape
# Data lens context is scrollable in both directions without altering its anchor.
console lens {head}
frames 40
expect lenses[0].address {head}
expect lenses[0].first {(head & ~15) - 32}
wheel 1600 220 -2
frames 40
expect lenses[0].first {(head & ~15) + 64}
screenshot {out / 'memory-context.png'}
wheel 1600 220 2
frames 40
expect lenses[0].first {(head & ~15) - 32}
# Same-parent pointer recursion preserves ancestors and detects a cycle.
click 1230 161
expect lens_count 2
expect lenses[1].cycle true
frames 30
click 2180 350
expect lens_count 1
key escape
console goto memory {head}
frames 40
# Both controls are available directly on the memory pane.
click 1240 822
expect unit 2
click 1240 822
expect unit 4
click 1240 822
expect unit 8
click 1278 822
expect number_format unsigned
click 1278 822
expect number_format signed
click 1278 822
expect number_format float
click 1278 822
expect number_format hex
'''
# Pointer hits cover the full eight-byte field, independently of grouping.
for unit in (1, 2, 4, 8):
commands += f'''console memory-format {unit} hex
console goto memory {head}
frames 40
click 1270 863
expect lens_count 1
expect lenses[0].address {symbols['middle']}
key escape
console mouse-button 3 1270 863
expect focus memory
expect nav[2].place.address {symbols['middle']}
'''
commands += f'''console memory-format 1 hex
console goto memory {head}
frames 40
# The last (zero) byte of a little-endian pointer follows the full pointer too.
click 1480 863
expect lenses[0].address {symbols['middle']}
frames 30
# The pointer's bytes in a recursive lens use the same shared hit geometry.
click 1250 231
expect lens_count 2
expect lenses[1].address {symbols['tail']}
key escape
# A stack pointer stored in ordinary data routes by its target mapping.
console goto memory {symbols['stack_pointer']}
frames 40
click {1270 + (symbols['stack_pointer'] % 16) * 30 + (12 if symbols['stack_pointer'] % 16 >= 8 else 0)} 863
expect lens_count 1
print lenses[0].address
key escape
console mouse-button 3 {1270 + (symbols['stack_pointer'] % 16) * 30 + (12 if symbols['stack_pointer'] % 16 >= 8 else 0)} 863
expect focus stack
print nav[1].place.address
expect nav[1].place.follow false
expect execution 1
console goto memory {head}
frames 40
'''
for unit in (1, 2, 4, 8):
for fmt in ('hex', 'unsigned', 'signed') + (('float',) if unit >= 4 else ()):
commands += f'console memory-format {unit} {fmt}\nexpect unit {unit}\nexpect number_format {fmt}\nframes 2\n'
commands += f'''console memory-format 8 hex
frames 10
screenshot {out / 'words.png'}
# Historical browsing never requests bytes from a live target.
key left_bracket
expect following false
print stops
console lens {head}
frames 30
wheel 1600 220 -20
frames 30
print stops
expect execution 1
key escape
expect-no-log CommandFailed
expect-no-log backdrop shader
'''
script.write_text(commands)
args = [str(a.gui), '--fresh', '--size', '2240x1260', '--state', str(out / 'session.state'), '--script', str(script)]
if not a.native:
args.append('--headless')
run = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
stdout, stderr = run.communicate(timeout=100)
(out / 'run.log').write_text(stdout + stderr)
assert run.returncode == 0, stderr
first = int(re.search(r'lenses\[0\].first = (\d+)', stderr)[1])
assert first > code, (first, code)
stack_addresses = re.findall(r'(?:lenses\[0\].address|nav\[1\].place.address) = (\d+)', stderr)
assert len(stack_addresses) == 2 and stack_addresses[0] == stack_addresses[1], stack_addresses
stops = re.findall(r'stops = (\d+)', stderr)
assert len(stops) == 2 and stops[0] == stops[1], stops
for recording in Path('/tmp').glob(f'ztrace-gui-{run.pid}-*.ztrace'):
recording.unlink()
print(('Native' if a.native else 'Headless') + ': code/data lenses, context scrolling, pointer recursion, memory formats and historical isolation passed')
|