Skip to content

Commit 73ac82f

Browse files
committed
runtime, syscall: use pointer types on wasmimport functions
Now that we support pointer types on wasmimport functions, use them, instead of unsafe.Pointer. This removes unsafe conversions. There is still one unsafe.Pointer argument left. It is actually a *Stat_t, which is an exported type with an int field, which is not allowed as a wasmimport field type. We probably cannot change it at this point. Updates #66984. Change-Id: I445c70b356c3877a5604bee67d19d99a538c682e Reviewed-on: https://go-review.googlesource.com/c/go/+/627059 Reviewed-by: Johan Brandhorst-Satzkorn <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent 6051da4 commit 73ac82f

File tree

7 files changed

+89
-84
lines changed

7 files changed

+89
-84
lines changed

src/go/build/deps_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ var depsRules = `
6161
internal/trace/traceviewer/format,
6262
log/internal,
6363
math/bits,
64+
structs,
6465
unicode,
6566
unicode/utf8,
6667
unicode/utf16;
@@ -78,7 +79,8 @@ var depsRules = `
7879
internal/goexperiment,
7980
internal/goos,
8081
internal/profilerecord,
81-
math/bits
82+
math/bits,
83+
structs
8284
< internal/bytealg
8385
< internal/stringslite
8486
< internal/itoa

src/internal/syscall/unix/at_wasip1.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ func Readlinkat(dirfd int, path string, buf []byte) (int, error) {
2727
var nwritten size
2828
errno := path_readlink(
2929
int32(dirfd),
30-
unsafe.Pointer(unsafe.StringData(path)),
30+
unsafe.StringData(path),
3131
size(len(path)),
32-
unsafe.Pointer(&buf[0]),
32+
&buf[0],
3333
size(len(buf)),
34-
unsafe.Pointer(&nwritten))
34+
&nwritten)
3535
return int(nwritten), errnoErr(errno)
3636

