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(NSK_CPP_STUB3(GetSystemProperties, 81 jvmti_env, &count, &propKeys))) { 82 result = STATUS_FAILED; 83 return; 84 } 85 NSK_DISPLAY1("%d keys obtained\n", 86 count); 87 88 if (count < PROP_NUM) { 89 result = STATUS_FAILED; 90 NSK_COMPLAIN2("TEST FAILED: GetSystemProperties() returns %d system property keys\n\texpected at least %d", 91 count, PROP_NUM); 92 } 93 94 for (i=0; i< count; i++) { 95 NSK_DISPLAY2("%d) getting property for the key \"%s\":\n", 96 i+1, propKeys[i]); 97 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetSystemProperty, 98 jvmti_env, (const char*) propKeys[i], &prop))) { 99 result = STATUS_FAILED; 100 return; 101 } 102 NSK_DISPLAY1("\tproperty=\"%s\"\n", 103 prop); 104 105 foundProps += findProp(propKeys[i]); 106 107 NSK_DISPLAY0("\tdeallocating system property\n"); 108 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 109 jvmti_env, (unsigned char*) prop))) { 110 result = STATUS_FAILED; 111 return; 112 } 113 114 NSK_DISPLAY0("\tdeallocating the system property key\n\n"); 115 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 116 jvmti_env, (unsigned char*) propKeys[i]))) { 117 result = STATUS_FAILED; 118 return; 119 } 120 } 121 122 /* NSK_DISPLAY0("Deallocating the property key array ...\n"); 123 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate, 124 jvmti_env, (unsigned char*) &propKeys))) { 125 result = STATUS_FAILED; 126 return; 127 }*/ 128 129 if (foundProps != PROP_NUM) { 130 result = STATUS_FAILED; 131 NSK_COMPLAIN2("TEST FAILED: only %d highly recommended system properties found\n\tinstead of %d as expected\n", 132 foundProps, PROP_NUM); 133 } 134 } 135 136 /** callback functions **/ 137 void JNICALL 138 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) { 139 NSK_DISPLAY0("VMInit event received\n"); 140 141 checkProps(jvmti_env, ">>> b) TEST CASE \"VMInit\""); 142 } 143 144 void JNICALL 145 VMDeath(jvmtiEnv *jvmti_env, JNIEnv *env) { 146 NSK_DISPLAY0("VMDeath event received\n"); 147 148 checkProps(jvmti_env, ">>> c) TEST CASE \"VMDeath\""); 149 150 if (result == STATUS_FAILED) 151 exit(STATUS_FAILED); 152 } 153 /************************/ 154 155 JNIEXPORT jint JNICALL 156 Java_nsk_jvmti_scenarios_general_1functions_GF01_gf01t001_check(JNIEnv *env, jobject obj) { 157 return result; 158 } 159 160 #ifdef STATIC_BUILD 161 JNIEXPORT jint JNICALL Agent_OnLoad_gf01t001(JavaVM *jvm, char *options, void *reserved) { 162 return Agent_Initialize(jvm, options, reserved); 163 } 164 JNIEXPORT jint JNICALL Agent_OnAttach_gf01t001(JavaVM *jvm, char *options, void *reserved) { 165 return Agent_Initialize(jvm, options, reserved); 166 } 167 JNIEXPORT jint JNI_OnLoad_gf01t001(JavaVM *jvm, char *options, void *reserved) { 168 return JNI_VERSION_1_8; 169 } 170 #endif 171 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 172 jvmtiEnv *jvmti; 173 174 /* init framework and parse options */ 175 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) 176 return JNI_ERR; 177 178 /* create JVMTI environment */ 179 if (!NSK_VERIFY((jvmti = 180 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) 181 return JNI_ERR; 182 183 NSK_DISPLAY0("setting event callbacks ...\n"); 184 185 (void) memset(&callbacks, 0, sizeof(callbacks)); 186 callbacks.VMInit = &VMInit; 187 callbacks.VMDeath = &VMDeath; 188 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks, 189 jvmti, &callbacks, sizeof(callbacks)))) 190 return JNI_ERR; 191 192 NSK_DISPLAY0("setting event callbacks done\nenabling events ...\n"); 193 194 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 195 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL))) 196 return JNI_ERR; 197 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode, 198 jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL))) 199 return JNI_ERR; 200 201 NSK_DISPLAY0("enabling events done\n\n"); 202 203 checkProps(jvmti, ">>> a) TEST CASE \"OnLoad\""); 204 205 return JNI_OK; 206 } 207 208 }