github.com/hikaru7719/go@v0.0.0-20181025140707-c8b2ac68906a/misc/cgo/test/issue21897.go (about)

     1  // Copyright 2017 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  // We skip this test in race mode because, for unknown reasons,
     6  // linking against CoreFoundation on macOS 10.10 causes mmap to ignore
     7  // the hint address, which makes the Go allocator incompatible with
     8  // TSAN. See golang.org/issue/26475.
     9  //
    10  // TODO(austin): Once support for macOS 10.10 is dropped, remove the
    11  // race constraint (and the one in issue21897b.go). See
    12  // golang.org/issue/26513.
    13  
    14  // +build darwin,cgo,!internal,!race
    15  
    16  package cgotest
    17  
    18  /*
    19  #cgo LDFLAGS: -framework CoreFoundation
    20  #include <CoreFoundation/CoreFoundation.h>
    21  */
    22  import "C"
    23  import (
    24  	"runtime/debug"
    25  	"testing"
    26  	"unsafe"
    27  )
    28  
    29  func test21897(t *testing.T) {
    30  	// Please write barrier, kick in soon.
    31  	defer debug.SetGCPercent(debug.SetGCPercent(1))
    32  
    33  	for i := 0; i < 10000; i++ {
    34  		testCFNumberRef()
    35  		testCFDateRef()
    36  		testCFBooleanRef()
    37  		// Allocate some memory, so eventually the write barrier is enabled
    38  		// and it will see writes of bad pointers in the test* functions below.
    39  		byteSliceSink = make([]byte, 1024)
    40  	}
    41  }
    42  
    43  var byteSliceSink []byte
    44  
    45  func testCFNumberRef() {
    46  	var v int64 = 0
    47  	xCFNumberRef = C.CFNumberCreate(C.kCFAllocatorSystemDefault, C.kCFNumberSInt64Type, unsafe.Pointer(&v))
    48  	//fmt.Printf("CFNumberRef: %x\n", uintptr(unsafe.Pointer(xCFNumberRef)))
    49  }
    50  
    51  var xCFNumberRef C.CFNumberRef
    52  
    53  func testCFDateRef() {
    54  	xCFDateRef = C.CFDateCreate(C.kCFAllocatorSystemDefault, 0) // 0 value is 1 Jan 2001 00:00:00 GMT
    55  	//fmt.Printf("CFDateRef: %x\n", uintptr(unsafe.Pointer(xCFDateRef)))
    56  }
    57  
    58  var xCFDateRef C.CFDateRef
    59  
    60  func testCFBooleanRef() {
    61  	xCFBooleanRef = C.kCFBooleanFalse
    62  	//fmt.Printf("CFBooleanRef: %x\n", uintptr(unsafe.Pointer(xCFBooleanRef)))
    63  }
    64  
    65  var xCFBooleanRef C.CFBooleanRef