jdk/test/vm/verifier/VerifyProtectedConstructor.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 32023 864c0bdf9b06
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @summary Verify that protected constructor calls are not allowed for classfile version >= 50 (but that they are allowed for lesser versions).
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    catch(VerifyError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
       throw new Exception("FAIL: should be no VerifyError for CF version 49");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
      t.checkClassVersion(50); // should throw VerifyError
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
      throw new Exception("FAIL: should be a VerifyError for CF version 50");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    catch(VerifyError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
       System.out.println("PASS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
  private void loadSuperClass() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /* -- code for super class A.A --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
       package A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
       public class A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
         protected A() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    long[] cls_data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      0xcafebabe00000032L, 0x000a0a0003000707L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
      0x0008070009010006L, 0x3c696e69743e0100L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      0x0328295601000443L, 0x6f64650c00040005L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
      0x010003412f410100L, 0x106a6176612f6c61L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
      0x6e672f4f626a6563L, 0x7400210002000300L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
      0x0000000001000400L, 0x0400050001000600L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
      0x0000110001000100L, 0x0000052ab70001b1L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      0x0000000000000000L // 2 bytes extra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    final int EXTRA = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    byte cf_bytes[] = toByteArray(cls_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    defineClass("A.A", cf_bytes, 0, cf_bytes.length - EXTRA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
  private int num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  private static String classNames[] = { "B.B", "C.C" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  private void checkClassVersion(int version) throws VerifyError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // This class is in violation of the spec since it accesses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // a protected constructor of a superclass while not being in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /* -- code for test class --
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        package B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public class B extends A.A {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
          public static void f() { new A.A(); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    long[] cls_data = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      0xcafebabe00000032L, 0x000b0a0002000807L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
      0x000907000a010006L, 0x3c696e69743e0100L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
      0x0328295601000443L, 0x6f6465010001660cL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
      0x0004000501000341L, 0x2f41010003422f42L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
      0x0021000300020000L, 0x0000000200010004L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
      0x0005000100060000L, 0x0011000100010000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      0x00052ab70001b100L, 0x0000000009000700L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      0x0500010006000000L, 0x1500020000000000L,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
      0x09bb000259b70001L, 0x57b1000000000000L // no extra bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    final int EXTRA = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    byte cf_bytes[] = toByteArray(cls_data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // set version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    cf_bytes[7] = (byte)version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    // Change B.B to C.C, D.D, ... for subsequent calls so we can call this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    // multiple times and define different classes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    cf_bytes[61] += num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    cf_bytes[63] += num_calls;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    String name = classNames[num_calls];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    num_calls++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    Class c = defineClass(name, cf_bytes, 0, cf_bytes.length - EXTRA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    try { c.newInstance(); } // to force linking, thus verification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    catch(InstantiationException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    catch(IllegalAccessException e) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  static private byte[] toByteArray(long arr[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // convert long array to byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    java.nio.ByteBuffer bbuf = java.nio.ByteBuffer.allocate(arr.length * 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    bbuf.asLongBuffer().put(java.nio.LongBuffer.wrap(arr));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    return bbuf.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
}