|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, 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 jdk.graal.compiler.hotspot.phases; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Optional; |
| 30 | + |
| 31 | +import jdk.graal.compiler.debug.GraalError; |
| 32 | +import jdk.graal.compiler.nodes.AbstractMergeNode; |
| 33 | +import jdk.graal.compiler.nodes.DeoptimizingNode; |
| 34 | +import jdk.graal.compiler.nodes.FixedNode; |
| 35 | +import jdk.graal.compiler.nodes.FrameState; |
| 36 | +import jdk.graal.compiler.nodes.GraphState; |
| 37 | +import jdk.graal.compiler.nodes.StructuredGraph; |
| 38 | +import jdk.graal.compiler.nodes.extended.OSRMonitorEnterNode; |
| 39 | +import jdk.graal.compiler.nodes.extended.OSRStartNode; |
| 40 | +import jdk.graal.compiler.nodes.java.AccessMonitorNode; |
| 41 | +import jdk.graal.compiler.nodes.java.MonitorEnterNode; |
| 42 | +import jdk.graal.compiler.nodes.java.MonitorExitNode; |
| 43 | +import jdk.graal.compiler.nodes.java.MonitorIdNode; |
| 44 | +import jdk.graal.compiler.phases.Phase; |
| 45 | +import jdk.graal.compiler.phases.graph.MergeableState; |
| 46 | +import jdk.graal.compiler.phases.graph.PostOrderNodeIterator; |
| 47 | + |
| 48 | +/** |
| 49 | + * Ensure that the lock depths and {@link MonitorIdNode ids} agree with the enter and exits. |
| 50 | + */ |
| 51 | +public class VerifyLockDepthPhase extends Phase { |
| 52 | + |
| 53 | + @Override |
| 54 | + public Optional<NotApplicable> notApplicableTo(GraphState graphState) { |
| 55 | + /* |
| 56 | + * The current algorithm is only correct before PEA because it creates virtual locks, which |
| 57 | + * complicates checking for the correct correspondence because the FrameState can be out of |
| 58 | + * sync with the actual locking operations. |
| 59 | + */ |
| 60 | + return NotApplicable.unlessRunBefore(this, GraphState.StageFlag.PARTIAL_ESCAPE, graphState); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean shouldApply(StructuredGraph graph) { |
| 65 | + return graph.getNodes(MonitorIdNode.TYPE).isNotEmpty(); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void run(StructuredGraph graph) { |
| 70 | + // The current algorithm is only correct before PEA because it might virtual locks, which |
| 71 | + // complicates the matching. |
| 72 | + VerifyLockDepths verify = new VerifyLockDepths(graph.start()); |
| 73 | + verify.apply(); |
| 74 | + } |
| 75 | + |
| 76 | + public static class LockStructureError extends RuntimeException { |
| 77 | + private static final long serialVersionUID = 1L; |
| 78 | + |
| 79 | + LockStructureError(String message, Object... args) { |
| 80 | + super(String.format(message, args)); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + static class VerifyLockDepths extends PostOrderNodeIterator<VerifyLockDepths.LockingState> { |
| 85 | + VerifyLockDepths(FixedNode start) { |
| 86 | + super(start, null); |
| 87 | + state = new LockingState(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * The current state of locks which are held. |
| 92 | + */ |
| 93 | + static class LockingState extends MergeableState<LockingState> implements Cloneable { |
| 94 | + final ArrayList<MonitorIdNode> locks; |
| 95 | + |
| 96 | + LockingState() { |
| 97 | + locks = new ArrayList<>(); |
| 98 | + } |
| 99 | + |
| 100 | + LockingState(LockingState state) { |
| 101 | + this.locks = new ArrayList<>(state.locks); |
| 102 | + } |
| 103 | + |
| 104 | + public void pop(MonitorExitNode exit) { |
| 105 | + MonitorIdNode id = exit.getMonitorId(); |
| 106 | + if (locks.isEmpty()) { |
| 107 | + throw new LockStructureError("%s: lock stack is empty at", exit); |
| 108 | + } |
| 109 | + MonitorIdNode top = locks.removeLast(); |
| 110 | + if (top != id) { |
| 111 | + throw new LockStructureError(top + " != " + id); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public void push(MonitorEnterNode enter) { |
| 116 | + MonitorIdNode id = enter.getMonitorId(); |
| 117 | + if (locks.size() != id.getLockDepth()) { |
| 118 | + throw new LockStructureError("%s: lock depth mismatch %d != %d", enter, locks.size(), id.getLockDepth()); |
| 119 | + } |
| 120 | + locks.add(id); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public LockingState clone() { |
| 125 | + return new LockingState(this); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public boolean merge(AbstractMergeNode merge, List<LockingState> withStates) { |
| 130 | + for (LockingState other : withStates) { |
| 131 | + if (!locks.equals(other.locks)) { |
| 132 | + throw new LockStructureError("%s: lock depth mismatch %d != %d", merge, locks, other.locks); |
| 133 | + } |
| 134 | + } |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String toString() { |
| 140 | + return "LockingState{" + locks + '}'; |
| 141 | + } |
| 142 | + |
| 143 | + public void verifyState(FixedNode forNode, FrameState frameState) { |
| 144 | + if (frameState == null) { |
| 145 | + return; |
| 146 | + } |
| 147 | + if (forNode instanceof OSRStartNode || forNode instanceof OSRMonitorEnterNode) { |
| 148 | + /* |
| 149 | + * Ignore the FrameState on these nodes as the FrameState on the OSRStartNode is |
| 150 | + * used to construct the OSRMonitorEnterNodes which come after the start so |
| 151 | + * there's always an apparent mismatch. These nodes all the have same state and |
| 152 | + * it's only truly valid for the very last OSRMonitorEnterNode. |
| 153 | + */ |
| 154 | + return; |
| 155 | + } |
| 156 | + if (locks.size() != frameState.nestedLockDepth()) { |
| 157 | + throw new LockStructureError("Wrong number of locks held at %s: %d != %d", forNode, locks.size(), frameState.nestedLockDepth()); |
| 158 | + } |
| 159 | + FrameState current = frameState; |
| 160 | + int depth = locks.size(); |
| 161 | + while (current != null) { |
| 162 | + depth -= current.locksSize(); |
| 163 | + for (int i = 0; i < current.locksSize(); i++) { |
| 164 | + MonitorIdNode frameId = locks.get(depth + i); |
| 165 | + MonitorIdNode stackId = current.monitorIdAt(i); |
| 166 | + if (frameId != stackId) { |
| 167 | + throw new LockStructureError("%s: mismatched lock at depth % in %s: %s != %s", forNode, i, current, frameId, stackId); |
| 168 | + } |
| 169 | + } |
| 170 | + current = current.outerFrameState(); |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + protected void node(FixedNode node) { |
| 177 | + if (node instanceof DeoptimizingNode.DeoptBefore) { |
| 178 | + DeoptimizingNode.DeoptBefore before = (DeoptimizingNode.DeoptBefore) node; |
| 179 | + state.verifyState(before.asFixedNode(), before.stateBefore()); |
| 180 | + } |
| 181 | + if (node instanceof DeoptimizingNode.DeoptDuring) { |
| 182 | + DeoptimizingNode.DeoptDuring during = (DeoptimizingNode.DeoptDuring) node; |
| 183 | + state.verifyState(during.asFixedNode(), during.stateDuring()); |
| 184 | + } |
| 185 | + if (node instanceof MonitorEnterNode) { |
| 186 | + MonitorEnterNode enter = (MonitorEnterNode) node; |
| 187 | + state.push(enter); |
| 188 | + state.verifyState(node, enter.stateAfter()); |
| 189 | + } else if (node instanceof MonitorExitNode) { |
| 190 | + state.pop((MonitorExitNode) node); |
| 191 | + } else if (node instanceof AccessMonitorNode) { |
| 192 | + throw GraalError.shouldNotReachHere(node.getClass().toString()); |
| 193 | + } |
| 194 | + if (node instanceof DeoptimizingNode.DeoptAfter) { |
| 195 | + DeoptimizingNode.DeoptAfter after = (DeoptimizingNode.DeoptAfter) node; |
| 196 | + state.verifyState(after.asFixedNode(), after.stateAfter()); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments