changeset 22480:1ef2ac5776c3

added check for correct usage of @CallerSensitive
author Doug Simon <doug.simon@oracle.com>
date Wed, 19 Aug 2015 11:09:29 +0200
parents 76796d99bcc9
children 5609b05ae0c6
files graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/CheckGraalInvariants.java graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyCallerSensitiveMethods.java
diffstat 2 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/CheckGraalInvariants.java	Tue Aug 18 20:54:23 2015 +0200
+++ b/graal/com.oracle.graal.compiler.test/src/com/oracle/graal/compiler/test/CheckGraalInvariants.java	Wed Aug 19 11:09:29 2015 +0200
@@ -225,6 +225,7 @@
             new VerifyUsageWithEquals(ArithmeticOpTable.Op.class).apply(graph, context);
         }
         new VerifyDebugUsage().apply(graph, context);
+        new VerifyCallerSensitiveMethods().apply(graph, context);
     }
 
     private static boolean matches(String[] filters, String s) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.graal.phases/src/com/oracle/graal/phases/verify/VerifyCallerSensitiveMethods.java	Wed Aug 19 11:09:29 2015 +0200
@@ -0,0 +1,66 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.graal.phases.verify;
+
+import jdk.internal.jvmci.meta.*;
+import sun.reflect.*;
+
+import com.oracle.graal.nodes.*;
+import com.oracle.graal.nodes.java.*;
+import com.oracle.graal.phases.*;
+import com.oracle.graal.phases.tiers.*;
+
+/**
+ * Verifies a method is annotated with {@link CallerSensitive} iff it calls
+ * {@link Reflection#getCallerClass()}.
+ */
+public class VerifyCallerSensitiveMethods extends VerifyPhase<PhaseContext> {
+
+    @Override
+    protected boolean verify(StructuredGraph graph, PhaseContext context) {
+        Invoke invoke = callsReflectionGetCallerClass(graph, context);
+        if (invoke != null) {
+            if (graph.method().getAnnotation(CallerSensitive.class) == null) {
+                StackTraceElement e = graph.method().asStackTraceElement(invoke.bci());
+                throw new VerificationError(String.format("%s: method that calls Reflection.getCallerClass() must be annotated with @CallerSensitive", e));
+            }
+
+        } else if (graph.method().getAnnotation(CallerSensitive.class) != null) {
+            throw new VerificationError(String.format("%s: method annotated with @CallerSensitive does not call Reflection.getCallerClass()", graph.method().format("%H.%n(%p)")));
+        }
+        return true;
+    }
+
+    private static Invoke callsReflectionGetCallerClass(StructuredGraph graph, PhaseContext context) {
+        for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
+            ResolvedJavaMethod callee = t.targetMethod();
+            ResolvedJavaType reflectionType = context.getMetaAccess().lookupJavaType(Reflection.class);
+            if (callee.getDeclaringClass().equals(reflectionType)) {
+                if (callee.getName().equals("getCallerClass")) {
+                    return t.invoke();
+                }
+            }
+        }
+        return null;
+    }
+}