2021-09-13 20:11:12 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
File: AuProcess.cpp
|
2021-09-13 20:11:12 +00:00
|
|
|
Date: 2021-8-20
|
|
|
|
Author: Reece
|
|
|
|
***/
|
2021-09-30 14:57:41 +00:00
|
|
|
#include <Source/RuntimeInternal.hpp>
|
2021-09-06 10:58:08 +00:00
|
|
|
#include "Process.hpp"
|
2022-01-24 21:37:50 +00:00
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
2022-01-24 21:37:50 +00:00
|
|
|
#include <stdlib.h>
|
2022-04-05 02:36:39 +00:00
|
|
|
#include <dlfcn.h>
|
2022-01-24 21:37:50 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2022-08-14 13:41:19 +00:00
|
|
|
#include <signal.h>
|
2021-09-06 10:58:08 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-16 01:10:00 +00:00
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
#include <Softpub.h>
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#include <wintrust.h>
|
|
|
|
#endif
|
|
|
|
|
2022-08-10 09:50:49 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessSectionView.NT.hpp"
|
2022-08-10 09:50:49 +00:00
|
|
|
#endif
|
|
|
|
|
2021-11-16 01:10:00 +00:00
|
|
|
#include <Source/IO/FS/FS.hpp>
|
|
|
|
#include <Source/IO/FS/Resources.hpp>
|
|
|
|
|
2022-12-17 20:14:19 +00:00
|
|
|
#include "AuProcessMap.hpp"
|
2022-01-24 18:37:06 +00:00
|
|
|
|
2023-08-30 00:28:05 +00:00
|
|
|
#if defined(AURORA_COMPILER_CLANG)
|
|
|
|
// warning: enumeration values 'kEnumCount' and 'kEnumInvalid' not handled in switch [-Wswitch
|
|
|
|
#pragma clang diagnostic ignored "-Wswitch"
|
|
|
|
// Yea, I don't give a shit.
|
|
|
|
#endif
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
namespace Aurora::Process
|
|
|
|
{
|
2024-04-20 04:41:56 +00:00
|
|
|
static AuCriticalSection gSpinLock;
|
2023-10-10 23:21:32 +00:00
|
|
|
static AuList<AuString> gClassPath;
|
2021-11-16 01:10:00 +00:00
|
|
|
static AuHashMap<AuString, void *> gModuleHandles;
|
|
|
|
static const bool kIsMainSigned = false;
|
|
|
|
|
2022-09-30 22:47:18 +00:00
|
|
|
void LoadProcessSectionViewSymbol();
|
|
|
|
|
2021-11-15 13:07:38 +00:00
|
|
|
static constexpr const char *GetPlatformString(Build::EPlatform platform)
|
|
|
|
{
|
|
|
|
switch (platform)
|
|
|
|
{
|
|
|
|
case Build::EPlatform::ePlatformWin32:
|
|
|
|
return ".Win32";
|
|
|
|
case Build::EPlatform::ePlatformLinux:
|
|
|
|
return ".Linux";
|
|
|
|
case Build::EPlatform::ePlatformAndroid:
|
|
|
|
return ".Android";
|
2022-01-26 00:01:06 +00:00
|
|
|
case Build::EPlatform::ePlatformAppleMacOS:
|
2021-11-15 13:07:38 +00:00
|
|
|
return ".Mac";
|
|
|
|
case Build::EPlatform::ePlatformIos:
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
return ".iOS";
|
|
|
|
case Build::EPlatform::eKernelBsd:
|
|
|
|
return ".BSD";
|
2021-11-15 13:07:38 +00:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr const char *GetPlatformExt(Build::EPlatform platform)
|
|
|
|
{
|
|
|
|
switch (platform)
|
|
|
|
{
|
|
|
|
case Build::EPlatform::ePlatformWin32:
|
|
|
|
return ".dll";
|
|
|
|
case Build::EPlatform::ePlatformLinux:
|
|
|
|
case Build::EPlatform::ePlatformAndroid:
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
case Build::EPlatform::eKernelBsd:
|
2021-11-15 13:07:38 +00:00
|
|
|
return ".so";
|
2022-01-26 00:01:06 +00:00
|
|
|
case Build::EPlatform::ePlatformAppleMacOS:
|
2021-11-15 13:07:38 +00:00
|
|
|
case Build::EPlatform::ePlatformIos:
|
2022-01-27 05:24:08 +00:00
|
|
|
return ".dylib";
|
2021-11-15 13:07:38 +00:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr const char *GetArchString(Build::EArchitecture architecture)
|
|
|
|
{
|
|
|
|
switch (architecture)
|
|
|
|
{
|
|
|
|
case Build::EArchitecture::eX86_32:
|
|
|
|
return ".x86_32";
|
|
|
|
case Build::EArchitecture::eX86_64:
|
|
|
|
return ".x86_64";
|
|
|
|
case Build::EArchitecture::eAArch64:
|
[*/+/-] MEGA COMMIT. ~2 weeks compressed.
The intention is to quickly improve and add util apis, enhance functionality given current demands, go back to the build pipeline, finish that, publish runtime tests, and then use what we have to go back to to linux support with a more stable api.
[+] AuMakeSharedArray
[+] Technet ArgvQuote
[+] Grug subsystem (UNIX signal thread async safe ipc + telemetry flusher + log flusher.)
[+] auEndianness -> Endian swap utils
[+] AuGet<N>(...)
[*] AUE_DEFINE conversion for
ECompresionType, EAnsiColor, EHashType, EStreamError, EHexDump
[+] ConsoleMessage ByteBuffer serialization
[+] CmdLine subsystem for parsing command line arguments and simple switch/flag checks
[*] Split logger from console subsystem
[+] StartupParameters -> A part of a clean up effort under Process
[*] Refactor SysErrors header + get caller hack
[+] Atomic APIs
[+] popcnt
[+] Ring Buffer sink
[+] Added more standard errors
Catch,
Submission,
LockError,
NoAccess,
ResourceMissing,
ResourceLocked,
MalformedData,
InSandboxContext,
ParseError
[+] Added ErrorCategorySet, ErrorCategoryClear, GetStackTrace
[+] IExitSubscriber, ETriggerLevel
[*] Write bias the high performance RWLockImpl read-lock operation operation
[+] ExitHandlerAdd/ExitHandlerRemove (exit subsystem)
[*] Updated API style
Digests
[+] CpuId::CpuBitCount
[+] GetUserProgramsFolder
[+] GetPackagePath
[*] Split IStreamReader with an inl file
[*] BlobWriter/BlobReader/BlobArbitraryReader can now take shared pointers to bytebuffers. default constructor allocates a new scalable bytebuffer
[+] ICharacterProvider
[+] ICharacterProviderEx
[+] IBufferedCharacterConsumer
[+] ProviderFromSharedString
[+] ProviderFromString
[+] BufferConsumerFromProvider
[*] Parse Subsystem uses character io bufferer
[*] Rewritten NT's high perf semaphore to use userland SRW/ConVars [like mutex, based on generic semaphore]
[+] ByteBuffer::ResetReadPointer
[*] Bug fix bytebuffer base not reset on free and some scaling issues
[+] ProcessMap -> Added kSectionNameStack, kSectionNameFile, kSectionNameHeap for Section
[*] ProcessMap -> Refactor Segment to Section. I was stupid for keeping a type conflict hack API facing
[+] Added 64 *byte* fast RNG seeds
[+] File Advisorys/File Lock Awareness
[+] Added extended IAuroraThread from OS identifier caches for debug purposes
[*] Tweaked how memory is reported on Windows. Better consistency of what values mean across functions.
[*] Broke AuroraUtils/Typedefs out into a separate library
[*] Update build script
[+] Put some more effort into adding detail to the readme before rewriting it, plus, added some media
[*] Improved public API documentation
[*] Bug fix `SetConsoleCtrlHandler`
[+] Locale TimeDateToFileNameISO8601
[+] Console config stdOutShortTime
[*] Begin using internal UTF8/16 decoders when platform support isnt available (instead of stl)
[*] Bug fixes in decoders
[*] Major bug fix, AuMax
[+] RateLimiter
[+] Binary file sink
[+] Log directory sink
[*] Data header usability (more operators)
[+] AuRemoveRange
[+] AuRemove
[+] AuTryRemove
[+] AuTryRemoveRange
[+] auCastUtils
[+] Finish NewLSWin32Source
[+] AuTryFindByTupleN, AuTryRemoveByTupleN
[+] Separated AuRead/Write types, now in auTypeUtils
[+] Added GetPosition/SetPosition to FileWriter
[*] Fix stupid AuMin in place of AuMax in SpawnThread.Unix.Cpp
[*] Refactored Arbitrary readers to SeekingReaders (as in, they could be atomic and/or parallelized, and accept an arbitrary position as a work parameter -> not Seekable, as in, you can simply set the position)
[*] Hack back in the sched deinit
[+] File AIO loop source interop
[+] Begin to prototype a LoopQueue object I had in mind for NT, untested btw
[+] Stub code for networking
[+] Compression BaseStream/IngestableStreamBase
[*] Major: read/write locks now support write-entrant read routines.
[*] Compression subsystem now uses the MemoryView concept
[*] Rewrite the base stream compressions, made them less broken
[*] Update hashing api
[*] WriterTryGoForward and ReaderTryGoForward now revert to the previous relative index instead of panicing
[+] Added new AuByteBuffer apis
Trim, Pad, WriteFrom, WriteString, [TODO: ReadString]
[+] Added ByteBufferPushReadState
[+] Added ByteBufferPushWriteState
[*] Move from USC-16 to full UTF-16. Win32 can handle full UTF-16.
[*] ELogLevel is now an Aurora enum
[+] Raised arbitrary limit in header to 255, the max filter buffer
[+] Explicit GZip support
[+] Explicit Zip support
[+] Added [some] compressors
et al
2022-02-17 00:11:40 +00:00
|
|
|
return ".arm";
|
2021-11-15 13:07:38 +00:00
|
|
|
default:
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 02:36:39 +00:00
|
|
|
static AuString ConstructAuDllSuffixUncached()
|
2021-11-15 13:07:38 +00:00
|
|
|
{
|
|
|
|
auto platform = GetPlatformString(Build::kCurrentPlatform);
|
|
|
|
auto architecture = GetArchString(Build::kCurrentArchitecture);
|
|
|
|
auto ext = GetPlatformExt(Build::kCurrentPlatform);
|
|
|
|
|
|
|
|
AuString ret;
|
|
|
|
|
|
|
|
#if defined(DEBUG)
|
|
|
|
ret += ".Debug";
|
|
|
|
#elif defined(STAGING)
|
|
|
|
ret += ".Stage";
|
|
|
|
#elif defined(SHIP)
|
|
|
|
ret += ".Ship";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (platform)
|
|
|
|
{
|
|
|
|
ret += platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (architecture)
|
|
|
|
{
|
|
|
|
ret += architecture;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext)
|
|
|
|
{
|
|
|
|
ret += ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-10-10 18:13:37 +00:00
|
|
|
AUKN_SYM const AuString& ConstructAuDllSuffix()
|
2022-04-05 02:36:39 +00:00
|
|
|
{
|
|
|
|
static AuString dllSuffixString {};
|
2023-10-10 18:13:37 +00:00
|
|
|
static AuInitOnce gInitOnce;
|
|
|
|
gInitOnce.Call([]
|
|
|
|
{
|
|
|
|
dllSuffixString = ConstructAuDllSuffixUncached();
|
|
|
|
});
|
2022-04-05 02:36:39 +00:00
|
|
|
return dllSuffixString;
|
|
|
|
}
|
|
|
|
|
2023-10-12 17:58:11 +00:00
|
|
|
static AuString GetModuleNameFromFileName(const AuString &filename)
|
|
|
|
{
|
|
|
|
static const auto &kStringSuffixA = ConstructAuDllSuffix();
|
|
|
|
static const auto kStringSuffixB = GetPlatformExt(Build::kCurrentPlatform);
|
|
|
|
|
|
|
|
if (filename.ends_with(kStringSuffixA))
|
|
|
|
{
|
|
|
|
return filename.substr(0, filename.size() - kStringSuffixA.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename.ends_with(kStringSuffixB))
|
|
|
|
{
|
|
|
|
return filename.substr(0, filename.size() - strlen(kStringSuffixB));
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:10:00 +00:00
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
bool VerifyEmbeddedSignature(const wchar_t *path, HANDLE handle)
|
|
|
|
{
|
|
|
|
LONG lStatus;
|
|
|
|
DWORD dwLastError;
|
|
|
|
|
|
|
|
WINTRUST_FILE_INFO FileData;
|
|
|
|
memset(&FileData, 0, sizeof(FileData));
|
|
|
|
FileData.cbStruct = sizeof(WINTRUST_FILE_INFO);
|
|
|
|
FileData.pcwszFilePath = path;
|
|
|
|
FileData.hFile = handle;
|
|
|
|
FileData.pgKnownSubject = NULL;
|
|
|
|
|
|
|
|
GUID WVTPolicyGUID = WINTRUST_ACTION_GENERIC_VERIFY_V2;
|
|
|
|
WINTRUST_DATA WinTrustData;
|
|
|
|
|
|
|
|
// Initialize the WinVerifyTrust input data structure.
|
|
|
|
|
|
|
|
// Default all fields to 0.
|
|
|
|
memset(&WinTrustData, 0, sizeof(WinTrustData));
|
|
|
|
|
|
|
|
WinTrustData.cbStruct = sizeof(WinTrustData);
|
|
|
|
|
|
|
|
// Use default code signing EKU.
|
|
|
|
WinTrustData.pPolicyCallbackData = NULL;
|
|
|
|
|
|
|
|
// No data to pass to SIP.
|
|
|
|
WinTrustData.pSIPClientData = NULL;
|
|
|
|
|
|
|
|
// Disable WVT UI.
|
|
|
|
WinTrustData.dwUIChoice = WTD_UI_NONE;
|
|
|
|
|
2022-04-05 02:36:39 +00:00
|
|
|
// Revocation checking.constexpr
|
2021-11-16 01:10:00 +00:00
|
|
|
WinTrustData.fdwRevocationChecks = WTD_REVOKE_WHOLECHAIN;
|
|
|
|
|
|
|
|
// Verify an embedded signature on a file.
|
|
|
|
WinTrustData.dwUnionChoice = WTD_CHOICE_FILE;
|
|
|
|
|
|
|
|
// Verify action.
|
|
|
|
WinTrustData.dwStateAction = WTD_STATEACTION_VERIFY;
|
|
|
|
|
|
|
|
// Verification sets this value.
|
|
|
|
WinTrustData.hWVTStateData = NULL;
|
|
|
|
|
|
|
|
// Not used.
|
|
|
|
WinTrustData.pwszURLReference = NULL;
|
|
|
|
|
|
|
|
// This is not applicable if there is no UI because it changes
|
|
|
|
// the UI to accommodate running applications instead of
|
|
|
|
// installing applications.
|
|
|
|
WinTrustData.dwUIContext = 0;
|
|
|
|
|
|
|
|
// Set pFile.
|
|
|
|
WinTrustData.pFile = &FileData;
|
|
|
|
|
2023-08-27 16:43:29 +00:00
|
|
|
if (!pWinVerifyTrust)
|
|
|
|
{
|
|
|
|
return gRuntimeConfig.debug.bWin32VerifyTrustFailMissingAPI;
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:10:00 +00:00
|
|
|
// WinVerifyTrust verifies signatures as specified by the GUID
|
|
|
|
// and Wintrust_Data.
|
2023-08-27 16:43:29 +00:00
|
|
|
lStatus = pWinVerifyTrust(
|
2021-11-16 01:10:00 +00:00
|
|
|
NULL,
|
|
|
|
&WVTPolicyGUID,
|
|
|
|
&WinTrustData);
|
|
|
|
|
|
|
|
bool status {};
|
|
|
|
|
|
|
|
switch (lStatus)
|
|
|
|
{
|
|
|
|
case ERROR_SUCCESS:
|
|
|
|
status = true;
|
|
|
|
break;
|
|
|
|
case TRUST_E_NOSIGNATURE:
|
|
|
|
// The file was not signed or had a signature
|
|
|
|
// that was not valid.
|
|
|
|
|
|
|
|
// Get the reason for no signature.
|
|
|
|
dwLastError = GetLastError();
|
|
|
|
if (TRUST_E_NOSIGNATURE == dwLastError ||
|
|
|
|
TRUST_E_SUBJECT_FORM_UNKNOWN == dwLastError ||
|
|
|
|
TRUST_E_PROVIDER_UNKNOWN == dwLastError)
|
|
|
|
{
|
|
|
|
SysPushErrorCrypt("The file is not signed.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysPushErrorCrypt("An unknown error occurred trying to verify the signature of the file.");
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
case TRUST_E_EXPLICIT_DISTRUST:
|
|
|
|
SysPushErrorCrypt("The signature is present, but specifically disallowed.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
case TRUST_E_SUBJECT_NOT_TRUSTED:
|
|
|
|
SysPushErrorCrypt("The signature is present, but not trusted.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
case CRYPT_E_SECURITY_SETTINGS:
|
|
|
|
SysPushErrorCrypt("CRYPT_E_SECURITY_SETTINGS - The hash "
|
|
|
|
"representing the subject or the publisher wasn't "
|
|
|
|
"explicitly trusted by the admin and admin policy "
|
|
|
|
"has disabled user trust. No signature, publisher "
|
|
|
|
"or timestamp errors.");
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
SysPushErrorCrypt("Dependency Injection Error");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
WinTrustData.dwStateAction = WTD_STATEACTION_CLOSE;
|
|
|
|
|
2023-08-27 16:43:29 +00:00
|
|
|
if (!pWinVerifyTrust)
|
|
|
|
{
|
|
|
|
return gRuntimeConfig.debug.bWin32VerifyTrustFailMissingAPI;
|
|
|
|
}
|
|
|
|
|
|
|
|
lStatus = pWinVerifyTrust(
|
2021-11-16 01:10:00 +00:00
|
|
|
NULL,
|
|
|
|
&WVTPolicyGUID,
|
|
|
|
&WinTrustData);
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
static void *LoadModule(const AuString &name, const AuString &path)
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-12 17:58:11 +00:00
|
|
|
auto itr = gModuleHandles.find(path);
|
|
|
|
if (itr != gModuleHandles.end())
|
|
|
|
{
|
|
|
|
return itr->second;
|
|
|
|
}
|
|
|
|
|
2021-11-16 01:10:00 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-09-17 21:06:48 +00:00
|
|
|
if (!pLoadLibraryW)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2023-09-17 21:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto handle = pLoadLibraryW(Locale::ConvertFromUTF8(path).c_str());
|
2021-11-16 01:10:00 +00:00
|
|
|
if (handle == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2021-11-16 01:58:26 +00:00
|
|
|
SysPushErrorNested("Could't link dynamic library {}", path);
|
2021-11-16 01:10:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
#else
|
2021-11-16 01:58:26 +00:00
|
|
|
auto handle = dlopen(path.c_str(), RTLD_DEEPBIND);
|
|
|
|
if (handle == nullptr)
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2021-11-16 01:58:26 +00:00
|
|
|
SysPushErrorNested("Could't link dynamic library {}", path);
|
2021-11-16 01:10:00 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
auto pRet = (void *)handle;
|
|
|
|
gModuleHandles.insert(AuMakePair(name, pRet));
|
2023-10-12 17:58:11 +00:00
|
|
|
gModuleHandles.insert(AuMakePair(path, pRet));
|
2023-10-10 23:21:32 +00:00
|
|
|
return pRet;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
static void *TryLoadModule(const AuString &path,
|
2021-11-16 01:10:00 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
const AuString &abs,
|
|
|
|
#endif
|
|
|
|
const ModuleLoadRequest &request,
|
|
|
|
bool &fail
|
|
|
|
)
|
|
|
|
{
|
|
|
|
fail = false;
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
auto pathNrml = AuIOFS::NormalizePathRet(path);
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
auto widePath = Locale::ConvertFromUTF8(pathNrml);
|
2023-09-13 01:27:18 +00:00
|
|
|
auto mitigateTimeOfUse = Win32Open(widePath.c_str(), GENERIC_READ, FILE_SHARE_READ, false, OPEN_EXISTING, 0, 0);
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
if (mitigateTimeOfUse == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
|
|
|
SysPushErrorNested("Couldn't open existing file. Race exploit?");
|
|
|
|
fail = true;
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
auto absPath = AuIOFS::NormalizePathRet(abs);
|
2021-11-16 01:10:00 +00:00
|
|
|
#endif
|
|
|
|
|
2023-10-10 18:13:37 +00:00
|
|
|
if (request.verify || ((kIsMainSigned || request.forceMitigations) && request.enableMitigations))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
if (!VerifyEmbeddedSignature(widePath.c_str(), mitigateTimeOfUse))
|
|
|
|
{
|
|
|
|
SysPushErrorNested("Couldn't verify file {}", path);
|
|
|
|
fail = true;
|
|
|
|
AuWin32CloseHandle(mitigateTimeOfUse);
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
#else
|
2022-01-24 18:37:06 +00:00
|
|
|
AuLogWarn("Can't verify {} on this platform", path);
|
2021-11-16 01:10:00 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_POSIX_DERIVED)
|
|
|
|
if (request.enableMitigations && request.unixCheckPlusX)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
|
|
|
|
if (stat(pathNrml.data(), &sb) != 0)
|
|
|
|
{
|
|
|
|
SysPushErrorNested("Couldn't open existing file. Race exploit?");
|
|
|
|
fail = true;
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((sb.st_mode & S_IXUSR) == 0)
|
|
|
|
{
|
|
|
|
fail = true;
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-10-10 20:24:41 +00:00
|
|
|
auto returnValue = LoadModule(request.mod, absPath);
|
2021-11-16 01:10:00 +00:00
|
|
|
#else
|
2023-10-10 20:24:41 +00:00
|
|
|
auto returnValue = LoadModule(request.mod, pathNrml);
|
2021-11-16 01:10:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2021-12-24 14:34:55 +00:00
|
|
|
// Still doesnt stop hotswaps, and idc
|
|
|
|
// Glitching would require software to time the attack
|
|
|
|
// ...requiring a certain degree of access (code execution) + effort
|
2021-11-16 01:10:00 +00:00
|
|
|
AuWin32CloseHandle(mitigateTimeOfUse);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return returnValue;
|
|
|
|
}
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
static void *TryLoadModule(const AuString &path, const AuString &auDll, const AuString &genericDll, const ModuleLoadRequest &request, bool &fail)
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-13 19:27:21 +00:00
|
|
|
AuString a = path + AuString({ AuFS::kPathSplitter }) + auDll;
|
|
|
|
AuString b = path + AuString({ AuFS::kPathSplitter }) + genericDll;
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-10-28 20:13:19 +00:00
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
if (gRuntimeConfig.win32Config.bProcessCheckWinLdrForModNameFirst)
|
|
|
|
{
|
|
|
|
auto aW = AuLocale::ConvertFromUTF8(genericDll);
|
|
|
|
auto bW = AuLocale::ConvertFromUTF8(auDll);
|
|
|
|
|
|
|
|
auto pDLL = ::GetModuleHandleW(aW.c_str());
|
|
|
|
if (!pDLL)
|
|
|
|
{
|
|
|
|
pDLL = ::GetModuleHandleW(bW.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pDLL)
|
|
|
|
{
|
|
|
|
gModuleHandles.insert(AuMakePair(request.mod, (void *)pDLL));
|
|
|
|
return pDLL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-10-13 19:27:21 +00:00
|
|
|
AuString aAbs = path + AuString({ AuFS::kPathSplitter }) + auDll + ".";
|
|
|
|
AuString bAbs = path + AuString({ AuFS::kPathSplitter }) + genericDll + ".";
|
2021-11-16 01:10:00 +00:00
|
|
|
#endif
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
if (AuIOFS::FileExists(a))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
|
|
|
return TryLoadModule(a,
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
aAbs,
|
|
|
|
#endif
|
|
|
|
request,
|
|
|
|
fail
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-24 18:37:06 +00:00
|
|
|
if (AuIOFS::FileExists(b))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
|
|
|
return TryLoadModule(b,
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
bAbs,
|
|
|
|
#endif
|
|
|
|
request,
|
|
|
|
fail
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
static void *TryLoadModule(EModulePath path, const AuString &auDll, const AuString &genericDll, const ModuleLoadRequest &request)
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
|
|
|
AuString pathA, pathB;
|
2023-10-10 23:21:32 +00:00
|
|
|
AuList<AuString> arrayPaths;
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
switch (path)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
case EModulePath::eClassPath:
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
arrayPaths = gClassPath;
|
|
|
|
break;
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
case EModulePath::eModulePathCWD:
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
|
|
|
if (!Process::GetWorkingDirectory(pathA))
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
break;
|
2023-10-10 23:21:32 +00:00
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
case EModulePath::eProcessDirectory:
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
2023-12-13 18:11:35 +00:00
|
|
|
if (auto pProcPath = Process::GetProcessDirectory())
|
|
|
|
{
|
|
|
|
pathA = *pProcPath;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
case EModulePath::eModulePathSystemDir:
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
pathA = AuIOFS::GetSystemLibPath().value_or(AuString {});
|
|
|
|
pathB = AuIOFS::GetSystemLibPath2().value_or(AuString {});
|
2021-11-16 01:10:00 +00:00
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
if (pathA.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
break;
|
2023-10-10 23:21:32 +00:00
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
case EModulePath::eModulePathUserDir:
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
2022-01-24 18:37:06 +00:00
|
|
|
pathA = AuIOFS::GetUserLibPath().value_or(AuString {});
|
|
|
|
pathB = AuIOFS::GetUserLibPath2().value_or(AuString {});
|
2023-10-10 23:21:32 +00:00
|
|
|
|
|
|
|
if (pathA.empty())
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
|
|
|
|
break;
|
2023-10-10 23:21:32 +00:00
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
//case EModulePath::eOSSpecified:
|
|
|
|
//
|
|
|
|
// break;
|
|
|
|
case EModulePath::eSpecified:
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool fail {};
|
|
|
|
|
|
|
|
if (pathA.size())
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
if (auto pRet = TryLoadModule(pathA, auDll, genericDll, request, fail))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return pRet;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pathB.size())
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
if (auto pRet = TryLoadModule(pathB, auDll, genericDll, request, fail))
|
|
|
|
{
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &dir : arrayPaths)
|
|
|
|
{
|
|
|
|
if (auto pRet = TryLoadModule(dir, auDll, genericDll, request, fail))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return pRet;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path == EModulePath::eSpecified && request.specifiedSearchPaths)
|
|
|
|
{
|
|
|
|
for (const auto &val : *request.specifiedSearchPaths)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
if (auto pRet = TryLoadModule(val, auDll, genericDll, request, fail))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return pRet;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fail)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-11-15 13:07:38 +00:00
|
|
|
AUKN_SYM bool LoadModule(const ModuleLoadRequest &request)
|
2023-10-10 23:21:32 +00:00
|
|
|
{
|
|
|
|
return bool(LoadModuleEx(request));
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *LoadModuleEx(const ModuleLoadRequest &request)
|
2021-11-15 13:07:38 +00:00
|
|
|
{
|
2021-11-16 01:10:00 +00:00
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
|
|
|
|
auto h = gModuleHandles.find(request.mod);
|
|
|
|
if (h != gModuleHandles.end())
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return h->second;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto au = request.mod + ConstructAuDllSuffix();
|
|
|
|
auto base = request.mod;
|
|
|
|
auto ext = GetPlatformExt(Build::kCurrentPlatform);
|
|
|
|
|
|
|
|
if (ext && !Build::kIsNtDerived)
|
|
|
|
{
|
|
|
|
base += ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (request.version.size())
|
|
|
|
{
|
|
|
|
au += "." + request.version;
|
|
|
|
base += "." + request.version;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ext && Build::kIsNtDerived)
|
|
|
|
{
|
|
|
|
base += ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto searchPath = request.searchPath ? request.searchPath : &kUserOverloadableSearchPath;
|
|
|
|
for (EModulePath path : *searchPath)
|
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
if (auto pRet = TryLoadModule(path, au, base, request))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2023-10-10 23:21:32 +00:00
|
|
|
return pRet;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
return {};
|
2021-11-15 13:07:38 +00:00
|
|
|
}
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
AUKN_SYM AuMach GetProcAddressEx(void *pHandle, const AuString &symbol)
|
2021-11-15 13:07:38 +00:00
|
|
|
{
|
2024-04-20 04:41:56 +00:00
|
|
|
AuMach ret {};
|
|
|
|
|
2023-09-18 05:09:29 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-09-17 21:06:48 +00:00
|
|
|
if (!pGetProcAddress)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2024-04-20 04:41:56 +00:00
|
|
|
if (pHandle)
|
|
|
|
{
|
|
|
|
ret = reinterpret_cast<AuMach>(pGetProcAddress(reinterpret_cast<HMODULE>(pHandle), symbol.c_str()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
ret = reinterpret_cast<AuMach>(pGetProcAddress(::GetModuleHandleW(nullptr), symbol.c_str()));
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
HMODULE hHandle;
|
|
|
|
if (GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
|
|
|
reinterpret_cast<LPCWSTR>(&GetProcAddressEx), &hHandle))
|
|
|
|
{
|
|
|
|
ret = reinterpret_cast<AuMach>(pGetProcAddress(hHandle, symbol.c_str()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
|
|
|
|
for (const auto &[string, hHandle] : gModuleHandles)
|
|
|
|
{
|
|
|
|
ret = reinterpret_cast<AuMach>(pGetProcAddress(reinterpret_cast<HMODULE>(hHandle), symbol.c_str()));
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
#else
|
2024-04-20 04:41:56 +00:00
|
|
|
if (pHandle)
|
|
|
|
{
|
|
|
|
ret = reinterpret_cast<AuMach>(dlsym(pHandle, symbol.c_str()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(RTLD_DEFAULT)
|
|
|
|
ret = reinterpret_cast<AuMach>(dlsym(RTLD_DEFAULT, symbol.c_str()));
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
|
|
|
|
for (const auto &[string, hHandle] : gModuleHandles)
|
|
|
|
{
|
|
|
|
ret = reinterpret_cast<AuMach>(dlsym(hHandle, symbol.c_str()));
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
#endif
|
2024-04-20 04:41:56 +00:00
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM void *GetProcHandle(const AuString &name)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
|
|
|
|
auto h = gModuleHandles.find(name);
|
|
|
|
if (h == gModuleHandles.end())
|
|
|
|
{
|
|
|
|
SysPushErrorGen("Module {} is not loaded", name);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return h->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuMach GetProcAddress(const AuString &mod, const AuString &symbol)
|
|
|
|
{
|
2024-04-20 04:41:56 +00:00
|
|
|
if (mod.empty())
|
|
|
|
{
|
|
|
|
return GetProcAddressEx(nullptr, symbol);
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
|
2024-04-20 04:41:56 +00:00
|
|
|
if (auto pHandle = GetProcHandle(mod))
|
2021-11-16 01:10:00 +00:00
|
|
|
{
|
2024-04-20 04:41:56 +00:00
|
|
|
auto ret = GetProcAddressEx(pHandle, symbol);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
SysPushErrorGen("Couldn't resolve symbol in module {} of mangled name '{}'", mod, symbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2021-11-16 01:10:00 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 04:41:56 +00:00
|
|
|
return {};
|
2021-11-15 13:07:38 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
AUKN_SYM void Exit(AuUInt32 exitcode)
|
|
|
|
{
|
2022-04-02 00:48:29 +00:00
|
|
|
Aurora::RuntimeShutdown();
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-07-11 20:00:04 +00:00
|
|
|
TerminateProcess(GetCurrentProcess(), exitcode);
|
|
|
|
Win32Terminate();
|
2021-09-06 10:58:08 +00:00
|
|
|
#elif defined(AURORA_IS_POSIX_DERIVED)
|
2022-08-14 13:41:19 +00:00
|
|
|
// TODO: if main thread, long jump back, and return exit code
|
|
|
|
::kill(getpgrp(), SIGKILL);
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
::sched_yield();
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
#else
|
|
|
|
// ???
|
2021-11-05 17:34:23 +00:00
|
|
|
*(AuUInt32 *)0 = exitcode;
|
|
|
|
*(AuUInt32 *)0xFFFF = exitcode;
|
|
|
|
*(AuUInt32 *)0xFFFFFFFF = exitcode;
|
2021-09-06 10:58:08 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-11-16 01:10:00 +00:00
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
static void PreloadDLLsDoOnce(const AuList<AuString> &dirs)
|
|
|
|
{
|
|
|
|
AU_DEBUG_MEMCRUNCH;
|
|
|
|
|
|
|
|
AuList<void *> cookieList;
|
|
|
|
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
for (const auto dir : dirs)
|
|
|
|
{
|
|
|
|
void *pCookie {};
|
|
|
|
|
|
|
|
if (pRemoveDllDirectory)
|
|
|
|
{
|
|
|
|
if (pAddDllDirectory)
|
|
|
|
{
|
|
|
|
pCookie = pAddDllDirectory(AuLocale::ConvertFromUTF8(dir).c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cookieList.push_back(pCookie);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (const auto dir : dirs)
|
|
|
|
{
|
|
|
|
AuList<AuString> files;
|
|
|
|
AuList<AuString> sharedObjects;
|
|
|
|
|
|
|
|
if (!AuFS::FilesInDirectory(dir, files))
|
|
|
|
{
|
|
|
|
SysPushErrorIO("Couldn't readdir: {}", dir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto endingPattern = ConstructAuDllSuffix();
|
|
|
|
auto endingAlt = GetPlatformExt(Build::kCurrentPlatform);
|
|
|
|
|
|
|
|
for (const auto &file : files)
|
|
|
|
{
|
|
|
|
if (!(file.ends_with(endingPattern.c_str()) ||
|
|
|
|
file.ends_with(endingAlt)))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedObjects.push_back(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
AuUInt uSuccess {};
|
|
|
|
do
|
|
|
|
{
|
|
|
|
uSuccess = 0;
|
|
|
|
|
|
|
|
for (auto itr = sharedObjects.begin();
|
|
|
|
itr != sharedObjects.end(); )
|
|
|
|
{
|
|
|
|
auto &sharedDLL = *itr;
|
|
|
|
|
2023-10-13 19:27:21 +00:00
|
|
|
AuString a = dir + AuString({ AuFS::kPathSplitter }) + sharedDLL;
|
2023-10-10 23:21:32 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2023-10-13 19:27:21 +00:00
|
|
|
AuString aAbs = dir + AuString({ AuFS::kPathSplitter }) + sharedDLL + ".";
|
2023-10-10 23:21:32 +00:00
|
|
|
#endif
|
|
|
|
|
2023-10-12 17:58:11 +00:00
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
if (GetProcHandle(aAbs))
|
|
|
|
#else
|
|
|
|
if (GetProcHandle(a))
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
itr++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto modName = GetModuleNameFromFileName(sharedDLL);
|
|
|
|
AuLogDbg("[attempt] Loading shared object: {} (path: {})", modName, sharedDLL);
|
|
|
|
|
2023-10-10 23:21:32 +00:00
|
|
|
bool fail {};
|
2023-10-12 17:58:11 +00:00
|
|
|
ModuleLoadRequest request { AuList<AuString>{ dir }, modName };
|
2023-10-10 23:21:32 +00:00
|
|
|
|
|
|
|
if (TryLoadModule(a,
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
|
|
|
aAbs,
|
|
|
|
#endif
|
|
|
|
request,
|
|
|
|
fail
|
|
|
|
))
|
|
|
|
{
|
|
|
|
if (!fail)
|
|
|
|
{
|
2023-10-12 17:58:11 +00:00
|
|
|
AuLogDbg("[attempt] Loaded shared object: {} (path: {})", modName, sharedDLL);
|
2023-10-10 23:21:32 +00:00
|
|
|
uSuccess++;
|
|
|
|
itr = sharedObjects.erase(itr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
itr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (uSuccess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(AURORA_PLATFORM_WIN32)
|
|
|
|
for (const auto pCookie : AuExchange(cookieList, {}))
|
|
|
|
{
|
|
|
|
if (pRemoveDllDirectory)
|
|
|
|
{
|
|
|
|
if (pCookie)
|
|
|
|
{
|
|
|
|
pRemoveDllDirectory(pCookie);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PreloadDLLs(const AuString &dir)
|
|
|
|
{
|
|
|
|
AU_DEBUG_MEMCRUNCH;
|
|
|
|
if (gRuntimeConfig.processConfig.bAlwaysPreloadEntireClassPath)
|
|
|
|
{
|
|
|
|
PreloadDLLsDoOnce(gClassPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PreloadDLLsDoOnce({ dir });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool SetBinaryClassPath(const AuList<AuString> &list, bool preloadAll)
|
|
|
|
{
|
|
|
|
AU_DEBUG_MEMCRUNCH;
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
AuExchange(gClassPath, list);
|
|
|
|
|
|
|
|
if (((preloadAll && gRuntimeConfig.processConfig.bEnablePreload)) ||
|
|
|
|
(gRuntimeConfig.processConfig.bForcePreload))
|
|
|
|
{
|
|
|
|
if (gRuntimeConfig.processConfig.bAlwaysPreloadEntireClassPath)
|
|
|
|
{
|
|
|
|
PreloadDLLsDoOnce(gClassPath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PreloadDLLsDoOnce(list);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM bool AddBinaryClassPath(const AuString &dir, bool preloadAll)
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
|
|
|
|
if (!AuTryInsert(gClassPath, dir))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (((preloadAll && gRuntimeConfig.processConfig.bEnablePreload)) ||
|
|
|
|
(gRuntimeConfig.processConfig.bForcePreload))
|
|
|
|
{
|
|
|
|
PreloadDLLs(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
AUKN_SYM AuList<AuString> GetBinaryClassPath()
|
|
|
|
{
|
|
|
|
AU_LOCK_GUARD(gSpinLock);
|
|
|
|
return gClassPath;
|
|
|
|
}
|
2022-01-24 18:37:06 +00:00
|
|
|
|
|
|
|
void InitProcess()
|
|
|
|
{
|
|
|
|
// TODO (reece): test if self is signed -> xref kIsMainSigned
|
|
|
|
|
|
|
|
InitProcessMap();
|
2022-08-10 09:50:49 +00:00
|
|
|
|
|
|
|
#if defined(AURORA_IS_MODERNNT_DERIVED)
|
2022-09-30 22:47:18 +00:00
|
|
|
LoadProcessSectionViewSymbol();
|
2022-08-10 09:50:49 +00:00
|
|
|
#endif
|
2022-01-24 18:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeinitProcess()
|
|
|
|
{
|
|
|
|
DeinitProcessMap();
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|