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(jvmti->SetThreadLocalStorage(testedThread, (void*)initialStorage))) {
  77             nsk_jvmti_setFailStatus();
  78             return;
  79         }
  80 
  81         NSK_DISPLAY0("Let tested thread to run\n");
  82         if (!nsk_jvmti_resumeSync())
  83             return;
  84 
  85         NSK_DISPLAY0("Wait for thread to run\n");
  86         if (!nsk_jvmti_waitForSync(timeout))
  87             return;
  88 
  89         NSK_DISPLAY0("GetThreadLocalStorage() for tested thread\n");
  90         if (!NSK_JVMTI_VERIFY(
  91                 jvmti->GetThreadLocalStorage(testedThread, (void**)&obtainedStorage))) {
  92             nsk_jvmti_setFailStatus();
  93             return;
  94         }
  95         NSK_DISPLAY1("  ... got storage: %p\n", (void*)obtainedStorage);
  96 
  97         NSK_DISPLAY0("Check storage data obtained for tested thread\n");
  98         if (obtainedStorage != initialStorage) {
  99             NSK_COMPLAIN3("Wrong storage pointer returned for tested thread:\n"
 100                           "#   thread:      %p\n"
 101                           "#   got pointer: %p\n"
 102                           "#   expected:    %p\n",
 103                             (void*)testedThread,
 104                             (void*)obtainedStorage, (void*)initialStorage);
 105             nsk_jvmti_setFailStatus();
 106         } else {
 107             int changed = 0;
 108             int i;
 109 
 110             for (i = 0; i < STORAGE_DATA_SIZE; i++) {
 111                 if (obtainedStorage->data[i] != STORAGE_DATA_CHAR) {
 112                     changed++;
 113                 }
 114             }
 115 
 116             if (changed > 0) {
 117                 NSK_COMPLAIN3("Data changed in returned storage for tested thread:\n"
 118                           "#   thread:        %p\n"
 119                           "#   changed bytes: %d\n"
 120                           "#   total bytes:   %d\n",
 121                             (void*)testedThread,
 122                             changed, STORAGE_DATA_SIZE);
 123                 nsk_jvmti_setFailStatus();
 124             }
 125         }
 126 
 127         NSK_DISPLAY0("Delete thread reference\n");
 128         NSK_TRACE(jni->DeleteGlobalRef(testedThread));
 129     }
 130 
 131     NSK_DISPLAY0("Let debugee to finish\n");
 132     if (!nsk_jvmti_resumeSync())
 133         return;
 134 }
 135 
 136 /* ============================================================================= */
 137 
 138 /** Agent library initialization. */
 139 #ifdef STATIC_BUILD
 140 JNIEXPORT jint JNICALL Agent_OnLoad_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 141     return Agent_Initialize(jvm, options, reserved);
 142 }
 143 JNIEXPORT jint JNICALL Agent_OnAttach_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 144     return Agent_Initialize(jvm, options, reserved);
 145 }
 146 JNIEXPORT jint JNI_OnLoad_setthrdstor001(JavaVM *jvm, char *options, void *reserved) {
 147     return JNI_VERSION_1_8;
 148 }
 149 #endif
 150 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 151     jvmtiEnv* jvmti = NULL;
 152 
 153     /* init framework and parse options */
 154     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 155         return JNI_ERR;
 156 
 157     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 158 
 159     /* create JVMTI environment */
 160     if (!NSK_VERIFY((jvmti =
 161             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 162         return JNI_ERR;
 163 
 164     /* register agent proc and arg */
 165     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 166         return JNI_ERR;
 167 
 168     return JNI_OK;
 169 }
 170 
 171 /* ============================================================================= */
 172 
 173 }