Skip to content

Commit

Permalink
cleanup: host takes ownership of memory blocks it gets as arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
zshipko committed Sep 5, 2024
1 parent 39b5924 commit 8a6b630
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions host.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,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 @@ -408,6 +410,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 @@ -442,6 +446,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 @@ -453,6 +459,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 @@ -475,6 +483,7 @@ func httpRequest(ctx context.Context, m api.Module, requestOffset uint64, bodyOf
requestJson, err := cp.ReadBytes(requestOffset)
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 8a6b630

Please sign in to comment.