From ce6bf8c35228d8b46ea36755a296f9216abc042b Mon Sep 17 00:00:00 2001 From: HugeFrog24 <62775760+HugeFrog24@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:16:50 +0200 Subject: [PATCH 1/2] Re-glob native sources at build time file(GLOB) without CONFIGURE_DEPENDS is only evaluated at configure time, so a build with an existing app/.cxx silently leaves out newly added .cpp files and the APK fails at runtime with UnsatisfiedLinkError. CONFIGURE_DEPENDS re-checks the glob on every build (CMake 3.12+, this build uses 3.22.1). --- app/src/main/cpp/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 9408451..3b41bfa 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -12,7 +12,7 @@ project("imgui") add_library(ktx_read SHARED IMPORTED) set_target_properties(ktx_read PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Utils/ktx/libktx_read.so) -file(GLOB CPP_FILES +file(GLOB CPP_FILES CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/Core/**/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Core/**/*.c ${CMAKE_CURRENT_SOURCE_DIR}/Utils/**/*.cpp From 784e2ae56b07853265ee978defa0060e808cb2b7 Mon Sep 17 00:00:00 2001 From: HugeFrog24 <62775760+HugeFrog24@users.noreply.github.com> Date: Sat, 17 Oct 2026 21:17:28 +0200 Subject: [PATCH 2/2] Add iOS-style headphones while another app plays music In the TGC Android build the native SystemIO check for other audio is a stub that always returns false, so the TuneBand headphones and the music-bus fade never trigger. Like the NetEase build, Canvas now answers it from Java: an AudioPlaybackCallback reports active USAGE_MEDIA players, not counting Canvas's own ExoPlayer, once a USAGE_GAME player has been seen; otherwise the device is reported unsupported. The native side (Core/OtherAudio) locates the headphones update, SystemIO and the slot by instruction shape with no per-build offsets, refuses and logs on any mismatch, and answers only the headphones call site, so FMOD init and recording are unchanged. Launcher setting in General, off by default, with a not-supported line on the row; strings in all locales. Host tests in app/src/test/cpp use fixtures from real 0.33.8 to 0.35.1 images; set OTHER_AUDIO_IMAGES to scan full libBootloader.so files. --- app/src/main/cpp/Core/OtherAudio/OtherAudio.h | 269 ++++++++++++ .../cpp/Core/OtherAudio/OtherAudioApply.cpp | 254 ++++++++++++ .../cpp/Core/OtherAudio/OtherAudioLocate.cpp | 155 +++++++ .../cpp/Core/OtherAudio/OtherAudioLocate.h | 22 + .../main/cpp/Core/OtherAudio/OtherAudioSwap.h | 154 +++++++ app/src/main/cpp/main.cpp | 3 + .../java/com/tgc/sky/ExoplayerService.java | 2 + .../java/com/tgc/sky/SystemIO_android.java | 136 ++++++ .../main/java/com/tgc/sky/VideoListener.java | 5 + .../artdeell/skymodloader/MainActivity.java | 4 + .../skymodloader/SettingsActivity.java | 14 + app/src/main/res/layout/setting_layout.xml | 49 +++ app/src/main/res/values-ar/strings.xml | 3 + app/src/main/res/values-de/strings.xml | 3 + app/src/main/res/values-es/strings.xml | 3 + app/src/main/res/values-hu/strings.xml | 3 + app/src/main/res/values-id/strings.xml | 3 + app/src/main/res/values-it/strings.xml | 3 + app/src/main/res/values-ja/strings.xml | 3 + app/src/main/res/values-pt/strings.xml | 3 + app/src/main/res/values-ru/strings.xml | 3 + app/src/main/res/values-tl/strings.xml | 3 + app/src/main/res/values-vi/strings.xml | 3 + app/src/main/res/values/strings.xml | 3 + app/src/test/cpp/CMakeLists.txt | 16 + app/src/test/cpp/OtherAudioFixtures.h | 49 +++ app/src/test/cpp/OtherAudioSwapFixtures.h | 38 ++ app/src/test/cpp/OtherAudioSwapTest.cpp | 392 ++++++++++++++++++ app/src/test/cpp/OtherAudioTest.cpp | 221 ++++++++++ 29 files changed, 1819 insertions(+) create mode 100644 app/src/main/cpp/Core/OtherAudio/OtherAudio.h create mode 100644 app/src/main/cpp/Core/OtherAudio/OtherAudioApply.cpp create mode 100644 app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.cpp create mode 100644 app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.h create mode 100644 app/src/main/cpp/Core/OtherAudio/OtherAudioSwap.h create mode 100644 app/src/test/cpp/OtherAudioFixtures.h create mode 100644 app/src/test/cpp/OtherAudioSwapFixtures.h create mode 100644 app/src/test/cpp/OtherAudioSwapTest.cpp create mode 100644 app/src/test/cpp/OtherAudioTest.cpp diff --git a/app/src/main/cpp/Core/OtherAudio/OtherAudio.h b/app/src/main/cpp/Core/OtherAudio/OtherAudio.h new file mode 100644 index 0000000..ec517bb --- /dev/null +++ b/app/src/main/cpp/Core/OtherAudio/OtherAudio.h @@ -0,0 +1,269 @@ +#pragma once + +#include +#include + +namespace OtherAudio { + +struct Chain { + std::uintptr_t site = 0; + std::uint32_t slot = 0; + std::uintptr_t gateReturn = 0; + std::uint32_t flagOffset = 0; + std::uintptr_t accessor = 0; + std::uintptr_t audioGlobal = 0; + std::uint32_t fadeSlot = 0; + std::uintptr_t fadeReturn = 0; +}; + +struct FmodSelect { + std::uintptr_t site = 0; + std::uint32_t slot = 0; + std::uint32_t systemIoOffset = 0; +}; + +struct Setter { + std::uintptr_t site = 0; + std::uint32_t systemIoOffset = 0; + std::uintptr_t systemIoGlobal = 0; +}; + +namespace Detail { + +inline std::uint32_t Rd(std::uint32_t w) { return w & 31u; } +inline std::uint32_t Rn(std::uint32_t w) { return (w >> 5) & 31u; } +inline std::uint32_t Rm(std::uint32_t w) { return (w >> 16) & 31u; } +inline std::uint32_t Imm12(std::uint32_t w) { return (w >> 10) & 0xfffu; } + +inline bool Strb(std::uint32_t w) { return (w >> 22) == 0xe4u; } +inline bool Ldrb(std::uint32_t w) { return (w >> 22) == 0xe5u; } +inline bool LdrX(std::uint32_t w) { return (w >> 22) == 0x3e5u; } +inline bool Blr(std::uint32_t w) { return (w & 0xfffffc1fu) == 0xd63f0000u; } +inline bool Bl(std::uint32_t w) { return (w >> 26) == 0x25u; } +inline bool Adrp(std::uint32_t w) { return (w & 0x9f000000u) == 0x90000000u; } +inline bool AddImm(std::uint32_t w) { return (w & 0xffc00000u) == 0x91000000u; } +inline bool AddImmHi(std::uint32_t w) { return (w & 0xffc00000u) == 0x91400000u; } +inline bool MovzW(std::uint32_t w, std::uint32_t imm) { return (w & 0xffffffe0u) == (0x52800000u | (imm << 5)); } +inline bool DefaultBlock(std::uint32_t w) { return MovzW(w, 0x80) || MovzW(w, 0x100); } + +inline std::uintptr_t BlTarget(std::uint32_t w, std::uintptr_t pc) { + std::int64_t imm = w & 0x3ffffffu; + if (imm & (std::int64_t{1} << 25)) + imm -= std::int64_t{1} << 26; + return pc + static_cast(imm * 4); +} + +inline std::uintptr_t AdrpPage(std::uint32_t w, std::uintptr_t pc) { + std::int64_t page = (static_cast((w >> 5) & 0x7ffffu) << 2) | ((w >> 29) & 3u); + if (page & (std::int64_t{1} << 20)) + page -= std::int64_t{1} << 21; + return (pc & ~static_cast(0xfffu)) + static_cast(page * 4096); +} + +template +bool Words(Read &read, std::uintptr_t at, std::uint32_t *out, int n) { + return read(at, out, sizeof(std::uint32_t) * static_cast(n)); +} + +template +std::uintptr_t AccessorGlobal(Read &read, std::uintptr_t accessor) { + std::uint32_t w[10]; + if (!Words(read, accessor, w, 10)) + return 0; + for (int i = 0; i < 10; ++i) { + if (!Adrp(w[i])) + continue; + for (int j = i + 1; j < 10; ++j) { + if (LdrX(w[j]) && Rd(w[j]) == 0 && Rn(w[j]) == Rd(w[i])) + return AdrpPage(w[i], accessor + 4u * i) + Imm12(w[j]) * 8u; + } + return 0; + } + return 0; +} + +template +bool ChainAt(Read &read, std::uintptr_t at, Chain &out) { + std::uint32_t w[16]; + if (!Words(read, at, w, 16) || !Strb(w[0])) + return false; + const std::uint32_t record = Rn(w[0]), store = Imm12(w[0]); + if (!LdrX(w[1]) || Imm12(w[1]) != 0) + return false; + const std::uint32_t vtReg = Rd(w[1]); + if (!LdrX(w[2]) || Rn(w[2]) != vtReg || !Blr(w[3]) || Rn(w[3]) != Rd(w[2])) + return false; + if (!Ldrb(w[4]) || Rn(w[4]) != record || Imm12(w[4]) != store) + return false; + const std::uint32_t u = Rd(w[4]); + if (w[5] != (0x7100001fu | (u << 5)) || w[6] != (0x1a9f17e0u | u)) + return false; + if ((w[7] & 0xffe0fc00u) != 0x0a000000u || Rd(w[7]) != u || Rn(w[7]) != 0 || Rm(w[7]) != u) + return false; + if (!Strb(w[8]) || Rn(w[8]) != record || Imm12(w[8]) != store + 1 || Rd(w[8]) != u) + return false; + int bl = -1; + for (int i = 9; i < 12 && bl < 0; ++i) + if (Bl(w[i])) + bl = i; + if (bl < 0) + return false; + Chain c; + c.site = at; + c.slot = Imm12(w[2]) * 8u; + c.gateReturn = at + 16u; + c.flagOffset = store + 1; + c.accessor = BlTarget(w[bl], at + 4u * bl); + c.audioGlobal = AccessorGlobal(read, c.accessor); + if (c.audioGlobal == 0) + return false; + std::uint32_t audioVt = 32, fadeSlotReg = 32; + bool flagArg = false, indexArg = false; + for (int i = bl + 1; i < 16; ++i) { + const std::uint32_t x = w[i]; + if (LdrX(x) && Rn(x) == 0 && Imm12(x) == 0) + audioVt = Rd(x); + else if (Ldrb(x) && Rd(x) == 2 && Rn(x) == record && Imm12(x) == c.flagOffset) + flagArg = true; + else if (MovzW(x, 1) && Rd(x) == 1) + indexArg = true; + else if (LdrX(x) && Rn(x) == audioVt) + c.fadeSlot = Imm12(x) * 8u, fadeSlotReg = Rd(x); + else if (Blr(x) && Rn(x) == fadeSlotReg) { + if (!flagArg || !indexArg) + return false; + c.fadeReturn = at + 4u * (i + 1); + out = c; + return true; + } + } + return false; +} + +template +bool FmodAt(Read &read, std::uintptr_t at, FmodSelect &out) { + std::uint32_t w[3]; + if (!Words(read, at, w, 3) || !DefaultBlock(w[0])) + return false; + if (!MovzW(w[1] & ~31u, 0x200) && !MovzW(w[2] & ~31u, 0x200)) + return false; + std::uint32_t p[40]; + const std::uintptr_t from = at - 4u * 40; + if (!Words(read, from, p, 40)) + return false; + const std::uint32_t blr = p[39], slot = p[38], vt = p[37], sio = p[36]; + if (!Blr(blr) || !LdrX(slot) || Rn(blr) != Rd(slot) || !LdrX(vt) || Imm12(vt) != 0 || Rn(slot) != Rd(vt)) + return false; + if (!LdrX(sio) || Rd(sio) != 0 || Rn(vt) != 0) + return false; + const std::uint32_t base = Rn(sio); + for (int j = 35; j >= 0; --j) { + if (!AddImm(p[j]) || Rd(p[j]) != base) + continue; + const std::uint32_t src = Rn(p[j]); + for (int k = j - 1; k >= 0; --k) { + if (AddImmHi(p[k]) && Rd(p[k]) == src) { + if (Rn(p[k]) != 19) + return false; + out.site = at; + out.slot = Imm12(slot) * 8u; + out.systemIoOffset = (Imm12(p[k]) << 12) + Imm12(p[j]) + Imm12(sio) * 8u; + return true; + } + } + return false; + } + return false; +} + +inline bool MovReg(std::uint32_t w, std::uint32_t &dst, std::uint32_t &src) { + if ((w & 0xffe0ffe0u) != 0xaa0003e0u) + return false; + dst = Rd(w); + src = Rm(w); + return true; +} + +template +bool SetterAt(Read &read, std::uintptr_t at, Setter &out) { + std::uint32_t w[8]; + if (!Words(read, at, w, 8)) + return false; + std::uint32_t self = 32, value = 32, d = 0, s = 0; + for (int i = 0; i < 3; ++i) { + if (!MovReg(w[i], d, s)) + return false; + if (s == 0) + self = d; + else if (s == 1 && d != 0) + value = d; + } + if (self == 32 || value == 32 || !Bl(w[3])) + return false; + const std::uintptr_t callee = BlTarget(w[3], at + 12u); + std::uint32_t c[6]; + if (!Words(read, callee, c, 6)) + return false; + std::uintptr_t global = 0; + for (int i = 0; i < 5 && global == 0; ++i) { + if (Adrp(c[i]) && (c[i + 1] >> 22) == 0x3e4u && Rd(c[i + 1]) == 0 && Rn(c[i + 1]) == Rd(c[i])) + global = AdrpPage(c[i], callee + 4u * i) + Imm12(c[i + 1]) * 8u; + } + if (global == 0) + return false; + const std::uint32_t lo = w[4], hi = w[5], st = w[6]; + if ((lo & 0xffe00000u) != 0x52800000u || (hi & 0xffe00000u) != 0x72a00000u || Rd(hi) != Rd(lo)) + return false; + if ((st & 0xffe0fc00u) != 0xf8206800u || Rm(st) != Rd(lo) || Rn(st) != self || Rd(st) != value) + return false; + out.site = at; + out.systemIoOffset = ((hi >> 5) & 0xffffu) << 16 | ((lo >> 5) & 0xffffu); + out.systemIoGlobal = global; + return true; +} + +} + +template +int FindSetter(Read &&read, std::uintptr_t begin, std::size_t size, Setter &out) { + int found = 0; + for (std::size_t i = 0; i + 32 <= size; i += 4) { + std::uint32_t w = 0; + if (!read(begin + i, &w, sizeof(w)) || (w & 0xffe0ffe0u) != 0xaa0003e0u) + continue; + Setter s; + if (Detail::SetterAt(read, begin + i, s) && ++found == 1) + out = s; + } + return found; +} + +template +int FindChain(Read &&read, std::uintptr_t begin, std::size_t size, Chain &out) { + int found = 0; + for (std::size_t i = 0; i + 64 <= size; i += 4) { + std::uint32_t w = 0; + if (!read(begin + i, &w, sizeof(w)) || !Detail::Strb(w)) + continue; + Chain c; + if (Detail::ChainAt(read, begin + i, c) && ++found == 1) + out = c; + } + return found; +} + +template +int FindFmod(Read &&read, std::uintptr_t begin, std::size_t size, FmodSelect &out) { + int found = 0; + for (std::size_t i = 160; i + 12 <= size; i += 4) { + std::uint32_t w = 0; + if (!read(begin + i, &w, sizeof(w)) || !Detail::DefaultBlock(w)) + continue; + FmodSelect f; + if (Detail::FmodAt(read, begin + i, f) && ++found == 1) + out = f; + } + return found; +} + +} diff --git a/app/src/main/cpp/Core/OtherAudio/OtherAudioApply.cpp b/app/src/main/cpp/Core/OtherAudio/OtherAudioApply.cpp new file mode 100644 index 0000000..a282d0f --- /dev/null +++ b/app/src/main/cpp/Core/OtherAudio/OtherAudioApply.cpp @@ -0,0 +1,254 @@ +#include "OtherAudioLocate.h" +#include "OtherAudioSwap.h" + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "../../include/misc/Logger.h" + +namespace { + +constexpr int kAttempts = 120; + +struct Mapping { + std::uintptr_t begin; + std::uintptr_t end; + char perms[5]; +}; + +std::vector ReadMaps() { + std::vector maps; + FILE *f = std::fopen("/proc/self/maps", "re"); + if (f == nullptr) + return maps; + char *line = nullptr; + std::size_t capacity = 0; + while (getline(&line, &capacity, f) != -1) { + unsigned long begin = 0, end = 0; + Mapping m{}; + if (std::sscanf(line, "%lx-%lx %4s", &begin, &end, m.perms) != 3) + continue; + m.begin = begin; + m.end = end; + maps.push_back(m); + } + std::free(line); + std::fclose(f); + return maps; +} + +const Mapping *Find(const std::vector &maps, std::uintptr_t at, std::size_t size) { + for (const Mapping &m : maps) + if (at >= m.begin && at < m.end && m.end - at >= size) + return &m; + return nullptr; +} + +struct Search { + std::uintptr_t base; + bool found; + std::vector segments; +}; + +int CollectSegments(dl_phdr_info *info, std::size_t, void *data) { + auto *search = static_cast(data); + if (info->dlpi_addr != search->base) + return 0; + search->found = true; + for (int i = 0; i < info->dlpi_phnum; ++i) { + const ElfW(Phdr) &ph = info->dlpi_phdr[i]; + OtherAudio::Segment s; + s.type = ph.p_type; + s.flags = ph.p_flags; + s.vaddr = static_cast(ph.p_vaddr); + s.memsz = static_cast(ph.p_memsz); + search->segments.push_back(s); + } + return 1; +} + +std::atomic g_enabled{false}; +std::atomic g_gate{0}; +std::mutex g_mutex; +bool g_started = false; + +bool Replacement(void *) { + return OtherAudio::Answer(reinterpret_cast(__builtin_return_address(0)), + g_gate.load(std::memory_order_acquire), g_enabled.load(std::memory_order_relaxed), + OtherAudio::IsPlaying()); +} + +void *Ptr(std::uintptr_t value) { + return reinterpret_cast(value); +} + +void Refuse(const OtherAudio::Plan &plan) { + using OtherAudio::Decision; + switch (plan.decision) { + case Decision::Disabled: + LOGW("Other audio: setting off, not swapping"); + break; + case Decision::NotLocated: + LOGW("Other audio: update site not located, not swapping"); + break; + case Decision::NotReady: + LOGW("Other audio: %p still null after %d attempts, not swapping", Ptr(plan.at), kAttempts); + break; + case Decision::Unreadable: + LOGW("Other audio: %p unreadable, not swapping", Ptr(plan.at)); + break; + case Decision::SystemIoMismatch: + LOGW("Other audio: SystemIO routes disagree (%p from audio, %p from global), not swapping", + Ptr(plan.systemIo), Ptr(plan.otherSystemIo)); + break; + case Decision::NotAVtable: + LOGW("Other audio: SystemIO %p points at %p, not a vtable address point, not swapping", Ptr(plan.systemIo), + Ptr(plan.vtable)); + break; + case Decision::SlotOutsideRelro: + LOGW("Other audio: slot %p is outside RELRO, not swapping", Ptr(plan.slotAddress)); + break; + case Decision::TargetOutsideCode: + LOGW("Other audio: slot %p holds %p, outside the game's code, not swapping", Ptr(plan.slotAddress), + Ptr(plan.current)); + break; + case Decision::NotTheStub: + LOGW("Other audio: slot %p holds %p, not the return-false stub, not swapping", Ptr(plan.slotAddress), + Ptr(plan.current)); + break; + case Decision::Swap: + case Decision::AlreadyApplied: + break; + } +} + +bool Write(const OtherAudio::Plan &plan, std::uintptr_t replacement, const std::vector &maps) { + const Mapping *m = Find(maps, plan.slotAddress, sizeof(std::uintptr_t)); + if (m == nullptr || std::strcmp(m->perms, "r--p") != 0) { + LOGW("Other audio: slot %p is mapped %s, expected r--p, not swapping", Ptr(plan.slotAddress), + m ? m->perms : "nowhere"); + return false; + } + const long pageSize = sysconf(_SC_PAGESIZE); + if (pageSize <= 0) { + LOGW("Other audio: page size unknown, not swapping"); + return false; + } + const std::uintptr_t page = plan.slotAddress & ~(static_cast(pageSize) - 1); + if (page < m->begin || m->end - page < static_cast(pageSize)) { + LOGW("Other audio: page %p is not inside one mapping, not swapping", Ptr(page)); + return false; + } + if (mprotect(Ptr(page), static_cast(pageSize), PROT_READ | PROT_WRITE) != 0) { + LOGW("Other audio: mprotect read-write at %p failed (errno %d), not swapping", Ptr(page), errno); + return false; + } + std::uintptr_t expected = plan.current; + const bool swapped = __atomic_compare_exchange_n(reinterpret_cast(plan.slotAddress), &expected, + replacement, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); + if (mprotect(Ptr(page), static_cast(pageSize), PROT_READ) != 0) + LOGW("Other audio: mprotect read-only at %p failed (errno %d), page left writable", Ptr(page), errno); + if (!swapped) { + LOGW("Other audio: slot %p changed to %p before the swap, not swapping", Ptr(plan.slotAddress), + Ptr(expected)); + return false; + } + std::uintptr_t written = 0; + std::memcpy(&written, reinterpret_cast(plan.slotAddress), sizeof(written)); + if (written != replacement) { + LOGW("Other audio: swap at %p did not stick (%p)", Ptr(plan.slotAddress), Ptr(written)); + return false; + } + return true; +} + +bool Attempt(OtherAudio::Plan &plan) { + const OtherAudio::Located *located = OtherAudio::Result(); + OtherAudio::Image image; + if (located != nullptr) { + Search search{located->base, false, {}}; + dl_iterate_phdr(CollectSegments, &search); + if (!search.found || !OtherAudio::Layout(located->base, search.segments.data(), + static_cast(search.segments.size()), image)) { + LOGW("Other audio: game library layout unexpected (found %d, %zu segments), not swapping", + search.found ? 1 : 0, search.segments.size()); + return true; + } + } + + const std::vector maps = ReadMaps(); + auto read = [&maps](std::uintptr_t at, void *out, std::size_t size) { + const Mapping *m = Find(maps, at, size); + if (m == nullptr || m->perms[0] != 'r') + return false; + std::memcpy(out, reinterpret_cast(at), size); + return true; + }; + const std::uintptr_t replacement = reinterpret_cast(&Replacement); + plan = OtherAudio::Decide(read, located, g_enabled.load(std::memory_order_relaxed), image, replacement); + + switch (plan.decision) { + case OtherAudio::Decision::NotReady: + return false; + case OtherAudio::Decision::AlreadyApplied: + g_gate.store(located->chain.gateReturn, std::memory_order_release); + LOGI("Other audio: SystemIO slot already swapped at %p", Ptr(plan.slotAddress)); + return true; + case OtherAudio::Decision::Swap: + break; + default: + Refuse(plan); + return true; + } + + g_gate.store(located->chain.gateReturn, std::memory_order_release); + if (Write(plan, replacement, maps)) + LOGI("Other audio: SystemIO slot 0x%x swapped at %p (vtable RVA 0x%lx, stub RVA 0x%lx), gate RVA 0x%lx", + located->chain.slot, Ptr(plan.slotAddress), static_cast(plan.vtable - located->base), + static_cast(plan.current - located->base), + static_cast(located->chain.gateReturn - located->base)); + return true; +} + +void Run() { + OtherAudio::Plan plan; + for (int attempt = 0; attempt < kAttempts; ++attempt) { + if (Attempt(plan)) + return; + sleep(1); + } + Refuse(plan); +} + +void Start() { + std::lock_guard lock(g_mutex); + if (g_started) + return; + g_started = true; + try { + std::thread(Run).detach(); + } catch (const std::system_error &e) { + LOGW("Other audio: worker thread not started (%s), not swapping", e.what()); + } +} + +} + +extern "C" JNIEXPORT void JNICALL +Java_git_artdeell_skymodloader_MainActivity_nativeSetOtherAudioEnabled(JNIEnv *, jclass, jboolean enabled) { + g_enabled.store(enabled == JNI_TRUE, std::memory_order_relaxed); + if (enabled == JNI_TRUE) + Start(); +} diff --git a/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.cpp b/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.cpp new file mode 100644 index 0000000..97b56d2 --- /dev/null +++ b/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.cpp @@ -0,0 +1,155 @@ +#include "OtherAudioLocate.h" + +#include +#include + +#include +#include +#include +#include + +#include "../../include/misc/Logger.h" + +namespace { + +struct Range { + std::uintptr_t begin; + std::size_t size; + bool executable; +}; + +struct Search { + const char *libName; + std::uintptr_t base; + std::vector ranges; +}; + +bool EndsWith(const char *text, const char *suffix) { + const std::size_t a = std::strlen(text), b = std::strlen(suffix); + return a >= b && std::strcmp(text + a - b, suffix) == 0; +} + +int CollectRanges(dl_phdr_info *info, std::size_t, void *data) { + auto *search = static_cast(data); + if (info->dlpi_name == nullptr || !EndsWith(info->dlpi_name, search->libName)) + return 0; + search->base = info->dlpi_addr; + for (int i = 0; i < info->dlpi_phnum; ++i) { + const ElfW(Phdr) &ph = info->dlpi_phdr[i]; + if (ph.p_type != PT_LOAD || (ph.p_flags & PF_R) == 0) + continue; + search->ranges.push_back({info->dlpi_addr + ph.p_vaddr, static_cast(ph.p_filesz), + (ph.p_flags & PF_X) != 0}); + } + return 1; +} + +std::mutex g_mutex; +bool g_done = false; +bool g_found = false; +OtherAudio::Located g_result; +std::atomic g_playing{false}; + +} + +namespace OtherAudio { + +bool Locate(const char *libName) { + std::lock_guard lock(g_mutex); + if (g_done) + return g_found; + + Search search{libName, 0, {}}; + dl_iterate_phdr(CollectRanges, &search); + if (search.ranges.empty()) { + LOGD("Other audio: %s not loaded yet", libName); + return false; + } + g_done = true; + + auto read = [&search](std::uintptr_t at, void *out, std::size_t size) { + for (const Range &range : search.ranges) { + if (at >= range.begin && size <= range.size && at - range.begin <= range.size - size) { + std::memcpy(out, reinterpret_cast(at), size); + return true; + } + } + return false; + }; + + int chains = 0, fmods = 0, setters = 0; + Chain chain; + FmodSelect fmod; + Setter setter; + for (const Range &range : search.ranges) { + if (!range.executable) + continue; + Chain c; + FmodSelect f; + Setter s; + const int nc = FindChain(read, range.begin, range.size, c); + const int nf = FindFmod(read, range.begin, range.size, f); + const int ns = FindSetter(read, range.begin, range.size, s); + if (nc && !chains) + chain = c; + if (nf && !fmods) + fmod = f; + if (ns && !setters) + setter = s; + chains += nc; + fmods += nf; + setters += ns; + } + + if (chains != 1) { + LOGW("Other audio: %d headphone update sites, not supported", chains); + return false; + } + if (fmods > 1 || setters > 1 || (fmods == 0 && setters == 0)) { + LOGW("Other audio: SystemIO not located (fmod %d, setter %d)", fmods, setters); + return false; + } + if (fmods == 1 && fmod.slot != chain.slot) { + LOGW("Other audio: slot mismatch (update 0x%x, fmod 0x%x)", chain.slot, fmod.slot); + return false; + } + if (fmods == 1 && setters == 1 && fmod.systemIoOffset != setter.systemIoOffset) { + LOGW("Other audio: SystemIO offset mismatch (fmod 0x%x, setter 0x%x)", fmod.systemIoOffset, + setter.systemIoOffset); + return false; + } + + g_result.base = search.base; + g_result.chain = chain; + g_result.systemIoOffset = fmods == 1 ? fmod.systemIoOffset : setter.systemIoOffset; + g_result.systemIoGlobal = setters == 1 ? setter.systemIoGlobal : 0; + g_found = true; + LOGI("Other audio: update RVA 0x%lx slot 0x%x gate RVA 0x%lx flag +0x%x fade slot 0x%x RVA 0x%lx", + static_cast(chain.site - search.base), chain.slot, + static_cast(chain.gateReturn - search.base), chain.flagOffset, chain.fadeSlot, + static_cast(chain.fadeReturn - search.base)); + LOGI("Other audio: audio global RVA 0x%lx SystemIO at +0x%x, SystemIO global RVA 0x%lx", + static_cast(chain.audioGlobal - search.base), g_result.systemIoOffset, + static_cast(g_result.systemIoGlobal ? g_result.systemIoGlobal - search.base : 0)); + return true; +} + +const Located *Result() { + std::lock_guard lock(g_mutex); + return g_found ? &g_result : nullptr; +} + +void SetPlaying(bool playing) { + g_playing.store(playing, std::memory_order_relaxed); +} + +bool IsPlaying() { + return g_playing.load(std::memory_order_relaxed); +} + +} + +extern "C" JNIEXPORT void JNICALL +Java_git_artdeell_skymodloader_MainActivity_nativeSetOtherAudioPlaying(JNIEnv *, jclass, jboolean playing) { + OtherAudio::SetPlaying(playing == JNI_TRUE); +} diff --git a/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.h b/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.h new file mode 100644 index 0000000..e310943 --- /dev/null +++ b/app/src/main/cpp/Core/OtherAudio/OtherAudioLocate.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#include "OtherAudio.h" + +namespace OtherAudio { + +struct Located { + std::uintptr_t base = 0; + Chain chain; + std::uint32_t systemIoOffset = 0; + std::uintptr_t systemIoGlobal = 0; +}; + +bool Locate(const char *libName); +const Located *Result(); + +void SetPlaying(bool playing); +bool IsPlaying(); + +} diff --git a/app/src/main/cpp/Core/OtherAudio/OtherAudioSwap.h b/app/src/main/cpp/Core/OtherAudio/OtherAudioSwap.h new file mode 100644 index 0000000..34e5f97 --- /dev/null +++ b/app/src/main/cpp/Core/OtherAudio/OtherAudioSwap.h @@ -0,0 +1,154 @@ +#pragma once + +#include +#include +#include + +#include "OtherAudioLocate.h" + +namespace OtherAudio { + +constexpr std::uint32_t kReturnFalseStub[5] = {0xa9bf7bfdu, 0x910003fdu, 0x2a1f03e0u, 0xa8c17bfdu, 0xd65f03c0u}; +constexpr std::uint32_t kPtLoad = 1; +constexpr std::uint32_t kPtGnuRelro = 0x6474e552u; +constexpr std::uint32_t kPfX = 1; + +struct Segment { + std::uint32_t type = 0; + std::uint32_t flags = 0; + std::uintptr_t vaddr = 0; + std::uintptr_t memsz = 0; +}; + +struct Image { + std::uintptr_t codeBegin = 0; + std::uintptr_t codeEnd = 0; + std::uintptr_t relroBegin = 0; + std::uintptr_t relroEnd = 0; +}; + +inline bool Layout(std::uintptr_t base, const Segment *segments, int count, Image &out) { + int code = 0, relro = 0; + Image image; + for (int i = 0; i < count; ++i) { + const Segment &s = segments[i]; + if (s.type == kPtLoad && (s.flags & kPfX) != 0) { + image.codeBegin = base + s.vaddr; + image.codeEnd = image.codeBegin + s.memsz; + ++code; + } else if (s.type == kPtGnuRelro) { + image.relroBegin = base + s.vaddr; + image.relroEnd = image.relroBegin + s.memsz; + ++relro; + } + } + if (code != 1 || relro != 1 || image.codeBegin >= image.codeEnd || image.relroBegin >= image.relroEnd) + return false; + out = image; + return true; +} + +enum class Decision { + Swap, + AlreadyApplied, + Disabled, + NotLocated, + NotReady, + Unreadable, + SystemIoMismatch, + NotAVtable, + SlotOutsideRelro, + TargetOutsideCode, + NotTheStub, +}; + +struct Plan { + Decision decision = Decision::NotLocated; + std::uintptr_t at = 0; + std::uintptr_t systemIo = 0; + std::uintptr_t otherSystemIo = 0; + std::uintptr_t vtable = 0; + std::uintptr_t slotAddress = 0; + std::uintptr_t current = 0; +}; + +namespace Detail { + +inline Plan Stop(Plan plan, Decision decision, std::uintptr_t at) { + plan.decision = decision; + plan.at = at; + return plan; +} + +inline bool Inside(std::uintptr_t at, std::size_t size, std::uintptr_t begin, std::uintptr_t end) { + return at >= begin && at < end && end - at >= size; +} + +template +bool Pointer(Read &read, std::uintptr_t at, std::uintptr_t &out) { + return read(at, &out, sizeof(out)); +} + +} + +template +Plan Decide(Read &&read, const Located *located, bool enabled, const Image &image, std::uintptr_t replacement) { + using Detail::Stop; + Plan plan; + if (!enabled) + return Stop(plan, Decision::Disabled, 0); + if (located == nullptr) + return Stop(plan, Decision::NotLocated, 0); + + const Chain &chain = located->chain; + std::uintptr_t audio = 0; + if (!Detail::Pointer(read, chain.audioGlobal, audio)) + return Stop(plan, Decision::Unreadable, chain.audioGlobal); + if (audio == 0) + return Stop(plan, Decision::NotReady, chain.audioGlobal); + const std::uintptr_t field = audio + located->systemIoOffset; + if (!Detail::Pointer(read, field, plan.systemIo)) + return Stop(plan, Decision::Unreadable, field); + if (plan.systemIo == 0) + return Stop(plan, Decision::NotReady, field); + if (located->systemIoGlobal != 0) { + if (!Detail::Pointer(read, located->systemIoGlobal, plan.otherSystemIo)) + return Stop(plan, Decision::Unreadable, located->systemIoGlobal); + if (plan.otherSystemIo == 0) + return Stop(plan, Decision::NotReady, located->systemIoGlobal); + if (plan.otherSystemIo != plan.systemIo) + return Stop(plan, Decision::SystemIoMismatch, located->systemIoGlobal); + } + + if (!Detail::Pointer(read, plan.systemIo, plan.vtable)) + return Stop(plan, Decision::Unreadable, plan.systemIo); + std::uintptr_t header[2] = {1, 1}; + if (plan.vtable % sizeof(std::uintptr_t) != 0 || plan.vtable < sizeof(header)) + return Stop(plan, Decision::NotAVtable, plan.vtable); + if (!read(plan.vtable - sizeof(header), header, sizeof(header))) + return Stop(plan, Decision::Unreadable, plan.vtable - sizeof(header)); + if (header[0] != 0 || header[1] != 0) + return Stop(plan, Decision::NotAVtable, plan.vtable); + + plan.slotAddress = plan.vtable + chain.slot; + if (!Detail::Inside(plan.slotAddress, sizeof(std::uintptr_t), image.relroBegin, image.relroEnd)) + return Stop(plan, Decision::SlotOutsideRelro, plan.slotAddress); + if (!Detail::Pointer(read, plan.slotAddress, plan.current)) + return Stop(plan, Decision::Unreadable, plan.slotAddress); + if (plan.current == replacement) + return Stop(plan, Decision::AlreadyApplied, plan.slotAddress); + if (!Detail::Inside(plan.current, sizeof(kReturnFalseStub), image.codeBegin, image.codeEnd)) + return Stop(plan, Decision::TargetOutsideCode, plan.current); + std::uint32_t words[5] = {}; + if (!read(plan.current, words, sizeof(words))) + return Stop(plan, Decision::Unreadable, plan.current); + if (std::memcmp(words, kReturnFalseStub, sizeof(words)) != 0) + return Stop(plan, Decision::NotTheStub, plan.current); + return Stop(plan, Decision::Swap, plan.slotAddress); +} + +inline bool Answer(std::uintptr_t returnAddress, std::uintptr_t gate, bool enabled, bool playing) { + return enabled && playing && gate != 0 && returnAddress == gate; +} + +} diff --git a/app/src/main/cpp/main.cpp b/app/src/main/cpp/main.cpp index 0e28ffe..e643a5c 100644 --- a/app/src/main/cpp/main.cpp +++ b/app/src/main/cpp/main.cpp @@ -24,6 +24,7 @@ #include #include "shadowhook.h" #include "ChatFlagFix/ChatFlagFixInstall.h" +#include "Core/OtherAudio/OtherAudioLocate.h" extern "C" { #include "Core/ceserver/ceserver.h" @@ -237,6 +238,7 @@ int main() { // changing that timing is a separate decision from moving the hook. install_chat_fix(); + OtherAudio::Locate(Canvas::libName); return 0; } @@ -273,6 +275,7 @@ Java_git_artdeell_skymodloader_MainActivity_settle( } install_chat_fix(); + OtherAudio::Locate(Canvas::libName ? Canvas::libName : "libBootloader.so"); } diff --git a/app/src/main/java/com/tgc/sky/ExoplayerService.java b/app/src/main/java/com/tgc/sky/ExoplayerService.java index 7027208..eae50f1 100644 --- a/app/src/main/java/com/tgc/sky/ExoplayerService.java +++ b/app/src/main/java/com/tgc/sky/ExoplayerService.java @@ -43,6 +43,7 @@ class ExoplayerService { // Written by VideoListener.onPlaybackStateChanged. TGC stores it but never // reads it; kept for parity. int m_state; + volatile boolean m_audible; public void Initialize(Context context) { if (this.m_player == null) { @@ -83,6 +84,7 @@ public void Terminate() { exoPlayer.release(); this.m_player = null; } + this.m_audible = false; ImageReader imageReader = this.m_imageReader; if (imageReader != null) { imageReader.close(); diff --git a/app/src/main/java/com/tgc/sky/SystemIO_android.java b/app/src/main/java/com/tgc/sky/SystemIO_android.java index a47a72b..d40daac 100644 --- a/app/src/main/java/com/tgc/sky/SystemIO_android.java +++ b/app/src/main/java/com/tgc/sky/SystemIO_android.java @@ -8,12 +8,16 @@ import android.content.SharedPreferences; import android.content.res.AssetManager; import android.hardware.HardwareBuffer; +import android.media.AudioAttributes; import android.media.AudioDeviceInfo; import android.media.AudioManager; +import android.media.AudioPlaybackConfiguration; import android.net.ConnectivityManager; import android.os.Build; import android.os.Debug; import android.os.Environment; +import android.os.Handler; +import android.os.Looper; import android.os.StatFs; import android.util.Log; import com.tgc.sky.io.AudioDeviceType; @@ -28,6 +32,7 @@ import java.net.HttpURLConnection; import java.net.URL; import java.util.ArrayList; +import java.util.List; import java.util.UUID; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; @@ -56,6 +61,16 @@ public class SystemIO_android { private Future mSubtitleRequest; public float m_batteryLevel; private boolean m_isOtherAudioPlaying; + private AudioManager.AudioPlaybackCallback m_otherAudioCallback; + private static final int OTHER_AUDIO_SELF_CHECK_MS = 30000; + private boolean m_otherAudioSupported; + private boolean m_otherAudioCheckFinished; + private Handler m_otherAudioHandler; + private final Runnable m_otherAudioSelfCheck = new Runnable() { + public void run() { + SystemIO_android.this.m_finishOtherAudioSelfCheck(); + } + }; private boolean m_isPhonecallActive; private boolean m_networkReachableByCellular; private boolean m_networkReachableByWifi; @@ -203,6 +218,7 @@ public void onResume() { exoplayerService.SetVolume(this.mExoplayerPauseVolume); } this.mIsPaused = false; + m_scheduleOtherAudioSelfCheck(); } @@ -214,11 +230,25 @@ public void onPause() { if (exoplayerService != null) { exoplayerService.SetVolume(0.0f); } + if (this.m_otherAudioHandler != null) { + this.m_otherAudioHandler.removeCallbacks(this.m_otherAudioSelfCheck); + } } /* access modifiers changed from: package-private */ public void onDestroy() { + if (this.m_otherAudioHandler != null) { + this.m_otherAudioHandler.removeCallbacks(this.m_otherAudioSelfCheck); + } + if (this.m_otherAudioCallback != null) { + AudioManager audioManager = (AudioManager) this.m_activity.getSystemService(Context.AUDIO_SERVICE); + if (audioManager != null) { + audioManager.unregisterAudioPlaybackCallback(this.m_otherAudioCallback); + } + this.m_otherAudioCallback = null; + } + git.artdeell.skymodloader.MainActivity.nativeSetOtherAudioPlaying(false); } public int GetPlatformInt() { @@ -758,6 +788,112 @@ public void onReceive(Context context, Intent intent) { SystemIO_android.this.m_onAudioDeviceChanged(audioManager, intent); } }, intentFilter)); + m_initializeOtherAudioMonitor(audioManager); + } + + private void m_initializeOtherAudioMonitor(AudioManager audioManager) { + boolean enabled = this.m_activity.getSharedPreferences("package_configs", Context.MODE_PRIVATE) + .getBoolean("other_audio_headphones", false); + if (!enabled || audioManager == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { + if (enabled) { + Log.w(TAG, "Other audio: playback monitor unavailable on Android " + Build.VERSION.SDK_INT + ", not supported"); + } + git.artdeell.skymodloader.MainActivity.nativeSetOtherAudioPlaying(false); + return; + } + this.m_otherAudioHandler = new Handler(Looper.getMainLooper()); + m_updateOtherAudioPlaying(audioManager.getActivePlaybackConfigurations()); + this.m_otherAudioCallback = new AudioManager.AudioPlaybackCallback() { + @Override + public void onPlaybackConfigChanged(List configs) { + SystemIO_android.this.m_updateOtherAudioPlaying(configs); + } + }; + audioManager.registerAudioPlaybackCallback(this.m_otherAudioCallback, null); + } + + void m_onOwnPlaybackChanged() { + if (this.m_otherAudioCallback == null) { + return; + } + AudioManager audioManager = (AudioManager) this.m_activity.getSystemService(Context.AUDIO_SERVICE); + if (audioManager != null) { + m_updateOtherAudioPlaying(audioManager.getActivePlaybackConfigurations()); + } + } + + private void m_updateOtherAudioPlaying(List configs) { + int media = 0; + boolean game = false; + if (configs != null) { + for (AudioPlaybackConfiguration config : configs) { + AudioAttributes attributes = config.getAudioAttributes(); + if (attributes == null) { + continue; + } + if (attributes.getUsage() == AudioAttributes.USAGE_MEDIA) { + media++; + } else if (attributes.getUsage() == AudioAttributes.USAGE_GAME) { + game = true; + } + } + } + if (game && !this.m_otherAudioSupported) { + m_setOtherAudioSupported(); + } + ExoplayerService ownPlayer = this.mExoplayer; + int own = ownPlayer != null && ownPlayer.m_audible ? 1 : 0; + boolean playing = this.m_otherAudioSupported && media > own; + this.m_isOtherAudioPlaying = playing; + git.artdeell.skymodloader.MainActivity.nativeSetOtherAudioPlaying(playing); + } + + private void m_setOtherAudioSupported() { + this.m_otherAudioSupported = true; + this.m_otherAudioCheckFinished = true; + this.m_otherAudioHandler.removeCallbacks(this.m_otherAudioSelfCheck); + Log.i(TAG, "Other audio: USAGE_GAME player seen, detection supported"); + m_storeOtherAudioUnsupported(false); + git.artdeell.skymodloader.MainActivity.nativeSetOtherAudioEnabled(true); + } + + private void m_scheduleOtherAudioSelfCheck() { + if (this.m_otherAudioHandler == null || this.m_otherAudioCheckFinished) { + return; + } + this.m_otherAudioHandler.removeCallbacks(this.m_otherAudioSelfCheck); + this.m_otherAudioHandler.postDelayed(this.m_otherAudioSelfCheck, OTHER_AUDIO_SELF_CHECK_MS); + } + + private void m_finishOtherAudioSelfCheck() { + if (this.m_otherAudioCheckFinished || this.m_otherAudioCallback == null) { + return; + } + AudioManager audioManager = (AudioManager) this.m_activity.getSystemService(Context.AUDIO_SERVICE); + List configs = audioManager != null ? audioManager.getActivePlaybackConfigurations() : null; + m_updateOtherAudioPlaying(configs); + if (this.m_otherAudioSupported) { + return; + } + StringBuilder usages = new StringBuilder(); + if (configs != null) { + for (AudioPlaybackConfiguration config : configs) { + AudioAttributes attributes = config.getAudioAttributes(); + if (usages.length() > 0) { + usages.append(", "); + } + usages.append(attributes == null ? "none" : String.valueOf(attributes.getUsage())); + } + } + this.m_otherAudioCheckFinished = true; + Log.w(TAG, "Other audio: no USAGE_GAME player after " + (OTHER_AUDIO_SELF_CHECK_MS / 1000) + + " s in the foreground (active usages: [" + usages + "]), detection not supported on this device"); + m_storeOtherAudioUnsupported(true); + } + + private void m_storeOtherAudioUnsupported(boolean unsupported) { + this.m_activity.getSharedPreferences("package_configs", Context.MODE_PRIVATE).edit() + .putBoolean("other_audio_headphones_unsupported", unsupported).apply(); } /* access modifiers changed from: private */ diff --git a/app/src/main/java/com/tgc/sky/VideoListener.java b/app/src/main/java/com/tgc/sky/VideoListener.java index 034c9e5..99956ad 100644 --- a/app/src/main/java/com/tgc/sky/VideoListener.java +++ b/app/src/main/java/com/tgc/sky/VideoListener.java @@ -36,6 +36,11 @@ public void onIsPlayingChanged(boolean z) { if (z) { this.m_retryTime = 0; } + this.m_service.m_audible = z && this.m_player.getAudioFormat() != null; + SystemIO_android systemIO = SystemIO_android.getInstance(); + if (systemIO != null) { + systemIO.m_onOwnPlaybackChanged(); + } } @Override diff --git a/app/src/main/java/git/artdeell/skymodloader/MainActivity.java b/app/src/main/java/git/artdeell/skymodloader/MainActivity.java index 5749ec8..6b89ff5 100644 --- a/app/src/main/java/git/artdeell/skymodloader/MainActivity.java +++ b/app/src/main/java/git/artdeell/skymodloader/MainActivity.java @@ -548,6 +548,10 @@ public static native void nativeSetVideoStats(long durationMs, long positionMs, int isLive, int isSeekable, int playbackState); + public static native void nativeSetOtherAudioPlaying(boolean playing); + + public static native void nativeSetOtherAudioEnabled(boolean enabled); + public static class DeviceInfo { public float xdpi; public float ydpi; diff --git a/app/src/main/java/git/artdeell/skymodloader/SettingsActivity.java b/app/src/main/java/git/artdeell/skymodloader/SettingsActivity.java index 25113c8..e628d90 100644 --- a/app/src/main/java/git/artdeell/skymodloader/SettingsActivity.java +++ b/app/src/main/java/git/artdeell/skymodloader/SettingsActivity.java @@ -33,6 +33,7 @@ public class SettingsActivity extends AppCompatActivity { private static final int REQUEST_EXPORT_BACKUP = 9002; private static final int REQUEST_IMPORT_BACKUP = 9003; private Switch hideCanvasMenuSwitch; + private Switch otherAudioHeadphonesSwitch; private Switch ceserverSwitch; private Switch customServerSwitch; private Switch daybreakerSwitch; @@ -46,6 +47,7 @@ protected void onCreate(Bundle savedInstanceState) { ImageView backButton = findViewById(R.id.back_button); hideCanvasMenuSwitch = findViewById(R.id.mm_hideCanvasMenu); + otherAudioHeadphonesSwitch = findViewById(R.id.mm_otherAudioHeadphones); ceserverSwitch = findViewById(R.id.mm_enableCeserver); customServerSwitch = findViewById(R.id.mm_enableCustomServer); daybreakerSwitch = findViewById(R.id.mm_enableDaybreaker); @@ -61,6 +63,18 @@ protected void onCreate(Bundle savedInstanceState) { .edit().putBoolean("hide_canvas_menu", isChecked).apply() ); + otherAudioHeadphonesSwitch.setChecked(getSharedPreferences("package_configs", MODE_PRIVATE) + .getBoolean("other_audio_headphones", false)); + otherAudioHeadphonesSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> + getSharedPreferences("package_configs", MODE_PRIVATE) + .edit().putBoolean("other_audio_headphones", isChecked).apply() + ); + boolean otherAudioUnsupported = Build.VERSION.SDK_INT < Build.VERSION_CODES.P + || getSharedPreferences("package_configs", MODE_PRIVATE) + .getBoolean("other_audio_headphones_unsupported", false); + findViewById(R.id.mm_otherAudioHeadphonesStatus) + .setVisibility(otherAudioUnsupported ? View.VISIBLE : View.GONE); + ceserverSwitch.setChecked(getSharedPreferences("package_configs", MODE_PRIVATE) .getBoolean("ceserver", false)); ceserverSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> diff --git a/app/src/main/res/layout/setting_layout.xml b/app/src/main/res/layout/setting_layout.xml index dddd9d4..155a51b 100644 --- a/app/src/main/res/layout/setting_layout.xml +++ b/app/src/main/res/layout/setting_layout.xml @@ -89,6 +89,55 @@ tools:ignore="UseSwitchCompatOrMaterialXml" /> + + + + + + + + + + + + + + + عام إخفاء قائمة Canvas + سماعات أثناء الاستماع + عندما يشغّل تطبيق آخر الموسيقى، ترتدي شخصيتك السماعات وتُكتم موسيقى Sky + غير مدعوم على هذا الجهاز خيارات المطور خادم محرك الغش خادم مخصص diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 86cf7a8..7b23545 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -90,6 +90,9 @@ Allgemein Canvas-Menü ausblenden + Kopfhörer beim Musikhören + Wenn eine andere App Musik spielt, trägt dein Avatar Kopfhörer und die Sky-Musik wird stummgeschaltet + Auf diesem Gerät nicht unterstützt Entwickleroptionen Cheat Engine Server Eigener Server diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index a997868..b735dda 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -90,6 +90,9 @@ APP GALLERY | VERSIÓN HUAWEI\nJUGAR General Ocultar menú Canvas + Auriculares al escuchar música + Cuando otra app reproduce música, tu avatar usa auriculares y la música de Sky se silencia + No compatible con este dispositivo Opciones de desarrollador Servidor de Cheat Engine Servidor personalizado diff --git a/app/src/main/res/values-hu/strings.xml b/app/src/main/res/values-hu/strings.xml index aba3402..e391c24 100644 --- a/app/src/main/res/values-hu/strings.xml +++ b/app/src/main/res/values-hu/strings.xml @@ -105,6 +105,9 @@ Általános Canvas menü elrejtése + Fejhallgató zenehallgatáskor + Ha egy másik alkalmazás zenét játszik, az avatarod fejhallgatót visel, és a Sky zenéje elnémul + Ezen az eszközön nem támogatott Fejlesztői beállítások Csalómotor-kiszolgáló (cheatengine) Egyéni szerver diff --git a/app/src/main/res/values-id/strings.xml b/app/src/main/res/values-id/strings.xml index 82042a7..07a0abb 100644 --- a/app/src/main/res/values-id/strings.xml +++ b/app/src/main/res/values-id/strings.xml @@ -107,6 +107,9 @@ Umum Sembunyikan menu Canvas + Headphone saat mendengarkan + Saat aplikasi lain memutar musik, avatarmu memakai headphone dan musik Sky dibisukan + Tidak didukung di perangkat ini Opsi Pengembang Server Cheat Engine Server kustom diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index da30826..64a00b4 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -105,6 +105,9 @@ Generali Nascondi il menu di Canvas + Cuffie durante l\'ascolto + Quando un\'altra app riproduce musica, il tuo avatar indossa le cuffie e la musica di Sky viene silenziata + Non supportato su questo dispositivo Opzioni Sviluppatore Server Cheat Engine Server personalizzato diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 5ab0e81..25cace9 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -105,6 +105,9 @@ 一般 Canvas メニューを非表示 + 音楽再生中のヘッドホン + 他のアプリで音楽を再生すると、アバターがヘッドホンを着け、Sky の音楽がミュートされます + この端末ではサポートされていません 開発者オプション Cheat Engine サーバー カスタムサーバー diff --git a/app/src/main/res/values-pt/strings.xml b/app/src/main/res/values-pt/strings.xml index b8fc3e3..5e0e818 100644 --- a/app/src/main/res/values-pt/strings.xml +++ b/app/src/main/res/values-pt/strings.xml @@ -105,6 +105,9 @@ Geral Ocultar menu do Canvas + Fones ao ouvir música + Quando outro app toca música, seu avatar usa fones de ouvido e a música do Sky é silenciada + Não suportado neste dispositivo OPÇÕES DO DESENVOLVEDOR Servidor do Cheat Engine Servidor personalizado diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index cf6d608..fa84cd7 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -93,6 +93,9 @@ Основные Скрыть меню Canvas + Наушники при прослушивании + Когда другое приложение играет музыку, ваш персонаж надевает наушники, а музыка Sky приглушается + Не поддерживается на этом устройстве Для разработчиков Сервер Cheat Engine Свой сервер diff --git a/app/src/main/res/values-tl/strings.xml b/app/src/main/res/values-tl/strings.xml index 0194f88..929afa7 100644 --- a/app/src/main/res/values-tl/strings.xml +++ b/app/src/main/res/values-tl/strings.xml @@ -105,6 +105,9 @@ General Hide Canvas menu + Headphones while listening + When another app plays music, your avatar wears headphones and Sky music is muted + Not supported on this device Developer Options Cheat Engine Server Custom server diff --git a/app/src/main/res/values-vi/strings.xml b/app/src/main/res/values-vi/strings.xml index cdd147f..6c9cb07 100644 --- a/app/src/main/res/values-vi/strings.xml +++ b/app/src/main/res/values-vi/strings.xml @@ -105,6 +105,9 @@ Chung Ẩn menu Canvas + Tai nghe khi nghe nhạc + Khi ứng dụng khác phát nhạc, nhân vật của bạn đeo tai nghe và nhạc Sky bị tắt + Không được hỗ trợ trên thiết bị này Tùy chọn nhà phát triển Máy chủ Cheat Engine Máy chủ tùy chỉnh diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8a3e1c5..2abe34f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -108,6 +108,9 @@ General Hide Canvas menu + Headphones while listening + When another app plays music, your avatar wears headphones and Sky music is muted + Not supported on this device Developer Options Cheat Engine Server Custom server diff --git a/app/src/test/cpp/CMakeLists.txt b/app/src/test/cpp/CMakeLists.txt index a59f742..2b2da41 100644 --- a/app/src/test/cpp/CMakeLists.txt +++ b/app/src/test/cpp/CMakeLists.txt @@ -14,3 +14,19 @@ target_include_directories(chat_flag_fix_test PRIVATE target_compile_options(chat_flag_fix_test PRIVATE -Wall -Wextra -Werror) add_test(NAME chat_flag_fix_test COMMAND chat_flag_fix_test) + +add_executable(other_audio_test OtherAudioTest.cpp) +target_include_directories(other_audio_test PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../main/cpp/Core) +target_compile_options(other_audio_test PRIVATE -Wall -Wextra -Werror) + +add_test(NAME other_audio_test COMMAND other_audio_test) + +add_executable(other_audio_swap_test OtherAudioSwapTest.cpp) +target_include_directories(other_audio_swap_test PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../../main/cpp/Core) +target_compile_options(other_audio_swap_test PRIVATE -Wall -Wextra -Werror) + +add_test(NAME other_audio_swap_test COMMAND other_audio_swap_test) diff --git a/app/src/test/cpp/OtherAudioFixtures.h b/app/src/test/cpp/OtherAudioFixtures.h new file mode 100644 index 0000000..7919b20 --- /dev/null +++ b/app/src/test/cpp/OtherAudioFixtures.h @@ -0,0 +1,49 @@ +#pragma once + +#include +#include + +namespace Fixtures { + +struct Window { + std::uintptr_t va; + std::vector bytes; +}; + +struct Image { + const char *tag; + std::vector windows; +}; + +inline const std::vector &All() { + static const std::vector images = { + {"0.35.1", { + {0x1a2d5e8u, {0x68,0xc2,0x29,0x39,0x88,0x02,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x68,0xc2,0x69,0x39,0x1f,0x01,0x00,0x71,0xe8,0x17,0x9f,0x1a,0x08,0x00,0x08,0x0a,0x68,0xc6,0x29,0x39,0x84,0xc4,0x3f,0x94,0x08,0x00,0x40,0xf9,0x62,0xc6,0x69,0x39,0x21,0x00,0x80,0x52,0x08,0x6d,0x40,0xf9,0x00,0x01,0x3f,0xd6,0xbf,0x03,0x01,0xd1}}, + {0x2a1e81cu, {0xfd,0x7b,0xbe,0xa9,0xfc,0x0b,0x00,0xf9,0xfd,0x03,0x00,0x91,0xe9,0x03,0x07,0xd1,0x3f,0xe9,0x7b,0x92,0x88,0x60,0x00,0xf0,0xe9,0xe3,0x03,0x91,0x00,0xc9,0x40,0xf9,0xa8,0x63,0x00,0x91,0xa9,0x0f,0x00,0xf9}}, + {0xdc3ad8u, {0x00,0x3b,0x00,0x91,0x41,0x00,0xa0,0x72,0x70,0x22,0x7a,0x94,0x60,0x10,0x00,0x35,0x08,0xe3,0x40,0xf8,0x28,0x10,0x00,0xb4,0x01,0x5b,0x00,0x91,0xe0,0x03,0x08,0xaa,0x6e,0x22,0x7a,0x94,0x60,0x10,0x00,0x35,0x08,0x63,0x41,0xf8,0x28,0x10,0x00,0xb4,0xa1,0x53,0x00,0xd1,0xe0,0x03,0x08,0xaa,0xbf,0xc3,0x1e,0xb8,0x6b,0x22,0x7a,0x94,0xa4,0xc3,0x5e,0xb8,0x03,0xbe,0xff,0xf0,0x63,0x78,0x36,0x91,0x80,0x00,0x80,0x52,0xe1,0x03,0x1f,0xaa,0xe2,0x03,0x1f,0x2a,0x85,0x7c,0x10,0x53,0x86,0x3c,0x08,0x53,0x87,0x1c,0x00,0x12,0xae,0x11,0x00,0x94,0xa5,0xc3,0x5e,0xb8,0xa8,0x06,0x00,0x51,0xbf,0x00,0x08,0x6b,0x09,0x35,0x00,0x54,0x00,0x63,0x41,0xf8,0x61,0x0e,0x40,0xb9,0x5e,0x22,0x7a,0x94,0xa0,0x36,0x00,0x35,0x68,0x6a,0x48,0x91,0x16,0x01,0x15,0x91,0xc0,0x96,0x41,0xf9,0x08,0x00,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x08,0x20,0x80,0x52,0x09,0x40,0x80,0x52,0x1f,0x00,0x00,0x72}}, + {0xdc786cu, {0xf4,0x03,0x00,0xaa,0xe0,0x03,0x01,0xaa,0xf3,0x03,0x01,0xaa,0x42,0x3f,0x00,0x94,0x08,0x0d,0x95,0x52,0x28,0x04,0xa0,0x72,0x93,0x6a,0x28,0xf8,0xf4,0x4f,0x41,0xa9}}, + {0xdd7580u, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0x28,0x16,0x01,0xf0,0x00,0xbd,0x07,0xf9,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + {"0.35.0", { + {0x1a2d5dcu, {0x68,0xc2,0x29,0x39,0x88,0x02,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x68,0xc2,0x69,0x39,0x1f,0x01,0x00,0x71,0xe8,0x17,0x9f,0x1a,0x08,0x00,0x08,0x0a,0x68,0xc6,0x29,0x39,0x87,0xc4,0x3f,0x94,0x08,0x00,0x40,0xf9,0x62,0xc6,0x69,0x39,0x21,0x00,0x80,0x52,0x08,0x6d,0x40,0xf9,0x00,0x01,0x3f,0xd6,0xbf,0x03,0x01,0xd1}}, + {0x2a1e81cu, {0xfd,0x7b,0xbe,0xa9,0xfc,0x0b,0x00,0xf9,0xfd,0x03,0x00,0x91,0xe9,0x03,0x07,0xd1,0x3f,0xe9,0x7b,0x92,0x88,0x60,0x00,0xf0,0xe9,0xe3,0x03,0x91,0x00,0xc1,0x40,0xf9,0xa8,0x63,0x00,0x91,0xa9,0x0f,0x00,0xf9}}, + {0xdc3ad8u, {0x00,0x3b,0x00,0x91,0x41,0x00,0xa0,0x72,0x6c,0x22,0x7a,0x94,0x60,0x10,0x00,0x35,0x08,0xe3,0x40,0xf8,0x28,0x10,0x00,0xb4,0x01,0x5b,0x00,0x91,0xe0,0x03,0x08,0xaa,0x6a,0x22,0x7a,0x94,0x60,0x10,0x00,0x35,0x08,0x63,0x41,0xf8,0x28,0x10,0x00,0xb4,0xa1,0x53,0x00,0xd1,0xe0,0x03,0x08,0xaa,0xbf,0xc3,0x1e,0xb8,0x67,0x22,0x7a,0x94,0xa4,0xc3,0x5e,0xb8,0x03,0xbe,0xff,0xf0,0x63,0x78,0x36,0x91,0x80,0x00,0x80,0x52,0xe1,0x03,0x1f,0xaa,0xe2,0x03,0x1f,0x2a,0x85,0x7c,0x10,0x53,0x86,0x3c,0x08,0x53,0x87,0x1c,0x00,0x12,0xae,0x11,0x00,0x94,0xa5,0xc3,0x5e,0xb8,0xa8,0x06,0x00,0x51,0xbf,0x00,0x08,0x6b,0x09,0x35,0x00,0x54,0x00,0x63,0x41,0xf8,0x61,0x0e,0x40,0xb9,0x5a,0x22,0x7a,0x94,0xa0,0x36,0x00,0x35,0x68,0x6a,0x48,0x91,0x16,0x01,0x15,0x91,0xc0,0x96,0x41,0xf9,0x08,0x00,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x08,0x20,0x80,0x52,0x09,0x40,0x80,0x52,0x1f,0x00,0x00,0x72}}, + {0xdc786cu, {0xf4,0x03,0x00,0xaa,0xe0,0x03,0x01,0xaa,0xf3,0x03,0x01,0xaa,0x42,0x3f,0x00,0x94,0x08,0x0d,0x95,0x52,0x28,0x04,0xa0,0x72,0x93,0x6a,0x28,0xf8,0xf4,0x4f,0x41,0xa9}}, + {0xdd7580u, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0x28,0x16,0x01,0xf0,0x00,0xb5,0x07,0xf9,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + {"0.34.5", { + {0x197be90u, {0x68,0xc2,0x29,0x39,0x88,0x02,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x68,0xc2,0x69,0x39,0x1f,0x01,0x00,0x71,0xe8,0x17,0x9f,0x1a,0x08,0x00,0x08,0x0a,0x68,0xc6,0x29,0x39,0x77,0xd1,0x3e,0x94,0x08,0x00,0x40,0xf9,0x62,0xc6,0x69,0x39,0x21,0x00,0x80,0x52,0x08,0x6d,0x40,0xf9,0x00,0x01,0x3f,0xd6,0xbf,0x03,0x01,0xd1}}, + {0x2930490u, {0xfd,0x7b,0xbe,0xa9,0xfc,0x0b,0x00,0xf9,0xfd,0x03,0x00,0x91,0xe9,0x03,0x07,0xd1,0x3f,0xe9,0x7b,0x92,0x68,0x5f,0x00,0xb0,0xe9,0xe3,0x03,0x91,0x00,0xf9,0x43,0xf9,0xa8,0x63,0x00,0x91,0xa9,0x0f,0x00,0xf9}}, + {0xd6eb54u, {0x00,0x3b,0x00,0x91,0x41,0x00,0xa0,0x72,0xc5,0xbe,0x77,0x94,0x60,0x10,0x00,0x35,0x08,0xe3,0x40,0xf8,0x28,0x10,0x00,0xb4,0x01,0x5b,0x00,0x91,0xe0,0x03,0x08,0xaa,0xc3,0xbe,0x77,0x94,0x60,0x10,0x00,0x35,0x08,0x63,0x41,0xf8,0x28,0x10,0x00,0xb4,0xa1,0x53,0x00,0xd1,0xe0,0x03,0x08,0xaa,0xbf,0xc3,0x1e,0xb8,0xc0,0xbe,0x77,0x94,0xa4,0xc3,0x5e,0xb8,0x83,0xbf,0xff,0xb0,0x63,0x54,0x12,0x91,0x80,0x00,0x80,0x52,0xe1,0x03,0x1f,0xaa,0xe2,0x03,0x1f,0x2a,0x85,0x7c,0x10,0x53,0x86,0x3c,0x08,0x53,0x87,0x1c,0x00,0x12,0x85,0x04,0x00,0x94,0xa5,0xc3,0x5e,0xb8,0xa8,0x06,0x00,0x51,0xbf,0x00,0x08,0x6b,0x09,0x35,0x00,0x54,0x00,0x63,0x41,0xf8,0x61,0x0e,0x40,0xb9,0xb3,0xbe,0x77,0x94,0xa0,0x36,0x00,0x35,0x68,0x6a,0x48,0x91,0x16,0x41,0x14,0x91,0xc0,0x96,0x41,0xf9,0x08,0x00,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x08,0x20,0x80,0x52,0x09,0x40,0x80,0x52,0x1f,0x00,0x00,0x72}}, + {0xd6aba0u, {0xf4,0x03,0x00,0xaa,0xe0,0x03,0x01,0xaa,0xf3,0x03,0x01,0xaa,0x94,0x4d,0x00,0x94,0x08,0x07,0x95,0x52,0x28,0x04,0xa0,0x72,0x93,0x6a,0x28,0xf8,0xf4,0x4f,0x41,0xa9}}, + {0xd7e1fcu, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0xe8,0x10,0x01,0x90,0x00,0x55,0x00,0xf9,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + {"0.33.8", { + {0x183a7a4u, {0x68,0x82,0x20,0x39,0x88,0x02,0x40,0xf9,0x08,0x55,0x40,0xf9,0x00,0x01,0x3f,0xd6,0x68,0x82,0x60,0x39,0x1f,0x01,0x00,0x71,0xe8,0x17,0x9f,0x1a,0x08,0x00,0x08,0x0a,0x68,0x86,0x20,0x39,0x7d,0xe8,0x3b,0x94,0x08,0x00,0x40,0xf9,0x62,0x86,0x60,0x39,0x21,0x00,0x80,0x52,0x08,0x6d,0x40,0xf9,0x00,0x01,0x3f,0xd6,0xbf,0x03,0x01,0xd1}}, + {0x27349bcu, {0xfd,0x7b,0xbe,0xa9,0xfc,0x0b,0x00,0xf9,0xfd,0x03,0x00,0x91,0xe9,0x03,0x07,0xd1,0x3f,0xe9,0x7b,0x92,0x88,0x5d,0x00,0x90,0xe9,0xe3,0x03,0x91,0x00,0x61,0x42,0xf9,0xa8,0x63,0x00,0x91,0xa9,0x0f,0x00,0xf9}}, + }}, + }; + return images; +} + +} diff --git a/app/src/test/cpp/OtherAudioSwapFixtures.h b/app/src/test/cpp/OtherAudioSwapFixtures.h new file mode 100644 index 0000000..bb4d0c5 --- /dev/null +++ b/app/src/test/cpp/OtherAudioSwapFixtures.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +namespace SwapFixtures { + +struct Window { + std::uintptr_t va; + std::vector bytes; +}; + +struct Image { + const char *tag; + std::uintptr_t codeBegin, codeEnd, relroBegin, relroEnd; + std::uintptr_t vtable, stub; + std::vector windows; +}; + +inline const std::vector &All() { + static const std::vector images = { + {"0.35.1", 0xce0000u, 0x2c4db70u, 0x2c51b70u, 0x2df7000u, 0x2c53888u, 0xce4410u, { + {0x2c53878u, {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x58,0xd1,0x00,0x00,0x00,0x00,0x00,0x04,0x0e,0xd1,0x00,0x00,0x00,0x00,0x00,0x70,0xa6,0xd0,0x00,0x00,0x00,0x00,0x00,0xc0,0xac,0xd0,0x00,0x00,0x00,0x00,0x00,0x6c,0x43,0xd0,0x00,0x00,0x00,0x00,0x00,0xac,0xaf,0xd0,0x00,0x00,0x00,0x00,0x00,0xf0,0xb1,0xd0,0x00,0x00,0x00,0x00,0x00,0x88,0xc0,0xd0,0x00,0x00,0x00,0x00,0x00,0xc4,0x1f,0xd1,0x00,0x00,0x00,0x00,0x00,0xd4,0x48,0xd0,0x00,0x00,0x00,0x00,0x00,0x24,0x42,0xd0,0x00,0x00,0x00,0x00,0x00,0xe4,0xaa,0xd0,0x00,0x00,0x00,0x00,0x00,0x44,0xc9,0xd0,0x00,0x00,0x00,0x00,0x00,0x98,0xa4,0xd0,0x00,0x00,0x00,0x00,0x00,0x80,0x41,0xd0,0x00,0x00,0x00,0x00,0x00,0xd8,0xb1,0xd0,0x00,0x00,0x00,0x00,0x00,0xe4,0x42,0xd0,0x00,0x00,0x00,0x00,0x00,0xc8,0x4b,0xd0,0x00,0x00,0x00,0x00,0x00,0x70,0xb3,0xd0,0x00,0x00,0x00,0x00,0x00,0x54,0xa9,0xd0,0x00,0x00,0x00,0x00,0x00,0xd4,0x44,0xce,0x00,0x00,0x00,0x00,0x00,0x10,0x44,0xce,0x00,0x00,0x00,0x00,0x00}}, + {0xce4410u, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0xe0,0x03,0x1f,0x2a,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + {"0.35.0", 0xce0000u, 0x2c4db60u, 0x2c51b60u, 0x2df7000u, 0x2c53878u, 0xce4410u, { + {0x2c53868u, {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xb0,0x58,0xd1,0x00,0x00,0x00,0x00,0x00,0x04,0x0e,0xd1,0x00,0x00,0x00,0x00,0x00,0x70,0xa6,0xd0,0x00,0x00,0x00,0x00,0x00,0xc0,0xac,0xd0,0x00,0x00,0x00,0x00,0x00,0x6c,0x43,0xd0,0x00,0x00,0x00,0x00,0x00,0xac,0xaf,0xd0,0x00,0x00,0x00,0x00,0x00,0xf0,0xb1,0xd0,0x00,0x00,0x00,0x00,0x00,0x88,0xc0,0xd0,0x00,0x00,0x00,0x00,0x00,0xc4,0x1f,0xd1,0x00,0x00,0x00,0x00,0x00,0xd4,0x48,0xd0,0x00,0x00,0x00,0x00,0x00,0x24,0x42,0xd0,0x00,0x00,0x00,0x00,0x00,0xe4,0xaa,0xd0,0x00,0x00,0x00,0x00,0x00,0x44,0xc9,0xd0,0x00,0x00,0x00,0x00,0x00,0x98,0xa4,0xd0,0x00,0x00,0x00,0x00,0x00,0x80,0x41,0xd0,0x00,0x00,0x00,0x00,0x00,0xd8,0xb1,0xd0,0x00,0x00,0x00,0x00,0x00,0xe4,0x42,0xd0,0x00,0x00,0x00,0x00,0x00,0xc8,0x4b,0xd0,0x00,0x00,0x00,0x00,0x00,0x70,0xb3,0xd0,0x00,0x00,0x00,0x00,0x00,0x54,0xa9,0xd0,0x00,0x00,0x00,0x00,0x00,0xd4,0x44,0xce,0x00,0x00,0x00,0x00,0x00,0x10,0x44,0xce,0x00,0x00,0x00,0x00,0x00}}, + {0xce4410u, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0xe0,0x03,0x1f,0x2a,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + {"0.34.5", 0xc89000u, 0x2b5fb70u, 0x2b63b70u, 0x2cfd000u, 0x2b65870u, 0xc8ccdcu, { + {0x2b65860u, {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0xaf,0xcb,0x00,0x00,0x00,0x00,0x00,0x04,0xa5,0xcb,0x00,0x00,0x00,0x00,0x00,0xc0,0x2f,0xcb,0x00,0x00,0x00,0x00,0x00,0x34,0x34,0xcb,0x00,0x00,0x00,0x00,0x00,0xf8,0x83,0xcb,0x00,0x00,0x00,0x00,0x00,0xa8,0x82,0xcb,0x00,0x00,0x00,0x00,0x00,0x2c,0x77,0xcb,0x00,0x00,0x00,0x00,0x00,0x18,0xec,0xca,0x00,0x00,0x00,0x00,0x00,0xd8,0xd7,0xcb,0x00,0x00,0x00,0x00,0x00,0xc8,0xf7,0xca,0x00,0x00,0x00,0x00,0x00,0xd0,0xe8,0xca,0x00,0x00,0x00,0x00,0x00,0x3c,0xec,0xca,0x00,0x00,0x00,0x00,0x00,0xd8,0x70,0xcb,0x00,0x00,0x00,0x00,0x00,0xd0,0x88,0xcb,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0xcb,0x00,0x00,0x00,0x00,0x00,0xf4,0x6f,0xcb,0x00,0x00,0x00,0x00,0x00,0x64,0xf5,0xca,0x00,0x00,0x00,0x00,0x00,0xa0,0xf4,0xca,0x00,0x00,0x00,0x00,0x00,0x1c,0x73,0xcb,0x00,0x00,0x00,0x00,0x00,0xa8,0x80,0xcb,0x00,0x00,0x00,0x00,0x00,0xf0,0xe4,0xc8,0x00,0x00,0x00,0x00,0x00,0xdc,0xcc,0xc8,0x00,0x00,0x00,0x00,0x00}}, + {0xc8ccdcu, {0xfd,0x7b,0xbf,0xa9,0xfd,0x03,0x00,0x91,0xe0,0x03,0x1f,0x2a,0xfd,0x7b,0xc1,0xa8,0xc0,0x03,0x5f,0xd6}}, + }}, + }; + return images; +} + +} diff --git a/app/src/test/cpp/OtherAudioSwapTest.cpp b/app/src/test/cpp/OtherAudioSwapTest.cpp new file mode 100644 index 0000000..db3d320 --- /dev/null +++ b/app/src/test/cpp/OtherAudioSwapTest.cpp @@ -0,0 +1,392 @@ +#include "OtherAudio/OtherAudio.h" +#include "OtherAudio/OtherAudioSwap.h" +#include "OtherAudioFixtures.h" +#include "OtherAudioSwapFixtures.h" + +#include +#include +#include +#include +#include + +namespace { + +int g_failures = 0; + +void Check(bool condition, const char *what, int line) { + if (!condition) { + std::printf(" FAIL line %d: %s\n", line, what); + ++g_failures; + } +} + +#define CHECK(x) Check((x), #x, __LINE__) + +using OtherAudio::Decision; + +constexpr std::uintptr_t kAudio = 0x7a00000000u; +constexpr std::uintptr_t kSystemIo = 0x7b00000000u; +constexpr std::uintptr_t kReplacement = 0x7c00001000u; + +struct Region { + std::uintptr_t base; + std::vector bytes; +}; + +struct Memory { + std::vector regions; + mutable int reads = 0; + + bool operator()(std::uintptr_t at, void *out, std::size_t size) const { + ++reads; + for (const Region &r : regions) { + if (at >= r.base && size <= r.bytes.size() && at - r.base <= r.bytes.size() - size) { + std::memcpy(out, r.bytes.data() + (at - r.base), size); + return true; + } + } + return false; + } + + void Put(std::uintptr_t base, const void *data, std::size_t size) { + Region r{base, {}}; + r.bytes.assign(static_cast(data), static_cast(data) + size); + regions.push_back(std::move(r)); + } + + void PutPointer(std::uintptr_t at, std::uintptr_t value) { Put(at, &value, sizeof(value)); } + + void Set(std::uintptr_t at, const void *data, std::size_t size) { + for (Region &r : regions) { + if (at >= r.base && at + size <= r.base + r.bytes.size()) { + std::memcpy(r.bytes.data() + (at - r.base), data, size); + return; + } + } + std::printf(" Set outside fixture at %#zx\n", static_cast(at)); + std::abort(); + } + + void SetPointer(std::uintptr_t at, std::uintptr_t value) { Set(at, &value, sizeof(value)); } + + void Drop(std::uintptr_t at) { + for (std::size_t i = 0; i < regions.size(); ++i) { + if (at >= regions[i].base && at < regions[i].base + regions[i].bytes.size()) { + regions.erase(regions.begin() + static_cast(i)); + return; + } + } + std::printf(" Drop outside fixture at %#zx\n", static_cast(at)); + std::abort(); + } +}; + +struct World { + Memory memory; + OtherAudio::Located located; + OtherAudio::Image image; + std::uintptr_t vtable = 0; + std::uintptr_t stub = 0; + std::uintptr_t fmodReturn = 0; + bool decoded = false; +}; + +const SwapFixtures::Image &SwapImage(const char *tag) { + for (const auto &img : SwapFixtures::All()) + if (std::strcmp(img.tag, tag) == 0) + return img; + std::printf(" no swap fixture %s\n", tag); + std::abort(); +} + +World Build(const char *tag) { + World w; + Memory code; + for (const auto &img : Fixtures::All()) + if (std::strcmp(img.tag, tag) == 0) + for (const auto &win : img.windows) + code.Put(win.va, win.bytes.data(), win.bytes.size()); + + int chains = 0, fmods = 0, setters = 0; + for (const Region &r : code.regions) { + OtherAudio::Chain c; + OtherAudio::FmodSelect f; + OtherAudio::Setter s; + const int nc = OtherAudio::FindChain(code, r.base, r.bytes.size(), c); + const int nf = OtherAudio::FindFmod(code, r.base, r.bytes.size(), f); + const int ns = OtherAudio::FindSetter(code, r.base, r.bytes.size(), s); + if (nc && !chains) + w.located.chain = c; + if (nf && !fmods) { + w.located.systemIoOffset = f.systemIoOffset; + w.fmodReturn = f.site; + } + if (ns && !setters) + w.located.systemIoGlobal = s.systemIoGlobal; + chains += nc; + fmods += nf; + setters += ns; + } + w.decoded = chains == 1 && fmods == 1 && setters == 1; + + const SwapFixtures::Image &img = SwapImage(tag); + for (const auto &win : img.windows) + w.memory.Put(win.va, win.bytes.data(), win.bytes.size()); + w.image.codeBegin = img.codeBegin; + w.image.codeEnd = img.codeEnd; + w.image.relroBegin = img.relroBegin; + w.image.relroEnd = img.relroEnd; + w.vtable = img.vtable; + w.stub = img.stub; + + w.memory.PutPointer(w.located.chain.audioGlobal, kAudio); + w.memory.PutPointer(kAudio + w.located.systemIoOffset, kSystemIo); + w.memory.PutPointer(w.located.systemIoGlobal, kSystemIo); + w.memory.PutPointer(kSystemIo, w.vtable); + return w; +} + +OtherAudio::Plan Decide(const World &w, bool enabled = true) { + return OtherAudio::Decide(w.memory, &w.located, enabled, w.image, kReplacement); +} + +void SwapsOnEveryAuditedBuild() { + for (const char *tag : {"0.35.1", "0.35.0", "0.34.5"}) { + const World w = Build(tag); + CHECK(w.decoded); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::Swap); + CHECK(plan.systemIo == kSystemIo); + CHECK(plan.otherSystemIo == kSystemIo); + CHECK(plan.vtable == w.vtable); + CHECK(plan.slotAddress == w.vtable + 0xa8u); + CHECK(plan.at == plan.slotAddress); + CHECK(plan.current == w.stub); + } +} + +void UsesTheAudioRouteAloneWhenNoGlobalWasDecoded() { + World w = Build("0.35.1"); + w.located.systemIoGlobal = 0; + CHECK(Decide(w).decision == Decision::Swap); +} + +void ReportsAlreadyAppliedWhenTheSlotHoldsTheReplacement() { + World w = Build("0.35.1"); + w.memory.SetPointer(w.vtable + 0xa8u, kReplacement); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::AlreadyApplied); + CHECK(plan.at == w.vtable + 0xa8u); +} + +void ReadsNothingWhenTheSettingIsOff() { + const World w = Build("0.35.1"); + w.memory.reads = 0; + CHECK(Decide(w, false).decision == Decision::Disabled); + CHECK(w.memory.reads == 0); +} + +void ReadsNothingWhenNotLocated() { + const World w = Build("0.35.1"); + w.memory.reads = 0; + CHECK(OtherAudio::Decide(w.memory, nullptr, true, w.image, kReplacement).decision == Decision::NotLocated); + CHECK(w.memory.reads == 0); +} + +void WaitsWhileTheGameObjectsAreNull() { + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.located.chain.audioGlobal, 0); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::NotReady); + CHECK(plan.at == w.located.chain.audioGlobal); + } + { + World w = Build("0.35.1"); + w.memory.SetPointer(kAudio + w.located.systemIoOffset, 0); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::NotReady); + CHECK(plan.at == kAudio + w.located.systemIoOffset); + } + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.located.systemIoGlobal, 0); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::NotReady); + CHECK(plan.at == w.located.systemIoGlobal); + } +} + +void RefusesWhenTheSystemIoRoutesDisagree() { + World w = Build("0.35.1"); + w.memory.SetPointer(w.located.systemIoGlobal, kSystemIo + 0x100u); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::SystemIoMismatch); + CHECK(plan.systemIo == kSystemIo); + CHECK(plan.otherSystemIo == kSystemIo + 0x100u); +} + +void RefusesUnreadableMemory() { + { + World w = Build("0.35.1"); + w.memory.Drop(w.located.chain.audioGlobal); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::Unreadable); + CHECK(plan.at == w.located.chain.audioGlobal); + } + { + World w = Build("0.35.1"); + w.memory.Drop(kSystemIo); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::Unreadable); + CHECK(plan.at == kSystemIo); + } + { + World w = Build("0.35.1"); + w.memory.Drop(w.stub); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::Unreadable); + CHECK(plan.at == w.stub); + } +} + +void RefusesASystemIoWithoutAVtableHeader() { + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.vtable - 8u, 0x2c00000u); + CHECK(Decide(w).decision == Decision::NotAVtable); + } + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.vtable - 16u, 8u); + CHECK(Decide(w).decision == Decision::NotAVtable); + } + { + World w = Build("0.35.1"); + w.memory.SetPointer(kSystemIo, w.vtable + 4u); + CHECK(Decide(w).decision == Decision::NotAVtable); + } +} + +void RefusesASlotOutsideRelro() { + { + World w = Build("0.35.1"); + w.image.relroEnd = w.vtable + 0xa8u + 4u; + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::SlotOutsideRelro); + CHECK(plan.at == w.vtable + 0xa8u); + } + { + World w = Build("0.35.1"); + w.image.relroBegin = w.vtable + 0xb0u; + CHECK(Decide(w).decision == Decision::SlotOutsideRelro); + } +} + +void RefusesASlotPointingOutsideTheGamesCode() { + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.vtable + 0xa8u, w.image.codeEnd - 8u); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::TargetOutsideCode); + CHECK(plan.at == w.image.codeEnd - 8u); + } + { + World w = Build("0.35.1"); + w.memory.SetPointer(w.vtable + 0xa8u, kReplacement + 0x40u); + CHECK(Decide(w).decision == Decision::TargetOutsideCode); + } +} + +void RefusesAnythingButTheExactStub() { + for (int word = 0; word < 5; ++word) { + World w = Build("0.35.1"); + std::uint32_t changed = OtherAudio::kReturnFalseStub[word] ^ 0x20u; + w.memory.Set(w.stub + 4u * static_cast(word), &changed, sizeof(changed)); + const OtherAudio::Plan plan = Decide(w); + CHECK(plan.decision == Decision::NotTheStub); + CHECK(plan.at == w.stub); + } + World w = Build("0.35.1"); + const std::uint32_t returnTrue = 0x52800020u; + w.memory.Set(w.stub + 8u, &returnTrue, sizeof(returnTrue)); + CHECK(Decide(w).decision == Decision::NotTheStub); +} + +void AnswersOnlyTheGateCaller() { + const World w = Build("0.35.1"); + const std::uintptr_t gate = w.located.chain.gateReturn; + CHECK(w.fmodReturn != 0); + CHECK(OtherAudio::Answer(gate, gate, true, true)); + CHECK(!OtherAudio::Answer(gate, gate, true, false)); + CHECK(!OtherAudio::Answer(gate, gate, false, true)); + CHECK(!OtherAudio::Answer(w.fmodReturn, gate, true, true)); + CHECK(!OtherAudio::Answer(w.located.chain.fadeReturn, gate, true, true)); + CHECK(!OtherAudio::Answer(gate + 4u, gate, true, true)); + CHECK(!OtherAudio::Answer(0, 0, true, true)); +} + +void LayoutNeedsOneCodeSegmentAndOneRelro() { + const std::uintptr_t base = 0x7000000000u; + std::vector segments = { + {OtherAudio::kPtLoad, 4, 0x0u, 0xcdb538u}, + {OtherAudio::kPtLoad, 5, 0xce0000u, 0x1f6db70u}, + {OtherAudio::kPtLoad, 6, 0x2c51b70u, 0x1a5490u}, + {OtherAudio::kPtLoad, 6, 0x2dfa520u, 0x9ceb30u}, + {OtherAudio::kPtGnuRelro, 4, 0x2c51b70u, 0x1a5490u}, + }; + OtherAudio::Image image; + CHECK(OtherAudio::Layout(base, segments.data(), static_cast(segments.size()), image)); + const SwapFixtures::Image &real = SwapImage("0.35.1"); + CHECK(image.codeBegin == base + real.codeBegin); + CHECK(image.codeEnd == base + real.codeEnd); + CHECK(image.relroBegin == base + real.relroBegin); + CHECK(image.relroEnd == base + real.relroEnd); + + std::vector twoCode = segments; + twoCode.push_back({OtherAudio::kPtLoad, 5, 0x4000000u, 0x1000u}); + OtherAudio::Image untouched; + CHECK(!OtherAudio::Layout(base, twoCode.data(), static_cast(twoCode.size()), untouched)); + CHECK(untouched.codeBegin == 0); + + std::vector noRelro(segments.begin(), segments.end() - 1); + CHECK(!OtherAudio::Layout(base, noRelro.data(), static_cast(noRelro.size()), untouched)); + + std::vector emptyRelro = segments; + emptyRelro.back().memsz = 0; + CHECK(!OtherAudio::Layout(base, emptyRelro.data(), static_cast(emptyRelro.size()), untouched)); +} + +} + +int main() { + std::setvbuf(stdout, nullptr, _IONBF, 0); + struct Case { + const char *name; + void (*run)(); + }; + const Case cases[] = { + {"swaps_on_every_audited_build", SwapsOnEveryAuditedBuild}, + {"uses_the_audio_route_alone_when_no_global_was_decoded", UsesTheAudioRouteAloneWhenNoGlobalWasDecoded}, + {"reports_already_applied_when_the_slot_holds_the_replacement", + ReportsAlreadyAppliedWhenTheSlotHoldsTheReplacement}, + {"reads_nothing_when_the_setting_is_off", ReadsNothingWhenTheSettingIsOff}, + {"reads_nothing_when_not_located", ReadsNothingWhenNotLocated}, + {"waits_while_the_game_objects_are_null", WaitsWhileTheGameObjectsAreNull}, + {"refuses_when_the_systemio_routes_disagree", RefusesWhenTheSystemIoRoutesDisagree}, + {"refuses_unreadable_memory", RefusesUnreadableMemory}, + {"refuses_a_systemio_without_a_vtable_header", RefusesASystemIoWithoutAVtableHeader}, + {"refuses_a_slot_outside_relro", RefusesASlotOutsideRelro}, + {"refuses_a_slot_pointing_outside_the_games_code", RefusesASlotPointingOutsideTheGamesCode}, + {"refuses_anything_but_the_exact_stub", RefusesAnythingButTheExactStub}, + {"answers_only_the_gate_caller", AnswersOnlyTheGateCaller}, + {"layout_needs_one_code_segment_and_one_relro", LayoutNeedsOneCodeSegmentAndOneRelro}, + }; + for (const Case &c : cases) { + const int before = g_failures; + c.run(); + std::printf("%s %s\n", g_failures == before ? "ok " : "FAIL", c.name); + } + std::printf("%d failure(s)\n", g_failures); + return g_failures == 0 ? 0 : 1; +} diff --git a/app/src/test/cpp/OtherAudioTest.cpp b/app/src/test/cpp/OtherAudioTest.cpp new file mode 100644 index 0000000..1e430f6 --- /dev/null +++ b/app/src/test/cpp/OtherAudioTest.cpp @@ -0,0 +1,221 @@ +#include "OtherAudio/OtherAudio.h" +#include "OtherAudioFixtures.h" + +#include +#include +#include +#include +#include +#include + +namespace { + +int g_failures = 0; +int g_checks = 0; + +void Check(bool ok, const std::string &what) { + ++g_checks; + if (!ok) { + ++g_failures; + std::printf("FAIL %s\n", what.c_str()); + } +} + +struct Memory { + std::vector>> windows; + std::map patches; + + bool read(std::uintptr_t at, void *out, std::size_t n) const { + for (const auto &w : windows) { + if (at >= w.first && n <= w.second.size() && at - w.first <= w.second.size() - n) { + std::memcpy(out, w.second.data() + (at - w.first), n); + for (const auto &p : patches) + if (p.first >= at && p.first + 4 <= at + n) + std::memcpy(static_cast(out) + (p.first - at), &p.second, 4); + return true; + } + } + return false; + } +}; + +struct Scan { + int chains = 0, fmods = 0, setters = 0; + OtherAudio::Chain chain; + OtherAudio::FmodSelect fmod; + OtherAudio::Setter setter; +}; + +Scan Run(const Memory &mem) { + Scan s; + auto read = [&mem](std::uintptr_t at, void *out, std::size_t n) { return mem.read(at, out, n); }; + for (const auto &w : mem.windows) { + OtherAudio::Chain c; + OtherAudio::FmodSelect f; + OtherAudio::Setter t; + const int nc = OtherAudio::FindChain(read, w.first, w.second.size(), c); + const int nf = OtherAudio::FindFmod(read, w.first, w.second.size(), f); + const int ns = OtherAudio::FindSetter(read, w.first, w.second.size(), t); + if (nc && !s.chains) + s.chain = c; + if (nf && !s.fmods) + s.fmod = f; + if (ns && !s.setters) + s.setter = t; + s.chains += nc; + s.fmods += nf; + s.setters += ns; + } + return s; +} + +Memory FromFixture(const char *tag) { + Memory m; + for (const auto &img : Fixtures::All()) + if (std::strcmp(img.tag, tag) == 0) + for (const auto &w : img.windows) + m.windows.push_back({w.va, w.bytes}); + return m; +} + +struct Expected { + const char *tag; + std::uintptr_t site, gate, accessor, global, fadeReturn; + std::uint32_t flag, systemIo; + std::uintptr_t systemIoGlobal; +}; + +const Expected kExpected[] = { + {"0.35.1", 0x1a2d5e8, 0x1a2d5f8, 0x2a1e81c, 0x3631190, 0x1a2d624, 0xa71, 0x21a868, 0x309ef78}, + {"0.35.0", 0x1a2d5dc, 0x1a2d5ec, 0x2a1e81c, 0x3631180, 0x1a2d618, 0xa71, 0x21a868, 0x309ef68}, + {"0.34.5", 0x197be90, 0x197bea0, 0x2930490, 0x351d7f0, 0x197becc, 0xa71, 0x21a838, 0x2f9a0a8}, + {"0.33.8", 0x183a7a4, 0x183a7b4, 0, 0x32e44c0, 0x183a7e0, 0x821, 0, 0}, +}; + +void ChainDecodesOnEveryFixture() { + for (const Expected &e : kExpected) { + const Scan s = Run(FromFixture(e.tag)); + const std::string t = e.tag; + Check(s.chains == 1, t + ": exactly one update site"); + Check(s.chain.site == e.site, t + ": update site"); + Check(s.chain.slot == 0xa8, t + ": SystemIO slot"); + Check(s.chain.gateReturn == e.gate, t + ": gate return"); + Check(s.chain.flagOffset == e.flag, t + ": flag offset"); + Check(s.chain.audioGlobal == e.global, t + ": audio global"); + Check(!e.accessor || s.chain.accessor == e.accessor, t + ": accessor"); + Check(s.chain.fadeSlot == 0xd8, t + ": fade slot"); + Check(s.chain.fadeReturn == e.fadeReturn, t + ": fade return"); + } +} + +void SystemIoRoutesAgree() { + for (const Expected &e : kExpected) { + const Scan s = Run(FromFixture(e.tag)); + const std::string t = e.tag; + if (e.systemIo == 0) { + Check(s.fmods == 0 && s.setters == 0, t + ": no SystemIO route on the older shape"); + continue; + } + Check(s.fmods == 1 && s.setters == 1, t + ": both SystemIO routes found"); + Check(s.fmod.slot == s.chain.slot, t + ": fmod slot equals update slot"); + Check(s.fmod.systemIoOffset == e.systemIo, t + ": fmod SystemIO offset"); + Check(s.setter.systemIoOffset == e.systemIo, t + ": setter SystemIO offset"); + Check(s.setter.systemIoGlobal == e.systemIoGlobal, t + ": SystemIO global"); + } +} + +void SingleWordMutantsFailClosed() { + const Expected &e = kExpected[0]; + const struct { + const char *what; + std::uintptr_t at; + std::uint32_t word; + int field; + } mutants[] = { + {"cset ne instead of eq", e.site + 24, 0x1a9f07e8u, 0}, + {"and with w1 instead of w0", e.site + 28, 0x0a080028u, 0}, + {"flag stored one byte off", e.site + 32, 0x39000000u | (0xa72u << 10) | (19u << 5) | 8u, 0}, + {"slot load without blr", e.site + 12, 0xd503201fu, 0}, + {"fade bus index 2", e.site + 48, 0x52800041u, 0}, + {"fmod 0x300 instead of 0x200", 0xdc3b7cu, 0x52806009u, 1}, + {"setter callee without the global store", 0xdd758cu, 0xd503201fu, 2}, + }; + for (const auto &m : mutants) { + Memory mem = FromFixture(e.tag); + mem.patches[m.at] = m.word; + const Scan s = Run(mem); + const int count = m.field == 0 ? s.chains : m.field == 1 ? s.fmods : s.setters; + Check(count == 0, std::string("mutant rejected: ") + m.what); + } +} + +bool LoadElf(const char *path, Memory &mem) { + FILE *f = std::fopen(path, "rb"); + if (!f) + return false; + std::fseek(f, 0, SEEK_END); + const long size = std::ftell(f); + std::fseek(f, 0, SEEK_SET); + std::vector data(static_cast(size)); + const std::size_t got = std::fread(data.data(), 1, data.size(), f); + std::fclose(f); + if (got != data.size() || size < 64) + return false; + std::uint64_t phoff = 0; + std::uint16_t phentsize = 0, phnum = 0; + std::memcpy(&phoff, &data[0x20], 8); + std::memcpy(&phentsize, &data[0x36], 2); + std::memcpy(&phnum, &data[0x38], 2); + for (int i = 0; i < phnum; ++i) { + const unsigned char *ph = &data[phoff + static_cast(i) * phentsize]; + std::uint32_t type = 0, flags = 0; + std::uint64_t off = 0, vaddr = 0, filesz = 0; + std::memcpy(&type, ph, 4); + std::memcpy(&flags, ph + 4, 4); + std::memcpy(&off, ph + 8, 8); + std::memcpy(&vaddr, ph + 16, 8); + std::memcpy(&filesz, ph + 32, 8); + if (type == 1 && (flags & 1)) + mem.windows.push_back({static_cast(vaddr), + std::vector(data.begin() + static_cast(off), + data.begin() + static_cast(off + filesz))}); + } + return !mem.windows.empty(); +} + +void FullImagesFromEnvironment() { + const char *list = std::getenv("OTHER_AUDIO_IMAGES"); + if (!list || !*list) + return; + std::string all = list; + std::size_t start = 0; + while (start <= all.size()) { + const std::size_t end = all.find(';', start); + const std::string path = all.substr(start, end == std::string::npos ? std::string::npos : end - start); + start = end == std::string::npos ? all.size() + 1 : end + 1; + if (path.empty()) + continue; + Memory mem; + if (!LoadElf(path.c_str(), mem)) { + Check(false, "cannot load " + path); + continue; + } + const Scan s = Run(mem); + Check(s.chains == 1, path + ": exactly one update site"); + if (s.fmods == 1 && s.setters == 1) + Check(s.fmod.systemIoOffset == s.setter.systemIoOffset, path + ": SystemIO routes agree"); + std::printf("%s: chains %d fmods %d setters %d\n", path.c_str(), s.chains, s.fmods, s.setters); + } +} + +} + +int main() { + std::setvbuf(stdout, nullptr, _IONBF, 0); + ChainDecodesOnEveryFixture(); + SystemIoRoutesAgree(); + SingleWordMutantsFailClosed(); + FullImagesFromEnvironment(); + std::printf("%d checks, %d failed\n", g_checks, g_failures); + return g_failures ? 1 : 0; +}