nonvacuity.sh 126 KB

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