/
abi.go
468 lines (410 loc) · 11.1 KB
/
abi.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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package wasmgo
import (
"bytes"
"crypto/rand"
"encoding/binary"
"log"
"math"
"time"
"github.com/perlin-network/life/exec"
)
func (w *WasmGo) loadString(addr int32) string {
saddr := w.getInt64(addr)
leng := w.getInt64(addr + 4)
return string(w.vm.Memory[saddr : saddr+leng])
}
// TODO properly endian
func (w *WasmGo) setUint8(addr int32, val uint8) {
w.vm.Memory[addr] = byte(val)
}
func (w *WasmGo) getFloat64(addr int32) float64 {
mem := w.vm.Memory[addr : addr+8]
var result float64
err := binary.Read(bytes.NewReader(mem), binary.LittleEndian, &result)
if err != nil {
panic(err)
}
return result
}
func (w *WasmGo) setFloat64(addr int32, val float64) {
data := make([]byte, 0, 8)
buf := bytes.NewBuffer(data)
err := binary.Write(buf, binary.LittleEndian, val)
if err != nil {
panic(err)
}
_, err = w.writeMem(addr, buf.Bytes())
if err != nil {
panic(err)
}
}
func (w *WasmGo) getInt32(addr int32) int32 {
mem := w.vm.Memory[addr : addr+4]
var result int32
err := binary.Read(bytes.NewReader(mem), binary.LittleEndian, &result)
if err != nil {
panic(err)
}
return result
}
func (w *WasmGo) setInt32(addr int32, val int32) {
data := make([]byte, 0, 4)
buf := bytes.NewBuffer(data)
err := binary.Write(buf, binary.LittleEndian, val)
if err != nil {
panic(err)
}
_, err = w.writeMem(addr, buf.Bytes())
if err != nil {
panic(err)
}
}
func (w *WasmGo) getUint32(addr int32) uint32 {
mem := w.vm.Memory[addr : addr+4]
var result uint32
err := binary.Read(bytes.NewReader(mem), binary.LittleEndian, &result)
if err != nil {
panic(err)
}
return result
}
func (w *WasmGo) setUint32(addr int32, val uint32) {
data := make([]byte, 0, 4)
buf := bytes.NewBuffer(data)
err := binary.Write(buf, binary.LittleEndian, val)
if err != nil {
panic(err)
}
_, err = w.writeMem(addr, buf.Bytes())
if err != nil {
panic(err)
}
}
func (w *WasmGo) getInt64(addr int32) int64 {
mem := w.vm.Memory[addr : addr+8]
var result int64
err := binary.Read(bytes.NewReader(mem), binary.LittleEndian, &result)
if err != nil {
panic(err)
}
return result
}
func (w *WasmGo) setInt64(addr int32, val int64) {
data := make([]byte, 0, 8)
buf := bytes.NewBuffer(data)
err := binary.Write(buf, binary.LittleEndian, val)
if err != nil {
panic(err)
}
_, err = w.writeMem(addr, buf.Bytes())
if err != nil {
panic(err)
}
}
func (w *WasmGo) loadValue(addr int32) interface{} {
f := w.getFloat64(addr)
if f == 0 {
return Undefined
}
if !math.IsNaN(f) {
return f
}
id := w.getUint32(addr)
return w.values[id]
}
func (w *WasmGo) storeValue(addr int32, v interface{}) {
const nanHead = 0x7FF80000
if i, ok := v.(int); ok {
v = float64(i)
}
if i, ok := v.(uint); ok {
v = float64(i)
}
if v, ok := v.(float64); ok {
if math.IsNaN(v) {
w.setUint32(addr+4, nanHead)
w.setUint32(addr, 0)
return
}
if v == 0 {
w.setUint32(addr+4, nanHead)
w.setUint32(addr, 1)
return
}
w.setFloat64(addr, v)
return
}
switch v {
case Undefined:
w.setFloat64(addr, 0)
return
case nil:
w.setUint32(addr+4, nanHead)
w.setUint32(addr, 2)
return
case true:
w.setUint32(addr+4, nanHead)
w.setUint32(addr, 3)
return
case false:
w.setUint32(addr+4, nanHead)
w.setUint32(addr, 4)
return
}
ref, ok := w.refs[v]
if !ok {
ref = len(w.values)
w.values = append(w.values, v)
w.refs[v] = ref
}
typeFlag := 0
switch v.(type) {
case string:
typeFlag = 1
// TODO symbol
// TODO function
}
w.setUint32(addr+4, uint32(nanHead|typeFlag))
w.setUint32(addr, uint32(ref))
}
func (w *WasmGo) loadSliceOfValues(addr int32) []interface{} {
array := int(w.getInt64(addr))
leng := int(w.getInt64(addr + 8))
result := make([]interface{}, leng)
for i := range result {
result[i] = w.loadValue(int32(array + i*8))
}
return result
}
// goRuntimeWasmExit implements the go runtime function runtime.wasmExit. It uses
// the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goRuntimeWasmExit(code int32)
func (w *WasmGo) goRuntimeWasmExit(sp int32) {
exitCode := w.getInt32(sp + 8)
w.Exited = true
w.StatusCode = exitCode
}
// goRuntimeWasmWrite wraps the write function.
//
// This has the effective type of:
//
// func (w *WasmGo) goRuntimeWasmWrite(fd int64, ptr int64, n int32)
func (w *WasmGo) goRuntimeWasmWrite(sp int32) {
fd := w.getInt64(sp + 8)
ptr := w.getInt64(sp + 16)
n := w.getInt32(sp + 24)
cnt, err := w.Files()[fd].Write(w.vm.Memory[ptr : ptr+int64(n)])
if err != nil {
log.Printf("goRuntimeWasmWrite(%d (%s), %x, %d): %v", fd, w.Files()[fd].Name(), ptr, n, err)
}
if int32(cnt) != n {
log.Printf("goRuntimeWasmWrite(%d (%s), %x, %d): cnt: %d, err: nil", fd, w.Files()[fd].Name(), ptr, n, cnt)
}
}
// goRuntimeNanotime implements the go runtime function runtime.nanotime. It uses
// the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goRuntimeNanotime() int64
func (w *WasmGo) goRuntimeNanotime(sp int32) {
now := time.Now().UnixNano()
w.setInt64(sp+8, int64(now))
}
// goRuntimeWalltime implements the go runtime function runtime.walltime. It uses
// the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goRuntimeWalltime() (int64, int32)
func (w *WasmGo) goRuntimeWalltime(sp int32) {
now := time.Now()
w.setInt64(sp+8, now.Unix())
w.setInt32(sp+16, int32(now.Nanosecond()))
}
// goSyscallJSValueGet implements the go runtime function syscall/js.valueGet. It uses
// the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueGet(ref, string) ref
func (w *WasmGo) goSyscallJSValueGet(sp int32) {
name := string(w.goLoadSlice(sp + 16))
result, ok := w.loadValue(sp + 8).(*Object).Props[name]
if !ok {
log.Panicf("ref doesn't have %s", name)
}
w.storeValue(sp+32, result)
}
// goSyscallJSValueSet implements the go runtime function syscall/js.valueSet. It uses
// the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueSet(v ref, p string, x ref)
func (w *WasmGo) goSyscallJSValueSet(sp int32) {
w.loadValue(sp + 8).(*Object).Props[w.loadString(sp+16)] = w.loadValue(sp + 32)
}
// goSyscallJSValueIndex implements the go runtime function syscall/js.valueIndex. It
// uses the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueIndex(v ref, i int) ref
func (w *WasmGo) goSyscallJSValueIndex(sp int32) {
result := (*w.loadValue(sp + 8).(*[]interface{}))[w.getInt64(sp+16)]
w.storeValue(sp+24, result)
}
// goSyscallJSValueCall implements the go runtime function syscall/js.valueCall. It
// uses the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueCall(v ref, m string, args []ref) (ref, bool)
func (w *WasmGo) goSyscallJSValueCall(sp int32) {
// TODO error handling
v := w.loadValue(sp + 8).(*Object)
name := w.loadString(sp + 16)
m, ok := v.Props[name]
if !ok {
panic("missing method: " + name) // TODO
}
args := w.loadSliceOfValues(sp + 32)
result := m.(func(*Object, []interface{}) interface{})(v, args)
// TODO getsp
w.storeValue(sp+56, result)
w.setUint8(sp+64, 1)
}
// goSyscallJSValueNew implements the go runtime function syscall/js.valueNew. It
// uses the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueNew(v ref, args []ref) (ref, bool)
func (w *WasmGo) goSyscallJSValueNew(sp int32) {
// TODO error handling
v := w.loadValue(sp + 8)
args := w.loadSliceOfValues(sp + 16)
result := v.(*Object).New(args)
// TODO getsp
w.storeValue(sp+40, result)
w.setUint8(sp+48, 1)
}
// goSyscallJSValueLength implements the go runtime function syscall/js.valueLength.
// It uses the Go abi.
//
// This has the effective type of:
//
// func (w *WasmGo) goSyscallJSValueLength(v ref) int
func (w *WasmGo) goSyscallJSValueLength(sp int32) {
array := w.loadValue(sp + 8).(*[]interface{})
w.setInt64(sp+16, int64(len(*array)))
}
// goLoadSlice loads a Go slice out of wasm memory. It uses the the Go abi.
func (w *WasmGo) goLoadSlice(sp int32) []byte {
arr := w.getInt64(sp)
len := w.getInt64(sp + 8)
return w.vm.Memory[arr : arr+len]
}
func (w *WasmGo) goRuntimeGetRandomData(sp int32) {
data := w.goLoadSlice(sp + 8)
_, err := rand.Read(data)
if err != nil {
panic(err)
}
_, err = w.writeMem(sp+8, data)
if err != nil {
panic(err)
}
}
func (w *WasmGo) notImplemented(module, field string) goABIFunc {
return func(sp int32) {
w.vm.PrintStackTrace()
log.Panicf("not implemented: %s %s", module, field)
}
}
type goABIFunc func(int32)
var (
// ImplementedGoABIFuncs is not used at runtime. This is used for documentation only.
ImplementedGoABIFuncs = []string{
"debug", // implemented
"runtime.wasmExit", // implemented
"runtime.wasmWrite", // implemented
"runtime.nanotime", // implemented
"runtime.walltime", // implemented
"runtime.scheduleTimeoutEvent", // stub
"runtime.clearTimeoutEvent", // stub
"runtime.getRandomData", // implemented
"syscall/js.stringVal", // stub
"syscall/js.valueGet", // implemented
"syscall/js.valueSet", // implemented
"syscall/js.valueIndex", // implemented
"syscall/js.valueSetIndex", // stub
"syscall/js.valueCall", // implemented
"syscall/js.valueInvoke", // stub
"syscall/js.valueNew", // implemented
"syscall/js.valueLength", // implemented
"syscall/js.valuePrepareString", // stub
"syscall/js.valueLoadString", // stub
"syscall/js.valueInstanceOf", // stub
}
)
func (w *WasmGo) runGoABI(doer func(int32)) exec.FunctionImport {
return func(vm *exec.VirtualMachine) int64 {
if w.vm == nil {
w.vm = vm
}
f := vm.GetCurrentFrame()
sp := int32(f.Locals[0])
doer(sp)
return 0
}
}
// ResolveGlobal does nothing, currently.
func (w *WasmGo) ResolveGlobal(module, field string) int64 { return 0 }
func (w *WasmGo) ResolveFunc(module, field string) exec.FunctionImport {
val := w.Process.ResolveFunc(module, field)
if val != nil {
return val
}
switch module {
case "go":
switch field {
case "debug":
return func(vm *exec.VirtualMachine) int64 {
f := vm.GetCurrentFrame()
log.Printf("debug: %x %d", f.Locals[0], f.Locals[0])
return 0
}
case "runtime.wasmExit":
return w.runGoABI(w.goRuntimeWasmExit)
case "runtime.wasmWrite":
return w.runGoABI(w.goRuntimeWasmWrite)
case "runtime.nanotime":
return w.runGoABI(w.goRuntimeNanotime)
case "runtime.walltime":
return w.runGoABI(w.goRuntimeWalltime)
case "runtime.getRandomData":
return w.runGoABI(w.goRuntimeGetRandomData)
case "syscall/js.valueGet":
return w.runGoABI(w.goSyscallJSValueGet)
case "syscall/js.valueSet":
return w.runGoABI(w.goSyscallJSValueSet)
case "syscall/js.valueIndex":
return w.runGoABI(w.goSyscallJSValueIndex)
case "syscall/js.valueCall":
return w.runGoABI(w.goSyscallJSValueCall)
case "syscall/js.valueNew":
return w.runGoABI(w.goSyscallJSValueNew)
case "syscall/js.valueLength":
return w.runGoABI(w.goSyscallJSValueLength)
default:
log.Printf("unknown module+field %s %s, using shim", module, field)
return w.runGoABI(w.notImplemented(module, field))
}
}
return nil
}