/jdk-13/src/jdk.internal.vm.compiler/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/VerifyCallerSensitiveMethods.java

https://github.com/zxiaofan/JDK · Java · 96 lines · 58 code · 13 blank · 25 comment · 12 complexity · 1f4602dd5f0377b1eb2ae48d4621bd23 MD5 · raw file

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