Skip to content

runtime: adjust the program name reference #79553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Runtimes/Core/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ target_include_directories(swiftRuntime PRIVATE

target_link_libraries(swiftRuntime PRIVATE
$<$<PLATFORM_ID:Windows>:User32>
$<$<PLATFORM_ID:Windows>:ShLwApi>
swiftShims
swiftDemangling)

Expand Down
39 changes: 39 additions & 0 deletions stdlib/public/runtime/LibPrespecialized.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@
#define HAS_OS_FEATURE 1
#endif

#if defined(_WIN32)
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <ShlWapi.h>
#include <Windows.h>
#endif

using namespace swift;

static bool prespecializedLoggingEnabled = false;
Expand Down Expand Up @@ -67,10 +74,42 @@ static bool environmentProcessListContainsProcess(const char *list,
}

static bool isThisProcessEnabled(const LibPrespecializedData<InProcess> *data) {
#if defined(_WIN32)
DWORD dwSize = MAX_PATH;
DWORD dwResult;
std::unique_ptr<WCHAR[]> pwszBuffer(new WCHAR[dwSize]);
while (true) {
dwResult = GetModuleFileNameW(nullptr, pwszBuffer.get(), dwSize);
if (dwResult == 0)
return true;
if (dwResult < dwSize)
break;
if (dwResult == dwSize && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
pwszBuffer.reset(new WCHAR[dwSize <<= 1]);
}

PCWSTR pwszBaseName = PathFindFileNameW(pwszBuffer.get());

DWORD cchLength =
WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS | WC_NO_BEST_FIT_CHARS,
pwszBaseName, -1, nullptr, 0, nullptr, nullptr);
if (cchLength == 0)
return true;

std::unique_ptr<char[]> pszBaseName{new char[cchLength]};
cchLength = WideCharToMultiByte(
CP_UTF8, WC_ERR_INVALID_CHARS | WC_NO_BEST_FIT_CHARS, pwszBaseName, -1,
pszBaseName.get(), cchLength, nullptr, nullptr);
if (cchLength == 0)
return true;

const char *__progname = pszBaseName.get();
#else
extern const char *__progname;

if (!__progname)
return true;
#endif

auto envEnabledProcesses =
runtime::environment::SWIFT_DEBUG_LIB_PRESPECIALIZED_ENABLED_PROCESSES();
Expand Down