Source file src/runtime/cgo/cgo_android.go
1 // Copyright 2026 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 //go:build goexperiment.runtimecgopurego && !msan && !tsan && android 6 7 package cgo 8 9 import "unsafe" 10 11 //go:linkname _cgo_libc_dlclose _cgo_libc_dlclose 12 var _cgo_libc_dlclose byte 13 var libc_dlclose = unsafe.Pointer(&_cgo_libc_dlclose) 14 15 //go:linkname _cgo_libc_android_log_write _cgo_libc_android_log_write 16 var _cgo_libc_android_log_write byte 17 var libc_android_log_write = unsafe.Pointer(&_cgo_libc_android_log_write) 18 19 // cgoInitTLS allocates or verifies a thread-local storage slot for g on Android. 20 // 21 //go:nosplit 22 //go:norace 23 //go:nocheckptr 24 func cgoInitTLS(tlsgAddr, tlsbase uintptr) { 25 // Truncated to a different magic value on 32-bit; that's ok. 26 const magic1 = uintptr(0x23581321345589 & (1<<(8*unsafe.Sizeof(uintptr(0))) - 1)) 27 tlsg := (*uintptr)(unsafe.Pointer(tlsgAddr)) 28 29 var done bool 30 var msg string 31 if done, msg = cgoInitTLSQ(tlsg); done { 32 if msg != "" { 33 cgoAndroidFatal(msg) 34 } 35 return 36 } 37 38 if msg = cgoInitTLSKey(tlsg, tlsbase, magic1); msg != "" { 39 cgoAndroidFatal(msg) 40 } 41 } 42 43 //go:nosplit 44 //go:norace 45 //go:nocheckptr 46 func cgoInitTLSQ(tlsg *uintptr) (bool, string) { 47 const _TLS_SLOT_APP = 2 48 const _RTLD_LAZY = 1 // from bionic's <dlfcn.h> 49 // Check for Android Q where we can use the free TLS_SLOT_APP slot. 50 const libcName = "libc.so\x00" 51 handle := asmcgocallNoG(uintptr(libc_dlopen), uintptr(unsafe.Pointer(unsafe.StringData(libcName))), _RTLD_LAZY, 0, 0, 0, 0) 52 if handle == 0 { 53 return true, "runtime/cgo: inittls: failed to dlopen main program\n\x00" 54 } 55 // android_get_device_api_level is introduced in Android Q, so its mere presence 56 // is enough. 57 const symName = "android_get_device_api_level\x00" 58 getVer := asmcgocallNoG(uintptr(libc_dlsym), handle, uintptr(unsafe.Pointer(unsafe.StringData(symName))), 0, 0, 0, 0) 59 asmcgocallNoG(uintptr(libc_dlclose), handle, 0, 0, 0, 0, 0) 60 if getVer == 0 { 61 return false, "" 62 } 63 const off = _TLS_SLOT_APP * unsafe.Sizeof(uintptr(0)) 64 // tlsg is initialized to Q's free TLS slot. Verify it while we're here. 65 if *tlsg != off { 66 return true, "runtime/cgo: tlsg offset wrong\n\x00" 67 } 68 return true, "" 69 } 70 71 //go:nosplit 72 //go:norace 73 //go:nocheckptr 74 func cgoInitTLSKey(tlsg *uintptr, tlsbase, magic1 uintptr) string { 75 var k uint32 // Bionic pthread_key_t is int 76 err := int32(asmcgocallNoG(uintptr(libc_pthread_key_create), uintptr(unsafe.Pointer(&k)), 0, 0, 0, 0, 0)) 77 if err != 0 { 78 return "runtime/cgo: pthread_key_create failed\n\x00" 79 } 80 asmcgocallNoG(uintptr(libc_pthread_setspecific), uintptr(k), magic1, 0, 0, 0, 0) 81 // If thread local slots are laid out as we expect, our magic word will 82 // be located at some low offset from tlsbase. However, just in case something went 83 // wrong, the search is limited to sensible offsets. PTHREAD_KEYS_MAX was the 84 // original limit, but issue 19472 made a higher limit necessary. 85 const ptrSize = unsafe.Sizeof(uintptr(0)) 86 for i := uintptr(0); i < 384; i++ { 87 if *(*uintptr)(unsafe.Pointer(tlsbase + i*ptrSize)) == magic1 { 88 *tlsg = i * ptrSize 89 asmcgocallNoG(uintptr(libc_pthread_setspecific), uintptr(k), 0, 0, 0, 0, 0) 90 return "" 91 } 92 } 93 return "runtime/cgo: inittls: could not find pthread key\n\x00" 94 } 95 96 //go:nosplit 97 //go:norace 98 //go:nocheckptr 99 func cgoAndroidFatal(msg string) { 100 // msg must end with "\n\x00". 101 // When running from an Android .apk, /dev/stderr and /dev/stdout 102 // redirect to /dev/null. And when running a test binary 103 // via adb shell, it's easy to miss logcat. So write to both. 104 // Write to logcat first: runtime.writeErrStr calls getg after 105 // writing to stderr, which is not safe before the TLS slot for g 106 // has been set up by cgoInitTLS. 107 const _ANDROID_LOG_FATAL = 7 108 const tag = "runtime/cgo\x00" 109 asmcgocallNoG(uintptr(libc_android_log_write), _ANDROID_LOG_FATAL, 110 uintptr(unsafe.Pointer(unsafe.StringData(tag))), 111 uintptr(unsafe.Pointer(unsafe.StringData(msg))), 0, 0, 0) 112 runtime_writeErrStr(msg[:len(msg)-1]) 113 runtime_abort() 114 } 115 116 // cgoFatal reports a fatal runtime/cgo error and aborts. 117 // msg must end with "\n\x00". 118 // 119 //go:nosplit 120 //go:norace 121 //go:nocheckptr 122 func cgoFatal(msg string) { 123 cgoAndroidFatal(msg) 124 } 125 126 //go:cgo_ldflag "-ldl" 127 //go:cgo_ldflag "-llog" 128 129 //go:cgo_import_dynamic _ _ "libc.so" 130 //go:cgo_import_dynamic _ _ "libdl.so" 131 //go:cgo_import_dynamic _ _ "liblog.so" 132 //go:cgo_import_dynamic _cgo_libc_dlopen dlopen "libdl.so" 133 //go:cgo_import_dynamic _cgo_libc_dlsym dlsym "libdl.so" 134 //go:cgo_import_dynamic _cgo_libc_dlclose dlclose "libdl.so" 135 //go:cgo_import_dynamic _cgo_libc_dlerror dlerror "libdl.so" 136 //go:cgo_import_dynamic _cgo_libc_android_log_write __android_log_write "liblog.so" 137 //go:cgo_import_dynamic _cgo_libc_mmap mmap "libc.so" 138 //go:cgo_import_dynamic _cgo_libc_munmap munmap "libc.so" 139 //go:cgo_import_dynamic _cgo_libc_sigaction sigaction "libc.so" 140 //go:cgo_import_dynamic _cgo_libc_sigfillset sigfillset "libc.so" 141 //go:cgo_import_dynamic _cgo_libc_errno_location __errno "libc.so" 142 //go:cgo_import_dynamic _cgo_libc_setenv setenv "libc.so" 143 //go:cgo_import_dynamic _cgo_libc_unsetenv unsetenv "libc.so" 144 //go:cgo_import_dynamic _cgo_libc_clearenv clearenv "libc.so" 145 //go:cgo_import_dynamic _cgo_libc_pthread_create pthread_create "libc.so" 146 //go:cgo_import_dynamic _cgo_libc_pthread_attr_init pthread_attr_init "libc.so" 147 //go:cgo_import_dynamic _cgo_libc_pthread_attr_setdetachstate pthread_attr_setdetachstate "libc.so" 148 //go:cgo_import_dynamic _cgo_libc_pthread_attr_getstacksize pthread_attr_getstacksize "libc.so" 149 //go:cgo_import_dynamic _cgo_libc_pthread_attr_destroy pthread_attr_destroy "libc.so" 150 //go:cgo_import_dynamic _cgo_libc_pthread_key_create pthread_key_create "libc.so" 151 //go:cgo_import_dynamic _cgo_libc_pthread_setspecific pthread_setspecific "libc.so" 152 //go:cgo_import_dynamic _cgo_libc_pthread_self pthread_self "libc.so" 153 //go:cgo_import_dynamic _cgo_libc_pthread_getattr_np pthread_getattr_np "libc.so" 154 //go:cgo_import_dynamic _cgo_libc_pthread_attr_getstack pthread_attr_getstack "libc.so" 155 //go:cgo_import_dynamic _cgo_libc_pthread_sigmask pthread_sigmask "libc.so" 156 //go:cgo_import_dynamic _cgo_libc_setegid setegid "libc.so" 157 //go:cgo_import_dynamic _cgo_libc_seteuid seteuid "libc.so" 158 //go:cgo_import_dynamic _cgo_libc_setgid setgid "libc.so" 159 //go:cgo_import_dynamic _cgo_libc_setgroups setgroups "libc.so" 160 //go:cgo_import_dynamic _cgo_libc_setregid setregid "libc.so" 161 //go:cgo_import_dynamic _cgo_libc_setresgid setresgid "libc.so" 162 //go:cgo_import_dynamic _cgo_libc_setresuid setresuid "libc.so" 163 //go:cgo_import_dynamic _cgo_libc_setreuid setreuid "libc.so" 164 //go:cgo_import_dynamic _cgo_libc_setuid setuid "libc.so" 165