From a1672a33d775dec91a33b7efa362e02189cc9e25 Mon Sep 17 00:00:00 2001 From: J Reece Wilson Date: Sat, 29 Jun 2024 16:06:42 +0100 Subject: [PATCH] [*] Linux alternative dynamic loader: AuProcess main process path has to be pulled from the procmap instead of arg[0] --- Source/AuRTEntrypoint.cpp | 2 +- Source/CmdLine/AuCmdLinePlatform.cpp | 3 +++ Source/Process/AuPaths.cpp | 37 ++++++++++++++++++--------- Source/Process/AuProcessMap.Linux.cpp | 37 ++++++++++++++++++++++----- 4 files changed, 60 insertions(+), 19 deletions(-) diff --git a/Source/AuRTEntrypoint.cpp b/Source/AuRTEntrypoint.cpp index 07c0c6ac..9da0334e 100644 --- a/Source/AuRTEntrypoint.cpp +++ b/Source/AuRTEntrypoint.cpp @@ -79,6 +79,7 @@ static void Init() AuProcess::WarmStartupCaches(); Aurora::Threading::InitSleep(); + Aurora::CmdLine::Init(); Aurora::Process::InitProcess(); Aurora::SWInfo::InitSwInfo(); @@ -99,7 +100,6 @@ static void Init() Aurora::Telemetry::Init(); Aurora::Debug::InitDebug(); Aurora::Locale::Init(); - Aurora::CmdLine::Init(); Aurora::Processes::Init(); Aurora::Hashing::InitHashing(); Aurora::Async::InitAsync(); diff --git a/Source/CmdLine/AuCmdLinePlatform.cpp b/Source/CmdLine/AuCmdLinePlatform.cpp index 33792299..53216d53 100644 --- a/Source/CmdLine/AuCmdLinePlatform.cpp +++ b/Source/CmdLine/AuCmdLinePlatform.cpp @@ -32,6 +32,8 @@ namespace Aurora::CmdLine } #if defined(AURORA_IS_POSIX_DERIVED) + AuString gHackLoader; + static AuList GetProcFSCmdString() { AuList args; @@ -67,6 +69,7 @@ namespace Aurora::CmdLine { if (AuStringContains(args[0], "ld-Aurora")) { + gHackLoader = args[0]; args.erase(args.begin()); } } diff --git a/Source/Process/AuPaths.cpp b/Source/Process/AuPaths.cpp index ab75a5d8..a362c955 100644 --- a/Source/Process/AuPaths.cpp +++ b/Source/Process/AuPaths.cpp @@ -18,6 +18,21 @@ namespace Aurora::Process { + static AuString gCachedModule, gCachedPartialPath, gCachedFullPath; + static AuInitOnce gInitOnce; + + void ForceReplacePathHack(const AuString &path) + { + static AuInitOnce gInitOnce2; + gInitOnce2.Call([&]() + { + AuResetMember(gCachedModule); + AuResetMember(gCachedPartialPath); + AuResetMember(gCachedFullPath, path); + AuResetMember(gInitOnce); + }); + } + AUKN_SYM bool GetWorkingDirectory(AuString &path) { #if !defined(AURORA_IS_MODERNNT_DERIVED) @@ -188,9 +203,6 @@ namespace Aurora::Process static bool GetModulePath(const AuString *&module, const AuString *&partialPath, const AuString *&fullPath) { - static AuString cachedModule, cachedPartialPath, cachedFullPath; - static AuInitOnce gInitOnce; - if (!AuThreading::InitOnceLocker::TryLock(&gInitOnce, true)) { gInitOnce.Wait(); @@ -203,17 +215,18 @@ namespace Aurora::Process { char spitter; - if (GetModulePathSlow(cachedFullPath, spitter)) + if (gCachedFullPath.size() || + GetModulePathSlow(gCachedFullPath, spitter)) { - auto indexA = cachedFullPath.find_last_of(spitter); + auto indexA = gCachedFullPath.find_last_of(spitter); if (indexA != AuString::npos) { - cachedModule = cachedFullPath.substr(indexA + 1); - cachedPartialPath = cachedFullPath.substr(0, indexA); + gCachedModule = gCachedFullPath.substr(indexA + 1); + gCachedPartialPath = gCachedFullPath.substr(0, indexA); - if (!cachedPartialPath.ends_with(AuFS::kPathSplitter)) + if (!gCachedPartialPath.ends_with(AuFS::kPathSplitter)) { - cachedPartialPath += AuFS::kPathSplitter; + gCachedPartialPath += AuFS::kPathSplitter; } } else @@ -240,9 +253,9 @@ namespace Aurora::Process return false; } - module = &cachedModule; - fullPath = &cachedFullPath; - partialPath = &cachedPartialPath; + module = &gCachedModule; + fullPath = &gCachedFullPath; + partialPath = &gCachedPartialPath; return true; } diff --git a/Source/Process/AuProcessMap.Linux.cpp b/Source/Process/AuProcessMap.Linux.cpp index f29648f1..b1f2a953 100644 --- a/Source/Process/AuProcessMap.Linux.cpp +++ b/Source/Process/AuProcessMap.Linux.cpp @@ -18,8 +18,15 @@ using Elf_Shdr = Elf32_Shdr; #endif +namespace Aurora::CmdLine +{ + extern AuString gHackLoader; +} + namespace Aurora::Process { + void ForceReplacePathHack(const AuString &path); + void InitProcessMapLinux() { @@ -44,10 +51,13 @@ namespace Aurora::Process fileName = *optProcessPath; } } - - if (!AuIOFS::FileExists(fileName)) + else { - return 0; + if (!AuIOFS::FileExists(fileName)) + { + // NO FILE linux-vdso.so.1 + return 0; + } } AuList> sections; @@ -124,16 +134,32 @@ namespace Aurora::Process name = kSectionNameFile; } + AuOptional optProcessPath; + if (AuCmdLine::gHackLoader.size()) + { + optProcessPath = GetProcessFullPath(); + } + if (name.size()) { bIsSpecialFile = AuIOFS::FileExists(name); if (bIsSpecialFile) { - for (const auto &[uBaseAddress, pathSectionPair] : gModuleMap) + for (auto &[uBaseAddress, pathSectionPair] : gModuleMap) { if (uBaseAddress == base) { + if (optProcessPath) + { + if (AuGet<0>(pathSectionPair) == optProcessPath.Value()) + { + ForceReplacePathHack(name); + AuGet<0>(pathSectionPair) = name; + AuResetMember(optProcessPath); + } + } + return; } @@ -268,8 +294,7 @@ namespace Aurora::Process object->SetOffset(strtab.sh_offset); if (!object->Read(AuMemoryViewStreamWrite(AuMemoryViewWrite(strHeap.data(), - strHeap.size() - ), + strHeap.size()), read))) { SysPushErrorIO();