• This repository has been archived on 20/Aug/2021
  • Stars
    star
    124
  • Rank 288,207 (Top 6 %)
  • Language
    JavaScript
  • License
    GNU Affero Genera...
  • Created over 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

K framework proof explorer & smart contract specification format

KLab

NOTE: This software is still in the early stages of development. If you are confused, find some bugs, or just want some help, please file an issue or come talk to us at https://dapphub.chat/channel/k-framework.

Klab is a tool for generating and debugging proofs in the K Framework, tailored for the formal verification of ethereum smart contracts. It includes a succinct specification language for expressing the behavior of ethereum contracts, and an interactive debugger.

Installation

Dependencies

See dependency installation instructions here

This project uses the GNU version of getopt and time. OSX and gnu have a complicated relationship but you can run:

export PATH=/usr/local/opt/gnu-getopt/bin:/usr/local/opt/gnu-time/libexec/gnubin:/usr/local/opt/coreutils/libexec/gnubin:$PATH

to make them get along.

Building

Clone the repo and install the latest stable version v0.4.0 with

git clone --branch v0.4.0 https://github.com/dapphub/klab.git
cd klab
make deps

OPTIONAL: klab has some optional Haskell components, for which the recommended installation method is nix. If you have nix, you can install the Haskell components with

make deps-haskell

Environment Setup

To make klab available from the terminal, you can either just export the path to the klab executable in bin/, or use:

make link

This installs symlinks globally at /usr/local/bin and /usr/local/libexec (will require sudo on Linux machines). You can also specify a custom directory for installation by doing:

PREFIX=/path/to/custom/prefix make link

The file env will setup the environment for you if sourced from the root directory of the repo.

source ./env

It sets three environment variables:

  • PATH: include the klab executable.
  • KLAB_EVMS_PATH: the EVM semantics to use.

OPTIONAL: If you want to use a different version of K than what the KEVM ships with, you can set:

  • KLAB_K_PATH: override implementation of K.

OPTIONAL: You might also want to add the K tool binaries in evm-semantics/.build/k/k-distribution/bin to your $PATH, if you didn't already have K installed.

OPTIONAL: You can also use nix-shell for a more deterministic environment experience. If you have nix installed, run nix-shell in this repo to start a deterministic shell environment.

Usage

To see how klab is used, we can explore the project in examples/SafeAdd:

cd examples/SafeAdd/

Specification

The file config.json tells klab where to look for both the specification and the implementation of our contract. In this case, our specification lives in src/, and our implementation lives in dapp/.

Note that this example includes dapp/out/SafeAdd.sol.json compiled from the solidity source. With solc installed, you can compile it yourself:

solc --combined-json=abi,bin,bin-runtime,srcmap,srcmap-runtime,ast dapp/src/SafeAdd.sol > dapp/out/SafeAdd.sol.json

Proof

Our goal is to prove that our implementation satisfies our specification. To do so, we'll start by building a set of K modules from our spec:

klab build

This will generate success and failure reachability rules for each act of our specification. We can find the results in the out/specs directory.

Now we're ready to prove each case, for example:

klab prove --dump SafeAdd_add_fail

The --dump flag outputs a log to out/data/<hash>.log, which will be needed later for interactive debugging. We can also do klab prove-all to prove all outstanding claims.

Once the proof is complete, we can explore the generated symbolic execution trace using:

klab debug <hash>

Embedded rules

klab comes with a set of pre-defined K rewrite rules, additional to the ones defined in evm-semantics. They are located in resources/rules.k.tmpl.

Key Bindings

Toggle different views by pressing any of the following keys:

View Commands:

  • t - display the (somewhat) pretty K term.
  • c - display current constraints.
  • k - display <k> cell.
  • b - display behavior tree.
  • s - diaplay source code.
  • e - display evm specific module.
  • m - display memory cell.
  • d - display debug cells (see toggling debug cells below).
  • r - display applied K rule.
  • z - display z3 feedback from attempted rule application.
  • Up/Dn - scroll view up and down.

Navigation Commands:

  • n - step to next opcode
  • p - step to previous opcode
  • Shift+n - step to next k term
  • Shift+p - step to previous k term
  • Ctrl+n - step to next branch point
  • Ctrl+p - step to previous branch point

Toggling Debug Cells:

