1 /* 2 * Copyright (c) 2022, 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 import jdk.test.lib.RandomFactory; 25 26 import java.lang.foreign.Arena; 27 import java.lang.foreign.MemorySegment; 28 29 import java.io.IOException; 30 import java.nio.ByteBuffer; 31 import java.nio.channels.FileChannel; 32 import java.nio.file.Path; 33 import java.util.Random; 34 import java.util.concurrent.TimeUnit; 35 36 import static java.nio.file.StandardOpenOption.*; 37 38 /* 39 * @test 40 * @bug 8286637 41 * @summary Ensure that memory mapping beyond 32-bit range does not cause an 42 * EXCEPTION_ACCESS_VIOLATION. 43 * @requires vm.bits == 64 44 * @library /test/lib 45 * @build jdk.test.lib.RandomFactory FileChannelUtils 46 * @run main/othervm LargeMapTest 47 * @key randomness 48 */ 49 public class LargeMapTest { 50 private static final long LENGTH = (1L << 32) + 512; 51 private static final int EXTRA = 1024; 52 private static final long BASE = LENGTH - EXTRA; 53 private static final Random GEN = RandomFactory.getRandom(); 54 55 public static void main(String[] args) throws IOException { 56 System.out.println(System.getProperty("sun.arch.data.model")); 57 System.out.println(System.getProperty("os.arch")); 58 System.out.println(System.getProperty("java.version")); 59 60 System.out.println(" Writing large file..."); 61 long t0 = System.nanoTime(); 62 Path p = FileChannelUtils.createSparseTempFile("test", ".dat"); 63 p.toFile().deleteOnExit(); 64 ByteBuffer bb; 65 try (FileChannel fc = FileChannel.open(p, WRITE)) { 66 fc.position(BASE); 67 byte[] b = new byte[EXTRA]; 68 GEN.nextBytes(b); 69 bb = ByteBuffer.wrap(b); 70 fc.write(bb); 71 long t1 = System.nanoTime(); 72 System.out.printf(" Wrote large file in %d ns (%d ms) %n", 73 t1 - t0, TimeUnit.NANOSECONDS.toMillis(t1 - t0)); 74 } 75 bb.rewind(); 76 77 try (FileChannel fc = FileChannel.open(p, READ, WRITE)) { 78 MemorySegment mappedMemorySegment = 79 fc.map(FileChannel.MapMode.READ_WRITE, 0, p.toFile().length(), 80 Arena.ofAuto()); 81 MemorySegment target = mappedMemorySegment.asSlice(BASE, EXTRA); 82 if (!target.asByteBuffer().equals(bb)) { 83 throw new RuntimeException("Expected buffers to be equal"); 84 } 85 } 86 } 87 }