src/java.instrument/share/native/libinstrument/Reentrancy.c
author rwestberg
Fri, 01 Jun 2018 10:15:48 +0200
changeset 50336 1b6ea6bcd21a
parent 49566 7c224ec572d0
permissions -rw-r--r--
8203237: JFR TestBiasedLockRevocationEvents should ignore events unrelated to the test Reviewed-by: egahlin, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2003, 2008, 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: 715
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: 715
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: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
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    "Reentrancy.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include    "JPLISAgent.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 provides some utility functions to support the "same thread" re-entrancy management.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *  Uses JVMTI TLS to store a single bit per thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *  Non-zero means the thread is already inside; zero means the thread is not inside.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *  Local prototypes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* Wrapper around set that does the set then re-fetches to make sure it worked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Degenerates to a simple set when assertions are disabled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * This routine is only here because of a bug in the JVMTI where set to 0 fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
confirmingTLSSet(   jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                    jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                    const void *    newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
/* Confirmation routine only; used to assure that the TLS slot holds the value we expect it to. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
assertTLSValue( jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                const void *    expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#define JPLIS_CURRENTLY_INSIDE_TOKEN                ((void *) 0x7EFFC0BB)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
#define JPLIS_CURRENTLY_OUTSIDE_TOKEN               ((void *) 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
jvmtiError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
confirmingTLSSet(   jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                    jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                    const void *    newValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    jvmtiError  error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    error = (*jvmtienv)->SetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                    jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                                    newValue);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
    77
    check_phase_ret_blob(error, error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    newValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    return error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
assertTLSValue( jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                jthread         thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                const void *    expected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    jvmtiError  error;
49566
7c224ec572d0 8201267: Disable warnings for VS2017 to enable building
erikj
parents: 49279
diff changeset
    93
    void *      test = (void *) 0x99999999ULL;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /* now check if we do a fetch we get what we wrote */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    error = (*jvmtienv)->GetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                                jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                                &test);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   100
    check_phase_ret(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    jplis_assert(test == expected);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
tryToAcquireReentrancyToken(    jvmtiEnv *  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                jthread     thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    jboolean    result      = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    jvmtiError  error       = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    void *      storedValue = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    error = (*jvmtienv)->GetThreadLocalStorage(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                                jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                &storedValue);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   116
    check_phase_ret_false(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    if ( error == JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* if this thread is already inside, just return false and short-circuit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if ( storedValue == JPLIS_CURRENTLY_INSIDE_TOKEN ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /* stuff in the sentinel and return true */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                            thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                            JPLIS_CURRENTLY_OUTSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            error = confirmingTLSSet (  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                                        thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                        JPLIS_CURRENTLY_INSIDE_TOKEN);
22610
f86a781eb0cd 7142035: assert in j.l.instrument agents during shutdown when daemon thread is running
sla
parents: 5506
diff changeset
   133
            check_phase_ret_false(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if ( error != JVMTI_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                result = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                result = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
releaseReentrancyToken(         jvmtiEnv *  jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                                jthread     thread)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    jvmtiError  error       = JVMTI_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
/* assert we hold the token */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
#if JPLISASSERT_ENABLEASSERTIONS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    assertTLSValue( jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    JPLIS_CURRENTLY_INSIDE_TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    error = confirmingTLSSet(   jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                                thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                JPLIS_CURRENTLY_OUTSIDE_TOKEN);
22610
f86a781eb0cd 7142035: assert in j.l.instrument agents during shutdown when daemon thread is running
sla
parents: 5506
diff changeset
   162
    check_phase_ret(error);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    jplis_assert(error == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
}