52910
|
1 |
/*
|
58299
|
2 |
* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.
|
52910
|
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 |
|
|
24 |
|
|
25 |
package org.graalvm.compiler.core.test;
|
|
26 |
|
|
27 |
import java.lang.annotation.Annotation;
|
|
28 |
|
|
29 |
import org.graalvm.compiler.nodes.Invoke;
|
|
30 |
import org.graalvm.compiler.nodes.StructuredGraph;
|
|
31 |
import org.graalvm.compiler.nodes.java.MethodCallTargetNode;
|
55509
|
32 |
import org.graalvm.compiler.nodes.spi.CoreProviders;
|
52910
|
33 |
import org.graalvm.compiler.phases.VerifyPhase;
|
55509
|
34 |
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
|
52910
|
35 |
|
|
36 |
import jdk.vm.ci.meta.ResolvedJavaMethod;
|
|
37 |
import jdk.vm.ci.meta.ResolvedJavaType;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Verifies a method is annotated with CallerSensitive iff it calls Reflection#getCallerClass().
|
|
41 |
*/
|
55509
|
42 |
public class VerifyCallerSensitiveMethods extends VerifyPhase<CoreProviders> {
|
52910
|
43 |
|
|
44 |
Class<? extends Annotation> callerSensitiveClass;
|
|
45 |
Class<?> reflectionClass;
|
|
46 |
|
|
47 |
@Override
|
|
48 |
public boolean checkContract() {
|
|
49 |
return false;
|
|
50 |
}
|
|
51 |
|
|
52 |
@SuppressWarnings("unchecked")
|
|
53 |
public VerifyCallerSensitiveMethods() {
|
|
54 |
try {
|
|
55 |
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
|
55509
|
56 |
if (JavaVersionUtil.JAVA_SPEC <= 8) {
|
52910
|
57 |
reflectionClass = classLoader.loadClass("sun.reflect.Reflection");
|
|
58 |
callerSensitiveClass = (Class<? extends Annotation>) classLoader.loadClass("sun.reflect.ConstantPool");
|
|
59 |
} else {
|
|
60 |
reflectionClass = classLoader.loadClass("jdk.internal.reflect.Reflection");
|
|
61 |
callerSensitiveClass = (Class<? extends Annotation>) classLoader.loadClass("jdk.internal.reflect.ConstantPool");
|
|
62 |
}
|
|
63 |
} catch (ClassNotFoundException e) {
|
|
64 |
throw new AssertionError(e);
|
|
65 |
}
|
|
66 |
}
|
|
67 |
|
|
68 |
@Override
|
55509
|
69 |
protected void verify(StructuredGraph graph, CoreProviders context) {
|
52910
|
70 |
Invoke invoke = callsReflectionGetCallerClass(graph, context);
|
|
71 |
Annotation annotation = graph.method().getAnnotation(callerSensitiveClass);
|
|
72 |
if (invoke != null) {
|
|
73 |
if (annotation == null) {
|
|
74 |
StackTraceElement e = graph.method().asStackTraceElement(invoke.bci());
|
|
75 |
throw new VerificationError(String.format("%s: method that calls Reflection.getCallerClass() must be annotated with @CallerSensitive", e));
|
|
76 |
}
|
|
77 |
|
|
78 |
} else if (annotation != null) {
|
|
79 |
throw new VerificationError(String.format("%s: method annotated with @CallerSensitive does not call Reflection.getCallerClass()", graph.method().format("%H.%n(%p)")));
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
55509
|
83 |
private Invoke callsReflectionGetCallerClass(StructuredGraph graph, CoreProviders context) {
|
52910
|
84 |
ResolvedJavaType reflectionType = context.getMetaAccess().lookupJavaType(reflectionClass);
|
|
85 |
for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
|
|
86 |
ResolvedJavaMethod callee = t.targetMethod();
|
|
87 |
if (callee.getDeclaringClass().equals(reflectionType)) {
|
|
88 |
if (callee.getName().equals("getCallerClass")) {
|
|
89 |
return t.invoke();
|
|
90 |
}
|
|
91 |
}
|
|
92 |
}
|
|
93 |
return null;
|
|
94 |
}
|
|
95 |
}
|