Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validation: avoids allocation per br_table #2252

Merged
merged 1 commit into from
Jun 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading