Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ImportFunction ignoring nonexistent imports #20

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ import (
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

var (
ErrModuleNotImported = fmt.Errorf("water: importing a module not imported by the WebAssembly module")
ErrFuncNotImported = fmt.Errorf("water: importing a function not imported by the WebAssembly module")
)

// Core provides the low-level access to the WebAssembly runtime
// environment.
type Core interface {
Expand Down Expand Up @@ -246,17 +251,11 @@ func (c *core) ImportFunction(module, name string, f any) error {
// Unsafe: check if the WebAssembly module really imports this function under
// the given module and name. If not, we warn and skip the import.
if mod, ok := c.ImportedFunctions()[module]; !ok {
log.LWarnf(c.config.Logger(), "water: module %s is not imported, skipping...", module)

// list all the imported modules
for mod := range c.ImportedFunctions() {
log.LWarnf(c.config.Logger(), "water: module %s is imported", mod)
}

return nil
log.LDebugf(c.config.Logger(), "water: module %s is not imported.", module)
return ErrModuleNotImported
} else if _, ok := mod[name]; !ok {
log.LWarnf(c.config.Logger(), "water: function %s.%s is not imported, skipping...", module, name)
return nil
log.LWarnf(c.config.Logger(), "water: function %s.%s is not imported.", module, name)
return ErrFuncNotImported
}

if _, ok := c.importModules[module]; !ok {
Expand Down
36 changes: 28 additions & 8 deletions transport/v0/transport_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ func (tm *TransportModule) LinkNetworkInterface(dialer *ManagedDialer, listener
}

if err := tm.core.ImportFunction("env", "host_dial", dialerFunc); err != nil {
return fmt.Errorf("water: linking dialer function, (*water.Core).ImportFunction: %w", err)
if dialer != nil || err != water.ErrFuncNotImported {
return fmt.Errorf("water: linking dialer function, (*water.Core).ImportFunction: %w", err)
}
}

var acceptFunc func() (fd int32)
Expand All @@ -327,7 +329,9 @@ func (tm *TransportModule) LinkNetworkInterface(dialer *ManagedDialer, listener
}

if err := tm.core.ImportFunction("env", "host_accept", acceptFunc); err != nil {
return fmt.Errorf("water: linking listener function, (*water.Core).ImportFunction: %w", err)
if listener != nil || err != water.ErrFuncNotImported {
return fmt.Errorf("water: linking listener function, (*water.Core).ImportFunction: %w", err)
}
}

return nil
Expand All @@ -347,17 +351,33 @@ func (tm *TransportModule) Initialize() error {
if err = tm.core.ImportFunction("env", "host_defer", func() {
tm.DeferAll()
}); err != nil {
return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+
"when importing host_defer function: %w", err)
if err == water.ErrFuncNotImported {
log.LWarnf(tm.core.Logger(), "water: host_defer function is not imported by WATM, "+
"deferred functions will not be executed when WATM exits")
} else {
return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+
"when importing host_defer function: %w", err)
}
}

// import pull_config function (it is called pushConfig here in the host)
// if tm.core.ImportedFunctions()["env"]["pull_config"] != nil {
if err = tm.core.ImportFunction("env", "pull_config", tm.pushConfig); err != nil {
return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+
"when importing pull_config function: %w", err)
if err == water.ErrFuncNotImported {
// If a config is provided, we will warn the user that the config WILL NOT be
// pushed to the WASM module.
if tm.core.Config().TransportModuleConfig != nil {
f, err := tm.core.Config().TransportModuleConfig.AsFile()
if f != nil && err == nil {
// there is a config file provided, must warn
log.LWarnf(tm.core.Logger(), "water: pull_config function is not imported by WATM, "+
"config file will not be pushed to the WASM module")
}
}
} else {
return fmt.Errorf("water: (*water.Core).ImportFunction returned error "+
"when importing pull_config function: %w", err)
}
}
// }

// instantiate the WASM module
if err = tm.core.Instantiate(); err != nil {
Expand Down