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

Changes Read to receive uint64 byte count. #2085

Merged
merged 3 commits into from
Feb 28, 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
2 changes: 1 addition & 1 deletion api/wasm.go
Original file line number Diff line number Diff line change
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, byteCount uint32) ([]byte, bool)
Read(offset uint32, byteCount uint64) ([]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, greetingSize); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(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, byteCount)
buf, ok := m.Memory().Read(offset, uint64(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, greetingSize); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(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, byteCount)
buf, ok := m.Memory().Read(offset, uint64(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, greetingSize); !ok {
if bytes, ok := mod.Memory().Read(greetingPtr, uint64(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, byteCount)
buf, ok := m.Memory().Read(offset, uint64(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 @@ -561,11 +561,11 @@ func (m *Memory) ReadFloat64Le(offset uint32) (float64, bool) {
return math.Float64frombits(v), ok
}

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

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

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

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

func (m *Memory) isOutOfRange(_offset, _length uint32) bool {
size := int64(m.Size())
offset := int64(_offset)
length := int64(_length)
func (m *Memory) isOutOfRange(_offset uint32, length uint64) 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, byteCount)
buf, ok := mem.Read(offset, uint64(byteCount))
if !ok {
return "", false
}
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 @@ -32,7 +32,7 @@ func Test_argsGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(argvBuf-1, uint32(len(expectedMemory)))
actual, ok := mod.Memory().Read(argvBuf-1, uint64(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
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, uint32(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultArgc-1, uint64(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
Expand Down
4 changes: 2 additions & 2 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), uint32(len(tc.expectedMemory)))
actual, ok := mod.Memory().Read(uint32(resultResolution-1), uint64(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), uint32(len(tc.expectedMemory)))
actual, ok := mod.Memory().Read(uint32(resultTimestamp-1), uint64(len(tc.expectedMemory)))
require.True(t, ok)
require.Equal(t, tc.expectedMemory, actual)
})
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 @@ -34,7 +34,7 @@ func Test_environGet(t *testing.T) {
<== errno=ESUCCESS
`, "\n"+log.String())

actual, ok := mod.Memory().Read(resultEnvironBuf-1, uint32(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultEnvironBuf-1, uint64(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
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, uint32(len(expectedMemory)))
actual, ok := mod.Memory().Read(resultEnvironc-1, uint64(len(expectedMemory)))
require.True(t, ok)
require.Equal(t, expectedMemory, actual)
}
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 := iovsCount << 3 // iovsCount * 8
iovsStop := uint64(iovsCount) * 8
iovsBuf, ok := mem.Read(iovs, iovsStop)
if !ok {
return 0, experimentalsys.EFAULT
}

for iovsPos := uint32(0); iovsPos < iovsStop; iovsPos += 8 {
for iovsPos := uint64(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, l)
b, ok := mem.Read(offset, uint64(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, bufToWrite)
buf, ok := mem.Read(buf, uint64(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 := iovsCount << 3 // iovsCount * 8
iovsStop := uint64(iovsCount) * 8
iovsBuf, ok := mem.Read(iovs, iovsStop)
if !ok {
return 0, experimentalsys.EFAULT
}

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

b, ok := mem.Read(offset, l)
b, ok := mem.Read(offset, uint64(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, pathLen)
b, ok := mem.Read(p, uint64(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, oldPathLen)
oldPathBuf, ok := mem.Read(oldPath, uint64(oldPathLen))
if !ok {
return experimentalsys.EFAULT
}

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