Skip to content

Commit

Permalink
Included #panic and #internalPanic in terminal rules. (#281)
Browse files Browse the repository at this point in the history
* Included `#panic` and `#internalPanic` in terminal rules.

This stops the verifier from potentially infinitely spinning on panics.

* Set Version: 0.2.5

* replacing elif

* added labels for panic rules

* `advance_proof` correctly references `KMIRSemantics.terminal_rules()`

* Added `cut_point_rules` (currently empty) as arg

* Set Version: 0.2.6

---------

Co-authored-by: devops <[email protected]>
Co-authored-by: rv-jenkins <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent 4faa989 commit 61ecb79
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 16 deletions.
6 changes: 2 additions & 4 deletions kmir/k-src/mir.md
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ module MIR-FINALIZATION
These are internal panics that are specific to KMIR.

```k
rule <k> #internalPanic(_FN_KEY, _PANIC, _MSG) ~> (_ITEM:KItem => .K) ... </k>
rule [iPanic]: <k> #internalPanic(_FN_KEY, _PANIC, _MSG) ~> (_ITEM:KItem => .K) ... </k>
<returncode> 4 => 1 </returncode>
```

Expand All @@ -711,9 +711,7 @@ These are internal panics that are specific to KMIR.
These panics are not specific to KMIR and caused by program-level reasons, i.e. assertion violations.

```k
rule <k> #panic(_FN_KEY, _PANIC, _MSG) ~> (_ITEM:KItem => .K) ... </k>
<returncode> 4 => 2 </returncode>
rule <k> #panic(_FN_KEY, _PANIC, _MSG) ... </k>
rule [panic]: <k> #panic(_FN_KEY, _PANIC, _MSG) ~> (_ITEM:KItem => .K) ... </k>
<returncode> 4 => 2 </returncode>
```

Expand Down
2 changes: 1 addition & 1 deletion kmir/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kmir/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api"

[tool.poetry]
name = "kmir"
version = "0.2.5"
version = "0.2.6"
description = ""
authors = [
"Runtime Verification, Inc. <[email protected]>",
Expand Down
2 changes: 1 addition & 1 deletion kmir/src/kmir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

from .kmir import KMIR

VERSION: Final = '0.2.5'
VERSION: Final = '0.2.6'
1 change: 1 addition & 0 deletions kmir/src/kmir/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ def _init_and_run_proof(claim: KClaim) -> tuple[bool, list[str] | None]:
proof_problem,
kcfg_explore,
max_depth=depth,
terminal_rules=KMIRSemantics.terminal_rules(),
)
failure_log = None
if not passed:
Expand Down
27 changes: 22 additions & 5 deletions kmir/src/kmir/kmir.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from pyk.cli.utils import check_dir_path, check_file_path
from pyk.cterm import CTerm
from pyk.kast.inner import KApply, KInner, KSequence, KVariable
from pyk.kast.inner import KApply, KInner, KLabel, KSequence, KVariable
from pyk.kcfg.semantics import KCFGSemantics
from pyk.ktool.kprint import KAstInput, KAstOutput, _kast, gen_glr_parser
from pyk.ktool.kprove import KProve
Expand All @@ -29,24 +29,27 @@ class KMIRSemantics(KCFGSemantics):
def is_terminal(self, cterm: CTerm) -> bool:
k_cell = cterm.cell('K_CELL')
# <k> #halt </k>
if k_cell == KMIR.halt():
if k_cell == KMIR.halt() or KMIR.is_panic(k_cell):
return True
elif type(k_cell) is KSequence:
# <k> . </k>
if k_cell.arity == 0:
return True
# <k> #halt </k>
elif k_cell.arity == 1 and k_cell[0] == KMIR.halt():
elif k_cell.arity == 1 and (k_cell[0] == KMIR.halt() or KMIR.is_panic(k_cell[0])):
return True
elif (
k_cell.arity == 2 and k_cell[0] == KMIR.halt() and type(k_cell[1]) is KVariable and k_cell[1].sort == K
k_cell.arity == 2
and (k_cell[0] == KMIR.halt() or KMIR.is_panic(k_cell[0]))
and type(k_cell[1]) is KVariable
and k_cell[1].sort == K
):
return True
return False

@staticmethod
def terminal_rules() -> list[str]:
terminal_rules = ['MIR.halt']
terminal_rules = ['MIR.halt', 'MIR.panic', 'MIR.iPanic']

# TODO: break every step and add to terminal rules. Semantics does not support this currently
return terminal_rules
Expand Down Expand Up @@ -212,3 +215,17 @@ def preprocess_and_run(program_file: Path, temp_file: Path) -> CompletedProcess:
@staticmethod
def halt() -> KApply:
return KApply('#halt_MIR_KItem')

@staticmethod
def panic_label() -> KLabel:
return KLabel('#panic(_,_,_)_PANICS_KItem_FunctionLikeKey_Panic_KItem')

@staticmethod
def internal_panic_label() -> KLabel:
return KLabel('#internalPanic(_,_,_)_PANICS_KItem_FunctionLikeKey_InternalPanic_KItem')

@staticmethod
def is_panic(inner: KInner) -> bool:
return type(inner) is KApply and (
inner.label == KMIR.internal_panic_label() or inner.label == KMIR.panic_label()
)
5 changes: 2 additions & 3 deletions kmir/src/kmir/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,12 @@ def kmir_prove(
kcfg_explore: KCFGExplore,
max_depth: int | None = 1000,
max_iterations: int | None = None,
is_terminal: Callable[[CTerm], bool] | None = None,
terminal_rules: Iterable[str] = (),
cut_point_rules: Iterable[str] = (), # TODO
extract_branches: Callable[[CTerm], Iterable[KInner]] | None = None,
abstract_node: Callable[[CTerm], CTerm] | None = None,
) -> bool:
proof = proof
terminal_rules: Iterable[str] = ['MIR.halt']
cut_point_rules: Iterable[str] = [] # TODO: cut point rules
prover: APRBMCProver | APRProver | EqualityProver
if type(proof) is APRBMCProof:
prover = APRBMCProver(proof, kcfg_explore)
Expand Down
2 changes: 1 addition & 1 deletion package/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.5
0.2.6

0 comments on commit 61ecb79

Please sign in to comment.