Changes ModuleID for listener existence per function (#1742)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
Takeshi Yoneda
2023-10-02 10:12:59 +09:00
committed by GitHub
parent 1c582ca1dc
commit a6ef3c7524
4 changed files with 91 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ package wasm
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"errors"
"fmt"
"io"
@@ -11,6 +12,7 @@ import (
"sync"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/internal/ieee754"
"github.com/tetratelabs/wazero/internal/leb128"
"github.com/tetratelabs/wazero/internal/wasmdebug"
@@ -198,13 +200,20 @@ const (
// AssignModuleID calculates a sha256 checksum on `wasm` and other args, and set Module.ID to the result.
// See the doc on Module.ID on what it's used for.
func (m *Module) AssignModuleID(wasm []byte, withListener, withEnsureTermination bool) {
func (m *Module) AssignModuleID(wasm []byte, listeners []experimental.FunctionListener, withEnsureTermination bool) {
h := sha256.New()
h.Write(wasm)
// Use the pre-allocated space on m.ID to append the booleans to sha256 hash.
m.ID[0] = boolToByte(withListener)
m.ID[1] = boolToByte(withEnsureTermination)
h.Write(m.ID[:2])
// Use the pre-allocated space backed by m.ID below.
// Write the existence of listeners to the checksum per function.
for i, l := range listeners {
binary.LittleEndian.PutUint32(m.ID[:], uint32(i))
m.ID[4] = boolToByte(l != nil)
h.Write(m.ID[:5])
}
// Write the flag of ensureTermination to the checksum.
m.ID[0] = boolToByte(withEnsureTermination)
h.Write(m.ID[:1])
// Get checksum by passing the slice underlying m.ID.
h.Sum(m.ID[:0])
}