1 /* 2 * Copyright (c) 2014, 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 jdk.vm.ci.hotspot; 24 25 import static java.lang.String.format; 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 import java.lang.reflect.Constructor; 30 import java.lang.reflect.Executable; 31 import java.lang.reflect.Method; 32 import java.lang.reflect.Modifier; 33 import java.util.Arrays; 34 import java.util.Objects; 35 36 import jdk.internal.misc.Unsafe; 37 import jdk.internal.org.objectweb.asm.ClassReader; 38 import jdk.internal.org.objectweb.asm.ClassVisitor; 39 import jdk.internal.org.objectweb.asm.Label; 40 import jdk.internal.org.objectweb.asm.MethodVisitor; 41 import jdk.internal.org.objectweb.asm.Opcodes; 42 import jdk.internal.org.objectweb.asm.Type; 43 44 /** 45 * A {@link ClassVisitor} that verifies {@link HotSpotVMConfig} does not access {@link Unsafe} from 46 * any of its non-static, non-constructor methods. This ensures that a deserialized 47 * {@link HotSpotVMConfig} object does not perform any unsafe reads on addresses that are only valid 48 * in the context in which the object was serialized. Note that this does not catch cases where a 49 * client uses an address stored in a {@link HotSpotVMConfig} field. 50 */ 51 final class HotSpotVMConfigVerifier extends ClassVisitor { 52 53 public static boolean check() { 54 Class<?> cls = HotSpotVMConfig.class; 55 String classFilePath = "/" + cls.getName().replace('.', '/') + ".class"; 56 try { 57 InputStream classfile = cls.getResourceAsStream(classFilePath); 58 ClassReader cr = new ClassReader(Objects.requireNonNull(classfile, "Could not find class file for " + cls.getName())); 59 ClassVisitor cv = new HotSpotVMConfigVerifier(); 60 cr.accept(cv, 0); 61 return true; 62 } catch (IOException e) { 63 throw new InternalError(e); 64 } 65 } 66 67 /** 68 * Source file context for error reporting. 69 */ 70 String sourceFile = null; 71 72 /** 73 * Line number for error reporting. 74 */ 75 int lineNo = -1; 76 77 private static Class<?> resolve(String name) { 78 try { 79 return Class.forName(name.replace('/', '.')); 80 } catch (ClassNotFoundException e) { 81 throw new InternalError(e); 82 } 83 } 84 85 HotSpotVMConfigVerifier() { 86 super(Opcodes.ASM5); 87 } 88 89 @Override 90 public void visitSource(String source, String debug) { 91 this.sourceFile = source; 92 } 93 94 void verify(boolean condition, String message) { 95 if (!condition) { 96 error(message); 97 } 98 } 99 100 void error(String message) { 101 String errorMessage = format("%s:%d: %s is not allowed in the context of compilation replay. The unsafe access should be moved into the %s constructor and the result cached in a field", 102 sourceFile, lineNo, message, HotSpotVMConfig.class.getSimpleName()); 103 throw new InternalError(errorMessage); 104 105 } 106 107 @Override 108 public MethodVisitor visitMethod(int access, String name, String d, String signature, String[] exceptions) { 109 if (!Modifier.isStatic(access) && Modifier.isPublic(access) && !name.equals("<init>")) { 110 return new MethodVisitor(Opcodes.ASM5) { 111 112 @Override 113 public void visitLineNumber(int line, Label start) { 114 lineNo = line; 115 } 116 117 private Executable resolveMethod(String owner, String methodName, String methodDesc) { 118 Class<?> declaringClass = resolve(owner); 119 while (declaringClass != null) { 120 if (methodName.equals("<init>")) { 121 for (Constructor<?> c : declaringClass.getDeclaredConstructors()) { 122 if (methodDesc.equals(Type.getConstructorDescriptor(c))) { 123 return c; 124 } 125 } 126 } else { 127 Type[] argumentTypes = Type.getArgumentTypes(methodDesc); 128 for (Method m : declaringClass.getDeclaredMethods()) { 129 if (m.getName().equals(methodName)) { 130 if (Arrays.equals(argumentTypes, Type.getArgumentTypes(m))) { 131 if (Type.getReturnType(methodDesc).equals(Type.getReturnType(m))) { 132 return m; 133 } 134 } 135 } 136 } 137 } 138 declaringClass = declaringClass.getSuperclass(); 139 } 140 throw new NoSuchMethodError(owner + "." + methodName + methodDesc); 141 } 142 143 /** 144 * Checks whether a given method is allowed to be called. 145 */ 146 private boolean checkInvokeTarget(Executable method) { 147 if (method.getDeclaringClass().equals(Unsafe.class)) { 148 return false; 149 } 150 return true; 151 } 152 153 @Override 154 public void visitMethodInsn(int opcode, String owner, String methodName, String methodDesc, boolean itf) { 155 Executable callee = resolveMethod(owner, methodName, methodDesc); 156 verify(checkInvokeTarget(callee), "invocation of " + callee); 157 } 158 }; 159 } else { 160 return null; 161 } 162 } 163 }