/* $info$ tags: Bin|IRLoader desc: Used to run IR Tests $end_info$ */ #include "Common/ArgumentLoader.h" #include "IRLoader/Loader.h" #include "Tests/LinuxSyscalls/SignalDelegator.h" #include #include #include #include #include #include #include #include #include #include #include #include #include namespace FEXCore::Context { struct Context; } void MsgHandler(LogMan::DebugLevels Level, char const *Message) { const char *CharLevel{nullptr}; switch (Level) { case LogMan::NONE: CharLevel = "NONE"; break; case LogMan::ASSERT: CharLevel = "ASSERT"; break; case LogMan::ERROR: CharLevel = "ERROR"; break; case LogMan::DEBUG: CharLevel = "DEBUG"; break; case LogMan::INFO: CharLevel = "Info"; break; case LogMan::STDOUT: CharLevel = "STDOUT"; break; case LogMan::STDERR: CharLevel = "STDERR"; break; default: CharLevel = "???"; break; } fmt::print("[{}] {}\n", CharLevel, Message); fflush(stdout); } void AssertHandler(char const *Message) { fmt::print("[ASSERT] {}\n", Message); fflush(stdout); } class IRCodeLoader final : public FEXCore::CodeLoader { public: IRCodeLoader(FEX::IRLoader::Loader *Loader) : IR{Loader} { } uint64_t StackSize() const override { return STACK_SIZE; } uint64_t GetStackPointer() override { return reinterpret_cast(FEXCore::Allocator::mmap(nullptr, STACK_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)); } uint64_t DefaultRIP() const override { return IR->GetEntryRIP(); } bool MapMemory(const MapperFn& Mapper, const UnmapperFn& Unmapper) override { // Map the memory regions the test file asks for IR->MapRegions(); LoadMemory(); return true; } void LoadMemory() { IR->LoadMemory(); } virtual void AddIR(IRHandler Handler) override { Handler(IR->GetEntryRIP(), IR->GetIREmitter()); } private: FEX::IRLoader::Loader *IR; constexpr static uint64_t STACK_SIZE = 8 * 1024 * 1024; }; int main(int argc, char **argv, char **const envp) { LogMan::Throw::InstallHandler(AssertHandler); LogMan::Msg::InstallHandler(MsgHandler); FEXCore::Config::Initialize(); FEXCore::Config::AddLayer(std::make_unique(argc, argv)); FEXCore::Config::AddLayer(FEXCore::Config::CreateEnvironmentLayer(envp)); FEXCore::Config::Load(); auto Args = FEX::ArgLoader::Get(); auto ParsedArgs = FEX::ArgLoader::GetParsedArgs(); LOGMAN_THROW_A_FMT(Args.size() > 1, "Not enough arguments"); FEXCore::Context::InitializeStaticTables(); auto CTX = FEXCore::Context::CreateNewContext(); FEXCore::Context::InitializeContext(CTX); std::unique_ptr SignalDelegation = std::make_unique(); FEXCore::Context::SetSignalDelegator(CTX, SignalDelegation.get()); FEX::IRLoader::Loader Loader(Args[0], Args[1]); int Return{}; if (Loader.IsValid()) { IRCodeLoader CodeLoader{&Loader}; CodeLoader.MapMemory(nullptr, nullptr); FEXCore::Context::InitCore(CTX, &CodeLoader); auto ShutdownReason = FEXCore::Context::ExitReason::EXIT_SHUTDOWN; // There might already be an exit handler, leave it installed if (!FEXCore::Context::GetExitHandler(CTX)) { FEXCore::Context::SetExitHandler(CTX, [&](uint64_t thread, FEXCore::Context::ExitReason reason) { if (reason != FEXCore::Context::ExitReason::EXIT_DEBUG) { ShutdownReason = reason; FEXCore::Context::Stop(CTX); } }); } FEXCore::Context::RunUntilExit(CTX); LogMan::Msg::DFmt("Reason we left VM: {}", ShutdownReason); // Just re-use compare state. It also checks against the expected values in config. FEXCore::Core::CPUState State; FEXCore::Context::GetCPUState(CTX, &State); const bool Passed = Loader.CompareStates(&State); LogMan::Msg::IFmt("Passed? {}\n", Passed ? "Yes" : "No"); Return = Passed ? 0 : -1; } else { LogMan::Msg::EFmt("Couldn't load IR"); Return = -1; } FEXCore::Context::DestroyContext(CTX); return Return; }