src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/ReadAfterCheckCastTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File hotspot Sdiff src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/ReadAfterCheckCastTest.java

Print this page




  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.core.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.debug.Debug;
  29 import org.graalvm.compiler.debug.Debug.Scope;
  30 import org.graalvm.compiler.debug.DebugDumpScope;
  31 import org.graalvm.compiler.nodes.ParameterNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.memory.FloatingReadNode;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  37 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  38 import org.graalvm.compiler.phases.common.LoweringPhase;
  39 import org.graalvm.compiler.phases.common.OptimizeGuardAnchorsPhase;
  40 import org.graalvm.compiler.phases.tiers.PhaseContext;
  41 
  42 /* consider
  43  *     B b = (B) a;
  44  *     return b.x10;
  45  *
  46  * With snippets a typecheck is performed and if it was successful, a PiNode is created.
  47  * For the read node, however, there is only a dependency to the PiNode, but not to the
  48  * typecheck itself. With special crafting, it's possible to get the scheduler moving the
  49  * FloatingReadNode before the typecheck. Assuming the object is of the wrong type (here for
  50  * example A), an invalid field read is done.
  51  *
  52  * In order to avoid this situation, an anchor node is introduced in CheckCastSnippts.
  53  */
  54 
  55 public class ReadAfterCheckCastTest extends GraphScheduleTest {
  56 
  57     public static long foo = 0;
  58 
  59     public static class A {


  76             b.x10 += 1;
  77             return b.x10;
  78         }
  79     }
  80 
  81     @Test
  82     public void test1() {
  83         test("test1Snippet");
  84     }
  85 
  86     @SuppressWarnings("try")
  87     private void test(final String snippet) {
  88         try (Scope s = Debug.scope("ReadAfterCheckCastTest", new DebugDumpScope(snippet))) {
  89             // check shape of graph, with lots of assumptions. will probably fail if graph
  90             // structure changes significantly
  91             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  92             PhaseContext context = new PhaseContext(getProviders());
  93             CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  94             new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  95             new FloatingReadPhase().apply(graph);
  96             new OptimizeGuardAnchorsPhase().apply(graph);
  97             canonicalizer.apply(graph, context);
  98 
  99             Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "After lowering");
 100 
 101             for (FloatingReadNode node : graph.getNodes(ParameterNode.TYPE).first().usages().filter(FloatingReadNode.class)) {
 102                 // Checking that the parameter a is not directly used for the access to field
 103                 // x10 (because x10 must be guarded by the checkcast).
 104                 Assert.assertTrue(node.getLocationIdentity().isImmutable());
 105             }
 106         } catch (Throwable e) {
 107             throw Debug.handle(e);
 108         }
 109     }
 110 }


  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.core.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.debug.Debug;
  29 import org.graalvm.compiler.debug.Debug.Scope;
  30 import org.graalvm.compiler.debug.DebugDumpScope;
  31 import org.graalvm.compiler.nodes.ParameterNode;
  32 import org.graalvm.compiler.nodes.StructuredGraph;
  33 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  34 import org.graalvm.compiler.nodes.memory.FloatingReadNode;
  35 import org.graalvm.compiler.nodes.spi.LoweringTool;
  36 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  37 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  38 import org.graalvm.compiler.phases.common.LoweringPhase;

  39 import org.graalvm.compiler.phases.tiers.PhaseContext;
  40 
  41 /* consider
  42  *     B b = (B) a;
  43  *     return b.x10;
  44  *
  45  * With snippets a typecheck is performed and if it was successful, a PiNode is created.
  46  * For the read node, however, there is only a dependency to the PiNode, but not to the
  47  * typecheck itself. With special crafting, it's possible to get the scheduler moving the
  48  * FloatingReadNode before the typecheck. Assuming the object is of the wrong type (here for
  49  * example A), an invalid field read is done.
  50  *
  51  * In order to avoid this situation, an anchor node is introduced in CheckCastSnippts.
  52  */
  53 
  54 public class ReadAfterCheckCastTest extends GraphScheduleTest {
  55 
  56     public static long foo = 0;
  57 
  58     public static class A {


  75             b.x10 += 1;
  76             return b.x10;
  77         }
  78     }
  79 
  80     @Test
  81     public void test1() {
  82         test("test1Snippet");
  83     }
  84 
  85     @SuppressWarnings("try")
  86     private void test(final String snippet) {
  87         try (Scope s = Debug.scope("ReadAfterCheckCastTest", new DebugDumpScope(snippet))) {
  88             // check shape of graph, with lots of assumptions. will probably fail if graph
  89             // structure changes significantly
  90             StructuredGraph graph = parseEager(snippet, AllowAssumptions.YES);
  91             PhaseContext context = new PhaseContext(getProviders());
  92             CanonicalizerPhase canonicalizer = new CanonicalizerPhase();
  93             new LoweringPhase(canonicalizer, LoweringTool.StandardLoweringStage.HIGH_TIER).apply(graph, context);
  94             new FloatingReadPhase().apply(graph);

  95             canonicalizer.apply(graph, context);
  96 
  97             Debug.dump(Debug.BASIC_LOG_LEVEL, graph, "After lowering");
  98 
  99             for (FloatingReadNode node : graph.getNodes(ParameterNode.TYPE).first().usages().filter(FloatingReadNode.class)) {
 100                 // Checking that the parameter a is not directly used for the access to field
 101                 // x10 (because x10 must be guarded by the checkcast).
 102                 Assert.assertTrue(node.getLocationIdentity().isImmutable());
 103             }
 104         } catch (Throwable e) {
 105             throw Debug.handle(e);
 106         }
 107     }
 108 }
src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.test/src/org/graalvm/compiler/core/test/ReadAfterCheckCastTest.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File