1 /*
   2  * Copyright (c) 2010, 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 /**
  26  * @test
  27  * @bug 6626217
  28  * @summary Loader-constraint table allows arrays instead of only the base-classes
  29  * @library /test/lib
  30  * @compile bug_21227.java from_loader2.java
  31  * @run driver ClassFileInstaller from_loader2
  32  * @compile impl2/many_loader.java
  33  * @run driver ClassFileInstaller many_loader
  34  * @compile many_loader.java
  35  * @run main/othervm -Xverify -Xint bug_21227
  36  */
  37 
  38 import java.lang.reflect.*;
  39 import java.security.*;
  40 
  41 abstract public class bug_21227 {
  42 
  43     // Jam anything you want in here, it will be cast to a You_Have_Been_P0wned.
  44     public static Object _p0wnee;
  45 
  46     public static void main(String argv[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  47         try {
  48             System.out.println("Warmup");
  49 
  50             // Make a Class 'many_loader' under the default loader.
  51             bug_21227 bug = new many_loader();
  52 
  53             // Some classes under a new Loader, LOADER2, including another version of 'many_loader'.
  54             ClassLoader LOADER2 = new Loader2();
  55             Class clazz2 = LOADER2.loadClass("from_loader2");
  56             IFace iface = (IFace)clazz2.newInstance();
  57 
  58             // Set the victim, a String of length 6.
  59             String s = "victim";
  60             _p0wnee = s;
  61 
  62             // Go cast '_p0wnee' to type You_Have_Been_P0wned.
  63             many_loader[] x2 = bug.make(iface);
  64 
  65             many_loader b = x2[0];
  66 
  67             // Make it clear that the runtime type many_loader (what we get from the
  68             // array X2) varies from the static type of many_loader.
  69             Class cl1 = b.getClass();
  70             ClassLoader ld1 = cl1.getClassLoader();
  71             Class cl2 = many_loader.class;
  72             ClassLoader ld2 = cl2.getClassLoader();
  73             System.out.println("bug.make()  "+ld1+":"+cl1);
  74             System.out.println("many_loader "+ld2+":"+cl2);
  75 
  76             // Read the victims guts out.
  77             You_Have_Been_P0wned q = b._p0wnee;
  78             System.out.println("q._a = 0x"+Integer.toHexString(q._a));
  79             System.out.println("q._b = 0x"+Integer.toHexString(q._b));
  80             System.out.println("q._c = 0x"+Integer.toHexString(q._c));
  81             System.out.println("q._d = 0x"+Integer.toHexString(q._d));
  82 
  83             System.out.println("I will now crash the VM:");
  84             // On 32-bit HotSpot Java6 this sets the victim String length shorter, then crashes the VM.
  85             //q._c = 3;
  86             q._a = -1;
  87 
  88             System.out.println(s);
  89             throw new RuntimeException("Expected LinkageError was not thrown.");
  90         } catch (LinkageError e) {
  91             String errorMsg = e.getMessage();
  92             if (!errorMsg.contains("")) {
  93                 throw new RuntimeException("Error message of LinkageError does not contain \"loader constraint\":" +
  94                                            errorMsg);
  95             }
  96             System.out.println("Passed with message: " + errorMsg);
  97         }
  98     }
  99 
 100     // I need to compile (hence call in a loop) a function which returns a value
 101     // loaded from classloader other than the system one.  The point of this
 102     // call is to give me an abstract 'hook' into a function loaded with a
 103     // foreign loader.
 104     public abstract many_loader[] make(IFace iface); // abstract factory
 105 }
 106