41
41
import java .io .File ;
42
42
import java .io .IOException ;
43
43
import java .io .RandomAccessFile ;
44
+ import java .lang .foreign .MemorySegment ;
44
45
import java .lang .reflect .Field ;
45
46
import java .lang .reflect .Method ;
46
47
import java .nio .MappedByteBuffer ;
@@ -60,6 +61,8 @@ public class InternalErrorTest {
60
61
private static final String failureMsg1 = "InternalError not thrown" ;
61
62
private static final String failureMsg2 = "Wrong InternalError: " ;
62
63
64
+ private static final int NUM_TESTS = 4 ;
65
+
63
66
public static void main (String [] args ) throws Throwable {
64
67
Unsafe unsafe = Unsafe .getUnsafe ();
65
68
@@ -71,9 +74,9 @@ public static void main(String[] args) throws Throwable {
71
74
s .append ("1" );
72
75
}
73
76
Files .write (file .toPath (), s .toString ().getBytes ());
74
- FileChannel fileChannel = new RandomAccessFile (file , "r " ).getChannel ();
77
+ FileChannel fileChannel = new RandomAccessFile (file , "rw " ).getChannel ();
75
78
MappedByteBuffer buffer =
76
- fileChannel .map (FileChannel .MapMode .READ_ONLY , 0 , fileChannel .size ());
79
+ fileChannel .map (FileChannel .MapMode .READ_WRITE , 0 , fileChannel .size ());
77
80
78
81
// Get address of mapped memory.
79
82
long mapAddr = 0 ;
@@ -86,13 +89,13 @@ public static void main(String[] args) throws Throwable {
86
89
}
87
90
long allocMem = unsafe .allocateMemory (4000 );
88
91
89
- for (int i = 0 ; i < 3 ; i ++) {
92
+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
90
93
test (buffer , unsafe , mapAddr , allocMem , i );
91
94
}
92
95
93
96
Files .write (file .toPath (), "2" .getBytes ());
94
97
buffer .position (buffer .position () + pageSize );
95
- for (int i = 0 ; i < 3 ; i ++) {
98
+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
96
99
try {
97
100
test (buffer , unsafe , mapAddr , allocMem , i );
98
101
WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -107,7 +110,7 @@ public static void main(String[] args) throws Throwable {
107
110
Method m = InternalErrorTest .class .getMethod ("test" , MappedByteBuffer .class , Unsafe .class , long .class , long .class , int .class );
108
111
WhiteBox .getWhiteBox ().enqueueMethodForCompilation (m , 3 );
109
112
110
- for (int i = 0 ; i < 3 ; i ++) {
113
+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
111
114
try {
112
115
test (buffer , unsafe , mapAddr , allocMem , i );
113
116
WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -121,7 +124,7 @@ public static void main(String[] args) throws Throwable {
121
124
122
125
WhiteBox .getWhiteBox ().enqueueMethodForCompilation (m , 4 );
123
126
124
- for (int i = 0 ; i < 3 ; i ++) {
127
+ for (int i = 0 ; i < NUM_TESTS ; i ++) {
125
128
try {
126
129
test (buffer , unsafe , mapAddr , allocMem , i );
127
130
WhiteBox .getWhiteBox ().forceSafepoint ();
@@ -143,13 +146,18 @@ public static void test(MappedByteBuffer buffer, Unsafe unsafe, long mapAddr, lo
143
146
buffer .get (new byte [8 ]);
144
147
break ;
145
148
case 1 :
146
- // testing Unsafe.copySwapMemory, trying to access next page after truncation.
149
+ // testing Unsafe.copySwapMemory, trying to access next page after truncation.
147
150
unsafe .copySwapMemory (null , mapAddr + pageSize , new byte [4000 ], 16 , 2000 , 2 );
148
151
break ;
149
152
case 2 :
150
- // testing Unsafe.copySwapMemory, trying to access next page after truncation.
153
+ // testing Unsafe.copySwapMemory, trying to access next page after truncation.
151
154
unsafe .copySwapMemory (null , mapAddr + pageSize , null , allocMem , 2000 , 2 );
152
155
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 ;
153
161
}
154
162
}
155
163
0 commit comments