Repository: go
Issue: cmd/cgo: provide some mechanism for treating certain C pointer types as uintptr
Labels: help wanted,NeedsInvestigation,FeatureRequest

Please answer these questions before submitting your issue. Thanks!


### What version of Go are you using (`go version`)?
go version go1.9.2 darwin/amd64

### Does this issue reproduce with the latest release?
yes

### What operating system and processor architecture are you using (`go env`)?
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/tschinke/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bv/6rt5ck194ls704dz9p4c0hlh0000gn/T/go-build985720357=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

### What did you do?
I'm not using goMobile but using jni + cgo directly to get maximum control. The code works without any problems on ARM but fails on x86 randomly. At first it looks like the usual cgo check, however I'm quite confident that this is a bug. One variant is this:

E/Go: panic: runtime error: cgo argument has Go pointer to Go pointer
 E/Go: goroutine 17 [running, locked to thread]:
 E/Go: main.GetPrimitiveArrayCritical.func1(0x5d12ef60, 0x72d00005, 0x72c68a80)
 E/Go: 	/Users/tschinke/repos/libs_wdy/go_snappy/go/src/wdy/jni.go:1852 +0x79
 E/Go: main.GetPrimitiveArrayCritical(0x5d12ef60, 0x72d00005, 0x62811b64)
 E/Go: 	/Users/tschinke/repos/libs_wdy/go_snappy/go/src/wdy/jni.go:1852 +0x23
 E/Go: main.Java_de_worldiety_snappy_go_GoSnappy_uncompress2(0x5d12ef60, 0x7c700001, 0x72d00005, 0x0, 0x8, 0x18)
 E/Go: 	/Users/tschinke/repos/libs_wdy/go_snappy/go/src/wdy/math.go:79 +0x27
 E/Go: main._cgoexpwrap_3ee6d16a6a1f_Java_de_worldiety_snappy_go_GoSnappy_uncompress2(0x5d12ef60, 0x7c700001, 0x72d00005, 0x0, 0x8, 0x0)
 E/Go: 	command-line-arguments/_obj/_cgo_gotypes.go:3055 +0x6a

But the cgo statement is wrong, math.go:79 looks like this:

77: //export Java_de_worldiety_snappy_go_GoSnappy_uncompress2
78: func Java_de_worldiety_snappy_go_GoSnappy_uncompress2(env *C.JNIEnv, clazz C.jclass, compressed C.jbyteArray, compressedOffset C.jint, compressedSize C.jint) C.jbyteArray {
79: ptrComp := GetPrimitiveArrayCritical(env, C.jarray(compressed))

To be complete, the GetPrimitiveArrayCritical method is declared like this:
// jni.h:
//     void * (JNICALL *GetPrimitiveArrayCritical)(JNIEnv *env, jarray array, jboolean *isCopy);
func GetPrimitiveArrayCritical(env *C.JNIEnv, array C.jarray) unsafe.Pointer {
	return C._GoJniGetPrimitiveArrayCritical(env, array)
}

And this:
static void* _GoJniGetPrimitiveArrayCritical(JNIEnv* env, jarray array)
{
	return (*env)->GetPrimitiveArrayCritical(env, array, JNI_FALSE);
}

Please correct me, but I can't see any Go-Pointer at all.

### What did you expect to see?
a) cgo check should not randomly fail
b) cgo check should be consistent across targets (x86 and arm)
c) a possibility to disable the cgo check at runtime, which works for an android library. Setting the environment variable through Android (Os.setenv or libcore) has no effect, and to set it from Go itself is to late. Give us some public variable like GOGC.

### What did you see instead?
Random panics on Dell Venue 8, running Android 4.4 on x86 (32bit), but works on ARMs without problems.