/
stack.go
263 lines (207 loc) · 6.96 KB
/
stack.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
// Copyright (c) 2018 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package image
import (
"encoding/binary"
"errors"
"fmt"
"log"
"math"
"gate.computer/internal/manifest"
"gate.computer/wag/binding"
"gate.computer/wag/object"
"gate.computer/wag/object/stack"
"gate.computer/wag/wa"
)
const initStackSize = 24
func putInitStack(portable []byte, start, entry *manifest.Function) {
if n := len(portable); n != initStackSize {
panic(n)
}
var (
startIndex uint64 = math.MaxUint64
entryIndex uint64 = math.MaxUint64
)
if start != nil {
startIndex = uint64(start.Index)
}
if entry != nil {
entryIndex = uint64(entry.Index)
}
const callIndex = 0 // Virtual call site at beginning of enter routine.
const stackOffset = 16 // The function address are on the stack.
binary.LittleEndian.PutUint64(portable[0:], stackOffset<<32|callIndex)
binary.LittleEndian.PutUint64(portable[8:], startIndex)
binary.LittleEndian.PutUint64(portable[16:], entryIndex)
}
// exportStack from native source buffer to portable target buffer.
func exportStack(portable, native []byte, textAddr uint64, textMap stack.TextMap) error {
if n := len(native); n == 0 || n&7 != 0 {
return fmt.Errorf("invalid stack size %d", n)
}
if n := len(portable); n != len(native) {
panic(n)
}
var level int
if false {
log.Printf("exportStack: textAddr=0x%x", textAddr)
}
var initStackOffset int32
for {
if len(native) == 0 {
return errors.New("ran out of stack before initial call")
}
absRetAddr := binary.LittleEndian.Uint64(native)
native = native[8:]
if false {
log.Printf("exportStack: level=%d absRetAddr=0x%x", level, absRetAddr)
}
retAddr := absRetAddr - textAddr
if retAddr > math.MaxUint32 {
return fmt.Errorf("return address 0x%x is not in text section", absRetAddr)
}
init, _, callIndex, stackOffset, _ := textMap.FindCall(uint32(retAddr))
if callIndex < 0 {
return fmt.Errorf("call instruction not found for return address 0x%x", retAddr)
}
binary.LittleEndian.PutUint64(portable, uint64(stackOffset)<<32|uint64(callIndex))
portable = portable[8:]
if false {
log.Printf("exportStack: level=%d callIndex=%d stackOffset=%d", level, callIndex, stackOffset)
level++
}
if init {
initStackOffset = stackOffset
break
}
if stackOffset == 0 || stackOffset&7 != 0 {
return fmt.Errorf("invalid stack offset %d", stackOffset)
}
copy(portable[:stackOffset-8], native[:stackOffset-8])
native = native[stackOffset-8:]
portable = portable[stackOffset-8:]
}
switch initStackOffset {
case 8:
// Stack still contains entry function address: this call site precedes
// entry function call; this is the start function return site.
funcAddr := binary.LittleEndian.Uint64(native)
native = native[8:]
if false {
log.Printf("exportStack: level=%d entry funcAddr=0x%x", level, funcAddr)
}
funcIndex := uint32(math.MaxUint32) // No entry function.
if funcAddr != 0 {
if i, found := textMap.FindFunc(uint32(funcAddr)); found {
funcIndex = uint32(i)
}
}
binary.LittleEndian.PutUint64(portable, uint64(funcIndex))
portable = portable[8:]
case 0:
// Entry function address has been popped off the stack: this call site
// follows start function call; this is the entry function return site.
default:
return fmt.Errorf("initial function call site has inconsistent stack offset %d", initStackOffset)
}
if n := len(native); n != 0 {
return fmt.Errorf("%d bytes of excess data at start of stack", n)
}
if n := len(portable); n != len(native) {
panic(n)
}
return nil
}
// importStack converts buffer from portable to native representation in-place.
func importStack(buf []byte, textAddr uint64, codeMap object.CallMap, types []wa.FuncType, funcTypeIndexes []uint32) error {
if n := len(buf); n == 0 || n&7 != 0 {
return fmt.Errorf("invalid stack size %d", n)
}
var level int
var minVars int
var call object.CallSite
for {
if len(buf) == 0 {
return errors.New("ran out of stack before initial call")
}
pair := binary.LittleEndian.Uint64(buf)
callIndex := uint32(pair)
if callIndex >= uint32(len(codeMap.CallSites)) {
return fmt.Errorf("function call site index %d is unknown", callIndex)
}
call = codeMap.CallSites[callIndex]
if off := int32(pair >> 32); off != call.StackOffset {
return fmt.Errorf("encoded stack offset %d of call site %d does not match offset %d in map", off, callIndex, call.StackOffset)
}
binary.LittleEndian.PutUint64(buf, textAddr+uint64(call.RetAddr))
buf = buf[8:]
if false {
log.Printf("importStack: level=%d callIndex=%d call.RetAddr=0x%x call.StackOffset=%d", level, callIndex, call.RetAddr, call.StackOffset)
level++
}
if len(codeMap.FuncAddrs) == 0 || call.RetAddr < codeMap.FuncAddrs[0] {
break
}
if call.StackOffset&7 != 0 {
return fmt.Errorf("invalid stack offset %d", call.StackOffset)
}
if int(call.StackOffset-8) < minVars*8 {
return errors.New("inconsistent call stack")
}
buf = buf[call.StackOffset-8:]
init, funcIndex, callIndexAgain, stackOffsetAgain, _ := codeMap.FindCall(call.RetAddr)
if init || callIndexAgain != int(callIndex) || stackOffsetAgain != call.StackOffset {
return fmt.Errorf("call instruction not found for return address 0x%x", call.RetAddr)
}
sigIndex := funcTypeIndexes[funcIndex]
sig := types[sigIndex]
minVars = len(sig.Params)
}
if minVars > 0 {
return errors.New("inconsistent call stack")
}
switch call.StackOffset {
case 16:
// Stack was synthesized by putInitStack.
var startAddr uint32
if funcIndex := binary.LittleEndian.Uint64(buf); funcIndex != math.MaxUint64 {
if funcIndex >= uint64(len(codeMap.FuncAddrs)) {
return fmt.Errorf("start function index %d is unknown", funcIndex)
}
startAddr = codeMap.FuncAddrs[funcIndex]
sigIndex := funcTypeIndexes[funcIndex]
sig := types[sigIndex]
if !sig.Equal(wa.FuncType{}) {
return fmt.Errorf("start function %d has invalid signature: %s", funcIndex, sig)
}
}
binary.LittleEndian.PutUint64(buf, uint64(startAddr))
buf = buf[8:]
fallthrough
case 8:
// See the comment in exportStack.
var entryAddr uint32
if funcIndex := binary.LittleEndian.Uint64(buf); funcIndex != math.MaxUint64 {
if funcIndex >= uint64(len(codeMap.FuncAddrs)) {
return fmt.Errorf("entry function index %d is unknown", funcIndex)
}
entryAddr = codeMap.FuncAddrs[funcIndex]
sigIndex := funcTypeIndexes[funcIndex]
sig := types[sigIndex]
if !binding.IsEntryFuncType(sig) {
return fmt.Errorf("entry function %d has invalid signature: %s", funcIndex, sig)
}
}
binary.LittleEndian.PutUint64(buf, uint64(entryAddr))
buf = buf[8:]
case 0:
// See the comment in exportStack.
default:
return fmt.Errorf("initial function call site 0x%x has inconsistent stack offset %d", call.RetAddr, call.StackOffset)
}
if n := len(buf); n != 0 {
return fmt.Errorf("%d bytes of excess data at start of stack", n)
}
return nil
}