core/vm: fix backward jump validation

This commit is contained in:
Marius van der Wijden 2024-08-30 12:50:26 +02:00
parent f314ec0847
commit ee8d477b88

View file

@ -163,7 +163,6 @@ func validateControlFlow2(code []byte, section int, metadata []*FunctionMetadata
if nextPC >= len(code) {
return 0, fmt.Errorf("%w: end with %s, pos %d", ErrInvalidCodeTermination, op, pos)
}
nextOP := code[nextPC]
if nextPC > pos {
// target reached via forward jump or seq flow
nextBounds, ok := stackBounds[nextPC]
@ -176,15 +175,9 @@ func validateControlFlow2(code []byte, section int, metadata []*FunctionMetadata
// target reached via backwards jump
nextBounds, ok := stackBounds[nextPC]
if !ok {
fmt.Print("2")
return 0, ErrInvalidBackwardJump
}
change := int(params.StackLimit) - jt[nextOP].maxStack + jt[nextOP].minStack
if have, want := nextBounds.max+change, currentBounds.max; have < want {
return 0, fmt.Errorf("%w want %d as max got %d at pos %d,", ErrInvalidBackwardJump, want, have, pos)
}
if have, want := nextBounds.min+change, currentBounds.min; have < want {
return 0, fmt.Errorf("%w want %d as min got %d at pos %d,", ErrInvalidBackwardJump, want, have, pos)
}
if currentStackMax != nextBounds.max {
return 0, fmt.Errorf("%w want %d as current max got %d at pos %d,", ErrInvalidBackwardJump, currentStackMax, nextBounds.max, pos)
}