From e8f054d59880f0bf459dacb6a80bea3626ca83d8 Mon Sep 17 00:00:00 2001 From: rrai35 Date: Fri, 29 Sep 2023 10:16:43 -0400 Subject: [PATCH] feat(syswall): Adding syswall, rand and time capability to wazero --- engines/wazero/wazero.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/engines/wazero/wazero.go b/engines/wazero/wazero.go index 7b9dd72..9f6b46d 100644 --- a/engines/wazero/wazero.go +++ b/engines/wazero/wazero.go @@ -2,6 +2,7 @@ package wazero import ( "context" + "crypto/rand" "errors" "fmt" "sync/atomic" @@ -124,7 +125,7 @@ func (e *engine) New(ctx context.Context, host wapc.HostCallHandler, guest []byt m := &Module{runtime: r, wapcHostCallHandler: host} m.config = wazero.NewModuleConfig(). - WithStartFunctions(functionStart, functionInit) // Call any WASI or waPC start functions on instantiate. + WithStartFunctions() // Manually call _start and _init if config.Stdout != nil { m.config = m.config.WithStdout(config.Stdout) @@ -132,6 +133,8 @@ func (e *engine) New(ctx context.Context, host wapc.HostCallHandler, guest []byt if config.Stderr != nil { m.config = m.config.WithStderr(config.Stderr) } + m.config = m.config.WithRandSource(rand.Reader).WithSysNanosleep().WithSysNanotime().WithSysWalltime() + mod = m if _, err = instantiateWapcHost(ctx, r, m.wapcHostCallHandler, config.Logger); err != nil { @@ -371,6 +374,21 @@ func (m *Module) Instantiate(ctx context.Context) (wapc.Instance, error) { return nil, err } + // Call any WASI or waPC start functions on instantiate. + funcs := []string{functionStart, functionInit} + for _, f := range funcs { + exportedFunc := module.ExportedFunction(f) + if exportedFunc != nil { + ic := invokeContext{operation: f, guestReq: nil} + ictx := newInvokeContext(ctx, &ic) + if _, err := exportedFunc.Call(ictx); err != nil { + module.Close(ctx) + + return nil, fmt.Errorf("error calling %s: %v", f, err) + } + } + } + instance := Instance{name: moduleName, m: module} if instance.guestCall = module.ExportedFunction(functionGuestCall); instance.guestCall == nil {