Threads

A guest thread is a host thread. pthread_create starts a host thread whose CPU state runs the start routine. Each thread that runs guest code has its own CPU state, guest stack and bionic TLS block: TPIDR_EL0 points to the TLS slots, where bionic's code reads the stack guard (slot 5) and the thread's own data. A thread of the JVM gets them the first time it calls a native method.

bionic's mutexes, condition variables, rwlocks, spinlocks, barriers, semaphores and thread attributes are the bridge's own (bionic_sync.c, hle_sync.c), in bionic's LP64 layout inside the guest's storage, as bionic keeps them on Android: no host object is behind them, so a static initializer, a copy or a struct the guest never zeroed means what it means on a phone. Each waits on the kernel library's futex, so a guest signal runs its handler during the wait. The host's own pthread types fit bionic's storage on glibc only: FreeBSD's and winpthreads' are pointers to heap objects, and macOS's mutex and rwlock are larger than bionic's.

Signals, threads' futexes and every number of syscall() are the GNU ABI runtime's kernel library (src/gnuabi/kernel/, GNU ABI) over the bridge's memory, whose guest addresses are the host's. bionic's functions (sigaction in bionic's LP64 layout, the masks, pthread_kill, the waits) only convert to it. The guest's signals are its own: the process is the JVM's too, and the guest never changes a host disposition or mask. A handler runs in the frame Linux gives it (common/runtime/linux_signal.c) where every guest register is in the thread's CPU structure: at RtAPI.poll, as an HLE call returns, or, for a thread that waits in a blocking HLE call (pthread_cond_wait, sem_wait, a read) or runs no guest code, from the handler of a host real-time signal that the sender gives it. A host fault in generated code still ends the app, as its registers are not exact there. An undefined instruction is exact (the code stores every register before it calls RtAPI.undef), so it is the guest's SIGILL with ILL_ILLOPC and the instruction's address, as Linux's force_sig_fault gives it: the handler runs when the guest has one that the thread does not block, and the app ends otherwise. OpenSSL finds the CPU's extensions this way: it runs an instruction of each one (SHA-512, SHA-3) under a handler that longjmps out. A host fault in generated code (SIGSEGV, SIGBUS, SIGILL, SIGFPE, or an exception on Windows) prints the guest's report before the JVM writes its own, which knows nothing of the guest (fault.c): the guest function or block whose compiled code holds the faulting host instruction, with its library and offset, then the pc and registers the code stored last and a backtrace by the guest's frame records. FreeBSD reports a general protection fault, such as a load through a non-canonical address, as SIGBUS with BUS_OBJERR at the pc. Every other fault goes to the JVM's handler. --log-signals logs each action, send and delivery, and --sample prints each guest thread's pc, x30 and HLE call every 5 seconds, for a stall.


Made from docs/platforms/android-apps/native.md at 69237645142c. GPL-3.0-or-later.