vpn: do not poison VpnNative class when the native library is missing
- Commit
07211d877cbc6e1a7899642a62543ce1e6ddc237- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-19 01:58 UTC (17 days ago)
- Parents
67aa4ef
VpnNative's object initializer caught UnsatisfiedLinkError from System.loadLibrary and rethrew it. That failed class initialization permanently: the service's onCreate only caught UnsatisfiedLinkError/SecurityException, so the resulting initializer failure escaped, and every later touch of VpnNative (newTunnel in the tunnel coroutine, handshake/timer calls in the pump loops) threw NoClassDefFoundError instead of the intended 'native lib missing' log-and-stop. VpnNative now records the outcome in isLoaded and never rethrows from init (UnsatisfiedLinkError and SecurityException both map to false). VpnTunnelService.onCreate logs from isLoaded, and runTunnel returns via stopVpn() when the library is absent instead of calling into natives that cannot link. Callers that already runCatching (key derivation on import) are unchanged. No Rust changed, so no cargo test was needed; ./check ran cargo check/clippy/fmt/doc for vpn_wireguard as part of the module check. Verified: ./gradlew :vpn:testDevUnitTest (six-module run, BUILD SUCCESSFUL) and ./check todo translate travel tuner vpn (exit 0).
Diff
vpn/src/main/java/com/vayunmathur/vpn/service/VpnTunnelService.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/service/VpnTunnelService.kt b/vpn/src/main/java/com/vayunmathur/vpn/service/VpnTunnelService.ktindex 0763429c2d..79f17d0e7a 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/service/VpnTunnelService.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/service/VpnTunnelService.kt@@ -95,12 +95,10 @@ class VpnTunnelService : VpnService() { override fun onCreate() { super.onCreate()- try {- VpnNative.init()- } catch (expected: UnsatisfiedLinkError) {- Log.error(TAG, "native lib missing", expected)- } catch (expected: SecurityException) {- Log.error(TAG, "native lib blocked", expected)+ // Touching VpnNative loads the class; isLoaded records whether the+ // .so actually loaded (VpnNative no longer rethrows from its init).+ if (!VpnNative.isLoaded) {+ Log.error(TAG, "native lib missing") } val nm = getSystemService(NotificationManager::class.java) nm?.createNotificationChannel(@@ -199,6 +197,14 @@ class VpnTunnelService : VpnService() { private suspend fun runTunnel(config: VpnConfig) { isRunning = true + // Without the .so every native call below throws+ // UnsatisfiedLinkError out of the tunnel coroutine.+ if (!VpnNative.isLoaded) {+ Log.error(TAG, "newTunnel skipped: native lib missing")+ stopVpn()+ return+ }+ val handle = VpnNative.newTunnel( config.privateKey, config.peerPublicKey, config.peerPresharedKey, config.peerKeepalive )
vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt
diff --git a/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt b/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.ktindex 8896437629..22307f1357 100644--- a/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt+++ b/vpn/src/main/java/com/vayunmathur/vpn/util/VpnNative.kt@@ -11,12 +11,26 @@ import com.vayunmathur.library.log.Log * Android TUN fd <-> UDP socket bridging lives in [com.vayunmathur.vpn.service.VpnTunnelService]. */ object VpnNative {+ /**+ * False when the native library could not be loaded (missing split /+ * wrong ABI). The load failure is recorded instead of rethrown from+ * the class initializer: rethrowing poisons the class, so every later+ * touch (including the service's "native missing" handling) would die+ * with NoClassDefFoundError/ExceptionInInitializerError instead.+ * Callers that need a tunnel must check this before calling natives.+ */+ val isLoaded: Boolean+ init {- try {+ isLoaded = try { System.loadLibrary("vpn_wireguard")+ true } catch (expected: UnsatisfiedLinkError) { Log.error("VpnNative", "System.loadLibrary(vpn_wireguard) failed", expected)- throw expected+ false+ } catch (expected: SecurityException) {+ Log.error("VpnNative", "System.loadLibrary(vpn_wireguard) blocked", expected)+ false } }