Skip to content

Commit 6832275

Browse files
committed
Mmap: check bounds also in POSIX abstraction, use memcpy in C, minor style fixes.
1 parent d63cb85 commit 6832275

File tree

4 files changed

+51
-16
lines changed

4 files changed

+51
-16
lines changed

graalpython/com.oracle.graal.python.cext/posix/posix.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,15 +526,11 @@ int8_t read_byte(int8_t *address, int64_t index) {
526526
}
527527

528528
void write_bytes(int8_t *address, int8_t* buffer, int64_t index, int32_t length) {
529-
for (int64_t i = 0; i < length; ++i) {
530-
address[index + i] = buffer[i];
531-
}
529+
memcpy(address + index, buffer, length);
532530
}
533531

534532
void read_bytes(int8_t *address, int8_t* buffer, int64_t index, int32_t length) {
535-
for (int64_t i = 0; i < length; ++i) {
536-
buffer[i] = address[index + i];
537-
}
533+
memcpy(buffer, address + index, length);
538534
}
539535

540536
int32_t get_errno() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/mmap/MMapBuiltins.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import static com.oracle.graal.python.builtins.objects.mmap.PMMap.ACCESS_READ;
4545
import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_CHANGED_LENGTH;
4646
import static com.oracle.graal.python.nodes.ErrorMessages.MMAP_INDEX_OUT_OF_RANGE;
47+
import static com.oracle.graal.python.nodes.ErrorMessages.READ_BYTE_OUT_OF_RANGE;
4748
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ADD__;
4849
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CONTAINS__;
4950
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ENTER__;
@@ -188,6 +189,7 @@ abstract static class ReprNode extends StrNode {
188189

189190
private static byte[] readBytes(PythonBuiltinBaseNode node, VirtualFrame frame, PMMap self, PosixSupportLibrary posixLib, long pos, int len) {
190191
try {
192+
assert pos + len <= self.getLength();
191193
byte[] buffer = new byte[len];
192194
posixLib.mmapReadBytes(node.getPosixSupport(), self.getPosixSupportHandle(), pos, buffer, buffer.length);
193195
return buffer;
@@ -385,6 +387,9 @@ abstract static class ReadByteNode extends PythonUnaryBuiltinNode {
385387
@Specialization
386388
int readByte(VirtualFrame frame, PMMap self,
387389
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixSupportLib) {
390+
if (self.getPos() >= self.getLength()) {
391+
throw raise(PythonBuiltinClassType.ValueError, READ_BYTE_OUT_OF_RANGE);
392+
}
388393
try {
389394
byte res = posixSupportLib.mmapReadByte(getPosixSupport(), self.getPosixSupportHandle(), self.getPos());
390395
self.setPos(self.getPos() + 1);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/LoggingPosixSupport.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,6 @@ public void mmapWriteBytes(Object mmap, long index, byte[] bytes, int length,
746746
logEnter("mmapWriteBytes", "%s, %d, %d", mmap, index, length);
747747
try {
748748
lib.mmapWriteBytes(delegate, mmap, index, bytes, length);
749-
logExit("mmapWriteBytes", "%s", "void");
750749
} catch (PosixException e) {
751750
throw logException("mmapWriteBytes", e);
752751
}
@@ -769,10 +768,9 @@ final int forkExec(Object[] executables, Object[] args, Object cwd, Object[] env
769768
@ExportMessage
770769
public void mmapFlush(Object mmap, long offset, long length,
771770
@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
772-
logEnter("mmapFlush", "%s %d %d", mmap, offset, length);
771+
logEnter("mmapFlush", "%s, %d, %d", mmap, offset, length);
773772
try {
774773
lib.mmapFlush(delegate, mmap, offset, length);
775-
logExit("mmapFlush", "%s", "void");
776774
} catch (PosixException e) {
777775
throw logException("mmapFlush", e);
778776
}
@@ -795,7 +793,6 @@ public void mmapUnmap(Object mmap, long length,
795793
logEnter("mmapUnmap", "%s %d", mmap, length);
796794
try {
797795
lib.mmapUnmap(delegate, mmap, length);
798-
logExit("mmapUnmap", "%s", "void");
799796
} catch (PosixException e) {
800797
throw logException("mmapUnmap", e);
801798
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/NFIPosixSupport.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,6 +1138,16 @@ private static long encodeCStringArray(byte[] data, long startOffset, long[] off
11381138
return offset;
11391139
}
11401140

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+
11411151
@ExportMessage
11421152
public Object mmap(long length, int prot, int flags, int fd, long offset,
11431153
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
@@ -1151,43 +1161,70 @@ public Object mmap(long length, int prot, int flags, int fd, long offset,
11511161
} catch (UnsupportedMessageException e) {
11521162
throw CompilerDirectives.shouldNotReachHere(e);
11531163
}
1154-
return address;
1164+
return new MMapHandle(address, length);
11551165
}
11561166

11571167
@ExportMessage
11581168
public byte mmapReadByte(Object mmap, long index,
11591169
@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);
11611176
}
11621177

11631178
@ExportMessage
11641179
public int mmapReadBytes(Object mmap, long index, byte[] bytes, int length,
11651180
@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);
11671184
return length;
11681185
}
11691186

11701187
@ExportMessage
11711188
public void mmapWriteBytes(Object mmap, long index, byte[] bytes, int length,
11721189
@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);
11741193
}
11751194

11761195
@ExportMessage
11771196
public void mmapFlush(Object mmap, long offset, long length,
11781197
@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);
11801201
}
11811202

11821203
@ExportMessage
11831204
public void mmapUnmap(Object mmap, long length,
11841205
@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);
11861212
if (result != 0) {
11871213
throw newPosixException(invokeNode, getErrno(invokeNode));
11881214
}
11891215
}
11901216

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+
11911228
// ------------------
11921229
// Path conversions
11931230

0 commit comments

Comments
 (0)