Skip to content

Extend SystemProcess API Implementation to Windows - #7845

Merged
babsingh merged 1 commit into
eclipse-omr:masterfrom
sharanpatil123:systemprocess-windows
Jul 22, 2025
Merged

Extend SystemProcess API Implementation to Windows#7845
babsingh merged 1 commit into
eclipse-omr:masterfrom
sharanpatil123:systemprocess-windows

Conversation

@sharanpatil123

@sharanpatil123 sharanpatil123 commented Jul 15, 2025

Copy link
Copy Markdown
Contributor

Use EnumProcesses and QueryFullProcessImageName to extract PID and executable paths on Windows.

@sharanpatil123
sharanpatil123 requested a review from babsingh as a code owner July 15, 2025 11:06
@sharanpatil123

Copy link
Copy Markdown
Contributor Author

@keithc-ca - Please review

Comment thread port/win32/omrsysinfo.c Outdated
Comment on lines +2070 to +2093
/*
* Fallback: Use Toolhelp32Snapshot to retrieve process name for restricted/system processes.
*/
static const char *
getProcessNameFallback(DWORD pid, char *buffer, size_t bufferSize)
{
HANDLE hSnap = NULL;
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE) {
if (Process32First(hSnap, &pe)) {
do {
if (pe.th32ProcessID == pid) {
strncpy(buffer, pe.szExeFile, bufferSize - 1);
buffer[bufferSize - 1] = '\0';
break;
}
} while (Process32Next(hSnap, &pe));
}
CloseHandle(hSnap);
}
return buffer;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not clear to me that use of CreateToolhelp32Snapshot() is reasonable because a 32-bit process cannot retrieve information for a 64-bit processes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keithc-ca - There are some system/protected processes - such as csrss, System, wininit, Registry Memory Compression etc. Attempting to access them with OpenProcess fails due to access denial and these do not have an executable path. In hotspot these processes are also listed as part of this event, hence had to use the fallback function CreateToolhelp32Snapshot to get the process name. I’ve tested this and is able to list these processes.

If these processes are not needed, I can remove the fallback.

Comment thread port/win32/omrsysinfo.c Outdated
DWORD numProcesses = 0;
DWORD bufferSize = 1024;
DWORD i = 0;
HANDLE hProcess = NULL;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this declaration to line 2156 - it's only used within that scope.

Comment thread port/win32/omrsysinfo.c Outdated
}
processes = (DWORD *)portLibrary->mem_allocate_memory(
portLibrary,
bufferSize * sizeof(DWORD),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just use bufferSize throughout instead of bufferSize * sizeof(DWORD). If reasonable, you could change the initial value:

	DWORD bufferSize = 1024 * sizeof(DWORD);

Comment thread port/win32/omrsysinfo.c Outdated
return (uintptr_t)(intptr_t)OMRPORT_ERROR_OPFAILED;
}
processes = (DWORD *)portLibrary->mem_allocate_memory(
portLibrary,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excessive indentation (one tab too many).

Comment thread port/win32/omrsysinfo.c Outdated
Comment on lines +2141 to +2145
portLibrary,
processes,
bufferSize * sizeof(DWORD),
OMR_GET_CALLSITE(),
OMRMEM_CATEGORY_PORT_LIBRARY);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation.

