1 /*
   2  * Copyright (c) 2017, 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.test;
  24 
  25 import org.graalvm.compiler.replacements.test.MethodSubstitutionTest;
  26 import org.junit.Test;
  27 
  28 import jdk.vm.ci.code.InstalledCode;
  29 import jdk.vm.ci.meta.ResolvedJavaMethod;
  30 import sun.misc.Unsafe;
  31 
  32 /**
  33  * Tests the VM independent intrinsification of {@link Unsafe} methods.
  34  */
  35 public class HotSpotUnsafeSubstitutionTest extends MethodSubstitutionTest {
  36 
  37     public void testSubstitution(String testMethodName, Class<?> holder, String methodName, Class<?>[] parameterTypes, Object receiver, Object[] args1, Object[] args2) {
  38         ResolvedJavaMethod testMethod = getResolvedJavaMethod(testMethodName);
  39         ResolvedJavaMethod originalMethod = getResolvedJavaMethod(holder, methodName, parameterTypes);
  40 
  41         // Force compilation
  42         InstalledCode code = getCode(testMethod);
  43         assert code != null;
  44 
  45         // Verify that the original method and the substitution produce the same value
  46         Object expected = invokeSafe(originalMethod, receiver, args1);
  47         Object actual = invokeSafe(testMethod, null, args2);
  48         assertDeepEquals(expected, actual);
  49 
  50         // Verify that the generated code and the original produce the same value
  51         expected = invokeSafe(originalMethod, receiver, args1);
  52         actual = executeVarargsSafe(code, args2);
  53         assertDeepEquals(expected, actual);
  54 
  55     }
  56 
  57     @Test
  58     public void testUnsafeSubstitutions() throws Exception {
  59         testGraph("unsafeCopyMemory");
  60     }
  61 
  62     public void unsafeCopyMemory(Object srcBase, long srcOffset, Object dstBase, long dstOffset, long bytes) {
  63         UNSAFE.copyMemory(srcBase, srcOffset, dstBase, dstOffset, bytes);
  64     }
  65 
  66     public byte[] testCopyMemorySnippet(long src, int bytes) {
  67         byte[] result = new byte[bytes];
  68         UNSAFE.copyMemory(null, src, result, Unsafe.ARRAY_BYTE_BASE_OFFSET, bytes);
  69         return result;
  70     }
  71 
  72     @Test
  73     public void testCopyMemory() {
  74         int size = 128;
  75         long src = UNSAFE.allocateMemory(size);
  76         for (int i = 0; i < size; i++) {
  77             UNSAFE.putByte(null, src + i, (byte) i);
  78         }
  79         test("testCopyMemorySnippet", src, size);
  80     }
  81 }