Path: blob/master/test/hotspot/jtreg/runtime/BadObjectClass/TestUnloadClassError.java
40942 views
/*1* Copyright (c) 2018, 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/*23* @test24* @bug 619436125* @bug 820266926* @summary Make sure the VM doesn't crash and throws a SecurityException27* if defineClass() is called on a byte buffer that parses into a invalid28* java.lang.Object class.29* Also, make sure the vm doesn't crash on notification for unloading an invalid30* java.lang.Object class.31* @library /test/lib32* @modules java.base/jdk.internal.misc33* java.compiler34* @run main TestUnloadClassError35*/3637import jdk.test.lib.compiler.InMemoryJavaCompiler;38import jdk.test.lib.classloader.ClassUnloadCommon;3940public class TestUnloadClassError extends ClassLoader {4142static String source =43" package java.lang;" +44" public class Object" +45" {" +46" int field;" +47" public boolean equals(Object o) {" +48" System.out.println(o.field);" +49" return false;" +50" }" +51" }";5253public static void main(String[] args) throws Exception54{55try {56TestUnloadClassError loader = new TestUnloadClassError();57byte[] buf = InMemoryJavaCompiler.compile("java.lang.Object", source,58"--patch-module=java.base");59Class c = loader.defineClass(buf, 0, buf.length);60System.out.println("test FAILS");61throw new RuntimeException("Did not get security exception");62} catch(SecurityException e) {63System.out.println("test expects SecurityException");64}6566// Unload bad class67ClassUnloadCommon.triggerUnloading();68System.out.println("test PASSES if it doesn't crash");69}70}717273