nonvacuity.sh 135 KB

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