1 /* 2 * Copyright (c) 2014, 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 package vm.mlvm.anonloader.share; 25 26 import jdk.internal.org.objectweb.asm.ClassReader; 27 import vm.mlvm.share.MlvmTest; 28 import vm.mlvm.share.MlvmTestExecutor; 29 import vm.mlvm.share.Env; 30 import vm.share.FileUtils; 31 import vm.share.UnsafeAccess; 32 import vm.share.options.Option; 33 34 /** 35 * This is a base class for kind of tests, which modify the parent class name of test dummy class vm.mlvm.share.AnonkTestee01 36 * with an arbitrary string, load the modified class using Unsafe.defineAnonymousClass and instantiate it. 37 * <p> 38 * The tests can extend this class or use it from command-line to provide the actual data: 39 * <ul> 40 * <li>new parent class name, 41 * <li>optionally the list of expected exceptions to be thrown during class loading and instantiation 42 * (see {@link vm.mlvm.share.MlvmTest#setRequiredExceptions(Class<? extends Throwable>... classes)} for details) 43 * </ul> 44 */ 45 46 public class ReplaceClassParentTest extends MlvmTest { 47 48 @Option(name = "newParent", default_value = "", description = "String to replace the name of the parent class of the testee") 49 private String newParentOpt; 50 51 public void setReplaceParent(String newParent) { 52 newParentOpt = newParent; 53 } 54 55 private static final Class<?> TESTEE_CLASS = AnonkTestee01.class; 56 57 public ReplaceClassParentTest() { 58 } 59 60 public boolean run() throws Throwable { 61 byte[] classBytes = FileUtils.readClass(TESTEE_CLASS.getName()); 62 ClassReader reader = new ClassReader(classBytes); 63 int superclassNameIdx = reader.readUnsignedShort(reader.header + 4); 64 int cpLength = reader.getItemCount(); 65 if (superclassNameIdx == 0) { 66 throw new RuntimeException("Test bug: unable to find super class" 67 + " name index"); 68 } 69 Env.traceDebug("Superclass name CP index: " + superclassNameIdx 70 + "; replacing CP entry with '" + newParentOpt + "'"); 71 // now, construction of cp patch 72 Object cpPatch[] = new Object[cpLength]; 73 cpPatch[superclassNameIdx] = newParentOpt; 74 // define and instantiate 75 UnsafeAccess.unsafe.defineAnonymousClass(TESTEE_CLASS, classBytes, 76 cpPatch).newInstance(); 77 // Whether test should pass or fail should be specified via requireExceptions mechanism in MlvmTest 78 return true; 79 80 } 81 82 public static void main(String[] args) { 83 MlvmTestExecutor.launch(args); 84 } 85 }