1
0
mirror of https://github.com/golang/go synced 2024-11-27 00:51:26 -07:00
go/misc/cgo/test/issue21897.go
Keith Randall b868616b63 cmd/cgo: special case C ptr types to use uintptr
Some C types are declared as pointers, but C code
stores non-pointers in them.  When the Go garbage
collector sees such a pointer, it gets unhappy.

Instead, for these types represent them on the Go
side with uintptr.

We need this change to handle Apple's CoreFoundation
CF*Ref types. Users of these types might need to
update their code like we do in root_cgo_darwin.go.
The only change that is required under normal
circumstances is converting some nils to 0.
A go fix module is provided to help.

Fixes #21897

RELNOTE=yes

Change-Id: I9716cfb255dc918792625f42952aa171cd31ec1b
Reviewed-on: https://go-review.googlesource.com/66332
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2017-11-17 22:11:03 +00:00

57 lines
1.4 KiB
Go

// Copyright 2017 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.
// +build darwin,cgo,!internal
package cgotest
/*
#cgo LDFLAGS: -framework CoreFoundation
#include <CoreFoundation/CoreFoundation.h>
*/
import "C"
import (
"runtime/debug"
"testing"
"unsafe"
)
func test21897(t *testing.T) {
// Please write barrier, kick in soon.
defer debug.SetGCPercent(debug.SetGCPercent(1))
for i := 0; i < 10000; i++ {
testCFNumberRef()
testCFDateRef()
testCFBooleanRef()
// Allocate some memory, so eventually the write barrier is enabled
// and it will see writes of bad pointers in the test* functions below.
byteSliceSink = make([]byte, 1024)
}
}
var byteSliceSink []byte
func testCFNumberRef() {
var v int64 = 0
xCFNumberRef = C.CFNumberCreate(C.kCFAllocatorSystemDefault, C.kCFNumberSInt64Type, unsafe.Pointer(&v))
//fmt.Printf("CFNumberRef: %x\n", uintptr(unsafe.Pointer(xCFNumberRef)))
}
var xCFNumberRef C.CFNumberRef
func testCFDateRef() {
xCFDateRef = C.CFDateCreate(C.kCFAllocatorSystemDefault, 0) // 0 value is 1 Jan 2001 00:00:00 GMT
//fmt.Printf("CFDateRef: %x\n", uintptr(unsafe.Pointer(xCFDateRef)))
}
var xCFDateRef C.CFDateRef
func testCFBooleanRef() {
xCFBooleanRef = C.kCFBooleanFalse
//fmt.Printf("CFBooleanRef: %x\n", uintptr(unsafe.Pointer(xCFBooleanRef)))
}
var xCFBooleanRef C.CFBooleanRef