The following commands are prefixed with : (and are typed at the bottom of the interface). It's possible to toggle the debug cells view for specific cells, which prints out the JSON representation of the given cells. Remember, you must turn on the debug cells view to see these (above).

  • :show ethereum.evm.callState.gas - show the contents of the <gas> cell in the debug cells view.
  • :hide ethereum.evm.callStack.pc - hide the contents of the <pc> cell in the debug cells view.
  • :omit gas pc - omit the contents of the <gas> and <pc> cells in the term view.
  • :unomit pc programBytes - unomit the contents of the <pc> and <programBytes> cells in the term view.

Available klab Commands

  • klab build - builds a set of K reachability claims in out/specs based on the spec, lemmas and source code as specified in the projects config.json.
  • klab prove <hash> [--dump] - executes a K reachability claim specified as a hash to the K prover. If the --dump flag is present, the proof can be explored using klab debug.
  • klab prove-all - builds and executes all proof objects in the project directory.
  • klab debug <hash> - opens up the cli proof explorer of a particular proof execution. See key bindings above.
  • klab focus <hash> - focus on a hash, allowing you to leave out it as an argument to other commands.
  • klab hash - prints the hash of the focused proof
  • klab get-gas <hash> - Traverses the execution trace of a proof object to fetch its gas usage, put in out/gas/<hash>gas.k.
  • klab solve-gas <hash> - Constructs the gas condition necessary for an execution to succeed.
  • klab evm <hash> - Shows opcodes and source code side-by-side (useful for extracting pc values).
  • klab status <hash> - Shows whether a proof has been run, and whether it was accepted or rejected.
  • klab status-js <hash> - Shows the behaviour tree for an executed proof.
  • klab fetch <url> - Fetches the execution trace of a proof object at the url, preparing it for local debugging.
  • klab compress <hash> - compresses an execution trace so you can share it with a friend (or enemy).
  • klab storage <contractName> - Guesses what the storage layout of a given contract is
  • klab report - Generates a html report of the current project state in out/report/index.html.
  • klab help - Generates this view

Configuration

The config.json file is used to configure klab.

Here's an example:

{
  "name": "k-dss",
  "url": "https://github.com/dapphub/k-dss",
  "src": {
    "specification": "./src/dss.md",
    "smt_prelude": "./src/prelude.smt2.md",
    "rules": [
      "./src/storage.k.md",
      "./src/lemmas.k.md"
    ],
    "dirty_rules": [
      "./src/dirty_lemmas.k.md"
    ]
  },
  "implementations": {
    "Vat": {
      "src": "src/vat.sol"
    },
    "Vow": {
      "src": "src/vow.sol"
    },
  },
  "timeouts": {
    "Vat_grab_pass_rough": "16h",
  },
  "memory" : {
    "Vat_frob-diff-nonzero_fail_rough": "25G",
  },
  "dapp_root": "./dss",
  "solc_output_path": "out/dapp.sol.json",
  "host": "127.0.0.1:8080"
}

Limits

Time

By default, klab-prove sets a timeout of 1 day. This can be changed by passing the --timeout flag a value of the format [0-9]+[dhms].

klab-prove-all defaults to a per-proof timeout of 200m. This can be changed by setting timeouts to a different value in config.json, as shown above.

Memory

By default, both klab-prove and klab-prove-all run the JVM with a maximum heap size of 10GB.

This can be changed by setting the K_OPTS environment variable to something like --Xmx4G. Refer to the JVM docs for more information.

klab-prove-all also reads the config.json file, and the maximum heap size can be changed with the memory key, as shown above.

Gas

In rough specs, the amount of gas available defaults to 3,000,000. This can be changed using the gas header.

Once a pass_rough spec has been proven, the gas used for each execution path is combined into a single expression, which is the upper gas bound for the stronger pass spec.

Zsh completions

There are automatic tab completions for zsh that can be installed by adding the following to your .zshrc:

# completions for klab
fpath=(~/dapphub/klab/resources/zsh $fpath)
autoload -U compinit
compinit

Troubleshooting

Outdated npm

You might have problems due to an outdated npm, in that case try updating it with:

npm install npm@latest -g
npm install -g n
n stable

KLab server requesting files at incorrect directory

What it looks like:

$ klab server

