Skip to content

Commit

Permalink
java: Set up class loader for libsignal_jni_testing.so too
Browse files Browse the repository at this point in the history
The class loader is a rare process-wide variable for us. When the
testing crates are compiled into the same .so as the main bridge
crates, they all use the same variable, but when we build for Android
we have two separate native library files that get loaded, and so we
need to initialize both.
  • Loading branch information
jrose-signal committed Aug 29, 2024
1 parent a2d5773 commit 9400604
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ private static void loadNativeCode() {

static {
loadNativeCode();
initializeLibrary();
}

private NativeTesting() {}
Expand Down Expand Up @@ -96,5 +97,7 @@ private NativeTesting() {}

public static native void TestingHandleType_Destroy(long handle);

public static native void initializeLibrary();

public static native int test_only_fn_returns_123();
}
1 change: 1 addition & 0 deletions rust/bridge/jni/bin/NativeTesting.java.in
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public final class NativeTesting {

static {
loadNativeCode();
initializeLibrary();
}

private NativeTesting() {}
Expand Down
2 changes: 2 additions & 0 deletions rust/bridge/jni/testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ crate-type = ["cdylib"]

[dependencies]
libsignal-bridge-testing = { path = "../../shared/testing", features = ["jni"] }
libsignal-bridge-types = { path = "../../shared/types", features = ["jni"] }

jni = { workspace = true }
log = { workspace = true }
30 changes: 30 additions & 0 deletions rust/bridge/jni/testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,37 @@
// SPDX-License-Identifier: AGPL-3.0-only
//

#![allow(clippy::missing_safety_doc)]

use jni::objects::JClass;
use jni::JNIEnv;
// Import bridged functions. Without this, the compiler and/or linker are too
// smart and don't include the symbols in the library.
#[allow(unused_imports)]
use libsignal_bridge_testing::*;
use libsignal_bridge_types::jni::run_ffi_safe;

/// Initialize internal data structures.
///
/// Initialization function used to set up internal data structures. This should be called once when
/// the library is first loaded. Must support being run in the same shared object as the
/// `initializeLibrary` in libsignal-jni, as well as the intended use case of running in a different
/// one.
#[no_mangle]
pub unsafe extern "C" fn Java_org_signal_libsignal_internal_NativeTesting_initializeLibrary<
'local,
>(
mut env: JNIEnv<'local>,
class: JClass<'local>,
) {
run_ffi_safe(&mut env, |env| {
#[cfg(target_os = "android")]
libsignal_bridge_types::jni::save_class_loader(env, &class)?;

// Silence the unused variable warning on non-Android.
_ = class;
_ = env;

Ok(())
})
}

0 comments on commit 9400604

Please sign in to comment.