src/jdk.attach/windows/native/libattach/VirtualMachineImpl.c
author iklam
Wed, 13 Nov 2019 19:55:11 -0800
changeset 59072 b987ea528c21
parent 53042 56fbb14251ca
permissions -rw-r--r--
8234133: VM build broken due to memory/archiveUtils.inline.hpp Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1765
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1765
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1765
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1765
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1765
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
    25
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
    26
#include "jni_util.h"
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
    27
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <windows.h>
26721
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
    29
#include <Sddl.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    32
#include "sun_tools_attach_VirtualMachineImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/* kernel32 */
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
    35
typedef HINSTANCE (WINAPI* GetModuleHandleFunc) (LPCTSTR);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
typedef FARPROC (WINAPI* GetProcAddressFunc)(HMODULE, LPCSTR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/* only on Windows 64-bit or 32-bit application running under WOW64 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
typedef BOOL (WINAPI *IsWow64ProcessFunc) (HANDLE, PBOOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
    41
static GetModuleHandleFunc _GetModuleHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static GetProcAddressFunc _GetProcAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static IsWow64ProcessFunc _IsWow64Process;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/* psapi */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
typedef BOOL  (WINAPI *EnumProcessModulesFunc)  (HANDLE, HMODULE *, DWORD, LPDWORD );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
typedef DWORD (WINAPI *GetModuleFileNameExFunc) ( HANDLE, HMODULE, LPTSTR, DWORD );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/* exported function in target VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
typedef jint (WINAPI* EnqueueOperationFunc)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    (const char* cmd, const char* arg1, const char* arg2, const char* arg3, const char* pipename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/* OpenProcess with SE_DEBUG_NAME privilege */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static HANDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
doPrivilegedOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
/* convert jstring to C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
static void jstring_to_cstring(JNIEnv* env, jstring jstr, char* cstr, int len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * Data copied to target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define MAX_LIBNAME_LENGTH      16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define MAX_FUNC_LENGTH         32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#define MAX_CMD_LENGTH          16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#define MAX_ARG_LENGTH          1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#define MAX_ARGS                3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#define MAX_PIPE_NAME_LENGTH    256
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
typedef struct {
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
    73
   GetModuleHandleFunc _GetModuleHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
   GetProcAddressFunc _GetProcAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
   char jvmLib[MAX_LIBNAME_LENGTH];         /* "jvm.dll" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
   char func1[MAX_FUNC_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
   char func2[MAX_FUNC_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
   char cmd[MAX_CMD_LENGTH];                /* "load", "dump", ...      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
   char arg[MAX_ARGS][MAX_ARG_LENGTH];      /* arguments to command     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
   char pipename[MAX_PIPE_NAME_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
} DataBlock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Return codes from enqueue function executed in target VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#define ERR_OPEN_JVM_FAIL           200
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#define ERR_GET_ENQUEUE_FUNC_FAIL   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26969
diff changeset
    89
/*
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26969
diff changeset
    90
 * Declare library specific JNI_Onload entry if static build
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26969
diff changeset
    91
 */
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 26969
diff changeset
    92
DEF_STATIC_JNI_OnLoad
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * Code copied to target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
#pragma check_stack (off)
26969
ca3ea2ba7c86 8059868: JVM crashes on attach on Windows when compiled with /RTC1
simonis
parents: 26721
diff changeset
    98
/* Switch off all runtime checks (checks caused by /RTC<x>). They cause the
ca3ea2ba7c86 8059868: JVM crashes on attach on Windows when compiled with /RTC1
simonis
parents: 26721
diff changeset
    99
 * generated code to contain relative jumps to check functions which make
ca3ea2ba7c86 8059868: JVM crashes on attach on Windows when compiled with /RTC1
simonis
parents: 26721
diff changeset
   100
 * the code position dependent. */
ca3ea2ba7c86 8059868: JVM crashes on attach on Windows when compiled with /RTC1
simonis
parents: 26721
diff changeset
   101
#pragma runtime_checks ("scu", off)
19442
00965155a4c5 8022071: Some vm/jvmti tests fail because cannot attach to the Java virtual machine
sla
parents: 14342
diff changeset
   102
DWORD WINAPI jvm_attach_thread_func(DataBlock *pData)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    HINSTANCE h;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    EnqueueOperationFunc addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   107
    h = pData->_GetModuleHandle(pData->jvmLib);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    if (h == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return ERR_OPEN_JVM_FAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    addr = (EnqueueOperationFunc)(pData->_GetProcAddress(h, pData->func1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    if (addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        addr = (EnqueueOperationFunc)(pData->_GetProcAddress(h, pData->func2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (addr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return ERR_GET_ENQUEUE_FUNC_FAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* "null" command - does nothing in the target VM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (pData->cmd[0] == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return (*addr)(pData->cmd, pData->arg[0], pData->arg[1], pData->arg[2], pData->pipename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
19442
00965155a4c5 8022071: Some vm/jvmti tests fail because cannot attach to the Java virtual machine
sla
parents: 14342
diff changeset
   128
/* This function marks the end of jvm_attach_thread_func. */
00965155a4c5 8022071: Some vm/jvmti tests fail because cannot attach to the Java virtual machine
sla
parents: 14342
diff changeset
   129
void jvm_attach_thread_func_end (void) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
#pragma check_stack
26969
ca3ea2ba7c86 8059868: JVM crashes on attach on Windows when compiled with /RTC1
simonis
parents: 26721
diff changeset
   132
#pragma runtime_checks ("scu", restore)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   135
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   139
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_init
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
{
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   142
    // All following APIs exist on Windows XP with SP2/Windows Server 2008
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   143
    _GetModuleHandle = (GetModuleHandleFunc)GetModuleHandle;
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   144
    _GetProcAddress = (GetProcAddressFunc)GetProcAddress;
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   145
    _IsWow64Process = (IsWow64ProcessFunc)IsWow64Process;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   150
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * Method:    generateStub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * Signature: ()[B
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   154
JNIEXPORT jbyteArray JNICALL Java_sun_tools_attach_VirtualMachineImpl_generateStub
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
  (JNIEnv *env, jclass cls)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * We should replace this with a real stub generator at some point
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    DWORD len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    jbyteArray array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
19442
00965155a4c5 8022071: Some vm/jvmti tests fail because cannot attach to the Java virtual machine
sla
parents: 14342
diff changeset
   163
    len = (DWORD)((LPBYTE) jvm_attach_thread_func_end - (LPBYTE) jvm_attach_thread_func);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    array= (*env)->NewByteArray(env, (jsize)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    if (array != NULL) {
19442
00965155a4c5 8022071: Some vm/jvmti tests fail because cannot attach to the Java virtual machine
sla
parents: 14342
diff changeset
   166
        (*env)->SetByteArrayRegion(env, array, 0, (jint)len, (jbyte*)&jvm_attach_thread_func);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   172
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * Method:    openProcess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
 * Signature: (I)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   176
JNIEXPORT jlong JNICALL Java_sun_tools_attach_VirtualMachineImpl_openProcess
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
  (JNIEnv *env, jclass cls, jint pid)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
{
8804
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   179
    HANDLE hProcess = NULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
8804
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   181
    if (pid == (jint) GetCurrentProcessId()) {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   182
        /* process is attaching to itself; get a pseudo handle instead */
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   183
        hProcess = GetCurrentProcess();
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   184
        /* duplicate the pseudo handle so it can be used in more contexts */
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   185
        if (DuplicateHandle(hProcess, hProcess, hProcess, &hProcess,
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   186
                PROCESS_ALL_ACCESS, FALSE, 0) == 0) {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   187
            /*
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   188
             * Could not duplicate the handle which isn't a good sign,
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   189
             * but we'll try again with OpenProcess() below.
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   190
             */
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   191
            hProcess = NULL;
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    if (hProcess == NULL) {
8804
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   196
        /*
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   197
         * Attempt to open process. If it fails then we try to enable the
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   198
         * SE_DEBUG_NAME privilege and retry.
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   199
         */
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   200
        hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   201
        if (hProcess == NULL && GetLastError() == ERROR_ACCESS_DENIED) {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   202
            hProcess = doPrivilegedOpenProcess(PROCESS_ALL_ACCESS, FALSE,
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   203
                           (DWORD)pid);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        }
8804
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   205
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   206
        if (hProcess == NULL) {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   207
            if (GetLastError() == ERROR_INVALID_PARAMETER) {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   208
                JNU_ThrowIOException(env, "no such process");
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   209
            } else {
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   210
                char err_mesg[255];
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   211
                /* include the last error in the default detail message */
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   212
                sprintf(err_mesg, "OpenProcess(pid=%d) failed; LastError=0x%x",
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   213
                    (int)pid, (int)GetLastError());
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   214
                JNU_ThrowIOExceptionWithLastError(env, err_mesg);
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   215
            }
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   216
            return (jlong)0;
2d69a944f4da 7028668: 3/4 improve diagnosibility and robustness of sun.tools.attach.WindowsVirtualMachine.openProcess()
dcubed
parents: 5506
diff changeset
   217
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * On Windows 64-bit we need to handle 32-bit tools trying to attach to 64-bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * processes (and visa versa). X-architecture attaching is currently not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * by this implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    if (_IsWow64Process != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        BOOL isCurrent32bit, isTarget32bit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        (*_IsWow64Process)(GetCurrentProcess(), &isCurrent32bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        (*_IsWow64Process)(hProcess, &isTarget32bit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (isCurrent32bit != isTarget32bit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            CloseHandle(hProcess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            #ifdef _WIN64
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
              JNU_ThrowByName(env, "com/sun/tools/attach/AttachNotSupportedException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                  "Unable to attach to 32-bit process running under WOW64");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            #else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
              JNU_ThrowByName(env, "com/sun/tools/attach/AttachNotSupportedException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                  "Unable to attach to 64-bit process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    return (jlong)hProcess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   247
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 * Method:    closeProcess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   251
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_closeProcess
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
  (JNIEnv *env, jclass cls, jlong hProcess)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    CloseHandle((HANDLE)hProcess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   259
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 * Method:    createPipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
 * Signature: (Ljava/lang/String;)J
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   263
JNIEXPORT jlong JNICALL Java_sun_tools_attach_VirtualMachineImpl_createPipe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
  (JNIEnv *env, jclass cls, jstring pipename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    HANDLE hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    char name[MAX_PIPE_NAME_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
26721
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   269
    SECURITY_ATTRIBUTES sa;
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   270
    LPSECURITY_ATTRIBUTES lpSA = NULL;
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   271
    // Custom Security Descriptor is required here to "get" Medium Integrity Level.
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   272
    // In order to allow Medium Integrity Level clients to open
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   273
    // and use a NamedPipe created by an High Integrity Level process.
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   274
    TCHAR *szSD = TEXT("D:")                  // Discretionary ACL
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   275
                  TEXT("(A;OICI;GRGW;;;WD)")  // Allow read/write to Everybody
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   276
                  TEXT("(A;OICI;GA;;;SY)")    // Allow full control to System
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   277
                  TEXT("(A;OICI;GA;;;BA)");   // Allow full control to Administrators
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   278
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   279
    sa.nLength = sizeof(SECURITY_ATTRIBUTES);
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   280
    sa.bInheritHandle = FALSE;
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   281
    sa.lpSecurityDescriptor = NULL;
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   282
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   283
    if (ConvertStringSecurityDescriptorToSecurityDescriptor
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   284
          (szSD, SDDL_REVISION_1, &(sa.lpSecurityDescriptor), NULL)) {
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   285
        lpSA = &sa;
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   286
    }
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   287
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    jstring_to_cstring(env, pipename, name, MAX_PIPE_NAME_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    hPipe = CreateNamedPipe(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
          name,                         // pipe name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
          PIPE_ACCESS_INBOUND,          // read access
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
          PIPE_TYPE_BYTE |              // byte mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            PIPE_READMODE_BYTE |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            PIPE_WAIT,                  // blocking mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
          1,                            // max. instances
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
          128,                          // output buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
          8192,                         // input buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
          NMPWAIT_USE_DEFAULT_WAIT,     // client time-out
26721
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   300
          lpSA);        // security attributes
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   301
ddeee2f1c05d 8057564: JVM hangs at getAgentProperties after attaching to VM with lower
sgabdura
parents: 26216
diff changeset
   302
    LocalFree(sa.lpSecurityDescriptor);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    if (hPipe == INVALID_HANDLE_VALUE) {
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
   305
        JNU_ThrowIOExceptionWithLastError(env, "CreateNamedPipe failed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    return (jlong)hPipe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   311
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
 * Method:    closePipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   315
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_closePipe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
  (JNIEnv *env, jclass cls, jlong hPipe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
{
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
   318
    CloseHandle((HANDLE)hPipe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   322
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
 * Method:    connectPipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Signature: (J)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   326
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_connectPipe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
  (JNIEnv *env, jclass cls, jlong hPipe)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    BOOL fConnected;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    fConnected = ConnectNamedPipe((HANDLE)hPipe, NULL) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    if (!fConnected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        JNU_ThrowIOExceptionWithLastError(env, "ConnectNamedPipe failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   339
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 * Method:    readPipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 * Signature: (J[BII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   343
JNIEXPORT jint JNICALL Java_sun_tools_attach_VirtualMachineImpl_readPipe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
  (JNIEnv *env, jclass cls, jlong hPipe, jbyteArray ba, jint off, jint baLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    unsigned char buf[128];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    DWORD len, nread, remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    BOOL fSuccess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    len = sizeof(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    remaining = (DWORD)(baLen - off);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (len > remaining) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        len = remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    fSuccess = ReadFile(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         (HANDLE)hPipe,         // handle to pipe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         buf,                   // buffer to receive data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         len,                   // size of buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         &nread,                // number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         NULL);                 // not overlapped I/O
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    if (!fSuccess) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (GetLastError() == ERROR_BROKEN_PIPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            return (jint)-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            JNU_ThrowIOExceptionWithLastError(env, "ReadFile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        if (nread == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            return (jint)-1;        // EOF
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } else {
24363
33b869a8806b 8039173: Propagate errors from Diagnostic Commands as exceptions in the attach framework
sla
parents: 23570
diff changeset
   373
            (*env)->SetByteArrayRegion(env, ba, off, (jint)nread, (jbyte *)(buf));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    return (jint)nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   382
 * Class:     sun_tools_attach_VirtualMachineImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
 * Method:    enqueue
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
 * Signature: (JZLjava/lang/String;[Ljava/lang/Object;)V
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
 */
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   386
JNIEXPORT void JNICALL Java_sun_tools_attach_VirtualMachineImpl_enqueue
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
  (JNIEnv *env, jclass cls, jlong handle, jbyteArray stub, jstring cmd,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
   jstring pipename, jobjectArray args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    DataBlock data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    DataBlock* pData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    DWORD* pCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    DWORD stubLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    HANDLE hProcess, hThread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    jint argsLen, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    jbyte* stubCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Setup data to copy to target process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 8804
diff changeset
   402
    data._GetModuleHandle = _GetModuleHandle;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    data._GetProcAddress = _GetProcAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    strcpy(data.jvmLib, "jvm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    strcpy(data.func1, "JVM_EnqueueOperation");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    strcpy(data.func2, "_JVM_EnqueueOperation@20");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Command and arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    jstring_to_cstring(env, cmd, data.cmd, MAX_CMD_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    argsLen = (*env)->GetArrayLength(env, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if (argsLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        if (argsLen > MAX_ARGS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            JNU_ThrowInternalError(env, "Too many arguments");
25657
659ec6a877c0 8035829: [parfait] JNI exception pending in jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c
jbachorik
parents: 24363
diff changeset
   418
            return;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        for (i=0; i<argsLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            jobject obj = (*env)->GetObjectArrayElement(env, args, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            if (obj == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                data.arg[i][0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                jstring_to_cstring(env, obj, data.arg[i], MAX_ARG_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if ((*env)->ExceptionOccurred(env)) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
49742
1196aa0be8be 8201247: Various cleanups in the attach framework
clanger
parents: 47216
diff changeset
   430
    for (i = argsLen; i < MAX_ARGS; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        data.arg[i][0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    /* pipe name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    jstring_to_cstring(env, pipename, data.pipename, MAX_PIPE_NAME_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Allocate memory in target process for data and code stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * (assumed aligned and matches architecture of target process)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    hProcess = (HANDLE)handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    pData = (DataBlock*) VirtualAllocEx( hProcess, 0, sizeof(DataBlock), MEM_COMMIT, PAGE_READWRITE );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    if (pData == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        JNU_ThrowIOExceptionWithLastError(env, "VirtualAllocEx failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
1765
ee78704ab554 6787009: (attach) Stub injection potentially unsafe on windows-x64
alanb
parents: 2
diff changeset
   448
    WriteProcessMemory( hProcess, (LPVOID)pData, (LPCVOID)&data, (SIZE_T)sizeof(DataBlock), NULL );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    stubLen = (DWORD)(*env)->GetArrayLength(env, stub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    stubCode = (*env)->GetByteArrayElements(env, stub, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
25657
659ec6a877c0 8035829: [parfait] JNI exception pending in jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c
jbachorik
parents: 24363
diff changeset
   454
    if ((*env)->ExceptionOccurred(env)) return;
659ec6a877c0 8035829: [parfait] JNI exception pending in jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c
jbachorik
parents: 24363
diff changeset
   455
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    pCode = (PDWORD) VirtualAllocEx( hProcess, 0, stubLen, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    if (pCode == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        JNU_ThrowIOExceptionWithLastError(env, "VirtualAllocEx failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        VirtualFreeEx(hProcess, pData, 0, MEM_RELEASE);
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 50859
diff changeset
   460
        (*env)->ReleaseByteArrayElements(env, stub, stubCode, JNI_ABORT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    }
1765
ee78704ab554 6787009: (attach) Stub injection potentially unsafe on windows-x64
alanb
parents: 2
diff changeset
   463
    WriteProcessMemory( hProcess, (LPVOID)pCode, (LPCVOID)stubCode, (SIZE_T)stubLen, NULL );
53042
56fbb14251ca 8215411: some GetByteArrayElements calls miss corresponding Release
mbaesken
parents: 50859
diff changeset
   464
    (*env)->ReleaseByteArrayElements(env, stub, stubCode, JNI_ABORT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Create thread in target process to execute code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    hThread = CreateRemoteThread( hProcess,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                                  NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                                  0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                                  (LPTHREAD_START_ROUTINE) pCode,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                                  pData,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                                  0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                                  NULL );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    if (hThread != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        if (WaitForSingleObject(hThread, INFINITE) != WAIT_OBJECT_0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            JNU_ThrowIOExceptionWithLastError(env, "WaitForSingleObject failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            DWORD exitCode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            GetExitCodeThread(hThread, &exitCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            if (exitCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                switch (exitCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                    case ERR_OPEN_JVM_FAIL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                        JNU_ThrowIOException(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                            "jvm.dll not loaded by target process");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    case ERR_GET_ENQUEUE_FUNC_FAIL :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                        JNU_ThrowIOException(env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                            "Unable to enqueue operation: the target VM does not support attach mechanism");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        break;
50859
29169633327c 8205719: Windows Java_sun_tools_attach_VirtualMachineImpl_enqueue() method should include exitCode in exception message
cjplummer
parents: 49742
diff changeset
   492
                    default : {
29169633327c 8205719: Windows Java_sun_tools_attach_VirtualMachineImpl_enqueue() method should include exitCode in exception message
cjplummer
parents: 49742
diff changeset
   493
                        char errmsg[128];
29169633327c 8205719: Windows Java_sun_tools_attach_VirtualMachineImpl_enqueue() method should include exitCode in exception message
cjplummer
parents: 49742
diff changeset
   494
                        sprintf(errmsg, "Remote thread failed for unknown reason (%d)", exitCode);
29169633327c 8205719: Windows Java_sun_tools_attach_VirtualMachineImpl_enqueue() method should include exitCode in exception message
cjplummer
parents: 49742
diff changeset
   495
                        JNU_ThrowInternalError(env, errmsg);
29169633327c 8205719: Windows Java_sun_tools_attach_VirtualMachineImpl_enqueue() method should include exitCode in exception message
cjplummer
parents: 49742
diff changeset
   496
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        CloseHandle(hThread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    } else {
12698
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   502
        if (GetLastError() == ERROR_NOT_ENOUGH_MEMORY) {
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   503
            //
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   504
            // This error will occur when attaching to a process belonging to
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   505
            // another terminal session. See "Remarks":
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   506
            // http://msdn.microsoft.com/en-us/library/ms682437%28VS.85%29.aspx
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   507
            //
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   508
            JNU_ThrowIOException(env,
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   509
                "Insufficient memory or insufficient privileges to attach");
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   510
        } else {
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   511
            JNU_ThrowIOExceptionWithLastError(env, "CreateRemoteThread failed");
8d48f8005004 7168110: Misleading jstack error message
robm
parents: 9840
diff changeset
   512
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    VirtualFreeEx(hProcess, pCode, 0, MEM_RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    VirtualFreeEx(hProcess, pData, 0, MEM_RELEASE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
 * Attempts to enable the SE_DEBUG_NAME privilege and open the given process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
static HANDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
doPrivilegedOpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle, DWORD dwProcessId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    HANDLE hToken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    HANDLE hProcess = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    LUID luid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    TOKEN_PRIVILEGES tp, tpPrevious;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    DWORD retLength, error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * Get the access token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    if (!OpenThreadToken(GetCurrentThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                         TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                         FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                         &hToken)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        if (GetLastError() != ERROR_NO_TOKEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            return (HANDLE)NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
         * No access token for the thread so impersonate the security context
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
         * of the process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        if (!ImpersonateSelf(SecurityImpersonation)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
            return (HANDLE)NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
        if (!OpenThreadToken(GetCurrentThread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                             TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                             FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                             &hToken)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            return (HANDLE)NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * Get LUID for the privilege
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    if(!LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &luid)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        CloseHandle(hToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        SetLastError(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return (HANDLE)NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Enable the privilege
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    ZeroMemory(&tp, sizeof(tp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    tp.PrivilegeCount = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    tp.Privileges[0].Luid = luid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    if (AdjustTokenPrivileges(hToken,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                              FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                              &tp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                              sizeof(TOKEN_PRIVILEGES),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                              &tpPrevious,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                              &retLength)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
         * If we enabled the privilege then attempt to open the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
         * process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        if (GetLastError() == ERROR_SUCCESS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            if (hProcess == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
            error = ERROR_ACCESS_DENIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
         * Revert to the previous privileges
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        AdjustTokenPrivileges(hToken,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                              FALSE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                              &tpPrevious,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                              retLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                              NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        error = GetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Close token and restore error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    CloseHandle(hToken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    SetLastError(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    return hProcess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
/* convert jstring to C string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
static void jstring_to_cstring(JNIEnv* env, jstring jstr, char* cstr, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
    const char* str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
    if (jstr == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        cstr[0] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        str = JNU_GetStringPlatformChars(env, jstr, &isCopy);
25657
659ec6a877c0 8035829: [parfait] JNI exception pending in jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c
jbachorik
parents: 24363
diff changeset
   626
        if ((*env)->ExceptionOccurred(env)) return;
659ec6a877c0 8035829: [parfait] JNI exception pending in jdk/src/windows/native/sun/tools/attach/WindowsVirtualMachine.c
jbachorik
parents: 24363
diff changeset
   627
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        strncpy(cstr, str, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        cstr[len-1] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            JNU_ReleaseStringPlatformChars(env, jstr, str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
}