Skip to content

Commit

Permalink
validation: avoids allocation per br_table (#2252)
Browse files Browse the repository at this point in the history
This eliminates the allocation that previously happened
per br_table instruction during function validation.

Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Jun 14, 2024
1 parent b9571df commit 6d3a77e
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions internal/wasm/func_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,14 +451,14 @@ func (m *Module) validateFunctionWithMaxStackValues(
return fmt.Errorf("read immediate: %w", err)
}

list := make([]uint32, nl)
sts.ls = sts.ls[:0]
for i := uint32(0); i < nl; i++ {
l, n, err := leb128.DecodeUint32(br)
if err != nil {
return fmt.Errorf("read immediate: %w", err)
}
num += n
list[i] = l
sts.ls = append(sts.ls, l)
}
ln, n, err := leb128.DecodeUint32(br)
if err != nil {
Expand Down Expand Up @@ -511,7 +511,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
}
}

for _, l := range list {
for _, l := range sts.ls {
if int(l) >= len(controlBlockStack.stack) {
return fmt.Errorf("invalid l param given for %s", OpcodeBrTableName)
}
Expand Down Expand Up @@ -2003,6 +2003,8 @@ var vecSplatValueTypes = [...]ValueType{
type stacks struct {
vs valueTypeStack
cs controlBlockStack
// ls is the label slice that is reused for each br_table instruction.
ls []uint32
}

func (sts *stacks) reset(functionType *FunctionType) {
Expand All @@ -2012,6 +2014,7 @@ func (sts *stacks) reset(functionType *FunctionType) {
sts.vs.maximumStackPointer = 0
sts.cs.stack = sts.cs.stack[:0]
sts.cs.stack = append(sts.cs.stack, controlBlock{blockType: functionType})
sts.ls = sts.ls[:0]
}

type controlBlockStack struct {
Expand Down

0 comments on commit 6d3a77e

Please sign in to comment.