diff --git a/CMakeLists.txt b/CMakeLists.txt index 765473e..c7029be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.13) set(CMAKE_C_FLAGS_INIT "-m32") -set(CMAKE_CXX_FLAGS_INIT "-m32") +set(CMAKE_CXX_FLAGS_INIT "-m32 -fno-exceptions -fno-rtti") set(CMAKE_EXE_LINKER_FLAGS_INIT "-m32") set(CMAKE_SHARED_LINKER_FLAGS_INIT "-m32") diff --git a/dll/kernel32/internal.h b/dll/kernel32/internal.h index 66e120a..a691fd8 100644 --- a/dll/kernel32/internal.h +++ b/dll/kernel32/internal.h @@ -25,8 +25,6 @@ struct FsObject : ObjectBase { }; struct FileObject : FsObject { - static constexpr ObjectType kType = ObjectType::File; - off_t filePos = 0; bool overlapped = false; bool appendOnly = false; @@ -34,7 +32,9 @@ struct FileObject : FsObject { // Used to notify overlapped operations without an event handle std::condition_variable overlappedCv; - explicit FileObject(int fd) : FsObject(kType, fd) { + explicit FileObject(int fd) : FileObject(ObjectType::File, fd) {} + FileObject(ObjectType type, int fd) : FsObject(type, fd) { + flags |= Of_File; if (fd >= 0) { off_t pos = lseek(fd, 0, SEEK_CUR); if (pos == -1 && errno == ESPIPE) { @@ -177,3 +177,11 @@ void setLastErrorFromErrno(); [[noreturn]] void exitInternal(DWORD exitCode); } // namespace kernel32 + +namespace detail { + +template <> constexpr bool typeMatches(const ObjectBase *o) noexcept { + return o && (o->flags & Of_File); +} + +} // namespace detail diff --git a/dll/kernel32/namedpipeapi.cpp b/dll/kernel32/namedpipeapi.cpp index fdbe659..9d2e72e 100644 --- a/dll/kernel32/namedpipeapi.cpp +++ b/dll/kernel32/namedpipeapi.cpp @@ -103,7 +103,7 @@ DWORD normalizeMaxInstances(DWORD value) { } struct NamedPipeState : ObjectBase { - static constexpr ObjectType kType = ObjectType::NamedPipe; + static constexpr ObjectType kType = ObjectType::NamedPipeState; std::mutex mutex; std::string key; @@ -171,6 +171,8 @@ struct NamedPipeState : ObjectBase { }; struct NamedPipeInstance final : FileObject { + static constexpr ObjectType kType = ObjectType::NamedPipe; + Pin state; int companionFd = -1; DWORD accessMode; @@ -182,7 +184,7 @@ struct NamedPipeInstance final : FileObject { std::condition_variable connectCv; NamedPipeInstance(int fd, Pin st, int companion, DWORD open, DWORD mode) - : FileObject(fd), state(std::move(st)), companionFd(companion), accessMode(open), pipeMode(mode) { + : FileObject(kType, fd), state(std::move(st)), companionFd(companion), accessMode(open), pipeMode(mode) { if (state) { state->registerInstance(this); } @@ -494,7 +496,7 @@ HANDLE WIN_FUNC CreateNamedPipeA(LPCSTR lpName, DWORD dwOpenMode, DWORD dwPipeMo configureInheritability(companionFd, inheritHandles); } - auto pipeObj = make_pin(serverFd, state.clone(), companionFd, accessMode, dwPipeMode); + auto pipeObj = make_pin(serverFd, std::move(state), companionFd, accessMode, dwPipeMode); if (!pipeObj) { return fail(ERROR_NOT_ENOUGH_MEMORY); } @@ -528,8 +530,7 @@ BOOL WIN_FUNC ConnectNamedPipe(HANDLE hNamedPipe, LPOVERLAPPED lpOverlapped) { HOST_CONTEXT_GUARD(); DEBUG_LOG("ConnectNamedPipe(%p, %p)\n", hNamedPipe, lpOverlapped); - auto pin = wibo::handles().get(hNamedPipe); - auto *pipe = pin ? dynamic_cast(pin.get()) : nullptr; + auto pipe = wibo::handles().getAs(hNamedPipe); if (!pipe) { wibo::lastError = ERROR_INVALID_HANDLE; return FALSE; diff --git a/src/files.cpp b/src/files.cpp index f22843e..e6a9333 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -107,16 +107,16 @@ std::filesystem::path pathFromWindows(const char *inStr) { if (followingExisting && !std::filesystem::exists(newPath2) && (component != ".." && component != "." && component != "")) { followingExisting = false; - try { - for (std::filesystem::path entry : std::filesystem::directory_iterator{newPath}) { + std::error_code ec; + std::filesystem::directory_iterator iter{newPath, ec}; + if (!ec) { + for (std::filesystem::path entry : iter) { if (strcasecmp(entry.filename().c_str(), component.c_str()) == 0) { followingExisting = true; newPath2 = entry; break; } } - } catch (const std::filesystem::filesystem_error &) { - // not a directory } } newPath = newPath2; diff --git a/src/handles.h b/src/handles.h index 42eadd8..fb28afd 100644 --- a/src/handles.h +++ b/src/handles.h @@ -26,11 +26,13 @@ enum class ObjectType : uint16_t { Heap, RegistryKey, NamedPipe, + NamedPipeState, }; enum ObjectFlags : uint16_t { Of_None = 0x0, Of_Waitable = 0x1, + Of_File = 0x2, }; struct ObjectBase { diff --git a/src/main.cpp b/src/main.cpp index 8f6d07a..537255b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,8 +4,8 @@ #include "modules.h" #include "processes.h" #include "strutil.h" -#include "version_info.h" #include "tls.h" +#include "version_info.h" #include #include