Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

experiment: Viper integration #3477

Merged
merged 88 commits into from
Nov 25, 2022
Merged

experiment: Viper integration #3477

merged 88 commits into from
Nov 25, 2022

Conversation

crusso
Copy link
Contributor

@crusso crusso commented Oct 7, 2022

Motoko-san, an experimental verification method

This PR adds (very experimental) Viper code generation from (a limited subset of) Motoko. This is a proof of concept, showing that we can generate appropriate assertions so that Viper can diagnose concurrency (e.g. re-entrancy) bugs. A few tests are provided to demonstrate this.

Note that this is (a.t.m.) not directed towards the general public, rather towards folks with verification experience.

To use the Viper capabilities

  • specify --viper on the command line (Viper generation)
  • set env MOC_UNLOCK_VERIFICATION=1 (regular compilation using extended grammar)

When the verification features are enabled, a convenience logical operator implies becomes a keyword. It has the following truth table:

a b a implies b
false false true
false true true
true false false
true true true

@github-actions
Copy link

github-actions bot commented Oct 7, 2022

Comparing from f1aa9b0 to 998f484:
The produced WebAssembly code seems to be completely unchanged.

@nomeata
Copy link
Collaborator

nomeata commented Oct 7, 2022

Which viper is that?

@ggreif
Copy link
Contributor

ggreif commented Oct 7, 2022

Which viper is that?

https://www.pm.inf.ethz.ch/research/viper.html

@nomeata
Copy link
Collaborator

nomeata commented Oct 7, 2022

Nice!

crusso and others added 22 commits October 7, 2022 15:29
Adds reverse (Viper -> Motoko) source region mapping.
* clean up junk

* split `pp_stmt`

* do XON/XOFF wrapping centrally in `pp_stmt`

* `prog_mapped` also points to the right (generated) file
Set up testing infrastructure for Viper
- moved tests to `test/viper`
- added `Makefile`
- augmented `run.sh`
- accepted initial output
- the `.vpr` files are also checked with `silicon` backend

Run these by `make -C test/viper`
but this is not (yet) added to the outer `Makefile`.
This implements actor-global invariants. They get (currently) declared with a actor-level `assert` and translated to a Viper macro, that gets both `require`d and `ensure`d from each method by calls to those macros.

TODOs:
- [ ] find better syntax
- [x] use invariant(s) for pre/postconditions on each actor method
- [x] `self` access in `define`
- [x] `__init__` should only have a postcondition (depends on #3486)
- [x] allow more than one invariant
…3486)

This PR extends the Motoko to Viper translation with initialization statements for each actor private field. These statements form the body of a special `__init__` method (that establishes the canister's invariant for the first time). 

Main assumptions:
* Initializing expressions can be translated as expressions; no imperative code (a.k.a. Viper statements) needs to be used
* The name `__init__` is not used elsewhere in the program
* The method `__init__` should be supplied with permissions to write into each actor private field (alternatively, the translation would need to be extended with `inhale` statements corresponding to each field)
This changes the srcloc of certain Viper subexpression to better track the original Motoko.
@ggreif ggreif changed the title experiment: viper integration experiment: Viper integration Nov 18, 2022
It still has somewhat outdated backends, but we never had problems with those.

But this gives a non-moving download URL, so that's a win!
@ggreif ggreif marked this pull request as ready for review November 18, 2022 14:53
@ggreif ggreif added the viper Verification related label Nov 18, 2022
@ggreif ggreif force-pushed the viper branch 2 times, most recently from 98c563f to 55af544 Compare November 22, 2022 07:28
Turns out I was doing a bunch of things wrong. Now we make the relevant `Lexer_lib.mode` accessible from the parser productions (previously it just accessed a constant thing).

This makes `--viper` to select the `verification` mode in the parser too (lexer was already fine).
@aterga
Copy link
Contributor

aterga commented Nov 22, 2022

Can implies be used at runtime? If yes, do we need to add support for short-circuiting semantics? I.e., the evaluation of false implies exp should not result in the evaluation of exp.

@ggreif
Copy link
Contributor

ggreif commented Nov 22, 2022

Can implies be used at runtime?

Yes.

We desugar it to the mathematical thing. That uses and, which has short-circuiting built in. The interpreter is naive (uses <= on bool in OCaml). Let me test:

$ env MOC_UNLOCK_VERIFICATION=1 rlwrap moc
> false implies (1 / 0 == 1);
> stdin:1.16-1.21: execution error, arithmetic overflow

Input:

$ cat short.mo
false implies (1 / 0 == 1);

Compiled:

$ env MOC_UNLOCK_VERIFICATION=1 rlwrap moc -wasi-system-api short.mo
$ wasmtime short.wasm 

$ echo $?
0

test/repl/viper.sh Outdated Show resolved Hide resolved
ggreif and others added 4 commits November 22, 2022 23:35
This PR adds a readme file that covers the following aspects behind Formal Motoko:

(1) Disclaimer; (2) Introduction — Overview | Formal specification | Static vs. dynamic assertions | Testing vs. formal verification | Precondition of public functions | Examples; (3) Contributing — Building | Running | Testing | File structure | Further information
@ggreif ggreif added the automerge-squash When ready, merge (using squash) label Nov 25, 2022
@mergify mergify bot merged commit 21a11fc into master Nov 25, 2022
@mergify mergify bot deleted the viper branch November 25, 2022 13:21
@mergify mergify bot removed the automerge-squash When ready, merge (using squash) label Nov 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
viper Verification related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants