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