From b4e7606226a6f0b2d9546631d959e637510701e8 Mon Sep 17 00:00:00 2001 From: momo5502 Date: Sun, 22 Dec 2024 16:33:50 +0100 Subject: [PATCH] Better NtClose --- src/windows-emulator/handles.hpp | 12 ++++-- src/windows-emulator/syscalls.cpp | 62 +++++++++++++++---------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/windows-emulator/handles.hpp b/src/windows-emulator/handles.hpp index 85c6f99a..50123adb 100644 --- a/src/windows-emulator/handles.hpp +++ b/src/windows-emulator/handles.hpp @@ -106,9 +106,15 @@ namespace handle_detail }; } +struct generic_handle_store +{ + virtual ~generic_handle_store() = default; + virtual bool erase(const handle h) = 0; +}; + template requires(utils::Serializable) -class handle_store +class handle_store : public generic_handle_store { public: using index_type = uint32_t; @@ -208,7 +214,7 @@ public: return this->erase(entry); } - bool erase(const handle h) + bool erase(const handle h) override { return this->erase(h.value); } @@ -352,4 +358,4 @@ constexpr auto CURRENT_THREAD = make_handle(~1ULL); constexpr auto CURRENT_PROCESS_TOKEN = make_handle(~3ULL); constexpr auto CURRENT_THREAD_TOKEN = make_handle(~4ULL); -constexpr auto CURRENT_THREAD_EFFECTIVE_TOKEN = make_handle(~5ULL); \ No newline at end of file +constexpr auto CURRENT_THREAD_EFFECTIVE_TOKEN = make_handle(~5ULL); diff --git a/src/windows-emulator/syscalls.cpp b/src/windows-emulator/syscalls.cpp index 17c6fd83..85aa563d 100644 --- a/src/windows-emulator/syscalls.cpp +++ b/src/windows-emulator/syscalls.cpp @@ -348,6 +348,33 @@ namespace return STATUS_SUCCESS; } + generic_handle_store* get_handle_store(process_context& proc, const handle h) + { + switch (h.value.type) + { + case handle_types::thread: + return &proc.threads; + case handle_types::event: + return &proc.events; + case handle_types::file: + return &proc.files; + case handle_types::device: + return &proc.devices; + case handle_types::semaphore: + return &proc.semaphores; + case handle_types::registry: + return &proc.registry_keys; + case handle_types::mutant: + return &proc.mutants; + case handle_types::port: + return &proc.ports; + case handle_types::section: + return &proc.sections; + default: + return nullptr; + } + } + NTSTATUS handle_NtClose(const syscall_context& c, const handle h) { const auto value = h.value; @@ -356,37 +383,8 @@ namespace return STATUS_SUCCESS; } - if (value.type == handle_types::thread && c.proc.threads.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::event && c.proc.events.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::file && c.proc.files.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::device && c.proc.devices.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::semaphore && c.proc.semaphores.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::registry && c.proc.registry_keys.erase(h)) - { - return STATUS_SUCCESS; - } - - if (value.type == handle_types::mutant && c.proc.mutants.erase(h)) + auto* handle_store = get_handle_store(c.proc, h); + if (handle_store && handle_store->erase(h)) { return STATUS_SUCCESS; } @@ -419,7 +417,7 @@ namespace if (previous_count) { - previous_count.write(old_count); + previous_count.write(static_cast(old_count)); } return STATUS_SUCCESS;