annotate graal/com.oracle.graal.hotspot/src/com/oracle/graal/hotspot/replacements/CipherBlockChainingSubstitutions.java @ 9793:b4f12c603be5

added support for the runtime to specify for each foreign call whether it is re-executable and what memory locations it kills
author Doug Simon <doug.simon@oracle.com>
date Wed, 22 May 2013 16:27:25 +0200
parents 9a32908912ae
children cf071af51d94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
1 /*
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
4 *
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
7 * published by the Free Software Foundation.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
8 *
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
13 * accompanied this code).
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
14 *
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
18 *
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
21 * questions.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
22 */
8415
2361bf148c06 rename packages: *snippets* -> *replacements*
Doug Simon <doug.simon@oracle.com>
parents: 8373
diff changeset
23 package com.oracle.graal.hotspot.replacements;
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
24
9793
b4f12c603be5 added support for the runtime to specify for each foreign call whether it is re-executable and what memory locations it kills
Doug Simon <doug.simon@oracle.com>
parents: 9770
diff changeset
25 import static com.oracle.graal.api.meta.LocationIdentity.*;
9513
659bb6cf930c rename: HotSpotSnippetUtils -> HotSpotReplacementsUtil
Doug Simon <doug.simon@oracle.com>
parents: 9181
diff changeset
26 import static com.oracle.graal.hotspot.replacements.HotSpotReplacementsUtil.*;
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
27 import sun.misc.*;
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
28
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
29 import com.oracle.graal.api.meta.*;
8420
5d3fa6f949fb created com.oracle.graal.api.replacements project and moved ClassSubstitution and MethodSubstitution there
Doug Simon <doug.simon@oracle.com>
parents: 8419
diff changeset
30 import com.oracle.graal.api.replacements.*;
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
31 import com.oracle.graal.graph.*;
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
32 import com.oracle.graal.graph.Node.ConstantNodeParameter;
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
33 import com.oracle.graal.graph.Node.NodeIntrinsic;
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
34 import com.oracle.graal.hotspot.nodes.*;
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
35 import com.oracle.graal.nodes.extended.*;
8420
5d3fa6f949fb created com.oracle.graal.api.replacements project and moved ClassSubstitution and MethodSubstitution there
Doug Simon <doug.simon@oracle.com>
parents: 8419
diff changeset
36 import com.oracle.graal.replacements.Snippet.Fold;
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
37 import com.oracle.graal.word.*;
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
38
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
39 /**
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
40 * Substitutions for {@code com.sun.crypto.provider.CipherBlockChaining} methods.
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
41 */
8373
86a9e3ea8fe9 support substitutions for classes that made not be part of the runtime (GRAAL-123)
Doug Simon <doug.simon@oracle.com>
parents: 7868
diff changeset
42 @ClassSubstitution(className = "com.sun.crypto.provider.CipherBlockChaining", optional = true)
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
43 public class CipherBlockChainingSubstitutions {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
44
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
45 private static final long embeddedCipherOffset;
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
46 private static final long rOffset;
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
47 static {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
48 try {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
49 // Need to use launcher class path as com.sun.crypto.provider.AESCrypt
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
50 // is normally not on the boot class path
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
51 ClassLoader cl = Launcher.getLauncher().getClassLoader();
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
52 embeddedCipherOffset = UnsafeAccess.unsafe.objectFieldOffset(Class.forName("com.sun.crypto.provider.FeedbackCipher", true, cl).getDeclaredField("embeddedCipher"));
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
53 rOffset = UnsafeAccess.unsafe.objectFieldOffset(Class.forName("com.sun.crypto.provider.CipherBlockChaining", true, cl).getDeclaredField("r"));
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
54 } catch (Exception ex) {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
55 throw new GraalInternalError(ex);
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
56 }
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
57 }
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
58
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
59 @Fold
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
60 private static Class getAESCryptClass() {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
61 return AESCryptSubstitutions.AESCryptClass;
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
62 }
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
63
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
64 @MethodSubstitution(isStatic = false)
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
65 static void encrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) {
8634
b6491ee579d3 remove UNKNOWN_LOCATION
Lukas Stadler <lukas.stadler@jku.at>
parents: 8420
diff changeset
66 Object embeddedCipher = Word.fromObject(rcvr).readObject(Word.unsigned(embeddedCipherOffset), ANY_LOCATION);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
67 if (getAESCryptClass().isInstance(embeddedCipher)) {
7541
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
68 crypt(rcvr, in, inOffset, inLength, out, outOffset, embeddedCipher, true);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
69 } else {
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
70 encrypt(rcvr, in, inOffset, inLength, out, outOffset);
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
71 }
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
72 }
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
73
9766
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
74 @MethodSubstitution(isStatic = false)
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
75 static void decrypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset) {
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
76 Object embeddedCipher = Word.fromObject(rcvr).readObject(Word.unsigned(embeddedCipherOffset), ANY_LOCATION);
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
77 if (in != out && getAESCryptClass().isInstance(embeddedCipher)) {
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
78 crypt(rcvr, in, inOffset, inLength, out, outOffset, embeddedCipher, false);
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
79 } else {
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
80 decrypt(rcvr, in, inOffset, inLength, out, outOffset);
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
81 }
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
82 }
a2074a73aeef added test for AES intrinsification
Doug Simon <doug.simon@oracle.com>
parents: 9741
diff changeset
83
7541
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
84 private static void crypt(Object rcvr, byte[] in, int inOffset, int inLength, byte[] out, int outOffset, Object embeddedCipher, boolean encrypt) {
8634
b6491ee579d3 remove UNKNOWN_LOCATION
Lukas Stadler <lukas.stadler@jku.at>
parents: 8420
diff changeset
85 Word kAddr = Word.fromObject(embeddedCipher).readWord(Word.unsigned(AESCryptSubstitutions.kOffset), ANY_LOCATION).add(arrayBaseOffset(Kind.Byte));
b6491ee579d3 remove UNKNOWN_LOCATION
Lukas Stadler <lukas.stadler@jku.at>
parents: 8420
diff changeset
86 Word rAddr = Word.unsigned(GetObjectAddressNode.get(rcvr)).readWord(Word.unsigned(rOffset), ANY_LOCATION).add(arrayBaseOffset(Kind.Byte));
7541
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
87 Word inAddr = Word.unsigned(GetObjectAddressNode.get(in) + arrayBaseOffset(Kind.Byte) + inOffset);
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
88 Word outAddr = Word.unsigned(GetObjectAddressNode.get(out) + arrayBaseOffset(Kind.Byte) + outOffset);
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
89 if (encrypt) {
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
90 encryptAESCryptStub(ENCRYPT, inAddr, outAddr, kAddr, rAddr, inLength);
7541
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
91 } else {
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
92 decryptAESCryptStub(DECRYPT, inAddr, outAddr, kAddr, rAddr, inLength);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
93 }
7541
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
94 }
aee6ce29fb17 factored out common code in AES crypto substitutions
Doug Simon <doug.simon@oracle.com>
parents: 7384
diff changeset
95
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
96 public static final ForeignCallDescriptor ENCRYPT = new ForeignCallDescriptor("encrypt", void.class, Word.class, Word.class, Word.class, Word.class, int.class);
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
97 public static final ForeignCallDescriptor DECRYPT = new ForeignCallDescriptor("decrypt", void.class, Word.class, Word.class, Word.class, Word.class, int.class);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
98
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
99 @NodeIntrinsic(ForeignCallNode.class)
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
100 public static native void encryptAESCryptStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Word key, Word r, int inLength);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
101
9770
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
102 @NodeIntrinsic(ForeignCallNode.class)
9a32908912ae replaced AESCryptStubCall (and its subclasses) with usage of ForeignCallNode
Doug Simon <doug.simon@oracle.com>
parents: 9766
diff changeset
103 public static native void decryptAESCryptStub(@ConstantNodeParameter ForeignCallDescriptor descriptor, Word in, Word out, Word key, Word r, int inLength);
7384
d0fbdf2f7a0e added com.sun.crypto.provider.CipherBlockChaining intrinsifications
Doug Simon <doug.simon@oracle.com>
parents:
diff changeset
104 }