libloading 0.9.0

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use libloading::{Library, Symbol};
use std::os::raw::c_void;

const TARGET_DIR: Option<&'static str> = option_env!("CARGO_TARGET_DIR");
const TARGET_TMPDIR: Option<&'static str> = option_env!("CARGO_TARGET_TMPDIR");

fn lib_path() -> std::path::PathBuf {
    [
        TARGET_TMPDIR.unwrap_or(TARGET_DIR.unwrap_or("target")),
        "libtest_helpers.module",
    ]
    .iter()
    .collect()
}

fn make_helpers() {
    static ONCE: ::std::sync::Once = ::std::sync::Once::new();
    ONCE.call_once(|| {
        if std::env::var_os("PRECOMPILED_TEST_HELPER").is_some() {
            //I can't be asked to make rustc work in wine.
            //I can call it myself from my linux host and then just move the file here this allows me to skip this.
            eprintln!("WILL NOT COMPILE TEST HELPERS, PROGRAM WILL ASSUME THAT {} EXISTS AND WAS EXTERNALLY PRE COMPILED", lib_path().display());
            return;
        }

        let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
        let mut cmd = ::std::process::Command::new(rustc);
        cmd.arg("src/test_helpers.rs").arg("-o").arg(lib_path());
        if let Some(target) = std::env::var_os("TARGET") {
            cmd.arg("--target").arg(target);
        } else {
            eprintln!("WARNING: $TARGET NOT SPECIFIED! BUILDING HELPER MODULE FOR NATIVE TARGET.");
        }
        assert!(cmd
            .status()
            .expect("could not compile the test helpers!")
            .success());
    });
}

#[cfg(not(windows))]
fn is_wine() -> bool {
    false
}

#[cfg(windows)]
fn is_wine() -> bool {
    unsafe {
        //This detects wine, the linux runtime for windows programs.
        //Wine exposes the symbol wine_get_version in ntdll.dll; naturally, this symbol is absent on actual windows.
        let lib = Library::new("ntdll.dll").expect("open library");
        let wine: Result<Symbol<extern "C" fn() -> i32>, _> = lib.get("wine_get_version");
        if wine.is_ok() {
            return true;
        }
    }

    false
}

#[test]
#[cfg(feature = "std")]
fn test_id_u32() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let f: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(b"test_identity_u32\0").unwrap();
        assert_eq!(42, f(42));
    }
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_osstring() {
    as_filename_test::<std::ffi::OsString>(lib_path().into_os_string(), "potato\0beetroot".into());
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_osstr() {
    let with_nulls = std::ffi::OsStr::new("hazelnut\0peanut");
    as_filename_test::<&std::ffi::OsStr>(lib_path().as_os_str(), with_nulls);
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_pathbuf() {
    as_filename_test::<std::path::PathBuf>(lib_path(), "orange\0grape".into());
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_path() {
    as_filename_test::<&std::path::Path>(&*lib_path(), std::path::Path::new("peach\0mango"));
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_str() {
    let path = lib_path();
    if let Some(p) = path.to_str() {
        as_filename_test::<&str>(p, "kiwi\0peach\0");
    }
}

#[cfg(feature = "std")]
#[test]
fn test_as_filename_string() {
    let path = lib_path();
    if let Some(p) = path.to_str() {
        as_filename_test::<String>(p.to_string(), "apple\0banana".to_string());
    }
}

#[cfg(feature = "std")]
fn as_filename_test<T: libloading::AsFilename>(path: T, with_interior_nulls: T) {
    make_helpers();
    unsafe {
        assert!(matches!(
            Library::new(with_interior_nulls).unwrap_err(),
            libloading::Error::InteriorZeroElements,
        ));
        let lib = Library::new(path).unwrap();
        let f: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(b"test_identity_u32\0").unwrap();
        assert_eq!(42, f(42));
    }
}

#[cfg(feature = "std")]
#[test]
fn test_as_symbol_name_string() {
    as_symbol_name_test::<String>("test_identity_u32".to_string());
    as_symbol_name_test::<String>("test_identity_u32\0".to_string());
    as_symbol_name_test_interior_nulls::<String>("test_iden\0tity_u32".to_string());
}

#[cfg(feature = "std")]
#[test]
fn test_as_symbol_name_str() {
    as_symbol_name_test::<&str>("test_identity_u32");
    as_symbol_name_test::<&str>("test_identity_u32\0");
    as_symbol_name_test_interior_nulls::<&str>("test_iden\0tity_u32\0");
}

#[cfg(feature = "std")]
#[test]
fn test_as_symbol_name_cstr() {
    as_symbol_name_test::<&std::ffi::CStr>(c"test_identity_u32");
}

#[cfg(feature = "std")]
#[test]
fn test_as_symbol_name_cstring() {
    as_symbol_name_test::<std::ffi::CString>(c"test_identity_u32".to_owned());
}

#[cfg(feature = "std")]
#[test]
fn test_as_symbol_name_bytes() {
    as_symbol_name_test::<&[u8]>(b"test_identity_u32");
    as_symbol_name_test::<&[u8]>(b"test_identity_u32\0");
    as_symbol_name_test::<&[u8; 18]>(b"test_identity_u32\0");
    as_symbol_name_test_interior_nulls::<&[u8]>(b"test_identity\0_u32");
    as_symbol_name_test_interior_nulls::<&[u8]>(b"test\0_identity_u32");
    as_symbol_name_test_interior_nulls::<&[u8; 19]>(b"test_iden\0tity_u32\0");
}

#[cfg(feature = "std")]
fn as_symbol_name_test<T: libloading::AsSymbolName>(symbol: T) {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let f: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(symbol).unwrap();
        assert_eq!(42, f(42));
    }
}

#[cfg(feature = "std")]
fn as_symbol_name_test_interior_nulls<T: libloading::AsSymbolName>(symbol: T) {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        assert!(matches!(
            lib.get::<unsafe extern "C" fn(u32) -> u32>(symbol),
            Err(libloading::Error::InteriorZeroElements),
        ));
    }
}

#[test]
#[cfg(feature = "std")]
fn test_try_into_ptr() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let f: Symbol<unsafe extern "C" fn(u32) -> u32> = lib.get(b"test_identity_u32\0").unwrap();
        let ptr: *mut c_void = f.try_as_raw_ptr().unwrap();
        assert!(!ptr.is_null());
        let ptr_casted: extern "C" fn(u32) -> u32 = std::mem::transmute(ptr);
        assert_eq!(42, ptr_casted(42));
    }
}

