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/byteMutation.
  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.byteMutation.Test
  38  * @run driver vm.mlvm.share.IndifiedClassesBuilder
  39  *
  40  * @run main/othervm vm.mlvm.anonloader.stress.byteMutation.Test -stressIterationsFactor 100000
  41  */
  42 
  43 package vm.mlvm.anonloader.stress.byteMutation;
  44 
  45 import vm.mlvm.anonloader.share.AnonkTestee01;
  46 import vm.mlvm.anonloader.share.StressClassLoadingTest;
  47 import vm.share.FileUtils;
  48 import vm.share.options.Option;
  49 
  50 /**
  51  * The test does the following in a cycle:
  52  *
  53  * <ol>
  54  * <li>Takes bytes from a valid class file
  55  * <li>Sets 1 to 5 bytes in random positions to random values
  56  * <li>Tries to load such class using:
  57  *     <ul>
  58  *       <li>a custom class loader,
  59  *       <li>{@link sun.misc.Unsafe#defineAnonymousClass}
  60  *           when {@code -unsafeLoad true} option is passed to the test.
  61  *     </ul>
  62  * </ol>
  63  *
  64  * <p>In most cases the resulting class file is invalid and rejected by
  65  * the VM verifier. But this test tries to find pathological cases, such
  66  * as infinite loops during verification or VM crashes.
  67  *
  68  * <p>NB: There is a tool to load invalid classes saved by this test.
  69  * Please see tool documentation at {@link vm.mlvm.tools.LoadClass}
  70  *
  71  */
  72 public class Test extends StressClassLoadingTest {
  73     private final static Class<?> HOST_CLASS = AnonkTestee01.class;
  74     private final byte[] testeeBytes;
  75     @Option(name = "mutationCount", default_value = "3",
  76             description = "How many bytes to mutate in a class")
  77     private int mutationCount = 3;
  78 
  79     /**
  80      * Constructs the test.
  81      * @throws Exception if there are any errors when
  82      * reading {@link vm.mlvm.anonloader.share.AnonkTestee01} class bytecodes.
  83      */
  84     public Test() throws Exception {
  85         this.testeeBytes = FileUtils.readClass(AnonkTestee01.class.getName());
  86     }
  87 
  88     /**
  89      * Returns {@link vm.mlvm.anonloader.share.AnonkTestee01} class to the
  90      * parent.
  91      * @return {@link vm.mlvm.anonloader.share.AnonkTestee01} class.
  92      */
  93     @Override
  94     protected Class<?> getHostClass() {
  95         return HOST_CLASS;
  96     }
  97 
  98     /**
  99      * Takes {@link vm.mlvm.anonloader.share.AnonkTestee01} class bytecodes
 100      * and modifies mutationCount bytes setting them to random values.
 101      * @return {@link vm.mlvm.anonloader.share.AnonkTestee01} class bytecodes with modified bytes.
 102      */
 103     @Override
 104     protected byte[] generateClassBytes() {
 105         // TODO: there is non-zero probability that generated bytecode will be
 106         // valid, so it should be a subject of fuzzing mechanism
 107         byte[] alteredBytes = testeeBytes.clone();
 108         for (int j = 0; j < mutationCount; ++j) {
 109             alteredBytes[getRNG().nextInt(alteredBytes.length)] = (byte) getRNG().nextInt(256);
 110         }
 111         return alteredBytes;
 112     }
 113 
 114     /**
 115      * Runs the test.
 116      * @param args Test arguments.
 117      */
 118     public static void main(String[] args) {
 119         StressClassLoadingTest.launch(args);
 120     }
 121 }