nonvacuity.sh 138 KB

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