#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
struct S {
    a: u64,
    b: u32,
    c: u16,
    d: u8,
}

#[test]
#[cfg(feature = "std")]
fn test_id_struct() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let f: Symbol<unsafe extern "C" fn(S) -> S> = lib.get(b"test_identity_struct\0").unwrap();
        assert_eq!(
            S {
                a: 1,
                b: 2,
                c: 3,
                d: 4
            },
            f(S {
                a: 1,
                b: 2,
                c: 3,
                d: 4
            })
        );
    }
}

#[test]
#[allow(unpredictable_function_pointer_comparisons)]
#[cfg(feature = "std")]
fn test_0_no_0() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let f: Symbol<unsafe extern "C" fn(S) -> S> = lib.get(b"test_identity_struct\0").unwrap();
        let f2: Symbol<unsafe extern "C" fn(S) -> S> = lib.get(b"test_identity_struct").unwrap();

        assert_eq!(*f, *f2);
    }
}

#[test]
fn wrong_name_fails() {
    unsafe {
        Library::new("target/this_location_is_definitely_non existent:^~")
            .err()
            .unwrap();
    }
}

#[test]
#[cfg(feature = "std")]
fn missing_symbol_fails() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        lib.get::<*mut ()>(b"test_does_not_exist").err().unwrap();
        lib.get::<*mut ()>(b"test_does_not_exist\0").err().unwrap();
    }
}

#[test]
#[cfg(feature = "std")]
fn interior_null_fails() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        lib.get::<*mut ()>(b"test_does\0_not_exist").err().unwrap();
        lib.get::<*mut ()>("test_does\0_not_exist").err().unwrap();
        lib.get::<*mut ()>(b"test\0_does_not_exist\0")
            .err()
            .unwrap();
        lib.get::<*mut ()>("test_does\0_not_exist\0").err().unwrap();
    }
}

#[test]
#[cfg(feature = "std")]
fn test_incompatible_type() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        assert!(match lib.get::<()>(b"test_identity_u32\0") {
            Err(libloading::Error::IncompatibleSize) => true,
            _ => false,
        })
    }
}

#[test]
#[cfg(feature = "std")]
fn test_incompatible_type_named_fn() {
    make_helpers();
    unsafe fn get<'a, T>(l: &'a Library, _: T) -> Result<Symbol<'a, T>, libloading::Error> {
        l.get::<T>(b"test_identity_u32\0")
    }
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        assert!(match get(&lib, test_incompatible_type_named_fn) {
            Err(libloading::Error::IncompatibleSize) => true,
            _ => false,
        })
    }
}

#[test]
#[cfg(feature = "std")]
fn test_static_u32() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let var: Symbol<*mut u32> = lib.get(b"TEST_STATIC_U32\0").unwrap();
        **var = 42;
        let help: Symbol<unsafe extern "C" fn() -> u32> =
            lib.get(b"test_get_static_u32\0").unwrap();
        assert_eq!(42, help());
    }
}

