| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package wasi_snapshot_preview1 |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/binary" |
| 22 |
|
| 23 |
"github.com/tetratelabs/wazero" |
| 24 |
"github.com/tetratelabs/wazero/api" |
| 25 |
"github.com/tetratelabs/wazero/experimental/sys" |
| 26 |
"github.com/tetratelabs/wazero/internal/wasip1" |
| 27 |
"github.com/tetratelabs/wazero/internal/wasm" |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const ModuleName = wasip1.InternalModuleName |
| 34 |
|
| 35 |
const i32, i64 = wasm.ValueTypeI32, wasm.ValueTypeI64 |
| 36 |
|
| 37 |
var le = binary.LittleEndian |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func MustInstantiate(ctx context.Context, r wazero.Runtime) { |
| 44 |
if _, err := Instantiate(ctx, r); err != nil { |
| 45 |
panic(err) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func Instantiate(ctx context.Context, r wazero.Runtime) (api.Closer, error) { |
| 56 |
return NewBuilder(r).Instantiate(ctx) |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
type Builder interface { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
Compile(context.Context) (wazero.CompiledModule, error) |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
Instantiate(context.Context) (api.Closer, error) |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func NewBuilder(r wazero.Runtime) Builder { |
| 79 |
return &builder{r} |
| 80 |
} |
| 81 |
|
| 82 |
type builder struct{ r wazero.Runtime } |
| 83 |
|
| 84 |
|
| 85 |
func (b *builder) hostModuleBuilder() wazero.HostModuleBuilder { |
| 86 |
ret := b.r.NewHostModuleBuilder(ModuleName) |
| 87 |
exportFunctions(ret) |
| 88 |
return ret |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func (b *builder) Compile(ctx context.Context) (wazero.CompiledModule, error) { |
| 93 |
return b.hostModuleBuilder().Compile(ctx) |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
func (b *builder) Instantiate(ctx context.Context) (api.Closer, error) { |
| 98 |
return b.hostModuleBuilder().Instantiate(ctx) |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
type FunctionExporter interface { |
| 108 |
ExportFunctions(wazero.HostModuleBuilder) |
| 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 |
func NewFunctionExporter() FunctionExporter { |
| 136 |
return &functionExporter{} |
| 137 |
} |
| 138 |
|
| 139 |
type functionExporter struct{} |
| 140 |
|
| 141 |
|
| 142 |
func (functionExporter) ExportFunctions(builder wazero.HostModuleBuilder) { |
| 143 |
exportFunctions(builder) |
| 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 |
func exportFunctions(builder wazero.HostModuleBuilder) { |
| 176 |
exporter := builder.(wasm.HostFuncExporter) |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
exporter.ExportHostFunc(argsGet) |
| 182 |
exporter.ExportHostFunc(argsSizesGet) |
| 183 |
exporter.ExportHostFunc(environGet) |
| 184 |
exporter.ExportHostFunc(environSizesGet) |
| 185 |
exporter.ExportHostFunc(clockResGet) |
| 186 |
exporter.ExportHostFunc(clockTimeGet) |
| 187 |
exporter.ExportHostFunc(fdAdvise) |
| 188 |
exporter.ExportHostFunc(fdAllocate) |
| 189 |
exporter.ExportHostFunc(fdClose) |
| 190 |
exporter.ExportHostFunc(fdDatasync) |
| 191 |
exporter.ExportHostFunc(fdFdstatGet) |
| 192 |
exporter.ExportHostFunc(fdFdstatSetFlags) |
| 193 |
exporter.ExportHostFunc(fdFdstatSetRights) |
| 194 |
exporter.ExportHostFunc(fdFilestatGet) |
| 195 |
exporter.ExportHostFunc(fdFilestatSetSize) |
| 196 |
exporter.ExportHostFunc(fdFilestatSetTimes) |
| 197 |
exporter.ExportHostFunc(fdPread) |
| 198 |
exporter.ExportHostFunc(fdPrestatGet) |
| 199 |
exporter.ExportHostFunc(fdPrestatDirName) |
| 200 |
exporter.ExportHostFunc(fdPwrite) |
| 201 |
exporter.ExportHostFunc(fdRead) |
| 202 |
exporter.ExportHostFunc(fdReaddir) |
| 203 |
exporter.ExportHostFunc(fdRenumber) |
| 204 |
exporter.ExportHostFunc(fdSeek) |
| 205 |
exporter.ExportHostFunc(fdSync) |
| 206 |
exporter.ExportHostFunc(fdTell) |
| 207 |
exporter.ExportHostFunc(fdWrite) |
| 208 |
exporter.ExportHostFunc(pathCreateDirectory) |
| 209 |
exporter.ExportHostFunc(pathFilestatGet) |
| 210 |
exporter.ExportHostFunc(pathFilestatSetTimes) |
| 211 |
exporter.ExportHostFunc(pathLink) |
| 212 |
exporter.ExportHostFunc(pathOpen) |
| 213 |
exporter.ExportHostFunc(pathReadlink) |
| 214 |
exporter.ExportHostFunc(pathRemoveDirectory) |
| 215 |
exporter.ExportHostFunc(pathRename) |
| 216 |
exporter.ExportHostFunc(pathSymlink) |
| 217 |
exporter.ExportHostFunc(pathUnlinkFile) |
| 218 |
exporter.ExportHostFunc(pollOneoff) |
| 219 |
exporter.ExportHostFunc(procExit) |
| 220 |
exporter.ExportHostFunc(procRaise) |
| 221 |
exporter.ExportHostFunc(schedYield) |
| 222 |
exporter.ExportHostFunc(randomGet) |
| 223 |
exporter.ExportHostFunc(sockAccept) |
| 224 |
exporter.ExportHostFunc(sockRecv) |
| 225 |
exporter.ExportHostFunc(sockSend) |
| 226 |
exporter.ExportHostFunc(sockShutdown) |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
func writeOffsetsAndNullTerminatedValues(mem api.Memory, values [][]byte, offsets, bytes, bytesLen uint32) sys.Errno { |
| 233 |
|
| 234 |
|
| 235 |
valuesLen := len(values) |
| 236 |
offsetsLen := uint32(valuesLen * 4) |
| 237 |
offsetsBuf, ok := mem.Read(offsets, offsetsLen) |
| 238 |
if !ok { |
| 239 |
return sys.EFAULT |
| 240 |
} |
| 241 |
bytesBuf, ok := mem.Read(bytes, bytesLen) |
| 242 |
if !ok { |
| 243 |
return sys.EFAULT |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
var oI, bI uint32 |
| 249 |
for _, value := range values { |
| 250 |
|
| 251 |
|
| 252 |
bytesOffset := bytes + bI |
| 253 |
offsetsBuf[oI] = byte(bytesOffset) |
| 254 |
offsetsBuf[oI+1] = byte(bytesOffset >> 8) |
| 255 |
offsetsBuf[oI+2] = byte(bytesOffset >> 16) |
| 256 |
offsetsBuf[oI+3] = byte(bytesOffset >> 24) |
| 257 |
oI += 4 |
| 258 |
|
| 259 |
|
| 260 |
copy(bytesBuf[bI:], value) |
| 261 |
bI += uint32(len(value)) |
| 262 |
bytesBuf[bI] = 0 |
| 263 |
bI++ |
| 264 |
} |
| 265 |
|
| 266 |
return 0 |
| 267 |
} |
| 268 |
|
| 269 |
func newHostFunc( |
| 270 |
name string, |
| 271 |
goFunc wasiFunc, |
| 272 |
paramTypes []wasm.ValueType, |
| 273 |
paramNames ...string, |
| 274 |
) *wasm.HostFunc { |
| 275 |
return &wasm.HostFunc{ |
| 276 |
ExportName: name, |
| 277 |
Name: name, |
| 278 |
ParamTypes: paramTypes, |
| 279 |
ParamNames: paramNames, |
| 280 |
ResultTypes: []wasm.ValueType{i32}, |
| 281 |
ResultNames: []string{"errno"}, |
| 282 |
Code: wasm.Code{GoFunc: goFunc}, |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
type wasiFunc func(ctx context.Context, mod api.Module, params []uint64) sys.Errno |
| 289 |
|
| 290 |
|
| 291 |
func (f wasiFunc) Call(ctx context.Context, mod api.Module, stack []uint64) { |
| 292 |
|
| 293 |
errno := f(ctx, mod, stack) |
| 294 |
if errno != 0 { |
| 295 |
stack[0] = uint64(wasip1.ToErrno(errno)) |
| 296 |
} else { |
| 297 |
stack[0] = 0 |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
func stubFunction(name string, paramTypes []wasm.ValueType, paramNames ...string) *wasm.HostFunc { |
| 303 |
return &wasm.HostFunc{ |
| 304 |
ExportName: name, |
| 305 |
Name: name, |
| 306 |
ParamTypes: paramTypes, |
| 307 |
ParamNames: paramNames, |
| 308 |
ResultTypes: []wasm.ValueType{i32}, |
| 309 |
ResultNames: []string{"errno"}, |
| 310 |
Code: wasm.Code{ |
| 311 |
GoFunc: api.GoModuleFunc(func(_ context.Context, _ api.Module, stack []uint64) { stack[0] = uint64(wasip1.ErrnoNosys) }), |
| 312 |
}, |
| 313 |
} |
| 314 |
} |