Disable comp cache for host modules (#949)
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
@@ -53,7 +53,7 @@ func (e *engine) getCodesFromMemory(module *wasm.Module) (codes []*code, ok bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) addCodesToCache(module *wasm.Module, codes []*code) (err error) {
|
func (e *engine) addCodesToCache(module *wasm.Module, codes []*code) (err error) {
|
||||||
if e.Cache == nil {
|
if e.Cache == nil || module.IsHostModule {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = e.Cache.Add(module.ID, serializeCodes(e.wazeroVersion, codes))
|
err = e.Cache.Add(module.ID, serializeCodes(e.wazeroVersion, codes))
|
||||||
@@ -61,7 +61,7 @@ func (e *engine) addCodesToCache(module *wasm.Module, codes []*code) (err error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *engine) getCodesFromCache(module *wasm.Module) (codes []*code, hit bool, err error) {
|
func (e *engine) getCodesFromCache(module *wasm.Module) (codes []*code, hit bool, err error) {
|
||||||
if e.Cache == nil {
|
if e.Cache == nil || module.IsHostModule {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package compiler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -212,10 +213,26 @@ func TestDeserializeCodes(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEngine_getCodesFromCache(t *testing.T) {
|
func TestEngine_getCodesFromCache(t *testing.T) {
|
||||||
|
valid := concat(
|
||||||
|
[]byte(wazeroMagic),
|
||||||
|
[]byte{byte(len(testVersion))},
|
||||||
|
[]byte(testVersion),
|
||||||
|
u32.LeBytes(2), // number of functions.
|
||||||
|
// Function index = 0.
|
||||||
|
u64.LeBytes(12345), // stack pointer ceil.
|
||||||
|
u64.LeBytes(5), // length of code.
|
||||||
|
[]byte{1, 2, 3, 4, 5}, // code.
|
||||||
|
// Function index = 1.
|
||||||
|
u64.LeBytes(0xffffffff), // stack pointer ceil.
|
||||||
|
u64.LeBytes(3), // length of code.
|
||||||
|
[]byte{1, 2, 3}, // code.
|
||||||
|
)
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
ext *testCache
|
ext *testCache
|
||||||
key wasm.ModuleID
|
key wasm.ModuleID
|
||||||
|
isHostMod bool
|
||||||
expCodes []*code
|
expCodes []*code
|
||||||
expHit bool
|
expHit bool
|
||||||
expErr string
|
expErr string
|
||||||
@@ -226,6 +243,11 @@ func TestEngine_getCodesFromCache(t *testing.T) {
|
|||||||
name: "not hit",
|
name: "not hit",
|
||||||
ext: &testCache{caches: map[wasm.ModuleID][]byte{}},
|
ext: &testCache{caches: map[wasm.ModuleID][]byte{}},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "host module",
|
||||||
|
ext: &testCache{caches: map[wasm.ModuleID][]byte{{}: valid}},
|
||||||
|
isHostMod: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "error in Cache.Get",
|
name: "error in Cache.Get",
|
||||||
ext: &testCache{caches: map[wasm.ModuleID][]byte{{}: {}}},
|
ext: &testCache{caches: map[wasm.ModuleID][]byte{{}: {}}},
|
||||||
@@ -249,20 +271,7 @@ func TestEngine_getCodesFromCache(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "hit",
|
name: "hit",
|
||||||
ext: &testCache{caches: map[wasm.ModuleID][]byte{
|
ext: &testCache{caches: map[wasm.ModuleID][]byte{
|
||||||
{}: concat(
|
{}: valid,
|
||||||
[]byte(wazeroMagic),
|
|
||||||
[]byte{byte(len(testVersion))},
|
|
||||||
[]byte(testVersion),
|
|
||||||
u32.LeBytes(2), // number of functions.
|
|
||||||
// Function index = 0.
|
|
||||||
u64.LeBytes(12345), // stack pointer ceil.
|
|
||||||
u64.LeBytes(5), // length of code.
|
|
||||||
[]byte{1, 2, 3, 4, 5}, // code.
|
|
||||||
// Function index = 1.
|
|
||||||
u64.LeBytes(0xffffffff), // stack pointer ceil.
|
|
||||||
u64.LeBytes(3), // length of code.
|
|
||||||
[]byte{1, 2, 3}, // code.
|
|
||||||
),
|
|
||||||
}},
|
}},
|
||||||
expHit: true,
|
expHit: true,
|
||||||
expCodes: []*code{
|
expCodes: []*code{
|
||||||
@@ -275,7 +284,7 @@ func TestEngine_getCodesFromCache(t *testing.T) {
|
|||||||
for _, tc := range tests {
|
for _, tc := range tests {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
m := &wasm.Module{ID: tc.key}
|
m := &wasm.Module{ID: tc.key, IsHostModule: tc.isHostMod}
|
||||||
for _, expC := range tc.expCodes {
|
for _, expC := range tc.expCodes {
|
||||||
expC.sourceModule = m
|
expC.sourceModule = m
|
||||||
}
|
}
|
||||||
@@ -308,6 +317,18 @@ func TestEngine_addCodesToCache(t *testing.T) {
|
|||||||
err := e.addCodesToCache(nil, nil)
|
err := e.addCodesToCache(nil, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
})
|
})
|
||||||
|
t.Run("host module", func(t *testing.T) {
|
||||||
|
tc := &testCache{caches: map[wasm.ModuleID][]byte{}}
|
||||||
|
e := engine{Cache: tc}
|
||||||
|
codes := []*code{{stackPointerCeil: 123, codeSegment: []byte{1, 2, 3}}}
|
||||||
|
m := &wasm.Module{ID: sha256.Sum256(nil), IsHostModule: true} // Host module!
|
||||||
|
err := e.addCodesToCache(m, codes)
|
||||||
|
require.NoError(t, err)
|
||||||
|
// Check the host module not cached.
|
||||||
|
content, ok := tc.caches[m.ID]
|
||||||
|
require.False(t, ok)
|
||||||
|
require.Nil(t, content)
|
||||||
|
})
|
||||||
t.Run("add", func(t *testing.T) {
|
t.Run("add", func(t *testing.T) {
|
||||||
ext := &testCache{caches: map[wasm.ModuleID][]byte{}}
|
ext := &testCache{caches: map[wasm.ModuleID][]byte{}}
|
||||||
e := engine{Cache: ext}
|
e := engine{Cache: ext}
|
||||||
|
|||||||
@@ -104,7 +104,7 @@ func NewHostModule(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assigns the ModuleID by calculating sha256 on inputs as host modules do not have `wasm` to hash.
|
m.IsHostModule = true
|
||||||
m.AssignModuleID([]byte(fmt.Sprintf("%s:%v:%v", moduleName, nameToGoFunc, enabledFeatures)))
|
m.AssignModuleID([]byte(fmt.Sprintf("%s:%v:%v", moduleName, nameToGoFunc, enabledFeatures)))
|
||||||
m.BuildFunctionDefinitions()
|
m.BuildFunctionDefinitions()
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -119,6 +119,7 @@ func TestNewHostModule(t *testing.T) {
|
|||||||
m, e := NewHostModule(tc.moduleName, tc.nameToGoFunc, tc.funcToNames, api.CoreFeaturesV2)
|
m, e := NewHostModule(tc.moduleName, tc.nameToGoFunc, tc.funcToNames, api.CoreFeaturesV2)
|
||||||
require.NoError(t, e)
|
require.NoError(t, e)
|
||||||
requireHostModuleEquals(t, tc.expected, m)
|
requireHostModuleEquals(t, tc.expected, m)
|
||||||
|
require.True(t, m.IsHostModule)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -180,6 +180,9 @@ type Module struct {
|
|||||||
// ID is the sha256 value of the source wasm and is used for caching.
|
// ID is the sha256 value of the source wasm and is used for caching.
|
||||||
ID ModuleID
|
ID ModuleID
|
||||||
|
|
||||||
|
// IsHostModule true if this is the host module, false otherwise.
|
||||||
|
IsHostModule bool
|
||||||
|
|
||||||
// FunctionDefinitionSection is a wazero-specific section built on Validate.
|
// FunctionDefinitionSection is a wazero-specific section built on Validate.
|
||||||
FunctionDefinitionSection []*FunctionDefinition
|
FunctionDefinitionSection []*FunctionDefinition
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user