Skip to content

Return immediately when FD_SETSIZE is exceeded #9602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions ext/mysqli/tests/gh9590.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
Bug GH-9602 (stream_select does not abort upon exception or empty valid fd set)
--SKIPIF--
<?php
require_once('skipif.inc');
require_once('connect.inc');
require_once('skipifconnectfailure.inc');

if (!$IS_MYSQLND)
die("skip mysqlnd only feature, compile PHP using --with-mysqli=mysqlnd");

if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
die("skip cannot connect");

if (mysqli_get_server_version($link) < 50012)
die("skip Test needs SQL function SLEEP() available as of MySQL 5.0.12");

if (!function_exists('posix_setrlimit') || !posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1))
die('skip Failed to set POSIX_RLIMIT_NOFILE');
?>
--FILE--
<?php
posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1);

$fds = [];
for ($i = 0; $i < 1023; $i++) {
$fds[] = @fopen(__DIR__ . "/GH-9590-tmpfile.$i", 'w');
}

require_once('connect.inc');

function get_connection() {
global $host, $user, $passwd, $db, $port, $socket;

if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket))
printf("[001] [%d] %s\n", mysqli_connect_errno(), mysqli_connect_error());
return $link;
}


$mysqli1 = get_connection();
$mysqli2 = get_connection();

var_dump(mysqli_query($mysqli1, "SELECT SLEEP(0.10)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));
var_dump(mysqli_query($mysqli2, "SELECT SLEEP(0.20)", MYSQLI_ASYNC | MYSQLI_USE_RESULT));

$links = $errors = $reject = array($mysqli1, $mysqli2);
var_dump(mysqli_poll($links, $errors, $reject, 0, 50000));

mysqli_close($mysqli1);
mysqli_close($mysqli2);

print "done!";
Comment on lines +23 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: extra indent

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many tests in this directory are like that, I tried to keep the existing style

?>
--EXPECTF--
bool(true)
bool(true)

Warning: mysqli_poll(): You MUST recompile PHP with a larger value of FD_SETSIZE.
It is set to 1024, but you have descriptors numbered at least as high as %d.
--enable-fd-setsize=%d is recommended, but you may want to set it
to equal the maximum number of open files supported by your system,
in order to avoid seeing this error again at a later date. in %s on line %d
bool(false)
done!
--CLEAN--
<?php
for ($i = 0; $i < 1023; $i++) {
@unlink(__DIR__ . "/GH-9590-tmpfile.$i");
}
?>
4 changes: 3 additions & 1 deletion ext/mysqlnd/mysqlnd_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -2615,7 +2615,9 @@ mysqlnd_poll(MYSQLND **r_array, MYSQLND **e_array, MYSQLND ***dont_poll, long se
DBG_RETURN(FAIL);
}

PHP_SAFE_MAX_FD(max_fd, max_set_count);
if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
DBG_RETURN(FAIL);
}

/* Solaris + BSD do not like microsecond values which are >= 1 sec */
if (usec > 999999) {
Expand Down
4 changes: 3 additions & 1 deletion ext/sockets/sockets.c
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,9 @@ PHP_FUNCTION(socket_select)
RETURN_THROWS();
}

PHP_SAFE_MAX_FD(max_fd, 0); /* someone needs to make this look more like stream_socket_select */
if (!PHP_SAFE_MAX_FD(max_fd, 0)) {
RETURN_FALSE;
}

/* If seconds is not set to null, build the timeval, else we wait indefinitely */
if (!sec_is_null) {
Expand Down
4 changes: 3 additions & 1 deletion ext/standard/streamsfuncs.c
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,9 @@ PHP_FUNCTION(stream_select)
RETURN_THROWS();
}

PHP_SAFE_MAX_FD(max_fd, max_set_count);
if (!PHP_SAFE_MAX_FD(max_fd, max_set_count)) {
RETURN_FALSE;
}

/* If seconds is not set to null, build the timeval, else we wait indefinitely */
if (!secnull) {
Expand Down
38 changes: 38 additions & 0 deletions ext/standard/tests/streams/gh9590-001.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Bug GH-9590 001 (stream_select does not abort upon exception or empty valid fd set)
--SKIPIF--
<?php
if (!function_exists('posix_setrlimit') || !posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1)) {
die('skip Failed to set POSIX_RLIMIT_NOFILE');
}
?>
--FILE--
<?php

posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1);

$fds = [];
for ($i = 0; $i < 1023; $i++) {
$fds[] = @fopen(__DIR__ . "/GH-9590-001-tmpfile.$i", 'w');
}

list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

$r = [$a];
$w = $e = [];
var_dump(stream_select($r, $w, $e, PHP_INT_MAX));

