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 static jlong timeout = 0;
  35 
  36 #define PROPERTIES_COUNT  3
  37 #define STEPS_COUNT       3
  38 
  39 typedef struct PropertyDescStruct {
  40     const char* name;
  41     const char* values[STEPS_COUNT];
  42 } PropertyDesc;
  43 
  44 static PropertyDesc propDescList[PROPERTIES_COUNT] = {
  45     {
  46         "nsk.jvmti.test.property",
  47         {
  48             "initial value of nsk.jvmti.test.property",
  49             "OnLoad phase value of nsk.jvmti.test.property",
  50             "live phase value of nsk.jvmti.test.property"
  51         }
  52     },
  53     {
  54         "nsk.jvmti.test.property.empty.old",
  55         {
  56             "",
  57             "OnLoad phase value of nsk.jvmti.test.property.empty.old",
  58             ""
  59         }
  60     },
  61     {
  62         "nsk.jvmti.test.property.empty.new",
  63         {
  64             "initial value of nsk.jvmti.test.property.empty.new",
  65             "",
  66             "live phase value of nsk.jvmti.test.property.empty.new"
  67         }
  68     }
  69 };
  70 
  71 /* ============================================================================= */
  72 
  73 static int checkPropertyValue(jvmtiEnv* jvmti, const char phase[],
  74                                 const char name[], const char* expectedValue) {
  75     int success = NSK_TRUE;
  76     char* value = NULL;
  77 
  78     NSK_DISPLAY1("  property: %s\n", name);
  79     if (!NSK_JVMTI_VERIFY(jvmti->GetSystemProperty(name, &value))) {
  80         return NSK_FALSE;
  81     }
  82     NSK_DISPLAY1("     value: \"%s\"\n", nsk_null_string(value));
  83 
  84     if (value == NULL
  85             || strcmp(value, expectedValue) != 0) {
  86         NSK_COMPLAIN4("In %s phase GetSystemProperty() returned unexpected value for property:\n"
  87                       "#   property name: %s\n"
  88                       "#   got value:     \"%s\"\n"
  89                       "#   expected:      \"%s\"\n",
  90                         phase, name,
  91                         nsk_null_string(value), expectedValue);
  92         success = NSK_FALSE;
  93     }
  94 
  95     if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*)value))) {
  96         success = NSK_FALSE;
  97     }
  98 
  99     return success;
 100 }
 101 
 102 static int checkProperties(jvmtiEnv* jvmti, const char phase[], int step) {
 103     int success = NSK_TRUE;
 104     int i;
 105 
 106     NSK_DISPLAY0("Check previously set values of tested properties\n");
 107     for (i = 0; i < PROPERTIES_COUNT; i++) {
 108         if (!checkPropertyValue(jvmti, phase,
 109                             propDescList[i].name, propDescList[i].values[step-1])) {
 110             success = NSK_FALSE;
 111         }
 112     }
 113 
 114     NSK_DISPLAY1("Set new values for tested properties %s\n",
 115         (step > 1) ? "(negative)" : "");
 116     for (i = 0; i < PROPERTIES_COUNT; i++) {
 117         NSK_DISPLAY1("  property: %s\n", propDescList[i].name);
 118         NSK_DISPLAY1("     value: \"%s\"\n", propDescList[i].values[step]);
 119         if (step > 1) {
 120             if (!NSK_JVMTI_VERIFY_CODE(JVMTI_ERROR_WRONG_PHASE,
 121                    jvmti->SetSystemProperty(propDescList[i].name, propDescList[i].values[step]))) {
 122                 success = NSK_FALSE;
 123             }
 124         } else {
 125             if (!NSK_JVMTI_VERIFY(
 126                    jvmti->SetSystemProperty(propDescList[i].name, propDescList[i].values[step]))) {
 127                 success = NSK_FALSE;
 128             }
 129         }
 130     }
 131 
 132     NSK_DISPLAY0("Check newly set values of tested properties\n");
 133     for (i = 0; i < PROPERTIES_COUNT; i++) {
 134         if (!checkPropertyValue(jvmti, phase, propDescList[i].name,
 135                 propDescList[i].values[1])) {
 136             success = NSK_FALSE;
 137         }
 138     }
 139 
 140     return success;
 141 }
 142 
 143 /* ============================================================================= */
 144 
 145 /** Agent algorithm. */
 146 static void JNICALL
 147 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
 148     NSK_DISPLAY0("Wait for debugee to become ready\n");
 149     if (!nsk_jvmti_waitForSync(timeout))
 150         return;
 151 
 152     NSK_DISPLAY0(">>> Check setting defined system properties in live phase\n");
 153     if (!checkProperties(jvmti, "live", 2)) {
 154         nsk_jvmti_setFailStatus();
 155     }
 156 
 157     NSK_DISPLAY0("Let debugee to finish\n");
 158     if (!nsk_jvmti_resumeSync())
 159         return;
 160 }
 161 
 162 /* ============================================================================= */
 163 
 164 /** Agent library initialization. */
 165 #ifdef STATIC_BUILD
 166 JNIEXPORT jint JNICALL Agent_OnLoad_setsysprop002(JavaVM *jvm, char *options, void *reserved) {
 167     return Agent_Initialize(jvm, options, reserved);
 168 }
 169 JNIEXPORT jint JNICALL Agent_OnAttach_setsysprop002(JavaVM *jvm, char *options, void *reserved) {
 170     return Agent_Initialize(jvm, options, reserved);
 171 }
 172 JNIEXPORT jint JNI_OnLoad_setsysprop002(JavaVM *jvm, char *options, void *reserved) {
 173     return JNI_VERSION_1_8;
 174 }
 175 #endif
 176 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 177     jvmtiEnv* jvmti = NULL;
 178 
 179     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 180         return JNI_ERR;
 181 
 182     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 183 
 184     if (!NSK_VERIFY((jvmti =
 185             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 186         return JNI_ERR;
 187 
 188     NSK_DISPLAY0(">>> Check setting defined system properties in OnLoad phase\n");
 189     if (!checkProperties(jvmti, "OnLoad", 1)) {
 190         nsk_jvmti_setFailStatus();
 191     }
 192 
 193     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 194         return JNI_ERR;
 195 
 196     return JNI_OK;
 197 }
 198 
 199 /* ============================================================================= */
 200 
 201 }