Fixes memory capacity with max larger than limit (#1356)

Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Takeshi Yoneda
2023-04-11 17:23:48 -07:00
committed by GitHub
parent 3aeeb0b01d
commit a56b4435c1
2 changed files with 24 additions and 16 deletions

View File

@@ -181,7 +181,7 @@ func newMemorySizer(memoryLimitPages uint32, memoryCapacityFromMax bool) memoryS
} }
// This is a valid value, but it goes over the run-time limit: return the limit. // This is a valid value, but it goes over the run-time limit: return the limit.
if *maxPages > memoryLimitPages { if *maxPages > memoryLimitPages {
return minPages, memoryLimitPages, memoryLimitPages return minPages, minPages, memoryLimitPages
} }
return minPages, minPages, *maxPages return minPages, minPages, *maxPages
} }

View File

@@ -12,7 +12,7 @@ import (
func Test_newMemorySizer(t *testing.T) { func Test_newMemorySizer(t *testing.T) {
zero := uint32(0) zero := uint32(0)
one := uint32(1) ten := uint32(10)
defaultLimit := wasm.MemoryLimitPages defaultLimit := wasm.MemoryLimitPages
tests := []struct { tests := []struct {
@@ -50,23 +50,23 @@ func Test_newMemorySizer(t *testing.T) {
expectedMax: zero, expectedMax: zero,
}, },
{ {
name: "min 0, max 1", name: "min 0, max 10",
limit: defaultLimit, limit: defaultLimit,
min: zero, min: zero,
max: &one, max: &ten,
expectedMin: zero, expectedMin: zero,
expectedCapacity: zero, expectedCapacity: zero,
expectedMax: one, expectedMax: ten,
}, },
{ {
name: "min 0, max 1 memoryCapacityFromMax", name: "min 0, max 10 memoryCapacityFromMax",
limit: defaultLimit, limit: defaultLimit,
memoryCapacityFromMax: true, memoryCapacityFromMax: true,
min: zero, min: zero,
max: &one, max: &ten,
expectedMin: zero, expectedMin: zero,
expectedCapacity: one, expectedCapacity: ten,
expectedMax: one, expectedMax: ten,
}, },
{ {
name: "min 10, no max", name: "min 10, no max",
@@ -88,11 +88,20 @@ func Test_newMemorySizer(t *testing.T) {
{ {
name: "min=max", name: "min=max",
limit: defaultLimit, limit: defaultLimit,
min: one, min: ten,
max: &one, max: &ten,
expectedMin: one, expectedMin: ten,
expectedCapacity: one, expectedCapacity: ten,
expectedMax: one, expectedMax: ten,
},
{
name: "max > memoryLimitPages",
limit: 5,
min: 0,
max: &ten,
expectedMin: 0,
expectedCapacity: 0,
expectedMax: 5,
}, },
} }
@@ -168,10 +177,9 @@ func TestMemoryType(t *testing.T) {
tmax := max tmax := max
expectedDecoded := tc.input expectedDecoded := tc.input
if tc.memoryLimitPages != 0 { if tc.memoryLimitPages != 0 {
// If a memory limit exists, then the expected module Max, Cap reflect that limit. // If a memory limit exists, then the expected module Max reflects that limit.
tmax = tc.memoryLimitPages tmax = tc.memoryLimitPages
expectedDecoded.Max = tmax expectedDecoded.Max = tmax
expectedDecoded.Cap = tmax
} }
binary, err := decodeMemory(bytes.NewReader(b), newMemorySizer(tmax, false), tmax) binary, err := decodeMemory(bytes.NewReader(b), newMemorySizer(tmax, false), tmax)