/jdk-13/src/jdk.internal.vm.compiler/org.graalvm.compiler.hotspot/src/org/graalvm/compiler/hotspot/replacements/ReflectionGetCallerClassNode.java

https://github.com/zxiaofan/JDK · Java · 117 lines · 69 code · 13 blank · 35 comment · 10 complexity · 34c40f245ff0f19bd22eb5ec008e26a5 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.hotspot.replacements;
  24. import org.graalvm.compiler.core.common.type.StampPair;
  25. import org.graalvm.compiler.debug.GraalError;
  26. import org.graalvm.compiler.graph.Node;
  27. import org.graalvm.compiler.graph.NodeClass;
  28. import org.graalvm.compiler.graph.spi.Canonicalizable;
  29. import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  30. import org.graalvm.compiler.nodeinfo.NodeInfo;
  31. import org.graalvm.compiler.nodes.CallTargetNode.InvokeKind;
  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.ValueNode;
  36. import org.graalvm.compiler.nodes.spi.Lowerable;
  37. import org.graalvm.compiler.nodes.spi.LoweringTool;
  38. import org.graalvm.compiler.replacements.nodes.MacroStateSplitNode;
  39. import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
  40. import jdk.vm.ci.hotspot.HotSpotResolvedObjectType;
  41. import jdk.vm.ci.meta.ConstantReflectionProvider;
  42. import jdk.vm.ci.meta.MetaAccessProvider;
  43. import jdk.vm.ci.meta.ResolvedJavaMethod;
  44. @NodeInfo
  45. public final class ReflectionGetCallerClassNode extends MacroStateSplitNode implements Canonicalizable, Lowerable {
  46. public static final NodeClass<ReflectionGetCallerClassNode> TYPE = NodeClass.create(ReflectionGetCallerClassNode.class);
  47. public ReflectionGetCallerClassNode(InvokeKind invokeKind, ResolvedJavaMethod targetMethod, int bci, StampPair returnStamp, ValueNode... arguments) {
  48. super(TYPE, invokeKind, targetMethod, bci, returnStamp, arguments);
  49. }
  50. @Override
  51. public Node canonical(CanonicalizerTool tool) {
  52. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection());
  53. if (callerClassNode != null) {
  54. return callerClassNode;
  55. }
  56. return this;
  57. }
  58. @Override
  59. public void lower(LoweringTool tool) {
  60. ConstantNode callerClassNode = getCallerClassNode(tool.getMetaAccess(), tool.getConstantReflection());
  61. if (callerClassNode != null) {
  62. graph().replaceFixedWithFloating(this, graph().addOrUniqueWithInputs(callerClassNode));
  63. } else {
  64. InvokeNode invoke = createInvoke();
  65. graph().replaceFixedWithFixed(this, invoke);
  66. invoke.lower(tool);
  67. }
  68. }
  69. /**
  70. * If inlining is deep enough this method returns a {@link ConstantNode} of the caller class by
  71. * walking the stack.
  72. *
  73. * @param metaAccess
  74. * @return ConstantNode of the caller class, or null
  75. */
  76. private ConstantNode getCallerClassNode(MetaAccessProvider metaAccess, ConstantReflectionProvider constantReflection) {
  77. // Walk back up the frame states to find the caller at the required depth.
  78. FrameState state = stateAfter();
  79. // Cf. JVM_GetCallerClass
  80. // NOTE: Start the loop at depth 1 because the current frame state does
  81. // not include the Reflection.getCallerClass() frame.
  82. for (int n = 1; state != null; state = state.outerFrameState(), n++) {
  83. HotSpotResolvedJavaMethod method = (HotSpotResolvedJavaMethod) state.getMethod();
  84. switch (n) {
  85. case 0:
  86. throw GraalError.shouldNotReachHere("current frame state does not include the Reflection.getCallerClass frame");
  87. case 1:
  88. // Frame 0 and 1 must be caller sensitive (see JVM_GetCallerClass).
  89. if (!method.isCallerSensitive()) {
  90. return null; // bail-out; let JVM_GetCallerClass do the work
  91. }
  92. break;
  93. default:
  94. if (!method.ignoredBySecurityStackWalk()) {
  95. // We have reached the desired frame; return the holder class.
  96. HotSpotResolvedObjectType callerClass = method.getDeclaringClass();
  97. return ConstantNode.forConstant(constantReflection.asJavaClass(callerClass), metaAccess);
  98. }
  99. break;
  100. }
  101. }
  102. return null; // bail-out; let JVM_GetCallerClass do the work
  103. }
  104. }