1 /* 2 * Copyright (c) 2016, 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 8033735 27 * @summary check backtrace field introspection 28 * @modules java.base/jdk.internal.misc 29 * @library /testlibrary 30 * @run main ThrowableIntrospectionSegfault 31 */ 32 33 import java.lang.reflect.*; 34 35 public class ThrowableIntrospectionSegfault { 36 public static void main(java.lang.String[] unused) { 37 // Construct a throwable object. 38 Throwable throwable = new Throwable(); 39 throwable.fillInStackTrace(); 40 41 // Retrieve a reflection handle to the private backtrace field. 42 Class class1 = throwable.getClass(); 43 Field field; 44 try { 45 field = class1.getDeclaredField("backtrace"); 46 } 47 catch (NoSuchFieldException e) { 48 System.err.println("Can't retrieve field handle Throwable.backtrace: " + e.toString()); 49 return; 50 } 51 field.setAccessible(true); 52 53 // Retrieve the value of the backtrace field. 54 Object backtrace; 55 try { 56 backtrace = field.get(throwable); 57 } 58 catch (IllegalAccessException e) { 59 System.err.println( "Can't retrieve field value for Throwable.backtrace: " + e.toString()); 60 return; 61 } 62 63 try { 64 65 // Retrieve the class of throwable.backtrace[0][0]. 66 Class class2 = ((Object[]) ((Object[]) backtrace)[2])[0].getClass(); 67 68 // Segfault occurs while executing this line, to retrieve the name of 69 // this class. 70 String class2Name = class2.getName(); 71 72 System.err.println("class2Name=" + class2Name); 73 return; // pass! Passes if it doesn't crash. 74 } catch (ClassCastException e) { 75 // Passes if it doesn't crash. Also if the backtrace changes this test might get 76 // ClassCastException and that's ok too. 77 System.out.println("Catch exception " + e); 78 return; // pass! Passes if it doesn't crash. 79 } 80 } 81 }