@@ -28,7 +28,7 @@ impl LibraryManager {
28
28
loaded_libraries : RwLock :: new ( HashMap :: new ( ) ) ,
29
29
}
30
30
}
31
- fn push_lib ( & mut self , libname : String , lib : ( Library , bool ) ) {
31
+ fn push_lib ( & self , libname : String , lib : ( Library , bool ) ) {
32
32
let libs = & mut self . loaded_libraries . write ( ) ;
33
33
libs. insert ( libname, SwitchableLibrary :: new ( lib. 0 , lib. 1 ) ) ;
34
34
}
@@ -47,7 +47,7 @@ impl LibraryManager {
47
47
}
48
48
}
49
49
50
- static mut LIBRARY_MANAGER : Lazy < LibraryManager > = Lazy :: new ( || LibraryManager :: new ( ) ) ;
50
+ static LIBRARY_MANAGER : Lazy < LibraryManager > = Lazy :: new ( || LibraryManager :: new ( ) ) ;
51
51
52
52
use jni:: {
53
53
objects:: { JClass , JObject , JString } ,
@@ -60,6 +60,20 @@ use parking_lot::{RwLock, RwLockReadGuard};
60
60
61
61
use std:: { collections:: HashMap , error:: Error , fmt:: Display } ;
62
62
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
+
63
77
#[ no_mangle]
64
78
pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin < ' a > (
65
79
mut env : JNIEnv < ' a > ,
@@ -71,32 +85,30 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_loadPlugin<'a>(
71
85
. unwrap ( )
72
86
. to_string_lossy ( )
73
87
. 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" , "" ) ;
99
110
}
111
+ LIBRARY_MANAGER . push_lib ( name, ( lib, true ) ) ;
100
112
}
101
113
102
114
#[ no_mangle]
@@ -110,9 +122,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_enablePlugin<'a>(
110
122
. unwrap ( )
111
123
. to_string_lossy ( )
112
124
. to_string ( ) ;
113
- unsafe {
114
- LIBRARY_MANAGER . set_enabled ( file, true ) ;
115
- }
125
+ LIBRARY_MANAGER . set_enabled ( file, true ) ;
116
126
}
117
127
118
128
#[ no_mangle]
@@ -126,17 +136,15 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_disablePlugin<'a>(
126
136
. unwrap ( )
127
137
. to_string_lossy ( )
128
138
. to_string ( ) ;
129
- unsafe {
130
- LIBRARY_MANAGER . set_enabled ( file, false ) ;
131
- }
139
+ LIBRARY_MANAGER . set_enabled ( file, false ) ;
132
140
}
133
141
134
142
#[ no_mangle]
135
143
pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryNames < ' a > (
136
144
env : JNIEnv < ' a > ,
137
145
_obj : JObject ,
138
146
) -> JString < ' a > {
139
- unsafe { env. new_string ( LIBRARY_MANAGER . library_names ( ) ) . unwrap ( ) }
147
+ env. new_string ( LIBRARY_MANAGER . library_names ( ) ) . unwrap ( )
140
148
}
141
149
142
150
#[ no_mangle]
@@ -155,7 +163,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_libraryHasFunction<'a>(
155
163
. to_string_lossy ( )
156
164
. to_string ( ) ;
157
165
158
- let manager = unsafe { LIBRARY_MANAGER . libraries ( ) } ;
166
+ let manager = LIBRARY_MANAGER . libraries ( ) ;
159
167
let lib = manager. get ( & libname) . unwrap ( ) . library ( ) ;
160
168
let func: Result <
161
169
libloading:: Symbol < unsafe extern "C" fn ( JNIEnv < ' _ > , JObject < ' _ > ) > ,
@@ -195,7 +203,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_sendEvent<'a>(
195
203
. to_string_lossy ( )
196
204
. to_string ( ) ;
197
205
198
- let manager = unsafe { LIBRARY_MANAGER . libraries ( ) } ;
206
+ let manager = LIBRARY_MANAGER . libraries ( ) ;
199
207
let lib = unwrap_option_or_java_error (
200
208
& mut env,
201
209
& libname,
@@ -245,7 +253,7 @@ pub extern "system" fn Java_net_ioixd_blackbox_Native_execute<'a>(
245
253
. to_string_lossy ( )
246
254
. to_string ( ) ;
247
255
248
- let manager = unsafe { LIBRARY_MANAGER . libraries ( ) } ;
256
+ let manager = LIBRARY_MANAGER . libraries ( ) ;
249
257
let lib = unwrap_option_or_java_error (
250
258
& mut env,
251
259
& libname,
@@ -326,7 +334,6 @@ where
326
334
match opt {
327
335
Some ( v) => v,
328
336
None => {
329
- panic ! ( ) ;
330
337
throw_with_error (
331
338
& mut env,
332
339
"net/ioixd/blackbox/exceptions/NativeLibrarySymbolLoadException" ,
0 commit comments