18.07.30 14-46-50: exec dfc688db4cc98b5de315bdfaa2512b84d14c3aaf3e58581ae728247097ff300d/run.sh
18.07.30 14-47-32: out Debugg: dfc688db4cc98b5de315bdfaa2512b84d14c3aaf3e58581ae728247097ff300d

fs.js:119
throw err;
^

Error: ENOENT: no such file or directory, open '/tmp/klab/b042c99687ae5018744dc96107032b291e4a91f1ab38a6286b2aff9a78056665/abstract-semantics.k'
at Object.openSync (fs.js:443:3)
at Object.readFileSync (fs.js:348:35)
at getFileExcerpt (/home/dev/src/klab/lib/rule.js:5:4)
at Object.parseRule (/home/dev/src/klab/lib/rule.js:21:16)
at Object.getblob (/home/dev/src/klab/lib/driver/dbDriver.js:49:19)
at Object.next (/home/dev/src/klab/lib/driver/dbDriver.js:113:56)
at Stream._n (/home/dev/src/klab/node_modules/xstream/index.js:797:18)
at /home/dev/src/klab/node_modules/@cycle/run/lib/cjs/index.js:57:61
at process._tickCallback (internal/process/next_tick.js:61:11)
[1] [dev@arch-ehildenb klab]% klab server
fs.js:119
throw err;

Notice how it's requesting abstract-semantics.k from proof-hash b042... but we're actually running proof-hash dfc6.... This is a problem with how K caches compiled definitions, and must be fixed upstream.

To fix this, run:

make clean && make deps

This will remove and recompile the KEVM semantics.

License

All contributions to this repository are licensed under AGPL-3.0. Authors:

  • Denis Erfurt
  • Martin Lundfall
  • Everett Hildenbrandt
  • Lev Livnev

More Repositories

1

dapptools

Dapp, Seth, Hevm, and more
Haskell
2,086
star
2

dappsys

Composable building blocks for Ethereum contracts
Nix
580
star
3

ds-proxy

a proxy object that can compose transactions on owner's behalf
Solidity
311
star
4

dapple

EVM contract system developer multitool
JavaScript
298
star
5

ds-math

Safe arithmetic
Solidity
268
star
6

dapp

This repository has been moved to dapphub/dapptools
249
star
7

ds-token

A simple and sufficient ERC20 implementation
Solidity
225
star
8

ds-test

Assertions, equality checks and other test helpers
Solidity
208
star
9

chai

ERC20 wrapper over the Dai Savings Rate
Solidity
147
star
10

seth

(OLD REPO) The command-line Ethereum army knife by DappHub
147
star
11

ds-auth

Updatable, unobtrusive Solidity authorization pattern
Solidity
138
star
12

hevm

(OLD REPO) A debug-oriented Ethereum VM (EVM)
114
star
13

dmap

registry contract with no time to argue
JavaScript
58
star
14

ds-dach

Dai Automated Clearing House
Shell
52
star
15

dpack

simple lockfile for your dapp's addresses and artifacts
TypeScript
46
star
16

k-dss

formal verification of multicollateral dai in the K framework
GCC Machine Description
45
star
17

ds-feeds

Simple data access pattern for dumb, durable software objects
Makefile
32
star
18

ds-chief

approval voting to select who wears the hat by consensus
Solidity
29
star
19

ds-weth

ETH->ERC20 with extra opinions
Solidity
27
star
20

ds-pause

Schedule function calls that can only be executed once some delay has elapsed
Solidity
26
star
21

ethrun

Directly run EVM bytecode (using Parity technology)
Rust
24
star
22

ds-vault

DSAuth-protected ERC20 token vault
Solidity
24
star
23

erc20

erc20 interface definition container package
Solidity
24
star
24

ds-guard

Whitelist DSAuthority for use with DSAuth
Solidity
22
star
25

ds-group

Multisig with a command-line interface
Shell
22
star
26

dapp-tools

Code for the dapp.tools website
Nix
21
star
27

dappsys-monolithic

Dappsys for all
Solidity
21
star
28

ds-cabal

Simple m-of-n multisig proxy written in handcrafted EVM bytcode
Makefile
21
star
29

keeper

Admin toolkit for incentive-following software daemons
Shell
19
star
30

ds-roles

A DSAuthority for up to 256 roles
Solidity
17
star
31

ds-note

Log function calls as events
Solidity
17
star
32

ds-value

Set and get a value
Solidity
14
star
33

fv-tutorial

