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

cleanup: host takes ownership of memory blocks it gets as arguments #70

Merged
merged 2 commits into from
Sep 23, 2024
Merged
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
10 changes: 10 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module) (
hostFunc(name, func(ctx context.Context, m api.Module, offset uint64) {
if plugin, ok := ctx.Value(PluginCtxKey("plugin")).(*Plugin); ok {
if LogLevel(pluginLogLevel.Load()) > level {
plugin.currentPlugin().Free(offset)
return
}

Expand All @@ -319,6 +320,8 @@ func buildEnvModule(ctx context.Context, rt wazero.Runtime, extism api.Module) (

plugin.Log(level, message)

plugin.currentPlugin().Free(offset)

return
}

Expand Down Expand Up @@ -414,6 +417,8 @@ func varGet(ctx context.Context, m api.Module, offset uint64) uint64 {
panic(fmt.Errorf("failed to read var name from memory: %v", err))
}

cp.Free(offset)

value, ok := plugin.Var[name]
if !ok {
// Return 0 without an error if key is not found
Expand Down Expand Up @@ -448,6 +453,8 @@ func varSet(ctx context.Context, m api.Module, nameOffset uint64, valueOffset ui
panic(fmt.Errorf("failed to read var name from memory: %v", err))
}

cp.Free(nameOffset)

// Remove if the value offset is 0
if valueOffset == 0 {
delete(plugin.Var, name)
Expand All @@ -459,6 +466,8 @@ func varSet(ctx context.Context, m api.Module, nameOffset uint64, valueOffset ui
panic(fmt.Errorf("failed to read var value from memory: %v", err))
}

cp.Free(valueOffset)

// Calculate size including current key/value
size := int(unsafe.Sizeof([]byte{})+unsafe.Sizeof("")) + len(name) + len(value)
for k, v := range plugin.Var {
Expand All @@ -485,6 +494,7 @@ func httpRequest(ctx context.Context, m api.Module, requestOffset uint64, bodyOf

var request HttpRequest
err = json.Unmarshal(requestJson, &request)
cp.Free(requestOffset)
if err != nil {
panic(fmt.Errorf("invalid http request: %v", err))
}
Expand Down
Loading