Remove some //go:linkname usages (#253)
* Stop using noescape This was just a performance improvement. Removing since we want to avoid using linkname to not hinder the Go ecosystem. * Stop using memmove * remove memmove linkname
- commit
- 8ebc748ec153c98f58511bcb274540e8b517a56b
- author
- TotallyGamerJet · · 2024-06-04 02:52 UTC
- committer
- GitHub · · 2024-06-04 02:52 UTC
- parent
- 88cc579
- signature
- GPG Unverified
B5690EEEBB952194The key is unknown
5 files · +7 -14
M func.go +1 -2
| @@ -340,8 +340,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||
| 340 | 340 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | |
| 341 | 341 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1))) | |
| 342 | 342 | case reflect.Ptr: | |
| 343 | - | // It is safe to have the address of syscall.r1 not escape because it is immediately dereferenced with .Elem() | |
| 344 | - | v = reflect.NewAt(outType, runtime_noescape(unsafe.Pointer(&syscall.a1))).Elem() | |
| 343 | + | v = reflect.NewAt(outType, unsafe.Pointer(&syscall.a1)).Elem() | |
| 345 | 344 | case reflect.Func: | |
| 346 | 345 | // wrap this C function in a nicely typed Go function | |
| 347 | 346 | v = reflect.New(outType) | |
| @@ -340,8 +340,7 @@ func RegisterFunc(fptr interface{}, cfn uintptr) { | |||||
| 340 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | 340 | // We take the address and then dereference it to trick go vet from creating a possible miss-use of unsafe.Pointer | ||
| 341 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1))) | 341 | v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1))) | ||
| 342 | case reflect.Ptr: | 342 | case reflect.Ptr: | ||
| 343 | - | // It is safe to have the address of syscall.r1 not escape because it is immediately dereferenced with .Elem() | 343 | + | v = reflect.NewAt(outType, unsafe.Pointer(&syscall.a1)).Elem() |
| 344 | - | v = reflect.NewAt(outType, runtime_noescape(unsafe.Pointer(&syscall.a1))).Elem() | |||
| 345 | case reflect.Func: | 344 | case reflect.Func: | ||
| 346 | // wrap this C function in a nicely typed Go function | 345 | // wrap this C function in a nicely typed Go function | ||
| 347 | v = reflect.New(outType) | 346 | v = reflect.New(outType) | ||
M go_runtime.go +0 -4
| @@ -11,7 +11,3 @@ import ( | |||
| 11 | 11 | ||
| 12 | 12 | //go:linkname runtime_cgocall runtime.cgocall | |
| 13 | 13 | func runtime_cgocall(fn uintptr, arg unsafe.Pointer) int32 // from runtime/sys_libc.go | |
| 14 | - | ||
| 15 | - | //go:linkname runtime_noescape runtime.noescape | |
| 16 | - | //go:noescape | |
| 17 | - | func runtime_noescape(p unsafe.Pointer) unsafe.Pointer // from runtime/stubs.go | |
| @@ -11,7 +11,3 @@ import ( | |||||
| 11 | 11 | ||||
| 12 | //go:linkname runtime_cgocall runtime.cgocall | 12 | //go:linkname runtime_cgocall runtime.cgocall | ||
| 13 | func runtime_cgocall(fn uintptr, arg unsafe.Pointer) int32 // from runtime/sys_libc.go | 13 | func runtime_cgocall(fn uintptr, arg unsafe.Pointer) int32 // from runtime/sys_libc.go | ||
| 14 | - | ||||
| 15 | - | //go:linkname runtime_noescape runtime.noescape | |||
| 16 | - | //go:noescape | |||
| 17 | - | func runtime_noescape(p unsafe.Pointer) unsafe.Pointer // from runtime/stubs.go | |||
M internal/fakecgo/gen.go +0 -3
| @@ -32,9 +32,6 @@ import ( | |||
| 32 | 32 | // setg_trampoline calls setg with the G provided | |
| 33 | 33 | func setg_trampoline(setg uintptr, G uintptr) | |
| 34 | 34 | ||
| 35 | - | //go:linkname memmove runtime.memmove | |
| 36 | - | func memmove(to, from unsafe.Pointer, n uintptr) | |
| 37 | - | ||
| 38 | 35 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | |
| 39 | 36 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | |
| 40 | 37 | ||
| @@ -32,9 +32,6 @@ import ( | |||||
| 32 | // setg_trampoline calls setg with the G provided | 32 | // setg_trampoline calls setg with the G provided | ||
| 33 | func setg_trampoline(setg uintptr, G uintptr) | 33 | func setg_trampoline(setg uintptr, G uintptr) | ||
| 34 | 34 | ||||
| 35 | - | //go:linkname memmove runtime.memmove | |||
| 36 | - | func memmove(to, from unsafe.Pointer, n uintptr) | |||
| 37 | - | ||||
| 38 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | 35 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | ||
| 39 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | 36 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | ||
| 40 | 37 | ||||
M internal/fakecgo/go_util.go +6 -2
| @@ -27,7 +27,11 @@ func x_cgo_thread_start(arg *ThreadStart) { | |||
| 27 | 27 | println("fakecgo: out of memory in thread_start") | |
| 28 | 28 | abort() | |
| 29 | 29 | } | |
| 30 | - | // *ts = *arg would cause a writebarrier so use memmove instead | |
| 31 | - | memmove(unsafe.Pointer(ts), unsafe.Pointer(arg), unsafe.Sizeof(*ts)) | |
| 30 | + | // *ts = *arg would cause a writebarrier so copy using slices | |
| 31 | + | s1 := unsafe.Slice((*uintptr)(unsafe.Pointer(ts)), unsafe.Sizeof(*ts)/8) | |
| 32 | + | s2 := unsafe.Slice((*uintptr)(unsafe.Pointer(arg)), unsafe.Sizeof(*arg)/8) | |
| 33 | + | for i := range s2 { | |
| 34 | + | s1[i] = s2[i] | |
| 35 | + | } | |
| 32 | 36 | _cgo_sys_thread_start(ts) // OS-dependent half | |
| 33 | 37 | } | |
| @@ -27,7 +27,11 @@ func x_cgo_thread_start(arg *ThreadStart) { | |||||
| 27 | println("fakecgo: out of memory in thread_start") | 27 | println("fakecgo: out of memory in thread_start") | ||
| 28 | abort() | 28 | abort() | ||
| 29 | } | 29 | } | ||
| 30 | - | // *ts = *arg would cause a writebarrier so use memmove instead | 30 | + | // *ts = *arg would cause a writebarrier so copy using slices |
| 31 | - | memmove(unsafe.Pointer(ts), unsafe.Pointer(arg), unsafe.Sizeof(*ts)) | 31 | + | s1 := unsafe.Slice((*uintptr)(unsafe.Pointer(ts)), unsafe.Sizeof(*ts)/8) |
| 32 | + | s2 := unsafe.Slice((*uintptr)(unsafe.Pointer(arg)), unsafe.Sizeof(*arg)/8) | |||
| 33 | + | for i := range s2 { | |||
| 34 | + | s1[i] = s2[i] | |||
| 35 | + | } | |||
| 32 | _cgo_sys_thread_start(ts) // OS-dependent half | 36 | _cgo_sys_thread_start(ts) // OS-dependent half | ||
| 33 | } | 37 | } | ||
M internal/fakecgo/symbols.go +0 -3
| @@ -15,9 +15,6 @@ import ( | |||
| 15 | 15 | // setg_trampoline calls setg with the G provided | |
| 16 | 16 | func setg_trampoline(setg uintptr, G uintptr) | |
| 17 | 17 | ||
| 18 | - | //go:linkname memmove runtime.memmove | |
| 19 | - | func memmove(to, from unsafe.Pointer, n uintptr) | |
| 20 | - | ||
| 21 | 18 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | |
| 22 | 19 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | |
| 23 | 20 | ||
| @@ -15,9 +15,6 @@ import ( | |||||
| 15 | // setg_trampoline calls setg with the G provided | 15 | // setg_trampoline calls setg with the G provided | ||
| 16 | func setg_trampoline(setg uintptr, G uintptr) | 16 | func setg_trampoline(setg uintptr, G uintptr) | ||
| 17 | 17 | ||||
| 18 | - | //go:linkname memmove runtime.memmove | |||
| 19 | - | func memmove(to, from unsafe.Pointer, n uintptr) | |||
| 20 | - | ||||
| 21 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | 18 | // call5 takes fn the C function and 5 arguments and calls the function with those arguments | ||
| 22 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | 19 | func call5(fn, a1, a2, a3, a4, a5 uintptr) uintptr | ||
| 23 | 20 | ||||