Add more tests for CUDA redirection

This commit is contained in:
Andrzej Janik 2021-12-02 23:47:37 +01:00
parent 400feaf015
commit 26bf0eeaf2
5 changed files with 65 additions and 6 deletions

View file

@ -21,6 +21,7 @@ fn main() -> Result<(), VarError> {
helpers_dir.push("tests");
helpers_dir.push("helpers");
let helpers_dir_as_string = helpers_dir.to_string_lossy();
println!("cargo:rerun-if-changed={}", helpers_dir_as_string);
for rust_file in fs::read_dir(&helpers_dir).unwrap().filter_map(rust_file) {
let full_file_path = format!(
"{}{}{}",
@ -28,7 +29,6 @@ fn main() -> Result<(), VarError> {
path::MAIN_SEPARATOR,
rust_file
);
println!("cargo:rerun-if-changed={}", full_file_path);
let mut rustc_cmd = Command::new(&*rustc_exe);
if debug {
rustc_cmd.arg("-g");

View file

@ -0,0 +1,10 @@
#![crate_type = "cdylib"]
extern "system" {
fn cuInit(flags: u32) -> u32;
}
#[no_mangle]
unsafe extern "system" fn do_cuinit(flags: u32) -> u32 {
cuInit(flags)
}

View file

@ -0,0 +1,23 @@
#![crate_type = "bin"]
use std::ffi::c_void;
use std::mem;
use std::env;
use std::path::PathBuf;
use std::ffi::CString;
extern "system" {
fn LoadLibraryA(lpFileName: *const i8) -> *mut c_void;
fn GetProcAddress(hModule: *mut c_void, lpProcName: *const u8) -> *mut c_void;
}
fn main() {
let current_exe = env::current_exe().unwrap();
let mut dll = PathBuf::from(current_exe.parent().unwrap());
dll.push("do_cuinit.dll");
let dll_cstring = CString::new(dll.to_str().unwrap()).unwrap();
let nvcuda = unsafe { LoadLibraryA(dll_cstring.as_ptr()) };
let cuInit = unsafe { GetProcAddress(nvcuda, b"do_cuinit\0".as_ptr()) };
let cuInit = unsafe { mem::transmute::<_, unsafe extern "system" fn(u32) -> u32>(cuInit) };
unsafe { cuInit(0) };
}

View file

@ -0,0 +1,16 @@
#![crate_type = "bin"]
use std::ffi::c_void;
use std::mem;
extern "system" {
fn LoadLibraryA(lpFileName: *const u8) -> *mut c_void;
fn GetProcAddress(hModule: *mut c_void, lpProcName: *const u8) -> *mut c_void;
}
fn main() {
let nvcuda = unsafe { LoadLibraryA(b"C:\\Windows\\System32\\nvcuda.dll\0".as_ptr()) };
let cuInit = unsafe { GetProcAddress(nvcuda, b"cuInit\0".as_ptr()) };
let cuInit = unsafe { mem::transmute::<_, unsafe extern "system" fn(u32) -> u32>(cuInit) };
unsafe { cuInit(0) };
}

View file

@ -2,15 +2,25 @@ use std::{env, io, path::PathBuf, process::Command};
#[test]
fn direct_cuinit() -> io::Result<()> {
run_process_and_check_for_zluda_dump("direct_cuinit")
}
#[test]
fn indirect_cuinit() -> io::Result<()> {
run_process_and_check_for_zluda_dump("indirect_cuinit")
}
#[test]
fn do_cuinit() -> io::Result<()> {
run_process_and_check_for_zluda_dump("do_cuinit_main")
}
fn run_process_and_check_for_zluda_dump(name: &'static str) -> io::Result<()> {
let zluda_with_exe = PathBuf::from(env!("CARGO_BIN_EXE_zluda_with"));
let mut zluda_dump_dll = zluda_with_exe.parent().unwrap().to_path_buf();
zluda_dump_dll.push("zluda_dump.dll");
let helpers_dir = env!("HELPERS_OUT_DIR");
let exe_under_test = format!(
"{}{}direct_cuinit.exe",
helpers_dir,
std::path::MAIN_SEPARATOR
);
let exe_under_test = format!("{}{}{}.exe", helpers_dir, std::path::MAIN_SEPARATOR, name);
let mut test_cmd = Command::new(&zluda_with_exe);
let test_cmd = test_cmd.arg(&zluda_dump_dll).arg("--").arg(&exe_under_test);
let test_output = test_cmd.output()?;