Skip to content

Commit

Permalink
cleanup: host takes ownership of memory blocks it gets as arguments (#70
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko authored Sep 23, 2024
1 parent b1ee244 commit 5eac189
Showing 1 changed file with 10 additions and 0 deletions.
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

0 comments on commit 5eac189

Please sign in to comment.