Repository navigation
Expand file tree
/
Copy pathwasm.go
More file actions
314 lines (274 loc) · 7.91 KB
/
Copy pathwasm.go
File metadata and controls
314 lines (274 loc) · 7.91 KB
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
// Copyright (C) 2019-2026 GoRE Authors.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gore
import (
"bytes"
"debug/dwarf"
"encoding/binary"
"errors"
"fmt"
"io"
"github.com/eliben/watgo"
"github.com/eliben/watgo/wasmir"
)
const (
wasmPageSize = 64 * 1024
// Eager reconstruction must be bounded independently of the input file size.
maxWasmMemorySize = 256 * 1024 * 1024
)
// WasmModule is watgo's semantic representation of a WebAssembly module.
type WasmModule = wasmir.Module
// WasmInfo is returned by GoFile.GetParsedFile for WebAssembly binaries.
type WasmInfo struct {
Module *wasmir.Module
Memory []byte
}
func openWasm(r io.ReaderAt) (*wasmFile, error) {
data, err := io.ReadAll(io.NewSectionReader(r, 0, int64(^uint64(0)>>1)))
if err != nil {
return nil, fmt.Errorf("error when reading the WebAssembly file: %w", err)
}
module, err := watgo.DecodeWASM(data)
if err != nil {
return nil, fmt.Errorf("error when parsing the WebAssembly file: %w", err)
}
memory, err := buildWasmMemory(module)
if err != nil {
return nil, err
}
return &wasmFile{module: module, memory: memory, reader: r}, nil
}
func buildWasmMemory(module *wasmir.Module) ([]byte, error) {
if module == nil || len(module.Memories) == 0 {
return nil, ErrWasmNoLinearMemory
}
minimumPages := module.Memories[0].Min
if minimumPages > maxWasmMemorySize/wasmPageSize {
return nil, fmt.Errorf("%w: exceeds the %d-byte reconstruction limit", ErrWasmMemoryTooLarge, maxWasmMemorySize)
}
maxInt := int(^uint(0) >> 1)
if minimumPages > uint64(maxInt)/wasmPageSize {
return nil, ErrWasmMemoryTooLarge
}
memory := make([]byte, int(minimumPages*wasmPageSize))
for i, segment := range module.Data {
if segment.Mode != wasmir.DataSegmentModeActive {
continue
}
if segment.MemoryIndex != 0 {
return nil, fmt.Errorf("WebAssembly data segment %d targets unsupported memory %d", i, segment.MemoryIndex)
}
offset, err := wasmConstExpression(module, segment.OffsetExpr, segment.OffsetI64, nil)
if err != nil {
return nil, fmt.Errorf("WebAssembly data segment %d has invalid offset: %w", i, err)
}
if offset < 0 || uint64(offset) > uint64(len(memory)) || uint64(len(segment.Init)) > uint64(len(memory))-uint64(offset) {
return nil, fmt.Errorf("WebAssembly data segment %d is outside linear memory", i)
}
copy(memory[int(offset):], segment.Init)
}
return memory, nil
}
func wasmConstExpression(module *wasmir.Module, expression []wasmir.Instruction, fallback int64, visiting map[uint32]bool) (int64, error) {
if len(expression) == 0 {
return fallback, nil
}
var value int64
valueSet := false
for _, instruction := range expression {
switch instruction.Kind {
case wasmir.InstrI32Const:
if valueSet {
return 0, ErrWasmConstExpressionMultipleValues
}
value = int64(instruction.I32Const)
valueSet = true
case wasmir.InstrI64Const:
if valueSet {
return 0, ErrWasmConstExpressionMultipleValues
}
value = instruction.I64Const
valueSet = true
case wasmir.InstrGlobalGet:
if valueSet {
return 0, ErrWasmConstExpressionMultipleValues
}
index := instruction.GlobalIndex
if int(index) >= len(module.Globals) {
return 0, fmt.Errorf("global index %d is out of bounds", index)
}
global := module.Globals[index]
if global.Mutable || len(global.Init) == 0 {
return 0, fmt.Errorf("global %d cannot be evaluated statically", index)
}
if visiting == nil {
visiting = make(map[uint32]bool)
}
if visiting[index] {
return 0, fmt.Errorf("global %d has a cyclic initializer", index)
}
visiting[index] = true
globalValue, err := wasmConstExpression(module, global.Init, 0, visiting)
delete(visiting, index)
if err != nil {
return 0, err
}
value = globalValue
valueSet = true
case wasmir.InstrEnd:
// The binary constant expression terminator carries no value.
default:
return 0, fmt.Errorf("unsupported instruction kind %d", instruction.Kind)
}
}
if !valueSet {
return 0, ErrWasmConstExpressionNoValue
}
return value, nil
}
var _ fileHandler = (*wasmFile)(nil)
type wasmFile struct {
module *wasmir.Module
memory []byte
reader io.ReaderAt
}
func (w *wasmFile) Close() error {
return tryClose(w.reader)
}
func (w *wasmFile) getSymbol(string) (Symbol, error) {
return Symbol{}, ErrSymbolNotFound
}
func (w *wasmFile) getRData() ([]byte, error) {
return w.memory, nil
}
func (w *wasmFile) getCodeSection() (uint64, []byte, error) {
return 0, nil, errors.New("WebAssembly code is not stored in linear memory")
}
func (w *wasmFile) getSectionDataFromAddress(address uint64) (uint64, []byte, error) {
if address >= uint64(len(w.memory)) {
return 0, nil, fmt.Errorf("address %#x is outside WebAssembly linear memory", address)
}
return 0, w.memory, nil
}
func (w *wasmFile) getSectionData(name string) (uint64, []byte, error) {
if name != "memory" {
return 0, nil, ErrSectionDoesNotExist
}
return 0, w.memory, nil
}
func (w *wasmFile) getVersion() (*GoVersion, error) {
section := w.customSection("producers")
if section == nil {
return nil, ErrNoGoVersionFound
}
version, err := goVersionFromProducers(section)
if err != nil {
return nil, err
}
if resolved := ResolveGoVersion(version); resolved != nil {
return resolved, nil
}
return &GoVersion{Name: version}, nil
}
func goVersionFromProducers(data []byte) (string, error) {
r := bytes.NewReader(data)
fieldCount, err := binary.ReadUvarint(r)
if err != nil {
return "", fmt.Errorf("invalid WebAssembly producers field count: %w", err)
}
for i := uint64(0); i < fieldCount; i++ {
fieldName, err := readWasmName(r)
if err != nil {
return "", fmt.Errorf("invalid WebAssembly producers field %d: %w", i, err)
}
valueCount, err := binary.ReadUvarint(r)
if err != nil {
return "", fmt.Errorf("invalid WebAssembly producers value count: %w", err)
}
for j := uint64(0); j < valueCount; j++ {
name, err := readWasmName(r)
if err != nil {
return "", fmt.Errorf("invalid WebAssembly producer name: %w", err)
}
version, err := readWasmName(r)
if err != nil {
return "", fmt.Errorf("invalid WebAssembly producer version: %w", err)
}
if fieldName == "language" && name == "Go" {
return version, nil
}
}
}
return "", ErrNoGoVersionFound
}
func readWasmName(r *bytes.Reader) (string, error) {
length, err := binary.ReadUvarint(r)
if err != nil {
return "", err
}
if length > uint64(r.Len()) {
return "", io.ErrUnexpectedEOF
}
data := make([]byte, int(length))
if _, err := io.ReadFull(r, data); err != nil {
return "", err
}
return string(data), nil
}
func (w *wasmFile) getFileInfo() *FileInfo {
version, _ := w.getVersion()
return &FileInfo{
Arch: ArchWASM,
OS: wasmOS(w.module),
ByteOrder: binary.LittleEndian,
WordSize: intSize64,
goversion: version,
}
}
func wasmOS(module *wasmir.Module) string {
for _, imported := range module.Imports {
switch imported.Module {
case "go", "gojs":
return "js"
case "wasi_snapshot_preview1":
return "wasip1"
}
}
return "wasm"
}
func (w *wasmFile) getPCLNTABData() (uint64, []byte, error) {
data, err := searchSectionForTab(w.memory, binary.LittleEndian)
if err != nil {
return 0, nil, err
}
return uint64(len(w.memory) - len(data)), data, nil
}
func (w *wasmFile) moduledataSection() string {
return "memory"
}
func (w *wasmFile) getBuildID() (string, error) {
section := w.customSection("go:buildid")
if section == nil {
return "", nil
}
return string(section), nil
}
func (w *wasmFile) getReader() io.ReaderAt {
return w.reader
}
func (w *wasmFile) getParsedFile() any {
return WasmInfo{Module: w.module, Memory: w.memory}
}
func (w *wasmFile) getDwarf() (*dwarf.Data, error) {
return nil, ErrUnsupportedDwarf
}
func (w *wasmFile) customSection(name string) []byte {
for _, section := range w.module.CustomSections {
if section.Name == name {
return section.Payload
}
}
return nil
}