diff --git a/src/game_engine/unity/mod.rs b/src/game_engine/unity/mod.rs index 7f2af377..d0cb62b7 100644 --- a/src/game_engine/unity/mod.rs +++ b/src/game_engine/unity/mod.rs @@ -98,6 +98,7 @@ const CSTR: usize = 128; enum BinaryFormat { PE, ELF, + #[cfg(feature = "alloc")] MachO, } diff --git a/src/game_engine/unity/scene_manager/attach_tests.rs b/src/game_engine/unity/scene_manager/attach_tests.rs index e6817ad1..951b7b9d 100644 --- a/src/game_engine/unity/scene_manager/attach_tests.rs +++ b/src/game_engine/unity/scene_manager/attach_tests.rs @@ -2,7 +2,7 @@ //! the anchor of the build is scanned in the engine module, every match //! must point at the same global, and the global holds the manager. -use super::{builds, Scene, SceneManager}; +use super::{builds, linux_builds, Scene, SceneManager}; use crate::{runtime::mock::with_modules, Address, PointerSize, Process}; use std::format; use std::vec; @@ -64,6 +64,12 @@ fn nearest_is_the_build_itself_on_a_measured_player() { assert_eq!(found.unity, build.unity); assert_eq!(found.profile.pointer_size, build.profile.pointer_size); } + for build in linux_builds::BUILDS { + assert_eq!( + linux_builds::nearest(build.unity).unwrap().unity, + build.unity + ); + } } // Checks the rule against every entry of the table, so an entry added or @@ -105,9 +111,14 @@ fn a_player_takes_the_newest_build_at_or_below_its_patch() { #[test] fn table_reads_oldest_to_newest() { - for pointer_size in [PointerSize::Bit64, PointerSize::Bit32] { + let tables = [ + (builds::BUILDS, PointerSize::Bit64), + (builds::BUILDS, PointerSize::Bit32), + (linux_builds::BUILDS, PointerSize::Bit64), + ]; + for (table, pointer_size) in tables { let mut last = (0, 0, 0, 0); - for build in builds::BUILDS { + for build in table { if build.profile.pointer_size != pointer_size { continue; } @@ -119,8 +130,13 @@ fn table_reads_oldest_to_newest() { #[test] fn each_build_differs_from_the_one_before_it() { - for pointer_size in [PointerSize::Bit64, PointerSize::Bit32] { - let table: Vec<_> = builds::BUILDS + let tables = [ + (builds::BUILDS, PointerSize::Bit64), + (builds::BUILDS, PointerSize::Bit32), + (linux_builds::BUILDS, PointerSize::Bit64), + ]; + for (table, pointer_size) in tables { + let table: Vec<_> = table .iter() .filter(|build| build.profile.pointer_size == pointer_size) .collect(); @@ -261,16 +277,18 @@ fn the_old_x86_anchor_leaves_the_scene_list_offset_open() { // The least of a PE header: the DOS header pointing at the COFF header, an // x64 machine, and an optional header carrying the size of the image. +#[cfg(feature = "alloc")] fn pe_header(image: &mut [u8]) { put(image, 0, b"MZ"); put(image, 0x3C, &0x80_u32.to_le_bytes()); - put(image, 0x80, b"PE"); + put(image, 0x80, b"PE\0\0"); put(image, 0x84, &0x8664_u16.to_le_bytes()); put(image, 0x94, &0xF0_u16.to_le_bytes()); put(image, 0x98, &0x20B_u16.to_le_bytes()); put(image, 0x98 + 0x38, &(MODULE as u32).to_le_bytes()); } +#[cfg(feature = "alloc")] #[test] fn the_engine_module_is_the_player_when_there_is_one() { let mut player = vec![0; 0x200]; @@ -306,3 +324,95 @@ fn the_engine_module_is_the_executable_when_there_is_no_player() { }, ); } + +// An older Linux player links the engine into its executable, which is an +// ELF. +#[cfg(feature = "alloc")] +#[test] +fn the_engine_module_is_the_linux_executable_when_there_is_no_player() { + let mut executable = vec![0; 0x200]; + put(&mut executable, 0, b"\x7fELF\x02\x01\x01"); + with_modules( + &[(BASE, &executable)], + &[("fixture", BASE, MODULE)], + |process| { + let (range, format) = SceneManager::engine_module(process).unwrap(); + assert_eq!(range, (Address::new(BASE), MODULE)); + assert_eq!(format, super::BinaryFormat::ELF); + }, + ); +} + +// A Linux player takes its build by the version string in `UnityPlayer.so`. +// The 2022.3.5 build keeps scene paths inline and the roots at 0xe8. +#[cfg(feature = "alloc")] +#[test] +fn a_linux_player_takes_the_build_measured_on_linux() { + let mut image = vec![0; 0x2000]; + put(&mut image, 0, b"\x7fELF\x02\x01\x01"); + let text = b"\x002022.3.5f1\0"; + put(&mut image, 0x300, text); + // push rbp; push r15; push r14; push r13; push r12; push rbx; push rax; + // mov r14, [global]; cmp dword ptr [r14 + 0x18], 0; je + put( + &mut image, + 0x100, + &[ + 0x55, 0x41, 0x57, 0x41, 0x56, 0x41, 0x55, 0x41, 0x54, 0x53, 0x50, 0x4C, 0x8B, 0x35, + ], + ); + let displacement = (0x800 - (0x100 + 14 + 4)) as i32; + put(&mut image, 0x100 + 14, &displacement.to_le_bytes()); + put( + &mut image, + 0x100 + 18, + &[0x41, 0x83, 0x7E, 0x18, 0x00, 0x74], + ); + put(&mut image, 0x800, &(BASE + 0x900).to_le_bytes()); + + let manager = with_modules( + &[(BASE, &image)], + &[("UnityPlayer.so", BASE, MODULE)], + SceneManager::attach, + ) + .unwrap(); + assert_eq!(manager.address, Address::new(BASE + 0x900)); + assert_eq!(manager.profile.path, super::offsets::PathShape::InlineNul); + assert_eq!(manager.profile.scene.roots, 0xe8); +} + +#[cfg(feature = "alloc")] +#[test] +fn an_executable_only_linux_player_attaches_automatically() { + let mut image = vec![0; 0x2000]; + put(&mut image, 0, b"\x7fELF\x02\x01\x01"); + put(&mut image, 0x300, b"\x005.6.7f1\0"); + // The old Linux anchor loads the manager global into rbp before + // checking the scene count at 0x18. + put( + &mut image, + 0x100, + &[0x41, 0x54, 0x49, 0x89, 0xFC, 0x55, 0x48, 0x8B, 0x2D], + ); + let displacement = (0x800 - (0x100 + 9 + 4)) as i32; + put(&mut image, 0x100 + 9, &displacement.to_le_bytes()); + put( + &mut image, + 0x100 + 13, + &[0x53, 0x8B, 0x45, 0x18, 0x85, 0xC0, 0x74], + ); + put(&mut image, 0x800, &(BASE + 0x900).to_le_bytes()); + + with_modules(&[(BASE, &image)], &[("fixture", BASE, MODULE)], |process| { + let manager = SceneManager::attach(process).unwrap(); + assert_eq!(manager.address, Address::new(BASE + 0x900)); + assert!(core::ptr::eq( + manager.profile, + &linux_builds::nearest((5, 6, 7, 0)).unwrap().profile, + )); + assert!(matches!( + crate::runtime::mock::poll_once(SceneManager::wait_attach(process)), + core::task::Poll::Ready(_), + )); + }); +} diff --git a/src/game_engine/unity/scene_manager/builds.rs b/src/game_engine/unity/scene_manager/builds.rs index 65a8ef29..94aa7937 100644 --- a/src/game_engine/unity/scene_manager/builds.rs +++ b/src/game_engine/unity/scene_manager/builds.rs @@ -24,6 +24,16 @@ pub(super) struct Build { pub(super) fn nearest( unity: (u16, u16, u16, u16), pointer_size: PointerSize, +) -> Option<&'static Build> { + nearest_in(BUILDS, unity, pointer_size) +} + +/// Finds the build for a player in the given table by the rule of +/// [`nearest`]. +pub(super) fn nearest_in( + builds: &'static [Build], + unity: (u16, u16, u16, u16), + pointer_size: PointerSize, ) -> Option<&'static Build> { // The comparison drops the build number, the fourth part, because a later // build of one patch can carry a lower number than an earlier build. @@ -31,7 +41,7 @@ pub(super) fn nearest( // The table reads from the oldest player to the newest, so the last match // is the newest build at or below the player's patch. - BUILDS + builds .iter() .filter(|build| build.profile.pointer_size == pointer_size) .rfind(|build| patch(build.unity) <= patch(unity)) @@ -993,11 +1003,12 @@ pub(super) const BUILDS: &[Build] = &[ }, ]; -/// The layout used for a Linux or Mac x64 player. Those players are not -/// measured yet, so this keeps the signature and the offsets the walk used -/// before the table existed. The offsets are the x64 layout of Unity 2018.4 -/// through 2022.3. -pub(super) const ELF_AND_MACHO_X64: Profile = Profile { +/// The layout used for a Mac x64 player. Those players are not measured +/// yet, so this keeps the signature and the offsets the walk used before the +/// table existed. The offsets are the x64 layout of Unity 2018.4 through +/// 2022.3. +#[cfg(feature = "alloc")] +pub(super) const MACHO_X64: Profile = Profile { pointer_size: PointerSize::Bit64, anchor: Anchor { signature: Signature::new( diff --git a/src/game_engine/unity/scene_manager/linux_builds.rs b/src/game_engine/unity/scene_manager/linux_builds.rs new file mode 100644 index 00000000..6fbbb2ef --- /dev/null +++ b/src/game_engine/unity/scene_manager/linux_builds.rs @@ -0,0 +1,616 @@ +//! Known Linux Unity players and the layout of their scene manager, scenes, +//! transforms and game objects. Each entry is one measured x64 player. The +//! offsets were measured on a running player with a known scene. They equal +//! the offsets of the Windows x64 build that the same version takes, so only +//! the anchors differ. + +use super::builds::{nearest_in, Build}; +use super::offsets::{ + Anchor, GameObjectOffsets, ManagerOffsets, ObjectOffsets, PathShape, Profile, ReferenceShape, + SceneOffsets, TransformOffsets, +}; +use crate::{signature::Signature, PointerSize}; + +/// Finds the build for a Linux x64 player by the rule of [`nearest_in`]. The +/// Unity version comes from the version string in the engine module, so its +/// fourth part is zero. +pub(super) fn nearest(unity: (u16, u16, u16, u16)) -> Option<&'static Build> { + nearest_in(BUILDS, unity, PointerSize::Bit64) +} + +// A function that loads the global into rbp and checks the scene count at +// 0x18. It hits once on every player from Unity 5.6 through 2017.2. +const COUNT_CHECK_RBP_X64: Anchor = Anchor { + signature: Signature::new( + "41 54 49 89 FC 55 48 8B 2D ?? ?? ?? ?? 53 8B 45 18 85 C0 74 ?? ?? ?? ??", + ), + displacement: 9, +}; + +// The same function once it copies rdi after the load instead of before. +// It hits once on every player from Unity 2017.3 through 2018.1. +const COUNT_CHECK_RBP_SAVED_X64: Anchor = Anchor { + signature: Signature::new( + "41 54 55 48 8B 2D ?? ?? ?? ?? 53 8B 45 18 85 C0 74 ?? 49 89 FC 31 DB ??", + ), + displacement: 6, +}; + +// The same function once the global lands in r14. It hits once on every +// player from Unity 2018.2 through 2018.3. +const COUNT_CHECK_R14_X64: Anchor = Anchor { + signature: Signature::new( + "41 56 4C 8B 35 ?? ?? ?? ?? 41 55 49 89 FD 41 54 45 31 E4 55 53 41 8B 46", + ), + displacement: 5, +}; + +// The end of the function that creates the manager: it calls the +// constructor and stores the new manager in the global. It hits once on every +// player from Unity 2019.1 through 2019.2. +const MANAGER_STORE_X64: Anchor = Anchor { + signature: Signature::new( + "74 ?? BE 61 00 00 00 48 89 C7 E8 ?? ?? ?? ?? 48 89 1D ?? ?? ?? ?? 5B C3", + ), + displacement: 18, +}; + +// The count check function once it pushes rbp, r15, r14, r13, r12, rbx and +// rax, copies rdi into r15 and then loads the global into r14. It hits once +// on every player from Unity 2019.3 through 2020.1. +const COUNT_CHECK_SAVED_RDI_X64: Anchor = Anchor { + signature: Signature::new( + "55 41 57 41 56 41 55 41 54 53 50 49 89 FF 4C 8B 35 ?? ?? ?? ?? 41 83 7E", + ), + displacement: 17, +}; + +// The same function once it copies rdi after the load. It hits once on every +// player from Unity 2020.2 through 6000.5. +const COUNT_CHECK_PROLOGUE_X64: Anchor = Anchor { + signature: Signature::new( + "55 41 57 41 56 41 55 41 54 53 50 4C 8B 35 ?? ?? ?? ?? 41 83 7E 18 00 74", + ), + displacement: 14, +}; + +// A function that loads the global into rdi and calls through its vtable. +// It hits once on every player from Unity 6000.0 through 6000.6. +const VIRTUAL_CALL_X64: Anchor = Anchor { + signature: Signature::new( + "48 8B 3D ?? ?? ?? ?? 48 8B 07 FF 50 10 48 89 DF 48 89 C6 E8 ?? ?? ?? ??", + ), + displacement: 3, +}; + +// The table reads from the oldest player to the newest. +pub(super) const BUILDS: &[Build] = &[ + // Unity 5.6.0f1, x64. + Build { + unity: (5, 6, 0, 23754), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_RBP_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: None, + }, + scene: SceneOffsets { + path: 0x18, + build_index: 0xa0, + roots: 0xb8, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 5.6.2f1, x64. Same layout as 5.6.0, but with the DontDestroyOnLoad + // scene. + Build { + unity: (5, 6, 2, 37180), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_RBP_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x18, + build_index: 0xa0, + roots: 0xb8, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2017.1.0f1, x64. + Build { + unity: (2017, 1, 0, 32737), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_RBP_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x68, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2017.3.0f1, x64. Same layout as 2017.1.0, but the anchor changed. + Build { + unity: (2017, 3, 0, 20311), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_RBP_SAVED_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x68, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2018.2.0f1, x64. + Build { + unity: (2018, 2, 0, 44229), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_R14_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x18, + build_index: 0xa0, + roots: 0xb8, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x68, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2018.3.0f1, x64. + Build { + unity: (2018, 3, 0, 9156), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_R14_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2019.1.0f1, x64. Same layout as 2018.3.0, but the anchor changed. + Build { + unity: (2019, 1, 0, 21026), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: MANAGER_STORE_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2019.3.0f1, x64. Same layout as 2018.3.0, but the anchor changed. + Build { + unity: (2019, 3, 0, 44266), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_SAVED_RDI_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2020.2.0f1, x64. Same layout as 2018.3.0, but the anchor changed. + Build { + unity: (2020, 2, 0, 8671), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::Pointer, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2021.1.0f1, x64. + Build { + unity: (2021, 1, 0, 42313), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineNul, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2022.3.5f1, x64. + Build { + unity: (2022, 3, 5, 29734), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineNul, + reference: ReferenceShape::CachedObject, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xe8, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x28, + }, + }, + }, + // Unity 2023.1.0f1, x64. + Build { + unity: (2023, 1, 0, 2298), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xb0, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x18, + }, + }, + }, + // Unity 2023.1.4f1, x64. + Build { + unity: (2023, 1, 4, 6702), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xe8, + }, + transform: TransformOffsets { + game_object: 0x30, + children: 0x70, + }, + game_object: GameObjectOffsets { + components: 0x30, + name: 0x60, + }, + object: ObjectOffsets { + managed_reference: 0x18, + }, + }, + }, + // Unity 2023.2.0f1, x64. + Build { + unity: (2023, 2, 0, 54845), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xe8, + }, + transform: TransformOffsets { + game_object: 0x20, + children: 0x60, + }, + game_object: GameObjectOffsets { + components: 0x20, + name: 0x50, + }, + object: ObjectOffsets { + managed_reference: 0x18, + }, + }, + }, + // Unity 6000.3.0f1, x64. + Build { + unity: (6000, 3, 0, 34572), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0xe8, + }, + transform: TransformOffsets { + game_object: 0x20, + children: 0x48, + }, + game_object: GameObjectOffsets { + components: 0x20, + name: 0x50, + }, + object: ObjectOffsets { + managed_reference: 0x18, + }, + }, + }, + // Unity 6000.5.0f1, x64. + Build { + unity: (6000, 5, 0, 46204), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: COUNT_CHECK_PROLOGUE_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0x110, + }, + transform: TransformOffsets { + game_object: 0x28, + children: 0x50, + }, + game_object: GameObjectOffsets { + components: 0x28, + name: 0x58, + }, + object: ObjectOffsets { + managed_reference: 0x20, + }, + }, + }, + // Unity 6000.6.0f1, x64. Same layout as 6000.5.0, but the anchor changed. + Build { + unity: (6000, 6, 0, 63725), + profile: Profile { + pointer_size: PointerSize::Bit64, + anchor: VIRTUAL_CALL_X64, + path: PathShape::InlineSpare, + reference: ReferenceShape::RootSlot, + manager: ManagerOffsets { + scenes: 0x8, + active_scene: 0x48, + dont_destroy_on_load_scene: Some(0x70), + }, + scene: SceneOffsets { + path: 0x10, + build_index: 0x98, + roots: 0x110, + }, + transform: TransformOffsets { + game_object: 0x28, + children: 0x50, + }, + game_object: GameObjectOffsets { + components: 0x28, + name: 0x58, + }, + object: ObjectOffsets { + managed_reference: 0x20, + }, + }, + }, +]; diff --git a/src/game_engine/unity/scene_manager/mod.rs b/src/game_engine/unity/scene_manager/mod.rs index c2f9d2ae..19c146c6 100644 --- a/src/game_engine/unity/scene_manager/mod.rs +++ b/src/game_engine/unity/scene_manager/mod.rs @@ -1,4 +1,9 @@ //! Support for identifying the current scene in Unity games. +//! +//! Automatic attachment requires the `alloc` feature so that a game without +//! a separate player module can be identified through its executable. A +//! `no_std` auto splitter must provide a global allocator. Reading scenes, +//! transforms, and components does not require allocation. // References: // https://gist.githubusercontent.com/just-ero/92457b51baf85bd1e5b8c87de8c9835e/raw/8aa3e6b8da01fd03ff2ff0c03cbd018e522ef988/UnityScene.hpp @@ -6,15 +11,19 @@ // The offsets come from the measured builds in `builds.rs`. The logic for // Transforms and GameObjects is taken from https://github.com/Micrologist/UnityInstanceDumper +#[cfg(feature = "alloc")] use crate::{ file_format::{elf, macho, pe}, future::retry, print_limited, - string::ArrayCString, - Address, Error, PointerSize, Process, }; +use crate::{string::ArrayCString, Address, Error, PointerSize, Process}; +// The tables are also used by allocation-free tests of the scene readers. +#[cfg(any(feature = "alloc", test))] mod builds; +#[cfg(any(feature = "alloc", test))] +mod linux_builds; mod game_objects; @@ -40,7 +49,9 @@ use offsets::Profile; mod scene; pub use scene::Scene; -use super::{BinaryFormat, CSTR}; +#[cfg(feature = "alloc")] +use super::BinaryFormat; +use super::CSTR; /// The most scenes a game can have loaded at once. No game comes anywhere /// near 4,096. @@ -58,10 +69,18 @@ pub struct SceneManager { } impl SceneManager { - /// Attaches to the scene manager in the given process. On Windows, the - /// Unity version and pointer size of the game select a measured build: - /// the newest build whose patch is at or below the game's patch. A game - /// below every build does not attach. + /// Attaches to the scene manager in the given process. On Windows, and + /// on x64 Linux, the Unity version and pointer size of the game select a + /// measured build: the newest build whose patch is at or below the + /// game's patch. A game below every build does not attach. + /// + /// This requires the `alloc` feature. A game without a separate player + /// module keeps the engine in its executable, and finding that executable + /// allocates its path. A `no_std` auto splitter must provide a global + /// allocator. Basic scene, transform, and component reads do not require + /// allocation. + #[cfg(feature = "alloc")] + #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pub fn attach(process: &Process) -> Option { let (unity_player, format) = Self::engine_module(process)?; @@ -71,25 +90,20 @@ impl SceneManager { pe::MachineType::read(process, unity_player.0)?.pointer_size()?; let unity = Self::unity_version(process, unity_player.0)?; let build = builds::nearest(unity, pointer_size)?; - print_limited::<128>(&format_args!( - "scene manager: unity {}.{}.{}.{} takes the build measured on {}.{}.{}.{}", - unity.0, - unity.1, - unity.2, - unity.3, - build.unity.0, - build.unity.1, - build.unity.2, - build.unity.3, - )); + Self::print_build(unity, build); + &build.profile + } + BinaryFormat::ELF => { + if elf::pointer_size(process, unity_player.0)? != PointerSize::Bit64 { + return None; + } + let unity = super::version_string(process, unity_player)?; + let build = linux_builds::nearest(unity)?; + Self::print_build(unity, build); &build.profile } - BinaryFormat::ELF => match elf::pointer_size(process, unity_player.0)? { - PointerSize::Bit64 => &builds::ELF_AND_MACHO_X64, - _ => return None, - }, BinaryFormat::MachO => match macho::pointer_size(process, unity_player)? { - PointerSize::Bit64 => &builds::ELF_AND_MACHO_X64, + PointerSize::Bit64 => &builds::MACHO_X64, _ => return None, }, }; @@ -97,18 +111,41 @@ impl SceneManager { Self::attach_with(process, unity_player, profile) } + /// Logs which measured build the game takes. + #[cfg(feature = "alloc")] + fn print_build(unity: (u16, u16, u16, u16), build: &builds::Build) { + print_limited::<128>(&format_args!( + "scene manager: unity {}.{}.{}.{} takes the build measured on {}.{}.{}.{}", + unity.0, + unity.1, + unity.2, + unity.3, + build.unity.0, + build.unity.1, + build.unity.2, + build.unity.3, + )); + } + /// Attaches to the scene manager in the given process. /// /// This is the `await`able version of the [`attach`](Self::attach) - /// function, yielding back to the runtime between each try. + /// function, yielding back to the runtime between each try. It requires + /// the `alloc` feature so that the executable's path can be allocated when + /// no player module is present. A `no_std` auto splitter must provide a + /// global allocator. + #[cfg(feature = "alloc")] + #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] pub async fn wait_attach(process: &Process) -> SceneManager { retry(|| Self::attach(process)).await } /// Finds the module that holds the engine: `UnityPlayer.dll` and its - /// Linux and Mac siblings, or the game's own executable before Unity - /// 2017.2, which linked the engine in. Finding the executable needs its - /// name, so that part needs the `alloc` feature. + /// Linux and Mac siblings, or the game's own executable, which linked the + /// engine in before Unity 2017.2 on Windows and on older Linux players. + /// Finding the executable needs its name, so that part needs the `alloc` + /// feature. + #[cfg(feature = "alloc")] fn engine_module(process: &Process) -> Option<((Address, u64), BinaryFormat)> { let player = [ ("UnityPlayer.dll", BinaryFormat::PE), @@ -127,18 +164,19 @@ impl SceneManager { _ => Some((process.get_module_range(name).ok()?, format)), }); - #[cfg(feature = "alloc")] - let player = player.or_else(|| { + player.or_else(|| { let executable = process.get_main_module_range().ok()?; - pe::MachineType::read(process, executable.0)?; - Some((executable, BinaryFormat::PE)) - }); - - player + if pe::MachineType::read(process, executable.0).is_some() { + return Some((executable, BinaryFormat::PE)); + } + elf::pointer_size(process, executable.0)?; + Some((executable, BinaryFormat::ELF)) + }) } /// Reads the four parts of the file version of the engine module, which /// name the Unity version of the game. + #[cfg(feature = "alloc")] fn unity_version(process: &Process, unity_player: Address) -> Option<(u16, u16, u16, u16)> { let file_version = pe::FileVersion::read(process, unity_player)?; Some(( @@ -152,6 +190,7 @@ impl SceneManager { /// Finds the scene manager in the engine module through the anchor of /// the profile. Every match of the anchor must name the same global, /// and the global must hold the manager. + #[cfg(any(feature = "alloc", test))] fn attach_with( process: &Process, unity_player: (Address, u64), @@ -235,7 +274,7 @@ impl SceneManager { .and_then(|scene| scene.index(process, self)) } - /// Returns the full path to the current scene. Use [`get_scene_name`] + /// Returns the full path to the current scene. Use [`get_name`] /// afterwards to get the scene name. pub fn get_current_scene_path( &self, diff --git a/src/game_engine/unity/scene_manager/offsets.rs b/src/game_engine/unity/scene_manager/offsets.rs index aab5b162..d3158135 100644 --- a/src/game_engine/unity/scene_manager/offsets.rs +++ b/src/game_engine/unity/scene_manager/offsets.rs @@ -1,3 +1,4 @@ +#[cfg(any(feature = "alloc", test))] use crate::{signature::Signature, PointerSize}; /// How the scene manager global is found: the body of a function that loads @@ -5,6 +6,7 @@ use crate::{signature::Signature, PointerSize}; /// displacement starts inside a match. A body shorter than the signature is /// padded with wildcards. #[derive(Debug)] +#[cfg(any(feature = "alloc", test))] pub(super) struct Anchor { pub(super) signature: Signature<24>, pub(super) displacement: u8, @@ -82,7 +84,9 @@ pub(super) struct ObjectOffsets { /// What the walk needs to know about one player. #[derive(Debug)] pub(super) struct Profile { + #[cfg(any(feature = "alloc", test))] pub(super) pointer_size: PointerSize, + #[cfg(any(feature = "alloc", test))] pub(super) anchor: Anchor, pub(super) path: PathShape, pub(super) reference: ReferenceShape, diff --git a/src/game_engine/unity/scene_manager/scene.rs b/src/game_engine/unity/scene_manager/scene.rs index f9b682ea..0cca8217 100644 --- a/src/game_engine/unity/scene_manager/scene.rs +++ b/src/game_engine/unity/scene_manager/scene.rs @@ -1,4 +1,6 @@ -use super::{offsets::PathShape, SceneManager, CSTR}; +#[cfg(feature = "alloc")] +use super::CSTR; +use super::{offsets::PathShape, SceneManager}; use crate::{string::ArrayCString, Address, Error, PointerSize, Process}; /// A scene loaded in the attached game. @@ -86,9 +88,12 @@ impl Scene { /// Returns the length of the path kept inline in the field, or [`None`] /// when the field holds no inline path. fn inline_len(field: &[u8; 32], shape: PathShape) -> Option { - let len = match shape { - PathShape::InlineSpare => 31_usize.checked_sub(field[31] as usize)?, - _ => field.iter().position(|&b| b == 0)?, + let len = if shape == PathShape::InlineNul { + field.iter().position(|&b| b == 0)? + } else if shape == PathShape::InlineSpare { + 31_usize.checked_sub(field[31] as usize)? + } else { + return None; }; let terminated = len == 31 || field[len] == 0; (len <= 31 && terminated && !field[..len].contains(&0)).then_some(len) diff --git a/src/game_engine/unity/scene_manager/transform.rs b/src/game_engine/unity/scene_manager/transform.rs index 578040b4..58b69400 100644 --- a/src/game_engine/unity/scene_manager/transform.rs +++ b/src/game_engine/unity/scene_manager/transform.rs @@ -98,9 +98,12 @@ impl Transform { }; Ok((1..number_of_components).filter_map(move |m| { let at = components[m] + reference; - match scene_manager.profile.reference { - ReferenceShape::CachedObject => read(at), - ReferenceShape::RootSlot => read(read(at)?), + if scene_manager.profile.reference == ReferenceShape::CachedObject { + read(at) + } else if scene_manager.profile.reference == ReferenceShape::RootSlot { + read(read(at)?) + } else { + None } })) }