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 
  24 
  25 package org.graalvm.compiler.core.test;
  26 
  27 import org.graalvm.compiler.debug.DebugContext;
  28 import org.graalvm.compiler.debug.DebugDumpScope;
  29 import org.graalvm.compiler.nodes.ParameterNode;
  30 import org.graalvm.compiler.nodes.StructuredGraph;
  31 import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions;
  32 import org.graalvm.compiler.nodes.memory.FloatingReadNode;
  33 import org.graalvm.compiler.nodes.spi.LoweringTool;
  34 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  35 import org.graalvm.compiler.phases.common.FloatingReadPhase;
  36 import org.graalvm.compiler.phases.common.LoweringPhase;
  37 import org.graalvm.compiler.phases.tiers.PhaseContext;
  38 import org.junit.Assert;
  39 import org.junit.Test;
  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 {
  59 
  60         public long x1;
  61     }
  62 
  63     public static class B extends A {
  64 
  65         public long x10;
  66     }
  67 
  68     public static long test1Snippet(A a) {
  69         if (foo > 4) {
  70             B b = (B) a;
  71             b.x10 += 1;
  72             return b.x10;
  73         } else {
  74             B b = (B) 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         DebugContext debug = getDebugContext();
  88         try (DebugContext.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             canonicalizer.apply(graph, context);
  97 
  98             debug.dump(DebugContext.BASIC_LEVEL, graph, "After lowering");
  99 
 100             for (FloatingReadNode node : graph.getNodes(ParameterNode.TYPE).first().usages().filter(FloatingReadNode.class)) {
 101                 // Checking that the parameter a is not directly used for the access to field
 102                 // x10 (because x10 must be guarded by the checkcast).
 103                 Assert.assertTrue(node.getLocationIdentity().isImmutable());
 104             }
 105         } catch (Throwable e) {
 106             throw debug.handle(e);
 107         }
 108     }
 109 }