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 <stdio.h> 25 #include <stdarg.h> 26 #include <stdlib.h> 27 #include <string.h> 28 29 #include <jvmti.h> 30 #include "agent_common.h" 31 32 #include "nsk_tools.h" 33 #include "JVMTITools.h" 34 #include "jvmti_tools.h" 35 36 extern "C" { 37 38 #define PASSED 0 39 #define STATUS_FAILED 2 40 41 /* the highly recommended system properties are below */ 42 #define PROP_NUM 6 43 static const char *expected_props[PROP_NUM] = { 44 "java.vm.vendor", 45 "java.vm.version", 46 "java.vm.name", 47 "java.vm.info", 48 "java.library.path", 49 "java.class.path" 50 }; 51 52 static jvmtiEventCallbacks callbacks; 53 static jint result = PASSED; 54 55 static int findProp(char *prop) { 56 int i; 57 58 for (i=0; i<PROP_NUM; i++) { 59 if (strcmp(expected_props[i], prop) == 0) { 60 NSK_DISPLAY1("CHECK PASSED: found highly recommended system property \"%s\" as expected\n", 61 expected_props[i]); 62 return 1; /* the property found */ 63 } 64 } 65 66 NSK_DISPLAY1("\tsystem property \"%s\" not found among highly recommended ones\n", 67 prop); 68 return 0; /* the property not found */ 69 } 70 71 static void checkProps(jvmtiEnv *jvmti_env, const char *stepMsg) { 72 jint count; 73 char **propKeys; 74 char *prop; 75 int i; 76 int foundProps = 0; 77 78 NSK_DISPLAY1("%s: Getting system property keys ...\n", 79 stepMsg); 80 if (!NSK_JVMTI_VERIFY(jvmti_env->GetSystemProperties(&count, &propKeys))) { 81 result = STATUS_FAILED; 82 return; 83 } 84 NSK_DISPLAY1("%d keys obtained\n", 85 count); 86 87 if (count < PROP_NUM) { 88 result = STATUS_FAILED; 89 NSK_COMPLAIN2("TEST FAILED: GetSystemProperties() returns %d system property keys\n\texpected at least %d", 90 count, PROP_NUM); 91 } 92 93 for (i=0; i< count; i++) { 94 NSK_DISPLAY2("%d) getting property for the key \"%s\":\n", 95 i+1, propKeys[i]); 96 if (!NSK_JVMTI_VERIFY(jvmti_env->GetSystemProperty((const char*) propKeys[i], &prop))) { 97 result = STATUS_FAILED; 98 return; 99 } 100 NSK_DISPLAY1("\tproperty=\"%s\"\n", 101 prop); 102 103 foundProps += findProp(propKeys[i]); 104 105 NSK_DISPLAY0("\tdeallocating system property\n"); 106 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) prop))) { 107 result = STATUS_FAILED; 108 return; 109 } 110 111 NSK_DISPLAY0("\tdeallocating the system property key\n\n"); 112 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) propKeys[i]))) { 113 result = STATUS_FAILED; 114 return; 115 } 116 } 117 118 /* NSK_DISPLAY0("Deallocating the property key array ...\n"); 119 if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*) &propKeys))) { 120 result = STATUS_FAILED; 121 return; 122 }*/ 123 124 if (foundProps != PROP_NUM) { 125 result = STATUS_FAILED; 126 NSK_COMPLAIN2("TEST FAILED: only %d highly recommended system properties found\n\tinstead of %d as expected\n", 127 foundProps, PROP_NUM); 128 } 129 } 130 131 /** callback functions **/ 132 void JNICALL 133 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) { 134 NSK_DISPLAY0("VMInit event received\n"); 135 136 checkProps(jvmti_env, ">>> b) TEST CASE \"VMInit\""); 137 } 138 139 void JNICALL 140 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 141 NSK_DISPLAY0("VMDeath event received\n"); 142 143 checkProps(jvmti_env, ">>> c) TEST CASE \"VMDeath\""); 144 145 if (result == STATUS_FAILED) 146 exit(STATUS_FAILED); 147 } 148 /************************/ 149 150 JNIEXPORT jint JNICALL 151 Java_nsk_jvmti_scenarios_general_1functions_GF01_gf01t001_check(JNIEnv *env, jobject obj) { 152 return result; 153 } 154 155 #ifdef STATIC_BUILD 156 JNIEXPORT jint JNICALL Agent_OnLoad_gf01t001(JavaVM *jvm, char *options, void *reserved) { 157 return Agent_Initialize(jvm, options, reserved); 158 } 159 JNIEXPORT jint JNICALL Agent_OnAttach_gf01t001(JavaVM *jvm, char *options, void *reserved) { 160 return Agent_Initialize(jvm, options, reserved); 161 } 162 JNIEXPORT jint JNI_OnLoad_gf01t001(JavaVM *jvm, char *options, void *reserved) { 163 return JNI_VERSION_1_8; 164 } 165 #endif 166 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 167 jvmtiEnv *jvmti; 168 169 /* init framework and parse options */ 170 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 171 return JNI_ERR; 172 173 /* create JVMTI environment */ 174 if (!NSK_VERIFY((jvmti = 175 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 176 return JNI_ERR; 177 178 NSK_DISPLAY0("setting event callbacks ...\n"); 179 180 (void) memset(&callbacks, 0, sizeof(callbacks)); 181 callbacks.VMInit = &VMInit; 182 callbacks.VMDeath = &VMDeath; 183 if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)))) 184 return JNI_ERR; 185 186 NSK_DISPLAY0("setting event callbacks done\nenabling events ...\n"); 187 188 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 189 return JNI_ERR; 190 if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) 191 return JNI_ERR; 192 193 NSK_DISPLAY0("enabling events done\n\n"); 194 195 checkProps(jvmti, ">>> a) TEST CASE \"OnLoad\""); 196 197 return JNI_OK; 198 } 199 200 }