diff --git a/qiling/os/posix/syscall/socket.py b/qiling/os/posix/syscall/socket.py index ea6ee93ba..44dd1d591 100644 --- a/qiling/os/posix/syscall/socket.py +++ b/qiling/os/posix/syscall/socket.py @@ -11,7 +11,7 @@ from qiling.const import QL_ARCH, QL_OS, QL_VERBOSE from qiling.os.posix.const_mapping import socket_type_mapping, socket_level_mapping, socket_domain_mapping, socket_ip_option_mapping, socket_tcp_option_mapping, socket_option_mapping -from qiling.os.posix.const import NR_OPEN, EMFILE, EBADF, EAFNOSUPPORT, EINVAL, ENOTSOCK +from qiling.os.posix.const import NR_OPEN, EAGAIN, EMFILE, EBADF, EAFNOSUPPORT, EINVAL, ENOTSOCK from qiling.os.posix.filestruct import ql_socket from qiling.os.posix.structs import * @@ -668,7 +668,10 @@ def ql_syscall_recv(ql: Qiling, sockfd: int, buf: int, length: int, flags: int): if isinstance(sock, int): return sock - content = sock.recv(length, flags) + try: + content = sock.recv(length, flags) + except BlockingIOError: + return -EAGAIN if content: ql.log.debug("recv() CONTENT:") diff --git a/tests/test_posix.py b/tests/test_posix.py index 067bff84c..a2f267496 100644 --- a/tests/test_posix.py +++ b/tests/test_posix.py @@ -8,6 +8,7 @@ from test_elf import * from test_riscv import * from test_qltool import * +from test_socket import * if __name__ == "__main__": unittest.main() diff --git a/tests/test_socket.py b/tests/test_socket.py new file mode 100644 index 000000000..cb772c7e4 --- /dev/null +++ b/tests/test_socket.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +# +# Cross Platform and Multi Architecture Advanced Binary Emulation Framework +# + +import socket +import unittest + +from qiling import Qiling +from qiling.const import QL_ARCH, QL_OS, QL_VERBOSE +from qiling.os.posix.const import EAGAIN +from qiling.os.posix.filestruct import ql_socket +from qiling.os.posix.syscall.socket import ql_syscall_recv + + +class SocketTest(unittest.TestCase): + def test_recv_on_nonblocking_socket_returns_eagain(self) -> None: + ql = Qiling(code=b'\x00\x00\x00\x00', archtype=QL_ARCH.X8664, ostype=QL_OS.LINUX, verbose=QL_VERBOSE.OFF) + host_sock, peer_sock = socket.socketpair() + + try: + host_sock.setblocking(False) + ql.os.fd[3] = ql_socket(host_sock) + + buf = ql.mem.map_anywhere(0x1000) + + self.assertEqual(ql_syscall_recv(ql, 3, buf, 16, 0), -EAGAIN) + + finally: + ql.os.fd[3] = None + host_sock.close() + peer_sock.close() + + +if __name__ == "__main__": + unittest.main()