-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtest_prove.py
350 lines (279 loc) · 10.8 KB
/
test_prove.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
from __future__ import annotations
import logging
import sys
from typing import TYPE_CHECKING, NamedTuple
import pytest
from filelock import SoftFileLock
from pyk.prelude.ml import is_top
from pyk.proof.reachability import APRProof
from kevm_pyk import config
from kevm_pyk.__main__ import exec_prove
from kevm_pyk.cli import ProveOptions
from kevm_pyk.kevm import KEVM
from kevm_pyk.kompile import KompileTarget, kevm_kompile
from ..utils import REPO_ROOT
if TYPE_CHECKING:
from collections.abc import Callable
from pathlib import Path
from typing import Any, Final
from pyk.utils import BugReport
from pytest import LogCaptureFixture, TempPathFactory
sys.setrecursionlimit(10**8)
TEST_DIR: Final = REPO_ROOT / 'tests'
SPEC_DIR: Final = TEST_DIR / 'specs'
# -------------------
# Test specifications
# -------------------
def spec_files(dir_name: str, glob: str) -> tuple[Path, ...]:
test_dir = SPEC_DIR / dir_name
res = tuple(test_dir.glob(glob))
assert res
return res
BENCHMARK_TESTS: Final = spec_files('benchmarks', '*-spec.k')
FUNCTIONAL_TESTS: Final = spec_files('functional', '*-spec.k')
OPCODES_TESTS: Final = spec_files('opcodes', '*-spec.k')
ERC20_TESTS: Final = spec_files('erc20', '*/*-spec.k')
BIHU_TESTS: Final = spec_files('bihu', '*-spec.k')
EXAMPLES_TESTS: Final = spec_files('examples', '*-spec.k') + spec_files('examples', '*-spec.md')
MCD_TESTS: Final = spec_files('mcd', '*-spec.k')
OPTIMIZATION_TESTS: Final = (SPEC_DIR / 'opcodes/evm-optimizations-spec.md',)
KONTROL_TESTS: Final = spec_files('kontrol', '*-spec.k')
ALL_TESTS: Final = sum(
[
BENCHMARK_TESTS,
FUNCTIONAL_TESTS,
OPCODES_TESTS,
ERC20_TESTS,
BIHU_TESTS,
EXAMPLES_TESTS,
MCD_TESTS,
OPTIMIZATION_TESTS,
KONTROL_TESTS,
],
(),
)
def exclude_list(exclude_file: Path) -> list[Path]:
res = [REPO_ROOT / test_path for test_path in exclude_file.read_text().splitlines()]
assert res
return res
FAILING_PYK_TESTS: Final = exclude_list(TEST_DIR / 'failing-symbolic.pyk')
FAILING_BOOSTER_TESTS: Final = exclude_list(TEST_DIR / 'failing-symbolic.haskell-booster')
FAILING_TESTS: Final = exclude_list(TEST_DIR / 'failing-symbolic.haskell')
# -----------
# Kompilation
# -----------
KOMPILE_MAIN_FILE: Final = {
'benchmarks/functional-spec.k': 'functional-spec.k',
'bihu/functional-spec.k': 'functional-spec.k',
'examples/solidity-code-spec.md': 'solidity-code-spec.md',
'examples/erc20-spec.md': 'erc20-spec.md',
'examples/erc721-spec.md': 'erc721-spec.md',
'examples/storage-spec.md': 'storage-spec.md',
'examples/sum-to-n-spec.k': 'sum-to-n-spec.k',
'examples/sum-to-n-foundry-spec.k': 'sum-to-n-foundry-spec.k',
'functional/infinite-gas-spec.k': 'infinite-gas-spec.k',
'functional/evm-int-simplifications-spec.k': 'evm-int-simplifications-spec.k',
'functional/int-simplifications-spec.k': 'int-simplifications-spec.k',
'functional/lemmas-no-smt-spec.k': 'lemmas-no-smt-spec.k',
'functional/lemmas-spec.k': 'lemmas-spec.k',
'functional/abi-spec.k': 'abi-spec.k',
'functional/merkle-spec.k': 'merkle-spec.k',
'functional/storageRoot-spec.k': 'storageRoot-spec.k',
'mcd/functional-spec.k': 'functional-spec.k',
'opcodes/evm-optimizations-spec.md': 'evm-optimizations-spec.md',
}
KOMPILE_MAIN_MODULE: Final = {
'benchmarks/functional-spec.k': 'FUNCTIONAL-SPEC-SYNTAX',
'bihu/functional-spec.k': 'FUNCTIONAL-SPEC-SYNTAX',
'erc20/functional-spec.k': 'FUNCTIONAL-SPEC-SYNTAX',
'mcd/functional-spec.k': 'FUNCTIONAL-SPEC-SYNTAX',
'opcodes/evm-optimizations-spec.md': 'EVM-OPTIMIZATIONS-SPEC-LEMMAS',
}
class Target(NamedTuple):
main_file: Path
main_module_name: str
@property
def id(self) -> str:
"""
The target's id is the two trailing path segments and the main module name
"""
return f'{self.main_file.parts[-2]}-{self.main_file.stem}-{self.main_module_name}'
def __call__(self, output_dir: Path) -> Path:
return kevm_kompile(
output_dir=output_dir,
target=KompileTarget.HASKELL,
main_file=self.main_file,
main_module=self.main_module_name,
syntax_module=self.main_module_name,
debug=True,
)
@pytest.fixture(scope='module')
def target_dir(kompiled_targets_dir: Path | None, tmp_path_factory: TempPathFactory) -> Path:
if kompiled_targets_dir:
kompiled_targets_dir.mkdir(parents=True, exist_ok=True)
return kompiled_targets_dir
return tmp_path_factory.mktemp('kompiled')
@pytest.fixture(scope='module')
def kompiled_target_for(target_dir: Path) -> Callable[[Path], Path]:
"""
Generate a function that returns a path to the kompiled defintion for a given K spec. Invoke `kompile` only if no kompiled directory is cached for the spec.
"""
def kompile(spec_file: Path) -> Path:
target = _target_for_spec(spec_file)
lock_file = target_dir / f'{target.id}.lock'
output_dir = target_dir / target.id
with SoftFileLock(lock_file):
if output_dir.exists():
return output_dir
return target(output_dir)
return kompile
def _target_for_spec(spec_file: Path) -> Target:
spec_file = spec_file.resolve()
spec_id = str(spec_file.relative_to(SPEC_DIR))
spec_root = SPEC_DIR / spec_file.relative_to(SPEC_DIR).parents[-2]
main_file = spec_root / KOMPILE_MAIN_FILE.get(spec_id, 'verification.k')
main_module_name = KOMPILE_MAIN_MODULE.get(spec_id, 'VERIFICATION')
return Target(main_file, main_module_name)
@pytest.mark.parametrize(
'spec_file',
ALL_TESTS,
ids=[str(spec_file.relative_to(SPEC_DIR)) for spec_file in ALL_TESTS],
)
def test_kompile_targets(
spec_file: Path, kompiled_target_for: Callable[[Path], Path], kompiled_targets_dir: Path | None
) -> None:
"""
This test function is intended to be used to pre-kompile all definitions,
so that the actual proof tests do not need to do the actual compilation,
which is disturbing performance measurment.
To achieve the desired caching, this test should be run like this:
pytest src/tests/integration/test_prove.py::test_kompile_targets --kompiled-targets-dir ./prekompiled
This test will be skipped if no --kompiled-targets-dir option is given
"""
if not kompiled_targets_dir or spec_file in FAILING_BOOSTER_TESTS:
pytest.skip()
kompiled_target_for(spec_file)
# ---------
# Pyk tests
# ---------
class TParams:
main_claim_id: str | None
leaf_number: int | None
break_on_calls: bool
def __init__(
self, main_claim_id: str | None = None, leaf_number: int | None = None, break_on_calls: bool = False
) -> None:
self.main_claim_id = main_claim_id
self.leaf_number = leaf_number
self.break_on_calls = break_on_calls
TEST_PARAMS: dict[str, TParams] = {
'mcd/vat-slip-pass-rough-spec.k': TParams(
main_claim_id='VAT-SLIP-PASS-ROUGH-SPEC.Vat.slip.pass.rough',
leaf_number=1,
),
}
for KONTROL_TEST in KONTROL_TESTS:
TEST_PARAMS[f'kontrol/{KONTROL_TEST.name}'] = TParams(break_on_calls=True) # noqa: B909
def leaf_number(proof: APRProof) -> int:
non_target_leaves = [nd for nd in proof.kcfg.leaves if not proof.is_target(nd.id)]
return len(non_target_leaves) + len(proof.kcfg.predecessors(proof.target))
@pytest.mark.parametrize(
'spec_file',
ALL_TESTS,
ids=[str(spec_file.relative_to(SPEC_DIR)) for spec_file in ALL_TESTS],
)
def test_pyk_prove(
spec_file: Path,
kompiled_target_for: Callable[[Path], Path],
tmp_path: Path,
caplog: LogCaptureFixture,
use_booster: bool,
bug_report: BugReport | None,
spec_name: str | None,
) -> None:
caplog.set_level(logging.INFO)
if (not use_booster and spec_file in FAILING_PYK_TESTS) or (use_booster and spec_file in FAILING_BOOSTER_TESTS):
pytest.skip()
if spec_name is not None and str(spec_file).find(spec_name) < 0:
pytest.skip()
# Given
log_file = tmp_path / 'log.txt'
use_directory = tmp_path / 'kprove'
use_directory.mkdir()
# When
try:
definition_dir = kompiled_target_for(spec_file)
name = str(spec_file.relative_to(SPEC_DIR))
break_on_calls = name in TEST_PARAMS and TEST_PARAMS[name].break_on_calls
options = ProveOptions(
{
'spec_file': spec_file,
'definition_dir': definition_dir,
'includes': [str(include_dir) for include_dir in config.INCLUDE_DIRS],
'save_directory': use_directory,
'md_selector': 'foo', # TODO Ignored flag, this is to avoid KeyError
'use_booster': use_booster,
'bug_report': bug_report,
'break_on_calls': break_on_calls,
}
)
exec_prove(options=options)
if name in TEST_PARAMS:
params = TEST_PARAMS[name]
if params.leaf_number is not None and params.main_claim_id is not None:
apr_proof = APRProof.read_proof_data(
proof_dir=use_directory,
id=params.main_claim_id,
)
expected_leaf_number = params.leaf_number
actual_leaf_number = leaf_number(apr_proof)
assert expected_leaf_number == actual_leaf_number
except BaseException:
raise
finally:
log_file.write_text(caplog.text)
# ------------
# Legacy tests
# ------------
PROVE_ARGS: Final[dict[str, Any]] = {
'functional/lemmas-no-smt-spec.k': {
'haskell_args': ['--smt=none'],
},
}
@pytest.mark.parametrize(
'spec_file',
FAILING_PYK_TESTS,
ids=[str(spec_file.relative_to(SPEC_DIR)) for spec_file in FAILING_PYK_TESTS],
)
def test_kprove_prove(
spec_file: Path,
kompiled_target_for: Callable[[Path], Path],
tmp_path: Path,
caplog: LogCaptureFixture,
bug_report: BugReport | None,
) -> None:
caplog.set_level(logging.INFO)
if spec_file in FAILING_TESTS:
pytest.skip()
# Given
spec_id = str(spec_file.relative_to(SPEC_DIR))
args = PROVE_ARGS.get(spec_id, {})
if 'haskell_args' not in args:
args['haskell_args'] = []
args['haskell_args'] += ['--smt-timeout', '300']
args['haskell_args'] += ['--smt-retry-limit', '10']
log_file = tmp_path / 'log.txt'
use_directory = tmp_path / 'kprove'
use_directory.mkdir()
# When
try:
definition_dir = kompiled_target_for(spec_file)
kevm = KEVM(definition_dir, use_directory=use_directory)
actual = kevm.prove(spec_file=spec_file, include_dirs=list(config.INCLUDE_DIRS), **args)
except BaseException:
raise
finally:
log_file.write_text(caplog.text)
# Then
assert len(actual) == 1
assert is_top(actual[0].kast, weak=True)