src/jdk.attach/windows/native/libattach/AttachProviderImpl.c
author amenkov
Wed, 13 Nov 2019 14:08:04 -0800
changeset 59066 439a147b2c0c
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231915: two JDI tests interfere with each other Reviewed-by: sspitsyn, dtitov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 9840
diff changeset
     2
 * Copyright (c) 2005, 2011, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <string.h>
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 5506
diff changeset
    28
#include <Psapi.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    33
#include "sun_tools_attach_AttachProviderImpl.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    36
 * Class:     sun_tools_attach_AttachProviderImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Method:    tempPath
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Signature: ()Ljava/lang/String;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
JNIEXPORT jstring JNICALL
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    41
Java_sun_tools_attach_AttachProviderImpl_tempPath(JNIEnv *env, jclass cls)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    char buf[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    DWORD bufLen, actualLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    jstring result = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    bufLen = sizeof(buf) / sizeof(char);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    actualLen = GetTempPath(bufLen, buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    if (actualLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        char* bufP = buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (actualLen > bufLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            actualLen += sizeof(char);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            bufP = (char*)malloc(actualLen * sizeof(char));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            actualLen = GetTempPath(actualLen, bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (actualLen > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            result = JNU_NewStringPlatform(env, bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (bufP != buf) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            free((void*)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    67
 * Class:     sun_tools_attach_AttachProviderImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * Method:    volumeFlags
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * Signature: ()J
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
JNIEXPORT jlong JNICALL
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
    72
Java_sun_tools_attach_AttachProviderImpl_volumeFlags(JNIEnv *env, jclass cls, jstring str)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    const char* volume;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    DWORD result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    volume = JNU_GetStringPlatformChars(env, str, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    if (volume != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        DWORD componentLen, flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        BOOL res = GetVolumeInformation(volume,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                        NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                                        0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                                        NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                                        &componentLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                                        &flags,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                                        NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                        0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
       if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
           result = flags;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
       if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            JNU_ReleaseStringPlatformChars(env, str, volume);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   101
 * Class:     sun_tools_attach_AttachProviderImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * Method:    enumProcesses
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Signature: ([JI)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
JNIEXPORT jint JNICALL
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   106
Java_sun_tools_attach_AttachProviderImpl_enumProcesses(JNIEnv *env, jclass cls,
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   107
                                                       jintArray arr, jint max)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    DWORD size, bytesReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    DWORD* ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    jint result = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    size = max * sizeof(DWORD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    ptr = (DWORD*)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    if (ptr != NULL) {
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 5506
diff changeset
   116
        BOOL res = EnumProcesses(ptr, size, &bytesReturned);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            result = (jint)(bytesReturned / sizeof(DWORD));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            (*env)->SetIntArrayRegion(env, arr, 0, (jsize)result, (jint*)ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        free((void*)ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/*
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   127
 * Class:     sun_tools_attach_AttachProviderImpl
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * Method:    isLibraryLoadedByProcess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * Signature: (I[Ljava/lang/String;)Z
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
JNIEXPORT jboolean JNICALL
26216
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   132
Java_sun_tools_attach_AttachProviderImpl_isLibraryLoadedByProcess(JNIEnv *env, jclass cls,
5e46c782b43c 8055230: Rename attach provider implementation class be platform neutral
mchung
parents: 25859
diff changeset
   133
                                                                  jstring str, jint processId)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    HANDLE hProcess;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    jboolean isCopy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    const char* lib;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    DWORD size, bytesReturned;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    HMODULE* ptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    jboolean result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION |
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                           PROCESS_VM_READ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                           FALSE, (DWORD)processId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    if (hProcess == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    lib = JNU_GetStringPlatformChars(env, str, &isCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    if (lib == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        CloseHandle(hProcess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Enumerate the modules that the process has opened and see if we have a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * match.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    size = 1024 * sizeof(HMODULE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    ptr = (HMODULE*)malloc(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    if (ptr != NULL) {
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 5506
diff changeset
   161
        BOOL res = EnumProcessModules(hProcess, ptr, size, &bytesReturned);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            int count = bytesReturned / sizeof(HMODULE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            while (i < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                char base[256];
9840
376425f4ed3d 7003964: SERV: securely load DLLs and launch executables using fully qualified path
zgu
parents: 5506
diff changeset
   167
                BOOL res = GetModuleBaseName(hProcess, ptr[i], base, sizeof(base));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                if (res != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    if (strcmp(base, lib) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                      result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        free((void*)ptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    if (isCopy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        JNU_ReleaseStringPlatformChars(env, str, lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    CloseHandle(hProcess);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
}