forked from go-interpreter/wagon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_test_runner.go
196 lines (181 loc) · 4.8 KB
/
spec_test_runner.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
// Copyright 2020 The go-interpreter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"github.com/ThinkiumGroup/wagon/exec"
"github.com/ThinkiumGroup/wagon/wasm"
)
// this file is based on github.com/perlin-network/life/spec/test_runner/runner.go
type Config struct {
SourceFilename string `json:"source_filename"`
Commands []Command `json:"commands"`
}
type Command struct {
Type string `json:"type"`
Line int `json:"line"`
Filename string `json:"filename"`
Name string `json:"name"`
Action CmdAction `json:"action"`
Text string `json:"text"`
ModuleType string `json:"module_type"`
Expected []ValueInfo `json:"expected"`
}
type CmdAction struct {
Type string `json:"type"`
Module string `json:"module"`
Field string `json:"field"`
Args []ValueInfo `json:"args"`
Expected []ValueInfo `json:"expected"`
}
type ValueInfo struct {
Type string `json:"type"`
Value string `json:"value"`
}
func LoadConfigFromFile(filename string) *Config {
raw, err := ioutil.ReadFile(filename)
if err != nil {
panic(err)
}
var cfg Config
err = json.Unmarshal(raw, &cfg)
if err != nil {
panic(err)
}
return &cfg
}
func (c *Config) Run(cfgPath string) {
var vm *exec.VM
namedVMs := make(map[string]*exec.VM)
dir, _ := filepath.Split(cfgPath)
for _, cmd := range c.Commands {
switch cmd.Type {
case "module":
input, err := ioutil.ReadFile(path.Join(dir, cmd.Filename))
if err != nil {
panic(err)
}
m, err := wasm.ReadModule(bytes.NewBuffer(input), nil)
if err != nil {
log.Fatalf("could not read module: %v", err)
}
vm, err = exec.NewVM(m)
if err != nil {
panic(fmt.Errorf("l%d: %s, could not create VM: %v", cmd.Line, cfgPath, err))
}
vm.RecoverPanic = true
if cmd.Name != "" {
namedVMs[cmd.Name] = vm
}
case "assert_return", "action":
localVM := vm
if cmd.Action.Module != "" {
if target, ok := namedVMs[cmd.Action.Module]; ok {
localVM = target
} else {
panic("named module not found")
}
}
if localVM == nil {
panic("module not found")
}
switch cmd.Action.Type {
case "invoke":
entryID, ok := localVM.GetExportEntry(cmd.Action.Field)
if !ok {
panic("export not found (func)")
}
args := make([]uint64, 0)
for _, arg := range cmd.Action.Args {
var val uint64
fmt.Sscanf(arg.Value, "%d", &val)
args = append(args, val)
}
ret, err := localVM.ExecCode(int64(entryID.Index), args...)
if err != nil {
panic(err)
}
if len(cmd.Expected) != 0 {
var _exp uint64
fmt.Sscanf(cmd.Expected[0].Value, "%d", &_exp)
exp := int64(_exp)
var result int64
if cmd.Expected[0].Type == "i32" || cmd.Expected[0].Type == "f32" {
result = int64(ret.(uint32))
exp = int64(uint32(exp))
} else {
result = int64(ret.(uint64))
}
if result != exp {
panic(fmt.Errorf("l%d: %s, ret mismatch: got %d, expected %d", cmd.Line, cfgPath, result, exp))
}
}
case "get":
val, ok := localVM.GetGlobal(cmd.Action.Field)
if !ok {
panic("export not found (global)")
}
var exp uint64
fmt.Sscanf(cmd.Expected[0].Value, "%d", &exp)
if cmd.Expected[0].Type == "i32" || cmd.Expected[0].Type == "f32" {
val = uint64(uint32(val))
exp = uint64(uint32(exp))
}
if val != exp {
panic(fmt.Errorf("val mismatch: got %d, expected %d\n", val, exp))
}
default:
panic(cmd.Action.Type)
}
case "assert_trap":
localVM := vm
if cmd.Action.Module != "" {
if target, ok := namedVMs[cmd.Action.Module]; ok {
localVM = target
} else {
panic("named module not found")
}
}
if localVM == nil {
panic("module not found")
}
switch cmd.Action.Type {
case "invoke":
entryID, ok := localVM.GetExportEntry(cmd.Action.Field)
if !ok {
panic("export not found (func)")
}
args := make([]uint64, 0)
for _, arg := range cmd.Action.Args {
var val uint64
fmt.Sscanf(arg.Value, "%d", &val)
args = append(args, val)
}
_, err := localVM.ExecCode(int64(entryID.Index), args...)
if err == nil {
panic(fmt.Errorf("L%d: %s, expect a trap\n", cmd.Line, cfgPath))
}
default:
panic(cmd.Action.Type)
}
case "assert_malformed", "assert_invalid", "assert_exhaustion", "assert_unlinkable",
"assert_return_canonical_nan", "assert_return_arithmetic_nan":
fmt.Printf("skipping %s\n", cmd.Type)
default:
panic(cmd.Type)
}
fmt.Printf("PASS L%d: %s\n", cmd.Line, cfgPath)
}
}
func main() {
cfg := LoadConfigFromFile(os.Args[1])
cfg.Run(os.Args[1])
}