1 /* 2 * Copyright (c) 2010, 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 25 /* 26 * @test 27 * @modules java.base/jdk.internal.misc 28 * 29 * @summary converted from VM Testbase vm/mlvm/anonloader/stress/randomBytecodes. 30 * VM Testbase keywords: [feature_mlvm, nonconcurrent] 31 * 32 * @library /vmTestbase 33 * /test/lib 34 * @run driver jdk.test.lib.FileInstaller . . 35 * 36 * @comment build test class and indify classes 37 * @build vm.mlvm.anonloader.stress.randomBytecodes.Test 38 * @run driver vm.mlvm.share.IndifiedClassesBuilder 39 * 40 * @run main/othervm vm.mlvm.anonloader.stress.randomBytecodes.Test -stressIterationsFactor 100000 41 */ 42 43 package vm.mlvm.anonloader.stress.randomBytecodes; 44 45 import java.util.Arrays; 46 import vm.mlvm.anonloader.share.StressClassLoadingTest; 47 48 /** 49 * The test does the following in a cycle: 50 * <ol> 51 * <li>Creates a class bytecodes that has a valid 12-byte header 52 * and has totally random bytes after the header 53 * <li>Tries to load such class using: 54 * <ul> 55 * <li>a custom class loader, or 56 * <li>{@link sun.misc.Unsafe#defineAnonymousClass} 57 * when {@code -unsafeLoad true} is set. 58 * </ul> 59 * </ol> 60 * 61 * <p>In most cases the resulting class file is invalid and rejected by 62 * the VM verifier. But this test is looking for pathological cases 63 * such as infinite loops in the verifier or VM crashes. 64 * 65 * <p>NB: There is a tool to load invalid classes saved by this test. 66 * Please see tool documentation at {@link vm.mlvm.tools.LoadClass}. 67 * 68 */ 69 public class Test extends StressClassLoadingTest { 70 private static final Class<?> HOST_CLASS = Test.class; 71 private static final int MAX_SIZE = 0xFFF7; 72 private static final byte[] CLASS_HEADER = new byte[] { 73 (byte)0xCA, (byte)0xFE, (byte)0xBA, (byte)0xBE, 74 0x00, 0x00, 0x00, 0x32 75 }; 76 77 /** 78 * Returns {@link vm.mlvm.anonloader.share.AnonkTestee01} class to the 79 * parent. 80 * @return {@link vm.mlvm.anonloader.share.AnonkTestee01} class. 81 */ 82 @Override 83 protected Class<?> getHostClass() { 84 return HOST_CLASS; 85 } 86 87 /** 88 * Generates a class with valid header (magic and version fields) and 89 * random bytes after the header. 90 * <p>Class size is random ([8..65527]). 91 * Byte values are limited to [0..11] range in order to increase 92 * possiblity that the random class passes the initial (dead-on-arrival) 93 * stages of the verifier and is rejected 94 * in more interesting ones, like method bytecode verification. 95 * Class version is 52. 96 * 97 * @return Class with valid Java header (8 bytes) and totally random bytes 98 * after the header 99 */ 100 @Override 101 protected byte[] generateClassBytes() { 102 final byte[] classBytes = Arrays.copyOf(CLASS_HEADER, 103 CLASS_HEADER.length + getRNG().nextInt(MAX_SIZE)); 104 for (int j = CLASS_HEADER.length; j < classBytes.length; j++) { 105 classBytes[j] = (byte) getRNG().nextInt(12); 106 } 107 108 return classBytes; 109 } 110 111 /** 112 * Runs the test. 113 * @param args Test arguments. 114 */ 115 public static void main(String[] args) { 116 StressClassLoadingTest.launch(args); 117 } 118 }