@@ -1138,6 +1138,16 @@ private static long encodeCStringArray(byte[] data, long startOffset, long[] off
1138
1138
return offset ;
1139
1139
}
1140
1140
1141
+ private static final class MMapHandle {
1142
+ private final Object pointer ;
1143
+ private final long length ;
1144
+
1145
+ public MMapHandle (Object pointer , long length ) {
1146
+ this .pointer = pointer ;
1147
+ this .length = length ;
1148
+ }
1149
+ }
1150
+
1141
1151
@ ExportMessage
1142
1152
public Object mmap (long length , int prot , int flags , int fd , long offset ,
1143
1153
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) throws PosixException {
@@ -1151,43 +1161,70 @@ public Object mmap(long length, int prot, int flags, int fd, long offset,
1151
1161
} catch (UnsupportedMessageException e ) {
1152
1162
throw CompilerDirectives .shouldNotReachHere (e );
1153
1163
}
1154
- return address ;
1164
+ return new MMapHandle ( address , length ) ;
1155
1165
}
1156
1166
1157
1167
@ ExportMessage
1158
1168
public byte mmapReadByte (Object mmap , long index ,
1159
1169
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) {
1160
- return invokeNode .callByte (this , PosixNativeFunction .read_byte , mmap , index );
1170
+ MMapHandle handle = (MMapHandle ) mmap ;
1171
+ if (index < 0 || index >= handle .length ) {
1172
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1173
+ throw new IndexOutOfBoundsException ();
1174
+ }
1175
+ return invokeNode .callByte (this , PosixNativeFunction .read_byte , handle .pointer , index );
1161
1176
}
1162
1177
1163
1178
@ ExportMessage
1164
1179
public int mmapReadBytes (Object mmap , long index , byte [] bytes , int length ,
1165
1180
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) {
1166
- invokeNode .call (this , PosixNativeFunction .read_bytes , mmap , wrap (bytes ), index , length );
1181
+ MMapHandle handle = (MMapHandle ) mmap ;
1182
+ checkIndexAndLen (handle , index , length );
1183
+ invokeNode .call (this , PosixNativeFunction .read_bytes , handle .pointer , wrap (bytes ), index , length );
1167
1184
return length ;
1168
1185
}
1169
1186
1170
1187
@ ExportMessage
1171
1188
public void mmapWriteBytes (Object mmap , long index , byte [] bytes , int length ,
1172
1189
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) {
1173
- invokeNode .call (this , PosixNativeFunction .write_bytes , mmap , wrap (bytes ), index , length );
1190
+ MMapHandle handle = (MMapHandle ) mmap ;
1191
+ checkIndexAndLen (handle , index , length );
1192
+ invokeNode .call (this , PosixNativeFunction .write_bytes , handle .pointer , wrap (bytes ), index , length );
1174
1193
}
1175
1194
1176
1195
@ ExportMessage
1177
1196
public void mmapFlush (Object mmap , long offset , long length ,
1178
1197
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) {
1179
- invokeNode .call (this , PosixNativeFunction .call_msync , mmap , offset , length );
1198
+ MMapHandle handle = (MMapHandle ) mmap ;
1199
+ checkIndexAndLen (handle , offset , length );
1200
+ invokeNode .call (this , PosixNativeFunction .call_msync , handle .pointer , offset , length );
1180
1201
}
1181
1202
1182
1203
@ ExportMessage
1183
1204
public void mmapUnmap (Object mmap , long length ,
1184
1205
@ Shared ("invoke" ) @ Cached InvokeNativeFunction invokeNode ) throws PosixException {
1185
- int result = invokeNode .callInt (this , PosixNativeFunction .call_munmap , mmap , length );
1206
+ MMapHandle handle = (MMapHandle ) mmap ;
1207
+ if (length != handle .length ) {
1208
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1209
+ throw new IllegalArgumentException ();
1210
+ }
1211
+ int result = invokeNode .callInt (this , PosixNativeFunction .call_munmap , handle .pointer , length );
1186
1212
if (result != 0 ) {
1187
1213
throw newPosixException (invokeNode , getErrno (invokeNode ));
1188
1214
}
1189
1215
}
1190
1216
1217
+ private static void checkIndexAndLen (MMapHandle handle , long index , long length ) {
1218
+ if (length < 0 ) {
1219
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1220
+ throw new IllegalArgumentException ();
1221
+ }
1222
+ if (index < 0 || index + length > handle .length ) {
1223
+ CompilerDirectives .transferToInterpreterAndInvalidate ();
1224
+ throw new IndexOutOfBoundsException ();
1225
+ }
1226
+ }
1227
+
1191
1228
// ------------------
1192
1229
// Path conversions
1193
1230
0 commit comments