Commit 0d4de70c authored by Bryan C. Mills's avatar Bryan C. Mills

misc/cgo/errors: align code snippets in ptr_test.go

Change-Id: Ic3e2819617375df653116d21d7361a46085250d5
Reviewed-on: https://go-review.googlesource.com/c/go/+/183986Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
parent e301e165
...@@ -156,8 +156,8 @@ var ptrTests = []ptrTest{ ...@@ -156,8 +156,8 @@ var ptrTests = []ptrTest{
// Storing a Go pointer into C memory should fail. // Storing a Go pointer into C memory should fail.
name: "barrier", name: "barrier",
c: `#include <stdlib.h> c: `#include <stdlib.h>
char **f14a() { return malloc(sizeof(char*)); } char **f14a() { return malloc(sizeof(char*)); }
void f14b(char **p) {}`, void f14b(char **p) {}`,
body: `p := C.f14a(); *p = new(C.char); C.f14b(p)`, body: `p := C.f14a(); *p = new(C.char); C.f14b(p)`,
fail: true, fail: true,
expensive: true, expensive: true,
...@@ -167,9 +167,9 @@ var ptrTests = []ptrTest{ ...@@ -167,9 +167,9 @@ var ptrTests = []ptrTest{
// large value should fail. // large value should fail.
name: "barrierstruct", name: "barrierstruct",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s15 { char *a[10]; }; struct s15 { char *a[10]; };
struct s15 *f15() { return malloc(sizeof(struct s15)); } struct s15 *f15() { return malloc(sizeof(struct s15)); }
void f15b(struct s15 *p) {}`, void f15b(struct s15 *p) {}`,
body: `p := C.f15(); p.a = [10]*C.char{new(C.char)}; C.f15b(p)`, body: `p := C.f15(); p.a = [10]*C.char{new(C.char)}; C.f15b(p)`,
fail: true, fail: true,
expensive: true, expensive: true,
...@@ -179,9 +179,9 @@ var ptrTests = []ptrTest{ ...@@ -179,9 +179,9 @@ var ptrTests = []ptrTest{
// copy should fail. // copy should fail.
name: "barrierslice", name: "barrierslice",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s16 { char *a[10]; }; struct s16 { char *a[10]; };
struct s16 *f16() { return malloc(sizeof(struct s16)); } struct s16 *f16() { return malloc(sizeof(struct s16)); }
void f16b(struct s16 *p) {}`, void f16b(struct s16 *p) {}`,
body: `p := C.f16(); copy(p.a[:], []*C.char{new(C.char)}); C.f16b(p)`, body: `p := C.f16(); copy(p.a[:], []*C.char{new(C.char)}); C.f16b(p)`,
fail: true, fail: true,
expensive: true, expensive: true,
...@@ -191,9 +191,9 @@ var ptrTests = []ptrTest{ ...@@ -191,9 +191,9 @@ var ptrTests = []ptrTest{
// different code path. // different code path.
name: "barriergcprogarray", name: "barriergcprogarray",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s17 { char *a[32769]; }; struct s17 { char *a[32769]; };
struct s17 *f17() { return malloc(sizeof(struct s17)); } struct s17 *f17() { return malloc(sizeof(struct s17)); }
void f17b(struct s17 *p) {}`, void f17b(struct s17 *p) {}`,
body: `p := C.f17(); p.a = [32769]*C.char{new(C.char)}; C.f17b(p)`, body: `p := C.f17(); p.a = [32769]*C.char{new(C.char)}; C.f17b(p)`,
fail: true, fail: true,
expensive: true, expensive: true,
...@@ -202,10 +202,10 @@ var ptrTests = []ptrTest{ ...@@ -202,10 +202,10 @@ var ptrTests = []ptrTest{
// Similar case, with a source on the heap. // Similar case, with a source on the heap.
name: "barriergcprogarrayheap", name: "barriergcprogarrayheap",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s18 { char *a[32769]; }; struct s18 { char *a[32769]; };
struct s18 *f18() { return malloc(sizeof(struct s18)); } struct s18 *f18() { return malloc(sizeof(struct s18)); }
void f18b(struct s18 *p) {} void f18b(struct s18 *p) {}
void f18c(void *p) {}`, void f18c(void *p) {}`,
imports: []string{"unsafe"}, 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))`, 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, fail: true,
...@@ -215,10 +215,10 @@ var ptrTests = []ptrTest{ ...@@ -215,10 +215,10 @@ var ptrTests = []ptrTest{
// A GC program with a struct. // A GC program with a struct.
name: "barriergcprogstruct", name: "barriergcprogstruct",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s19a { char *a[32769]; }; struct s19a { char *a[32769]; };
struct s19b { struct s19a f; }; struct s19b { struct s19a f; };
struct s19b *f19() { return malloc(sizeof(struct s19b)); } struct s19b *f19() { return malloc(sizeof(struct s19b)); }
void f19b(struct s19b *p) {}`, void f19b(struct s19b *p) {}`,
body: `p := C.f19(); p.f = C.struct_s19a{[32769]*C.char{new(C.char)}}; C.f19b(p)`, body: `p := C.f19(); p.f = C.struct_s19a{[32769]*C.char{new(C.char)}}; C.f19b(p)`,
fail: true, fail: true,
expensive: true, expensive: true,
...@@ -227,11 +227,11 @@ var ptrTests = []ptrTest{ ...@@ -227,11 +227,11 @@ var ptrTests = []ptrTest{
// Similar case, with a source on the heap. // Similar case, with a source on the heap.
name: "barriergcprogstructheap", name: "barriergcprogstructheap",
c: `#include <stdlib.h> c: `#include <stdlib.h>
struct s20a { char *a[32769]; }; struct s20a { char *a[32769]; };
struct s20b { struct s20a f; }; struct s20b { struct s20a f; };
struct s20b *f20() { return malloc(sizeof(struct s20b)); } struct s20b *f20() { return malloc(sizeof(struct s20b)); }
void f20b(struct s20b *p) {} void f20b(struct s20b *p) {}
void f20c(void *p) {}`, void f20c(void *p) {}`,
imports: []string{"unsafe"}, 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))`, 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, fail: true,
...@@ -242,7 +242,7 @@ var ptrTests = []ptrTest{ ...@@ -242,7 +242,7 @@ var ptrTests = []ptrTest{
name: "export1", name: "export1",
c: `extern unsigned char *GoFn21();`, c: `extern unsigned char *GoFn21();`,
support: `//export GoFn21 support: `//export GoFn21
func GoFn21() *byte { return new(byte) }`, func GoFn21() *byte { return new(byte) }`,
body: `C.GoFn21()`, body: `C.GoFn21()`,
fail: true, fail: true,
}, },
...@@ -250,17 +250,17 @@ var ptrTests = []ptrTest{ ...@@ -250,17 +250,17 @@ var ptrTests = []ptrTest{
// Returning a C pointer is fine. // Returning a C pointer is fine.
name: "exportok", name: "exportok",
c: `#include <stdlib.h> c: `#include <stdlib.h>
extern unsigned char *GoFn22();`, extern unsigned char *GoFn22();`,
support: `//export GoFn22 support: `//export GoFn22
func GoFn22() *byte { return (*byte)(C.malloc(1)) }`, func GoFn22() *byte { return (*byte)(C.malloc(1)) }`,
body: `C.GoFn22()`, body: `C.GoFn22()`,
}, },
{ {
// Passing a Go string is fine. // Passing a Go string is fine.
name: "passstring", name: "passstring",
c: `#include <stddef.h> c: `#include <stddef.h>
typedef struct { const char *p; ptrdiff_t n; } gostring23; typedef struct { const char *p; ptrdiff_t n; } gostring23;
gostring23 f23(gostring23 s) { return s; }`, gostring23 f23(gostring23 s) { return s; }`,
imports: []string{"unsafe"}, imports: []string{"unsafe"},
body: `s := "a"; r := C.f23(*(*C.gostring23)(unsafe.Pointer(&s))); if *(*string)(unsafe.Pointer(&r)) != s { panic(r) }`, 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{ ...@@ -279,12 +279,12 @@ var ptrTests = []ptrTest{
c: `extern void f25();`, c: `extern void f25();`,
imports: []string{"strings"}, imports: []string{"strings"},
support: `//export GoStr25 support: `//export GoStr25
func GoStr25() string { return strings.Repeat("a", 2) }`, func GoStr25() string { return strings.Repeat("a", 2) }`,
body: `C.f25()`, body: `C.f25()`,
c1: `#include <stddef.h> c1: `#include <stddef.h>
typedef struct { const char *p; ptrdiff_t n; } gostring25; typedef struct { const char *p; ptrdiff_t n; } gostring25;
extern gostring25 GoStr25(); extern gostring25 GoStr25();
void f25() { GoStr25(); }`, void f25() { GoStr25(); }`,
fail: true, fail: true,
}, },
{ {
...@@ -295,7 +295,7 @@ var ptrTests = []ptrTest{ ...@@ -295,7 +295,7 @@ var ptrTests = []ptrTest{
// that is, we are testing something that is not unsafe. // that is, we are testing something that is not unsafe.
name: "ptrdata1", name: "ptrdata1",
c: `#include <stdlib.h> c: `#include <stdlib.h>
void f26(void* p) {}`, void f26(void* p) {}`,
imports: []string{"unsafe"}, imports: []string{"unsafe"},
support: `type S26 struct { p *int; a [8*8]byte; u uintptr }`, 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))`, 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{ ...@@ -305,7 +305,7 @@ var ptrTests = []ptrTest{
// Like ptrdata1, but with a type that uses a GC program. // Like ptrdata1, but with a type that uses a GC program.
name: "ptrdata2", name: "ptrdata2",
c: `#include <stdlib.h> c: `#include <stdlib.h>
void f27(void* p) {}`, void f27(void* p) {}`,
imports: []string{"unsafe"}, imports: []string{"unsafe"},
support: `type S27 struct { p *int; a [32769*8]byte; q *int; u uintptr }`, 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))`, 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))`,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment