Skip to content

Commit

Permalink
Changes the type of Size() to uint64 (#2074)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Feb 19, 2024
1 parent c1c071e commit 1458ccc
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 57 deletions.
2 changes: 1 addition & 1 deletion api/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ type Memory interface {
// has 1 page: 65536
//
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#-hrefsyntax-instr-memorymathsfmemorysize%E2%91%A0
Size() uint32
Size() uint64

// Grow increases memory by the delta in pages (65536 bytes per page).
// The return val is the previous memory size in pages, or false if the
Expand Down
10 changes: 6 additions & 4 deletions experimental/wazerotest/wazerotest.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ func (m *Memory) Definition() api.MemoryDefinition {
return memoryDefinition{memory: m}
}

func (m *Memory) Size() uint32 {
return uint32(len(m.Bytes))
func (m *Memory) Size() uint64 {
return uint64(len(m.Bytes))
}

func (m *Memory) Grow(deltaPages uint32) (previousPages uint32, ok bool) {
Expand Down Expand Up @@ -624,8 +624,10 @@ func (m *Memory) WriteString(offset uint32, value string) bool {
return true
}

func (m *Memory) isOutOfRange(offset, length uint32) bool {
size := m.Size()
func (m *Memory) isOutOfRange(_offset, _length uint32) bool {
size := int64(m.Size())
offset := int64(_offset)
length := int64(_length)
return offset >= size || length > size || offset > (size-length)
}

Expand Down
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func Test_argsGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
validAddress := uint32(0) // arbitrary

tests := []struct {
Expand Down Expand Up @@ -133,7 +133,7 @@ func Test_argsSizesGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
validAddress := uint32(0) // arbitrary valid address as arguments to args_sizes_get. We chose 0 here.

tests := []struct {
Expand Down
2 changes: 1 addition & 1 deletion imports/wasi_snapshot_preview1/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func Test_clockTimeGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

tests := []struct {
name string
Expand Down
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/environ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Test_environGet_Errors(t *testing.T) {
WithEnv("a", "bc").WithEnv("b", "cd"))
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
validAddress := uint32(0) // arbitrary valid address as arguments to environ_get. We chose 0 here.

tests := []struct {
Expand Down Expand Up @@ -138,7 +138,7 @@ func Test_environSizesGet_Errors(t *testing.T) {
WithEnv("a", "b").WithEnv("b", "cd"))
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
validAddress := uint32(0) // arbitrary

tests := []struct {
Expand Down
44 changes: 22 additions & 22 deletions imports/wasi_snapshot_preview1/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func Test_fdFdstatGet(t *testing.T) {
file, dir := "animals.txt", "sub"
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
defer r.Close(testCtx)
memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

// open both paths without using WASI
fsc := mod.(*wasm.ModuleInstance).Sys.FS()
Expand Down Expand Up @@ -659,7 +659,7 @@ func Test_fdFilestatGet(t *testing.T) {
file, dir := "animals.txt", "sub"
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
defer r.Close(testCtx)
memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

// open both paths without using WASI
fsc := mod.(*wasm.ModuleInstance).Sys.FS()
Expand Down Expand Up @@ -1397,7 +1397,7 @@ func Test_fdPrestatGet_Errors(t *testing.T) {
mod, dirFD, log, r := requireOpenFile(t, t.TempDir(), "tmp", nil, true)
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
tests := []struct {
name string
fd int32
Expand Down Expand Up @@ -1477,7 +1477,7 @@ func Test_fdPrestatDirName_Errors(t *testing.T) {
mod, dirFD, log, r := requireOpenFile(t, t.TempDir(), "tmp", nil, true)
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())
maskMemory(t, mod, 10)

validAddress := uint32(0) // Arbitrary valid address as arguments to fd_prestat_dir_name. We chose 0 here.
Expand Down Expand Up @@ -2304,7 +2304,7 @@ func Test_fdReaddir_Rewind(t *testing.T) {
func Test_fdReaddir_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
defer r.Close(testCtx)
memLen := mod.Memory().Size()
memLen := uint32(mod.Memory().Size())

fsc := mod.(*wasm.ModuleInstance).Sys.FS()
preopen := fsc.RootFS()
Expand Down Expand Up @@ -2640,7 +2640,7 @@ func Test_fdSeek_Errors(t *testing.T) {
require.Zero(t, fsc.RootFS().Mkdir("dir", 0o0777))
dirFD := requireOpenFD(t, mod, "dir")

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

tests := []struct {
name string
Expand Down Expand Up @@ -2791,7 +2791,7 @@ func Test_fdTell_Errors(t *testing.T) {
mod, fd, log, r := requireOpenFile(t, t.TempDir(), "test_path", []byte("wazero"), true)
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

tests := []struct {
name string
Expand Down Expand Up @@ -2888,7 +2888,7 @@ func Test_fdWrite_Errors(t *testing.T) {

// Setup valid test memory
iovsCount := uint32(1)
memSize := mod.Memory().Size()
memSize := uint32(mod.Memory().Size())

tests := []struct {
name string
Expand Down Expand Up @@ -3058,7 +3058,7 @@ func Test_pathCreateDirectory_Errors(t *testing.T) {
{
name: "out-of-memory reading path",
fd: sys.FdPreopen,
path: mod.Memory().Size(),
path: uint32(mod.Memory().Size()),
pathLen: 1,
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -3070,7 +3070,7 @@ func Test_pathCreateDirectory_Errors(t *testing.T) {
name: "out-of-memory reading pathLen",
fd: sys.FdPreopen,
path: 0,
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=OOM(0,65537))
Expand Down Expand Up @@ -3126,7 +3126,7 @@ func Test_pathFilestatGet(t *testing.T) {

mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
defer r.Close(testCtx)
memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

fileFD := requireOpenFD(t, mod, file)

Expand Down Expand Up @@ -4078,7 +4078,7 @@ func Test_pathOpen_Errors(t *testing.T) {
{
name: "out-of-memory reading path",
fd: sys.FdPreopen,
path: mod.Memory().Size(),
path: uint32(mod.Memory().Size()),
pathLen: uint32(len(file)),
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -4090,7 +4090,7 @@ func Test_pathOpen_Errors(t *testing.T) {
name: "out-of-memory reading pathLen",
fd: sys.FdPreopen,
path: 0,
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=OOM(0,65537),oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=)
Expand Down Expand Up @@ -4163,7 +4163,7 @@ func Test_pathOpen_Errors(t *testing.T) {
pathName: dir,
path: 0,
pathLen: uint32(len(dir)),
resultOpenedFd: mod.Memory().Size(), // path and pathLen correctly point to the right path, but where to write the opened FD is outside memory.
resultOpenedFd: uint32(mod.Memory().Size()), // path and pathLen correctly point to the right path, but where to write the opened FD is outside memory.
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=)
Expand Down Expand Up @@ -4416,7 +4416,7 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
{
name: "out-of-memory reading path",
fd: sys.FdPreopen,
path: mod.Memory().Size(),
path: uint32(mod.Memory().Size()),
pathLen: 1,
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -4428,7 +4428,7 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
name: "out-of-memory reading pathLen",
fd: sys.FdPreopen,
path: 0,
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=OOM(0,65537))
Expand Down Expand Up @@ -4682,7 +4682,7 @@ func Test_pathRename_Errors(t *testing.T) {
name: "out-of-memory reading old path",
oldFd: sys.FdPreopen,
newFd: sys.FdPreopen,
oldPath: mod.Memory().Size(),
oldPath: uint32(mod.Memory().Size()),
oldPathLen: 1,
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -4697,7 +4697,7 @@ func Test_pathRename_Errors(t *testing.T) {
oldPath: 0,
oldPathName: "a",
oldPathLen: 1,
newPath: mod.Memory().Size(),
newPath: uint32(mod.Memory().Size()),
newPathLen: 1,
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -4710,7 +4710,7 @@ func Test_pathRename_Errors(t *testing.T) {
oldFd: sys.FdPreopen,
newFd: sys.FdPreopen,
oldPath: 0,
oldPathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
oldPathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=OOM(0,65537),new_fd=3,new_path=)
Expand All @@ -4724,7 +4724,7 @@ func Test_pathRename_Errors(t *testing.T) {
oldPathName: file,
oldPathLen: uint32(len(file)),
newPath: 0,
newPathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
newPathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=file,new_fd=3,new_path=OOM(0,65537))
Expand Down Expand Up @@ -4853,7 +4853,7 @@ func Test_pathUnlinkFile_Errors(t *testing.T) {
{
name: "out-of-memory reading path",
fd: sys.FdPreopen,
path: mod.Memory().Size(),
path: uint32(mod.Memory().Size()),
pathLen: 1,
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
Expand All @@ -4865,7 +4865,7 @@ func Test_pathUnlinkFile_Errors(t *testing.T) {
name: "out-of-memory reading pathLen",
fd: sys.FdPreopen,
path: 0,
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
expectedErrno: wasip1.ErrnoFault,
expectedLog: `
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=OOM(0,65537))
Expand Down
2 changes: 1 addition & 1 deletion imports/wasi_snapshot_preview1/random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_randomGet_Errors(t *testing.T) {
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
defer r.Close(testCtx)

memorySize := mod.Memory().Size()
memorySize := uint32(mod.Memory().Size())

tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4021,7 +4021,7 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
panic(wasmruntime.ErrRuntimeUnalignedAtomic)
}
// Just a bounds check
if offset >= memoryInst.Size() {
if int(offset) >= len(memoryInst.Buffer) {
panic(wasmruntime.ErrRuntimeOutOfBoundsMemoryAccess)
}
res := memoryInst.Notify(offset, uint32(count))
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/wazevo/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ func TestE2E_reexported_memory(t *testing.T) {
mem := m1Inst.Memory()
require.Equal(t, mem, m3Inst.Memory())
require.Equal(t, mem, m2Inst.Memory())
require.Equal(t, uint32(11), mem.Size()/65536)
require.Equal(t, uint64(11), mem.Size()/65536)
}

func TestStackUnwind_panic_in_host(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/integration_test/engine/adhoc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ func testMemOps(t *testing.T, r wazero.Runtime) {
results, err = sizeFn.Call(testCtx)
require.NoError(t, err)
require.Equal(t, uint64(1), results[0]) // 1 page
require.Equal(t, uint32(65536), memory.Size()) // 64KB
require.Equal(t, uint64(65536), memory.Size()) // 64KB

// Grow again so that the memory size matches memory capacity.
results, err = growFn.Call(testCtx, 1)
Expand Down
12 changes: 6 additions & 6 deletions internal/wasm/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ func (m *MemoryInstance) Definition() api.MemoryDefinition {
}

// Size implements the same method as documented on api.Memory.
func (m *MemoryInstance) Size() uint32 {
return m.size()
func (m *MemoryInstance) Size() uint64 {
return uint64(m.size())
}

// ReadByte implements the same method as documented on api.Memory.
func (m *MemoryInstance) ReadByte(offset uint32) (byte, bool) {
if offset >= m.size() {
if int64(offset) >= int64(m.size()) {
return 0, false
}
return m.Buffer[offset], true
Expand Down Expand Up @@ -188,7 +188,7 @@ func (m *MemoryInstance) Read(offset, byteCount uint32) ([]byte, bool) {

// WriteByte implements the same method as documented on api.Memory.
func (m *MemoryInstance) WriteByte(offset uint32, v byte) bool {
if offset >= m.size() {
if int64(offset) >= m.size() {
return false
}
m.Buffer[offset] = v
Expand Down Expand Up @@ -309,8 +309,8 @@ func memoryBytesNumToPages(bytesNum uint64) (pages uint32) {
}

// size returns the size in bytes of the buffer.
func (m *MemoryInstance) size() uint32 {
return uint32(len(m.Buffer)) // We don't lock here because size can't become smaller.
func (m *MemoryInstance) size() int64 {
return int64(len(m.Buffer)) // We don't lock here because size can't become smaller.
}

// hasSize returns true if Len is sufficient for byteCount at the given offset.
Expand Down
Loading

0 comments on commit 1458ccc

Please sign in to comment.