Skip to content

Commit

Permalink
Added test for the fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Marchenko committed Mar 7, 2024
1 parent 815699c commit 897091c
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
61 changes: 61 additions & 0 deletions test/hotspot/jtreg/vmTestbase/nsk/jvmti/unit/extcallback/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/*
* @test
*
* @summary Tests subscribing ClassUnload JVMTI extension event after unsubscribing all events.
* @bug 8286490
* @requires vm.jvmti
* @library /vmTestbase
* /test/lib
* @run driver nsk.share.ExtraClassesBuilder loadClass
* @run main/othervm/native nsk.jvmti.unit.extcallback.Test
*/

package nsk.jvmti.unit.extcallback;

import jdk.test.lib.process.ProcessTools;

public class Test {

public static void main(String[] args) throws Exception {
ProcessTools.executeTestJvm("-agentlib:extcallback", Test1.class.getName())
.shouldHaveExitValue(0)
.shouldContain("callbackClassUnload called");
}

public static class Test1 {
public static void main(String[] args) throws Exception {
System.out.println("App started");
{
var unloader = new nsk.share.ClassUnloader();
unloader.loadClass("nsk.jvmti.unit.extcallback.Test2", "./bin/loadClass");
unloader.unloadClass();
unloader = null;
}
System.gc();
System.out.println("App finished");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023, Azul Systems, Inc. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

#include <jvmti.h>
#include <string.h>

void JNICALL callbackClassUnload(jvmtiEnv* jvmti_env, ...) {
printf("callbackClassUnload called\n");
fflush(NULL);
}

void JNICALL callbackClassLoad(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jclass klass) {
// nothing to do, just a stub
}

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM* jvm, char* options, void* reserved) {
printf("%s:%d : %s : JVMTI agent loading...\n", __FILE__, __LINE__, __FUNCTION__);

jvmtiEnv* jvmti = NULL;
jint extensionEventCount = 0;
jvmtiExtensionEventInfo* extensionEvents = NULL;
(*jvm)->GetEnv(jvm, (void**)&jvmti, JVMTI_VERSION_1_0);

// Set extension event callback
(*jvmti)->GetExtensionEvents(jvmti, &extensionEventCount, &extensionEvents);
for (int i = 0; i < extensionEventCount; ++i) {
if (0 == strcmp("com.sun.hotspot.events.ClassUnload", extensionEvents[i].id)) {
(*jvmti)->SetExtensionEventCallback(jvmti, extensionEvents[i].extension_event_index, &callbackClassUnload);
}
}

{
// Set some event callbacks
jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.ClassLoad = &callbackClassLoad;
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));

// Clear event callbacks
memset(&callbacks, 0, sizeof(callbacks));
(*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
}

return JNI_OK;
}

JNIEXPORT void JNICALL Agent_OnUnload(JavaVM* jvm) {
printf("%s:%d : %s : JVMTI agent unloading...\n", __FILE__, __LINE__, __FUNCTION__);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

package nsk.jvmti.unit.extcallback;

public class Test2 {
public Test2() {}
}

0 comments on commit 897091c

Please sign in to comment.