4141import java .io .File ;
4242import java .io .IOException ;
4343import java .io .RandomAccessFile ;
44+ import java .lang .foreign .MemorySegment ;
4445import java .lang .reflect .Field ;
4546import java .lang .reflect .Method ;
4647import java .nio .MappedByteBuffer ;
@@ -60,6 +61,8 @@ public class InternalErrorTest {
6061 private static final String failureMsg1 = "InternalError not thrown" ;
6162 private static final String failureMsg2 = "Wrong InternalError: " ;
6263
64+ private static final int NUM_TESTS = 4 ;
65+
6366 public static void main (String [] args ) throws Throwable {
6467 Unsafe unsafe = Unsafe .getUnsafe ();
6568
@@ -71,9 +74,9 @@ public static void main(String[] args) throws Throwable {
7174 s .append ("1" );
7275 }
7376 Files .write (file .toPath (), s .toString ().getBytes ());
74- FileChannel fileChannel = new RandomAccessFile (file , "r " ).getChannel ();
77+ FileChannel fileChannel = new RandomAccessFile (file , "rw " ).getChannel ();
7578 MappedByteBuffer buffer =
76- fileChannel .map (FileChannel .MapMode .READ_ONLY , 0 , fileChannel .size ());
79+ fileChannel .map (FileChannel .MapMode .READ_WRITE , 0 , fileChannel .size ());
7780
7881 // Get address of mapped memory.
7982 long mapAddr = 0 ;
@@ -86,13 +89,13 @@ public static void main(String[] args) throws Throwable {
8689 }
8790 long allocMem = unsafe .allocateMemory (4000 );
8891
89- for (int i = 0 ; i < 3 ; i ++) {
92+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
9093 test (buffer , unsafe , mapAddr , allocMem , i );
9194 }
9295
9396 Files .write (file .toPath (), "2" .getBytes ());
9497 buffer .position (buffer .position () + pageSize );
95- for (int i = 0 ; i < 3 ; i ++) {
98+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
9699 try {
97100 test (buffer , unsafe , mapAddr , allocMem , i );
98101 WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -107,7 +110,7 @@ public static void main(String[] args) throws Throwable {
107110 Method m = InternalErrorTest .class .getMethod ("test" , MappedByteBuffer .class , Unsafe .class , long .class , long .class , int .class );
108111 WhiteBox .getWhiteBox ().enqueueMethodForCompilation (m , 3 );
109112
110- for (int i = 0 ; i < 3 ; i ++) {
113+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
111114 try {
112115 test (buffer , unsafe , mapAddr , allocMem , i );
113116 WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -121,7 +124,7 @@ public static void main(String[] args) throws Throwable {
121124
122125 WhiteBox .getWhiteBox ().enqueueMethodForCompilation (m , 4 );
123126
124- for (int i = 0 ; i < 3 ; i ++) {
127+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
125128 try {
126129 test (buffer , unsafe , mapAddr , allocMem , i );
127130 WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -143,13 +146,18 @@ public static void test(MappedByteBuffer buffer, Unsafe unsafe, long mapAddr, lo
143146 buffer .get (new byte [8 ]);
144147 break ;
145148 case 1 :
146- // testing Unsafe.copySwapMemory, trying to access next page after truncation.
149+ // testing Unsafe.copySwapMemory, trying to access next page after truncation.
147150 unsafe .copySwapMemory (null , mapAddr + pageSize , new byte [4000 ], 16 , 2000 , 2 );
148151 break ;
149152 case 2 :
150- // testing Unsafe.copySwapMemory, trying to access next page after truncation.
153+ // testing Unsafe.copySwapMemory, trying to access next page after truncation.
151154 unsafe .copySwapMemory (null , mapAddr + pageSize , null , allocMem , 2000 , 2 );
152155 break ;
156+ case 3 :
157+ MemorySegment segment = MemorySegment .ofBuffer (buffer );
158+ // testing Unsafe.setMemory, trying to access next page after truncation.
159+ segment .fill ((byte ) 0xF0 );
160+ break ;
153161 }
154162 }
155163
0 commit comments