This repository has been archived by the owner on Oct 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwasmextractor.go
175 lines (131 loc) · 3.17 KB
/
wasmextractor.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
/*
* Copyright 2024 Hypermode, Inc.
* MIT License - https://opensource.org/licenses/MIT
*/
package wasmextractor
import (
"bytes"
"encoding/binary"
"fmt"
"os"
)
type WasmInfo struct {
Imports []WasmItem
Exports []WasmItem
}
type WasmItem struct {
Name string
Kind WasmItemKind
Index uint32
}
func (i *WasmItem) String() string {
return fmt.Sprintf("%s: %s (index: %d)", i.Kind, i.Name, i.Index)
}
type WasmItemKind int32
const (
WasmFunction WasmItemKind = 0
WasmTable WasmItemKind = 1
WasmMemory WasmItemKind = 2
WasmGlobal WasmItemKind = 3
)
func (k WasmItemKind) String() string {
switch k {
case WasmFunction:
return "Function"
case WasmTable:
return "Table"
case WasmMemory:
return "Memory"
case WasmGlobal:
return "Global"
default:
return "Unknown"
}
}
func ReadWasmFile(wasmFilePath string) ([]byte, error) {
wasmBytes, err := os.ReadFile(wasmFilePath)
if err != nil {
return nil, fmt.Errorf("error reading wasm file: %v", err)
}
if err := validateWasm(wasmBytes); err != nil {
return nil, err
}
return wasmBytes, nil
}
func ExtractWasmInfo(wasmBytes []byte) (*WasmInfo, error) {
if err := validateWasm(wasmBytes); err != nil {
return nil, err
}
info := &WasmInfo{}
offset := 8
for offset < len(wasmBytes) {
sectionID := wasmBytes[offset]
offset++
size, n := binary.Uvarint(wasmBytes[offset:])
offset += n
switch sectionID {
case 2: // Import section
info.Imports = readImports(wasmBytes[offset : offset+int(size)])
case 7: // Export section
info.Exports = readExports(wasmBytes[offset : offset+int(size)])
}
offset += int(size)
}
return info, nil
}
var magic = []byte{0x00, 0x61, 0x73, 0x6D} // "\0asm"
func validateWasm(data []byte) error {
if len(data) < 8 || !bytes.Equal(data[:4], magic) {
return fmt.Errorf("invalid wasm file")
}
if binary.LittleEndian.Uint32(data[4:8]) != 1 {
return fmt.Errorf("unsupported wasm version")
}
return nil
}
func readImports(data []byte) []WasmItem {
numItems, n := binary.Uvarint(data)
offset := n
imports := make([]WasmItem, numItems)
for i := 0; i < int(numItems); i++ {
moduleLen, n := binary.Uvarint(data[offset:])
offset += n
moduleName := string(data[offset : offset+int(moduleLen)])
offset += int(moduleLen)
fieldLen, n := binary.Uvarint(data[offset:])
offset += n
fieldName := string(data[offset : offset+int(fieldLen)])
offset += int(fieldLen)
kind := data[offset]
offset++
index, n := binary.Uvarint(data[offset:])
offset += n
imports[i] = WasmItem{
Name: fmt.Sprintf("%s.%s", moduleName, fieldName),
Kind: WasmItemKind(kind),
Index: uint32(index),
}
}
return imports
}
func readExports(data []byte) []WasmItem {
numItems, n := binary.Uvarint(data)
offset := n
exports := make([]WasmItem, numItems)
for i := 0; i < int(numItems); i++ {
fieldLen, n := binary.Uvarint(data[offset:])
offset += n
fieldName := string(data[offset : offset+int(fieldLen)])
offset += int(fieldLen)
kind := data[offset]
offset++
index, n := binary.Uvarint(data[offset:])
offset += n
exports[i] = WasmItem{
Name: fieldName,
Kind: WasmItemKind(kind),
Index: uint32(index),
}
}
return exports
}