| 1 |
package experimental |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
|
| 6 |
"github.com/tetratelabs/wazero/api" |
| 7 |
"github.com/tetratelabs/wazero/internal/expctxkeys" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
type StackIterator interface { |
| 17 |
|
| 18 |
|
| 19 |
Next() bool |
| 20 |
|
| 21 |
Function() InternalFunction |
| 22 |
|
| 23 |
|
| 24 |
ProgramCounter() ProgramCounter |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func WithFunctionListenerFactory(ctx context.Context, factory FunctionListenerFactory) context.Context { |
| 30 |
return context.WithValue(ctx, expctxkeys.FunctionListenerFactoryKey{}, factory) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
type FunctionListenerFactory interface { |
| 36 |
|
| 37 |
|
| 38 |
NewFunctionListener(api.FunctionDefinition) FunctionListener |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
type FunctionListener interface { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
Before(ctx context.Context, mod api.Module, def api.FunctionDefinition, params []uint64, stackIterator StackIterator) |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
After(ctx context.Context, mod api.Module, def api.FunctionDefinition, results []uint64) |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
Abort(ctx context.Context, mod api.Module, def api.FunctionDefinition, err error) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
type FunctionListenerFunc func(context.Context, api.Module, api.FunctionDefinition, []uint64, StackIterator) |
| 108 |
|
| 109 |
|
| 110 |
func (f FunctionListenerFunc) Before(ctx context.Context, mod api.Module, def api.FunctionDefinition, params []uint64, stackIterator StackIterator) { |
| 111 |
f(ctx, mod, def, params, stackIterator) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
func (f FunctionListenerFunc) After(context.Context, api.Module, api.FunctionDefinition, []uint64) { |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
func (f FunctionListenerFunc) Abort(context.Context, api.Module, api.FunctionDefinition, error) { |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
type FunctionListenerFactoryFunc func(api.FunctionDefinition) FunctionListener |
| 128 |
|
| 129 |
|
| 130 |
func (f FunctionListenerFactoryFunc) NewFunctionListener(def api.FunctionDefinition) FunctionListener { |
| 131 |
return f(def) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
func MultiFunctionListenerFactory(factories ...FunctionListenerFactory) FunctionListenerFactory { |
| 146 |
multi := make(multiFunctionListenerFactory, len(factories)) |
| 147 |
copy(multi, factories) |
| 148 |
return multi |
| 149 |
} |
| 150 |
|
| 151 |
type multiFunctionListenerFactory []FunctionListenerFactory |
| 152 |
|
| 153 |
func (multi multiFunctionListenerFactory) NewFunctionListener(def api.FunctionDefinition) FunctionListener { |
| 154 |
var lstns []FunctionListener |
| 155 |
for _, factory := range multi { |
| 156 |
if lstn := factory.NewFunctionListener(def); lstn != nil { |
| 157 |
lstns = append(lstns, lstn) |
| 158 |
} |
| 159 |
} |
| 160 |
switch len(lstns) { |
| 161 |
case 0: |
| 162 |
return nil |
| 163 |
case 1: |
| 164 |
return lstns[0] |
| 165 |
default: |
| 166 |
return &multiFunctionListener{lstns: lstns} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
type multiFunctionListener struct { |
| 171 |
lstns []FunctionListener |
| 172 |
stack stackIterator |
| 173 |
} |
| 174 |
|
| 175 |
func (multi *multiFunctionListener) Before(ctx context.Context, mod api.Module, def api.FunctionDefinition, params []uint64, si StackIterator) { |
| 176 |
multi.stack.base = si |
| 177 |
for _, lstn := range multi.lstns { |
| 178 |
multi.stack.index = -1 |
| 179 |
lstn.Before(ctx, mod, def, params, &multi.stack) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
func (multi *multiFunctionListener) After(ctx context.Context, mod api.Module, def api.FunctionDefinition, results []uint64) { |
| 184 |
for _, lstn := range multi.lstns { |
| 185 |
lstn.After(ctx, mod, def, results) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func (multi *multiFunctionListener) Abort(ctx context.Context, mod api.Module, def api.FunctionDefinition, err error) { |
| 190 |
for _, lstn := range multi.lstns { |
| 191 |
lstn.Abort(ctx, mod, def, err) |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
type stackIterator struct { |
| 196 |
base StackIterator |
| 197 |
index int |
| 198 |
pcs []uint64 |
| 199 |
fns []InternalFunction |
| 200 |
} |
| 201 |
|
| 202 |
func (si *stackIterator) Next() bool { |
| 203 |
if si.base != nil { |
| 204 |
si.pcs = si.pcs[:0] |
| 205 |
si.fns = si.fns[:0] |
| 206 |
|
| 207 |
for si.base.Next() { |
| 208 |
si.pcs = append(si.pcs, uint64(si.base.ProgramCounter())) |
| 209 |
si.fns = append(si.fns, si.base.Function()) |
| 210 |
} |
| 211 |
|
| 212 |
si.base = nil |
| 213 |
} |
| 214 |
si.index++ |
| 215 |
return si.index < len(si.pcs) |
| 216 |
} |
| 217 |
|
| 218 |
func (si *stackIterator) ProgramCounter() ProgramCounter { |
| 219 |
return ProgramCounter(si.pcs[si.index]) |
| 220 |
} |
| 221 |
|
| 222 |
func (si *stackIterator) Function() InternalFunction { |
| 223 |
return si.fns[si.index] |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
type StackFrame struct { |
| 228 |
Function api.Function |
| 229 |
Params []uint64 |
| 230 |
Results []uint64 |
| 231 |
PC uint64 |
| 232 |
SourceOffset uint64 |
| 233 |
} |
| 234 |
|
| 235 |
type internalFunction struct { |
| 236 |
definition api.FunctionDefinition |
| 237 |
sourceOffset uint64 |
| 238 |
} |
| 239 |
|
| 240 |
func (f internalFunction) Definition() api.FunctionDefinition { |
| 241 |
return f.definition |
| 242 |
} |
| 243 |
|
| 244 |
func (f internalFunction) SourceOffsetForPC(pc ProgramCounter) uint64 { |
| 245 |
return f.sourceOffset |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
type stackFrameIterator struct { |
| 251 |
index int |
| 252 |
stack []StackFrame |
| 253 |
fndef []api.FunctionDefinition |
| 254 |
} |
| 255 |
|
| 256 |
func (si *stackFrameIterator) Next() bool { |
| 257 |
si.index++ |
| 258 |
return si.index < len(si.stack) |
| 259 |
} |
| 260 |
|
| 261 |
func (si *stackFrameIterator) Function() InternalFunction { |
| 262 |
return internalFunction{ |
| 263 |
definition: si.fndef[si.index], |
| 264 |
sourceOffset: si.stack[si.index].SourceOffset, |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
func (si *stackFrameIterator) ProgramCounter() ProgramCounter { |
| 269 |
return ProgramCounter(si.stack[si.index].PC) |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
func NewStackIterator(stack ...StackFrame) StackIterator { |
| 275 |
si := &stackFrameIterator{ |
| 276 |
index: -1, |
| 277 |
stack: make([]StackFrame, len(stack)), |
| 278 |
fndef: make([]api.FunctionDefinition, len(stack)), |
| 279 |
} |
| 280 |
for i := range stack { |
| 281 |
si.stack[i] = stack[len(stack)-(i+1)] |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
for i, frame := range stack { |
| 289 |
si.fndef[i] = frame.Function.Definition() |
| 290 |
} |
| 291 |
return si |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
func BenchmarkFunctionListener(n int, module api.Module, stack []StackFrame, listener FunctionListener) { |
| 303 |
if len(stack) == 0 { |
| 304 |
panic("cannot benchmark function listener with an empty stack") |
| 305 |
} |
| 306 |
|
| 307 |
ctx := context.Background() |
| 308 |
def := stack[0].Function.Definition() |
| 309 |
params := stack[0].Params |
| 310 |
results := stack[0].Results |
| 311 |
stackIterator := &stackIterator{base: NewStackIterator(stack...)} |
| 312 |
|
| 313 |
for i := 0; i < n; i++ { |
| 314 |
stackIterator.index = -1 |
| 315 |
listener.Before(ctx, module, def, params, stackIterator) |
| 316 |
listener.After(ctx, module, def, results) |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|