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