lib.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //! Our implementation of the Xbox system API surface.
  2. //!
  3. //! A title imports from three kinds of DLLs, and each gets its own policy:
  4. //!
  5. //! | kind | examples | handled by |
  6. //! |------|----------|------------|
  7. //! | Windows API sets the console shares with PC | `api-ms-win-core-synch-*`, `ws2_32.dll` | host pass-through, overridden here where console behaviour differs (paths, memory) |
  8. //! | console-only system APIs | `xgameruntime.dll`, `xmem.dll`, `d3d12_xs.dll` | implemented here, in [`modules`] |
  9. //! | anything else | | an unimplemented-import stub from the loader |
  10. //!
  11. //! Resolution order for every import: an implementation in [`modules`],
  12. //! then the host if [`HostPolicy`] allows that DLL, then `Missing`.
  13. mod host;
  14. mod image_watch;
  15. #[cfg(all(windows, feature = "diagnostics"))]
  16. pub use victory_gpu::frame_phase;
  17. pub mod modules;
  18. mod registry;
  19. pub mod runtime;
  20. pub use host::HostPolicy;
  21. pub use registry::{HleExport, HleModule, registry};
  22. pub use victory_gpu::console;
  23. #[cfg(all(windows, feature = "diagnostics"))]
  24. pub use victory_gpu::replay;
  25. use victory_loader::{ImportResolver, Provenance, Resolution};
  26. use victory_pe::ImportSymbol;
  27. /// Resolves a title's imports against our runtime and the host.
  28. #[derive(Debug, Default)]
  29. pub struct HleResolver {
  30. pub host: HostPolicy,
  31. }
  32. impl HleResolver {
  33. pub fn new(host: HostPolicy) -> Self {
  34. HleResolver { host }
  35. }
  36. /// Where an import would come from, without binding anything.
  37. pub fn classify(&self, dll: &str, symbol: &ImportSymbol) -> Option<Provenance> {
  38. match self.resolve(dll, symbol) {
  39. Resolution::Found { provenance, .. } => Some(provenance),
  40. Resolution::Missing => None,
  41. }
  42. }
  43. }
  44. impl ImportResolver for HleResolver {
  45. fn resolve(&self, dll: &str, symbol: &ImportSymbol) -> Resolution {
  46. // Ordinal imports look up `#n`.
  47. if let Some(address) = registry::lookup(dll, &symbol.to_string()) {
  48. return Resolution::Found {
  49. address,
  50. provenance: Provenance::Hle,
  51. };
  52. }
  53. match self.host.resolve(dll, symbol) {
  54. Some(address) => Resolution::Found {
  55. address,
  56. provenance: Provenance::Host,
  57. },
  58. None => Resolution::Missing,
  59. }
  60. }
  61. }
  62. // The registry is code-only, so the "Missing" outcome can be checked on any
  63. // host; host pass-through itself needs Windows.
  64. #[cfg(test)]
  65. mod tests {
  66. use super::*;
  67. fn name(n: &str) -> ImportSymbol {
  68. ImportSymbol::Name {
  69. hint: 0,
  70. name: n.into(),
  71. }
  72. }
  73. #[test]
  74. fn console_api_resolves_to_hle() {
  75. let r = HleResolver::new(HostPolicy::none());
  76. assert_eq!(
  77. r.classify("XGameRuntime.dll", &name("XGameRuntimeInitialize")),
  78. Some(Provenance::Hle)
  79. );
  80. }
  81. #[test]
  82. fn strict_policy_reports_missing() {
  83. let r = HleResolver::new(HostPolicy::none());
  84. assert_eq!(
  85. r.classify("api-ms-win-core-synch-l1-1-0.dll", &name("Sleep")),
  86. None
  87. );
  88. }
  89. #[cfg(windows)]
  90. #[test]
  91. fn shared_api_sets_pass_through_to_host() {
  92. let r = HleResolver::default();
  93. assert_eq!(
  94. r.classify("api-ms-win-core-synch-l1-2-0.dll", &name("Sleep")),
  95. Some(Provenance::Host)
  96. );
  97. // Console-only DLLs never pass through.
  98. assert_eq!(
  99. r.classify("xmem.dll", &name("XMemSetAllocationHooks")),
  100. None
  101. );
  102. }
  103. #[cfg(windows)]
  104. #[test]
  105. fn overrides_win_over_host() {
  106. let r = HleResolver::default();
  107. assert_eq!(
  108. r.classify(
  109. "api-ms-win-core-libraryloader-l1-2-0.dll",
  110. &name("GetModuleHandleW")
  111. ),
  112. Some(Provenance::Hle)
  113. );
  114. }
  115. // #510: the RPC runtime's GUID calls are ours, so the DLL itself stays
  116. // outside the host policy (its own `ws2_32` imports would be a network
  117. // path `modules::net` never sees).
  118. #[test]
  119. fn the_rpc_runtime_is_implemented_here() {
  120. let r = HleResolver::new(HostPolicy::none());
  121. for call in [
  122. "UuidCreate",
  123. "UuidCreateSequential",
  124. "UuidFromStringA",
  125. "UuidFromStringW",
  126. "UuidToStringA",
  127. "UuidToStringW",
  128. "RpcStringFreeA",
  129. "RpcStringFreeW",
  130. ] {
  131. assert_eq!(
  132. r.classify("rpcrt4.dll", &name(call)),
  133. Some(Provenance::Hle),
  134. "{call}"
  135. );
  136. }
  137. // A call we do not implement is not passed to the host either.
  138. assert_eq!(r.classify("rpcrt4.dll", &name("RpcServerRegisterIf")), None);
  139. }
  140. }