|
@@ -11,20 +11,38 @@ use std::ffi::{CStr, c_void};
|
|
|
|
|
|
|
|
use crate::{HleModule, HostPolicy, hle_export, registry, runtime};
|
|
use crate::{HleModule, HostPolicy, hle_export, registry, runtime};
|
|
|
|
|
|
|
|
-/// `GetModuleHandleW(NULL)` must return the title's image, not our exe.
|
|
|
|
|
|
|
+/// `GetModuleHandleW(NULL)` must return the title's image, not our exe;
|
|
|
|
|
+/// title images and our console modules are found by name.
|
|
|
pub extern "system" fn GetModuleHandleW(name: *const u16) -> *mut c_void {
|
|
pub extern "system" fn GetModuleHandleW(name: *const u16) -> *mut c_void {
|
|
|
if name.is_null() {
|
|
if name.is_null() {
|
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
|
}
|
|
}
|
|
|
- host::get_module_handle_w(name)
|
|
|
|
|
|
|
+ find_module(&wide(name), false).unwrap_or_else(|| host::get_module_handle_w(name))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-/// `GetModuleHandleA(NULL)` must return the title's image, not our exe.
|
|
|
|
|
|
|
+/// See [`GetModuleHandleW`].
|
|
|
pub extern "system" fn GetModuleHandleA(name: *const u8) -> *mut c_void {
|
|
pub extern "system" fn GetModuleHandleA(name: *const u8) -> *mut c_void {
|
|
|
if name.is_null() {
|
|
if name.is_null() {
|
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
return runtime::main_image().unwrap_or(0) as *mut c_void;
|
|
|
}
|
|
}
|
|
|
- host::get_module_handle_a(name)
|
|
|
|
|
|
|
+ find_module(&narrow(name), false).unwrap_or_else(|| host::get_module_handle_a(name))
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// A NUL-terminated string from the title.
|
|
|
|
|
+fn narrow(text: *const u8) -> String {
|
|
|
|
|
+ // SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
+ unsafe { CStr::from_ptr(text.cast()) }
|
|
|
|
|
+ .to_string_lossy()
|
|
|
|
|
+ .into_owned()
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/// See [`narrow`].
|
|
|
|
|
+fn wide(text: *const u16) -> String {
|
|
|
|
|
+ // SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
+ unsafe {
|
|
|
|
|
+ let len = (0..).take_while(|&i| *text.add(i) != 0).count();
|
|
|
|
|
+ String::from_utf16_lossy(std::slice::from_raw_parts(text, len))
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// A title image's path is its original file, not our cache copy;
|
|
/// A title image's path is its original file, not our cache copy;
|
|
@@ -81,18 +99,17 @@ fn copy_truncated<T: Copy + Default>(text: &[T], out: &mut [T]) -> u32 {
|
|
|
const ERROR_INSUFFICIENT_BUFFER: u32 = 122;
|
|
const ERROR_INSUFFICIENT_BUFFER: u32 = 122;
|
|
|
|
|
|
|
|
/// Console-only DLLs we implement load as a pseudo handle (titles load
|
|
/// Console-only DLLs we implement load as a pseudo handle (titles load
|
|
|
-/// `xgameruntime.dll` this way); anything else loads on the host.
|
|
|
|
|
|
|
+/// `xgameruntime.dll` this way), the title's DLLs as the images we map
|
|
|
|
|
+/// (on first use if nothing imports them); anything else loads on the host.
|
|
|
pub extern "system" fn LoadLibraryExA(
|
|
pub extern "system" fn LoadLibraryExA(
|
|
|
name: *const u8,
|
|
name: *const u8,
|
|
|
file: *mut c_void,
|
|
file: *mut c_void,
|
|
|
flags: u32,
|
|
flags: u32,
|
|
|
) -> *mut c_void {
|
|
) -> *mut c_void {
|
|
|
- if !name.is_null() {
|
|
|
|
|
- // SAFETY: the title passes a NUL-terminated file name.
|
|
|
|
|
- let text = unsafe { CStr::from_ptr(name.cast()) }.to_string_lossy();
|
|
|
|
|
- if let Some(handle) = console_module(&text) {
|
|
|
|
|
- return handle;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if !name.is_null()
|
|
|
|
|
+ && let Some(handle) = find_module(&narrow(name), runs_code(flags))
|
|
|
|
|
+ {
|
|
|
|
|
+ return handle;
|
|
|
}
|
|
}
|
|
|
host::load_library_ex_a(name, file, flags)
|
|
host::load_library_ex_a(name, file, flags)
|
|
|
}
|
|
}
|
|
@@ -103,19 +120,21 @@ pub extern "system" fn LoadLibraryExW(
|
|
|
file: *mut c_void,
|
|
file: *mut c_void,
|
|
|
flags: u32,
|
|
flags: u32,
|
|
|
) -> *mut c_void {
|
|
) -> *mut c_void {
|
|
|
- if !name.is_null() {
|
|
|
|
|
- // SAFETY: the title passes a NUL-terminated file name.
|
|
|
|
|
- let text = unsafe {
|
|
|
|
|
- let len = (0..).take_while(|&i| *name.add(i) != 0).count();
|
|
|
|
|
- String::from_utf16_lossy(std::slice::from_raw_parts(name, len))
|
|
|
|
|
- };
|
|
|
|
|
- if let Some(handle) = console_module(&text) {
|
|
|
|
|
- return handle;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ if !name.is_null()
|
|
|
|
|
+ && let Some(handle) = find_module(&wide(name), runs_code(flags))
|
|
|
|
|
+ {
|
|
|
|
|
+ return handle;
|
|
|
}
|
|
}
|
|
|
host::load_library_ex_w(name, file, flags)
|
|
host::load_library_ex_w(name, file, flags)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// False for loads as data or resources (`DONT_RESOLVE_DLL_REFERENCES`,
|
|
|
|
|
+/// `LOAD_LIBRARY_AS_DATAFILE(_EXCLUSIVE)`, `AS_IMAGE_RESOURCE`): those run
|
|
|
|
|
+/// no code, so a title DLL we have not mapped can go to the host.
|
|
|
|
|
+fn runs_code(flags: u32) -> bool {
|
|
|
|
|
+ flags & (0x1 | 0x2 | 0x20 | 0x40) == 0
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
pub extern "system" fn LoadLibraryA(name: *const u8) -> *mut c_void {
|
|
pub extern "system" fn LoadLibraryA(name: *const u8) -> *mut c_void {
|
|
|
LoadLibraryExA(name, std::ptr::null_mut(), 0)
|
|
LoadLibraryExA(name, std::ptr::null_mut(), 0)
|
|
|
}
|
|
}
|
|
@@ -150,8 +169,12 @@ pub extern "system" fn GetProcAddress(module: *mut c_void, name: *const u8) -> *
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+/// Our pseudo modules and the title's images stay loaded: `LoadLibrary`
|
|
|
|
|
+/// took no reference on them, so the host's count must not drop.
|
|
|
pub extern "system" fn FreeLibrary(module: *mut c_void) -> i32 {
|
|
pub extern "system" fn FreeLibrary(module: *mut c_void) -> i32 {
|
|
|
- if registry().iter().any(|m| handle(m) == module) {
|
|
|
|
|
|
|
+ if registry().iter().any(|m| handle(m) == module)
|
|
|
|
|
+ || runtime::image_path(module as usize).is_some()
|
|
|
|
|
+ {
|
|
|
return 1;
|
|
return 1;
|
|
|
}
|
|
}
|
|
|
host::free_library(module)
|
|
host::free_library(module)
|
|
@@ -160,21 +183,14 @@ pub extern "system" fn FreeLibrary(module: *mut c_void) -> i32 {
|
|
|
/// Title debug output goes to our log (no debugger needs to be attached).
|
|
/// Title debug output goes to our log (no debugger needs to be attached).
|
|
|
pub extern "system" fn OutputDebugStringA(text: *const u8) {
|
|
pub extern "system" fn OutputDebugStringA(text: *const u8) {
|
|
|
if !text.is_null() {
|
|
if !text.is_null() {
|
|
|
- // SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
- let text = unsafe { CStr::from_ptr(text.cast()) }.to_string_lossy();
|
|
|
|
|
- tracing::info!(target: "title", "{}", text.trim_end());
|
|
|
|
|
|
|
+ tracing::info!(target: "title", "{}", narrow(text).trim_end());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// See [`OutputDebugStringA`].
|
|
/// See [`OutputDebugStringA`].
|
|
|
pub extern "system" fn OutputDebugStringW(text: *const u16) {
|
|
pub extern "system" fn OutputDebugStringW(text: *const u16) {
|
|
|
if !text.is_null() {
|
|
if !text.is_null() {
|
|
|
- // SAFETY: the title passes a NUL-terminated string.
|
|
|
|
|
- let text = unsafe {
|
|
|
|
|
- let len = (0..).take_while(|&i| *text.add(i) != 0).count();
|
|
|
|
|
- String::from_utf16_lossy(std::slice::from_raw_parts(text, len))
|
|
|
|
|
- };
|
|
|
|
|
- tracing::info!(target: "title", "{}", text.trim_end());
|
|
|
|
|
|
|
+ tracing::info!(target: "title", "{}", wide(text).trim_end());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -217,17 +233,44 @@ fn handle(m: &HleModule) -> *mut c_void {
|
|
|
m as *const HleModule as *mut c_void
|
|
m as *const HleModule as *mut c_void
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-fn console_module(path: &str) -> Option<*mut c_void> {
|
|
|
|
|
|
|
+/// The title's images and our console modules by name; `None` leaves the
|
|
|
|
|
+/// name to the host. With `load`, a title DLL not mapped yet is mapped now.
|
|
|
|
|
+fn find_module(path: &str, load: bool) -> Option<*mut c_void> {
|
|
|
let file = path.rsplit(['\\', '/']).next().unwrap_or(path);
|
|
let file = path.rsplit(['\\', '/']).next().unwrap_or(path);
|
|
|
|
|
+ // As the Windows loader does, a name without extension means a DLL.
|
|
|
|
|
+ let file = if file.contains('.') {
|
|
|
|
|
+ file.to_owned()
|
|
|
|
|
+ } else {
|
|
|
|
|
+ format!("{file}.dll")
|
|
|
|
|
+ };
|
|
|
|
|
+ if let Some(base) = runtime::image_named(&file) {
|
|
|
|
|
+ return Some(base as *mut c_void);
|
|
|
|
|
+ }
|
|
|
// DLLs the host has too are real host modules, whatever `--strict` says.
|
|
// DLLs the host has too are real host modules, whatever `--strict` says.
|
|
|
- if HostPolicy::default().allows(file) {
|
|
|
|
|
|
|
+ if !HostPolicy::default().allows(&file)
|
|
|
|
|
+ && let Some(m) = registry().iter().find(|m| m.serves(&file))
|
|
|
|
|
+ {
|
|
|
|
|
+ if load {
|
|
|
|
|
+ tracing::info!(dll = file, "loaded as a pseudo module");
|
|
|
|
|
+ }
|
|
|
|
|
+ return Some(handle(m));
|
|
|
|
|
+ }
|
|
|
|
|
+ if !load {
|
|
|
return None;
|
|
return None;
|
|
|
}
|
|
}
|
|
|
- let m = registry().iter().find(|m| m.serves(file))?;
|
|
|
|
|
- tracing::info!(dll = file, "loaded as a pseudo module");
|
|
|
|
|
- Some(handle(m))
|
|
|
|
|
|
|
+ match runtime::load_title_dll(&file) {
|
|
|
|
|
+ Ok(base) => base.map(|b| b as *mut c_void),
|
|
|
|
|
+ // Not the host: it would load the unpatched DLL from the title's folder.
|
|
|
|
|
+ Err(error) => {
|
|
|
|
|
+ tracing::error!(dll = file, %error, "mapping a title DLL failed");
|
|
|
|
|
+ host::set_last_error(ERROR_MOD_NOT_FOUND);
|
|
|
|
|
+ Some(std::ptr::null_mut())
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+const ERROR_MOD_NOT_FOUND: u32 = 126;
|
|
|
|
|
+
|
|
|
pub(crate) fn module() -> HleModule {
|
|
pub(crate) fn module() -> HleModule {
|
|
|
HleModule {
|
|
HleModule {
|
|
|
name: "kernel",
|
|
name: "kernel",
|
|
@@ -444,4 +487,39 @@ mod tests {
|
|
|
assert_eq!(GetModuleFileNameA(module, small.as_mut_ptr(), 4), 4);
|
|
assert_eq!(GetModuleFileNameA(module, small.as_mut_ptr(), 4), 4);
|
|
|
assert_eq!(&small, &[b'C', b':', b'\\', 0]);
|
|
assert_eq!(&small, &[b'C', b':', b'\\', 0]);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ fn w(text: &str) -> Vec<u16> {
|
|
|
|
|
+ text.encode_utf16().chain(Some(0)).collect()
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn title_dlls_and_pseudo_modules_are_found_by_name() {
|
|
|
|
|
+ let base = 0x7100_0000_0000usize;
|
|
|
|
|
+ runtime::set_image_path(base, std::path::Path::new(r"C:\title\Mapped.dll"));
|
|
|
|
|
+ runtime::set_title_dll_loader(Box::new(|file| match file {
|
|
|
|
|
+ "late.dll" => Ok(Some(0x7200_0000_0000)),
|
|
|
|
|
+ "broken.dll" => Err("bad image".into()),
|
|
|
|
|
+ _ => Ok(None),
|
|
|
|
|
+ }));
|
|
|
|
|
+ let mapped = base as *mut c_void;
|
|
|
|
|
+ assert_eq!(GetModuleHandleW(w("MAPPED").as_ptr()), mapped);
|
|
|
|
|
+ assert_eq!(GetModuleHandleA(c"sub\\mapped.dll".as_ptr().cast()), mapped);
|
|
|
|
|
+ assert_eq!(LoadLibraryW(w("mapped.dll").as_ptr()), mapped);
|
|
|
|
|
+ assert_eq!(FreeLibrary(mapped), 1, "title images stay loaded");
|
|
|
|
|
+
|
|
|
|
|
+ // Not mapped yet: `LoadLibrary` maps it, `GetModuleHandle` does not.
|
|
|
|
|
+ assert!(GetModuleHandleW(w("late.dll").as_ptr()).is_null());
|
|
|
|
|
+ assert_eq!(
|
|
|
|
|
+ LoadLibraryA(c"late".as_ptr().cast()) as usize,
|
|
|
|
|
+ 0x7200_0000_0000
|
|
|
|
|
+ );
|
|
|
|
|
+ assert!(LoadLibraryA(c"broken.dll".as_ptr().cast()).is_null());
|
|
|
|
|
+ // As data it runs no code, so the host loads it (and finds nothing).
|
|
|
|
|
+ let data = LoadLibraryExW(w("late.dll").as_ptr(), std::ptr::null_mut(), 0x2);
|
|
|
|
|
+ assert_ne!(data as usize, 0x7200_0000_0000);
|
|
|
|
|
+
|
|
|
|
|
+ let runtime = LoadLibraryA(c"xgameruntime.dll".as_ptr().cast());
|
|
|
|
|
+ assert!(!runtime.is_null());
|
|
|
|
|
+ assert_eq!(GetModuleHandleW(w("XGameRuntime.dll").as_ptr()), runtime);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|