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 package gc.g1.unloading.bytecode;
  24 
  25 import java.io.*;
  26 import java.nio.charset.*;
  27 import java.util.*;
  28 
  29 
  30 /**
  31  * This BytecodeFactory produces bytecode that is golden bytecode with className substituted.
  32  */
  33 public class BytecodeMutatorFactory implements BytecodeFactory {
  34 
  35     private static final String FILLER_CHARACTER = "_";
  36 
  37     /**
  38      * Utility method in this class
  39      */
  40     public static String padName(String s, int length) {
  41         int difference = length - s.length();
  42         StringBuilder sb = new StringBuilder(s);
  43         for (int i = 0; i < difference; i++) {
  44             sb.append(FILLER_CHARACTER);
  45         }
  46         return sb.toString();
  47     }
  48 
  49     public String padName(String s) {
  50         return padName(s, getNameLength());
  51     }
  52 
  53     private final Charset CHARACTER_SET = StandardCharsets.UTF_8;
  54 
  55     private List<Integer> offsets = new LinkedList<>();
  56 
  57     private byte[] templateBytecode;
  58 
  59     private String templateClassName;
  60 
  61     private byte[] templateClassNameAsBytes;
  62 
  63     public BytecodeMutatorFactory() {
  64         this(DefaultTemplateClass.class.getName());
  65     }
  66 
  67     public BytecodeMutatorFactory(String templateClassName) {
  68         this.templateClassName = templateClassName;
  69 
  70         // Read bytecode to array
  71         InputStream is = ClassLoader.getSystemResourceAsStream(templateClassName.replace('.', '/').concat(".class"));
  72         try {
  73             templateBytecode = new byte[is.available()];
  74             is.read(templateBytecode);
  75             is.close();
  76         } catch (IOException e) {
  77             throw new RuntimeException(e);
  78         }
  79 
  80         // Save offsets
  81         templateClassNameAsBytes = templateClassName.replace('.', '/').getBytes(CHARACTER_SET);
  82         for (int i = 0; i < templateBytecode.length; i++) {
  83             boolean match = true;
  84             for (int j = 0; j < templateClassNameAsBytes.length; j++) {
  85                 if (i + j >= templateBytecode.length || templateClassNameAsBytes[j] != templateBytecode[i + j]) {
  86                     match = false;
  87                     break;
  88                 }
  89             }
  90             if (match) {
  91                 offsets.add(i);
  92             }
  93         }
  94     }
  95 
  96     public byte[] getBytecode(String className) {
  97 
  98         // Check size of name constraint
  99         byte[] newClassNameAsBytes = className.replace('.', '/').getBytes(CHARACTER_SET);
 100         if (newClassNameAsBytes.length != templateClassNameAsBytes.length) {
 101             throw new RuntimeException("Can't produce bytecode with \"" + className + "\" substituted as class name. " +
 102                     "Length of this name differs from length of \"" + templateClassName + "\" which equals to " + templateClassName.length() +
 103                     ". Length of \"" + className + "\" is " + className.length() + ".");
 104         }
 105 
 106         // Prepare bytecode
 107         byte[] result = Arrays.copyOf(templateBytecode, templateBytecode.length);
 108         for (int offset : offsets) {
 109             System.arraycopy(newClassNameAsBytes, 0, result, offset, newClassNameAsBytes.length);
 110         }
 111         return result;
 112     }
 113 
 114     public int getNameLength() {
 115         return templateClassName.length();
 116     }
 117 
 118     @Override
 119     public Bytecode createBytecode(String className) {
 120         String finalClassName = padName(className);
 121         byte[] bytecode = getBytecode(finalClassName);
 122         return new Bytecode(finalClassName, bytecode);
 123     }
 124 
 125 }