1 /*
   2  * Copyright (c) 2016, 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 package org.graalvm.compiler.hotspot.replacements;
  24 
  25 import static org.graalvm.compiler.core.common.util.Util.Java8OrEarlier;
  26 import static jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider.getArrayBaseOffset;
  27 
  28 import org.graalvm.compiler.api.replacements.ClassSubstitution;
  29 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  30 import org.graalvm.compiler.core.common.LocationIdentity;
  31 import org.graalvm.compiler.debug.GraalError;
  32 import org.graalvm.compiler.hotspot.HotSpotBackend;
  33 import org.graalvm.compiler.hotspot.nodes.ComputeObjectAddressNode;
  34 import org.graalvm.compiler.nodes.PiNode;
  35 import org.graalvm.compiler.nodes.extended.RawLoadNode;
  36 import org.graalvm.compiler.word.Word;
  37 
  38 import jdk.vm.ci.meta.JavaKind;
  39 
  40 @ClassSubstitution(className = "sun.security.provider.SHA5", optional = true)
  41 public class SHA5Substitutions {
  42 
  43     static final long stateOffset;
  44 
  45     static final Class<?> shaClass;
  46 
  47     public static final String implCompressName = Java8OrEarlier ? "implCompress" : "implCompress0";
  48 
  49     static {
  50         try {
  51             // Need to use the system class loader as com.sun.crypto.provider.AESCrypt
  52             // is normally loaded by the extension class loader which is not delegated
  53             // to by the JVMCI class loader.
  54             ClassLoader cl = ClassLoader.getSystemClassLoader();
  55             shaClass = Class.forName("sun.security.provider.SHA5", true, cl);
  56             stateOffset = UnsafeAccess.UNSAFE.objectFieldOffset(shaClass.getDeclaredField("state"));
  57         } catch (Exception ex) {
  58             throw new GraalError(ex);
  59         }
  60     }
  61 
  62     @MethodSubstitution(isStatic = false)
  63     static void implCompress0(Object receiver, byte[] buf, int ofs) {
  64         Object realReceiver = PiNode.piCastNonNull(receiver, shaClass);
  65         Object state = RawLoadNode.load(realReceiver, stateOffset, JavaKind.Object, LocationIdentity.any());
  66         Word bufAddr = Word.unsigned(ComputeObjectAddressNode.get(buf, getArrayBaseOffset(JavaKind.Byte) + ofs));
  67         Word stateAddr = Word.unsigned(ComputeObjectAddressNode.get(state, getArrayBaseOffset(JavaKind.Int)));
  68         HotSpotBackend.sha5ImplCompressStub(bufAddr, stateAddr);
  69     }
  70 }