cli: lower scalar binary operands iteratively
This commit is contained in:
1 parent
e3f4695efe
commit
6945b511a4
5 files changed
+572
-117
No files matched your search
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Unary depth, exact lowering/fuel and rejected-value positions (Q-061)."""
|
||||
"""Operand depth, exact lowering/CFG/fuel and rejected-value positions (Q-061)."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
@@ -58,6 +58,89 @@ with tempfile.TemporaryDirectory(prefix='aji-expression-depth-') as directory:
|
||||
assert json.loads(record.read_text()) == {'status': 'returned', 'value': expected}
|
||||
print(f'unary depth: {kind} {depth}', flush=True)
|
||||
|
||||
for depth in (0, 1, 16, 1024, 16384, 65536):
|
||||
path = root / f'add-{depth}.aji'
|
||||
path.write_text('module depth\nfn main() -> i64 effects [] { return ' + ' + '.join(['1i64'] * (depth + 1)) + '; }\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
instructions = 2 * depth + 2
|
||||
assert sum(b' = ' in line or line.strip().startswith(b'ret ') for line in lowered.splitlines()) == instructions
|
||||
if depth <= 1024:
|
||||
assert json.loads(cli('run', path, '--fuel', instructions)) == {'status': 'returned', 'value': depth + 1}
|
||||
assert json.loads(cli('run', path, '--fuel', instructions - 1, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': instructions - 1, 'steps': instructions - 1}
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
assert cli('run', air, '--fuel', instructions) == cli('run', path, '--fuel', instructions)
|
||||
record = root / 'hosted.json'
|
||||
assert cli('run', path, '--fuel', instructions, '--profile', 'hosted', '--result-file', record) == b''
|
||||
assert json.loads(record.read_text()) == {'status': 'returned', 'value': depth + 1}
|
||||
print(f'binary depth: add {depth}', flush=True)
|
||||
|
||||
for depth in (0, 1, 16, 256, 1024):
|
||||
for op in ('and', 'or'):
|
||||
for short in (False, True):
|
||||
path = root / f'{op}-{short}-{depth}.aji'
|
||||
value = (op == 'and') != short
|
||||
leaf = 'true' if op == 'and' else 'false'
|
||||
text = ('true' if value else 'false') + (' ' + op + ' ' + leaf) * depth
|
||||
path.write_text('module depth\nfn main() -> bool effects [] { return ' + text + '; }\n')
|
||||
assert cli('check', path) == b''
|
||||
lowered = cli('lower', path)
|
||||
# Every node emits a short constant, cbr, RHS literal and br.
|
||||
instructions = sum(b' = ' in line or line.strip().startswith((b'ret ', b'cbr ', b'br ')) for line in lowered.splitlines())
|
||||
assert instructions == 4 * depth + 2, (op, short, depth, instructions)
|
||||
if depth <= 256:
|
||||
fuel = (2 if short else 4) * depth + 2
|
||||
assert json.loads(cli('run', path, '--fuel', fuel)) == {'status': 'returned', 'value': value}
|
||||
assert json.loads(cli('run', path, '--fuel', fuel - 1, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': fuel - 1, 'steps': fuel - 1}
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(lowered)
|
||||
assert cli('run', air, '--fuel', fuel) == cli('run', path, '--fuel', fuel)
|
||||
record = root / 'hosted.json'
|
||||
assert cli('run', path, '--fuel', fuel, '--profile', 'hosted', '--result-file', record) == b''
|
||||
assert json.loads(record.read_text()) == {'status': 'returned', 'value': value}
|
||||
print(f'binary depth: {op} short={short} {depth}', flush=True)
|
||||
|
||||
# The same lowering reaches the existing normative block limit, not a
|
||||
# host-stack failure. `lower` emits AIR; verification entry points reject it.
|
||||
limit_path = root / 'boolean-block-limit.aji'
|
||||
limit_path.write_text('module depth\nfn main() -> bool effects [] { return ' + ' and '.join(['true'] * 16385) + '; }\n')
|
||||
error = json.loads(cli('check', limit_path, code=1))
|
||||
assert (error['code'], error['kind'], error['limit'], error['max'], error['found']) == (
|
||||
'E5101', 'verifier.resource_limit', 'blocks per function', 16384, 32769), error
|
||||
assert (error['file'], error['line'], error['col']) == (str(limit_path), 2, 1), error
|
||||
limit_air = cli('lower', limit_path)
|
||||
assert limit_air.startswith(b'module depth\n')
|
||||
assert sum(line.rstrip().endswith(b':') for line in limit_air.splitlines()) == 32769
|
||||
|
||||
# The skipped operand still lowers, but its division trap must not execute.
|
||||
for op in ('and', 'or'):
|
||||
for short in (False, True):
|
||||
value = (op == 'and') != short
|
||||
path = root / f'short-trap-{op}-{short}.aji'
|
||||
path.write_text('module depth\nfn main() -> bool effects [] { return ' + ('true' if value else 'false') + ' ' + op + ' bad(); }\n'
|
||||
'fn bad() -> bool effects [] { return (1u64 / 0u64) == 0u64; }\n')
|
||||
assert cli('check', path) == b''
|
||||
air = path.with_suffix('.air')
|
||||
air.write_bytes(cli('lower', path))
|
||||
if short:
|
||||
result = cli('run', path, '--fuel', 4)
|
||||
assert json.loads(result) == {'status': 'returned', 'value': value}
|
||||
assert cli('run', air, '--fuel', 4) == result
|
||||
assert json.loads(cli('run', path, '--fuel', 3, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': 3, 'steps': 3}
|
||||
else:
|
||||
result = cli('run', path, '--fuel', 7, code=4)
|
||||
trap = json.loads(result)
|
||||
assert (trap['status'], trap['code'], trap['kind'], trap['function'], trap['inst']) == (
|
||||
'trapped', 'T0004', 'trap.division_by_zero', 'bad', 2), trap
|
||||
assert trap.pop('source_location') == {'file': str(path), 'line': 3, 'col': 39}
|
||||
assert json.loads(cli('run', air, '--fuel', 7, code=4)) == trap
|
||||
assert json.loads(cli('run', path, '--fuel', 6, code=5)) == {
|
||||
'status': 'limit_exceeded', 'kind': 'fuel', 'limit': 6, 'steps': 6}
|
||||
|
||||
for depth in (1024, 16384, 65536):
|
||||
# A cast removes the expected-use hint; deep traversal must retain
|
||||
# E7007 at the original unsuffixed initializer, without new inference.
|
||||
|
||||
Reference in new issue
Block a user