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 4185411 27 * @summary This program crashes in 1.1, but runs okay in 1.2. 28 * @run main/othervm -Xss512k ReflectStackOverflow 29 */ 30 import java.lang.reflect.*; 31 32 public class ReflectStackOverflow { 33 private static final int COUNT = 11000; 34 35 public static void main(String[] cmdline) throws Throwable { 36 for (int i = 0; i < COUNT+1; i++) { 37 stuff(i); 38 } 39 } 40 41 private static void stuff(int count) throws Throwable { 42 if (count < COUNT) 43 return; // don't do anything the first COUNT times. 44 45 try { 46 final Method method = 47 Method.class.getMethod 48 ("invoke", new Class[] { Object.class, Object[].class }); 49 50 final Object[] args = new Object[] { method, null }; 51 args[1] = args; 52 53 method.invoke(method, args); // "recursive reflection" 54 // exception should have been thrown by now... 55 System.out.println("how did I get here?"); 56 } catch(Throwable t) { 57 int layers; 58 for(layers = 0; t instanceof InvocationTargetException; layers++) 59 t = ((InvocationTargetException)t).getTargetException(); 60 61 System.err.println("Found " + layers + " layers of wrappers."); 62 if (!(t instanceof StackOverflowError)) { 63 throw t; 64 } 65 } 66 } 67 }