1 /* 2 * Copyright (c) 2015, 2016, 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 * @test 26 * @summary Verifies the behaviour of Unsafe.defineClass 27 * @library /test/lib 28 * @modules java.base/jdk.internal.misc 29 * java.compiler 30 * java.management 31 * @run main DefineClass 32 */ 33 34 import java.security.ProtectionDomain; 35 import java.io.InputStream; 36 import jdk.test.lib.InMemoryJavaCompiler; 37 import jdk.test.lib.unsafe.UnsafeHelper; 38 import jdk.internal.misc.Unsafe; 39 import static jdk.test.lib.Asserts.*; 40 41 public class DefineClass { 42 public static void main(String args[]) throws Exception { 43 Unsafe unsafe = UnsafeHelper.getUnsafe(); 44 TestClassLoader classloader = new TestClassLoader(); 45 ProtectionDomain pd = new ProtectionDomain(null, null); 46 47 byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass", "class TestClass { }"); 48 49 // Invalid class data 50 try { 51 unsafe.defineClass(null, klassbuf, 4, klassbuf.length - 4, classloader, pd); 52 throw new RuntimeException("defineClass did not throw expected ClassFormatError"); 53 } catch (ClassFormatError e) { 54 // Expected 55 } 56 57 // Negative offset 58 try { 59 unsafe.defineClass(null, klassbuf, -1, klassbuf.length, classloader, pd); 60 throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException"); 61 } catch (IndexOutOfBoundsException e) { 62 // Expected 63 } 64 65 // Negative length 66 try { 67 unsafe.defineClass(null, klassbuf, 0, -1, classloader, pd); 68 throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException"); 69 } catch (IndexOutOfBoundsException e) { 70 // Expected 71 } 72 73 // Offset greater than klassbuf.length 74 try { 75 unsafe.defineClass(null, klassbuf, klassbuf.length + 1, klassbuf.length, classloader, pd); 76 throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException"); 77 } catch (IndexOutOfBoundsException e) { 78 // Expected 79 } 80 81 // Length greater than klassbuf.length 82 try { 83 unsafe.defineClass(null, klassbuf, 0, klassbuf.length + 1, classloader, pd); 84 throw new RuntimeException("defineClass did not throw expected IndexOutOfBoundsException"); 85 } catch (IndexOutOfBoundsException e) { 86 // Expected 87 } 88 89 Class klass = unsafe.defineClass(null, klassbuf, 0, klassbuf.length, classloader, pd); 90 assertEquals(klass.getClassLoader(), classloader); 91 assertEquals(klass.getProtectionDomain(), pd); 92 } 93 94 private static class TestClassLoader extends ClassLoader { 95 public TestClassLoader(ClassLoader parent) { 96 super(parent); 97 } 98 99 public TestClassLoader() { 100 super(); 101 } 102 } 103 }