AuroraRuntime/Source/AuRTEntrypoint.cpp
J Reece Wilson 7a0593adeb [+] AuCrypto::CA::ICertificateStore
[+] AuCrypto::CA::INewCertificateStore
[+] AuCrypto::CA::IPinCertificate
[+] AuCrypto::CA::PinAlwaysFail
[+] AuCrypto::CA::PinAlwaysPass
[+] AuCrypto::CA::PinCheckOS
[+] AuCrypto::CA::PinCheckDefault
[+] AuCrypto::CA::PinCheckBuiltin
[+] AuCrypto::CA::PinCheckGlobal
[+] AuCrypto::CA::PinCheckTwoAnd
[+] AuCrypto::CA::PinCheckTwoOr
[+] AuCrypto::CA::SetGlobalTLSPinner
[*] Minor AuCrypto::X509 decoder work
[*] AuCrypto::X509: transition to memory views (x509 is bytebuffer era and earlier code, beri early)
[+] AuCrypto::IPrivateKeyProvider
[+] AuCrypto::IPrivateKeyPair
[+] AuCrypto::PrivateKeyPair
[+] AuCrypto::ImportPrivateKeyPair
[*] Refactor: AuCrypto::X509::GenerateCertificate(...)
[+] AuCrypto::X509::NewChainFromOneDer
[+] AuCrypto::X509::NewChainFromManyDer
[+] AuCrypto::X509::NewChainFromManyDerInStream
[+] AuCrypto::X509::NewChainFromOnePem
[+] AuCrypto::X509::NewChainFromManyPem
[+] AuCrypto::X509::NewChainFromManyPemInStream
[*] Fix TLS code that was abandoned since its introduction with the net code. mbedtls is a hairbrained mess. so many *blocking* github issues starting after 2017. so little progress.
[+] AuIO::TLS::TLSMeta::pKeyPairProvider
[+] AuIO::TLS::TLSServer::bAllowSNIToFallBackDefault
[+] AuIO::TLS::TLSServer::bAllowSNILessUseDefaultCert
2024-10-16 02:07:24 +01:00

