diff --git a/External/FEXCore/Source/Interface/Config/Config.cpp b/External/FEXCore/Source/Interface/Config/Config.cpp index fc05a978d..62fe92777 100644 --- a/External/FEXCore/Source/Interface/Config/Config.cpp +++ b/External/FEXCore/Source/Interface/Config/Config.cpp @@ -1,11 +1,11 @@ #include "Common/StringConv.h" #include "Common/StringUtils.h" #include "Common/Paths.h" -#include "Utils/FileLoading.h" #include #include #include +#include #include #include #include diff --git a/External/FEXCore/Source/Interface/Core/CPUID.cpp b/External/FEXCore/Source/Interface/Core/CPUID.cpp index f8bd02fdf..087d7dca0 100644 --- a/External/FEXCore/Source/Interface/Core/CPUID.cpp +++ b/External/FEXCore/Source/Interface/Core/CPUID.cpp @@ -8,12 +8,12 @@ $end_info$ #include "Common/StringConv.h" #include "Interface/Context/Context.h" #include "Interface/Core/CPUID.h" -#include "Utils/FileLoading.h" #include #include #include #include +#include #include #include diff --git a/External/FEXCore/Source/Interface/Core/GdbServer.cpp b/External/FEXCore/Source/Interface/Core/GdbServer.cpp index 999b150eb..11ff2f80c 100644 --- a/External/FEXCore/Source/Interface/Core/GdbServer.cpp +++ b/External/FEXCore/Source/Interface/Core/GdbServer.cpp @@ -24,6 +24,7 @@ $end_info$ #include #include #include +#include #include #include #include @@ -137,16 +138,9 @@ static fextl::string encodeHex(const unsigned char *data, size_t length) { static fextl::string getThreadName(uint32_t ThreadID) { const auto ThreadFile = fextl::fmt::format("/proc/{}/task/{}/comm", getpid(), ThreadID); - std::fstream fs(ThreadFile.c_str(), std::fstream::in | std::fstream::binary); - - if (fs.is_open()) { - fextl::string ThreadName; - fs >> ThreadName; - fs.close(); - return ThreadName; - } - - return ""; + fextl::string ThreadName {""}; + FEXCore::FileLoading::LoadFile(ThreadName, ThreadFile); + return ThreadName; } // Packet parser @@ -544,7 +538,10 @@ void GdbServer::buildLibraryMap() { fextl::ostringstream xml; - std::fstream fs("/proc/self/maps", std::fstream::in | std::fstream::binary); + fextl::string MapsFile; + FEXCore::FileLoading::LoadFile(MapsFile, "/proc/self/maps"); + fextl::istringstream MapsStream(MapsFile); + fextl::string Line; struct FileData { @@ -555,7 +552,7 @@ void GdbServer::buildLibraryMap() { // 7ff5dd6d2000-7ff5dd6d3000 rw-p 0000a000 103:0b 1881447 /usr/lib/x86_64-linux-gnu/libnss_compat.so.2 fextl::string const &RuntimeExecutable = Filename(); - while (std::getline(fs, Line)) { + while (std::getline(MapsStream, Line)) { auto ss = fextl::istringstream(Line); fextl::string Tmp; fextl::string Begin; @@ -737,11 +734,14 @@ GdbServer::HandledPacketType GdbServer::handleXfer(const fextl::string &packet) static size_t CheckMemMapping(uint64_t Address, size_t Size) { uint64_t AddressEnd = Address + Size; - std::fstream fs("/proc/self/maps", std::fstream::in | std::fstream::binary); + fextl::string MapsFile; + FEXCore::FileLoading::LoadFile(MapsFile, "/proc/self/maps"); + fextl::istringstream MapsStream(MapsFile); + fextl::string Line; - while (std::getline(fs, Line)) { - if (fs.eof()) break; + while (std::getline(MapsStream, Line)) { + if (MapsStream.eof()) break; uint64_t Begin, End; char r,w,x,p; if (sscanf(Line.c_str(), "%lx-%lx %c%c%c%c", &Begin, &End, &r, &w, &x, &p) == 6) { @@ -1280,7 +1280,7 @@ void GdbServer::StartThread() { } void GdbServer::OpenListenSocket() { - // open socket + FEXCore::Allocator::YesIKnowImNotSupposedToUseTheGlibcAllocator glibc; struct addrinfo hints, *res; memset(&hints, 0, sizeof(hints)); @@ -1309,9 +1309,11 @@ void GdbServer::OpenListenSocket() { } listen(ListenSocket, 1); + + freeaddrinfo(res); } -std::unique_ptr GdbServer::OpenSocket() { +fextl::unique_ptr GdbServer::OpenSocket() { // Block until a connection arrives struct sockaddr_storage their_addr{}; socklen_t addr_size{}; @@ -1319,7 +1321,7 @@ std::unique_ptr GdbServer::OpenSocket() { LogMan::Msg::IFmt("GdbServer, waiting for connection on localhost:8086"); int new_fd = accept(ListenSocket, (struct sockaddr *)&their_addr, &addr_size); - return std::make_unique(new_fd); + return fextl::make_unique(new_fd); } } // namespace FEXCore diff --git a/External/FEXCore/Source/Interface/Core/GdbServer.h b/External/FEXCore/Source/Interface/Core/GdbServer.h index 5179b8bf5..d96697efa 100644 --- a/External/FEXCore/Source/Interface/Core/GdbServer.h +++ b/External/FEXCore/Source/Interface/Core/GdbServer.h @@ -8,6 +8,7 @@ $end_info$ #include #include #include +#include #include #include @@ -37,7 +38,7 @@ private: void Break(int signal); void OpenListenSocket(); - std::unique_ptr OpenSocket(); + fextl::unique_ptr OpenSocket(); void StartThread(); fextl::string ReadPacket(std::iostream &stream); void SendPacket(std::ostream &stream, const fextl::string& packet); @@ -76,8 +77,8 @@ private: HandledPacketType readReg(const fextl::string& packet); FEXCore::Context::ContextImpl *CTX; - std::unique_ptr gdbServerThread; - std::unique_ptr CommsStream; + fextl::unique_ptr gdbServerThread; + fextl::unique_ptr CommsStream; std::mutex sendMutex; bool SettingNoAckMode{false}; bool NoAckMode{false}; diff --git a/External/FEXCore/Source/Utils/FileLoading.cpp b/External/FEXCore/Source/Utils/FileLoading.cpp index df63ffe8c..31d6dabe5 100644 --- a/External/FEXCore/Source/Utils/FileLoading.cpp +++ b/External/FEXCore/Source/Utils/FileLoading.cpp @@ -9,7 +9,8 @@ namespace FEXCore::FileLoading { -bool LoadFile(fextl::vector &Data, const fextl::string &Filepath, size_t FixedSize) { +template +bool LoadFileImpl(T &Data, const fextl::string &Filepath, size_t FixedSize) { int FD = open(Filepath.c_str(), O_RDONLY); if (FD == -1) { @@ -39,6 +40,14 @@ bool LoadFile(fextl::vector &Data, const fextl::string &Filepath, size_t F return Read == FileSize; } +bool LoadFile(fextl::vector &Data, const fextl::string &Filepath, size_t FixedSize) { + return LoadFileImpl(Data, Filepath, FixedSize); +} + +bool LoadFile(fextl::string &Data, const fextl::string &Filepath, size_t FixedSize) { + return LoadFileImpl(Data, Filepath, FixedSize); +} + ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span Buffer) { int FD = open(Filepath.c_str(), O_RDONLY); diff --git a/External/FEXCore/Source/Utils/FileLoading.h b/External/FEXCore/include/FEXCore/Utils/FileLoading.h similarity index 64% rename from External/FEXCore/Source/Utils/FileLoading.h rename to External/FEXCore/include/FEXCore/Utils/FileLoading.h index 146ea036a..e80e1e7e2 100644 --- a/External/FEXCore/Source/Utils/FileLoading.h +++ b/External/FEXCore/include/FEXCore/Utils/FileLoading.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include @@ -15,7 +16,8 @@ namespace FEXCore::FileLoading { * * @return true on file loaded, false on failure */ - bool LoadFile(fextl::vector &Data, const fextl::string &Filepath, size_t FixedSize = 0); + FEX_DEFAULT_VISIBILITY bool LoadFile(fextl::vector &Data, const fextl::string &Filepath, size_t FixedSize = 0); + FEX_DEFAULT_VISIBILITY bool LoadFile(fextl::string &Data, const fextl::string &Filepath, size_t FixedSize = 0); /** * @brief Loads a filepath in to a buffer of data with a fixed size @@ -25,6 +27,6 @@ namespace FEXCore::FileLoading { * * @return The amount of data read or -1 on error. */ - ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span Buffer); + FEX_DEFAULT_VISIBILITY ssize_t LoadFileToBuffer(const fextl::string &Filepath, std::span Buffer); } diff --git a/Source/Common/Config.cpp b/Source/Common/Config.cpp index 96d84420f..23f83e15a 100644 --- a/Source/Common/Config.cpp +++ b/Source/Common/Config.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include #include @@ -37,10 +36,12 @@ namespace FEX::Config { Dest = json_objClose(Dest); json_end(Dest); - std::ofstream Output (fextl::string_from_string(Filename), std::ios::out | std::ios::binary); - if (Output.is_open()) { - Output.write(Buffer, strlen(Buffer)); - Output.close(); + constexpr int USER_PERMS = S_IRWXU | S_IRWXG | S_IRWXO; + int FD = open(Filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC, USER_PERMS); + + if (FD != -1) { + write(FD, Buffer, strlen(Buffer)); + close(FD); } }