Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/RedefineTests/libRedefineDoubleDelete.c
32284 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include <stdio.h>24#include <string.h>25#include "jvmti.h"2627#ifdef __cplusplus28extern "C" {29#endif3031#ifndef JNI_ENV_ARG3233#ifdef __cplusplus34#define JNI_ENV_ARG(x, y) y35#define JNI_ENV_PTR(x) x36#else37#define JNI_ENV_ARG(x,y) x, y38#define JNI_ENV_PTR(x) (*x)39#endif4041#endif4243#define TranslateError(err) "JVMTI error"4445static jvmtiEnv *jvmti = NULL;4647static jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved);4849JNIEXPORT50jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {51return Agent_Initialize(jvm, options, reserved);52}5354JNIEXPORT55jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {56return Agent_Initialize(jvm, options, reserved);57}5859JNIEXPORT60jint JNICALL JNI_OnLoad(JavaVM *jvm, void *reserved) {61return JNI_VERSION_1_8;62}636465static jint newClassDataLen = 0;66static unsigned char* newClassData = NULL;6768static jint69getBytecodes(jvmtiEnv *jvmti_env,70jint class_data_len, const unsigned char* class_data) {71int i;72jint res;7374newClassDataLen = class_data_len;75res = (*jvmti_env)->Allocate(jvmti_env, newClassDataLen, &newClassData);76if (res != JNI_OK) {77printf(" Unable to allocate bytes\n");78return JNI_ERR;79}80for (i = 0; i < newClassDataLen; i++) {81newClassData[i] = class_data[i];82// Rewrite oo in class to aa83if (i > 0 && class_data[i] == 'o' && class_data[i-1] == 'o') {84newClassData[i] = newClassData[i-1] = 'a';85}86}87printf(" ... copied bytecode: %d bytes\n", (int)newClassDataLen);88return JNI_OK;89}909192static void JNICALL93Callback_ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env,94jclass class_being_redefined,95jobject loader, const char* name, jobject protection_domain,96jint class_data_len, const unsigned char* class_data,97jint *new_class_data_len, unsigned char** new_class_data) {98if (name != NULL && strcmp(name, "RedefineDoubleDelete$B") == 0) {99if (newClassData == NULL) {100jint res = getBytecodes(jvmti_env, class_data_len, class_data);101if (res == JNI_ERR) {102printf(">>> ClassFileLoadHook event: class name %s FAILED\n", name);103return;104}105// Only change for first CFLH event.106*new_class_data_len = newClassDataLen;107*new_class_data = newClassData;108}109printf(">>> ClassFileLoadHook event: class name %s\n", name);110}111}112113static114jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {115jint res, size;116jvmtiCapabilities caps;117jvmtiEventCallbacks callbacks;118jvmtiError err;119120res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),121JVMTI_VERSION_1_2);122if (res != JNI_OK || jvmti == NULL) {123printf(" Error: wrong result of a valid call to GetEnv!\n");124return JNI_ERR;125}126127printf("Enabling following capabilities: can_generate_all_class_hook_events, "128"can_retransform_classes, can_redefine_classes");129memset(&caps, 0, sizeof(caps));130caps.can_generate_all_class_hook_events = 1;131caps.can_retransform_classes = 1;132caps.can_redefine_classes = 1;133printf("\n");134135err = (*jvmti)->AddCapabilities(jvmti, &caps);136if (err != JVMTI_ERROR_NONE) {137printf(" Error in AddCapabilites: %s (%d)\n", TranslateError(err), err);138return JNI_ERR;139}140141size = (jint)sizeof(callbacks);142143memset(&callbacks, 0, sizeof(callbacks));144callbacks.ClassFileLoadHook = Callback_ClassFileLoadHook;145146err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, size);147if (err != JVMTI_ERROR_NONE) {148printf(" Error in SetEventCallbacks: %s (%d)\n", TranslateError(err), err);149return JNI_ERR;150}151152err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);153if (err != JVMTI_ERROR_NONE) {154printf(" Error in SetEventNotificationMode: %s (%d)\n", TranslateError(err), err);155return JNI_ERR;156}157158return JNI_OK;159}160161#ifdef __cplusplus162}163#endif164165166