]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: fix unsafe.Pointer alignment on Linux
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sat, 19 Oct 2019 08:18:34 +0000 (15:18 +0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 22 Oct 2019 01:03:09 +0000 (01:03 +0000)
Caught by go test -a -short -gcflags=all=-d=checkptr runtime

TestMincoreErrorSign intentionally uses uintptr(1) to get -EINVAL,
but it violates unsafe pointer rules 2. So use another misaligned
pointer add(new(int32), 1), but do not violate unsafe pointer rules.

TestEpollctlErrorSign passes an unsafe.Pointer of &struct{}{} to
Epollctl, which is then casted to epollevent, causes mis-alignment.
Fixing it by exporting epollevent on runtime_test package, so it can be
passed to Epollctl.

Updates #34972

Change-Id: I78ebfbeaf706fd1d372272af0bbc4e2cabca4631
Reviewed-on: https://go-review.googlesource.com/c/go/+/202157
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/runtime/export_linux_test.go
src/runtime/runtime_linux_test.go

index c73f2f33d1fda77f86600ff433e32f38aad9287e..b7c901f238ff29479ef2f4c8fa3466e62540f0c2 100644 (file)
@@ -10,6 +10,9 @@ import "unsafe"
 
 var NewOSProc0 = newosproc0
 var Mincore = mincore
+var Add = add
+
+type EpollEvent epollevent
 
 func Epollctl(epfd, op, fd int32, ev unsafe.Pointer) int32 {
        return epollctl(epfd, op, fd, (*epollevent)(ev))
index 17d6fbde467686986abcda94645aa28b6db595bb..cd59368cb2af4311ee0bca69cd64aade889350f9 100644 (file)
@@ -41,11 +41,11 @@ func TestLockOSThread(t *testing.T) {
        }
 }
 
-// Test that error values are negative. Use address 1 (a misaligned
-// pointer) to get -EINVAL.
+// Test that error values are negative.
+// Use a misaligned pointer to get -EINVAL.
 func TestMincoreErrorSign(t *testing.T) {
        var dst byte
-       v := Mincore(unsafe.Pointer(uintptr(1)), 1, &dst)
+       v := Mincore(Add(unsafe.Pointer(new(int32)), 1), 1, &dst)
 
        const EINVAL = 0x16
        if v != -EINVAL {
@@ -54,7 +54,7 @@ func TestMincoreErrorSign(t *testing.T) {
 }
 
 func TestEpollctlErrorSign(t *testing.T) {
-       v := Epollctl(-1, 1, -1, unsafe.Pointer(&struct{}{}))
+       v := Epollctl(-1, 1, -1, unsafe.Pointer(&EpollEvent{}))
 
        const EBADF = 0x09
        if v != -EBADF {