1 /*
2   +----------------------------------------------------------------------+
3   | Swoole                                                               |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 2.0 of the Apache license,    |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.apache.org/licenses/LICENSE-2.0.html                      |
9   | If you did not receive a copy of the Apache2.0 license and are unable|
10   | to obtain it through the world-wide-web, please send a note to       |
11   | license@swoole.com so we can mail you a copy immediately.            |
12   +----------------------------------------------------------------------+
13   | Author: Tianfeng Han  <mikan.tenny@gmail.com>                        |
14   +----------------------------------------------------------------------+
15 */
16 
17 #ifndef PHP_SWOOLE_PRIVATE_H
18 #define PHP_SWOOLE_PRIVATE_H
19 
20 // C++ build format macros must defined earlier
21 #ifdef __cplusplus
22 #define __STDC_FORMAT_MACROS
23 #endif
24 
25 #include "php_swoole.h"
26 
27 #define SW_HAVE_COUNTABLE 1
28 
29 #include "swoole_c_api.h"
30 #include "swoole_api.h"
31 #include "swoole_async.h"
32 
33 #ifdef SW_HAVE_ZLIB
34 #include <zlib.h>
35 #endif
36 
37 BEGIN_EXTERN_C()
38 #include <ext/date/php_date.h>
39 #include <ext/standard/url.h>
40 #include <ext/standard/info.h>
41 #include <ext/standard/php_array.h>
42 #include <ext/standard/php_var.h>
43 #include <ext/standard/basic_functions.h>
44 #include <ext/standard/php_http.h>
45 
46 #define PHP_SWOOLE_VERSION SWOOLE_VERSION
47 #define PHP_SWOOLE_CLIENT_USE_POLL
48 
49 extern PHPAPI int php_array_merge(zend_array *dest, zend_array *src);
50 
51 #ifdef PHP_WIN32
52 #define PHP_SWOOLE_API __declspec(dllexport)
53 #elif defined(__GNUC__) && __GNUC__ >= 4
54 #define PHP_SWOOLE_API __attribute__((visibility("default")))
55 #else
56 #define PHP_SWOOLE_API
57 #endif
58 
59 #define SW_CHECK_RETURN(s)                                                                                             \
60     if (s < 0) {                                                                                                       \
61         RETURN_FALSE;                                                                                                  \
62     } else {                                                                                                           \
63         RETURN_TRUE;                                                                                                   \
64     }
65 #define SW_LOCK_CHECK_RETURN(s)																						   \
66     zend_long ___tmp_return_value = s; 																				   \
67     if (___tmp_return_value == 0) {                                                                                    \
68         RETURN_TRUE;                                                                                                   \
69     } else {                                                                                                           \
70         zend_update_property_long(NULL, SW_Z8_OBJ_P(ZEND_THIS), SW_STRL("errCode"), ___tmp_return_value );             \
71         RETURN_FALSE;                                                                                                  \
72     }
73 
74 #define php_swoole_fatal_error(level, fmt_str, ...)                                                                    \
75     php_error_docref(NULL, level, (const char *) (fmt_str), ##__VA_ARGS__)
76 
77 #define php_swoole_error(level, fmt_str, ...)                                                                          \
78     if (SWOOLE_G(display_errors) || level == E_ERROR) php_swoole_fatal_error(level, fmt_str, ##__VA_ARGS__)
79 
80 #define php_swoole_sys_error(level, fmt_str, ...)                                                                      \
81     php_swoole_error(level, fmt_str ", Error: %s[%d]", ##__VA_ARGS__, strerror(errno), errno)
82 
83 #ifdef SW_USE_OPENSSL
84 #ifndef HAVE_OPENSSL
85 #error "Enable openssl support, require openssl library"
86 #endif
87 #endif
88 
89 #ifdef SW_USE_CARES
90 #ifndef HAVE_CARES
91 #error "Enable c-ares support, require c-ares library"
92 #endif
93 #endif
94 
95 #ifdef SW_SOCKETS
96 #include "ext/sockets/php_sockets.h"
97 #define SWOOLE_SOCKETS_SUPPORT
98 #endif
99 
100 #if PHP_VERSION_ID < 70200
101 #error "require PHP version 7.2 or later"
102 #endif
103 
104 #if defined(ZTS) && defined(SW_USE_THREAD_CONTEXT)
105 #error "thread context cannot be used with ZTS"
106 #endif
107 
108 //--------------------------------------------------------
109 #define SW_MAX_FIND_COUNT 100  // for swoole_server::connection_list
110 #define SW_PHP_CLIENT_BUFFER_SIZE 65535
111 //--------------------------------------------------------
112 enum php_swoole_client_callback_type {
113     SW_CLIENT_CB_onConnect = 1,
114     SW_CLIENT_CB_onReceive,
115     SW_CLIENT_CB_onClose,
116     SW_CLIENT_CB_onError,
117     SW_CLIENT_CB_onBufferFull,
118     SW_CLIENT_CB_onBufferEmpty,
119 #ifdef SW_USE_OPENSSL
120     SW_CLIENT_CB_onSSLReady,
121 #endif
122 };
123 //---------------------------------------------------------
124 #define SW_FLAG_KEEP (1u << 12)
125 #define SW_FLAG_ASYNC (1u << 10)
126 #define SW_FLAG_SYNC (1u << 11)
127 //---------------------------------------------------------
128 enum php_swoole_fd_type {
129     PHP_SWOOLE_FD_STREAM_CLIENT = SW_FD_STREAM_CLIENT,
130     PHP_SWOOLE_FD_DGRAM_CLIENT,
131     PHP_SWOOLE_FD_MYSQL,
132     PHP_SWOOLE_FD_REDIS,
133     PHP_SWOOLE_FD_HTTPCLIENT,
134     PHP_SWOOLE_FD_PROCESS_STREAM,
135     PHP_SWOOLE_FD_MYSQL_CORO,
136     PHP_SWOOLE_FD_REDIS_CORO,
137     PHP_SWOOLE_FD_POSTGRESQL,
138     PHP_SWOOLE_FD_SOCKET,
139     PHP_SWOOLE_FD_CO_CURL,
140 };
141 //---------------------------------------------------------
142 enum php_swoole_req_status {
143     PHP_SWOOLE_RINIT_BEGIN,
144     PHP_SWOOLE_RINIT_END,
145     PHP_SWOOLE_CALL_USER_SHUTDOWNFUNC_BEGIN,
146     PHP_SWOOLE_RSHUTDOWN_BEGIN,
147     PHP_SWOOLE_RSHUTDOWN_END,
148 };
149 //---------------------------------------------------------
150 enum php_swoole_hook_type {
151     PHP_SWOOLE_HOOK_BEFORE_ENABLE_HOOK = SW_GLOBAL_HOOK_USER,
152     PHP_SWOOLE_HOOK_AFTER_ENABLE_HOOK,
153 };
154 //---------------------------------------------------------
155 
156 
php_swoole_socktype(long type)157 static sw_inline enum swSocketType php_swoole_socktype(long type) {
158     return (enum swSocketType)(type & (~SW_FLAG_SYNC) & (~SW_FLAG_ASYNC) & (~SW_FLAG_KEEP) & (~SW_SOCK_SSL));
159 }
160 
161 extern zend_class_entry *swoole_event_ce;
162 extern zend_class_entry *swoole_timer_ce;
163 extern zend_class_entry *swoole_socket_coro_ce;
164 extern zend_class_entry *swoole_client_ce;
165 extern zend_class_entry *swoole_server_ce;
166 extern zend_object_handlers swoole_server_handlers;
167 extern zend_class_entry *swoole_redis_server_ce;
168 extern zend_object_handlers swoole_redis_server_handlers;
169 extern zend_class_entry *swoole_connection_iterator_ce;
170 extern zend_class_entry *swoole_process_ce;
171 extern zend_class_entry *swoole_http_server_ce;
172 extern zend_object_handlers swoole_http_server_handlers;
173 extern zend_class_entry *swoole_websocket_server_ce;
174 extern zend_class_entry *swoole_websocket_frame_ce;
175 extern zend_class_entry *swoole_server_port_ce;
176 extern zend_class_entry *swoole_exception_ce;
177 extern zend_object_handlers swoole_exception_handlers;
178 extern zend_class_entry *swoole_error_ce;
179 
180 PHP_FUNCTION(swoole_clear_dns_cache);
181 PHP_FUNCTION(swoole_last_error);
182 PHP_FUNCTION(swoole_set_process_name);
183 //---------------------------------------------------------
184 //                  Coroutine API
185 //---------------------------------------------------------
186 PHP_FUNCTION(swoole_coroutine_create);
187 PHP_FUNCTION(swoole_coroutine_exec);
188 PHP_FUNCTION(swoole_coroutine_gethostbyname);
189 PHP_FUNCTION(swoole_coroutine_defer);
190 PHP_FUNCTION(swoole_coroutine_socketpair);
191 PHP_FUNCTION(swoole_test_kernel_coroutine);  // for tests
192 //---------------------------------------------------------
193 //                  event
194 //---------------------------------------------------------
195 PHP_FUNCTION(swoole_client_select);
196 //---------------------------------------------------------
197 //                  async[coro]
198 //---------------------------------------------------------
199 PHP_FUNCTION(swoole_async_set);
200 PHP_FUNCTION(swoole_async_dns_lookup_coro);
201 //---------------------------------------------------------
202 //                  error
203 //---------------------------------------------------------
204 #define SW_STRERROR_SYSTEM 0
205 #define SW_STRERROR_GAI 1
206 #define SW_STRERROR_DNS 2
207 #define SW_STRERROR_SWOOLE 9
208 
209 /**
210  * MINIT <Sort by dependency>
211  * ==============================================================
212  */
213 void php_swoole_event_minit(int module_number);
214 // base
215 void php_swoole_atomic_minit(int module_number);
216 void php_swoole_lock_minit(int module_number);
217 void php_swoole_process_minit(int module_number);
218 void php_swoole_process_pool_minit(int module_number);
219 void php_swoole_table_minit(int module_number);
220 void php_swoole_timer_minit(int module_number);
221 // coroutine
222 void php_swoole_coroutine_minit(int module_number);
223 void php_swoole_coroutine_system_minit(int module_number);
224 void php_swoole_coroutine_scheduler_minit(int module_number);
225 void php_swoole_channel_coro_minit(int module_number);
226 void php_swoole_runtime_minit(int module_number);
227 // client
228 void php_swoole_socket_coro_minit(int module_number);
229 void php_swoole_client_minit(int module_number);
230 void php_swoole_client_coro_minit(int module_number);
231 void php_swoole_http_client_coro_minit(int module_number);
232 void php_swoole_mysql_coro_minit(int module_number);
233 void php_swoole_redis_coro_minit(int module_number);
234 #ifdef SW_USE_HTTP2
235 void php_swoole_http2_client_coro_minit(int module_number);
236 #endif
237 // server
238 void php_swoole_server_minit(int module_number);
239 void php_swoole_server_port_minit(int module_number);
240 void php_swoole_http_request_minit(int module_number);
241 void php_swoole_http_response_minit(int module_number);
242 void php_swoole_http_server_minit(int module_number);
243 void php_swoole_http_server_coro_minit(int module_number);
244 void php_swoole_websocket_server_minit(int module_number);
245 void php_swoole_redis_server_minit(int module_number);
246 
247 /**
248  * RINIT
249  * ==============================================================
250  */
251 void php_swoole_coroutine_rinit();
252 void php_swoole_runtime_rinit();
253 
254 /**
255  * RSHUTDOWN
256  * ==============================================================
257  */
258 void php_swoole_async_coro_rshutdown();
259 void php_swoole_redis_server_rshutdown();
260 void php_swoole_coroutine_rshutdown();
261 void php_swoole_runtime_rshutdown();
262 void php_swoole_server_rshutdown();
263 
264 int php_swoole_reactor_init();
265 void php_swoole_set_global_option(zend_array *vht);
266 void php_swoole_set_coroutine_option(zend_array *vht);
267 void php_swoole_set_aio_option(zend_array *vht);
268 
269 // shutdown
270 void php_swoole_register_shutdown_function(const char *function);
271 void php_swoole_register_shutdown_function_prepend(const char *function);
272 
273 // event
274 void php_swoole_event_init();
275 void php_swoole_event_wait();
276 void php_swoole_event_exit();
277 
278 /**
279  * MSHUTDOWN
280  * ==============================================================
281  */
282 void php_swoole_runtime_mshutdown();
283 
php_swoole_websocket_frame_is_object(zval * zdata)284 static sw_inline zend_bool php_swoole_websocket_frame_is_object(zval *zdata) {
285     return Z_TYPE_P(zdata) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zdata), swoole_websocket_frame_ce);
286 }
287 
php_swoole_get_send_data(zval * zdata,char ** str)288 static sw_inline size_t php_swoole_get_send_data(zval *zdata, char **str) {
289     convert_to_string(zdata);
290     *str = Z_STRVAL_P(zdata);
291     return Z_STRLEN_P(zdata);
292 }
293 
294 int php_swoole_convert_to_fd(zval *zsocket);
295 int php_swoole_convert_to_fd_ex(zval *zsocket, int *async);
296 
297 #ifdef SWOOLE_SOCKETS_SUPPORT
298 php_socket *php_swoole_convert_to_socket(int sock);
299 #endif
300 
301 zend_bool php_swoole_signal_isset_handler(int signo);
302 
303 /* PHP 7 compatibility patches */
304 #define sw_zend_bailout() zend_bailout()
305 
306 // Fixed in php-7.2.3RC1 (https://github.com/php/php-src/commit/e88e83d3e5c33fcd76f08b23e1a2e4e8dc98ce41)
307 #if PHP_MAJOR_VERSION == 7 && ((PHP_MINOR_VERSION == 2 && PHP_RELEASE_VERSION < 3))
308 // See https://github.com/php/php-src/commit/0495bf5650995cd8f18d6a9909eb4c5dcefde669
309 // Then https://github.com/php/php-src/commit/2dcfd8d16f5fa69582015cbd882aff833075a34c
310 // See https://github.com/php/php-src/commit/52db03b3e52bfc886896925d050af79bc4dc1ba3
311 #if PHP_MINOR_VERSION == 2
312 #define SW_ZEND_WRONG_PARAMETERS_COUNT_ERROR                                                                           \
313     zend_wrong_parameters_count_error(_flags &ZEND_PARSE_PARAMS_THROW, _num_args, _min_num_args, _max_num_args)
314 #else
315 #define SW_ZEND_WRONG_PARAMETERS_COUNT_ERROR zend_wrong_parameters_count_error(_num_args, _min_num_args, _max_num_args)
316 #endif
317 
318 #undef ZEND_PARSE_PARAMETERS_START_EX
319 
320 #define ZEND_PARSE_PARAMETERS_START_EX(flags, min_num_args, max_num_args)                                              \
321     do {                                                                                                               \
322         const int _flags = (flags);                                                                                    \
323         int _min_num_args = (min_num_args);                                                                            \
324         int _max_num_args = (max_num_args);                                                                            \
325         int _num_args = EX_NUM_ARGS();                                                                                 \
326         int _i;                                                                                                        \
327         zval *_real_arg, *_arg = NULL;                                                                                 \
328         zend_expected_type _expected_type = Z_EXPECTED_LONG;                                                           \
329         char *_error = NULL;                                                                                           \
330         zend_bool _dummy;                                                                                              \
331         zend_bool _optional = 0;                                                                                       \
332         int error_code = ZPP_ERROR_OK;                                                                                 \
333         ((void) _i);                                                                                                   \
334         ((void) _real_arg);                                                                                            \
335         ((void) _arg);                                                                                                 \
336         ((void) _expected_type);                                                                                       \
337         ((void) _error);                                                                                               \
338         ((void) _dummy);                                                                                               \
339         ((void) _optional);                                                                                            \
340                                                                                                                        \
341         do {                                                                                                           \
342             if (UNEXPECTED(_num_args < _min_num_args) ||                                                               \
343                 (UNEXPECTED(_num_args > _max_num_args) && EXPECTED(_max_num_args >= 0))) {                             \
344                 if (!(_flags & ZEND_PARSE_PARAMS_QUIET)) {                                                             \
345                     SW_ZEND_WRONG_PARAMETERS_COUNT_ERROR;                                                              \
346                 }                                                                                                      \
347                 error_code = ZPP_ERROR_FAILURE;                                                                        \
348                 break;                                                                                                 \
349             }                                                                                                          \
350             _i = 0;                                                                                                    \
351             _real_arg = ZEND_CALL_ARG(execute_data, 0);
352 #endif
353 
354 /* PHP 7.3 compatibility macro {{{*/
355 
356 #ifndef GC_ADDREF
357 #define GC_ADDREF(ref) ++GC_REFCOUNT(ref)
358 #define GC_DELREF(ref) --GC_REFCOUNT(ref)
359 #endif
360 
361 #ifndef ZEND_CLOSURE_OBJECT
362 #define ZEND_CLOSURE_OBJECT(func) (zend_object *) func->op_array.prototype
363 #endif
364 
365 /* PHP 7.4 compatibility macro {{{*/
366 #ifndef ZEND_COMPILE_EXTENDED_STMT
367 #define ZEND_COMPILE_EXTENDED_STMT ZEND_COMPILE_EXTENDED_INFO
368 #endif
369 
370 #ifndef ZVAL_EMPTY_ARRAY
371 #define ZVAL_EMPTY_ARRAY(zval) (array_init((zval)))
372 #endif
373 #ifndef RETVAL_EMPTY_ARRAY
374 #define RETVAL_EMPTY_ARRAY() ZVAL_EMPTY_ARRAY(return_value)
375 #endif
376 #ifndef RETURN_EMPTY_ARRAY
377 #define RETURN_EMPTY_ARRAY()                                                                                           \
378     do {                                                                                                               \
379         RETVAL_EMPTY_ARRAY();                                                                                          \
380         return;                                                                                                        \
381     } while (0)
382 #endif
383 
384 #ifndef ZEND_THIS
385 #define ZEND_THIS (&EX(This))
386 #endif
387 
388 #ifndef ZEND_THIS_OBJECT
389 #define ZEND_THIS_OBJECT Z_OBJ_P(ZEND_THIS)
390 #endif
391 
392 #ifndef E_FATAL_ERRORS
393 #define E_FATAL_ERRORS (E_ERROR | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR | E_PARSE)
394 #endif
395 /*}}}*/
396 
397 /* PHP 8 compatibility macro {{{*/
398 #if PHP_VERSION_ID < 80000
399 #define sw_zend7_object zval
400 #define SW_Z7_OBJ_P(object) Z_OBJ_P(object)
401 #define SW_Z8_OBJ_P(zobj) zobj
402 #else
403 #define sw_zend7_object zend_object
404 #define SW_Z7_OBJ_P(object) object
405 #define SW_Z8_OBJ_P(zobj) Z_OBJ_P(zobj)
406 #endif
407 /*}}}*/
408 
409 #if PHP_VERSION_ID < 70400
410 typedef size_t php_stream_size_t;
411 #else
412 typedef ssize_t php_stream_size_t;
413 #endif
414 
415 /* PHP 7 wrapper functions / macros */
416 
417 //----------------------------------Zval API------------------------------------
418 
419 // Deprecated: do not use it anymore
420 // do not use sw_copy_to_stack(return_value, foo);
421 #define sw_copy_to_stack(ptr, val)                                                                                     \
422     do {                                                                                                               \
423         (val) = *(zval *) (ptr);                                                                                       \
424         (ptr) = &(val);                                                                                                \
425     } while (0)
426 
427 #if PHP_VERSION_ID < 80000
428 #define SW_ZVAL_SOCKET(return_value, result)                                                                           \
429     ZVAL_RES(return_value, zend_register_resource((void *) (result), php_sockets_le_socket()))
430 #else
431 #define SW_ZVAL_SOCKET(return_value, result) ZVAL_OBJ(return_value, &result->std)
432 #endif
433 
434 #if PHP_VERSION_ID < 80000
435 #define SW_Z_SOCKET_P(zsocket) (php_socket *) zend_fetch_resource_ex(zsocket, nullptr, php_sockets_le_socket())
436 #else
437 #define SW_Z_SOCKET_P(zsocket) Z_SOCKET_P(zsocket)
438 #endif
439 
440 #ifndef ZVAL_IS_BOOL
ZVAL_IS_BOOL(zval * v)441 static sw_inline zend_bool ZVAL_IS_BOOL(zval *v) {
442     return Z_TYPE_P(v) == IS_TRUE || Z_TYPE_P(v) == IS_FALSE;
443 }
444 #endif
445 
446 #ifndef ZVAL_IS_TRUE
ZVAL_IS_TRUE(zval * v)447 static sw_inline zend_bool ZVAL_IS_TRUE(zval *v) {
448     return Z_TYPE_P(v) == IS_TRUE;
449 }
450 #endif
451 
452 #ifndef ZVAL_IS_FALSE
ZVAL_IS_FALSE(zval * v)453 static sw_inline zend_bool ZVAL_IS_FALSE(zval *v) {
454     return Z_TYPE_P(v) == IS_FALSE;
455 }
456 #endif
457 
458 #ifndef ZVAL_IS_LONG
ZVAL_IS_LONG(zval * v)459 static sw_inline zend_bool ZVAL_IS_LONG(zval *v) {
460     return Z_TYPE_P(v) == IS_LONG;
461 }
462 #endif
463 
464 #ifndef ZVAL_IS_STRING
ZVAL_IS_STRING(zval * v)465 static sw_inline zend_bool ZVAL_IS_STRING(zval *v) {
466     return Z_TYPE_P(v) == IS_STRING;
467 }
468 #endif
469 
470 #ifndef Z_BVAL_P
Z_BVAL_P(zval * v)471 static sw_inline zend_bool Z_BVAL_P(zval *v) {
472     return Z_TYPE_P(v) == IS_TRUE;
473 }
474 #endif
475 
476 #ifndef ZVAL_IS_ARRAY
ZVAL_IS_ARRAY(zval * v)477 static sw_inline zend_bool ZVAL_IS_ARRAY(zval *v) {
478     return Z_TYPE_P(v) == IS_ARRAY;
479 }
480 #endif
481 
482 #ifndef ZVAL_IS_OBJECT
ZVAL_IS_OBJECT(zval * v)483 static sw_inline zend_bool ZVAL_IS_OBJECT(zval *v) {
484     return Z_TYPE_P(v) == IS_OBJECT;
485 }
486 #endif
487 
488 #ifndef IS_MIXED
489 #define IS_MIXED 0
490 #endif
491 
sw_malloc_zval()492 static sw_inline zval *sw_malloc_zval() {
493     return (zval *) emalloc(sizeof(zval));
494 }
495 
sw_zval_dup(zval * val)496 static sw_inline zval *sw_zval_dup(zval *val) {
497     zval *dup = sw_malloc_zval();
498     memcpy(dup, val, sizeof(zval));
499     return dup;
500 }
501 
sw_zval_free(zval * val)502 static sw_inline void sw_zval_free(zval *val) {
503     zval_ptr_dtor(val);
504     efree(val);
505 }
506 
507 //----------------------------------Constant API------------------------------------
508 
509 #define SW_REGISTER_NULL_CONSTANT(name) REGISTER_NULL_CONSTANT(name, CONST_CS | CONST_PERSISTENT)
510 #define SW_REGISTER_BOOL_CONSTANT(name, value) REGISTER_BOOL_CONSTANT(name, value, CONST_CS | CONST_PERSISTENT)
511 #define SW_REGISTER_LONG_CONSTANT(name, value) REGISTER_LONG_CONSTANT(name, value, CONST_CS | CONST_PERSISTENT)
512 #define SW_REGISTER_DOUBLE_CONSTANT(name, value) REGISTER_DOUBLE_CONSTANT(name, value, CONST_CS | CONST_PERSISTENT)
513 #define SW_REGISTER_STRING_CONSTANT(name, value)                                                                       \
514     REGISTER_STRING_CONSTANT(name, (char *) value, CONST_CS | CONST_PERSISTENT)
515 #define SW_REGISTER_STRINGL_CONSTANT(name, value)                                                                      \
516     REGISTER_STRINGL_CONSTANT(name, (char *) value, CONST_CS | CONST_PERSISTENT)
517 
518 //----------------------------------Number API-----------------------------------
519 
520 #define sw_php_math_round(value, places, mode) _php_math_round(value, places, mode)
521 
522 //----------------------------------String API-----------------------------------
523 
524 #define SW_PHP_OB_START(zoutput)                                                                                       \
525     zval zoutput;                                                                                                      \
526     do {                                                                                                               \
527         php_output_start_user(NULL, 0, PHP_OUTPUT_HANDLER_STDFLAGS);
528 #define SW_PHP_OB_END()                                                                                                \
529     php_output_get_contents(&zoutput);                                                                                 \
530     php_output_discard();                                                                                              \
531     }                                                                                                                  \
532     while (0)
533 
sw_zend_string_recycle(zend_string * s,size_t alloc_len,size_t real_len)534 static sw_inline zend_string *sw_zend_string_recycle(zend_string *s, size_t alloc_len, size_t real_len) {
535     SW_ASSERT(!ZSTR_IS_INTERNED(s));
536     if (UNEXPECTED(alloc_len != real_len)) {
537         if (alloc_len > SwooleG.pagesize && alloc_len > real_len * 2) {
538             s = zend_string_realloc(s, real_len, 0);
539         } else {
540             ZSTR_LEN(s) = real_len;
541         }
542     }
543     ZSTR_VAL(s)[real_len] = '\0';
544     return s;
545 }
546 
547 //----------------------------------Array API------------------------------------
548 
549 #define php_swoole_array_length(zarray) zend_hash_num_elements(Z_ARRVAL_P(zarray))
550 #define php_swoole_array_get_value(ht, str, v) ((v = zend_hash_str_find(ht, str, sizeof(str) - 1)) && !ZVAL_IS_NULL(v))
551 #define php_swoole_array_get_value_ex(ht, str, v) ((v = zend_hash_str_find(ht, str, strlen(str))) && !ZVAL_IS_NULL(v))
552 
php_swoole_array_length_safe(zval * zarray)553 static sw_inline int php_swoole_array_length_safe(zval *zarray) {
554     if (zarray && ZVAL_IS_ARRAY(zarray)) {
555         return php_swoole_array_length(zarray);
556     } else {
557         return 0;
558     }
559 }
560 
561 void php_swoole_sha1(const char *str, int _len, uchar *digest);
562 void php_swoole_sha256(const char *str, int _len, uchar *digest);
563 
564 #define SW_HASHTABLE_FOREACH_START(ht, _val)                                                                           \
565     ZEND_HASH_FOREACH_VAL(ht, _val);                                                                                   \
566     {
567 #define SW_HASHTABLE_FOREACH_START2(ht, k, klen, ktype, _val)                                                          \
568     zend_string *_foreach_key;                                                                                         \
569     ZEND_HASH_FOREACH_STR_KEY_VAL(ht, _foreach_key, _val);                                                             \
570     if (!_foreach_key) {                                                                                               \
571         k = NULL;                                                                                                      \
572         klen = 0;                                                                                                      \
573         ktype = 0;                                                                                                     \
574     } else {                                                                                                           \
575         k = ZSTR_VAL(_foreach_key), klen = ZSTR_LEN(_foreach_key);                                                     \
576         ktype = 1;                                                                                                     \
577     }                                                                                                                  \
578     {
579 #define SW_HASHTABLE_FOREACH_END()                                                                                     \
580     }                                                                                                                  \
581     ZEND_HASH_FOREACH_END();
582 
add_assoc_ulong_safe_ex(zval * arg,const char * key,size_t key_len,zend_ulong value)583 static sw_inline void add_assoc_ulong_safe_ex(zval *arg, const char *key, size_t key_len, zend_ulong value) {
584     if (sw_likely(value <= ZEND_LONG_MAX)) {
585         add_assoc_long_ex(arg, key, key_len, value);
586     } else {
587         char buf[MAX_LENGTH_OF_LONG + 1];
588         size_t len = sw_snprintf(buf, sizeof(buf), ZEND_ULONG_FMT, value);
589         add_assoc_stringl_ex(arg, key, key_len, buf, len);
590     }
591 }
592 
add_assoc_ulong_safe(zval * arg,const char * key,zend_ulong value)593 static sw_inline void add_assoc_ulong_safe(zval *arg, const char *key, zend_ulong value) {
594     add_assoc_ulong_safe_ex(arg, key, strlen(key), value);
595 }
596 
597 //----------------------------------Class API------------------------------------
598 
599 #define SW_Z_OBJCE_NAME_VAL_P(zobject) ZSTR_VAL(Z_OBJCE_P(zobject)->name)
600 
601 /* PHP 7 class declaration macros */
602 
603 #define SW_INIT_CLASS_ENTRY_BASE(module, namespace_name, snake_name, short_name, methods, parent_ce)                   \
604     do {                                                                                                               \
605         zend_class_entry _##module##_ce = {};                                                                          \
606         INIT_CLASS_ENTRY(_##module##_ce, namespace_name, methods);                                                     \
607         module##_ce = zend_register_internal_class_ex(&_##module##_ce, parent_ce);                                     \
608         if (snake_name) SW_CLASS_ALIAS(snake_name, module);                                                            \
609         if (short_name) SW_CLASS_ALIAS_SHORT_NAME(short_name, module);                                                 \
610     } while (0)
611 
612 #define SW_INIT_CLASS_ENTRY(module, namespace_name, snake_name, short_name, methods)                                   \
613     SW_INIT_CLASS_ENTRY_BASE(module, namespace_name, snake_name, short_name, methods, NULL);                           \
614     memcpy(&module##_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers))
615 
616 #define SW_INIT_CLASS_ENTRY_EX(module, namespace_name, snake_name, short_name, methods, parent_module)                 \
617     SW_INIT_CLASS_ENTRY_BASE(module, namespace_name, snake_name, short_name, methods, parent_module##_ce);             \
618     memcpy(&module##_handlers, &parent_module##_handlers, sizeof(zend_object_handlers))
619 
620 #define SW_INIT_CLASS_ENTRY_EX2(                                                                                       \
621     module, namespace_name, snake_name, short_name, methods, parent_module_ce, parent_module_handlers)                 \
622     SW_INIT_CLASS_ENTRY_BASE(module, namespace_name, snake_name, short_name, methods, parent_module_ce);               \
623     memcpy(&module##_handlers, parent_module_handlers, sizeof(zend_object_handlers))
624 
625 // Data Object: no methods, no parent
626 #define SW_INIT_CLASS_ENTRY_DATA_OBJECT(module, namespace_name)                                                        \
627     SW_INIT_CLASS_ENTRY_BASE(module, namespace_name, NULL, NULL, NULL, NULL);                                          \
628     memcpy(&module##_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers))
629 
630 #define SW_CLASS_ALIAS(name, module)                                                                                   \
631     do {                                                                                                               \
632         if (name) {                                                                                                    \
633             sw_zend_register_class_alias(ZEND_STRL(name), module##_ce);                                                \
634         }                                                                                                              \
635     } while (0)
636 
637 #define SW_CLASS_ALIAS_SHORT_NAME(short_name, module)                                                                  \
638     do {                                                                                                               \
639         if (SWOOLE_G(use_shortname)) {                                                                                 \
640             SW_CLASS_ALIAS(short_name, module);                                                                        \
641         }                                                                                                              \
642     } while (0)
643 
644 #if PHP_VERSION_ID < 80100
645 #define SW_SET_CLASS_NOT_SERIALIZABLE(module)                                                                          \
646     module##_ce->serialize = zend_class_serialize_deny;                                                                               \
647     module##_ce->unserialize = zend_class_unserialize_deny;
648 #else
649 #define SW_SET_CLASS_NOT_SERIALIZABLE(module)                                                                          \
650     module##_ce->ce_flags |= ZEND_ACC_NOT_SERIALIZABLE;
651 #endif
652 
653 #define sw_zend_class_clone_deny NULL
654 #define SW_SET_CLASS_CLONEABLE(module, _clone_obj) module##_handlers.clone_obj = _clone_obj
655 
656 #define SW_SET_CLASS_UNSET_PROPERTY_HANDLER(module, _unset_property) module##_handlers.unset_property = _unset_property
657 
658 #define SW_SET_CLASS_CREATE(module, _create_object) module##_ce->create_object = _create_object
659 
660 #define SW_SET_CLASS_DTOR(module, _dtor_obj) module##_handlers.dtor_obj = _dtor_obj
661 
662 #define SW_SET_CLASS_FREE(module, _free_obj) module##_handlers.free_obj = _free_obj
663 
664 #define SW_SET_CLASS_CREATE_AND_FREE(module, _create_object, _free_obj)                                                \
665     SW_SET_CLASS_CREATE(module, _create_object);                                                                       \
666     SW_SET_CLASS_FREE(module, _free_obj)
667 
668 #define SW_SET_CLASS_CUSTOM_OBJECT(module, _create_object, _free_obj, _struct, _std)                                   \
669     SW_SET_CLASS_CREATE_AND_FREE(module, _create_object, _free_obj);                                                   \
670     module##_handlers.offset = XtOffsetOf(_struct, _std)
671 
672 #define SW_PREVENT_USER_DESTRUCT()                                                                                     \
673     do {                                                                                                               \
674         if (sw_unlikely(!(GC_FLAGS(Z_OBJ_P(ZEND_THIS)) & IS_OBJ_DESTRUCTOR_CALLED))) {                                 \
675             RETURN_NULL();                                                                                             \
676         }                                                                                                              \
677     } while (0)
678 
679 #define SW_FUNCTION_ALIAS(origin_function_table, origin, alias_function_table, alias)                                  \
680     sw_zend_register_function_alias(origin_function_table, ZEND_STRL(origin), alias_function_table, ZEND_STRL(alias))
681 
sw_zend_register_function_alias(zend_array * origin_function_table,const char * origin,size_t origin_length,zend_array * alias_function_table,const char * alias,size_t alias_length)682 static sw_inline int sw_zend_register_function_alias(zend_array *origin_function_table,
683                                                      const char *origin,
684                                                      size_t origin_length,
685                                                      zend_array *alias_function_table,
686                                                      const char *alias,
687                                                      size_t alias_length) {
688     zend_string *lowercase_origin = zend_string_alloc(origin_length, 0);
689     zend_str_tolower_copy(ZSTR_VAL(lowercase_origin), origin, origin_length);
690     zend_function *origin_function = (zend_function *) zend_hash_find_ptr(origin_function_table, lowercase_origin);
691     zend_string_release(lowercase_origin);
692     if (UNEXPECTED(!origin_function)) {
693         return FAILURE;
694     }
695     SW_ASSERT(origin_function->common.type == ZEND_INTERNAL_FUNCTION);
696     char *_alias = (char *) emalloc(alias_length + 1);
697     ((char *) memcpy(_alias, alias, alias_length))[alias_length] = '\0';
698     zend_function_entry zfe[] = {{_alias,
699                                   origin_function->internal_function.handler,
700                                   ((zend_internal_arg_info *) origin_function->common.arg_info) - 1,
701                                   origin_function->common.num_args,
702                                   0},
703                                  PHP_FE_END};
704     int ret =
705         zend_register_functions(origin_function->common.scope, zfe, alias_function_table, origin_function->common.type);
706     efree(_alias);
707     return ret;
708 }
709 
sw_zend_register_class_alias(const char * name,size_t name_len,zend_class_entry * ce)710 static sw_inline int sw_zend_register_class_alias(const char *name, size_t name_len, zend_class_entry *ce) {
711     zend_string *_name;
712     if (name[0] == '\\') {
713         _name = zend_string_init(name, name_len, 1);
714         zend_str_tolower_copy(ZSTR_VAL(_name), name + 1, name_len - 1);
715     } else {
716         _name = zend_string_init(name, name_len, 1);
717         zend_str_tolower_copy(ZSTR_VAL(_name), name, name_len);
718     }
719 
720     zend_string *_interned_name = zend_new_interned_string(_name);
721 
722 #if PHP_VERSION_ID >= 70300
723     return zend_register_class_alias_ex(ZSTR_VAL(_interned_name), ZSTR_LEN(_interned_name), ce, 1);
724 #else
725     return zend_register_class_alias_ex(ZSTR_VAL(_interned_name), ZSTR_LEN(_interned_name), ce);
726 #endif
727 }
728 
729 #if PHP_VERSION_ID < 70300
730 /* Allocates object type and zeros it, but not the properties.
731  * Properties MUST be initialized using object_properties_init(). */
zend_object_alloc(size_t obj_size,zend_class_entry * ce)732 static zend_always_inline void *zend_object_alloc(size_t obj_size, zend_class_entry *ce) {
733     void *obj = emalloc(obj_size + zend_object_properties_size(ce));
734     /* Subtraction of sizeof(zval) is necessary, because zend_object_properties_size() may be
735      * -sizeof(zval), if the object has no properties. */
736     memset(obj, 0, obj_size - sizeof(zval));
737     return obj;
738 }
739 #endif
740 
sw_zend_create_object(zend_class_entry * ce,zend_object_handlers * handlers)741 static sw_inline zend_object *sw_zend_create_object(zend_class_entry *ce, zend_object_handlers *handlers) {
742     zend_object *object = (zend_object *) zend_object_alloc(sizeof(zend_object), ce);
743     zend_object_std_init(object, ce);
744     object_properties_init(object, ce);
745     object->handlers = handlers;
746     return object;
747 }
748 
sw_zend_create_object_deny(zend_class_entry * ce)749 static sw_inline zend_object *sw_zend_create_object_deny(zend_class_entry *ce) {
750     zend_object *object;
751     object = zend_objects_new(ce);
752     /* Initialize default properties */
753     if (EXPECTED(ce->default_properties_count != 0)) {
754         zval *p = object->properties_table;
755         zval *end = p + ce->default_properties_count;
756         do {
757             ZVAL_UNDEF(p);
758             p++;
759         } while (p != end);
760     }
761     zend_throw_error(NULL, "The object of %s can not be created for security reasons", ZSTR_VAL(ce->name));
762     return object;
763 }
764 
765 #if PHP_VERSION_ID < 80000
sw_zend_class_unset_property_deny(zval * zobject,zval * zmember,void ** cache_slot)766 static sw_inline void sw_zend_class_unset_property_deny(zval *zobject, zval *zmember, void **cache_slot) {
767     zend_class_entry *ce = Z_OBJCE_P(zobject);
768     while (ce->parent) {
769         ce = ce->parent;
770     }
771     SW_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
772     if (EXPECTED(zend_hash_find(&ce->properties_info, Z_STR_P(zmember)))) {
773         zend_throw_error(
774             NULL, "Property %s of class %s cannot be unset", Z_STRVAL_P(zmember), SW_Z_OBJCE_NAME_VAL_P(zobject));
775         return;
776     }
777     std_object_handlers.unset_property(zobject, zmember, cache_slot);
778 }
779 #else
sw_zend_class_unset_property_deny(zend_object * object,zend_string * member,void ** cache_slot)780 static sw_inline void sw_zend_class_unset_property_deny(zend_object *object, zend_string *member, void **cache_slot) {
781     zend_class_entry *ce = object->ce;
782     while (ce->parent) {
783         ce = ce->parent;
784     }
785     SW_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
786     if (EXPECTED(zend_hash_find(&ce->properties_info, member))) {
787         zend_throw_error(NULL, "Property %s of class %s cannot be unset", ZSTR_VAL(member), ZSTR_VAL(object->ce->name));
788         return;
789     }
790     std_object_handlers.unset_property(object, member, cache_slot);
791 }
792 #endif
793 
sw_zend_read_property(zend_class_entry * ce,zval * obj,const char * s,int len,int silent)794 static sw_inline zval *sw_zend_read_property(zend_class_entry *ce, zval *obj, const char *s, int len, int silent) {
795     zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
796     if (UNEXPECTED(property == &EG(uninitialized_zval))) {
797         zend_update_property_null(ce, SW_Z8_OBJ_P(obj), s, len);
798         return zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
799     }
800     return property;
801 }
802 
sw_zend_update_property_null_ex(zend_class_entry * scope,zval * object,zend_string * s)803 static sw_inline void sw_zend_update_property_null_ex(zend_class_entry *scope, zval *object, zend_string *s) {
804     zval tmp;
805 
806     ZVAL_NULL(&tmp);
807     zend_update_property_ex(scope, SW_Z8_OBJ_P(object), s, &tmp);
808 }
809 
sw_zend_read_property_ex(zend_class_entry * ce,zval * obj,zend_string * s,int silent)810 static sw_inline zval *sw_zend_read_property_ex(zend_class_entry *ce, zval *obj, zend_string *s, int silent) {
811     zval rv, *property = zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
812     if (UNEXPECTED(property == &EG(uninitialized_zval))) {
813         sw_zend_update_property_null_ex(ce, obj, s);
814         return zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
815     }
816     return property;
817 }
818 
sw_zend_read_property_not_null(zend_class_entry * ce,zval * obj,const char * s,int len,int silent)819 static sw_inline zval *sw_zend_read_property_not_null(
820     zend_class_entry *ce, zval *obj, const char *s, int len, int silent) {
821     zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
822     zend_uchar type = Z_TYPE_P(property);
823     return (type == IS_NULL || UNEXPECTED(type == IS_UNDEF)) ? NULL : property;
824 }
825 
sw_zend_read_property_not_null_ex(zend_class_entry * ce,zval * obj,zend_string * s,int silent)826 static sw_inline zval *sw_zend_read_property_not_null_ex(zend_class_entry *ce, zval *obj, zend_string *s, int silent) {
827     zval rv, *property = zend_read_property_ex(ce, SW_Z8_OBJ_P(obj), s, silent, &rv);
828     zend_uchar type = Z_TYPE_P(property);
829     return (type == IS_NULL || UNEXPECTED(type == IS_UNDEF)) ? NULL : property;
830 }
831 
sw_zend_update_and_read_property_array(zend_class_entry * ce,zval * obj,const char * s,int len)832 static sw_inline zval *sw_zend_update_and_read_property_array(zend_class_entry *ce, zval *obj, const char *s, int len) {
833     zval ztmp;
834     array_init(&ztmp);
835     zend_update_property(ce, SW_Z8_OBJ_P(obj), s, len, &ztmp);
836     zval_ptr_dtor(&ztmp);
837     return zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, 1, &ztmp);
838 }
839 
sw_zend_read_and_convert_property_array(zend_class_entry * ce,zval * obj,const char * s,int len,int silent)840 static sw_inline zval *sw_zend_read_and_convert_property_array(
841     zend_class_entry *ce, zval *obj, const char *s, int len, int silent) {
842     zval rv, *property = zend_read_property(ce, SW_Z8_OBJ_P(obj), s, len, silent, &rv);
843     if (Z_TYPE_P(property) != IS_ARRAY) {
844         // NOTICE: if user unset the property, zend_read_property will return uninitialized_zval instead of NULL pointer
845         if (UNEXPECTED(property == &EG(uninitialized_zval))) {
846             property = sw_zend_update_and_read_property_array(ce, obj, s, len);
847         } else {
848             zval_ptr_dtor(property);
849             array_init(property);
850         }
851     }
852 
853     return property;
854 }
855 
856 #define SW_RETURN_PROPERTY(name)                                                                                       \
857     do {                                                                                                               \
858         RETURN_ZVAL(sw_zend_read_property(Z_OBJCE_P(ZEND_THIS), ZEND_THIS, ZEND_STRL(name), 0), 1, 0);                 \
859     } while (0)
860 
861 #define RETURN_SW_STRING(buf)                                                                                          \
862     do {                                                                                                               \
863         RETURN_STRINGL(buf->str, buf->length);                                                                         \
864     } while (0)
865 
866 //----------------------------------Function API------------------------------------
867 
868 /**
869  * Notice (sw_zend_call_method_with_%u_params): If you don't want to check the return value, please set retval to NULL
870  */
871 #define sw_zend_call_method_with_0_params(zobj, obj_ce, fn_ptr_ptr, fn_name, retval)                                   \
872     zend_call_method_with_0_params(SW_Z8_OBJ_P(zobj), obj_ce, fn_ptr_ptr, fn_name, retval)
873 
874 #define sw_zend_call_method_with_1_params(zobj, obj_ce, fn_ptr_ptr, fn_name, retval, v1)                               \
875     zend_call_method_with_1_params(SW_Z8_OBJ_P(zobj), obj_ce, fn_ptr_ptr, fn_name, retval, v1)
876 
877 #define sw_zend_call_method_with_2_params(zobj, obj_ce, fn_ptr_ptr, fn_name, retval, v1, v2)                           \
878     zend_call_method_with_2_params(SW_Z8_OBJ_P(zobj), obj_ce, fn_ptr_ptr, fn_name, retval, v1, v2)
879 
sw_zend_function_max_num_args(zend_function * function)880 static sw_inline int sw_zend_function_max_num_args(zend_function *function) {
881     // https://github.com/php/php-src/commit/2646f7bcb98dcdd322ea21701c8bb101104ea619
882     // zend_function.common.num_args don't include the variadic argument anymore.
883     return (function->common.fn_flags & ZEND_ACC_VARIADIC) ? UINT32_MAX : function->common.num_args;
884 }
885 
886 // TODO: remove it after remove async modules
sw_zend_is_callable(zval * callable,int check_flags,char ** callable_name)887 static sw_inline zend_bool sw_zend_is_callable(zval *callable, int check_flags, char **callable_name) {
888     zend_string *name;
889     zend_bool ret = zend_is_callable(callable, check_flags, &name);
890     *callable_name = estrndup(ZSTR_VAL(name), ZSTR_LEN(name));
891     zend_string_release(name);
892     return ret;
893 }
894 
sw_zend_is_callable_at_frame(zval * zcallable,zval * zobject,zend_execute_data * frame,uint check_flags,char ** callable_name,size_t * callable_name_len,zend_fcall_info_cache * fci_cache,char ** error)895 static sw_inline zend_bool sw_zend_is_callable_at_frame(zval *zcallable,
896                                                         zval *zobject,
897                                                         zend_execute_data *frame,
898                                                         uint check_flags,
899                                                         char **callable_name,
900                                                         size_t *callable_name_len,
901                                                         zend_fcall_info_cache *fci_cache,
902                                                         char **error) {
903     zend_string *name;
904     zend_bool ret;
905 #if PHP_VERSION_ID < 80001
906     ret = zend_is_callable_ex(zcallable, zobject ? Z_OBJ_P(zobject) : NULL, check_flags, &name, fci_cache, error);
907 #else
908     ret = zend_is_callable_at_frame(zcallable, zobject ? Z_OBJ_P(zobject) : NULL, frame, check_flags, fci_cache, error);
909     name = zend_get_callable_name_ex(zcallable, zobject ? Z_OBJ_P(zobject) : NULL);
910 #endif
911     if (callable_name) {
912         *callable_name = estrndup(ZSTR_VAL(name), ZSTR_LEN(name));
913     }
914     if (callable_name_len) {
915         *callable_name_len = ZSTR_LEN(name);
916     }
917     zend_string_release(name);
918     return ret;
919 }
920 
sw_zend_is_callable_ex(zval * zcallable,zval * zobject,uint check_flags,char ** callable_name,size_t * callable_name_len,zend_fcall_info_cache * fci_cache,char ** error)921 static sw_inline zend_bool sw_zend_is_callable_ex(zval *zcallable,
922                                                   zval *zobject,
923                                                   uint check_flags,
924                                                   char **callable_name,
925                                                   size_t *callable_name_len,
926                                                   zend_fcall_info_cache *fci_cache,
927                                                   char **error) {
928     return sw_zend_is_callable_at_frame(
929         zcallable, zobject, NULL, check_flags, callable_name, callable_name_len, fci_cache, error);
930 }
931 
932 /* this API can work well when retval is NULL */
sw_zend_call_function_ex(zval * function_name,zend_fcall_info_cache * fci_cache,uint32_t param_count,zval * params,zval * retval)933 static sw_inline int sw_zend_call_function_ex(
934     zval *function_name, zend_fcall_info_cache *fci_cache, uint32_t param_count, zval *params, zval *retval) {
935     zend_fcall_info fci;
936     zval _retval;
937     int ret;
938 
939     fci.size = sizeof(fci);
940     fci.object = NULL;
941     if (!fci_cache || !fci_cache->function_handler) {
942         if (!function_name) {
943             php_swoole_fatal_error(E_WARNING, "Bad function");
944             return FAILURE;
945         }
946         ZVAL_COPY_VALUE(&fci.function_name, function_name);
947     } else {
948         ZVAL_UNDEF(&fci.function_name);
949     }
950     fci.retval = retval ? retval : &_retval;
951     fci.param_count = param_count;
952     fci.params = params;
953 #if PHP_VERSION_ID >= 80000
954     fci.named_params = NULL;
955 #else
956     fci.no_separation = 0;
957 #endif
958 
959     ret = zend_call_function(&fci, fci_cache);
960 
961     if (!retval) {
962         zval_ptr_dtor(&_retval);
963     }
964     return ret;
965 }
966 
967 /* we must check for exception immediately if we don't have chances to go back to ZendVM (e.g event loop) */
sw_zend_call_function_ex2(zval * function_name,zend_fcall_info_cache * fci_cache,uint32_t param_count,zval * params,zval * retval)968 static sw_inline int sw_zend_call_function_ex2(
969     zval *function_name, zend_fcall_info_cache *fci_cache, uint32_t param_count, zval *params, zval *retval) {
970     int ret = sw_zend_call_function_ex(function_name, fci_cache, param_count, params, retval);
971     if (UNEXPECTED(EG(exception))) {
972         zend_exception_error(EG(exception), E_ERROR);
973     }
974     return ret;
975 }
976 
sw_zend_call_function_anyway(zend_fcall_info * fci,zend_fcall_info_cache * fci_cache)977 static sw_inline int sw_zend_call_function_anyway(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) {
978     zval retval;
979     zend_object *exception = EG(exception);
980     if (exception) {
981         EG(exception) = NULL;
982     }
983     if (!fci->retval) {
984         fci->retval = &retval;
985     }
986     int ret = zend_call_function(fci, fci_cache);
987     if (fci->retval == &retval) {
988         zval_ptr_dtor(&retval);
989     }
990     if (exception) {
991         EG(exception) = exception;
992     }
993     return ret;
994 }
995 
sw_zend_fci_params_persist(zend_fcall_info * fci)996 static sw_inline void sw_zend_fci_params_persist(zend_fcall_info *fci) {
997     if (fci->param_count > 0) {
998         uint32_t i;
999         zval *params = (zval *) ecalloc(fci->param_count, sizeof(zval));
1000         for (i = 0; i < fci->param_count; i++) {
1001             ZVAL_COPY(&params[i], &fci->params[i]);
1002         }
1003         fci->params = params;
1004     }
1005 }
1006 
sw_zend_fci_params_discard(zend_fcall_info * fci)1007 static sw_inline void sw_zend_fci_params_discard(zend_fcall_info *fci) {
1008     if (fci->param_count > 0) {
1009         uint32_t i;
1010         for (i = 0; i < fci->param_count; i++) {
1011             zval_ptr_dtor(&fci->params[i]);
1012         }
1013         efree(fci->params);
1014     }
1015 }
1016 
sw_zend_fci_cache_persist(zend_fcall_info_cache * fci_cache)1017 static sw_inline void sw_zend_fci_cache_persist(zend_fcall_info_cache *fci_cache) {
1018     if (fci_cache->object) {
1019         GC_ADDREF(fci_cache->object);
1020     }
1021     if (fci_cache->function_handler->op_array.fn_flags & ZEND_ACC_CLOSURE) {
1022         GC_ADDREF(ZEND_CLOSURE_OBJECT(fci_cache->function_handler));
1023     }
1024 }
1025 
sw_zend_fci_cache_discard(zend_fcall_info_cache * fci_cache)1026 static sw_inline void sw_zend_fci_cache_discard(zend_fcall_info_cache *fci_cache) {
1027     if (fci_cache->object) {
1028         OBJ_RELEASE(fci_cache->object);
1029     }
1030     if (fci_cache->function_handler->op_array.fn_flags & ZEND_ACC_CLOSURE) {
1031         OBJ_RELEASE(ZEND_CLOSURE_OBJECT(fci_cache->function_handler));
1032     }
1033 }
1034 
1035 /* use void* to match some C callback function pointers */
sw_zend_fci_cache_free(void * fci_cache)1036 static sw_inline void sw_zend_fci_cache_free(void *fci_cache) {
1037     sw_zend_fci_cache_discard((zend_fcall_info_cache *) fci_cache);
1038     efree((zend_fcall_info_cache *) fci_cache);
1039 }
1040 
1041 //----------------------------------Misc API------------------------------------
1042 
php_swoole_check_reactor()1043 static sw_inline int php_swoole_check_reactor() {
1044     if (SWOOLE_G(req_status) == PHP_SWOOLE_RSHUTDOWN_BEGIN) {
1045         return -1;
1046     }
1047     if (sw_unlikely(!sw_reactor())) {
1048         return php_swoole_reactor_init() == SW_OK ? 1 : -1;
1049     } else {
1050         return 0;
1051     }
1052 }
1053 
php_swoole_format_date(char * format,size_t format_len,time_t ts,int localtime)1054 static sw_inline char *php_swoole_format_date(char *format, size_t format_len, time_t ts, int localtime) {
1055     zend_string *time = php_format_date(format, format_len, ts, localtime);
1056     char *return_str = estrndup(ZSTR_VAL(time), ZSTR_LEN(time));
1057     zend_string_release(time);
1058     return return_str;
1059 }
1060 
php_swoole_url_encode(const char * value,size_t value_len,int * exten)1061 static sw_inline char *php_swoole_url_encode(const char *value, size_t value_len, int *exten) {
1062     zend_string *str = php_url_encode(value, value_len);
1063     *exten = ZSTR_LEN(str);
1064     char *return_str = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
1065     zend_string_release(str);
1066     return return_str;
1067 }
1068 
php_swoole_http_build_query(zval * zdata,size_t * length,smart_str * formstr)1069 static sw_inline char *php_swoole_http_build_query(zval *zdata, size_t *length, smart_str *formstr) {
1070 #if PHP_VERSION_ID < 80000
1071     if (php_url_encode_hash_ex(
1072             HASH_OF(zdata), formstr, NULL, 0, NULL, 0, NULL, 0, NULL, NULL, (int) PHP_QUERY_RFC1738) == FAILURE) {
1073 #else
1074     if (HASH_OF(zdata)) {
1075         php_url_encode_hash_ex(HASH_OF(zdata), formstr, NULL, 0, NULL, 0, NULL, 0, NULL, NULL, (int) PHP_QUERY_RFC1738);
1076     } else {
1077 #endif
1078         if (formstr->s) {
1079             smart_str_free(formstr);
1080         }
1081         return NULL;
1082     }
1083     if (!formstr->s) {
1084         return NULL;
1085     }
1086     smart_str_0(formstr);
1087     *length = formstr->s->len;
1088     return formstr->s->val;
1089 }
1090 
1091 END_EXTERN_C()
1092 
1093 #endif /* PHP_SWOOLE_PRIVATE_H */
1094