Skip to content

Commit

Permalink
Fix compatibility with PHP 8.3 ZEND_CHECK_STACK_LIMIT (swoole#5223)
Browse files Browse the repository at this point in the history
* Fix compatibility with PHP 8.3 ZEND_CHECK_STACK_LIMIT

* Optimize
  • Loading branch information
Yurunsoft authored and deminy committed Dec 27, 2023
1 parent 6a5eb9d commit ff908bb
Show file tree
Hide file tree
Showing 6 changed files with 685 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,8 @@ EOF
thirdparty/php/sockets/conversions.cc \
thirdparty/php/sockets/sockaddr_conv.cc \
thirdparty/php/standard/var_decoder.cc \
thirdparty/php/standard/proc_open.cc"
thirdparty/php/standard/proc_open.cc \
thirdparty/php83/Zend/zend_call_stack.cc"

swoole_source_file="$swoole_source_file \
thirdparty/swoole_http_parser.c \
Expand Down
38 changes: 38 additions & 0 deletions ext-src/php_swoole_call_stack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
+----------------------------------------------------------------------+
| Swoole |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.apache.org/licenses/LICENSE-2.0.html |
| If you did not receive a copy of the Apache2.0 license and are unable|
| to obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Yurun <[email protected]> |
+----------------------------------------------------------------------+
*/

#pragma once

#include "php_swoole.h"

#ifdef ZEND_CHECK_STACK_LIMIT
#include "thirdparty/php83/Zend/zend_call_stack.h"
#endif

#ifdef ZEND_CHECK_STACK_LIMIT
#define HOOK_PHP_CALL_STACK(exp) \
zend_call_stack __stack; \
zend_call_stack_get(&__stack); \
auto __stack_base = EG(stack_base); \
auto __stack_limit = EG(stack_limit); \
EG(stack_base) = __stack.base; \
EG(stack_limit) = zend_call_stack_limit(__stack.base, __stack.max_size, EG(reserved_stack_size)); \
exp \
EG(stack_base) = __stack_base; \
EG(stack_limit) = __stack_limit;
#else
#define HOOK_PHP_CALL_STACK(exp) exp
#endif
6 changes: 5 additions & 1 deletion ext-src/swoole_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "php_swoole_server.h"
#include "php_swoole_http_server.h"
#include "php_swoole_process.h"
#include "php_swoole_call_stack.h"
#include "swoole_msg_queue.h"

#include "ext/standard/php_var.h"
Expand Down Expand Up @@ -1773,7 +1774,10 @@ static int php_swoole_server_dispatch_func(Server *serv, Connection *conn, SendD
zdata = &args[3];
ZVAL_STRINGL(zdata, data->data, data->info.len > SW_IPC_BUFFER_SIZE ? SW_IPC_BUFFER_SIZE : data->info.len);
}
if (UNEXPECTED(sw_zend_call_function_ex(nullptr, fci_cache, zdata ? 4 : 3, args, &retval) != SUCCESS)) {
HOOK_PHP_CALL_STACK(
auto call_result = sw_zend_call_function_ex(nullptr, fci_cache, zdata ? 4 : 3, args, &retval);
);
if (UNEXPECTED(call_result != SUCCESS)) {
php_swoole_error(E_WARNING, "%s->onDispatch handler error", SW_Z_OBJCE_NAME_VAL_P(zserv));
} else if (!ZVAL_IS_NULL(&retval)) {
worker_id = zval_get_long(&retval);
Expand Down
6 changes: 5 additions & 1 deletion ext-src/swoole_server_port.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "php_swoole_server.h"
#include "php_swoole_call_stack.h"

BEGIN_EXTERN_C()
#include "stubs/php_swoole_server_port_arginfo.h"
Expand Down Expand Up @@ -198,7 +199,10 @@ static ssize_t php_swoole_server_length_func(const Protocol *protocol, network::
ssize_t ret = -1;

ZVAL_STRINGL(&zdata, pl->buf, pl->buf_size);
if (UNEXPECTED(sw_zend_call_function_ex(nullptr, fci_cache, 1, &zdata, &retval) != SUCCESS)) {
HOOK_PHP_CALL_STACK(
auto call_result = sw_zend_call_function_ex(nullptr, fci_cache, 1, &zdata, &retval);
);
if (UNEXPECTED(call_result) != SUCCESS) {
php_swoole_fatal_error(E_WARNING, "length function handler error");
} else {
ret = zval_get_long(&retval);
Expand Down
Loading

0 comments on commit ff908bb

Please sign in to comment.