Skip to content

Commit

Permalink
Merge branch 'main' into fix_module_linking
Browse files Browse the repository at this point in the history
  • Loading branch information
mhmd-azeez authored Jan 23, 2025
2 parents 1d744f0 + 1e14b80 commit 9114be4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
9 changes: 8 additions & 1 deletion extism.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ type Runtime struct {

// PluginInstanceConfig contains configuration options for the Extism plugin.
type PluginInstanceConfig struct {
// ModuleConfig allows the user to specify custom module configuration.
//
// NOTE: Module name and start functions are ignored as they are overridden by Extism, also if Manifest contains
// non-empty AllowedPaths, then FS is also ignored. If EXTISM_ENABLE_WASI_OUTPUT is set, then stdout and stderr are
// set to os.Stdout and os.Stderr respectively (ignoring user defined module config).
ModuleConfig wazero.ModuleConfig
}

Expand Down Expand Up @@ -109,11 +114,13 @@ func (l LogLevel) String() string {
type Plugin struct {
close []func(ctx context.Context) error
extism api.Module

mainModule api.Module
modules map[string]api.Module
Timeout time.Duration

Check failure on line 119 in extism.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

other declaration of Timeout

Check failure on line 119 in extism.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

other declaration of Timeout
Config map[string]string

Check failure on line 120 in extism.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

other declaration of Config

Check failure on line 120 in extism.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

other declaration of Config
module api.Module
Timeout time.Duration

Check failure on line 122 in extism.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

Timeout redeclared

Check failure on line 122 in extism.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

Timeout redeclared
Config map[string]string

Check failure on line 123 in extism.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

Config redeclared

Check failure on line 123 in extism.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

Config redeclared
Var map[string][]byte
AllowedHosts []string
AllowedPaths map[string]string
Expand Down
47 changes: 27 additions & 20 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ type PluginConfig struct {
// When plugins are built using NewCompiledPlugin, the ModuleConfig has no
// effect because the instance is not created. Instead, the ModuleConfig is
// passed directly in calls to the CompiledPlugin.Instance method.
//
// NOTE: Module name and start functions are ignored as they are overridden by Extism, also if Manifest contains
// non-empty AllowedPaths, then FS is also ignored. If EXTISM_ENABLE_WASI_OUTPUT is set, then stdout and stderr are
// set to os.Stdout and os.Stderr respectively (ignoring user defined module config).
ModuleConfig wazero.ModuleConfig
}

Expand Down Expand Up @@ -116,27 +120,25 @@ func NewCompiledPlugin(
return nil, fmt.Errorf("manifest can't be empty")
}

var cfg wazero.RuntimeConfig
if config.RuntimeConfig == nil {
cfg = wazero.NewRuntimeConfig()
} else {
cfg = config.RuntimeConfig
runtimeConfig := config.RuntimeConfig
if runtimeConfig == nil {
runtimeConfig = wazero.NewRuntimeConfig()
}

// Make sure function calls are cancelled if the context is cancelled
if manifest.Timeout > 0 {
cfg = cfg.WithCloseOnContextDone(true)
runtimeConfig = runtimeConfig.WithCloseOnContextDone(true)
}

if manifest.Memory != nil {
if manifest.Memory.MaxPages > 0 {
cfg = cfg.WithMemoryLimitPages(manifest.Memory.MaxPages)
runtimeConfig = runtimeConfig.WithMemoryLimitPages(manifest.Memory.MaxPages)
}
}

p := CompiledPlugin{
manifest: manifest,
runtime: wazero.NewRuntimeWithConfig(ctx, cfg),
runtime: wazero.NewRuntimeWithConfig(ctx, runtimeConfig),
observeAdapter: config.ObserveAdapter,
observeOptions: config.ObserveOptions,
enableHttpResponseHeaders: config.EnableHttpResponseHeaders,
Expand Down Expand Up @@ -315,22 +317,27 @@ func (p *CompiledPlugin) Instance(ctx context.Context, config PluginInstanceConf
moduleConfig = wazero.NewModuleConfig()
}

// NOTE: this is only necessary for guest modules because
// host modules have the same access privileges as the host itself
fs := wazero.NewFSConfig()
for host, guest := range p.manifest.AllowedPaths {
if strings.HasPrefix(host, "ro:") {
trimmed := strings.TrimPrefix(host, "ro:")
fs = fs.WithReadOnlyDirMount(trimmed, guest)
} else {
fs = fs.WithDirMount(host, guest)
}
}
moduleConfig = moduleConfig.WithName(strconv.Itoa(int(p.instanceCount.Add(1))))

Check failure on line 320 in plugin.go

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, stable)

undefined: strconv

Check failure on line 320 in plugin.go

View workflow job for this annotation

GitHub Actions / build (macos-latest, stable)

undefined: strconv

// NOTE: we don't want wazero to call the start function, we will initialize
// the guest runtime manually.
// See: https://github.com/extism/go-sdk/pull/1#issuecomment-1650527495
moduleConfig = moduleConfig.WithStartFunctions().WithFSConfig(fs)
moduleConfig = moduleConfig.WithStartFunctions()

if len(p.manifest.AllowedPaths) > 0 {
// NOTE: this is only necessary for guest modules because
// host modules have the same access privileges as the host itself
fs := wazero.NewFSConfig()
for host, guest := range p.manifest.AllowedPaths {
if strings.HasPrefix(host, "ro:") {
trimmed := strings.TrimPrefix(host, "ro:")
fs = fs.WithReadOnlyDirMount(trimmed, guest)
} else {
fs = fs.WithDirMount(host, guest)
}
}
moduleConfig = moduleConfig.WithFSConfig(fs)
}

_, wasiOutput := os.LookupEnv("EXTISM_ENABLE_WASI_OUTPUT")
if p.hasWasi && wasiOutput {
Expand Down

0 comments on commit 9114be4

Please sign in to comment.