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

Handle negative grow pages to avoid shrinking memory #2120

Merged
merged 1 commit into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions internal/integration_test/fuzzcases/fuzzcases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,3 +1056,10 @@ func Test2112(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "invalid function[0]: unknown misc opcode 0x30")
}

func Test2118(t *testing.T) {
if !platform.CompilerSupported() {
return
}
nodiff.RequireNoDiffT(t, getWasmBinary(t, "2118"), true, true)
}
Binary file not shown.
38 changes: 38 additions & 0 deletions internal/integration_test/fuzzcases/testdata/2118.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(module
(type (;0;) (func (param i32 i32 i32 i32 i32 i64 i32 i32 i32)))
(func (;0;) (type 0) (param i32 i32 i32 i32 i32 i64 i32 i32 i32)
(local v128)
f64.const -0x1.34dbf7bd7ba6p+771 (;=-14984674124766183000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000;)
i32.const 0
i64.const 0
i64.store offset=72462
local.get 7
i64.const 0
i64.store offset=72462
i32.const 0
i64.const 0
i64.store offset=6876
i32.const 1
i64.const 1
i64.store offset=31806 align=1
i32.const 1
i64.const 1
i64.store offset=72462
local.get 7
i64.const 0
i64.store offset=72462
i32.const 1
i64.const 1
i64.store offset=72462
i32.const -8476938
memory.grow
memory.grow
i32.ctz
local.get 7
i64.const 0
i64.store offset=72462
unreachable
)
(memory (;0;) 2 7)
(export "" (func 0))
)
2 changes: 1 addition & 1 deletion internal/wasm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (m *MemoryInstance) Grow(delta uint32) (result uint32, ok bool) {

// If exceeds the max of memory size, we push -1 according to the spec.
newPages := currentPages + delta
if newPages > m.Max {
if newPages > m.Max || int32(delta) < 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't newPages > m.Max || newPages < currentPages be clearer, or am I misunderstanding it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah clearer

return 0, false
} else if newPages > m.Cap { // grow the memory.
if m.Shared {
Expand Down
10 changes: 10 additions & 0 deletions internal/wasm/memory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ func TestMemoryInstance_Grow_Size(t *testing.T) {
}
}

func TestMemoryInstance_NegativeDelta(t *testing.T) {
m := &MemoryInstance{Buffer: make([]byte, 2*MemoryPageSize)}
_negative := -1
negativeu32 := uint32(_negative)
_, ok := m.Grow(negativeu32)
// If the negative page size is given, current_page+delta might overflow, and it can result in accidentally shrinking the memory,
// which is obviously not spec compliant.
require.False(t, ok)
}

func TestMemoryInstance_ReadByte(t *testing.T) {
mem := &MemoryInstance{Buffer: []byte{0, 0, 0, 0, 0, 0, 0, 16}, Min: 1}
v, ok := mem.ReadByte(7)
Expand Down
Loading