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