lib.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. pub mod modules;
  15. mod registry;
  16. pub mod runtime;
  17. pub use host::HostPolicy;
  18. pub use registry::{HleExport, HleModule, registry};
  19. pub use victory_gpu::console;
  20. use victory_loader::{ImportResolver, Provenance, Resolution};
  21. use victory_pe::ImportSymbol;
  22. /// Resolves a title's imports against our runtime and the host.
  23. #[derive(Debug, Default)]
  24. pub struct HleResolver {
  25. pub host: HostPolicy,
  26. }
  27. impl HleResolver {
  28. pub fn new(host: HostPolicy) -> Self {
  29. HleResolver { host }
  30. }
  31. /// Where an import would come from, without binding anything.
  32. pub fn classify(&self, dll: &str, symbol: &ImportSymbol) -> Option<Provenance> {
  33. match self.resolve(dll, symbol) {
  34. Resolution::Found { provenance, .. } => Some(provenance),
  35. Resolution::Missing => None,
  36. }
  37. }
  38. }
  39. impl ImportResolver for HleResolver {
  40. fn resolve(&self, dll: &str, symbol: &ImportSymbol) -> Resolution {
  41. // Ordinal imports look up `#n`.
  42. if let Some(address) = registry::lookup(dll, &symbol.to_string()) {
  43. return Resolution::Found {
  44. address,
  45. provenance: Provenance::Hle,
  46. };
  47. }
  48. match self.host.resolve(dll, symbol) {
  49. Some(address) => Resolution::Found {
  50. address,
  51. provenance: Provenance::Host,
  52. },
  53. None => Resolution::Missing,
  54. }
  55. }
  56. }
  57. // The registry is code-only, so the "Missing" outcome can be checked on any
  58. // host; host pass-through itself needs Windows.
  59. #[cfg(test)]
  60. mod tests {
  61. use super::*;
  62. fn name(n: &str) -> ImportSymbol {
  63. ImportSymbol::Name {
  64. hint: 0,
  65. name: n.into(),
  66. }
  67. }
  68. #[test]
  69. fn console_api_resolves_to_hle() {
  70. let r = HleResolver::new(HostPolicy::none());
  71. assert_eq!(
  72. r.classify("XGameRuntime.dll", &name("XGameRuntimeInitialize")),
  73. Some(Provenance::Hle)
  74. );
  75. }
  76. #[test]
  77. fn strict_policy_reports_missing() {
  78. let r = HleResolver::new(HostPolicy::none());
  79. assert_eq!(
  80. r.classify("api-ms-win-core-synch-l1-1-0.dll", &name("Sleep")),
  81. None
  82. );
  83. }
  84. #[cfg(windows)]
  85. #[test]
  86. fn shared_api_sets_pass_through_to_host() {
  87. let r = HleResolver::default();
  88. assert_eq!(
  89. r.classify("api-ms-win-core-synch-l1-2-0.dll", &name("Sleep")),
  90. Some(Provenance::Host)
  91. );
  92. // Console-only DLLs never pass through.
  93. assert_eq!(
  94. r.classify("xmem.dll", &name("XMemSetAllocationHooks")),
  95. None
  96. );
  97. }
  98. #[cfg(windows)]
  99. #[test]
  100. fn overrides_win_over_host() {
  101. let r = HleResolver::default();
  102. assert_eq!(
  103. r.classify(
  104. "api-ms-win-core-libraryloader-l1-2-0.dll",
  105. &name("GetModuleHandleW")
  106. ),
  107. Some(Provenance::Hle)
  108. );
  109. }
  110. }