Path: blob/master/test/hotspot/jtreg/runtime/6626217/bug_21227.java
40942 views
/*1* Copyright (c) 2010, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324/**25* @test26* @bug 662621727* @summary Loader-constraint table allows arrays instead of only the base-classes28* @library /test/lib29* @compile bug_21227.java from_loader2.java30* @run driver jdk.test.lib.helpers.ClassFileInstaller from_loader231* @compile impl2/many_loader.java32* @run driver jdk.test.lib.helpers.ClassFileInstaller many_loader33* @compile many_loader.java34* @run main/othervm -Xverify -Xint bug_2122735*/3637import java.lang.reflect.*;38import java.security.*;3940abstract public class bug_21227 {4142// Jam anything you want in here, it will be cast to a You_Have_Been_P0wned.43public static Object _p0wnee;4445public static void main(String argv[]) throws ClassNotFoundException, InstantiationException, IllegalAccessException {46try {47System.out.println("Warmup");4849// Make a Class 'many_loader' under the default loader.50bug_21227 bug = new many_loader();5152// Some classes under a new Loader, LOADER2, including another version of 'many_loader'.53ClassLoader LOADER2 = new Loader2();54Class clazz2 = LOADER2.loadClass("from_loader2");55IFace iface = (IFace)clazz2.newInstance();5657// Set the victim, a String of length 6.58String s = "victim";59_p0wnee = s;6061// Go cast '_p0wnee' to type You_Have_Been_P0wned.62many_loader[] x2 = bug.make(iface);6364many_loader b = x2[0];6566// Make it clear that the runtime type many_loader (what we get from the67// array X2) varies from the static type of many_loader.68Class cl1 = b.getClass();69ClassLoader ld1 = cl1.getClassLoader();70Class cl2 = many_loader.class;71ClassLoader ld2 = cl2.getClassLoader();72System.out.println("bug.make() "+ld1+":"+cl1);73System.out.println("many_loader "+ld2+":"+cl2);7475// Read the victims guts out.76You_Have_Been_P0wned q = b._p0wnee;77System.out.println("q._a = 0x"+Integer.toHexString(q._a));78System.out.println("q._b = 0x"+Integer.toHexString(q._b));79System.out.println("q._c = 0x"+Integer.toHexString(q._c));80System.out.println("q._d = 0x"+Integer.toHexString(q._d));8182System.out.println("I will now crash the VM:");83// On 32-bit HotSpot Java6 this sets the victim String length shorter, then crashes the VM.84//q._c = 3;85q._a = -1;8687System.out.println(s);88throw new RuntimeException("Expected LinkageError was not thrown.");89} catch (LinkageError e) {90String errorMsg = e.getMessage();91if (!errorMsg.contains("loader constraint")) {92throw new RuntimeException("Error message of LinkageError does not contain \"loader constraint\":" +93errorMsg);94}95System.out.println("Passed with message: " + errorMsg);96}97}9899// I need to compile (hence call in a loop) a function which returns a value100// loaded from classloader other than the system one. The point of this101// call is to give me an abstract 'hook' into a function loaded with a102// foreign loader.103public abstract many_loader[] make(IFace iface); // abstract factory104}105106107108