nonvacuity.sh 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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
  15. # audit_helpers.py coverage: catches a helper that has silently
  16. # dropped OUT of the audit, which is a green report about
  17. # a subject nobody looked at
  18. # run_com_tests.sh the .COM layout: catches a header that cannot be
  19. # located, a runtime size that disagrees with the image,
  20. # and an entry jump that starts in the wrong place
  21. # check_runtime.py golden: catches the same thing in the built
  22. # image
  23. # check_runtime.py decode sweep: catches a wrong instruction LENGTH
  24. # check_runtime.py branch targets: catches a wrong fixup
  25. # check_runtime.py entry goldens: catches a broken prologue
  26. # probe/modrm11.py the mod=11 table: catches the ModRM column itself
  27. # going wrong, which no amount of decoding will show
  28. # check_framedisp.py the BP disp rule: catches a displacement that reads
  29. # a different address than the symbol table named
  30. # rt_exec.py the BP contract: catches an entry that borrows BP to
  31. # reach its argument and does not hand it back. The bytes
  32. # are well formed, the golden is satisfied, the audit says
  33. # every helper emits what its name says, and the machine
  34. # triple-faults on the second call - so nothing short of
  35. # running it, or of stating the register contract
  36. # explicitly, can see it.
  37. #
  38. # The mod=11 cases do not need a rebuild -- they read the probe sources
  39. # directly -- so they are cheap, and they are the ones that matter most: the
  40. # table they guard is the one thing in this project that was wrong in the
  41. # documentation while the code was right, and a table that is wrong in the
  42. # code produces bytes that decode perfectly.
  43. #
  44. # Usage: tests/nonvacuity.sh (from shell/; leaves Runtime.mod restored)
  45. set -u
  46. cd "$(dirname "$0")/.." || exit 1
  47. GM2=/home/eric/bin/Modula2/Gm2/bin/gm2
  48. SAVED=/tmp/opencode/nonvacuity.Runtime.mod
  49. PROBE=/tmp/opencode/nonvacuity.rtprobe
  50. DUMP=/tmp/opencode/nonvacuity.dump
  51. cp Runtime.mod "$SAVED" || exit 1
  52. trap 'cp "$SAVED" Runtime.mod; "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1' EXIT
  53. pass=0
  54. fail=0
  55. # mutate <file> <sed-expr> -- apply a deliberate breakage and INSIST it landed.
  56. #
  57. # Four cases in this file were already dead when first run, all the same way:
  58. # the helper they name had been renamed or reformatted since the case was
  59. # written, the sed matched nothing, the source was unchanged, and the check
  60. # correctly passed - so the harness reported "NOT NON-VACUOUS" and, worse, a
  61. # reader skimming the output could take "the check still passed" for a passing
  62. # test. A case that cannot fire is worse than no case: it is a claim of
  63. # coverage that was never tested.
  64. #
  65. # So the mutation is verified, not assumed. If the file is byte-identical
  66. # afterwards, that is reported as a FAILURE of the harness, naming the sed, and
  67. # the case is not run - because running it would only produce a meaningless
  68. # green. The message says what to do (fix the sed) rather than what it found.
  69. mutate () {
  70. mf=$1
  71. msed=$2
  72. cp "$mf" /tmp/opencode/nonvacuity.mut.bak
  73. sed -i "$msed" "$mf"
  74. if cmp -s "$mf" /tmp/opencode/nonvacuity.mut.bak; then
  75. echo " BROKEN CASE: the mutation did not change $mf"
  76. echo " sed: $msed"
  77. echo " the named code has probably been renamed or reformatted -"
  78. echo " fix this case, it is asserting nothing"
  79. fail=$((fail + 1))
  80. return 1
  81. fi
  82. return 0
  83. }
  84. # rebuild <label> -- re-emit the runtime and dump it
  85. rebuild () {
  86. "$GM2" -fiso -c Runtime.mod >/dev/null 2>&1 || return 1
  87. "$GM2" -fiso -o "$PROBE" tests/RtProbe.mod Runtime.o Posix.o \
  88. >/dev/null 2>&1 || return 1
  89. "$PROBE" > "$DUMP" || return 1
  90. return 0
  91. }
  92. # expect_red <label> <pattern> <checker-cmd...>
  93. # <pattern> is a grep the failure output must match, so a check cannot
  94. # "pass" by failing for some unrelated reason.
  95. expect_red () {
  96. label=$1
  97. want=$2
  98. shift 2
  99. if out=$("$@" 2>&1); then
  100. echo "NOT NON-VACUOUS: $label -- the check still passed"
  101. fail=$((fail + 1))
  102. elif ! printf '%s\n' "$out" | grep -qi "$want"; then
  103. echo "WRONG FAILURE: $label -- went red, but not for the stated reason"
  104. printf '%s\n' "$out" | sed 's/^/ /'
  105. fail=$((fail + 1))
  106. else
  107. echo " ok: $label"
  108. printf '%s\n' "$out" | grep -im1 "$want" | sed 's/^/ /'
  109. pass=$((pass + 1))
  110. fi
  111. }
  112. echo "== each mutation must turn the named check red"
  113. echo
  114. # --- 1. name-versus-decode -------------------------------------------
  115. # MovSiBx was `89 DC`, which is MOV SP,BX. Two bytes either way, decodes
  116. # cleanly, and no structural check can see it.
  117. cp "$SAVED" Runtime.mod
  118. mutate Runtime.mod 's|B (0DEH) END MovSiBx|B (0DCH) END MovSiBx|'
  119. expect_red "audit_helpers catches MovSiBx emitting MOV SP,BX" \
  120. "MovSiBx" python3 tests/audit_helpers.py
  121. # CmpSiBx had the identical mistake, which is how you know a single fix is
  122. # not enough -- the same misreading was written twice.
  123. cp "$SAVED" Runtime.mod
  124. mutate Runtime.mod 's|B (39H) ; B (0DEH) END CmpSiBx|B (39H) ; B (0DCH) END CmpSiBx|'
  125. expect_red "audit_helpers catches CmpSiBx emitting CMP SP,BX" \
  126. "CmpSiBx" python3 tests/audit_helpers.py
  127. # --- 2. golden, and entry goldens ------------------------------------
  128. # MovDlAl was `88 C0` = MOV AL,AL instead of MOV DL,AL. This is the case that
  129. # motivated runtime.golden: the sweep stayed in sync, every branch target
  130. # stayed on a boundary, no entry's first bytes moved, and the size did not
  131. # change. The target helper was MovAlDh when this case was written, which is
  132. # the fourth way a case here can rot - see the note on `mutate` below.
  133. cp "$SAVED" Runtime.mod
  134. mutate Runtime.mod 's|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C2H)|PROCEDURE MovDlAl ; BEGIN B (88H) ; B (0C0H)|'
  135. rebuild
  136. expect_red "runtime.golden catches MOV AL,AL" \
  137. "mov al,al" python3 tests/check_runtime.py "$DUMP"
  138. # initmem opened with the mis-emitted MovSiAx, so its entry golden was the
  139. # thing that noticed the prologue was a no-op.
  140. cp "$SAVED" Runtime.mod
  141. mutate Runtime.mod 's|B (0F0H) END MovSiAx|B (0C0H) END MovSiAx|'
  142. rebuild
  143. expect_red "check_runtime catches a broken initmem prologue" \
  144. "mov ax,ax" python3 tests/check_runtime.py "$DUMP"
  145. # --- 3. decode sweep / length ----------------------------------------
  146. # StDiDl was `88 97` = [BX+disp16],DL: mod=10, so the instruction needs a
  147. # disp16 it was not given, and the sweep loses sync two bytes later.
  148. cp "$SAVED" Runtime.mod
  149. mutate Runtime.mod 's|PROCEDURE StDiDl ; BEGIN B (88H) ; B (15H)|PROCEDURE StDiDl ; BEGIN B (88H) ; B (97H)|'
  150. rebuild
  151. expect_red "decode sweep catches a mod=10 byte move with no displacement" \
  152. "mov byte ptr \[bx+5b5fh\],dl" python3 tests/check_runtime.py "$DUMP"
  153. # --- 4. branch targets ------------------------------------------------
  154. # FixUp measures a rel8 from the end of the instruction, one byte past the
  155. # displacement field. Drop the +1 and every short branch lands one byte into
  156. # its target, which for a 3-byte instruction means the middle of it. The
  157. # bytes themselves are all perfectly well formed -- only the fixups are
  158. # wrong -- so this is the one failure mode the golden cannot be expected to
  159. # catch on its own.
  160. cp "$SAVED" Runtime.mod
  161. mutate Runtime.mod 's|rel := (t + 100H - (fix \[i\].place + 1)) MOD 100H|rel := (t + 100H - fix [i].place) MOD 100H|'
  162. rebuild
  163. expect_red "branch check catches rel8 fixups measured from the wrong byte" \
  164. "not an instruction boundary" \
  165. python3 tests/check_runtime.py "$DUMP"
  166. echo
  167. echo "== everything restored and green again"
  168. cp "$SAVED" Runtime.mod
  169. if rebuild; then
  170. if python3 tests/audit_helpers.py >/dev/null 2>&1 &&
  171. python3 tests/check_runtime.py "$DUMP" >/dev/null 2>&1; then
  172. echo " ok: both checks pass on the restored source"
  173. pass=$((pass + 1))
  174. else
  175. echo "NOT RESTORED: a check is red after restoring Runtime.mod"
  176. fail=$((fail + 1))
  177. fi
  178. else
  179. echo "NOT RESTORED: the runtime would not rebuild"
  180. fail=$((fail + 1))
  181. fi
  182. echo
  183. echo "== the mod=11 table (probe/modrm11.py)"
  184. # These mutate the probe's own sources, not the runtime, so there is no
  185. # rebuild in the loop. SAVED_PY / SAVED_S are restored after each case.
  186. SAVED_PY=/tmp/opencode/nonvacuity.modrm11.py
  187. SAVED_S=/tmp/opencode/nonvacuity.modrm11.s
  188. cp tests/probe/modrm11.py "$SAVED_PY" || exit 1
  189. cp tests/probe/modrm11.s "$SAVED_S" || exit 1
  190. M11="python3 tests/probe/modrm11.py"
  191. restore_probe () {
  192. cp "$SAVED_PY" tests/probe/modrm11.py
  193. cp "$SAVED_S" tests/probe/modrm11.s
  194. }
  195. # 1. one cell of the table moved
  196. mutate tests/probe/modrm11.py 's|"Si", "Di"\]$|"Bp", "Di"]|'
  197. expect_red "anchor pins a moved table cell" \
  198. "anchor ADD SI, 2" $M11
  199. restore_probe
  200. # 2. the table this project actually shipped: AX dropped off the front and a
  201. # duplicate BX invented at the end, which shifts every code down by one
  202. mutate tests/probe/modrm11.py 's|^REG = .*$|REG = ["Cx", "Dx", "Bx", "Sp", "Bp", "Si", "Di", "Bx"]|'
  203. expect_red "the table shifted by one (AX dropped, BX duplicated)" \
  204. "anchor MOV SP, BP" $M11
  205. restore_probe
  206. # 3. the .s edited to contradict the table. This is the case that shows why
  207. # the hard-coded EXPECT bytes exist: the assembler encodes the new claim
  208. # correctly, so comparing the .s against `as` alone can never fail here.
  209. mutate tests/probe/modrm11.s 's|movw %sp, %di # reg 100|movw %bp, %di # reg 100|'
  210. expect_red "probe source edited away from the recorded bytes" \
  211. "expected 89 E7" $M11
  212. restore_probe
  213. # 4. the 8-bit list edited, which is a different table from the word one
  214. mutate tests/probe/modrm11.s 's|movb %al, %dl # 88 C2 -> DL := AL|movb %al, %bl # was DL|'
  215. expect_red "the 8-bit register list edited" \
  216. "expected 88 C2" $M11
  217. restore_probe
  218. # 5. an anchor's recorded byte corrupted, so the anchor can no longer
  219. # corroborate itself
  220. mutate tests/probe/modrm11.py 's|"8B EC", "8B E5"|"8B ED", "8B E5"|'
  221. expect_red "anchor byte no longer matches the emitted code" \
  222. "expected 8B ED" $M11
  223. restore_probe
  224. if $M11 >/dev/null 2>&1; then
  225. echo " ok: modrm11.py passes on the restored probe sources"
  226. pass=$((pass + 1))
  227. else
  228. echo "NOT RESTORED: modrm11.py is red after restoring its sources"
  229. $M11 2>&1 | sed 's/^/ /'
  230. fail=$((fail + 1))
  231. fi
  232. echo
  233. echo "== the BP displacement rule (check_framedisp.py)"
  234. # This one is about Compiler.mod rather than the runtime, and it needs the
  235. # whole toolchain rebuilt (comtest, not rtprobe), so it gets its own rebuild.
  236. SAVED_C=/tmp/opencode/nonvacuity.Compiler.mod
  237. cp Compiler.mod "$SAVED_C" || exit 1
  238. rebuild_compiler () {
  239. $GM2 -fiso -c Compiler.mod >/dev/null 2>&1 || return 1
  240. $GM2 -fiso -fgen-module-list=tests/ct.lst -o /dev/null \
  241. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  242. >/dev/null 2>&1
  243. $GM2 -fiso -fuse-list=tests/ct.lst -o comtest \
  244. tests/ComTest.mod TextBuf.o Posix.o Compiler.o Runtime.o Linker.o \
  245. >/dev/null 2>&1 || return 1
  246. return 0
  247. }
  248. # 1. the original bug: `off MOD 100H`, always disp8. Restores exactly the code
  249. # that was there before EmBpDisp existed. t28's [BP+128] read becomes
  250. # [BP-128], which is the failure this whole check is named after.
  251. python3 - "$SAVED_C" <<'PYEOF'
  252. import sys
  253. p = 'Compiler.mod'
  254. s = open(p).read()
  255. old = """BEGIN
  256. IF off <= 127 THEN
  257. Ebyte (46H) ; Ebyte (VAL (BYTE, off))
  258. ELSE
  259. Ebyte (86H) ; Eword (off)
  260. END
  261. END EmBpDisp ;"""
  262. new = """VAR disp : CARDINAL ;
  263. BEGIN
  264. disp := off MOD 100H ;
  265. Ebyte (46H) ; Ebyte (VAL (BYTE, disp))
  266. END EmBpDisp ;"""
  267. assert old in s, "EmBpDisp body not found -- update this mutation"
  268. open(p, 'w').write(s.replace(old, new))
  269. PYEOF
  270. if rebuild_compiler; then
  271. expect_red "displacement truncation reads a different address" \
  272. "no 8B access at \[BP+128\]" python3 tests/check_framedisp.py
  273. else
  274. echo " FAIL: the compiler would not rebuild with the truncation"
  275. fail=$((fail + 1))
  276. fi
  277. cp "$SAVED_C" Compiler.mod
  278. # 2. the other half of the rule: always use the 4-byte form, ignoring the
  279. # <= 127 case. This is over-cautious rather than wrong, so the checker must
  280. # still be happy -- which is worth asserting, because a check that only
  281. # ever fails on a smaller encoding is a check that pins one answer instead
  282. # of the rule.
  283. python3 - <<'PYEOF'
  284. p = 'Compiler.mod'
  285. s = open(p).read()
  286. old = """ IF off <= 127 THEN
  287. Ebyte (46H) ; Ebyte (VAL (BYTE, off))
  288. ELSE
  289. Ebyte (86H) ; Eword (off)
  290. END"""
  291. new = """ Ebyte (86H) ; Eword (off)"""
  292. assert old in s, "EmBpDisp branch not found -- update this mutation"
  293. open(p, 'w').write(s.replace(old, new))
  294. PYEOF
  295. if rebuild_compiler; then
  296. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  297. echo " ok: always-disp16 is accepted, so the check pins the rule"
  298. echo " and not one particular encoding"
  299. pass=$((pass + 1))
  300. else
  301. echo " FAIL: check_framedisp rejects a safe, over-long encoding"
  302. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  303. fail=$((fail + 1))
  304. fi
  305. else
  306. echo " FAIL: the compiler would not rebuild with always-disp16"
  307. fail=$((fail + 1))
  308. fi
  309. cp "$SAVED_C" Compiler.mod
  310. if rebuild_compiler; then
  311. if python3 tests/check_framedisp.py >/dev/null 2>&1; then
  312. echo " ok: check_framedisp passes on the restored source"
  313. pass=$((pass + 1))
  314. else
  315. echo "NOT RESTORED: check_framedisp is red after restoring Compiler.mod"
  316. python3 tests/check_framedisp.py 2>&1 | sed 's/^/ /'
  317. fail=$((fail + 1))
  318. fi
  319. else
  320. echo "NOT RESTORED: the compiler would not rebuild"
  321. fail=$((fail + 1))
  322. fi
  323. echo
  324. echo "== the emitter-name audit of Compiler.mod (audit_helpers.py)"
  325. # These need no rebuild: the audit reads the SOURCE, not the built object, so
  326. # they are the cheapest cases here and they cover the module the audit used
  327. # not to look at at all. That is the point of the section: the audit reported
  328. # "every helper agrees with its name" for a module it had never examined, and
  329. # EmXchgAxCx was `93` (XCHG BX,AX) under a name that says XCHG AX,CX for the
  330. # whole life of the project. Two of these five are for faults that were real.
  331. SAVED_C2=/tmp/opencode/nonvacuity.Compiler.mod.2
  332. SAVED_R2=/tmp/opencode/nonvacuity.Runtime.mod.2
  333. cp Compiler.mod "$SAVED_C2" || exit 1
  334. cp Runtime.mod "$SAVED_R2" || exit 1
  335. restore_audit_sources () {
  336. cp "$SAVED_C2" Compiler.mod
  337. cp "$SAVED_R2" Runtime.mod
  338. }
  339. AUD="python3 tests/audit_helpers.py"
  340. # 1. THE fault. 91h is XCHG AX,CX; 93h is XCHG BX,AX. Both are one byte, so
  341. # the compile matrix never moved and the byte counts never moved.
  342. cp "$SAVED_C2" Compiler.mod
  343. mutate Compiler.mod 's|^ Ebyte (91H)$| Ebyte (93H)|'
  344. expect_red "audit catches XchgAxCx emitting XCHG BX,AX" \
  345. "exchanges Ax and Bx" $AUD
  346. restore_audit_sources
  347. # 2. the coverage check itself. A parameter list that find_helpers does not
  348. # accept is exactly how the real emitter was missed, and the inventory is
  349. # scanned separately on purpose so this can be caught. Without the
  350. # independent scan this case is silent, because both lists would come from
  351. # the same parser and agree that the helper does not exist.
  352. cp "$SAVED_C2" Compiler.mod
  353. mutate Compiler.mod 's|^PROCEDURE EmXchgAxCx () ;$|PROCEDURE EmXchgAxCx (why : CARDINAL) ;|'
  354. expect_red "audit reports an emitter it cannot reach, rather than skipping it" \
  355. "never examined it" $AUD
  356. restore_audit_sources
  357. # 3. EmXchgAxDx was named EmMoveAxDx, which said MOV where the bytes say XCHG.
  358. # 93h here is XCHG AX,BX - one letter away, the exact class of mistake the
  359. # name is supposed to make impossible.
  360. cp "$SAVED_C2" Compiler.mod
  361. mutate Compiler.mod 's|^ Ebyte (92H)$| Ebyte (93H)|'
  362. expect_red "audit catches XchgAxDx emitting XCHG BX,AX" \
  363. "XchgAxDx" $AUD
  364. restore_audit_sources
  365. # 4. CmpArgW0's [BP+2] written as the 386 SIB form, which decodes on a 8086 as
  366. # [SI+24h]. A real bug: the runtime was clearing the wrong memory.
  367. cp "$SAVED_R2" Runtime.mod
  368. mutate Runtime.mod 's| B (83H) ; B (7EH) ; B (2) ; B (0) ;| B (83H) ; B (7CH) ; B (24) ; B (0) ; B (0) ;|'
  369. expect_red "audit catches the [SI+24h] encoding of [BP+2]" \
  370. "memory base is 'si" $AUD
  371. restore_audit_sources
  372. # 5. MovAxSp is POP then PUSH, because MOV AX,[SP] does not exist on an 8086.
  373. # Dropping the POP leaves the stack one word short - a fault in the shape,
  374. # not in a byte value.
  375. cp "$SAVED_C2" Compiler.mod
  376. python3 - <<'PYEOF'
  377. p='Compiler.mod'; s=open(p).read()
  378. a=" Ebyte (58H) ; (* POP AX *)\n"
  379. assert s.count(a)==1, "EmMovAxSp POP line not found -- update this mutation"
  380. open(p,'w').write(s.replace(a, ""))
  381. PYEOF
  382. expect_red "audit catches MovAxSp with its POP missing" \
  383. "MovAxSp" $AUD
  384. restore_audit_sources
  385. # 6. The two-instruction shape: IDIV is CWD then IDIV, and dropping the CWD
  386. # leaves an un-sign-extended dividend in DX:AX. Both are still present as
  387. # a two-step spec, so a missing step has to be visible.
  388. cp "$SAVED_C2" Compiler.mod
  389. mutate Compiler.mod 's| Ebyte (99H) ; Ebyte (0F7H) ; Ebyte (0F9H)| Ebyte (0F7H) ; Ebyte (0F9H)|'
  390. expect_red "audit catches IDiv without the CWD that extends the dividend" \
  391. "IDivAxCx" $AUD
  392. restore_audit_sources
  393. if $AUD >/dev/null 2>&1; then
  394. echo " ok: the audit passes on both restored sources"
  395. pass=$((pass + 1))
  396. else
  397. echo "NOT RESTORED: the audit is red after restoring the sources"
  398. $AUD 2>&1 | sed 's/^/ /'
  399. fail=$((fail + 1))
  400. fi
  401. echo
  402. echo "== the BP contract rt_exec.py checks before it starts a machine"
  403. # wrchar and wrbool both borrowed BP to reach their argument -- [SP] is not
  404. # encodable in 16-bit mode -- and neither saved it. The driver's cursor into
  405. # the case record lives in BP precisely because BP is the one register an entry
  406. # may keep, so "wrchar borrowed it and did not give it back" sent the second
  407. # call to a garbage address, the machine triple-faulted, and the run printed the
  408. # record header twice and hung. Both the golden and the audit call that shape
  409. # CORRECT: the bytes are well formed, every branch is on a boundary, the size
  410. # is unchanged, and the decode says exactly what it says. So the rule is now
  411. # checked directly, and these two cases are what make that check more than a
  412. # claim.
  413. #
  414. # rt_exec.py needs the whole runtime rebuilt and then boots 36 machines, so this
  415. # section is the slow one. The baseline comes first and is asserted: a case
  416. # that mutates a red tree proves nothing.
  417. SAVED_R3=/tmp/opencode/nonvacuity.Runtime.mod.3
  418. cp Runtime.mod "$SAVED_R3" || exit 1
  419. if rebuild; then
  420. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  421. echo " ok: baseline - rt_exec.py passes on the unmutated runtime"
  422. pass=$((pass + 1))
  423. else
  424. echo " FAIL: the baseline is already red, so the cases below prove"
  425. echo " nothing - fix the baseline before reading them"
  426. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  427. fail=$((fail + 1))
  428. fi
  429. else
  430. echo " FAIL: could not rebuild the runtime for the baseline"
  431. fail=$((fail + 1))
  432. fi
  433. # 1. THE fault: drop the PUSH, exactly as EmitWrChar was written. This is the
  434. # shape the behavioural case found, so the byte-level check must find it too
  435. # -- a check that only the expensive test can trip is a check that has not
  436. # been made to earn its place.
  437. cp "$SAVED_R3" Runtime.mod
  438. python3 - <<'PYEOF'
  439. p = 'Runtime.mod'
  440. s = open(p).read()
  441. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n'
  442. assert s.count(a) == 1, "EmitWrChar prologue not found exactly once"
  443. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n'))
  444. PYEOF
  445. if rebuild; then
  446. expect_red "the BP contract catches wrchar borrowing BP unsaved" \
  447. "borrows BP but does not open with" python3 tests/rt_exec.py
  448. else
  449. echo " FAIL: the runtime would not rebuild with the PUSH removed"
  450. fail=$((fail + 1))
  451. fi
  452. cp "$SAVED_R3" Runtime.mod
  453. # 2. The mirror image: push it and never pop it. A different one-instruction
  454. # omission with the same consequence for a caller, and the reason the rule
  455. # asks for the 5D and not just the 55.
  456. cp "$SAVED_R3" Runtime.mod
  457. mutate Runtime.mod 's|^ MovSpBp ; PopBp ;$| MovSpBp ;|'
  458. if rebuild; then
  459. expect_red "the BP contract catches a BP that is pushed and never popped" \
  460. "pushes BP but never pops it" python3 tests/rt_exec.py
  461. else
  462. echo " FAIL: the runtime would not rebuild with the POP removed"
  463. fail=$((fail + 1))
  464. fi
  465. cp "$SAVED_R3" Runtime.mod
  466. # 3. The scan's other silent failure: a rule that matches nothing looks exactly
  467. # like a rule that passes. The pattern the check looks for is changed to one
  468. # the blob does not contain -- MOV BP,DI, which the runtime has no reason to
  469. # emit -- so the check has examined thirteen entries and matched nothing and
  470. # MUST say so rather than report a clean sweep. This is the general shape of
  471. # the fault this project keeps making: a check whose SUBJECT has drifted
  472. # reports a confident answer about the wrong thing.
  473. cp "$SAVED_R3" Runtime.mod
  474. SAVED_X=/tmp/opencode/nonvacuity.rt_exec.py
  475. cp tests/rt_exec.py "$SAVED_X" || exit 1
  476. mutate tests/rt_exec.py 's|^MOV_BP_SP = b"\\x8b\\xec" .*$|MOV_BP_SP = b"\\x8b\\xed" # MOV BP,DI: never emitted|' \
  477. expect_red "a check that matched nothing is a failure, not a pass" \
  478. "matched nothing" python3 tests/rt_exec.py
  479. cp "$SAVED_X" tests/rt_exec.py
  480. if rebuild; then
  481. if python3 tests/rt_exec.py >/dev/null 2>&1; then
  482. echo " ok: rt_exec.py passes on the restored source"
  483. pass=$((pass + 1))
  484. else
  485. echo "NOT RESTORED: rt_exec.py is red after restoring Runtime.mod"
  486. python3 tests/rt_exec.py 2>&1 | tail -3 | sed 's/^/ /'
  487. fail=$((fail + 1))
  488. fi
  489. else
  490. echo "NOT RESTORED: the runtime would not rebuild"
  491. fail=$((fail + 1))
  492. fi
  493. # 4. The GOLDEN and the entry goldens, which are the other half of the same
  494. # rule. rt_exec.py states the contract; check_runtime.py pins the bytes.
  495. # They are separate mechanisms and the case below is what shows the golden
  496. # one works on its own -- a re-baseline of runtime.golden would otherwise
  497. # have absorbed the new prologues silently, and the next person to bless it
  498. # would have no way to know the PUSH and POP were ever missing.
  499. cp "$SAVED_R3" Runtime.mod
  500. python3 - <<'PYEOF'
  501. p = 'Runtime.mod'
  502. s = open(p).read()
  503. a = ' M ("wrchar") ;\n PushBp ; MovBpSp ;\n MovAlArg4 ;\n MovSpBp ; PopBp ;\n'
  504. assert s.count(a) == 1, "EmitWrChar frame not found exactly once"
  505. open(p, 'w').write(s.replace(a, ' M ("wrchar") ;\n MovBpSp ;\n MovAlArg2 ;\n MovSpBp ;\n'))
  506. PYEOF
  507. if rebuild; then
  508. expect_red "the entry golden catches wrchar without its PUSH BP" \
  509. "entry wrchar starts 8B EC" python3 tests/check_runtime.py "$DUMP"
  510. else
  511. echo " FAIL: the runtime would not rebuild with wrchar's frame removed"
  512. fail=$((fail + 1))
  513. fi
  514. cp "$SAVED_R3" Runtime.mod
  515. echo
  516. echo "== the .COM layout check, and the runtime size it now measures"
  517. # The checker used to RESTATE the runtime's size as a literal. It was wrong
  518. # by 41 bytes for an unknown time, and every one of the 30 .COM files "failed"
  519. # on a header read out of the code stream. A duplicated constant that has
  520. # drifted does not fail loudly; it re-reports the same falsehood, in which the
  521. # real failures hide. The size is now MEASURED from the image.
  522. #
  523. # These cases corrupt a real emitted .COM and require the checker to notice.
  524. # They need the images, so they are built once and copied; the checker has a
  525. # --check-only mode for exactly this, because its scratch directory is normally
  526. # deleted on exit and a check that has only ever seen the truth is not a check.
  527. KEEPDIR=/tmp/opencode/nonvacuity.com
  528. rm -rf "$KEEPDIR"
  529. TP_COM_KEEP=1 tests/run_com_tests.sh >/tmp/opencode/nonvacuity.com.log 2>&1
  530. KEEP=$(sed -n 's/^TP_COM_KEEP=1: images left in //p' \
  531. /tmp/opencode/nonvacuity.com.log | tail -1)
  532. if [ -z "$KEEP" ] || [ ! -d "$KEEP" ]; then
  533. echo " FAIL: could not obtain emitted .COM images for the layout cases"
  534. fail=$((fail + 1))
  535. else
  536. COMCHK="tests/run_com_tests.sh --check-only"
  537. # 0. The baseline. Every case below is a claim that a specific assertion
  538. # turns red, and none of them means anything if the copies of untouched
  539. # images already fail. (The stale RT_SZ produced exactly that: 30
  540. # failures that were not findings.) So this is asserted first, and a
  541. # failure here is reported as a broken baseline rather than a red test.
  542. #
  543. # The baseline is also WHERE THE HEADER OFFSET COMES FROM. Cases 1 and 2
  544. # used to carry it as the literal 435 and 439, which were ENT_SZ + the
  545. # runtime size at the time -- 432. Two 4-byte changes to the runtime
  546. # later, both cases were editing the wrong bytes: 435 had become four
  547. # bytes inside the runtime itself, so case 1 had stopped testing "the
  548. # header cannot be found" and had started testing "the checker also
  549. # notices you scribbled on the code", and case 2 had become a no-op
  550. # that wrote the header where it already was. Both still went red, for
  551. # reasons the messages did not name, which is the whole problem: a
  552. # hard-coded offset is a claim about the world that expires silently.
  553. #
  554. # So the offset is read back out of the checker's own report on the
  555. # untouched images, and the expected numbers below are ARITHMETIC ON IT.
  556. # If the runtime grows again, these cases still test what they say.
  557. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  558. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  559. BASE_OUT=$($COMCHK "$KEEPDIR" 2>&1)
  560. if [ $? -eq 0 ]; then
  561. echo " ok: baseline - untouched copies of the real images all pass"
  562. pass=$((pass + 1))
  563. else
  564. echo " FAIL: the baseline is already red, so the cases below prove"
  565. echo " nothing - fix the baseline before reading them"
  566. printf '%s\n' "$BASE_OUT" | grep FAIL | head -3 | sed 's/^/ /'
  567. fail=$((fail + 1))
  568. fi
  569. BASE_RT=$(printf '%s\n' "$BASE_OUT" \
  570. | sed -n 's/.*measured runtime size: \([0-9][0-9]*\) bytes.*/\1/p' \
  571. | head -1)
  572. if [ -z "$BASE_RT" ]; then
  573. echo " FAIL: the checker did not report the runtime size it measured,"
  574. echo " so the cases below cannot find the header to edit"
  575. fail=$((fail + 1))
  576. BASE_RT=0
  577. fi
  578. # The layout constants are ENT_SZ 3 and HDR_SZ 16 (run_com_tests.sh), so the
  579. # header sits at hdrOff = 3 + rtSz and the entry jump's displacement must be
  580. # (hdrOff + 16) - 3. The wanted number is therefore computed from the header
  581. # OFFSET, not from rtSz: the two differ by 3, and getting that backwards
  582. # produces a plausible-looking expectation three bytes out, which is how
  583. # this case came to expect "want 452" and then be reported as not proving
  584. # what it said.
  585. BASE_OFF=$((3 + BASE_RT))
  586. # Case 2 claims a runtime four bytes LONGER, so the header - and with it the
  587. # demanded jump target - moves four bytes further on.
  588. SHIFTED_WANT=$((BASE_OFF + 4 + 16 - 3))
  589. echo " (measured runtime size $BASE_RT, header at image offset $BASE_OFF)"
  590. # 1. Break hdrDS so it no longer ties the header to its own offset. The
  591. # header must become UNFINDABLE and be reported as such - a checker that
  592. # fell back to a remembered offset would report a confident number here,
  593. # which is the failure mode the measurement was introduced to remove.
  594. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  595. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  596. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" <<'PYEOF'
  597. import sys
  598. p, off = sys.argv[1], int(sys.argv[2])
  599. d = bytearray(open(p, 'rb').read())
  600. d[off + 4:off + 6] = (0x1234).to_bytes(2, 'little') # hdrDS, no longer self-consistent
  601. open(p, 'wb').write(bytes(d))
  602. PYEOF
  603. expect_red "a header that cannot be located is reported, not assumed" \
  604. "no program header found" $COMCHK "$KEEPDIR"
  605. # 2. A complete, self-consistent header four bytes later, so the measured
  606. # runtime size becomes $((BASE_RT + 4)) instead of $BASE_RT. This is the
  607. # positive half of the same check: the derivation must FOLLOW the file,
  608. # and the entry jump assertion - expressed in terms of the measurement -
  609. # must follow it too, demanding $SHIFTED_WANT rather than $((BASE_OFF + 16 - 3)).
  610. #
  611. # hdrCS is copied from the header already in the file rather than written
  612. # as a literal. It used to be the literal 464+100h, which was the image
  613. # length when the image was 720 bytes; four bytes of runtime later it was
  614. # 464+100h against a 724-byte image, so this case was ALSO failing on
  615. # hdrCS, for a reason three lines below the one it was written to test.
  616. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  617. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  618. python3 - "$KEEPDIR/t01_minimal.COM" "$BASE_OFF" "$SHIFTED_WANT" <<'PYEOF'
  619. import sys
  620. p, off, want = sys.argv[1], int(sys.argv[2]), int(sys.argv[3])
  621. d = bytearray(open(p, 'rb').read())
  622. hdrCS = int.from_bytes(d[off + 2:off + 4], 'little') # unchanged: still the image end
  623. off += 4
  624. ds = off + 0x1000 + 0x100
  625. w = [1, hdrCS, ds, ds + 4, 0, 0, 0, 0]
  626. for i, x in enumerate(w):
  627. d[off + 2 * i:off + 2 * i + 2] = x.to_bytes(2, 'little')
  628. open(p, 'wb').write(bytes(d))
  629. PYEOF
  630. expect_red "the measured runtime size follows the image ($BASE_RT -> $((BASE_RT + 4)))" \
  631. "want $SHIFTED_WANT" $COMCHK "$KEEPDIR"
  632. # 3. The entry jump's opcode. One byte, and the only assertion in the
  633. # project that can see where execution STARTS.
  634. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  635. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  636. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  637. import sys
  638. p = sys.argv[1]
  639. d = bytearray(open(p, 'rb').read())
  640. d[0] = 0xEA
  641. open(p, 'wb').write(bytes(d))
  642. PYEOF
  643. expect_red "the entry jump must be E9, not a near JMP" \
  644. "not the E9 of the entry jump" $COMCHK "$KEEPDIR"
  645. # 4. The entry jump's target, moved one instruction earlier. A .COM that
  646. # lands in the middle of the prologue runs, prints something and exits
  647. # cleanly, so no size or structure check can see this.
  648. rm -rf "$KEEPDIR"; mkdir -p "$KEEPDIR"
  649. cp "$KEEP"/*.COM "$KEEP"/raw.txt "$KEEPDIR"/
  650. python3 - "$KEEPDIR/t01_minimal.COM" <<'PYEOF'
  651. import sys
  652. p = sys.argv[1]
  653. d = bytearray(open(p, 'rb').read())
  654. d[1:3] = (100).to_bytes(2, 'little')
  655. open(p, 'wb').write(bytes(d))
  656. PYEOF
  657. expect_red "the entry jump must land on the first instruction" \
  658. "entry jump rel16=100" $COMCHK "$KEEPDIR"
  659. rm -rf "$KEEPDIR"
  660. fi
  661. echo
  662. echo "non-vacuity: $pass ok, $fail failed"
  663. [ "$fail" -eq 0 ]