3737
}
@@ -42,22 +42,22 @@ type (
4242

4343
//go:wasmimport wasi_snapshot_preview1 path_readlink
4444
//go:noescape
45-
func path_readlink(fd int32, path unsafe.Pointer, pathLen size, buf unsafe.Pointer, bufLen size, nwritten unsafe.Pointer) syscall.Errno
45+
func path_readlink(fd int32, path *byte, pathLen size, buf *byte, bufLen size, nwritten *size) syscall.Errno
4646

4747
func Mkdirat(dirfd int, path string, mode uint32) error {
4848
if path == "" {
4949
return syscall.EINVAL
5050
}
5151
return errnoErr(path_create_directory(
5252
int32(dirfd),
53-
unsafe.Pointer(unsafe.StringData(path)),
53+
unsafe.StringData(path),
5454
size(len(path)),
5555
))
5656
}
5757

5858
//go:wasmimport wasi_snapshot_preview1 path_create_directory
5959
//go:noescape
60-
func path_create_directory(fd int32, path unsafe.Pointer, pathLen size) syscall.Errno
60+
func path_create_directory(fd int32, path *byte, pathLen size) syscall.Errno
6161

6262
func errnoErr(errno syscall.Errno) error {
6363
if errno == 0 {

src/runtime/netpoll_wasip1.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ func netpoll(delay int64) (gList, int32) {
209209

210210
retry:
211211
var nevents size
212-
errno := poll_oneoff(unsafe.Pointer(&pollsubs[0]), unsafe.Pointer(&evts[0]), uint32(len(pollsubs)), unsafe.Pointer(&nevents))
212+
errno := poll_oneoff(&pollsubs[0], &evts[0], uint32(len(pollsubs)), &nevents)
213213
if errno != 0 {
214214
if errno != _EINTR {
215215
println("errno=", errno, " len(pollsubs)=", len(pollsubs))

src/runtime/os_wasip1.go

+26-18
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
package runtime
88

9-
import "unsafe"
9+
import (
10+
"structs"
11+
"unsafe"
12+
)
1013

1114
// GOARCH=wasm currently has 64 bits pointers, but the WebAssembly host expects
1215
// pointers to be 32 bits so we use this type alias to represent pointers in
@@ -48,31 +51,31 @@ func exit(code int32)
4851

4952
//go:wasmimport wasi_snapshot_preview1 args_get
5053
//go:noescape
51-
func args_get(argv, argvBuf unsafe.Pointer) errno
54+
func args_get(argv *uintptr32, argvBuf *byte) errno
5255

5356
//go:wasmimport wasi_snapshot_preview1 args_sizes_get
5457
//go:noescape
55-
func args_sizes_get(argc, argvBufLen unsafe.Pointer) errno
58+
func args_sizes_get(argc, argvBufLen *size) errno
5659

5760
//go:wasmimport wasi_snapshot_preview1 clock_time_get
5861
//go:noescape
59-
func clock_time_get(clock_id clockid, precision timestamp, time unsafe.Pointer) errno
62+
func clock_time_get(clock_id clockid, precision timestamp, time *timestamp) errno
6063

6164
//go:wasmimport wasi_snapshot_preview1 environ_get
6265
//go:noescape
63-
func environ_get(environ, environBuf unsafe.Pointer) errno
66+
func environ_get(environ *uintptr32, environBuf *byte) errno
6467

6568
//go:wasmimport wasi_snapshot_preview1 environ_sizes_get
6669
//go:noescape
67-
func environ_sizes_get(environCount, environBufLen unsafe.Pointer) errno
70+
func environ_sizes_get(environCount, environBufLen *size) errno
6871

6972
//go:wasmimport wasi_snapshot_preview1 fd_write
7073
//go:noescape
71-
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten unsafe.Pointer) errno
74+
func fd_write(fd int32, iovs unsafe.Pointer, iovsLen size, nwritten *size) errno
7275

7376
//go:wasmimport wasi_snapshot_preview1 random_get
7477
//go:noescape
75-
func random_get(buf unsafe.Pointer, bufLen size) errno
78+
func random_get(buf *byte, bufLen size) errno
7679

7780
type eventtype = uint8
7881

@@ -99,13 +102,15 @@ type userdata = uint64
99102
// struct size because errno is declared as a 32 bits type, so we declare the
100103
// error field as a plain uint16.
101104
type event struct {
105+
_ structs.HostLayout
102106
userdata userdata
103107
error uint16
104108
typ eventtype
105109
fdReadwrite eventFdReadwrite
106110
}
107111

108112
type eventFdReadwrite struct {
113+
_ structs.HostLayout
109114
nbytes filesize
110115
flags eventrwflags
111116
}
@@ -117,17 +122,20 @@ const (
117122
)
118123

119124
type subscriptionClock struct {
125+
_ structs.HostLayout
120126
id clockid
121127
timeout timestamp
122128
precision timestamp
123129
flags subclockflags
124130
}
125131

126132
type subscriptionFdReadwrite struct {
133+
_ structs.HostLayout
127134
fd int32
128135
}
129136

130137
type subscription struct {
138+
_ structs.HostLayout
131139
userdata userdata
132140
u subscriptionUnion
133141
}
@@ -148,15 +156,15 @@ func (u *subscriptionUnion) subscriptionFdReadwrite() *subscriptionFdReadwrite {
148156

149157
//go:wasmimport wasi_snapshot_preview1 poll_oneoff
150158
//go:noescape
151-
func poll_oneoff(in, out unsafe.Pointer, nsubscriptions size, nevents unsafe.Pointer) errno
159+
func poll_oneoff(in *subscription, out *event, nsubscriptions size, nevents *size) errno
152160

153161
func write1(fd uintptr, p unsafe.Pointer, n int32) int32 {
154162
iov := iovec{
155163
buf: uintptr32(uintptr(p)),
156164
bufLen: size(n),
157165
}
158166
var nwritten size
159-
if fd_write(int32(fd), unsafe.Pointer(&iov), 1, unsafe.Pointer(&nwritten)) != 0 {
167+
if fd_write(int32(fd), unsafe.Pointer(&iov), 1, &nwritten) != 0 {
160168
throw("fd_write failed")
161169
}
162170
return int32(nwritten)
@@ -175,13 +183,13 @@ func usleep(usec uint32) {
175183
subscription.timeout = timestamp(usec) * 1e3
176184
subscription.precision = 1e3
177185

178-
if poll_oneoff(unsafe.Pointer(&in), unsafe.Pointer(&out), 1, unsafe.Pointer(&nevents)) != 0 {
186+
if poll_oneoff(&in, &out, 1, &nevents) != 0 {
179187
throw("wasi_snapshot_preview1.poll_oneoff")
180188
}
181189
}
182190

183191
func readRandom(r []byte) int {
184-
if random_get(unsafe.Pointer(&r[0]), size(len(r))) != 0 {
192+
if random_get(&r[0], size(len(r))) != 0 {
185193
return 0
186194
}
187195
return len(r)
@@ -191,15 +199,15 @@ func goenvs() {
191199
// arguments
192200
var argc size
193201
var argvBufLen size
194-
if args_sizes_get(unsafe.Pointer(&argc), unsafe.Pointer(&argvBufLen)) != 0 {
202+
if args_sizes_get(&argc, &argvBufLen) != 0 {
195203
throw("args_sizes_get failed")
196204
}
197205

198206
argslice = make([]string, argc)
199207
if argc > 0 {
200208
argv := make([]uintptr32, argc)
201209
argvBuf := make([]byte, argvBufLen)
202-
if args_get(unsafe.Pointer(&argv[0]), unsafe.Pointer(&argvBuf[0])) != 0 {
210+
if args_get(&argv[0], &argvBuf[0]) != 0 {
203211
throw("args_get failed")
204212
}
205213

@@ -216,15 +224,15 @@ func goenvs() {
216224
// environment
217225
var environCount size
218226
var environBufLen size
219-
if environ_sizes_get(unsafe.Pointer(&environCount), unsafe.Pointer(&environBufLen)) != 0 {
227+
if environ_sizes_get(&environCount, &environBufLen) != 0 {
220228
throw("environ_sizes_get failed")
221229
}
222230

223231
envs = make([]string, environCount)
224232
if environCount > 0 {
225233
environ := make([]uintptr32, environCount)
226234
environBuf := make([]byte, environBufLen)
227-
if environ_get(unsafe.Pointer(&environ[0]), unsafe.Pointer(&environBuf[0])) != 0 {
235+
if environ_get(&environ[0], &environBuf[0]) != 0 {
228236
throw("environ_get failed")
229237
}
230238

@@ -245,15 +253,15 @@ func walltime() (sec int64, nsec int32) {
245253

246254
func walltime1() (sec int64, nsec int32) {
247255
var time timestamp
248-
if clock_time_get(clockRealtime, 0, unsafe.Pointer(&time)) != 0 {
256+
if clock_time_get(clockRealtime, 0, &time) != 0 {
249257
throw("clock_time_get failed")
250258
}
251259
return int64(time / 1000000000), int32(time % 1000000000)
252260
}
253261

254262
func nanotime1() int64 {
255263
var time timestamp
256-
if clock_time_get(clockMonotonic, 0, unsafe.Pointer(&time)) != 0 {
264+
if clock_time_get(clockMonotonic, 0, &time) != 0 {
257265
throw("clock_time_get failed")
258266
}
259267
return int64(time)

0 commit comments

Comments
 (0)