Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/vm/verifier/VerifyProtectedConstructor.java
38833 views
/*1* Copyright (c) 2007, 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*/222324/**25* @test2627* @bug 649043628* @summary Verify that protected constructor calls are not allowed for classfile version >= 50 (but that they are allowed for lesser versions).29* @author Keith McGuigan30*/3132public class VerifyProtectedConstructor extends ClassLoader {33public static void main(String argv[]) throws Exception {34VerifyProtectedConstructor t = new VerifyProtectedConstructor();3536t.loadSuperClass();3738try {39t.checkClassVersion(49); // should not throw VerifyError40}41catch(VerifyError e) {42throw new Exception("FAIL: should be no VerifyError for CF version 49");43}4445try {46t.checkClassVersion(50); // should throw VerifyError47throw new Exception("FAIL: should be a VerifyError for CF version 50");48}49catch(VerifyError e) {50System.out.println("PASS");51}52}5354private void loadSuperClass() {55/* -- code for super class A.A --56package A;57public class A {58protected A() {}59}60*/61long[] cls_data = {620xcafebabe00000032L, 0x000a0a0003000707L,630x0008070009010006L, 0x3c696e69743e0100L,640x0328295601000443L, 0x6f64650c00040005L,650x010003412f410100L, 0x106a6176612f6c61L,660x6e672f4f626a6563L, 0x7400210002000300L,670x0000000001000400L, 0x0400050001000600L,680x0000110001000100L, 0x0000052ab70001b1L,690x0000000000000000L // 2 bytes extra70};71final int EXTRA = 2;72byte cf_bytes[] = toByteArray(cls_data);73defineClass("A.A", cf_bytes, 0, cf_bytes.length - EXTRA);74}7576private int num_calls;77private static String classNames[] = { "B.B", "C.C" };7879private void checkClassVersion(int version) throws VerifyError {80// This class is in violation of the spec since it accesses81// a protected constructor of a superclass while not being in the82// same package.83/* -- code for test class --84package B;85public class B extends A.A {86public static void f() { new A.A(); }87}88*/89long[] cls_data = {900xcafebabe00000032L, 0x000b0a0002000807L,910x000907000a010006L, 0x3c696e69743e0100L,920x0328295601000443L, 0x6f6465010001660cL,930x0004000501000341L, 0x2f41010003422f42L,940x0021000300020000L, 0x0000000200010004L,950x0005000100060000L, 0x0011000100010000L,960x00052ab70001b100L, 0x0000000009000700L,970x0500010006000000L, 0x1500020000000000L,980x09bb000259b70001L, 0x57b1000000000000L // no extra bytes99};100final int EXTRA = 0;101102byte cf_bytes[] = toByteArray(cls_data);103104// set version105cf_bytes[7] = (byte)version;106107// Change B.B to C.C, D.D, ... for subsequent calls so we can call this108// multiple times and define different classes.109cf_bytes[61] += num_calls;110cf_bytes[63] += num_calls;111String name = classNames[num_calls];112num_calls++;113114Class c = defineClass(name, cf_bytes, 0, cf_bytes.length - EXTRA);115116try { c.newInstance(); } // to force linking, thus verification117catch(InstantiationException e) {}118catch(IllegalAccessException e) {}119}120121static private byte[] toByteArray(long arr[]) {122// convert long array to byte array123java.nio.ByteBuffer bbuf = java.nio.ByteBuffer.allocate(arr.length * 8);124bbuf.asLongBuffer().put(java.nio.LongBuffer.wrap(arr));125return bbuf.array();126}127}128129130