forked from runtimeverification/evm-semantics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkevm
executable file
·440 lines (376 loc) · 20 KB
/
kevm
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env bash
set -euo pipefail
shopt -s extglob
debug=false
profile=false
KEVM=kevm
notif() { echo "== ${KEVM}: $*" >&2 ; }
fatal() { notif "[FATAL] $*" ; exit 1 ; }
execute () {
( if ${profile}; then
TIMEFORMAT="%lR %lU %lS $*"
time "$@"
else
"$@"
fi
)
}
check_k_install() {
which kast &> /dev/null \
|| fatal "Must have K installed! See https://github.com/kframework/k/releases."
which krun &> /dev/null \
|| fatal "Must have K installed! See https://github.com/kframework/k/releases."
}
INSTALL_BIN="$(cd $(dirname $0) && pwd)"
INSTALL_LIB="$(dirname ${INSTALL_BIN})/lib/kevm"
INSTALL_INCLUDE=${INSTALL_LIB}/include
install_k_bin=${INSTALL_LIB}/kframework/bin
install_k_lib=${INSTALL_LIB}/kframework/lib
plugin_include=${INSTALL_LIB}/blockchain-k-plugin/include
libff_dir=${INSTALL_LIB}/libff
libcryptopp_dir=${INSTALL_LIB}/cryptopp
export PATH="${INSTALL_BIN}:${INSTALL_LIB}:${install_k_bin}:${PATH}"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:/usr/local/lib
export K_OPTS="${K_OPTS:--Xmx16G -Xss512m}"
export KLAB_OUT="${KLAB_OUT:-~/.klab}"
# Runners
# -------
# User Commands
run_kompile() {
local kompile_opts openssl_root
kompile_opts=( "${run_file}" --directory "${backend_dir}" )
kompile_opts+=( -I "${INSTALL_INCLUDE}/kframework" -I "${plugin_include}/kframework" )
kompile_opts+=( --hook-namespaces "JSON KRYPTO BLOCKCHAIN" )
kompile_opts+=( --emit-json )
if [[ ! -z ${concrete_rules_file} ]]; then
if [[ -f ${concrete_rules_file} ]]; then
kompile_opts+=(--concrete-rules "$(cat ${concrete_rules_file} | tr '\n' ',')")
else
fatal "Concrete rules file doesn't exist: ${concrete_rules_file}"
fi
fi
case "${backend}" in
haskell) kompile_opts+=( --md-selector 'k & ! nobytes & ! node' --backend haskell) ;;
java) kompile_opts+=( --md-selector 'k & ! bytes & ! node' --backend java ) ;;
llvm) kompile_opts+=( --md-selector 'k & ! nobytes & ! node' ) ;;&
node) kompile_opts+=( --md-selector 'k & ! nobytes & ! standalone' --no-llvm-kompile ) ;;&
llvm|node) kompile_opts+=( --backend llvm )
kompile_opts+=( -ccopt -L${libff_dir}/lib -ccopt -I${libff_dir}/include )
kompile_opts+=( -ccopt ${plugin_include}/c/plugin_util.cpp )
kompile_opts+=( -ccopt ${plugin_include}/c/crypto.cpp )
kompile_opts+=( -ccopt ${plugin_include}/c/blake2.cpp )
kompile_opts+=( -ccopt -g -ccopt -std=c++14 )
kompile_opts+=( -ccopt -lff -ccopt -lcryptopp -ccopt -lsecp256k1 -ccopt -lssl -ccopt -lcrypto )
if [[ "$(uname -s)" == 'Linux' ]]; then
kompile_opts+=( -ccopt -lprocps )
elif [[ "$(uname -s)" == 'Darwin' ]]; then
openssl_root="$(brew --prefix openssl)"
brew_root="$(brew --prefix)"
kompile_opts+=( -ccopt -I${brew_root}/include -ccopt -L/${brew_root}/lib -ccopt -I${openssl_root}/include -ccopt -L${openssl_root}/lib )
kompile_opts+=( -ccopt -I${libcryptopp_dir}/include -ccopt -L/${libcryptopp_dir}/lib )
fi
;;
*) fatal "Unknown backend for kompile: ${backend}" ;;
esac
execute kompile "${kompile_opts[@]}" "$@"
}
run_krun() {
local cschedule cmode cchainid parser
check_k_install
case "$backend" in
java)
cschedule=$cSCHEDULE_kast
cmode=$cMODE_kast
cchainid=$cCHAINID_kast
parser='printf %s'
;;
*)
cschedule=$cSCHEDULE_kore
cmode=$cMODE_kore
cchainid=$cCHAINID_kore
parser='cat'
;;
esac
execute krun --directory "$backend_dir" \
-cSCHEDULE="$cschedule" -pSCHEDULE="$parser" \
-cMODE="$cmode" -pMODE="$parser" \
-cCHAINID="$cchainid" -pCHAINID="$parser" \
"$run_file" "$@"
}
run_kast() {
local output_mode
output_mode="${1:-kore}" ; shift
case "$run_file-$output_mode" in
*.json-kast) execute kast-json.py "$run_file" "$cSCHEDULE_kast" "$cMODE_kast" "$cCHAINID_kast" ;;
*.json-kore) execute kore-json.py "$run_file" "$cSCHEDULE_kore" "$cMODE_kore" "$cCHAINID_kore" ;;
*) check_k_install ; kast --directory "$backend_dir" "$run_file" --output "$output_mode" "$@" ;;
esac
}
run_prove() {
local def_module run_dir proof_args bug_report_name \
eventlog_name kprove omit_cells omit_labels klab_log pyk_args
check_k_install
bug_report_name="kevm-bug-$(basename "${run_file%-spec.k}")"
eventlog_name="${run_file}.eventlog"
kprove=kprove
proof_args=(--directory "${backend_dir}" "${run_file}")
proof_args+=( -I "${INSTALL_INCLUDE}/kframework" -I "${plugin_include}/kframework" )
! ${provex} || kprove=kprovex
${provex} || proof_args+=(--def-module "$verif_module")
! ${debug} || proof_args+=(--debug)
[[ ! -f ${concrete_rules_file} ]] || proof_args+=(--concrete-rules "$(cat ${concrete_rules_file} | tr '\n' ',')")
case "${backend}" in
haskell) ! ${bug_report} || haskell_backend_command+=(--bug-report "${bug_report_name}")
! ${profile_haskell} || haskell_backend_command+=(+RTS -l -ol${eventlog_name} -RTS)
[[ -z ${max_counterexamples} ]] || haskell_backend_command+=(--max-counterexamples ${max_counterexamples})
[[ -z ${branching_allowed} ]] || haskell_backend_command+=(--breadth ${branching_allowed})
! ${debugger} || proof_args+=(--debugger)
[[ ${#haskell_backend_command[@]} -le 1 ]] || proof_args+=(--haskell-backend-command "${haskell_backend_command[*]}")
;;
java) if ${debugger}; then
omit_cells='<substate> <jumpDests> <program> <code> <callGas> <touchedAccounts> <interimStates> <callStack> <callData> <block> <txOrder> <txPending> <messages>'
omit_labels='#mkCall________EVM #callWithCode_________EVM #create_____EVM #mkCreate_____EVM #newAddrCreate2 #finishCodeDeposit___EVM'
klab_log="$(basename "${run_file%-spec.k}").k"
proof_args+=( --state-log --state-log-path "${KLAB_OUT}/data" --state-log-id "${klab_log}" )
proof_args+=( --state-log-events OPEN,REACHINIT,REACHTARGET,REACHPROVED,RULE,SRULE,NODE,CLOSE )
proof_args+=( --output-flatten "_Map_ #And" )
proof_args+=( --output-omit "${omit_cells} ${omit_labels}" )
proof_args+=( --no-alpha-renaming --restore-original-names --no-sort-collections )
proof_args+=( --output json )
fi
[[ -z ${branching_allowed} ]] || proof_args+=(--branching-allowed ${branching_allowed})
;;
*) fatal "Unknown backend for proving! ${backend}" ;;
esac
if ${profile_haskell}; then
timeout -s INT "${profile_timeout}" ${kprove} "${proof_args[@]}" "$@" || true
execute kore-prof "${eventlog_name}" ${kore_prof_args} > "${eventlog_name}.json"
else
if ${pyk_minimize}; then
pyk_args=(${backend_dir}/*-kompiled print /dev/stdin --minimize)
[[ -z "${pyk_omit_labels}" ]] || pyk_args+=(--omit-labels "${pyk_omit_labels}")
execute ${kprove} "${proof_args[@]}" "$@" --output json | kpyk "${pyk_args[@]}"
else
execute ${kprove} "${proof_args[@]}" "$@"
fi
fi
}
run_search() {
local search_pattern
search_pattern="$1" ; shift
run_krun --search --pattern "$search_pattern" "$@"
}
view_klab() {
local klab_log
klab_log="$(basename "${run_file%-spec.k}")"
# klab often runs out of stack space when running long-running KEVM programs
# klab debug "$klab_log"
KLAB_NODE_STACK_SIZE="${KLAB_NODE_STACK_SIZE:-300000}"
node --max-old-space-size=4096 --stack-size=$KLAB_NODE_STACK_SIZE $(dirname $(which klab))/../libexec/klab-debug "$klab_log"
}
run_interpret() {
local interpreter kast output output_text output_format exit_status cmdprefix
interpreter="$backend_dir/driver-kompiled/interpreter"
kast="$(mktemp)"
output="$(mktemp)"
output_text="$(mktemp)"
trap "rm -rf $kast $output $output_text" INT TERM EXIT
exit_status=0
cmdprefix=
output_format='kore'
case "$backend" in
java) run_kast kast > "$kast"
output_format='kast'
run_file="$kast"
run_krun --parser 'cat' --output kast > "$output" || exit_status="$?"
if [[ "$unparse" == 'true' ]] && [[ "$exit_status" != '0' ]]; then
cat "$output" | "${KEVM}" kast --backend "$backend" - pretty --input "$output_format"
fi
exit "$exit_status"
;;
llvm) run_kast kore > "$kast"
if $debugger; then cmdprefix="gdb --args"; fi
execute $cmdprefix "$interpreter" "$kast" -1 "$output" "$@" || exit_status="$?"
if [[ "$unparse" == 'true' ]] && [[ "$exit_status" != '0' ]]; then
cat "$output" | "${KEVM}" kast --backend "$backend" - pretty --input "$output_format" --sort GeneratedTopCell
fi
exit "$exit_status"
;;
haskell) run_kast kore > "$kast"
execute kore-exec "$backend_dir/driver-kompiled/definition.kore" --pattern "$kast" --module ETHEREUM-SIMULATION --smt none --output "$output" || exit_status="$?"
if [[ "$unparse" == 'true' ]] && [[ "$exit_status" != '0' ]]; then
cat "$output" | "${KEVM}" kast --backend "$backend" - pretty --input "$output_format" --sort GeneratedTopCell
fi
exit "$exit_status"
;;
*) fatal "Bad backend for interpreter: '$backend'"
;;
esac
}
run_solc() {
local contract_name json_run_file
contract_name="$1" ; shift
json_run_file="${run_file}.json"
execute python3 -m kevm_pyk solc-to-k "$@" ${backend_dir}/*-kompiled "${run_file}" "${contract_name}"
}
run_gen_spec() {
execute python3 -m kevm_pyk gen-spec-modules "$@"
}
# Main
# ----
run_command="$1" ; shift
if [[ "$run_command" == 'help' ]] || [[ "$run_command" == '--help' ]] ; then
echo "
usage: ${KEVM} run [--backend (llvm|java|haskell)] [--profile|--debug] <KEVM_arg>* <pgm> <K arg>*
${KEVM} interpret [--backend (llvm)] [--no-unparse] [--profile|--debug] <KEVM_arg>* <pgm> <interpreter arg>*
${KEVM} interpret [--backend (java|haskell)] [--no-unparse] [--profile|--debug] <KEVM_arg>* <pgm>
${KEVM} kast [--backend (llvm|java|haskell)] [--profile|--debug] <KEVM_arg>* <pgm> <output format> <K arg>*
${KEVM} prove [--backend (java|haskell)] [--profile|--debug] <spec> <KEVM_arg>* <K arg>*
${KEVM} search [--backend (java|haskell)] [--profile|--debug] <pgm> <pattern> <K arg>*
${KEVM} kompile [--backend (java|llvm|haskell)] [--profile|--debug] <main> <K arg>*
${KEVM} klab-view [--profile|--debug] <spec>
${KEVM} solc-to-k [--profile|--debug] <sol> <sol contract> <solc arg>*
${KEVM} gen-spec [--profile|--debug] <kompiled-dir>
${KEVM} [help|--help|version|--version]
${KEVM} run : Run a single EVM program
${KEVM} interpret : Run JSON EVM programs without K Frontend (external parser)
${KEVM} kast : Parse an EVM program and output it in a supported format
${KEVM} prove : Run an EVM K proof
${KEVM} search : Search for a K pattern in an EVM program execution
${KEVM} kompile : Run kompile with arguments setup to include KEVM parameters as defaults
${KEVM} klab-view : View the statelog associated with a given program or spec
${KEVM} solc-to-k : Generate helper K modules for verifying a given contract
${KEVM} gen-spec : Generate specification modules for contracts declared in generated K from solc
${KEVM} help : Display this help message.
${KEVM} version : Display the versions of KEVM, K, Kore, and Z3 in use.
Note: <pgm> is a path to a file containing an EVM program/test.
<spec> is a K specification to be proved.
<main> is a K definition to be kompiled, which may include files from KEVM.
<KEVM arg> is one of [--mode (NORMAL|VMTESTS)]
[--schedule (LONDON|BERLIN|ISTANBUL|PETERSBURG|CONSTANTINOPLE|BYZANTIUM|SPURIOUS_DRAGON|TANGERINE_WHISTLE|HOMESTEAD|FRONTIER|DEFAULT)]
[--chainid NNN]
[--concrete-rules-file <concrete_rules_file>]
[--bug-report]
[--profile-haskell]
[--profile-timeout <duration>]
[--kore-prof-args \"<kore-prof arg>*\"]
[--no-provex]
[--verif-module <verification_module>]
[--pyk-minimize]
[--pyk-omit-labels <comma_separated_labels>]
[--max-counterexamples <number_counterexamples>]
[--branching-allowed <max_branches>]
[--haskell-backend-arg <haskell_backend_arg>]
<K arg> is an argument you want to pass to K.
<interpreter arg> is an argument you want to pass to the derived interpreter.
<kore-prof arg> is an argument you want to pass to kore-prof.
<output format> is the format for Kast to output the term in.
<pattern> is the configuration pattern to search for.
<def_module> is the module to take as axioms when doing verification.
<duration> is a timeout specifier as for GNU timeout.
<sol> is a Solidity source code file.
<sol contract> is the name of a Solidity contract provided in the <sol>.
<solc arg> are arguments to pass to solc.
klab-view: Make sure that the 'klab/bin' directory is on your PATH to use this option.
"
exit 0
fi
if [[ "$run_command" == 'version' ]] || [[ "$run_command" == '--version' ]]; then
notif "KEVM Version"
cat $INSTALL_LIB/version
exit 0
fi
backend="llvm"
dump=false
unparse=true
debugger=false
profile_haskell=false
profile_timeout="0"
kore_prof_args=""
bug_report=false
mode=NORMAL
schedule=LONDON
chainid=1
concrete_rules_file=
provex=true
verif_module=VERIFICATION
pyk_minimize=false
pyk_omit_labels=
max_counterexamples=
branching_allowed=
haskell_backend_command=(kore-exec)
[[ ! "$run_command" == 'prove' ]] || backend='haskell'
[[ ! "$run_command" == 'solc-to-k' ]] || backend='haskell'
kevm_port='8545'
kevm_host='127.0.0.1'
args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--debug) debug=true ; args+=("$1") ; shift ;;
--profile) profile=true ; args+=("$1") ; shift ;;
--dump) dump=true ; shift ;;
--no-unparse) unparse=false ; shift ;;
--debugger) debugger=true ; shift ;;
--profile-haskell) profile_haskell=true ; shift ;;
--profile-timeout) profile_timeout="$2" ; shift 2 ;;
--kore-prof-args) kore_prof_args="$2" ; shift 2 ;;
--bug-report) bug_report=true ; shift ;;
--backend) backend="$2" ; shift 2 ;;
--directory) backend_dir="$2" ; shift 2 ;;
--concrete-rules-file) concrete_rules_file="$2" ; shift 2 ;;
--no-provex) provex=false ; shift ;;
--verif-module) verif_module="$2" ; shift 2 ;;
--pyk-minimize) pyk_minimize=true ; shift ;;
--pyk-omit-labels) pyk_omit_labels="$2" ; shift 2 ;;
--max-counterexamples) max_counterexamples="$2" ; shift 2 ;;
--branching-allowed) branching_allowed="$2" ; shift 2 ;;
--haskell-backend-arg) haskell_backend_command+=("$2") ; shift 2 ;;
--mode) mode="$2" ; shift 2 ;;
--schedule) schedule="$2" ; shift 2 ;;
--chainid) chainid="$2" ; shift 2 ;;
-p|--port) kevm_port="$2" ; shift 2 ;;
-h|--host|--hostname) kevm_host="$2" ; shift 2 ;;
*) args+=("$1") ; shift ;;
esac
done
[[ "${#args[@]}" -le 0 ]] || set -- "${args[@]}"
backend_dir="${backend_dir:-$INSTALL_LIB/$backend}"
! $profile_haskell || [[ "$backend" == haskell ]] || fatal "Option --profile-haskell only usable with --backend haskell!"
[[ $profile_timeout = "0" ]] || $profile_haskell || fatal "Option --profile-timeout only usable with --profile-haskell!"
[[ $kore_prof_args = "" ]] || $profile_haskell || fatal "Option --kore-prof-args only usable with --profile-haskell!"
if ${pyk_minimize}; then
! ${debugger} || fatal "Option --pyk-minimize not usable with --debugger!"
! ${profile_haskell} || fatal "Option --pyk-minimize not usable with --profile-haskell!"
elif [[ ! -z ${pyk_omit_labels} ]]; then
fatal "Option --pyk-label-omit only usable with --pyk-minimize!"
fi
# get the run file
if [[ "${run_command}" != 'gen-spec' ]]; then
run_file="$1" ; shift
if [[ "${run_file}" == '-' ]]; then
tmp_input="$(mktemp)"
trap "rm -rf ${tmp_input}" INT TERM EXIT
cat - > "${tmp_input}"
run_file="${tmp_input}"
fi
[[ -f "${run_file}" ]] || fatal "File does not exist: ${run_file}"
fi
cMODE_kore="Lbl${mode}{}()"
cSCHEDULE_kore="Lbl${schedule}'Unds'EVM{}()"
cCHAINID_kore="\dv{SortInt{}}(\"${chainid}\")"
cMODE_kast="\`${mode}\`(.KList)"
cSCHEDULE_kast="\`${schedule}_EVM\`(.KList)"
cCHAINID_kast="#token(\"${chainid}\",\"Int\")"
! ${debug} || set -x
case "$run_command-$backend" in
kompile-@(java|llvm|haskell|node) ) run_kompile "$@" ;;
run-@(java|llvm|haskell) ) run_krun "$@" ;;
kast-@(java|llvm|haskell) ) run_kast "$@" ;;
interpret-@(llvm|haskell|java) ) run_interpret "$@" ;;
prove-@(java|haskell) ) run_prove "$@" ;;
search-@(java|haskell) ) run_search "$@" ;;
klab-view-* ) view_klab "$@" ;;
solc-to-k-* ) run_solc "$@" ;;
gen-spec-* ) run_gen_spec "$@" ;;
*) ${KEVM} help ; fatal "Unknown command on backend: $run_command $backend" ;;
esac