diff --git a/AK/CMakeLists.txt b/AK/CMakeLists.txt index cd20f3402b5..44c77d56d15 100644 --- a/AK/CMakeLists.txt +++ b/AK/CMakeLists.txt @@ -36,7 +36,10 @@ set(SOURCES ) if (WIN32) - list(APPEND SOURCES LexicalPathWindows.cpp) + list(APPEND SOURCES + LexicalPathWindows.cpp + DemangleWindows.cpp + ) else() list(APPEND SOURCES LexicalPath.cpp) endif() @@ -73,6 +76,8 @@ if (WIN32) # FIXME: Windows on ARM target_link_libraries(AK PRIVATE clang_rt.builtins-x86_64.lib) target_link_libraries(AK PRIVATE Bcrypt.lib) + target_link_libraries(AK PRIVATE delayimp.lib) + target_link_options(AK PRIVATE /DELAYLOAD:dbghelp.dll) elseif (APPLE) set(ASSERTION_HANDLER_VISIBILITY PRIVATE) if (NOT BUILD_SHARED_LIBS) diff --git a/AK/Demangle.h b/AK/Demangle.h index 290d3c3753f..433f8603496 100644 --- a/AK/Demangle.h +++ b/AK/Demangle.h @@ -26,11 +26,7 @@ inline ByteString demangle(StringView name) return string; } #else -inline ByteString demangle(StringView name) -{ - // FIXME: Implement AK::demangle on Windows - return name; -} +ByteString demangle(StringView name); #endif } diff --git a/AK/DemangleWindows.cpp b/AK/DemangleWindows.cpp new file mode 100644 index 00000000000..a4ffe237a61 --- /dev/null +++ b/AK/DemangleWindows.cpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2025, Tomasz Strejczek + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include +#include + +#include +#include + +#pragma comment(lib, "dbghelp.lib") // /DELAYLOAD:dbghelp.dll is configured in CMakeLists.txt + +#include + +namespace AK { + +ByteString demangle(StringView name) +{ + // The buffer size is arbitrary but should be large enough for most cases. + // Unfortunately, there is no way to know the exact size needed beforehand. + // Also calling UnDecorateSymbolName with a too small buffer will not return an error, it will just truncate the result. + char buffer[4096] = {}; + + // UNDNAME_COMPLETE asks for the full decoration (equivalent to the Itanium demangle method in libgcc/libcxxabi) + auto chars_written = UnDecorateSymbolName(name.to_byte_string().characters(), buffer, sizeof(buffer), UNDNAME_COMPLETE); + + return ByteString(chars_written > 0 ? StringView { buffer, static_cast(chars_written) } : name); +} + +} // namespace AK diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txt index a36e4a20469..4d7ab411a38 100644 --- a/Tests/AK/CMakeLists.txt +++ b/Tests/AK/CMakeLists.txt @@ -17,6 +17,7 @@ set(AK_TEST_SOURCES TestChecked.cpp TestCircularBuffer.cpp TestCircularQueue.cpp + TestDemangle.cpp TestDisjointChunks.cpp TestDistinctNumeric.cpp TestDoublyLinkedList.cpp @@ -86,6 +87,8 @@ set(AK_TEST_SOURCES # it's own platform-specific tests to avoid if-def soup in the Unix-based tests. if(NOT WIN32) list(APPEND AK_TEST_SOURCES TestLexicalPath.cpp) +else() + list(APPEND AK_TEST_SOURCES TestDelayLoadWindows.cpp) endif() foreach(source IN LISTS AK_TEST_SOURCES) diff --git a/Tests/AK/TestDelayLoadWindows.cpp b/Tests/AK/TestDelayLoadWindows.cpp new file mode 100644 index 00000000000..e275bab7533 --- /dev/null +++ b/Tests/AK/TestDelayLoadWindows.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025, Tomasz Strejczek + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +#include +#include + +static bool is_dll_loaded(wchar_t const* dll_name) +{ + HMODULE mods[1024]; + DWORD needed; + HANDLE process = GetCurrentProcess(); + + if (EnumProcessModules(process, mods, sizeof(mods), &needed)) { + for (unsigned int i = 0; i < (needed / sizeof(HMODULE)); i++) { + wchar_t mod_name[MAX_PATH]; + if (GetModuleFileNameExW(process, mods[i], mod_name, + sizeof(mod_name) / sizeof(wchar_t))) { + wchar_t const* base = wcsrchr(mod_name, L'\\'); + if (base && _wcsicmp(base + 1, dll_name) == 0) { + return true; + } + } + } + } + return false; +} + +TEST_CASE(class_method) +{ + auto test_string = "?unicode_substring_view@Utf16View@AK@@QEBA?AV12@_K0@Z"sv; + auto expected_result = "public: class AK::Utf16View __cdecl AK::Utf16View::unicode_substring_view(unsigned __int64,unsigned __int64)const __ptr64"sv; + + EXPECT_EQ(false, is_dll_loaded(L"dbghelp.dll")); + EXPECT_EQ(expected_result, demangle(test_string)); + EXPECT_EQ(true, is_dll_loaded(L"dbghelp.dll")); +} diff --git a/Tests/AK/TestDemangle.cpp b/Tests/AK/TestDemangle.cpp new file mode 100644 index 00000000000..5d27aa9115a --- /dev/null +++ b/Tests/AK/TestDemangle.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2025, Tomasz Strejczek + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +#include + +TEST_CASE(class_method) +{ +#ifndef AK_OS_WINDOWS + auto test_string = "_ZNK2AK9Utf16View22unicode_substring_viewEmm"sv; + auto expected_result = "AK::Utf16View::unicode_substring_view(unsigned long, unsigned long) const"sv; +#else + auto test_string = "?unicode_substring_view@Utf16View@AK@@QEBA?AV12@_K0@Z"sv; + auto expected_result = "public: class AK::Utf16View __cdecl AK::Utf16View::unicode_substring_view(unsigned __int64,unsigned __int64)const __ptr64"sv; +#endif + + EXPECT_EQ(expected_result, demangle(test_string)); +}