Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/ClassFileLoadHook/classfloadhk009.java
40948 views
/*1* Copyright (c) 2003, 2018, 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*/2223package nsk.jvmti.ClassFileLoadHook;2425import java.io.*;26import java.lang.reflect.*;2728import nsk.share.*;29import nsk.share.jvmti.*;3031/**32* Debuggee class of JVMTI test.33*/34public class classfloadhk009 extends DebugeeClass {3536/** Load native library if required. */37static {38System.loadLibrary("classfloadhk009");39}4041/** Run test from command line. */42public static void main(String argv[]) {43argv = nsk.share.jvmti.JVMTITest.commonInit(argv);4445// JCK-compatible exit46System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);47}4849/** Run test from JCK-compatible environment. */50public static int run(String argv[], PrintStream out) {51return new classfloadhk009().runIt(argv, out);52}5354/* =================================================================== */5556/* constant names */57public static final String PACKAGE_NAME = "nsk.jvmti.ClassFileLoadHook";58public static final String TESTED_CLASS_NAME = PACKAGE_NAME + ".classfloadhk009r";5960/* scaffold objects */61ArgumentHandler argHandler = null;62Log log = null;63long timeout = 0;64int status = Consts.TEST_PASSED;6566/* bytecode of redefined class */67public static byte redefClassBytes[] = null;68/* bytecode of instrumented class */69public static byte newClassBytes[] = null;7071/* tested class to de redefined */72public static Class<?> testedClass = null;7374/* tested method name */75public static final String TESTED_METHOD_NAME = "testedStaticMethod";7677/* possible values returned by tested method */78public static final long ORIG_VALUE = 20;79public static final long REDEF_VALUE = 12;80public static final long NEW_VALUE = 58;8182/** Run debuggee code. */83public int runIt(String argv[], PrintStream out) {84argHandler = new ArgumentHandler(argv);85log = new Log(out, argHandler);86timeout = argHandler.getWaitTime() * 60 * 1000; // milliseconds8788String args[] = argHandler.getArguments();89if (args.length <= 0) {90throw new Failure("Path for tested class file to load not specified");91}9293String location = args[0];94String origPath = location + File.separator + "loadclass";95log.display("Using path to original class: \n\t" + origPath);96String redefPath = location + File.separator + "newclass";97log.display("Using path to redefined class: \n\t" + redefPath);98String newPath = location + File.separator + "newclass01";99log.display("Using path to instrumented class: \n\t" + newPath);100101classfloadhk009ClassLoader classLoader = new classfloadhk009ClassLoader(origPath);102103log.display("Reading redefined bytecode of tested class: \n\t" + TESTED_CLASS_NAME);104try {105redefClassBytes = classLoader.readBytes(redefPath, TESTED_CLASS_NAME);106} catch (IOException e) {107throw new Failure("IOException in reading redefined bytecode of tested class file:\n\t" + e);108}109110log.display("Reading instrumented bytecode of tested class: \n\t" + TESTED_CLASS_NAME);111try {112newClassBytes = classLoader.readBytes(newPath, TESTED_CLASS_NAME);113} catch (IOException e) {114throw new Failure("IOException in reading instrumented bytecode of tested class file:\n\t" + e);115}116117try {118log.display("Loading original tested class: " + TESTED_CLASS_NAME);119testedClass = Class.forName(TESTED_CLASS_NAME, true, classLoader);120} catch (ClassNotFoundException e) {121throw new Failure("Tested class not found: \n\t" + e);122}123124log.display("Sync: tested class loaded");125status = checkStatus(status);126127log.display("Checking if the tested class was actually redefined");128try {129Method meth = testedClass.getMethod(TESTED_METHOD_NAME, new Class[0]);130Object res = meth.invoke(null, new Object[0]);131if (!(res instanceof Long)) {132log.complain("Tested method of redefined and instrumented class retured not Long value: " + res);133log.complain("The tested class was redefined and instrumented incorrectly!");134status = Consts.TEST_FAILED;135} else {136long value = ((Long)res).longValue();137log.display("Tested method of redefined and instrumented class returned value: " + value);138139if (value == ORIG_VALUE) {140log.complain("Tested method of redefined and instrumented class returned original value: " + value);141log.complain("The tested class was not redefined nor instrumented!");142status = Consts.TEST_FAILED;143} else if (value == REDEF_VALUE) {144log.complain("Tested method of redefined and instrumented class returned redefined value: " + value);145log.complain("The tested class was redefined but not instrumented!");146status = Consts.TEST_FAILED;147} else if (value != NEW_VALUE) {148log.complain("Tested method of redefined and instrumented class returned unexpected value: " + value);149log.complain("The tested class was redefined and instrumented incorrectly!");150status = Consts.TEST_FAILED;151} else {152log.display("Tested method of redefined and instrumented class returned expected new value: " + value);153log.display("The tested class was redefined and instrumented correctly!");154}155}156} catch (NoSuchMethodException e) {157log.complain("No tested method found in redefined and instrumented class:\n\t" + e);158status = Consts.TEST_FAILED;159} catch (Exception e) {160log.complain("Exception in invoking method of redefined and instrumented class:\n\t" + e);161status = Consts.TEST_FAILED;162}163164return status;165}166}167168/* =================================================================== */169170/** Classloader for tested class. */171class classfloadhk009ClassLoader extends ClassLoader {172private String path;173174/** Make classloader providing path to class files. */175public classfloadhk009ClassLoader(String path) {176this.path = path;177}178179/** Load specified class. */180protected Class<?> findClass(String name) throws ClassNotFoundException {181try {182byte[] bytes = readBytes(path, name);183return defineClass(name, bytes, 0, bytes.length);184} catch (IOException e) {185throw new ClassNotFoundException("IOException in loading class: " + name, e);186}187}188189/** Read classfile for specified path and class name. */190public static byte[] readBytes(String path, String classname) throws IOException {191String filename = path + File.separator192+ classname.replace('.', File.separatorChar) + ".class";193FileInputStream in = new FileInputStream(filename);194byte[] bytes = new byte[in.available()];195in.read(bytes);196in.close();197return bytes;198}199}200201202