-
Notifications
You must be signed in to change notification settings - Fork 33
/
kiele
executable file
·297 lines (259 loc) · 10.7 KB
/
kiele
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
#!/usr/bin/env bash
set -euo pipefail
notif() { echo "== $@" >&2 ; }
fatal() { echo "[FATAL] $@" ; exit 1 ; }
INSTALL_BIN="$(cd $(dirname $0) && pwd)"
INSTALL_LIB="$(dirname ${INSTALL_BIN})/lib/kiele"
FIREFLY_ENDPOINT=${FIREFLY_ENDPOINT:-https://fireflyblockchain.com}
export PATH="${INSTALL_BIN}:${INSTALL_LIB}:${PATH}"
export PATH="${PATH}:${INSTALL_LIB}/standalone/iele-testing-kompiled"
export PATH="${PATH}:${INSTALL_LIB}/check/well-formedness-kompiled"
export PATH="${PATH}:${INSTALL_LIB}/node/iele-testing-kompiled"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:/usr/local/lib
export PYTHONPATH=${PYTHONPATH:-}:"$(dirname $0)/../lib/kiele"
check_k_install() {
type -p kore-exec &> /dev/null \
|| fatal "Must have K installed! See https://github.com/kframework/k/releases."
type -p kast &> /dev/null \
|| fatal "Must have K installed! See https://github.com/kframework/k/releases."
type -p krun &> /dev/null \
|| fatal "Must have K installed! See https://github.com/kframework/k/releases."
}
check_token() { [ -v FIREFLY_TOKEN ] || fatal "FIREFLY_TOKEN environment variable isn't set!" ; }
check_listener() {
if ! nc -z "$kiele_host" "$kiele_port"; then
fatal "No listener found on port $kiele_port"
fi
}
make_temp_trap() {
local tmpdesc tmpfile
tmpdesc="$1" ; shift
tmpfile="$(mktemp)"
if $debug; then
notif "$tmpdesc: $tmpfile"
else
trap "rm -rf $tmpfile" INT TERM EXIT
fi
echo "$tmpfile"
}
# Runners
# -------
### Does not depend on K Install
run_assemble() {
iele-assemble "$run_file" "$@"
}
run_pyiele() {
python3 -m pyiele --host "$kiele_host" --port "$kiele_port" --output "$output_path" "$@"
}
run_interpret() {
local kast output status
kast=$(make_temp_trap kast)
output=$(make_temp_trap output)
status=0
kore-json.py "$run_file" "$cschedule" "$cmode" "$cenablecoverage" > "$kast"
case "$backend" in
standalone)
interpret_cmd=(iele-interpreter "$kast" "$depth" "$output" "$@")
if $debug; then
gdb --args "${interpret_cmd[@]}"
else
"${interpret_cmd[@]}" || status="$?"
fi
;;
haskell)
check_k_install
kore_cmd=(kore-exec "$backend_dir/$definition-kompiled/definition.kore" --pattern "$kast" --module "$module" --output "$output")
[[ "$depth" == "-1" ]] || kore_cmd+=(--depth "$depth")
kore_cmd+=("$@")
"${kore_cmd[@]}" || status="$?"
;;
*)
fatal "Unknown backend: $backend"
;;
esac
if $unparse || [[ "$status" != '0' ]]; then
kprint "$backend_dir/$definition-kompiled" "$output"
printf "\n"
exit "$status"
fi
}
run_krun_internal() {
local krun_command
check_k_install
krun_command=(krun --directory "${backend_dir}")
krun_command+=(-cSCHEDULE="${cschedule}" -pSCHEDULE="${parser}")
[[ "${depth}" == '-1' ]] || krun_command+=(--depth "${depth}")
"${krun_command[@]}" "${run_file}" "$@"
}
run_krun() {
run_krun_internal -cMODE="$cmode" -pMODE="${parser}" -cENABLECOVERAGE="$cenablecoverage" -pENABLECOVERAGE="${parser}" "$@"
}
run_check() {
if run_krun_internal --output none "$@"; then
notif "Contract is well-formed."
exit 0
else
notif "Contract is ill-formed."
exit 1
fi
}
run_vm() {
cmd=(exec iele-vm "$kiele_port" "$kiele_host")
[[ $enablecoverage == 'true' ]] && cmd+=(--coverage)
"${cmd[@]}"
}
run_coverage() {
notif "Generating coverage json from and client on port $kiele_port"
check_listener
run_pyiele coverage $@
}
run_generate_report() {
if [[ -f $(dirname $(which kiele))/static-report.html ]]; then
report_template=$(dirname $(which kiele))/static-report.html
else
report_template=$INSTALL_LIB/static-report.html
fi
kiele-generate-report.py "$report_template" "$run_file" "$output_path" "$create_archive"
}
run_upload_report() {
check_token
echo "Uploading report"
kiele generate-report $run_file --create-archive # This will generate report.zip archive file
curl --request POST --fail \
--form access-token="$FIREFLY_TOKEN" \
--form 'status=pass' \
--form 'type=iele' \
--form 'file=@report.zip' \
"$FIREFLY_ENDPOINT/report"
rm report.zip
}
run_compile() {
run_pyiele compile "$@"
}
run_testrunner() {
local run_file cmd
check_listener
cmd=(run_pyiele test)
if [[ $# -gt 0 ]] && ([[ -f "$1" ]] || [[ -d "$1" ]]); then
run_file="$1"; shift
if [[ "$run_file" == *.py ]]; then
cmd=(python3 "$run_file")
else
cmd+=(--file "$run_file")
fi
fi
"${cmd[@]}" "$@"
}
run_blackbox() {
check_listener
run_pyiele blackbox "$@"
}
# Main
# ----
run_command="$1" ; shift
if [[ "$run_command" == 'help' ]] || [[ "$run_command" == '--help' ]] ; then
echo "
usage: $0 assemble <pgm> <assemble arg>*
$0 interpret [--backend (standalone|haskell)] <KIELE arg>* <pgm> <interpret arg>*
$0 krun [--backend (standalone|haskell|node|check)] <KIELE arg>* <pgm> <K arg>*
$0 check <KIELE arg>* <pgm> <K arg>*
$0 vm <KIELE arg>*
$0 coverage <combined.json path> [-p|--port <client port>] [-o|--output <Report output path>]
$0 generate-report [--create-archive] <Report json path> [-o|--output <Report output path>]
$0 upload <Report json path>
$0 compile <COMPILE arg>*
$0 test <TEST arg>*
$0 blackbox <BLACKBOX arg>*
$0 [help|--help|version|--version]
$0 assemble : Translate IELE assembly <pgm to IELE bytecode
$0 interpret : Run given input JSON or IELE <pgm>
$0 krun : Run given input <pgm> using 'krun'
$0 check : Run the IELE well-formedness checker on <pgm>
$0 vm : Run the KIELE VM server.
$0 coverage : Generates a report file containing contract information and coverage data.
$0 generate-report : Generate the static HTML report.
$0 upload : Upload the HTML report to the fireflyblockchain.com website.
$0 compile : Compile solidity contracts using isolc. If no path is provided, all the found .sol files will be compiled. For more information run 'kiele compile --help'.
$0 test : Runs either a python or a solidity test file. If no path is provided, will run all the tests found under the test folder. For more information run 'kiele test --help'.
$0 blackbox : Attempt to find new test inputs via blackbox random testing. For more information run 'kiele blackbox --help'.
$0 help : Display this help message.
$0 version : Display the versions of KIELE in use.
Note: <pgm> is a path to a file containing a IELE program/test (possible in JSON format)
<assemble arg> is an argument to the assembler
<interpret arg> is an argument you want to pass to the interpreter
<K arg> is an argument you want to pass to K
<KIELE arg> is one of (--no-unparse|--debug|--host|--port|--mode|--coverage
|--schedule|--module|--definition|--depth)
<combined.json path> is the path to the build artifacts from 'isolc --combined-json'
<Report json path> is the path to the report json file
<Report output path> is the output HTML report file path (default: \$uuid.html)
"
exit 0
fi
if [[ "$run_command" == 'version' ]] || [[ "$run_command" == '--version' ]] ; then
notif "KIELE Version"
cat $INSTALL_LIB/version
exit 0
fi
backend='standalone'
[[ "$run_command" != 'check' ]] || backend='check'
debug=false
unparse=true
mode='NORMAL'
schedule='DANSE'
enablecoverage='false'
kiele_port='8545'
kiele_host='127.0.0.1'
module='IELE-TESTING'
definition='iele-testing'
depth=-1
args=()
create_archive='false'
parser='cat'
output_path='/dev/stdout'
while [[ $# -gt 0 ]]; do
arg="$1"
case $arg in
--backend) backend="$2" ; shift 2 ;;
--debug) debug=true ; shift ;;
--no-unparse) unparse=false ; shift ;;
--mode) mode="$2" ; shift 2 ;;
--module) module="$2" ; shift 2 ;;
--definition) definition="$2" ; shift 2 ;;
--schedule) schedule="$2" ; shift 2 ;;
--depth) depth="$2" ; shift 2 ;;
--coverage) enablecoverage='true' ; shift ;;
--create-archive) create_archive='true' ; shift ;;
--parser) parser="$2" ; shift 2 ;;
-o|--output) output_path="$2" ; shift 2 ;;
-p|--port) kiele_port="$2" ; shift 2 ;;
-h|--host) kiele_host="$2" ; shift 2 ;;
*) args+=("$1") ; shift ;;
esac
done
if [[ "${#args[@]}" -gt 0 ]]; then
set -- "${args[@]}"
fi
! $debug || set -x
backend_dir="${backend_dir:-$INSTALL_LIB/$backend}"
cmode='Lbl'$mode'{}()'
cschedule='Lbl'$schedule'{}()'
cenablecoverage='\dv{SortBool{}}("'$enablecoverage'")'
if [[ "$run_command" != 'vm' ]] && [[ "$run_command" != 'coverage' ]] && [[ "$run_command" != 'compile' ]] && [[ "$run_command" != 'test' ]] && [[ "$run_command" != 'blackbox' ]]; then
run_file="$1" ; shift
[[ -f "$run_file" ]] || [[ "$run_file" == '-' ]] || fatal "File does not exist: $run_file"
fi
case "$run_command" in
assemble) run_assemble "$@" ;;
interpret) run_interpret "$@" ;;
krun) run_krun "$@" ;;
check) run_check "$@" ;;
vm) run_vm "$@" ;;
coverage) run_coverage "$@" ;;
generate-report) run_generate_report "$@" ;;
upload) run_upload_report "$@" ;;
compile) run_compile "$@" ;;
blackbox) run_blackbox "$@" ;;
test) run_testrunner "$@" ;;
*) $0 help ; fatal "Unknown command: $run_command" ;;
esac