forked from extism/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime.go
150 lines (119 loc) · 3.51 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
150
package extism
import (
"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() error
initialized bool
}
func detectGuestRuntime(p *Plugin) guestRuntime {
m := p.Main
runtime, ok := haskellRuntime(p, m)
if ok {
return runtime
}
runtime, ok = wasiRuntime(p, m)
if ok {
return runtime
}
p.Log(LogLevelTrace, "No runtime detected")
return guestRuntime{runtimeType: None, init: func() 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(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() error {
if reactorInit != nil {
_, err := reactorInit.Call(p.Runtime.ctx)
if err != nil {
p.Logf(LogLevelError, "Error running reactor _initialize: %s", err.Error())
}
}
_, err := initFunc.Call(p.Runtime.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(p *Plugin, m api.Module) (guestRuntime, bool) {
if !p.Runtime.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(m, p); ok {
return r, ok
}
return commandModule(m, p)
}
// Check for `_initialize` this is used by WASI to initialize certain interfaces.
func reactorModule(m api.Module, p *Plugin) (guestRuntime, bool) {
init := findFunc(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(m api.Module, p *Plugin) (guestRuntime, bool) {
init := findFunc(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(m api.Module, p *Plugin, name string) func() 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() error {
p.Logf(LogLevelDebug, "Calling %v", name)
_, err := initFunc.Call(p.Runtime.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
}