2
* Copyright (c) 2005, 2024, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
32
#include "sun_tools_attach_VirtualMachineImpl.h"
35
typedef HINSTANCE (WINAPI* GetModuleHandleFunc) (LPCTSTR);
36
typedef FARPROC (WINAPI* GetProcAddressFunc)(HMODULE, LPCSTR);
38
/* only on Windows 64-bit or 32-bit application running under WOW64 */
39
typedef BOOL (WINAPI *IsWow64ProcessFunc) (HANDLE, PBOOL);
41
static GetModuleHandleFunc _GetModuleHandle;
42
static GetProcAddressFunc _GetProcAddress;
43
static IsWow64ProcessFunc _IsWow64Process;
46
typedef BOOL (WINAPI *EnumProcessModulesFunc) (HANDLE, HMODULE *, DWORD, LPDWORD );
47
typedef DWORD (WINAPI *GetModuleFileNameExFunc) ( HANDLE, HMODULE, LPTSTR, DWORD );
49
/* exported function in target VM */
50
typedef jint (WINAPI* EnqueueOperationFunc)
51
(const char* cmd, const char* arg1, const char* arg2, const char* arg3, const char* pipename);
53
/* OpenProcess with SE_DEBUG_NAME privilege */
55
doPrivilegedOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
57
/* Converts jstring to C string, returns JNI_FALSE if the string has been truncated. */
58
static jboolean jstring_to_cstring(JNIEnv* env, jstring jstr, char* cstr, size_t cstr_buf_size);
62
* Data copied to target process
65
#define MAX_LIBNAME_LENGTH 16
66
#define MAX_FUNC_LENGTH 32
67
#define MAX_CMD_LENGTH 16
68
#define MAX_ARG_LENGTH 1024
70
#define MAX_PIPE_NAME_LENGTH 256
73
GetModuleHandleFunc _GetModuleHandle;
74
GetProcAddressFunc _GetProcAddress;
75
char jvmLib[MAX_LIBNAME_LENGTH]; /* "jvm.dll" */
76
char func1[MAX_FUNC_LENGTH];
77
char func2[MAX_FUNC_LENGTH];
78
char cmd[MAX_CMD_LENGTH + 1]; /* "load", "dump", ... */
79
char arg[MAX_ARGS][MAX_ARG_LENGTH + 1]; /* arguments to command */
80
char pipename[MAX_PIPE_NAME_LENGTH + 1];
84
* Return codes from enqueue function executed in target VM
86
#define ERR_OPEN_JVM_FAIL 200
87
#define ERR_GET_ENQUEUE_FUNC_FAIL 201
90
* Declare library specific JNI_Onload entry if static build
95
* Code copied to target process
97
#pragma check_stack (off)
98
/* Switch off all runtime checks (checks caused by /RTC<x>). They cause the
99
* generated code to contain relative jumps to check functions which make
100
* the code position dependent. */
101
#pragma runtime_checks ("scu", off)
102
DWORD WINAPI jvm_attach_thread_func(DataBlock *pData)
105
EnqueueOperationFunc addr;
107
h = pData->_GetModuleHandle(pData->jvmLib);
109
return ERR_OPEN_JVM_FAIL;
112
addr = (EnqueueOperationFunc)(pData->_GetProcAddress(h, pData->func1));
114
addr = (EnqueueOperationFunc)(pData->_GetProcAddress(h, pData->func2));
117
return ERR_GET_ENQUEUE_FUNC_FAIL;
120
/* "null" command - does nothing in the target VM */
121
if (pData->cmd[0] == '\0') {
124
return (*addr)(pData->cmd, pData->arg[0], pData->arg[1], pData->arg[2], pData->pipename);
128
/* This function marks the end of jvm_attach_thread_func. */
129
void jvm_attach_thread_func_end (void) {
132
#pragma runtime_checks ("scu", restore)
135
* Class: sun_tools_attach_VirtualMachineImpl
139
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_init
140
(JNIEnv *env, jclass cls)
142
// All following APIs exist on Windows XP with SP2/Windows Server 2008
143
_GetModuleHandle = (GetModuleHandleFunc)GetModuleHandle;
144
_GetProcAddress = (GetProcAddressFunc)GetProcAddress;
145
_IsWow64Process = (IsWow64ProcessFunc)IsWow64Process;
150
* Class: sun_tools_attach_VirtualMachineImpl
151
* Method: generateStub
154
JNIEXPORT jbyteArray JNICALL Java_sun_tools_attach_VirtualMachineImpl_generateStub
155
(JNIEnv *env, jclass cls)
158
* We should replace this with a real stub generator at some point
163
len = (DWORD)((LPBYTE) jvm_attach_thread_func_end - (LPBYTE) jvm_attach_thread_func);
164
array= (*env)->NewByteArray(env, (jsize)len);
166
(*env)->SetByteArrayRegion(env, array, 0, (jint)len, (jbyte*)&jvm_attach_thread_func);
172
* Class: sun_tools_attach_VirtualMachineImpl
173
* Method: openProcess
176
JNIEXPORT jlong JNICALL Java_sun_tools_attach_VirtualMachineImpl_openProcess
177
(JNIEnv *env, jclass cls, jint pid)
179
HANDLE hProcess = NULL;
181
if (pid == (jint) GetCurrentProcessId()) {
182
/* process is attaching to itself; get a pseudo handle instead */
183
hProcess = GetCurrentProcess();
184
/* duplicate the pseudo handle so it can be used in more contexts */
185
if (DuplicateHandle(hProcess, hProcess, hProcess, &hProcess,
186
PROCESS_ALL_ACCESS, FALSE, 0) == 0) {
188
* Could not duplicate the handle which isn't a good sign,
189
* but we'll try again with OpenProcess() below.
195
if (hProcess == NULL) {
197
* Attempt to open process. If it fails then we try to enable the
198
* SE_DEBUG_NAME privilege and retry.
200
hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
201
if (hProcess == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
202
hProcess = doPrivilegedOpenProcess(PROCESS_ALL_ACCESS, FALSE,
206
if (hProcess == NULL) {
207
if (GetLastError() == ERROR_INVALID_PARAMETER) {
208
JNU_ThrowIOException(env, "no such process");
211
/* include the last error in the default detail message */
212
snprintf(err_mesg, sizeof(err_mesg), "OpenProcess(pid=%d) failed; LastError=0x%x",
213
(int)pid, (int)GetLastError());
214
JNU_ThrowIOExceptionWithLastError(env, err_mesg);
221
* On Windows 64-bit we need to handle 32-bit tools trying to attach to 64-bit
222
* processes (and visa versa). X-architecture attaching is currently not supported
223
* by this implementation.
225
if (_IsWow64Process != NULL) {
226
BOOL isCurrent32bit, isTarget32bit;
227
(*_IsWow64Process)(GetCurrentProcess(), &isCurrent32bit);
228
(*_IsWow64Process)(hProcess, &isTarget32bit);
230
if (isCurrent32bit != isTarget32bit) {
231
CloseHandle(hProcess);
233
JNU_ThrowByName(env, "com/sun/tools/attach/AttachNotSupportedException",
234
"Unable to attach to 32-bit process running under WOW64");
236
JNU_ThrowByName(env, "com/sun/tools/attach/AttachNotSupportedException",
237
"Unable to attach to 64-bit process");
242
return (jlong)hProcess;
247
* Class: sun_tools_attach_VirtualMachineImpl
248
* Method: closeProcess
251
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_closeProcess
252
(JNIEnv *env, jclass cls, jlong hProcess)
254
CloseHandle((HANDLE)hProcess);
259
* Class: sun_tools_attach_VirtualMachineImpl
261
* Signature: (Ljava/lang/String;)J
263
JNIEXPORT jlong JNICALL Java_sun_tools_attach_VirtualMachineImpl_createPipe
264
(JNIEnv *env, jclass cls, jstring pipename)
267
char name[MAX_PIPE_NAME_LENGTH];
269
SECURITY_ATTRIBUTES sa;
270
LPSECURITY_ATTRIBUTES lpSA = NULL;
271
// Custom Security Descriptor is required here to "get" Medium Integrity Level.
272
// In order to allow Medium Integrity Level clients to open
273
// and use a NamedPipe created by an High Integrity Level process.
274
TCHAR *szSD = TEXT("D:") // Discretionary ACL
275
TEXT("(A;OICI;GRGW;;;WD)") // Allow read/write to Everybody
276
TEXT("(A;OICI;GA;;;SY)") // Allow full control to System
277
TEXT("(A;OICI;GA;;;BA)"); // Allow full control to Administrators
279
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
280
sa.bInheritHandle = FALSE;
281
sa.lpSecurityDescriptor = NULL;
283
if (ConvertStringSecurityDescriptorToSecurityDescriptor
284
(szSD, SDDL_REVISION_1, &(sa.lpSecurityDescriptor), NULL)) {
288
jstring_to_cstring(env, pipename, name, MAX_PIPE_NAME_LENGTH);
290
hPipe = CreateNamedPipe(
292
PIPE_ACCESS_INBOUND, // read access
293
PIPE_TYPE_BYTE | // byte mode
295
PIPE_WAIT, // blocking mode
297
128, // output buffer size
298
8192, // input buffer size
299
NMPWAIT_USE_DEFAULT_WAIT, // client time-out
300
lpSA); // security attributes
302
LocalFree(sa.lpSecurityDescriptor);
304
if (hPipe == INVALID_HANDLE_VALUE) {
305
JNU_ThrowIOExceptionWithLastError(env, "CreateNamedPipe failed");
311
* Class: sun_tools_attach_VirtualMachineImpl
315
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_closePipe
316
(JNIEnv *env, jclass cls, jlong hPipe)
318
CloseHandle((HANDLE)hPipe);
322
* Class: sun_tools_attach_VirtualMachineImpl
323
* Method: connectPipe
326
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_connectPipe
327
(JNIEnv *env, jclass cls, jlong hPipe)
331
fConnected = ConnectNamedPipe((HANDLE)hPipe, NULL) ?
332
TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
334
JNU_ThrowIOExceptionWithLastError(env, "ConnectNamedPipe failed");
339
* Class: sun_tools_attach_VirtualMachineImpl
341
* Signature: (J[BII)I
343
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_readPipe
344
(JNIEnv *env, jclass cls, jlong hPipe, jbyteArray ba, jint off, jint baLen)
346
unsigned char buf[128];
347
DWORD len, nread, remaining;
351
remaining = (DWORD)(baLen - off);
352
if (len > remaining) {
357
(HANDLE)hPipe, // handle to pipe
358
buf, // buffer to receive data
359
len, // size of buffer
360
&nread, // number of bytes read
361
NULL); // not overlapped I/O
364
if (GetLastError() == ERROR_BROKEN_PIPE) {
367
JNU_ThrowIOExceptionWithLastError(env, "ReadFile");
371
return (jint)-1; // EOF
373
(*env)->SetByteArrayRegion(env, ba, off, (jint)nread, (jbyte *)(buf));
381
* Class: sun_tools_attach_VirtualMachineImpl
383
* Signature: (JZLjava/lang/String;[Ljava/lang/Object;)V
385
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_enqueue
386
(JNIEnv *env, jclass cls, jlong handle, jbyteArray stub, jstring cmd,
387
jstring pipename, jobjectArray args)
393
HANDLE hProcess, hThread;
399
* Setup data to copy to target process
401
memset(&data, 0, sizeof(data));
402
data._GetModuleHandle = _GetModuleHandle;
403
data._GetProcAddress = _GetProcAddress;
405
strcpy(data.jvmLib, "jvm");
406
strcpy(data.func1, "JVM_EnqueueOperation");
407
strcpy(data.func2, "_JVM_EnqueueOperation@20");
410
* Command and arguments
412
if (!jstring_to_cstring(env, cmd, data.cmd, sizeof(data.cmd))) {
413
JNU_ThrowByName(env, "com/sun/tools/attach/AttachOperationFailedException",
414
"command is too long");
417
argsLen = (*env)->GetArrayLength(env, args);
420
if (argsLen > MAX_ARGS) {
421
JNU_ThrowInternalError(env, "Too many arguments");
424
for (i=0; i<argsLen; i++) {
425
jobject obj = (*env)->GetObjectArrayElement(env, args, i);
427
data.arg[i][0] = '\0';
429
if (!jstring_to_cstring(env, obj, data.arg[i], sizeof(data.arg[i]))) {
430
JNU_ThrowByName(env, "com/sun/tools/attach/AttachOperationFailedException",
431
"argument is too long");
435
if ((*env)->ExceptionOccurred(env)) return;
438
for (i = argsLen; i < MAX_ARGS; i++) {
439
data.arg[i][0] = '\0';
443
if (!jstring_to_cstring(env, pipename, data.pipename, sizeof(data.pipename))) {
444
JNU_ThrowByName(env, "com/sun/tools/attach/AttachOperationFailedException",
445
"pipe name is too long");
450
* Allocate memory in target process for data and code stub
451
* (assumed aligned and matches architecture of target process)
453
hProcess = (HANDLE)handle;
455
pData = (DataBlock*) VirtualAllocEx( hProcess, 0, sizeof(DataBlock), MEM_COMMIT, PAGE_READWRITE );
457
JNU_ThrowIOExceptionWithLastError(env, "VirtualAllocEx failed");
460
WriteProcessMemory( hProcess, (LPVOID)pData, (LPCVOID)&data, (SIZE_T)sizeof(DataBlock), NULL );
463
stubLen = (DWORD)(*env)->GetArrayLength(env, stub);
464
stubCode = (*env)->GetByteArrayElements(env, stub, &isCopy);
466
if ((*env)->ExceptionOccurred(env)) return;
468
pCode = (PDWORD) VirtualAllocEx( hProcess, 0, stubLen, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
470
JNU_ThrowIOExceptionWithLastError(env, "VirtualAllocEx failed");
471
VirtualFreeEx(hProcess, pData, 0, MEM_RELEASE);
472
(*env)->ReleaseByteArrayElements(env, stub, stubCode, JNI_ABORT);
475
WriteProcessMemory( hProcess, (LPVOID)pCode, (LPCVOID)stubCode, (SIZE_T)stubLen, NULL );
476
(*env)->ReleaseByteArrayElements(env, stub, stubCode, JNI_ABORT);
479
* Create thread in target process to execute code
481
hThread = CreateRemoteThread( hProcess,
484
(LPTHREAD_START_ROUTINE) pCode,
488
if (hThread != NULL) {
489
if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0) {
490
JNU_ThrowIOExceptionWithLastError(env, "WaitForSingleObject failed");
493
GetExitCodeThread(hThread, &exitCode);
496
case ERR_OPEN_JVM_FAIL :
497
JNU_ThrowIOException(env,
498
"jvm.dll not loaded by target process");
500
case ERR_GET_ENQUEUE_FUNC_FAIL :
501
JNU_ThrowIOException(env,
502
"Unable to enqueue operation: the target VM does not support attach mechanism");
506
snprintf(errmsg, sizeof(errmsg), "Remote thread failed for unknown reason (%d)", exitCode);
507
JNU_ThrowInternalError(env, errmsg);
512
CloseHandle(hThread);
514
if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
516
// This error will occur when attaching to a process belonging to
517
// another terminal session. See "Remarks":
518
// http://msdn.microsoft.com/en-us/library/ms682437%28VS.85%29.aspx
520
JNU_ThrowIOException(env,
521
"Insufficient memory or insufficient privileges to attach");
523
JNU_ThrowIOExceptionWithLastError(env, "CreateRemoteThread failed");
527
VirtualFreeEx(hProcess, pCode, 0, MEM_RELEASE);
528
VirtualFreeEx(hProcess, pData, 0, MEM_RELEASE);
532
* Attempts to enable the SE_DEBUG_NAME privilege and open the given process.
535
doPrivilegedOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) {
537
HANDLE hProcess = NULL;
539
TOKEN_PRIVILEGES tp, tpPrevious;
540
DWORD retLength, error;
543
* Get the access token
545
if (!OpenThreadToken(GetCurrentThread(),
546
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
549
if (GetLastError() != ERROR_NO_TOKEN) {
554
* No access token for the thread so impersonate the security context
557
if (!ImpersonateSelf(SecurityImpersonation)) {
560
if (!OpenThreadToken(GetCurrentThread(),
561
TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
569
* Get LUID for the privilege
571
if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
572
error = GetLastError();
579
* Enable the privilege
581
ZeroMemory(&tp, sizeof(tp));
582
tp.PrivilegeCount = 1;
583
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
584
tp.Privileges[0].Luid = luid;
587
if (AdjustTokenPrivileges(hToken,
590
sizeof(TOKEN_PRIVILEGES),
594
* If we enabled the privilege then attempt to open the
597
if (GetLastError() == ERROR_SUCCESS) {
598
hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
599
if (hProcess == NULL) {
600
error = GetLastError();
603
error = ERROR_ACCESS_DENIED;
607
* Revert to the previous privileges
609
AdjustTokenPrivileges(hToken,
616
error = GetLastError();
621
* Close token and restore error
629
/* Converts jstring to C string, returns JNI_FALSE if the string has been truncated. */
630
static jboolean jstring_to_cstring(JNIEnv* env, jstring jstr, char* cstr, size_t cstr_buf_size) {
633
jboolean result = JNI_TRUE;
638
str = JNU_GetStringPlatformChars(env, jstr, &isCopy);
639
if ((*env)->ExceptionOccurred(env)) {
642
if (strlen(str) >= cstr_buf_size) {
646
strncpy(cstr, str, cstr_buf_size);
647
cstr[cstr_buf_size - 1] = '\0';
649
JNU_ReleaseStringPlatformChars(env, jstr, str);