/compiler/src/org.graalvm.compiler.replacements/src/org/graalvm/compiler/replacements/nodes/ReflectionGetCallerClassNode.java

https://github.com/oracle/graal · Java · 115 lines · 66 code · 12 blank · 37 comment · 10 complexity · ebde441807c4ea17976f85c3db5504a1 MD5 · raw file

  1. /*
  2. * Copyright (c) 2013, 2020, 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. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package org.graalvm.compiler.replacements.nodes;
  26. import org.graalvm.compiler.debug.GraalError;
  27. import org.graalvm.compiler.graph.Node;
  28. import org.graalvm.compiler.graph.NodeClass;
  29. import org.graalvm.compiler.graph.spi.Canonicalizable;
  30. import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  31. import org.graalvm.compiler.nodeinfo.NodeInfo;
  32. import org.graalvm.compiler.nodes.ConstantNode;
  33. import org.graalvm.compiler.nodes.FrameState;
  34. import org.graalvm.compiler.nodes.InvokeNode;
  35. import org.graalvm.compiler.nodes.spi.Lowerable;
  36. import org.graalvm.compiler.nodes.spi.LoweringTool;
  37. import jdk.vm.ci.meta.ConstantReflectionProvider;
  38. import jdk.vm.ci.meta.MetaAccessProvider;
  39. import jdk.vm.ci.meta.ResolvedJavaMethod;
  40. import jdk.vm.ci.meta.ResolvedJavaType;
  41. @NodeInfo
  42. public abstract class ReflectionGetCallerClassNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
  43. public static final NodeClass<ReflectionGetCallerClassNode> TYPE = NodeClass.create(ReflectionGetCallerClassNode.class);
  44. protected ReflectionGetCallerClassNode(NodeClass<? extends ReflectionGetCallerClassNode> c, MacroParams p) {
  45. super(c, p);
  46. }
  47. @Override
  48. public Node canonical(CanonicalizerTool tool) {
  49. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection());
  50. if (callerClassNode != null) {
  51. return callerClassNode;
  52. }
  53. return this;
  54. }
  55. @Override
  56. public void lower(LoweringTool tool) {
  57. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection());
  58. if (callerClassNode != null) {
  59. graph().replaceFixedWithFloating(this, graph().addOrUniqueWithInputs(callerClassNode));
  60. } else {
  61. InvokeNode invoke = createInvoke();
  62. graph().replaceFixedWithFixed(this, invoke);
  63. invoke.lower(tool);
  64. }
  65. }
  66. /**
  67. * If inlining is deep enough this method returns a {@link ConstantNode} of the caller class by
  68. * walking the stack.
  69. *
  70. * @param metaAccess
  71. * @return ConstantNode of the caller class, or null
  72. */
  73. private ConstantNode getCallerClassNode(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  74. // Walk back up the frame states to find the caller at the required depth.
  75. FrameState state = stateAfter();
  76. // Cf. JVM_GetCallerClass
  77. // NOTE: Start the loop at depth 1 because the current frame state does
  78. // not include the Reflection.getCallerClass() frame.
  79. for (int n = 1; state != null; state = state.outerFrameState(), n++) {
  80. ResolvedJavaMethod method = state.getMethod();
  81. switch (n) {
  82. case 0:
  83. throw GraalError.shouldNotReachHere("current frame state does not include the Reflection.getCallerClass frame");
  84. case 1:
  85. // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
  86. if (!isCallerSensitive(method)) {
  87. return null; // bail-out; let JVM_GetCallerClass do the work
  88. }
  89. break;
  90. default:
  91. if (!ignoredBySecurityStackWalk(method)) {
  92. // We have reached the desired frame; return the holder class.
  93. ResolvedJavaType callerClass = method.getDeclaringClass();
  94. return ConstantNode.forConstant(constantReflection.asJavaClass(callerClass), metaAccess);
  95. }
  96. break;
  97. }
  98. }
  99. return null; // bail-out; let JVM_GetCallerClass do the work
  100. }
  101. protected abstract boolean isCallerSensitive(ResolvedJavaMethod method);
  102. protected abstract boolean ignoredBySecurityStackWalk(ResolvedJavaMethod method);
  103. }