mln-rs-style 0.0.21

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! The Java VM that the crates' Android services call: HTTP through
//! `HttpURLConnection`, and `android.icu` for the locale-sensitive
//! expressions.

#![expect(unsafe_code, reason = "dlsym and the JNI invocation API are the FFI here")]

use std::ffi::{CStr, c_void};

use jni::JavaVM;
use jni::sys::{JNI_OK, jint, jsize};

/// The Java VM: the one that Android initialization recorded as `jni`'s
/// (`mln_android_init()`, or `mln_engine::storage`'s and `mln_engine::text::system_fonts`' `init`),
/// or else the process's own, such as an app's that didn't initialize the
/// library, or that of a test that runs in a Java VM's process. `None` in a
/// process without one.
#[must_use]
pub fn java_vm() -> Option<JavaVM> {
    JavaVM::singleton().ok().or_else(process_java_vm)
}

/// The process's Java VM, through `JNI_GetCreatedJavaVMs`, which
/// libnativehelper exports to apps from API 31. A process with a Java VM has
/// loaded libnativehelper, so this doesn't load it (`RTLD_NOLOAD`). Before API
/// 31, a process without a Java VM can still have loaded libnativehelper, whose
/// `JNI_GetCreatedJavaVMs` then aborts the process instead of finding none.
fn process_java_vm() -> Option<JavaVM> {
    type GetCreatedJavaVms = unsafe extern "system" fn(*mut *mut jni::sys::JavaVM, jsize, *mut jsize) -> jint;
    if device_api_level() < 31 {
        return None;
    }
    // SAFETY: a library name and flags that dlopen takes.
    let library = unsafe { libc::dlopen(c"libnativehelper.so".as_ptr(), libc::RTLD_NOW | libc::RTLD_NOLOAD) };
    if library.is_null() {
        return None;
    }
    // SAFETY: the loaded library, and a symbol name.
    let symbol = unsafe { libc::dlsym(library, c"JNI_GetCreatedJavaVMs".as_ptr()) };
    if symbol.is_null() {
        return None;
    }
    // SAFETY: JNI_GetCreatedJavaVMs has this signature.
    let get_created_java_vms = unsafe { std::mem::transmute::<*mut c_void, GetCreatedJavaVms>(symbol) };
    let mut vm = std::ptr::null_mut();
    let mut count = 0;
    // SAFETY: room for one VM, and the count.
    let status = unsafe { get_created_java_vms(&raw mut vm, 1, &raw mut count) };
    // SAFETY: the process's live VM, which it keeps until it exits.
    (status == JNI_OK && count == 1 && !vm.is_null()).then(|| unsafe { JavaVM::from_raw(vm) })
}

/// The device's API level, from `ro.build.version.sdk`, or 0 where it reads
/// none.
fn device_api_level() -> i32 {
    let mut value = [0; libc::PROP_VALUE_MAX as usize];
    // SAFETY: a property name, and room for the longest value and its NUL.
    unsafe { libc::__system_property_get(c"ro.build.version.sdk".as_ptr(), value.as_mut_ptr()) };
    // SAFETY: the value, NUL-terminated within the buffer, or empty.
    let value = unsafe { CStr::from_ptr(value.as_ptr()) };
    value.to_str().ok().and_then(|value| value.parse().ok()).unwrap_or(0)
}