mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-08 09:09:43 +00:00
AK: Implement demangle() for MSVC ABI
This implements demangle() using Windows API. Also some rudimentary test is provided.
This commit is contained in:
parent
1861f24979
commit
e03c558a0a
Notes:
github-actions[bot]
2025-06-18 00:40:29 +00:00
Author: https://github.com/tomaszstrejczek 🔰
Commit: e03c558a0a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/5000
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/R-Goc
6 changed files with 107 additions and 6 deletions
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
||||
}
|
||||
|
|
32
AK/DemangleWindows.cpp
Normal file
32
AK/DemangleWindows.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Tomasz Strejczek
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <AK/StringView.h>
|
||||
|
||||
#include <Windows.h>
|
||||
#include <dbghelp.h>
|
||||
|
||||
#pragma comment(lib, "dbghelp.lib") // /DELAYLOAD:dbghelp.dll is configured in CMakeLists.txt
|
||||
|
||||
#include <AK/Demangle.h>
|
||||
|
||||
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<size_t>(chars_written) } : name);
|
||||
}
|
||||
|
||||
} // namespace AK
|
|
@ -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)
|
||||
|
|
43
Tests/AK/TestDelayLoadWindows.cpp
Normal file
43
Tests/AK/TestDelayLoadWindows.cpp
Normal file
|
@ -0,0 +1,43 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Tomasz Strejczek
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/Demangle.h>
|
||||
|
||||
#include <AK/Windows.h>
|
||||
#include <psapi.h>
|
||||
|
||||
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"));
|
||||
}
|
22
Tests/AK/TestDemangle.cpp
Normal file
22
Tests/AK/TestDemangle.cpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2025, Tomasz Strejczek
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibTest/TestCase.h>
|
||||
|
||||
#include <AK/Demangle.h>
|
||||
|
||||
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));
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue