forked from iotaledger/wasp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wasmvm.go
438 lines (376 loc) · 10.2 KB
/
wasmvm.go
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
// Copyright 2020 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
package wasmhost
import (
"encoding/binary"
"errors"
"fmt"
"time"
"github.com/iotaledger/wasp/packages/wasmvm/wasmlib/go/wasmlib"
"github.com/iotaledger/wasp/packages/wasmvm/wasmlib/go/wasmlib/wasmtypes"
)
const (
defaultTimeout = 5 * time.Second
FuncAbort = "abort"
FuncFdWrite = "fd_write"
FuncHostStateGet = "hostStateGet"
FuncHostStateSet = "hostStateSet"
ModuleEnv = "env"
ModuleWasi1 = "wasi_unstable"
ModuleWasi2 = "wasi_snapshot_preview1"
ModuleWasmLib = "WasmLib"
)
var (
// DisableWasmTimeout can be used to disable the annoying timeout during debugging
DisableWasmTimeout = false
// HostTracing turns on debug tracing for ScHost calls
HostTracing = false
// WasmTimeout set this to non-zero for a one-time override of the defaultTimeout
WasmTimeout = 0 * time.Second
)
type WasmVM interface {
GasBudget(budget uint64) error
GasBurned() uint64
GasEnable(enable bool)
Instantiate() error
Interrupt()
LinkHost(proc *WasmProcessor) error
LoadWasm(wasmData []byte) error
NewInstance() WasmVM
RunFunction(functionName string, args ...interface{}) error
RunScFunction(index int32) error
UnsafeMemory() []byte
VMGetBytes(offset int32, size int32) []byte
VMGetSize() int32
VMSetBytes(offset int32, size int32, bytes []byte) int32
}
type WasmVMBase struct {
cachedResult []byte
gasEnabled bool
panicErr error
proc *WasmProcessor
timeoutStarted bool
}
func (vm *WasmVMBase) GasBudget(budget uint64) error {
// ignore gas budget
return nil
}
func (vm *WasmVMBase) GasBurned() uint64 {
// burn nothing
return 0
}
func (vm *WasmVMBase) GasEnable(enable bool) {
vm.gasEnabled = enable
}
//nolint:unparam
func (vm *WasmVMBase) getContext(id int32) *WasmContext {
return vm.proc.GetContext(id)
}
func (vm *WasmVMBase) HostAbort(errMsg, fileName, line, col int32) {
vm.reportGasBurned()
defer vm.wrapUp()
// crude implementation assumes texts to only use ASCII part of UTF-16
impl := vm.proc.vm
// null-terminated UTF-16 error message
str1 := make([]byte, 0)
ptr := impl.VMGetBytes(errMsg, 2)
for i := errMsg; ptr[0] != 0; i += 2 {
str1 = append(str1, ptr[0])
ptr = impl.VMGetBytes(i, 2)
}
// null-terminated UTF-16 file name
str2 := make([]byte, 0)
ptr = impl.VMGetBytes(fileName, 2)
for i := fileName; ptr[0] != 0; i += 2 {
str2 = append(str2, ptr[0])
ptr = impl.VMGetBytes(i, 2)
}
panic(fmt.Sprintf("AssemblyScript panic: %s (%s %d:%d)", string(str1), string(str2), line, col))
}
func (vm *WasmVMBase) HostFdWrite(_fd, iovs, _size, written int32) int32 {
vm.reportGasBurned()
defer vm.wrapUp()
ctx := vm.getContext(0)
ctx.log().Debugf("HostFdWrite(...)")
impl := vm.proc.vm
// very basic implementation that expects fd to be stdout and iovs to be only one element
ptr := impl.VMGetBytes(iovs, 8)
text := int32(binary.LittleEndian.Uint32(ptr[0:4]))
size := int32(binary.LittleEndian.Uint32(ptr[4:8]))
// msg := vm.impl.VMGetBytes(text, size)
// fmt.Print(string(msg))
ptr = make([]byte, 4)
binary.LittleEndian.PutUint32(ptr, uint32(size))
impl.VMSetBytes(written, size, ptr)
// strip off "panic: " prefix and call sandbox panic function
vm.HostStateGet(0, wasmlib.FnPanic, text+7, size)
return size
}
func (vm *WasmVMBase) HostStateGet(keyRef, keyLen, valRef, valLen int32) int32 {
vm.reportGasBurned()
defer vm.wrapUp()
ctx := vm.getContext(0)
impl := vm.proc.vm
if HostTracing {
vm.traceGet(ctx, keyRef, keyLen, valRef, valLen)
}
// only check for existence ?
if valLen < 0 {
key := impl.VMGetBytes(keyRef, keyLen)
if ctx.StateExists(key) {
return 0
}
// missing key is indicated by -1
return -1
}
// get value for key request, or get cached result request (keyLen == 0)
if keyLen >= 0 {
if keyLen > 0 {
// retrieve value associated with key
key := impl.VMGetBytes(keyRef, keyLen)
vm.cachedResult = ctx.StateGet(key)
}
if vm.cachedResult == nil {
return -1
}
return impl.VMSetBytes(valRef, valLen, vm.cachedResult)
}
// sandbox func call request, keyLen is func nr
params := impl.VMGetBytes(valRef, valLen)
vm.cachedResult = ctx.Sandbox(keyLen, params)
return int32(len(vm.cachedResult))
}
func (vm *WasmVMBase) HostStateSet(keyRef, keyLen, valRef, valLen int32) {
vm.reportGasBurned()
defer vm.wrapUp()
ctx := vm.getContext(0)
impl := vm.proc.vm
if HostTracing {
vm.traceSet(ctx, keyRef, keyLen, valRef, valLen)
}
// export name?
if keyRef == 0 {
name := string(impl.VMGetBytes(valRef, valLen))
if keyLen < 0 {
// ExportWasmTag, log the wasm tag name
ctx.proc.log.Infof(name)
return
}
ctx.ExportName(keyLen, name)
return
}
key := impl.VMGetBytes(keyRef, keyLen)
// delete key ?
if valLen < 0 {
ctx.StateDelete(key)
return
}
// set key
value := impl.VMGetBytes(valRef, valLen)
ctx.StateSet(key, value)
}
func (vm *WasmVMBase) Instantiate() error {
return errors.New("cannot be cloned")
}
func (vm *WasmVMBase) LinkHost(proc *WasmProcessor) error {
// trick vm into thinking it doesn't have to start the timeout timer
// useful when debugging to prevent timing out on breakpoints
vm.timeoutStarted = DisableWasmTimeout
vm.proc = proc
return nil
}
func (vm *WasmVMBase) reportGasBurned() {
if vm.gasEnabled {
ctx := vm.proc.GetContext(0)
vm.proc.vm.GasBudget(ctx.GasBudget())
}
}
func (vm *WasmVMBase) Run(runner func() error) (err error) {
defer func() {
r := recover()
if r == nil {
return
}
// could be the wrong panic message due to a WasmTime bug, so we always
// rethrow our intercepted first panic instead of WasmTime's last panic
if vm.panicErr != nil {
panic(vm.panicErr)
}
panic(r)
}()
if vm.timeoutStarted {
// no need to wrap nested calls in timeout code
err = runner()
if vm.panicErr != nil {
err = vm.panicErr
vm.panicErr = nil
}
return err
}
timeout := defaultTimeout
if WasmTimeout != 0 {
timeout = WasmTimeout
WasmTimeout = 0
}
done := make(chan bool, 2)
// start timeout handler
go func() {
select {
case <-done: // runner was done before timeout
case <-time.After(timeout):
// timeout: interrupt Wasm
vm.proc.vm.Interrupt()
// wait for runner to finish
<-done
}
}()
vm.timeoutStarted = true
err = runner()
done <- true
vm.timeoutStarted = false
if vm.panicErr != nil {
err = vm.panicErr
vm.panicErr = nil
}
return err
}
func (vm *WasmVMBase) VMGetBytes(offset, size int32) []byte {
ptr := vm.proc.vm.UnsafeMemory()
bytes := make([]byte, size)
copy(bytes, ptr[offset:offset+size])
return bytes
}
func (vm *WasmVMBase) VMGetSize() int32 {
ptr := vm.proc.vm.UnsafeMemory()
return int32(len(ptr))
}
func (vm *WasmVMBase) VMSetBytes(offset, size int32, bytes []byte) int32 {
if size != 0 {
ptr := vm.proc.vm.UnsafeMemory()
copy(ptr[offset:offset+size], bytes)
}
return int32(len(bytes))
}
func (vm *WasmVMBase) traceGet(ctx *WasmContext, keyRef, keyLen, valRef, valLen int32) {
impl := vm.proc.vm
// only check for existence ?
if valLen < 0 {
key := impl.VMGetBytes(keyRef, keyLen)
ctx.log().Debugf("StateExists(%s) = %v", vm.traceKey(key), ctx.StateExists(key))
return
}
// get value for key request, or get cached result request (keyLen == 0)
if keyLen >= 0 {
if keyLen == 0 {
ctx.log().Debugf(" => %s", vm.traceVal(vm.cachedResult))
return
}
// retrieve value associated with key
key := impl.VMGetBytes(keyRef, keyLen)
ctx.log().Debugf("StateGet(%s)", vm.traceKey(key))
return
}
// sandbox func call request, keyLen is func nr
if keyLen == wasmlib.FnLog {
return
}
params := impl.VMGetBytes(valRef, valLen)
ctx.log().Debugf("Sandbox(%s)", vm.traceSandbox(keyLen, params))
}
func (vm *WasmVMBase) traceSandbox(funcNr int32, params []byte) string {
name := sandboxFuncNames[-funcNr]
if name[0] == '$' {
return name[1:] + ", " + string(params)
}
if name[0] != '#' {
return name
}
return name[1:] + ", " + hex(params)
}
func (vm *WasmVMBase) traceSet(ctx *WasmContext, keyRef, keyLen, valRef, valLen int32) {
impl := vm.proc.vm
// export name?
if keyRef == 0 {
name := string(impl.VMGetBytes(valRef, valLen))
ctx.log().Debugf("ExportName(%d, %s)", keyLen, name)
return
}
key := impl.VMGetBytes(keyRef, keyLen)
// delete key ?
if valLen < 0 {
ctx.log().Debugf("StateDelete(%s)", vm.traceKey(key))
return
}
// set key
val := impl.VMGetBytes(valRef, valLen)
ctx.log().Debugf("StateSet(%s, %s)", vm.traceKey(key), vm.traceVal(val))
}
func (vm *WasmVMBase) traceKey(key []byte) string {
name := ""
for i, b := range key {
if b == '.' {
return string(key[:i+1]) + hex(key[i+1:])
}
if b == '#' {
name = string(key[:i+1])
j := i + 1
for ; (key[j] & 0x80) != 0; j++ {
}
dec := wasmtypes.NewWasmDecoder(key[i+1 : j+1])
index := wasmtypes.Uint64Decode(dec)
name += wasmtypes.Uint64ToString(index)
if j+1 == len(key) {
return name
}
return name + "..." + hex(key[j+1:])
}
}
return `"` + string(key) + `"`
}
func (vm *WasmVMBase) traceVal(val []byte) string {
for _, b := range val {
if b < ' ' || b > '~' {
return hex(val)
}
}
return string(val)
}
// wrapUp is used in every host function to catch any panic.
// It will save the first panic it encounters in the WasmVMBase so that
// the caller of the Wasm function can retrieve the correct error.
// This is a workaround to WasmTime saving the *last* panic instead of
// the first, thereby reporting the wrong panic error sometimes
// wrapUp will also update the Wasm code that initiated the call with
// the remaining gas budget (the Wasp node may have burned some)
func (vm *WasmVMBase) wrapUp() {
panicMsg := recover()
if panicMsg == nil {
if vm.gasEnabled {
// update gas budget
ctx := vm.getContext(0)
ctx.GasBurned(vm.proc.vm.GasBurned())
}
return
}
// panic means no need to update gas budget
if vm.panicErr == nil {
switch msg := panicMsg.(type) {
case error:
vm.panicErr = msg
default:
vm.panicErr = fmt.Errorf("%v", msg)
}
}
// rethrow and let nature run its course...
panic(panicMsg)
}
// hex returns a hex string representing the byte buffer
func hex(buf []byte) string {
const hexa = "0123456789abcdef"
res := make([]byte, len(buf)*2)
for i, b := range buf {
res[i*2] = hexa[b>>4]
res[i*2+1] = hexa[b&0x0f]
}
return string(res)
}