Comment thread port/win32/omrsysinfo.c Outdated
}
numProcesses = bytesReturned / sizeof(DWORD);
for (i = 0; i < numProcesses; i++) {
char exePath[MAX_PATH] = {0};

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't initialize an array like this; instead after line 2156:

		exePath[0] = '\0';

Comment thread port/win32/omrsysinfo.c Outdated
DWORD pid = processes[i];
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_QUERY_INFORMATION, FALSE, pid);
if (NULL != hProcess) {
if (0 == QueryFullProcessImageNameA(hProcess, 0, exePath, &pathLen)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use QueryFullProcessImageName() instead of QueryFullProcessImageNameA().

Comment thread port/win32/omrsysinfo.c Outdated
getProcessNameFallback(pid, exePath, MAX_PATH);
}
/* Skip entries with no name. */
if (exePath[0] == '\0') {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant on left of ==:

		if ('\0' == exePath[0]) {

Comment thread port/win32/omrsysinfo.c Outdated
numProcesses = bytesReturned / sizeof(DWORD);
for (i = 0; i < numProcesses; i++) {
char exePath[MAX_PATH] = {0};
DWORD pathLen = MAX_PATH;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only use of MAX_PATH should be on line 2153; other places should use sizeof(exePath).

@sharanpatil123

Copy link
Copy Markdown
Contributor Author

@keithc-ca - Addressed review comments.

@sharanpatil123
sharanpatil123 requested a review from keithc-ca July 16, 2025 07:00
Comment thread port/win32/omrsysinfo.c Outdated
}

/*
* Fallback: Use Toolhelp32Snapshot to retrieve process name for restricted/system processes.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we want to use this function at all: the documentation for CreateToolhelp32Snapshot() says 32-bit processes cannot retrieve information for a 64-bit process. If we do need to keep it, the comment should refer to the actual system call used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I have removed the fallback.

Comment thread port/win32/omrsysinfo.c Outdated
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnap != INVALID_HANDLE_VALUE) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Constant on the left of !=, please:

	if (INVALID_HANDLE_VALUE != hSnap) {

Comment thread port/win32/omrsysinfo.c Outdated
{
HANDLE hSnap = NULL;
PROCESSENTRY32 pe;
pe.dwSize = sizeof(PROCESSENTRY32);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rest of pe should be cleared:

	memset(&pe, 0, sizeof(pe));
	pe.dwSize = sizeof(pe);

Comment thread port/win32/omrsysinfo.c Outdated
}
CloseHandle(hSnap);
}
return buffer;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value is not actually used; suggest this should have void return type (if the function stays).

Comment thread port/win32/omrsysinfo.c Outdated
portLibrary,
OMRPORT_ERROR_OPFAILED,
"Callback function is NULL.");
return (uintptr_t)(intptr_t)OMRPORT_ERROR_OPFAILED;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 2116 should be indented the same as line 2112.

Comment thread port/win32/omrsysinfo.c Outdated
Comment on lines +2155 to +2156
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_QUERY_INFORMATION, FALSE, pid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please join these lines:

		HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_QUERY_INFORMATION, FALSE, pid);

@sharanpatil123

Copy link
Copy Markdown
Contributor Author

@keithc-ca - Addressed review comments.

@sharanpatil123
sharanpatil123 requested a review from keithc-ca July 18, 2025 13:36
Comment thread port/win32/omrsysinfo.c Outdated
char exePath[MAX_PATH];
DWORD pathLen = sizeof(exePath);
DWORD pid = processes[i];
HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION | PROCESS_QUERY_INFORMATION, FALSE, pid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should request the minimum permissions necessary:

		HANDLE hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);

Comment thread port/win32/omrsysinfo.c Outdated
Comment on lines +2132 to +2142
if (NULL != hProcess) {
if (0 == QueryFullProcessImageName(hProcess, 0, exePath, &pathLen)) {
CloseHandle(hProcess);
continue;
}
CloseHandle(hProcess);
}
/* Skip entries with no name. */
if ('\0' == exePath[0]) {
continue;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return type of QueryFullProcessImageName() is BOOL which should not be compared with 0. Suggest using pathLen to detect no name. Only one call to CloseHandle() is needed:

		if (NULL == hProcess) {
			continue;
		}
		if (!QueryFullProcessImageName(hProcess, 0, exePath, &pathLen)) {
			pathLen = 0;
		}
		CloseHandle(hProcess);
		/* Skip entries with no name. */
		if (0 == pathLen) {
			continue;
		}

@keithc-ca keithc-ca left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be squashed. The commit message should mention QueryFullProcessImageName instead of QueryFullProcessImageNameA and should spell "Windows" consistently.

Use EnumProcesses and QueryFullProcessImageName to
extract PID and executable paths on Windows.

Signed-off-by: Sharanabasava <sharan16@in.ibm.com>
@babsingh

Copy link
Copy Markdown
Contributor

jenkins build all

@babsingh babsingh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Linux x86 and riscv64 failures are infra-related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants