1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/runtime/RedefineTests/libRedefineDoubleDelete.c Thu Sep 12 15:15:22 2019 -0400 1.3 @@ -0,0 +1,164 @@ 1.4 +/* 1.5 + * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +#include <stdio.h> 1.28 +#include <string.h> 1.29 +#include "jvmti.h" 1.30 + 1.31 +#ifdef __cplusplus 1.32 +extern "C" { 1.33 +#endif 1.34 + 1.35 +#ifndef JNI_ENV_ARG 1.36 + 1.37 +#ifdef __cplusplus 1.38 +#define JNI_ENV_ARG(x, y) y 1.39 +#define JNI_ENV_PTR(x) x 1.40 +#else 1.41 +#define JNI_ENV_ARG(x,y) x, y 1.42 +#define JNI_ENV_PTR(x) (*x) 1.43 +#endif 1.44 + 1.45 +#endif 1.46 + 1.47 +#define TranslateError(err) "JVMTI error" 1.48 + 1.49 +static jvmtiEnv *jvmti = NULL; 1.50 + 1.51 +static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved); 1.52 + 1.53 +JNIEXPORT 1.54 +jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) { 1.55 + return Agent_Initialize(jvm, options, reserved); 1.56 +} 1.57 + 1.58 +JNIEXPORT 1.59 +jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) { 1.60 + return Agent_Initialize(jvm, options, reserved); 1.61 +} 1.62 + 1.63 +JNIEXPORT 1.64 +jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) { 1.65 + return JNI_VERSION_1_8; 1.66 +} 1.67 + 1.68 + 1.69 +static jint newClassDataLen = 0; 1.70 +static unsigned char* newClassData = NULL; 1.71 + 1.72 +static jint 1.73 +getBytecodes(jvmtiEnv *jvmti_env, 1.74 + jint class_data_len, const unsigned char* class_data) { 1.75 + int i; 1.76 + jint res; 1.77 + 1.78 + newClassDataLen = class_data_len; 1.79 + res = (*jvmti_env)->Allocate(jvmti_env, newClassDataLen, &newClassData); 1.80 + if (res != JNI_OK) { 1.81 + printf(" Unable to allocate bytes\n"); 1.82 + return JNI_ERR; 1.83 + } 1.84 + for (i = 0; i < newClassDataLen; i++) { 1.85 + newClassData[i] = class_data[i]; 1.86 + // Rewrite oo in class to aa 1.87 + if (i > 0 && class_data[i] == 'o' && class_data[i-1] == 'o') { 1.88 + newClassData[i] = newClassData[i-1] = 'a'; 1.89 + } 1.90 + } 1.91 + printf(" ... copied bytecode: %d bytes\n", (int)newClassDataLen); 1.92 + return JNI_OK; 1.93 +} 1.94 + 1.95 + 1.96 +static void JNICALL 1.97 +Callback_ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, 1.98 + jclass class_being_redefined, 1.99 + jobject loader, const char* name, jobject protection_domain, 1.100 + jint class_data_len, const unsigned char* class_data, 1.101 + jint *new_class_data_len, unsigned char** new_class_data) { 1.102 + if (name != NULL && strcmp(name, "RedefineDoubleDelete$B") == 0) { 1.103 + if (newClassData == NULL) { 1.104 + jint res = getBytecodes(jvmti_env, class_data_len, class_data); 1.105 + if (res == JNI_ERR) { 1.106 + printf(">>> ClassFileLoadHook event: class name %s FAILED\n", name); 1.107 + return; 1.108 + } 1.109 + // Only change for first CFLH event. 1.110 + *new_class_data_len = newClassDataLen; 1.111 + *new_class_data = newClassData; 1.112 + } 1.113 + printf(">>> ClassFileLoadHook event: class name %s\n", name); 1.114 + } 1.115 +} 1.116 + 1.117 +static 1.118 +jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 1.119 + jint res, size; 1.120 + jvmtiCapabilities caps; 1.121 + jvmtiEventCallbacks callbacks; 1.122 + jvmtiError err; 1.123 + 1.124 + res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti), 1.125 + JVMTI_VERSION_1_2); 1.126 + if (res != JNI_OK || jvmti == NULL) { 1.127 + printf(" Error: wrong result of a valid call to GetEnv!\n"); 1.128 + return JNI_ERR; 1.129 + } 1.130 + 1.131 + printf("Enabling following capabilities: can_generate_all_class_hook_events, " 1.132 + "can_retransform_classes, can_redefine_classes"); 1.133 + memset(&caps, 0, sizeof(caps)); 1.134 + caps.can_generate_all_class_hook_events = 1; 1.135 + caps.can_retransform_classes = 1; 1.136 + caps.can_redefine_classes = 1; 1.137 + printf("\n"); 1.138 + 1.139 + err = (*jvmti)->AddCapabilities(jvmti, &caps); 1.140 + if (err != JVMTI_ERROR_NONE) { 1.141 + printf(" Error in AddCapabilites: %s (%d)\n", TranslateError(err), err); 1.142 + return JNI_ERR; 1.143 + } 1.144 + 1.145 + size = (jint)sizeof(callbacks); 1.146 + 1.147 + memset(&callbacks, 0, sizeof(callbacks)); 1.148 + callbacks.ClassFileLoadHook = Callback_ClassFileLoadHook; 1.149 + 1.150 + err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size); 1.151 + if (err != JVMTI_ERROR_NONE) { 1.152 + printf(" Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err); 1.153 + return JNI_ERR; 1.154 + } 1.155 + 1.156 + err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL); 1.157 + if (err != JVMTI_ERROR_NONE) { 1.158 + printf(" Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err); 1.159 + return JNI_ERR; 1.160 + } 1.161 + 1.162 + return JNI_OK; 1.163 +} 1.164 + 1.165 +#ifdef __cplusplus 1.166 +} 1.167 +#endif