nonvacuity.sh 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446
  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. # rt_exec.py the string entries: catches rdstr storing its first
  48. # character where the length byte belongs (a missing IncSi
  49. # puts `hello' back as 05 65 6c 6c 6f), and strstore
  50. # running LOOP with CX = 0 when the source is empty - 65536
  51. # iterations that wrap the segment and walk through the
  52. # driver's own case record. Both are same-size,
  53. # matrix-green slips that only a byte dump can see.
  54. # run_com_exec.py the frame rule: catches a local STRING packed with the
  55. # INTEGER rule, so its characters grow up over the saved BP
  56. # and the return address; readln into such a local then
  57. # runs the string's own bytes as an address. The fix moves
  58. # no well-formed instruction, so only execution - or the
  59. # size it pins in the compile matrix - can tell.
  60. # run_com_exec.py the string arms: catches the destination limit loaded
  61. # into AX instead of CX, the assignment pushes swapped so
  62. # the destination is read as its own source, and a string
  63. # read dispatched to the integer reader. Every one is the
  64. # right shape reading the wrong thing, and the MovCxi
  65. # helper is COMP in the audit, so no byte check can see it.
  66. # check_8086.py the 8086 opcodes: catches a conditional branch or a
  67. # SETcc emitted as `0F 8x'/`0F 9x', which are 386-only.
  68. # Nothing else here can: qemu-system-i386's lowest CPU
  69. # model is 486, where both are ordinary instructions, and
  70. # FCML's -m16 mode is a 386 too. Clause H additionally
  71. # catches the branch polarity being inverted, which is
  72. # still a legal opcode and therefore invisible to every
  73. # shape-based check.
  74. # run_com_exec.py behaviour: catches a*b that emits an ADD, `>'
  75. # and `>=' swapped, REPEAT..UNTIL that stops after one
  76. # pass, two procedures whose parameters collide, a left
  77. # operand overwritten by the right one's code (SaveLeft),
  78. # and a boolean `not' lowered as the integer one. Every
  79. # one of them sat in a fixture that COMPILED and was
  80. # never RUN, with every byte-level check green.
  81. # run_compile_tests.sh refusal: catches a boolean READ slipping
  82. # through to the runtime. TP3 refuses it with error 66
  83. # (TPSRC8 prderrtp; its runtime has no xrdbool), and
  84. # this compiler used to accept it and let rdbool store
  85. # a WORD into the one-byte variable. The mutation is
  86. # the one arm of IoCall's type dispatch that refuses it,
  87. # and only the COMPILE MATRIX can see a refusal: an ERR
  88. # fixture is never executed, so nothing that runs
  89. # programs on it can fail the check.
  90. # run_exec86.py behaviour (Exec86): catches the interpreter's OWN
  91. # reading of the machine: JE inverted in Exec86's Cond,
  92. # which swaps the two arms of every `=' in every program
  93. # while the emitted bytes, the sizes and every
  94. # byte-level check stay green. The image is fine; only
  95. # the thing reading it is wrong, so nothing that looks at
  96. # the image can fail this one.
  97. # runtest.py the R key: catches CmdRun poking nothing into
  98. # the interpreter at all, which faults on the first step
  99. # and prints "interpreter fault" instead of the guest's
  100. # answer. It is the only case here that needs a rebuilt
  101. # SHELL rather than a rebuilt compiler or runtime.
  102. #
  103. # The mod=11 cases do not need a rebuild -- they read the probe sources
  104. # directly -- so they are cheap, and they are the ones that matter most: the
  105. # table they guard is the one thing in this project that was wrong in the
  106. # documentation while the code was right, and a table that is wrong in the
  107. # code produces bytes that decode perfectly.
  108. #
  109. # Usage: tests/nonvacuity.sh (from shell/; leaves Runtime.mod restored)
  110. set -u
  111. cd "$(dirname "$0")/.." || exit 1
  112. GM2=/home/eric/bin/Modula2/Gm2/bin/gm2
  113. SAVED=../tmp/nonvacuity.Runtime.mod
  114. PROBE=../tmp/nonvacuity.rtprobe
  115. DUMP=../tmp/nonvacuity.dump
  116. # Exec86.mod is UNTRACKED, so unlike Runtime.mod git cannot put a botched
  117. # mutation back: the copy taken here is the only correct one in existence.
  118. # Shell.mod is tracked but is mutated by the R-key case below, and a trap that
  119. # restored only the sources would leave tpshell BUILT FROM the mutation - so
  120. # the trap rebuilds too. All three copies live in the project's own tmp/ and
  121. # are taken here, before the first mutation, rather than beside each case.
  122. #
  123. # comimage.py and comtest.py are the image layout and one of its three readers;
  124. # both are mutated by cases further down, and neither is recoverable from git
  125. # once the mutation is staged over - so they join the trap here for the same
  126. # reason, and an interrupted run cannot leave the layout half-written.
  127. #
  128. # Compiler.mod is the file mutated by the most cases, and it used to be saved
  129. # BESIDE the first case that needed it - the BP displacement section halfway
  130. # down this file, which is well AFTER this trap is set. An interrupted run
  131. # therefore left Compiler.mod half-mutated, with `git status` showing a source
  132. # that no case had finished with. It joins the copies above, taken before the
  133. # first mutation and covered by the same trap.
  134. #
  135. # tests/probe/modrm11.{py,s} are the last two: the mod=11 table section mutates
  136. # them and puts them back after each case, so a run that died between the two
  137. # left them edited. They move here for the same reason. restore_all copies
  138. # them directly instead of calling restore_probe, which is defined halfway down
  139. # this file: a trap firing before that definition would abort the rest of the
  140. # restore with "command not found".
  141. mkdir -p ../tmp
  142. SAVED_E=../tmp/nonvacuity.Exec86.mod
  143. SAVED_SH=../tmp/nonvacuity.Shell.mod
  144. SAVED_CI=../tmp/nonvacuity.comimage.py
  145. SAVED_T=../tmp/nonvacuity.comtest.py
  146. SAVED_C=../tmp/nonvacuity.Compiler.mod
  147. SAVED_PY=../tmp/nonvacuity.modrm11.py
  148. SAVED_S=../tmp/nonvacuity.modrm11.s
  149. cp Runtime.mod "$SAVED" || exit 1
  150. cp Exec86.mod "$SAVED_E" || exit 1
  151. cp Shell.mod "$SAVED_SH" || exit 1
  152. cp tests/comimage.py "$SAVED_CI" || exit 1
  153. cp tests/comtest.py "$SAVED_T" || exit 1
  154. cp Compiler.mod "$SAVED_C" || exit 1
  155. cp tests/probe/modrm11.py "$SAVED_PY" || exit 1
  156. cp tests/probe/modrm11.s "$SAVED_S" || exit 1
  157. restore_all () {
  158. cp "$SAVED" Runtime.mod
  159. cp "$SAVED_E" Exec86.mod
  160. cp "$SAVED_SH" Shell.mod
  161. cp "$SAVED_CI" tests/comimage.py
  162. cp "$SAVED_T" tests/comtest.py
  163. cp "$SAVED_C" Compiler.mod
  164. cp "$SAVED_PY" tests/probe/modrm11.py
  165. cp "$SAVED_S" tests/probe/modrm11.s
  166. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1
  167. "$GM2" -fiso -c Exec86.mod >/dev/null 2>&1
  168. # The shell is rebuilt as well: a test that runs against a binary built
  169. # from a half-restored tree is reporting on the mutation, not on the code.
  170. # make rebuilds Compiler.o out of the Compiler.mod restored just above.
  171. make >/dev/null 2>&1
  172. }
  173. trap restore_all EXIT
  174. #
  175. # An unhandled signal does NOT run an EXIT trap: bash dies on the spot. That is
  176. # what the comment above promises ("an interrupted run cannot leave the layout
  177. # half-written") and what this file did not actually do. A handler that only
  178. # `exit's is enough, because `exit' from a trap DOES fire the EXIT trap -- and
  179. # it has to exit, or the run would go on using a tree it has just put back.
  180. #
  181. # Measured on bash 5.2: TERM and HUP fire these and restore everything, so
  182. # `kill' and a session teardown are covered. INT is trapped too, but in the
  183. # usual case it never reaches this script: a foreground child that dies of
  184. # SIGINT makes bash treat the signal as the CHILD's and continue the script
  185. # instead. So Ctrl-C does not abort this run at all -- it kills the current
  186. # case's child and the next case starts -- and SIGTERM is what actually stops
  187. # it. 129/130/143 = 128+SIGHUP/INT/TERM.
  188. trap 'exit 129' HUP
  189. trap 'exit 130' INT
  190. trap 'exit 143' TERM
  191. pass=0
  192. fail=0
  193. # mutate <file> <sed-expr> -- apply a deliberate breakage and INSIST it landed.
  194. #
  195. # Four cases in this file were already dead when first run, all the same way:
  196. # the helper they name had been renamed or reformatted since the case was
  197. # written, the sed matched nothing, the source was unchanged, and the check
  198. # correctly passed - so the harness reported "NOT NON-VACUOUS" and, worse, a
  199. # reader skimming the output could take "the check still passed" for a passing
  200. # test. A case that cannot fire is worse than no case: it is a claim of
  201. # coverage that was never tested.
  202. #
  203. # So the mutation is verified, not assumed. If the file is byte-identical
  204. # afterwards, that is reported as a FAILURE of the harness, naming the sed, and
  205. # the case is not run - because running it would only produce a meaningless
  206. # green. The message says what to do (fix the sed) rather than what it found.
  207. mutate () {
  208. mf=$1
  209. msed=$2
  210. cp "$mf" ../tmp/nonvacuity.mut.bak
  211. sed -i "$msed" "$mf"
  212. if cmp -s "$mf" ../tmp/nonvacuity.mut.bak; then
  213. echo " BROKEN CASE: the mutation did not change $mf"
  214. echo " sed: $msed"
  215. echo " the named code has probably been renamed or reformatted -"
  216. echo " fix this case, it is asserting nothing"
  217. fail=$((fail + 1))
  218. return 1
  219. fi
  220. return 0
  221. }
  222. # rebuild <label> -- re-emit the runtime and dump it
  223. rebuild () {
  224. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1 || return 1
  225. "$GM2" -fiso -o "$PROBE" tests/RtProbe.mod Runtime.o Posix.o \
  226. >/dev/null 2>&1 || return 1
  227. "$PROBE" > "$DUMP" || return 1
  228. return 0
  229. }
  230. # expect_red <label> <pattern> <checker-cmd...>
  231. # <pattern> is a grep the failure output must match, so a check cannot
  232. # "pass" by failing for some unrelated reason.
  233. expect_red () {
  234. label=$1
  235. want=$2
  236. shift 2
  237. if out=$("$@" 2>&1); then
  238. echo "NOT NON-VACUOUS: $label -- the check still passed"
  239. fail=$((fail + 1))
  240. elif ! printf '%s\n' "$out" | grep -qi "$want"; then
  241. echo "WRONG FAILURE: $label -- went red, but not for the stated reason"
  242. printf '%s\n' "$out" | sed 's/^/ /'
  243. fail=$((fail + 1))
  244. else
  245. echo " ok: $label"
  246. printf '%s\n' "$out" | grep -im1 "$want" | sed 's/^/ /'
  247. pass=$((pass + 1))
  248. fi
  249. }
  250. echo "== each mutation must turn the named check red"
  251. echo
  252. # --- 1. name-versus-decode -------------------------------------------
  253. # MovSiBx was `89 DC`, which is MOV SP,BX. Two bytes either way, decodes
  254. # cleanly, and no structural check can see it.
  255. cp "$SAVED" Runtime.mod
  256. mutate Runtime.mod 's|B (0DEH) END MovSiBx|B (0DCH) END MovSiBx|'
  257. expect_red "audit_helpers catches MovSiBx emitting MOV SP,BX" \
  258. "MovSiBx" python3 tests/audit_helpers.py
  259. # CmpSiBx had the identical mistake, which is how you know a single fix is
  260. # not enough -- the same misreading was written twice.
  261. cp "$SAVED" Runtime.mod
  262. mutate Runtime.mod 's|B (39H) ; B (0DEH) END CmpSiBx|B (39H) ; B (0DCH) END CmpSiBx|'
  263. expect_red "audit_helpers catches CmpSiBx emitting CMP SP,BX" \
  264. "CmpSiBx" python3 tests/audit_helpers.py
  265. # --- 2. golden, and entry goldens ------------------------------------
  266. # MovDlAl was `88 C0` = MOV AL,AL instead of MOV DL,AL. This is the case that
  267. # motivated runtime.golden: the sweep stayed in sync, every branch target
  268. # stayed on a boundary, no entry's first bytes moved, and the size did not
  269. # change. The target helper was MovAlDh when this case was written, which is
  270. # the fourth way a case here can rot - see the note on `mutate` below.
  271. cp "$SAVED" Runtime.mod
  272. mutate Runtime.mod 's|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H)|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C0H)|'
  273. rebuild
  274. expect_red "runtime.golden catches MOV AL,AL" \
  275. "mov al,al" python3 tests/check_runtime.py "$DUMP"
  276. # initmem opened with the mis-emitted MovSiAx, so its entry golden was the
  277. # thing that noticed the prologue was a no-op.
  278. cp "$SAVED" Runtime.mod
  279. mutate Runtime.mod 's|B (0F0H) END MovSiAx|B (0C0H) END MovSiAx|'
  280. rebuild
  281. expect_red "check_runtime catches a broken initmem prologue" \
  282. "mov ax,ax" python3 tests/check_runtime.py "$DUMP"
  283. # --- 3. decode sweep / length ----------------------------------------
  284. # StDiDl was `88 97` = [BX+disp16],DL: mod=10, so the instruction needs a
  285. # disp16 it was not given, and the sweep loses sync two bytes later.
  286. cp "$SAVED" Runtime.mod
  287. mutate Runtime.mod 's|PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H)|PROCEDURE StDiDl ; BEGIN B (88H) ; B (97H)|'
  288. rebuild
  289. expect_red "decode sweep catches a mod=10 byte move with no displacement" \
  290. "mov byte ptr \[bx+5b5fh\],dl" python3 tests/check_runtime.py "$DUMP"
  291. # --- 4. branch targets ------------------------------------------------
  292. # FixUp measures a rel8 from the end of the instruction, one byte past the
  293. # displacement field. Drop the +1 and every short branch lands one byte into
  294. # its target, which for a 3-byte instruction means the middle of it. The
  295. # bytes themselves are all perfectly well formed -- only the fixups are
  296. # wrong -- so this is the one failure mode the golden cannot be expected to
  297. # catch on its own.
  298. cp "$SAVED" Runtime.mod
  299. mutate Runtime.mod 's|rel := (t + 100H - (fix \[i\].place + 1)) MOD 100H|rel := (t + 100H - fix [i].place) MOD 100H|'
  300. rebuild
  301. expect_red "branch check catches rel8 fixups measured from the wrong byte" \
  302. "not an instruction boundary" \
  303. python3 tests/check_runtime.py "$DUMP"
  304. echo
  305. echo "== everything restored and green again"
  306. cp "$SAVED" Runtime.mod
  307. if rebuild; then
  308. if python3 tests/audit_helpers.py >/dev/null 2>&1 &&
  309. python3 tests/check_runtime.py "$DUMP" >/dev/null 2>&1; then
  310. echo " ok: both checks pass on the restored source"
  311. pass=$((pass + 1))
  312. else
  313. echo "NOT RESTORED: a check is red after restoring Runtime.mod"
  314. fail=$((fail + 1))
  315. fi
  316. else
  317. echo "NOT RESTORED: the runtime would not rebuild"
  318. fail=$((fail + 1))
  319. fi
  320. echo
  321. echo "== the mod=11 table (probe/modrm11.py)"
  322. # These mutate the probe's own sources, not the runtime, so there is no
  323. # rebuild in the loop. SAVED_PY / SAVED_S are taken at the top of this file,
  324. # beside the EXIT trap, so an interrupted run restores them too; each case
  325. # still puts them back here so the next one starts from the real table.
  326. M11="python3 tests/probe/modrm11.py"
  327. restore_probe () {
  328. cp "$SAVED_PY" tests/probe/modrm11.py
  329. cp "$SAVED_S" tests/probe/modrm11.s
  330. }
  331. # 1. one cell of the table moved
  332. mutate tests/probe/modrm11.py 's|"Si", "Di"\]$|"Bp", "Di"]|'
  333. expect_red "anchor pins a moved table cell" \
  334. "anchor ADD SI, 2" $M11
  335. restore_probe
  336. # 2. the table this project actually shipped: AX dropped off the front and a
  337. # duplicate BX invented at the end, which shifts every code down by one
  338. mutate tests/probe/modrm11.py 's|^REG = .*$|REG = ["Cx", "Dx", "Bx", "Sp", "Bp", "Si", "Di", "Bx"]|'
  339. expect_red "the table shifted by one (AX dropped, BX duplicated)" \
  340. "anchor MOV SP, BP" $M11
  341. restore_probe
  342. # 3. the .s edited to contradict the table. This is the case that shows why
  343. # the hard-coded EXPECT bytes exist: the assembler encodes the new claim
  344. # correctly, so comparing the .s against `as` alone can never fail here.
  345. mutate tests/probe/modrm11.s 's|movw %sp, %di # reg 100|movw %bp, %di # reg 100|'
  346. expect_red "probe source edited away from the recorded bytes" \
  347. "expected 89 E7" $M11
  348. restore_probe
  349. # 4. the 8-bit list edited, which is a different table from the word one
  350. mutate tests/probe/modrm11.s 's|movb %al, %dl # 88 C2 -> DL := AL|movb %al, %bl # was DL|'
  351. expect_red "the 8-bit register list edited" \
  352. "expected 88 C2" $M11
  353. restore_probe
  354. # 5. an anchor's recorded byte corrupted, so the anchor can no longer
  355. # corroborate itself
  356. mutate tests/probe/modrm11.py 's|"8B EC", "8B E5"|"8B ED", "8B E5"|'
  357. expect_red "anchor byte no longer matches the emitted code" \
  358. "expected 8B ED" $M11
  359. restore_probe
  360. if $M11 >/dev/null 2>&1; then
  361. echo " ok: modrm11.py passes on the restored probe sources"
  362. pass=$((pass + 1))
  363. else
  364. echo "NOT RESTORED: modrm11.py is red after restoring its sources"
  365. $M11 2>&1 | sed 's/^/ /'
  366. fail=$((fail + 1))
  367. fi
  368. echo
  369. echo "== the BP displacement rule (check_framedisp.py)"
  370. # This one is about Compiler.mod rather than the runtime, and it needs the
  371. # whole toolchain rebuilt (comtest, not rtprobe), so it gets its own rebuild.
  372. # SAVED_C itself is taken at the top of this file, beside the EXIT trap, so an
  373. # interrupted run restores Compiler.mod as well.
  374. rebuild_compiler () {
  375. $GM2 -fiso -c Compiler.mod >/dev/null 2>&1 || return 1
  376. $GM2 -fiso -fgen-module-list=tests/ct.lst -o /dev/null \
  377. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  378. >/dev/null 2>&1
  379. $GM2 -fiso -fuse-list=tests/ct.lst -o comtest \
  380. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  381. >/dev/null 2>&1 || return 1
  382. return 0
  383. }
  384. # 1. the original bug: `off MOD 100H`, always disp8. Restores the code that
  385. # was there before EmBpDisp existed. t28's [BP+128] read becomes [BP-128],
  386. # which is the failure this whole check is named after.
  387. #
  388. # regf is KEPT. It is the other half of EmBpDisp's job - the ModR/M reg
  389. # field, which the DI forms (regf 7) need - and it was added after this
  390. # case was written. Hard-coding 46H here again would break the DI forms at
  391. # the same time and turn one mutation into two faults; the check would
  392. # still go red, but for a reason the label does not name.
  393. python3 - "$SAVED_C" <<'PYEOF'
  394. import sys
  395. p = 'Compiler.mod'
  396. s = open(p).read()
  397. old = """ IF off <= 127 THEN
  398. Ebyte (VAL (BYTE, 40H + regf * 8 + 6H)) ; Ebyte (VAL (BYTE, off))
  399. ELSE
  400. Ebyte (VAL (BYTE, 80H + regf * 8 + 6H)) ; Eword (off)
  401. END
  402. END EmBpDisp ;"""
  403. new = """ Ebyte (VAL (BYTE, 40H + regf * 8 + 6H)) ; Ebyte (VAL (BYTE, off MOD 100H))
  404. END EmBpDisp ;"""
  405. assert old in s, "EmBpDisp body not found -- update this mutation"
  406. open(p, 'w').write(s.replace(old, new))
  407. PYEOF
  408. if rebuild_compiler; then
  409. expect_red "displacement truncation reads a different address" \
  410. "no 8B access at \[BP+128\]" python3 tests/check_framedisp.py
  411. else
  412. echo " FAIL: the compiler would not rebuild with the truncation"
  413. fail=$((fail + 1))
  414. fi
  415. cp "$SAVED_C" Compiler.mod
  416. # 2. the other half of the rule: always use the 4-byte form, ignoring the
  417. # <= 127 case. This is over-cautious rather than wrong, so the checker must
  418. # still be happy -- which is worth asserting, because a check that only
  419. # ever fails on a smaller encoding is a check that pins one answer instead
  420. # of the rule.
  421. python3 - <<'PYEOF'
  422. p = 'Compiler.mod'
  423. s = open(p).read()
  424. old = """ IF off <= 127 THEN
  425. Ebyte (VAL (BYTE, 40H + regf * 8 + 6H)) ; Ebyte (VAL (BYTE, off))
  426. ELSE
  427. Ebyte (VAL (BYTE, 80H + regf * 8 + 6H)) ; Eword (off)
  428. END"""
  429. new = """ Ebyte (VAL (BYTE, 80H + regf * 8 + 6H)) ; Eword (off)"""
  430. assert old in s, "EmBpDisp branch not found -- update this mutation"
  431. open(p, 'w').write(s.replace(old, new))
  432. PYEOF
  433. if rebuild_compiler; then
  434. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  435. echo " ok: always-disp16 is accepted, so the check pins the rule"
  436. echo " and not one particular encoding"
  437. pass=$((pass + 1))
  438. else
  439. echo " FAIL: check_framedisp rejects a safe, over-long encoding"
  440. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  441. fail=$((fail + 1))
  442. fi
  443. else
  444. echo " FAIL: the compiler would not rebuild with always-disp16"
  445. fail=$((fail + 1))
  446. fi
  447. cp "$SAVED_C" Compiler.mod
  448. if rebuild_compiler; then
  449. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  450. echo " ok: check_framedisp passes on the restored source"
  451. pass=$((pass + 1))
  452. else
  453. echo "NOT RESTORED: check_framedisp is red after restoring Compiler.mod"
  454. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  455. fail=$((fail + 1))
  456. fi
  457. else
  458. echo "NOT RESTORED: the compiler would not rebuild"
  459. fail=$((fail + 1))
  460. fi
  461. # --- the region check_framedisp sweeps, and the one file it comes from -----
  462. echo
  463. echo "== the image layout, measured once (tests/comimage.py)"
  464. # check_framedisp.py decodes instructions over a REGION of the image, and a
  465. # wrong region does not stop it: the byte patterns it searches for are in the
  466. # image wherever they happen to be, and a decode that starts mid-instruction
  467. # reached the same offsets anyway. That is how RT_SZ = 391 - a literal that had
  468. # drifted from the runtime it described - went on reporting PASS while its sweep
  469. # began inside the runtime, part way through an instruction. Nothing in the
  470. # check could see where its own region began.
  471. #
  472. # So the region is two independent readings of the file - where the entry jump
  473. # says execution starts, and where the program header says the runtime ends -
  474. # and the two must AGREE or the region is undetermined. Both readings, and the
  475. # layout itself, live in tests/comimage.py, which is also where comtest.py and
  476. # the independent checker in run_com_tests.sh now get the copy each of them
  477. # used to keep: a duplicated constant that has silently drifted is not an
  478. # independent check, it is a second source of truth that lies, and it lies in
  479. # the direction of looking like the code under test is broken.
  480. #
  481. # None of this needs a rebuild - comimage.py is Python the checkers import -
  482. # so these cases are cheap, and they turn the READERS red one at a time.
  483. # 1. entry_target answers with the literal the region used to be built from.
  484. # The two readings now disagree, and there is no measured region to sweep.
  485. python3 - <<'PYEOF'
  486. p = 'tests/comimage.py'
  487. s = open(p).read()
  488. old = ' return ENT_SZ + int.from_bytes(d[1:ENT_SZ], "little", signed=True)'
  489. new = ' return ENT_SZ + 391 # the literal RT_SZ was'
  490. assert s.count(old) == 1, "entry_target's return not found -- update this mutation"
  491. open(p, 'w').write(s.replace(old, new))
  492. PYEOF
  493. expect_red "a sweep region that is not where execution starts is rejected" \
  494. "neither region is swept" python3 tests/check_framedisp.py
  495. cp "$SAVED_CI" tests/comimage.py
  496. # 2 and 3. The header's own equation, one byte out, so the header cannot be
  497. # located at all - the case where a checker that fell back on a remembered
  498. # offset would report a confident number instead. Two readers, two
  499. # different sentences: each must say in its own words that it does not know
  500. # where the program's code begins.
  501. python3 - <<'PYEOF'
  502. p = 'tests/comimage.py'
  503. s = open(p).read()
  504. old = ' if w(HDR_DS_WORD) != off + 0x1000 + LOAD_BIAS: # hdrDS'
  505. new = ' if w(HDR_DS_WORD) != off + 0x1000 + LOAD_BIAS + 1: # hdrDS'
  506. assert s.count(old) == 1, "the hdrDS equation not found -- update this mutation"
  507. open(p, 'w').write(s.replace(old, new))
  508. PYEOF
  509. expect_red "check_framedisp reports a header it cannot locate" \
  510. "no program header found" python3 tests/check_framedisp.py
  511. expect_red "check_8086 reports the same unlocatable header" \
  512. "could not locate the code region" python3 tests/check_8086.py
  513. cp "$SAVED_CI" tests/comimage.py
  514. # 4-7. The single copy itself, now that there is one. A second find_header,
  515. # a second HDR_SZ, a caller that reaches the helper through another
  516. # consumer instead of through comimage, and - the case that exists
  517. # because of rt_exec.py's "matched nothing" lesson - the definition the
  518. # audit counts. A rule that matches nothing looks exactly like a rule
  519. # that passes, so a deleted definition must be a red report rather than a
  520. # silent green one.
  521. python3 - <<'PYEOF'
  522. p = 'tests/comtest.py'
  523. s = open(p).read()
  524. old = 'def check_com(path, expected_src_len):'
  525. new = ('def find_header(d):\n'
  526. ' return None # a second copy of the helper\n'
  527. '\n\n'
  528. 'def check_com(path, expected_src_len):')
  529. assert s.count(old) == 1, "check_com not found -- update this mutation"
  530. open(p, 'w').write(s.replace(old, new))
  531. PYEOF
  532. expect_red "the audit reports a find_header copied out of comimage" \
  533. "comtest.py defines its own find_header" python3 tests/check_comimage.py
  534. cp "$SAVED_T" tests/comtest.py
  535. python3 - <<'PYEOF'
  536. p = 'tests/comtest.py'
  537. s = open(p).read()
  538. old = 'from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,'
  539. new = ('HDR_SZ = 16 # a second copy of the layout\n'
  540. 'from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,')
  541. assert s.count(old) == 1, "the comimage import not found -- update this mutation"
  542. open(p, 'w').write(s.replace(old, new))
  543. PYEOF
  544. expect_red "the audit reports a layout constant defined elsewhere" \
  545. "comtest.py defines its own HDR_SZ" python3 tests/check_comimage.py
  546. cp "$SAVED_T" tests/comtest.py
  547. python3 - <<'PYEOF'
  548. p = 'tests/comimage.py'
  549. s = open(p).read()
  550. old = 'def find_header(d):'
  551. new = 'def _find_header(d): # the definition is gone'
  552. assert s.count(old) == 1, "def find_header not found -- update this mutation"
  553. open(p, 'w').write(s.replace(old, new))
  554. PYEOF
  555. expect_red "the audit counts its own subject rather than matching nothing" \
  556. "defines find_header 0 times" python3 tests/check_comimage.py
  557. cp "$SAVED_CI" tests/comimage.py
  558. # 7. The import dropped while the CALLS stay - the shape a refactor makes when
  559. # one reader starts taking the helper from another reader instead. It would
  560. # NameError the moment comtest ran, but the audit says so first, and in the
  561. # vocabulary of the layout rather than in the vocabulary of Python: the
  562. # failure this is guarding is "two sources of truth", not "undefined name".
  563. python3 - <<'PYEOF'
  564. p = 'tests/comtest.py'
  565. s = open(p).read()
  566. old = ('from comimage import (ENT_SZ, HDR_SZ, LOAD_BIAS, HEAD, HDR_DS_WORD,\n'
  567. ' HDR_HEAP_WORD, find_header)')
  568. new = '# MUTATION: the shared import is gone, but the calls remain\n'
  569. assert s.count(old) == 1, "the comimage import not found -- update this mutation"
  570. open(p, 'w').write(s.replace(old, new))
  571. PYEOF
  572. expect_red "the audit reports a caller that does not take it from comimage" \
  573. "comtest.py calls find_header without importing it from comimage" \
  574. python3 tests/check_comimage.py
  575. cp "$SAVED_T" tests/comtest.py
  576. # Green again, on all three readers at once: the case above mutated the one
  577. # file all of them import, so restoring it has to satisfy each of them, not
  578. # just the one that was last run.
  579. for c in tests/check_framedisp.py tests/check_comimage.py tests/check_8086.py; do
  580. if python3 "$c" >/dev/null 2>&1; then
  581. echo " ok: $c passes on the restored sources"
  582. pass=$((pass + 1))
  583. else
  584. echo "NOT RESTORED: $c is red after restoring comimage.py and comtest.py"
  585. python3 "$c" 2>&1 | tail -3 | sed 's/^/ /'
  586. fail=$((fail + 1))
  587. fi
  588. done
  589. # --- 3. the operator bugs the nine dead fixtures were hiding ------------
  590. echo
  591. echo "== the bugs the never-executed fixtures were hiding"
  592. echo
  593. # A different KIND of case from everything above. The others break the code
  594. # and assert a byte-level check notices; these break the code and assert a
  595. # BEHAVIOURAL check notices, which is the only kind that could have found them.
  596. # Each of the original four shipped with a green compile matrix, a passing .COM
  597. # layout check, a passing golden and a passing emitter audit:
  598. #
  599. # OpMul = 1 `a * b` emitted ADD AX,CX. `*' and `+' both numbered
  600. # their operator 1, and BinOpEmit cannot see which
  601. # precedence level called it, so every multiplication
  602. # dispatched to the addition. The constant-folding arm was
  603. # correct, which is why `n * n' with n a CONST was right and
  604. # `a * a' with a a variable was not -- and t08_const is the
  605. # only fixture that ever multiplied.
  606. # 9Dh / 9FH `>' got SETGE and `>=' got SETG: swapped, one letter
  607. # apart in the mnemonic. Only a==b could see it.
  608. # JNZ -> body REPEAT..UNTIL looped back while the condition was TRUE,
  609. # which is WHILE, so the body ran once and stopped.
  610. #
  611. # They are mutated back to the original defect and run_com_exec.py must go red
  612. # on the exact fixture that pins the behaviour. The fourth (HideLocals) is a
  613. # scoping bug rather than an operator bug; it was hiding in the same place.
  614. #
  615. # Two joined them when the operand-lifetime and `not' fixes landed, each run
  616. # red by hand before it was written down here, and each with the same property
  617. # as the four above -- green everywhere except execution:
  618. #
  619. # SaveLeft's park `(p > q) or (q > p)' evaluated `(q > p) or (q > p)'. A
  620. # kind-2 value exists only in AX, and the right operand's
  621. # code is emitted before the two are ever brought together,
  622. # so without the push the left one is simply gone.
  623. # neglevel's split `not' on a boolean emitted the INTEGER `not', which left
  624. # 0FFFEh where a boolean belongs, and wrbool reads anything
  625. # non-zero as TRUE -- so `not (a = a)' answered TRUE and so
  626. # did every other `not'.
  627. mutate_compiler () { # reuse mutate's verified-change discipline on Compiler.mod
  628. mf=Compiler.mod
  629. msed=$1
  630. cp "$SAVED_C" ../tmp/nonvacuity.mut2.bak
  631. sed -i "$msed" "$mf"
  632. if cmp -s "$mf" ../tmp/nonvacuity.mut2.bak; then
  633. echo " BROKEN CASE: the mutation did not change Compiler.mod"
  634. echo " sed: $msed"
  635. echo " the named code has probably been renamed or reformatted -"
  636. echo " fix this case, it is asserting nothing"
  637. fail=$((fail + 1))
  638. return 1
  639. fi
  640. return 0
  641. }
  642. # The SETcc swap and the HideLocals removal are done in python rather than
  643. # with sed: both need to match source text containing `*` and `(` in a way that
  644. # is tedious and fragile as a regex, and a case whose only failure mode is a
  645. # malformed sed is a case that silently asserts nothing.
  646. #
  647. # And a python helper fails in a way sed does not: a syntax error in the helper
  648. # is a non-zero exit, `if mutate_foo; then` is simply false, and the case is
  649. # SKIPPED -- with no failure counted and nothing on stdout but whatever python
  650. # printed. That is how the SETcc case spent its first run: an apostrophe in an
  651. # assert message ("the `>' arm") closed the string early, python died, the
  652. # compiler was never broken, and the suite still reported 0 failed. A skipped
  653. # case and a passing case look the same in the total. So each helper below
  654. # fails LOUDLY: a non-zero exit from python is reported as a BROKEN CASE and
  655. # counted, never swallowed.
  656. #
  657. # Each one also counts its targets before replacing. `assert s != before' only
  658. # says the file changed; with two edits it would pass if just one of them
  659. # landed, and with two identical HideLocals call sites it would happily delete
  660. # the wrong one -- still a changed file, still a working compiler, still green
  661. # for the wrong reason.
  662. mutate_cc_swap () {
  663. if python3 - <<'PYX'
  664. p = 'Compiler.mod'
  665. s = open(p).read()
  666. GT = 'EmSetcc (9FH) ; (* > SETG *)' # the greater-than arm
  667. GE = 'EmSetcc (9DH) ; (* >= SETGE *)' # the greater-equal arm
  668. assert s.count(GT) == 1, 'expected 1 greater-than arm, found %d' % s.count(GT)
  669. assert s.count(GE) == 1, 'expected 1 greater-equal arm, found %d' % s.count(GE)
  670. s = s.replace(GT, GT.replace('9FH', '9DH'))
  671. s = s.replace(GE, GE.replace('9DH', '9FH'))
  672. open(p, 'w').write(s)
  673. PYX
  674. then
  675. return 0
  676. fi
  677. echo " BROKEN CASE: the SETcc swap did not apply"
  678. echo " the two EmSetcc arms are probably renamed or reformatted -"
  679. echo " fix this case, it is asserting nothing"
  680. fail=$((fail + 1))
  681. return 1
  682. }
  683. mutate_no_hidelocals () {
  684. if python3 - <<'PYX'
  685. p = 'Compiler.mod'
  686. s = open(p).read()
  687. # Two HideLocals calls exist. Only the body-exit one may go: deleting the
  688. # FORWARD one instead would still change the file, still rebuild, and still
  689. # leave t13_proc compiling, so the case would go green for the wrong reason.
  690. HL = ' HideLocals (nestMark) ; (* parameters and locals stop here *)\n'
  691. assert s.count(HL) == 1, 'expected 1 body-exit HideLocals, found %d' % s.count(HL)
  692. open(p, 'w').write(s.replace(HL, ''))
  693. PYX
  694. then
  695. return 0
  696. fi
  697. echo " BROKEN CASE: HideLocals was not removed"
  698. echo " the call or its comment has probably been reformatted -"
  699. echo " fix this case, it is asserting nothing"
  700. fail=$((fail + 1))
  701. return 1
  702. }
  703. cp "$SAVED_C" Compiler.mod
  704. if mutate_compiler 's|^ op := OpMul ; DropCh| op := OpAdd ; DropCh|'; then
  705. if rebuild_compiler; then
  706. expect_red "execution catches '*' emitting an ADD (t08_const, n*n)" \
  707. "t08_const" python3 tests/run_com_exec.py t08_const
  708. # t08 only ever multiplied two CONSTANTS, which is the one path that was
  709. # never wrong, because BinOpEmit folds it. So the case above is close
  710. # to vacuous: it proves the mutation changed the binary, not that the
  711. # emitted multiply is covered. The check that matters needs a
  712. # VARIABLE operand, and no shipped fixture has one -- which is why the
  713. # bug survived at all. So this writes a throwaway fixture that
  714. # multiplies a variable, runs it, and asserts the multiply is right.
  715. # The fixture is deleted afterwards; it is here to close the coverage
  716. # hole, not to become a permanent test (that is what a real fixture
  717. # with a `*' in it would be for).
  718. cat > tests/fixtures/zzmul.pas <<'ZZEOF'
  719. program zzmul;
  720. var a : integer ;
  721. begin
  722. a := 7 ;
  723. writeln (a * 6)
  724. end.
  725. ZZEOF
  726. printf '42\r\n' > tests/fixtures/zzmul.out
  727. expect_red "execution catches '*' on a VARIABLE (the unfolded path)" \
  728. "zzmul" python3 tests/run_com_exec.py zzmul
  729. rm -f tests/fixtures/zzmul.pas tests/fixtures/zzmul.out
  730. else
  731. echo " FAIL: the compiler would not rebuild with OpAdd for '*'"
  732. fail=$((fail + 1))
  733. fi
  734. fi
  735. cp "$SAVED_C" Compiler.mod
  736. cp "$SAVED_C" Compiler.mod
  737. if mutate_cc_swap; then
  738. if rebuild_compiler; then
  739. expect_red "execution catches '>' and '>=' swapped (t09_if)" \
  740. "t09_if" python3 tests/run_com_exec.py t09_if
  741. else
  742. echo " FAIL: the compiler would not rebuild with the SETcc swap"
  743. fail=$((fail + 1))
  744. fi
  745. fi
  746. cp "$SAVED_C" Compiler.mod
  747. cp "$SAVED_C" Compiler.mod
  748. if mutate_compiler 's| DropC (EmJcc (84H, L1)) ; (\* JZ -> body again \*)| zj := EmJcc (85H, L1) ;|'; then
  749. if rebuild_compiler; then
  750. expect_red "execution catches REPEAT..UNTIL exiting after one pass (t12)" \
  751. "t12_repeat" python3 tests/run_com_exec.py t12_repeat
  752. else
  753. echo " FAIL: the compiler would not rebuild with the JNZ repeat"
  754. fail=$((fail + 1))
  755. fi
  756. fi
  757. cp "$SAVED_C" Compiler.mod
  758. # The fourth: sibling procedures shared one parameter namespace, because a
  759. # finished procedure's symbols were left at a level Search still accepts.
  760. # Removing HideLocals puts two procedures' `a : integer' back in collision.
  761. cp "$SAVED_C" Compiler.mod
  762. if mutate_no_hidelocals; then
  763. if rebuild_compiler; then
  764. expect_red "a duplicate parameter in two procedures is a compile error again" \
  765. "ERROR 41" python3 tests/run_com_exec.py t13_proc
  766. else
  767. echo " FAIL: the compiler would not rebuild without HideLocals"
  768. fail=$((fail + 1))
  769. fi
  770. else
  771. echo " FAIL: could not remove HideLocals to test the scoping fix"
  772. fail=$((fail + 1))
  773. fi
  774. # M6: the LEFT operand, parked for the right one. kind 2 means "this value
  775. # exists in AX and nowhere else", and the right-hand operand's code is emitted
  776. # between recognizing the operator and using both operands - so without
  777. # SaveLeft's push the left value is overwritten before it is ever read, and
  778. # LoadPair's kind-4 shape can never trigger. `(p > q) or (q > p)' then
  779. # evaluates `(q > p) or (q > p)'. Every byte, every size and the whole compile
  780. # matrix stay green: nothing is malformed, the value is simply the wrong one.
  781. cp "$SAVED_C" Compiler.mod
  782. if python3 - <<'PYX'
  783. p = 'Compiler.mod'
  784. s = open(p).read()
  785. # Both guards are matched, and each must be unique. `IF left.kind = 2 THEN'
  786. # reads like it can only be SaveLeft's, but LoadAtom and friends also branch
  787. # on kind, so an unasserted replace could disable one of THEM instead and the
  788. # case would still go red - for a reason the label does not name.
  789. #
  790. # The kind-5 ("a string element whose address is in DI") guard did not exist
  791. # when this case was written; it arrived with `s[i]`. Making only the kind-2
  792. # park unreachable would leave that one alive, so the mutation would no longer
  793. # be "SaveLeft parks nothing" - it would be a half-disabled SaveLeft, and the
  794. # case would be testing something narrower than it says.
  795. g2 = ' IF left.kind = 2 THEN\n'
  796. g5 = ' ELSIF left.kind = 5 THEN\n'
  797. assert s.count(g2) == 1, 'the kind-2 guard found %d times -- update this mutation' % s.count(g2)
  798. assert s.count(g5) == 1, 'the kind-5 guard found %d times -- update this mutation' % s.count(g5)
  799. s = s.replace(g2, ' IF left.kind = 99 THEN\n')
  800. s = s.replace(g5, ' ELSIF left.kind = 99 THEN\n')
  801. open(p, 'w').write(s)
  802. PYX
  803. then
  804. if rebuild_compiler; then
  805. expect_red "execution catches a left operand clobbered by the right one" \
  806. "t34_arith" python3 tests/run_com_exec.py t34_arith
  807. else
  808. echo " FAIL: the compiler would not rebuild without SaveLeft's push"
  809. fail=$((fail + 1))
  810. fi
  811. else
  812. echo " BROKEN CASE: the SaveLeft mutation did not apply"
  813. fail=$((fail + 1))
  814. fi
  815. cp "$SAVED_C" Compiler.mod
  816. # M7: the type split in TPSRC9's neglevel, wrong half. A boolean NOT lowered
  817. # as the INTEGER one leaves 0FFFEh/0FFFFh, and wrbool tests [BP+4] <> 0 - so
  818. # `not (a = a)' answers TRUE, and so does every other `not'. One instruction,
  819. # same length, same sizes, and the compile matrix cannot see it because both
  820. # halves emit well-formed code for a type the parser already accepted.
  821. cp "$SAVED_C" Compiler.mod
  822. if python3 - <<'PYX'
  823. p = 'Compiler.mod'
  824. s = open(p).read()
  825. old = """ IF r.cls = TBool THEN
  826. LoadAtom (r) ;
  827. EmXorAl01 () ;"""
  828. new = """ IF r.cls = TBool THEN
  829. LoadAtom (r) ;
  830. EmNotAx () ; (* MUTATION: integer NOT on a boolean *)"""
  831. assert s.count(old) == 1, 'the TBool arm of ParseNeg found %d times -- update this mutation' % s.count(old)
  832. open(p, 'w').write(s.replace(old, new))
  833. PYX
  834. then
  835. if rebuild_compiler; then
  836. expect_red "execution catches a boolean NOT lowered as an integer one" \
  837. "t35_not" python3 tests/run_com_exec.py t35_not
  838. else
  839. echo " FAIL: the compiler would not rebuild with EmNotAx for a boolean"
  840. fail=$((fail + 1))
  841. fi
  842. else
  843. echo " BROKEN CASE: the neglevel type-split mutation did not apply"
  844. fail=$((fail + 1))
  845. fi
  846. cp "$SAVED_C" Compiler.mod
  847. # Bug 36, argument order: the fix has FOUR independent halves - three parsers
  848. # that push each argument the moment it is parsed instead of after the whole
  849. # list, and the callee that reads its parameters backwards from BP+4 - and
  850. # t36_argclobber is the one fixture that tells them apart. Before these four
  851. # cases existed, its only proof was the pre-fix run quoted in the commit
  852. # message, which is a fact about the past rather than a check: it cannot be
  853. # re-derived, and nothing stops the four halves from drifting apart.
  854. #
  855. # Each revert below was run to a red BEFORE it was written here, and the four
  856. # fail on four DIFFERENT rows, which is what makes them four cases and not one
  857. # wearing four hats: the callee revert turns `5 3 7' into `7 3 5' (every
  858. # argument lands on its neighbour's parameter), the deferred ParseCallArgs
  859. # zeroes the rows built from computed arguments, ParseCall changes the
  860. # function call's result (19 -> 10) and IoCall changes the last write
  861. # (`FALSE 1' -> `TRUE 544', the first argument printed as the second
  862. # argument's value). A revert that left t36 green is reported by expect_red
  863. # as NOT NON-VACUOUS; a revert that did not land is a BROKEN CASE.
  864. # revert_procedure <name> -- put <name> back the way it was before bug 36 was
  865. # fixed, spliced out of the tree the fix landed on top of. The old code's
  866. # source of truth is the old code: quoting a copy of it in this file is a copy
  867. # that can drift from the tag it claims to reproduce, and a revert that
  868. # reproduces nothing is a case asserting nothing.
  869. revert_procedure () {
  870. python3 - "$1" <<'PYX'
  871. import subprocess, sys
  872. name = sys.argv[1]
  873. cur = open('Compiler.mod').read()
  874. r = subprocess.run(['git', 'show', 'v-TP3-IMAGELAYOUT:shell/Compiler.mod'],
  875. capture_output=True)
  876. assert r.returncode == 0, 'cannot read the pre-fix tree: %s' % r.stderr.decode()[:200]
  877. pre = r.stdout.decode()
  878. def span(text, where):
  879. # The DEFINITION, not the FORWARD declaration a few screens earlier: the
  880. # anchor runs to the end of the line, and a FORWARD line ends `FORWARD ;'
  881. # instead of nothing.
  882. a, b = 'PROCEDURE %s (idx : CARDINAL) ;\n' % name, 'END %s ;' % name
  883. na, nb = text.count(a), text.count(b)
  884. assert na == 1 and nb == 1, \
  885. '%s: %d definitions, %d ends in %s -- update this case' % (name, na, nb, where)
  886. i = text.index(a)
  887. j = text.index(b, i) + len(b)
  888. return text[i:j], i, j
  889. new, i, j = span(cur, 'Compiler.mod')
  890. old, _, _ = span(pre, 'the pre-fix tree')
  891. assert old != new, 'the pre-fix %s is identical to the current one' % name
  892. open('Compiler.mod', 'w').write(cur[:i] + old + cur[j:])
  893. PYX
  894. }
  895. # The callee half: no remap, so parameters are read in declaration order from
  896. # BP+4 while the arguments are pushed in parse order - the FIRST argument ends
  897. # up in the LAST parameter's slot, and one-argument calls (all of
  898. # Runtime.mod's own cases) cannot tell the two layouts apart.
  899. cp "$SAVED_C" Compiler.mod
  900. if python3 - <<'PYX'
  901. p = 'Compiler.mod'
  902. s = open(p).read()
  903. old = """ i := nestMark ;
  904. WHILE i < symTop DO
  905. symtab [i].off := 8 + (parmOff - 4) - symtab [i].off
  906. - SlotSize (symtab [i].cls, symtab [i].size) ;
  907. INC (i)
  908. END ;
  909. """
  910. assert s.count(old) == 1, 'the ProcFunc parameter remap found %d times -- update this mutation' % s.count(old)
  911. open(p, 'w').write(s.replace(old, ''))
  912. PYX
  913. then
  914. if rebuild_compiler; then
  915. expect_red "t36 catches the callee laid out in declaration order (no remap)" \
  916. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  917. else
  918. echo " FAIL: the compiler would not rebuild without the parameter remap"
  919. fail=$((fail + 1))
  920. fi
  921. else
  922. echo " BROKEN CASE: the ProcFunc remap mutation did not apply"
  923. fail=$((fail + 1))
  924. fi
  925. cp "$SAVED_C" Compiler.mod
  926. # The caller half, site 1 of 3: a call used as a statement. The old code
  927. # read the whole list into args[] and pushed it backwards afterwards, so a
  928. # computed argument (which exists only in AX) was gone before the push loop
  929. # reached it.
  930. cp "$SAVED_C" Compiler.mod
  931. if revert_procedure ParseCallArgs; then
  932. if rebuild_compiler; then
  933. expect_red "t36 catches ParseCallArgs pushing after the whole list" \
  934. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  935. else
  936. echo " FAIL: the compiler would not rebuild with ParseCallArgs deferred"
  937. fail=$((fail + 1))
  938. fi
  939. else
  940. echo " BROKEN CASE: the ParseCallArgs revert did not apply"
  941. fail=$((fail + 1))
  942. fi
  943. cp "$SAVED_C" Compiler.mod
  944. # Site 2 of 3: the same call reached from ParseAtom, when a FUNCTION is
  945. # called inside an expression. A separate site, because it is a separate
  946. # procedure - and t36's `19' row is the only thing that notices this one.
  947. cp "$SAVED_C" Compiler.mod
  948. if revert_procedure ParseCall; then
  949. if rebuild_compiler; then
  950. expect_red "t36 catches ParseCall pushing after the whole list" \
  951. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  952. else
  953. echo " FAIL: the compiler would not rebuild with ParseCall deferred"
  954. fail=$((fail + 1))
  955. fi
  956. else
  957. echo " BROKEN CASE: the ParseCall revert did not apply"
  958. fail=$((fail + 1))
  959. fi
  960. cp "$SAVED_C" Compiler.mod
  961. # Site 3 of 3: write/writeln/read/readln, where the argument list is also
  962. # where an inline string literal gets its call. Reverting this procedure
  963. # also reverts the per-argument dispatch inside it (the BYTE read landed
  964. # here), which is harmless for t36 and is why the splice is the whole
  965. # procedure rather than the push alone: half a revert is a state the tree
  966. # was never in.
  967. cp "$SAVED_C" Compiler.mod
  968. if revert_procedure IoCall; then
  969. if rebuild_compiler; then
  970. expect_red "t36 catches IoCall printing the last argument's value" \
  971. "t36_argclobber" python3 tests/run_com_exec.py t36_argclobber
  972. else
  973. echo " FAIL: the compiler would not rebuild with IoCall's loops split"
  974. fail=$((fail + 1))
  975. fi
  976. else
  977. echo " BROKEN CASE: the IoCall revert did not apply"
  978. fail=$((fail + 1))
  979. fi
  980. cp "$SAVED_C" Compiler.mod
  981. # The boolean read (t38): TP3 refuses `readln (b)' with error 66 - prderrtp
  982. # rejects the whole class and the runtime has no xrdbool - and this compiler
  983. # used to accept it and dispatch to rdbool, which stores a WORD into the
  984. # one-byte variable, its second byte landing on the variable AFTER b (t37's
  985. # overflow, unobserved only because no fixture read a boolean). The fix is
  986. # one arm of IoCall's type dispatch, so the mutation restores exactly that
  987. # arm. The check is the COMPILE MATRIX rather than any run: an ERR fixture
  988. # is never executed, so nothing that runs programs on it can see a refusal.
  989. cp "$SAVED_C" Compiler.mod
  990. if python3 - <<'PYX'
  991. p = 'Compiler.mod'
  992. s = open(p).read()
  993. old = """ ELSIF acls = TBool THEN
  994. (* TP3 refuses boolean I/O: TPSRC8 prderrtp is error 66,
  995. "I/O not allowed here", and the runtime has no xrdbool
  996. at all. rdbool stores a WORD, so one byte of an
  997. accepted read would land on the variable AFTER b - the
  998. exact overflow t37_readbyte pins for a byte. TP3's
  999. answer to the whole class is the error; so is ours. *)
  1000. Err (EIoHere) ;
  1001. RETURN"""
  1002. assert s.count(old) == 1, 'the boolean-read error arm found %d times -- update this mutation' % s.count(old)
  1003. rep = """ ELSIF acls = TBool THEN
  1004. ent := TU_RdBool"""
  1005. open(p, 'w').write(s.replace(old, rep))
  1006. PYX
  1007. then
  1008. if rebuild_compiler; then
  1009. expect_red "t38 red when a boolean read dispatches to TU_RdBool" \
  1010. "t38_readbool" tests/run_compile_tests.sh
  1011. else
  1012. echo " FAIL: the compiler would not rebuild with the boolean read accepted"
  1013. fail=$((fail + 1))
  1014. fi
  1015. else
  1016. echo " BROKEN CASE: the boolean-read mutation did not apply"
  1017. fail=$((fail + 1))
  1018. fi
  1019. cp "$SAVED_C" Compiler.mod
  1020. # The string variable (t39). A STRING is the first declared type bigger than
  1021. # a word that this compiler accepts, and it is accepted in two places: DefVar
  1022. # exempts TString from its `size > 2' catch-all so `var s : string [10]' can
  1023. # exist at all, and the two arms below USE it once it does. The three
  1024. # mutations are the three places the feature lives, each restored to the
  1025. # exact pre-fix state (all three used to be ENoLib, or never existed):
  1026. #
  1027. # 1. the DEPARSE of assignment: before the fix, `s := 'hello'' fell out of
  1028. # Assignment's size check. Removing the string arm puts it back there.
  1029. # 2. the WRITE: before the fix, `writeln (s)' was refused by IoCall's
  1030. # type dispatch. Restoring the refusal arm is removing the feature.
  1031. # 3. the RUNTIME: wrstr's print loop is only as good as its length byte,
  1032. # and only rt_exec.py can see a wrstr that reads no length at all.
  1033. # 1. Assignment's string arm is gone, so `s := 'hello'' trips the old
  1034. # `size > 2' refusal again. The arm is spliced between its guard and the
  1035. # `size > 2' check that follows it, so a comment inside cannot break the
  1036. # match - but the GUARD is matched by text, and it has to be the whole
  1037. # guard. It grew an `AND (r.cls = TString)' when `s[i] := c' made a
  1038. # TString destination reach a second arm, and ParseSub grew a look-alike
  1039. # `IF symtab [r.idx].cls = TString THEN' for `s[i]'. The shorter anchor
  1040. # therefore now finds ParseSub's, some 800 lines earlier, and the splice
  1041. # below deletes everything between the two - which does not test the
  1042. # refusal, it tests whether the compiler still compiles. Hence the assert
  1043. # and the spelled-out guard.
  1044. cp "$SAVED_C" Compiler.mod
  1045. if python3 - <<'PYX'
  1046. p = 'Compiler.mod'
  1047. s = open(p).read()
  1048. g = ' IF (r.cls = TString) AND (VarSize (r) > 1) THEN\n'
  1049. assert s.count(g) == 1, 'the string-assignment guard found %d times' % s.count(g)
  1050. a = s.index(g)
  1051. m = ' RETURN\n END ;\n IF (r.kind = 5) OR (VarSize (r) > 2) THEN\n'
  1052. i = s.index(m, a)
  1053. new = g + ' Err (ENoLib) ; (* MUTATION: the arm is refused *)\n RETURN\n'
  1054. open(p, 'w').write(s[:a] + new + s[i + len(' RETURN\n'):])
  1055. PYX
  1056. then
  1057. if rebuild_compiler; then
  1058. expect_red "t39 red when the string-assignment arm is refused" \
  1059. "t39_strvar" tests/run_compile_tests.sh
  1060. else
  1061. echo " FAIL: the compiler would not rebuild with the string assignment refused"
  1062. fail=$((fail + 1))
  1063. fi
  1064. else
  1065. echo " BROKEN CASE: the string-assignment mutation did not apply"
  1066. fail=$((fail + 1))
  1067. fi
  1068. cp "$SAVED_C" Compiler.mod
  1069. # 2. The write arm of IoCall, restored to its pre-fix refusal.
  1070. cp "$SAVED_C" Compiler.mod
  1071. if python3 - <<'PYX'
  1072. p = 'Compiler.mod'
  1073. s = open(p).read()
  1074. old = """ EmPushVarAddr (symtab [arg.idx].local, symtab [arg.idx].off) ;
  1075. ent := TU_WrStr"""
  1076. assert s.count(old) == 1, 'the string-write arm found %d times -- update this mutation' % s.count(old)
  1077. rep = """ Err (ENoLib) ;
  1078. RETURN"""
  1079. open(p, 'w').write(s.replace(old, rep))
  1080. PYX
  1081. then
  1082. if rebuild_compiler; then
  1083. expect_red "t39 red when a string write is refused again (ENoLib)" \
  1084. "t39_strvar" tests/run_compile_tests.sh
  1085. else
  1086. echo " FAIL: the compiler would not rebuild with the string write refused"
  1087. fail=$((fail + 1))
  1088. fi
  1089. else
  1090. echo " BROKEN CASE: the string-write mutation did not apply"
  1091. fail=$((fail + 1))
  1092. fi
  1093. cp "$SAVED_C" Compiler.mod
  1094. # 3. The runtime half: wrstr never reads the length byte, so every string -
  1095. # "hello" and the empty one alike - prints nothing. The compiler, the
  1096. # golden and the audit all stay green; only executing the entry finds it.
  1097. cp "$SAVED" Runtime.mod
  1098. if python3 - <<'PYX'
  1099. p = 'Runtime.mod'
  1100. s = open(p).read()
  1101. old = """ MovBxBp4 ; (* BX := the caller's string address *)
  1102. XorCxCx ;
  1103. MovClBx ; (* CX := the length byte at [BX] *)"""
  1104. assert s.count(old) == 1, 'the wrstr length load found %d times -- update this mutation' % s.count(old)
  1105. rep = """ MovBxBp4 ; (* BX := the caller's string address *)
  1106. XorCxCx ; (* CX is never loaded: prints nothing *)"""
  1107. open(p, 'w').write(s.replace(old, rep))
  1108. PYX
  1109. then
  1110. if rebuild; then
  1111. expect_red "the runtime cases catch a wrstr that reads no length" \
  1112. "wrstr" python3 tests/rt_exec.py
  1113. else
  1114. echo " FAIL: the runtime would not rebuild with the length load dropped"
  1115. fail=$((fail + 1))
  1116. fi
  1117. else
  1118. echo " BROKEN CASE: the wrstr length-load mutation did not apply"
  1119. fail=$((fail + 1))
  1120. fi
  1121. cp "$SAVED" Runtime.mod
  1122. # Re-emit the object: the `rebuild` above compiled Runtime.o from the MUTATED
  1123. # source, and the restored-compiler check next links that object - without
  1124. # this recompile it would report on the mutation, not on the restored code.
  1125. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1 || {
  1126. echo " FAIL: Runtime.mod would not recompile after the restore"
  1127. fail=$((fail + 1))
  1128. }
  1129. if rebuild_compiler; then
  1130. if python3 tests/run_com_exec.py >/dev/null 2>&1; then
  1131. # No count: the matrix grows every time a fixture is added, and a
  1132. # number here would be right only until the next one.
  1133. echo " ok: every executed fixture passes on the restored compiler"
  1134. pass=$((pass + 1))
  1135. else
  1136. echo "NOT RESTORED: run_com_exec.py is red after restoring Compiler.mod"
  1137. python3 tests/run_com_exec.py 2>&1 | grep -i fail | head -3 | sed 's/^/ /'
  1138. fail=$((fail + 1))
  1139. fi
  1140. else
  1141. echo "NOT RESTORED: the compiler would not rebuild"
  1142. fail=$((fail + 1))
  1143. fi
  1144. echo
  1145. echo "== the string READ and ASSIGNMENT entries (t40, t41)"
  1146. # t39 above gave a STRING variable a frame and printed it. This milestone puts
  1147. # one on the RECEIVING end of the two operations that move data into a string
  1148. # and between two strings - readln(s)/read(s), and s := t - and each one is
  1149. # three things at once: a compiler arm that chooses a runtime entry, the
  1150. # convention that carries the limit, and the entry itself. The cases below
  1151. # break each of the six in turn. Every mutation is a fault, or a plausible
  1152. # neighbour of one, that was run to a red BY HAND before it was written here.
  1153. #
  1154. # The frame rule is the odd one out: it is not a new entry at all, but a
  1155. # pre-existing compiler bug that only this milestone could reach. A local
  1156. # STRING was packed with the INTEGER rule - the symbol took the pre-decrement
  1157. # locFree, so a string[5] local sat at BP-2 and its characters grew UP into
  1158. # BP+3, over the saved BP at [BP] and the return address at [BP+2]. `readln(s)'
  1159. # into such a local stored 'hello' over its own return address, and t40 then ran
  1160. # the string's own bytes as an address: Exec86 said `fault at IP=0x6F6C', which
  1161. # is 'l','o'. The fix packs locals top-anchored, so the first INTEGER (size 2)
  1162. # is still at -2 - byte for byte what the old rule produced - while a string[5]
  1163. # (size 6) goes to -6..-1, inside the frame. t39's size (217) and t40's
  1164. # behaviour are the two independent things that pin it.
  1165. # --- the two runtime entries ---------------------------------------------
  1166. # rdstr stored the first character where the length byte belongs, because the
  1167. # IncSi that steps SI past dest[0] was missing. The length byte then
  1168. # overwrote it: `hello' read back as 05 65 6c 6c 6f. Same length, same sizes,
  1169. # and the whole compile matrix stays green - only rt_exec.py's byte dump can
  1170. # see it.
  1171. cp "$SAVED" Runtime.mod
  1172. if python3 - <<'PYX'
  1173. p = 'Runtime.mod'
  1174. lines = open(p).read().split('\n')
  1175. out, i, n = [], 0, 0
  1176. while i < len(lines):
  1177. if lines[i].startswith(' IncSi ;') and 'dest + 1' in lines[i]:
  1178. out.append(' (* MUTATION: the IncSi that puts chars at dest+1 is gone *)')
  1179. i += 1
  1180. while i < len(lines) and not lines[i].rstrip().endswith('*)'):
  1181. i += 1
  1182. i += 1
  1183. n += 1
  1184. continue
  1185. out.append(lines[i])
  1186. i += 1
  1187. assert n == 1, 'the rdstr IncSi found %d times -- update this mutation' % n
  1188. open(p, 'w').write('\n'.join(out))
  1189. PYX
  1190. then
  1191. if rebuild; then
  1192. expect_red "rt_exec catches rdstr storing its first character at dest[0]" \
  1193. "rdstr reads a line into a string" python3 tests/rt_exec.py
  1194. else
  1195. echo " FAIL: the runtime would not rebuild with rdstr's IncSi removed"
  1196. fail=$((fail + 1))
  1197. fi
  1198. else
  1199. echo " BROKEN CASE: the rdstr IncSi mutation did not apply"
  1200. fail=$((fail + 1))
  1201. fi
  1202. cp "$SAVED" Runtime.mod
  1203. # strstore's empty-source guard. A source of length 0 sets CX := 0, and LOOP
  1204. # with CX = 0 runs 65536 times rather than zero - so `s := ''' copied 64K,
  1205. # wrapped the segment, and walked through the driver's own case record. That is
  1206. # why the pattern below matches the CASE, not a byte count: rt_exec.py reports
  1207. # this one as a malformed record, which is the honest shape of "the entry wrote
  1208. # where it had no business writing".
  1209. if python3 - <<'PYX'
  1210. p = 'Runtime.mod'
  1211. lines = open(p).read().split('\n')
  1212. out, i, n = [], 0, 0
  1213. while i < len(lines):
  1214. if lines[i].startswith(' Jcxz8 ("ss_done")'):
  1215. out.append(' (* MUTATION: the empty-source guard is gone *)')
  1216. i += 1
  1217. while i < len(lines) and not lines[i].rstrip().endswith('*)'):
  1218. i += 1
  1219. i += 1
  1220. n += 1
  1221. continue
  1222. out.append(lines[i])
  1223. i += 1
  1224. assert n == 1, 'the strstore Jcxz8 found %d times -- update this mutation' % n
  1225. open(p, 'w').write('\n'.join(out))
  1226. PYX
  1227. then
  1228. if rebuild; then
  1229. expect_red "rt_exec catches a strstore that runs LOOP with CX=0" \
  1230. "strstore copies an empty string" python3 tests/rt_exec.py
  1231. else
  1232. echo " FAIL: the runtime would not rebuild with the empty-source guard removed"
  1233. fail=$((fail + 1))
  1234. fi
  1235. else
  1236. echo " BROKEN CASE: the strstore empty-source guard mutation did not apply"
  1237. fail=$((fail + 1))
  1238. fi
  1239. cp "$SAVED" Runtime.mod
  1240. # Re-emit the object: `rebuild` above compiled Runtime.o from the MUTATED
  1241. # source, and the compiler rebuild below links that object - without this
  1242. # recompile the restored-compiler check at the end would report on the mutation.
  1243. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1 || {
  1244. echo " FAIL: Runtime.mod would not recompile after the restore"
  1245. fail=$((fail + 1))
  1246. }
  1247. # --- the frame rule -------------------------------------------------------
  1248. # The pre-fix layout, exactly: the symbol took locFree itself instead of its
  1249. # top, and the frame began at 0FFFEh. For INTEGER locals that is byte-for-byte
  1250. # the same -2, -4, ... the new rule produces, which is why the old rule survived
  1251. # so long unnoticed; for a STRING it puts characters where the return address
  1252. # is. Two independent readers go red together, so both are asserted: the
  1253. # compile matrix pins the LAYOUT (t39's code size drops 217 -> 215), and t40
  1254. # pins that the program RUNS (with the overlap its readln never sees the line
  1255. # terminator and the machine hangs).
  1256. cp "$SAVED_C" Compiler.mod
  1257. if python3 - <<'PYX'
  1258. p = 'Compiler.mod'
  1259. s = open(p).read()
  1260. a = '(locFree - size + 1) MOD 10000H'
  1261. assert s.count(a) == 2, 'the top-anchored base found %d times -- update this mutation' % s.count(a)
  1262. s = s.replace(a, 'locFree') # the old pre-decrement base
  1263. b = 'locFree := 0FFFFH ;'
  1264. assert s.count(b) == 2, 'the frame top found %d times -- update this mutation' % s.count(b)
  1265. s = s.replace(b, 'locFree := 0FFFEH ;') # the old starting point
  1266. open(p, 'w').write(s)
  1267. PYX
  1268. then
  1269. if rebuild_compiler; then
  1270. expect_red "the compile matrix catches a local string packed over the frame" \
  1271. "expected: OK 217" tests/run_compile_tests.sh
  1272. expect_red "t40 red when readln into a local string overwrites its return address" \
  1273. "never halted" python3 tests/run_com_exec.py t40_readln
  1274. else
  1275. echo " FAIL: the compiler would not rebuild with the old frame layout"
  1276. fail=$((fail + 1))
  1277. fi
  1278. else
  1279. echo " BROKEN CASE: the frame-layout revert did not apply"
  1280. fail=$((fail + 1))
  1281. fi
  1282. cp "$SAVED_C" Compiler.mod
  1283. # --- the compiler's arms --------------------------------------------------
  1284. # The limit both entries read is MOV CX,imm16. MOV AX,imm16 is one opcode away,
  1285. # the same length, and the audit's MovCxi row is COMP - "no single sequence to
  1286. # disassemble" - so nothing in the byte-level checks can tell them apart. Only
  1287. # execution can: t41's `s := t' truncates to 4 under CX and does not under AX.
  1288. # (t40 passes either way, because its CX happens to survive large enough for the
  1289. # five characters it reads - exactly the kind of luck a behavioural case is for.)
  1290. cp "$SAVED_C" Compiler.mod
  1291. if mutate_compiler 's| Ebyte (0B9H) ; Eword (imm)| Ebyte (0B8H) ; Eword (imm)|'; then
  1292. if rebuild_compiler; then
  1293. expect_red "t41 red when the destination limit is loaded into AX, not CX" \
  1294. "t41_strassign" python3 tests/run_com_exec.py t41_strassign
  1295. else
  1296. echo " FAIL: the compiler would not rebuild with MovCxi emitting MOV AX"
  1297. fail=$((fail + 1))
  1298. fi
  1299. fi
  1300. cp "$SAVED_C" Compiler.mod
  1301. # Assignment pushes the source first, then the destination, so the source sits
  1302. # at [BP+6] and the destination at [BP+4]. Swapped, strstore takes the
  1303. # destination as its own source - t41's `worl' rows become copies of what was
  1304. # already in the destination, which looks like it works and prints the wrong
  1305. # bytes.
  1306. cp "$SAVED_C" Compiler.mod
  1307. if python3 - <<'PYX'
  1308. p = 'Compiler.mod'
  1309. s = open(p).read()
  1310. src = (' EmPushVarAddr (symtab [src.idx].local,\n'
  1311. ' (symtab [src.idx].off + src.boff) MOD 10000H) ;\n'
  1312. ' EmPushVarAddr (symtab [r.idx].local,\n'
  1313. ' (symtab [r.idx].off + r.boff) MOD 10000H) ;')
  1314. dst = (' EmPushVarAddr (symtab [r.idx].local,\n'
  1315. ' (symtab [r.idx].off + r.boff) MOD 10000H) ;\n'
  1316. ' EmPushVarAddr (symtab [src.idx].local,\n'
  1317. ' (symtab [src.idx].off + src.boff) MOD 10000H) ;')
  1318. assert s.count(src) == 1, 'the assignment pushes found %d times -- update this mutation' % s.count(src)
  1319. open(p, 'w').write(s.replace(src, dst))
  1320. PYX
  1321. then
  1322. if rebuild_compiler; then
  1323. expect_red "t41 red when the assignment pushes are swapped" \
  1324. "t41_strassign" python3 tests/run_com_exec.py t41_strassign
  1325. else
  1326. echo " FAIL: the compiler would not rebuild with the pushes swapped"
  1327. fail=$((fail + 1))
  1328. fi
  1329. else
  1330. echo " BROKEN CASE: the assignment push-order mutation did not apply"
  1331. fail=$((fail + 1))
  1332. fi
  1333. cp "$SAVED_C" Compiler.mod
  1334. # The read dispatch's string arm. TU_RdStr and TU_RdInt sit next to each other
  1335. # in the same table and both take one address; TU_RdInt parses the line as a
  1336. # number, which stores nothing on `world', so readln(s) leaves s empty and the
  1337. # program prints a blank line. A refusal would at least say so; this arm looks
  1338. # like it worked and answers with the wrong bytes.
  1339. cp "$SAVED_C" Compiler.mod
  1340. if python3 - <<'PYX'
  1341. p = 'Compiler.mod'
  1342. s = open(p).read()
  1343. old = (' EmMovCxi ((VarSize (arg) - 1) MOD 10000H) ;\n'
  1344. ' ent := TU_RdStr')
  1345. assert s.count(old) == 1, 'the string-read arm found %d times -- update this mutation' % s.count(old)
  1346. open(p, 'w').write(s.replace(old, old.replace('TU_RdStr', 'TU_RdInt')))
  1347. PYX
  1348. then
  1349. if rebuild_compiler; then
  1350. expect_red "t40 red when a string read dispatches to TU_RdInt" \
  1351. "t40_readln" python3 tests/run_com_exec.py t40_readln
  1352. else
  1353. echo " FAIL: the compiler would not rebuild with the read dispatch on TU_RdInt"
  1354. fail=$((fail + 1))
  1355. fi
  1356. else
  1357. echo " BROKEN CASE: the string-read dispatch mutation did not apply"
  1358. fail=$((fail + 1))
  1359. fi
  1360. cp "$SAVED_C" Compiler.mod
  1361. # --- the include pre-pass -------------------------------------------------
  1362. # ExpandIncludes is the only thing that turns {$I name} into the named file;
  1363. # forced off, it is exactly the old bug - the directive is a comment and the
  1364. # include's own symbols do not exist. t56_include's `FromInclude' lives only in
  1365. # the .inc, so the compile matrix demands OK 56 4 and gets ERR 41 instead. That
  1366. # pins the fixture to the include ACTUALLY happening, not merely to the buffer
  1367. # being well formed; t57/t58 pin the two refusals (90 and 96) separately.
  1368. cp "$SAVED_C" Compiler.mod
  1369. if python3 - <<'PYX'
  1370. p = 'Compiler.mod'
  1371. s = open(p).read()
  1372. old = 'isInc := TRUE'
  1373. assert s.count(old) == 1, 'the include flag found %d times -- update this mutation' % s.count(old)
  1374. open(p, 'w').write(s.replace(old, 'isInc := FALSE'))
  1375. PYX
  1376. then
  1377. if rebuild_compiler; then
  1378. expect_red "t56 red when the include is swallowed, not expanded" \
  1379. "expected: OK 56 4" tests/run_compile_tests.sh
  1380. else
  1381. echo " FAIL: the compiler would not rebuild with the include forced off"
  1382. fail=$((fail + 1))
  1383. fi
  1384. else
  1385. echo " BROKEN CASE: the include-flag mutation did not apply"
  1386. fail=$((fail + 1))
  1387. fi
  1388. cp "$SAVED_C" Compiler.mod
  1389. # --- typed constants, and the caret/decimal character constants -----------
  1390. # A scalar typed constant's whole point is the CLASS its declaration gives the
  1391. # symbol: the value folds to an immediate either way, so the code size does not
  1392. # move when the class is dropped - `cls := typCls' -> `cls := cls' turns
  1393. # t59's `write (FXMax)' from the char writer to the integer one and the program
  1394. # prints 71 for 'G'. The compile matrix stays GREEN (same OK 65 4); only
  1395. # execution can see it, which is why this case runs the .COM and not the matrix.
  1396. cp "$SAVED_C" Compiler.mod
  1397. if python3 - <<'PYX'
  1398. p = 'Compiler.mod'
  1399. s = open(p).read()
  1400. old = ' cls := typCls'
  1401. assert s.count(old) == 1, 'the declared-type assignment found %d times -- update this mutation' % s.count(old)
  1402. open(p, 'w').write(s.replace(old, ' cls := cls'))
  1403. PYX
  1404. then
  1405. if rebuild_compiler; then
  1406. expect_red "t59 red when a typed constant's declared class is thrown away" \
  1407. "t59_typedconst" python3 tests/run_com_exec.py t59_typedconst
  1408. else
  1409. echo " FAIL: the compiler would not rebuild with the typed-const class dropped"
  1410. fail=$((fail + 1))
  1411. fi
  1412. else
  1413. echo " BROKEN CASE: the typed-const class mutation did not apply"
  1414. fail=$((fail + 1))
  1415. fi
  1416. cp "$SAVED_C" Compiler.mod
  1417. # ^M and #27 are CHARACTER constants, and ParseAtom must accept them as
  1418. # literals - RdConst alone only helps in a declaration. Drop them from the
  1419. # literal test and `write (^M)' is a syntax error, so the MATRIX goes red here
  1420. # (unlike the class case above, this one is visible at compile time).
  1421. cp "$SAVED_C" Compiler.mod
  1422. if python3 - <<'PYX'
  1423. p = 'Compiler.mod'
  1424. s = open(p).read()
  1425. old = " OR (CurCh () = '^') OR (CurCh () = '#') THEN"
  1426. assert s.count(old) == 1, 'the caret/decimal literal test found %d times -- update this mutation' % s.count(old)
  1427. open(p, 'w').write(s.replace(old, ' THEN'))
  1428. PYX
  1429. then
  1430. if rebuild_compiler; then
  1431. expect_red "t60 red when ^M / #nn are not character literals" \
  1432. "expected: OK 75 4" tests/run_compile_tests.sh
  1433. else
  1434. echo " FAIL: the compiler would not rebuild with the caret literal test removed"
  1435. fail=$((fail + 1))
  1436. fi
  1437. else
  1438. echo " BROKEN CASE: the caret-literal mutation did not apply"
  1439. fail=$((fail + 1))
  1440. fi
  1441. cp "$SAVED_C" Compiler.mod
  1442. # --- the set membership bit -----------------------------------------------
  1443. # Compile-time `in' reads a byte out of the emitted set and tests one bit. Flip
  1444. # the test and every answer is wrong while the sizes are untouched, so the
  1445. # compile matrix stays GREEN (same OK 183 4) and only execution sees it - the
  1446. # same shape as the typed-constant class case above, and the reason t61 has a
  1447. # .out rather than just a row.
  1448. cp "$SAVED_C" Compiler.mod
  1449. if python3 - <<'PYX'
  1450. p = 'Compiler.mod'
  1451. s = open(p).read()
  1452. old = 'isIn := (o DIV m) MOD 2 # 0'
  1453. assert s.count(old) == 1, 'the set membership bit test found %d times -- update this mutation' % s.count(old)
  1454. open(p, 'w').write(s.replace(old, 'isIn := (o DIV m) MOD 2 = 0'))
  1455. PYX
  1456. then
  1457. if rebuild_compiler; then
  1458. expect_red "t61 red when the set membership bit test is inverted" \
  1459. "t61_setconst" python3 tests/run_com_exec.py t61_setconst
  1460. else
  1461. echo " FAIL: the compiler would not rebuild with the set bit test inverted"
  1462. fail=$((fail + 1))
  1463. fi
  1464. else
  1465. echo " BROKEN CASE: the set-bit mutation did not apply"
  1466. fail=$((fail + 1))
  1467. fi
  1468. cp "$SAVED_C" Compiler.mod
  1469. # --- an enumerator's ordinal ----------------------------------------------
  1470. # Every enumerator must get its own ordinal (0,1,2,..). Give them all 0 and
  1471. # every `a = SomeEnumerator' is TRUE, while the sizes do not move: the compile
  1472. # matrix stays green (OK 187 5) and only execution sees it.
  1473. cp "$SAVED_C" Compiler.mod
  1474. if python3 - <<'PYX'
  1475. p = 'Compiler.mod'
  1476. s = open(p).read()
  1477. old = 'KConst, TScalar, 1, 1, 0, hi + 1, FALSE'
  1478. assert s.count(old) == 1, 'the enumerator ordinal found %d times -- update this mutation' % s.count(old)
  1479. open(p, 'w').write(s.replace(old, 'KConst, TScalar, 1, 1, 0, 0, FALSE'))
  1480. PYX
  1481. then
  1482. if rebuild_compiler; then
  1483. expect_red "t62 red when every enumerator gets ordinal 0" \
  1484. "t62_enum" python3 tests/run_com_exec.py t62_enum
  1485. else
  1486. echo " FAIL: the compiler would not rebuild with the enumerator ordinals flattened"
  1487. fail=$((fail + 1))
  1488. fi
  1489. else
  1490. echo " BROKEN CASE: the enumerator-ordinal mutation did not apply"
  1491. fail=$((fail + 1))
  1492. fi
  1493. cp "$SAVED_C" Compiler.mod
  1494. # --- subrange sizing ------------------------------------------------------
  1495. # TP3 sizes a subrange by its bounds: one byte when both are inside 0..255.
  1496. # Force two bytes and CellRec grows from 80 to 82, so the compile matrix's
  1497. # `data=84' becomes `data=86' - this one is visible at compile time, unlike the
  1498. # set/class cases above.
  1499. cp "$SAVED_C" Compiler.mod
  1500. if python3 - <<'PYX'
  1501. p = 'Compiler.mod'
  1502. s = open(p).read()
  1503. old = 'IF (lo >= 0) AND (hi <= 255) AND (lo <= hi) THEN\n size := 1'
  1504. assert s.count(old) == 1, 'the subrange size test found %d times -- update this mutation' % s.count(old)
  1505. open(p, 'w').write(s.replace(old, old.replace('size := 1', 'size := 2')))
  1506. PYX
  1507. then
  1508. if rebuild_compiler; then
  1509. expect_red "t63 red when a small subrange is sized two bytes" \
  1510. "expected: OK 57 84" tests/run_compile_tests.sh
  1511. else
  1512. echo " FAIL: the compiler would not rebuild with subranges forced to two bytes"
  1513. fail=$((fail + 1))
  1514. fi
  1515. else
  1516. echo " BROKEN CASE: the subrange-size mutation did not apply"
  1517. fail=$((fail + 1))
  1518. fi
  1519. cp "$SAVED_C" Compiler.mod
  1520. # --- an element's width ---------------------------------------------------
  1521. # A constant array subscript must hand the ELEMENT's size to the store, or the
  1522. # array's own size is used and `v[1] := 11' writes the whole array. Drop the
  1523. # width and the compile matrix stays green (same OK 101 16) while execution
  1524. # sees 0s and a corrupted neighbour - the shape the width exists to prevent.
  1525. cp "$SAVED_C" Compiler.mod
  1526. if python3 - <<'PYX'
  1527. p = 'Compiler.mod'
  1528. s = open(p).read()
  1529. old = 'r.sz := arrs [ai].elem'
  1530. assert s.count(old) == 1, 'the element width assignment found %d times -- update this mutation' % s.count(old)
  1531. open(p, 'w').write(s.replace(old, 'r.sz := 0'))
  1532. PYX
  1533. then
  1534. if rebuild_compiler; then
  1535. expect_red "t65 red when a subscripted element keeps the array's width" \
  1536. "t65_array_access" python3 tests/run_com_exec.py t65_array_access
  1537. else
  1538. echo " FAIL: the compiler would not rebuild with the element width dropped"
  1539. fail=$((fail + 1))
  1540. fi
  1541. else
  1542. echo " BROKEN CASE: the element-width mutation did not apply"
  1543. fail=$((fail + 1))
  1544. fi
  1545. cp "$SAVED_C" Compiler.mod
  1546. # --- the file variable's size ---------------------------------------------
  1547. # Our file control block is FileVarSize bytes; shrink it and every file
  1548. # variable's slot moves, so t66's `data=12' becomes `data=8'. Visible at
  1549. # compile time, so the matrix carries it.
  1550. cp "$SAVED_C" Compiler.mod
  1551. if python3 - <<'PYX'
  1552. p = 'Compiler.mod'
  1553. s = open(p).read()
  1554. old = 'FileVarSize = 4 ;'
  1555. assert s.count(old) == 1, 'the file variable size found %d times -- update this mutation' % s.count(old)
  1556. open(p, 'w').write(s.replace(old, 'FileVarSize = 2 ;'))
  1557. PYX
  1558. then
  1559. if rebuild_compiler; then
  1560. expect_red "t66 red when the file variable is the wrong size" \
  1561. "expected: OK 50 12" tests/run_compile_tests.sh
  1562. else
  1563. echo " FAIL: the compiler would not rebuild with the file size changed"
  1564. fail=$((fail + 1))
  1565. fi
  1566. else
  1567. echo " BROKEN CASE: the file-size mutation did not apply"
  1568. fail=$((fail + 1))
  1569. fi
  1570. cp "$SAVED_C" Compiler.mod
  1571. # --- a string parameter's slot --------------------------------------------
  1572. # A string VALUE parameter's slot is its size (n+1 bytes). Collapse SlotSize
  1573. # to 2 for it and BOTH sides agree on 2 - so the compiler still compiles and
  1574. # the matrix stays green - but the callee's "string" is just a length byte and
  1575. # garbage; t67 prints something other than `hello'.
  1576. cp "$SAVED_C" Compiler.mod
  1577. if python3 - <<'PYX'
  1578. p = 'Compiler.mod'
  1579. s = open(p).read()
  1580. old = ' RETURN size\nEND SlotSize ;'
  1581. assert s.count(old) == 1, 'the SlotSize fallthrough found %d times -- update this mutation' % s.count(old)
  1582. open(p, 'w').write(s.replace(old, ' RETURN 2\nEND SlotSize ;'))
  1583. PYX
  1584. then
  1585. if rebuild_compiler; then
  1586. expect_red "t67 red when a string parameter's slot collapses to a word" \
  1587. "t67_strparam" python3 tests/run_com_exec.py t67_strparam
  1588. else
  1589. echo " FAIL: the compiler would not rebuild with SlotSize collapsed"
  1590. fail=$((fail + 1))
  1591. fi
  1592. else
  1593. echo " BROKEN CASE: the string-slot mutation did not apply"
  1594. fail=$((fail + 1))
  1595. fi
  1596. cp "$SAVED_C" Compiler.mod
  1597. # --- a string builtin is registered ---------------------------------------
  1598. # LENGTH has to be in the symbol table, or the name is simply unknown. Remove
  1599. # its registration and t68 stops compiling (ERR 41), so the compile matrix
  1600. # carries this one.
  1601. cp "$SAVED_C" Compiler.mod
  1602. if python3 - <<'PYX'
  1603. p = 'Compiler.mod'
  1604. s = open(p).read()
  1605. old = (' i := NewSym ("LENGTH" , KBuiltin, BI_Length , 0, 0, 0, 0, FALSE) ;\n'
  1606. ' symtab [i].defnd := TRUE ;\n'
  1607. ' symtab [i].goPos := 0 ; (* inline: the length byte, no entry *)\n')
  1608. assert s.count(old) == 1, 'the LENGTH registration found %d times -- update this mutation' % s.count(old)
  1609. open(p, 'w').write(s.replace(old, ''))
  1610. PYX
  1611. then
  1612. if rebuild_compiler; then
  1613. expect_red "t68 red when LENGTH is not registered" \
  1614. "expected: OK 122 25" tests/run_compile_tests.sh
  1615. else
  1616. echo " FAIL: the compiler would not rebuild without the LENGTH builtin"
  1617. fail=$((fail + 1))
  1618. fi
  1619. else
  1620. echo " BROKEN CASE: the LENGTH-registration mutation did not apply"
  1621. fail=$((fail + 1))
  1622. fi
  1623. cp "$SAVED_C" Compiler.mod
  1624. # --- a CRT builtin is registered ------------------------------------------
  1625. # ClrScr must be in the symbol table for t69 to compile. Remove it and the
  1626. # fixture stops at ERR 41 against its expected OK 102 4.
  1627. cp "$SAVED_C" Compiler.mod
  1628. if python3 - <<'PYX'
  1629. p = 'Compiler.mod'
  1630. s = open(p).read()
  1631. old = ' i := NewSym ("CLRSCR" , KBuiltin, BI_ClrScr , 0, 0, 0, 0, FALSE) ;\n symtab [i].defnd := TRUE ;\n'
  1632. assert s.count(old) == 1, 'the CLRSCR registration found %d times -- update this mutation' % s.count(old)
  1633. open(p, 'w').write(s.replace(old, ''))
  1634. PYX
  1635. then
  1636. if rebuild_compiler; then
  1637. expect_red "t69 red when CLRSCR is not registered" \
  1638. "expected: OK 119 4" tests/run_compile_tests.sh
  1639. else
  1640. echo " FAIL: the compiler would not rebuild without the CLRSCR builtin"
  1641. fail=$((fail + 1))
  1642. fi
  1643. else
  1644. echo " BROKEN CASE: the CLRSCR-registration mutation did not apply"
  1645. fail=$((fail + 1))
  1646. fi
  1647. cp "$SAVED_C" Compiler.mod
  1648. # --- KBD is registered ----------------------------------------------------
  1649. # Without the predefined KBD file, `Read(Kbd, Ch)' is an unknown identifier and
  1650. # t70 stops compiling against its expected OK 55 5.
  1651. cp "$SAVED_C" Compiler.mod
  1652. if python3 - <<'PYX'
  1653. p = 'Compiler.mod'
  1654. s = open(p).read()
  1655. old = (' i := NewSym ("KBD", KVar, TText, FileVarSize, 1, 0, 0, FALSE) ;\n'
  1656. ' symtab [i].isKbd := TRUE\n')
  1657. assert s.count(old) == 1, 'the KBD registration found %d times -- update this mutation' % s.count(old)
  1658. open(p, 'w').write(s.replace(old, ''))
  1659. PYX
  1660. then
  1661. if rebuild_compiler; then
  1662. expect_red "t70 red when KBD is not registered" \
  1663. "expected: OK 55 5" tests/run_compile_tests.sh
  1664. else
  1665. echo " FAIL: the compiler would not rebuild without the KBD file"
  1666. fail=$((fail + 1))
  1667. fi
  1668. else
  1669. echo " BROKEN CASE: the KBD-registration mutation did not apply"
  1670. fail=$((fail + 1))
  1671. fi
  1672. cp "$SAVED_C" Compiler.mod
  1673. if rebuild_compiler; then
  1674. if python3 tests/run_com_exec.py >/dev/null 2>&1; then
  1675. echo " ok: every executed fixture passes on the restored sources"
  1676. pass=$((pass + 1))
  1677. else
  1678. echo "NOT RESTORED: run_com_exec.py is red after restoring the sources"
  1679. python3 tests/run_com_exec.py 2>&1 | grep -i fail | head -3 | sed 's/^/ /'
  1680. fail=$((fail + 1))
  1681. fi
  1682. else
  1683. echo "NOT RESTORED: the compiler would not rebuild"
  1684. fail=$((fail + 1))
  1685. fi
  1686. echo
  1687. echo "== the second execution oracle (run_exec86.py)"
  1688. # Everything above runs the image under qemu-system-i386. This one runs the
  1689. # SAME image inside shell/Exec86.mod, this project's own in-process 8086
  1690. # interpreter, and requires its output to agree with BOTH the hand-derived .out
  1691. # and qemu, byte for byte. Two execution checks that could only ever agree
  1692. # with each other would be one check: the point of this one is that it was
  1693. # written against the 8086's own reference rather than against qemu, whose
  1694. # lowest CPU model is a 486 and whose `0F 84' is an ordinary JZ.
  1695. #
  1696. # So the only mutation worth writing here is one qemu cannot make at all - the
  1697. # interpreter's own reading of the machine. Cond is that reading: nibble 4 is
  1698. # JE, and inverting it swaps the two arms of every `=' in every program. The
  1699. # emitted bytes, the sizes and the compile matrix are untouched, because
  1700. # nothing is emitted here - the fault is in who interprets it.
  1701. #
  1702. # Exec86.mod is recompiled explicitly rather than left to the harness:
  1703. # ensure_exec86run() notices the source changed and RELINKS, but does not
  1704. # recompile it, so a mutated source with a stale object would link the good
  1705. # interpreter straight back in and stay green - which is the trap its own
  1706. # docstring records, and the reason this case compiles first.
  1707. #
  1708. # SAVED_E itself was taken at the top of this file, next to the EXIT trap that
  1709. # puts it back.
  1710. if python3 - <<'PYX'
  1711. p = 'Exec86.mod'
  1712. s = open(p).read()
  1713. old = "| 4H : r := ZF"
  1714. new = "| 4H : r := NOT ZF (* MUTATION: JE inverted *)"
  1715. assert s.count(old) == 1, 'the JE arm of Cond found %d times -- update this mutation' % s.count(old)
  1716. open(p, 'w').write(s.replace(old, new))
  1717. PYX
  1718. then
  1719. if $GM2 -fiso -c Exec86.mod >/dev/null 2>&1; then
  1720. expect_red "the interpreter's own oracle catches JE inverted" \
  1721. "t33_cmpops" python3 tests/run_exec86.py t33_cmpops
  1722. else
  1723. echo " FAIL: Exec86.mod would not compile with JE inverted"
  1724. fail=$((fail + 1))
  1725. fi
  1726. else
  1727. echo " BROKEN CASE: the Cond JE mutation did not apply"
  1728. fail=$((fail + 1))
  1729. fi
  1730. cp "$SAVED_E" Exec86.mod
  1731. # And the check on the RESTORED interpreter, because a green above could also
  1732. # come from a mutation that never took and an object left mutated by a run
  1733. # that died in between.
  1734. if $GM2 -fiso -c Exec86.mod >/dev/null 2>&1; then
  1735. if python3 tests/run_exec86.py >/dev/null 2>&1; then
  1736. echo " ok: run_exec86 green on the restored interpreter"
  1737. pass=$((pass + 1))
  1738. else
  1739. echo "NOT RESTORED: run_exec86.py is red after restoring Exec86.mod"
  1740. python3 tests/run_exec86.py 2>&1 | grep -i "fail" | head -3 | sed 's/^/ /'
  1741. fail=$((fail + 1))
  1742. fi
  1743. else
  1744. echo "NOT RESTORED: Exec86.mod would not recompile"
  1745. fail=$((fail + 1))
  1746. fi
  1747. echo
  1748. echo "== the R key: compile, poke, run, report (runtest.py)"
  1749. # The only check in the project that exercises CmdRun. Everything above looks
  1750. # at bytes, or at what qemu says the image does; this one drives the shell
  1751. # through a pty the way a person would, presses R, and compares the GUEST's
  1752. # output against the fixture's hand-derived .out - so it fails on things no
  1753. # byte check can see and no file records either, because R writes no file at
  1754. # all (which is itself asserted).
  1755. #
  1756. # The mutation is the poke loop's count. With n = 0 nothing is copied into
  1757. # the interpreter, loadHi stays where Clear86 left it - 0100h - and Run86
  1758. # faults on its very first step, "execution left the loaded image", before the
  1759. # guest has executed a single instruction. That is fast and deterministic, which
  1760. # matters: the obvious alternative (poking the image somewhere else) leaves the
  1761. # machine executing zeros and buys a step-limit timeout instead of a finding.
  1762. cp "$SAVED_SH" Shell.mod
  1763. if python3 - <<'PYX'
  1764. p = 'Shell.mod'
  1765. s = open(p).read()
  1766. old = " n := LinkSize () ;"
  1767. new = " n := 0 ; (* MUTATION: nothing is poked *)"
  1768. assert s.count(old) == 1, 'the poke count in CmdRun found %d times -- update this mutation' % s.count(old)
  1769. open(p, 'w').write(s.replace(old, new))
  1770. PYX
  1771. then
  1772. if make > ../tmp/nonvacuity.make.log 2>&1; then
  1773. expect_red "the R check catches an image that was never poked" \
  1774. "AH=4Ch exit" python3 tests/runtest.py
  1775. else
  1776. echo " FAIL: the shell would not rebuild with the poke loop neutered"
  1777. tail -5 ../tmp/nonvacuity.make.log | sed 's/^/ /'
  1778. fail=$((fail + 1))
  1779. fi
  1780. else
  1781. echo " BROKEN CASE: the CmdRun poke mutation did not apply"
  1782. fail=$((fail + 1))
  1783. fi
  1784. cp "$SAVED_SH" Shell.mod
  1785. if make > ../tmp/nonvacuity.make.log 2>&1; then
  1786. if python3 tests/runtest.py >/dev/null 2>&1; then
  1787. echo " ok: runtest green on the restored shell"
  1788. pass=$((pass + 1))
  1789. else
  1790. echo "NOT RESTORED: runtest.py is red after restoring Shell.mod"
  1791. python3 tests/runtest.py 2>&1 | grep -i "fail" | head -3 | sed 's/^/ /'
  1792. fail=$((fail + 1))
  1793. fi
  1794. else
  1795. echo "NOT RESTORED: the shell would not rebuild"
  1796. tail -5 ../tmp/nonvacuity.make.log | sed 's/^/ /'
  1797. fail=$((fail + 1))
  1798. fi
  1799. echo
  1800. echo "== 8086 legality of the conditional lowering (check_8086.py)"
  1801. # This whole section exists because of a fault that every other check in the
  1802. # project was blind to, and being blunt about WHY is the point of listing it.
  1803. #
  1804. # EmJcc and EmSetcc emitted `0F 8x rel16' and `0F 9x rel8'. `0F' is a
  1805. # 386-and-later opcode prefix; the 8086 has none. So EVERY conditional branch
  1806. # and EVERY comparison in EVERY compiled program was an illegal instruction on
  1807. # the machine this compiler targets. And all of the following were green while
  1808. # it was: the compile matrix, the .COM layout checker, the runtime golden, the
  1809. # emitter audit, and the whole execution suite answering correctly under qemu.
  1810. #
  1811. # Two reasons, and the second is the one worth keeping:
  1812. # 1. qemu-system-i386 has no 8086 model. Its lowest is 486, where `0F 84' is
  1813. # a perfectly ordinary JZ. So the execution oracle CANNOT see this class
  1814. # of fault, ever.
  1815. # 2. FCML is this project's INDEPENDENT disassembler, and FCML's -m16 mode is
  1816. # a 386. The one tool whose job is to say "this is not a real instruction"
  1817. # was architecturally guaranteed to agree with the bug.
  1818. #
  1819. # So a check was needed that asks the question nothing else was asking. The
  1820. # five mutations below are the mutations that check has to catch, and each was
  1821. # run by hand and confirmed red BEFORE this section existed.
  1822. #
  1823. # M4 and M5 are the interesting pair. M4 is a fault this project actually
  1824. # introduced while fixing the above: EmJcc jumps TO its target, but the 8086
  1825. # shape steps OVER a 3-byte EJMP, so the naive port inverts every conditional
  1826. # in every program. It was caught by execution, not by any byte check. M5 is
  1827. # the partial version -- the inversion dropped for the IF and CASE sites only,
  1828. # kept for FOR -- and MEASURING that is what produced clause H, because the
  1829. # check was blind to M5 as first written: IF declares nibble 4, CASE declares
  1830. # 5, they negate into each other, and both are declared, so nothing complained
  1831. # while every conditional in every program took the wrong path. FOR declares
  1832. # C and F, whose negations D and E are declared for nothing at all, so the
  1833. # whole-suite version was caught by luck.
  1834. #
  1835. # Note these need `rebuild_compiler' only to refresh comtest; check_8086.py
  1836. # relinks the fixtures itself. Getting that wrong is how this section's first
  1837. # draft reported a mutation as VACUOUS: the build was skipped, a stale comtest
  1838. # ran the GOOD compiler, and the check passed. A helper that exits non-zero
  1839. # makes `if mutate_x; then' false and the case is silently skipped, which looks
  1840. # exactly like a pass -- so rebuild_compiler is checked, not assumed.
  1841. # M1: the original defect, restored verbatim.
  1842. cp "$SAVED_C" Compiler.mod
  1843. if python3 - <<'PYX'
  1844. p = 'Compiler.mod'
  1845. s = open(p).read()
  1846. old = """ Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)
  1847. Ebyte (03H) ; (* rel8: step over the 3-byte EJMP *)
  1848. RETURN EmJmpNear (target) (* target = 0 => forward, see above *)"""
  1849. new = """ Ebyte (0FH) ; Ebyte (cc) ; Eword (0) ;
  1850. IF target = 0 THEN
  1851. RETURN nPatch
  1852. END ;
  1853. RETURN 0"""
  1854. assert s.count(old) == 1, 'EmJcc body found %d times -- update this mutation' % s.count(old)
  1855. open(p, 'w').write(s.replace(old, new))
  1856. PYX
  1857. then
  1858. if rebuild_compiler; then
  1859. expect_red "check_8086 catches a 0F 8x branch (the original bug)" \
  1860. "0F 84" python3 tests/check_8086.py
  1861. else
  1862. echo " FAIL: the compiler would not rebuild with the 0F branch"
  1863. fail=$((fail + 1))
  1864. fi
  1865. else
  1866. echo " BROKEN CASE: the 0F branch mutation did not apply"
  1867. fail=$((fail + 1))
  1868. fi
  1869. cp "$SAVED_C" Compiler.mod
  1870. # M2: the other original defect, SETcc plus the MOV AH,0 it needed.
  1871. cp "$SAVED_C" Compiler.mod
  1872. if python3 - <<'PYX'
  1873. p = 'Compiler.mod'
  1874. s = open(p).read()
  1875. old = """ Ebyte (0B8H) ; Eword (1) ; (* MOV AX,#0001 *)
  1876. Ebyte (JccShort (cc)) ; (* taken when the comparison HOLDS *)
  1877. Ebyte (01H) ; (* rel8: step over the DEC AX *)
  1878. Ebyte (48H) (* DEC AX *)"""
  1879. new = """ Ebyte (0FH) ; Ebyte (cc) ; Ebyte (0C0H) ;
  1880. EmMovAh0 ()"""
  1881. assert s.count(old) == 1, 'EmSetcc body found %d times -- update this mutation' % s.count(old)
  1882. open(p, 'w').write(s.replace(old, new))
  1883. PYX
  1884. then
  1885. if rebuild_compiler; then
  1886. expect_red "check_8086 catches a 0F 9x SETcc (the original bug)" \
  1887. "0F 9F C0" python3 tests/check_8086.py
  1888. else
  1889. echo " FAIL: the compiler would not rebuild with the 0F SETcc"
  1890. fail=$((fail + 1))
  1891. fi
  1892. else
  1893. echo " BROKEN CASE: the 0F SETcc mutation did not apply"
  1894. fail=$((fail + 1))
  1895. fi
  1896. cp "$SAVED_C" Compiler.mod
  1897. # M4: the polarity inversion, everywhere. Note the expected text is clause H's,
  1898. # not a shape complaint: every byte here is a legal 8086 shape, which is the
  1899. # entire reason a separate clause had to be written.
  1900. cp "$SAVED_C" Compiler.mod
  1901. if python3 - <<'PYX'
  1902. p = 'Compiler.mod'
  1903. s = open(p).read()
  1904. old = ' Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)'
  1905. new = ' Ebyte (JccShort (cc)) ; (* MUTATION: inversion dropped *)'
  1906. assert s.count(old) == 1, 'EmJcc Ebyte found %d times -- update this mutation' % s.count(old)
  1907. open(p, 'w').write(s.replace(old, new))
  1908. PYX
  1909. then
  1910. if rebuild_compiler; then
  1911. expect_red "clause H catches the branch polarity inverted everywhere" \
  1912. "but reading the source says" python3 tests/check_8086.py
  1913. else
  1914. echo " FAIL: the compiler would not rebuild with the inversion dropped"
  1915. fail=$((fail + 1))
  1916. fi
  1917. else
  1918. echo " BROKEN CASE: the polarity mutation did not apply"
  1919. fail=$((fail + 1))
  1920. fi
  1921. cp "$SAVED_C" Compiler.mod
  1922. # M5: the same inversion dropped for the IF and CASE sites ONLY. This one is
  1923. # the reason clause H exists: it was run by hand and the check stayed GREEN,
  1924. # and FOR's C/F nibbles were the only reason the whole-suite version (M4)
  1925. # happened to be caught.
  1926. cp "$SAVED_C" Compiler.mod
  1927. if python3 - <<'PYX'
  1928. p = 'Compiler.mod'
  1929. s = open(p).read()
  1930. old = ' Ebyte (JccShortInv (cc)) ; (* Jcc_s, taken when cc does NOT hold *)'
  1931. new = """ IF (cc = 84H) OR (cc = 85H) THEN
  1932. Ebyte (JccShort (cc)) (* MUTATION: no inversion here *)
  1933. ELSE
  1934. Ebyte (JccShortInv (cc))
  1935. END ;"""
  1936. assert s.count(old) == 1, 'EmJcc Ebyte found %d times -- update this mutation' % s.count(old)
  1937. open(p, 'w').write(s.replace(old, new))
  1938. PYX
  1939. then
  1940. if rebuild_compiler; then
  1941. expect_red "clause H catches the inversion dropped for IF and CASE only" \
  1942. "t22_case" python3 tests/check_8086.py
  1943. else
  1944. echo " FAIL: the compiler would not rebuild with the partial inversion"
  1945. fail=$((fail + 1))
  1946. fi
  1947. else
  1948. echo " BROKEN CASE: the partial-inversion mutation did not apply"
  1949. fail=$((fail + 1))
  1950. fi
  1951. cp "$SAVED_C" Compiler.mod
  1952. # And the check on the RESTORED compiler, so a green above cannot come from a
  1953. # mutation that failed to take and left the real defect in place.
  1954. if rebuild_compiler; then
  1955. if python3 tests/check_8086.py >/dev/null 2>&1; then
  1956. echo " ok: check_8086 green on the restored compiler"
  1957. pass=$((pass + 1))
  1958. else
  1959. echo "NOT RESTORED: check_8086.py is red after restoring Compiler.mod"
  1960. python3 tests/check_8086.py 2>&1 | grep -m3 -- ' - ' | sed 's/^/ /'
  1961. fail=$((fail + 1))
  1962. fi
  1963. else
  1964. echo "NOT RESTORED: the compiler would not rebuild"
  1965. fail=$((fail + 1))
  1966. fi
  1967. echo
  1968. echo "== the emitter-name audit of Compiler.mod (audit_helpers.py)"
  1969. # These need no rebuild: the audit reads the SOURCE, not the built object, so
  1970. # they are the cheapest cases here and they cover the module the audit used
  1971. # not to look at at all. That is the point of the section: the audit reported
  1972. # "every helper agrees with its name" for a module it had never examined, and
  1973. # EmXchgAxCx was `93` (XCHG BX,AX) under a name that says XCHG AX,CX for the
  1974. # whole life of the project. Two of these five are for faults that were real.
  1975. SAVED_C2=../tmp/nonvacuity.Compiler.mod.2
  1976. SAVED_R2=../tmp/nonvacuity.Runtime.mod.2
  1977. cp Compiler.mod "$SAVED_C2" || exit 1
  1978. cp Runtime.mod "$SAVED_R2" || exit 1
  1979. restore_audit_sources () {
  1980. cp "$SAVED_C2" Compiler.mod
  1981. cp "$SAVED_R2" Runtime.mod
  1982. }
  1983. AUD="python3 tests/audit_helpers.py"
  1984. # 1. THE fault. 91h is XCHG AX,CX; 93h is XCHG BX,AX. Both are one byte, so
  1985. # the compile matrix never moved and the byte counts never moved.
  1986. cp "$SAVED_C2" Compiler.mod
  1987. mutate Compiler.mod 's|^ Ebyte (91H)$| Ebyte (93H)|'
  1988. expect_red "audit catches XchgAxCx emitting XCHG BX,AX" \
  1989. "exchanges Ax and Bx" $AUD
  1990. restore_audit_sources
  1991. # 2. the coverage check itself. A parameter list that find_helpers does not
  1992. # accept is exactly how the real emitter was missed, and the inventory is
  1993. # scanned separately on purpose so this can be caught. Without the
  1994. # independent scan this case is silent, because both lists would come from
  1995. # the same parser and agree that the helper does not exist.
  1996. cp "$SAVED_C2" Compiler.mod
  1997. mutate Compiler.mod 's|^PROCEDURE EmXchgAxCx () ;$|PROCEDURE EmXchgAxCx (why : CARDINAL) ;|'
  1998. expect_red "audit reports an emitter it cannot reach, rather than skipping it" \
  1999. "never examined it" $AUD
  2000. restore_audit_sources
  2001. # 3. EmXchgAxDx was named EmMoveAxDx, which said MOV where the bytes say XCHG.
  2002. # 93h here is XCHG AX,BX - one letter away, the exact class of mistake the
  2003. # name is supposed to make impossible.
  2004. cp "$SAVED_C2" Compiler.mod
  2005. mutate Compiler.mod 's|^ Ebyte (92H)$| Ebyte (93H)|'
  2006. expect_red "audit catches XchgAxDx emitting XCHG BX,AX" \
  2007. "XchgAxDx" $AUD
  2008. restore_audit_sources
  2009. # 4. CmpArgW0's [BP+2] written as the 386 SIB form, which decodes on a 8086 as
  2010. # [SI+24h]. A real bug: the runtime was clearing the wrong memory.
  2011. cp "$SAVED_R2" Runtime.mod
  2012. mutate Runtime.mod 's| B (83H) ; B (7EH) ; B (2) ; B (0) ;| B (83H) ; B (7CH) ; B (24) ; B (0) ; B (0) ;|'
  2013. expect_red "audit catches the [SI+24h] encoding of [BP+2]" \
  2014. "memory base is 'si" $AUD
  2015. restore_audit_sources
  2016. # 5. MovAxSp is POP then PUSH, because MOV AX,[SP] does not exist on an 8086.
  2017. # Dropping the POP leaves the stack one word short - a fault in the shape,
  2018. # not in a byte value.
  2019. cp "$SAVED_C2" Compiler.mod
  2020. python3 - <<'PYEOF'
  2021. p='Compiler.mod'; s=open(p).read()
  2022. a=" Ebyte (58H) ; (* POP AX *)\n"
  2023. assert s.count(a)==1, "EmMovAxSp POP line not found -- update this mutation"
  2024. open(p,'w').write(s.replace(a, ""))
  2025. PYEOF
  2026. expect_red "audit catches MovAxSp with its POP missing" \
  2027. "MovAxSp" $AUD
  2028. restore_audit_sources
  2029. # 6. The two-instruction shape: IDIV is CWD then IDIV, and dropping the CWD
  2030. # leaves an un-sign-extended dividend in DX:AX. Both are still present as
  2031. # a two-step spec, so a missing step has to be visible.
  2032. cp "$SAVED_C2" Compiler.mod
  2033. mutate Compiler.mod 's| Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)| Ebyte (0F7H) ; Ebyte (0F9H)|'
  2034. expect_red "audit catches IDiv without the CWD that extends the dividend" \
  2035. "IDivAxCx" $AUD
  2036. restore_audit_sources
  2037. # 7. The byte under EmXorAl01's name, and the row in PATTERNS that was added
  2038. # to admit it. `34 01' is XOR AL,#01 - the boolean NOT, and the one emitter
  2039. # in Compiler.mod with no ModRM byte at all. A grammar row nobody has ever
  2040. # seen reject anything cannot be trusted to accept only the truth, so here
  2041. # is the rejection: `34 02' is the same length, decodes just as cleanly, and
  2042. # `not x' would flip bit 1 instead of bit 0.
  2043. cp "$SAVED_C2" Compiler.mod
  2044. mutate Compiler.mod 's|^ Ebyte (34H) ; Ebyte (01H)| Ebyte (34H) ; Ebyte (02H)|'
  2045. expect_red "audit catches EmXorAl01 emitting xor al,#2 under a name saying #1" \
  2046. "XorAl01" $AUD
  2047. restore_audit_sources
  2048. # 7b. The other half of that row: the opcode gate. 34h is XOR AL,#imm and
  2049. # 35h is XOR AX,#imm - a word, not a byte, under a name that says AL. The
  2050. # row is pinned to {034h} on purpose, and a pin that has never been asked
  2051. # to hold is a pin. The report is the one thing the row above cannot
  2052. # produce: with no reading at all, the helper falls out of the grammar.
  2053. cp "$SAVED_C2" Compiler.mod
  2054. mutate Compiler.mod 's|^ Ebyte (34H) ; Ebyte (01H)| Ebyte (35H) ; Ebyte (01H)|'
  2055. expect_red "the opcode gate rejects XOR AX under an XOR AL name" \
  2056. "no name pattern accepts it" $AUD
  2057. restore_audit_sources
  2058. if $AUD >/dev/null 2>&1; then
  2059. echo " ok: the audit passes on both restored sources"
  2060. pass=$((pass + 1))
  2061. else
  2062. echo "NOT RESTORED: the audit is red after restoring the sources"
  2063. $AUD 2>&1 | sed 's/^/ /'
  2064. fail=$((fail + 1))
  2065. fi
  2066. echo
  2067. echo "== the BP contract rt_exec.py checks before it starts a machine"
  2068. # wrchar and wrbool both borrowed BP to reach their argument -- [SP] is not
  2069. # encodable in 16-bit mode -- and neither saved it. The driver's cursor into
  2070. # the case record lives in BP precisely because BP is the one register an entry
  2071. # may keep, so "wrchar borrowed it and did not give it back" sent the second
  2072. # call to a garbage address, the machine triple-faulted, and the run printed the
  2073. # record header twice and hung. Both the golden and the audit call that shape
  2074. # CORRECT: the bytes are well formed, every branch is on a boundary, the size
  2075. # is unchanged, and the decode says exactly what it says. So the rule is now
  2076. # checked directly, and these two cases are what make that check more than a
  2077. # claim.
  2078. #
  2079. # rt_exec.py needs the whole runtime rebuilt and then boots 36 machines, so this
  2080. # section is the slow one. The baseline comes first and is asserted: a case
  2081. # that mutates a red tree proves nothing.
  2082. SAVED_R3=../tmp/nonvacuity.Runtime.mod.3
  2083. cp Runtime.mod "$SAVED_R3" || exit 1
  2084. if rebuild; then
  2085. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  2086. echo " ok: baseline - rt_exec.py passes on the unmutated runtime"
  2087. pass=$((pass + 1))
  2088. else
  2089. echo " FAIL: the baseline is already red, so the cases below prove"
  2090. echo " nothing - fix the baseline before reading them"
  2091. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  2092. fail=$((fail + 1))
  2093. fi
  2094. else
  2095. echo " FAIL: could not rebuild the runtime for the baseline"
  2096. fail=$((fail + 1))
  2097. fi
  2098. # 1. THE fault: drop the PUSH, exactly as EmitWrChar was written. This is the
  2099. # shape the behavioural case found, so the byte-level check must find it too
  2100. # -- a check that only the expensive test can trip is a check that has not
  2101. # been made to earn its place.
  2102. cp "$SAVED_R3" Runtime.mod
  2103. python3 - <<'PYEOF'
  2104. p = 'Runtime.mod'
  2105. s = open(p).read()
  2106. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n'
  2107. assert s.count(a) == 1, "EmitWrChar prologue not found exactly once"
  2108. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n'))
  2109. PYEOF
  2110. if rebuild; then
  2111. expect_red "the BP contract catches wrchar borrowing BP unsaved" \
  2112. "borrows BP but does not open with" python3 tests/rt_exec.py
  2113. else
  2114. echo " FAIL: the runtime would not rebuild with the PUSH removed"
  2115. fail=$((fail + 1))
  2116. fi
  2117. cp "$SAVED_R3" Runtime.mod
  2118. # 2. The mirror image: push it and never pop it. A different one-instruction
  2119. # omission with the same consequence for a caller, and the reason the rule
  2120. # asks for the 5D and not just the 55.
  2121. cp "$SAVED_R3" Runtime.mod
  2122. mutate Runtime.mod 's|^ MovSpBp ; PopBp ;$| MovSpBp ;|'
  2123. if rebuild; then
  2124. expect_red "the BP contract catches a BP that is pushed and never popped" \
  2125. "pushes BP but never pops it" python3 tests/rt_exec.py
  2126. else
  2127. echo " FAIL: the runtime would not rebuild with the POP removed"
  2128. fail=$((fail + 1))
  2129. fi
  2130. cp "$SAVED_R3" Runtime.mod
  2131. # 3. The scan's other silent failure: a rule that matches nothing looks exactly
  2132. # like a rule that passes. The pattern the check looks for is changed to one
  2133. # the blob does not contain -- MOV BP,DI, which the runtime has no reason to
  2134. # emit -- so the check has examined thirteen entries and matched nothing and
  2135. # MUST say so rather than report a clean sweep. This is the general shape of
  2136. # the fault this project keeps making: a check whose SUBJECT has drifted
  2137. # reports a confident answer about the wrong thing.
  2138. cp "$SAVED_R3" Runtime.mod
  2139. SAVED_X=../tmp/nonvacuity.rt_exec.py
  2140. cp tests/rt_exec.py "$SAVED_X" || exit 1
  2141. mutate tests/rt_exec.py 's|^MOV_BP_SP = b"\\x8b\\xec" .*$|MOV_BP_SP = b"\\x8b\\xed" # MOV BP,DI: never emitted|' \
  2142. expect_red "a check that matched nothing is a failure, not a pass" \
  2143. "matched nothing" python3 tests/rt_exec.py
  2144. cp "$SAVED_X" tests/rt_exec.py
  2145. if rebuild; then
  2146. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  2147. echo " ok: rt_exec.py passes on the restored source"
  2148. pass=$((pass + 1))
  2149. else
  2150. echo "NOT RESTORED: rt_exec.py is red after restoring Runtime.mod"
  2151. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  2152. fail=$((fail + 1))
  2153. fi
  2154. else
  2155. echo "NOT RESTORED: the runtime would not rebuild"
  2156. fail=$((fail + 1))
  2157. fi
  2158. # 4. The GOLDEN and the entry goldens, which are the other half of the same
  2159. # rule. rt_exec.py states the contract; check_runtime.py pins the bytes.
  2160. # They are separate mechanisms and the case below is what shows the golden
  2161. # one works on its own -- a re-baseline of runtime.golden would otherwise
  2162. # have absorbed the new prologues silently, and the next person to bless it
  2163. # would have no way to know the PUSH and POP were ever missing.
  2164. cp "$SAVED_R3" Runtime.mod
  2165. python3 - <<'PYEOF'
  2166. p = 'Runtime.mod'
  2167. s = open(p).read()
  2168. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n MovAlArg4 ;\n MovSpBp ; PopBp ;\n'
  2169. assert s.count(a) == 1, "EmitWrChar frame not found exactly once"
  2170. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n MovAlArg2 ;\n MovSpBp ;\n'))
  2171. PYEOF
  2172. if rebuild; then
  2173. expect_red "the entry golden catches wrchar without its PUSH BP" \
  2174. "entry wrchar starts 8B EC" python3 tests/check_runtime.py "$DUMP"
  2175. else
  2176. echo " FAIL: the runtime would not rebuild with wrchar's frame removed"
  2177. fail=$((fail + 1))
  2178. fi
  2179. cp "$SAVED_R3" Runtime.mod
  2180. echo
  2181. echo "== the .COM layout check, and the runtime size it now measures"
  2182. # The checker used to RESTATE the runtime's size as a literal. It had drifted
  2183. # from the runtime it described, so the header was read out of the code stream
  2184. # and EVERY linked fixture "failed" on it - a whole file of "failures" that were
  2185. # not findings at all. A duplicated constant that has drifted does not fail
  2186. # loudly; it re-reports the same falsehood, in which the real failures hide.
  2187. # The size is now MEASURED from the image.
  2188. #
  2189. # These cases corrupt a real emitted .COM and require the checker to notice.
  2190. # They need the images, so they are built once and copied; the checker has a
  2191. # --check-only mode for exactly this, because its scratch directory is normally
  2192. # deleted on exit and a check that has only ever seen the truth is not a check.
  2193. KEEPDIR=../tmp/nonvacuity.com
  2194. rm -rf "$KEEPDIR"
  2195. TP_COM_KEEP=1 tests/run_com_tests.sh >../tmp/nonvacuity.com.log 2>&1
  2196. KEEP=$(sed -n 's/^TP_COM_KEEP=1: images left in //p' \
  2197. ../tmp/nonvacuity.com.log | tail -1)
  2198. if [ -z "$KEEP" ] || [ ! -d "$KEEP" ]; then
  2199. echo " FAIL: could not obtain emitted .COM images for the layout cases"
  2200. fail=$((fail + 1))
  2201. else
  2202. COMCHK="tests/run_com_tests.sh --check-only"
  2203. # 0. The baseline. Every case below is a claim that a specific assertion
  2204. # turns red, and none of them means anything if the copies of untouched
  2205. # images already fail. (The stale RT_SZ produced exactly that: a whole
  2206. # file of "failures" that were not findings.) So this is asserted first,
  2207. # and a failure here is reported as a broken baseline rather than a red
  2208. # test.
  2209. #
  2210. # The baseline is also WHERE THE HEADER OFFSET COMES FROM. Cases 1 and 2
  2211. # used to carry it as the literal 435 and 439, which were ENT_SZ + the
  2212. # runtime size at the time -- 432. Two 4-byte changes to the runtime
  2213. # later, both cases were editing the wrong bytes: 435 had become four
  2214. # bytes inside the runtime itself, so case 1 had stopped testing "the
  2215. # header cannot be found" and had started testing "the checker also
  2216. # notices you scribbled on the code", and case 2 had become a no-op
  2217. # that wrote the header where it already was. Both still went red, for
  2218. # reasons the messages did not name, which is the whole problem: a
  2219. # hard-coded offset is a claim about the world that expires silently.
  2220. #
  2221. # So the offset is read back out of the checker's own report on the
  2222. # untouched images, and the expected numbers below are ARITHMETIC ON IT.
  2223. # If the runtime grows again, these cases still test what they say.
  2224. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  2225. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  2226. BASE_OUT=$($COMCHK "$KEEPDIR" 2>&1)
  2227. if [ $? -eq 0 ]; then
  2228. echo " ok: baseline - untouched copies of the real images all pass"
  2229. pass=$((pass + 1))
  2230. else
  2231. echo " FAIL: the baseline is already red, so the cases below prove"
  2232. echo " nothing - fix the baseline before reading them"
  2233. printf '%s\n' "$BASE_OUT" | grep FAIL | head -3 | sed 's/^/ /'
  2234. fail=$((fail + 1))
  2235. fi
  2236. BASE_RT=$(printf '%s\n' "$BASE_OUT" \
  2237. | sed -n 's/.*measured runtime size: \([0-9][0-9]*\) bytes.*/\1/p' \
  2238. | head -1)
  2239. if [ -z "$BASE_RT" ]; then
  2240. echo " FAIL: the checker did not report the runtime size it measured,"
  2241. echo " so the cases below cannot find the header to edit"
  2242. fail=$((fail + 1))
  2243. BASE_RT=0
  2244. fi
  2245. # The layout constants are ENT_SZ 3 and HDR_SZ 16 (tests/comimage.py), so the
  2246. # header sits at hdrOff = 3 + rtSz and the entry jump's displacement must be
  2247. # (hdrOff + 16) - 3. The wanted number is therefore computed from the header
  2248. # OFFSET, not from rtSz: the two differ by 3, and getting that backwards
  2249. # produces a plausible-looking expectation three bytes out, which is how
  2250. # this case came to expect "want 452" and then be reported as not proving
  2251. # what it said.
  2252. BASE_OFF=$((3 + BASE_RT))
  2253. # Case 2 claims a runtime four bytes LONGER, so the header - and with it the
  2254. # demanded jump target - moves four bytes further on.
  2255. SHIFTED_WANT=$((BASE_OFF + 4 + 16 - 3))
  2256. echo " (measured runtime size $BASE_RT, header at image offset $BASE_OFF)"
  2257. # 1. Break hdrDS so it no longer ties the header to its own offset. The
  2258. # header must become UNFINDABLE and be reported as such - a checker that
  2259. # fell back to a remembered offset would report a confident number here,
  2260. # which is the failure mode the measurement was introduced to remove.
  2261. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  2262. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  2263. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" <<'PYEOF'
  2264. import sys
  2265. p, off = sys.argv[1], int(sys.argv[2])
  2266. d = bytearray(open(p, 'rb').read())
  2267. d[off + 4:off + 6] = (0x1234).to_bytes(2, 'little') # hdrDS, no longer self-consistent
  2268. open(p, 'wb').write(bytes(d))
  2269. PYEOF
  2270. expect_red "a header that cannot be located is reported, not assumed" \
  2271. "no program header found" $COMCHK "$KEEPDIR"
  2272. # 2. A complete, self-consistent header four bytes later, so the measured
  2273. # runtime size becomes $((BASE_RT + 4)) instead of $BASE_RT. This is the
  2274. # positive half of the same check: the derivation must FOLLOW the file,
  2275. # and the entry jump assertion - expressed in terms of the measurement -
  2276. # must follow it too, demanding $SHIFTED_WANT rather than $((BASE_OFF + 16 - 3)).
  2277. #
  2278. # hdrCS is copied from the header already in the file rather than written
  2279. # as a literal. It used to be the literal 464+100h, which was the image
  2280. # length when the image was 720 bytes; four bytes of runtime later it was
  2281. # 464+100h against a 724-byte image, so this case was ALSO failing on
  2282. # hdrCS, for a reason three lines below the one it was written to test.
  2283. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  2284. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  2285. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" "$SHIFTED_WANT" <<'PYEOF'
  2286. import sys
  2287. p, off, want = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
  2288. d = bytearray(open(p, 'rb').read())
  2289. hdrCS = int.from_bytes(d[off + 2:off + 4], 'little') # unchanged: still the image end
  2290. off += 4
  2291. ds = off + 0x1000 + 0x100
  2292. w = [1, hdrCS, ds, ds + 4, 0, 0, 0, 0]
  2293. for i, x in enumerate(w):
  2294. d[off + 2 * i:off + 2 * i + 2] = x.to_bytes(2, 'little')
  2295. open(p, 'wb').write(bytes(d))
  2296. PYEOF
  2297. expect_red "the measured runtime size follows the image ($BASE_RT -> $((BASE_RT + 4)))" \
  2298. "want $SHIFTED_WANT" $COMCHK "$KEEPDIR"
  2299. # 3. The entry jump's opcode. One byte, and the only assertion in the
  2300. # project that can see where execution STARTS.
  2301. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  2302. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  2303. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  2304. import sys
  2305. p = sys.argv[1]
  2306. d = bytearray(open(p, 'rb').read())
  2307. d[0] = 0xEA
  2308. open(p, 'wb').write(bytes(d))
  2309. PYEOF
  2310. expect_red "the entry jump must be E9, not a near JMP" \
  2311. "not the E9 of the entry jump" $COMCHK "$KEEPDIR"
  2312. # 4. The entry jump's target, moved one instruction earlier. A .COM that
  2313. # lands in the middle of the prologue runs, prints something and exits
  2314. # cleanly, so no size or structure check can see this.
  2315. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  2316. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  2317. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  2318. import sys
  2319. p = sys.argv[1]
  2320. d = bytearray(open(p, 'rb').read())
  2321. d[1:3] = (100).to_bytes(2, 'little')
  2322. open(p, 'wb').write(bytes(d))
  2323. PYEOF
  2324. expect_red "the entry jump must land on the first instruction" \
  2325. "entry jump rel16=100" $COMCHK "$KEEPDIR"
  2326. rm -rf "$KEEPDIR"
  2327. fi
  2328. echo
  2329. echo "non-vacuity: $pass ok, $fail failed"
  2330. [ "$fail" -eq 0 ]