#[test]
#[cfg(feature = "std")]
fn test_static_ptr() {
    make_helpers();
    unsafe {
        let lib = Library::new(lib_path()).unwrap();
        let var: Symbol<*mut *mut ()> = lib.get(b"TEST_STATIC_PTR\0").unwrap();
        **var = *var as *mut _;
        let works: Symbol<unsafe extern "C" fn() -> bool> =
            lib.get(b"test_check_static_ptr\0").unwrap();
        assert!(works());
    }
}

#[test]
// Something about i686-pc-windows-gnu, makes dll initialisation code call abort when it is loaded
// and unloaded many times. So far it seems like an issue with mingw, not libloading, so ignoring
// the target. Especially since it is very unlikely to be fixed given the state of support its
// support.
#[cfg(not(all(target_arch = "x86", target_os = "windows", target_env = "gnu")))]
// Cygwin returns errors on `close`.
#[cfg(not(target_os = "cygwin"))]
#[cfg(feature = "std")]
fn manual_close_many_times() {
    if is_wine() {
        // The wine runtime to run windows programs under linux
        // will run out of thread local storage indices and fail this test.
        eprintln!("DETECTED WINE RUNTIME, WILL SKIP THIS TEST");
        return;
    }

    make_helpers();
    let join_handles: Vec<_> = (0..16)
        .map(|_| {
            std::thread::spawn(|| unsafe {
                for _ in 0..10000 {
                    let lib = Library::new(lib_path()).expect("open library");
                    let _: Symbol<unsafe extern "C" fn(u32) -> u32> =
                        lib.get(b"test_identity_u32").expect("get fn");
                    lib.close().expect("close is successful");
                }
            })
        })
        .collect();
    for handle in join_handles {
        handle.join().expect("thread should succeed");
    }
}

#[cfg(unix)]
#[cfg(feature = "std")]
#[test]
fn library_this_get() {
    use libloading::os::unix::Library;
    make_helpers();
    // SAFE: functions are never called
    unsafe {
        let _lib = Library::new(lib_path()).unwrap();
        let this = Library::this();
        // Library we loaded in `_lib` (should be RTLD_LOCAL).
        assert!(this
            .get::<unsafe extern "C" fn()>(b"test_identity_u32")
            .is_err());
        // Something obscure from libc...
        // Cygwin behaves like Windows so ignore it.
        #[cfg(not(target_os = "cygwin"))]
        assert!(this.get::<unsafe extern "C" fn()>(b"freopen").is_ok());
    }
}

#[cfg(windows)]
#[cfg(feature = "std")]
#[test]
fn library_this() {
    use libloading::os::windows::Library;
    make_helpers();
    unsafe {
        // SAFE: well-known library without initialisers is loaded.
        let _lib = Library::new(lib_path()).unwrap();
        let this = Library::this().expect("this library");
        // SAFE: functions are never called.
        // Library we loaded in `_lib`.
        assert!(this
            .get::<unsafe extern "C" fn()>(b"test_identity_u32")
            .is_err());
        // Something "obscure" from kernel32...
        assert!(this.get::<unsafe extern "C" fn()>(b"GetLastError").is_err());
    }
}

#[cfg(windows)]
#[test]
fn works_getlasterror() {
    use libloading::os::windows::{Library, Symbol};
    use windows_sys::Win32::Foundation::{GetLastError, SetLastError};

    unsafe {
        let lib = Library::new("kernel32.dll").unwrap();
        let gle: Symbol<unsafe extern "system" fn() -> u32> = lib.get(b"GetLastError").unwrap();
        SetLastError(42);
        assert_eq!(GetLastError(), gle())
    }
}

#[cfg(windows)]
#[test]
fn works_getlasterror0() {
    use libloading::os::windows::{Library, Symbol};
    use windows_sys::Win32::Foundation::{GetLastError, SetLastError};

    unsafe {
        let lib = Library::new("kernel32.dll").unwrap();
        let gle: Symbol<unsafe extern "system" fn() -> u32> = lib.get(b"GetLastError\0").unwrap();
        SetLastError(42);
        assert_eq!(GetLastError(), gle())
    }
}

#[cfg(windows)]
#[test]
fn works_pin_module() {
    use libloading::os::windows::Library;

    unsafe {
        let lib = Library::new("kernel32.dll").unwrap();
        lib.pin().unwrap();
    }
}

#[cfg(windows)]
#[test]
fn library_open_already_loaded() {
    use libloading::os::windows::Library;

    // Present on Windows systems and NOT used by any other tests to prevent races.
    const LIBPATH: &str = "Msftedit.dll";

    // Not loaded yet.
    assert!(match Library::open_already_loaded(LIBPATH) {
        Err(libloading::Error::GetModuleHandleExW { .. }) => true,
        _ => false,
    });

    unsafe {
        let _lib = Library::new(LIBPATH).unwrap();
        // Loaded now.
        assert!(Library::open_already_loaded(LIBPATH).is_ok());
    }
}