1 /*
   2  * Copyright (c) 2015, 2015, 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.core.test;
  24 
  25 import static java.nio.file.StandardOpenOption.READ;
  26 import static java.nio.file.StandardOpenOption.WRITE;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.nio.ByteBuffer;
  31 import java.nio.MappedByteBuffer;
  32 import java.nio.channels.FileChannel;
  33 import java.nio.channels.FileChannel.MapMode;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 
  37 import org.graalvm.compiler.nodes.StructuredGraph;
  38 import org.graalvm.compiler.phases.common.CanonicalizerPhase;
  39 import org.graalvm.compiler.phases.common.inlining.InliningPhase;
  40 import org.graalvm.compiler.phases.common.inlining.policy.InlineEverythingPolicy;
  41 import org.graalvm.compiler.phases.tiers.HighTierContext;
  42 import org.junit.Assert;
  43 import org.junit.Assume;
  44 import org.junit.Test;
  45 
  46 import jdk.vm.ci.code.InstalledCode;
  47 import jdk.vm.ci.code.InvalidInstalledCodeException;
  48 import jdk.vm.ci.meta.ResolvedJavaMethod;
  49 import jdk.vm.ci.meta.ResolvedJavaType;
  50 import sun.misc.Unsafe;
  51 
  52 public class MarkUnsafeAccessTest extends GraalCompilerTest {
  53 
  54     public static Unsafe unsafe;
  55 
  56     public void getRaw() {
  57         unsafe.getInt(0L);
  58     }
  59 
  60     public void get() {
  61         unsafe.getInt(null, 0L);
  62     }
  63 
  64     public void putRaw() {
  65         unsafe.putInt(0L, 0);
  66     }
  67 
  68     public void put() {
  69         unsafe.putInt(null, 0L, 0);
  70     }
  71 
  72     public void cas() {
  73         unsafe.compareAndSwapInt(null, 0, 0, 0);
  74     }
  75 
  76     public void noAccess() {
  77         unsafe.addressSize();
  78         unsafe.pageSize();
  79     }
  80 
  81     private void assertHasUnsafe(String name, boolean hasUnsafe) {
  82         Assert.assertEquals(hasUnsafe, compile(getResolvedJavaMethod(name), null).hasUnsafeAccess());
  83     }
  84 
  85     @Test
  86     public void testGet() {
  87         assertHasUnsafe("get", true);
  88         assertHasUnsafe("getRaw", true);
  89     }
  90 
  91     @Test
  92     public void testPut() {
  93         assertHasUnsafe("put", true);
  94         assertHasUnsafe("putRaw", true);
  95     }
  96 
  97     @Test
  98     public void testCas() {
  99         assertHasUnsafe("cas", true);
 100     }
 101 
 102     @Test
 103     public void testNoAcces() {
 104         assertHasUnsafe("noAccess", false);
 105     }
 106 
 107     @FunctionalInterface
 108     private interface MappedByteBufferGetter {
 109         byte get(MappedByteBuffer mbb);
 110     }
 111 
 112     @Test
 113     public void testStandard() throws IOException {
 114         testMappedByteBuffer(MappedByteBuffer::get);
 115     }
 116 
 117     @Test
 118     public void testCompiled() throws IOException {
 119         ResolvedJavaMethod getMethod = asResolvedJavaMethod(getMethod(ByteBuffer.class, "get", new Class<?>[]{}));
 120         ResolvedJavaType mbbClass = getMetaAccess().lookupJavaType(MappedByteBuffer.class);
 121         ResolvedJavaMethod getMethodImpl = mbbClass.findUniqueConcreteMethod(getMethod).getResult();
 122         Assert.assertNotNull(getMethodImpl);
 123         StructuredGraph graph = parseForCompile(getMethodImpl);
 124         HighTierContext highContext = getDefaultHighTierContext();
 125         new CanonicalizerPhase().apply(graph, highContext);
 126         new InliningPhase(new InlineEverythingPolicy(), new CanonicalizerPhase()).apply(graph, highContext);
 127         InstalledCode compiledCode = getCode(getMethodImpl, graph);
 128         testMappedByteBuffer(mbb -> {
 129             try {
 130                 return (byte) compiledCode.executeVarargs(mbb);
 131             } catch (InvalidInstalledCodeException e) {
 132                 Assert.fail();
 133                 return 0;
 134             }
 135         });
 136     }
 137 
 138     private static final int BLOCK_SIZE = 512;
 139     private static final int BLOCK_COUNT = 16;
 140 
 141     public void testMappedByteBuffer(MappedByteBufferGetter getter) throws IOException {
 142         Path tmp = Files.createTempFile(null, null);
 143         tmp.toFile().deleteOnExit();
 144         FileChannel tmpFileChannel = FileChannel.open(tmp, READ, WRITE);
 145         ByteBuffer bb = ByteBuffer.allocate(BLOCK_SIZE);
 146         while (bb.remaining() >= 4) {
 147             bb.putInt(0xA8A8A8A8);
 148         }
 149         for (int i = 0; i < BLOCK_COUNT; ++i) {
 150             bb.flip();
 151             while (bb.hasRemaining()) {
 152                 tmpFileChannel.write(bb);
 153             }
 154         }
 155         tmpFileChannel.force(true);
 156         MappedByteBuffer mbb = tmpFileChannel.map(MapMode.READ_WRITE, 0, BLOCK_SIZE * BLOCK_COUNT);
 157         Assert.assertEquals((byte) 0xA8, mbb.get());
 158         mbb.position(mbb.position() + BLOCK_SIZE);
 159         Assert.assertEquals((byte) 0xA8, mbb.get());
 160         boolean truncated = false;
 161         try {
 162             tmpFileChannel.truncate(0);
 163             tmpFileChannel.force(true);
 164             truncated = true;
 165         } catch (IOException e) {
 166             // not all platforms support truncating memory-mapped files
 167         }
 168         Assume.assumeTrue(truncated);
 169         try {
 170             mbb.position(BLOCK_SIZE);
 171             getter.get(mbb);
 172 
 173             // Make a call that goes into native code to materialize async exception
 174             new File("").exists();
 175         } catch (InternalError e) {
 176             return;
 177         }
 178         Assert.fail("Expected exception");
 179     }
 180 }