1 /* 2 * Copyright (c) 2017, 2018, 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.meta; 26 27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfig.INJECTED_VMCONFIG; 28 import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.doingUnsafeAccessOffset; 29 30 import org.graalvm.compiler.api.replacements.ClassSubstitution; 31 import org.graalvm.compiler.api.replacements.MethodSubstitution; 32 import org.graalvm.compiler.hotspot.HotSpotBackend; 33 import org.graalvm.compiler.hotspot.nodes.CurrentJavaThreadNode; 34 import org.graalvm.compiler.nodes.ComputeObjectAddressNode; 35 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 36 import org.graalvm.compiler.word.Word; 37 import jdk.internal.vm.compiler.word.LocationIdentity; 38 import jdk.internal.vm.compiler.word.WordFactory; 39 40 @ClassSubstitution(className = {"jdk.internal.misc.Unsafe", "sun.misc.Unsafe"}) 41 public class HotSpotUnsafeSubstitutions { 42 43 public static final String copyMemoryName = JavaVersionUtil.JAVA_SPEC <= 8 ? "copyMemory" : "copyMemory0"; 44 45 @SuppressWarnings("unused") 46 @MethodSubstitution(isStatic = false) 47 static void copyMemory(Object receiver, Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) { 48 Word srcAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(srcBase, srcOffset)); 49 Word dstAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(destBase, destOffset)); 50 Word size = WordFactory.signed(bytes); 51 52 HotSpotBackend.unsafeArraycopy(srcAddr, dstAddr, size); 53 } 54 55 @SuppressWarnings("unused") 56 @MethodSubstitution(value = "copyMemory", isStatic = false) 57 static void copyMemoryGuarded(Object receiver, Object srcBase, long srcOffset, Object destBase, long destOffset, long bytes) { 58 Word srcAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(srcBase, srcOffset)); 59 Word dstAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(destBase, destOffset)); 60 Word size = WordFactory.signed(bytes); 61 Word javaThread = CurrentJavaThreadNode.get(); 62 int offset = doingUnsafeAccessOffset(INJECTED_VMCONFIG); 63 LocationIdentity any = LocationIdentity.any(); 64 65 /* Set doingUnsafeAccess to guard and handle unsafe memory access failures */ 66 javaThread.writeByte(offset, (byte) 1, any); 67 HotSpotBackend.unsafeArraycopy(srcAddr, dstAddr, size); 68 /* Reset doingUnsafeAccess */ 69 javaThread.writeByte(offset, (byte) 0, any); 70 } 71 }