jdk/test/vm/verifier/VerifyProtectedConstructor.java
author hseigel
Wed, 01 Mar 2017 08:00:02 -0500
changeset 46194 5596e6f63072
parent 32023 864c0bdf9b06
permissions -rw-r--r--
8172307: Remove ununsed JVM API JVM_GetModuleByPackageName() Summary: Remove get_module_by_package_name() etc., and unneeded test. Reviewed-by: sspitsyn, gtriantafill
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
32023
864c0bdf9b06 8081695: Old verifier fails to reject bad access to protected <init> method
hseigel
parents: 5506
diff changeset
     2
 * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @bug 6490436
32023
864c0bdf9b06 8081695: Old verifier fails to reject bad access to protected <init> method
hseigel
parents: 5506
diff changeset
    29
 * @summary Verify that protected constructor calls are not allowed for any classfile versions in either verifier.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Keith McGuigan
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class VerifyProtectedConstructor extends ClassLoader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
  public static void main(String argv[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    VerifyProtectedConstructor t = new VerifyProtectedConstructor();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    t.loadSuperClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
      t.checkClassVersion(49); // should not throw VerifyError
32023
864c0bdf9b06 8081695: Old verifier fails to reject bad access to protected <init> method
hseigel
parents: 5506
diff changeset
    41
      throw new Exception("FAIL: should be a VerifyError for CF version 49");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    catch(VerifyError e) {
32023
864c0bdf9b06 8081695: Old verifier fails to reject bad access to protected <init> method
hseigel
parents: 5506
diff changeset
    44
       System.out.println("PASS for CF version 49");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
      t.checkClassVersion(50); // should throw VerifyError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
      throw new Exception("FAIL: should be a VerifyError for CF version 50");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    catch(VerifyError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
       System.out.println("PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
  private void loadSuperClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* -- code for super class A.A --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
       package A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
       public class A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
         protected A() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    long[] cls_data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      0xcafebabe00000032L, 0x000a0a0003000707L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      0x0008070009010006L, 0x3c696e69743e0100L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      0x0328295601000443L, 0x6f64650c00040005L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      0x010003412f410100L, 0x106a6176612f6c61L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      0x6e672f4f626a6563L, 0x7400210002000300L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      0x0000000001000400L, 0x0400050001000600L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      0x0000110001000100L, 0x0000052ab70001b1L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
      0x0000000000000000L // 2 bytes extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    final int EXTRA = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    byte cf_bytes[] = toByteArray(cls_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    defineClass("A.A", cf_bytes, 0, cf_bytes.length - EXTRA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  private int num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
  private static String classNames[] = { "B.B", "C.C" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
  private void checkClassVersion(int version) throws VerifyError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // This class is in violation of the spec since it accesses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // a protected constructor of a superclass while not being in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* -- code for test class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        package B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        public class B extends A.A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
          public static void f() { new A.A(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    long[] cls_data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      0xcafebabe00000032L, 0x000b0a0002000807L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      0x000907000a010006L, 0x3c696e69743e0100L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      0x0328295601000443L, 0x6f6465010001660cL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      0x0004000501000341L, 0x2f41010003422f42L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      0x0021000300020000L, 0x0000000200010004L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      0x0005000100060000L, 0x0011000100010000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      0x00052ab70001b100L, 0x0000000009000700L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      0x0500010006000000L, 0x1500020000000000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
      0x09bb000259b70001L, 0x57b1000000000000L // no extra bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    final int EXTRA = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    byte cf_bytes[] = toByteArray(cls_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // set version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    cf_bytes[7] = (byte)version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // Change B.B to C.C, D.D, ... for subsequent calls so we can call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    // multiple times and define different classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    cf_bytes[61] += num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    cf_bytes[63] += num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    String name = classNames[num_calls];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    num_calls++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    Class c = defineClass(name, cf_bytes, 0, cf_bytes.length - EXTRA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    try { c.newInstance(); } // to force linking, thus verification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    catch(InstantiationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    catch(IllegalAccessException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
  static private byte[] toByteArray(long arr[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    // convert long array to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    java.nio.ByteBuffer bbuf = java.nio.ByteBuffer.allocate(arr.length * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    bbuf.asLongBuffer().put(java.nio.LongBuffer.wrap(arr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    return bbuf.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}