-
Notifications
You must be signed in to change notification settings - Fork 2
/
metering.go
419 lines (376 loc) · 10.5 KB
/
metering.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
package metering
import (
"fmt"
"github.com/yyh1102/go-wasm-metering/toolkit"
"reflect"
"strconv"
)
const (
defaultModuleStr = "metering"
defaultFieldStr = "usegas"
defaultMeterType = "i64"
defaultCost = uint64(0)
)
var (
branchOps = map[string]struct{}{
"grow_memory": {},
"end": {},
"br": {},
"br_table": {},
"br_if": {},
"if": {},
"else": {},
"return": {},
"loop": {},
}
)
// MeterWASM injects metering into WebAssembly binary code.
// This func is the real exported function used by outer callers.
func MeterWASM(wasm []byte, opts *Options) ([]byte, uint64, error) {
module := toolkit.Wasm2Json(wasm)
if opts == nil {
opts = &Options{}
}
metering, err := newMetring(*opts)
if err != nil {
return nil, 0, err
}
module, gasCost, err := metering.meterJSON(module)
if err != nil {
return nil, 0, err
}
return toolkit.Json2Wasm(module), gasCost, nil
}
type Options struct {
CostTable toolkit.JSON // path of cost table file.
ModuleStr string // the import string for metering function.
FieldStr string // the field string for the metering function.
MeterType string // the register type that is used to meter. Can be `i64`, `i32`, `f64`, `f32`.
}
type Metering struct {
opts Options
}
func newMetring(opts Options) (*Metering, error) {
// set defaults.
if opts.CostTable == nil {
opts.CostTable = defaultCostTable
}
if opts.ModuleStr == "" {
opts.ModuleStr = defaultModuleStr
}
if opts.FieldStr == "" {
opts.FieldStr = defaultFieldStr
}
if opts.MeterType == "" {
opts.MeterType = defaultMeterType
}
return &Metering{
opts: opts,
}, nil
}
// meterJSON injects metering into a JSON output of Wasm2Json.
func (m *Metering) meterJSON(module []toolkit.JSON) ([]toolkit.JSON, uint64, error) {
// find section.
findSection := func(module []toolkit.JSON, sectionName string) toolkit.JSON {
for _, section := range module {
if name, exist := section["name"]; exist {
if name.(string) == sectionName {
return section
}
}
}
return nil
}
// create section.
createSection := func(module []toolkit.JSON, sectionName string) []toolkit.JSON {
newSectionId := toolkit.J2W_SECTION_IDS[sectionName]
for i, section := range module {
name, exist := section["name"]
if exist {
secId, exist := toolkit.J2W_SECTION_IDS[name.(string)]
//fmt.Printf("%v %v %v\n", name, secId, exist)
if exist && secId > 0 && newSectionId < secId {
rest := append([]toolkit.JSON{}, module[i:]...)
// insert the section at pos `i`
module = append(module[:i], toolkit.JSON{
"name": sectionName,
})
module = append(module, rest...)
break
}
}
}
return module
}
//fmt.Printf("%#v\n", module)
// add necessary `type` and `import` sections if and only if they don't exist.
if findSection(module, "type") == nil {
module = createSection(module, "type")
}
if findSection(module, "import") == nil {
module = createSection(module, "import")
}
importEntry := toolkit.ImportEntry{
ModuleStr: m.opts.ModuleStr,
FieldStr: m.opts.FieldStr,
Kind: "function",
}
importType := toolkit.TypeEntry{
Form: "func",
Params: []string{m.opts.MeterType},
}
var (
typeModule toolkit.JSON
functionModule toolkit.JSON
funcIndex int
newModule = make([]toolkit.JSON, len(module))
gasCost uint64
)
copy(newModule, module)
//fmt.Printf("%#v", newModule)
for _, section := range newModule {
sectionName, exist := section["name"]
if !exist {
continue
}
switch sectionName.(string) {
case "type":
var entries []toolkit.TypeEntry
ientries, exist := section["entries"]
if exist {
entries = ientries.([]toolkit.TypeEntry)
}
//fmt.Printf("Entries %#v\n", entries)
importEntry.Type = uint64(len(entries))
entries = append(entries, importType)
section["entries"] = entries
// save for use for the code section.
typeModule = section
case "function":
// save for use for the code section.
functionModule = section
case "import":
var entries []toolkit.ImportEntry
ientries, exist := section["entries"]
if exist {
entries = ientries.([]toolkit.ImportEntry)
}
for _, entry := range entries {
if entry.ModuleStr == m.opts.ModuleStr && entry.FieldStr == m.opts.FieldStr {
return nil, 0, ErrImportMeterFunc
}
if entry.Kind == "function" {
funcIndex += 1
}
}
// append the metering import.
section["entries"] = append(entries, importEntry)
case "export":
var entries []toolkit.ExportEntry
ientries, exist := section["entries"]
if exist {
entries = ientries.([]toolkit.ExportEntry)
}
for i, entry := range entries {
if entry.Kind == "function" && entry.Index >= uint32(funcIndex) {
entries[i].Index = entry.Index + 1
}
}
case "element":
var entries []toolkit.ElementEntry
ientries, exist := section["entries"]
if exist {
entries = ientries.([]toolkit.ElementEntry)
}
for i, entry := range entries {
// remap element indices.
newElements := make([]uint64, 0, len(entry.Elements))
for _, el := range entry.Elements {
if el >= uint64(funcIndex) {
el += 1
}
newElements = append(newElements, el)
}
entries[i].Elements = newElements
}
case "start":
index := section["index"].(uint32)
if index >= uint32(funcIndex) {
index += 1
}
section["index"] = index
case "code":
entries := section["entries"].([]toolkit.CodeBody)
funcEntries := functionModule["entries"].([]uint64)
typEntries := typeModule["entries"].([]toolkit.TypeEntry)
for i, entry := range entries {
typeIndex := funcEntries[i]
typ := typEntries[typeIndex]
cost := getCost(typ, m.opts.CostTable["type"].(toolkit.JSON), defaultCost)
entry, cost = meterCodeEntry(entry, m.opts.CostTable["code"].(toolkit.JSON), m.opts.MeterType, funcIndex, cost)
gasCost += cost
entries[i] = entry
}
}
}
return newModule, gasCost, nil
}
// getCost returns the cost of an operation for the entry in a section from the cost table.
func getCost(j interface{}, costTable toolkit.JSON, defaultCost uint64) (cost uint64) {
if dc, exist := costTable["DEFAULT"]; exist {
defaultCost = uint64(dc.(int))
}
rval := reflect.ValueOf(j)
kind := rval.Type().Kind()
if kind == reflect.Slice {
for i := 0; i < rval.Len(); i++ {
cost += getCost(rval.Index(i).Interface(), costTable, 0)
}
} else if kind == reflect.Struct {
rtype := rval.Type()
for i := 0; i < rval.NumField(); i++ {
rv := rval.Field(i)
propCost, exist := costTable[toolkit.Lcfirst(rtype.Field(i).Name)]
if exist {
cost += getCost(rv.Interface(), propCost.(toolkit.JSON), defaultCost)
}
}
} else if kind == reflect.String {
key := j.(string)
if key == "" {
return 0
}
c, exist := costTable[key]
if exist {
cost = uint64(c.(int))
} else {
cost = defaultCost
}
} else {
cost = defaultCost
}
//fmt.Printf("json %#v cost %v\n", j, cost)
return
}
// meterCodeEntry meters a single code entry (see toolkit.CodeBody).
func meterCodeEntry(entry toolkit.CodeBody, costTable toolkit.JSON, meterType string, meterFuncIndex int, cost uint64) (toolkit.CodeBody, uint64) {
getImmediateFromOP := func(name, opType string) string {
var immediatesKey string
if name == "const" {
immediatesKey = opType
} else {
immediatesKey = name
}
return toolkit.OP_IMMEDIATES[immediatesKey]
}
meteringStatement := func(cost uint64, meteringImportIndex int) (ops []toolkit.OP) {
opsJson := toolkit.Text2Json(fmt.Sprintf("%s.const %v call %v", meterType, cost, meteringImportIndex))
for _, op := range opsJson {
//immediates, _ := strconv.ParseUint(op["immediates"].(string), 10, 64)
oop := toolkit.OP{
Name: op["name"].(string),
}
// convert immediates.
imm := getImmediateFromOP(oop.Name, meterType)
if imm != "" {
opImm := op["immediates"]
switch imm {
case "varuint1":
imme, _ := strconv.ParseInt(opImm.(string), 10, 8)
oop.Immediates = int8(imme)
case "varuint32":
imme, _ := strconv.ParseUint(opImm.(string), 10, 32)
oop.Immediates = uint32(imme)
case "varint32":
imme, _ := strconv.ParseInt(opImm.(string), 10, 32)
oop.Immediates = int32(imme)
case "varint64":
imme, _ := strconv.ParseInt(opImm.(string), 10, 64)
oop.Immediates = int64(imme)
case "uint32":
oop.Immediates = opImm.([]byte)
case "uint64":
oop.Immediates = opImm.([]byte)
case "block_type":
oop.Immediates = opImm.(string)
case "br_table", "call_indirect", "memory_immediate":
oop.Immediates = opImm.(toolkit.JSON)
}
//fmt.Printf("immediate %v %v\n", imm, oop.Immediates)
}
if rt, ok := op["return_type"]; ok {
oop.ReturnType = rt.(string)
}
if rt, ok := op["type"]; ok {
oop.Type = rt.(string)
}
ops = append(ops, oop)
}
return
}
remapOp := func(op *toolkit.OP, funcIndex int) {
if op.Name == "call" {
switch imm := op.Immediates.(type) {
case string:
rv, _ := strconv.ParseInt(imm, 10, 64)
if rv >= int64(funcIndex) {
rv += 1
op.Immediates = strconv.FormatInt(rv, 10)
}
case uint32:
//fmt.Printf("funcIndex %d, imm %d\n", funcIndex, imm)
if imm >= uint32(funcIndex) {
imm += 1
op.Immediates = imm
}
default:
panic(fmt.Sprintf("invalid immediates type: %v", imm))
}
}
}
meterTheMeteringStatement := func() uint64 {
code := meteringStatement(0, meterFuncIndex)
// sum the operations cost
sum := uint64(0)
for _, op := range code {
sum += getCost(op.Name, costTable["code"].(toolkit.JSON), defaultCost)
}
return sum
}
var (
meteringCost = meterTheMeteringStatement()
code = make([]toolkit.OP, len(entry.Code))
meteredCode []toolkit.OP
)
//fmt.Printf("meter the meter cost %d\n", meteringCost)
// create a code copy.
copy(code, entry.Code)
cost += getCost(entry.Locals, costTable["locals"].(toolkit.JSON), defaultCost)
sum := cost
for len(code) > 0 {
i := 0
// meter a segment of wasm code.
for {
op := &code[i]
remapOp(op, meterFuncIndex)
cost += getCost(code[i].Name, costTable["code"].(toolkit.JSON), defaultCost)
i += 1
if _, exist := branchOps[op.Name]; exist {
break
}
}
// add the metering statement.
if cost != 0 {
// add the cost of metering
cost += meteringCost
ops := meteringStatement(cost, meterFuncIndex)
meteredCode = append(meteredCode, ops...)
}
sum += cost
meteredCode = append(meteredCode, code[:i]...)
code = code[i:]
cost = 0
}
entry.Code = meteredCode
return entry, sum
}