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         StorageStructure storageData;
  62         StorageStructure* initialStorage = &storageData;
  63         StorageStructure* obtainedStorage = NULL;
  64 
  65         memset(storageData.data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE);
  66 
  67         NSK_DISPLAY1("SetThreadLocalStorage() for current agent thread with pointer: %p\n",
  68                                                     (void*)initialStorage);
  69         if (!NSK_JVMTI_VERIFY(jvmti->SetThreadLocalStorage(NULL, (void*)initialStorage))) {
  70             nsk_jvmti_setFailStatus();
  71             return;
  72         }
  73 
  74         NSK_DISPLAY0("Let debuggee to run\n");
  75         if (!nsk_jvmti_resumeSync())
  76             return;
  77 
  78         NSK_DISPLAY0("Wait for debuggee to run\n");
  79         if (!nsk_jvmti_waitForSync(timeout))
  80             return;
  81 
  82         NSK_DISPLAY0("GetThreadLocalStorage() for current agent thread\n");
  83         if (!NSK_JVMTI_VERIFY(jvmti->GetThreadLocalStorage(NULL, (void**)&obtainedStorage))) {
  84             nsk_jvmti_setFailStatus();
  85             return;
  86         }
  87         NSK_DISPLAY1("  ... got storage: %p\n", (void*)obtainedStorage);
  88 
  89         NSK_DISPLAY0("Check storage data obtained for current agent thread\n");
  90         if (obtainedStorage != initialStorage) {
  91             NSK_COMPLAIN2("Wrong storage pointer returned for tested thread:\n"
  92                           "#   got pointer: %p\n"
  93                           "#   expected:    %p\n",
  94                             (void*)obtainedStorage, (void*)initialStorage);
  95             nsk_jvmti_setFailStatus();
  96         } else {
  97             int changed = 0;
  98             int i;
  99 
 100             for (i = 0; i < STORAGE_DATA_SIZE; i++) {
 101                 if (obtainedStorage->data[i] != STORAGE_DATA_CHAR) {
 102                     changed++;
 103                 }
 104             }
 105 
 106             if (changed > 0) {
 107                 NSK_COMPLAIN2("Data changed in returned storage for current agent thread:\n"
 108                           "#   changed bytes: %d\n"
 109                           "#   total bytes:   %d\n",
 110                             changed, STORAGE_DATA_SIZE);
 111                 nsk_jvmti_setFailStatus();
 112             }
 113         }
 114     }
 115 
 116     NSK_DISPLAY0("Let debugee to finish\n");
 117     if (!nsk_jvmti_resumeSync())
 118         return;
 119 }
 120 
 121 /* ============================================================================= */
 122 
 123 /** Agent library initialization. */
 124 #ifdef STATIC_BUILD
 125 JNIEXPORT jint JNICALL Agent_OnLoad_setthrdstor002(JavaVM *jvm, char *options, void *reserved) {
 126     return Agent_Initialize(jvm, options, reserved);
 127 }
 128 JNIEXPORT jint JNICALL Agent_OnAttach_setthrdstor002(JavaVM *jvm, char *options, void *reserved) {
 129     return Agent_Initialize(jvm, options, reserved);
 130 }
 131 JNIEXPORT jint JNI_OnLoad_setthrdstor002(JavaVM *jvm, char *options, void *reserved) {
 132     return JNI_VERSION_1_8;
 133 }
 134 #endif
 135 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 136     jvmtiEnv* jvmti = NULL;
 137 
 138     /* init framework and parse options */
 139     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 140         return JNI_ERR;
 141 
 142     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 143 
 144     /* create JVMTI environment */
 145     if (!NSK_VERIFY((jvmti =
 146             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 147         return JNI_ERR;
 148 
 149     /* register agent proc and arg */
 150     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 151         return JNI_ERR;
 152 
 153     return JNI_OK;
 154 }
 155 
 156 /* ============================================================================= */
 157 
 158 }