Skip to content

Commit 8ea06b5

Browse files
committed
changed library.open to a function that fixes bug that the libloading people won't fix)
1 parent beebc80 commit 8ea06b5

File tree

1 file changed

+45
-38
lines changed

1 file changed

+45
-38
lines changed

native/src/lib.rs

+45-38
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl LibraryManager {
2828
loaded_libraries: RwLock::new(HashMap::new()),
2929
}
3030
}
31-
fn push_lib(&mut self, libname: String, lib: (Library, bool)) {
31+
fn push_lib(&self, libname: String, lib: (Library, bool)) {
3232
let libs = &mut self.loaded_libraries.write();
3333
libs.insert(libname, SwitchableLibrary::new(lib.0, lib.1));
3434
}
@@ -47,7 +47,7 @@ impl LibraryManager {
4747
}
4848
}
4949

50-
static mut LIBRARY_MANAGER: Lazy<LibraryManager> = Lazy::new(|| LibraryManager::new());
50+
static LIBRARY_MANAGER: Lazy<LibraryManager> = Lazy::new(|| LibraryManager::new());
5151

5252
use jni::{
5353
objects::{JClass, JObject, JString},
@@ -60,6 +60,20 @@ use parking_lot::{RwLock, RwLockReadGuard};
6060

6161
use std::{collections::HashMap, error::Error, fmt::Display};
6262

63+
fn load_library(path: String) -> Result<Library, libloading::Error> {
64+
unsafe {
65+
// Load and initialize library
66+
#[cfg(target_os = "linux")]
67+
let library: Library = {
68+
// Load library with `RTLD_NOW | RTLD_NODELETE` to fix a SIGSEGV
69+
::libloading::os::unix::Library::open(Some(&path), 0x2 | 0x1000)?.into()
70+
};
71+
#[cfg(not(target_os = "linux"))]
72+
let library = Library::new(&path)?;
73+
Ok(library)
74+
}
75+
}
76+
6377
#[no_mangle]
6478
pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
6579
mut env: JNIEnv<'a>,
@@ -71,32 +85,30 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
7185
.unwrap()
7286
.to_string_lossy()
7387
.to_string();
74-
unsafe {
75-
let lib = libloading::Library::new(&file);
76-
if let Err(e) = &lib {
77-
throw_libloading_error(&mut env, &file, e);
78-
return;
79-
}
80-
let lib = lib.unwrap();
81-
let parts = file
82-
.split(std::path::MAIN_SEPARATOR)
83-
.map(|f| f.to_string())
84-
.collect::<Vec<String>>();
85-
let mut name = parts.get(parts.len() - 1).unwrap().clone();
86-
#[cfg(target_os = "windows")]
87-
{
88-
name = name.replace(".dll", "");
89-
}
90-
#[cfg(target_os = "macos")]
91-
{
92-
name = name.replace(".dylib", "");
93-
}
94-
#[cfg(target_os = "linux")]
95-
{
96-
name = name.replace(".so", "");
97-
}
98-
LIBRARY_MANAGER.push_lib(name, (lib, true));
88+
let lib = load_library(file.clone());
89+
if let Err(e) = &lib {
90+
throw_libloading_error(&mut env, &file, e);
91+
return;
92+
}
93+
let lib = lib.unwrap();
94+
let parts = file
95+
.split(std::path::MAIN_SEPARATOR)
96+
.map(|f| f.to_string())
97+
.collect::<Vec<String>>();
98+
let mut name = parts.get(parts.len() - 1).unwrap().clone();
99+
#[cfg(target_os = "windows")]
100+
{
101+
name = name.replace(".dll", "");
102+
}
103+
#[cfg(target_os = "macos")]
104+
{
105+
name = name.replace(".dylib", "");
106+
}
107+
#[cfg(target_os = "linux")]
108+
{
109+
name = name.replace(".so", "");
99110
}
111+
LIBRARY_MANAGER.push_lib(name, (lib, true));
100112
}
101113

102114
#[no_mangle]
@@ -110,9 +122,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_enablePlugin<'a>(
110122
.unwrap()
111123
.to_string_lossy()
112124
.to_string();
113-
unsafe {
114-
LIBRARY_MANAGER.set_enabled(file, true);
115-
}
125+
LIBRARY_MANAGER.set_enabled(file, true);
116126
}
117127

118128
#[no_mangle]
@@ -126,17 +136,15 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_disablePlugin<'a>(
126136
.unwrap()
127137
.to_string_lossy()
128138
.to_string();
129-
unsafe {
130-
LIBRARY_MANAGER.set_enabled(file, false);
131-
}
139+
LIBRARY_MANAGER.set_enabled(file, false);
132140
}
133141

134142
#[no_mangle]
135143
pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryNames<'a>(
136144
env: JNIEnv<'a>,
137145
_obj: JObject,
138146
) -> JString<'a> {
139-
unsafe { env.new_string(LIBRARY_MANAGER.library_names()).unwrap() }
147+
env.new_string(LIBRARY_MANAGER.library_names()).unwrap()
140148
}
141149

142150
#[no_mangle]
@@ -155,7 +163,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
155163
.to_string_lossy()
156164
.to_string();
157165

158-
let manager = unsafe { LIBRARY_MANAGER.libraries() };
166+
let manager = LIBRARY_MANAGER.libraries();
159167
let lib = manager.get(&libname).unwrap().library();
160168
let func: Result<
161169
libloading::Symbol<unsafe extern "C" fn(JNIEnv<'_>, JObject<'_>)>,
@@ -195,7 +203,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
195203
.to_string_lossy()
196204
.to_string();
197205

198-
let manager = unsafe { LIBRARY_MANAGER.libraries() };
206+
let manager = LIBRARY_MANAGER.libraries();
199207
let lib = unwrap_option_or_java_error(
200208
&mut env,
201209
&libname,
@@ -245,7 +253,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
245253
.to_string_lossy()
246254
.to_string();
247255

248-
let manager = unsafe { LIBRARY_MANAGER.libraries() };
256+
let manager = LIBRARY_MANAGER.libraries();
249257
let lib = unwrap_option_or_java_error(
250258
&mut env,
251259
&libname,
@@ -326,7 +334,6 @@ where
326334
match opt {
327335
Some(v) => v,
328336
None => {
329-
panic!();
330337
throw_with_error(
331338
&mut env,
332339
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException",

0 commit comments

Comments
 (0)