1 /*
   2  * Copyright (c) 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  * @test
  26  * @bug 7179701
  27  * @summary MaxJavaStackTraceDepth of zero is not handled correctly/consistently in the VM
  28  * @modules java.base/jdk.internal.misc:open
  29  * @modules java.base/java.lang:open
  30  * @library /test/lib
  31  * @run driver TestMaxJavaStackTraceDepth runTest
  32  */
  33 
  34 import java.lang.reflect.Field;
  35 import jdk.test.lib.Asserts;
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.process.ProcessTools;
  38 
  39 
  40 public class TestMaxJavaStackTraceDepth {
  41 
  42   static final int maxDepth = 2010;
  43 
  44   // Inner class that throws a lot of exceptions
  45   static class Thrower {
  46     int count = 0;
  47 
  48     int getDepth(Throwable t) throws Exception {
  49       Field f = Throwable.class.getDeclaredField("depth");
  50       f.setAccessible(true); // it's private
  51       return f.getInt(t);
  52     }
  53 
  54     void callThrow() throws Exception {
  55       if (++count < maxDepth) {
  56         callThrow();
  57       } else {
  58         throw new RuntimeException("depth tested " + maxDepth);
  59       }
  60     }
  61 
  62     void testThrow() throws Exception {
  63       try {
  64         count = getDepth(new Throwable()); // count stack to this point.
  65         callThrow();
  66       } catch(Exception e) {
  67         e.getStackTrace();
  68         System.out.println(e.getMessage());
  69         int throwableDepth = getDepth(e);
  70         System.out.println("java.lang.RuntimeException, " + throwableDepth);
  71       }
  72     }
  73   }
  74 
  75   public static void main(String args[]) throws Exception {
  76     if (args.length > 0) {
  77       // Test values of MaxJavaStackTraceDepth
  78       int[] depths = {0, 20, 1024};
  79       for (int d : depths) {
  80         System.out.println("running test with -XX:MaxJavaStackTraceDepth=" + d);
  81         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xlog:stacktrace=info",
  82                                                                   "-XX:MaxJavaStackTraceDepth=" + d,
  83                                                                   "--add-opens",
  84                                                                   "java.base/java.lang=ALL-UNNAMED",
  85                                                                   "TestMaxJavaStackTraceDepth");
  86         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  87         if (d == 0) {
  88           // Should get all the elements in stack trace
  89           output.shouldContain("java.lang.RuntimeException, " + maxDepth);
  90         } else {
  91           output.shouldContain("java.lang.RuntimeException, " + d);
  92         }
  93         output.shouldHaveExitValue(0);
  94       }
  95     } else {
  96       // run the test
  97       Thrower t = new Thrower();
  98       t.testThrow();
  99     }
 100   }
 101 }