From ee5371268cbfe04728107177c3bbb2ac8deaf2fa Mon Sep 17 00:00:00 2001 From: Ryan Macnak Date: Wed, 4 Mar 2026 16:07:20 -0800 Subject: [PATCH] [vm, windows] Get DSO offsets without needing SymInitialize. TEST=ci Bug: https://github.com/dart-lang/sdk/issues/60499 Change-Id: Ie9d45072e5a74522c22a99a18277ccbed53636b5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/485581 Reviewed-by: Alexander Aprelev Commit-Queue: Ryan Macnak --- runtime/vm/native_symbol_win.cc | 36 ++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/runtime/vm/native_symbol_win.cc b/runtime/vm/native_symbol_win.cc index 1f55ab1e4c7..7e0dfce0d08 100644 --- a/runtime/vm/native_symbol_win.cc +++ b/runtime/vm/native_symbol_win.cc @@ -92,22 +92,48 @@ void NativeSymbolResolver::FreeSymbolName(const char* name) { free(const_cast(name)); } +struct Context { + uword pc; + uword dso_base; + const char* dso_name; + bool success; +}; + +BOOL CALLBACK EnumerateModulesCallback(PCSTR ModuleName, + DWORD64 ModuleBase, + ULONG ModuleSize, + PVOID UserContext) { + Context* context = reinterpret_cast(UserContext); + if ((ModuleBase <= context->pc) && (context->pc < ModuleBase + ModuleSize)) { + context->dso_base = ModuleBase; + context->dso_name = Utils::StrDup(ModuleName); + context->success = true; + return FALSE; // stop enumeration + } + return TRUE; // continue enumeration +} + bool NativeSymbolResolver::LookupSharedObject(uword pc, uword* dso_base, const char** dso_name) { #ifdef DART_TARGET_OS_WINDOWS_UWP return false; #else - IMAGEHLP_MODULE64 info = {}; - info.SizeOfStruct = sizeof(info); - if (!SymGetModuleInfo64(GetCurrentProcess(), pc, &info)) { + Context context; + context.pc = pc; + context.success = false; + if (!EnumerateLoadedModulesEx(GetCurrentProcess(), EnumerateModulesCallback, + &context)) { + return false; + } + if (!context.success) { return false; } if (dso_base != nullptr) { - *dso_base = info.BaseOfImage; + *dso_base = context.dso_base; } if (dso_name != nullptr) { - *dso_name = Utils::StrDup(info.ImageName); + *dso_name = context.dso_name; } return true; #endif // ifdef DART_TARGET_OS_WINDOWS_UWP