-
Notifications
You must be signed in to change notification settings - Fork 10
/
runtime.go
149 lines (119 loc) · 3.72 KB
/
runtime.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
package extism
import (
"context"
"github.com/tetratelabs/wazero/api"
)
// TODO: test runtime initialization for WASI and Haskell
type runtimeType uint8
const (
None runtimeType = iota
Haskell
Wasi
)
type guestRuntime struct {
runtimeType runtimeType
init func(ctx context.Context) error
initialized bool
}
func detectGuestRuntime(ctx context.Context, p *Plugin) guestRuntime {
runtime, ok := haskellRuntime(ctx, p, p.module)
if ok {
return runtime
}
runtime, ok = wasiRuntime(ctx, p, p.module)
if ok {
return runtime
}
p.Log(LogLevelTrace, "No runtime detected")
return guestRuntime{runtimeType: None, init: func(_ context.Context) error { return nil }, initialized: true}
}
// Check for Haskell runtime initialization functions
// Initialize Haskell runtime if `hs_init` and `hs_exit` are present,
// by calling the `hs_init` export
func haskellRuntime(ctx context.Context, p *Plugin, m api.Module) (guestRuntime, bool) {
initFunc := m.ExportedFunction("hs_init")
if initFunc == nil {
return guestRuntime{}, false
}
params := initFunc.Definition().ParamTypes()
if len(params) != 2 || params[0] != api.ValueTypeI32 || params[1] != api.ValueTypeI32 {
p.Logf(LogLevelTrace, "hs_init function found with type %v", params)
}
reactorInit := m.ExportedFunction("_initialize")
init := func(ctx context.Context) error {
if reactorInit != nil {
_, err := reactorInit.Call(ctx)
if err != nil {
p.Logf(LogLevelError, "Error running reactor _initialize: %s", err.Error())
}
}
_, err := initFunc.Call(ctx, 0, 0)
if err == nil {
p.Log(LogLevelDebug, "Initialized Haskell language runtime.")
}
return err
}
p.Log(LogLevelTrace, "Haskell runtime detected")
return guestRuntime{runtimeType: Haskell, init: init}, true
}
// Check for initialization functions defined by the WASI standard
func wasiRuntime(ctx context.Context, p *Plugin, m api.Module) (guestRuntime, bool) {
if !p.hasWasi {
return guestRuntime{}, false
}
// WASI supports two modules: Reactors and Commands
// we prioritize Reactors over Commands
// see: https://github.com/WebAssembly/WASI/blob/main/legacy/application-abi.md
if r, ok := reactorModule(ctx, m, p); ok {
return r, ok
}
return commandModule(ctx, m, p)
}
// Check for `_initialize` this is used by WASI to initialize certain interfaces.
func reactorModule(ctx context.Context, m api.Module, p *Plugin) (guestRuntime, bool) {
init := findFunc(ctx, m, p, "_initialize")
if init == nil {
return guestRuntime{}, false
}
p.Logf(LogLevelTrace, "WASI runtime detected")
p.Logf(LogLevelTrace, "Reactor module detected")
return guestRuntime{runtimeType: Wasi, init: init}, true
}
// Check for `__wasm__call_ctors`, this is used by WASI to
// initialize certain interfaces.
func commandModule(ctx context.Context, m api.Module, p *Plugin) (guestRuntime, bool) {
init := findFunc(ctx, m, p, "__wasm_call_ctors")
if init == nil {
return guestRuntime{}, false
}
p.Logf(LogLevelTrace, "WASI runtime detected")
p.Logf(LogLevelTrace, "Command module detected")
return guestRuntime{runtimeType: Wasi, init: init}, true
}
func findFunc(ctx context.Context, m api.Module, p *Plugin, name string) func(context.Context) error {
initFunc := m.ExportedFunction(name)
if initFunc == nil {
return nil
}
params := initFunc.Definition().ParamTypes()
if len(params) != 0 {
p.Logf(LogLevelTrace, "%v function found with type %v", name, params)
return nil
}
return func(ctx context.Context) error {
p.Logf(LogLevelDebug, "Calling %v", name)
_, err := initFunc.Call(ctx)
return err
}
}
func equal(actual []byte, expected []byte) bool {
if len(actual) != len(expected) {
return false
}
for i, k := range actual {
if expected[i] != k {
return false
}
}
return true
}