nonvacuity.sh 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. #!/bin/sh
  2. # nonvacuity.sh -- prove the runtime checks can actually fail.
  3. #
  4. # A test that has never been seen red is not a test. This script breaks the
  5. # runtime on purpose, once per check, and asserts that the check goes red and
  6. # says something useful about the breakage. Then it restores the source and
  7. # asserts everything is green again.
  8. #
  9. # Each mutation below is a real bug that was in this file at some point, not an
  10. # invented one. That is the point: these are the mistakes we actually make
  11. # with 16-bit ModRM, so these are the ones the checks have to catch.
  12. #
  13. # audit_helpers.py name-versus-decode: catches a wrong ModRM that still
  14. # decodes cleanly, and both halves of a name that admits
  15. # an operand at all - `34 02' where the name promises
  16. # `34 01', and `35 01' where the row's opcode gate admits
  17. # only 34h. Both rows are new, and a row nobody has seen
  18. # reject anything accepts whatever it is shown.
  19. # audit_helpers.py coverage: catches a helper that has silently
  20. # dropped OUT of the audit, which is a green report about
  21. # a subject nobody looked at
  22. # run_com_tests.sh the .COM layout: catches a header that cannot be
  23. # located, a runtime size that disagrees with the image,
  24. # and an entry jump that starts in the wrong place
  25. # check_runtime.py golden: catches the same thing in the built
  26. # image
  27. # check_runtime.py decode sweep: catches a wrong instruction LENGTH
  28. # check_runtime.py branch targets: catches a wrong fixup
  29. # check_runtime.py entry goldens: catches a broken prologue
  30. # probe/modrm11.py the mod=11 table: catches the ModRM column itself
  31. # going wrong, which no amount of decoding will show
  32. # check_framedisp.py the BP disp rule: catches a displacement that reads
  33. # a different address than the symbol table named
  34. # tests/comimage.py the image layout: catches a sweep region built from a
  35. # literal instead of from two independent readings of the
  36. # file - the old RT_SZ went on reporting PASS while its
  37. # region began inside the runtime - and the layout itself
  38. # being written down a second time, now that one file
  39. # defines it and all three readers import it
  40. # rt_exec.py the BP contract: catches an entry that borrows BP to
  41. # reach its argument and does not hand it back. The bytes
  42. # are well formed, the golden is satisfied, the audit says
  43. # every helper emits what its name says, and the machine
  44. # triple-faults on the second call - so nothing short of
  45. # running it, or of stating the register contract
  46. # explicitly, can see it.
  47. # check_8086.py the 8086 opcodes: catches a conditional branch or a
  48. # SETcc emitted as `0F 8x'/`0F 9x', which are 386-only.
  49. # Nothing else here can: qemu-system-i386's lowest CPU
  50. # model is 486, where both are ordinary instructions, and
  51. # FCML's -m16 mode is a 386 too. Clause H additionally
  52. # catches the branch polarity being inverted, which is
  53. # still a legal opcode and therefore invisible to every
  54. # shape-based check.
  55. # run_com_exec.py behaviour: catches a*b that emits an ADD, `>'
  56. # and `>=' swapped, REPEAT..UNTIL that stops after one
  57. # pass, two procedures whose parameters collide, a left
  58. # operand overwritten by the right one's code (SaveLeft),
  59. # and a boolean `not' lowered as the integer one. Every
  60. # one of them sat in a fixture that COMPILED and was
  61. # never RUN, with every byte-level check green.
  62. # run_compile_tests.sh refusal: catches a boolean READ slipping
  63. # through to the runtime. TP3 refuses it with error 66
  64. # (TPSRC8 prderrtp; its runtime has no xrdbool), and
  65. # this compiler used to accept it and let rdbool store
  66. # a WORD into the one-byte variable. The mutation is
  67. # the one arm of IoCall's type dispatch that refuses it,
  68. # and only the COMPILE MATRIX can see a refusal: an ERR
  69. # fixture is never executed, so nothing that runs
  70. # programs on it can fail the check.
  71. # run_exec86.py behaviour (Exec86): catches the interpreter's OWN
  72. # reading of the machine: JE inverted in Exec86's Cond,
  73. # which swaps the two arms of every `=' in every program
  74. # while the emitted bytes, the sizes and every
  75. # byte-level check stay green. The image is fine; only
  76. # the thing reading it is wrong, so nothing that looks at
  77. # the image can fail this one.
  78. # runtest.py the R key: catches CmdRun poking nothing into
  79. # the interpreter at all, which faults on the first step
  80. # and prints "interpreter fault" instead of the guest's
  81. # answer. It is the only case here that needs a rebuilt
  82. # SHELL rather than a rebuilt compiler or runtime.
  83. #
  84. # The mod=11 cases do not need a rebuild -- they read the probe sources
  85. # directly -- so they are cheap, and they are the ones that matter most: the
  86. # table they guard is the one thing in this project that was wrong in the
  87. # documentation while the code was right, and a table that is wrong in the
  88. # code produces bytes that decode perfectly.
  89. #
  90. # Usage: tests/nonvacuity.sh (from shell/; leaves Runtime.mod restored)
  91. set -u
  92. cd "$(dirname "$0")/.." || exit 1
  93. GM2=/home/eric/bin/Modula2/Gm2/bin/gm2
  94. SAVED=../tmp/nonvacuity.Runtime.mod
  95. PROBE=../tmp/nonvacuity.rtprobe
  96. DUMP=../tmp/nonvacuity.dump
  97. # Exec86.mod is UNTRACKED, so unlike Runtime.mod git cannot put a botched
  98. # mutation back: the copy taken here is the only correct one in existence.
  99. # Shell.mod is tracked but is mutated by the R-key case below, and a trap that
  100. # restored only the sources would leave tpshell BUILT FROM the mutation - so
  101. # the trap rebuilds too. All three copies live in the project's own tmp/ and
  102. # are taken here, before the first mutation, rather than beside each case.
  103. #
  104. # comimage.py and comtest.py are the image layout and one of its three readers;
  105. # both are mutated by cases further down, and neither is recoverable from git
  106. # once the mutation is staged over - so they join the trap here for the same
  107. # reason, and an interrupted run cannot leave the layout half-written.
  108. mkdir -p ../tmp
  109. SAVED_E=../tmp/nonvacuity.Exec86.mod
  110. SAVED_SH=../tmp/nonvacuity.Shell.mod
  111. SAVED_CI=../tmp/nonvacuity.comimage.py
  112. SAVED_T=../tmp/nonvacuity.comtest.py
  113. cp Runtime.mod "$SAVED" || exit 1
  114. cp Exec86.mod "$SAVED_E" || exit 1
  115. cp Shell.mod "$SAVED_SH" || exit 1
  116. cp tests/comimage.py "$SAVED_CI" || exit 1
  117. cp tests/comtest.py "$SAVED_T" || exit 1
  118. restore_all () {
  119. cp "$SAVED" Runtime.mod
  120. cp "$SAVED_E" Exec86.mod
  121. cp "$SAVED_SH" Shell.mod
  122. cp "$SAVED_CI" tests/comimage.py
  123. cp "$SAVED_T" tests/comtest.py
  124. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1
  125. "$GM2" -fiso -c Exec86.mod >/dev/null 2>&1
  126. # The shell is rebuilt as well: a test that runs against a binary built
  127. # from a half-restored tree is reporting on the mutation, not on the code.
  128. make >/dev/null 2>&1
  129. }
  130. trap restore_all EXIT
  131. pass=0
  132. fail=0
  133. # mutate <file> <sed-expr> -- apply a deliberate breakage and INSIST it landed.
  134. #
  135. # Four cases in this file were already dead when first run, all the same way:
  136. # the helper they name had been renamed or reformatted since the case was
  137. # written, the sed matched nothing, the source was unchanged, and the check
  138. # correctly passed - so the harness reported "NOT NON-VACUOUS" and, worse, a
  139. # reader skimming the output could take "the check still passed" for a passing
  140. # test. A case that cannot fire is worse than no case: it is a claim of
  141. # coverage that was never tested.
  142. #
  143. # So the mutation is verified, not assumed. If the file is byte-identical
  144. # afterwards, that is reported as a FAILURE of the harness, naming the sed, and
  145. # the case is not run - because running it would only produce a meaningless
  146. # green. The message says what to do (fix the sed) rather than what it found.
  147. mutate () {
  148. mf=$1
  149. msed=$2
  150. cp "$mf" ../tmp/nonvacuity.mut.bak
  151. sed -i "$msed" "$mf"
  152. if cmp -s "$mf" ../tmp/nonvacuity.mut.bak; then
  153. echo " BROKEN CASE: the mutation did not change $mf"
  154. echo " sed: $msed"
  155. echo " the named code has probably been renamed or reformatted -"
  156. echo " fix this case, it is asserting nothing"
  157. fail=$((fail + 1))
  158. return 1
  159. fi
  160. return 0
  161. }
  162. # rebuild <label> -- re-emit the runtime and dump it
  163. rebuild () {
  164. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1 || return 1
  165. "$GM2" -fiso -o "$PROBE" tests/RtProbe.mod Runtime.o Posix.o \
  166. >/dev/null 2>&1 || return 1
  167. "$PROBE" > "$DUMP" || return 1
  168. return 0
  169. }
  170. # expect_red <label> <pattern> <checker-cmd...>
  171. # <pattern> is a grep the failure output must match, so a check cannot
  172. # "pass" by failing for some unrelated reason.
  173. expect_red () {
  174. label=$1
  175. want=$2
  176. shift 2
  177. if out=$("$@" 2>&1); then
  178. echo "NOT NON-VACUOUS: $label -- the check still passed"
  179. fail=$((fail + 1))
  180. elif ! printf '%s\n' "$out" | grep -qi "$want"; then
  181. echo "WRONG FAILURE: $label -- went red, but not for the stated reason"
  182. printf '%s\n' "$out" | sed 's/^/ /'
  183. fail=$((fail + 1))
  184. else
  185. echo " ok: $label"
  186. printf '%s\n' "$out" | grep -im1 "$want" | sed 's/^/ /'
  187. pass=$((pass + 1))
  188. fi
  189. }
  190. echo "== each mutation must turn the named check red"
  191. echo
  192. # --- 1. name-versus-decode -------------------------------------------
  193. # MovSiBx was `89 DC`, which is MOV SP,BX. Two bytes either way, decodes
  194. # cleanly, and no structural check can see it.
  195. cp "$SAVED" Runtime.mod
  196. mutate Runtime.mod 's|B (0DEH) END MovSiBx|B (0DCH) END MovSiBx|'
  197. expect_red "audit_helpers catches MovSiBx emitting MOV SP,BX" \
  198. "MovSiBx" python3 tests/audit_helpers.py
  199. # CmpSiBx had the identical mistake, which is how you know a single fix is
  200. # not enough -- the same misreading was written twice.
  201. cp "$SAVED" Runtime.mod
  202. mutate Runtime.mod 's|B (39H) ; B (0DEH) END CmpSiBx|B (39H) ; B (0DCH) END CmpSiBx|'
  203. expect_red "audit_helpers catches CmpSiBx emitting CMP SP,BX" \
  204. "CmpSiBx" python3 tests/audit_helpers.py
  205. # --- 2. golden, and entry goldens ------------------------------------
  206. # MovDlAl was `88 C0` = MOV AL,AL instead of MOV DL,AL. This is the case that
  207. # motivated runtime.golden: the sweep stayed in sync, every branch target
  208. # stayed on a boundary, no entry's first bytes moved, and the size did not
  209. # change. The target helper was MovAlDh when this case was written, which is
  210. # the fourth way a case here can rot - see the note on `mutate` below.
  211. cp "$SAVED" Runtime.mod
  212. mutate Runtime.mod 's|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H)|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C0H)|'
  213. rebuild
  214. expect_red "runtime.golden catches MOV AL,AL" \
  215. "mov al,al" python3 tests/check_runtime.py "$DUMP"
  216. # initmem opened with the mis-emitted MovSiAx, so its entry golden was the
  217. # thing that noticed the prologue was a no-op.
  218. cp "$SAVED" Runtime.mod
  219. mutate Runtime.mod 's|B (0F0H) END MovSiAx|B (0C0H) END MovSiAx|'
  220. rebuild
  221. expect_red "check_runtime catches a broken initmem prologue" \
  222. "mov ax,ax" python3 tests/check_runtime.py "$DUMP"
  223. # --- 3. decode sweep / length ----------------------------------------
  224. # StDiDl was `88 97` = [BX+disp16],DL: mod=10, so the instruction needs a
  225. # disp16 it was not given, and the sweep loses sync two bytes later.
  226. cp "$SAVED" Runtime.mod
  227. mutate Runtime.mod 's|PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H)|PROCEDURE StDiDl ; BEGIN B (88H) ; B (97H)|'
  228. rebuild
  229. expect_red "decode sweep catches a mod=10 byte move with no displacement" \
  230. "mov byte ptr \[bx+5b5fh\],dl" python3 tests/check_runtime.py "$DUMP"
  231. # --- 4. branch targets ------------------------------------------------
  232. # FixUp measures a rel8 from the end of the instruction, one byte past the
  233. # displacement field. Drop the +1 and every short branch lands one byte into
  234. # its target, which for a 3-byte instruction means the middle of it. The
  235. # bytes themselves are all perfectly well formed -- only the fixups are
  236. # wrong -- so this is the one failure mode the golden cannot be expected to
  237. # catch on its own.
  238. cp "$SAVED" Runtime.mod
  239. mutate Runtime.mod 's|rel := (t + 100H - (fix \[i\].place + 1)) MOD 100H|rel := (t + 100H - fix [i].place) MOD 100H|'
  240. rebuild
  241. expect_red "branch check catches rel8 fixups measured from the wrong byte" \
  242. "not an instruction boundary" \
  243. python3 tests/check_runtime.py "$DUMP"
  244. echo
  245. echo "== everything restored and green again"
  246. cp "$SAVED" Runtime.mod
  247. if rebuild; then
  248. if python3 tests/audit_helpers.py >/dev/null 2>&1 &&
  249. python3 tests/check_runtime.py "$DUMP" >/dev/null 2>&1; then
  250. echo " ok: both checks pass on the restored source"
  251. pass=$((pass + 1))
  252. else
  253. echo "NOT RESTORED: a check is red after restoring Runtime.mod"
  254. fail=$((fail + 1))
  255. fi
  256. else
  257. echo "NOT RESTORED: the runtime would not rebuild"
  258. fail=$((fail + 1))
  259. fi
  260. echo
  261. echo "== the mod=11 table (probe/modrm11.py)"
  262. # These mutate the probe's own sources, not the runtime, so there is no
  263. # rebuild in the loop. SAVED_PY / SAVED_S are restored after each case.
  264. SAVED_PY=../tmp/nonvacuity.modrm11.py
  265. SAVED_S=../tmp/nonvacuity.modrm11.s
  266. cp tests/probe/modrm11.py "$SAVED_PY" || exit 1
  267. cp tests/probe/modrm11.s "$SAVED_S" || exit 1
  268. M11="python3 tests/probe/modrm11.py"
  269. restore_probe () {
  270. cp "$SAVED_PY" tests/probe/modrm11.py
  271. cp "$SAVED_S" tests/probe/modrm11.s
  272. }
  273. # 1. one cell of the table moved
  274. mutate tests/probe/modrm11.py 's|"Si", "Di"\]$|"Bp", "Di"]|'
  275. expect_red "anchor pins a moved table cell" \
  276. "anchor ADD SI, 2" $M11
  277. restore_probe
  278. # 2. the table this project actually shipped: AX dropped off the front and a
  279. # duplicate BX invented at the end, which shifts every code down by one
  280. mutate tests/probe/modrm11.py 's|^REG = .*$|REG = ["Cx", "Dx", "Bx", "Sp", "Bp", "Si", "Di", "Bx"]|'
  281. expect_red "the table shifted by one (AX dropped, BX duplicated)" \
  282. "anchor MOV SP, BP" $M11
  283. restore_probe
  284. # 3. the .s edited to contradict the table. This is the case that shows why
  285. # the hard-coded EXPECT bytes exist: the assembler encodes the new claim
  286. # correctly, so comparing the .s against `as` alone can never fail here.
  287. mutate tests/probe/modrm11.s 's|movw %sp, %di # reg 100|movw %bp, %di # reg 100|'
  288. expect_red "probe source edited away from the recorded bytes" \
  289. "expected 89 E7" $M11
  290. restore_probe
  291. # 4. the 8-bit list edited, which is a different table from the word one
  292. mutate tests/probe/modrm11.s 's|movb %al, %dl # 88 C2 -> DL := AL|movb %al, %bl # was DL|'
  293. expect_red "the 8-bit register list edited" \
  294. "expected 88 C2" $M11
  295. restore_probe
  296. # 5. an anchor's recorded byte corrupted, so the anchor can no longer
  297. # corroborate itself
  298. mutate tests/probe/modrm11.py 's|"8B EC", "8B E5"|"8B ED", "8B E5"|'
  299. expect_red "anchor byte no longer matches the emitted code" \
  300. "expected 8B ED" $M11
  301. restore_probe
  302. if $M11 >/dev/null 2>&1; then
  303. echo " ok: modrm11.py passes on the restored probe sources"
  304. pass=$((pass + 1))
  305. else
  306. echo "NOT RESTORED: modrm11.py is red after restoring its sources"
  307. $M11 2>&1 | sed 's/^/ /'
  308. fail=$((fail + 1))
  309. fi
  310. echo
  311. echo "== the BP displacement rule (check_framedisp.py)"
  312. # This one is about Compiler.mod rather than the runtime, and it needs the
  313. # whole toolchain rebuilt (comtest, not rtprobe), so it gets its own rebuild.
  314. SAVED_C=../tmp/nonvacuity.Compiler.mod
  315. cp Compiler.mod "$SAVED_C" || exit 1
  316. rebuild_compiler () {
  317. $GM2 -fiso -c Compiler.mod >/dev/null 2>&1 || return 1
  318. $GM2 -fiso -fgen-module-list=tests/ct.lst -o /dev/null \
  319. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  320. >/dev/null 2>&1
  321. $GM2 -fiso -fuse-list=tests/ct.lst -o comtest \
  322. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  323. >/dev/null 2>&1 || return 1
  324. return 0
  325. }
  326. # 1. the original bug: `off MOD 100H`, always disp8. Restores exactly the code
  327. # that was there before EmBpDisp existed. t28's [BP+128] read becomes
  328. # [BP-128], which is the failure this whole check is named after.
  329. python3 - "$SAVED_C" <<'PYEOF'
  330. import sys
  331. p = 'Compiler.mod'
  332. s = open(p).read()
  333. old = """BEGIN
  334. IF off <= 127 THEN
  335. Ebyte (46H) ; Ebyte (VAL (BYTE, off))
  336. ELSE
  337. Ebyte (86H) ; Eword (off)
  338. END
  339. END EmBpDisp ;"""
  340. new = """VAR disp : CARDINAL ;
  341. BEGIN
  342. disp := off MOD 100H ;
  343. Ebyte (46H) ; Ebyte (VAL (BYTE, disp))
  344. END EmBpDisp ;"""
  345. assert old in s, "EmBpDisp body not found -- update this mutation"
  346. open(p, 'w').write(s.replace(old, new))
  347. PYEOF
  348. if rebuild_compiler; then
  349. expect_red "displacement truncation reads a different address" \
  350. "no 8B access at \[BP+128\]" python3 tests/check_framedisp.py
  351. else
  352. echo " FAIL: the compiler would not rebuild with the truncation"
  353. fail=$((fail + 1))
  354. fi
  355. cp "$SAVED_C" Compiler.mod
  356. # 2. the other half of the rule: always use the 4-byte form, ignoring the
  357. # <= 127 case. This is over-cautious rather than wrong, so the checker must
  358. # still be happy -- which is worth asserting, because a check that only
  359. # ever fails on a smaller encoding is a check that pins one answer instead
  360. # of the rule.
  361. python3 - <<'PYEOF'
  362. p = 'Compiler.mod'
  363. s = open(p).read()
  364. old = """ IF off <= 127 THEN
  365. Ebyte (46H) ; Ebyte (VAL (BYTE, off))
  366. ELSE
  367. Ebyte (86H) ; Eword (off)
  368. END"""
  369. new = """ Ebyte (86H) ; Eword (off)"""
  370. assert old in s, "EmBpDisp branch not found -- update this mutation"
  371. open(p, 'w').write(s.replace(old, new))
  372. PYEOF
  373. if rebuild_compiler; then
  374. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  375. echo " ok: always-disp16 is accepted, so the check pins the rule"
  376. echo " and not one particular encoding"
  377. pass=$((pass + 1))
  378. else
  379. echo " FAIL: check_framedisp rejects a safe, over-long encoding"
  380. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  381. fail=$((fail + 1))
  382. fi
  383. else
  384. echo " FAIL: the compiler would not rebuild with always-disp16"
  385. fail=$((fail + 1))
  386. fi
  387. cp "$SAVED_C" Compiler.mod
  388. if rebuild_compiler; then
  389. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  390. echo " ok: check_framedisp passes on the restored source"
  391. pass=$((pass + 1))
  392. else
  393. echo "NOT RESTORED: check_framedisp is red after restoring Compiler.mod"
  394. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  395. fail=$((fail + 1))
  396. fi
  397. else
  398. echo "NOT RESTORED: the compiler would not rebuild"
  399. fail=$((fail + 1))
  400. fi
  401. # --- the region check_framedisp sweeps, and the one file it comes from -----
  402. echo
  403. echo "== the image layout, measured once (tests/comimage.py)"
  404. # check_framedisp.py decodes instructions over a REGION of the image, and a
  405. # wrong region does not stop it: the byte patterns it searches for are in the
  406. # image wherever they happen to be, and a decode that starts mid-instruction
  407. # reached the same offsets anyway. That is how RT_SZ = 391 - a literal that had
  408. # drifted from the runtime it described - went on reporting PASS while its sweep
  409. # began inside the runtime, part way through an instruction. Nothing in the
  410. # check could see where its own region began.
  411. #
  412. # So the region is two independent readings of the file - where the entry jump
  413. # says execution starts, and where the program header says the runtime ends -
  414. # and the two must AGREE or the region is undetermined. Both readings, and the
  415. # layout itself, live in tests/comimage.py, which is also where comtest.py and
  416. # the independent checker in run_com_tests.sh now get the copy each of them
  417. # used to keep: a duplicated constant that has silently drifted is not an
  418. # independent check, it is a second source of truth that lies, and it lies in
  419. # the direction of looking like the code under test is broken.
  420. #
  421. # None of this needs a rebuild - comimage.py is Python the checkers import -
  422. # so these cases are cheap, and they turn the READERS red one at a time.
  423. # 1. entry_target answers with the literal the region used to be built from.
  424. # The two readings now disagree, and there is no measured region to sweep.
  425. python3 - <<'PYEOF'
  426. p = 'tests/comimage.py'
  427. s = open(p).read()
  428. old = ' return ENT_SZ + int.from_bytes(d[1:ENT_SZ], "little", signed=True)'
  429. new = ' return ENT_SZ + 391 # the literal RT_SZ was'
  430. assert s.count(old) == 1, "entry_target's return not found -- update this mutation"
  431. open(p, 'w').write(s.replace(old, new))
  432. PYEOF
  433. expect_red "a sweep region that is not where execution starts is rejected" \
  434. "neither region is swept" python3 tests/check_framedisp.py
  435. cp "$SAVED_CI" tests/comimage.py
  436. # 2 and 3. The header's own equation, one byte out, so the header cannot be
  437. # located at all - the case where a checker that fell back on a remembered
  438. # offset would report a confident number instead. Two readers, two
  439. # different sentences: each must say in its own words that it does not know
  440. # where the program's code begins.
  441. python3 - <<'PYEOF'
  442. p = 'tests/comimage.py'
  443. s = open(p).read()
  444. old = ' if w(HDR_DS_WORD) != off + 0x1000 + LOAD_BIAS: # hdrDS'
  445. new = ' if w(HDR_DS_WORD) != off + 0x1000 + LOAD_BIAS + 1: # hdrDS'
  446. assert s.count(old) == 1, "the hdrDS equation not found -- update this mutation"
  447. open(p, 'w').write(s.replace(old, new))
  448. PYEOF
  449. expect_red "check_framedisp reports a header it cannot locate" \
  450. "no program header found" python3 tests/check_framedisp.py
  451. expect_red "check_8086 reports the same unlocatable header" \
  452. "could not locate the code region" python3 tests/check_8086.py
  453. cp "$SAVED_CI" tests/comimage.py
  454. # 4-7. The single copy itself, now that there is one. A second find_header,
  455. # a second HDR_SZ, a caller that reaches the helper through another
  456. # consumer instead of through comimage, and - the case that exists
  457. # because of rt_exec.py's "matched nothing" lesson - the definition the
  458. # audit counts. A rule that matches nothing looks exactly like a rule
  459. # that passes, so a deleted definition must be a red report rather than a
  460. # silent green one.
  461. python3 - <<'PYEOF'
  462. p = 'tests/comtest.py'
  463. s = open(p).read()
  464. old = 'def check_com(path, expected_src_len):'
  465. new = ('def find_header(d):\n'
  466. ' return None # a second copy of the helper\n'
  467. '\n\n'
  468. 'def check_com(path, expected_src_len):')
  469. assert s.count(old) == 1, "check_com not found -- update this mutation"
  470. open(p, 'w').write(s.replace(old, new))
  471. PYEOF
  472. expect_red "the audit reports a find_header copied out of comimage" \
  473. "comtest.py defines its own find_header" python3 tests/check_comimage.py
  474. cp "$SAVED_T" tests/comtest.py
  475. python3 - <<'PYEOF'
  476. p = 'tests/comtest.py'
  477. s = open(p).read()
  478. old = 'from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,'
  479. new = ('HDR_SZ = 16 # a second copy of the layout\n'
  480. 'from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,')
  481. assert s.count(old) == 1, "the comimage import not found -- update this mutation"
  482. open(p, 'w').write(s.replace(old, new))
  483. PYEOF
  484. expect_red "the audit reports a layout constant defined elsewhere" \
  485. "comtest.py defines its own HDR_SZ" python3 tests/check_comimage.py
  486. cp "$SAVED_T" tests/comtest.py
  487. python3 - <<'PYEOF'
  488. p = 'tests/comimage.py'
  489. s = open(p).read()
  490. old = 'def find_header(d):'
  491. new = 'def _find_header(d): # the definition is gone'
  492. assert s.count(old) == 1, "def find_header not found -- update this mutation"
  493. open(p, 'w').write(s.replace(old, new))
  494. PYEOF
  495. expect_red "the audit counts its own subject rather than matching nothing" \
  496. "defines find_header 0 times" python3 tests/check_comimage.py
  497. cp "$SAVED_CI" tests/comimage.py
  498. # 7. The import dropped while the CALLS stay - the shape a refactor makes when
  499. # one reader starts taking the helper from another reader instead. It would
  500. # NameError the moment comtest ran, but the audit says so first, and in the
  501. # vocabulary of the layout rather than in the vocabulary of Python: the
  502. # failure this is guarding is "two sources of truth", not "undefined name".
  503. python3 - <<'PYEOF'
  504. p = 'tests/comtest.py'
  505. s = open(p).read()
  506. old = ('from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,\n'
  507. ' HDR_HEAP_WORD, find_header)')
  508. new = '# MUTATION: the shared import is gone, but the calls remain\n'
  509. assert s.count(old) == 1, "the comimage import not found -- update this mutation"
  510. open(p, 'w').write(s.replace(old, new))
  511. PYEOF
  512. expect_red "the audit reports a caller that does not take it from comimage" \
  513. "comtest.py calls find_header without importing it from comimage" \
  514. python3 tests/check_comimage.py
  515. cp "$SAVED_T" tests/comtest.py
  516. # Green again, on all three readers at once: the case above mutated the one
  517. # file all of them import, so restoring it has to satisfy each of them, not
  518. # just the one that was last run.
  519. for c in tests/check_framedisp.py tests/check_comimage.py tests/check_8086.py; do
  520. if python3 "$c" >/dev/null 2>&1; then
  521. echo " ok: $c passes on the restored sources"
  522. pass=$((pass + 1))
  523. else
  524. echo "NOT RESTORED: $c is red after restoring comimage.py and comtest.py"
  525. python3 "$c" 2>&1 | tail -3 | sed 's/^/ /'
  526. fail=$((fail + 1))
  527. fi
  528. done
  529. # --- 3. the operator bugs the nine dead fixtures were hiding ------------
  530. echo
  531. echo "== the bugs the never-executed fixtures were hiding"
  532. echo
  533. # A different KIND of case from everything above. The others break the code
  534. # and assert a byte-level check notices; these break the code and assert a
  535. # BEHAVIOURAL check notices, which is the only kind that could have found them.
  536. # Each of the original four shipped with a green compile matrix, a passing .COM
  537. # layout check, a passing golden and a passing emitter audit:
  538. #
  539. # OpMul = 1 `a * b` emitted ADD AX,CX. `*' and `+' both numbered
  540. # their operator 1, and BinOpEmit cannot see which
  541. # precedence level called it, so every multiplication
  542. # dispatched to the addition. The constant-folding arm was
  543. # correct, which is why `n * n' with n a CONST was right and
  544. # `a * a' with a a variable was not -- and t08_const is the
  545. # only fixture that ever multiplied.
  546. # 9Dh / 9FH `>' got SETGE and `>=' got SETG: swapped, one letter
  547. # apart in the mnemonic. Only a==b could see it.
  548. # JNZ -> body REPEAT..UNTIL looped back while the condition was TRUE,
  549. # which is WHILE, so the body ran once and stopped.
  550. #
  551. # They are mutated back to the original defect and run_com_exec.py must go red
  552. # on the exact fixture that pins the behaviour. The fourth (HideLocals) is a
  553. # scoping bug rather than an operator bug; it was hiding in the same place.
  554. #
  555. # Two joined them when the operand-lifetime and `not' fixes landed, each run
  556. # red by hand before it was written down here, and each with the same property
  557. # as the four above -- green everywhere except execution:
  558. #
  559. # SaveLeft's park `(p > q) or (q > p)' evaluated `(q > p) or (q > p)'. A
  560. # kind-2 value exists only in AX, and the right operand's
  561. # code is emitted before the two are ever brought together,
  562. # so without the push the left one is simply gone.
  563. # neglevel's split `not' on a boolean emitted the INTEGER `not', which left
  564. # 0FFFEh where a boolean belongs, and wrbool reads anything
  565. # non-zero as TRUE -- so `not (a = a)' answered TRUE and so
  566. # did every other `not'.
  567. mutate_compiler () { # reuse mutate's verified-change discipline on Compiler.mod
  568. mf=Compiler.mod
  569. msed=$1
  570. cp "$SAVED_C" ../tmp/nonvacuity.mut2.bak
  571. sed -i "$msed" "$mf"
  572. if cmp -s "$mf" ../tmp/nonvacuity.mut2.bak; then
  573. echo " BROKEN CASE: the mutation did not change Compiler.mod"
  574. echo " sed: $msed"
  575. echo " the named code has probably been renamed or reformatted -"
  576. echo " fix this case, it is asserting nothing"
  577. fail=$((fail + 1))
  578. return 1
  579. fi
  580. return 0
  581. }
  582. # The SETcc swap and the HideLocals removal are done in python rather than
  583. # with sed: both need to match source text containing `*` and `(` in a way that
  584. # is tedious and fragile as a regex, and a case whose only failure mode is a
  585. # malformed sed is a case that silently asserts nothing.
  586. #
  587. # And a python helper fails in a way sed does not: a syntax error in the helper
  588. # is a non-zero exit, `if mutate_foo; then` is simply false, and the case is
  589. # SKIPPED -- with no failure counted and nothing on stdout but whatever python
  590. # printed. That is how the SETcc case spent its first run: an apostrophe in an
  591. # assert message ("the `>' arm") closed the string early, python died, the
  592. # compiler was never broken, and the suite still reported 0 failed. A skipped
  593. # case and a passing case look the same in the total. So each helper below
  594. # fails LOUDLY: a non-zero exit from python is reported as a BROKEN CASE and
  595. # counted, never swallowed.
  596. #
  597. # Each one also counts its targets before replacing. `assert s != before' only
  598. # says the file changed; with two edits it would pass if just one of them
  599. # landed, and with two identical HideLocals call sites it would happily delete
  600. # the wrong one -- still a changed file, still a working compiler, still green
  601. # for the wrong reason.
  602. mutate_cc_swap () {
  603. if python3 - <<'PYX'
  604. p = 'Compiler.mod'
  605. s = open(p).read()
  606. GT = 'EmSetcc (9FH) ; (* > SETG *)' # the greater-than arm
  607. GE = 'EmSetcc (9DH) ; (* >= SETGE *)' # the greater-equal arm
  608. assert s.count(GT) == 1, 'expected 1 greater-than arm, found %d' % s.count(GT)
  609. assert s.count(GE) == 1, 'expected 1 greater-equal arm, found %d' % s.count(GE)
  610. s = s.replace(GT, GT.replace('9FH', '9DH'))
  611. s = s.replace(GE, GE.replace('9DH', '9FH'))
  612. open(p, 'w').write(s)
  613. PYX
  614. then
  615. return 0
  616. fi
  617. echo " BROKEN CASE: the SETcc swap did not apply"
  618. echo " the two EmSetcc arms are probably renamed or reformatted -"
  619. echo " fix this case, it is asserting nothing"
  620. fail=$((fail + 1))
  621. return 1
  622. }
  623. mutate_no_hidelocals () {
  624. if python3 - <<'PYX'
  625. p = 'Compiler.mod'
  626. s = open(p).read()
  627. # Two HideLocals calls exist. Only the body-exit one may go: deleting the
  628. # FORWARD one instead would still change the file, still rebuild, and still
  629. # leave t13_proc compiling, so the case would go green for the wrong reason.
  630. HL = ' HideLocals (nestMark) ; (* parameters and locals stop here *)\n'
  631. assert s.count(HL) == 1, 'expected 1 body-exit HideLocals, found %d' % s.count(HL)
  632. open(p, 'w').write(s.replace(HL, ''))
  633. PYX
  634. then
  635. return 0
  636. fi
  637. echo " BROKEN CASE: HideLocals was not removed"
  638. echo " the call or its comment has probably been reformatted -"
  639. echo " fix this case, it is asserting nothing"
  640. fail=$((fail + 1))
  641. return 1
  642. }
  643. cp "$SAVED_C" Compiler.mod
  644. if mutate_compiler 's|^ op := OpMul ; DropCh| op := OpAdd ; DropCh|'; then
  645. if rebuild_compiler; then
  646. expect_red "execution catches '*' emitting an ADD (t08_const, n*n)" \
  647. "t08_const" python3 tests/run_com_exec.py t08_const
  648. # t08 only ever multiplied two CONSTANTS, which is the one path that was
  649. # never wrong, because BinOpEmit folds it. So the case above is close
  650. # to vacuous: it proves the mutation changed the binary, not that the
  651. # emitted multiply is covered. The check that matters needs a
  652. # VARIABLE operand, and no shipped fixture has one -- which is why the
  653. # bug survived at all. So this writes a throwaway fixture that
  654. # multiplies a variable, runs it, and asserts the multiply is right.
  655. # The fixture is deleted afterwards; it is here to close the coverage
  656. # hole, not to become a permanent test (that is what a real fixture
  657. # with a `*' in it would be for).
  658. cat > tests/fixtures/zzmul.pas <<'ZZEOF'
  659. program zzmul;
  660. var a : integer ;
  661. begin
  662. a := 7 ;
  663. writeln (a * 6)
  664. end.
  665. ZZEOF
  666. printf '42\r\n' > tests/fixtures/zzmul.out
  667. expect_red "execution catches '*' on a VARIABLE (the unfolded path)" \
  668. "zzmul" python3 tests/run_com_exec.py zzmul
  669. rm -f tests/fixtures/zzmul.pas tests/fixtures/zzmul.out
  670. else
  671. echo " FAIL: the compiler would not rebuild with OpAdd for '*'"
  672. fail=$((fail + 1))
  673. fi
  674. fi
  675. cp "$SAVED_C" Compiler.mod
  676. cp "$SAVED_C" Compiler.mod
  677. if mutate_cc_swap; then
  678. if rebuild_compiler; then
  679. expect_red "execution catches '>' and '>=' swapped (t09_if)" \
  680. "t09_if" python3 tests/run_com_exec.py t09_if
  681. else
  682. echo " FAIL: the compiler would not rebuild with the SETcc swap"
  683. fail=$((fail + 1))
  684. fi
  685. fi
  686. cp "$SAVED_C" Compiler.mod
  687. cp "$SAVED_C" Compiler.mod
  688. if mutate_compiler 's| DropC (EmJcc (84H, L1)) ; (\* JZ -> body again \*)| zj := EmJcc (85H, L1) ;|'; then
  689. if rebuild_compiler; then
  690. expect_red "execution catches REPEAT..UNTIL exiting after one pass (t12)" \
  691. "t12_repeat" python3 tests/run_com_exec.py t12_repeat
  692. else
  693. echo " FAIL: the compiler would not rebuild with the JNZ repeat"
  694. fail=$((fail + 1))
  695. fi
  696. fi
  697. cp "$SAVED_C" Compiler.mod
  698. # The fourth: sibling procedures shared one parameter namespace, because a
  699. # finished procedure's symbols were left at a level Search still accepts.
  700. # Removing HideLocals puts two procedures' `a : integer' back in collision.
  701. cp "$SAVED_C" Compiler.mod
  702. if mutate_no_hidelocals; then
  703. if rebuild_compiler; then
  704. expect_red "a duplicate parameter in two procedures is a compile error again" \
  705. "ERROR 41" python3 tests/run_com_exec.py t13_proc
  706. else
  707. echo " FAIL: the compiler would not rebuild without HideLocals"
  708. fail=$((fail + 1))
  709. fi
  710. else
  711. echo " FAIL: could not remove HideLocals to test the scoping fix"
  712. fail=$((fail + 1))
  713. fi
  714. # M6: the LEFT operand, parked for the right one. kind 2 means "this value
  715. # exists in AX and nowhere else", and the right-hand operand's code is emitted
  716. # between recognizing the operator and using both operands - so without
  717. # SaveLeft's push the left value is overwritten before it is ever read, and
  718. # LoadPair's kind-4 shape can never trigger. `(p > q) or (q > p)' then
  719. # evaluates `(q > p) or (q > p)'. Every byte, every size and the whole compile
  720. # matrix stay green: nothing is malformed, the value is simply the wrong one.
  721. cp "$SAVED_C" Compiler.mod
  722. if python3 - <<'PYX'
  723. p = 'Compiler.mod'
  724. s = open(p).read()
  725. old = """BEGIN
  726. IF left.kind = 2 THEN
  727. EmPushAx () ;
  728. left.kind := 4 (* 4 = on the top of the stack *)
  729. END
  730. END SaveLeft ;"""
  731. new = """BEGIN
  732. (* MUTATION: the park is unreachable, so nothing survives the parse *)
  733. IF left.kind = 99 THEN
  734. EmPushAx () ;
  735. left.kind := 4
  736. END
  737. END SaveLeft ;"""
  738. assert s.count(old) == 1, 'SaveLeft body found %d times -- update this mutation' % s.count(old)
  739. open(p, 'w').write(s.replace(old, new))
  740. PYX
  741. then
  742. if rebuild_compiler; then
  743. expect_red "execution catches a left operand clobbered by the right one" \
  744. "t34_arith" python3 tests/run_com_exec.py t34_arith
  745. else
  746. echo " FAIL: the compiler would not rebuild without SaveLeft's push"
  747. fail=$((fail + 1))
  748. fi
  749. else
  750. echo " BROKEN CASE: the SaveLeft mutation did not apply"
  751. fail=$((fail + 1))
  752. fi
  753. cp "$SAVED_C" Compiler.mod
  754. # M7: the type split in TPSRC9's neglevel, wrong half. A boolean NOT lowered
  755. # as the INTEGER one leaves 0FFFEh/0FFFFh, and wrbool tests [BP+4] <> 0 - so
  756. # `not (a = a)' answers TRUE, and so does every other `not'. One instruction,
  757. # same length, same sizes, and the compile matrix cannot see it because both
  758. # halves emit well-formed code for a type the parser already accepted.
  759. cp "$SAVED_C" Compiler.mod
  760. if python3 - <<'PYX'
  761. p = 'Compiler.mod'
  762. s = open(p).read()
  763. old = """ IF r.cls = TBool THEN
  764. LoadAtom (r) ;
  765. EmXorAl01 () ;"""
  766. new = """ IF r.cls = TBool THEN
  767. LoadAtom (r) ;
  768. EmNotAx () ; (* MUTATION: integer NOT on a boolean *)"""
  769. assert s.count(old) == 1, 'the TBool arm of ParseNeg found %d times -- update this mutation' % s.count(old)
  770. open(p, 'w').write(s.replace(old, new))
  771. PYX
  772. then
  773. if rebuild_compiler; then
  774. expect_red "execution catches a boolean NOT lowered as an integer one" \
  775. "t35_not" python3 tests/run_com_exec.py t35_not
  776. else
  777. echo " FAIL: the compiler would not rebuild with EmNotAx for a boolean"
  778. fail=$((fail + 1))
  779. fi
  780. else
  781. echo " BROKEN CASE: the neglevel type-split mutation did not apply"
  782. fail=$((fail + 1))
  783. fi
  784. cp "$SAVED_C" Compiler.mod
  785. # Bug 36, argument order: the fix has FOUR independent halves - three parsers
  786. # that push each argument the moment it is parsed instead of after the whole
  787. # list, and the callee that reads its parameters backwards from BP+4 - and
  788. # t36_argclobber is the one fixture that tells them apart. Before these four
  789. # cases existed, its only proof was the pre-fix run quoted in the commit
  790. # message, which is a fact about the past rather than a check: it cannot be
  791. # re-derived, and nothing stops the four halves from drifting apart.
  792. #
  793. # Each revert below was run to a red BEFORE it was written here, and the four
  794. # fail on four DIFFERENT rows, which is what makes them four cases and not one
  795. # wearing four hats: the callee revert turns `5 3 7' into `7 3 5' (every
  796. # argument lands on its neighbour's parameter), the deferred ParseCallArgs
  797. # zeroes the rows built from computed arguments, ParseCall changes the
  798. # function call's result (19 -> 10) and IoCall changes the last write
  799. # (`FALSE 1' -> `TRUE 544', the first argument printed as the second
  800. # argument's value). A revert that left t36 green is reported by expect_red
  801. # as NOT NON-VACUOUS; a revert that did not land is a BROKEN CASE.
  802. # revert_procedure <name> -- put <name> back the way it was before bug 36 was
  803. # fixed, spliced out of the tree the fix landed on top of. The old code's
  804. # source of truth is the old code: quoting a copy of it in this file is a copy
  805. # that can drift from the tag it claims to reproduce, and a revert that
  806. # reproduces nothing is a case asserting nothing.
  807. revert_procedure () {
  808. python3 - "$1" <<'PYX'
  809. import subprocess, sys
  810. name = sys.argv[1]
  811. cur = open('Compiler.mod').read()
  812. r = subprocess.run(['git', 'show', 'v-TP3-IMAGELAYOUT:shell/Compiler.mod'],
  813. capture_output=True)
  814. assert r.returncode == 0, 'cannot read the pre-fix tree: %s' % r.stderr.decode()[:200]
  815. pre = r.stdout.decode()
  816. def span(text, where):
  817. # The DEFINITION, not the FORWARD declaration a few screens earlier: the
  818. # anchor runs to the end of the line, and a FORWARD line ends `FORWARD ;'
  819. # instead of nothing.
  820. a, b = 'PROCEDURE %s (idx : CARDINAL) ;\n' % name, 'END %s ;' % name
  821. na, nb = text.count(a), text.count(b)
  822. assert na == 1 and nb == 1, \
  823. '%s: %d definitions, %d ends in %s -- update this case' % (name, na, nb, where)
  824. i = text.index(a)
  825. j = text.index(b, i) + len(b)
  826. return text[i:j], i, j
  827. new, i, j = span(cur, 'Compiler.mod')
  828. old, _, _ = span(pre, 'the pre-fix tree')
  829. assert old != new, 'the pre-fix %s is identical to the current one' % name
  830. open('Compiler.mod', 'w').write(cur[:i] + old + cur[j:])
  831. PYX
  832. }
  833. # The callee half: no remap, so parameters are read in declaration order from
  834. # BP+4 while the arguments are pushed in parse order - the FIRST argument ends
  835. # up in the LAST parameter's slot, and one-argument calls (all of
  836. # Runtime.mod's own cases) cannot tell the two layouts apart.
  837. cp "$SAVED_C" Compiler.mod
  838. if python3 - <<'PYX'
  839. p = 'Compiler.mod'
  840. s = open(p).read()
  841. old = """ i := nestMark ;
  842. WHILE i < symTop DO
  843. symtab [i].off := parmOff + 2 - symtab [i].off ;
  844. INC (i)
  845. END ;
  846. """
  847. assert s.count(old) == 1, 'the ProcFunc parameter remap found %d times -- update this mutation' % s.count(old)
  848. open(p, 'w').write(s.replace(old, ''))
  849. PYX
  850. then
  851. if rebuild_compiler; then
  852. expect_red "t36 catches the callee laid out in declaration order (no remap)" \
  853. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  854. else
  855. echo " FAIL: the compiler would not rebuild without the parameter remap"
  856. fail=$((fail + 1))
  857. fi
  858. else
  859. echo " BROKEN CASE: the ProcFunc remap mutation did not apply"
  860. fail=$((fail + 1))
  861. fi
  862. cp "$SAVED_C" Compiler.mod
  863. # The caller half, site 1 of 3: a call used as a statement. The old code
  864. # read the whole list into args[] and pushed it backwards afterwards, so a
  865. # computed argument (which exists only in AX) was gone before the push loop
  866. # reached it.
  867. cp "$SAVED_C" Compiler.mod
  868. if revert_procedure ParseCallArgs; then
  869. if rebuild_compiler; then
  870. expect_red "t36 catches ParseCallArgs pushing after the whole list" \
  871. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  872. else
  873. echo " FAIL: the compiler would not rebuild with ParseCallArgs deferred"
  874. fail=$((fail + 1))
  875. fi
  876. else
  877. echo " BROKEN CASE: the ParseCallArgs revert did not apply"
  878. fail=$((fail + 1))
  879. fi
  880. cp "$SAVED_C" Compiler.mod
  881. # Site 2 of 3: the same call reached from ParseAtom, when a FUNCTION is
  882. # called inside an expression. A separate site, because it is a separate
  883. # procedure - and t36's `19' row is the only thing that notices this one.
  884. cp "$SAVED_C" Compiler.mod
  885. if revert_procedure ParseCall; then
  886. if rebuild_compiler; then
  887. expect_red "t36 catches ParseCall pushing after the whole list" \
  888. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  889. else
  890. echo " FAIL: the compiler would not rebuild with ParseCall deferred"
  891. fail=$((fail + 1))
  892. fi
  893. else
  894. echo " BROKEN CASE: the ParseCall revert did not apply"
  895. fail=$((fail + 1))
  896. fi
  897. cp "$SAVED_C" Compiler.mod
  898. # Site 3 of 3: write/writeln/read/readln, where the argument list is also
  899. # where an inline string literal gets its call. Reverting this procedure
  900. # also reverts the per-argument dispatch inside it (the BYTE read landed
  901. # here), which is harmless for t36 and is why the splice is the whole
  902. # procedure rather than the push alone: half a revert is a state the tree
  903. # was never in.
  904. cp "$SAVED_C" Compiler.mod
  905. if revert_procedure IoCall; then
  906. if rebuild_compiler; then
  907. expect_red "t36 catches IoCall printing the last argument's value" \
  908. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  909. else
  910. echo " FAIL: the compiler would not rebuild with IoCall's loops split"
  911. fail=$((fail + 1))
  912. fi
  913. else
  914. echo " BROKEN CASE: the IoCall revert did not apply"
  915. fail=$((fail + 1))
  916. fi
  917. cp "$SAVED_C" Compiler.mod
  918. # The boolean read (t38): TP3 refuses `readln (b)' with error 66 - prderrtp
  919. # rejects the whole class and the runtime has no xrdbool - and this compiler
  920. # used to accept it and dispatch to rdbool, which stores a WORD into the
  921. # one-byte variable, its second byte landing on the variable AFTER b (t37's
  922. # overflow, unobserved only because no fixture read a boolean). The fix is
  923. # one arm of IoCall's type dispatch, so the mutation restores exactly that
  924. # arm. The check is the COMPILE MATRIX rather than any run: an ERR fixture
  925. # is never executed, so nothing that runs programs on it can see a refusal.
  926. cp "$SAVED_C" Compiler.mod
  927. if python3 - <<'PYX'
  928. p = 'Compiler.mod'
  929. s = open(p).read()
  930. old = """ ELSIF acls = TBool THEN
  931. (* TP3 refuses boolean I/O: TPSRC8 prderrtp is error 66,
  932. "I/O not allowed here", and the runtime has no xrdbool
  933. at all. rdbool stores a WORD, so one byte of an
  934. accepted read would land on the variable AFTER b - the
  935. exact overflow t37_readbyte pins for a byte. TP3's
  936. answer to the whole class is the error; so is ours. *)
  937. Err (EIoHere) ;
  938. RETURN"""
  939. assert s.count(old) == 1, 'the boolean-read error arm found %d times -- update this mutation' % s.count(old)
  940. rep = """ ELSIF acls = TBool THEN
  941. ent := TU_RdBool"""
  942. open(p, 'w').write(s.replace(old, rep))
  943. PYX
  944. then
  945. if rebuild_compiler; then
  946. expect_red "t38 red when a boolean read dispatches to TU_RdBool" \
  947. "t38_readbool" tests/run_compile_tests.sh
  948. else
  949. echo " FAIL: the compiler would not rebuild with the boolean read accepted"
  950. fail=$((fail + 1))
  951. fi
  952. else
  953. echo " BROKEN CASE: the boolean-read mutation did not apply"
  954. fail=$((fail + 1))
  955. fi
  956. cp "$SAVED_C" Compiler.mod
  957. if rebuild_compiler; then
  958. if python3 tests/run_com_exec.py >/dev/null 2>&1; then
  959. # No count: the matrix grows every time a fixture is added, and a
  960. # number here would be right only until the next one.
  961. echo " ok: every executed fixture passes on the restored compiler"
  962. pass=$((pass + 1))
  963. else
  964. echo "NOT RESTORED: run_com_exec.py is red after restoring Compiler.mod"
  965. python3 tests/run_com_exec.py 2>&1 | grep -i fail | head -3 | sed 's/^/ /'
  966. fail=$((fail + 1))
  967. fi
  968. else
  969. echo "NOT RESTORED: the compiler would not rebuild"
  970. fail=$((fail + 1))
  971. fi
  972. echo
  973. echo "== the second execution oracle (run_exec86.py)"
  974. # Everything above runs the image under qemu-system-i386. This one runs the
  975. # SAME image inside shell/Exec86.mod, this project's own in-process 8086
  976. # interpreter, and requires its output to agree with BOTH the hand-derived .out
  977. # and qemu, byte for byte. Two execution checks that could only ever agree
  978. # with each other would be one check: the point of this one is that it was
  979. # written against the 8086's own reference rather than against qemu, whose
  980. # lowest CPU model is a 486 and whose `0F 84' is an ordinary JZ.
  981. #
  982. # So the only mutation worth writing here is one qemu cannot make at all - the
  983. # interpreter's own reading of the machine. Cond is that reading: nibble 4 is
  984. # JE, and inverting it swaps the two arms of every `=' in every program. The
  985. # emitted bytes, the sizes and the compile matrix are untouched, because
  986. # nothing is emitted here - the fault is in who interprets it.
  987. #
  988. # Exec86.mod is recompiled explicitly rather than left to the harness:
  989. # ensure_exec86run() notices the source changed and RELINKS, but does not
  990. # recompile it, so a mutated source with a stale object would link the good
  991. # interpreter straight back in and stay green - which is the trap its own
  992. # docstring records, and the reason this case compiles first.
  993. #
  994. # SAVED_E itself was taken at the top of this file, next to the EXIT trap that
  995. # puts it back.
  996. if python3 - <<'PYX'
  997. p = 'Exec86.mod'
  998. s = open(p).read()
  999. old = "| 4H : r := ZF"
  1000. new = "| 4H : r := NOT ZF (* MUTATION: JE inverted *)"
  1001. assert s.count(old) == 1, 'the JE arm of Cond found %d times -- update this mutation' % s.count(old)
  1002. open(p, 'w').write(s.replace(old, new))
  1003. PYX
  1004. then
  1005. if $GM2 -fiso -c Exec86.mod >/dev/null 2>&1; then
  1006. expect_red "the interpreter's own oracle catches JE inverted" \
  1007. "t33_cmpops" python3 tests/run_exec86.py t33_cmpops
  1008. else
  1009. echo " FAIL: Exec86.mod would not compile with JE inverted"
  1010. fail=$((fail + 1))
  1011. fi
  1012. else
  1013. echo " BROKEN CASE: the Cond JE mutation did not apply"
  1014. fail=$((fail + 1))
  1015. fi
  1016. cp "$SAVED_E" Exec86.mod
  1017. # And the check on the RESTORED interpreter, because a green above could also
  1018. # come from a mutation that never took and an object left mutated by a run
  1019. # that died in between.
  1020. if $GM2 -fiso -c Exec86.mod >/dev/null 2>&1; then
  1021. if python3 tests/run_exec86.py >/dev/null 2>&1; then
  1022. echo " ok: run_exec86 green on the restored interpreter"
  1023. pass=$((pass + 1))
  1024. else
  1025. echo "NOT RESTORED: run_exec86.py is red after restoring Exec86.mod"
  1026. python3 tests/run_exec86.py 2>&1 | grep -i "fail" | head -3 | sed 's/^/ /'
  1027. fail=$((fail + 1))
  1028. fi
  1029. else
  1030. echo "NOT RESTORED: Exec86.mod would not recompile"
  1031. fail=$((fail + 1))
  1032. fi
  1033. echo
  1034. echo "== the R key: compile, poke, run, report (runtest.py)"
  1035. # The only check in the project that exercises CmdRun. Everything above looks
  1036. # at bytes, or at what qemu says the image does; this one drives the shell
  1037. # through a pty the way a person would, presses R, and compares the GUEST's
  1038. # output against the fixture's hand-derived .out - so it fails on things no
  1039. # byte check can see and no file records either, because R writes no file at
  1040. # all (which is itself asserted).
  1041. #
  1042. # The mutation is the poke loop's count. With n = 0 nothing is copied into
  1043. # the interpreter, loadHi stays where Clear86 left it - 0100h - and Run86
  1044. # faults on its very first step, "execution left the loaded image", before the
  1045. # guest has executed a single instruction. That is fast and deterministic, which
  1046. # matters: the obvious alternative (poking the image somewhere else) leaves the
  1047. # machine executing zeros and buys a step-limit timeout instead of a finding.
  1048. cp "$SAVED_SH" Shell.mod
  1049. if python3 - <<'PYX'
  1050. p = 'Shell.mod'
  1051. s = open(p).read()
  1052. old = " n := LinkSize () ;"
  1053. new = " n := 0 ; (* MUTATION: nothing is poked *)"
  1054. assert s.count(old) == 1, 'the poke count in CmdRun found %d times -- update this mutation' % s.count(old)
  1055. open(p, 'w').write(s.replace(old, new))
  1056. PYX
  1057. then
  1058. if make > ../tmp/nonvacuity.make.log 2>&1; then
  1059. expect_red "the R check catches an image that was never poked" \
  1060. "AH=4Ch exit" python3 tests/runtest.py
  1061. else
  1062. echo " FAIL: the shell would not rebuild with the poke loop neutered"
  1063. tail -5 ../tmp/nonvacuity.make.log | sed 's/^/ /'
  1064. fail=$((fail + 1))
  1065. fi
  1066. else
  1067. echo " BROKEN CASE: the CmdRun poke mutation did not apply"
  1068. fail=$((fail + 1))
  1069. fi
  1070. cp "$SAVED_SH" Shell.mod
  1071. if make > ../tmp/nonvacuity.make.log 2>&1; then
  1072. if python3 tests/runtest.py >/dev/null 2>&1; then
  1073. echo " ok: runtest green on the restored shell"
  1074. pass=$((pass + 1))
  1075. else
  1076. echo "NOT RESTORED: runtest.py is red after restoring Shell.mod"
  1077. python3 tests/runtest.py 2>&1 | grep -i "fail" | head -3 | sed 's/^/ /'
  1078. fail=$((fail + 1))
  1079. fi
  1080. else
  1081. echo "NOT RESTORED: the shell would not rebuild"
  1082. tail -5 ../tmp/nonvacuity.make.log | sed 's/^/ /'
  1083. fail=$((fail + 1))
  1084. fi
  1085. echo
  1086. echo "== 8086 legality of the conditional lowering (check_8086.py)"
  1087. # This whole section exists because of a fault that every other check in the
  1088. # project was blind to, and being blunt about WHY is the point of listing it.
  1089. #
  1090. # EmJcc and EmSetcc emitted `0F 8x rel16' and `0F 9x rel8'. `0F' is a
  1091. # 386-and-later opcode prefix; the 8086 has none. So EVERY conditional branch
  1092. # and EVERY comparison in EVERY compiled program was an illegal instruction on
  1093. # the machine this compiler targets. And all of the following were green while
  1094. # it was: the compile matrix, the .COM layout checker, the runtime golden, the
  1095. # emitter audit, and the whole execution suite answering correctly under qemu.
  1096. #
  1097. # Two reasons, and the second is the one worth keeping:
  1098. # 1. qemu-system-i386 has no 8086 model. Its lowest is 486, where `0F 84' is
  1099. # a perfectly ordinary JZ. So the execution oracle CANNOT see this class
  1100. # of fault, ever.
  1101. # 2. FCML is this project's INDEPENDENT disassembler, and FCML's -m16 mode is
  1102. # a 386. The one tool whose job is to say "this is not a real instruction"
  1103. # was architecturally guaranteed to agree with the bug.
  1104. #
  1105. # So a check was needed that asks the question nothing else was asking. The
  1106. # five mutations below are the mutations that check has to catch, and each was
  1107. # run by hand and confirmed red BEFORE this section existed.
  1108. #
  1109. # M4 and M5 are the interesting pair. M4 is a fault this project actually
  1110. # introduced while fixing the above: EmJcc jumps TO its target, but the 8086
  1111. # shape steps OVER a 3-byte EJMP, so the naive port inverts every conditional
  1112. # in every program. It was caught by execution, not by any byte check. M5 is
  1113. # the partial version -- the inversion dropped for the IF and CASE sites only,
  1114. # kept for FOR -- and MEASURING that is what produced clause H, because the
  1115. # check was blind to M5 as first written: IF declares nibble 4, CASE declares
  1116. # 5, they negate into each other, and both are declared, so nothing complained
  1117. # while every conditional in every program took the wrong path. FOR declares
  1118. # C and F, whose negations D and E are declared for nothing at all, so the
  1119. # whole-suite version was caught by luck.
  1120. #
  1121. # Note these need `rebuild_compiler' only to refresh comtest; check_8086.py
  1122. # relinks the fixtures itself. Getting that wrong is how this section's first
  1123. # draft reported a mutation as VACUOUS: the build was skipped, a stale comtest
  1124. # ran the GOOD compiler, and the check passed. A helper that exits non-zero
  1125. # makes `if mutate_x; then' false and the case is silently skipped, which looks
  1126. # exactly like a pass -- so rebuild_compiler is checked, not assumed.
  1127. # M1: the original defect, restored verbatim.
  1128. cp "$SAVED_C" Compiler.mod
  1129. if python3 - <<'PYX'
  1130. p = 'Compiler.mod'
  1131. s = open(p).read()
  1132. old = """ Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)
  1133. Ebyte (03H) ; (* rel8: step over the 3-byte EJMP *)
  1134. RETURN EmJmpNear (target) (* target = 0 => forward, see above *)"""
  1135. new = """ Ebyte (0FH) ; Ebyte (cc) ; Eword (0) ;
  1136. IF target = 0 THEN
  1137. RETURN nPatch
  1138. END ;
  1139. RETURN 0"""
  1140. assert s.count(old) == 1, 'EmJcc body found %d times -- update this mutation' % s.count(old)
  1141. open(p, 'w').write(s.replace(old, new))
  1142. PYX
  1143. then
  1144. if rebuild_compiler; then
  1145. expect_red "check_8086 catches a 0F 8x branch (the original bug)" \
  1146. "0F 84" python3 tests/check_8086.py
  1147. else
  1148. echo " FAIL: the compiler would not rebuild with the 0F branch"
  1149. fail=$((fail + 1))
  1150. fi
  1151. else
  1152. echo " BROKEN CASE: the 0F branch mutation did not apply"
  1153. fail=$((fail + 1))
  1154. fi
  1155. cp "$SAVED_C" Compiler.mod
  1156. # M2: the other original defect, SETcc plus the MOV AH,0 it needed.
  1157. cp "$SAVED_C" Compiler.mod
  1158. if python3 - <<'PYX'
  1159. p = 'Compiler.mod'
  1160. s = open(p).read()
  1161. old = """ Ebyte (0B8H) ; Eword (1) ; (* MOV AX,#0001 *)
  1162. Ebyte (JccShort (cc)) ; (* taken when the comparison HOLDS *)
  1163. Ebyte (01H) ; (* rel8: step over the DEC AX *)
  1164. Ebyte (48H) (* DEC AX *)"""
  1165. new = """ Ebyte (0FH) ; Ebyte (cc) ; Ebyte (0C0H) ;
  1166. EmMovAh0 ()"""
  1167. assert s.count(old) == 1, 'EmSetcc body found %d times -- update this mutation' % s.count(old)
  1168. open(p, 'w').write(s.replace(old, new))
  1169. PYX
  1170. then
  1171. if rebuild_compiler; then
  1172. expect_red "check_8086 catches a 0F 9x SETcc (the original bug)" \
  1173. "0F 9F C0" python3 tests/check_8086.py
  1174. else
  1175. echo " FAIL: the compiler would not rebuild with the 0F SETcc"
  1176. fail=$((fail + 1))
  1177. fi
  1178. else
  1179. echo " BROKEN CASE: the 0F SETcc mutation did not apply"
  1180. fail=$((fail + 1))
  1181. fi
  1182. cp "$SAVED_C" Compiler.mod
  1183. # M4: the polarity inversion, everywhere. Note the expected text is clause H's,
  1184. # not a shape complaint: every byte here is a legal 8086 shape, which is the
  1185. # entire reason a separate clause had to be written.
  1186. cp "$SAVED_C" Compiler.mod
  1187. if python3 - <<'PYX'
  1188. p = 'Compiler.mod'
  1189. s = open(p).read()
  1190. old = ' Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)'
  1191. new = ' Ebyte (JccShort (cc)) ; (* MUTATION: inversion dropped *)'
  1192. assert s.count(old) == 1, 'EmJcc Ebyte found %d times -- update this mutation' % s.count(old)
  1193. open(p, 'w').write(s.replace(old, new))
  1194. PYX
  1195. then
  1196. if rebuild_compiler; then
  1197. expect_red "clause H catches the branch polarity inverted everywhere" \
  1198. "but reading the source says" python3 tests/check_8086.py
  1199. else
  1200. echo " FAIL: the compiler would not rebuild with the inversion dropped"
  1201. fail=$((fail + 1))
  1202. fi
  1203. else
  1204. echo " BROKEN CASE: the polarity mutation did not apply"
  1205. fail=$((fail + 1))
  1206. fi
  1207. cp "$SAVED_C" Compiler.mod
  1208. # M5: the same inversion dropped for the IF and CASE sites ONLY. This one is
  1209. # the reason clause H exists: it was run by hand and the check stayed GREEN,
  1210. # and FOR's C/F nibbles were the only reason the whole-suite version (M4)
  1211. # happened to be caught.
  1212. cp "$SAVED_C" Compiler.mod
  1213. if python3 - <<'PYX'
  1214. p = 'Compiler.mod'
  1215. s = open(p).read()
  1216. old = ' Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)'
  1217. new = """ IF (cc = 84H) OR (cc = 85H) THEN
  1218. Ebyte (JccShort (cc)) (* MUTATION: no inversion here *)
  1219. ELSE
  1220. Ebyte (JccShortInv (cc))
  1221. END ;"""
  1222. assert s.count(old) == 1, 'EmJcc Ebyte found %d times -- update this mutation' % s.count(old)
  1223. open(p, 'w').write(s.replace(old, new))
  1224. PYX
  1225. then
  1226. if rebuild_compiler; then
  1227. expect_red "clause H catches the inversion dropped for IF and CASE only" \
  1228. "t22_case" python3 tests/check_8086.py
  1229. else
  1230. echo " FAIL: the compiler would not rebuild with the partial inversion"
  1231. fail=$((fail + 1))
  1232. fi
  1233. else
  1234. echo " BROKEN CASE: the partial-inversion mutation did not apply"
  1235. fail=$((fail + 1))
  1236. fi
  1237. cp "$SAVED_C" Compiler.mod
  1238. # And the check on the RESTORED compiler, so a green above cannot come from a
  1239. # mutation that failed to take and left the real defect in place.
  1240. if rebuild_compiler; then
  1241. if python3 tests/check_8086.py >/dev/null 2>&1; then
  1242. echo " ok: check_8086 green on the restored compiler"
  1243. pass=$((pass + 1))
  1244. else
  1245. echo "NOT RESTORED: check_8086.py is red after restoring Compiler.mod"
  1246. python3 tests/check_8086.py 2>&1 | grep -m3 -- ' - ' | sed 's/^/ /'
  1247. fail=$((fail + 1))
  1248. fi
  1249. else
  1250. echo "NOT RESTORED: the compiler would not rebuild"
  1251. fail=$((fail + 1))
  1252. fi
  1253. echo
  1254. echo "== the emitter-name audit of Compiler.mod (audit_helpers.py)"
  1255. # These need no rebuild: the audit reads the SOURCE, not the built object, so
  1256. # they are the cheapest cases here and they cover the module the audit used
  1257. # not to look at at all. That is the point of the section: the audit reported
  1258. # "every helper agrees with its name" for a module it had never examined, and
  1259. # EmXchgAxCx was `93` (XCHG BX,AX) under a name that says XCHG AX,CX for the
  1260. # whole life of the project. Two of these five are for faults that were real.
  1261. SAVED_C2=../tmp/nonvacuity.Compiler.mod.2
  1262. SAVED_R2=../tmp/nonvacuity.Runtime.mod.2
  1263. cp Compiler.mod "$SAVED_C2" || exit 1
  1264. cp Runtime.mod "$SAVED_R2" || exit 1
  1265. restore_audit_sources () {
  1266. cp "$SAVED_C2" Compiler.mod
  1267. cp "$SAVED_R2" Runtime.mod
  1268. }
  1269. AUD="python3 tests/audit_helpers.py"
  1270. # 1. THE fault. 91h is XCHG AX,CX; 93h is XCHG BX,AX. Both are one byte, so
  1271. # the compile matrix never moved and the byte counts never moved.
  1272. cp "$SAVED_C2" Compiler.mod
  1273. mutate Compiler.mod 's|^ Ebyte (91H)$| Ebyte (93H)|'
  1274. expect_red "audit catches XchgAxCx emitting XCHG BX,AX" \
  1275. "exchanges Ax and Bx" $AUD
  1276. restore_audit_sources
  1277. # 2. the coverage check itself. A parameter list that find_helpers does not
  1278. # accept is exactly how the real emitter was missed, and the inventory is
  1279. # scanned separately on purpose so this can be caught. Without the
  1280. # independent scan this case is silent, because both lists would come from
  1281. # the same parser and agree that the helper does not exist.
  1282. cp "$SAVED_C2" Compiler.mod
  1283. mutate Compiler.mod 's|^PROCEDURE EmXchgAxCx () ;$|PROCEDURE EmXchgAxCx (why : CARDINAL) ;|'
  1284. expect_red "audit reports an emitter it cannot reach, rather than skipping it" \
  1285. "never examined it" $AUD
  1286. restore_audit_sources
  1287. # 3. EmXchgAxDx was named EmMoveAxDx, which said MOV where the bytes say XCHG.
  1288. # 93h here is XCHG AX,BX - one letter away, the exact class of mistake the
  1289. # name is supposed to make impossible.
  1290. cp "$SAVED_C2" Compiler.mod
  1291. mutate Compiler.mod 's|^ Ebyte (92H)$| Ebyte (93H)|'
  1292. expect_red "audit catches XchgAxDx emitting XCHG BX,AX" \
  1293. "XchgAxDx" $AUD
  1294. restore_audit_sources
  1295. # 4. CmpArgW0's [BP+2] written as the 386 SIB form, which decodes on a 8086 as
  1296. # [SI+24h]. A real bug: the runtime was clearing the wrong memory.
  1297. cp "$SAVED_R2" Runtime.mod
  1298. mutate Runtime.mod 's| B (83H) ; B (7EH) ; B (2) ; B (0) ;| B (83H) ; B (7CH) ; B (24) ; B (0) ; B (0) ;|'
  1299. expect_red "audit catches the [SI+24h] encoding of [BP+2]" \
  1300. "memory base is 'si" $AUD
  1301. restore_audit_sources
  1302. # 5. MovAxSp is POP then PUSH, because MOV AX,[SP] does not exist on an 8086.
  1303. # Dropping the POP leaves the stack one word short - a fault in the shape,
  1304. # not in a byte value.
  1305. cp "$SAVED_C2" Compiler.mod
  1306. python3 - <<'PYEOF'
  1307. p='Compiler.mod'; s=open(p).read()
  1308. a=" Ebyte (58H) ; (* POP AX *)\n"
  1309. assert s.count(a)==1, "EmMovAxSp POP line not found -- update this mutation"
  1310. open(p,'w').write(s.replace(a, ""))
  1311. PYEOF
  1312. expect_red "audit catches MovAxSp with its POP missing" \
  1313. "MovAxSp" $AUD
  1314. restore_audit_sources
  1315. # 6. The two-instruction shape: IDIV is CWD then IDIV, and dropping the CWD
  1316. # leaves an un-sign-extended dividend in DX:AX. Both are still present as
  1317. # a two-step spec, so a missing step has to be visible.
  1318. cp "$SAVED_C2" Compiler.mod
  1319. mutate Compiler.mod 's| Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)| Ebyte (0F7H) ; Ebyte (0F9H)|'
  1320. expect_red "audit catches IDiv without the CWD that extends the dividend" \
  1321. "IDivAxCx" $AUD
  1322. restore_audit_sources
  1323. # 7. The byte under EmXorAl01's name, and the row in PATTERNS that was added
  1324. # to admit it. `34 01' is XOR AL,#01 - the boolean NOT, and the one emitter
  1325. # in Compiler.mod with no ModRM byte at all. A grammar row nobody has ever
  1326. # seen reject anything cannot be trusted to accept only the truth, so here
  1327. # is the rejection: `34 02' is the same length, decodes just as cleanly, and
  1328. # `not x' would flip bit 1 instead of bit 0.
  1329. cp "$SAVED_C2" Compiler.mod
  1330. mutate Compiler.mod 's|^ Ebyte (34H) ; Ebyte (01H)| Ebyte (34H) ; Ebyte (02H)|'
  1331. expect_red "audit catches EmXorAl01 emitting xor al,#2 under a name saying #1" \
  1332. "XorAl01" $AUD
  1333. restore_audit_sources
  1334. # 7b. The other half of that row: the opcode gate. 34h is XOR AL,#imm and
  1335. # 35h is XOR AX,#imm - a word, not a byte, under a name that says AL. The
  1336. # row is pinned to {034h} on purpose, and a pin that has never been asked
  1337. # to hold is a pin. The report is the one thing the row above cannot
  1338. # produce: with no reading at all, the helper falls out of the grammar.
  1339. cp "$SAVED_C2" Compiler.mod
  1340. mutate Compiler.mod 's|^ Ebyte (34H) ; Ebyte (01H)| Ebyte (35H) ; Ebyte (01H)|'
  1341. expect_red "the opcode gate rejects XOR AX under an XOR AL name" \
  1342. "no name pattern accepts it" $AUD
  1343. restore_audit_sources
  1344. if $AUD >/dev/null 2>&1; then
  1345. echo " ok: the audit passes on both restored sources"
  1346. pass=$((pass + 1))
  1347. else
  1348. echo "NOT RESTORED: the audit is red after restoring the sources"
  1349. $AUD 2>&1 | sed 's/^/ /'
  1350. fail=$((fail + 1))
  1351. fi
  1352. echo
  1353. echo "== the BP contract rt_exec.py checks before it starts a machine"
  1354. # wrchar and wrbool both borrowed BP to reach their argument -- [SP] is not
  1355. # encodable in 16-bit mode -- and neither saved it. The driver's cursor into
  1356. # the case record lives in BP precisely because BP is the one register an entry
  1357. # may keep, so "wrchar borrowed it and did not give it back" sent the second
  1358. # call to a garbage address, the machine triple-faulted, and the run printed the
  1359. # record header twice and hung. Both the golden and the audit call that shape
  1360. # CORRECT: the bytes are well formed, every branch is on a boundary, the size
  1361. # is unchanged, and the decode says exactly what it says. So the rule is now
  1362. # checked directly, and these two cases are what make that check more than a
  1363. # claim.
  1364. #
  1365. # rt_exec.py needs the whole runtime rebuilt and then boots 36 machines, so this
  1366. # section is the slow one. The baseline comes first and is asserted: a case
  1367. # that mutates a red tree proves nothing.
  1368. SAVED_R3=../tmp/nonvacuity.Runtime.mod.3
  1369. cp Runtime.mod "$SAVED_R3" || exit 1
  1370. if rebuild; then
  1371. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  1372. echo " ok: baseline - rt_exec.py passes on the unmutated runtime"
  1373. pass=$((pass + 1))
  1374. else
  1375. echo " FAIL: the baseline is already red, so the cases below prove"
  1376. echo " nothing - fix the baseline before reading them"
  1377. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  1378. fail=$((fail + 1))
  1379. fi
  1380. else
  1381. echo " FAIL: could not rebuild the runtime for the baseline"
  1382. fail=$((fail + 1))
  1383. fi
  1384. # 1. THE fault: drop the PUSH, exactly as EmitWrChar was written. This is the
  1385. # shape the behavioural case found, so the byte-level check must find it too
  1386. # -- a check that only the expensive test can trip is a check that has not
  1387. # been made to earn its place.
  1388. cp "$SAVED_R3" Runtime.mod
  1389. python3 - <<'PYEOF'
  1390. p = 'Runtime.mod'
  1391. s = open(p).read()
  1392. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n'
  1393. assert s.count(a) == 1, "EmitWrChar prologue not found exactly once"
  1394. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n'))
  1395. PYEOF
  1396. if rebuild; then
  1397. expect_red "the BP contract catches wrchar borrowing BP unsaved" \
  1398. "borrows BP but does not open with" python3 tests/rt_exec.py
  1399. else
  1400. echo " FAIL: the runtime would not rebuild with the PUSH removed"
  1401. fail=$((fail + 1))
  1402. fi
  1403. cp "$SAVED_R3" Runtime.mod
  1404. # 2. The mirror image: push it and never pop it. A different one-instruction
  1405. # omission with the same consequence for a caller, and the reason the rule
  1406. # asks for the 5D and not just the 55.
  1407. cp "$SAVED_R3" Runtime.mod
  1408. mutate Runtime.mod 's|^ MovSpBp ; PopBp ;$| MovSpBp ;|'
  1409. if rebuild; then
  1410. expect_red "the BP contract catches a BP that is pushed and never popped" \
  1411. "pushes BP but never pops it" python3 tests/rt_exec.py
  1412. else
  1413. echo " FAIL: the runtime would not rebuild with the POP removed"
  1414. fail=$((fail + 1))
  1415. fi
  1416. cp "$SAVED_R3" Runtime.mod
  1417. # 3. The scan's other silent failure: a rule that matches nothing looks exactly
  1418. # like a rule that passes. The pattern the check looks for is changed to one
  1419. # the blob does not contain -- MOV BP,DI, which the runtime has no reason to
  1420. # emit -- so the check has examined thirteen entries and matched nothing and
  1421. # MUST say so rather than report a clean sweep. This is the general shape of
  1422. # the fault this project keeps making: a check whose SUBJECT has drifted
  1423. # reports a confident answer about the wrong thing.
  1424. cp "$SAVED_R3" Runtime.mod
  1425. SAVED_X=../tmp/nonvacuity.rt_exec.py
  1426. cp tests/rt_exec.py "$SAVED_X" || exit 1
  1427. mutate tests/rt_exec.py 's|^MOV_BP_SP = b"\\x8b\\xec" .*$|MOV_BP_SP = b"\\x8b\\xed" # MOV BP,DI: never emitted|' \
  1428. expect_red "a check that matched nothing is a failure, not a pass" \
  1429. "matched nothing" python3 tests/rt_exec.py
  1430. cp "$SAVED_X" tests/rt_exec.py
  1431. if rebuild; then
  1432. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  1433. echo " ok: rt_exec.py passes on the restored source"
  1434. pass=$((pass + 1))
  1435. else
  1436. echo "NOT RESTORED: rt_exec.py is red after restoring Runtime.mod"
  1437. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  1438. fail=$((fail + 1))
  1439. fi
  1440. else
  1441. echo "NOT RESTORED: the runtime would not rebuild"
  1442. fail=$((fail + 1))
  1443. fi
  1444. # 4. The GOLDEN and the entry goldens, which are the other half of the same
  1445. # rule. rt_exec.py states the contract; check_runtime.py pins the bytes.
  1446. # They are separate mechanisms and the case below is what shows the golden
  1447. # one works on its own -- a re-baseline of runtime.golden would otherwise
  1448. # have absorbed the new prologues silently, and the next person to bless it
  1449. # would have no way to know the PUSH and POP were ever missing.
  1450. cp "$SAVED_R3" Runtime.mod
  1451. python3 - <<'PYEOF'
  1452. p = 'Runtime.mod'
  1453. s = open(p).read()
  1454. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n MovAlArg4 ;\n MovSpBp ; PopBp ;\n'
  1455. assert s.count(a) == 1, "EmitWrChar frame not found exactly once"
  1456. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n MovAlArg2 ;\n MovSpBp ;\n'))
  1457. PYEOF
  1458. if rebuild; then
  1459. expect_red "the entry golden catches wrchar without its PUSH BP" \
  1460. "entry wrchar starts 8B EC" python3 tests/check_runtime.py "$DUMP"
  1461. else
  1462. echo " FAIL: the runtime would not rebuild with wrchar's frame removed"
  1463. fail=$((fail + 1))
  1464. fi
  1465. cp "$SAVED_R3" Runtime.mod
  1466. echo
  1467. echo "== the .COM layout check, and the runtime size it now measures"
  1468. # The checker used to RESTATE the runtime's size as a literal. It had drifted
  1469. # from the runtime it described, so the header was read out of the code stream
  1470. # and EVERY linked fixture "failed" on it - a whole file of "failures" that were
  1471. # not findings at all. A duplicated constant that has drifted does not fail
  1472. # loudly; it re-reports the same falsehood, in which the real failures hide.
  1473. # The size is now MEASURED from the image.
  1474. #
  1475. # These cases corrupt a real emitted .COM and require the checker to notice.
  1476. # They need the images, so they are built once and copied; the checker has a
  1477. # --check-only mode for exactly this, because its scratch directory is normally
  1478. # deleted on exit and a check that has only ever seen the truth is not a check.
  1479. KEEPDIR=../tmp/nonvacuity.com
  1480. rm -rf "$KEEPDIR"
  1481. TP_COM_KEEP=1 tests/run_com_tests.sh >../tmp/nonvacuity.com.log 2>&1
  1482. KEEP=$(sed -n 's/^TP_COM_KEEP=1: images left in //p' \
  1483. ../tmp/nonvacuity.com.log | tail -1)
  1484. if [ -z "$KEEP" ] || [ ! -d "$KEEP" ]; then
  1485. echo " FAIL: could not obtain emitted .COM images for the layout cases"
  1486. fail=$((fail + 1))
  1487. else
  1488. COMCHK="tests/run_com_tests.sh --check-only"
  1489. # 0. The baseline. Every case below is a claim that a specific assertion
  1490. # turns red, and none of them means anything if the copies of untouched
  1491. # images already fail. (The stale RT_SZ produced exactly that: a whole
  1492. # file of "failures" that were not findings.) So this is asserted first,
  1493. # and a failure here is reported as a broken baseline rather than a red
  1494. # test.
  1495. #
  1496. # The baseline is also WHERE THE HEADER OFFSET COMES FROM. Cases 1 and 2
  1497. # used to carry it as the literal 435 and 439, which were ENT_SZ + the
  1498. # runtime size at the time -- 432. Two 4-byte changes to the runtime
  1499. # later, both cases were editing the wrong bytes: 435 had become four
  1500. # bytes inside the runtime itself, so case 1 had stopped testing "the
  1501. # header cannot be found" and had started testing "the checker also
  1502. # notices you scribbled on the code", and case 2 had become a no-op
  1503. # that wrote the header where it already was. Both still went red, for
  1504. # reasons the messages did not name, which is the whole problem: a
  1505. # hard-coded offset is a claim about the world that expires silently.
  1506. #
  1507. # So the offset is read back out of the checker's own report on the
  1508. # untouched images, and the expected numbers below are ARITHMETIC ON IT.
  1509. # If the runtime grows again, these cases still test what they say.
  1510. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  1511. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  1512. BASE_OUT=$($COMCHK "$KEEPDIR" 2>&1)
  1513. if [ $? -eq 0 ]; then
  1514. echo " ok: baseline - untouched copies of the real images all pass"
  1515. pass=$((pass + 1))
  1516. else
  1517. echo " FAIL: the baseline is already red, so the cases below prove"
  1518. echo " nothing - fix the baseline before reading them"
  1519. printf '%s\n' "$BASE_OUT" | grep FAIL | head -3 | sed 's/^/ /'
  1520. fail=$((fail + 1))
  1521. fi
  1522. BASE_RT=$(printf '%s\n' "$BASE_OUT" \
  1523. | sed -n 's/.*measured runtime size: \([0-9][0-9]*\) bytes.*/\1/p' \
  1524. | head -1)
  1525. if [ -z "$BASE_RT" ]; then
  1526. echo " FAIL: the checker did not report the runtime size it measured,"
  1527. echo " so the cases below cannot find the header to edit"
  1528. fail=$((fail + 1))
  1529. BASE_RT=0
  1530. fi
  1531. # The layout constants are ENT_SZ 3 and HDR_SZ 16 (tests/comimage.py), so the
  1532. # header sits at hdrOff = 3 + rtSz and the entry jump's displacement must be
  1533. # (hdrOff + 16) - 3. The wanted number is therefore computed from the header
  1534. # OFFSET, not from rtSz: the two differ by 3, and getting that backwards
  1535. # produces a plausible-looking expectation three bytes out, which is how
  1536. # this case came to expect "want 452" and then be reported as not proving
  1537. # what it said.
  1538. BASE_OFF=$((3 + BASE_RT))
  1539. # Case 2 claims a runtime four bytes LONGER, so the header - and with it the
  1540. # demanded jump target - moves four bytes further on.
  1541. SHIFTED_WANT=$((BASE_OFF + 4 + 16 - 3))
  1542. echo " (measured runtime size $BASE_RT, header at image offset $BASE_OFF)"
  1543. # 1. Break hdrDS so it no longer ties the header to its own offset. The
  1544. # header must become UNFINDABLE and be reported as such - a checker that
  1545. # fell back to a remembered offset would report a confident number here,
  1546. # which is the failure mode the measurement was introduced to remove.
  1547. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  1548. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  1549. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" <<'PYEOF'
  1550. import sys
  1551. p, off = sys.argv[1], int(sys.argv[2])
  1552. d = bytearray(open(p, 'rb').read())
  1553. d[off + 4:off + 6] = (0x1234).to_bytes(2, 'little') # hdrDS, no longer self-consistent
  1554. open(p, 'wb').write(bytes(d))
  1555. PYEOF
  1556. expect_red "a header that cannot be located is reported, not assumed" \
  1557. "no program header found" $COMCHK "$KEEPDIR"
  1558. # 2. A complete, self-consistent header four bytes later, so the measured
  1559. # runtime size becomes $((BASE_RT + 4)) instead of $BASE_RT. This is the
  1560. # positive half of the same check: the derivation must FOLLOW the file,
  1561. # and the entry jump assertion - expressed in terms of the measurement -
  1562. # must follow it too, demanding $SHIFTED_WANT rather than $((BASE_OFF + 16 - 3)).
  1563. #
  1564. # hdrCS is copied from the header already in the file rather than written
  1565. # as a literal. It used to be the literal 464+100h, which was the image
  1566. # length when the image was 720 bytes; four bytes of runtime later it was
  1567. # 464+100h against a 724-byte image, so this case was ALSO failing on
  1568. # hdrCS, for a reason three lines below the one it was written to test.
  1569. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  1570. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  1571. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" "$SHIFTED_WANT" <<'PYEOF'
  1572. import sys
  1573. p, off, want = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
  1574. d = bytearray(open(p, 'rb').read())
  1575. hdrCS = int.from_bytes(d[off + 2:off + 4], 'little') # unchanged: still the image end
  1576. off += 4
  1577. ds = off + 0x1000 + 0x100
  1578. w = [1, hdrCS, ds, ds + 4, 0, 0, 0, 0]
  1579. for i, x in enumerate(w):
  1580. d[off + 2 * i:off + 2 * i + 2] = x.to_bytes(2, 'little')
  1581. open(p, 'wb').write(bytes(d))
  1582. PYEOF
  1583. expect_red "the measured runtime size follows the image ($BASE_RT -> $((BASE_RT + 4)))" \
  1584. "want $SHIFTED_WANT" $COMCHK "$KEEPDIR"
  1585. # 3. The entry jump's opcode. One byte, and the only assertion in the
  1586. # project that can see where execution STARTS.
  1587. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  1588. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  1589. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  1590. import sys
  1591. p = sys.argv[1]
  1592. d = bytearray(open(p, 'rb').read())
  1593. d[0] = 0xEA
  1594. open(p, 'wb').write(bytes(d))
  1595. PYEOF
  1596. expect_red "the entry jump must be E9, not a near JMP" \
  1597. "not the E9 of the entry jump" $COMCHK "$KEEPDIR"
  1598. # 4. The entry jump's target, moved one instruction earlier. A .COM that
  1599. # lands in the middle of the prologue runs, prints something and exits
  1600. # cleanly, so no size or structure check can see this.
  1601. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  1602. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  1603. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  1604. import sys
  1605. p = sys.argv[1]
  1606. d = bytearray(open(p, 'rb').read())
  1607. d[1:3] = (100).to_bytes(2, 'little')
  1608. open(p, 'wb').write(bytes(d))
  1609. PYEOF
  1610. expect_red "the entry jump must land on the first instruction" \
  1611. "entry jump rel16=100" $COMCHK "$KEEPDIR"
  1612. rm -rf "$KEEPDIR"
  1613. fi
  1614. echo
  1615. echo "non-vacuity: $pass ok, $fail failed"
  1616. [ "$fail" -eq 0 ]