test/compiler/7184394/TestAESBase.java

Mon, 28 Jul 2014 15:06:38 -0700

author
fzhinkin
date
Mon, 28 Jul 2014 15:06:38 -0700
changeset 6997
dbb05f6d93c4
parent 6653
03214612e77e
child 6876
710a3c8b516e
child 9788
44ef77ad417c
permissions
-rw-r--r--

8051344: JVM crashed in Compile::start() during method parsing w/ UseRTMDeopt turned on
Summary: call rtm_deopt() only if there were no compilation bailouts before.
Reviewed-by: kvn

     1 /*
     2  * Copyright (c) 2012, 2014, 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  */
    25 /**
    26  * @author Tom Deneau
    27  */
    29 import javax.crypto.Cipher;
    30 import javax.crypto.KeyGenerator;
    31 import javax.crypto.SecretKey;
    32 import javax.crypto.spec.IvParameterSpec;
    33 import javax.crypto.spec.SecretKeySpec;
    34 import java.security.AlgorithmParameters;
    36 import java.util.Random;
    37 import java.util.Arrays;
    39 abstract public class TestAESBase {
    40   int msgSize = Integer.getInteger("msgSize", 646);
    41   boolean checkOutput = Boolean.getBoolean("checkOutput");
    42   boolean noReinit = Boolean.getBoolean("noReinit");
    43   boolean testingMisalignment;
    44   private static final int ALIGN = 8;
    45   int encInputOffset = Integer.getInteger("encInputOffset", 0) % ALIGN;
    46   int encOutputOffset = Integer.getInteger("encOutputOffset", 0) % ALIGN;
    47   int decOutputOffset = Integer.getInteger("decOutputOffset", 0) % ALIGN;
    48   int lastChunkSize = Integer.getInteger("lastChunkSize", 32);
    49   int keySize = Integer.getInteger("keySize", 128);
    50   int inputLength;
    51   int encodeLength;
    52   int decodeLength;
    53   int decodeMsgSize;
    54   String algorithm = System.getProperty("algorithm", "AES");
    55   String mode = System.getProperty("mode", "CBC");
    56   String paddingStr = System.getProperty("paddingStr", "PKCS5Padding");
    57   byte[] input;
    58   byte[] encode;
    59   byte[] expectedEncode;
    60   byte[] decode;
    61   byte[] expectedDecode;
    62   Random random = new Random(0);
    63   Cipher cipher;
    64   Cipher dCipher;
    65   AlgorithmParameters algParams;
    66   SecretKey key;
    68   static int numThreads = 0;
    69   int  threadId;
    70   static synchronized int getThreadId() {
    71     int id = numThreads;
    72     numThreads++;
    73     return id;
    74   }
    76   abstract public void run();
    78   public void prepare() {
    79     try {
    80     System.out.println("\nalgorithm=" + algorithm + ", mode=" + mode + ", paddingStr=" + paddingStr + ", msgSize=" + msgSize + ", keySize=" + keySize + ", noReinit=" + noReinit + ", checkOutput=" + checkOutput + ", encInputOffset=" + encInputOffset + ", encOutputOffset=" + encOutputOffset + ", decOutputOffset=" + decOutputOffset + ", lastChunkSize=" +lastChunkSize );
    82       if (encInputOffset % ALIGN != 0 || encOutputOffset % ALIGN != 0 || decOutputOffset % ALIGN !=0 )
    83         testingMisalignment = true;
    85       int keyLenBytes = (keySize == 0 ? 16 : keySize/8);
    86       byte keyBytes[] = new byte[keyLenBytes];
    87       if (keySize == 128)
    88         keyBytes = new byte[] {-8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
    89       else
    90         random.nextBytes(keyBytes);
    92       key = new SecretKeySpec(keyBytes, algorithm);
    93       if (threadId == 0) {
    94         System.out.println("Algorithm: " + key.getAlgorithm() + "("
    95                            + key.getEncoded().length * 8 + "bit)");
    96       }
    98       cipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
    99       dCipher = Cipher.getInstance(algorithm + "/" + mode + "/" + paddingStr, "SunJCE");
   101       if (mode.equals("CBC")) {
   102         int ivLen = (algorithm.equals("AES") ? 16 : algorithm.equals("DES") ? 8 : 0);
   103         IvParameterSpec initVector = new IvParameterSpec(new byte[ivLen]);
   104         cipher.init(Cipher.ENCRYPT_MODE, key, initVector);
   105       } else {
   106         algParams = cipher.getParameters();
   107         cipher.init(Cipher.ENCRYPT_MODE, key, algParams);
   108       }
   109       algParams = cipher.getParameters();
   110       dCipher.init(Cipher.DECRYPT_MODE, key, algParams);
   111       if (threadId == 0) {
   112         childShowCipher();
   113       }
   115       inputLength = msgSize + encInputOffset;
   116       if (testingMisalignment) {
   117         encodeLength = cipher.getOutputSize(msgSize - lastChunkSize) + encOutputOffset;
   118         encodeLength += cipher.getOutputSize(lastChunkSize);
   119         decodeLength = dCipher.getOutputSize(encodeLength - lastChunkSize) + decOutputOffset;
   120         decodeLength += dCipher.getOutputSize(lastChunkSize);
   121       } else {
   122         encodeLength = cipher.getOutputSize(msgSize) + encOutputOffset;
   123         decodeLength = dCipher.getOutputSize(encodeLength) + decOutputOffset;
   124       }
   126       input = new byte[inputLength];
   127       for (int i=encInputOffset, j=0; i<inputLength; i++, j++) {
   128         input[i] = (byte) (j & 0xff);
   129       }
   131       // do one encode and decode in preparation
   132       encode = new byte[encodeLength];
   133       decode = new byte[decodeLength];
   134       if (testingMisalignment) {
   135         decodeMsgSize = cipher.update(input, encInputOffset, (msgSize - lastChunkSize), encode, encOutputOffset);
   136         decodeMsgSize += cipher.doFinal(input, (encInputOffset + msgSize - lastChunkSize), lastChunkSize, encode, (encOutputOffset + decodeMsgSize));
   138         int tempSize = dCipher.update(encode, encOutputOffset, (decodeMsgSize - lastChunkSize), decode, decOutputOffset);
   139         dCipher.doFinal(encode, (encOutputOffset + decodeMsgSize - lastChunkSize), lastChunkSize, decode, (decOutputOffset + tempSize));
   140       } else {
   141         decodeMsgSize = cipher.doFinal(input, encInputOffset, msgSize, encode, encOutputOffset);
   142         dCipher.doFinal(encode, encOutputOffset, decodeMsgSize, decode, decOutputOffset);
   143       }
   144       if (checkOutput) {
   145         expectedEncode = (byte[]) encode.clone();
   146         expectedDecode = (byte[]) decode.clone();
   147         showArray(key.getEncoded()  ,  "key:    ");
   148         showArray(input,  "input:  ");
   149         showArray(encode, "encode: ");
   150         showArray(decode, "decode: ");
   151       }
   152     }
   153     catch (Exception e) {
   154       e.printStackTrace();
   155       System.exit(1);
   156     }
   157   }
   159   void showArray(byte b[], String name) {
   160     System.out.format("%s [%d]: ", name, b.length);
   161     for (int i=0; i<Math.min(b.length, 32); i++) {
   162       System.out.format("%02x ", b[i] & 0xff);
   163     }
   164     System.out.println();
   165   }
   167   void compareArrays(byte b[], byte exp[]) {
   168     if (b.length != exp.length) {
   169       System.out.format("different lengths for actual and expected output arrays\n");
   170       showArray(b, "test: ");
   171       showArray(exp, "exp : ");
   172       System.exit(1);
   173     }
   174     for (int i=0; i< exp.length; i++) {
   175       if (b[i] != exp[i]) {
   176         System.out.format("output error at index %d: got %02x, expected %02x\n", i, b[i] & 0xff, exp[i] & 0xff);
   177         showArray(b, "test: ");
   178         showArray(exp, "exp : ");
   179         System.exit(1);
   180       }
   181     }
   182   }
   185   void showCipher(Cipher c, String kind) {
   186     System.out.println(kind + " cipher provider: " + cipher.getProvider());
   187     System.out.println(kind + " cipher algorithm: " + cipher.getAlgorithm());
   188   }
   190   abstract void childShowCipher();
   191 }

mercurial