Modern Apps git

← Modern-Apps.git

native: finish the guarded-loader migration

Commit
3906f460ab1fcd5b190e000d2c049609b2a7ad0f
Author
Vayun Mathur <[email protected]>
When
2026-11-20 19:16 UTC (15 days ago)
Parents
d982209
KartNative and YuuAvatarNative hand-rolled their catch around
loadLibrary + nativeInit and missed LinkageError /
NoClassDefFoundError from JNI registration, which could escape
class init the way unguarded loads did before. Load now goes
through NativeLoader and the nativeInit call is guarded by a
Throwable catch with runCatching-guarded logging.

SqlCipher caught only UnsatisfiedLinkError - the exact bug the
NativeLoader doc calls out. It now delegates to
NativeLoader.load (no cycle: library:log has no dependencies
and every library module already gets it from the conventions;
'already loaded' counts as success, failure still retries on
next call). Public APIs unchanged.

WebRtcInit deliberately keeps its rethrow: SignalCallManager
catches it to fail calls soft, and other callers rely on the
throw arriving before any PeerConnectionFactory is built.

Verified: compile for kart/avatar/room/communicate; KartHost
3/3, YuuAvatarNative 5/5, SqlCipher 6/6 tests.

Diff

games/kart/src/main/java/com/vayunmathur/games/kart/platform/KartNative.kt
diff --git a/games/kart/src/main/java/com/vayunmathur/games/kart/platform/KartNative.kt b/games/kart/src/main/java/com/vayunmathur/games/kart/platform/KartNative.ktindex 7e2115719a..fd1691cc4d 100644--- a/games/kart/src/main/java/com/vayunmathur/games/kart/platform/KartNative.kt+++ b/games/kart/src/main/java/com/vayunmathur/games/kart/platform/KartNative.kt@@ -4,6 +4,7 @@ package com.vayunmathur.games.kart.util  import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader  /**  * The engine bridge, shaped like `VoxelsNative.onMoveInput`.@@ -22,21 +23,30 @@ import com.vayunmathur.library.log.Log @Suppress("TooManyFunctions") object KartNative {     /** False until `libkart_engine` ships. The game checks this once per surface, like voxels. */-    var isAvailable: Boolean = try {-        System.loadLibrary("kart_engine")+    var isAvailable: Boolean = NativeLoader.load("kart_engine", "KartNative") && initEngine()+        // Internal so host tests can force the placeholder race on machines with the engine.+        internal set++    /**+     * Load + init are one guarded unit: [NativeLoader.load] only loads, and+     * `nativeInit` can fail with any `Throwable` (`UnsatisfiedLinkError` if a+     * symbol is missing, `NoClassDefFoundError`/`LinkageError` from the JNI+     * registration, ...), not just the two errors the hand-rolled guard used+     * to catch. Any failure leaves [isAvailable] false so the game runs the+     * placeholder race instead of poisoning this class's initializer. The+     * `nativeInit` return value stays ignored, as before. Logging is+     * `runCatching`-guarded for the same host-JVM reason as in [NativeLoader]:+     * `Log` can throw on a plain JVM, and that must not escape class init.+     */+    private fun initEngine(): Boolean = try {         nativeInit()-        Log.status("KartNative", "libkart_engine loaded")         true-    } catch (e: UnsatisfiedLinkError) {-        // Library not packaged in this build: fall back to the placeholder race.-        Log.status("KartNative", "libkart_engine absent - running the placeholder race", e)-        false-    } catch (e: SecurityException) {-        Log.status("KartNative", "libkart_engine load denied - running the placeholder race", e)+    } catch (failure: Throwable) {+        runCatching {+            Log.error("KartNative", "libkart_engine init failed - running the placeholder race", failure)+        }         false     }-        // Internal so host tests can force the placeholder race on machines with the engine.-        internal set      /**      * Start (or restart) the native race. `playerIds` is the grid in seat order;
library/avatar/src/main/java/com/vayunmathur/library/avatar/YuuAvatarNative.kt
diff --git a/library/avatar/src/main/java/com/vayunmathur/library/avatar/YuuAvatarNative.kt b/library/avatar/src/main/java/com/vayunmathur/library/avatar/YuuAvatarNative.ktindex fb3423efc3..92191ae7e3 100644--- a/library/avatar/src/main/java/com/vayunmathur/library/avatar/YuuAvatarNative.kt+++ b/library/avatar/src/main/java/com/vayunmathur/library/avatar/YuuAvatarNative.kt@@ -1,6 +1,7 @@ package com.vayunmathur.library.avatar  import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader  /**  * The 3D avatar engine bridge, shaped like kart's `KartNative` and@@ -17,17 +18,26 @@ import com.vayunmathur.library.log.Log  */ internal object YuuAvatarNative {     /** False until `libyuu_avatar` ships. Checked once per surface, like kart. */-    val isAvailable: Boolean = try {-        System.loadLibrary("yuu_avatar")+    val isAvailable: Boolean = NativeLoader.load("yuu_avatar", "YuuAvatarNative") && initEngine()++    /**+     * Load + init are one guarded unit: [NativeLoader.load] only loads, and+     * `nativeInit` can fail with any `Throwable` (`UnsatisfiedLinkError` if a+     * symbol is missing, `NoClassDefFoundError`/`LinkageError` from the JNI+     * registration, ...), not just the two errors the hand-rolled guard used+     * to catch. Any failure leaves [isAvailable] false so hero surfaces use+     * the placeholder instead of poisoning this class's initializer. The+     * `nativeInit` return value stays ignored, as before. Logging is+     * `runCatching`-guarded for the same host-JVM reason as in [NativeLoader]:+     * `Log` can throw on a plain JVM, and that must not escape class init.+     */+    private fun initEngine(): Boolean = try {         nativeInit()-        Log.status("YuuAvatarNative", "libyuu_avatar loaded")         true-    } catch (e: UnsatisfiedLinkError) {-        // Library absent on this device: surfaces use the placeholder.-        Log.status("YuuAvatarNative", "libyuu_avatar absent - using placeholder (${e.message})")-        false-    } catch (e: SecurityException) {-        Log.status("YuuAvatarNative", "libyuu_avatar load denied - using placeholder (${e.message})")+    } catch (failure: Throwable) {+        runCatching {+            Log.error("YuuAvatarNative", "libyuu_avatar init failed - using placeholder", failure)+        }         false     } 
library/room/src/main/java/com/vayunmathur/library/room/SqlCipher.kt
diff --git a/library/room/src/main/java/com/vayunmathur/library/room/SqlCipher.kt b/library/room/src/main/java/com/vayunmathur/library/room/SqlCipher.ktindex 2d7aa8e9a3..40f7c9f173 100644--- a/library/room/src/main/java/com/vayunmathur/library/room/SqlCipher.kt+++ b/library/room/src/main/java/com/vayunmathur/library/room/SqlCipher.kt@@ -2,6 +2,7 @@ package com.vayunmathur.library.room  import android.content.Context import com.vayunmathur.library.log.Log+import com.vayunmathur.library.log.NativeLoader import androidx.room3.Room import androidx.room3.RoomDatabase import androidx.room3.migration.Migration@@ -15,12 +16,13 @@ import java.io.FileInputStream private var sqlCipherLoaded = false fun loadSqlCipher() {     if (sqlCipherLoaded) return-    try {-        System.loadLibrary("sqlcipher")-        sqlCipherLoaded = true-    } catch (expected: UnsatisfiedLinkError) {-        Log.error(TAG, "Failed to load sqlcipher native library", expected)-    }+    // Delegates to the shared guard (catches every Throwable, keeps its own+    // logging host-JVM-safe, treats an already-loaded report as success).+    // Depending on :library:log cannot cycle: that module has no dependencies+    // at all, and common-conventions-library already puts it on this module's+    // classpath, so no build-file change is needed. On failure the flag stays+    // false, so the next call retries — same contract as the hand-rolled try.+    if (NativeLoader.load("sqlcipher", TAG)) sqlCipherLoaded = true }  private const val TAG = "SqlCipher"