From 68008115975a15ea0bff4709f840ec7856428e35 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Wed, 10 Feb 2021 15:26:24 +0900 Subject: [PATCH] Load JNI in all classes that have native methods. The tethering module uses JNI in various classes, but only calls System.loadLibrary in TetheringService#makeTethering. This means that: 1. Any test that uses a class that uses JNI must load the library itself. 2. Any code that runs before TetheringService#makeTethering could potentially crash if it uses JNI. We may never have such code though. Instead, make every class that has a native method load the JNI library itself at static initialization time. This guarantees that the class will have the JNI code available in any context (production, test, etc.) System.loadLibrary is documented not to do anything if called more than once with the same library name: https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#loadLibrary(java.lang.String) and the implementation has a lock so it is safe to call from multiple threads concurrently. Test: builds, boots, tethering starts Test: atest TetheringCoverageTests Change-Id: I9c0147ae9a28877f416aaff387b426d304ae552d --- Tethering/src/android/net/util/TetheringUtils.java | 4 ++++ Tethering/src/com/android/networkstack/tethering/BpfMap.java | 4 ++++ .../com/android/networkstack/tethering/TetheringService.java | 1 - 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Tethering/src/android/net/util/TetheringUtils.java b/Tethering/src/android/net/util/TetheringUtils.java index 706d78c1cc..9e7cc2fc14 100644 --- a/Tethering/src/android/net/util/TetheringUtils.java +++ b/Tethering/src/android/net/util/TetheringUtils.java @@ -36,6 +36,10 @@ import java.util.Objects; * {@hide} */ public class TetheringUtils { + static { + System.loadLibrary("tetherutilsjni"); + } + public static final byte[] ALL_NODES = new byte[] { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; diff --git a/Tethering/src/com/android/networkstack/tethering/BpfMap.java b/Tethering/src/com/android/networkstack/tethering/BpfMap.java index bc01dbd41d..9a9376f0a7 100644 --- a/Tethering/src/com/android/networkstack/tethering/BpfMap.java +++ b/Tethering/src/com/android/networkstack/tethering/BpfMap.java @@ -41,6 +41,10 @@ import java.util.function.BiConsumer; * @param the value of the map. */ public class BpfMap implements AutoCloseable { + static { + System.loadLibrary("tetherutilsjni"); + } + // Following definitions from kernel include/uapi/linux/bpf.h public static final int BPF_F_RDWR = 0; public static final int BPF_F_RDONLY = 1 << 3; diff --git a/Tethering/src/com/android/networkstack/tethering/TetheringService.java b/Tethering/src/com/android/networkstack/tethering/TetheringService.java index 613328d1c1..c69dc49c25 100644 --- a/Tethering/src/com/android/networkstack/tethering/TetheringService.java +++ b/Tethering/src/com/android/networkstack/tethering/TetheringService.java @@ -82,7 +82,6 @@ public class TetheringService extends Service { */ @VisibleForTesting public Tethering makeTethering(TetheringDependencies deps) { - System.loadLibrary("tetherutilsjni"); return new Tethering(deps); }