summaryrefslogtreecommitdiff
path: root/gui/test/lenses.py
blob: e42e6fef07bf14240a8c50297fa66065e8b1fc3d (plain) (blame)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/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
# Offsets, including negative/zero/positive ones, address their stack storage.
move 0 0
frames 30
screenshot {out / 'offset-before.png'}
move 185 863
frames 30
screenshot {out / 'offset-hover.png'}
expect-different-screenshot {out / 'offset-before.png'} {out / 'offset-hover.png'} 156 852 42 24
click 250 863
print lenses[0].address
key escape
click 185 863
print lenses[0].address
key escape
console mouse-button 3 185 863
expect focus stack
print nav[1].place.address
expect nav[1].place.follow false
key f
frames 40
click 185 935
print lenses[0].address
key escape
click 185 959
print lenses[0].address
key escape
expect execution 1
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
# Computed operands use this stop's registers; future register operands stay inert.
hold left_shift
key f
release left_shift
console break operand_stack
key c
await execution 2
expect pc {symbols['operand_stack']}
console set source false
console set annotations false
console goto code {symbols['operand_stack']}
frames 40
click 400 180
expect lens_count 0
move 0 0
frames 30
screenshot {out / 'operand-before.png'}
move 400 156
frames 30
screenshot {out / 'operand-hover.png'}
expect-different-screenshot {out / 'operand-before.png'} {out / 'operand-hover.png'} 348 144 108 24
click 400 156
expect lens_count 1
print lenses[0].address
key escape
console mouse-button 3 400 156
expect focus stack
print nav[1].place.address
# RIP-relative storage is independent of future register values.
click 400 204
expect lenses[0].address {head}
key escape
console mouse-button 3 400 204
expect focus memory
expect nav[2].place.address {head}
key s
await execution 3
expect pc {symbols['operand_indexed']}
frames 40
click 400 180
expect lenses[0].address {head + 16}
key escape
console mouse-button 3 400 180
expect focus memory
expect nav[2].place.address {head + 16}
# Register hints use the displayed value and the same address interactions.
console set annotations true
frames 40
move 0 0
frames 30
screenshot {out / 'hint-before.png'}
move 640 180
frames 30
screenshot {out / 'hint-hover.png'}
expect-different-screenshot {out / 'hint-before.png'} {out / 'hint-hover.png'} 612 168 144 24
click 640 180
expect lens_count 1
print lenses[0].address
key escape
console mouse-button 3 640 180
expect focus stack
print nav[1].place.address
click 850 180
expect lenses[0].address {head}
key escape
console mouse-button 3 850 180
expect focus memory
expect nav[2].place.address {head}
# The scalar index is not an address.
click 1000 180
expect lens_count 0
console set annotations false
# Historical operands use historical registers, without another execution.
key left_bracket
expect following false
console goto code {symbols['operand_stack']}
frames 40
click 400 156
print lenses[0].address
key escape
expect execution 3
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) == 12, stack_addresses
    base, negative, followed, zero, positive, pointer, pointer_followed, operand, operand_followed, hint, hint_followed, historical = map(int, stack_addresses)
    assert base == negative == followed and zero == base + 24 and positive == base + 32, stack_addresses
    assert pointer == pointer_followed, stack_addresses
    assert operand == operand_followed == hint == hint_followed == historical == pointer + 8, 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') + ': lenses, stack offsets, computed operands, pointer bytes, formats and historical isolation passed')