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
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ keywords = ["dlopen", "load", "shared", "dylib"]
categories = ["api-bindings"]
rust-version = "1.88.0"
edition = "2021"
autotests = false
include = ["Cargo.toml", "LICENSE", "README.mkd", "src/**/*.rs", "tests/**/*.rs", "tests/ordinals.def"]

[features]
default = ["std"]
Expand Down Expand Up @@ -42,3 +44,8 @@ unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(libloading_docs)',
'cfg(target_os, values("cygwin"))',
] }

[[test]]
path = "tests/lib.rs"
name = "integration-tests"
harness = true
9 changes: 7 additions & 2 deletions src/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This is a separate file containing helpers for tests of this library. It is built into a
//! dynamic library by the build.rs script.
#![crate_type="cdylib"]
#![crate_type = "cdylib"]

#[no_mangle]
pub static mut TEST_STATIC_U32: u32 = 0;
Expand All @@ -18,7 +18,7 @@ pub struct S {
a: u64,
b: u32,
c: u16,
d: u8
d: u8,
}

#[no_mangle]
Expand All @@ -35,3 +35,8 @@ pub unsafe extern "C" fn test_get_static_u32() -> u32 {
pub unsafe extern "C" fn test_check_static_ptr() -> bool {
TEST_STATIC_PTR == (&mut TEST_STATIC_PTR as *mut *mut _ as *mut _)
}

#[unsafe(no_mangle)]
pub extern "C" fn test_ordinals() -> *const std::ffi::c_char {
"bunny\0".as_ptr().cast()
}
11 changes: 7 additions & 4 deletions tests/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
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");
const MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");

fn lib_path() -> std::path::PathBuf {
pub fn lib_path() -> std::path::PathBuf {
[
TARGET_TMPDIR.unwrap_or(TARGET_DIR.unwrap_or("target")),
"libtest_helpers.module",
Expand All @@ -13,7 +13,7 @@ fn lib_path() -> std::path::PathBuf {
.collect()
}

fn make_helpers() {
pub 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() {
Expand All @@ -31,6 +31,9 @@ fn make_helpers() {
} else {
eprintln!("WARNING: $TARGET NOT SPECIFIED! BUILDING HELPER MODULE FOR NATIVE TARGET.");
}
if cfg!(target_env = "msvc") {
cmd.arg(format!("-Clink-arg=/DEF:{MANIFEST_DIR}/tests/ordinals.def"));
}
assert!(cmd
.status()
.expect("could not compile the test helpers!")
Expand Down Expand Up @@ -194,7 +197,7 @@ fn test_try_into_ptr() {
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();
let ptr: *mut std::ffi::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));
Expand Down
5 changes: 5 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mod constants;
mod functions;
mod library_filename;
mod markers;
mod windows;
Binary file removed tests/nagisa32.dll
Binary file not shown.
Binary file removed tests/nagisa64.dll
Binary file not shown.
9 changes: 9 additions & 0 deletions tests/ordinals.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
LIBRARY test_helpers
EXPORTS
test_ordinals @1 NONAME
test_identity_u32 @2
test_identity_struct @3
test_check_static_ptr @4
test_get_static_u32 @5
TEST_STATIC_U32 @6
TEST_STATIC_PTR @7
29 changes: 9 additions & 20 deletions tests/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,15 @@ extern crate libloading;
use libloading::os::windows::*;
use std::ffi::CStr;
use std::os::raw::c_void;
// The ordinal DLL contains exactly one function (other than DllMain, that is) with ordinal number
// 1. This function has the sugnature `fn() -> *const c_char` and returns a string "bunny\0" (in
// reference to WindowsBunny).
//
// Both x86_64 and x86 versions of the .dll are functionally the same. Ideally we would compile the
// dlls with well known ordinals from our own testing helpers library, but rustc does not allow
// specifying a custom .def file (https://github.com/rust-lang/rust/issues/35089)
//
// The DLLs were kindly compiled by WindowsBunny (aka. @retep998).

#[cfg(target_arch = "x86")]
fn load_ordinal_lib() -> Library {
unsafe { Library::new("tests/nagisa32.dll").expect("nagisa32.dll") }
}

#[cfg(target_arch = "x86_64")]
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
fn load_ordinal_lib() -> Library {
unsafe { Library::new("tests/nagisa64.dll").expect("nagisa64.dll") }
let path = super::functions::lib_path();
super::functions::make_helpers();
unsafe { Library::new(path.display().to_string()).expect("Windows test dll not found") }
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_env = "msvc"))]
#[test]
fn test_ordinal() {
let lib = load_ordinal_lib();
Expand All @@ -33,7 +21,7 @@ fn test_ordinal() {
}
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_env = "msvc"))]
#[test]
fn test_try_into_ptr() {
let lib = load_ordinal_lib();
Expand All @@ -44,12 +32,13 @@ fn test_try_into_ptr() {
}
}

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), target_env = "msvc"))]
#[test]
fn test_ordinal_missing_fails() {
let lib = load_ordinal_lib();
unsafe {
let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(2);
// there are a few other symbols in the test DLL
let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(8);
r.err().unwrap();
let r: Result<Symbol<unsafe fn() -> *const i8>, _> = lib.get_ordinal(!0);
r.err().unwrap();
Expand Down