summaryrefslogtreecommitdiff
path: root/libgo/misc/cgo
diff options
context:
space:
mode:
authorIan Lance Taylor <ian@gcc.gnu.org>2019-09-12 23:22:53 +0000
committerIan Lance Taylor <ian@gcc.gnu.org>2019-09-12 23:22:53 +0000
commit656297e1fec9a127ff742df16958ee279ccacec5 (patch)
tree24347a35dacea36ce742c32c17420f3e31f17e3d /libgo/misc/cgo
parentd6ecb707cc5a58816d27908a7aa324c4b0bc67bb (diff)
libgo: update to Go1.13
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/194698 From-SVN: r275691
Diffstat (limited to 'libgo/misc/cgo')
-rw-r--r--libgo/misc/cgo/errors/errors_test.go1
-rw-r--r--libgo/misc/cgo/errors/ptr_test.go82
-rw-r--r--libgo/misc/cgo/errors/testdata/issue33061.go17
-rw-r--r--libgo/misc/cgo/testshared/shared_test.go7
-rw-r--r--libgo/misc/cgo/testshared/testdata/issue30768/issue30768lib/lib.go11
-rw-r--r--libgo/misc/cgo/testshared/testdata/issue30768/x_test.go22
6 files changed, 105 insertions, 35 deletions
diff --git a/libgo/misc/cgo/errors/errors_test.go b/libgo/misc/cgo/errors/errors_test.go
index e6bac0fff45..1bdf843451d 100644
--- a/libgo/misc/cgo/errors/errors_test.go
+++ b/libgo/misc/cgo/errors/errors_test.go
@@ -112,6 +112,7 @@ func TestReportsTypeErrors(t *testing.T) {
"issue18452.go",
"issue18889.go",
"issue28721.go",
+ "issue33061.go",
} {
check(t, file)
}
diff --git a/libgo/misc/cgo/errors/ptr_test.go b/libgo/misc/cgo/errors/ptr_test.go
index d1ef191bf54..4a46b6023bb 100644
--- a/libgo/misc/cgo/errors/ptr_test.go
+++ b/libgo/misc/cgo/errors/ptr_test.go
@@ -156,8 +156,8 @@ var ptrTests = []ptrTest{
// Storing a Go pointer into C memory should fail.
name: "barrier",
c: `#include <stdlib.h>
- char **f14a() { return malloc(sizeof(char*)); }
- void f14b(char **p) {}`,
+ char **f14a() { return malloc(sizeof(char*)); }
+ void f14b(char **p) {}`,
body: `p := C.f14a(); *p = new(C.char); C.f14b(p)`,
fail: true,
expensive: true,
@@ -167,9 +167,9 @@ var ptrTests = []ptrTest{
// large value should fail.
name: "barrierstruct",
c: `#include <stdlib.h>
- struct s15 { char *a[10]; };
- struct s15 *f15() { return malloc(sizeof(struct s15)); }
- void f15b(struct s15 *p) {}`,
+ struct s15 { char *a[10]; };
+ struct s15 *f15() { return malloc(sizeof(struct s15)); }
+ void f15b(struct s15 *p) {}`,
body: `p := C.f15(); p.a = [10]*C.char{new(C.char)}; C.f15b(p)`,
fail: true,
expensive: true,
@@ -179,9 +179,9 @@ var ptrTests = []ptrTest{
// copy should fail.
name: "barrierslice",
c: `#include <stdlib.h>
- struct s16 { char *a[10]; };
- struct s16 *f16() { return malloc(sizeof(struct s16)); }
- void f16b(struct s16 *p) {}`,
+ struct s16 { char *a[10]; };
+ struct s16 *f16() { return malloc(sizeof(struct s16)); }
+ void f16b(struct s16 *p) {}`,
body: `p := C.f16(); copy(p.a[:], []*C.char{new(C.char)}); C.f16b(p)`,
fail: true,
expensive: true,
@@ -191,9 +191,9 @@ var ptrTests = []ptrTest{
// different code path.
name: "barriergcprogarray",
c: `#include <stdlib.h>
- struct s17 { char *a[32769]; };
- struct s17 *f17() { return malloc(sizeof(struct s17)); }
- void f17b(struct s17 *p) {}`,
+ struct s17 { char *a[32769]; };
+ struct s17 *f17() { return malloc(sizeof(struct s17)); }
+ void f17b(struct s17 *p) {}`,
body: `p := C.f17(); p.a = [32769]*C.char{new(C.char)}; C.f17b(p)`,
fail: true,
expensive: true,
@@ -202,10 +202,10 @@ var ptrTests = []ptrTest{
// Similar case, with a source on the heap.
name: "barriergcprogarrayheap",
c: `#include <stdlib.h>
- struct s18 { char *a[32769]; };
- struct s18 *f18() { return malloc(sizeof(struct s18)); }
- void f18b(struct s18 *p) {}
- void f18c(void *p) {}`,
+ struct s18 { char *a[32769]; };
+ struct s18 *f18() { return malloc(sizeof(struct s18)); }
+ void f18b(struct s18 *p) {}
+ void f18c(void *p) {}`,
imports: []string{"unsafe"},
body: `p := C.f18(); n := &[32769]*C.char{new(C.char)}; p.a = *n; C.f18b(p); n[0] = nil; C.f18c(unsafe.Pointer(n))`,
fail: true,
@@ -215,10 +215,10 @@ var ptrTests = []ptrTest{
// A GC program with a struct.
name: "barriergcprogstruct",
c: `#include <stdlib.h>
- struct s19a { char *a[32769]; };
- struct s19b { struct s19a f; };
- struct s19b *f19() { return malloc(sizeof(struct s19b)); }
- void f19b(struct s19b *p) {}`,
+ struct s19a { char *a[32769]; };
+ struct s19b { struct s19a f; };
+ struct s19b *f19() { return malloc(sizeof(struct s19b)); }
+ void f19b(struct s19b *p) {}`,
body: `p := C.f19(); p.f = C.struct_s19a{[32769]*C.char{new(C.char)}}; C.f19b(p)`,
fail: true,
expensive: true,
@@ -227,11 +227,11 @@ var ptrTests = []ptrTest{
// Similar case, with a source on the heap.
name: "barriergcprogstructheap",
c: `#include <stdlib.h>
- struct s20a { char *a[32769]; };
- struct s20b { struct s20a f; };
- struct s20b *f20() { return malloc(sizeof(struct s20b)); }
- void f20b(struct s20b *p) {}
- void f20c(void *p) {}`,
+ struct s20a { char *a[32769]; };
+ struct s20b { struct s20a f; };
+ struct s20b *f20() { return malloc(sizeof(struct s20b)); }
+ void f20b(struct s20b *p) {}
+ void f20c(void *p) {}`,
imports: []string{"unsafe"},
body: `p := C.f20(); n := &C.struct_s20a{[32769]*C.char{new(C.char)}}; p.f = *n; C.f20b(p); n.a[0] = nil; C.f20c(unsafe.Pointer(n))`,
fail: true,
@@ -242,7 +242,7 @@ var ptrTests = []ptrTest{
name: "export1",
c: `extern unsigned char *GoFn21();`,
support: `//export GoFn21
- func GoFn21() *byte { return new(byte) }`,
+ func GoFn21() *byte { return new(byte) }`,
body: `C.GoFn21()`,
fail: true,
},
@@ -250,17 +250,17 @@ var ptrTests = []ptrTest{
// Returning a C pointer is fine.
name: "exportok",
c: `#include <stdlib.h>
- extern unsigned char *GoFn22();`,
+ extern unsigned char *GoFn22();`,
support: `//export GoFn22
- func GoFn22() *byte { return (*byte)(C.malloc(1)) }`,
+ func GoFn22() *byte { return (*byte)(C.malloc(1)) }`,
body: `C.GoFn22()`,
},
{
// Passing a Go string is fine.
name: "passstring",
c: `#include <stddef.h>
- typedef struct { const char *p; ptrdiff_t n; } gostring23;
- gostring23 f23(gostring23 s) { return s; }`,
+ typedef struct { const char *p; ptrdiff_t n; } gostring23;
+ gostring23 f23(gostring23 s) { return s; }`,
imports: []string{"unsafe"},
body: `s := "a"; r := C.f23(*(*C.gostring23)(unsafe.Pointer(&s))); if *(*string)(unsafe.Pointer(&r)) != s { panic(r) }`,
},
@@ -279,12 +279,12 @@ var ptrTests = []ptrTest{
c: `extern void f25();`,
imports: []string{"strings"},
support: `//export GoStr25
- func GoStr25() string { return strings.Repeat("a", 2) }`,
+ func GoStr25() string { return strings.Repeat("a", 2) }`,
body: `C.f25()`,
c1: `#include <stddef.h>
- typedef struct { const char *p; ptrdiff_t n; } gostring25;
- extern gostring25 GoStr25();
- void f25() { GoStr25(); }`,
+ typedef struct { const char *p; ptrdiff_t n; } gostring25;
+ extern gostring25 GoStr25();
+ void f25() { GoStr25(); }`,
fail: true,
},
{
@@ -295,7 +295,7 @@ var ptrTests = []ptrTest{
// that is, we are testing something that is not unsafe.
name: "ptrdata1",
c: `#include <stdlib.h>
- void f26(void* p) {}`,
+ void f26(void* p) {}`,
imports: []string{"unsafe"},
support: `type S26 struct { p *int; a [8*8]byte; u uintptr }`,
body: `i := 0; p := &S26{u:uintptr(unsafe.Pointer(&i))}; q := (*S26)(C.malloc(C.size_t(unsafe.Sizeof(*p)))); *q = *p; C.f26(unsafe.Pointer(q))`,
@@ -305,7 +305,7 @@ var ptrTests = []ptrTest{
// Like ptrdata1, but with a type that uses a GC program.
name: "ptrdata2",
c: `#include <stdlib.h>
- void f27(void* p) {}`,
+ void f27(void* p) {}`,
imports: []string{"unsafe"},
support: `type S27 struct { p *int; a [32769*8]byte; q *int; u uintptr }`,
body: `i := 0; p := S27{u:uintptr(unsafe.Pointer(&i))}; q := (*S27)(C.malloc(C.size_t(unsafe.Sizeof(p)))); *q = p; C.f27(unsafe.Pointer(q))`,
@@ -423,6 +423,15 @@ var ptrTests = []ptrTest{
body: `t := reflect.StructOf([]reflect.StructField{{Name: "MyInt38", Type: reflect.TypeOf(MyInt38(0)), Anonymous: true}}); v := reflect.New(t).Elem(); v.Interface().(Getter38).Get()`,
fail: false,
},
+ {
+ // Test that a converted address of a struct field results
+ // in a check for just that field and not the whole struct.
+ name: "structfieldcast",
+ c: `struct S40i { int i; int* p; }; void f40(struct S40i* p) {}`,
+ support: `type S40 struct { p *int; a C.struct_S40i }`,
+ body: `s := &S40{p: new(int)}; C.f40((*C.struct_S40i)(&s.a))`,
+ fail: false,
+ },
}
func TestPointerChecks(t *testing.T) {
@@ -466,6 +475,9 @@ func buildPtrTests(t *testing.T) (dir, exe string) {
if err := os.MkdirAll(src, 0777); err != nil {
t.Fatal(err)
}
+ if err := ioutil.WriteFile(filepath.Join(src, "go.mod"), []byte("module ptrtest"), 0666); err != nil {
+ t.Fatal(err)
+ }
// Prepare two cgo inputs: one for standard cgo and one for //export cgo.
// (The latter cannot have C definitions, only declarations.)
diff --git a/libgo/misc/cgo/errors/testdata/issue33061.go b/libgo/misc/cgo/errors/testdata/issue33061.go
new file mode 100644
index 00000000000..77d5f7a7c91
--- /dev/null
+++ b/libgo/misc/cgo/errors/testdata/issue33061.go
@@ -0,0 +1,17 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// cgo shouldn't crash if there is an extra argument with a C reference.
+
+package main
+
+// void F(void* p) {};
+import "C"
+
+import "unsafe"
+
+func F() {
+ var i int
+ C.F(unsafe.Pointer(&i), C.int(0)) // ERROR HERE
+}
diff --git a/libgo/misc/cgo/testshared/shared_test.go b/libgo/misc/cgo/testshared/shared_test.go
index ac1a1c7f1a8..9d16338c0f6 100644
--- a/libgo/misc/cgo/testshared/shared_test.go
+++ b/libgo/misc/cgo/testshared/shared_test.go
@@ -941,3 +941,10 @@ func TestTestInstalledShared(t *testing.T) {
func TestGeneratedMethod(t *testing.T) {
goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065")
}
+
+// Test use of shared library struct with generated hash function.
+// Issue 30768.
+func TestGeneratedHash(t *testing.T) {
+ goCmd(nil, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib")
+ goCmd(nil, "test", "-linkshared", "./issue30768")
+}
diff --git a/libgo/misc/cgo/testshared/testdata/issue30768/issue30768lib/lib.go b/libgo/misc/cgo/testshared/testdata/issue30768/issue30768lib/lib.go
new file mode 100644
index 00000000000..9e45ebe6835
--- /dev/null
+++ b/libgo/misc/cgo/testshared/testdata/issue30768/issue30768lib/lib.go
@@ -0,0 +1,11 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package issue30768lib
+
+// S is a struct that requires a generated hash function.
+type S struct {
+ A string
+ B int
+}
diff --git a/libgo/misc/cgo/testshared/testdata/issue30768/x_test.go b/libgo/misc/cgo/testshared/testdata/issue30768/x_test.go
new file mode 100644
index 00000000000..1bbd139d3eb
--- /dev/null
+++ b/libgo/misc/cgo/testshared/testdata/issue30768/x_test.go
@@ -0,0 +1,22 @@
+// Copyright 2019 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package issue30768_test
+
+import (
+ "testing"
+
+ "testshared/issue30768/issue30768lib"
+)
+
+type s struct {
+ s issue30768lib.S
+}
+
+func Test30768(t *testing.T) {
+ // Calling t.Log will convert S to an empty interface,
+ // which will force a reference to the generated hash function,
+ // defined in the shared library.
+ t.Log(s{})
+}