Skip to content

Commit c176991

Browse files
committed
Do not allow interleaved recursive locks to be virtualized.
1 parent 51994b1 commit c176991

File tree

5 files changed

+62
-4
lines changed

5 files changed

+62
-4
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/hotspot/test/MonitorPEATest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,4 +234,44 @@ public static void snippet6() {
234234
public void testSnippet6() {
235235
test("snippet6");
236236
}
237+
238+
public static void snippet7() {
239+
B b = new B();
240+
C c = new C(b);
241+
synchronized (c) {
242+
synchronized (c) {
243+
synchronized (b) {
244+
synchronized (b) {
245+
synchronized (B.class) {
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
253+
@Test
254+
public void testSnippet7() {
255+
test("snippet7");
256+
}
257+
258+
public static void snippet8() {
259+
B b = new B();
260+
C c = new C(b);
261+
synchronized (c) {
262+
synchronized (b) {
263+
synchronized (c) {
264+
synchronized (b) {
265+
synchronized (B.class) {
266+
}
267+
}
268+
}
269+
}
270+
}
271+
}
272+
273+
@Test
274+
public void testSnippet8() {
275+
test("snippet8");
276+
}
237277
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/java/MonitorEnterNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public void virtualize(VirtualizerTool tool) {
104104
ValueNode alias = tool.getAlias(object());
105105
if (alias instanceof VirtualObjectNode) {
106106
VirtualObjectNode virtual = (VirtualObjectNode) alias;
107-
if (virtual.hasIdentity()) {
107+
if (virtual.hasIdentity() && tool.canVirtualizeLock(virtual, getMonitorId())) {
108108
tool.addLock(virtual, getMonitorId());
109109
if (!tool.getPlatformConfigurationProvider().areLocksSideEffectFree()) {
110110
// Ensure that the locks appear to have been acquired in the nearest FrameState.

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/spi/VirtualizerTool.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@
3030
import jdk.graal.compiler.debug.GraalError;
3131
import jdk.graal.compiler.graph.Node;
3232
import jdk.graal.compiler.graph.NodeSourcePosition;
33-
import jdk.graal.compiler.nodes.java.MonitorIdNode;
34-
import jdk.graal.compiler.nodes.virtual.VirtualObjectNode;
3533
import jdk.graal.compiler.nodes.ValueNode;
3634
import jdk.graal.compiler.nodes.WithExceptionNode;
35+
import jdk.graal.compiler.nodes.java.MonitorIdNode;
36+
import jdk.graal.compiler.nodes.virtual.VirtualObjectNode;
3737
import jdk.graal.compiler.options.OptionValues;
38-
3938
import jdk.vm.ci.meta.JavaKind;
4039

4140
/**
@@ -97,6 +96,8 @@ default void setVirtualEntry(VirtualObjectNode virtualObject, int index, ValueNo
9796

9897
ValueNode getEntry(VirtualObjectNode virtualObject, int index);
9998

99+
boolean canVirtualizeLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId);
100+
100101
void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId);
101102

102103
MonitorIdNode removeLock(VirtualObjectNode virtualObject);

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/virtual/phases/ea/PartialEscapeBlockState.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,14 @@ public void escape(int object, ValueNode materialized) {
172172
getObjectStateForModification(object).escape(materialized);
173173
}
174174

175+
public boolean isNonImmediateRecursiveLock(int object, MonitorIdNode monitorId) {
176+
ObjectState state = getObjectStateForModification(object);
177+
if (state.hasLocks()) {
178+
return state.getLocks().monitorId.getLockDepth() < monitorId.getLockDepth() - 1;
179+
}
180+
return false;
181+
}
182+
175183
public void addLock(int object, MonitorIdNode monitorId) {
176184
getObjectStateForModification(object).addLock(monitorId);
177185
}

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/virtual/phases/ea/VirtualizerToolImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ public boolean ensureMaterialized(VirtualObjectNode virtualObject) {
364364
return closure.ensureMaterialized(state, virtualObject.getObjectId(), position, effects, PartialEscapeClosure.COUNTER_MATERIALIZATIONS_UNHANDLED);
365365
}
366366

367+
@Override
368+
public boolean canVirtualizeLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) {
369+
if (getPlatformConfigurationProvider().requiresStrictLockOrder()) {
370+
int id = virtualObject.getObjectId();
371+
return !state.isNonImmediateRecursiveLock(id, monitorId);
372+
}
373+
return true;
374+
}
375+
367376
@Override
368377
public void addLock(VirtualObjectNode virtualObject, MonitorIdNode monitorId) {
369378
int id = virtualObject.getObjectId();

0 commit comments

Comments
 (0)