1 /*
   2  * Copyright (c) 2016, 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.tools;
  25 
  26 import java.io.File;
  27 import vm.share.UnsafeAccess;
  28 import vm.share.FileUtils;
  29 import vm.mlvm.share.CustomClassLoaders;
  30 import vm.mlvm.anonloader.share.AnonkTestee01;
  31 
  32 import vm.mlvm.share.Env;
  33 
  34 /**
  35  * A tool, which loads a class file specified in command line into VM using:
  36  * <ul>
  37  *   <li>A custom class loader,
  38  *   <li>{@link sun.misc.Unsafe#defineAnonymousClass(Class, byte[], Object[])}
  39  *   call.
  40  * </ul>
  41  *
  42  * <p>Syntax:
  43  * <pre>{@code
  44  * $ java [options...] vm.mlvm.tool.LoadClass class-file-name [class-FQDN]
  45  * }</pre>
  46  *
  47  * The first argument, class file name is mandatory.
  48  * The second one is optional &mdash; a fully qualified class name.
  49  * If the second argument is not specified, it is constructed from the first
  50  * argument, replacing '/' with '.'
  51  *
  52  * <p>The tool can be used for investigating failures of vm.mlvm.anon tests.
  53  *
  54  */
  55 public class LoadClass {
  56 
  57     private static final Class<?> HOST_CLASS = AnonkTestee01.class;
  58 
  59     private static void usage() {
  60         System.out.println("Usage: java " + LoadClass.class.getName()
  61                 + " <class-file-to-load> [class-name]");
  62     }
  63 
  64     /**
  65      * Runs the tool.
  66      * @param args Tool arguments
  67      */
  68     @SuppressWarnings("deprecation")
  69     public static void main(String[] args) {
  70         if (args.length < 1) {
  71             usage();
  72             System.exit(1);
  73         }
  74 
  75         try {
  76             final String classFileName = args[0];
  77             final String className = (args.length > 1) ? args[1]
  78                     : classFileName.replaceAll("\\.class$", "")
  79                             .replace("/", ".");
  80             final byte[] classBytes = FileUtils
  81                     .readFile(new File(classFileName));
  82 
  83             Env.traceImportant("Loading class '%s' from file '%s'...",
  84                     className, classFileName);
  85 
  86             Env.traceImportant("...using custom ClassLoader");
  87             try {
  88                 ClassLoader cl = CustomClassLoaders
  89                         .makeClassBytesLoader(classBytes, className);
  90                 Class<?> c = cl.loadClass(className);
  91                 c.newInstance();
  92                 Env.traceImportant("OK");
  93             } catch (Throwable e) {
  94                 Env.traceImportant(e,
  95                         "Couldn't load class '%s' via custom ClassLoader",
  96                         classFileName);
  97             }
  98 
  99             Env.traceImportant(
 100                     "...using sun.misc.Unsafe.defineAnonymousClass():");
 101             try {
 102                 Class<?> c = UnsafeAccess.unsafe.defineAnonymousClass(HOST_CLASS,
 103                         classBytes, new Object[0]);
 104                 c.newInstance();
 105                 Env.traceImportant("OK");
 106             } catch (Throwable e) {
 107                 Env.traceImportant(e, "Couldn't load class '%s' via sun.misc."
 108                         + "Unsafe.defineAnonymousClass()", classFileName);
 109             }
 110         } catch (Throwable e) {
 111             Env.traceImportant(e, "Can't load class");
 112         }
 113     }
 114 }