Materials for the devcon4 workshop
GCC Machine Description
14
star
34

ds-eth-token

ERC20 ETH token wrapper. No more special case logic for Ether.
Makefile
13
star
35

ds-exec

Better exception handling
Solidity
9
star
36

ll

Linear Logic references
HTML
8
star
37

awesome-dmap

awesome dmap links
7
star
38

nixpkgs-dapphub

Dapp development overlay for Nix
Nix
7
star
39

locktopus

sqlite file of locked values in the dmap
JavaScript
6
star
40

ds-warp

Time travel for Ethereum
Nix
6
star
41

token

(OLD REPO) Command-line utility for ERC20 tokens
6
star
42

ACS

Applied Computer Science Working Group
6
star
43

ds-pain

Shell
5
star
44

erc20-golf

GCC Machine Description
5
star
45

chronobank-contracts

audit branch of chronobank contracts
JavaScript
5
star
46

ds-items

Ownable, transferrable, non-fungible unsigned integers
Makefile
4
star
47

dapphub.com

HTML
4
star
48

dapphub-registry

JavaScript
4
star
49

ds-forkable

Forkable Datastore Service
4
star
50

ds-cache

DSValue with expiry
Solidity
3
star
51

dapple-script

DappleScript - A simple ethereum interaction language.
JavaScript
3
star
52

ds-thing

Your things should be DSThings
Solidity
3
star
53

dapp-wizard

Racket
3
star
54

remappings-test

dummy project for testing dapp remappings
Solidity
2
star
55

kyo

know-your-origin, don't use this terrible anti-pattern you fool
2
star
56

k

k-fork
Java
2
star
57

ethsign

(OLD REPO) Simple Ethereum transaction signer using Geth as a library
2
star
58

ds-spell

An object that does one thing once.
Makefile
2
star
59

dappsys-graphics

dappsys visualizations
JavaScript
2
star
60

ethkey

Create Ethereum accounts using Geth as a library
Go
2
star
61

ds-store

Dappsys data management components
Makefile
2
star
62

qrtx

JavaScript
2
star
63

dapple-quicktest

Blazingly fast EVM test runner
JavaScript
2
star
64

cage

Nix
2
star
65

maker-docs

Reference documentation for Maker and the Dai Credit System
HTML
2
star
66

libethjet-haskell

2
star
67

tr-base

dummy repo for testing some dapp-remappings stuff
Solidity
1
star
68

lockpack

1
star
69

LLPoster

TeX
1
star
70

dapp.org-old

HTML
1
star
71

guts

1
star
72

ds-delay

Makefile
1
star
73

dapp.tools

Unused - obsolete github pages site for dapp.tools.
HTML
1
star
74

fv-tutorial-solutions

GCC Machine Description
1
star
75

celf-evm

EVM written in celf
JavaScript
1
star
76

llmotivation

Motivational Paper on Linear Logic as a blockchain programming language
TeX
1
star
77

ds-read

Makefile
1
star
78

dapp.org

1
star
79

dapp.coop

HTML
1
star
80

exp-squaring-percise-dapp

Makefile
1
star
81

mod-blockchain

JavaScript
1
star
82

wiki.dapp.coop

JavaScript
1
star
83

klab-explorer.github.io

klab explorer webpage
1
star
84

ds-rpow

Makefile
1
star
85

k-gas-analyser

Haskell
1
star
86

chronobank-review

review of chronobank contracts
HTML
1
star
87

ds-base

building up a reasonable base class
Makefile
1
star
88

ds-stop

DSAuth-protected stop and start
Solidity
1
star
89

dappsys-docs

Python
1
star
90

klab-multipleCalls

proving that one contract can call another one, wow
GCC Machine Description
1
star
91

tr0

dummy repo for testing dapp-remappings
Solidity
1
star
92

k-ds-rpow

SMT
1
star
93

manny

Manny O'Malley, Malicious Oracle
1
star
94

bat-rewrite

Makefile
1
star
95

hubot-channel-welcome

JavaScript
1
star
96

dappsys-nix

Shell
1
star
97

dapple-wevm

Wallet side ethereum virtual machine
JavaScript
1
star
98

dpath

dpath syntax and semantics
1
star
99

dai-semantics

K specification of the dai stablecoin system
Lua
1
star
100

dapple-pkg

Dapple packages module
JavaScript
1
star