nonvacuity.sh 136 KB

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