Follow unsafe rules. (#1449)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
"io/fs"
|
||||
"math"
|
||||
"path"
|
||||
"reflect"
|
||||
"strings"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
@@ -1988,18 +1987,15 @@ func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) syscall.E
|
||||
return dir.FS.Symlink(
|
||||
// Do not join old path since it's only resolved when dereference the link created here.
|
||||
// And the dereference result depends on the opening directory's file descriptor at that point.
|
||||
bufToStr(oldPathBuf, int(oldPathLen)),
|
||||
path.Join(dir.Name, bufToStr(newPathBuf, int(newPathLen))),
|
||||
bufToStr(oldPathBuf),
|
||||
path.Join(dir.Name, bufToStr(newPathBuf)),
|
||||
)
|
||||
}
|
||||
|
||||
// bufToStr converts the given byte slice as string unsafely.
|
||||
func bufToStr(buf []byte, l int) string {
|
||||
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{ //nolint
|
||||
Data: uintptr(unsafe.Pointer(&buf[0])),
|
||||
Len: l,
|
||||
Cap: l,
|
||||
}))
|
||||
func bufToStr(buf []byte) string {
|
||||
// TODO: use unsafe.String after flooring Go 1.20.
|
||||
return *(*string)(unsafe.Pointer(&buf))
|
||||
}
|
||||
|
||||
// pathUnlinkFile is the WASI function named PathUnlinkFileName which unlinks a
|
||||
|
||||
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"encoding/base64"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
@@ -27,12 +26,7 @@ func getRandomString() string {
|
||||
var bufPtr *byte
|
||||
var bufSize int
|
||||
getRandomStringRaw(&bufPtr, &bufSize)
|
||||
//nolint
|
||||
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: uintptr(unsafe.Pointer(bufPtr)),
|
||||
Len: uintptr(bufSize),
|
||||
Cap: uintptr(bufSize),
|
||||
}))
|
||||
return unsafe.String(bufPtr, bufSize)
|
||||
}
|
||||
|
||||
//export base64
|
||||
|
||||
BIN
internal/integration_test/bench/testdata/case.wasm
vendored
BIN
internal/integration_test/bench/testdata/case.wasm
vendored
Binary file not shown.
@@ -21,17 +21,18 @@ import (
|
||||
//
|
||||
//export require_no_diff
|
||||
func require_no_diff(binaryPtr uintptr, binarySize int, watPtr uintptr, watSize int, checkMemory bool) {
|
||||
wasmBin := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: binaryPtr,
|
||||
Len: binarySize,
|
||||
Cap: binarySize,
|
||||
}))
|
||||
// TODO: use unsafe.Slice after flooring Go 1.20.
|
||||
var wasmBin []byte
|
||||
wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
|
||||
wasmHdr.Data = binaryPtr
|
||||
wasmHdr.Len = binarySize
|
||||
wasmHdr.Cap = binarySize
|
||||
|
||||
wat := *(*string)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: watPtr,
|
||||
Len: watSize,
|
||||
Cap: watSize,
|
||||
}))
|
||||
// TODO: use unsafe.String after flooring Go 1.20.
|
||||
var wat string
|
||||
watHdr := (*reflect.StringHeader)(unsafe.Pointer(&wat))
|
||||
watHdr.Data = watPtr
|
||||
watHdr.Len = watSize
|
||||
|
||||
failed := true
|
||||
defer func() {
|
||||
@@ -48,7 +49,6 @@ func require_no_diff(binaryPtr uintptr, binarySize int, watPtr uintptr, watSize
|
||||
})
|
||||
|
||||
failed = false
|
||||
return
|
||||
}
|
||||
|
||||
// We haven't had public APIs for referencing all the imported entries from wazero.CompiledModule,
|
||||
|
||||
@@ -14,11 +14,12 @@ import (
|
||||
//
|
||||
//export validate
|
||||
func validate(binaryPtr uintptr, binarySize int) {
|
||||
wasmBin := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{
|
||||
Data: binaryPtr,
|
||||
Len: binarySize,
|
||||
Cap: binarySize,
|
||||
}))
|
||||
// TODO: use unsafe.Slice after flooring Go 1.20.
|
||||
var wasmBin []byte
|
||||
wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
|
||||
wasmHdr.Data = binaryPtr
|
||||
wasmHdr.Len = binarySize
|
||||
wasmHdr.Cap = binarySize
|
||||
|
||||
failed := true
|
||||
defer func() {
|
||||
|
||||
@@ -17,7 +17,6 @@ import (
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
//go:noescape
|
||||
//go:linkname nanotime runtime.nanotime
|
||||
func nanotime() int64
|
||||
|
||||
|
||||
@@ -41,10 +41,10 @@ func allocateMemory(size uintptr, protect uintptr) (uintptr, error) {
|
||||
// freeMemory releases the memory region via the "VirtualFree" function.
|
||||
// See https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree
|
||||
func freeMemory(code []byte) error {
|
||||
address := uintptr(unsafe.Pointer(&code[0]))
|
||||
address := unsafe.Pointer(&code[0])
|
||||
size := uintptr(0) // size must be 0 because we're using MEM_RELEASE.
|
||||
freetype := windows_MEM_RELEASE
|
||||
if r, _, err := procVirtualFree.Call(address, size, freetype); r == 0 {
|
||||
if r, _, err := procVirtualFree.Call(uintptr(address), size, freetype); r == 0 {
|
||||
return fmt.Errorf("compiler: VirtualFree error: %w", ensureErr(err))
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -108,13 +108,13 @@ func pollNamedPipe(ctx context.Context, pipeHandle syscall.Handle, duration *tim
|
||||
// see https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-peeknamedpipe
|
||||
func peekNamedPipe(handle syscall.Handle) (bool, error) {
|
||||
var totalBytesAvail uint32
|
||||
totalBytesPtr := uintptr(unsafe.Pointer(&totalBytesAvail))
|
||||
totalBytesPtr := unsafe.Pointer(&totalBytesAvail)
|
||||
_, _, err := procPeekNamedPipe.Call(
|
||||
uintptr(handle), // [in] HANDLE hNamedPipe,
|
||||
0, // [out, optional] LPVOID lpBuffer,
|
||||
0, // [in] DWORD nBufferSize,
|
||||
0, // [out, optional] LPDWORD lpBytesRead
|
||||
totalBytesPtr, // [out, optional] LPDWORD lpTotalBytesAvail,
|
||||
uintptr(totalBytesPtr), // [out, optional] LPDWORD lpTotalBytesAvail,
|
||||
0) // [out, optional] LPDWORD lpBytesLeftThisMessage
|
||||
if err == syscall.Errno(0) {
|
||||
return totalBytesAvail > 0, nil
|
||||
|
||||
@@ -7,6 +7,5 @@ import _ "unsafe" // for go:linkname
|
||||
// nanotime uses runtime.nanotime as it is available on all platforms and
|
||||
// benchmarks faster than using time.Since.
|
||||
//
|
||||
//go:noescape
|
||||
//go:linkname nanotime runtime.nanotime
|
||||
func nanotime() int64
|
||||
|
||||
Reference in New Issue
Block a user