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