?>
--EXPECTF--
Warning: stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
It is set to 1024, but you have descriptors numbered at least as high as %d.
--enable-fd-setsize=%d is recommended, but you may want to set it
to equal the maximum number of open files supported by your system,
in order to avoid seeing this error again at a later date. in %s on line %d
bool(false)
--CLEAN--
<?php
for ($i = 0; $i < 1023; $i++) {
@unlink(__DIR__ . "/GH-9590-001-tmpfile.$i");
}
?>
40 changes: 40 additions & 0 deletions ext/standard/tests/streams/gh9590-002.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--TEST--
Bug GH-9590 002 (stream_select does not abort upon exception or empty valid fd set)
--SKIPIF--
<?php
if (!function_exists('posix_setrlimit') || !posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1)) {
die('skip Failed to set POSIX_RLIMIT_NOFILE');
}
?>
--FILE--
<?php

posix_setrlimit(POSIX_RLIMIT_NOFILE, 2048, -1);

$fds = [];
for ($i = 0; $i < 1023; $i++) {
$fds[] = @fopen(__DIR__ . "/GH-9590-002-tmpfile.$i", 'w');
}

list($a, $b) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);

set_error_handler(function($errno, $errstr) { throw new \Exception($errstr); });

$r = [$a];
$w = $e = [];
var_dump(stream_select($r, $w, $e, PHP_INT_MAX));

?>
--EXPECTF--
Fatal error: Uncaught Exception: stream_select(): You MUST recompile PHP with a larger value of FD_SETSIZE.
It is set to 1024, but you have descriptors numbered at least as high as %d.
--enable-fd-setsize=%d is recommended, but you may want to set it
to equal the maximum number of open files supported by your system,
in order to avoid seeing this error again at a later date. in %s:%d
Stack trace:%a
--CLEAN--
<?php
for ($i = 0; $i < 1023; $i++) {
@unlink(__DIR__ . "/GH-9590-002-tmpfile.$i");
}
?>
9 changes: 8 additions & 1 deletion main/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,14 @@ PHPAPI int php_poll2(php_pollfd *ufds, unsigned int nfds, int timeout)
}
#endif

PHP_SAFE_MAX_FD(max_fd, nfds + 1);
if (!PHP_SAFE_MAX_FD(max_fd, nfds + 1)) {
#ifdef PHP_WIN32
WSASetLastError(WSAEINVAL);
#else
errno = ERANGE;
#endif
return -1;
}

FD_ZERO(&rset);
FD_ZERO(&wset);
Expand Down
21 changes: 19 additions & 2 deletions main/php_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,35 @@ static inline int php_pollfd_for_ms(php_socket_t fd, int events, int timeout)
/* emit warning and suggestion for unsafe select(2) usage */
PHPAPI void _php_emit_fd_setsize_warning(int max_fd);

static inline bool _php_check_fd_setsize(php_socket_t *max_fd, int setsize)
{
#ifdef PHP_WIN32
if (setsize + 1 >= FD_SETSIZE) {
_php_emit_fd_setsize_warning(setsize);
return false;
}
#else
if (*max_fd >= FD_SETSIZE) {
_php_emit_fd_setsize_warning(*max_fd);
*max_fd = FD_SETSIZE - 1;
return false;
}
#endif
return true;
}

#ifdef PHP_WIN32
/* it is safe to FD_SET too many fd's under win32; the macro will simply ignore
* descriptors that go beyond the default FD_SETSIZE */
# define PHP_SAFE_FD_SET(fd, set) FD_SET(fd, set)
# define PHP_SAFE_FD_CLR(fd, set) FD_CLR(fd, set)
# define PHP_SAFE_FD_ISSET(fd, set) FD_ISSET(fd, set)
# define PHP_SAFE_MAX_FD(m, n) do { if (n + 1 >= FD_SETSIZE) { _php_emit_fd_setsize_warning(n); }} while(0)
# define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n)
#else
# define PHP_SAFE_FD_SET(fd, set) do { if (fd < FD_SETSIZE) FD_SET(fd, set); } while(0)
# define PHP_SAFE_FD_CLR(fd, set) do { if (fd < FD_SETSIZE) FD_CLR(fd, set); } while(0)
# define PHP_SAFE_FD_ISSET(fd, set) ((fd < FD_SETSIZE) && FD_ISSET(fd, set))
# define PHP_SAFE_MAX_FD(m, n) do { if (m >= FD_SETSIZE) { _php_emit_fd_setsize_warning(m); m = FD_SETSIZE - 1; }} while(0)
# define PHP_SAFE_MAX_FD(m, n) _php_check_fd_setsize(&m, n)
#endif


Expand Down