Follow unsafe rules. (#1449)

Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
This commit is contained in:
Nuno Cruces
2023-05-09 15:34:08 +01:00
committed by GitHub
parent daad2fdb66
commit 6503e82d3c
9 changed files with 32 additions and 43 deletions

View File

@@ -6,7 +6,6 @@ import (
"io/fs" "io/fs"
"math" "math"
"path" "path"
"reflect"
"strings" "strings"
"syscall" "syscall"
"unsafe" "unsafe"
@@ -1988,18 +1987,15 @@ func pathSymlinkFn(_ context.Context, mod api.Module, params []uint64) syscall.E
return dir.FS.Symlink( return dir.FS.Symlink(
// Do not join old path since it's only resolved when dereference the link created here. // 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. // And the dereference result depends on the opening directory's file descriptor at that point.
bufToStr(oldPathBuf, int(oldPathLen)), bufToStr(oldPathBuf),
path.Join(dir.Name, bufToStr(newPathBuf, int(newPathLen))), path.Join(dir.Name, bufToStr(newPathBuf)),
) )
} }
// bufToStr converts the given byte slice as string unsafely. // bufToStr converts the given byte slice as string unsafely.
func bufToStr(buf []byte, l int) string { func bufToStr(buf []byte) string {
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{ //nolint // TODO: use unsafe.String after flooring Go 1.20.
Data: uintptr(unsafe.Pointer(&buf[0])), return *(*string)(unsafe.Pointer(&buf))
Len: l,
Cap: l,
}))
} }
// pathUnlinkFile is the WASI function named PathUnlinkFileName which unlinks a // pathUnlinkFile is the WASI function named PathUnlinkFileName which unlinks a

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"encoding/base64" "encoding/base64"
"math/rand" "math/rand"
"reflect"
"strings" "strings"
"unsafe" "unsafe"
) )
@@ -27,12 +26,7 @@ func getRandomString() string {
var bufPtr *byte var bufPtr *byte
var bufSize int var bufSize int
getRandomStringRaw(&bufPtr, &bufSize) getRandomStringRaw(&bufPtr, &bufSize)
//nolint return unsafe.String(bufPtr, bufSize)
return *(*string)(unsafe.Pointer(&reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(bufPtr)),
Len: uintptr(bufSize),
Cap: uintptr(bufSize),
}))
} }
//export base64 //export base64

View File

@@ -21,17 +21,18 @@ import (
// //
//export require_no_diff //export require_no_diff
func require_no_diff(binaryPtr uintptr, binarySize int, watPtr uintptr, watSize int, checkMemory bool) { func require_no_diff(binaryPtr uintptr, binarySize int, watPtr uintptr, watSize int, checkMemory bool) {
wasmBin := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ // TODO: use unsafe.Slice after flooring Go 1.20.
Data: binaryPtr, var wasmBin []byte
Len: binarySize, wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
Cap: binarySize, wasmHdr.Data = binaryPtr
})) wasmHdr.Len = binarySize
wasmHdr.Cap = binarySize
wat := *(*string)(unsafe.Pointer(&reflect.SliceHeader{ // TODO: use unsafe.String after flooring Go 1.20.
Data: watPtr, var wat string
Len: watSize, watHdr := (*reflect.StringHeader)(unsafe.Pointer(&wat))
Cap: watSize, watHdr.Data = watPtr
})) watHdr.Len = watSize
failed := true failed := true
defer func() { defer func() {
@@ -48,7 +49,6 @@ func require_no_diff(binaryPtr uintptr, binarySize int, watPtr uintptr, watSize
}) })
failed = false failed = false
return
} }
// We haven't had public APIs for referencing all the imported entries from wazero.CompiledModule, // We haven't had public APIs for referencing all the imported entries from wazero.CompiledModule,

View File

@@ -14,11 +14,12 @@ import (
// //
//export validate //export validate
func validate(binaryPtr uintptr, binarySize int) { func validate(binaryPtr uintptr, binarySize int) {
wasmBin := *(*[]byte)(unsafe.Pointer(&reflect.SliceHeader{ // TODO: use unsafe.Slice after flooring Go 1.20.
Data: binaryPtr, var wasmBin []byte
Len: binarySize, wasmHdr := (*reflect.SliceHeader)(unsafe.Pointer(&wasmBin))
Cap: binarySize, wasmHdr.Data = binaryPtr
})) wasmHdr.Len = binarySize
wasmHdr.Cap = binarySize
failed := true failed := true
defer func() { defer func() {

View File

@@ -17,7 +17,6 @@ import (
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
//go:noescape
//go:linkname nanotime runtime.nanotime //go:linkname nanotime runtime.nanotime
func nanotime() int64 func nanotime() int64

View File

@@ -41,10 +41,10 @@ func allocateMemory(size uintptr, protect uintptr) (uintptr, error) {
// freeMemory releases the memory region via the "VirtualFree" function. // freeMemory releases the memory region via the "VirtualFree" function.
// See https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree // See https://docs.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-virtualfree
func freeMemory(code []byte) error { 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. size := uintptr(0) // size must be 0 because we're using MEM_RELEASE.
freetype := windows_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 fmt.Errorf("compiler: VirtualFree error: %w", ensureErr(err))
} }
return nil return nil

View File

@@ -108,14 +108,14 @@ func pollNamedPipe(ctx context.Context, pipeHandle syscall.Handle, duration *tim
// see https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-peeknamedpipe // see https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-peeknamedpipe
func peekNamedPipe(handle syscall.Handle) (bool, error) { func peekNamedPipe(handle syscall.Handle) (bool, error) {
var totalBytesAvail uint32 var totalBytesAvail uint32
totalBytesPtr := uintptr(unsafe.Pointer(&totalBytesAvail)) totalBytesPtr := unsafe.Pointer(&totalBytesAvail)
_, _, err := procPeekNamedPipe.Call( _, _, err := procPeekNamedPipe.Call(
uintptr(handle), // [in] HANDLE hNamedPipe, uintptr(handle), // [in] HANDLE hNamedPipe,
0, // [out, optional] LPVOID lpBuffer, 0, // [out, optional] LPVOID lpBuffer,
0, // [in] DWORD nBufferSize, 0, // [in] DWORD nBufferSize,
0, // [out, optional] LPDWORD lpBytesRead 0, // [out, optional] LPDWORD lpBytesRead
totalBytesPtr, // [out, optional] LPDWORD lpTotalBytesAvail, uintptr(totalBytesPtr), // [out, optional] LPDWORD lpTotalBytesAvail,
0) // [out, optional] LPDWORD lpBytesLeftThisMessage 0) // [out, optional] LPDWORD lpBytesLeftThisMessage
if err == syscall.Errno(0) { if err == syscall.Errno(0) {
return totalBytesAvail > 0, nil return totalBytesAvail > 0, nil
} }

View File

@@ -7,6 +7,5 @@ import _ "unsafe" // for go:linkname
// nanotime uses runtime.nanotime as it is available on all platforms and // nanotime uses runtime.nanotime as it is available on all platforms and
// benchmarks faster than using time.Since. // benchmarks faster than using time.Since.
// //
//go:noescape
//go:linkname nanotime runtime.nanotime //go:linkname nanotime runtime.nanotime
func nanotime() int64 func nanotime() int64