PageRenderTime 71ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/zend_API.c

http://github.com/php/php-src
C | 4372 lines | 3516 code | 582 blank | 274 comment | 900 complexity | 7b0abf5c34e929660435e49cc03972e1 MD5 | raw file
Possible License(s): BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. | Andrei Zmievski <andrei@php.net> |
  18. | Dmitry Stogov <dmitry@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "zend.h"
  22. #include "zend_execute.h"
  23. #include "zend_API.h"
  24. #include "zend_modules.h"
  25. #include "zend_extensions.h"
  26. #include "zend_constants.h"
  27. #include "zend_interfaces.h"
  28. #include "zend_exceptions.h"
  29. #include "zend_closures.h"
  30. #include "zend_inheritance.h"
  31. #include "zend_ini.h"
  32. #include <stdarg.h>
  33. /* these variables are true statics/globals, and have to be mutex'ed on every access */
  34. ZEND_API HashTable module_registry;
  35. static zend_module_entry **module_request_startup_handlers;
  36. static zend_module_entry **module_request_shutdown_handlers;
  37. static zend_module_entry **module_post_deactivate_handlers;
  38. static zend_class_entry **class_cleanup_handlers;
  39. ZEND_API int _zend_get_parameters_array_ex(int param_count, zval *argument_array) /* {{{ */
  40. {
  41. zval *param_ptr;
  42. int arg_count;
  43. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  44. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  45. if (param_count>arg_count) {
  46. return FAILURE;
  47. }
  48. while (param_count-->0) {
  49. ZVAL_COPY_VALUE(argument_array, param_ptr);
  50. argument_array++;
  51. param_ptr++;
  52. }
  53. return SUCCESS;
  54. }
  55. /* }}} */
  56. ZEND_API int zend_copy_parameters_array(int param_count, zval *argument_array) /* {{{ */
  57. {
  58. zval *param_ptr;
  59. int arg_count;
  60. param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
  61. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  62. if (param_count>arg_count) {
  63. return FAILURE;
  64. }
  65. while (param_count-->0) {
  66. Z_TRY_ADDREF_P(param_ptr);
  67. zend_hash_next_index_insert_new(Z_ARRVAL_P(argument_array), param_ptr);
  68. param_ptr++;
  69. }
  70. return SUCCESS;
  71. }
  72. /* }}} */
  73. ZEND_API ZEND_COLD void zend_wrong_param_count(void) /* {{{ */
  74. {
  75. const char *space;
  76. const char *class_name = get_active_class_name(&space);
  77. zend_argument_count_error("Wrong parameter count for %s%s%s()", class_name, space, get_active_function_name());
  78. }
  79. /* }}} */
  80. /* Argument parsing API -- andrei */
  81. ZEND_API const char *zend_get_type_by_const(int type) /* {{{ */
  82. {
  83. switch(type) {
  84. case IS_FALSE:
  85. case IS_TRUE:
  86. case _IS_BOOL:
  87. return "bool";
  88. case IS_LONG:
  89. return "int";
  90. case IS_DOUBLE:
  91. return "float";
  92. case IS_STRING:
  93. return "string";
  94. case IS_OBJECT:
  95. return "object";
  96. case IS_RESOURCE:
  97. return "resource";
  98. case IS_NULL:
  99. return "null";
  100. case IS_CALLABLE:
  101. return "callable";
  102. case IS_ITERABLE:
  103. return "iterable";
  104. case IS_ARRAY:
  105. return "array";
  106. case IS_VOID:
  107. return "void";
  108. case _IS_NUMBER:
  109. return "number";
  110. default:
  111. return "unknown";
  112. }
  113. }
  114. /* }}} */
  115. ZEND_API const char *zend_zval_type_name(const zval *arg) /* {{{ */
  116. {
  117. ZVAL_DEREF(arg);
  118. return zend_get_type_by_const(Z_TYPE_P(arg));
  119. }
  120. /* }}} */
  121. ZEND_API zend_string *zend_zval_get_type(const zval *arg) /* {{{ */
  122. {
  123. switch (Z_TYPE_P(arg)) {
  124. case IS_NULL:
  125. return ZSTR_KNOWN(ZEND_STR_NULL);
  126. case IS_FALSE:
  127. case IS_TRUE:
  128. return ZSTR_KNOWN(ZEND_STR_BOOLEAN);
  129. case IS_LONG:
  130. return ZSTR_KNOWN(ZEND_STR_INTEGER);
  131. case IS_DOUBLE:
  132. return ZSTR_KNOWN(ZEND_STR_DOUBLE);
  133. case IS_STRING:
  134. return ZSTR_KNOWN(ZEND_STR_STRING);
  135. case IS_ARRAY:
  136. return ZSTR_KNOWN(ZEND_STR_ARRAY);
  137. case IS_OBJECT:
  138. return ZSTR_KNOWN(ZEND_STR_OBJECT);
  139. case IS_RESOURCE:
  140. if (zend_rsrc_list_get_rsrc_type(Z_RES_P(arg))) {
  141. return ZSTR_KNOWN(ZEND_STR_RESOURCE);
  142. } else {
  143. return ZSTR_KNOWN(ZEND_STR_CLOSED_RESOURCE);
  144. }
  145. default:
  146. return NULL;
  147. }
  148. }
  149. /* }}} */
  150. ZEND_API ZEND_COLD int ZEND_FASTCALL zend_wrong_parameters_none_error(void) /* {{{ */
  151. {
  152. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  153. zend_function *active_function = EG(current_execute_data)->func;
  154. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  155. zend_argument_count_error(
  156. "%s%s%s() expects %s %d parameter%s, %d given",
  157. class_name, \
  158. class_name[0] ? "::" : "", \
  159. ZSTR_VAL(active_function->common.function_name),
  160. "exactly",
  161. 0,
  162. "s",
  163. num_args);
  164. return FAILURE;
  165. }
  166. /* }}} */
  167. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameters_count_error(int min_num_args, int max_num_args) /* {{{ */
  168. {
  169. int num_args = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  170. zend_function *active_function = EG(current_execute_data)->func;
  171. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  172. zend_argument_count_error(
  173. "%s%s%s() expects %s %d parameter%s, %d given",
  174. class_name, \
  175. class_name[0] ? "::" : "", \
  176. ZSTR_VAL(active_function->common.function_name),
  177. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  178. num_args < min_num_args ? min_num_args : max_num_args,
  179. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  180. num_args);
  181. }
  182. /* }}} */
  183. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_type_error(int num, zend_expected_type expected_type, zval *arg) /* {{{ */
  184. {
  185. static const char * const expected_error[] = {
  186. Z_EXPECTED_TYPES(Z_EXPECTED_TYPE_STR)
  187. NULL
  188. };
  189. if (EG(exception)) {
  190. return;
  191. }
  192. zend_argument_type_error(num, "must be %s, %s given", expected_error[expected_type], zend_zval_type_name(arg));
  193. }
  194. /* }}} */
  195. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_error(int num, const char *name, zval *arg) /* {{{ */
  196. {
  197. if (EG(exception)) {
  198. return;
  199. }
  200. zend_argument_type_error(num, "must be of type %s, %s given", name, zend_zval_type_name(arg));
  201. }
  202. /* }}} */
  203. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_parameter_class_or_null_error(int num, const char *name, zval *arg) /* {{{ */
  204. {
  205. if (EG(exception)) {
  206. return;
  207. }
  208. zend_argument_type_error(num, "must be of type ?%s, %s given", name, zend_zval_type_name(arg));
  209. }
  210. /* }}} */
  211. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_wrong_callback_error(int num, char *error) /* {{{ */
  212. {
  213. if (EG(exception)) {
  214. return;
  215. }
  216. zend_argument_type_error(num, "must be a valid callback, %s", error);
  217. efree(error);
  218. }
  219. /* }}} */
  220. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_entry *error_ce, uint32_t arg_num, const char *format, va_list va) /* {{{ */
  221. {
  222. const char *space;
  223. const char *class_name;
  224. const char *arg_name;
  225. char *message = NULL;
  226. if (EG(exception)) {
  227. return;
  228. }
  229. class_name = get_active_class_name(&space);
  230. arg_name = get_active_function_arg_name(arg_num);
  231. zend_vspprintf(&message, 0, format, va);
  232. zend_throw_error(error_ce, "%s%s%s(): Argument #%d%s%s%s %s",
  233. class_name, space, get_active_function_name(), arg_num,
  234. arg_name ? " ($" : "", arg_name ? arg_name : "", arg_name ? ")" : "", message
  235. );
  236. efree(message);
  237. }
  238. /* }}} */
  239. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
  240. {
  241. va_list va;
  242. va_start(va, format);
  243. zend_argument_error_variadic(error_ce, arg_num, format, va);
  244. va_end(va);
  245. }
  246. /* }}} */
  247. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_type_error(uint32_t arg_num, const char *format, ...) /* {{{ */
  248. {
  249. va_list va;
  250. va_start(va, format);
  251. zend_argument_error_variadic(zend_ce_type_error, arg_num, format, va);
  252. va_end(va);
  253. }
  254. /* }}} */
  255. ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_value_error(uint32_t arg_num, const char *format, ...) /* {{{ */
  256. {
  257. va_list va;
  258. va_start(va, format);
  259. zend_argument_error_variadic(zend_ce_value_error, arg_num, format, va);
  260. va_end(va);
  261. }
  262. /* }}} */
  263. ZEND_API int ZEND_FASTCALL zend_parse_arg_class(zval *arg, zend_class_entry **pce, int num, int check_null) /* {{{ */
  264. {
  265. zend_class_entry *ce_base = *pce;
  266. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  267. *pce = NULL;
  268. return 1;
  269. }
  270. if (!try_convert_to_string(arg)) {
  271. *pce = NULL;
  272. return 0;
  273. }
  274. *pce = zend_lookup_class(Z_STR_P(arg));
  275. if (ce_base) {
  276. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  277. zend_argument_type_error(num, "must be a class name derived from %s, '%s' given", ZSTR_VAL(ce_base->name), Z_STRVAL_P(arg));
  278. *pce = NULL;
  279. return 0;
  280. }
  281. }
  282. if (!*pce) {
  283. zend_argument_type_error(num, "must be a valid class name, '%s' given", Z_STRVAL_P(arg));
  284. return 0;
  285. }
  286. return 1;
  287. }
  288. /* }}} */
  289. ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_weak(zval *arg, zend_bool *dest) /* {{{ */
  290. {
  291. if (EXPECTED(Z_TYPE_P(arg) <= IS_STRING)) {
  292. *dest = zend_is_true(arg);
  293. } else {
  294. return 0;
  295. }
  296. return 1;
  297. }
  298. /* }}} */
  299. ZEND_API int ZEND_FASTCALL zend_parse_arg_bool_slow(zval *arg, zend_bool *dest) /* {{{ */
  300. {
  301. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  302. return 0;
  303. }
  304. return zend_parse_arg_bool_weak(arg, dest);
  305. }
  306. /* }}} */
  307. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_weak(zval *arg, zend_long *dest) /* {{{ */
  308. {
  309. if (EXPECTED(Z_TYPE_P(arg) == IS_DOUBLE)) {
  310. if (UNEXPECTED(zend_isnan(Z_DVAL_P(arg)))) {
  311. return 0;
  312. }
  313. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(Z_DVAL_P(arg)))) {
  314. return 0;
  315. } else {
  316. *dest = zend_dval_to_lval(Z_DVAL_P(arg));
  317. }
  318. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  319. double d;
  320. int type;
  321. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), dest, &d)) != IS_LONG)) {
  322. if (EXPECTED(type != 0)) {
  323. if (UNEXPECTED(zend_isnan(d))) {
  324. return 0;
  325. }
  326. if (UNEXPECTED(!ZEND_DOUBLE_FITS_LONG(d))) {
  327. return 0;
  328. } else {
  329. *dest = zend_dval_to_lval(d);
  330. }
  331. } else {
  332. return 0;
  333. }
  334. }
  335. if (UNEXPECTED(EG(exception))) {
  336. return 0;
  337. }
  338. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  339. *dest = 0;
  340. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  341. *dest = 1;
  342. } else {
  343. return 0;
  344. }
  345. return 1;
  346. }
  347. /* }}} */
  348. ZEND_API int ZEND_FASTCALL zend_parse_arg_long_slow(zval *arg, zend_long *dest) /* {{{ */
  349. {
  350. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  351. return 0;
  352. }
  353. return zend_parse_arg_long_weak(arg, dest);
  354. }
  355. /* }}} */
  356. ZEND_API int ZEND_FASTCALL zend_parse_arg_double_weak(zval *arg, double *dest) /* {{{ */
  357. {
  358. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  359. *dest = (double)Z_LVAL_P(arg);
  360. } else if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
  361. zend_long l;
  362. int type;
  363. if (UNEXPECTED((type = is_numeric_str_function(Z_STR_P(arg), &l, dest)) != IS_DOUBLE)) {
  364. if (EXPECTED(type != 0)) {
  365. *dest = (double)(l);
  366. } else {
  367. return 0;
  368. }
  369. }
  370. if (UNEXPECTED(EG(exception))) {
  371. return 0;
  372. }
  373. } else if (EXPECTED(Z_TYPE_P(arg) < IS_TRUE)) {
  374. *dest = 0.0;
  375. } else if (EXPECTED(Z_TYPE_P(arg) == IS_TRUE)) {
  376. *dest = 1.0;
  377. } else {
  378. return 0;
  379. }
  380. return 1;
  381. }
  382. /* }}} */
  383. ZEND_API int ZEND_FASTCALL zend_parse_arg_double_slow(zval *arg, double *dest) /* {{{ */
  384. {
  385. if (EXPECTED(Z_TYPE_P(arg) == IS_LONG)) {
  386. /* SSTH Exception: IS_LONG may be accepted instead as IS_DOUBLE */
  387. *dest = (double)Z_LVAL_P(arg);
  388. } else if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  389. return 0;
  390. }
  391. return zend_parse_arg_double_weak(arg, dest);
  392. }
  393. /* }}} */
  394. ZEND_API int ZEND_FASTCALL zend_parse_arg_number_slow(zval *arg, zval **dest) /* {{{ */
  395. {
  396. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  397. return 0;
  398. }
  399. if (Z_TYPE_P(arg) == IS_STRING) {
  400. zend_string *str = Z_STR_P(arg);
  401. zend_long lval;
  402. double dval;
  403. zend_uchar type = is_numeric_string(ZSTR_VAL(str), ZSTR_LEN(str), &lval, &dval, -1);
  404. if (type == IS_LONG) {
  405. ZVAL_LONG(arg, lval);
  406. } else if (type == IS_DOUBLE) {
  407. ZVAL_DOUBLE(arg, dval);
  408. } else {
  409. return 0;
  410. }
  411. zend_string_release(str);
  412. } else if (Z_TYPE_P(arg) < IS_TRUE) {
  413. ZVAL_LONG(arg, 0);
  414. } else if (Z_TYPE_P(arg) == IS_TRUE) {
  415. ZVAL_LONG(arg, 1);
  416. } else {
  417. return 0;
  418. }
  419. *dest = arg;
  420. return 1;
  421. }
  422. /* }}} */
  423. ZEND_API int ZEND_FASTCALL zend_parse_arg_str_weak(zval *arg, zend_string **dest) /* {{{ */
  424. {
  425. if (EXPECTED(Z_TYPE_P(arg) < IS_STRING)) {
  426. convert_to_string(arg);
  427. *dest = Z_STR_P(arg);
  428. } else if (UNEXPECTED(Z_TYPE_P(arg) == IS_OBJECT)) {
  429. zend_object *zobj = Z_OBJ_P(arg);
  430. zval obj;
  431. if (zobj->handlers->cast_object(zobj, &obj, IS_STRING) == SUCCESS) {
  432. OBJ_RELEASE(zobj);
  433. ZVAL_COPY_VALUE(arg, &obj);
  434. *dest = Z_STR_P(arg);
  435. return 1;
  436. }
  437. return 0;
  438. } else {
  439. return 0;
  440. }
  441. return 1;
  442. }
  443. /* }}} */
  444. ZEND_API int ZEND_FASTCALL zend_parse_arg_str_slow(zval *arg, zend_string **dest) /* {{{ */
  445. {
  446. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  447. return 0;
  448. }
  449. return zend_parse_arg_str_weak(arg, dest);
  450. }
  451. /* }}} */
  452. ZEND_API int ZEND_FASTCALL zend_parse_arg_str_or_long_slow(zval *arg, zend_string **dest_str, zend_long *dest_long) /* {{{ */
  453. {
  454. if (UNEXPECTED(ZEND_ARG_USES_STRICT_TYPES())) {
  455. return 0;
  456. }
  457. if (zend_parse_arg_long_weak(arg, dest_long)) {
  458. *dest_str = NULL;
  459. return 1;
  460. } else if (zend_parse_arg_str_weak(arg, dest_str)) {
  461. *dest_long = 0;
  462. return 1;
  463. } else {
  464. return 0;
  465. }
  466. }
  467. /* }}} */
  468. static const char *zend_parse_arg_impl(int arg_num, zval *arg, va_list *va, const char **spec, char **error) /* {{{ */
  469. {
  470. const char *spec_walk = *spec;
  471. char c = *spec_walk++;
  472. int check_null = 0;
  473. int separate = 0;
  474. zval *real_arg = arg;
  475. /* scan through modifiers */
  476. ZVAL_DEREF(arg);
  477. while (1) {
  478. if (*spec_walk == '/') {
  479. SEPARATE_ZVAL_NOREF(arg);
  480. real_arg = arg;
  481. separate = 1;
  482. } else if (*spec_walk == '!') {
  483. check_null = 1;
  484. } else {
  485. break;
  486. }
  487. spec_walk++;
  488. }
  489. switch (c) {
  490. case 'l':
  491. {
  492. zend_long *p = va_arg(*va, zend_long *);
  493. zend_bool *is_null = NULL;
  494. if (check_null) {
  495. is_null = va_arg(*va, zend_bool *);
  496. }
  497. if (!zend_parse_arg_long(arg, p, is_null, check_null)) {
  498. return check_null ? "?int" : "int";
  499. }
  500. }
  501. break;
  502. case 'd':
  503. {
  504. double *p = va_arg(*va, double *);
  505. zend_bool *is_null = NULL;
  506. if (check_null) {
  507. is_null = va_arg(*va, zend_bool *);
  508. }
  509. if (!zend_parse_arg_double(arg, p, is_null, check_null)) {
  510. return check_null ? "?float" : "float";
  511. }
  512. }
  513. break;
  514. case 'n':
  515. {
  516. zval **p = va_arg(*va, zval **);
  517. if (!zend_parse_arg_number(arg, p, check_null)) {
  518. return check_null ? "int|float|null" : "int|float";
  519. }
  520. }
  521. break;
  522. case 's':
  523. {
  524. char **p = va_arg(*va, char **);
  525. size_t *pl = va_arg(*va, size_t *);
  526. if (!zend_parse_arg_string(arg, p, pl, check_null)) {
  527. return check_null ? "?string" : "string";
  528. }
  529. }
  530. break;
  531. case 'p':
  532. {
  533. char **p = va_arg(*va, char **);
  534. size_t *pl = va_arg(*va, size_t *);
  535. if (!zend_parse_arg_path(arg, p, pl, check_null)) {
  536. zend_spprintf(error, 0, "a valid path%s, %s given",
  537. check_null ? " or null" : "", zend_zval_type_name(arg)
  538. );
  539. return "";
  540. }
  541. }
  542. break;
  543. case 'P':
  544. {
  545. zend_string **str = va_arg(*va, zend_string **);
  546. if (!zend_parse_arg_path_str(arg, str, check_null)) {
  547. zend_spprintf(error, 0, "a valid path%s, %s given",
  548. check_null ? " or null" : "", zend_zval_type_name(arg)
  549. );
  550. return "";
  551. }
  552. }
  553. break;
  554. case 'S':
  555. {
  556. zend_string **str = va_arg(*va, zend_string **);
  557. if (!zend_parse_arg_str(arg, str, check_null)) {
  558. return check_null ? "?string" : "string";
  559. }
  560. }
  561. break;
  562. case 'b':
  563. {
  564. zend_bool *p = va_arg(*va, zend_bool *);
  565. zend_bool *is_null = NULL;
  566. if (check_null) {
  567. is_null = va_arg(*va, zend_bool *);
  568. }
  569. if (!zend_parse_arg_bool(arg, p, is_null, check_null)) {
  570. return check_null ? "?bool" : "bool";
  571. }
  572. }
  573. break;
  574. case 'r':
  575. {
  576. zval **p = va_arg(*va, zval **);
  577. if (!zend_parse_arg_resource(arg, p, check_null)) {
  578. return check_null ? "resource or null" : "resource";
  579. }
  580. }
  581. break;
  582. case 'A':
  583. case 'a':
  584. {
  585. zval **p = va_arg(*va, zval **);
  586. if (!zend_parse_arg_array(arg, p, check_null, c == 'A')) {
  587. return check_null ? "?array" : "array";
  588. }
  589. }
  590. break;
  591. case 'H':
  592. case 'h':
  593. {
  594. HashTable **p = va_arg(*va, HashTable **);
  595. if (!zend_parse_arg_array_ht(arg, p, check_null, c == 'H', separate)) {
  596. return check_null ? "?array" : "array";
  597. }
  598. }
  599. break;
  600. case 'o':
  601. {
  602. zval **p = va_arg(*va, zval **);
  603. if (!zend_parse_arg_object(arg, p, NULL, check_null)) {
  604. return check_null ? "?object" : "object";
  605. }
  606. }
  607. break;
  608. case 'O':
  609. {
  610. zval **p = va_arg(*va, zval **);
  611. zend_class_entry *ce = va_arg(*va, zend_class_entry *);
  612. if (!zend_parse_arg_object(arg, p, ce, check_null)) {
  613. if (ce) {
  614. if (check_null) {
  615. zend_spprintf(error, 0, "of type ?%s, %s given", ZSTR_VAL(ce->name), zend_zval_type_name(arg));
  616. return "";
  617. } else {
  618. return ZSTR_VAL(ce->name);
  619. }
  620. } else {
  621. return check_null ? "?object" : "object";
  622. }
  623. }
  624. }
  625. break;
  626. case 'C':
  627. {
  628. zend_class_entry *lookup, **pce = va_arg(*va, zend_class_entry **);
  629. zend_class_entry *ce_base = *pce;
  630. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  631. *pce = NULL;
  632. break;
  633. }
  634. if (!try_convert_to_string(arg)) {
  635. *pce = NULL;
  636. return ""; /* try_convert_to_string() throws an exception */
  637. }
  638. if ((lookup = zend_lookup_class(Z_STR_P(arg))) == NULL) {
  639. *pce = NULL;
  640. } else {
  641. *pce = lookup;
  642. }
  643. if (ce_base) {
  644. if ((!*pce || !instanceof_function(*pce, ce_base))) {
  645. zend_spprintf(error, 0, "a class name derived from %s%s, '%s' given",
  646. ZSTR_VAL(ce_base->name), check_null ? " or null" : "", Z_STRVAL_P(arg));
  647. *pce = NULL;
  648. return "";
  649. }
  650. }
  651. if (!*pce) {
  652. zend_spprintf(error, 0, "a valid class name%s, '%s' given",
  653. check_null ? " or null" : "", Z_STRVAL_P(arg));
  654. return "";
  655. }
  656. break;
  657. }
  658. break;
  659. case 'f':
  660. {
  661. zend_fcall_info *fci = va_arg(*va, zend_fcall_info *);
  662. zend_fcall_info_cache *fcc = va_arg(*va, zend_fcall_info_cache *);
  663. char *is_callable_error = NULL;
  664. if (check_null && Z_TYPE_P(arg) == IS_NULL) {
  665. fci->size = 0;
  666. fcc->function_handler = 0;
  667. break;
  668. }
  669. if (zend_fcall_info_init(arg, 0, fci, fcc, NULL, &is_callable_error) == SUCCESS) {
  670. ZEND_ASSERT(!is_callable_error);
  671. break;
  672. }
  673. if (is_callable_error) {
  674. zend_spprintf(error, 0, "a valid callback%s, %s", check_null ? " or null" : "", is_callable_error);
  675. efree(is_callable_error);
  676. return "";
  677. } else {
  678. return check_null ? "a valid callback or null" : "a valid callback";
  679. }
  680. }
  681. case 'z':
  682. {
  683. zval **p = va_arg(*va, zval **);
  684. zend_parse_arg_zval_deref(real_arg, p, check_null);
  685. }
  686. break;
  687. case 'Z': /* replace with 'z' */
  688. case 'L': /* replace with 'l' */
  689. ZEND_ASSERT(0 && "ZPP modifier no longer supported");
  690. default:
  691. return "unknown";
  692. }
  693. *spec = spec_walk;
  694. return NULL;
  695. }
  696. /* }}} */
  697. static int zend_parse_arg(int arg_num, zval *arg, va_list *va, const char **spec, int flags) /* {{{ */
  698. {
  699. const char *expected_type = NULL;
  700. char *error = NULL;
  701. expected_type = zend_parse_arg_impl(arg_num, arg, va, spec, &error);
  702. if (expected_type) {
  703. if (EG(exception)) {
  704. return FAILURE;
  705. }
  706. if (!(flags & ZEND_PARSE_PARAMS_QUIET) && (*expected_type || error)) {
  707. if (error) {
  708. zend_argument_type_error(arg_num, "must be %s", error);
  709. efree(error);
  710. } else {
  711. zend_argument_type_error(arg_num, "must be of type %s, %s given", expected_type, zend_zval_type_name(arg));
  712. }
  713. } else if (error) {
  714. efree(error);
  715. }
  716. return FAILURE;
  717. }
  718. return SUCCESS;
  719. }
  720. /* }}} */
  721. ZEND_API int zend_parse_parameter(int flags, int arg_num, zval *arg, const char *spec, ...)
  722. {
  723. va_list va;
  724. int ret;
  725. va_start(va, spec);
  726. ret = zend_parse_arg(arg_num, arg, &va, &spec, flags);
  727. va_end(va);
  728. return ret;
  729. }
  730. static ZEND_COLD void zend_parse_parameters_debug_error(const char *msg) {
  731. zend_function *active_function = EG(current_execute_data)->func;
  732. const char *class_name = active_function->common.scope
  733. ? ZSTR_VAL(active_function->common.scope->name) : "";
  734. zend_error_noreturn(E_CORE_ERROR, "%s%s%s(): %s",
  735. class_name, class_name[0] ? "::" : "",
  736. ZSTR_VAL(active_function->common.function_name), msg);
  737. }
  738. static int zend_parse_va_args(int num_args, const char *type_spec, va_list *va, int flags) /* {{{ */
  739. {
  740. const char *spec_walk;
  741. int c, i;
  742. int min_num_args = -1;
  743. int max_num_args = 0;
  744. int post_varargs = 0;
  745. zval *arg;
  746. int arg_count;
  747. zend_bool have_varargs = 0;
  748. zval **varargs = NULL;
  749. int *n_varargs = NULL;
  750. for (spec_walk = type_spec; *spec_walk; spec_walk++) {
  751. c = *spec_walk;
  752. switch (c) {
  753. case 'l': case 'd':
  754. case 's': case 'b':
  755. case 'r': case 'a':
  756. case 'o': case 'O':
  757. case 'z': case 'Z':
  758. case 'C': case 'h':
  759. case 'f': case 'A':
  760. case 'H': case 'p':
  761. case 'S': case 'P':
  762. case 'L': case 'n':
  763. max_num_args++;
  764. break;
  765. case '|':
  766. min_num_args = max_num_args;
  767. break;
  768. case '/':
  769. case '!':
  770. /* Pass */
  771. break;
  772. case '*':
  773. case '+':
  774. if (have_varargs) {
  775. zend_parse_parameters_debug_error(
  776. "only one varargs specifier (* or +) is permitted");
  777. return FAILURE;
  778. }
  779. have_varargs = 1;
  780. /* we expect at least one parameter in varargs */
  781. if (c == '+') {
  782. max_num_args++;
  783. }
  784. /* mark the beginning of varargs */
  785. post_varargs = max_num_args;
  786. break;
  787. default:
  788. zend_parse_parameters_debug_error("bad type specifier while parsing parameters");
  789. return FAILURE;
  790. }
  791. }
  792. if (min_num_args < 0) {
  793. min_num_args = max_num_args;
  794. }
  795. if (have_varargs) {
  796. /* calculate how many required args are at the end of the specifier list */
  797. post_varargs = max_num_args - post_varargs;
  798. max_num_args = -1;
  799. }
  800. if (num_args < min_num_args || (num_args > max_num_args && max_num_args >= 0)) {
  801. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  802. zend_function *active_function = EG(current_execute_data)->func;
  803. const char *class_name = active_function->common.scope ? ZSTR_VAL(active_function->common.scope->name) : "";
  804. zend_argument_count_error("%s%s%s() expects %s %d parameter%s, %d given",
  805. class_name,
  806. class_name[0] ? "::" : "",
  807. ZSTR_VAL(active_function->common.function_name),
  808. min_num_args == max_num_args ? "exactly" : num_args < min_num_args ? "at least" : "at most",
  809. num_args < min_num_args ? min_num_args : max_num_args,
  810. (num_args < min_num_args ? min_num_args : max_num_args) == 1 ? "" : "s",
  811. num_args);
  812. }
  813. return FAILURE;
  814. }
  815. arg_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
  816. if (num_args > arg_count) {
  817. zend_parse_parameters_debug_error("could not obtain parameters for parsing");
  818. return FAILURE;
  819. }
  820. i = 0;
  821. while (num_args-- > 0) {
  822. if (*type_spec == '|') {
  823. type_spec++;
  824. }
  825. if (*type_spec == '*' || *type_spec == '+') {
  826. int num_varargs = num_args + 1 - post_varargs;
  827. /* eat up the passed in storage even if it won't be filled in with varargs */
  828. varargs = va_arg(*va, zval **);
  829. n_varargs = va_arg(*va, int *);
  830. type_spec++;
  831. if (num_varargs > 0) {
  832. *n_varargs = num_varargs;
  833. *varargs = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  834. /* adjust how many args we have left and restart loop */
  835. num_args += 1 - num_varargs;
  836. i += num_varargs;
  837. continue;
  838. } else {
  839. *varargs = NULL;
  840. *n_varargs = 0;
  841. }
  842. }
  843. arg = ZEND_CALL_ARG(EG(current_execute_data), i + 1);
  844. if (zend_parse_arg(i+1, arg, va, &type_spec, flags) == FAILURE) {
  845. /* clean up varargs array if it was used */
  846. if (varargs && *varargs) {
  847. *varargs = NULL;
  848. }
  849. return FAILURE;
  850. }
  851. i++;
  852. }
  853. return SUCCESS;
  854. }
  855. /* }}} */
  856. ZEND_API int zend_parse_parameters_ex(int flags, int num_args, const char *type_spec, ...) /* {{{ */
  857. {
  858. va_list va;
  859. int retval;
  860. va_start(va, type_spec);
  861. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  862. va_end(va);
  863. return retval;
  864. }
  865. /* }}} */
  866. ZEND_API int zend_parse_parameters(int num_args, const char *type_spec, ...) /* {{{ */
  867. {
  868. va_list va;
  869. int retval;
  870. int flags = 0;
  871. va_start(va, type_spec);
  872. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  873. va_end(va);
  874. return retval;
  875. }
  876. /* }}} */
  877. ZEND_API int zend_parse_method_parameters(int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  878. {
  879. va_list va;
  880. int retval;
  881. int flags = 0;
  882. const char *p = type_spec;
  883. zval **object;
  884. zend_class_entry *ce;
  885. /* Just checking this_ptr is not enough, because fcall_common_helper does not set
  886. * Z_OBJ(EG(This)) to NULL when calling an internal function with common.scope == NULL.
  887. * In that case EG(This) would still be the $this from the calling code and we'd take the
  888. * wrong branch here. */
  889. zend_bool is_method = EG(current_execute_data)->func->common.scope != NULL;
  890. if (!is_method || !this_ptr || Z_TYPE_P(this_ptr) != IS_OBJECT) {
  891. va_start(va, type_spec);
  892. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  893. va_end(va);
  894. } else {
  895. p++;
  896. va_start(va, type_spec);
  897. object = va_arg(va, zval **);
  898. ce = va_arg(va, zend_class_entry *);
  899. *object = this_ptr;
  900. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  901. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
  902. ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name(), ZSTR_VAL(ce->name), get_active_function_name());
  903. }
  904. retval = zend_parse_va_args(num_args, p, &va, flags);
  905. va_end(va);
  906. }
  907. return retval;
  908. }
  909. /* }}} */
  910. ZEND_API int zend_parse_method_parameters_ex(int flags, int num_args, zval *this_ptr, const char *type_spec, ...) /* {{{ */
  911. {
  912. va_list va;
  913. int retval;
  914. const char *p = type_spec;
  915. zval **object;
  916. zend_class_entry *ce;
  917. if (!this_ptr) {
  918. va_start(va, type_spec);
  919. retval = zend_parse_va_args(num_args, type_spec, &va, flags);
  920. va_end(va);
  921. } else {
  922. p++;
  923. va_start(va, type_spec);
  924. object = va_arg(va, zval **);
  925. ce = va_arg(va, zend_class_entry *);
  926. *object = this_ptr;
  927. if (ce && !instanceof_function(Z_OBJCE_P(this_ptr), ce)) {
  928. if (!(flags & ZEND_PARSE_PARAMS_QUIET)) {
  929. zend_error_noreturn(E_CORE_ERROR, "%s::%s() must be derived from %s::%s",
  930. ZSTR_VAL(ce->name), get_active_function_name(), ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), get_active_function_name());
  931. }
  932. va_end(va);
  933. return FAILURE;
  934. }
  935. retval = zend_parse_va_args(num_args, p, &va, flags);
  936. va_end(va);
  937. }
  938. return retval;
  939. }
  940. /* }}} */
  941. /* This function should be called after the constructor has been called
  942. * because it may call __set from the uninitialized object otherwise. */
  943. ZEND_API void zend_merge_properties(zval *obj, HashTable *properties) /* {{{ */
  944. {
  945. zend_object *zobj = Z_OBJ_P(obj);
  946. zend_object_write_property_t write_property = zobj->handlers->write_property;
  947. zend_class_entry *old_scope = EG(fake_scope);
  948. zend_string *key;
  949. zval *value;
  950. EG(fake_scope) = Z_OBJCE_P(obj);
  951. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, value) {
  952. if (key) {
  953. write_property(zobj, key, value, NULL);
  954. }
  955. } ZEND_HASH_FOREACH_END();
  956. EG(fake_scope) = old_scope;
  957. }
  958. /* }}} */
  959. ZEND_API int zend_update_class_constants(zend_class_entry *class_type) /* {{{ */
  960. {
  961. if (!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED)) {
  962. zend_class_constant *c;
  963. zval *val;
  964. zend_property_info *prop_info;
  965. if (class_type->parent) {
  966. if (UNEXPECTED(zend_update_class_constants(class_type->parent) != SUCCESS)) {
  967. return FAILURE;
  968. }
  969. }
  970. ZEND_HASH_FOREACH_PTR(&class_type->constants_table, c) {
  971. val = &c->value;
  972. if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
  973. if (UNEXPECTED(zval_update_constant_ex(val, c->ce) != SUCCESS)) {
  974. return FAILURE;
  975. }
  976. }
  977. } ZEND_HASH_FOREACH_END();
  978. if (class_type->default_static_members_count && !CE_STATIC_MEMBERS(class_type)) {
  979. if (class_type->type == ZEND_INTERNAL_CLASS || (class_type->ce_flags & (ZEND_ACC_IMMUTABLE|ZEND_ACC_PRELOADED))) {
  980. zend_class_init_statics(class_type);
  981. }
  982. }
  983. ZEND_HASH_FOREACH_PTR(&class_type->properties_info, prop_info) {
  984. if (prop_info->flags & ZEND_ACC_STATIC) {
  985. val = CE_STATIC_MEMBERS(class_type) + prop_info->offset;
  986. } else {
  987. val = (zval*)((char*)class_type->default_properties_table + prop_info->offset - OBJ_PROP_TO_OFFSET(0));
  988. }
  989. if (Z_TYPE_P(val) == IS_CONSTANT_AST) {
  990. if (ZEND_TYPE_IS_SET(prop_info->type)) {
  991. zval tmp;
  992. ZVAL_COPY(&tmp, val);
  993. if (UNEXPECTED(zval_update_constant_ex(&tmp, prop_info->ce) != SUCCESS)) {
  994. zval_ptr_dtor(&tmp);
  995. return FAILURE;
  996. }
  997. /* property initializers must always be evaluated with strict types */;
  998. if (UNEXPECTED(!zend_verify_property_type(prop_info, &tmp, /* strict */ 1))) {
  999. zval_ptr_dtor(&tmp);
  1000. return FAILURE;
  1001. }
  1002. zval_ptr_dtor(val);
  1003. ZVAL_COPY_VALUE(val, &tmp);
  1004. } else if (UNEXPECTED(zval_update_constant_ex(val, prop_info->ce) != SUCCESS)) {
  1005. return FAILURE;
  1006. }
  1007. }
  1008. } ZEND_HASH_FOREACH_END();
  1009. class_type->ce_flags |= ZEND_ACC_CONSTANTS_UPDATED;
  1010. }
  1011. return SUCCESS;
  1012. }
  1013. /* }}} */
  1014. static zend_always_inline void _object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
  1015. {
  1016. if (class_type->default_properties_count) {
  1017. zval *src = class_type->default_properties_table;
  1018. zval *dst = object->properties_table;
  1019. zval *end = src + class_type->default_properties_count;
  1020. if (UNEXPECTED(class_type->type == ZEND_INTERNAL_CLASS)) {
  1021. do {
  1022. ZVAL_COPY_OR_DUP_PROP(dst, src);
  1023. src++;
  1024. dst++;
  1025. } while (src != end);
  1026. } else {
  1027. do {
  1028. ZVAL_COPY_PROP(dst, src);
  1029. src++;
  1030. dst++;
  1031. } while (src != end);
  1032. }
  1033. }
  1034. }
  1035. /* }}} */
  1036. ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type) /* {{{ */
  1037. {
  1038. object->properties = NULL;
  1039. _object_properties_init(object, class_type);
  1040. }
  1041. /* }}} */
  1042. ZEND_API void object_properties_init_ex(zend_object *object, HashTable *properties) /* {{{ */
  1043. {
  1044. object->properties = properties;
  1045. if (object->ce->default_properties_count) {
  1046. zval *prop;
  1047. zend_string *key;
  1048. zend_property_info *property_info;
  1049. ZEND_HASH_FOREACH_STR_KEY_VAL(properties, key, prop) {
  1050. property_info = zend_get_property_info(object->ce, key, 1);
  1051. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1052. property_info &&
  1053. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1054. zval *slot = OBJ_PROP(object, property_info->offset);
  1055. if (UNEXPECTED(ZEND_TYPE_IS_SET(property_info->type))) {
  1056. zval tmp;
  1057. ZVAL_COPY_VALUE(&tmp, prop);
  1058. if (UNEXPECTED(!zend_verify_property_type(property_info, &tmp, 0))) {
  1059. continue;
  1060. }
  1061. ZVAL_COPY_VALUE(slot, &tmp);
  1062. } else {
  1063. ZVAL_COPY_VALUE(slot, prop);
  1064. }
  1065. ZVAL_INDIRECT(prop, slot);
  1066. }
  1067. } ZEND_HASH_FOREACH_END();
  1068. }
  1069. }
  1070. /* }}} */
  1071. ZEND_API void object_properties_load(zend_object *object, HashTable *properties) /* {{{ */
  1072. {
  1073. zval *prop, tmp;
  1074. zend_string *key;
  1075. zend_long h;
  1076. zend_property_info *property_info;
  1077. ZEND_HASH_FOREACH_KEY_VAL(properties, h, key, prop) {
  1078. if (key) {
  1079. if (ZSTR_VAL(key)[0] == '\0') {
  1080. const char *class_name, *prop_name;
  1081. size_t prop_name_len;
  1082. if (zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_name_len) == SUCCESS) {
  1083. zend_string *pname = zend_string_init(prop_name, prop_name_len, 0);
  1084. zend_class_entry *prev_scope = EG(fake_scope);
  1085. if (class_name && class_name[0] != '*') {
  1086. zend_string *cname = zend_string_init(class_name, strlen(class_name), 0);
  1087. EG(fake_scope) = zend_lookup_class(cname);
  1088. zend_string_release_ex(cname, 0);
  1089. }
  1090. property_info = zend_get_property_info(object->ce, pname, 1);
  1091. zend_string_release_ex(pname, 0);
  1092. EG(fake_scope) = prev_scope;
  1093. } else {
  1094. property_info = ZEND_WRONG_PROPERTY_INFO;
  1095. }
  1096. } else {
  1097. property_info = zend_get_property_info(object->ce, key, 1);
  1098. }
  1099. if (property_info != ZEND_WRONG_PROPERTY_INFO &&
  1100. property_info &&
  1101. (property_info->flags & ZEND_ACC_STATIC) == 0) {
  1102. zval *slot = OBJ_PROP(object, property_info->offset);
  1103. zval_ptr_dtor(slot);
  1104. ZVAL_COPY_VALUE(slot, prop);
  1105. zval_add_ref(slot);
  1106. if (object->properties) {
  1107. ZVAL_INDIRECT(&tmp, slot);
  1108. zend_hash_update(object->properties, key, &tmp);
  1109. }
  1110. } else {
  1111. if (!object->properties) {
  1112. rebuild_object_properties(object);
  1113. }
  1114. prop = zend_hash_update(object->properties, key, prop);
  1115. zval_add_ref(prop);
  1116. }
  1117. } else {
  1118. if (!object->properties) {
  1119. rebuild_object_properties(object);
  1120. }
  1121. prop = zend_hash_index_update(object->properties, h, prop);
  1122. zval_add_ref(prop);
  1123. }
  1124. } ZEND_HASH_FOREACH_END();
  1125. }
  1126. /* }}} */
  1127. /* This function requires 'properties' to contain all props declared in the
  1128. * class and all props being public. If only a subset is given or the class
  1129. * has protected members then you need to merge the properties separately by
  1130. * calling zend_merge_properties(). */
  1131. static zend_always_inline int _object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
  1132. {
  1133. if (UNEXPECTED(class_type->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
  1134. if (class_type->ce_flags & ZEND_ACC_INTERFACE) {
  1135. zend_throw_error(NULL, "Cannot instantiate interface %s", ZSTR_VAL(class_type->name));
  1136. } else if (class_type->ce_flags & ZEND_ACC_TRAIT) {
  1137. zend_throw_error(NULL, "Cannot instantiate trait %s", ZSTR_VAL(class_type->name));
  1138. } else {
  1139. zend_throw_error(NULL, "Cannot instantiate abstract class %s", ZSTR_VAL(class_type->name));
  1140. }
  1141. ZVAL_NULL(arg);
  1142. Z_OBJ_P(arg) = NULL;
  1143. return FAILURE;
  1144. }
  1145. if (UNEXPECTED(!(class_type->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  1146. if (UNEXPECTED(zend_update_class_constants(class_type) != SUCCESS)) {
  1147. ZVAL_NULL(arg);
  1148. Z_OBJ_P(arg) = NULL;
  1149. return FAILURE;
  1150. }
  1151. }
  1152. if (class_type->create_object == NULL) {
  1153. zend_object *obj = zend_objects_new(class_type);
  1154. ZVAL_OBJ(arg, obj);
  1155. if (properties) {
  1156. object_properties_init_ex(obj, properties);
  1157. } else {
  1158. _object_properties_init(obj, class_type);
  1159. }
  1160. } else {
  1161. ZVAL_OBJ(arg, class_type->create_object(class_type));
  1162. }
  1163. return SUCCESS;
  1164. }
  1165. /* }}} */
  1166. ZEND_API int object_and_properties_init(zval *arg, zend_class_entry *class_type, HashTable *properties) /* {{{ */
  1167. {
  1168. return _object_and_properties_init(arg, class_type, properties);
  1169. }
  1170. /* }}} */
  1171. ZEND_API int object_init_ex(zval *arg, zend_class_entry *class_type) /* {{{ */
  1172. {
  1173. return _object_and_properties_init(arg, class_type, NULL);
  1174. }
  1175. /* }}} */
  1176. ZEND_API int object_init(zval *arg) /* {{{ */
  1177. {
  1178. ZVAL_OBJ(arg, zend_objects_new(zend_standard_class_def));
  1179. return SUCCESS;
  1180. }
  1181. /* }}} */
  1182. ZEND_API int add_assoc_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1183. {
  1184. zval tmp;
  1185. ZVAL_LONG(&tmp, n);
  1186. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1187. return SUCCESS;
  1188. }
  1189. /* }}} */
  1190. ZEND_API int add_assoc_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1191. {
  1192. zval tmp;
  1193. ZVAL_NULL(&tmp);
  1194. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1195. return SUCCESS;
  1196. }
  1197. /* }}} */
  1198. ZEND_API int add_assoc_bool_ex(zval *arg, const char *key, size_t key_len, int b) /* {{{ */
  1199. {
  1200. zval tmp;
  1201. ZVAL_BOOL(&tmp, b);
  1202. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1203. return SUCCESS;
  1204. }
  1205. /* }}} */
  1206. ZEND_API int add_assoc_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1207. {
  1208. zval tmp;
  1209. ZVAL_RES(&tmp, r);
  1210. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1211. return SUCCESS;
  1212. }
  1213. /* }}} */
  1214. ZEND_API int add_assoc_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1215. {
  1216. zval tmp;
  1217. ZVAL_DOUBLE(&tmp, d);
  1218. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1219. return SUCCESS;
  1220. }
  1221. /* }}} */
  1222. ZEND_API int add_assoc_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1223. {
  1224. zval tmp;
  1225. ZVAL_STR(&tmp, str);
  1226. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1227. return SUCCESS;
  1228. }
  1229. /* }}} */
  1230. ZEND_API int add_assoc_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1231. {
  1232. zval tmp;
  1233. ZVAL_STRING(&tmp, str);
  1234. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1235. return SUCCESS;
  1236. }
  1237. /* }}} */
  1238. ZEND_API int add_assoc_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1239. {
  1240. zval tmp;
  1241. ZVAL_STRINGL(&tmp, str, length);
  1242. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, &tmp);
  1243. return SUCCESS;
  1244. }
  1245. /* }}} */
  1246. ZEND_API int add_assoc_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1247. {
  1248. zend_symtable_str_update(Z_ARRVAL_P(arg), key, key_len, value);
  1249. return SUCCESS;
  1250. }
  1251. /* }}} */
  1252. ZEND_API int add_index_long(zval *arg, zend_ulong index, zend_long n) /* {{{ */
  1253. {
  1254. zval tmp;
  1255. ZVAL_LONG(&tmp, n);
  1256. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1257. return SUCCESS;
  1258. }
  1259. /* }}} */
  1260. ZEND_API int add_index_null(zval *arg, zend_ulong index) /* {{{ */
  1261. {
  1262. zval tmp;
  1263. ZVAL_NULL(&tmp);
  1264. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1265. return SUCCESS;
  1266. }
  1267. /* }}} */
  1268. ZEND_API int add_index_bool(zval *arg, zend_ulong index, int b) /* {{{ */
  1269. {
  1270. zval tmp;
  1271. ZVAL_BOOL(&tmp, b);
  1272. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1273. return SUCCESS;
  1274. }
  1275. /* }}} */
  1276. ZEND_API int add_index_resource(zval *arg, zend_ulong index, zend_resource *r) /* {{{ */
  1277. {
  1278. zval tmp;
  1279. ZVAL_RES(&tmp, r);
  1280. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1281. return SUCCESS;
  1282. }
  1283. /* }}} */
  1284. ZEND_API int add_index_double(zval *arg, zend_ulong index, double d) /* {{{ */
  1285. {
  1286. zval tmp;
  1287. ZVAL_DOUBLE(&tmp, d);
  1288. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1289. return SUCCESS;
  1290. }
  1291. /* }}} */
  1292. ZEND_API int add_index_str(zval *arg, zend_ulong index, zend_string *str) /* {{{ */
  1293. {
  1294. zval tmp;
  1295. ZVAL_STR(&tmp, str);
  1296. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1297. return SUCCESS;
  1298. }
  1299. /* }}} */
  1300. ZEND_API int add_index_string(zval *arg, zend_ulong index, const char *str) /* {{{ */
  1301. {
  1302. zval tmp;
  1303. ZVAL_STRING(&tmp, str);
  1304. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1305. return SUCCESS;
  1306. }
  1307. /* }}} */
  1308. ZEND_API int add_index_stringl(zval *arg, zend_ulong index, const char *str, size_t length) /* {{{ */
  1309. {
  1310. zval tmp;
  1311. ZVAL_STRINGL(&tmp, str, length);
  1312. zend_hash_index_update(Z_ARRVAL_P(arg), index, &tmp);
  1313. return SUCCESS;
  1314. }
  1315. /* }}} */
  1316. ZEND_API int add_next_index_long(zval *arg, zend_long n) /* {{{ */
  1317. {
  1318. zval tmp;
  1319. ZVAL_LONG(&tmp, n);
  1320. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1321. }
  1322. /* }}} */
  1323. ZEND_API int add_next_index_null(zval *arg) /* {{{ */
  1324. {
  1325. zval tmp;
  1326. ZVAL_NULL(&tmp);
  1327. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1328. }
  1329. /* }}} */
  1330. ZEND_API int add_next_index_bool(zval *arg, int b) /* {{{ */
  1331. {
  1332. zval tmp;
  1333. ZVAL_BOOL(&tmp, b);
  1334. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1335. }
  1336. /* }}} */
  1337. ZEND_API int add_next_index_resource(zval *arg, zend_resource *r) /* {{{ */
  1338. {
  1339. zval tmp;
  1340. ZVAL_RES(&tmp, r);
  1341. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1342. }
  1343. /* }}} */
  1344. ZEND_API int add_next_index_double(zval *arg, double d) /* {{{ */
  1345. {
  1346. zval tmp;
  1347. ZVAL_DOUBLE(&tmp, d);
  1348. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1349. }
  1350. /* }}} */
  1351. ZEND_API int add_next_index_str(zval *arg, zend_string *str) /* {{{ */
  1352. {
  1353. zval tmp;
  1354. ZVAL_STR(&tmp, str);
  1355. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1356. }
  1357. /* }}} */
  1358. ZEND_API int add_next_index_string(zval *arg, const char *str) /* {{{ */
  1359. {
  1360. zval tmp;
  1361. ZVAL_STRING(&tmp, str);
  1362. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1363. }
  1364. /* }}} */
  1365. ZEND_API int add_next_index_stringl(zval *arg, const char *str, size_t length) /* {{{ */
  1366. {
  1367. zval tmp;
  1368. ZVAL_STRINGL(&tmp, str, length);
  1369. return zend_hash_next_index_insert(Z_ARRVAL_P(arg), &tmp) ? SUCCESS : FAILURE;
  1370. }
  1371. /* }}} */
  1372. ZEND_API int array_set_zval_key(HashTable *ht, zval *key, zval *value) /* {{{ */
  1373. {
  1374. zval *result;
  1375. switch (Z_TYPE_P(key)) {
  1376. case IS_STRING:
  1377. result = zend_symtable_update(ht, Z_STR_P(key), value);
  1378. break;
  1379. case IS_NULL:
  1380. result = zend_hash_update(ht, ZSTR_EMPTY_ALLOC(), value);
  1381. break;
  1382. case IS_RESOURCE:
  1383. zend_error(E_WARNING, "Resource ID#%d used as offset, casting to integer (%d)", Z_RES_HANDLE_P(key), Z_RES_HANDLE_P(key));
  1384. result = zend_hash_index_update(ht, Z_RES_HANDLE_P(key), value);
  1385. break;
  1386. case IS_FALSE:
  1387. result = zend_hash_index_update(ht, 0, value);
  1388. break;
  1389. case IS_TRUE:
  1390. result = zend_hash_index_update(ht, 1, value);
  1391. break;
  1392. case IS_LONG:
  1393. result = zend_hash_index_update(ht, Z_LVAL_P(key), value);
  1394. break;
  1395. case IS_DOUBLE:
  1396. result = zend_hash_index_update(ht, zend_dval_to_lval(Z_DVAL_P(key)), value);
  1397. break;
  1398. default:
  1399. zend_type_error("Illegal offset type");
  1400. result = NULL;
  1401. }
  1402. if (result) {
  1403. Z_TRY_ADDREF_P(result);
  1404. return SUCCESS;
  1405. } else {
  1406. return FAILURE;
  1407. }
  1408. }
  1409. /* }}} */
  1410. ZEND_API int add_property_long_ex(zval *arg, const char *key, size_t key_len, zend_long n) /* {{{ */
  1411. {
  1412. zval tmp;
  1413. ZVAL_LONG(&tmp, n);
  1414. return add_property_zval_ex(arg, key, key_len, &tmp);
  1415. }
  1416. /* }}} */
  1417. ZEND_API int add_property_bool_ex(zval *arg, const char *key, size_t key_len, zend_long b) /* {{{ */
  1418. {
  1419. zval tmp;
  1420. ZVAL_BOOL(&tmp, b);
  1421. return add_property_zval_ex(arg, key, key_len, &tmp);
  1422. }
  1423. /* }}} */
  1424. ZEND_API int add_property_null_ex(zval *arg, const char *key, size_t key_len) /* {{{ */
  1425. {
  1426. zval tmp;
  1427. ZVAL_NULL(&tmp);
  1428. return add_property_zval_ex(arg, key, key_len, &tmp);
  1429. }
  1430. /* }}} */
  1431. ZEND_API int add_property_resource_ex(zval *arg, const char *key, size_t key_len, zend_resource *r) /* {{{ */
  1432. {
  1433. zval tmp;
  1434. ZVAL_RES(&tmp, r);
  1435. add_property_zval_ex(arg, key, key_len, &tmp);
  1436. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1437. return SUCCESS;
  1438. }
  1439. /* }}} */
  1440. ZEND_API int add_property_double_ex(zval *arg, const char *key, size_t key_len, double d) /* {{{ */
  1441. {
  1442. zval tmp;
  1443. ZVAL_DOUBLE(&tmp, d);
  1444. return add_property_zval_ex(arg, key, key_len, &tmp);
  1445. }
  1446. /* }}} */
  1447. ZEND_API int add_property_str_ex(zval *arg, const char *key, size_t key_len, zend_string *str) /* {{{ */
  1448. {
  1449. zval tmp;
  1450. ZVAL_STR(&tmp, str);
  1451. add_property_zval_ex(arg, key, key_len, &tmp);
  1452. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1453. return SUCCESS;
  1454. }
  1455. /* }}} */
  1456. ZEND_API int add_property_string_ex(zval *arg, const char *key, size_t key_len, const char *str) /* {{{ */
  1457. {
  1458. zval tmp;
  1459. ZVAL_STRING(&tmp, str);
  1460. add_property_zval_ex(arg, key, key_len, &tmp);
  1461. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1462. return SUCCESS;
  1463. }
  1464. /* }}} */
  1465. ZEND_API int add_property_stringl_ex(zval *arg, const char *key, size_t key_len, const char *str, size_t length) /* {{{ */
  1466. {
  1467. zval tmp;
  1468. ZVAL_STRINGL(&tmp, str, length);
  1469. add_property_zval_ex(arg, key, key_len, &tmp);
  1470. zval_ptr_dtor(&tmp); /* write_property will add 1 to refcount */
  1471. return SUCCESS;
  1472. }
  1473. /* }}} */
  1474. ZEND_API int add_property_zval_ex(zval *arg, const char *key, size_t key_len, zval *value) /* {{{ */
  1475. {
  1476. zend_string *str;
  1477. str = zend_string_init(key, key_len, 0);
  1478. Z_OBJ_HANDLER_P(arg, write_property)(Z_OBJ_P(arg), str, value, NULL);
  1479. zend_string_release_ex(str, 0);
  1480. return SUCCESS;
  1481. }
  1482. /* }}} */
  1483. ZEND_API int zend_startup_module_ex(zend_module_entry *module) /* {{{ */
  1484. {
  1485. size_t name_len;
  1486. zend_string *lcname;
  1487. if (module->module_started) {
  1488. return SUCCESS;
  1489. }
  1490. module->module_started = 1;
  1491. /* Check module dependencies */
  1492. if (module->deps) {
  1493. const zend_module_dep *dep = module->deps;
  1494. while (dep->name) {
  1495. if (dep->type == MODULE_DEP_REQUIRED) {
  1496. zend_module_entry *req_mod;
  1497. name_len = strlen(dep->name);
  1498. lcname = zend_string_alloc(name_len, 0);
  1499. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  1500. if ((req_mod = zend_hash_find_ptr(&module_registry, lcname)) == NULL || !req_mod->module_started) {
  1501. zend_string_efree(lcname);
  1502. /* TODO: Check version relationship */
  1503. zend_error(E_CORE_WARNING, "Cannot load module '%s' because required module '%s' is not loaded", module->name, dep->name);
  1504. module->module_started = 0;
  1505. return FAILURE;
  1506. }
  1507. zend_string_efree(lcname);
  1508. }
  1509. ++dep;
  1510. }
  1511. }
  1512. /* Initialize module globals */
  1513. if (module->globals_size) {
  1514. #ifdef ZTS
  1515. ts_allocate_id(module->globals_id_ptr, module->globals_size, (ts_allocate_ctor) module->globals_ctor, (ts_allocate_dtor) module->globals_dtor);
  1516. #else
  1517. if (module->globals_ctor) {
  1518. module->globals_ctor(module->globals_ptr);
  1519. }
  1520. #endif
  1521. }
  1522. if (module->module_startup_func) {
  1523. EG(current_module) = module;
  1524. if (module->module_startup_func(module->type, module->module_number)==FAILURE) {
  1525. zend_error_noreturn(E_CORE_ERROR,"Unable to start %s module", module->name);
  1526. EG(current_module) = NULL;
  1527. return FAILURE;
  1528. }
  1529. EG(current_module) = NULL;
  1530. }
  1531. return SUCCESS;
  1532. }
  1533. /* }}} */
  1534. static int zend_startup_module_zval(zval *zv) /* {{{ */
  1535. {
  1536. zend_module_entry *module = Z_PTR_P(zv);
  1537. return (zend_startup_module_ex(module) == SUCCESS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  1538. }
  1539. /* }}} */
  1540. static void zend_sort_modules(void *base, size_t count, size_t siz, compare_func_t compare, swap_func_t swp) /* {{{ */
  1541. {
  1542. Bucket *b1 = base;
  1543. Bucket *b2;
  1544. Bucket *end = b1 + count;
  1545. Bucket tmp;
  1546. zend_module_entry *m, *r;
  1547. while (b1 < end) {
  1548. try_again:
  1549. m = (zend_module_entry*)Z_PTR(b1->val);
  1550. if (!m->module_started && m->deps) {
  1551. const zend_module_dep *dep = m->deps;
  1552. while (dep->name) {
  1553. if (dep->type == MODULE_DEP_REQUIRED || dep->type == MODULE_DEP_OPTIONAL) {
  1554. b2 = b1 + 1;
  1555. while (b2 < end) {
  1556. r = (zend_module_entry*)Z_PTR(b2->val);
  1557. if (strcasecmp(dep->name, r->name) == 0) {
  1558. tmp = *b1;
  1559. *b1 = *b2;
  1560. *b2 = tmp;
  1561. goto try_again;
  1562. }
  1563. b2++;
  1564. }
  1565. }
  1566. dep++;
  1567. }
  1568. }
  1569. b1++;
  1570. }
  1571. }
  1572. /* }}} */
  1573. ZEND_API void zend_collect_module_handlers(void) /* {{{ */
  1574. {
  1575. zend_module_entry *module;
  1576. int startup_count = 0;
  1577. int shutdown_count = 0;
  1578. int post_deactivate_count = 0;
  1579. zend_class_entry *ce;
  1580. int class_count = 0;
  1581. /* Collect extensions with request startup/shutdown handlers */
  1582. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1583. if (module->request_startup_func) {
  1584. startup_count++;
  1585. }
  1586. if (module->request_shutdown_func) {
  1587. shutdown_count++;
  1588. }
  1589. if (module->post_deactivate_func) {
  1590. post_deactivate_count++;
  1591. }
  1592. } ZEND_HASH_FOREACH_END();
  1593. module_request_startup_handlers = (zend_module_entry**)malloc(
  1594. sizeof(zend_module_entry*) *
  1595. (startup_count + 1 +
  1596. shutdown_count + 1 +
  1597. post_deactivate_count + 1));
  1598. module_request_startup_handlers[startup_count] = NULL;
  1599. module_request_shutdown_handlers = module_request_startup_handlers + startup_count + 1;
  1600. module_request_shutdown_handlers[shutdown_count] = NULL;
  1601. module_post_deactivate_handlers = module_request_shutdown_handlers + shutdown_count + 1;
  1602. module_post_deactivate_handlers[post_deactivate_count] = NULL;
  1603. startup_count = 0;
  1604. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1605. if (module->request_startup_func) {
  1606. module_request_startup_handlers[startup_count++] = module;
  1607. }
  1608. if (module->request_shutdown_func) {
  1609. module_request_shutdown_handlers[--shutdown_count] = module;
  1610. }
  1611. if (module->post_deactivate_func) {
  1612. module_post_deactivate_handlers[--post_deactivate_count] = module;
  1613. }
  1614. } ZEND_HASH_FOREACH_END();
  1615. /* Collect internal classes with static members */
  1616. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1617. if (ce->type == ZEND_INTERNAL_CLASS &&
  1618. ce->default_static_members_count > 0) {
  1619. class_count++;
  1620. }
  1621. } ZEND_HASH_FOREACH_END();
  1622. class_cleanup_handlers = (zend_class_entry**)malloc(
  1623. sizeof(zend_class_entry*) *
  1624. (class_count + 1));
  1625. class_cleanup_handlers[class_count] = NULL;
  1626. if (class_count) {
  1627. ZEND_HASH_FOREACH_PTR(CG(class_table), ce) {
  1628. if (ce->type == ZEND_INTERNAL_CLASS &&
  1629. ce->default_static_members_count > 0) {
  1630. class_cleanup_handlers[--class_count] = ce;
  1631. }
  1632. } ZEND_HASH_FOREACH_END();
  1633. }
  1634. }
  1635. /* }}} */
  1636. ZEND_API int zend_startup_modules(void) /* {{{ */
  1637. {
  1638. zend_hash_sort_ex(&module_registry, zend_sort_modules, NULL, 0);
  1639. zend_hash_apply(&module_registry, zend_startup_module_zval);
  1640. return SUCCESS;
  1641. }
  1642. /* }}} */
  1643. ZEND_API void zend_destroy_modules(void) /* {{{ */
  1644. {
  1645. free(class_cleanup_handlers);
  1646. free(module_request_startup_handlers);
  1647. zend_hash_graceful_reverse_destroy(&module_registry);
  1648. }
  1649. /* }}} */
  1650. ZEND_API zend_module_entry* zend_register_module_ex(zend_module_entry *module) /* {{{ */
  1651. {
  1652. size_t name_len;
  1653. zend_string *lcname;
  1654. zend_module_entry *module_ptr;
  1655. if (!module) {
  1656. return NULL;
  1657. }
  1658. #if 0
  1659. zend_printf("%s: Registering module %d\n", module->name, module->module_number);
  1660. #endif
  1661. /* Check module dependencies */
  1662. if (module->deps) {
  1663. const zend_module_dep *dep = module->deps;
  1664. while (dep->name) {
  1665. if (dep->type == MODULE_DEP_CONFLICTS) {
  1666. name_len = strlen(dep->name);
  1667. lcname = zend_string_alloc(name_len, 0);
  1668. zend_str_tolower_copy(ZSTR_VAL(lcname), dep->name, name_len);
  1669. if (zend_hash_exists(&module_registry, lcname) || zend_get_extension(dep->name)) {
  1670. zend_string_efree(lcname);
  1671. /* TODO: Check version relationship */
  1672. zend_error(E_CORE_WARNING, "Cannot load module '%s' because conflicting module '%s' is already loaded", module->name, dep->name);
  1673. return NULL;
  1674. }
  1675. zend_string_efree(lcname);
  1676. }
  1677. ++dep;
  1678. }
  1679. }
  1680. name_len = strlen(module->name);
  1681. lcname = zend_string_alloc(name_len, module->type == MODULE_PERSISTENT);
  1682. zend_str_tolower_copy(ZSTR_VAL(lcname), module->name, name_len);
  1683. lcname = zend_new_interned_string(lcname);
  1684. if ((module_ptr = zend_hash_add_mem(&module_registry, lcname, module, sizeof(zend_module_entry))) == NULL) {
  1685. zend_error(E_CORE_WARNING, "Module '%s' already loaded", module->name);
  1686. zend_string_release(lcname);
  1687. return NULL;
  1688. }
  1689. module = module_ptr;
  1690. EG(current_module) = module;
  1691. if (module->functions && zend_register_functions(NULL, module->functions, NULL, module->type)==FAILURE) {
  1692. zend_hash_del(&module_registry, lcname);
  1693. zend_string_release(lcname);
  1694. EG(current_module) = NULL;
  1695. zend_error(E_CORE_WARNING,"%s: Unable to register functions, unable to load", module->name);
  1696. return NULL;
  1697. }
  1698. EG(current_module) = NULL;
  1699. zend_string_release(lcname);
  1700. return module;
  1701. }
  1702. /* }}} */
  1703. ZEND_API zend_module_entry* zend_register_internal_module(zend_module_entry *module) /* {{{ */
  1704. {
  1705. module->module_number = zend_next_free_module();
  1706. module->type = MODULE_PERSISTENT;
  1707. return zend_register_module_ex(module);
  1708. }
  1709. /* }}} */
  1710. ZEND_API void zend_check_magic_method_implementation(const zend_class_entry *ce, const zend_function *fptr, zend_string *lcname, int error_type) /* {{{ */
  1711. {
  1712. if (ZSTR_VAL(fptr->common.function_name)[0] != '_'
  1713. || ZSTR_VAL(fptr->common.function_name)[1] != '_') {
  1714. return;
  1715. }
  1716. if (zend_string_equals_literal(lcname, ZEND_DESTRUCTOR_FUNC_NAME) && fptr->common.num_args != 0) {
  1717. zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(ce->name), ZEND_DESTRUCTOR_FUNC_NAME);
  1718. } else if (zend_string_equals_literal(lcname, ZEND_CLONE_FUNC_NAME) && fptr->common.num_args != 0) {
  1719. zend_error(error_type, "Method %s::%s() cannot accept any arguments", ZSTR_VAL(ce->name), ZEND_CLONE_FUNC_NAME);
  1720. } else if (zend_string_equals_literal(lcname, ZEND_GET_FUNC_NAME)) {
  1721. if (fptr->common.num_args != 1) {
  1722. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
  1723. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1724. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_GET_FUNC_NAME);
  1725. }
  1726. } else if (zend_string_equals_literal(lcname, ZEND_SET_FUNC_NAME)) {
  1727. if (fptr->common.num_args != 2) {
  1728. zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
  1729. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1730. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_SET_FUNC_NAME);
  1731. }
  1732. } else if (zend_string_equals_literal(lcname, ZEND_UNSET_FUNC_NAME)) {
  1733. if (fptr->common.num_args != 1) {
  1734. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
  1735. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1736. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_UNSET_FUNC_NAME);
  1737. }
  1738. } else if (zend_string_equals_literal(lcname, ZEND_ISSET_FUNC_NAME)) {
  1739. if (fptr->common.num_args != 1) {
  1740. zend_error(error_type, "Method %s::%s() must take exactly 1 argument", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
  1741. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1)) {
  1742. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_ISSET_FUNC_NAME);
  1743. }
  1744. } else if (zend_string_equals_literal(lcname, ZEND_CALL_FUNC_NAME)) {
  1745. if (fptr->common.num_args != 2) {
  1746. zend_error(error_type, "Method %s::%s() must take exactly 2 arguments", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
  1747. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1748. zend_error(error_type, "Method %s::%s() cannot take arguments by reference", ZSTR_VAL(ce->name), ZEND_CALL_FUNC_NAME);
  1749. }
  1750. } else if (zend_string_equals_literal(lcname, ZEND_CALLSTATIC_FUNC_NAME)) {
  1751. if (fptr->common.num_args != 2) {
  1752. zend_error(error_type, "Method %s::__callStatic() must take exactly 2 arguments", ZSTR_VAL(ce->name));
  1753. } else if (QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 1) || QUICK_ARG_SHOULD_BE_SENT_BY_REF(fptr, 2)) {
  1754. zend_error(error_type, "Method %s::__callStatic() cannot take arguments by reference", ZSTR_VAL(ce->name));
  1755. }
  1756. } else if (zend_string_equals_literal(lcname, ZEND_TOSTRING_FUNC_NAME) && fptr->common.num_args != 0) {
  1757. zend_error(error_type, "Method %s::__toString() cannot take arguments", ZSTR_VAL(ce->name));
  1758. } else if (zend_string_equals_literal(lcname, ZEND_DEBUGINFO_FUNC_NAME) && fptr->common.num_args != 0) {
  1759. zend_error(error_type, "Method %s::__debugInfo() cannot take arguments", ZSTR_VAL(ce->name));
  1760. } else if (zend_string_equals_literal(lcname, "__serialize") && fptr->common.num_args != 0) {
  1761. zend_error(error_type, "Method %s::__serialize() cannot take arguments", ZSTR_VAL(ce->name));
  1762. } else if (zend_string_equals_literal(lcname, "__unserialize") && fptr->common.num_args != 1) {
  1763. zend_error(error_type, "Method %s::__unserialize() must take exactly 1 argument", ZSTR_VAL(ce->name));
  1764. }
  1765. }
  1766. /* }}} */
  1767. /* registers all functions in *library_functions in the function hash */
  1768. ZEND_API int zend_register_functions(zend_class_entry *scope, const zend_function_entry *functions, HashTable *function_table, int type) /* {{{ */
  1769. {
  1770. const zend_function_entry *ptr = functions;
  1771. zend_function function, *reg_function;
  1772. zend_internal_function *internal_function = (zend_internal_function *)&function;
  1773. int count=0, unload=0;
  1774. HashTable *target_function_table = function_table;
  1775. int error_type;
  1776. zend_function *ctor = NULL, *dtor = NULL, *clone = NULL, *__get = NULL, *__set = NULL, *__unset = NULL, *__isset = NULL, *__call = NULL, *__callstatic = NULL, *__tostring = NULL, *__debugInfo = NULL, *serialize_func = NULL, *unserialize_func = NULL;
  1777. zend_string *lowercase_name;
  1778. size_t fname_len;
  1779. const char *lc_class_name = NULL;
  1780. size_t class_name_len = 0;
  1781. if (type==MODULE_PERSISTENT) {
  1782. error_type = E_CORE_WARNING;
  1783. } else {
  1784. error_type = E_WARNING;
  1785. }
  1786. if (!target_function_table) {
  1787. target_function_table = CG(function_table);
  1788. }
  1789. internal_function->type = ZEND_INTERNAL_FUNCTION;
  1790. internal_function->module = EG(current_module);
  1791. memset(internal_function->reserved, 0, ZEND_MAX_RESERVED_RESOURCES * sizeof(void*));
  1792. if (scope) {
  1793. class_name_len = ZSTR_LEN(scope->name);
  1794. if ((lc_class_name = zend_memrchr(ZSTR_VAL(scope->name), '\\', class_name_len))) {
  1795. ++lc_class_name;
  1796. class_name_len -= (lc_class_name - ZSTR_VAL(scope->name));
  1797. lc_class_name = zend_str_tolower_dup(lc_class_name, class_name_len);
  1798. } else {
  1799. lc_class_name = zend_str_tolower_dup(ZSTR_VAL(scope->name), class_name_len);
  1800. }
  1801. }
  1802. while (ptr->fname) {
  1803. fname_len = strlen(ptr->fname);
  1804. internal_function->handler = ptr->handler;
  1805. internal_function->function_name = zend_string_init_interned(ptr->fname, fname_len, 1);
  1806. internal_function->scope = scope;
  1807. internal_function->prototype = NULL;
  1808. if (ptr->flags) {
  1809. if (!(ptr->flags & ZEND_ACC_PPP_MASK)) {
  1810. if (ptr->flags != ZEND_ACC_DEPRECATED && scope) {
  1811. zend_error(error_type, "Invalid access level for %s%s%s() - access must be exactly one of public, protected or private", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1812. }
  1813. internal_function->fn_flags = ZEND_ACC_PUBLIC | ptr->flags;
  1814. } else {
  1815. internal_function->fn_flags = ptr->flags;
  1816. }
  1817. } else {
  1818. internal_function->fn_flags = ZEND_ACC_PUBLIC;
  1819. }
  1820. if (ptr->arg_info) {
  1821. zend_internal_function_info *info = (zend_internal_function_info*)ptr->arg_info;
  1822. internal_function->arg_info = (zend_internal_arg_info*)ptr->arg_info+1;
  1823. internal_function->num_args = ptr->num_args;
  1824. /* Currently you cannot denote that the function can accept less arguments than num_args */
  1825. if (info->required_num_args == (zend_uintptr_t)-1) {
  1826. internal_function->required_num_args = ptr->num_args;
  1827. } else {
  1828. internal_function->required_num_args = info->required_num_args;
  1829. }
  1830. if (ZEND_ARG_SEND_MODE(info)) {
  1831. internal_function->fn_flags |= ZEND_ACC_RETURN_REFERENCE;
  1832. }
  1833. if (ZEND_ARG_IS_VARIADIC(&ptr->arg_info[ptr->num_args])) {
  1834. internal_function->fn_flags |= ZEND_ACC_VARIADIC;
  1835. /* Don't count the variadic argument */
  1836. internal_function->num_args--;
  1837. }
  1838. if (ZEND_TYPE_IS_SET(info->type)) {
  1839. if (ZEND_TYPE_HAS_NAME(info->type)) {
  1840. const char *type_name = ZEND_TYPE_LITERAL_NAME(info->type);
  1841. if (!scope && (!strcasecmp(type_name, "self") || !strcasecmp(type_name, "parent"))) {
  1842. zend_error_noreturn(E_CORE_ERROR, "Cannot declare a return type of %s outside of a class scope", type_name);
  1843. }
  1844. }
  1845. internal_function->fn_flags |= ZEND_ACC_HAS_RETURN_TYPE;
  1846. }
  1847. } else {
  1848. zend_error(E_CORE_WARNING, "Missing arginfo for %s%s%s()",
  1849. scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1850. internal_function->arg_info = NULL;
  1851. internal_function->num_args = 0;
  1852. internal_function->required_num_args = 0;
  1853. }
  1854. zend_set_function_arg_flags((zend_function*)internal_function);
  1855. if (ptr->flags & ZEND_ACC_ABSTRACT) {
  1856. if (scope) {
  1857. /* This is a class that must be abstract itself. Here we set the check info. */
  1858. scope->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
  1859. if (!(scope->ce_flags & ZEND_ACC_INTERFACE)) {
  1860. /* Since the class is not an interface it needs to be declared as a abstract class. */
  1861. /* Since here we are handling internal functions only we can add the keyword flag. */
  1862. /* This time we set the flag for the keyword 'abstract'. */
  1863. scope->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
  1864. }
  1865. }
  1866. if (ptr->flags & ZEND_ACC_STATIC && (!scope || !(scope->ce_flags & ZEND_ACC_INTERFACE))) {
  1867. zend_error(error_type, "Static function %s%s%s() cannot be abstract", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1868. }
  1869. } else {
  1870. if (scope && (scope->ce_flags & ZEND_ACC_INTERFACE)) {
  1871. efree((char*)lc_class_name);
  1872. zend_error(error_type, "Interface %s cannot contain non abstract method %s()", ZSTR_VAL(scope->name), ptr->fname);
  1873. return FAILURE;
  1874. }
  1875. if (!internal_function->handler) {
  1876. if (scope) {
  1877. efree((char*)lc_class_name);
  1878. }
  1879. zend_error(error_type, "Method %s%s%s() cannot be a NULL function", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1880. zend_unregister_functions(functions, count, target_function_table);
  1881. return FAILURE;
  1882. }
  1883. }
  1884. lowercase_name = zend_string_tolower_ex(internal_function->function_name, type == MODULE_PERSISTENT);
  1885. lowercase_name = zend_new_interned_string(lowercase_name);
  1886. reg_function = malloc(sizeof(zend_internal_function));
  1887. memcpy(reg_function, &function, sizeof(zend_internal_function));
  1888. if (zend_hash_add_ptr(target_function_table, lowercase_name, reg_function) == NULL) {
  1889. unload=1;
  1890. free(reg_function);
  1891. zend_string_release(lowercase_name);
  1892. break;
  1893. }
  1894. /* If types of arguments have to be checked */
  1895. if (reg_function->common.arg_info && reg_function->common.num_args) {
  1896. uint32_t i;
  1897. for (i = 0; i < reg_function->common.num_args; i++) {
  1898. zend_arg_info *arg_info = &reg_function->common.arg_info[i];
  1899. ZEND_ASSERT(arg_info->name && "Parameter must have a name");
  1900. if (ZEND_TYPE_IS_SET(arg_info->type)) {
  1901. reg_function->common.fn_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  1902. }
  1903. }
  1904. }
  1905. if (reg_function->common.arg_info &&
  1906. (reg_function->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))) {
  1907. /* convert "const char*" class type names into "zend_string*" */
  1908. uint32_t i;
  1909. uint32_t num_args = reg_function->common.num_args + 1;
  1910. zend_arg_info *arg_info = reg_function->common.arg_info - 1;
  1911. zend_arg_info *new_arg_info;
  1912. if (reg_function->common.fn_flags & ZEND_ACC_VARIADIC) {
  1913. num_args++;
  1914. }
  1915. new_arg_info = malloc(sizeof(zend_arg_info) * num_args);
  1916. memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
  1917. reg_function->common.arg_info = new_arg_info + 1;
  1918. for (i = 0; i < num_args; i++) {
  1919. if (ZEND_TYPE_HAS_CLASS(new_arg_info[i].type)) {
  1920. ZEND_ASSERT(ZEND_TYPE_HAS_NAME(new_arg_info[i].type)
  1921. && "Only simple classes are currently supported");
  1922. const char *class_name = ZEND_TYPE_LITERAL_NAME(new_arg_info[i].type);
  1923. ZEND_TYPE_SET_PTR(new_arg_info[i].type,
  1924. zend_string_init_interned(class_name, strlen(class_name), 1));
  1925. }
  1926. }
  1927. }
  1928. if (scope) {
  1929. /* Look for ctor, dtor, clone */
  1930. if (zend_string_equals_literal(lowercase_name, "serialize")) {
  1931. serialize_func = reg_function;
  1932. } else if (zend_string_equals_literal(lowercase_name, "unserialize")) {
  1933. unserialize_func = reg_function;
  1934. } else if (ZSTR_VAL(lowercase_name)[0] != '_' || ZSTR_VAL(lowercase_name)[1] != '_') {
  1935. reg_function = NULL;
  1936. } else if (zend_string_equals_literal(lowercase_name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  1937. ctor = reg_function;
  1938. } else if (zend_string_equals_literal(lowercase_name, ZEND_DESTRUCTOR_FUNC_NAME)) {
  1939. dtor = reg_function;
  1940. if (internal_function->num_args) {
  1941. zend_error(error_type, "Destructor %s::%s() cannot take arguments", ZSTR_VAL(scope->name), ptr->fname);
  1942. }
  1943. } else if (zend_string_equals_literal(lowercase_name, ZEND_CLONE_FUNC_NAME)) {
  1944. clone = reg_function;
  1945. } else if (zend_string_equals_literal(lowercase_name, ZEND_CALL_FUNC_NAME)) {
  1946. __call = reg_function;
  1947. } else if (zend_string_equals_literal(lowercase_name, ZEND_CALLSTATIC_FUNC_NAME)) {
  1948. __callstatic = reg_function;
  1949. } else if (zend_string_equals_literal(lowercase_name, ZEND_TOSTRING_FUNC_NAME)) {
  1950. __tostring = reg_function;
  1951. } else if (zend_string_equals_literal(lowercase_name, ZEND_GET_FUNC_NAME)) {
  1952. __get = reg_function;
  1953. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  1954. } else if (zend_string_equals_literal(lowercase_name, ZEND_SET_FUNC_NAME)) {
  1955. __set = reg_function;
  1956. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  1957. } else if (zend_string_equals_literal(lowercase_name, ZEND_UNSET_FUNC_NAME)) {
  1958. __unset = reg_function;
  1959. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  1960. } else if (zend_string_equals_literal(lowercase_name, ZEND_ISSET_FUNC_NAME)) {
  1961. __isset = reg_function;
  1962. scope->ce_flags |= ZEND_ACC_USE_GUARDS;
  1963. } else if (zend_string_equals_literal(lowercase_name, ZEND_DEBUGINFO_FUNC_NAME)) {
  1964. __debugInfo = reg_function;
  1965. } else {
  1966. reg_function = NULL;
  1967. }
  1968. if (reg_function) {
  1969. zend_check_magic_method_implementation(
  1970. scope, reg_function, lowercase_name, error_type);
  1971. }
  1972. }
  1973. ptr++;
  1974. count++;
  1975. zend_string_release(lowercase_name);
  1976. }
  1977. if (unload) { /* before unloading, display all remaining bad function in the module */
  1978. if (scope) {
  1979. efree((char*)lc_class_name);
  1980. }
  1981. while (ptr->fname) {
  1982. fname_len = strlen(ptr->fname);
  1983. lowercase_name = zend_string_alloc(fname_len, 0);
  1984. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  1985. if (zend_hash_exists(target_function_table, lowercase_name)) {
  1986. zend_error(error_type, "Function registration failed - duplicate name - %s%s%s", scope ? ZSTR_VAL(scope->name) : "", scope ? "::" : "", ptr->fname);
  1987. }
  1988. zend_string_efree(lowercase_name);
  1989. ptr++;
  1990. }
  1991. zend_unregister_functions(functions, count, target_function_table);
  1992. return FAILURE;
  1993. }
  1994. if (scope) {
  1995. scope->constructor = ctor;
  1996. scope->destructor = dtor;
  1997. scope->clone = clone;
  1998. scope->__call = __call;
  1999. scope->__callstatic = __callstatic;
  2000. scope->__tostring = __tostring;
  2001. scope->__get = __get;
  2002. scope->__set = __set;
  2003. scope->__unset = __unset;
  2004. scope->__isset = __isset;
  2005. scope->__debugInfo = __debugInfo;
  2006. scope->serialize_func = serialize_func;
  2007. scope->unserialize_func = unserialize_func;
  2008. if (ctor) {
  2009. ctor->common.fn_flags |= ZEND_ACC_CTOR;
  2010. if (ctor->common.fn_flags & ZEND_ACC_STATIC) {
  2011. zend_error(error_type, "Constructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
  2012. }
  2013. }
  2014. if (dtor) {
  2015. if (dtor->common.fn_flags & ZEND_ACC_STATIC) {
  2016. zend_error(error_type, "Destructor %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
  2017. }
  2018. }
  2019. if (clone) {
  2020. if (clone->common.fn_flags & ZEND_ACC_STATIC) {
  2021. zend_error(error_type, "%s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
  2022. }
  2023. }
  2024. if (__call) {
  2025. if (__call->common.fn_flags & ZEND_ACC_STATIC) {
  2026. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__call->common.function_name));
  2027. }
  2028. }
  2029. if (__callstatic) {
  2030. if (!(__callstatic->common.fn_flags & ZEND_ACC_STATIC)) {
  2031. zend_error(error_type, "Method %s::%s() must be static", ZSTR_VAL(scope->name), ZSTR_VAL(__callstatic->common.function_name));
  2032. }
  2033. __callstatic->common.fn_flags |= ZEND_ACC_STATIC;
  2034. }
  2035. if (__tostring) {
  2036. if (__tostring->common.fn_flags & ZEND_ACC_STATIC) {
  2037. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__tostring->common.function_name));
  2038. }
  2039. }
  2040. if (__get) {
  2041. if (__get->common.fn_flags & ZEND_ACC_STATIC) {
  2042. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__get->common.function_name));
  2043. }
  2044. }
  2045. if (__set) {
  2046. if (__set->common.fn_flags & ZEND_ACC_STATIC) {
  2047. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__set->common.function_name));
  2048. }
  2049. }
  2050. if (__unset) {
  2051. if (__unset->common.fn_flags & ZEND_ACC_STATIC) {
  2052. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__unset->common.function_name));
  2053. }
  2054. }
  2055. if (__isset) {
  2056. if (__isset->common.fn_flags & ZEND_ACC_STATIC) {
  2057. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__isset->common.function_name));
  2058. }
  2059. }
  2060. if (__debugInfo) {
  2061. if (__debugInfo->common.fn_flags & ZEND_ACC_STATIC) {
  2062. zend_error(error_type, "Method %s::%s() cannot be static", ZSTR_VAL(scope->name), ZSTR_VAL(__debugInfo->common.function_name));
  2063. }
  2064. }
  2065. if (ctor && (ctor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2066. zend_error_noreturn(E_CORE_ERROR, "Constructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(ctor->common.function_name));
  2067. }
  2068. if (dtor && (dtor->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2069. zend_error_noreturn(E_CORE_ERROR, "Destructor %s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(dtor->common.function_name));
  2070. }
  2071. if (clone && (clone->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
  2072. zend_error_noreturn(E_CORE_ERROR, "%s::%s() cannot declare a return type", ZSTR_VAL(scope->name), ZSTR_VAL(clone->common.function_name));
  2073. }
  2074. efree((char*)lc_class_name);
  2075. }
  2076. return SUCCESS;
  2077. }
  2078. /* }}} */
  2079. /* count=-1 means erase all functions, otherwise,
  2080. * erase the first count functions
  2081. */
  2082. ZEND_API void zend_unregister_functions(const zend_function_entry *functions, int count, HashTable *function_table) /* {{{ */
  2083. {
  2084. const zend_function_entry *ptr = functions;
  2085. int i=0;
  2086. HashTable *target_function_table = function_table;
  2087. zend_string *lowercase_name;
  2088. size_t fname_len;
  2089. if (!target_function_table) {
  2090. target_function_table = CG(function_table);
  2091. }
  2092. while (ptr->fname) {
  2093. if (count!=-1 && i>=count) {
  2094. break;
  2095. }
  2096. fname_len = strlen(ptr->fname);
  2097. lowercase_name = zend_string_alloc(fname_len, 0);
  2098. zend_str_tolower_copy(ZSTR_VAL(lowercase_name), ptr->fname, fname_len);
  2099. zend_hash_del(target_function_table, lowercase_name);
  2100. zend_string_efree(lowercase_name);
  2101. ptr++;
  2102. i++;
  2103. }
  2104. }
  2105. /* }}} */
  2106. ZEND_API int zend_startup_module(zend_module_entry *module) /* {{{ */
  2107. {
  2108. if ((module = zend_register_internal_module(module)) != NULL && zend_startup_module_ex(module) == SUCCESS) {
  2109. return SUCCESS;
  2110. }
  2111. return FAILURE;
  2112. }
  2113. /* }}} */
  2114. ZEND_API int zend_get_module_started(const char *module_name) /* {{{ */
  2115. {
  2116. zend_module_entry *module;
  2117. module = zend_hash_str_find_ptr(&module_registry, module_name, strlen(module_name));
  2118. return (module && module->module_started) ? SUCCESS : FAILURE;
  2119. }
  2120. /* }}} */
  2121. static int clean_module_class(zval *el, void *arg) /* {{{ */
  2122. {
  2123. zend_class_entry *ce = (zend_class_entry *)Z_PTR_P(el);
  2124. int module_number = *(int *)arg;
  2125. if (ce->type == ZEND_INTERNAL_CLASS && ce->info.internal.module->module_number == module_number) {
  2126. return ZEND_HASH_APPLY_REMOVE;
  2127. } else {
  2128. return ZEND_HASH_APPLY_KEEP;
  2129. }
  2130. }
  2131. /* }}} */
  2132. static void clean_module_classes(int module_number) /* {{{ */
  2133. {
  2134. zend_hash_apply_with_argument(EG(class_table), clean_module_class, (void *) &module_number);
  2135. }
  2136. /* }}} */
  2137. void module_destructor(zend_module_entry *module) /* {{{ */
  2138. {
  2139. if (module->type == MODULE_TEMPORARY) {
  2140. zend_clean_module_rsrc_dtors(module->module_number);
  2141. clean_module_constants(module->module_number);
  2142. clean_module_classes(module->module_number);
  2143. }
  2144. if (module->module_started && module->module_shutdown_func) {
  2145. #if 0
  2146. zend_printf("%s: Module shutdown\n", module->name);
  2147. #endif
  2148. module->module_shutdown_func(module->type, module->module_number);
  2149. }
  2150. if (module->module_started
  2151. && !module->module_shutdown_func
  2152. && module->type == MODULE_TEMPORARY) {
  2153. zend_unregister_ini_entries(module->module_number);
  2154. }
  2155. /* Deinitilaise module globals */
  2156. if (module->globals_size) {
  2157. #ifdef ZTS
  2158. if (*module->globals_id_ptr) {
  2159. ts_free_id(*module->globals_id_ptr);
  2160. }
  2161. #else
  2162. if (module->globals_dtor) {
  2163. module->globals_dtor(module->globals_ptr);
  2164. }
  2165. #endif
  2166. }
  2167. module->module_started=0;
  2168. if (module->type == MODULE_TEMPORARY && module->functions) {
  2169. zend_unregister_functions(module->functions, -1, NULL);
  2170. }
  2171. #if HAVE_LIBDL
  2172. if (module->handle && !getenv("ZEND_DONT_UNLOAD_MODULES")) {
  2173. DL_UNLOAD(module->handle);
  2174. }
  2175. #endif
  2176. }
  2177. /* }}} */
  2178. ZEND_API void zend_activate_modules(void) /* {{{ */
  2179. {
  2180. zend_module_entry **p = module_request_startup_handlers;
  2181. while (*p) {
  2182. zend_module_entry *module = *p;
  2183. if (module->request_startup_func(module->type, module->module_number)==FAILURE) {
  2184. zend_error(E_WARNING, "request_startup() for %s module failed", module->name);
  2185. exit(1);
  2186. }
  2187. p++;
  2188. }
  2189. }
  2190. /* }}} */
  2191. ZEND_API void zend_deactivate_modules(void) /* {{{ */
  2192. {
  2193. EG(current_execute_data) = NULL; /* we're no longer executing anything */
  2194. zend_try {
  2195. if (EG(full_tables_cleanup)) {
  2196. zend_module_entry *module;
  2197. ZEND_HASH_REVERSE_FOREACH_PTR(&module_registry, module) {
  2198. if (module->request_shutdown_func) {
  2199. #if 0
  2200. zend_printf("%s: Request shutdown\n", module->name);
  2201. #endif
  2202. module->request_shutdown_func(module->type, module->module_number);
  2203. }
  2204. } ZEND_HASH_FOREACH_END();
  2205. } else {
  2206. zend_module_entry **p = module_request_shutdown_handlers;
  2207. while (*p) {
  2208. zend_module_entry *module = *p;
  2209. module->request_shutdown_func(module->type, module->module_number);
  2210. p++;
  2211. }
  2212. }
  2213. } zend_end_try();
  2214. }
  2215. /* }}} */
  2216. ZEND_API void zend_cleanup_internal_classes(void) /* {{{ */
  2217. {
  2218. zend_class_entry **p = class_cleanup_handlers;
  2219. while (*p) {
  2220. zend_cleanup_internal_class_data(*p);
  2221. p++;
  2222. }
  2223. }
  2224. /* }}} */
  2225. ZEND_API void zend_post_deactivate_modules(void) /* {{{ */
  2226. {
  2227. if (EG(full_tables_cleanup)) {
  2228. zend_module_entry *module;
  2229. zval *zv;
  2230. zend_string *key;
  2231. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  2232. if (module->post_deactivate_func) {
  2233. module->post_deactivate_func();
  2234. }
  2235. } ZEND_HASH_FOREACH_END();
  2236. ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(&module_registry, key, zv) {
  2237. module = Z_PTR_P(zv);
  2238. if (module->type != MODULE_TEMPORARY) {
  2239. break;
  2240. }
  2241. module_destructor(module);
  2242. free(module);
  2243. zend_string_release_ex(key, 0);
  2244. } ZEND_HASH_FOREACH_END_DEL();
  2245. } else {
  2246. zend_module_entry **p = module_post_deactivate_handlers;
  2247. while (*p) {
  2248. zend_module_entry *module = *p;
  2249. module->post_deactivate_func();
  2250. p++;
  2251. }
  2252. }
  2253. }
  2254. /* }}} */
  2255. /* return the next free module number */
  2256. ZEND_API int zend_next_free_module(void) /* {{{ */
  2257. {
  2258. return zend_hash_num_elements(&module_registry) + 1;
  2259. }
  2260. /* }}} */
  2261. static zend_class_entry *do_register_internal_class(zend_class_entry *orig_class_entry, uint32_t ce_flags) /* {{{ */
  2262. {
  2263. zend_class_entry *class_entry = malloc(sizeof(zend_class_entry));
  2264. zend_string *lowercase_name;
  2265. *class_entry = *orig_class_entry;
  2266. class_entry->type = ZEND_INTERNAL_CLASS;
  2267. zend_initialize_class_data(class_entry, 0);
  2268. class_entry->ce_flags = ce_flags | ZEND_ACC_CONSTANTS_UPDATED | ZEND_ACC_LINKED | ZEND_ACC_RESOLVED_PARENT | ZEND_ACC_RESOLVED_INTERFACES;
  2269. class_entry->info.internal.module = EG(current_module);
  2270. if (class_entry->info.internal.builtin_functions) {
  2271. zend_register_functions(class_entry, class_entry->info.internal.builtin_functions, &class_entry->function_table, EG(current_module)->type);
  2272. }
  2273. lowercase_name = zend_string_tolower_ex(orig_class_entry->name, EG(current_module)->type == MODULE_PERSISTENT);
  2274. lowercase_name = zend_new_interned_string(lowercase_name);
  2275. zend_hash_update_ptr(CG(class_table), lowercase_name, class_entry);
  2276. zend_string_release_ex(lowercase_name, 1);
  2277. return class_entry;
  2278. }
  2279. /* }}} */
  2280. /* If parent_ce is not NULL then it inherits from parent_ce
  2281. * If parent_ce is NULL and parent_name isn't then it looks for the parent and inherits from it
  2282. * If both parent_ce and parent_name are NULL it does a regular class registration
  2283. * If parent_name is specified but not found NULL is returned
  2284. */
  2285. ZEND_API zend_class_entry *zend_register_internal_class_ex(zend_class_entry *class_entry, zend_class_entry *parent_ce) /* {{{ */
  2286. {
  2287. zend_class_entry *register_class;
  2288. register_class = zend_register_internal_class(class_entry);
  2289. if (parent_ce) {
  2290. zend_do_inheritance(register_class, parent_ce);
  2291. zend_build_properties_info_table(register_class);
  2292. }
  2293. return register_class;
  2294. }
  2295. /* }}} */
  2296. ZEND_API void zend_class_implements(zend_class_entry *class_entry, int num_interfaces, ...) /* {{{ */
  2297. {
  2298. zend_class_entry *interface_entry;
  2299. va_list interface_list;
  2300. va_start(interface_list, num_interfaces);
  2301. while (num_interfaces--) {
  2302. interface_entry = va_arg(interface_list, zend_class_entry *);
  2303. zend_do_implement_interface(class_entry, interface_entry);
  2304. }
  2305. va_end(interface_list);
  2306. }
  2307. /* }}} */
  2308. /* A class that contains at least one abstract method automatically becomes an abstract class.
  2309. */
  2310. ZEND_API zend_class_entry *zend_register_internal_class(zend_class_entry *orig_class_entry) /* {{{ */
  2311. {
  2312. return do_register_internal_class(orig_class_entry, 0);
  2313. }
  2314. /* }}} */
  2315. ZEND_API zend_class_entry *zend_register_internal_interface(zend_class_entry *orig_class_entry) /* {{{ */
  2316. {
  2317. return do_register_internal_class(orig_class_entry, ZEND_ACC_INTERFACE);
  2318. }
  2319. /* }}} */
  2320. ZEND_API int zend_register_class_alias_ex(const char *name, size_t name_len, zend_class_entry *ce, int persistent) /* {{{ */
  2321. {
  2322. zend_string *lcname;
  2323. zval zv, *ret;
  2324. /* TODO: Move this out of here in 7.4. */
  2325. if (persistent && EG(current_module) && EG(current_module)->type == MODULE_TEMPORARY) {
  2326. persistent = 0;
  2327. }
  2328. if (name[0] == '\\') {
  2329. lcname = zend_string_alloc(name_len-1, persistent);
  2330. zend_str_tolower_copy(ZSTR_VAL(lcname), name+1, name_len-1);
  2331. } else {
  2332. lcname = zend_string_alloc(name_len, persistent);
  2333. zend_str_tolower_copy(ZSTR_VAL(lcname), name, name_len);
  2334. }
  2335. zend_assert_valid_class_name(lcname);
  2336. lcname = zend_new_interned_string(lcname);
  2337. ZVAL_ALIAS_PTR(&zv, ce);
  2338. ret = zend_hash_add(CG(class_table), lcname, &zv);
  2339. zend_string_release_ex(lcname, 0);
  2340. if (ret) {
  2341. if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
  2342. ce->refcount++;
  2343. }
  2344. return SUCCESS;
  2345. }
  2346. return FAILURE;
  2347. }
  2348. /* }}} */
  2349. ZEND_API int zend_set_hash_symbol(zval *symbol, const char *name, int name_length, zend_bool is_ref, int num_symbol_tables, ...) /* {{{ */
  2350. {
  2351. HashTable *symbol_table;
  2352. va_list symbol_table_list;
  2353. if (num_symbol_tables <= 0) return FAILURE;
  2354. if (is_ref) {
  2355. ZVAL_MAKE_REF(symbol);
  2356. }
  2357. va_start(symbol_table_list, num_symbol_tables);
  2358. while (num_symbol_tables-- > 0) {
  2359. symbol_table = va_arg(symbol_table_list, HashTable *);
  2360. zend_hash_str_update(symbol_table, name, name_length, symbol);
  2361. Z_TRY_ADDREF_P(symbol);
  2362. }
  2363. va_end(symbol_table_list);
  2364. return SUCCESS;
  2365. }
  2366. /* }}} */
  2367. /* Disabled functions support */
  2368. ZEND_API int zend_disable_function(char *function_name, size_t function_name_length) /* {{{ */
  2369. {
  2370. return zend_hash_str_del(CG(function_table), function_name, function_name_length);
  2371. }
  2372. /* }}} */
  2373. #ifdef ZEND_WIN32
  2374. #pragma optimize("", off)
  2375. #endif
  2376. static ZEND_COLD zend_object *display_disabled_class(zend_class_entry *class_type) /* {{{ */
  2377. {
  2378. zend_object *intern;
  2379. intern = zend_objects_new(class_type);
  2380. /* Initialize default properties */
  2381. if (EXPECTED(class_type->default_properties_count != 0)) {
  2382. zval *p = intern->properties_table;
  2383. zval *end = p + class_type->default_properties_count;
  2384. do {
  2385. ZVAL_UNDEF(p);
  2386. p++;
  2387. } while (p != end);
  2388. }
  2389. zend_error(E_WARNING, "%s() has been disabled for security reasons", ZSTR_VAL(class_type->name));
  2390. return intern;
  2391. }
  2392. #ifdef ZEND_WIN32
  2393. #pragma optimize("", on)
  2394. #endif
  2395. /* }}} */
  2396. static const zend_function_entry disabled_class_new[] = {
  2397. ZEND_FE_END
  2398. };
  2399. ZEND_API int zend_disable_class(char *class_name, size_t class_name_length) /* {{{ */
  2400. {
  2401. zend_class_entry *disabled_class;
  2402. zend_string *key;
  2403. zend_function *fn;
  2404. key = zend_string_alloc(class_name_length, 0);
  2405. zend_str_tolower_copy(ZSTR_VAL(key), class_name, class_name_length);
  2406. disabled_class = zend_hash_find_ptr(CG(class_table), key);
  2407. zend_string_release_ex(key, 0);
  2408. if (!disabled_class) {
  2409. return FAILURE;
  2410. }
  2411. INIT_CLASS_ENTRY_INIT_METHODS((*disabled_class), disabled_class_new);
  2412. disabled_class->create_object = display_disabled_class;
  2413. ZEND_HASH_FOREACH_PTR(&disabled_class->function_table, fn) {
  2414. if ((fn->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS)) &&
  2415. fn->common.scope == disabled_class) {
  2416. zend_free_internal_arg_info(&fn->internal_function);
  2417. }
  2418. } ZEND_HASH_FOREACH_END();
  2419. zend_hash_clean(&disabled_class->function_table);
  2420. return SUCCESS;
  2421. }
  2422. /* }}} */
  2423. static int zend_is_callable_check_class(zend_string *name, zend_class_entry *scope, zend_fcall_info_cache *fcc, int *strict_class, char **error) /* {{{ */
  2424. {
  2425. int ret = 0;
  2426. zend_class_entry *ce;
  2427. size_t name_len = ZSTR_LEN(name);
  2428. zend_string *lcname;
  2429. ALLOCA_FLAG(use_heap);
  2430. ZSTR_ALLOCA_ALLOC(lcname, name_len, use_heap);
  2431. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name), name_len);
  2432. *strict_class = 0;
  2433. if (zend_string_equals_literal(lcname, "self")) {
  2434. if (!scope) {
  2435. if (error) *error = estrdup("cannot access self:: when no class scope is active");
  2436. } else {
  2437. fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
  2438. fcc->calling_scope = scope;
  2439. if (!fcc->object) {
  2440. fcc->object = zend_get_this_object(EG(current_execute_data));
  2441. }
  2442. ret = 1;
  2443. }
  2444. } else if (zend_string_equals_literal(lcname, "parent")) {
  2445. if (!scope) {
  2446. if (error) *error = estrdup("cannot access parent:: when no class scope is active");
  2447. } else if (!scope->parent) {
  2448. if (error) *error = estrdup("cannot access parent:: when current class scope has no parent");
  2449. } else {
  2450. fcc->called_scope = zend_get_called_scope(EG(current_execute_data));
  2451. fcc->calling_scope = scope->parent;
  2452. if (!fcc->object) {
  2453. fcc->object = zend_get_this_object(EG(current_execute_data));
  2454. }
  2455. *strict_class = 1;
  2456. ret = 1;
  2457. }
  2458. } else if (zend_string_equals_literal(lcname, "static")) {
  2459. zend_class_entry *called_scope = zend_get_called_scope(EG(current_execute_data));
  2460. if (!called_scope) {
  2461. if (error) *error = estrdup("cannot access static:: when no class scope is active");
  2462. } else {
  2463. fcc->called_scope = called_scope;
  2464. fcc->calling_scope = called_scope;
  2465. if (!fcc->object) {
  2466. fcc->object = zend_get_this_object(EG(current_execute_data));
  2467. }
  2468. *strict_class = 1;
  2469. ret = 1;
  2470. }
  2471. } else if ((ce = zend_lookup_class(name)) != NULL) {
  2472. zend_class_entry *scope;
  2473. zend_execute_data *ex = EG(current_execute_data);
  2474. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  2475. ex = ex->prev_execute_data;
  2476. }
  2477. scope = ex ? ex->func->common.scope : NULL;
  2478. fcc->calling_scope = ce;
  2479. if (scope && !fcc->object) {
  2480. zend_object *object = zend_get_this_object(EG(current_execute_data));
  2481. if (object &&
  2482. instanceof_function(object->ce, scope) &&
  2483. instanceof_function(scope, ce)) {
  2484. fcc->object = object;
  2485. fcc->called_scope = object->ce;
  2486. } else {
  2487. fcc->called_scope = ce;
  2488. }
  2489. } else {
  2490. fcc->called_scope = fcc->object ? fcc->object->ce : ce;
  2491. }
  2492. *strict_class = 1;
  2493. ret = 1;
  2494. } else {
  2495. if (error) zend_spprintf(error, 0, "class '%.*s' not found", (int)name_len, ZSTR_VAL(name));
  2496. }
  2497. ZSTR_ALLOCA_FREE(lcname, use_heap);
  2498. return ret;
  2499. }
  2500. /* }}} */
  2501. ZEND_API void zend_release_fcall_info_cache(zend_fcall_info_cache *fcc) {
  2502. if (fcc->function_handler &&
  2503. (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  2504. if (fcc->function_handler->common.function_name) {
  2505. zend_string_release_ex(fcc->function_handler->common.function_name, 0);
  2506. }
  2507. zend_free_trampoline(fcc->function_handler);
  2508. }
  2509. fcc->function_handler = NULL;
  2510. }
  2511. static zend_always_inline int zend_is_callable_check_func(int check_flags, zval *callable, zend_fcall_info_cache *fcc, int strict_class, char **error) /* {{{ */
  2512. {
  2513. zend_class_entry *ce_org = fcc->calling_scope;
  2514. int retval = 0;
  2515. zend_string *mname, *cname;
  2516. zend_string *lmname;
  2517. const char *colon;
  2518. size_t clen;
  2519. HashTable *ftable;
  2520. int call_via_handler = 0;
  2521. zend_class_entry *scope;
  2522. zval *zv;
  2523. ALLOCA_FLAG(use_heap)
  2524. fcc->calling_scope = NULL;
  2525. if (!ce_org) {
  2526. zend_function *func;
  2527. zend_string *lmname;
  2528. /* Check if function with given name exists.
  2529. * This may be a compound name that includes namespace name */
  2530. if (UNEXPECTED(Z_STRVAL_P(callable)[0] == '\\')) {
  2531. /* Skip leading \ */
  2532. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable) - 1, use_heap);
  2533. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable) + 1, Z_STRLEN_P(callable) - 1);
  2534. func = zend_fetch_function(lmname);
  2535. ZSTR_ALLOCA_FREE(lmname, use_heap);
  2536. } else {
  2537. lmname = Z_STR_P(callable);
  2538. func = zend_fetch_function(lmname);
  2539. if (!func) {
  2540. ZSTR_ALLOCA_ALLOC(lmname, Z_STRLEN_P(callable), use_heap);
  2541. zend_str_tolower_copy(ZSTR_VAL(lmname), Z_STRVAL_P(callable), Z_STRLEN_P(callable));
  2542. func = zend_fetch_function(lmname);
  2543. ZSTR_ALLOCA_FREE(lmname, use_heap);
  2544. }
  2545. }
  2546. if (EXPECTED(func != NULL)) {
  2547. fcc->function_handler = func;
  2548. return 1;
  2549. }
  2550. }
  2551. /* Split name into class/namespace and method/function names */
  2552. if ((colon = zend_memrchr(Z_STRVAL_P(callable), ':', Z_STRLEN_P(callable))) != NULL &&
  2553. colon > Z_STRVAL_P(callable) &&
  2554. *(colon-1) == ':'
  2555. ) {
  2556. size_t mlen;
  2557. colon--;
  2558. clen = colon - Z_STRVAL_P(callable);
  2559. mlen = Z_STRLEN_P(callable) - clen - 2;
  2560. if (colon == Z_STRVAL_P(callable)) {
  2561. if (error) *error = estrdup("invalid function name");
  2562. return 0;
  2563. }
  2564. /* This is a compound name.
  2565. * Try to fetch class and then find static method. */
  2566. if (ce_org) {
  2567. scope = ce_org;
  2568. } else {
  2569. scope = zend_get_executed_scope();
  2570. }
  2571. cname = zend_string_init(Z_STRVAL_P(callable), clen, 0);
  2572. if (!zend_is_callable_check_class(cname, scope, fcc, &strict_class, error)) {
  2573. zend_string_release_ex(cname, 0);
  2574. return 0;
  2575. }
  2576. zend_string_release_ex(cname, 0);
  2577. ftable = &fcc->calling_scope->function_table;
  2578. if (ce_org && !instanceof_function(ce_org, fcc->calling_scope)) {
  2579. if (error) zend_spprintf(error, 0, "class '%s' is not a subclass of '%s'", ZSTR_VAL(ce_org->name), ZSTR_VAL(fcc->calling_scope->name));
  2580. return 0;
  2581. }
  2582. mname = zend_string_init(Z_STRVAL_P(callable) + clen + 2, mlen, 0);
  2583. } else if (ce_org) {
  2584. /* Try to fetch find static method of given class. */
  2585. mname = Z_STR_P(callable);
  2586. zend_string_addref(mname);
  2587. ftable = &ce_org->function_table;
  2588. fcc->calling_scope = ce_org;
  2589. } else {
  2590. /* We already checked for plain function before. */
  2591. if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  2592. zend_spprintf(error, 0, "function '%s' not found or invalid function name", Z_STRVAL_P(callable));
  2593. }
  2594. return 0;
  2595. }
  2596. lmname = zend_string_tolower(mname);
  2597. if (strict_class &&
  2598. fcc->calling_scope &&
  2599. zend_string_equals_literal(lmname, ZEND_CONSTRUCTOR_FUNC_NAME)) {
  2600. fcc->function_handler = fcc->calling_scope->constructor;
  2601. if (fcc->function_handler) {
  2602. retval = 1;
  2603. }
  2604. } else if ((zv = zend_hash_find(ftable, lmname)) != NULL) {
  2605. fcc->function_handler = Z_PTR_P(zv);
  2606. retval = 1;
  2607. if ((fcc->function_handler->op_array.fn_flags & ZEND_ACC_CHANGED) &&
  2608. !strict_class) {
  2609. scope = zend_get_executed_scope();
  2610. if (scope &&
  2611. instanceof_function(fcc->function_handler->common.scope, scope)) {
  2612. zv = zend_hash_find(&scope->function_table, lmname);
  2613. if (zv != NULL) {
  2614. zend_function *priv_fbc = Z_PTR_P(zv);
  2615. if (priv_fbc->common.fn_flags & ZEND_ACC_PRIVATE
  2616. && priv_fbc->common.scope == scope) {
  2617. fcc->function_handler = priv_fbc;
  2618. }
  2619. }
  2620. }
  2621. }
  2622. if (!(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC) &&
  2623. (fcc->calling_scope &&
  2624. ((fcc->object && fcc->calling_scope->__call) ||
  2625. (!fcc->object && fcc->calling_scope->__callstatic)))) {
  2626. scope = zend_get_executed_scope();
  2627. if (fcc->function_handler->common.scope != scope) {
  2628. if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
  2629. || !zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope)) {
  2630. retval = 0;
  2631. fcc->function_handler = NULL;
  2632. goto get_function_via_handler;
  2633. }
  2634. }
  2635. }
  2636. } else {
  2637. get_function_via_handler:
  2638. if (fcc->object && fcc->calling_scope == ce_org) {
  2639. if (strict_class && ce_org->__call) {
  2640. fcc->function_handler = zend_get_call_trampoline_func(ce_org, mname, 0);
  2641. call_via_handler = 1;
  2642. retval = 1;
  2643. } else {
  2644. fcc->function_handler = fcc->object->handlers->get_method(&fcc->object, mname, NULL);
  2645. if (fcc->function_handler) {
  2646. if (strict_class &&
  2647. (!fcc->function_handler->common.scope ||
  2648. !instanceof_function(ce_org, fcc->function_handler->common.scope))) {
  2649. zend_release_fcall_info_cache(fcc);
  2650. } else {
  2651. retval = 1;
  2652. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  2653. }
  2654. }
  2655. }
  2656. } else if (fcc->calling_scope) {
  2657. if (fcc->calling_scope->get_static_method) {
  2658. fcc->function_handler = fcc->calling_scope->get_static_method(fcc->calling_scope, mname);
  2659. } else {
  2660. fcc->function_handler = zend_std_get_static_method(fcc->calling_scope, mname, NULL);
  2661. }
  2662. if (fcc->function_handler) {
  2663. retval = 1;
  2664. call_via_handler = (fcc->function_handler->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  2665. if (call_via_handler && !fcc->object) {
  2666. zend_object *object = zend_get_this_object(EG(current_execute_data));
  2667. if (object &&
  2668. instanceof_function(object->ce, fcc->calling_scope)) {
  2669. fcc->object = object;
  2670. }
  2671. }
  2672. }
  2673. }
  2674. }
  2675. if (retval) {
  2676. if (fcc->calling_scope && !call_via_handler) {
  2677. if (fcc->function_handler->common.fn_flags & ZEND_ACC_ABSTRACT) {
  2678. retval = 0;
  2679. if (error) {
  2680. zend_spprintf(error, 0, "cannot call abstract method %s::%s()", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2681. }
  2682. } else if (!fcc->object && !(fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC)) {
  2683. retval = 0;
  2684. if (error) {
  2685. zend_spprintf(error, 0, "non-static method %s::%s() cannot be called statically", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2686. }
  2687. }
  2688. if (retval
  2689. && !(fcc->function_handler->common.fn_flags & ZEND_ACC_PUBLIC)) {
  2690. scope = zend_get_executed_scope();
  2691. if (fcc->function_handler->common.scope != scope) {
  2692. if ((fcc->function_handler->common.fn_flags & ZEND_ACC_PRIVATE)
  2693. || (!zend_check_protected(zend_get_function_root_class(fcc->function_handler), scope))) {
  2694. if (error) {
  2695. if (*error) {
  2696. efree(*error);
  2697. }
  2698. zend_spprintf(error, 0, "cannot access %s method %s::%s()", zend_visibility_string(fcc->function_handler->common.fn_flags), ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(fcc->function_handler->common.function_name));
  2699. }
  2700. retval = 0;
  2701. }
  2702. }
  2703. }
  2704. }
  2705. } else if (error && !(check_flags & IS_CALLABLE_CHECK_SILENT)) {
  2706. if (fcc->calling_scope) {
  2707. if (error) zend_spprintf(error, 0, "class '%s' does not have a method '%s'", ZSTR_VAL(fcc->calling_scope->name), ZSTR_VAL(mname));
  2708. } else {
  2709. if (error) zend_spprintf(error, 0, "function '%s' does not exist", ZSTR_VAL(mname));
  2710. }
  2711. }
  2712. zend_string_release_ex(lmname, 0);
  2713. zend_string_release_ex(mname, 0);
  2714. if (fcc->object) {
  2715. fcc->called_scope = fcc->object->ce;
  2716. if (fcc->function_handler
  2717. && fcc->function_handler->common.fn_flags & ZEND_ACC_STATIC) {
  2718. fcc->object = NULL;
  2719. }
  2720. }
  2721. return retval;
  2722. }
  2723. /* }}} */
  2724. ZEND_API zend_string *zend_get_callable_name_ex(zval *callable, zend_object *object) /* {{{ */
  2725. {
  2726. try_again:
  2727. switch (Z_TYPE_P(callable)) {
  2728. case IS_STRING:
  2729. if (object) {
  2730. return zend_create_member_string(object->ce->name, Z_STR_P(callable));
  2731. }
  2732. return zend_string_copy(Z_STR_P(callable));
  2733. case IS_ARRAY:
  2734. {
  2735. zval *method = NULL;
  2736. zval *obj = NULL;
  2737. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  2738. obj = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 0);
  2739. method = zend_hash_index_find_deref(Z_ARRVAL_P(callable), 1);
  2740. }
  2741. if (obj == NULL || method == NULL || Z_TYPE_P(method) != IS_STRING) {
  2742. return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
  2743. }
  2744. if (Z_TYPE_P(obj) == IS_STRING) {
  2745. return zend_create_member_string(Z_STR_P(obj), Z_STR_P(method));
  2746. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  2747. return zend_create_member_string(Z_OBJCE_P(obj)->name, Z_STR_P(method));
  2748. } else {
  2749. return ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED);
  2750. }
  2751. }
  2752. case IS_OBJECT:
  2753. {
  2754. zend_class_entry *ce = Z_OBJCE_P(callable);
  2755. return zend_string_concat2(
  2756. ZSTR_VAL(ce->name), ZSTR_LEN(ce->name),
  2757. "::__invoke", sizeof("::__invoke") - 1);
  2758. }
  2759. case IS_REFERENCE:
  2760. callable = Z_REFVAL_P(callable);
  2761. goto try_again;
  2762. default:
  2763. return zval_get_string_func(callable);
  2764. }
  2765. }
  2766. /* }}} */
  2767. ZEND_API zend_string *zend_get_callable_name(zval *callable) /* {{{ */
  2768. {
  2769. return zend_get_callable_name_ex(callable, NULL);
  2770. }
  2771. /* }}} */
  2772. static zend_always_inline zend_bool zend_is_callable_impl(zval *callable, zend_object *object, uint32_t check_flags, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  2773. {
  2774. zend_bool ret;
  2775. zend_fcall_info_cache fcc_local;
  2776. int strict_class = 0;
  2777. if (fcc == NULL) {
  2778. fcc = &fcc_local;
  2779. }
  2780. if (error) {
  2781. *error = NULL;
  2782. }
  2783. fcc->calling_scope = NULL;
  2784. fcc->called_scope = NULL;
  2785. fcc->function_handler = NULL;
  2786. fcc->object = NULL;
  2787. again:
  2788. switch (Z_TYPE_P(callable)) {
  2789. case IS_STRING:
  2790. if (object) {
  2791. fcc->object = object;
  2792. fcc->calling_scope = object->ce;
  2793. }
  2794. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  2795. fcc->called_scope = fcc->calling_scope;
  2796. return 1;
  2797. }
  2798. check_func:
  2799. ret = zend_is_callable_check_func(check_flags, callable, fcc, strict_class, error);
  2800. if (fcc == &fcc_local) {
  2801. zend_release_fcall_info_cache(fcc);
  2802. }
  2803. return ret;
  2804. case IS_ARRAY:
  2805. {
  2806. zval *method = NULL;
  2807. zval *obj = NULL;
  2808. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  2809. obj = zend_hash_index_find(Z_ARRVAL_P(callable), 0);
  2810. method = zend_hash_index_find(Z_ARRVAL_P(callable), 1);
  2811. }
  2812. do {
  2813. if (obj == NULL || method == NULL) {
  2814. break;
  2815. }
  2816. ZVAL_DEREF(method);
  2817. if (Z_TYPE_P(method) != IS_STRING) {
  2818. break;
  2819. }
  2820. ZVAL_DEREF(obj);
  2821. if (Z_TYPE_P(obj) == IS_STRING) {
  2822. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  2823. return 1;
  2824. }
  2825. if (!zend_is_callable_check_class(Z_STR_P(obj), zend_get_executed_scope(), fcc, &strict_class, error)) {
  2826. return 0;
  2827. }
  2828. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  2829. fcc->calling_scope = Z_OBJCE_P(obj); /* TBFixed: what if it's overloaded? */
  2830. fcc->object = Z_OBJ_P(obj);
  2831. if (check_flags & IS_CALLABLE_CHECK_SYNTAX_ONLY) {
  2832. fcc->called_scope = fcc->calling_scope;
  2833. return 1;
  2834. }
  2835. } else {
  2836. break;
  2837. }
  2838. callable = method;
  2839. goto check_func;
  2840. } while (0);
  2841. if (zend_hash_num_elements(Z_ARRVAL_P(callable)) == 2) {
  2842. if (!obj || (!Z_ISREF_P(obj)?
  2843. (Z_TYPE_P(obj) != IS_STRING && Z_TYPE_P(obj) != IS_OBJECT) :
  2844. (Z_TYPE_P(Z_REFVAL_P(obj)) != IS_STRING && Z_TYPE_P(Z_REFVAL_P(obj)) != IS_OBJECT))) {
  2845. if (error) *error = estrdup("first array member is not a valid class name or object");
  2846. } else {
  2847. if (error) *error = estrdup("second array member is not a valid method");
  2848. }
  2849. } else {
  2850. if (error) *error = estrdup("array must have exactly two members");
  2851. }
  2852. }
  2853. return 0;
  2854. case IS_OBJECT:
  2855. if (Z_OBJ_HANDLER_P(callable, get_closure) && Z_OBJ_HANDLER_P(callable, get_closure)(Z_OBJ_P(callable), &fcc->calling_scope, &fcc->function_handler, &fcc->object, 1) == SUCCESS) {
  2856. fcc->called_scope = fcc->calling_scope;
  2857. if (fcc == &fcc_local) {
  2858. zend_release_fcall_info_cache(fcc);
  2859. }
  2860. return 1;
  2861. }
  2862. if (error) *error = estrdup("no array or string given");
  2863. return 0;
  2864. case IS_REFERENCE:
  2865. callable = Z_REFVAL_P(callable);
  2866. goto again;
  2867. default:
  2868. if (error) *error = estrdup("no array or string given");
  2869. return 0;
  2870. }
  2871. }
  2872. /* }}} */
  2873. ZEND_API zend_bool zend_is_callable_ex(zval *callable, zend_object *object, uint32_t check_flags, zend_string **callable_name, zend_fcall_info_cache *fcc, char **error) /* {{{ */
  2874. {
  2875. zend_bool ret = zend_is_callable_impl(callable, object, check_flags, fcc, error);
  2876. if (callable_name) {
  2877. *callable_name = zend_get_callable_name_ex(callable, object);
  2878. }
  2879. return ret;
  2880. }
  2881. ZEND_API zend_bool zend_is_callable(zval *callable, uint32_t check_flags, zend_string **callable_name) /* {{{ */
  2882. {
  2883. return zend_is_callable_ex(callable, NULL, check_flags, callable_name, NULL, NULL);
  2884. }
  2885. /* }}} */
  2886. ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_name) /* {{{ */
  2887. {
  2888. zend_fcall_info_cache fcc;
  2889. if (zend_is_callable_ex(callable, NULL, 0, callable_name, &fcc, NULL)) {
  2890. if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
  2891. zval_ptr_dtor_str(callable);
  2892. array_init(callable);
  2893. add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
  2894. add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
  2895. }
  2896. zend_release_fcall_info_cache(&fcc);
  2897. return 1;
  2898. }
  2899. return 0;
  2900. }
  2901. /* }}} */
  2902. ZEND_API int zend_fcall_info_init(zval *callable, uint32_t check_flags, zend_fcall_info *fci, zend_fcall_info_cache *fcc, zend_string **callable_name, char **error) /* {{{ */
  2903. {
  2904. if (!zend_is_callable_ex(callable, NULL, check_flags, callable_name, fcc, error)) {
  2905. return FAILURE;
  2906. }
  2907. fci->size = sizeof(*fci);
  2908. fci->object = fcc->object;
  2909. ZVAL_COPY_VALUE(&fci->function_name, callable);
  2910. fci->retval = NULL;
  2911. fci->param_count = 0;
  2912. fci->params = NULL;
  2913. fci->no_separation = 1;
  2914. return SUCCESS;
  2915. }
  2916. /* }}} */
  2917. ZEND_API void zend_fcall_info_args_clear(zend_fcall_info *fci, int free_mem) /* {{{ */
  2918. {
  2919. if (fci->params) {
  2920. zval *p = fci->params;
  2921. zval *end = p + fci->param_count;
  2922. while (p != end) {
  2923. i_zval_ptr_dtor(p);
  2924. p++;
  2925. }
  2926. if (free_mem) {
  2927. efree(fci->params);
  2928. fci->params = NULL;
  2929. }
  2930. }
  2931. fci->param_count = 0;
  2932. }
  2933. /* }}} */
  2934. ZEND_API void zend_fcall_info_args_save(zend_fcall_info *fci, int *param_count, zval **params) /* {{{ */
  2935. {
  2936. *param_count = fci->param_count;
  2937. *params = fci->params;
  2938. fci->param_count = 0;
  2939. fci->params = NULL;
  2940. }
  2941. /* }}} */
  2942. ZEND_API void zend_fcall_info_args_restore(zend_fcall_info *fci, int param_count, zval *params) /* {{{ */
  2943. {
  2944. zend_fcall_info_args_clear(fci, 1);
  2945. fci->param_count = param_count;
  2946. fci->params = params;
  2947. }
  2948. /* }}} */
  2949. ZEND_API int zend_fcall_info_args_ex(zend_fcall_info *fci, zend_function *func, zval *args) /* {{{ */
  2950. {
  2951. zval *arg, *params;
  2952. uint32_t n = 1;
  2953. zend_fcall_info_args_clear(fci, !args);
  2954. if (!args) {
  2955. return SUCCESS;
  2956. }
  2957. if (Z_TYPE_P(args) != IS_ARRAY) {
  2958. return FAILURE;
  2959. }
  2960. fci->param_count = zend_hash_num_elements(Z_ARRVAL_P(args));
  2961. fci->params = params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  2962. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), arg) {
  2963. if (func && !Z_ISREF_P(arg) && ARG_SHOULD_BE_SENT_BY_REF(func, n)) {
  2964. ZVAL_NEW_REF(params, arg);
  2965. Z_TRY_ADDREF_P(arg);
  2966. } else {
  2967. ZVAL_COPY(params, arg);
  2968. }
  2969. params++;
  2970. n++;
  2971. } ZEND_HASH_FOREACH_END();
  2972. return SUCCESS;
  2973. }
  2974. /* }}} */
  2975. ZEND_API int zend_fcall_info_args(zend_fcall_info *fci, zval *args) /* {{{ */
  2976. {
  2977. return zend_fcall_info_args_ex(fci, NULL, args);
  2978. }
  2979. /* }}} */
  2980. ZEND_API int zend_fcall_info_argp(zend_fcall_info *fci, int argc, zval *argv) /* {{{ */
  2981. {
  2982. int i;
  2983. if (argc < 0) {
  2984. return FAILURE;
  2985. }
  2986. zend_fcall_info_args_clear(fci, !argc);
  2987. if (argc) {
  2988. fci->param_count = argc;
  2989. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  2990. for (i = 0; i < argc; ++i) {
  2991. ZVAL_COPY(&fci->params[i], &argv[i]);
  2992. }
  2993. }
  2994. return SUCCESS;
  2995. }
  2996. /* }}} */
  2997. ZEND_API int zend_fcall_info_argv(zend_fcall_info *fci, int argc, va_list *argv) /* {{{ */
  2998. {
  2999. int i;
  3000. zval *arg;
  3001. if (argc < 0) {
  3002. return FAILURE;
  3003. }
  3004. zend_fcall_info_args_clear(fci, !argc);
  3005. if (argc) {
  3006. fci->param_count = argc;
  3007. fci->params = (zval *) erealloc(fci->params, fci->param_count * sizeof(zval));
  3008. for (i = 0; i < argc; ++i) {
  3009. arg = va_arg(*argv, zval *);
  3010. ZVAL_COPY(&fci->params[i], arg);
  3011. }
  3012. }
  3013. return SUCCESS;
  3014. }
  3015. /* }}} */
  3016. ZEND_API int zend_fcall_info_argn(zend_fcall_info *fci, int argc, ...) /* {{{ */
  3017. {
  3018. int ret;
  3019. va_list argv;
  3020. va_start(argv, argc);
  3021. ret = zend_fcall_info_argv(fci, argc, &argv);
  3022. va_end(argv);
  3023. return ret;
  3024. }
  3025. /* }}} */
  3026. ZEND_API int zend_fcall_info_call(zend_fcall_info *fci, zend_fcall_info_cache *fcc, zval *retval_ptr, zval *args) /* {{{ */
  3027. {
  3028. zval retval, *org_params = NULL;
  3029. int result, org_count = 0;
  3030. fci->retval = retval_ptr ? retval_ptr : &retval;
  3031. if (args) {
  3032. zend_fcall_info_args_save(fci, &org_count, &org_params);
  3033. zend_fcall_info_args(fci, args);
  3034. }
  3035. result = zend_call_function(fci, fcc);
  3036. if (!retval_ptr && Z_TYPE(retval) != IS_UNDEF) {
  3037. zval_ptr_dtor(&retval);
  3038. }
  3039. if (args) {
  3040. zend_fcall_info_args_restore(fci, org_count, org_params);
  3041. }
  3042. return result;
  3043. }
  3044. /* }}} */
  3045. ZEND_API const char *zend_get_module_version(const char *module_name) /* {{{ */
  3046. {
  3047. zend_string *lname;
  3048. size_t name_len = strlen(module_name);
  3049. zend_module_entry *module;
  3050. lname = zend_string_alloc(name_len, 0);
  3051. zend_str_tolower_copy(ZSTR_VAL(lname), module_name, name_len);
  3052. module = zend_hash_find_ptr(&module_registry, lname);
  3053. zend_string_efree(lname);
  3054. return module ? module->version : NULL;
  3055. }
  3056. /* }}} */
  3057. static inline zend_string *zval_make_interned_string(zval *zv) /* {{{ */
  3058. {
  3059. ZEND_ASSERT(Z_TYPE_P(zv) == IS_STRING);
  3060. Z_STR_P(zv) = zend_new_interned_string(Z_STR_P(zv));
  3061. if (ZSTR_IS_INTERNED(Z_STR_P(zv))) {
  3062. Z_TYPE_FLAGS_P(zv) = 0;
  3063. }
  3064. return Z_STR_P(zv);
  3065. }
  3066. static zend_always_inline zend_bool is_persistent_class(zend_class_entry *ce) {
  3067. return (ce->type & ZEND_INTERNAL_CLASS)
  3068. && ce->info.internal.module->type == MODULE_PERSISTENT;
  3069. }
  3070. ZEND_API int zend_declare_typed_property(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment, zend_type type) /* {{{ */
  3071. {
  3072. zend_property_info *property_info, *property_info_ptr;
  3073. if (ZEND_TYPE_IS_SET(type)) {
  3074. ce->ce_flags |= ZEND_ACC_HAS_TYPE_HINTS;
  3075. }
  3076. if (ce->type == ZEND_INTERNAL_CLASS) {
  3077. property_info = pemalloc(sizeof(zend_property_info), 1);
  3078. } else {
  3079. property_info = zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
  3080. if (Z_TYPE_P(property) == IS_CONSTANT_AST) {
  3081. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3082. }
  3083. }
  3084. if (Z_TYPE_P(property) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(property))) {
  3085. zval_make_interned_string(property);
  3086. }
  3087. if (!(access_type & ZEND_ACC_PPP_MASK)) {
  3088. access_type |= ZEND_ACC_PUBLIC;
  3089. }
  3090. if (access_type & ZEND_ACC_STATIC) {
  3091. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3092. (property_info_ptr->flags & ZEND_ACC_STATIC) != 0) {
  3093. property_info->offset = property_info_ptr->offset;
  3094. zval_ptr_dtor(&ce->default_static_members_table[property_info->offset]);
  3095. zend_hash_del(&ce->properties_info, name);
  3096. } else {
  3097. property_info->offset = ce->default_static_members_count++;
  3098. ce->default_static_members_table = perealloc(ce->default_static_members_table, sizeof(zval) * ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
  3099. }
  3100. ZVAL_COPY_VALUE(&ce->default_static_members_table[property_info->offset], property);
  3101. if (!ZEND_MAP_PTR(ce->static_members_table)) {
  3102. ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
  3103. if (!EG(current_execute_data)) {
  3104. ZEND_MAP_PTR_NEW(ce->static_members_table);
  3105. } else {
  3106. /* internal class loaded by dl() */
  3107. ZEND_MAP_PTR_INIT(ce->static_members_table, &ce->default_static_members_table);
  3108. }
  3109. }
  3110. } else {
  3111. zval *property_default_ptr;
  3112. if ((property_info_ptr = zend_hash_find_ptr(&ce->properties_info, name)) != NULL &&
  3113. (property_info_ptr->flags & ZEND_ACC_STATIC) == 0) {
  3114. property_info->offset = property_info_ptr->offset;
  3115. zval_ptr_dtor(&ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)]);
  3116. zend_hash_del(&ce->properties_info, name);
  3117. ZEND_ASSERT(ce->type == ZEND_INTERNAL_CLASS);
  3118. ZEND_ASSERT(ce->properties_info_table != NULL);
  3119. ce->properties_info_table[OBJ_PROP_TO_NUM(property_info->offset)] = property_info;
  3120. } else {
  3121. property_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
  3122. ce->default_properties_count++;
  3123. ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
  3124. /* For user classes this is handled during linking */
  3125. if (ce->type == ZEND_INTERNAL_CLASS) {
  3126. ce->properties_info_table = perealloc(ce->properties_info_table, sizeof(zend_property_info *) * ce->default_properties_count, 1);
  3127. ce->properties_info_table[ce->default_properties_count - 1] = property_info;
  3128. }
  3129. }
  3130. property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
  3131. ZVAL_COPY_VALUE(property_default_ptr, property);
  3132. Z_PROP_FLAG_P(property_default_ptr) = Z_ISUNDEF_P(property) ? IS_PROP_UNINIT : 0;
  3133. }
  3134. if (ce->type & ZEND_INTERNAL_CLASS) {
  3135. switch(Z_TYPE_P(property)) {
  3136. case IS_ARRAY:
  3137. case IS_OBJECT:
  3138. case IS_RESOURCE:
  3139. zend_error_noreturn(E_CORE_ERROR, "Internal zval's can't be arrays, objects or resources");
  3140. break;
  3141. default:
  3142. break;
  3143. }
  3144. /* Must be interned to avoid ZTS data races */
  3145. if (is_persistent_class(ce)) {
  3146. name = zend_new_interned_string(zend_string_copy(name));
  3147. }
  3148. }
  3149. if (access_type & ZEND_ACC_PUBLIC) {
  3150. property_info->name = zend_string_copy(name);
  3151. } else if (access_type & ZEND_ACC_PRIVATE) {
  3152. property_info->name = zend_mangle_property_name(ZSTR_VAL(ce->name), ZSTR_LEN(ce->name), ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3153. } else {
  3154. ZEND_ASSERT(access_type & ZEND_ACC_PROTECTED);
  3155. property_info->name = zend_mangle_property_name("*", 1, ZSTR_VAL(name), ZSTR_LEN(name), is_persistent_class(ce));
  3156. }
  3157. property_info->name = zend_new_interned_string(property_info->name);
  3158. property_info->flags = access_type;
  3159. property_info->doc_comment = doc_comment;
  3160. property_info->ce = ce;
  3161. property_info->type = type;
  3162. zend_hash_update_ptr(&ce->properties_info, name, property_info);
  3163. return SUCCESS;
  3164. }
  3165. /* }}} */
  3166. ZEND_API int zend_try_assign_typed_ref_ex(zend_reference *ref, zval *val, zend_bool strict) /* {{{ */
  3167. {
  3168. if (UNEXPECTED(!zend_verify_ref_assignable_zval(ref, val, strict))) {
  3169. zval_ptr_dtor(val);
  3170. return FAILURE;
  3171. } else {
  3172. zval_ptr_dtor(&ref->val);
  3173. ZVAL_COPY_VALUE(&ref->val, val);
  3174. return SUCCESS;
  3175. }
  3176. }
  3177. /* }}} */
  3178. ZEND_API int zend_try_assign_typed_ref(zend_reference *ref, zval *val) /* {{{ */
  3179. {
  3180. return zend_try_assign_typed_ref_ex(ref, val, ZEND_ARG_USES_STRICT_TYPES());
  3181. }
  3182. /* }}} */
  3183. ZEND_API int zend_try_assign_typed_ref_null(zend_reference *ref) /* {{{ */
  3184. {
  3185. zval tmp;
  3186. ZVAL_NULL(&tmp);
  3187. return zend_try_assign_typed_ref(ref, &tmp);
  3188. }
  3189. /* }}} */
  3190. ZEND_API int zend_try_assign_typed_ref_bool(zend_reference *ref, zend_bool val) /* {{{ */
  3191. {
  3192. zval tmp;
  3193. ZVAL_BOOL(&tmp, val);
  3194. return zend_try_assign_typed_ref(ref, &tmp);
  3195. }
  3196. /* }}} */
  3197. ZEND_API int zend_try_assign_typed_ref_long(zend_reference *ref, zend_long lval) /* {{{ */
  3198. {
  3199. zval tmp;
  3200. ZVAL_LONG(&tmp, lval);
  3201. return zend_try_assign_typed_ref(ref, &tmp);
  3202. }
  3203. /* }}} */
  3204. ZEND_API int zend_try_assign_typed_ref_double(zend_reference *ref, double dval) /* {{{ */
  3205. {
  3206. zval tmp;
  3207. ZVAL_DOUBLE(&tmp, dval);
  3208. return zend_try_assign_typed_ref(ref, &tmp);
  3209. }
  3210. /* }}} */
  3211. ZEND_API int zend_try_assign_typed_ref_empty_string(zend_reference *ref) /* {{{ */
  3212. {
  3213. zval tmp;
  3214. ZVAL_EMPTY_STRING(&tmp);
  3215. return zend_try_assign_typed_ref(ref, &tmp);
  3216. }
  3217. /* }}} */
  3218. ZEND_API int zend_try_assign_typed_ref_str(zend_reference *ref, zend_string *str) /* {{{ */
  3219. {
  3220. zval tmp;
  3221. ZVAL_STR(&tmp, str);
  3222. return zend_try_assign_typed_ref(ref, &tmp);
  3223. }
  3224. /* }}} */
  3225. ZEND_API int zend_try_assign_typed_ref_string(zend_reference *ref, const char *string) /* {{{ */
  3226. {
  3227. zval tmp;
  3228. ZVAL_STRING(&tmp, string);
  3229. return zend_try_assign_typed_ref(ref, &tmp);
  3230. }
  3231. /* }}} */
  3232. ZEND_API int zend_try_assign_typed_ref_stringl(zend_reference *ref, const char *string, size_t len) /* {{{ */
  3233. {
  3234. zval tmp;
  3235. ZVAL_STRINGL(&tmp, string, len);
  3236. return zend_try_assign_typed_ref(ref, &tmp);
  3237. }
  3238. /* }}} */
  3239. ZEND_API int zend_try_assign_typed_ref_arr(zend_reference *ref, zend_array *arr) /* {{{ */
  3240. {
  3241. zval tmp;
  3242. ZVAL_ARR(&tmp, arr);
  3243. return zend_try_assign_typed_ref(ref, &tmp);
  3244. }
  3245. /* }}} */
  3246. ZEND_API int zend_try_assign_typed_ref_res(zend_reference *ref, zend_resource *res) /* {{{ */
  3247. {
  3248. zval tmp;
  3249. ZVAL_RES(&tmp, res);
  3250. return zend_try_assign_typed_ref(ref, &tmp);
  3251. }
  3252. /* }}} */
  3253. ZEND_API int zend_try_assign_typed_ref_zval(zend_reference *ref, zval *zv) /* {{{ */
  3254. {
  3255. zval tmp;
  3256. ZVAL_COPY_VALUE(&tmp, zv);
  3257. return zend_try_assign_typed_ref(ref, &tmp);
  3258. }
  3259. /* }}} */
  3260. ZEND_API int zend_try_assign_typed_ref_zval_ex(zend_reference *ref, zval *zv, zend_bool strict) /* {{{ */
  3261. {
  3262. zval tmp;
  3263. ZVAL_COPY_VALUE(&tmp, zv);
  3264. return zend_try_assign_typed_ref_ex(ref, &tmp, strict);
  3265. }
  3266. /* }}} */
  3267. ZEND_API int zend_declare_property_ex(zend_class_entry *ce, zend_string *name, zval *property, int access_type, zend_string *doc_comment) /* {{{ */
  3268. {
  3269. return zend_declare_typed_property(ce, name, property, access_type, doc_comment, (zend_type) ZEND_TYPE_INIT_NONE(0));
  3270. }
  3271. /* }}} */
  3272. ZEND_API int zend_declare_property(zend_class_entry *ce, const char *name, size_t name_length, zval *property, int access_type) /* {{{ */
  3273. {
  3274. zend_string *key = zend_string_init(name, name_length, is_persistent_class(ce));
  3275. int ret = zend_declare_property_ex(ce, key, property, access_type, NULL);
  3276. zend_string_release(key);
  3277. return ret;
  3278. }
  3279. /* }}} */
  3280. ZEND_API int zend_declare_property_null(zend_class_entry *ce, const char *name, size_t name_length, int access_type) /* {{{ */
  3281. {
  3282. zval property;
  3283. ZVAL_NULL(&property);
  3284. return zend_declare_property(ce, name, name_length, &property, access_type);
  3285. }
  3286. /* }}} */
  3287. ZEND_API int zend_declare_property_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3288. {
  3289. zval property;
  3290. ZVAL_BOOL(&property, value);
  3291. return zend_declare_property(ce, name, name_length, &property, access_type);
  3292. }
  3293. /* }}} */
  3294. ZEND_API int zend_declare_property_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value, int access_type) /* {{{ */
  3295. {
  3296. zval property;
  3297. ZVAL_LONG(&property, value);
  3298. return zend_declare_property(ce, name, name_length, &property, access_type);
  3299. }
  3300. /* }}} */
  3301. ZEND_API int zend_declare_property_double(zend_class_entry *ce, const char *name, size_t name_length, double value, int access_type) /* {{{ */
  3302. {
  3303. zval property;
  3304. ZVAL_DOUBLE(&property, value);
  3305. return zend_declare_property(ce, name, name_length, &property, access_type);
  3306. }
  3307. /* }}} */
  3308. ZEND_API int zend_declare_property_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value, int access_type) /* {{{ */
  3309. {
  3310. zval property;
  3311. ZVAL_NEW_STR(&property, zend_string_init(value, strlen(value), ce->type & ZEND_INTERNAL_CLASS));
  3312. return zend_declare_property(ce, name, name_length, &property, access_type);
  3313. }
  3314. /* }}} */
  3315. ZEND_API int zend_declare_property_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_len, int access_type) /* {{{ */
  3316. {
  3317. zval property;
  3318. ZVAL_NEW_STR(&property, zend_string_init(value, value_len, ce->type & ZEND_INTERNAL_CLASS));
  3319. return zend_declare_property(ce, name, name_length, &property, access_type);
  3320. }
  3321. /* }}} */
  3322. ZEND_API int zend_declare_class_constant_ex(zend_class_entry *ce, zend_string *name, zval *value, int access_type, zend_string *doc_comment) /* {{{ */
  3323. {
  3324. zend_class_constant *c;
  3325. if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  3326. if (access_type != ZEND_ACC_PUBLIC) {
  3327. zend_error_noreturn(E_COMPILE_ERROR, "Access type for interface constant %s::%s must be public", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3328. }
  3329. }
  3330. if (zend_string_equals_literal_ci(name, "class")) {
  3331. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3332. "A class constant must not be called 'class'; it is reserved for class name fetching");
  3333. }
  3334. if (Z_TYPE_P(value) == IS_STRING && !ZSTR_IS_INTERNED(Z_STR_P(value))) {
  3335. zval_make_interned_string(value);
  3336. }
  3337. if (ce->type == ZEND_INTERNAL_CLASS) {
  3338. c = pemalloc(sizeof(zend_class_constant), 1);
  3339. } else {
  3340. c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
  3341. }
  3342. ZVAL_COPY_VALUE(&c->value, value);
  3343. Z_ACCESS_FLAGS(c->value) = access_type;
  3344. c->doc_comment = doc_comment;
  3345. c->ce = ce;
  3346. if (Z_TYPE_P(value) == IS_CONSTANT_AST) {
  3347. ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
  3348. }
  3349. if (!zend_hash_add_ptr(&ce->constants_table, name, c)) {
  3350. zend_error_noreturn(ce->type == ZEND_INTERNAL_CLASS ? E_CORE_ERROR : E_COMPILE_ERROR,
  3351. "Cannot redefine class constant %s::%s", ZSTR_VAL(ce->name), ZSTR_VAL(name));
  3352. }
  3353. return SUCCESS;
  3354. }
  3355. /* }}} */
  3356. ZEND_API int zend_declare_class_constant(zend_class_entry *ce, const char *name, size_t name_length, zval *value) /* {{{ */
  3357. {
  3358. int ret;
  3359. zend_string *key;
  3360. if (ce->type == ZEND_INTERNAL_CLASS) {
  3361. key = zend_string_init_interned(name, name_length, 1);
  3362. } else {
  3363. key = zend_string_init(name, name_length, 0);
  3364. }
  3365. ret = zend_declare_class_constant_ex(ce, key, value, ZEND_ACC_PUBLIC, NULL);
  3366. zend_string_release(key);
  3367. return ret;
  3368. }
  3369. /* }}} */
  3370. ZEND_API int zend_declare_class_constant_null(zend_class_entry *ce, const char *name, size_t name_length) /* {{{ */
  3371. {
  3372. zval constant;
  3373. ZVAL_NULL(&constant);
  3374. return zend_declare_class_constant(ce, name, name_length, &constant);
  3375. }
  3376. /* }}} */
  3377. ZEND_API int zend_declare_class_constant_long(zend_class_entry *ce, const char *name, size_t name_length, zend_long value) /* {{{ */
  3378. {
  3379. zval constant;
  3380. ZVAL_LONG(&constant, value);
  3381. return zend_declare_class_constant(ce, name, name_length, &constant);
  3382. }
  3383. /* }}} */
  3384. ZEND_API int zend_declare_class_constant_bool(zend_class_entry *ce, const char *name, size_t name_length, zend_bool value) /* {{{ */
  3385. {
  3386. zval constant;
  3387. ZVAL_BOOL(&constant, value);
  3388. return zend_declare_class_constant(ce, name, name_length, &constant);
  3389. }
  3390. /* }}} */
  3391. ZEND_API int zend_declare_class_constant_double(zend_class_entry *ce, const char *name, size_t name_length, double value) /* {{{ */
  3392. {
  3393. zval constant;
  3394. ZVAL_DOUBLE(&constant, value);
  3395. return zend_declare_class_constant(ce, name, name_length, &constant);
  3396. }
  3397. /* }}} */
  3398. ZEND_API int zend_declare_class_constant_stringl(zend_class_entry *ce, const char *name, size_t name_length, const char *value, size_t value_length) /* {{{ */
  3399. {
  3400. zval constant;
  3401. ZVAL_NEW_STR(&constant, zend_string_init(value, value_length, ce->type & ZEND_INTERNAL_CLASS));
  3402. return zend_declare_class_constant(ce, name, name_length, &constant);
  3403. }
  3404. /* }}} */
  3405. ZEND_API int zend_declare_class_constant_string(zend_class_entry *ce, const char *name, size_t name_length, const char *value) /* {{{ */
  3406. {
  3407. return zend_declare_class_constant_stringl(ce, name, name_length, value, strlen(value));
  3408. }
  3409. /* }}} */
  3410. ZEND_API void zend_update_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zval *value) /* {{{ */
  3411. {
  3412. zend_class_entry *old_scope = EG(fake_scope);
  3413. EG(fake_scope) = scope;
  3414. Z_OBJ_HT_P(object)->write_property(Z_OBJ_P(object), name, value, NULL);
  3415. EG(fake_scope) = old_scope;
  3416. }
  3417. /* }}} */
  3418. ZEND_API void zend_update_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zval *value) /* {{{ */
  3419. {
  3420. zend_string *property;
  3421. zend_class_entry *old_scope = EG(fake_scope);
  3422. EG(fake_scope) = scope;
  3423. property = zend_string_init(name, name_length, 0);
  3424. Z_OBJ_HT_P(object)->write_property(Z_OBJ_P(object), property, value, NULL);
  3425. zend_string_release_ex(property, 0);
  3426. EG(fake_scope) = old_scope;
  3427. }
  3428. /* }}} */
  3429. ZEND_API void zend_update_property_null(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
  3430. {
  3431. zval tmp;
  3432. ZVAL_NULL(&tmp);
  3433. zend_update_property(scope, object, name, name_length, &tmp);
  3434. }
  3435. /* }}} */
  3436. ZEND_API void zend_unset_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length) /* {{{ */
  3437. {
  3438. zend_string *property;
  3439. zend_class_entry *old_scope = EG(fake_scope);
  3440. EG(fake_scope) = scope;
  3441. property = zend_string_init(name, name_length, 0);
  3442. Z_OBJ_HT_P(object)->unset_property(Z_OBJ_P(object), property, 0);
  3443. zend_string_release_ex(property, 0);
  3444. EG(fake_scope) = old_scope;
  3445. }
  3446. /* }}} */
  3447. ZEND_API void zend_update_property_bool(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3448. {
  3449. zval tmp;
  3450. ZVAL_BOOL(&tmp, value);
  3451. zend_update_property(scope, object, name, name_length, &tmp);
  3452. }
  3453. /* }}} */
  3454. ZEND_API void zend_update_property_long(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_long value) /* {{{ */
  3455. {
  3456. zval tmp;
  3457. ZVAL_LONG(&tmp, value);
  3458. zend_update_property(scope, object, name, name_length, &tmp);
  3459. }
  3460. /* }}} */
  3461. ZEND_API void zend_update_property_double(zend_class_entry *scope, zval *object, const char *name, size_t name_length, double value) /* {{{ */
  3462. {
  3463. zval tmp;
  3464. ZVAL_DOUBLE(&tmp, value);
  3465. zend_update_property(scope, object, name, name_length, &tmp);
  3466. }
  3467. /* }}} */
  3468. ZEND_API void zend_update_property_str(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_string *value) /* {{{ */
  3469. {
  3470. zval tmp;
  3471. ZVAL_STR(&tmp, value);
  3472. zend_update_property(scope, object, name, name_length, &tmp);
  3473. }
  3474. /* }}} */
  3475. ZEND_API void zend_update_property_string(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value) /* {{{ */
  3476. {
  3477. zval tmp;
  3478. ZVAL_STRING(&tmp, value);
  3479. Z_SET_REFCOUNT(tmp, 0);
  3480. zend_update_property(scope, object, name, name_length, &tmp);
  3481. }
  3482. /* }}} */
  3483. ZEND_API void zend_update_property_stringl(zend_class_entry *scope, zval *object, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  3484. {
  3485. zval tmp;
  3486. ZVAL_STRINGL(&tmp, value, value_len);
  3487. Z_SET_REFCOUNT(tmp, 0);
  3488. zend_update_property(scope, object, name, name_length, &tmp);
  3489. }
  3490. /* }}} */
  3491. ZEND_API int zend_update_static_property_ex(zend_class_entry *scope, zend_string *name, zval *value) /* {{{ */
  3492. {
  3493. zval *property, tmp;
  3494. zend_property_info *prop_info;
  3495. zend_class_entry *old_scope = EG(fake_scope);
  3496. if (UNEXPECTED(!(scope->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  3497. if (UNEXPECTED(zend_update_class_constants(scope)) != SUCCESS) {
  3498. return FAILURE;
  3499. }
  3500. }
  3501. EG(fake_scope) = scope;
  3502. property = zend_std_get_static_property_with_info(scope, name, BP_VAR_W, &prop_info);
  3503. EG(fake_scope) = old_scope;
  3504. if (!property) {
  3505. return FAILURE;
  3506. }
  3507. ZEND_ASSERT(!Z_ISREF_P(value));
  3508. Z_TRY_ADDREF_P(value);
  3509. if (ZEND_TYPE_IS_SET(prop_info->type)) {
  3510. ZVAL_COPY_VALUE(&tmp, value);
  3511. if (!zend_verify_property_type(prop_info, &tmp, /* strict */ 0)) {
  3512. Z_TRY_DELREF_P(value);
  3513. return FAILURE;
  3514. }
  3515. value = &tmp;
  3516. }
  3517. zend_assign_to_variable(property, value, IS_TMP_VAR, /* strict */ 0);
  3518. return SUCCESS;
  3519. }
  3520. /* }}} */
  3521. ZEND_API int zend_update_static_property(zend_class_entry *scope, const char *name, size_t name_length, zval *value) /* {{{ */
  3522. {
  3523. zend_string *key = zend_string_init(name, name_length, 0);
  3524. int retval = zend_update_static_property_ex(scope, key, value);
  3525. zend_string_efree(key);
  3526. return retval;
  3527. }
  3528. /* }}} */
  3529. ZEND_API int zend_update_static_property_null(zend_class_entry *scope, const char *name, size_t name_length) /* {{{ */
  3530. {
  3531. zval tmp;
  3532. ZVAL_NULL(&tmp);
  3533. return zend_update_static_property(scope, name, name_length, &tmp);
  3534. }
  3535. /* }}} */
  3536. ZEND_API int zend_update_static_property_bool(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  3537. {
  3538. zval tmp;
  3539. ZVAL_BOOL(&tmp, value);
  3540. return zend_update_static_property(scope, name, name_length, &tmp);
  3541. }
  3542. /* }}} */
  3543. ZEND_API int zend_update_static_property_long(zend_class_entry *scope, const char *name, size_t name_length, zend_long value) /* {{{ */
  3544. {
  3545. zval tmp;
  3546. ZVAL_LONG(&tmp, value);
  3547. return zend_update_static_property(scope, name, name_length, &tmp);
  3548. }
  3549. /* }}} */
  3550. ZEND_API int zend_update_static_property_double(zend_class_entry *scope, const char *name, size_t name_length, double value) /* {{{ */
  3551. {
  3552. zval tmp;
  3553. ZVAL_DOUBLE(&tmp, value);
  3554. return zend_update_static_property(scope, name, name_length, &tmp);
  3555. }
  3556. /* }}} */
  3557. ZEND_API int zend_update_static_property_string(zend_class_entry *scope, const char *name, size_t name_length, const char *value) /* {{{ */
  3558. {
  3559. zval tmp;
  3560. ZVAL_STRING(&tmp, value);
  3561. Z_SET_REFCOUNT(tmp, 0);
  3562. return zend_update_static_property(scope, name, name_length, &tmp);
  3563. }
  3564. /* }}} */
  3565. ZEND_API int zend_update_static_property_stringl(zend_class_entry *scope, const char *name, size_t name_length, const char *value, size_t value_len) /* {{{ */
  3566. {
  3567. zval tmp;
  3568. ZVAL_STRINGL(&tmp, value, value_len);
  3569. Z_SET_REFCOUNT(tmp, 0);
  3570. return zend_update_static_property(scope, name, name_length, &tmp);
  3571. }
  3572. /* }}} */
  3573. ZEND_API zval *zend_read_property_ex(zend_class_entry *scope, zval *object, zend_string *name, zend_bool silent, zval *rv) /* {{{ */
  3574. {
  3575. zval *value;
  3576. zend_class_entry *old_scope = EG(fake_scope);
  3577. EG(fake_scope) = scope;
  3578. value = Z_OBJ_HT_P(object)->read_property(Z_OBJ_P(object), name, silent?BP_VAR_IS:BP_VAR_R, NULL, rv);
  3579. EG(fake_scope) = old_scope;
  3580. return value;
  3581. }
  3582. /* }}} */
  3583. ZEND_API zval *zend_read_property(zend_class_entry *scope, zval *object, const char *name, size_t name_length, zend_bool silent, zval *rv) /* {{{ */
  3584. {
  3585. zval *value;
  3586. zend_string *str;
  3587. str = zend_string_init(name, name_length, 0);
  3588. value = zend_read_property_ex(scope, object, str, silent, rv);
  3589. zend_string_release_ex(str, 0);
  3590. return value;
  3591. }
  3592. /* }}} */
  3593. ZEND_API zval *zend_read_static_property_ex(zend_class_entry *scope, zend_string *name, zend_bool silent) /* {{{ */
  3594. {
  3595. zval *property;
  3596. zend_class_entry *old_scope = EG(fake_scope);
  3597. EG(fake_scope) = scope;
  3598. property = zend_std_get_static_property(scope, name, silent ? BP_VAR_IS : BP_VAR_R);
  3599. EG(fake_scope) = old_scope;
  3600. return property;
  3601. }
  3602. /* }}} */
  3603. ZEND_API zval *zend_read_static_property(zend_class_entry *scope, const char *name, size_t name_length, zend_bool silent) /* {{{ */
  3604. {
  3605. zend_string *key = zend_string_init(name, name_length, 0);
  3606. zval *property = zend_read_static_property_ex(scope, key, silent);
  3607. zend_string_efree(key);
  3608. return property;
  3609. }
  3610. /* }}} */
  3611. ZEND_API void zend_save_error_handling(zend_error_handling *current) /* {{{ */
  3612. {
  3613. current->handling = EG(error_handling);
  3614. current->exception = EG(exception_class);
  3615. ZVAL_COPY(&current->user_handler, &EG(user_error_handler));
  3616. }
  3617. /* }}} */
  3618. ZEND_API void zend_replace_error_handling(zend_error_handling_t error_handling, zend_class_entry *exception_class, zend_error_handling *current) /* {{{ */
  3619. {
  3620. if (current) {
  3621. zend_save_error_handling(current);
  3622. if (error_handling != EH_NORMAL && Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  3623. zval_ptr_dtor(&EG(user_error_handler));
  3624. ZVAL_UNDEF(&EG(user_error_handler));
  3625. }
  3626. }
  3627. EG(error_handling) = error_handling;
  3628. EG(exception_class) = error_handling == EH_THROW ? exception_class : NULL;
  3629. }
  3630. /* }}} */
  3631. static int same_zval(zval *zv1, zval *zv2) /* {{{ */
  3632. {
  3633. if (Z_TYPE_P(zv1) != Z_TYPE_P(zv2)) {
  3634. return 0;
  3635. }
  3636. switch (Z_TYPE_P(zv1)) {
  3637. case IS_UNDEF:
  3638. case IS_NULL:
  3639. case IS_FALSE:
  3640. case IS_TRUE:
  3641. return 1;
  3642. case IS_LONG:
  3643. return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
  3644. case IS_DOUBLE:
  3645. return Z_LVAL_P(zv1) == Z_LVAL_P(zv2);
  3646. case IS_STRING:
  3647. case IS_ARRAY:
  3648. case IS_OBJECT:
  3649. case IS_RESOURCE:
  3650. return Z_COUNTED_P(zv1) == Z_COUNTED_P(zv2);
  3651. default:
  3652. return 0;
  3653. }
  3654. }
  3655. /* }}} */
  3656. ZEND_API void zend_restore_error_handling(zend_error_handling *saved) /* {{{ */
  3657. {
  3658. EG(error_handling) = saved->handling;
  3659. EG(exception_class) = saved->handling == EH_THROW ? saved->exception : NULL;
  3660. if (Z_TYPE(saved->user_handler) != IS_UNDEF
  3661. && !same_zval(&saved->user_handler, &EG(user_error_handler))) {
  3662. zval_ptr_dtor(&EG(user_error_handler));
  3663. ZVAL_COPY_VALUE(&EG(user_error_handler), &saved->user_handler);
  3664. } else if (Z_TYPE(saved->user_handler)) {
  3665. zval_ptr_dtor(&saved->user_handler);
  3666. }
  3667. ZVAL_UNDEF(&saved->user_handler);
  3668. }
  3669. /* }}} */
  3670. ZEND_API ZEND_COLD const char *zend_get_object_type(const zend_class_entry *ce) /* {{{ */
  3671. {
  3672. if(ce->ce_flags & ZEND_ACC_TRAIT) {
  3673. return "trait";
  3674. } else if (ce->ce_flags & ZEND_ACC_INTERFACE) {
  3675. return "interface";
  3676. } else {
  3677. return "class";
  3678. }
  3679. }
  3680. /* }}} */
  3681. ZEND_API zend_bool zend_is_iterable(zval *iterable) /* {{{ */
  3682. {
  3683. switch (Z_TYPE_P(iterable)) {
  3684. case IS_ARRAY:
  3685. return 1;
  3686. case IS_OBJECT:
  3687. return zend_class_implements_interface(Z_OBJCE_P(iterable), zend_ce_traversable);
  3688. default:
  3689. return 0;
  3690. }
  3691. }
  3692. /* }}} */
  3693. ZEND_API zend_bool zend_is_countable(zval *countable) /* {{{ */
  3694. {
  3695. switch (Z_TYPE_P(countable)) {
  3696. case IS_ARRAY:
  3697. return 1;
  3698. case IS_OBJECT:
  3699. if (Z_OBJ_HT_P(countable)->count_elements) {
  3700. return 1;
  3701. }
  3702. return zend_class_implements_interface(Z_OBJCE_P(countable), zend_ce_countable);
  3703. default:
  3704. return 0;
  3705. }
  3706. }
  3707. /* }}} */
  3708. static int get_default_via_ast(zval *default_value_zval, const char *default_value) {
  3709. zend_ast *ast;
  3710. zend_arena *ast_arena;
  3711. zend_string *code = zend_string_concat3(
  3712. "<?php ", sizeof("<?php ") - 1, default_value, strlen(default_value), ";", 1);
  3713. ast = zend_compile_string_to_ast(code, &ast_arena, "");
  3714. zend_string_release(code);
  3715. if (!ast) {
  3716. return FAILURE;
  3717. }
  3718. zend_ast_list *statement_list = zend_ast_get_list(ast);
  3719. zend_ast *const_expression_ast = statement_list->child[0];
  3720. zend_arena *original_ast_arena = CG(ast_arena);
  3721. uint32_t original_compiler_options = CG(compiler_options);
  3722. zend_file_context original_file_context;
  3723. CG(ast_arena) = ast_arena;
  3724. /* Disable constant substitution, to make getDefaultValueConstant() work. */
  3725. CG(compiler_options) |= ZEND_COMPILE_NO_CONSTANT_SUBSTITUTION | ZEND_COMPILE_NO_PERSISTENT_CONSTANT_SUBSTITUTION;
  3726. zend_file_context_begin(&original_file_context);
  3727. zend_const_expr_to_zval(default_value_zval, const_expression_ast);
  3728. CG(ast_arena) = original_ast_arena;
  3729. CG(compiler_options) = original_compiler_options;
  3730. zend_file_context_end(&original_file_context);
  3731. zend_ast_destroy(ast);
  3732. zend_arena_destroy(ast_arena);
  3733. return SUCCESS;
  3734. }
  3735. static zend_string *try_parse_string(const char *str, size_t len, char quote) {
  3736. if (len == 0) {
  3737. return ZSTR_EMPTY_ALLOC();
  3738. }
  3739. for (size_t i = 0; i < len; i++) {
  3740. if (str[i] == '\\' || str[i] == quote) {
  3741. return NULL;
  3742. }
  3743. }
  3744. return zend_string_init(str, len, 0);
  3745. }
  3746. ZEND_API int zend_get_default_from_internal_arg_info(
  3747. zval *default_value_zval, zend_internal_arg_info *arg_info)
  3748. {
  3749. const char *default_value = arg_info->default_value;
  3750. if (!default_value) {
  3751. return FAILURE;
  3752. }
  3753. /* Avoid going through the full AST machinery for some simple and common cases. */
  3754. size_t default_value_len = strlen(default_value);
  3755. zend_ulong lval;
  3756. if (default_value_len == sizeof("null")-1
  3757. && !memcmp(default_value, "null", sizeof("null")-1)) {
  3758. ZVAL_NULL(default_value_zval);
  3759. return SUCCESS;
  3760. } else if (default_value_len == sizeof("true")-1
  3761. && !memcmp(default_value, "true", sizeof("true")-1)) {
  3762. ZVAL_TRUE(default_value_zval);
  3763. return SUCCESS;
  3764. } else if (default_value_len == sizeof("false")-1
  3765. && !memcmp(default_value, "false", sizeof("false")-1)) {
  3766. ZVAL_FALSE(default_value_zval);
  3767. return SUCCESS;
  3768. } else if (default_value_len >= 2
  3769. && (default_value[0] == '\'' || default_value[0] == '"')
  3770. && default_value[default_value_len - 1] == default_value[0]) {
  3771. zend_string *str = try_parse_string(
  3772. default_value + 1, default_value_len - 2, default_value[0]);
  3773. if (str) {
  3774. ZVAL_STR(default_value_zval, str);
  3775. return SUCCESS;
  3776. }
  3777. } else if (default_value_len == sizeof("[]")-1
  3778. && !memcmp(default_value, "[]", sizeof("[]")-1)) {
  3779. ZVAL_EMPTY_ARRAY(default_value_zval);
  3780. return SUCCESS;
  3781. } else if (ZEND_HANDLE_NUMERIC_STR(default_value, default_value_len, lval)) {
  3782. ZVAL_LONG(default_value_zval, lval);
  3783. return SUCCESS;
  3784. }
  3785. #if 0
  3786. fprintf(stderr, "Evaluating %s via AST\n", default_value);
  3787. #endif
  3788. return get_default_via_ast(default_value_zval, default_value);
  3789. }