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