Skip to content

Commit 955e19c

Browse files
committed
[GR-28432] Add offset parameter to send/recv PosixSupportLibrary messages
PullRequest: graalpython/1836
2 parents 449fe0f + 4a9bc92 commit 955e19c

File tree

7 files changed

+69
-54
lines changed

7 files changed

+69
-54
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -619,24 +619,24 @@ int32_t call_getsockname(int32_t sockfd, int8_t *addr, int32_t *len_and_family)
619619
}
620620

621621
//TODO len should be size_t, retval should be ssize_t
622-
int32_t call_send(int32_t sockfd, void *buf, int32_t len, int32_t flags) {
623-
return send(sockfd, buf, len, flags);
622+
int32_t call_send(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags) {
623+
return send(sockfd, buf + offset, len, flags);
624624
}
625625

626-
int32_t call_sendto(int32_t sockfd, void *buf, int32_t len, int32_t flags, int8_t *addr, int32_t addr_len) {
626+
int32_t call_sendto(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags, int8_t *addr, int32_t addr_len) {
627627
struct sockaddr_storage sa;
628628
memcpy(&sa, addr, addr_len);
629-
return sendto(sockfd, buf, len, flags, (struct sockaddr *) &sa, addr_len);
629+
return sendto(sockfd, buf + offset, len, flags, (struct sockaddr *) &sa, addr_len);
630630
}
631631

632-
int32_t call_recv(int32_t sockfd, void *buf, int32_t len, int32_t flags) {
633-
return recv(sockfd, buf, len, flags);
632+
int32_t call_recv(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags) {
633+
return recv(sockfd, buf + offset, len, flags);
634634
}
635635

636-
int32_t call_recvfrom(int32_t sockfd, void *buf, int32_t len, int32_t flags, int8_t *src_addr, int32_t *len_and_family) {
636+
int32_t call_recvfrom(int32_t sockfd, void *buf, int32_t offset, int32_t len, int32_t flags, int8_t *src_addr, int32_t *len_and_family) {
637637
struct sockaddr_storage sa;
638638
socklen_t l = sizeof(sa);
639-
int res = recvfrom(sockfd, buf, len, flags, (struct sockaddr *) &sa, &l);
639+
int res = recvfrom(sockfd, buf + offset, len, flags, (struct sockaddr *) &sa, &l);
640640
if (res != -1) {
641641
assert(l <= sizeof(sockaddr_storage)); // l is small enough to be representable by int32_t...
642642
len_and_family[0] = l; // ...so this unsigned->signed conversion is well defined

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/SocketTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ public void sendtoRecvfromInet6() throws PosixException {
165165

166166
int cliSocket = createSocket(AF_INET6.value, SOCK_DGRAM.value, 0);
167167
byte[] data = new byte[]{1, 2, 3};
168-
int sentCount = lib.sendto(posixSupport, cliSocket, data, data.length, 0, srvUsa);
168+
int sentCount = lib.sendto(posixSupport, cliSocket, data, 0, data.length, 0, srvUsa);
169169
assertEquals(data.length, sentCount);
170170

171171
byte[] buf = new byte[100];
172-
RecvfromResult recvfromResult = lib.recvfrom(posixSupport, srvSocket, buf, buf.length, 0);
172+
RecvfromResult recvfromResult = lib.recvfrom(posixSupport, srvSocket, buf, 0, buf.length, 0);
173173

174174
assertEquals(data.length, recvfromResult.readBytes);
175175
assertArrayEquals(data, Arrays.copyOf(buf, recvfromResult.readBytes));
@@ -195,11 +195,11 @@ public void sendtoRecvfromInet() throws PosixException {
195195

196196
int cliSocket = createSocket(AF_INET.value, SOCK_DGRAM.value, 0);
197197
byte[] data = new byte[]{1, 2, 3};
198-
int sentCount = lib.sendto(posixSupport, cliSocket, data, data.length, 0, srvUsa);
198+
int sentCount = lib.sendto(posixSupport, cliSocket, data, 0, data.length, 0, srvUsa);
199199
assertEquals(data.length, sentCount);
200200

201201
byte[] buf = new byte[100];
202-
RecvfromResult recvfromResult = lib.recvfrom(posixSupport, srvSocket, buf, buf.length, 0);
202+
RecvfromResult recvfromResult = lib.recvfrom(posixSupport, srvSocket, buf, 0, buf.length, 0);
203203

204204
assertEquals(data.length, recvfromResult.readBytes);
205205
assertArrayEquals(data, Arrays.copyOf(buf, recvfromResult.readBytes));
@@ -242,10 +242,10 @@ public void acceptConnectInet() throws PosixException {
242242
assertEquals(usaLib.asInet4SockAddr(srvAddrOnServerUsa).getPort(), usaLib.asInet4SockAddr(srvAddrOnClientUsa).getPort());
243243

244244
byte[] data = new byte[]{1, 2, 3};
245-
assertEquals(data.length, lib.send(posixSupport, acceptResult.socketFd, data, data.length, 0));
245+
assertEquals(data.length, lib.send(posixSupport, acceptResult.socketFd, data, 0, data.length, 0));
246246

247247
byte[] buf = new byte[100];
248-
int cnt = lib.recv(posixSupport, cliSocket, buf, buf.length, 0);
248+
int cnt = lib.recv(posixSupport, cliSocket, buf, 0, buf.length, 0);
249249
assertEquals(data.length, cnt);
250250

251251
assertArrayEquals(data, Arrays.copyOf(buf, cnt));

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,25 +2288,25 @@ public UniversalSockAddr getsockname(int sockfd) throws PosixException {
22882288

22892289
@ExportMessage
22902290
@SuppressWarnings("static-method")
2291-
public int send(int sockfd, byte[] buf, int len, int flags) throws PosixException {
2291+
public int send(int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException {
22922292
throw shouldNotReachHere("Not implemented");
22932293
}
22942294

22952295
@ExportMessage
22962296
@SuppressWarnings("static-method")
2297-
public int sendto(int sockfd, byte[] buf, int len, int flags, UniversalSockAddr destAddr) throws PosixException {
2297+
public int sendto(int sockfd, byte[] buf, int offset, int len, int flags, UniversalSockAddr destAddr) throws PosixException {
22982298
throw shouldNotReachHere("Not implemented");
22992299
}
23002300

23012301
@ExportMessage
23022302
@SuppressWarnings("static-method")
2303-
public int recv(int sockfd, byte[] buf, int len, int flags) throws PosixException {
2303+
public int recv(int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException {
23042304
throw shouldNotReachHere("Not implemented");
23052305
}
23062306

23072307
@ExportMessage
23082308
@SuppressWarnings("static-method")
2309-
public RecvfromResult recvfrom(int sockfd, byte[] buf, int len, int flags) throws PosixException {
2309+
public RecvfromResult recvfrom(int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException {
23102310
throw shouldNotReachHere("Not implemented");
23112311
}
23122312

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -721,31 +721,31 @@ final UniversalSockAddr getsockname(int sockfd,
721721
}
722722

723723
@ExportMessage
724-
final int send(int sockfd, byte[] buf, int len, int flags,
724+
final int send(int sockfd, byte[] buf, int offset, int len, int flags,
725725
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
726726
checkNotInImageBuildtime();
727-
return nativeLib.send(nativePosixSupport, sockfd, buf, len, flags);
727+
return nativeLib.send(nativePosixSupport, sockfd, buf, offset, len, flags);
728728
}
729729

730730
@ExportMessage
731-
final int sendto(int sockfd, byte[] buf, int len, int flags, UniversalSockAddr destAddr,
731+
final int sendto(int sockfd, byte[] buf, int offset, int len, int flags, UniversalSockAddr destAddr,
732732
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
733733
checkNotInImageBuildtime();
734-
return nativeLib.sendto(nativePosixSupport, sockfd, buf, len, flags, destAddr);
734+
return nativeLib.sendto(nativePosixSupport, sockfd, buf, offset, len, flags, destAddr);
735735
}
736736

737737
@ExportMessage
738-
final int recv(int sockfd, byte[] buf, int len, int flags,
738+
final int recv(int sockfd, byte[] buf, int offset, int len, int flags,
739739
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
740740
checkNotInImageBuildtime();
741-
return nativeLib.recv(nativePosixSupport, sockfd, buf, len, flags);
741+
return nativeLib.recv(nativePosixSupport, sockfd, buf, offset, len, flags);
742742
}
743743

744744
@ExportMessage
745-
final RecvfromResult recvfrom(int sockfd, byte[] buf, int len, int flags,
745+
final RecvfromResult recvfrom(int sockfd, byte[] buf, int offset, int len, int flags,
746746
@CachedLibrary("this.nativePosixSupport") PosixSupportLibrary nativeLib) throws PosixException {
747747
checkNotInImageBuildtime();
748-
return nativeLib.recvfrom(nativePosixSupport, sockfd, buf, len, flags);
748+
return nativeLib.recvfrom(nativePosixSupport, sockfd, buf, offset, len, flags);
749749
}
750750

751751
@ExportMessage

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -925,44 +925,44 @@ final UniversalSockAddr getsockname(int sockfd,
925925
}
926926

927927
@ExportMessage
928-
final int send(int sockfd, byte[] buf, int len, int flags,
928+
final int send(int sockfd, byte[] buf, int offset, int len, int flags,
929929
@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
930-
logEnter("send", "%d, %d, %d", sockfd, len, flags);
930+
logEnter("send", "%d, %d, %d, %d", sockfd, offset, len, flags);
931931
try {
932-
return logExit("send", "%d", lib.send(delegate, sockfd, buf, len, flags));
932+
return logExit("send", "%d", lib.send(delegate, sockfd, buf, offset, len, flags));
933933
} catch (PosixException e) {
934934
throw logException("send", e);
935935
}
936936
}
937937

938938
@ExportMessage
939-
final int sendto(int sockfd, byte[] buf, int len, int flags, UniversalSockAddr destAddr,
939+
final int sendto(int sockfd, byte[] buf, int offset, int len, int flags, UniversalSockAddr destAddr,
940940
@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
941-
logEnter("sendto", "%d, %d, %d, %s", sockfd, len, flags, destAddr);
941+
logEnter("sendto", "%d, %d, %d, %d, %s", sockfd, offset, len, flags, destAddr);
942942
try {
943-
return logExit("sendto", "%d", lib.sendto(delegate, sockfd, buf, len, flags, destAddr));
943+
return logExit("sendto", "%d", lib.sendto(delegate, sockfd, buf, offset, len, flags, destAddr));
944944
} catch (PosixException e) {
945945
throw logException("sendto", e);
946946
}
947947
}
948948

949949
@ExportMessage
950-
final int recv(int sockfd, byte[] buf, int len, int flags,
950+
final int recv(int sockfd, byte[] buf, int offset, int len, int flags,
951951
@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
952-
logEnter("recv", "%d, %d, %d", sockfd, len, flags);
952+
logEnter("recv", "%d, %d, %d, %d", sockfd, offset, len, flags);
953953
try {
954-
return logExit("recv", "%d", lib.recv(delegate, sockfd, buf, len, flags));
954+
return logExit("recv", "%d", lib.recv(delegate, sockfd, buf, offset, len, flags));
955955
} catch (PosixException e) {
956956
throw logException("recv", e);
957957
}
958958
}
959959

960960
@ExportMessage
961-
final RecvfromResult recvfrom(int sockfd, byte[] buf, int len, int flags,
961+
final RecvfromResult recvfrom(int sockfd, byte[] buf, int offset, int len, int flags,
962962
@CachedLibrary("this.delegate") PosixSupportLibrary lib) throws PosixException {
963-
logEnter("recvfrom", "%d, %d, %d", sockfd, len, flags);
963+
logEnter("recvfrom", "%d, %d, %d, %d", sockfd, offset, len, flags);
964964
try {
965-
return logExit("recvfrom", "%s", lib.recvfrom(delegate, sockfd, buf, len, flags));
965+
return logExit("recvfrom", "%s", lib.recvfrom(delegate, sockfd, buf, offset, len, flags));
966966
} catch (PosixException e) {
967967
throw logException("recvfrom", e);
968968
}

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

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ private enum PosixNativeFunction {
217217
call_listen("(sint32, sint32):sint32"),
218218
call_getpeername("(sint32, [sint8], [sint32]):sint32"),
219219
call_getsockname("(sint32, [sint8], [sint32]):sint32"),
220-
call_send("(sint32, [sint8], sint32, sint32):sint32"),
221-
call_sendto("(sint32, [sint8], sint32, sint32, [sint8], sint32):sint32"),
222-
call_recv("(sint32, [sint8], sint32, sint32):sint32"),
223-
call_recvfrom("(sint32, [sint8], sint32, sint32, [sint8], [sint32]):sint32"),
220+
call_send("(sint32, [sint8], sint32, sint32, sint32):sint32"),
221+
call_sendto("(sint32, [sint8], sint32, sint32, sint32, [sint8], sint32):sint32"),
222+
call_recv("(sint32, [sint8], sint32, sint32, sint32):sint32"),
223+
call_recvfrom("(sint32, [sint8], sint32, sint32, sint32, [sint8], [sint32]):sint32"),
224224
call_shutdown("(sint32, sint32): sint32"),
225225
call_getsockopt("(sint32, sint32, sint32, [sint8], [sint32]):sint32"),
226226
call_setsockopt("(sint32, sint32, sint32, [sint8], sint32):sint32"),
@@ -1425,41 +1425,45 @@ public UniversalSockAddr getsockname(int sockfd,
14251425
}
14261426

14271427
@ExportMessage
1428-
public int send(int sockfd, byte[] buf, int len, int flags,
1428+
public int send(int sockfd, byte[] buf, int offset, int len, int flags,
14291429
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
1430-
int result = invokeNode.callInt(this, PosixNativeFunction.call_send, sockfd, wrap(buf), len, flags);
1430+
checkBounds(buf, offset, len);
1431+
int result = invokeNode.callInt(this, PosixNativeFunction.call_send, sockfd, wrap(buf), offset, len, flags);
14311432
if (result == -1) {
14321433
throw getErrnoAndThrowPosixException(invokeNode);
14331434
}
14341435
return result;
14351436
}
14361437

14371438
@ExportMessage
1438-
public int sendto(int sockfd, byte[] buf, int len, int flags, UniversalSockAddr usa,
1439+
public int sendto(int sockfd, byte[] buf, int offset, int len, int flags, UniversalSockAddr usa,
14391440
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
1441+
checkBounds(buf, offset, len);
14401442
UniversalSockAddrImpl destAddr = (UniversalSockAddrImpl) usa;
1441-
int result = invokeNode.callInt(this, PosixNativeFunction.call_sendto, sockfd, wrap(buf), len, flags, wrap(destAddr.data), destAddr.getLen());
1443+
int result = invokeNode.callInt(this, PosixNativeFunction.call_sendto, sockfd, wrap(buf), offset, len, flags, wrap(destAddr.data), destAddr.getLen());
14421444
if (result == -1) {
14431445
throw getErrnoAndThrowPosixException(invokeNode);
14441446
}
14451447
return result;
14461448
}
14471449

14481450
@ExportMessage
1449-
public int recv(int sockfd, byte[] buf, int len, int flags,
1451+
public int recv(int sockfd, byte[] buf, int offset, int len, int flags,
14501452
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
1451-
int result = invokeNode.callInt(this, PosixNativeFunction.call_recv, sockfd, wrap(buf), len, flags);
1453+
checkBounds(buf, offset, len);
1454+
int result = invokeNode.callInt(this, PosixNativeFunction.call_recv, sockfd, wrap(buf), offset, len, flags);
14521455
if (result == -1) {
14531456
throw getErrnoAndThrowPosixException(invokeNode);
14541457
}
14551458
return result;
14561459
}
14571460

14581461
@ExportMessage
1459-
public RecvfromResult recvfrom(int sockfd, byte[] buf, int len, int flags,
1462+
public RecvfromResult recvfrom(int sockfd, byte[] buf, int offset, int len, int flags,
14601463
@Shared("invoke") @Cached InvokeNativeFunction invokeNode) throws PosixException {
1464+
checkBounds(buf, offset, len);
14611465
UniversalSockAddrImpl srcAddr = new UniversalSockAddrImpl(this);
1462-
int result = invokeNode.callInt(this, PosixNativeFunction.call_recvfrom, sockfd, wrap(buf), len, flags, wrap(srcAddr.data), wrap(srcAddr.lenAndFamily));
1466+
int result = invokeNode.callInt(this, PosixNativeFunction.call_recvfrom, sockfd, wrap(buf), offset, len, flags, wrap(srcAddr.data), wrap(srcAddr.lenAndFamily));
14631467
if (result == -1) {
14641468
throw getErrnoAndThrowPosixException(invokeNode);
14651469
}
@@ -2042,6 +2046,17 @@ private static byte[] nullTerminate(byte[] str, int length) {
20422046
return terminated;
20432047
}
20442048

2049+
private static void checkBounds(byte[] buf, int offset, int length) {
2050+
if (length < 0) {
2051+
CompilerDirectives.transferToInterpreterAndInvalidate();
2052+
throw new IllegalArgumentException();
2053+
}
2054+
if (offset < 0 || offset + length > buf.length) {
2055+
CompilerDirectives.transferToInterpreterAndInvalidate();
2056+
throw new IndexOutOfBoundsException();
2057+
}
2058+
}
2059+
20452060
@TruffleBoundary
20462061
private static void log(Level level, String fmt, Object... args) {
20472062
if (LOGGER.isLoggable(level)) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,14 +467,14 @@ public int getScopeId() {
467467

468468
public abstract UniversalSockAddr getsockname(Object receiver, int sockfd) throws PosixException;
469469

470-
public abstract int send(Object receiver, int sockfd, byte[] buf, int len, int flags) throws PosixException;
470+
public abstract int send(Object receiver, int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException;
471471

472472
// Unlike POSIX sendto(), we don't support destAddr == null. Use plain send instead.
473-
public abstract int sendto(Object receiver, int sockfd, byte[] buf, int len, int flags, UniversalSockAddr destAddr) throws PosixException;
473+
public abstract int sendto(Object receiver, int sockfd, byte[] buf, int offset, int len, int flags, UniversalSockAddr destAddr) throws PosixException;
474474

475-
public abstract int recv(Object receiver, int sockfd, byte[] buf, int len, int flags) throws PosixException;
475+
public abstract int recv(Object receiver, int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException;
476476

477-
public abstract RecvfromResult recvfrom(Object receiver, int sockfd, byte[] buf, int len, int flags) throws PosixException;
477+
public abstract RecvfromResult recvfrom(Object receiver, int sockfd, byte[] buf, int offset, int len, int flags) throws PosixException;
478478

479479
public static final class AcceptResult {
480480
public final int socketFd;

0 commit comments

Comments
 (0)