Add failing test for overriding directly linked CUDA dll

This commit is contained in:
Andrzej Janik
2021-11-30 01:25:51 +01:00
parent 3558a0a65c
commit fd1c13560f
2 changed files with 24 additions and 20 deletions

View File

@ -1,13 +1,23 @@
use std::{env, io, process::Command}; use std::{env, io, path::PathBuf, process::Command};
#[test] #[test]
fn direct_cuinit() -> io::Result<()> { fn direct_cuinit() -> io::Result<()> {
let zluda_with_exe = PathBuf::from(env!("CARGO_BIN_EXE_zluda_with"));
let mut zluda_redirect_dll = zluda_with_exe.parent().unwrap().to_path_buf();
zluda_redirect_dll.push("zluda_redirect.dll");
let helpers_dir = env!("HELPERS_OUT_DIR"); let helpers_dir = env!("HELPERS_OUT_DIR");
let mut main_exe = Command::new(format!( let exe_under_test = format!(
"{}{}direct_cuinit.exe", "{}{}direct_cuinit.exe",
helpers_dir, helpers_dir,
std::path::MAIN_SEPARATOR std::path::MAIN_SEPARATOR
)); );
assert!(main_exe.status()?.success()); let mut test_cmd = Command::new(&zluda_with_exe);
test_cmd
.arg(&zluda_redirect_dll)
.arg("--")
.arg(&exe_under_test);
let test_output = test_cmd.output()?;
let stderr_text = String::from_utf8(test_output.stderr).unwrap();
assert!(stderr_text.contains("ZLUDA_DUMP"));
Ok(()) Ok(())
} }

View File

@ -361,7 +361,7 @@ struct DetourDetachGuard {
state: DetourUndoState, state: DetourUndoState,
suspended_threads: Vec<*mut c_void>, suspended_threads: Vec<*mut c_void>,
// First element is the original fn, second is the new fn // First element is the original fn, second is the new fn
overriden_functions: Vec<(*mut *mut c_void, *mut c_void)>, overriden_functions: Vec<(*mut c_void, *mut c_void)>,
} }
impl DetourDetachGuard { impl DetourDetachGuard {
@ -371,7 +371,7 @@ impl DetourDetachGuard {
// also get overriden, so for example ZludaLoadLibraryExW ends calling // also get overriden, so for example ZludaLoadLibraryExW ends calling
// itself recursively until stack overflow exception occurs // itself recursively until stack overflow exception occurs
unsafe fn detour_functions<'a>( unsafe fn detour_functions<'a>(
override_fn_pairs: Vec<(*mut *mut c_void, *mut c_void)>, override_fn_pairs: Vec<(*mut c_void, *mut c_void)>,
) -> Option<Self> { ) -> Option<Self> {
let mut result = DetourDetachGuard { let mut result = DetourDetachGuard {
state: DetourUndoState::DoNothing, state: DetourUndoState::DoNothing,
@ -391,29 +391,23 @@ impl DetourDetachGuard {
} }
} }
result.overriden_functions.extend_from_slice(&[ result.overriden_functions.extend_from_slice(&[
(CREATE_PROCESS_A as _, ZludaCreateProcessA as _),
(CREATE_PROCESS_W as _, ZludaCreateProcessW as _),
( (
&mut CREATE_PROCESS_A as *mut _ as _, CREATE_PROCESS_AS_USER_W as _,
ZludaCreateProcessA as _,
),
(
&mut CREATE_PROCESS_W as *mut _ as _,
ZludaCreateProcessW as _,
),
(
&mut CREATE_PROCESS_AS_USER_W as *mut _ as _,
ZludaCreateProcessAsUserW as _, ZludaCreateProcessAsUserW as _,
), ),
( (
&mut CREATE_PROCESS_WITH_LOGON_W as *mut _ as _, CREATE_PROCESS_WITH_LOGON_W as _,
ZludaCreateProcessWithLogonW as _, ZludaCreateProcessWithLogonW as _,
), ),
( (
&mut CREATE_PROCESS_WITH_TOKEN_W as *mut _ as _, CREATE_PROCESS_WITH_TOKEN_W as _,
ZludaCreateProcessWithTokenW as _, ZludaCreateProcessWithTokenW as _,
), ),
]); ]);
for (original_fn, new_fn) in result.overriden_functions.iter().copied() { for (original_fn, new_fn) in result.overriden_functions.iter_mut() {
if DetourAttach(original_fn, new_fn) != NO_ERROR as i32 { if DetourAttach(original_fn as *mut _, *new_fn) != NO_ERROR as i32 {
return None; return None;
} }
} }
@ -708,7 +702,7 @@ unsafe fn attach_cuinit(nvcuda_mod: HMODULE) -> Option<DetourDetachGuard> {
cuda_unsupported as _ cuda_unsupported as _
} }
}; };
override_fn_pairs.push((&mut original_fn_address as *mut _ as _, override_fn_address)); override_fn_pairs.push((original_fn_address as _, override_fn_address));
} }
DetourDetachGuard::detour_functions(override_fn_pairs) DetourDetachGuard::detour_functions(override_fn_pairs)
} }