142 lines
7.6 KiB
Python
142 lines
7.6 KiB
Python
#!/usr/bin/env python3
|
|
"""Exact supported input-context/show differential; preserve raw oracle artifacts.
|
|
|
|
External INITEX is used only by this acceptance program. Native jobs use their
|
|
own token executor. Fatal probes halt; show probes continue with status one.
|
|
"""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import re
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
HEADER = r'\catcode123=1\catcode125=2\catcode35=6\catcode94=7\catcode0=12\catcode127=12\errorcontextlines=100 '
|
|
|
|
|
|
def cases():
|
|
nested = r'\def\A{\B\relax}\def\B{\C\relax}\def\C{\bad\relax}\A\end'
|
|
for limit in [-2, -1, 0, 1, 2, 100]:
|
|
yield f'limit-{limit}', HEADER + f'\\errorcontextlines={limit}\n' + nested + '\n', {}, 'fatal'
|
|
bodies = {
|
|
'tail': r'\def\A{\B}\def\B{\bad}\A\end',
|
|
'argument': r'\def\A#1{#1\relax}\A{\bad x}\end',
|
|
'alias': r'\def\A#1x{#1\relax}\let\alias\A\alias\bad x\end',
|
|
'active': r'\catcode33=13\def!{\bad\relax}!\end',
|
|
'edef': r'\def\A{\bad x}\edef\B{\A z}\end',
|
|
'message': r'\def\A{\bad x}\message{\A z}\end',
|
|
'expanded': r'\def\A{\bad x}\expanded{\A z}\end',
|
|
'inserted': r'\unexpanded{\bad}\end',
|
|
'backed-up': r'\expandafter\bad\relax\end',
|
|
'write': r'\immediate\write16{\bad}\end',
|
|
'hyphenation': r'\def\A{\bad x}\hyphenation{\A z}\end',
|
|
'parameter-char': r'\catcode33=6\def\A!1{!1\relax}\A{\bad x}\end',
|
|
'byte-body': r'\def\A{\^^62ad ^^00 ^^7f ^^c8}\A\end',
|
|
}
|
|
for name, body in bodies.items():
|
|
yield name, HEADER + '\n' + body + '\n', {}, 'fatal'
|
|
yield 'nested-source', HEADER + '\n\\input child \\end\n', {'child': '\\def\\A{\\bad x}\n\\A\\endinput\n'}, 'fatal'
|
|
for before, after in [(0, 100), (100, 0), (100, 100)]:
|
|
yield f'crop-{before}-{after}', HEADER + '\n' + '\\relax ' * before + '\\bad ' + '\\relax ' * after + '\\end\n', {}, 'fatal'
|
|
for name, body in {
|
|
'show-raw': r'\show\relax',
|
|
'show-undefined': r'\show\unknown',
|
|
'show-macro': r'\long\def\A#1x{hello #1}\show\A',
|
|
'show-parameters': r'\catcode33=6\def\A#1!2{#1!2}\show\A',
|
|
'showthe-count': r'\count0=17\showthe\count0\relax',
|
|
'showthe-glue': r'\skip0=1pt plus2fil minus3pt\showthe\skip0\relax',
|
|
'showthe-toks': r'\toks0={\relax ## x}\showthe\toks0\relax',
|
|
}.items():
|
|
yield name, HEADER + '\n' + body + r'\message{G1_AFTER_SHOW}\end' + '\n', {}, 'show'
|
|
for online in [-1, 0, 1]:
|
|
for message in [False, True]:
|
|
body = (f'\\tracingonline={online}' + r'\setbox0=\hbox{}\showboxbreadth=20\showboxdepth=20'
|
|
+ (r'\message{BEFORE_SHOW}' if message else '') + '\n'
|
|
+ r'\showbox0\relax\message{G1_AFTER_SHOW}\end')
|
|
yield f'showbox-{online}-{int(message)}', HEADER + '\n' + body + '\n', {}, 'showbox'
|
|
yield 'errmessage', HEADER + '\n' + r'\def\A#1{\B{#1}\relax}\def\B#1{\C{#1}\relax}\def\C#1{\errmessage{AUDIT_NESTED_ERROR}}\A{X}' + '\n\\end\n', {}, 'errmessage'
|
|
mark = r'\setbox1=\vbox{\mark{\noexpand\bad x}\hbox{}}\setbox0=\vsplit1 to0pt\relax '
|
|
for limit in [-2, -1, 0, 1, 2, 100]:
|
|
yield f'split-mark-limit-{limit}', HEADER + '\n' + f'\\errorcontextlines={limit}' + mark + r'\splitfirstmark\end' + '\n', {}, 'fatal'
|
|
for name, action in {
|
|
'alias': r'\let\K=\splitbotmark\def\A{\K\relax}\A',
|
|
'message': r'\message{\splitfirstmark}',
|
|
'edef': r'\edef\A{\splitbotmark}',
|
|
}.items():
|
|
yield 'split-mark-' + name, HEADER + '\n' + mark + action + '\\end\n', {}, 'fatal'
|
|
yield 'split-marks-extended', HEADER + '\n' + r'\setbox1=\vbox{\marks32767{\noexpand\bad x}\hbox{}}\setbox0=\vsplit1 to0pt\relax\splitbotmarks32767\end' + '\n', {}, 'fatal'
|
|
long_mark = r'\setbox1=\vbox{\mark{' + r'\noexpand\relax ' * 100 + r'\noexpand\bad ' + r'\noexpand\relax ' * 100 + r'}\hbox{}}\setbox0=\vsplit1 to0pt\relax\splitfirstmark\end'
|
|
yield 'split-mark-crop', HEADER + '\n' + long_mark + '\n', {}, 'fatal'
|
|
for limit in [-2, -1, 0, 1, 2, 100]:
|
|
for name, action in {
|
|
'direct': r'\bad x',
|
|
'macro': r'\A\relax',
|
|
'message': r'\message{\bad x}',
|
|
'write': r'\immediate\write16{\bad x}',
|
|
}.items():
|
|
source = (HEADER + f'\\errorcontextlines={limit}'
|
|
+ r'\vsize=20pt\topskip=10pt\maxdepth=2pt\def\A{\bad x}'
|
|
+ f'\\output{{{action}}}\n' + r'\hbox{}\penalty-10000\end' + '\n')
|
|
yield f'output-{name}-limit-{limit}', source, {}, 'fatal-job'
|
|
|
|
|
|
def run(binary, out, item):
|
|
name, source, files, kind = item
|
|
d = out / name
|
|
d.mkdir(parents=True, exist_ok=True)
|
|
(d / 'probe.tex').write_text(source)
|
|
before = hashlib.sha256((d / 'probe.tex').read_bytes()).hexdigest()
|
|
for file, text in files.items():
|
|
(d / file).write_text(text)
|
|
pdf = d / 'native.pdf'
|
|
pdf.write_bytes(b'KEEP_EXISTING_PDF')
|
|
n = subprocess.run([str(binary), 'native', '--initex', '--etex']
|
|
+ (['--job'] if kind == 'fatal-job' else [])
|
|
+ ['probe.tex', 'native.pdf'], cwd=d, capture_output=True, text=True, timeout=30)
|
|
args = ['pdftex', '-ini', '-etex', '-interaction=nonstopmode']
|
|
if kind not in ['show', 'showbox']:
|
|
args += ['-halt-on-error']
|
|
r = subprocess.run(args + ['probe.tex'], cwd=d, capture_output=True, text=True, timeout=30)
|
|
for label, p in [('native', n), ('reference', r)]:
|
|
(d / (label + '-stdout.txt')).write_text(p.stdout)
|
|
(d / (label + '-stderr.txt')).write_text(p.stderr)
|
|
transcript = (d / 'native.log').read_text()
|
|
if kind in ['show', 'showbox']:
|
|
prefix = '> ' if kind == 'show' else '! OK'
|
|
exact = r.stdout[r.stdout.index(prefix):r.stdout.rindex('G1_AFTER_SHOW')]
|
|
reference_log = (d / 'probe.log').read_text()
|
|
exact_log = reference_log[reference_log.index(prefix):reference_log.rindex('G1_AFTER_SHOW')]
|
|
equal = exact in n.stderr and exact_log in transcript and 'error:' not in n.stderr
|
|
else:
|
|
header = '! AUDIT_NESTED_ERROR.\n' if kind == 'errmessage' else '! Undefined control sequence.\n'
|
|
exact = r.stdout.split(header, 1)[1].split('No pages of output.', 1)[0]
|
|
native_header = re.search(r'(?m)^! [^\n]*\n', n.stderr)
|
|
error = re.search(r'(?m)^error: ', n.stderr)
|
|
actual = n.stderr[native_header.end():error.start()] if native_header and error else None
|
|
equal = actual == exact and exact in transcript
|
|
result = dict(id=name, kind=kind, exit_codes=[n.returncode, r.returncode], exact_body_context_equal=equal,
|
|
pdf_protected=pdf.read_bytes() == b'KEEP_EXISTING_PDF', source_sha256=before,
|
|
source_unchanged=before == hashlib.sha256((d / 'probe.tex').read_bytes()).hexdigest())
|
|
result['passed'] = result['exit_codes'] == [1, 1] and equal and result['pdf_protected'] and result['source_unchanged']
|
|
return result
|
|
|
|
|
|
def main():
|
|
p = argparse.ArgumentParser(description=__doc__)
|
|
p.add_argument('--binary', type=Path, required=True)
|
|
p.add_argument('--output-dir', type=Path, required=True)
|
|
a = p.parse_args()
|
|
out = a.output_dir.resolve()
|
|
out.mkdir(parents=True, exist_ok=True)
|
|
results = [run(a.binary.resolve(), out, item) for item in cases()]
|
|
report = dict(passed=all(r['passed'] for r in results), cases=len(results), results=results,
|
|
scope='Exact supported body/context bytes and real status/channels; general interactive recovery, showlists and compound math diagnostics are outside G1.')
|
|
(out / 'audit.json').write_text(json.dumps(report, indent=2) + '\n')
|
|
print(json.dumps(report, indent=2))
|
|
if not report['passed']:
|
|
raise SystemExit(1)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|