Skip to content

Commit

Permalink
api: reverts the uint32->uint64 breaking change for 2.0 release (#2106)
Browse files Browse the repository at this point in the history
Signed-off-by: Takeshi Yoneda <[email protected]>
  • Loading branch information
mathetake authored Mar 2, 2024
1 parent 0b0a23c commit 712f937
Show file tree
Hide file tree
Showing 28 changed files with 135 additions and 136 deletions.
4 changes: 2 additions & 2 deletions 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() uint64
Size() uint32

// 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 Expand Up @@ -635,7 +635,7 @@ type Memory interface {
// shared. Those who need a stable view must set Wasm memory min=max, or
// use wazero.RuntimeConfig WithMemoryCapacityPages to ensure max is always
// allocated.
Read(offset uint32, byteCount uint64) ([]byte, bool)
Read(offset, byteCount uint32) ([]byte, bool)

// WriteByte writes a single byte to the underlying buffer at the offset in or returns false if out of range.
WriteByte(offset uint32, v byte) bool
Expand Down
4 changes: 2 additions & 2 deletions examples/allocation/rust/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func main() {
defer deallocate.Call(ctx, uint64(greetingPtr), uint64(greetingSize))

// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(greetingSize)); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size())
} else {
Expand All @@ -100,7 +100,7 @@ func main() {
}

func logString(ctx context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(offset, uint64(byteCount))
buf, ok := m.Memory().Read(offset, byteCount)
if !ok {
log.Panicf("Memory.Read(%d, %d) out of range", offset, byteCount)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/allocation/tinygo/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func main() {
}

// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(greetingSize)); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
log.Panicf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size())
} else {
Expand All @@ -115,7 +115,7 @@ func main() {
}

func logString(_ context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(offset, uint64(byteCount))
buf, ok := m.Memory().Read(offset, byteCount)
if !ok {
log.Panicf("Memory.Read(%d, %d) out of range", offset, byteCount)
}
Expand Down
4 changes: 2 additions & 2 deletions examples/allocation/zig/greet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func run() error {
greetingPtr := uint32(ptrSize[0] >> 32)
greetingSize := uint32(ptrSize[0])
// The pointer is a linear memory offset, which is where we write the name.
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(greetingSize)); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, greetingSize); !ok {
return fmt.Errorf("Memory.Read(%d, %d) out of range of memory size %d",
greetingPtr, greetingSize, mod.Memory().Size())
} else {
Expand All @@ -109,7 +109,7 @@ func run() error {
}

func logString(_ context.Context, m api.Module, offset, byteCount uint32) {
buf, ok := m.Memory().Read(offset, uint64(byteCount))
buf, ok := m.Memory().Read(offset, byteCount)
if !ok {
log.Panicf("Memory.Read(%d, %d) out of range", offset, byteCount)
}
Expand Down
15 changes: 7 additions & 8 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() uint64 {
return uint64(len(m.Bytes))
func (m *Memory) Size() uint32 {
return uint32(len(m.Bytes))
}

func (m *Memory) Grow(deltaPages uint32) (previousPages uint32, ok bool) {
Expand Down Expand Up @@ -561,11 +561,11 @@ func (m *Memory) ReadFloat64Le(offset uint32) (float64, bool) {
return math.Float64frombits(v), ok
}

func (m *Memory) Read(offset uint32, length uint64) ([]byte, bool) {
func (m *Memory) Read(offset, length uint32) ([]byte, bool) {
if m.isOutOfRange(offset, length) {
return nil, false
}
return m.Bytes[offset : uint64(offset)+length : uint64(offset)+length], true
return m.Bytes[offset : offset+length : offset+length], true
}

func (m *Memory) WriteByte(offset uint32, value byte) bool {
Expand Down Expand Up @@ -609,24 +609,23 @@ func (m *Memory) WriteFloat64Le(offset uint32, value float64) bool {
}

func (m *Memory) Write(offset uint32, value []byte) bool {
if m.isOutOfRange(offset, uint64(len(value))) {
if m.isOutOfRange(offset, uint32(len(value))) {
return false
}
copy(m.Bytes[offset:], value)
return true
}

func (m *Memory) WriteString(offset uint32, value string) bool {
if m.isOutOfRange(offset, uint64(len(value))) {
if m.isOutOfRange(offset, uint32(len(value))) {
return false
}
copy(m.Bytes[offset:], value)
return true
}

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

Expand Down
2 changes: 1 addition & 1 deletion imports/assemblyscript/assemblyscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func readAssemblyScriptString(mem api.Memory, offset uint32) (string, bool) {
if !ok || byteCount%2 != 0 {
return "", false
}
buf, ok := mem.Read(offset, uint64(byteCount))
buf, ok := mem.Read(offset, byteCount)
if !ok {
return "", false
}
Expand Down
8 changes: 4 additions & 4 deletions imports/wasi_snapshot_preview1/args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Test_argsGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(argvBuf-1, uint64(len(expectedMemory)))
actual, ok := mod.Memory().Read(argvBuf-1, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
Expand All @@ -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 := uint32(mod.Memory().Size())
memorySize := mod.Memory().Size()
validAddress := uint32(0) // arbitrary

tests := []struct {
Expand Down Expand Up @@ -124,7 +124,7 @@ func Test_argsSizesGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(resultArgc-1, uint64(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultArgc-1, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
Expand All @@ -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 := uint32(mod.Memory().Size())
memorySize := mod.Memory().Size()
validAddress := uint32(0) // arbitrary valid address as arguments to args_sizes_get. We chose 0 here.

tests := []struct {
Expand Down
6 changes: 3 additions & 3 deletions imports/wasi_snapshot_preview1/clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func Test_clockResGet(t *testing.T) {
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.ClockResGetName, uint64(tc.clockID), uint64(resultResolution))
require.Equal(t, tc.expectedLog, "\n"+log.String())

actual, ok := mod.Memory().Read(uint32(resultResolution-1), uint64(len(tc.expectedMemory)))
actual, ok := mod.Memory().Read(uint32(resultResolution-1), uint32(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
})
Expand Down Expand Up @@ -170,7 +170,7 @@ func Test_clockTimeGet(t *testing.T) {
requireErrnoResult(t, wasip1.ErrnoSuccess, mod, wasip1.ClockTimeGetName, uint64(tc.clockID), 0 /* TODO: precision */, uint64(resultTimestamp))
require.Equal(t, tc.expectedLog, "\n"+log.String())

actual, ok := mod.Memory().Read(uint32(resultTimestamp-1), uint64(len(tc.expectedMemory)))
actual, ok := mod.Memory().Read(uint32(resultTimestamp-1), uint32(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
})
Expand Down 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 := uint32(mod.Memory().Size())
memorySize := mod.Memory().Size()

tests := []struct {
name string
Expand Down
8 changes: 4 additions & 4 deletions imports/wasi_snapshot_preview1/environ_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Test_environGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(resultEnvironBuf-1, uint64(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultEnvironBuf-1, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
Expand All @@ -44,7 +44,7 @@ func Test_environGet_Errors(t *testing.T) {
WithEnv("a", "bc").WithEnv("b", "cd"))
defer r.Close(testCtx)

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

tests := []struct {
Expand Down Expand Up @@ -128,7 +128,7 @@ func Test_environSizesGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(resultEnvironc-1, uint64(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultEnvironc-1, uint32(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
Expand All @@ -138,7 +138,7 @@ func Test_environSizesGet_Errors(t *testing.T) {
WithEnv("a", "b").WithEnv("b", "cd"))
defer r.Close(testCtx)

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

tests := []struct {
Expand Down
20 changes: 10 additions & 10 deletions imports/wasi_snapshot_preview1/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,21 +800,21 @@ func fdReadOrPread(mod api.Module, params []uint64, isPread bool) experimentalsy

func readv(mem api.Memory, iovs uint32, iovsCount uint32, reader func(buf []byte) (nread int, errno experimentalsys.Errno)) (uint32, experimentalsys.Errno) {
var nread uint32
iovsStop := uint64(iovsCount) * 8
iovsStop := iovsCount << 3 // iovsCount * 8
iovsBuf, ok := mem.Read(iovs, iovsStop)
if !ok {
return 0, experimentalsys.EFAULT
}

for iovsPos := uint64(0); iovsPos < iovsStop; iovsPos += 8 {
for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 {
offset := le.Uint32(iovsBuf[iovsPos:])
l := le.Uint32(iovsBuf[iovsPos+4:])

if l == 0 { // A zero length iovec could be ahead of another.
continue
}

b, ok := mem.Read(offset, uint64(l))
b, ok := mem.Read(offset, l)
if !ok {
return 0, experimentalsys.EFAULT
}
Expand Down Expand Up @@ -917,7 +917,7 @@ func fdReaddirFn(_ context.Context, mod api.Module, params []uint64) experimenta
// ^^ yes this can overflow to negative, which means our implementation
// doesn't support writing greater than max int64 entries.

buf, ok := mem.Read(buf, uint64(bufToWrite))
buf, ok := mem.Read(buf, bufToWrite)
if !ok {
return experimentalsys.EFAULT
}
Expand Down Expand Up @@ -1284,17 +1284,17 @@ func fdWriteOrPwrite(mod api.Module, params []uint64, isPwrite bool) experimenta

func writev(mem api.Memory, iovs uint32, iovsCount uint32, writer func(buf []byte) (n int, errno experimentalsys.Errno)) (uint32, experimentalsys.Errno) {
var nwritten uint32
iovsStop := uint64(iovsCount) * 8
iovsStop := iovsCount << 3 // iovsCount * 8
iovsBuf, ok := mem.Read(iovs, iovsStop)
if !ok {
return 0, experimentalsys.EFAULT
}

for iovsPos := uint64(0); iovsPos < iovsStop; iovsPos += 8 {
for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 {
offset := le.Uint32(iovsBuf[iovsPos:])
l := le.Uint32(iovsBuf[iovsPos+4:])

b, ok := mem.Read(offset, uint64(l))
b, ok := mem.Read(offset, l)
if !ok {
return 0, experimentalsys.EFAULT
}
Expand Down Expand Up @@ -1645,7 +1645,7 @@ func pathOpenFn(_ context.Context, mod api.Module, params []uint64) experimental
// See https://github.com/WebAssembly/wasi-libc/blob/659ff414560721b1660a19685110e484a081c3d4/libc-bottom-half/sources/at_fdcwd.c
// See https://linux.die.net/man/2/openat
func atPath(fsc *sys.FSContext, mem api.Memory, fd int32, p, pathLen uint32) (experimentalsys.FS, string, experimentalsys.Errno) {
b, ok := mem.Read(p, uint64(pathLen))
b, ok := mem.Read(p, pathLen)
if !ok {
return nil, "", experimentalsys.EFAULT
}
Expand Down Expand Up @@ -1946,12 +1946,12 @@ func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) experimen
return experimentalsys.EINVAL
}

oldPathBuf, ok := mem.Read(oldPath, uint64(oldPathLen))
oldPathBuf, ok := mem.Read(oldPath, oldPathLen)
if !ok {
return experimentalsys.EFAULT
}

newPathBuf, ok := mem.Read(newPath, uint64(newPathLen))
newPathBuf, ok := mem.Read(newPath, newPathLen)
if !ok {
return experimentalsys.EFAULT
}
Expand Down
Loading

0 comments on commit 712f937

Please sign in to comment.