Mercurial > hg > truffle
comparison graal/com.oracle.graal.hotspot.sourcegen/src/com/oracle/graal/hotspot/sourcegen/OptionsVerifier.java @ 16030:bddb3eb57e90
moved verification of OptionValue declaring classes from run time to build time
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Thu, 05 Jun 2014 11:05:46 +0200 |
parents | |
children | addc0564e5b5 |
comparison
equal
deleted
inserted
replaced
16029:8bbfddf8483f | 16030:bddb3eb57e90 |
---|---|
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 com.oracle.graal.hotspot.sourcegen; | |
24 | |
25 import static java.lang.String.*; | |
26 | |
27 import java.io.*; | |
28 import java.lang.reflect.*; | |
29 import java.util.*; | |
30 import java.util.zip.*; | |
31 | |
32 import jdk.internal.org.objectweb.asm.*; | |
33 import jdk.internal.org.objectweb.asm.Type; | |
34 | |
35 import com.oracle.graal.options.*; | |
36 | |
37 /** | |
38 * A {@link ClassVisitor} that verifies a class declaring one or more {@linkplain OptionValue | |
39 * options} has a class initializer that only initializes the option(s). This sanity check mitigates | |
40 * the possibility of an option value being used before the code that sets the value (e.g., from the | |
41 * command line) has been executed. | |
42 */ | |
43 final class OptionsVerifier extends ClassVisitor { | |
44 | |
45 public static void checkClass(Class<?> cls, OptionDescriptor option, Set<Class<?>> checked, ZipFile graalJar) throws IOException { | |
46 if (!checked.contains(cls)) { | |
47 checked.add(cls); | |
48 Class<?> superclass = cls.getSuperclass(); | |
49 if (superclass != null && !superclass.equals(Object.class)) { | |
50 checkClass(superclass, option, checked, graalJar); | |
51 } | |
52 | |
53 String classFilePath = cls.getName().replace('.', '/') + ".class"; | |
54 ZipEntry entry = Objects.requireNonNull(graalJar.getEntry(classFilePath), "Could not find class file for " + cls.getName()); | |
55 ClassReader cr = new ClassReader(graalJar.getInputStream(entry)); | |
56 | |
57 ClassVisitor cv = new OptionsVerifier(cls, option); | |
58 cr.accept(cv, 0); | |
59 } | |
60 } | |
61 | |
62 /** | |
63 * The option field context of the verification. | |
64 */ | |
65 private final OptionDescriptor option; | |
66 | |
67 /** | |
68 * The class in which {@link #option} is declared or a super-class of that class. This is the | |
69 * class whose {@code <clinit>} method is being verified. | |
70 */ | |
71 private final Class<?> cls; | |
72 | |
73 /** | |
74 * Source file context for error reporting. | |
75 */ | |
76 String sourceFile = null; | |
77 | |
78 /** | |
79 * Line number for error reporting. | |
80 */ | |
81 int lineNo = -1; | |
82 | |
83 final Class<?>[] boxingTypes = {Boolean.class, Byte.class, Short.class, Character.class, Integer.class, Float.class, Long.class, Double.class}; | |
84 | |
85 private static Class<?> resolve(String name) { | |
86 try { | |
87 return Class.forName(name.replace('/', '.')); | |
88 } catch (ClassNotFoundException e) { | |
89 throw new InternalError(e); | |
90 } | |
91 } | |
92 | |
93 OptionsVerifier(Class<?> cls, OptionDescriptor desc) { | |
94 super(Opcodes.ASM5); | |
95 this.cls = cls; | |
96 this.option = desc; | |
97 } | |
98 | |
99 @Override | |
100 public void visitSource(String source, String debug) { | |
101 this.sourceFile = source; | |
102 } | |
103 | |
104 void verify(boolean condition, String message) { | |
105 if (!condition) { | |
106 error(message); | |
107 } | |
108 } | |
109 | |
110 void error(String message) { | |
111 String errorMessage = format("%s:%d: Illegal code in %s.<clinit> which may be executed when %s.%s is initialized:%n%n %s%n%n" + "The recommended solution is to move " + option.getName() + | |
112 " into a separate class (e.g., %s.Options).%n", sourceFile, lineNo, cls.getSimpleName(), option.getDeclaringClass().getSimpleName(), option.getName(), message, | |
113 option.getDeclaringClass().getSimpleName()); | |
114 throw new InternalError(errorMessage); | |
115 | |
116 } | |
117 | |
118 @Override | |
119 public MethodVisitor visitMethod(int access, String name, String d, String signature, String[] exceptions) { | |
120 if (name.equals("<clinit>")) { | |
121 return new MethodVisitor(Opcodes.ASM5) { | |
122 | |
123 @Override | |
124 public void visitLineNumber(int line, Label start) { | |
125 lineNo = line; | |
126 } | |
127 | |
128 @Override | |
129 public void visitFieldInsn(int opcode, String owner, String fieldName, String fieldDesc) { | |
130 if (opcode == Opcodes.PUTFIELD || opcode == Opcodes.PUTSTATIC) { | |
131 verify(resolve(owner).equals(option.getDeclaringClass()), format("store to field %s.%s", resolve(owner).getSimpleName(), fieldName)); | |
132 verify(opcode != Opcodes.PUTFIELD, format("store to non-static field %s.%s", resolve(owner).getSimpleName(), fieldName)); | |
133 } | |
134 } | |
135 | |
136 private Executable resolveMethod(String owner, String methodName, String methodDesc) { | |
137 Class<?> declaringClass = resolve(owner); | |
138 if (methodName.equals("<init>")) { | |
139 for (Constructor<?> c : declaringClass.getDeclaredConstructors()) { | |
140 if (methodDesc.equals(Type.getConstructorDescriptor(c))) { | |
141 return c; | |
142 } | |
143 } | |
144 } else { | |
145 Type[] argumentTypes = Type.getArgumentTypes(methodDesc); | |
146 for (Method m : declaringClass.getDeclaredMethods()) { | |
147 if (m.getName().equals(methodName)) { | |
148 if (Arrays.equals(argumentTypes, Type.getArgumentTypes(m))) { | |
149 if (Type.getReturnType(methodDesc).equals(Type.getReturnType(m))) { | |
150 return m; | |
151 } | |
152 } | |
153 } | |
154 } | |
155 } | |
156 throw new NoSuchMethodError(declaringClass + "." + methodName + methodDesc); | |
157 } | |
158 | |
159 /** | |
160 * Checks whether a given method is allowed to be called. | |
161 */ | |
162 private boolean checkInvokeTarget(Executable method) { | |
163 Class<?> holder = method.getDeclaringClass(); | |
164 if (method instanceof Constructor) { | |
165 if (OptionValue.class.isAssignableFrom(holder)) { | |
166 return true; | |
167 } | |
168 } else if (Arrays.asList(boxingTypes).contains(holder)) { | |
169 return method.getName().equals("valueOf"); | |
170 } else if (method.getDeclaringClass().equals(Class.class)) { | |
171 return method.getName().equals("desiredAssertionStatus"); | |
172 } | |
173 return false; | |
174 } | |
175 | |
176 @Override | |
177 public void visitMethodInsn(int opcode, String owner, String methodName, String methodDesc, boolean itf) { | |
178 Executable callee = resolveMethod(owner, methodName, methodDesc); | |
179 verify(checkInvokeTarget(callee), "invocation of " + callee); | |
180 } | |
181 }; | |
182 } else { | |
183 return null; | |
184 } | |
185 } | |
186 } |