github.com/huandu/go@v0.0.0-20151114150818-04e615e41150/misc/cgo/test/issue10303.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Issue 10303. Pointers passed to C were not marked as escaping (bug in cgo). 6 7 package cgotest 8 9 /* 10 typedef int *intptr; 11 12 void setintstar(int *x) { 13 *x = 1; 14 } 15 16 void setintptr(intptr x) { 17 *x = 1; 18 } 19 20 void setvoidptr(void *x) { 21 *(int*)x = 1; 22 } 23 24 typedef struct Struct Struct; 25 struct Struct { 26 int *P; 27 }; 28 29 void setstruct(Struct s) { 30 *s.P = 1; 31 } 32 33 */ 34 import "C" 35 36 import ( 37 "testing" 38 "unsafe" 39 ) 40 41 func test10303(t *testing.T, n int) { 42 // Run at a few different stack depths just to avoid an unlucky pass 43 // due to variables ending up on different pages. 44 if n > 0 { 45 test10303(t, n-1) 46 } 47 if t.Failed() { 48 return 49 } 50 var x, y, z, v, si C.int 51 var s C.Struct 52 C.setintstar(&x) 53 C.setintptr(&y) 54 C.setvoidptr(unsafe.Pointer(&v)) 55 s.P = &si 56 C.setstruct(s) 57 58 if uintptr(unsafe.Pointer(&x))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { 59 t.Error("C int* argument on stack") 60 } 61 if uintptr(unsafe.Pointer(&y))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { 62 t.Error("C intptr argument on stack") 63 } 64 if uintptr(unsafe.Pointer(&v))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { 65 t.Error("C void* argument on stack") 66 } 67 if uintptr(unsafe.Pointer(&si))&^0xfff == uintptr(unsafe.Pointer(&z))&^0xfff { 68 t.Error("C struct field pointer on stack") 69 } 70 }