Repository navigation
Expand file tree
/
Copy pathtest_scdecomp.py
More file actions
executable file
·3274 lines (2977 loc) · 187 KB
/
Copy pathtest_scdecomp.py
File metadata and controls
executable file
·3274 lines (2977 loc) · 187 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Canary for the SOCOM II decomp harness (the shape of claude-eqoa's test_eqdecomp.py).
A checker that cannot fail reports success having compared nothing. Every test here
therefore pairs a positive control with a negative one where that is possible, and
the runner prints the number of assertions actually executed -- an inert suite shows
up as a low count rather than as a pass.
Run: ./test_scdecomp.py (also works under pytest)
"""
from __future__ import annotations
import os
import shutil
import struct
import sys
import tempfile
from dataclasses import replace
from pathlib import Path
HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
sys.path.insert(0, str(HERE / "tools"))
import scdecomp as E
import scdiff as D
import sccheck as C
import seed_symbols as S
CHECKS = 0
ZLIB = HERE / "src" / "zlib"
BUILD = HERE / "build" / "test"
def ck(cond, msg):
global CHECKS
CHECKS += 1
assert cond, msg
class gcc_absolute:
"""GCC built by the old absolute paths inside the block (scdiff.compile_obj's SC_GCC_ABSOLUTE): what makes
a unit's code a function of where the tree lies, for the negatives and for objcmp's padded sides."""
def __enter__(self):
os.environ["SC_GCC_ABSOLUTE"] = "1"
def __exit__(self, *exc):
del os.environ["SC_GCC_ABSOLUTE"]
def build(src: Path, name: str, compiler: str = D.COMPILER, flags=None) -> Path:
obj = BUILD / f"{name}.o"
D.compile_obj(src, obj, compiler, flags, [HERE / "include", src.parent])
return obj
# --- the target ---------------------------------------------------------------
def test_inputs_are_pinned():
"""The three pieces load, and a one-byte change to an overlay is refused."""
img = E.image()
ck([s.name for s in img.segments] == ["MAIN", "OVL1", "OVL2"], "MAIN + OVL1 + OVL2")
with tempfile.TemporaryDirectory(dir=HERE / "build") as d:
for f in ("ftscore.bin", "zsealetc.bin"):
shutil.copy(E.OVERLAYS / f, Path(d) / f)
b = bytearray((Path(d) / "zsealetc.bin").read_bytes())
b[0x1000] ^= 1
(Path(d) / "zsealetc.bin").write_bytes(b)
try:
E.Image(overlays=Path(d))
ck(False, "a tampered overlay must be refused")
except RuntimeError:
ck(True, "")
def test_mwo3_header_and_code_ranges():
"""The header is 0x40 bytes and code starts right after it; OVL1's text ends in VU1
microcode, which is not R5900 code and must not be swept."""
img = E.image()
o1, o2 = img.segments[1], img.segments[2]
ck(o1.header.name == "FTSCore.bin" and o1.header.kind == 1, "OVL1 is FTSCore")
ck(o2.header.name == "ZSealEtc.bin" and o2.header.kind == 3, "OVL2 is ZSealEtc")
ck(o1.code[0] == 0x1E7040 and o2.code[0] == 0x4C53C0, "code starts at base+0x40 (0x4C53C0 is the boot's entry)")
w = img.word(0x1E7040)
ck(w >> 16 == 0x27BD, "OVL1's first word is a prologue")
ck(o1.code[1] == 0x3D5980 and o1.vu == (0x3D5980, 0x3DBFF8), "OVL1: code ends 0x3D5980, microcode to text end")
ck(img.word(0x3D5944) != 0 and img.word(0x3D5948) == 0, "the last R5900 function's delay slot is 0x3D5944")
def test_vu_tail_has_no_false_positive_on_r5900_code():
"""The microcode detector must say 'none' over a span that is all R5900 code."""
img = E.image()
m = img.segments[0]
ck(E.vu_tail(m.data, m.base, 0x1C5000, 0x1CA3D0) == 0x1CA3D0, "no VU1 microcode in MAIN's zlib/loader code")
def test_disasm_keeps_every_word():
"""objdump collapses zero runs to '...' in binary mode; every word must come back."""
img, st = E.image(), E.symbols()
s = st.lookup("0x307270") # CQuat::Normalize: nop nop before the jal
rows = E.disasm(img.read(s.addr, s.size), s.addr)
ck(len(rows) == s.size // 4, f"{len(rows)} rows for {s.size // 4} words")
ck([a for a, _, _ in rows] == list(range(s.addr, s.end, 4)), "no address skipped")
zeros = E.disasm(bytes(32), 0)
ck(len(zeros) == 8 and all(w == 0 for _, w, _ in zeros), "eight zero words disassemble as eight nops")
# --- the map --------------------------------------------------------------------
def test_symbol_map_is_sane():
img, st = E.image(), E.symbols()
ck(len(st) > 13000, f"the seeded map has {len(st)} rows")
prev = None
for s in st.symbols:
if prev is not None and prev.addr != s.addr:
assert prev.end <= s.addr, f"{prev.name} 0x{prev.addr:08X}+0x{prev.size:X} overlaps {s.name}"
prev = s
ck(True, "no two rows overlap")
ck(all(s.size > 0 for s in st.symbols), "every row has a size")
ck(all(img.segment(s.addr) is not None for s in st.symbols), "every row is inside the image")
def test_mangled_names_spell_their_lengths():
"""Every mangled row's length prefixes agree with its name (tools/manglecheck.py, GCC round
23a): a row spelled with a wrong length is a name no unit can define. Negatives: the UI
lane's round-16 slip (70 for 72, unbalanced) is caught and corrected, and a length that
swallows the const qualifier (`9bad_castCFv`, still well-formed) is suspect."""
import manglecheck as MC
rows = [r for r in MC.map_rows([E.SYMBOLS, E.DATA]) if MC.claims_mangled(r[3])]
ck(len(rows) > 7000, f"{len(rows)} mangled rows read")
bad = [r[3] for r in rows if not MC.parses(r[3])]
ck(not bad, f"{len(bad)} mangled row(s) whose lengths do not parse: {bad[:3]}")
voc = MC.vocabulary(r[3] for r in rows)
sus = [r[3] for r in rows if MC.suspects(r[3], voc)]
ck(not sus, f"{len(sus)} mangled row(s) spelling a name nowhere else spelled: {sus[:3]}")
slip = "clear__Q23std70__vector_deleter<15CDlgDesignModel,Q23std28allocator<15CDlgDesignModel>>Fv"
ck(not MC.parses(slip), "the UI's slip does not parse")
ck(MC.corrections(slip) == [slip.replace("std70", "std72")], "and its one correction is 72")
ck(MC.parses("what__Q23std9bad_castCFv") and MC.suspects("what__Q23std9bad_castCFv", voc),
"a length one too long that swallows `C` is suspect")
ck(not MC.parses("__dt__Q23std14length_errorFv"), "a destructor is no static datum")
ck(MC.parses("@GUARD@id__Q23std8ctype<w>") and not MC.parses("@GUARD@id__Q23std9ctype<w>"),
"a class template static member's guard parses, and its length is still checked")
ck(not MC.parses("@LOCAL@id__Q23std8ctype<w>"), "a function-local static still needs its function and name")
def test_sweep_finds_the_hard_boundaries():
"""Three boundaries the first drafts got wrong, each now pinned."""
img = E.image()
regs, starts, found = S.run(img)
got = {a: b - a for _, a, b in found}
ck(got.get(0x180E00) == 0x10, "the 16-byte `j` thunk at 0x180E00 ends at its delay slot")
ck(0x1E7100 in got, "OVL1's entry loops forever and is followed by 0xFFFFFFFF; 0x1E7100 is a function")
ck(got.get(0x1C5DA0) == 0x114, "the loader's overlay decode is 0x114 bytes")
ck(not any(img.segments[1].vu[0] <= a < img.segments[1].vu[1] for a in got), "no function inside the microcode")
# --- the toolchain ----------------------------------------------------------------
def test_toolchain_is_settled_on_zlib():
"""zlib 1.1.4, unmodified: the settled build matches; 3.0b22-020926 -- the build the
ELF's '2.4.1.01' .comment points at -- does not, at the same flags."""
img, st = E.image(), E.symbols()
good = build(ZLIB / "adler32.c", "adler32_good")
res = D.compare(good, "adler32", st.get("adler32"), img, st)
ck(res.match, f"adler32 with {D.COMPILER}: {res.verdict}")
bad = build(ZLIB / "adler32.c", "adler32_020926", compiler="mwcps2-3.0b22-020926")
res = D.compare(bad, "adler32", st.get("adler32"), img, st)
ck(not res.match, "adler32 with 3.0b22-020926 must NOT match (it misses the short-loop nops)")
# -O3,p still matches 18 of zlib's 21 (inflate among them); inflate_fast is one it misses
o3 = build(ZLIB / "inffast.c", "inffast_O3", flags=[f if f != "-O4,p" else "-O3,p" for f in D.FLAGS])
res = D.compare(o3, "inflate_fast", st.get("inflate_fast"), img, st)
ck(not res.match, "inflate_fast at -O3,p must NOT match")
o4 = build(ZLIB / "inffast.c", "inffast_O4")
res = D.compare(o4, "inflate_fast", st.get("inflate_fast"), img, st)
ck(res.match, "inflate_fast at -O4,p matches")
def test_directive_takes_effect_or_refuses():
"""A unit's `sc: compiler=` without `flags=` builds under that toolchain at the default flags
(the kit lane's question, GCC round 16a): adler32 under 3.0b22-020926 must miss as above, built
as sccheck builds a unit. And a directive the tools cannot read is a build failure, not the
defaults in silence: one past the third line, an unknown key, a toolchain that is not there."""
img, st = E.image(), E.symbols()
tree = BUILD / f"directive.{os.getpid()}"
shutil.rmtree(tree, ignore_errors=True)
shutil.copytree(ZLIB, tree, ignore=shutil.ignore_patterns("*.c", "*.md"))
base = (ZLIB / "adler32.c").read_text()
unit = tree / "adler32.c"
def built(text):
unit.write_text(text)
_, obj, err = C.build_one(unit, tree / "obj", root=tree)
return obj, err
try:
obj, err = built(base)
ck(obj is not None and D.compare(obj, "adler32", st.get("adler32"), img, st).match,
f"no directive: the settled build matches ({err})")
obj, err = built("/* sc: compiler=mwcps2-3.0b22-020926 */\n" + base)
ck(obj is not None, f"compiler= alone builds: {err}")
ck(not D.compare(obj, "adler32", st.get("adler32"), img, st).match,
"compiler=mwcps2-3.0b22-020926 alone takes effect (adler32 misses)")
for text, why in (("/*\n*\n*/\n/* sc: compiler=mwcps2-3.0b22-020926 */\n" + base, "past the third line"),
("/* sc: compilr=mwcps2-3.0b22-020926 */\n" + base, "unknown `sc:` key"),
("/* sc: compiler=mwcps2-NOPE */\n" + base, "no such toolchain")):
obj, err = built(text)
ck(obj is None and why in err, f"a directive {why} is refused: {err!r}")
finally:
shutil.rmtree(tree, ignore_errors=True)
def test_relocation_check_catches_a_wrong_callee():
"""Masking alone accepts a call to the wrong function; the resolver must not."""
img, st = E.image(), E.symbols()
src = (HERE / "src" / "main" / "loader.c").read_text()
with tempfile.TemporaryDirectory(dir=HERE / "build") as d:
good = Path(d) / "good.c"
good.write_text(src)
bad = Path(d) / "bad.c"
bad.write_text(src.replace("func_001C5EC0", "func_001C5EC4"))
sym = st.get("func_001C5DA0")
r = D.compare(build(good, "loader_good"), "func_001C5DA0", sym, img, st)
ck(r.match, f"the loader matches: {r.verdict}")
r = D.compare(build(bad, "loader_bad"), "func_001C5DA0", sym, img, st)
ck(r.masked_equal and not r.match, "same bytes, wrong callee: WRONG REFERENCE")
ck(any(f.status == "MISMATCH" and f.want == 0x1C5EC0 for f in r.relocs), "the bad call is named")
def test_jump_table_is_checked_by_target():
"""inflate_blocks' switch table must point into inflate_blocks at the same offsets;
corrupt one entry in a copy of the image and the check must fail."""
img, st = E.image(), E.symbols()
obj = build(ZLIB / "infblock.c", "infblock")
sym = st.get("inflate_blocks")
r = D.compare(obj, "inflate_blocks", sym, img, st)
tables = [f for f in r.relocs if f.symbol.endswith("(local)") and f.status == "content-ok"]
ck(r.match and tables, "the jump table resolves into the function")
seg = img.segments[0]
table = tables[0].want
bad = bytearray(seg.data)
struct.pack_into("<I", bad, table - seg.base, sym.addr + 0x10)
img2 = E.Image.__new__(E.Image)
img2.segments = [replace(seg, data=bytes(bad))] + img.segments[1:]
r = D.compare(obj, "inflate_blocks", sym, img2, st)
ck(not r.match and any(f.status == "CONTENT-MISMATCH" for f in r.relocs), "a corrupted entry is caught")
def _image_with_word(img: E.Image, addr: int, value: int) -> E.Image:
"""A copy of the image with one word changed."""
segs = []
for seg in img.segments:
if seg.base <= addr < seg.base + len(seg.data):
bad = bytearray(seg.data)
struct.pack_into("<I", bad, addr - seg.base, value)
seg = replace(seg, data=bytes(bad))
segs.append(seg)
img2 = E.Image.__new__(E.Image)
img2.segments = segs
return img2
def test_gnu_table_is_compared_whole():
"""A GNU object relocates its statics against the section, so only the label's size says how
far a table reaches. FillLanguageVirtualTable copies the recogniser's 79-slot _LangVirtualTable,
whose first slot is null: read as a string, it was compared over one byte (round 6a). Whole, its
slots resolve through the candidate's own functions by name (language.c's OPEN functions compile
to other sizes, so 73 slots sit at other distances than the target's): a slot pointing elsewhere
must fail. And a GNU float literal is compared over the width its load reads -- the codec's
1.40234375f (0x3FB38000) starts with a zero byte, which the string rule read as the whole."""
img, st = E.image(), E.symbols()
lang = HERE / "src" / "lib" / "asr" / "language.c"
opts = C.tu_options(lang)
obj = build(lang, "language", opts["compiler"], opts["flags"].split())
D.qualify_statics(obj, lang.stem, st)
sym = st.get("FillLanguageVirtualTable")
r = D.compare(obj, sym.name, sym, img, st)
table = st.data_by_name["_LangVirtualTable"]
ck(r.match and any(f.symbol == "_LangVirtualTable" and f.status == "ok" for f in r.relocs),
f"the table resolves, slot by slot: {r.verdict}")
slot40 = table.addr + 40 * 4
r = D.compare(obj, sym.name, sym, _image_with_word(img, slot40, img.word(slot40) + 8), st)
ck(not r.match and any(f.status == "CONTENT-MISMATCH" for f in r.relocs), "a slot naming another address fails")
nelly = HERE / "src" / "lib" / "ovl1" / "nelly.c"
opts = C.tu_options(nelly)
obj = build(nelly, "nelly", opts["compiler"], opts["flags"].split())
sym = st.get("func_00251700")
lits = [f.want for f in D.compare(obj, sym.name, sym, img, st).relocs if f.type == "R_MIPS_LITERAL"]
lit = next(a for a in lits if img.word(a) == 0x3FB38000)
r = D.compare(obj, sym.name, sym, _image_with_word(img, lit, 0x3FB48000), st)
ck(not r.match and any(f.status == "CONTENT-MISMATCH" for f in r.relocs), "a float literal's high bytes count")
def test_gnu_literal_is_its_own_entry():
"""A GNU .lit4 reference is compared over its own entry: the object's pool holds its constants
in first-use order, the image's in the link's (round 12a). QP0SC3's dequantiser loads
0.0117479693 (0x3C407A8E at 0x1D544C) and 5.0751734 (0x1D5444); the word after the first is
another function's constant -- changing it must not matter, changing the entry itself must fail."""
img, st = E.image(), E.symbols()
unit = HERE / "src" / "lib" / "ovl1" / "sase_misc.c"
opts = C.tu_options(unit)
obj = build(unit, "sase_misc", opts["compiler"], opts["flags"].split())
sym = st.get("func_00250C68")
r = D.compare(obj, sym.name, sym, img, st)
ck(r.match, f"the dequantiser's literals each match their entry: {r.verdict}")
r = D.compare(obj, sym.name, sym, _image_with_word(img, 0x1D5450, img.word(0x1D5450) ^ 0x100), st)
ck(r.match, "the next entry, another function's constant, is not this reference's")
r = D.compare(obj, sym.name, sym, _image_with_word(img, 0x1D544C, 0x3C407A8F), st)
ck(not r.match and any(f.status == "CONTENT-MISMATCH" for f in r.relocs), "the entry itself counts")
def test_gnu_code_label_resolves_by_the_map():
"""An asm's data label inside a GNU object's code (`.text`+N: GNU as relocates the local label
against its section) resolves through the data map when it names the label: libmpeg's
_copyAddRefImage loads _maxval, the clamp constant _copyRefImage's asm carries. By distance the
reference went unchecked while the unit's layout before it is short; by the label it is checked
-- and the label mapped elsewhere must fail."""
img, st = E.image(), E.symbols()
unit = HERE / "src" / "lib" / "sdk" / "mpeg_dec.c"
opts = C.tu_options(unit)
obj = build(unit, "mpeg_dec", opts["compiler"], opts["flags"].split())
sym = st.get("_copyAddRefImage")
r = D.compare(obj, sym.name, sym, img, st)
ck(r.match and any(f.symbol == "_maxval" and f.status == "ok" for f in r.relocs),
f"_maxval resolves by its data row: {r.verdict}")
moved = HERE / "build" / "tmp" / f"data_moved.{os.getpid()}.txt"
moved.parent.mkdir(parents=True, exist_ok=True)
moved.write_text(E.DATA.read_text().replace("0x001B6E90 0x0 _maxval", "0x001B6EA0 0x0 _maxval"))
try:
r = D.compare(obj, sym.name, sym, img, E.SymbolTable(E.SYMBOLS, moved))
finally:
moved.unlink()
ck(not r.match and any(f.symbol == "_maxval" and f.status == "MISMATCH" for f in r.relocs),
"the label mapped elsewhere fails")
def test_shared_stem_qualifies_by_path():
"""A GNU unit's statics score as `NAME@UNIT` rows; libkernl's ExecPS2 object (sdk/exit.c) and
newlib's (libc/exit.c) share the stem `exit`, so a bare `kCopy@exit` would score either
object's static `kCopy` as the one row. A shared stem qualifies only by a tail of the unit's
path (`kCopy@sdk/exit`): sdk/exit.c's static resolves so, libc/exit.c's does not, and the
bare-stem row no longer resolves for either -- while a unique stem (tlbfunc) still does."""
st = E.symbols()
sdk, libc = HERE / "src" / "lib" / "sdk" / "exit.c", HERE / "src" / "lib" / "libc" / "exit.c"
ck(D.unit_qualifiers(sdk) == ["lib/sdk/exit", "sdk/exit"], f"no bare stem: {D.unit_qualifiers(sdk)}")
ck("exit" not in D.unit_qualifiers(libc), "nor for the other unit of that stem")
ck(D.unit_qualifiers(HERE / "src" / "lib" / "sdk" / "tlbfunc.c")[-1] == "tlbfunc", "a unique stem stands")
opts = C.tu_options(sdk)
obj = build(sdk, "exit", opts["compiler"], opts["flags"].split())
ck(("kCopy", "kCopy@sdk/exit") in D.qualify_statics(obj, sdk, st), "sdk/exit.c's kCopy is its row")
obj = build(sdk, "exit", opts["compiler"], opts["flags"].split())
ck(not any(b == "kCopy@sdk/exit" for _, b in D.qualify_statics(obj, libc, st)),
"the other unit of that stem does not take it")
rows = HERE / "build" / "tmp" / f"syms_bare.{os.getpid()}.txt"
rows.parent.mkdir(parents=True, exist_ok=True)
rows.write_text(E.SYMBOLS.read_text().replace(" kCopy@sdk/exit ", " kCopy@exit "))
try:
obj = build(sdk, "exit", opts["compiler"], opts["flags"].split())
bare = D.qualify_statics(obj, sdk, E.SymbolTable(rows, E.DATA))
finally:
rows.unlink()
ck(not any(b == "kCopy@exit" for _, b in bare), "a bare row of a shared stem resolves for neither")
def test_mw_statics_qualify_by_unit():
"""Two Metrowerks units' file statics can carry one name: SOCOM 1 calls both grid_di.cpp's and
grid_clutter.cpp's `static CStack` `stack`, MW names each object's LOCAL symbol so, and the data
map holds grid_di's bare and grid_clutter's as `stack@grid_clutter` (scdiff.qualify_statics, GNU
objects' only before GCC round 25a, so the clutter's was spelt `clutterStack`). Built under
SOCOM 1's name, grid_clutter's initialiser resolves its static through the qualified row; with
no qualified row the bare name is grid_di's and the reference fails, so does the qualified row
moved -- and grid_di.cpp's own `stack` still scores bare."""
import re
img, st = E.image(), E.symbols()
znode = HERE / "src" / "gamez" / "znode"
text = (znode / "grid_clutter.cpp").read_text()
var = re.search(r"static CStack (\w+)\(false\);", text).group(1)
tmp = HERE / "build" / "tmp" / f"qual.{os.getpid()}"
tmp.mkdir(parents=True, exist_ok=True)
unit = tmp / "grid_clutter.cpp"
unit.write_text(re.sub(rf"\b{var}\b", "stack", text))
data = E.DATA.read_text()
clutter = next(l for l in data.splitlines(True) if l.startswith("0x0044F0F0 "))
rows = tmp / "data.txt"
try:
def table(row: str) -> E.SymbolTable:
rows.write_text(data.replace(clutter, row))
return E.SymbolTable(E.SYMBOLS, rows)
def score(row: str | None):
obj = BUILD / "grid_clutter_q.o"
D.compile_obj(unit, obj, includes=[HERE / "include", HERE / "src", znode])
t = table(row) if row is not None else st
pairs = D.qualify_statics(obj, unit, t)
sym = st.get("__sinit_grid_clutter.cpp")
return pairs, D.compare(obj, sym.name, sym, img, t)
qual = "0x0044F0F0 0x1 stack@grid_clutter # the canary's\n"
pairs, r = score(qual)
ck(("stack", "stack@grid_clutter") in pairs, f"the clutter's `stack` is its unit's row: {pairs}")
ck(r.match and any(f.symbol == "stack@grid_clutter" and f.want == 0x44F0F0 and f.status == "ok"
for f in r.relocs), f"and its initialiser resolves it there: {r.verdict}")
# the map itself holds the qualified row since the engine core's round 6 renamed the
# static: take it out (an unqualified name at 0x44F0F0) rather than score the map as is
pairs, r = score("0x0044F0F0 0x1 clutterStack # the canary's: no qualified row\n")
ck(not pairs and not r.match and any(f.symbol == "stack" and f.got == 0x44D750 and f.status == "MISMATCH"
for f in r.relocs), "with no qualified row, the bare name is grid_di's: fails")
pairs, r = score(qual.replace("0x0044F0F0", "0x0044F100"))
ck(not r.match and any(f.symbol == "stack@grid_clutter" and f.status == "MISMATCH" for f in r.relocs),
"the qualified row moved fails")
obj = BUILD / "grid_di_q.o"
D.compile_obj(znode / "grid_di.cpp", obj, includes=[HERE / "include", HERE / "src"])
t = table(qual)
ck(not any(a == "stack" for a, _ in D.qualify_statics(obj, znode / "grid_di.cpp", t)),
"grid_di.cpp's own static is not renamed")
sym = st.get("__sinit_grid_di.cpp")
r = D.compare(obj, sym.name, sym, img, t)
ck(r.match and any(f.symbol == "stack" and f.want == 0x44D750 and f.status == "ok" for f in r.relocs),
f"and still resolves bare: {r.verdict}")
finally:
shutil.rmtree(tmp, ignore_errors=True)
def test_mw_function_static_resolves_by_its_function():
"""Metrowerks names a function-local static of a function emitted once `name$N` and its guard
`@GUARD@name$N`, N a per-unit counter no map row can carry (an inline function's own are
`@LOCAL@<fn>@name`, `@GUARD@<fn>@name`). The data map names both kinds the second way, and the
first resolves through the compared function's row: the HUD lane's ShowArrows reads its
`static CPnt3D s_grey` under its guard (8 references, unchecked before). Its rows moved in the
map must fail; its rows gone, the references are unchecked again -- never passed."""
img, st = E.image(), E.symbols()
unit = HERE / "src" / "Apps" / "FTS" / "hud_mpweaponsel.cpp"
obj = BUILD / "hud_mpweaponsel.o"
D.compile_obj(unit, obj, includes=[HERE / "include", HERE / "src"])
sym = st.get("ShowArrows__19CMPWeaponCategoriesFv")
guard, local = (f"@{k}@{sym.name}@s_grey" for k in ("GUARD", "LOCAL"))
r = D.compare(obj, sym.name, sym, img, st)
ck(r.match and not r.reloc_unchecked, f"s_grey and its guard resolve by their rows: {r.verdict}")
ck(sum(f.symbol == guard and f.status == "ok" for f in r.relocs) == 2
and sum(f.symbol == local and f.status == "ok" for f in r.relocs) == 6, "two guard, six object references")
rows = HERE / "build" / "tmp" / f"data_fnstatic.{os.getpid()}.txt"
rows.parent.mkdir(parents=True, exist_ok=True)
try:
rows.write_text(E.DATA.read_text().replace(f"0x004147D0 0xC {local} ", f"0x004147E0 0xC {local} "))
r = D.compare(obj, sym.name, sym, img, E.SymbolTable(E.SYMBOLS, rows))
ck(not r.match and any(f.symbol == local and f.status == "MISMATCH" for f in r.relocs),
"the object mapped elsewhere fails")
rows.write_text("".join(l for l in E.DATA.read_text().splitlines(True) if "@s_grey " not in l))
r = D.compare(obj, sym.name, sym, img, E.SymbolTable(E.SYMBOLS, rows))
ck(r.match and len(r.reloc_unchecked) == 8, "with no rows, unchecked as before")
finally:
rows.unlink()
def test_mw_chain_record_resolves_by_its_object():
"""GCC round 37a (the kit's round 51): Metrowerks registers a static object with a destructor by
__register_global_object(object, destructor, &record), the record an ANONYMOUS 12-byte `@N` in the
.bss section right before the object's (a function-local static: its guard, the record, the object
-- IssueCommand's 0x66B8C0, 0x66B8C8, 0x66B8E0). The data map names it `@CHAIN@<fn>@<name>` or, for
a file-scope object, `@CHAIN@<object>`, and a reference resolves by that row. std::locale::global
(0x18CF20, byte-exact) registers its function-local global_locale with the record at 0x1D5F38, and
__sinit_camera.cpp (0x3FFDD0) its four file-scope DiIntersects dilr0-3 with theirs, 16 bytes before
each. Negatives: a row moved fails; with no row the record is unchecked, as before; a row naming
another object's record fails; no registering call, no row taken; and a slipped length in the new
name form does not parse."""
import manglecheck as M
img = E.image()
rows = HERE / "build" / "tmp" / f"data_chain.{os.getpid()}.txt"
rows.parent.mkdir(parents=True, exist_ok=True)
def scored(unit: str, fn: str, extra: str):
rows.write_text(E.DATA.read_text() + extra)
st = E.SymbolTable(E.SYMBOLS, rows)
_, obj, err = C.build_one(HERE / "src" / unit, BUILD / "chain")
assert obj is not None, err
return D.compare(obj, fn, st.get(fn), img, st)
loc = "@CHAIN@global__Q23std6localeFv@global_locale"
try:
r = scored("main/msl/locale.cpp", "global__Q23std6localeFv", f"0x001D5F38 0xC {loc} # test\n")
ck(r.match and any(f.symbol == loc and f.want == 0x1D5F38 and f.status == "ok" for f in r.relocs),
f"a function-local static's record resolves by its row: {r.verdict}")
unchecked = len(r.reloc_unchecked)
r = scored("main/msl/locale.cpp", "global__Q23std6localeFv", f"0x001D5F48 0xC {loc} # test\n")
ck(not r.match and any(f.symbol == loc and f.status == "MISMATCH" for f in r.relocs),
"the row moved fails")
r = scored("main/msl/locale.cpp", "global__Q23std6localeFv", "")
ck(r.match and len(r.reloc_unchecked) == unchecked + 1
and any(f.symbol.startswith("@") and f.symbol.endswith("(local)") and f.want == 0x1D5F38
for f in r.reloc_unchecked), "with no row, unchecked as before")
cam = "".join(f"0x{a:08X} 0xC @CHAIN@dilr{i} # test\n"
for i, a in enumerate((0x416040, 0x4160A0, 0x416100, 0x416160)))
r = scored("Apps/FTS/camera.cpp", "__sinit_camera.cpp", cam)
ck(r.match and sorted(f.want for f in r.relocs if f.symbol.startswith("@CHAIN@dilr") and f.status == "ok")
== [0x416040, 0x4160A0, 0x416100, 0x416160], f"four file-scope records by their objects' rows: {r.verdict}")
swapped = cam.replace("@CHAIN@dilr0", "@CHAIN@dilrX").replace("@CHAIN@dilr1", "@CHAIN@dilr0") \
.replace("@CHAIN@dilrX", "@CHAIN@dilr1")
r = scored("Apps/FTS/camera.cpp", "__sinit_camera.cpp", swapped)
ck(not r.match and sum(f.status == "MISMATCH" for f in r.relocs) == 2, "two objects' rows swapped fail")
rows.write_text(E.DATA.read_text() + f"0x001D5F38 0xC {loc} # test\n")
st = E.SymbolTable(E.SYMBOLS, rows)
elf = E.Elf.read(BUILD / "chain" / "main" / "msl" / "locale.o")
rec = next(s for s in elf.symbols if s.size == 12 and D._ANON_OBJECT.match(s.name)
and elf.symbols[0] is not s and elf.sections[s.shndx].type == 8
and any(x.shndx == s.shndx + 1 and x.name.startswith("global_locale$") for x in elf.symbols))
fsym = next(f for f in elf.functions() if f.name == "global__Q23std6localeFv")
ck(D.chain_record_row(elf, rec, fsym, st, 0x18CF20, True) == loc
and D.chain_record_row(elf, rec, fsym, st, 0x18CF20, False) is None,
"a record is taken only from a function that registers it")
finally:
rows.unlink(missing_ok=True)
ck(M.parses("@CHAIN@IssueCommand__9CSealUnitFP10CZSealBody10FT_COMMANDPvb@proto") and M.parses("@CHAIN@dilr0x__4CCam")
and not M.parses("@CHAIN@IssueCommand__8CSealUnitFP10CZSealBody10FT_COMMANDPvb@proto"),
"manglecheck reads both forms and a slipped length")
def test_mw_local_function_resolves_by_its_code():
"""A Metrowerks static initialiser registers each static array's destructor, a local function
`__arraydtor$N` -- N a per-unit counter that shifts with any header edit, so no map row can carry
it. hud_newhudmap.cpp's __sinit_hud_newhudmap.cpp (0x3FDE80) takes four such addresses; compared
as a local object (raw bytes, relocation fields included) each was a CONTENT-MISMATCH and the
code-exact initialiser WRONG REFERENCE (the HUD lane's round 30). Each resolves by its code now:
the candidate's destructor against the target's function at the referenced address, masked, its
own relocations resolved. The target's destructor with another count (an unrelocated word) must
fail, so must its array's %lo pointing 16 bytes on (masked alike, so only the destructor's own
relocation check can see it) -- and with the resolution switched off the four fail again."""
img, st = E.image(), E.symbols()
unit = HERE / "src" / "Apps" / "FTS" / "hud_newhudmap.cpp"
obj = BUILD / "hud_newhudmap.o"
D.compile_obj(unit, obj, includes=[HERE / "include", HERE / "src"])
sym = st.get("__sinit_hud_newhudmap.cpp")
dtors = lambda r: [f for f in r.relocs if f.symbol.startswith("__arraydtor$")]
r = D.compare(obj, sym.name, sym, img, st)
ck(r.match and len(dtors(r)) == 4 and all(f.status == "content-ok" for f in dtors(r)),
f"the four array destructors resolve by their code: {r.verdict}")
ck(sorted(f.want for f in dtors(r)) == [0x20CD30, 0x20CE10, 0x20CE30, 0x20CE50], "at their own rows")
for addr, word, what in ((0x20CE68, 0x24070008, "another count"), (0x20CE58, 0x24849F30, "the array 16 bytes on")):
bad = _image_with_word(img, addr, word)
r = D.compare(obj, sym.name, sym, bad, st)
ck(not r.match and any(f.want == 0x20CE50 and f.status == "CONTENT-MISMATCH" for f in r.relocs),
f"a destructor with {what} fails: {r.verdict}")
depth, D.LOCAL_CODE_DEPTH = D.LOCAL_CODE_DEPTH, 0
try:
r = D.compare(obj, sym.name, sym, img, st)
finally:
D.LOCAL_CODE_DEPTH = depth
ck(not r.match and sum(f.status == "CONTENT-MISMATCH" for f in r.relocs) == 4, "switched off, the four fail")
LOCAL_CALL_FIXTURE = """/* sc: compiler=ee-gcc2.9-991111-01-dtls13010; flags=-O2 -G0 */
typedef unsigned char u_char;
typedef struct {
u_char stat, second, minute, hour, pad, day, month, year;
} sceCdCLOCK;
static u_char ig_tobcd(u_char x)
{
return x + (x / 10) * 6;
}
void converttobcd(sceCdCLOCK *t)
{
t->year = ig_tobcd(t->year);
t->month = ig_tobcd(t->month);
t->day = ig_tobcd(t->day);
t->hour = ig_tobcd(t->hour);
t->minute = ig_tobcd(t->minute);
t->second = ig_tobcd(t->second);
}
"""
def test_local_call_is_checked_by_its_code():
"""A call to a TU-local function no map row names is checked by the callee's CODE (GCC round 42a,
the integrator's request: the zdb world's round 31 saw CShadowCaster "match" with a jal to the wrong
static, node_world.cpp's `static inline` NodeBBox -- local_code_ok said the code differed, by_code
returned None and the call stayed `unchecked`, which passes). Strictly now: content-ok when the
candidate's local function is the target's at the jal's address, CONTENT-MISMATCH when it is not --
its bytes differ, or the map has no row of its size there. GCC: libscf's converttobcd calling its
static tobcd under another name (no row; not at the target's distance) matches by tobcd's code, six
calls content-ok; the same caller calling a static whose body differs (`* 7`) -- its own bytes
unchanged -- does not, and under the old rule (SC_LOCAL_CALLS=lenient) it did. Metrowerks: ldimage.cpp
with its static setLoadImageTagsYX renamed (setLoadImageTags' tail call by code, content-ok at 0x30B770);
the target's jal retargeted to loadImage (a row of another size) or its callee's body changed by a
word: no MATCH, where the old rule matched both."""
img, st = E.image(), E.symbols()
BUILD.mkdir(parents=True, exist_ok=True)
def scored(src: Path, fn: str, image=None, home: Path | None = None):
if home is None: # a GCC fixture: built as sccheck builds it, its directive's
_, obj, err = C.build_one(src, BUILD / "localcall", root=src.parent)
assert obj is not None, f"{src.name} did not build: {err[-300:]}"
else: # a unit's copy: its own directory's headers on the path
obj = BUILD / "localcall" / f"{src.stem}.o"
D.compile_obj(src, obj, includes=[HERE / "include", HERE / "src", home])
r = D.compare(obj, fn, st.get(fn), image or img, st)
calls = [f for f in r.relocs if f.type == "R_MIPS_26"]
D.STRICT_LOCAL_CODE = False
try:
old = D.compare(obj, fn, st.get(fn), image or img, st).match
finally:
D.STRICT_LOCAL_CODE = True
return r, calls, old
gcc = BUILD / "ig_localcall.c"
gcc.write_text(LOCAL_CALL_FIXTURE)
r, calls, old = scored(gcc, "converttobcd")
ck(r.match and old and len(calls) == 6 and all(f.status == "content-ok" and f.want == st.get("tobcd").addr
for f in calls),
f"GCC: six calls to a static the map does not name resolve by its code: {r.verdict}, {[f.status for f in calls]}")
wrong = BUILD / "ig_localcall_wrong.c"
wrong.write_text(LOCAL_CALL_FIXTURE.replace("(x / 10) * 6", "(x / 10) * 7"))
r, calls, old = scored(wrong, "converttobcd")
ck(r.masked_equal and not r.match and calls and all(f.status == "CONTENT-MISMATCH" for f in calls),
f"GCC: calling a static whose code is not the target's callee's -- the caller's bytes the same -- fails: "
f"{r.verdict}")
ck(old, "and under the old rule (lenient) it matched: the integrity bug, reproduced")
unit = HERE / "src" / "gamez" / "zmpeg" / "ldimage.cpp"
mw = BUILD / "ig_ldimage.cpp"
mw.write_text(unit.read_text().replace("setLoadImageTagsYX", "igLocalTagsYX"))
fn = "setLoadImageTags__FPUiPviiUi"
r, calls, old = scored(mw, fn, home=unit.parent)
ck(r.match and old and len(calls) == 1 and calls[0].status == "content-ok" and calls[0].want == 0x30B770,
f"MW: the renamed static's call resolves by its code: {r.verdict}, {[(f.status, hex(f.want)) for f in calls]}")
row = st.get(fn)
a, w = next((a, w) for a, w, t in E.disasm(img.read(row.addr, row.size), row.addr) if t.startswith(("jal ", "j ")))
for image, what in ((_image_with_word(img, a, (w & ~0x03FFFFFF) | ((0x30B740 >> 2) & 0x03FFFFFF)),
"the jal retargeted to loadImage"),
(_image_with_word(img, 0x30B770, struct.unpack("<I", img.read(0x30B770, 4))[0] ^ 0x8),
"the callee's first word (its frame) changed")):
r, calls, old = scored(mw, fn, image, home=unit.parent)
ck(r.masked_equal and not r.match and calls and calls[0].status == "CONTENT-MISMATCH" and old,
f"MW: {what} -- no MATCH (the old rule matched): {r.verdict}")
def test_padding_after_a_function_must_be_zero():
"""A candidate one instruction short of a function whose next word is code fails."""
img, st = E.image(), E.symbols()
sym = st.get("uncompress")
short = E.Symbol(sym.name, sym.addr, sym.size - 4)
obj = build(ZLIB / "uncompr.c", "uncompr")
r = D.compare(obj, "uncompress", short, img, st)
ck(not r.match, "a wrong size in the map cannot match")
# --- SOCOM 1's demo, the name source --------------------------------------------------
def test_demo_relocations_land_on_their_fields():
"""`.relmain`'s r_offset is a virtual address: keyed one section base off, every exact
comparison masked words 1 MB away from the real fields (and named SetStance wrongly)."""
import s1names as N
if not N.DEMO.exists():
print(" (skipped: the May 2002 demo is not extracted)")
return
demo = N.Demo()
by_type = {}
for a, t in demo.relocs.items():
by_type.setdefault(t, []).append(struct.unpack_from("<I", demo.data, a - demo.base)[0] >> 26)
ck(len(by_type["R_MIPS_26"]) > 50000 and all(op in (2, 3) for op in by_type["R_MIPS_26"]),
"every R_MIPS_26 is on a j/jal")
ck(all(op == 0x0F for op in by_type["R_MIPS_HI16"]), "every R_MIPS_HI16 is on a lui")
ck(all(op == 0x09 or op >= 0x1A for op in by_type["R_MIPS_LO16"]), "every R_MIPS_LO16 is on an addiu or a load/store")
# negative control: one section base off, almost none would be
off = sum(1 for a, t in demo.relocs.items() if t == "R_MIPS_26" and a + 0x100000 - demo.base < len(demo.data)
and struct.unpack_from("<I", demo.data, a + 0x100000 - demo.base)[0] >> 26 in (2, 3))
ck(off < len(by_type["R_MIPS_26"]) // 10, "the same table read one base off lands on calls by chance only")
def test_exact_pass_refuses_a_thin_wrapper():
"""`return DoSetStance(x);` masks to the same words as every `return f(x);`: the exact
pass must not name it, and the callee pass must not inherit a name through it."""
import s1names as N
if not N.DEMO.exists():
print(" (skipped: the May 2002 demo is not extracted)")
return
img, st, demo = E.image(), E.symbols(), N.Demo()
exact, callee, _ = N.run(demo, img, st)
# 0x319580 is CSaveLoad::LoadAssetLib, a thin wrapper of LoadAssetLib_PS2: once the callee is
# named, the exact pass may pair it with SOCOM 1's own LoadAssetLib (its relocation names
# agree) -- but never with the SetStance wrapper its masked words also equal
ck("SetStance" not in exact.get(0x319580, ""), "the SetStance-shaped wrapper is not named SetStance")
ck("DoSetStance" not in callee.get(0x3199C0, ""), "nor is its callee named DoSetStance through it")
ck(exact.get(0x3070C0) == "Mul__5CQuatCFPC5CQuatP5CQuat", "positive control: CQuat::Mul is exact")
def test_vtables_are_found_by_their_rtti():
"""CNode's table is found from its RTTI's name alone, with SOCOM II's extra slot; its
destructor is recognised by writing the table; the aligner refuses to swap CBatchRelocator's
slots the way an anchor on the align pass's wrong StartBatchReloc once did."""
import s1names as N
import s1vtables as V
if not N.DEMO.exists():
print(" (skipped: the May 2002 demo is not extracted)")
return
img, st, demo = E.image(), E.symbols(), N.Demo()
tables = {cls: (vt, slots) for vt, _, cls, slots in V.our_vtables(img)}
vt, slots = tables.get("zdb::CNode", (0, []))
ck(vt == 0x4067B0 and len(slots) == 15, "CNode's vtable at 0x4067B0, 15 slots (SOCOM 1: 14)")
al = V.Aligner(img, st, demo, 0.5, 0.03)
ck(al.writes_vtable(slots[0], vt) if slots else False, "CNode's destructor stores its own vtable")
ck(V.mangle_class("zdb::CCell") == "Q23zdb5CCell", "class names mangle as the demo's symbols")
al.hand = set()
th = V.demo_vtables(demo)["Q23zdb15CBatchRelocator"]
vt, slots = tables["zdb::CBatchRelocator"]
got = al.align(vt, slots, th, hide=0x3559E0).get(0x3559E0)
ck(got is not None and got[0].startswith("AddToBatch__") and got[2], "slot 0 is AddToBatch, forced")
# --- the tree ---------------------------------------------------------------------
# The canary's own fixture: m2c's draft of OVL1's ~vector<CZDecalDef> (0x2CDB30), byte-exact as it
# stood in src/auto/ovl1/002CDB30.c until the game states' round 28 matched the row by hand
# (fts_ovl2statics.cpp's template tail) and automatch's stale pass deleted it (7092f929's text).
DRAFT_002CDB30 = """\
/* GENERATED by tools/automatch.py: m2c's draft of __dt__Q23std52vector<10CZDecalDef,Q23std23allocator<10CZDecalDef>>Fv, byte-exact as it stands
* (every relocation resolved). Replace it with real source; the generator then deletes it. */
/* sc: as=func_002CDB30 __dt__Q23std52vector<10CZDecalDef,Q23std23allocator<10CZDecalDef>>Fv */
typedef signed char s8; typedef unsigned char u8; typedef short s16; typedef unsigned short u16;
typedef int s32; typedef unsigned int u32; typedef long s64; typedef unsigned long u64;
typedef float f32; typedef double f64; typedef int M2C_UNK; typedef int M2C_UNK32;
#define M2C_FIELD(expr, type, offset) (*(type)((char *)(expr) + (offset)))
#define SC_MEMBER(base, type, offset) (*(type)((base) + (offset)))
#define M2C_ERROR(x) 0
#define NULL 0
M2C_UNK __dl__FPv(char *); /* extern */
M2C_UNK func_002CE770(); /* extern */
char *func_002CDB30(char *arg0, s64 arg1) {
char *temp_a0;
if (arg0 != NULL) {
if ((arg0 != NULL) && (arg0 != NULL)) {
func_002CE770();
temp_a0 = SC_MEMBER(arg0, char **, 8);
if (temp_a0 != NULL) {
__dl__FPv(temp_a0);
}
}
if (((s64) (arg1 << 0x30) >> 0x30) > 0) {
__dl__FPv(arg0);
}
}
return arg0;
}
"""
def test_copy_supersedes_a_draft_only_where_it_matched():
"""A hand-written inline copy that matches supersedes a generated draft only at the address it
matched (the integrator's fix, 2026-11-04): each overlay links its own template copies, so
seal_env.cpp's ~vector<CZDecalDef> scores against OVL2's row 0x5A3C60 (`ovl-copy:`), while
OVL1's 0x2CDB30, the named row, is its draft's. By name, the copy had dropped the draft from
scoring (a false regression) and the summary listed it superseded. Built together, both must
match and nothing be listed superseded; with the rows' roles swapped in the map (the name at
0x5A3C60, so the draft's home is OVL2 and its address the copy's), the same draft is superseded
and listed so. Self-contained since GCC round 25a: the draft is the fixture above, built with
seal_env.cpp in a scratch copy of the hand-written tree (sccheck.main's `root`), so no unit
src/ gains or loses can take the test's ground away."""
import contextlib
import io
import re
files = r"^(gamez/zseal/seal_env\.cpp|auto/ovl1/002CDB30\.c)$"
name = "__dt__Q23std52vector<10CZDecalDef,Q23std23allocator<10CZDecalDef>>Fv"
tree = HERE / "build" / "tmp" / f"supersede.{os.getpid()}"
if tree.exists():
shutil.rmtree(tree)
shutil.copytree(HERE / "src", tree, ignore=shutil.ignore_patterns("auto", "*.o"))
(tree / "auto" / "ovl1").mkdir(parents=True)
(tree / "auto" / "ovl1" / "002CDB30.c").write_text(DRAFT_002CDB30)
def run(st):
orig, E.symbols = E.symbols, (lambda: st)
out = io.StringIO()
try:
with contextlib.redirect_stdout(out):
rc = C.main(["--files", files, "--build-dir", str(BUILD / "supersede"), "-j", "2", "-v"],
root=tree)
finally:
E.symbols = orig
return rc, out.getvalue()
try:
_copy_supersedes(run, name)
finally:
shutil.rmtree(tree, ignore_errors=True)
def _copy_supersedes(run, name: str) -> None:
import re
rc, out = run(E.symbols())
row = lambda unit: re.search(rf"^match\s+{re.escape(unit)}\s+{re.escape(name)}\s", out, re.M)
ck(rc == 0 and row("002CDB30.c") and row("seal_env.cpp"), "the OVL1 draft and the OVL2 copy each match")
ck("superseded" not in out, "and no draft is listed superseded")
swapped = HERE / "build" / "tmp" / f"syms_swapped.{os.getpid()}.txt"
swapped.parent.mkdir(parents=True, exist_ok=True)
text = E.SYMBOLS.read_text()
a = next(l for l in text.splitlines(True) if l.startswith(f"0x002CDB30 0x78 {name} "))
b = next(l for l in text.splitlines(True) if l.startswith("0x005A3C60 0x60 func_005A3C60 "))
swapped.write_text(text.replace(a, f"0x002CDB30 0x78 func_002CDB30 # ovl-copy: {name}\n")
.replace(b, f"0x005A3C60 0x60 {name} # swapped by the canary\n"))
try:
rc, out = run(E.SymbolTable(swapped, E.DATA))
finally:
swapped.unlink()
ck(row("seal_env.cpp") and not row("002CDB30.c"), "swapped, the copy matches at the draft's address")
ck(re.search(rf"1 generated function\(s\) superseded .*{re.escape(name)}", out), "and the draft is superseded")
# OVL1's 0x210A60, `func_00210A60 # ovl-copy: __ml__...const_iterator CFv`: the const iterator's *, one of
# the ten `ovl-copy:` placeholder drafts the HUD's round 35 integration dropped (8d60f16a^'s text)
DRAFT_00210A60 = """\
/* GENERATED by tools/automatch.py: m2c's draft of func_00210A60, byte-exact as it stands
* (every relocation resolved). Replace it with real source; the generator then deletes it. */
typedef signed char s8; typedef unsigned char u8; typedef short s16; typedef unsigned short u16;
typedef int s32; typedef unsigned int u32; typedef long s64; typedef unsigned long u64;
typedef float f32; typedef double f64; typedef int M2C_UNK; typedef int M2C_UNK32;
s32 func_00210A60(s32 *arg0) {
return *arg0;
}
"""
def test_superseded_lists_drafts_by_address():
"""sccheck's "N generated function(s) superseded" line lists what automatch --emit drops, one entry
per draft by its row's address (GCC round 28a). hud_linemap.cpp's inline copy of the const
iterator's * scores at OVL1's copy row 0x210A60 (`ovl-copy:`), whose draft defines the row's
placeholder, func_00210A60 -- by name the line never listed it, yet --emit drops it (the HUD's round
35 integration dropped fourteen where the line named four). The draft is still scored (its match
listed under -v, the scoring unchanged). With the row's mark taken out of a copy of the map, the
copy scores at the named OVL2 row and nothing is superseded."""
import contextlib
import io
import re
files = r"^(Apps/FTS/hud_linemap\.cpp|auto/ovl1/00210A60\.c)$"
tree = HERE / "build" / "tmp" / f"byaddr.{os.getpid()}"
if tree.exists():
shutil.rmtree(tree)
shutil.copytree(HERE / "src", tree, ignore=shutil.ignore_patterns("auto", "*.o"))
(tree / "auto" / "ovl1").mkdir(parents=True)
(tree / "auto" / "ovl1" / "00210A60.c").write_text(DRAFT_00210A60)
def run(st):
orig, E.symbols = E.symbols, (lambda: st)
out = io.StringIO()
try:
with contextlib.redirect_stdout(out):
C.main(["--files", files, "--build-dir", str(BUILD / "byaddr"), "-j", "2", "-v"], root=tree)
finally:
E.symbols = orig
return out.getvalue()
unmarked = HERE / "build" / "tmp" / f"syms_byaddr.{os.getpid()}.txt"
try:
out = run(E.symbols())
ck(re.search(r"^match\s+hud_linemap\.cpp\s+__ml__\S+14const_iteratorCFv\s", out, re.M),
"the copy matches (at OVL1's 0x210A60)")
ck(re.search(r"^match\s+00210A60\.c\s+func_00210A60\s", out, re.M), "the draft is still scored")
ck(re.search(r"1 generated function\(s\) superseded .*func_00210A60", out),
"and listed superseded at its address")
text = E.SYMBOLS.read_text()
row = next(l for l in text.splitlines(True) if l.startswith("0x00210A60 "))
unmarked.parent.mkdir(parents=True, exist_ok=True)
unmarked.write_text(text.replace(row, "0x00210A60 0x8 func_00210A60 # the canary's: no mark\n"))
out = run(E.SymbolTable(unmarked, E.DATA))
ck("superseded" not in out, "unmarked, the copy is not this row's: nothing superseded")
finally:
shutil.rmtree(tree, ignore_errors=True)
unmarked.unlink(missing_ok=True)
def test_partial_check_holds_a_row_another_emitter_matches():
"""sccheck --files, GCC round 38a (the integrator's request): built alone, zsys_init.cpp's inline
copy of ~vector<CIOPManager::CRegistrant> (6 words off) lost 0x39CD40 -- util_main.cpp's copy
matches it, and the full check never lost it -- like the thirteen rows README "Working in
parallel" lists, held by their drafts. A row a partial check loses only by inline copies is now
tried against its other emitters outside --files (the row's draft, a hand-written unit naming the
copy's own identifiers) and reported `not lost` when one matches. A fixture with a SYNTHETIC name
on a real 8-byte row (0x39CDC0, `jr ra; lw v0,8(a0)`, in a copy of the map and of MATCHED.txt):
one unit defines CFxGet::GetSize to load +8, another emits an inline copy loading +0. Built alone,
the copy is WRONG and the row held by the definition, rc 0; with the definition gone it is a
REGRESSION, rc 1 (the negative); a strong definition that misses is never held; and the row's
draft holds it as the definition did."""
import contextlib
import io
import re
va, name = 0x39CDC0, "GetSize__6CFxGetCFv"
tree = HERE / "build" / "tmp" / f"held.{os.getpid()}"
sym = HERE / "build" / "tmp" / f"held_symbols.{os.getpid()}.txt"
matched = HERE / "build" / "tmp" / f"held_matched.{os.getpid()}.txt"
if tree.exists():
shutil.rmtree(tree)
(tree / "fx").mkdir(parents=True)
text = E.SYMBOLS.read_text()
row = next(l for l in text.splitlines(True) if l.startswith(f"0x{va:08X} 0x8 "))
sym.write_text(text.replace(row, f"0x{va:08X} 0x8 {name} # the canary's synthetic name\n"))
matched.write_text(f"# the canary's baseline\n0x{va:08X} {name}\n")
cls = "struct CFxGet {{ int a, b, c; int GetSize() const{}; }};\n"
definition = cls.format("") + "int CFxGet::GetSize() const { return c; }\n"
copy = cls.format(" { return a; }") + "int (CFxGet::*fx_keep)() const = &CFxGet::GetSize;\n"
strong = cls.format("") + "int CFxGet::GetSize() const { return a; }\n"
draft = ("/* sc: as=func_0039CDC0 GetSize__6CFxGetCFv */\n"
"int func_0039CDC0(int *self) { return self[2]; }\n")
def run(files: str):
st = E.SymbolTable(sym, E.DATA)
orig, orig_m, E.symbols = E.symbols, C.MATCHED, (lambda: st)
C.MATCHED = matched
out = io.StringIO()
try:
with contextlib.redirect_stdout(out):
rc = C.main(["--files", files, "--build-dir", str(BUILD / "held"), "-j", "2"], root=tree)
finally:
E.symbols, C.MATCHED = orig, orig_m
return rc, out.getvalue()
try:
(tree / "fx" / "held_def.cpp").write_text(definition)
(tree / "fx" / "held_copy.cpp").write_text(copy)
rc, out = run(r"^fx/held_def\.cpp$")
ck(rc == 0 and re.search(rf"^match\s+held_def\.cpp\s+{name}\s", out, re.M), f"the definition matches: {out}")
rc, out = run(r"^fx/held_copy\.cpp$")
ck(re.search(rf"^WRONG\s+held_copy\.cpp\s+{name}\s", out, re.M), f"built alone, the copy is WRONG: {out}")
ck(rc == 0 and "REGRESSIONS" not in out and re.search(
rf"^not lost -- .*{va:#x} {name} by fx/held_def\.cpp", out, re.M), f"and the row is held: {out}")
(tree / "fx" / "held_def.cpp").unlink()
rc, out = run(r"^fx/held_copy\.cpp$")
ck(rc == 1 and re.search(rf"^REGRESSIONS against \S+: {va:#x} {name}", out, re.M)
and "not lost" not in out, f"negative: with no other emitter it is a regression: {out}")
(tree / "fx" / "held_def.cpp").write_text(definition)
(tree / "fx" / "held_copy.cpp").write_text(strong)
rc, out = run(r"^fx/held_copy\.cpp$")
ck(rc == 1 and "REGRESSIONS" in out and "not lost" not in out,
f"negative: a strong definition that misses is never held: {out}")
(tree / "fx" / "held_copy.cpp").write_text(copy)
(tree / "fx" / "held_def.cpp").unlink()
(tree / "auto" / "ovl1").mkdir(parents=True)
(tree / "auto" / "ovl1" / f"{va:08X}.c").write_text(draft)
rc, out = run(r"^fx/held_copy\.cpp$")
ck(rc == 0 and re.search(rf"^not lost -- .*{va:#x} {name} by auto/ovl1/{va:08X}\.c", out, re.M),
f"and the row's draft holds it: {out}")
finally:
shutil.rmtree(tree, ignore_errors=True)
sym.unlink(missing_ok=True)
matched.unlink(missing_ok=True)
def test_stale_follows_a_callee_rename():
"""automatch --stale redoes a draft that calls a name the map renamed, not only a placeholder
(GCC round 28a): the UI's round 20 integration -- 0x2808D0's draft went on calling SkbOpen__Fv
after the row took its signature, a call no row resolves, which only the full check caught. The
map's old names are git's history of symbols.txt and data.txt; a fixture draft spelling the old
name is stale, spelling the map's name is not, and with no history only the placeholder rule is
left (the negative: the old rule's blind spot). The fixture's own row is SYNTHETIC (GCC round
30a): a placeholder added to a copy of the map at an address no row can hold (past OVL2), so the
test does not depend on a real row staying a placeholder -- it had used 0x2808D0's own, which the
engine core then matched from source as UIGetTextInput (a draft whose row is renamed is stale by
the own-row rule, so every `== set()` failed)."""
import automatch as A
real = E.symbols()
hist = A.map_history_names()
ck("SkbOpen__Fv" in hist and "SkbOpen__Fv" not in real._by_name, "the old name is in the map's history only")
ck("SkbOpen__FP13C2DAnimCmdHdrPf" in real._by_name, "and the name it took is the map's")
va = 0xFFFFF0
ck(real.containing(va) is None and real.at_addr(va) is None, "the fixture's address is no real row's")
auto = HERE / "build" / "tmp" / f"stale.{os.getpid()}"
(auto / "fixture").mkdir(parents=True, exist_ok=True)
sym, dat = auto.parent / f"stale_symbols.{os.getpid()}.txt", auto.parent / f"stale_data.{os.getpid()}.txt"
sym.write_text(E.SYMBOLS.read_text() + f"0x{va:08X} 0x8 func_{va:08X} # synthetic: the stale test's fixture row\n")
shutil.copy(E.DATA, dat)
draft = (f"/* GENERATED by tools/automatch.py: m2c's draft of func_{va:08X} */\n"
"M2C_UNK {callee}(); /* extern */\n\n"
f"void func_{va:08X}(void) {{{{\n {{callee}}();\n}}}}\n")
path = auto / "fixture" / f"{va:08X}.c"
orig = A.AUTO
A.AUTO = auto
try:
st = E.SymbolTable(sym, dat)
ck(st.at_addr(va) is not None and st.at_addr(va).name == f"func_{va:08X}", "the copy holds the synthetic row")
path.write_text(draft.format(callee="SkbOpen__Fv"))
ck(A.stale(st, hist) == {va}, "a draft calling the renamed name is stale")
ck(A.stale(st, set()) == set(), "which the placeholder rule alone misses")
path.write_text(draft.format(callee="SkbOpen__FP13C2DAnimCmdHdrPf"))
ck(A.stale(st, hist) == set(), "calling the map's name, it is not")
path.write_text(
draft.format(callee="SkbOpen__FP13C2DAnimCmdHdrPf").replace("*/\n", "*/ /* was SkbOpen__Fv */\n", 1))
ck(A.stale(st, hist) == set(), "and an old name in a comment is no call")
ck(A.stale(real, hist) == {va}, "against the real map (no such row) the same draft is stale: its row is the copy's")
finally:
A.AUTO = orig
shutil.rmtree(auto, ignore_errors=True)
sym.unlink(missing_ok=True)
dat.unlink(missing_ok=True)
def test_copy_index_is_never_read_half_built():
"""GCC round 37a (the AI core's round 32: `tools/objcmp.py -j 2` printed `DIFF ai/ai_astar.cpp`