Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 67 additions & 4 deletions webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -105,6 +107,12 @@ public class NativeLoader {

private static final Set<String> LOADED_LIB_SET = new HashSet<>();

/** The libraries the thread that holds the lock of this class is loading. */
private static final Set<String> LOADING_LIB_SET = new HashSet<>();

/** What is to run once a library is loaded, by the name of the library. */
private static final Map<String, List<Runnable>> WHEN_LOADED = new HashMap<>();

/** Whether this process has swept up after earlier ones yet. */
private static boolean swept;

Expand Down Expand Up @@ -160,15 +168,70 @@ public static void loadLibrary(final String libName) throws Exception {
*/
public static synchronized void loadLibrary(final String libName,
final String... dependencies) throws Exception {
if (LOADED_LIB_SET.contains(libName)) {
loadOnce(libName, () -> load(libName + "-" + platform(),
Arrays.asList(dependencies), Locations.fromSystem(),
NativeLoader::classPathResource, Linker.SYSTEM));
}

/**
* Runs the given loader, unless the library is loaded already or is being
* loaded by this thread, and then what is to run once it is loaded.
* <p>
* Loading a library runs its {@code JNI_OnLoad}, which looks up Java
* classes and with that initializes them. A class that loads the same
* library in its static initializer then calls back in on this thread,
* which the lock of this class lets through. That nested call must not
* load the library again, which would put a second copy of it into the
* process: it returns at once, before the library is loaded, and the outer
* call finishes loading it. Code that needs the library at that point
* goes through {@link #whenLoaded(String, Runnable)}. A loader that fails
* leaves the library not loaded, so that a later call tries anew.
*
* @param libName The name of the library.
* @param loader Loads the library.
*/
static synchronized void loadOnce(String libName, Runnable loader) {
if (LOADED_LIB_SET.contains(libName) || !LOADING_LIB_SET.add(libName)) {
return;
}

load(libName + "-" + platform(),
Arrays.asList(dependencies), Locations.fromSystem(),
NativeLoader::classPathResource, Linker.SYSTEM);
try {
loader.run();
}
finally {
LOADING_LIB_SET.remove(libName);
}

LOADED_LIB_SET.add(libName);

List<Runnable> actions = WHEN_LOADED.remove(libName);

if (actions != null) {
for (Runnable action : actions) {
action.run();
}
}
}

/**
* Runs the given action now if the library is loaded, or else once it is.
* A static initializer that loads a library and then calls one of its
* native methods makes that call through this. When the loading of the
* library is what runs the initializer, the library is not loaded yet, see
* {@link #loadOnce(String, Runnable)}, and the JVM finds none of its
* native methods before its {@code JNI_OnLoad} has returned.
*
* @param libName The name of the library, as given to {@link
* #loadLibrary(String, String...)}.
* @param action What needs the library.
*/
static synchronized void whenLoaded(String libName, Runnable action) {
if (LOADED_LIB_SET.contains(libName)) {
action.run();
}
else {
WHEN_LOADED.computeIfAbsent(libName, name -> new ArrayList<>()).add(action);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ public abstract class NativeObject {
throw new RuntimeException("Load library 'webrtc-java' failed", e);
}

LeakDetector.initialize();
// When loading the library is what initializes this class, the call
// above returns before the library is loaded.
NativeLoader.whenLoaded("webrtc-java", LeakDetector::initialize);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import dev.onvoid.webrtc.RTCConfiguration;
import dev.onvoid.webrtc.RTCPeerConnection;
import dev.onvoid.webrtc.RTCRtpSender;
import dev.onvoid.webrtc.logging.Logging;
import dev.onvoid.webrtc.media.MediaStreamTrack;
import dev.onvoid.webrtc.media.audio.AudioConverter;
import dev.onvoid.webrtc.media.audio.AudioDeviceModule;
Expand All @@ -44,7 +45,18 @@
*/
public final class LeakDetectionUse {

/**
* The system property that has {@link Logging} load the library, which
* then initializes {@link NativeObject} while it is being loaded.
*/
static final String LOGGING_FIRST_PROPERTY = "test.leak.logging.first";


public static void main(String[] args) {
if (Boolean.getBoolean(LOGGING_FIRST_PROPERTY)) {
Logging.logTimestamps(false);
}

List<String> reports = Collections.synchronizedList(new ArrayList<>());
LeakDetector.reporter = reports::add;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ void forgottenObjectsReported() throws Exception {
assertEquals(0, result.exitValue, result.output);
}

@Test
void reportedWithLoggingFirst() throws Exception {
Result result = run("-D" + LeakDetector.PROPERTY + "=true",
"-D" + LeakDetectionUse.LOGGING_FIRST_PROPERTY + "=true");

assertEquals(0, result.exitValue, result.output);
}

@Test
void traceNamesWhereCreated() throws Exception {
Result result = run("-D" + LeakDetector.PROPERTY + "=true", "-D" + LeakDetector.TRACE_PROPERTY + "=true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
Expand All @@ -37,6 +38,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import dev.onvoid.webrtc.TestBase;
Expand All @@ -45,8 +47,8 @@
import org.junit.jupiter.api.io.TempDir;

/**
* Covers where native libraries are looked for and the cleanup of extracted
* ones. What is swept is an isolated directory set up here, never the real
* Covers that a native library is loaded once, where native libraries are
* looked for and the cleanup of extracted ones. What is swept is an isolated directory set up here, never the real
* temporary directory, and the time a sweep runs at is given rather than read
* from the clock. The lookup is driven with a linker that only records what it
* is handed; {@link LibraryLocationTests} loads the real library.
Expand All @@ -63,6 +65,8 @@ class NativeLoaderTests extends TestBase {

private static final String LIB_FILE = System.mapLibraryName(LIB);

private static final AtomicInteger LIB_COUNTER = new AtomicInteger();

@TempDir
Path root;

Expand Down Expand Up @@ -137,6 +141,59 @@ void holdsOwnDirectory() throws Exception {
assertTrue(NativeLoader.isInUse(directory));
}

@Test
void nestedLoadLoadsOnce() {
String lib = freshLib();
AtomicInteger loads = new AtomicInteger();

NativeLoader.loadOnce(lib, () -> {
loads.incrementAndGet();

// As when JNI_OnLoad initializes a class that loads the library too.
NativeLoader.loadOnce(lib, loads::incrementAndGet);
});
NativeLoader.loadOnce(lib, loads::incrementAndGet);

assertEquals(1, loads.get());
}

@Test
void whenLoadedAwaitsLoad() {
String lib = freshLib();
List<String> events = new ArrayList<>();

NativeLoader.loadOnce(lib, () -> {
// Called while the library is being loaded.
NativeLoader.whenLoaded(lib, () -> events.add("action"));
events.add("loaded");
});
NativeLoader.whenLoaded(lib, () -> events.add("later action"));

assertEquals(Arrays.asList("loaded", "action", "later action"), events);
}

@Test
void failedLoadRetried() {
String lib = freshLib();
List<String> events = new ArrayList<>();
UnsatisfiedLinkError failure = new UnsatisfiedLinkError("failed");

UnsatisfiedLinkError error = assertThrows(UnsatisfiedLinkError.class,
() -> NativeLoader.loadOnce(lib, () -> {
NativeLoader.whenLoaded(lib, () -> events.add("action"));

throw failure;
}));

assertSame(failure, error);
assertTrue(events.isEmpty());

// The action waits for the load that succeeds.
NativeLoader.loadOnce(lib, () -> events.add("loaded"));

assertEquals(Arrays.asList("loaded", "action"), events);
}

@Test
void libraryDirWins() throws Exception {
Path dir = Files.createDirectory(root.resolve("installed"));
Expand Down Expand Up @@ -267,6 +324,14 @@ void detectsMusl() throws Exception {
assertFalse(NativeLoader.failureMessage(LIB_FILE, Collections.emptyList(), false).contains("musl"));
}

/**
* A library name no test has used yet, since a library stays loaded for
* as long as the JVM runs.
*/
private static String freshLib() {
return LIB + "-" + LIB_COUNTER.incrementAndGet();
}

private NativeLoader.Locations locations(Path libraryDir, List<Path> searchPath) {
return new NativeLoader.Locations(libraryDir, root.resolve("extract"), searchPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package dev.onvoid.webrtc.internal;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -80,6 +81,11 @@ private static void runAlone(String name) throws Exception {

String log = new String(output.toByteArray(), StandardCharsets.UTF_8);
assertEquals(0, process.exitValue(), () -> name + " failed in a fresh JVM:\n" + log);

// A second copy of the library shows on macOS, where the Objective-C
// runtime warns of each class that both copies implement.
assertFalse(log.contains("is implemented in both"),
() -> name + " loaded the native library twice:\n" + log);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.nio.ByteBuffer;

import dev.onvoid.webrtc.logging.Logging;
import dev.onvoid.webrtc.media.FourCC;
import dev.onvoid.webrtc.media.audio.AudioConverter;
import dev.onvoid.webrtc.media.audio.AudioResampler;
Expand All @@ -41,6 +42,7 @@ public final class StandaloneUse {
"AudioResampler",
"CustomAudioSource",
"CustomVideoSource",
"Logging",
"NativeI420Buffer",
"VideoBufferConverter",
"VideoCapture"
Expand All @@ -61,6 +63,11 @@ public static void main(String[] args) throws Exception {
case "CustomVideoSource":
new CustomVideoSource().dispose();
break;
case "Logging":
// Not a native object: the library initializes NativeObject
// while it is being loaded.
Logging.logTimestamps(true);
break;
case "NativeI420Buffer":
NativeI420Buffer.allocate(2, 2).release();
break;
Expand Down
Loading