src/java.instrument/share/native/libinstrument/JavaExceptions.c
author dholmes
Sat, 23 Jun 2018 01:32:41 -0400
changeset 50735 2f2af62dfac7
parent 47216 71c04702a3d5
permissions -rw-r--r--
8010319: Implementation of JEP 181: Nest-Based Access Control Reviewed-by: alanb, psandoz, mchung, coleenp, acorn, mcimadamore, forax, jlahoda, sspitsyn, abuckley Contributed-by: alex.buckley@oracle.com, maurizio.mimadamore@oracle.com, mandy.chung@oracle.com, tobias.hartmann@oracle.com, david.holmes@oracle.com, vladimir.x.ivanov@oracle.com, karen.kinnear@oracle.com, vladimir.kozlov@oracle.com, john.r.rose@oracle.com, daniel.smith@oracle.com, serguei.spitsyn@oracle.com, kumardotsrinivasan@gmail.com, boris.ulasevich@bell-sw.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
     2
 * Copyright (c) 2003, 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: 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
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    <jvmti.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include    "JPLISAssert.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include    "Utilities.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include    "JavaExceptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * This module contains utility routines for manipulating Java throwables
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and JNIEnv throwable state from native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
static jthrowable   sFallbackInternalError  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * Local forward declarations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/* insist on having a throwable. If we already have one, return it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * If not, map to fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
forceFallback(jthrowable potentialException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
forceFallback(jthrowable potentialException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    if ( potentialException == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return sFallbackInternalError;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        return potentialException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *  Returns true if it properly sets up a fallback exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
initializeFallbackError(JNIEnv* jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    sFallbackInternalError = createInternalError(jnienv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return (sFallbackInternalError != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *  Map everything to InternalError.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
mapAllCheckedToInternalErrorMapper( JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                    jthrowable  throwableToMap) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jthrowable  mappedThrowable = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jstring     message         = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    jplis_assert(throwableToMap != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    jplis_assert(!isUnchecked(jnienv, throwableToMap));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    message = getMessageFromThrowable(jnienv, throwableToMap);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    mappedThrowable = createInternalError(jnienv, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    return mappedThrowable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
checkForThrowable(  JNIEnv*     jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    return (*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
isSafeForJNICalls(  JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    return !(*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
logThrowable(   JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if ( checkForThrowable(jnienv) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        (*jnienv)->ExceptionDescribe(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 *  Creates an exception or error with the fully qualified classname (ie java/lang/Error)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *  and message passed to its constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
createThrowable(    JNIEnv *        jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    const char *    className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                    jstring         message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    jthrowable  exception           = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    jmethodID   constructor         = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    jclass      exceptionClass      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    jboolean    errorOutstanding    = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    jplis_assert(className != NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    /* create new VMError with message from exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    exceptionClass = (*jnienv)->FindClass(jnienv, className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        constructor = (*jnienv)->GetMethodID(   jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                                                exceptionClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                                                "<init>",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                                                "(Ljava/lang/String;)V");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        exception = (*jnienv)->NewObject(jnienv, exceptionClass, constructor, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
createInternalError(JNIEnv * jnienv, jstring message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    return createThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                            "java/lang/InternalError",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                            message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
createThrowableFromJVMTIErrorCode(JNIEnv * jnienv, jvmtiError errorCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    const char * throwableClassName = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    const char * message            = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    jstring messageString           = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    switch ( errorCode ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        case JVMTI_ERROR_NULL_POINTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                throwableClassName = "java/lang/NullPointerException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        case JVMTI_ERROR_ILLEGAL_ARGUMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                throwableClassName = "java/lang/IllegalArgumentException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        case JVMTI_ERROR_OUT_OF_MEMORY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                throwableClassName = "java/lang/OutOfMemoryError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        case JVMTI_ERROR_CIRCULAR_CLASS_DEFINITION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                throwableClassName = "java/lang/ClassCircularityError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        case JVMTI_ERROR_FAILS_VERIFICATION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                throwableClassName = "java/lang/VerifyError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_ADDED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                message = "class redefinition failed: attempted to add a method";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_SCHEMA_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                message = "class redefinition failed: attempted to change the schema (add/remove fields)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_HIERARCHY_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                message = "class redefinition failed: attempted to change superclass or interfaces";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_DELETED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                message = "class redefinition failed: attempted to delete a method";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_MODIFIERS_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                message = "class redefinition failed: attempted to change the class modifiers";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
50735
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   217
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_CLASS_ATTRIBUTE_CHANGED:
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   218
                throwableClassName = "java/lang/UnsupportedOperationException";
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   219
                message = "class redefinition failed: attempted to change the class NestHost or NestMembers attribute";
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   220
                break;
2f2af62dfac7 8010319: Implementation of JEP 181: Nest-Based Access Control
dholmes
parents: 47216
diff changeset
   221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        case JVMTI_ERROR_UNSUPPORTED_REDEFINITION_METHOD_MODIFIERS_CHANGED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                message = "class redefinition failed: attempted to change method modifiers";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        case JVMTI_ERROR_UNSUPPORTED_VERSION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                throwableClassName = "java/lang/UnsupportedClassVersionError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        case JVMTI_ERROR_NAMES_DONT_MATCH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                throwableClassName = "java/lang/NoClassDefFoundError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                message = "class names don't match";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        case JVMTI_ERROR_INVALID_CLASS_FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                throwableClassName = "java/lang/ClassFormatError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        case JVMTI_ERROR_UNMODIFIABLE_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                throwableClassName = "java/lang/instrument/UnmodifiableClassException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        case JVMTI_ERROR_INVALID_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                throwableClassName = "java/lang/InternalError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                message = "class redefinition failed: invalid class";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        case JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                throwableClassName = "java/lang/UnsupportedOperationException";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                message = "unsupported operation";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        case JVMTI_ERROR_INTERNAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                throwableClassName = "java/lang/InternalError";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    if ( message != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        jboolean errorOutstanding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        messageString = (*jnienv)->NewStringUTF(jnienv, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        jplis_assert_msg(!errorOutstanding, "can't create exception java string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    return createThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                            throwableClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                            messageString);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *  Calls toString() on the given message which is the same call made by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *  Exception when passed a throwable to its constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
jstring
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
getMessageFromThrowable(    JNIEnv*     jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                            jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    jclass      exceptionClass      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    jmethodID   method              = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    jstring     message             = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    jboolean    errorOutstanding    = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    /* call getMessage on exception */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    exceptionClass = (*jnienv)->GetObjectClass(jnienv, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        method = (*jnienv)->GetMethodID(jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                                        exceptionClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                                        "toString",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                        "()Ljava/lang/String;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    if (!errorOutstanding) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        message = (*jnienv)->CallObjectMethod(jnienv, exception, method);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        errorOutstanding = checkForAndClearThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        jplis_assert(!errorOutstanding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    return message;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 *  Returns whether the exception given is an unchecked exception:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 *  a subclass of Error or RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
isUnchecked(    JNIEnv*     jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    jboolean result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    result =    (exception == NULL) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                isInstanceofClassName(jnienv, exception, "java/lang/Error") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                isInstanceofClassName(jnienv, exception, "java/lang/RuntimeException");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    jplis_assert(isSafeForJNICalls(jnienv));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 *  Returns the current throwable, if any. Clears the throwable state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *  Clients can use this to preserve the current throwable state on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
jthrowable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
preserveThrowable(JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    jthrowable result = (*jnienv)->ExceptionOccurred(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    if ( result != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        (*jnienv)->ExceptionClear(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 *  Installs the supplied throwable into the JNIEnv if the throwable is not null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 *  Clients can use this to preserve the current throwable state on the stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
restoreThrowable(   JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    jthrowable  preservedException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    throwThrowable( jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    preservedException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
throwThrowable(     JNIEnv *    jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    jthrowable  exception) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    if ( exception != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        jint result = (*jnienv)->Throw(jnienv, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        jplis_assert_msg(result == JNI_OK, "throwThrowable failed to re-throw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 *  Always clears the JNIEnv throwable state. Returns true if an exception was present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 *  before the clearing operation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
checkForAndClearThrowable(  JNIEnv *    jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    jboolean result = (*jnienv)->ExceptionCheck(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    if ( result ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        (*jnienv)->ExceptionClear(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
/* creates a java.lang.InternalError and installs it into the JNIEnv */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
createAndThrowInternalError(JNIEnv * jnienv) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    jthrowable internalError = createInternalError( jnienv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    throwThrowable(jnienv, forceFallback(internalError));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
createAndThrowThrowableFromJVMTIErrorCode(JNIEnv * jnienv, jvmtiError errorCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    jthrowable throwable = createThrowableFromJVMTIErrorCode(jnienv, errorCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    throwThrowable(jnienv, forceFallback(throwable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
mapThrownThrowableIfNecessary(  JNIEnv *                jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                                CheckedExceptionMapper  mapper) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    jthrowable  originalThrowable   = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    jthrowable  resultThrowable     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    originalThrowable = preserveThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    /* the throwable is now cleared, so JNI calls are safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    if ( originalThrowable != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        /* if there is an exception: we can just throw it if it is unchecked. If checked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         * we need to map it (mapper is conditional, will vary by usage, hence the callback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        if ( isUnchecked(jnienv, originalThrowable) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            resultThrowable = originalThrowable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            resultThrowable = (*mapper) (jnienv, originalThrowable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    /* re-establish the correct throwable */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    if ( resultThrowable != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        throwThrowable(jnienv, forceFallback(resultThrowable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
}