1 /*
   2  * Copyright (c) 2019, 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 
  24 
  25 package org.graalvm.compiler.hotspot.replacements;
  26 
  27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_METAACCESS;
  28 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfigBase.INJECTED_INTRINSIC_CONTEXT;
  29 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfigBase.INJECTED_VMCONFIG;
  30 import static org.graalvm.compiler.nodes.java.InstanceOfNode.doInstanceof;
  31 
  32 import org.graalvm.compiler.api.replacements.ClassSubstitution;
  33 import org.graalvm.compiler.api.replacements.Fold;
  34 import org.graalvm.compiler.api.replacements.MethodSubstitution;
  35 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  36 import org.graalvm.compiler.hotspot.HotSpotBackend;
  37 import org.graalvm.compiler.nodes.ComputeObjectAddressNode;
  38 import org.graalvm.compiler.nodes.PiNode;
  39 import org.graalvm.compiler.nodes.extended.RawLoadNode;
  40 import org.graalvm.compiler.replacements.ReplacementsUtil;
  41 import org.graalvm.compiler.word.Word;
  42 import jdk.internal.vm.compiler.word.LocationIdentity;
  43 import jdk.internal.vm.compiler.word.WordFactory;
  44 
  45 import jdk.vm.ci.meta.JavaKind;
  46 import jdk.vm.ci.meta.ResolvedJavaType;
  47 
  48 @ClassSubstitution(className = "sun.security.provider.DigestBase", optional = true)
  49 public class DigestBaseSubstitutions {
  50 
  51     @MethodSubstitution(isStatic = false)
  52     static int implCompressMultiBlock0(Object receiver, byte[] buf, int ofs, int limit) {
  53         Object realReceiver = PiNode.piCastNonNull(receiver, HotSpotReplacementsUtil.methodHolderClass(INJECTED_INTRINSIC_CONTEXT));
  54         ResolvedJavaType sha1type = HotSpotReplacementsUtil.getType(INJECTED_INTRINSIC_CONTEXT, "Lsun/security/provider/SHA;");
  55         ResolvedJavaType sha256type = HotSpotReplacementsUtil.getType(INJECTED_INTRINSIC_CONTEXT, "Lsun/security/provider/SHA2;");
  56         ResolvedJavaType sha512type = HotSpotReplacementsUtil.getType(INJECTED_INTRINSIC_CONTEXT, "Lsun/security/provider/SHA5;");
  57 
  58         Word bufAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(buf, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Byte) + ofs));
  59         if (useSHA1Intrinsics(INJECTED_VMCONFIG) && doInstanceof(sha1type, realReceiver)) {
  60             Object sha1obj = PiNode.piCastNonNull(realReceiver, sha1type);
  61             Object state = RawLoadNode.load(sha1obj, HotSpotReplacementsUtil.getFieldOffset(sha1type, "state"), JavaKind.Object, LocationIdentity.any());
  62             Word stateAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(state, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Int)));
  63             return HotSpotBackend.shaImplCompressMBStub(bufAddr, stateAddr, ofs, limit);
  64         } else if (useSHA256Intrinsics(INJECTED_VMCONFIG) && doInstanceof(sha256type, realReceiver)) {
  65             Object sha256obj = PiNode.piCastNonNull(realReceiver, sha256type);
  66             Object state = RawLoadNode.load(sha256obj, HotSpotReplacementsUtil.getFieldOffset(sha256type, "state"), JavaKind.Object, LocationIdentity.any());
  67             Word stateAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(state, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Int)));
  68             return HotSpotBackend.sha2ImplCompressMBStub(bufAddr, stateAddr, ofs, limit);
  69         } else if (useSHA512Intrinsics(INJECTED_VMCONFIG) && doInstanceof(sha512type, realReceiver)) {
  70             Object sha512obj = PiNode.piCastNonNull(realReceiver, sha512type);
  71             Object state = RawLoadNode.load(sha512obj, HotSpotReplacementsUtil.getFieldOffset(sha512type, "state"), JavaKind.Object, LocationIdentity.any());
  72             Word stateAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(state, ReplacementsUtil.getArrayBaseOffset(INJECTED_METAACCESS, JavaKind.Int)));
  73             return HotSpotBackend.sha5ImplCompressMBStub(bufAddr, stateAddr, ofs, limit);
  74         } else {
  75             return implCompressMultiBlock0(realReceiver, buf, ofs, limit);
  76         }
  77     }
  78 
  79     @Fold
  80     public static boolean useSHA1Intrinsics(@Fold.InjectedParameter GraalHotSpotVMConfig config) {
  81         return config.useSHA1Intrinsics();
  82     }
  83 
  84     @Fold
  85     public static boolean useSHA256Intrinsics(@Fold.InjectedParameter GraalHotSpotVMConfig config) {
  86         return config.useSHA256Intrinsics();
  87     }
  88 
  89     @Fold
  90     public static boolean useSHA512Intrinsics(@Fold.InjectedParameter GraalHotSpotVMConfig config) {
  91         return config.useSHA512Intrinsics();
  92     }
  93 }