lib.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //! A stand-in for a title, written by us. It imports from DLLs that do not
  2. //! exist on a PC, which is exactly the situation victory-loader handles.
  3. //! Loaded normally, this DLL would fail with "module not found".
  4. use std::ffi::c_void;
  5. use std::sync::atomic::{AtomicU32, Ordering};
  6. const DLL_THREAD_ATTACH: u32 = 2;
  7. #[link(name = "xgameruntime", kind = "raw-dylib")]
  8. unsafe extern "system" {
  9. fn XGameRuntimeInitialize() -> i32;
  10. }
  11. #[link(name = "victory-probe-missing", kind = "raw-dylib")]
  12. unsafe extern "system" {
  13. fn VIctoryProbeNotImplemented(x: u64) -> u64;
  14. }
  15. // A DLL the "title" ships; the test supplies a renamed copy of version.dll.
  16. #[link(name = "victory-probe-title", kind = "raw-dylib")]
  17. unsafe extern "system" {
  18. fn GetFileVersionInfoSizeW(path: *const u16, handle: *mut u32) -> u32;
  19. }
  20. // Delay-loaded (see build.rs): bound through the delay-load table.
  21. #[link(name = "victory-probe-delayed", kind = "raw-dylib")]
  22. unsafe extern "system" {
  23. fn VIctoryProbeDelayed() -> u32;
  24. }
  25. #[link(name = "kernel32")]
  26. unsafe extern "system" {
  27. fn GetTickCount() -> u32;
  28. fn GetCurrentProcessId() -> u32;
  29. }
  30. /// Console-only API, answered by the resolver's HLE implementation.
  31. #[unsafe(no_mangle)]
  32. pub extern "system" fn probe_xgameruntime_init() -> i32 {
  33. unsafe { XGameRuntimeInitialize() }
  34. }
  35. /// Exists on the host too, but the resolver overrides it.
  36. #[unsafe(no_mangle)]
  37. pub extern "system" fn probe_tick() -> u32 {
  38. unsafe { GetTickCount() }
  39. }
  40. /// Exists on the host and is passed through to it.
  41. #[unsafe(no_mangle)]
  42. pub extern "system" fn probe_pid() -> u32 {
  43. unsafe { GetCurrentProcessId() }
  44. }
  45. /// An export of a title-shipped DLL: the version-info size of kernel32.dll.
  46. #[unsafe(no_mangle)]
  47. pub extern "system" fn probe_title() -> u32 {
  48. let path: Vec<u16> = r"C:\Windows\System32\kernel32.dll"
  49. .encode_utf16()
  50. .chain(Some(0))
  51. .collect();
  52. let mut handle = 0;
  53. unsafe { GetFileVersionInfoSizeW(path.as_ptr(), &mut handle) }
  54. }
  55. /// A delay-loaded console API.
  56. #[unsafe(no_mangle)]
  57. pub extern "system" fn probe_delayed() -> u32 {
  58. unsafe { VIctoryProbeDelayed() }
  59. }
  60. /// Nobody implements this one: it must land in an unimplemented-import stub.
  61. #[unsafe(no_mangle)]
  62. pub extern "system" fn probe_missing() -> u64 {
  63. unsafe { VIctoryProbeNotImplemented(42) }
  64. }
  65. static TLS_THREAD_STARTS: AtomicU32 = AtomicU32::new(0);
  66. static MAIN_THREAD_STARTS: AtomicU32 = AtomicU32::new(0);
  67. /// A TLS callback, as titles have: counts thread starts.
  68. unsafe extern "system" fn on_thread(_: *mut c_void, reason: u32, _: *mut c_void) {
  69. if reason == DLL_THREAD_ATTACH {
  70. TLS_THREAD_STARTS.fetch_add(1, Ordering::Relaxed);
  71. }
  72. }
  73. #[used]
  74. #[unsafe(link_section = ".CRT$XLB")]
  75. static ON_THREAD: unsafe extern "system" fn(*mut c_void, u32, *mut c_void) = on_thread;
  76. /// Called by the CRT's DLL entry point: counts thread starts too, and calls
  77. /// an import the resolver overrides, as title code calls our runtime.
  78. #[unsafe(no_mangle)]
  79. pub extern "system" fn DllMain(_: *mut c_void, reason: u32, _: *mut c_void) -> i32 {
  80. if reason == DLL_THREAD_ATTACH {
  81. MAIN_THREAD_STARTS.fetch_add(1, Ordering::Relaxed);
  82. unsafe { GetTickCount() };
  83. }
  84. if reason == DLL_PROCESS_DETACH {
  85. DETACHES.fetch_add(1, Ordering::Relaxed);
  86. }
  87. 1
  88. }
  89. const DLL_PROCESS_DETACH: u32 = 0;
  90. static DETACHES: AtomicU32 = AtomicU32::new(0);
  91. /// `DLL_PROCESS_DETACH` notices seen by `DllMain`.
  92. #[unsafe(no_mangle)]
  93. pub extern "system" fn probe_detaches() -> u32 {
  94. DETACHES.load(Ordering::Relaxed)
  95. }
  96. /// Thread starts seen by the TLS callback (high half) and `DllMain` (low).
  97. #[unsafe(no_mangle)]
  98. pub extern "system" fn probe_thread_starts() -> u64 {
  99. // SAFETY: reading our own static keeps the linker from dropping it.
  100. unsafe { std::ptr::from_ref(&ON_THREAD).read_volatile() };
  101. u64::from(TLS_THREAD_STARTS.load(Ordering::Relaxed)) << 32
  102. | u64::from(MAIN_THREAD_STARTS.load(Ordering::Relaxed))
  103. }