1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 #include <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* ============================================================================= */
  33 
  34 /* scaffold objects */
  35 static jlong timeout = 0;
  36 
  37 /* constant names */
  38 #define THREAD_NAME     "TestedThread"
  39 
  40 /* constants */
  41 #define STORAGE_DATA_SIZE       1024
  42 #define STORAGE_DATA_CHAR       'X'
  43 
  44 /* storage structure */
  45 typedef struct _StorageStructure {
  46     char data[STORAGE_DATA_SIZE];
  47 } StorageStructure;
  48 
  49 /* ============================================================================= */
  50 
  51 /** Agent algorithm. */
  52 static void JNICALL
  53 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  54 
  55     NSK_DISPLAY0("Wait for thread to start\n");
  56     if (!nsk_jvmti_waitForSync(timeout))
  57         return;
  58 
  59     /* perform testing */
  60     {
  61         jthread testedThread = NULL;
  62         StorageStructure storageData;
  63         StorageStructure* initialStorage = &storageData;
  64         StorageStructure* obtainedStorage = NULL;
  65 
  66         memset(storageData.data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE);
  67 
  68         NSK_DISPLAY1("Find thread: %s\n", THREAD_NAME);
  69         if (!NSK_VERIFY((testedThread =
  70                 nsk_jvmti_threadByName(THREAD_NAME)) != NULL))
  71             return;
  72         NSK_DISPLAY1("  ... found thread: %p\n", (void*)testedThread);
  73 
  74         NSK_DISPLAY1("SetThreadLocalStorage() for tested thread with pointer: %p\n",
  75                                                     (void*)initialStorage);
  76         if (!NSK_JVMTI_VERIFY(
  77                 NSK_CPP_STUB3(SetThreadLocalStorage, jvmti,
  78                                         testedThread, (void*)initialStorage))) {
  79             nsk_jvmti_setFailStatus();
  80             return;
  81         }
  82 
  83         NSK_DISPLAY0("Let tested thread to run\n");
  84         if (!nsk_jvmti_resumeSync())
  85             return;
  86 
  87         NSK_DISPLAY0("Wait for thread to run\n");
  88         if (!nsk_jvmti_waitForSync(timeout))
  89             return;
  90 
  91         NSK_DISPLAY0("GetThreadLocalStorage() for tested thread\n");
  92         if (!NSK_JVMTI_VERIFY(
  93                 NSK_CPP_STUB3(GetThreadLocalStorage, jvmti,
  94                                         testedThread, (void**)&obtainedStorage))) {
  95             nsk_jvmti_setFailStatus();
  96             return;
  97         }
  98         NSK_DISPLAY1("  ... got storage: %p\n", (void*)obtainedStorage);
  99 
 100         NSK_DISPLAY0("Check storage data obtained for tested thread\n");
 101         if (obtainedStorage != initialStorage) {
 102             NSK_COMPLAIN3("Wrong storage pointer returned for tested thread:\n"
 103                           "#   thread:      %p\n"
 104                           "#   got pointer: %p\n"
 105                           "#   expected:    %p\n",
 106                             (void*)testedThread,
 107                             (void*)obtainedStorage, (void*)initialStorage);
 108             nsk_jvmti_setFailStatus();
 109         } else {
 110             int changed = 0;
 111             int i;
 112 
 113             for (i = 0; i < STORAGE_DATA_SIZE; i++) {
 114                 if (obtainedStorage->data[i] != STORAGE_DATA_CHAR) {
 115                     changed++;
 116                 }
 117             }
 118 
 119             if (changed > 0) {
 120                 NSK_COMPLAIN3("Data changed in returned storage for tested thread:\n"
 121                           "#   thread:        %p\n"
 122                           "#   changed bytes: %d\n"
 123                           "#   total bytes:   %d\n",
 124                             (void*)testedThread,
 125                             changed, STORAGE_DATA_SIZE);
 126                 nsk_jvmti_setFailStatus();
 127             }
 128         }
 129 
 130         NSK_DISPLAY0("Delete thread reference\n");
 131         NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedThread));
 132     }
 133 
 134     NSK_DISPLAY0("Let debugee to finish\n");
 135     if (!nsk_jvmti_resumeSync())
 136         return;
 137 }
 138 
 139 /* ============================================================================= */
 140 
 141 /** Agent library initialization. */
 142 #ifdef STATIC_BUILD
 143 JNIEXPORT jint JNICALL Agent_OnLoad_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 144     return Agent_Initialize(jvm, options, reserved);
 145 }
 146 JNIEXPORT jint JNICALL Agent_OnAttach_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 147     return Agent_Initialize(jvm, options, reserved);
 148 }
 149 JNIEXPORT jint JNI_OnLoad_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 150     return JNI_VERSION_1_8;
 151 }
 152 #endif
 153 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 154     jvmtiEnv* jvmti = NULL;
 155 
 156     /* init framework and parse options */
 157     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 158         return JNI_ERR;
 159 
 160     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 161 
 162     /* create JVMTI environment */
 163     if (!NSK_VERIFY((jvmti =
 164             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 165         return JNI_ERR;
 166 
 167     /* register agent proc and arg */
 168     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 169         return JNI_ERR;
 170 
 171     return JNI_OK;
 172 }
 173 
 174 /* ============================================================================= */
 175 
 176 }