297 lines
7.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: Entrypoint.cpp
Date: 2021-5-13
Author: Reece
***/
#define _AURORA_RUNTIME_BUILD_API_INTERFACES
#include <AuroraCommon.hpp>
#include <AuroraRuntime.hpp>
#include "RuntimeInternal.hpp"
#include "AuCrypto.hpp"
#include "Processes/AuProcesses.hpp"
#include "RNG/AuRNG.hpp"
#include "Locale/Locale.hpp"
#include "Console/Console.hpp"
#include "IO/FS/FS.hpp"
#include "IO/FS/Resources.hpp"
#include "IO/IO.hpp"
#include "IO/Net/Net.hpp"
#include "Hashing/AuHashing.hpp"
#include "Debug/Debug.hpp"
#include "Async/Async.hpp"
#include "HWInfo/AuHWInfo.hpp"
#include "Telemetry/Telemetry.hpp"
#include "Threading/Threads/AuOSThread.hpp"
#include "SWInfo/AuSWInfo.hpp"
#if defined(AURORA_PLATFORM_WIN32)
#include "Extensions/Win32/DarkTheme.hpp"
#include <timeapi.h>
#endif
#if defined(AURORA_IS_MODERNNT_DERIVED)
#include <Source/IO/NT/HANDLEPipeServer.hpp>
#endif
#include "Debug/MemoryCrunch.hpp"
#include "Process/Process.hpp"
#include "Exit/AuExit.hpp"
#include "CmdLine/AuCmdLine.hpp"
#include "Grug/AuGrug.hpp"
#include "Threading/AuSleep.hpp"
#include "Memory/AuMemoryCache.hpp"
#include "Threading/Primitives/SMTYield.hpp"
#include <mimalloc.h>
#include "Process/AuProcessStartTime.hpp"
#include "AuProcAddresses.hpp"
#include "Crypto/CA/CA.hpp"
#if defined(AURORA_IS_LINUX_DERIVED)
#include "Crypto/CA/AuPinOS.Linux.hpp"
void LinuxSuperSecretIOTick();
#endif
namespace Aurora::IO::TLS
{
void TLSInit();
}
static thread_local bool tlsHackIsMainThread {};
static void Pump();
static void Init()
{
Aurora::Threading::Primitives::InitCfg();
Aurora::InitProcAddresses();
gRuntimeRunLevel = 1;
tlsHackIsMainThread = true;
#if defined(AURORA_PLATFORM_WIN32)
Aurora::Extensions::Win32::InitDarkMode();
#endif
Aurora::Memory::Cache::InitCache();
Aurora::RNG::Init();
Crypto::InitCrypto();
Aurora::IO::TLS::TLSInit();
Aurora::Crypto::CA::InitDefaultBuiltinCA();
#if defined(AURORA_IS_LINUX_DERIVED)
Aurora::Crypto::CA::InitLinuxPinner();
#endif
AuProcess::WarmStartupCaches();
Aurora::Threading::InitSleep();
Aurora::CmdLine::Init();
Aurora::Process::InitProcess();
Aurora::SWInfo::InitSwInfo();
#if defined(AURORA_PLATFORM_WIN32)
Aurora::Win32DropInit();
Aurora::Win32DropSchedulerResolution();
#endif
Aurora::Threading::Threads::InitThreading();
Aurora::Exit::InitExit();
Aurora::IO::Net::InitNetworking();
Aurora::Console::Init();
Aurora::IO::FS::InitResources();
Aurora::IO::Init();
Aurora::Grug::InitGrug();
Aurora::Console::Init2();
Aurora::HWInfo::Init();
Aurora::Telemetry::Init();
Aurora::Debug::InitDebug();
Aurora::Locale::Init();
Aurora::Processes::Init();
Aurora::Hashing::InitHashing();
Aurora::Async::InitAsync();
Aurora::Threading::Primitives::InitAdaptiveThresholdFirstTime();
gRuntimeRunLevel = 2;
#if defined(AURORA_PLATFORM_WIN32)
if (!gRuntimeConfig.console.asyncVSLog)
{
if (IsDebuggerPresent())
{
AuLogWarn("A debugger is attached and RuntimeStartInfo::console.asyncVSLog is assigned false - log-lines will be flushed to Visual Studios output window immediately. Expect a slowdown. Do not benchmark.");
}
else
{
gRuntimeConfig.console.asyncVSLog = true;
}
}
#endif
Pump();
}
static void Pump()
{
Aurora::Console::Pump();
#if defined(AURORA_IS_LINUX_DERIVED)
::LinuxSuperSecretIOTick();
#endif
#if defined(AURORA_PLATFORM_WIN32)
Aurora::Win32DropSchedulerResolution();
#endif
}
static void RuntimeLateClean();
static void Deinit()
{
//tlsHackIsMainThread = true;
gRuntimeRunLevel = 3;
Aurora::Exit::PostLevel(AuThreads::GetThread(), Aurora::Exit::ETriggerLevel::eSafeTermination);
gRuntimeRunLevel = 4;
Aurora::Async::ShutdownAsync();
RuntimeLateClean();
}
static void RuntimeLateClean()
{
// TODO: elevate leval, freeze all threads
Aurora::Console::Exit(); // Final flush (i hope? coping?)
Aurora::Grug::DeinitGrug(); // TODO: this was once broken above "deinit" for some unix crash reason.
// now that the runtime is far more stable, im putting this down here to ensure
// console exit doesnt slam into a deadlocked grug thread.
Aurora::Processes::Deinit();
#if defined(AURORA_IS_MODERNNT_DERIVED)
Aurora::IO::NT::ShutdownNTIPCHandleServer();
#endif
// Static variables we should probably manually release ->
// (there is no real logic under the following functions)
Aurora::IO::Deinit();
Aurora::Exit::DeinitExit();
Aurora::IO::FS::DeinitResources();
Aurora::CmdLine::Deinit();
Aurora::Crypto::CA::DeinitDefaultBuiltinCA();
#if defined(AURORA_IS_LINUX_DERIVED)
Aurora::Crypto::CA::DeinitLinuxPinner();
#endif
Aurora::RNG::Release(); // RNG and crypto should remain alive whilst IO work is ongoing.
// At this point, we should assume libtomcrypt and mbedtls wont be called
// The default gFastDevice shouldn't be hit for any reason
// ...we should be clear to start freeing those contexts and related system handles
// Exterminate reserve debug memory which may very well be in use
// So long as we assume aurora runtime users are dead, bye bye underlying (low-memory condition) heap
AuDebug::gReservePoolStart = 0;
AuDebug::gReservePoolEnd = 0;
AuDebug::gReserveHeap.reset();
gRuntimeRunLevel = 5;
// TODO: forcefully terminate spawned threads
// TODO: debate terminate process for safety reasons i cant be bothered to explain (including AuDebug heap)
}
namespace Aurora
{
static bool gRuntimeHasStarted {};
bool RuntimeIsMainThread()
{
return tlsHackIsMainThread;
}
AUKN_SYM void RuntimeStart(const RuntimeStartInfo &info)
{
gRuntimeConfig = info;
if (gRuntimeConfig.threadingConfig.bResetToRuntimeDefaults)
{
// <= C++17 users will struggle to fill this bit field and I dont want a massive properties object library yet just to handle runtime init.
AuResetMember(gRuntimeConfig.threadingConfig);
}
if (gRuntimeHasStarted)
{
SysPanic("Do not nest RuntimeStart/RuntimeShutdowns. Modules should respect RuntimeHasStarted()");
}
try
{
Init();
}
catch (...)
{
SysPanic("A fatal error occurred during the initialization of Aurora Runtime");
}
gRuntimeHasStarted = true;
}
AUKN_SYM bool RuntimeHasStarted()
{
return gRuntimeHasStarted;
}
AUKN_SYM void RuntimeShutdown()
{
RuntimeSysPump();
gRuntimeHasStarted = false;
Deinit();
}
AUKN_SYM void RuntimeSysPump()
{
Pump();
}
AUKN_SYM void RuntimeWaitForSecondaryTick()
{
Grug::WaitForGrugTick();
}
AUKN_SYM void RuntimeCollectMemory()
{
mi_collect(true);
}
void RuntimeLateClean()
{
::RuntimeLateClean();
}
}
#if defined(AURORA_PLATFORM_WIN32)
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
//DisableThreadLibraryCalls(hinstDLL);
}
if (fdwReason == DLL_THREAD_DETACH)
{
mi_thread_done();
}
return TRUE;
}
#endif