Skip to content

Commit 1458ccc

Browse files
authored
Changes the type of Size() to uint64 (#2074)
Signed-off-by: Takeshi Yoneda <[email protected]>
1 parent c1c071e commit 1458ccc

File tree

14 files changed

+59
-57
lines changed

14 files changed

+59
-57
lines changed

Diff for: api/wasm.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ type Memory interface {
563563
// has 1 page: 65536
564564
//
565565
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#-hrefsyntax-instr-memorymathsfmemorysize%E2%91%A0
566-
Size() uint32
566+
Size() uint64
567567

568568
// Grow increases memory by the delta in pages (65536 bytes per page).
569569
// The return val is the previous memory size in pages, or false if the

Diff for: experimental/wazerotest/wazerotest.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ func (m *Memory) Definition() api.MemoryDefinition {
507507
return memoryDefinition{memory: m}
508508
}
509509

510-
func (m *Memory) Size() uint32 {
511-
return uint32(len(m.Bytes))
510+
func (m *Memory) Size() uint64 {
511+
return uint64(len(m.Bytes))
512512
}
513513

514514
func (m *Memory) Grow(deltaPages uint32) (previousPages uint32, ok bool) {
@@ -624,8 +624,10 @@ func (m *Memory) WriteString(offset uint32, value string) bool {
624624
return true
625625
}
626626

627-
func (m *Memory) isOutOfRange(offset, length uint32) bool {
628-
size := m.Size()
627+
func (m *Memory) isOutOfRange(_offset, _length uint32) bool {
628+
size := int64(m.Size())
629+
offset := int64(_offset)
630+
length := int64(_length)
629631
return offset >= size || length > size || offset > (size-length)
630632
}
631633

Diff for: imports/wasi_snapshot_preview1/args_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Test_argsGet_Errors(t *testing.T) {
4141
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithArgs("a", "bc"))
4242
defer r.Close(testCtx)
4343

44-
memorySize := mod.Memory().Size()
44+
memorySize := uint32(mod.Memory().Size())
4545
validAddress := uint32(0) // arbitrary
4646

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

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

139139
tests := []struct {

Diff for: imports/wasi_snapshot_preview1/clock_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ func Test_clockTimeGet_Errors(t *testing.T) {
259259
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
260260
defer r.Close(testCtx)
261261

262-
memorySize := mod.Memory().Size()
262+
memorySize := uint32(mod.Memory().Size())
263263

264264
tests := []struct {
265265
name string

Diff for: imports/wasi_snapshot_preview1/environ_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func Test_environGet_Errors(t *testing.T) {
4444
WithEnv("a", "bc").WithEnv("b", "cd"))
4545
defer r.Close(testCtx)
4646

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

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

141-
memorySize := mod.Memory().Size()
141+
memorySize := uint32(mod.Memory().Size())
142142
validAddress := uint32(0) // arbitrary
143143

144144
tests := []struct {

Diff for: imports/wasi_snapshot_preview1/fs_test.go

+22-22
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ func Test_fdFdstatGet(t *testing.T) {
233233
file, dir := "animals.txt", "sub"
234234
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
235235
defer r.Close(testCtx)
236-
memorySize := mod.Memory().Size()
236+
memorySize := uint32(mod.Memory().Size())
237237

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

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

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

1480-
memorySize := mod.Memory().Size()
1480+
memorySize := uint32(mod.Memory().Size())
14811481
maskMemory(t, mod, 10)
14821482

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

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

2643-
memorySize := mod.Memory().Size()
2643+
memorySize := uint32(mod.Memory().Size())
26442644

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

2794-
memorySize := mod.Memory().Size()
2794+
memorySize := uint32(mod.Memory().Size())
27952795

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

28892889
// Setup valid test memory
28902890
iovsCount := uint32(1)
2891-
memSize := mod.Memory().Size()
2891+
memSize := uint32(mod.Memory().Size())
28922892

28932893
tests := []struct {
28942894
name string
@@ -3058,7 +3058,7 @@ func Test_pathCreateDirectory_Errors(t *testing.T) {
30583058
{
30593059
name: "out-of-memory reading path",
30603060
fd: sys.FdPreopen,
3061-
path: mod.Memory().Size(),
3061+
path: uint32(mod.Memory().Size()),
30623062
pathLen: 1,
30633063
expectedErrno: wasip1.ErrnoFault,
30643064
expectedLog: `
@@ -3070,7 +3070,7 @@ func Test_pathCreateDirectory_Errors(t *testing.T) {
30703070
name: "out-of-memory reading pathLen",
30713071
fd: sys.FdPreopen,
30723072
path: 0,
3073-
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
3073+
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
30743074
expectedErrno: wasip1.ErrnoFault,
30753075
expectedLog: `
30763076
==> wasi_snapshot_preview1.path_create_directory(fd=3,path=OOM(0,65537))
@@ -3126,7 +3126,7 @@ func Test_pathFilestatGet(t *testing.T) {
31263126

31273127
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig().WithFS(fstest.FS))
31283128
defer r.Close(testCtx)
3129-
memorySize := mod.Memory().Size()
3129+
memorySize := uint32(mod.Memory().Size())
31303130

31313131
fileFD := requireOpenFD(t, mod, file)
31323132

@@ -4078,7 +4078,7 @@ func Test_pathOpen_Errors(t *testing.T) {
40784078
{
40794079
name: "out-of-memory reading path",
40804080
fd: sys.FdPreopen,
4081-
path: mod.Memory().Size(),
4081+
path: uint32(mod.Memory().Size()),
40824082
pathLen: uint32(len(file)),
40834083
expectedErrno: wasip1.ErrnoFault,
40844084
expectedLog: `
@@ -4090,7 +4090,7 @@ func Test_pathOpen_Errors(t *testing.T) {
40904090
name: "out-of-memory reading pathLen",
40914091
fd: sys.FdPreopen,
40924092
path: 0,
4093-
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
4093+
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
40944094
expectedErrno: wasip1.ErrnoFault,
40954095
expectedLog: `
40964096
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=OOM(0,65537),oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=)
@@ -4163,7 +4163,7 @@ func Test_pathOpen_Errors(t *testing.T) {
41634163
pathName: dir,
41644164
path: 0,
41654165
pathLen: uint32(len(dir)),
4166-
resultOpenedFd: mod.Memory().Size(), // path and pathLen correctly point to the right path, but where to write the opened FD is outside memory.
4166+
resultOpenedFd: uint32(mod.Memory().Size()), // path and pathLen correctly point to the right path, but where to write the opened FD is outside memory.
41674167
expectedErrno: wasip1.ErrnoFault,
41684168
expectedLog: `
41694169
==> wasi_snapshot_preview1.path_open(fd=3,dirflags=,path=dir,oflags=,fs_rights_base=,fs_rights_inheriting=,fdflags=)
@@ -4416,7 +4416,7 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
44164416
{
44174417
name: "out-of-memory reading path",
44184418
fd: sys.FdPreopen,
4419-
path: mod.Memory().Size(),
4419+
path: uint32(mod.Memory().Size()),
44204420
pathLen: 1,
44214421
expectedErrno: wasip1.ErrnoFault,
44224422
expectedLog: `
@@ -4428,7 +4428,7 @@ func Test_pathRemoveDirectory_Errors(t *testing.T) {
44284428
name: "out-of-memory reading pathLen",
44294429
fd: sys.FdPreopen,
44304430
path: 0,
4431-
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
4431+
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
44324432
expectedErrno: wasip1.ErrnoFault,
44334433
expectedLog: `
44344434
==> wasi_snapshot_preview1.path_remove_directory(fd=3,path=OOM(0,65537))
@@ -4682,7 +4682,7 @@ func Test_pathRename_Errors(t *testing.T) {
46824682
name: "out-of-memory reading old path",
46834683
oldFd: sys.FdPreopen,
46844684
newFd: sys.FdPreopen,
4685-
oldPath: mod.Memory().Size(),
4685+
oldPath: uint32(mod.Memory().Size()),
46864686
oldPathLen: 1,
46874687
expectedErrno: wasip1.ErrnoFault,
46884688
expectedLog: `
@@ -4697,7 +4697,7 @@ func Test_pathRename_Errors(t *testing.T) {
46974697
oldPath: 0,
46984698
oldPathName: "a",
46994699
oldPathLen: 1,
4700-
newPath: mod.Memory().Size(),
4700+
newPath: uint32(mod.Memory().Size()),
47014701
newPathLen: 1,
47024702
expectedErrno: wasip1.ErrnoFault,
47034703
expectedLog: `
@@ -4710,7 +4710,7 @@ func Test_pathRename_Errors(t *testing.T) {
47104710
oldFd: sys.FdPreopen,
47114711
newFd: sys.FdPreopen,
47124712
oldPath: 0,
4713-
oldPathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
4713+
oldPathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
47144714
expectedErrno: wasip1.ErrnoFault,
47154715
expectedLog: `
47164716
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=OOM(0,65537),new_fd=3,new_path=)
@@ -4724,7 +4724,7 @@ func Test_pathRename_Errors(t *testing.T) {
47244724
oldPathName: file,
47254725
oldPathLen: uint32(len(file)),
47264726
newPath: 0,
4727-
newPathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
4727+
newPathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
47284728
expectedErrno: wasip1.ErrnoFault,
47294729
expectedLog: `
47304730
==> wasi_snapshot_preview1.path_rename(fd=3,old_path=file,new_fd=3,new_path=OOM(0,65537))
@@ -4853,7 +4853,7 @@ func Test_pathUnlinkFile_Errors(t *testing.T) {
48534853
{
48544854
name: "out-of-memory reading path",
48554855
fd: sys.FdPreopen,
4856-
path: mod.Memory().Size(),
4856+
path: uint32(mod.Memory().Size()),
48574857
pathLen: 1,
48584858
expectedErrno: wasip1.ErrnoFault,
48594859
expectedLog: `
@@ -4865,7 +4865,7 @@ func Test_pathUnlinkFile_Errors(t *testing.T) {
48654865
name: "out-of-memory reading pathLen",
48664866
fd: sys.FdPreopen,
48674867
path: 0,
4868-
pathLen: mod.Memory().Size() + 1, // path is in the valid memory range, but pathLen is OOM for path
4868+
pathLen: uint32(mod.Memory().Size()) + 1, // path is in the valid memory range, but pathLen is OOM for path
48694869
expectedErrno: wasip1.ErrnoFault,
48704870
expectedLog: `
48714871
==> wasi_snapshot_preview1.path_unlink_file(fd=3,path=OOM(0,65537))

Diff for: imports/wasi_snapshot_preview1/random_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func Test_randomGet_Errors(t *testing.T) {
4343
mod, r, log := requireProxyModule(t, wazero.NewModuleConfig())
4444
defer r.Close(testCtx)
4545

46-
memorySize := mod.Memory().Size()
46+
memorySize := uint32(mod.Memory().Size())
4747

4848
tests := []struct {
4949
name string

Diff for: internal/engine/interpreter/interpreter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4021,7 +4021,7 @@ func (ce *callEngine) callNativeFunc(ctx context.Context, m *wasm.ModuleInstance
40214021
panic(wasmruntime.ErrRuntimeUnalignedAtomic)
40224022
}
40234023
// Just a bounds check
4024-
if offset >= memoryInst.Size() {
4024+
if int(offset) >= len(memoryInst.Buffer) {
40254025
panic(wasmruntime.ErrRuntimeOutOfBoundsMemoryAccess)
40264026
}
40274027
res := memoryInst.Notify(offset, uint32(count))

Diff for: internal/engine/wazevo/e2e_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1150,7 +1150,7 @@ func TestE2E_reexported_memory(t *testing.T) {
11501150
mem := m1Inst.Memory()
11511151
require.Equal(t, mem, m3Inst.Memory())
11521152
require.Equal(t, mem, m2Inst.Memory())
1153-
require.Equal(t, uint32(11), mem.Size()/65536)
1153+
require.Equal(t, uint64(11), mem.Size()/65536)
11541154
}
11551155

11561156
func TestStackUnwind_panic_in_host(t *testing.T) {

Diff for: internal/integration_test/engine/adhoc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ func testMemOps(t *testing.T, r wazero.Runtime) {
818818
results, err = sizeFn.Call(testCtx)
819819
require.NoError(t, err)
820820
require.Equal(t, uint64(1), results[0]) // 1 page
821-
require.Equal(t, uint32(65536), memory.Size()) // 64KB
821+
require.Equal(t, uint64(65536), memory.Size()) // 64KB
822822

823823
// Grow again so that the memory size matches memory capacity.
824824
results, err = growFn.Call(testCtx, 1)

Diff for: internal/wasm/memory.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ func (m *MemoryInstance) Definition() api.MemoryDefinition {
130130
}
131131

132132
// Size implements the same method as documented on api.Memory.
133-
func (m *MemoryInstance) Size() uint32 {
134-
return m.size()
133+
func (m *MemoryInstance) Size() uint64 {
134+
return uint64(m.size())
135135
}
136136

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

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

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

316316
// hasSize returns true if Len is sufficient for byteCount at the given offset.

0 commit comments

Comments
 (0)