Skip to content

Commit 35dee66

Browse files
committed
[GR-29372] Avoid access to a non-public package in socket builtins
PullRequest: graalpython/1636
2 parents d7da3e1 + 55a3e25 commit 35dee66

File tree

7 files changed

+531
-51
lines changed

7 files changed

+531
-51
lines changed

graalpython/com.oracle.graal.python.test/src/tests/unittest_tags/test_xmlrpc.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
*graalpython.lib-python.3.test.test_xmlrpc.DateTimeTestCase.test_time
1111
*graalpython.lib-python.3.test.test_xmlrpc.DateTimeTestCase.test_time_struct
1212
*graalpython.lib-python.3.test.test_xmlrpc.DateTimeTestCase.test_time_tuple
13-
*graalpython.lib-python.3.test.test_xmlrpc.FailingServerTestCase.test_fail_no_info
14-
*graalpython.lib-python.3.test.test_xmlrpc.FailingServerTestCase.test_fail_with_info
1513
*graalpython.lib-python.3.test.test_xmlrpc.FaultTestCase.test_dotted_attribute
1614
*graalpython.lib-python.3.test.test_xmlrpc.FaultTestCase.test_repr
1715
*graalpython.lib-python.3.test.test_xmlrpc.GzipUtilTestCase.test_gzip_decode_limit

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/SocketModuleBuiltins.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import com.oracle.graal.python.runtime.exception.PException;
8585
import com.oracle.graal.python.runtime.exception.PythonErrorType;
8686
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
87+
import com.oracle.graal.python.util.IPAddressUtil;
8788
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8889
import com.oracle.truffle.api.TruffleFile;
8990
import com.oracle.truffle.api.TruffleLanguage;
@@ -96,8 +97,6 @@
9697
import com.oracle.truffle.api.frame.VirtualFrame;
9798
import com.oracle.truffle.api.profiles.BranchProfile;
9899

