1 /*
   2  * Copyright (c) 2007, 2017, 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 //    THIS TEST IS LINE NUMBER SENSITIVE
  25 
  26 /**
  27  * @test
  28  * @bug 4359312 4450091
  29  * @summary Test PTR 1421 JVM exceptions making a call to LocalVariable.type().name()
  30  * @author Tim Bell (based on the PTR 1421 report submitted by IBM).
  31  *
  32  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  * @run compile -g GetLocalVariables.java
  34  * @run driver GetLocalVariables
  35  */
  36 
  37 import com.sun.jdi.*;
  38 import com.sun.jdi.event.*;
  39 import com.sun.jdi.request.*;
  40 
  41 import java.util.*;
  42 
  43  /********** target program **********/
  44 
  45 class GetLocalVariablesTarg {
  46     private static char s_char1 = 'a';
  47     private static char s_char2 = (char) 0;
  48     private static char s_char3 = (char) 1;
  49     private static char s_char4 = (char) 32;
  50     private static char s_char5 = '\u7ffe';
  51     private static char s_char6 = '\u7fff';
  52     private static char s_char7 = '\u8000';
  53     private static char s_char8 = '\u8001';
  54     private static char s_char9 = '\ufffe';
  55     private static char s_char10 = '\uffff';
  56 
  57     private static byte s_byte1 = (byte) 146;
  58     private static byte s_byte2 = (byte) 0;
  59     private static byte s_byte3 = (byte) 1;
  60     private static byte s_byte4 = (byte) 15;
  61     private static byte s_byte5 = (byte) 127;
  62     private static byte s_byte6 = (byte) 128;
  63     private static byte s_byte7 = (byte) - 1;
  64     private static byte s_byte8 = (byte) - 15;
  65     private static byte s_byte9 = (byte) - 127;
  66     private static byte s_byte10 = (byte) - 128;
  67 
  68     private static short s_short1 = (short) 28123;
  69     private static short s_short2 = (short) 0;
  70     private static short s_short3 = (short) 1;
  71     private static short s_short4 = (short) 15;
  72     private static short s_short5 = (short) 0x7ffe;
  73     private static short s_short6 = (short) 0x7fff;
  74     private static short s_short7 = (short) -1;
  75     private static short s_short8 = (short) -15;
  76     private static short s_short9 = (short) -0x7ffe;
  77     private static short s_short10 = (short) -0x7fff;
  78 
  79     private static int s_int1 = 3101246;
  80     private static int s_int2 = 0;
  81     private static int s_int3 = 1;
  82     private static int s_int4 = 15;
  83     private static int s_int5 = 0x7ffffffe;
  84     private static int s_int6 = 0x7fffffff;
  85     private static int s_int7 = -1;
  86     private static int s_int8 = -15;
  87     private static int s_int9 = -0x7ffffffe;
  88     private static int s_int10 = -0x7fffffff;
  89 
  90     private static long s_long1 = 0x0123456789ABCDEFL;
  91     private static long s_long2 = 0;
  92     private static long s_long3 = 1;
  93     private static long s_long4 = 15;
  94     private static long s_long5 = 0x000000007fffffffL;
  95     private static long s_long6 = 0x0000000080000000L;
  96     private static long s_long7 = 0x0000000100000000L;
  97     private static long s_long8 = 0x7fffffffffffffffL;
  98     private static long s_long9 = 0x8000000000000000L;
  99     private static long s_long10 = -15;
 100 
 101     private static float s_float1 = 2.3145f;
 102     private static float s_float2 = 0f;
 103 
 104     private static double s_double1 = 1.469d;
 105     private static double s_double2 = 0;
 106 
 107     private static int s_iarray1[] = {1, 2, 3};
 108     private static int s_iarray2[] = null;
 109 
 110     private static int s_marray1[][] = {{1, 2, 3}, {3, 4, 5}, null, {6, 7}};
 111     private static int s_marray2[][] = null;
 112 
 113     private static String s_sarray1[] = {"abc", null, "def", "ghi"};
 114     private static String s_sarray2[] = null;
 115     private static Object s_sarray3[] = s_sarray1;
 116 
 117     private static String s_string1 = "abcdef";
 118     private static String s_string2 = null;
 119     private static String s_string3 = "a\u1234b\u7777";
 120 
 121     private char i_char;
 122     private byte i_byte;
 123     private short i_short;
 124     private int i_int;
 125     private long i_long;
 126     private float i_float;
 127     private double i_double;
 128     private int i_iarray[];
 129     private int i_marray[][];
 130     private String i_string;
 131 
 132     public GetLocalVariablesTarg()
 133     {
 134         int index;
 135 
 136         i_char = 'B';
 137         i_byte = 120;
 138         i_short = 12048;
 139         i_int = 0x192842;
 140         i_long = 123591230941L;
 141         i_float = 235.15e5f;
 142         i_double = 176e-1d;
 143         i_iarray = new int[5];
 144         i_marray = new int[7][];
 145         i_string = "empty";
 146 
 147         for (index = 0; index < i_iarray.length; ++index)
 148             i_iarray[index] = index + 1;
 149 
 150         i_marray[0] = new int[2];
 151         i_marray[1] = new int[4];
 152         i_marray[2] = null;
 153         i_marray[3] = new int[1];
 154         i_marray[4] = new int[3];
 155         i_marray[5] = null;
 156         i_marray[6] = new int[7];
 157 
 158         for (index = 0; index < i_marray.length; ++index)
 159             if (i_marray[index] != null)
 160                 for (int index2 = 0; index2 < i_marray[index].length; ++index2)
 161                     i_marray[index][index2] = index + index2;
 162     }
 163 
 164     public GetLocalVariablesTarg(char p_char, byte p_byte, short p_short,
 165                         int p_int, long p_long, float p_float,
 166                         double p_double, int p_iarray[], int p_marray[][],
 167                         String p_string)
 168     {
 169         i_char = p_char;
 170         i_byte = p_byte;
 171         i_short = p_short;
 172         i_int = p_int;
 173         i_long = p_long;
 174         i_float = p_float;
 175         i_double = p_double;
 176         i_iarray = p_iarray;
 177         i_marray = p_marray;
 178         i_string = p_string;
 179     }
 180 
 181     public static void test_expressions()
 182     {
 183         GetLocalVariablesTarg e1 = new GetLocalVariablesTarg();
 184         GetLocalVariablesTarg e2 = null;
 185         GetLocalVariablesTarg e3 = e1;
 186         GetLocalVariablesTarg e4 = new GetLocalVariablesTarg(s_char1, s_byte1, s_short1, s_int1,
 187                                            s_long1, s_float1, s_double1,
 188                                            s_iarray1, s_marray1, "e4");
 189         GetLocalVariablesTarg e5 = new GetLocalVariablesTarg(s_char2, s_byte2, s_short2, s_int2,
 190                                            s_long2, s_float2, s_double2,
 191                                            s_iarray2, s_marray2, "e5");
 192 
 193         char l_char = (char) (
 194                            s_char1 + s_char2 + s_char3 + s_char4 + s_char5 +
 195                           s_char6 + s_char7 + s_char8 + s_char9 + s_char10);
 196         byte l_byte = (byte) (
 197                            s_byte1 + s_byte2 + s_byte3 + s_byte4 + s_byte5 +
 198                           s_byte6 + s_byte7 + s_byte8 + s_byte9 + s_byte10);
 199         short l_short = (short) (
 200                       s_short1 + s_short2 + s_short3 + s_short4 + s_short5 +
 201                      s_short6 + s_short7 + s_short8 + s_short9 + s_short10);
 202         int l_int = s_int1 + s_int2 + s_int3 + s_int4 + s_int5 +
 203         s_int6 + s_int7 + s_int8 + s_int9 + s_int10;
 204         long l_long = s_long1 + s_long2 + s_long3 + s_long4 + s_long5 +
 205         s_long6 + s_long7 + s_long8 + s_long9 + s_long10;
 206         float l_float = s_float1 + s_float2;
 207         double l_double = s_double1 + s_double2;
 208         int[] l_iarray = null;
 209         int[][] l_marray = null;
 210         String l_string = s_string1 + s_string3 + s_sarray1[0];
 211 
 212         if (s_sarray2 == null)
 213             l_string += "?";
 214 
 215         if (s_sarray3 instanceof String[])
 216             l_string += "<io>";
 217 
 218         Object e6 = new GetLocalVariablesTarg(l_char, l_byte, l_short, l_int,
 219                                      l_long, l_float, l_double, l_iarray,
 220                                      l_marray, l_string);
 221 
 222         e1.test_1();            // RESUME_TO_LINE
 223         e3.test_1();
 224         e4.test_1();
 225         e5.test_1();
 226         ((GetLocalVariablesTarg) e6).test_1();
 227 
 228         e3 = null;
 229         if (e3 == e1)
 230             e3.test_1();
 231         e3 = e4;
 232         if (e3 == e2)
 233             e3 = e5;
 234         e3.test_1();
 235 
 236     }
 237 
 238     public void test_1()
 239     {
 240         double l_add = i_double + i_short;
 241         long l_subtract = i_long - i_int;
 242         long l_multiply = i_byte * i_int;
 243 
 244         i_double = l_add + i_float;
 245         i_short = (short) l_subtract;
 246         i_long = l_multiply + i_byte + i_short;
 247     }
 248 
 249     public static void main(String[] args) {
 250         System.out.println("Howdy!");
 251         test_expressions();
 252         System.out.println("Goodbye from GetLocalVariablesTarg!");
 253     }
 254 }
 255 
 256  /********** test program **********/
 257 
 258 public class GetLocalVariables extends TestScaffold {
 259     static final int RESUME_TO_LINE = 222;
 260     ReferenceType targetClass;
 261     ThreadReference mainThread;
 262 
 263     GetLocalVariables (String args[]) {
 264         super(args);
 265     }
 266 
 267     public static void main(String[] args)
 268         throws Exception
 269     {
 270         new GetLocalVariables (args).startTests();
 271     }
 272 
 273     /********** test core **********/
 274 
 275     protected void runTests()
 276         throws Exception
 277     {
 278         /*
 279          * Get to the top of main() to determine targetClass and mainThread
 280          */
 281         BreakpointEvent bpe = startToMain("GetLocalVariablesTarg");
 282         targetClass = bpe.location().declaringType();
 283         mainThread = bpe.thread();
 284         EventRequestManager erm = vm().eventRequestManager();
 285 
 286         bpe = resumeTo("GetLocalVariablesTarg", RESUME_TO_LINE);
 287         /*
 288          * We've arrived.  Look around at some variables.
 289          */
 290         StackFrame frame = bpe.thread().frame(0);
 291         List localVars = frame.visibleVariables();
 292         System.out.println("    Visible variables at this point are: ");
 293         for (Iterator it = localVars.iterator(); it.hasNext();) {
 294             LocalVariable lv = (LocalVariable) it.next();
 295             System.out.print(lv.name());
 296             System.out.print(" typeName: ");
 297             System.out.print(lv.typeName());
 298             System.out.print(" signature: ");
 299             System.out.print(lv.type().signature());
 300             System.out.print(" primitive type: ");
 301             System.out.println(lv.type().name());
 302         }
 303 
 304         /*
 305          * resume the target listening for events
 306          */
 307         listenUntilVMDisconnect();
 308 
 309         /*
 310          * deal with results of test if anything has called failure("foo")
 311          * testFailed will be true
 312          */
 313         if (!testFailed) {
 314             println("GetLocalVariables: passed");
 315         } else {
 316             throw new Exception("GetLocalVariables: failed");
 317         }
 318     }
 319 }