1 /*
   2  * Copyright 2014 Google, Inc.  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  * @test
  26  * @bug 8043354
  27  * @summary  bcEscapeAnalyzer allocated_escapes not conservative enough
  28  * @run main/othervm -XX:CompileOnly=.visitAndPop TestAllocatedEscapesPtrComparison
  29  * @author Chuck Rasbold rasbold@google.com
  30  */
  31 
  32 /*
  33  * Test always passes with -XX:-OptmimizePtrCompare
  34  */
  35 
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 
  39 public class TestAllocatedEscapesPtrComparison {
  40 
  41   static TestAllocatedEscapesPtrComparison dummy;
  42 
  43   class Marker {
  44   }
  45 
  46   List<Marker> markerList = new ArrayList<>();
  47 
  48   // Suppress compilation of this method, it must be processed
  49   // by the bytecode escape analyzer.
  50 
  51   // Make a new marker and put it on the List
  52   Marker getMarker() {
  53     // result escapes through markerList
  54     final Marker result = new Marker();
  55     markerList.add(result);
  56     return result;
  57   }
  58 
  59   void visit(int depth) {
  60     // Make a new marker
  61     getMarker();
  62 
  63     // Call visitAndPop every once in a while
  64     // Cap the depth of our recursive visits
  65     if (depth % 10 == 2) {
  66       visitAndPop(depth + 1);
  67     } else if (depth < 15) {
  68       visit(depth + 1);
  69     }
  70   }
  71 
  72    void visitAndPop(int depth) {
  73     // Random dummy allocation to force EscapeAnalysis to process this method
  74     dummy = new TestAllocatedEscapesPtrComparison();
  75 
  76     // Make a new marker
  77     Marker marker = getMarker();
  78 
  79     visit(depth + 1);
  80 
  81     // Walk and pop the marker list up to the current marker
  82     boolean found = false;
  83     for (int i = markerList.size() - 1; i >= 0; i--) {
  84       Marker removed = markerList.remove(i);
  85 
  86       // In the failure, EA mistakenly converts this comparison to false
  87       if (removed == marker) {
  88         found = true;
  89         break;
  90       }
  91     }
  92 
  93     if (!found) {
  94       throw new RuntimeException("test fails");
  95     }
  96   }
  97 
  98 
  99   public static void main(String args[]) {
 100     TestAllocatedEscapesPtrComparison tc = new TestAllocatedEscapesPtrComparison();
 101 
 102     // Warmup and run enough times
 103     for (int i = 0; i < 20000; i++) {
 104       tc.visit(0);
 105     }
 106   }
 107 }