99-
import sun.net.util.IPAddressUtil;
100-
101100
@CoreFunctions(defineModule = "_socket")
102101
public class SocketModuleBuiltins extends PythonBuiltins {
103102
// address families

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/ints/PInt.java

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,12 @@
3232
import com.oracle.graal.python.PythonLanguage;
3333
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
3434
import com.oracle.graal.python.builtins.modules.SysModuleBuiltins;
35-
import com.oracle.graal.python.builtins.modules.WarningsModuleBuiltins.WarnNode;
3635
import com.oracle.graal.python.builtins.objects.cext.capi.PythonNativeWrapperLibrary;
37-
import com.oracle.graal.python.builtins.objects.function.PArguments;
3836
import com.oracle.graal.python.builtins.objects.function.PArguments.ThreadState;
3937
import com.oracle.graal.python.builtins.objects.object.PythonBuiltinObject;
4038
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
4139
import com.oracle.graal.python.nodes.ErrorMessages;
4240
import com.oracle.graal.python.nodes.PRaiseNode;
43-
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
4441
import com.oracle.graal.python.nodes.util.CastToJavaDoubleNode;
4542
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
4643
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
@@ -54,7 +51,6 @@
5451
import com.oracle.truffle.api.dsl.Cached.Exclusive;
5552
import com.oracle.truffle.api.dsl.Cached.Shared;
5653
import com.oracle.truffle.api.dsl.CachedContext;
57-
import com.oracle.truffle.api.frame.VirtualFrame;
5854
import com.oracle.truffle.api.interop.InteropLibrary;
5955
import com.oracle.truffle.api.interop.UnsupportedMessageException;
6056
import com.oracle.truffle.api.library.CachedLibrary;
@@ -231,19 +227,7 @@ public boolean canBeIndex() {
231227
}
232228

233229
@ExportMessage
234-
public Object asIndexWithState(@SuppressWarnings("unused") ThreadState threadState,
235-
@Cached ConditionProfile gotState,
236-
@Cached IsBuiltinClassProfile isInt,
237-
@Cached WarnNode warnNode) {
238-
if (!isInt.profileObject(this, PythonBuiltinClassType.PInt)) {
239-
VirtualFrame frame = null;
240-
if (gotState.profile(threadState != null)) {
241-
frame = PArguments.frameForCall(threadState);
242-
}
243-
warnNode.warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
244-
ErrorMessages.P_RETURNED_NON_P,
245-
this, "__index__", "int", this, "int");
246-
}
230+
public Object asIndexWithState(@SuppressWarnings("unused") ThreadState threadState) {
247231
return this;
248232
}
249233

@@ -275,10 +259,9 @@ public boolean canBeJavaDouble() {
275259
}
276260

277261
@ExportMessage
278-
public double asJavaDoubleWithState(ThreadState threadState,
279-
@CachedLibrary("this") PythonObjectLibrary lib,
262+
public double asJavaDoubleWithState(@SuppressWarnings("unused") ThreadState threadState,
280263
@Cached CastToJavaDoubleNode castToDouble) {
281-
return castToDouble.execute(lib.asIndexWithState(this, threadState));
264+
return castToDouble.execute(this);
282265
}
283266

284267
@SuppressWarnings("static-method")

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/PSocket.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,7 @@ private static InetSocketAddress getEphemeralAddress() {
110110

111111
private ServerSocketChannel serverSocket;
112112

113-
// TODO this should be true by default, but until we have threads, blocking sockets cause too
114-
// many deadlocks in the tests
115-
private boolean blocking = false;
113+
private boolean blocking = true;
116114

117115
private HashMap<Object, Object> options;
118116

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/socket/SocketBuiltins.java

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ abstract static class AcceptNode extends PythonUnaryBuiltinNode {
124124
@Specialization
125125
@TruffleBoundary
126126
Object accept(PSocket socket) {
127+
if (socket.getServerSocket() == null) {
128+
throw raiseOSError(null, OSErrorEnum.EINVAL);
129+
}
127130
try {
128131
SocketChannel acceptSocket = SocketUtils.accept(this, socket);
129132
if (acceptSocket == null) {
@@ -214,6 +217,7 @@ private static void doConnect(PSocket socket, Object[] hostAndPort) throws IOExc
214217
InetSocketAddress socketAddress = new InetSocketAddress((String) hostAndPort[0], (Integer) hostAndPort[1]);
215218
SocketChannel channel = SocketChannel.open();
216219
channel.connect(socketAddress);
220+
channel.configureBlocking(socket.isBlocking());
217221
socket.setSocket(channel);
218222
}
219223
}
@@ -338,6 +342,9 @@ Object listen(PSocket socket, PNone backlog) {
338342
abstract static class RecvNode extends PythonTernaryClinicBuiltinNode {
339343
@Specialization
340344
Object recv(VirtualFrame frame, PSocket socket, int bufsize, int flags) {
345+
if (socket.getSocket() == null) {
346+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
347+
}
341348
ByteBuffer readBytes = PythonUtils.allocateByteBuffer(bufsize);
342349
try {
343350
int length = SocketUtils.recv(this, socket, readBytes);
@@ -384,6 +391,9 @@ Object recvInto(VirtualFrame frame, PSocket socket, PMemoryView buffer, Object f
384391
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
385392
@Cached("create(__LEN__)") LookupAndCallUnaryNode callLen,
386393
@Cached("create(__SETITEM__)") LookupAndCallTernaryNode setItem) {
394+
if (socket.getSocket() == null) {
395+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
396+
}
387397
int bufferLen = lib.asSizeWithState(callLen.executeObject(frame, buffer), PArguments.getThreadState(frame));
388398
byte[] targetBuffer = new byte[bufferLen];
389399
ByteBuffer byteBuffer = PythonUtils.wrapByteBuffer(targetBuffer);
@@ -410,6 +420,9 @@ Object recvInto(VirtualFrame frame, PSocket socket, PByteArray buffer, Object fl
410420
@Cached("createBinaryProfile()") ConditionProfile byteStorage,
411421
@Cached SequenceStorageNodes.LenNode lenNode,
412422
@Cached("createSetItem()") SequenceStorageNodes.SetItemNode setItem) {
423+
if (socket.getSocket() == null) {
424+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
425+
}
413426
SequenceStorage storage = buffer.getSequenceStorage();
414427
int bufferLen = lenNode.execute(storage);
415428
if (byteStorage.profile(storage instanceof ByteSequenceStorage)) {
@@ -470,17 +483,14 @@ Object send(VirtualFrame frame, PSocket socket, PBytes bytes, Object flags,
470483
@Cached SequenceStorageNodes.ToByteArrayNode toBytes) {
471484
// TODO: do not ignore flags
472485
if (socket.getSocket() == null) {
473-
throw raise(OSError);
474-
}
475-
476-
if (!socket.isOpen()) {
477-
throw raise(OSError);
486+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
478487
}
479-
480488
int written;
481489
ByteBuffer buffer = PythonUtils.wrapByteBuffer(toBytes.execute(bytes.getSequenceStorage()));
482490
try {
483491
written = SocketUtils.send(this, socket, buffer);
492+
} catch (NotYetConnectedException e) {
493+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
484494
} catch (IOException e) {
485495
throw raise(OSError);
486496
}
@@ -500,6 +510,9 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flag
500510
@Cached SequenceStorageNodes.ToByteArrayNode toBytes,
501511
@Cached ConditionProfile hasTimeoutProfile) {
502512
// TODO: do not ignore flags
513+
if (socket.getSocket() == null) {
514+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
515+
}
503516
ByteBuffer buffer = PythonUtils.wrapByteBuffer(toBytes.execute(bytes.getSequenceStorage()));
504517
long timeoutMillis = socket.getTimeoutInMilliseconds();
505518
TimeoutHelper timeoutHelper = null;
@@ -513,6 +526,8 @@ Object sendAll(VirtualFrame frame, PSocket socket, PBytesLike bytes, Object flag
513526
int written;
514527
try {
515528
written = SocketUtils.send(this, socket, buffer, timeoutMillis);
529+
} catch (NotYetConnectedException e) {
530+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
516531
} catch (IOException e) {
517532
throw raise(OSError);
518533
}
@@ -606,24 +621,29 @@ Object setTimeout(PSocket socket, Object secondsObj,
606621
@GenerateNodeFactory
607622
abstract static class shutdownNode extends PythonBinaryBuiltinNode {
608623
@Specialization
609-
@TruffleBoundary
610-
Object family(PSocket socket, int how) {
611-
if (socket.getSocket() != null) {
612-
try {
613-
if (how == 0 || how == 2) {
614-
socket.getSocket().shutdownInput();
615-
}
616-
if (how == 1 || how == 2) {
617-
socket.getSocket().shutdownOutput();
618-
}
619-
} catch (IOException e) {
620-
throw raise(OSError);
621-
}
622-
} else {
624+
Object family(VirtualFrame frame, PSocket socket, int how) {
625+
if (socket.getSocket() == null) {
626+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
627+
}
628+
try {
629+
shutdown(socket, how);
630+
} catch (NotYetConnectedException e) {
631+
throw raiseOSError(frame, OSErrorEnum.ENOTCONN);
632+
} catch (IOException e) {
623633
throw raise(OSError);
624634
}
625635
return PNone.NO_VALUE;
626636
}
637+
638+
@TruffleBoundary
639+
private static void shutdown(PSocket socket, int how) throws IOException {
640+
if (how == 0 || how == 2) {
641+
socket.getSocket().shutdownInput();
642+
}
643+
if (how == 1 || how == 2) {
644+
socket.getSocket().shutdownOutput();
645+
}
646+
}
627647
}
628648

629649
// family

0 commit comments

Comments
 (0)