/jdk-11.0.2/src/jdk.internal.vm.compiler/org.graalvm.compiler.phases/src/org/graalvm/compiler/phases/verify/VerifyCallerSensitiveMethods.java

https://github.com/zxiaofan/JDK · Java · 97 lines · 59 code · 13 blank · 25 comment · 12 complexity · f245b45032c6b8baf876e1c7839dd6d2 MD5 · raw file

  1. /*
  2. * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
  3. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  4. *
  5. *
  6. *
  7. *
  8. *
  9. *
  10. *
  11. *
  12. *
  13. *
  14. *
  15. *
  16. *
  17. *
  18. *
  19. *
  20. *
  21. *
  22. */
  23. package org.graalvm.compiler.phases.verify;
  24. import static org.graalvm.compiler.serviceprovider.GraalServices.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 boolean 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. return true;
  71. }
  72. private Invoke callsReflectionGetCallerClass(StructuredGraph graph, PhaseContext context) {
  73. ResolvedJavaType reflectionType = context.getMetaAccess().lookupJavaType(reflectionClass);
  74. for (MethodCallTargetNode t : graph.getNodes(MethodCallTargetNode.TYPE)) {
  75. ResolvedJavaMethod callee = t.targetMethod();
  76. if (callee.getDeclaringClass().equals(reflectionType)) {
  77. if (callee.getName().equals("getCallerClass")) {
  78. return t.invoke();
  79. }
  80. }
  81. }
  82. return null;
  83. }
  84. }