PageRenderTime 39ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/Zend/zend_execute_API.c

http://github.com/php/php-src
C | 1606 lines | 1400 code | 131 blank | 75 comment | 181 complexity | d6d808012381dc277ec781f5b8e5fcd5 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. | Dmitry Stogov <dmitry@php.net> |
  18. +----------------------------------------------------------------------+
  19. */
  20. #include <stdio.h>
  21. #include <signal.h>
  22. #include "zend.h"
  23. #include "zend_compile.h"
  24. #include "zend_execute.h"
  25. #include "zend_API.h"
  26. #include "zend_stack.h"
  27. #include "zend_constants.h"
  28. #include "zend_extensions.h"
  29. #include "zend_exceptions.h"
  30. #include "zend_closures.h"
  31. #include "zend_generators.h"
  32. #include "zend_vm.h"
  33. #include "zend_float.h"
  34. #include "zend_weakrefs.h"
  35. #include "zend_inheritance.h"
  36. #ifdef HAVE_SYS_TIME_H
  37. #include <sys/time.h>
  38. #endif
  39. #ifdef HAVE_UNISTD_H
  40. #include <unistd.h>
  41. #endif
  42. ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data);
  43. ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data, zval *return_value);
  44. /* true globals */
  45. ZEND_API const zend_fcall_info empty_fcall_info = {0};
  46. ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { NULL, NULL, NULL, NULL };
  47. #ifdef ZEND_WIN32
  48. ZEND_TLS HANDLE tq_timer = NULL;
  49. #endif
  50. #if 0&&ZEND_DEBUG
  51. static void (*original_sigsegv_handler)(int);
  52. static void zend_handle_sigsegv(int dummy) /* {{{ */
  53. {
  54. fflush(stdout);
  55. fflush(stderr);
  56. if (original_sigsegv_handler == zend_handle_sigsegv) {
  57. signal(SIGSEGV, original_sigsegv_handler);
  58. } else {
  59. signal(SIGSEGV, SIG_DFL);
  60. }
  61. {
  62. fprintf(stderr, "SIGSEGV caught on opcode %d on opline %d of %s() at %s:%d\n\n",
  63. active_opline->opcode,
  64. active_opline-EG(active_op_array)->opcodes,
  65. get_active_function_name(),
  66. zend_get_executed_filename(),
  67. zend_get_executed_lineno());
  68. /* See http://support.microsoft.com/kb/190351 */
  69. #ifdef ZEND_WIN32
  70. fflush(stderr);
  71. #endif
  72. }
  73. if (original_sigsegv_handler!=zend_handle_sigsegv) {
  74. original_sigsegv_handler(dummy);
  75. }
  76. }
  77. /* }}} */
  78. #endif
  79. static void zend_extension_activator(zend_extension *extension) /* {{{ */
  80. {
  81. if (extension->activate) {
  82. extension->activate();
  83. }
  84. }
  85. /* }}} */
  86. static void zend_extension_deactivator(zend_extension *extension) /* {{{ */
  87. {
  88. if (extension->deactivate) {
  89. extension->deactivate();
  90. }
  91. }
  92. /* }}} */
  93. static int clean_non_persistent_constant_full(zval *zv) /* {{{ */
  94. {
  95. zend_constant *c = Z_PTR_P(zv);
  96. return (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  97. }
  98. /* }}} */
  99. static int clean_non_persistent_function_full(zval *zv) /* {{{ */
  100. {
  101. zend_function *function = Z_PTR_P(zv);
  102. return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  103. }
  104. /* }}} */
  105. static int clean_non_persistent_class_full(zval *zv) /* {{{ */
  106. {
  107. zend_class_entry *ce = Z_PTR_P(zv);
  108. return (ce->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  109. }
  110. /* }}} */
  111. void init_executor(void) /* {{{ */
  112. {
  113. zend_init_fpu();
  114. ZVAL_NULL(&EG(uninitialized_zval));
  115. ZVAL_ERROR(&EG(error_zval));
  116. /* destroys stack frame, therefore makes core dumps worthless */
  117. #if 0&&ZEND_DEBUG
  118. original_sigsegv_handler = signal(SIGSEGV, zend_handle_sigsegv);
  119. #endif
  120. EG(symtable_cache_ptr) = EG(symtable_cache);
  121. EG(symtable_cache_limit) = EG(symtable_cache) + SYMTABLE_CACHE_SIZE;
  122. EG(no_extensions) = 0;
  123. EG(function_table) = CG(function_table);
  124. EG(class_table) = CG(class_table);
  125. EG(in_autoload) = NULL;
  126. EG(autoload_func) = NULL;
  127. EG(error_handling) = EH_NORMAL;
  128. EG(flags) = EG_FLAGS_INITIAL;
  129. zend_vm_stack_init();
  130. zend_hash_init(&EG(symbol_table), 64, NULL, ZVAL_PTR_DTOR, 0);
  131. zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_activator);
  132. zend_hash_init(&EG(included_files), 8, NULL, NULL, 0);
  133. EG(ticks_count) = 0;
  134. ZVAL_UNDEF(&EG(user_error_handler));
  135. ZVAL_UNDEF(&EG(user_exception_handler));
  136. EG(current_execute_data) = NULL;
  137. zend_stack_init(&EG(user_error_handlers_error_reporting), sizeof(int));
  138. zend_stack_init(&EG(user_error_handlers), sizeof(zval));
  139. zend_stack_init(&EG(user_exception_handlers), sizeof(zval));
  140. zend_objects_store_init(&EG(objects_store), 1024);
  141. EG(full_tables_cleanup) = 0;
  142. EG(vm_interrupt) = 0;
  143. EG(timed_out) = 0;
  144. EG(exception) = NULL;
  145. EG(prev_exception) = NULL;
  146. EG(fake_scope) = NULL;
  147. EG(trampoline).common.function_name = NULL;
  148. EG(ht_iterators_count) = sizeof(EG(ht_iterators_slots)) / sizeof(HashTableIterator);
  149. EG(ht_iterators_used) = 0;
  150. EG(ht_iterators) = EG(ht_iterators_slots);
  151. memset(EG(ht_iterators), 0, sizeof(EG(ht_iterators_slots)));
  152. EG(persistent_constants_count) = EG(zend_constants)->nNumUsed;
  153. EG(persistent_functions_count) = EG(function_table)->nNumUsed;
  154. EG(persistent_classes_count) = EG(class_table)->nNumUsed;
  155. EG(get_gc_buffer).start = EG(get_gc_buffer).end = EG(get_gc_buffer).cur = NULL;
  156. zend_weakrefs_init();
  157. EG(active) = 1;
  158. }
  159. /* }}} */
  160. static int zval_call_destructor(zval *zv) /* {{{ */
  161. {
  162. if (Z_TYPE_P(zv) == IS_INDIRECT) {
  163. zv = Z_INDIRECT_P(zv);
  164. }
  165. if (Z_TYPE_P(zv) == IS_OBJECT && Z_REFCOUNT_P(zv) == 1) {
  166. return ZEND_HASH_APPLY_REMOVE;
  167. } else {
  168. return ZEND_HASH_APPLY_KEEP;
  169. }
  170. }
  171. /* }}} */
  172. static void zend_unclean_zval_ptr_dtor(zval *zv) /* {{{ */
  173. {
  174. if (Z_TYPE_P(zv) == IS_INDIRECT) {
  175. zv = Z_INDIRECT_P(zv);
  176. }
  177. i_zval_ptr_dtor(zv);
  178. }
  179. /* }}} */
  180. static ZEND_COLD void zend_throw_or_error(int fetch_type, zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
  181. {
  182. va_list va;
  183. char *message = NULL;
  184. va_start(va, format);
  185. zend_vspprintf(&message, 0, format, va);
  186. if (fetch_type & ZEND_FETCH_CLASS_EXCEPTION) {
  187. zend_throw_error(exception_ce, "%s", message);
  188. } else {
  189. zend_error(E_ERROR, "%s", message);
  190. }
  191. efree(message);
  192. va_end(va);
  193. }
  194. /* }}} */
  195. void shutdown_destructors(void) /* {{{ */
  196. {
  197. if (CG(unclean_shutdown)) {
  198. EG(symbol_table).pDestructor = zend_unclean_zval_ptr_dtor;
  199. }
  200. zend_try {
  201. uint32_t symbols;
  202. do {
  203. symbols = zend_hash_num_elements(&EG(symbol_table));
  204. zend_hash_reverse_apply(&EG(symbol_table), (apply_func_t) zval_call_destructor);
  205. } while (symbols != zend_hash_num_elements(&EG(symbol_table)));
  206. zend_objects_store_call_destructors(&EG(objects_store));
  207. } zend_catch {
  208. /* if we couldn't destruct cleanly, mark all objects as destructed anyway */
  209. zend_objects_store_mark_destructed(&EG(objects_store));
  210. } zend_end_try();
  211. }
  212. /* }}} */
  213. void shutdown_executor(void) /* {{{ */
  214. {
  215. zend_string *key;
  216. zval *zv;
  217. #if ZEND_DEBUG
  218. zend_bool fast_shutdown = 0;
  219. #else
  220. zend_bool fast_shutdown = is_zend_mm() && !EG(full_tables_cleanup);
  221. #endif
  222. zend_try {
  223. zend_llist_destroy(&CG(open_files));
  224. } zend_end_try();
  225. EG(flags) |= EG_FLAGS_IN_RESOURCE_SHUTDOWN;
  226. zend_try {
  227. zend_close_rsrc_list(&EG(regular_list));
  228. } zend_end_try();
  229. /* No PHP callback functions should be called after this point. */
  230. EG(active) = 0;
  231. if (!fast_shutdown) {
  232. zend_hash_graceful_reverse_destroy(&EG(symbol_table));
  233. /* Release static properties and static variables prior to the final GC run,
  234. * as they may hold GC roots. */
  235. ZEND_HASH_REVERSE_FOREACH_VAL(EG(function_table), zv) {
  236. zend_op_array *op_array = Z_PTR_P(zv);
  237. if (op_array->type == ZEND_INTERNAL_FUNCTION) {
  238. break;
  239. }
  240. if (op_array->static_variables) {
  241. HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr);
  242. if (ht) {
  243. ZEND_ASSERT(GC_REFCOUNT(ht) == 1);
  244. zend_array_destroy(ht);
  245. ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
  246. }
  247. }
  248. } ZEND_HASH_FOREACH_END();
  249. ZEND_HASH_REVERSE_FOREACH_VAL(EG(class_table), zv) {
  250. zend_class_entry *ce = Z_PTR_P(zv);
  251. if (ce->default_static_members_count) {
  252. zend_cleanup_internal_class_data(ce);
  253. }
  254. if (ce->ce_flags & ZEND_HAS_STATIC_IN_METHODS) {
  255. zend_op_array *op_array;
  256. ZEND_HASH_FOREACH_PTR(&ce->function_table, op_array) {
  257. if (op_array->type == ZEND_USER_FUNCTION) {
  258. if (op_array->static_variables) {
  259. HashTable *ht = ZEND_MAP_PTR_GET(op_array->static_variables_ptr);
  260. if (ht) {
  261. zend_array_release(ht);
  262. ZEND_MAP_PTR_SET(op_array->static_variables_ptr, NULL);
  263. }
  264. }
  265. }
  266. } ZEND_HASH_FOREACH_END();
  267. }
  268. } ZEND_HASH_FOREACH_END();
  269. /* Also release error and exception handlers, which may hold objects. */
  270. if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  271. zval_ptr_dtor(&EG(user_error_handler));
  272. ZVAL_UNDEF(&EG(user_error_handler));
  273. }
  274. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  275. zval_ptr_dtor(&EG(user_exception_handler));
  276. ZVAL_UNDEF(&EG(user_exception_handler));
  277. }
  278. zend_stack_clean(&EG(user_error_handlers_error_reporting), NULL, 1);
  279. zend_stack_clean(&EG(user_error_handlers), (void (*)(void *))ZVAL_PTR_DTOR, 1);
  280. zend_stack_clean(&EG(user_exception_handlers), (void (*)(void *))ZVAL_PTR_DTOR, 1);
  281. #if ZEND_DEBUG
  282. if (gc_enabled() && !CG(unclean_shutdown)) {
  283. gc_collect_cycles();
  284. }
  285. #endif
  286. }
  287. zend_objects_store_free_object_storage(&EG(objects_store), fast_shutdown);
  288. zend_weakrefs_shutdown();
  289. zend_try {
  290. zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_deactivator);
  291. } zend_end_try();
  292. if (fast_shutdown) {
  293. /* Fast Request Shutdown
  294. * =====================
  295. * Zend Memory Manager frees memory by its own. We don't have to free
  296. * each allocated block separately.
  297. */
  298. zend_hash_discard(EG(zend_constants), EG(persistent_constants_count));
  299. zend_hash_discard(EG(function_table), EG(persistent_functions_count));
  300. zend_hash_discard(EG(class_table), EG(persistent_classes_count));
  301. zend_cleanup_internal_classes();
  302. } else {
  303. zend_vm_stack_destroy();
  304. if (EG(full_tables_cleanup)) {
  305. zend_hash_reverse_apply(EG(zend_constants), clean_non_persistent_constant_full);
  306. zend_hash_reverse_apply(EG(function_table), clean_non_persistent_function_full);
  307. zend_hash_reverse_apply(EG(class_table), clean_non_persistent_class_full);
  308. } else {
  309. ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(EG(zend_constants), key, zv) {
  310. zend_constant *c = Z_PTR_P(zv);
  311. if (_idx == EG(persistent_constants_count)) {
  312. break;
  313. }
  314. zval_ptr_dtor_nogc(&c->value);
  315. if (c->name) {
  316. zend_string_release_ex(c->name, 0);
  317. }
  318. efree(c);
  319. zend_string_release_ex(key, 0);
  320. } ZEND_HASH_FOREACH_END_DEL();
  321. ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(EG(function_table), key, zv) {
  322. zend_function *func = Z_PTR_P(zv);
  323. if (_idx == EG(persistent_functions_count)) {
  324. break;
  325. }
  326. destroy_op_array(&func->op_array);
  327. zend_string_release_ex(key, 0);
  328. } ZEND_HASH_FOREACH_END_DEL();
  329. ZEND_HASH_REVERSE_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
  330. if (_idx == EG(persistent_classes_count)) {
  331. break;
  332. }
  333. destroy_zend_class(zv);
  334. zend_string_release_ex(key, 0);
  335. } ZEND_HASH_FOREACH_END_DEL();
  336. }
  337. while (EG(symtable_cache_ptr) > EG(symtable_cache)) {
  338. EG(symtable_cache_ptr)--;
  339. zend_hash_destroy(*EG(symtable_cache_ptr));
  340. FREE_HASHTABLE(*EG(symtable_cache_ptr));
  341. }
  342. zend_hash_destroy(&EG(included_files));
  343. zend_stack_destroy(&EG(user_error_handlers_error_reporting));
  344. zend_stack_destroy(&EG(user_error_handlers));
  345. zend_stack_destroy(&EG(user_exception_handlers));
  346. zend_objects_store_destroy(&EG(objects_store));
  347. if (EG(in_autoload)) {
  348. zend_hash_destroy(EG(in_autoload));
  349. FREE_HASHTABLE(EG(in_autoload));
  350. }
  351. if (EG(ht_iterators) != EG(ht_iterators_slots)) {
  352. efree(EG(ht_iterators));
  353. }
  354. }
  355. #if ZEND_DEBUG
  356. if (EG(ht_iterators_used) && !CG(unclean_shutdown)) {
  357. zend_error(E_WARNING, "Leaked %" PRIu32 " hashtable iterators", EG(ht_iterators_used));
  358. }
  359. #endif
  360. EG(ht_iterators_used) = 0;
  361. zend_shutdown_fpu();
  362. }
  363. /* }}} */
  364. /* return class name and "::" or "". */
  365. ZEND_API const char *get_active_class_name(const char **space) /* {{{ */
  366. {
  367. zend_function *func;
  368. if (!zend_is_executing()) {
  369. if (space) {
  370. *space = "";
  371. }
  372. return "";
  373. }
  374. func = EG(current_execute_data)->func;
  375. switch (func->type) {
  376. case ZEND_USER_FUNCTION:
  377. case ZEND_INTERNAL_FUNCTION:
  378. {
  379. zend_class_entry *ce = func->common.scope;
  380. if (space) {
  381. *space = ce ? "::" : "";
  382. }
  383. return ce ? ZSTR_VAL(ce->name) : "";
  384. }
  385. default:
  386. if (space) {
  387. *space = "";
  388. }
  389. return "";
  390. }
  391. }
  392. /* }}} */
  393. ZEND_API const char *get_active_function_name(void) /* {{{ */
  394. {
  395. zend_function *func;
  396. if (!zend_is_executing()) {
  397. return NULL;
  398. }
  399. func = EG(current_execute_data)->func;
  400. switch (func->type) {
  401. case ZEND_USER_FUNCTION: {
  402. zend_string *function_name = func->common.function_name;
  403. if (function_name) {
  404. return ZSTR_VAL(function_name);
  405. } else {
  406. return "main";
  407. }
  408. }
  409. break;
  410. case ZEND_INTERNAL_FUNCTION:
  411. return ZSTR_VAL(func->common.function_name);
  412. break;
  413. default:
  414. return NULL;
  415. }
  416. }
  417. /* }}} */
  418. ZEND_API const char *get_active_function_arg_name(uint32_t arg_num) /* {{{ */
  419. {
  420. zend_function *func;
  421. if (!zend_is_executing()) {
  422. return NULL;
  423. }
  424. func = EG(current_execute_data)->func;
  425. return get_function_arg_name(func, arg_num);
  426. }
  427. /* }}} */
  428. ZEND_API const char *get_function_arg_name(const zend_function *func, uint32_t arg_num) /* {{{ */
  429. {
  430. if (!func || func->common.num_args < arg_num) {
  431. return NULL;
  432. }
  433. switch (func->type) {
  434. case ZEND_USER_FUNCTION:
  435. return ZSTR_VAL(func->common.arg_info[arg_num - 1].name);
  436. case ZEND_INTERNAL_FUNCTION:
  437. return ((zend_internal_arg_info*) func->common.arg_info)[arg_num - 1].name;
  438. default:
  439. return NULL;
  440. }
  441. }
  442. /* }}} */
  443. ZEND_API const char *zend_get_executed_filename(void) /* {{{ */
  444. {
  445. zend_execute_data *ex = EG(current_execute_data);
  446. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  447. ex = ex->prev_execute_data;
  448. }
  449. if (ex) {
  450. return ZSTR_VAL(ex->func->op_array.filename);
  451. } else {
  452. return "[no active file]";
  453. }
  454. }
  455. /* }}} */
  456. ZEND_API zend_string *zend_get_executed_filename_ex(void) /* {{{ */
  457. {
  458. zend_execute_data *ex = EG(current_execute_data);
  459. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  460. ex = ex->prev_execute_data;
  461. }
  462. if (ex) {
  463. return ex->func->op_array.filename;
  464. } else {
  465. return NULL;
  466. }
  467. }
  468. /* }}} */
  469. ZEND_API uint32_t zend_get_executed_lineno(void) /* {{{ */
  470. {
  471. zend_execute_data *ex = EG(current_execute_data);
  472. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  473. ex = ex->prev_execute_data;
  474. }
  475. if (ex) {
  476. if (EG(exception) && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
  477. ex->opline->lineno == 0 && EG(opline_before_exception)) {
  478. return EG(opline_before_exception)->lineno;
  479. }
  480. return ex->opline->lineno;
  481. } else {
  482. return 0;
  483. }
  484. }
  485. /* }}} */
  486. ZEND_API zend_class_entry *zend_get_executed_scope(void) /* {{{ */
  487. {
  488. zend_execute_data *ex = EG(current_execute_data);
  489. while (1) {
  490. if (!ex) {
  491. return NULL;
  492. } else if (ex->func && (ZEND_USER_CODE(ex->func->type) || ex->func->common.scope)) {
  493. return ex->func->common.scope;
  494. }
  495. ex = ex->prev_execute_data;
  496. }
  497. }
  498. /* }}} */
  499. ZEND_API zend_bool zend_is_executing(void) /* {{{ */
  500. {
  501. return EG(current_execute_data) != 0;
  502. }
  503. /* }}} */
  504. ZEND_API int zval_update_constant_ex(zval *p, zend_class_entry *scope) /* {{{ */
  505. {
  506. if (Z_TYPE_P(p) == IS_CONSTANT_AST) {
  507. zend_ast *ast = Z_ASTVAL_P(p);
  508. if (ast->kind == ZEND_AST_CONSTANT) {
  509. zend_string *name = zend_ast_get_constant_name(ast);
  510. zval *zv = zend_get_constant_ex(name, scope, ast->attr);
  511. if (UNEXPECTED(zv == NULL)) {
  512. return FAILURE;
  513. }
  514. zval_ptr_dtor_nogc(p);
  515. ZVAL_COPY_OR_DUP(p, zv);
  516. } else {
  517. zval tmp;
  518. if (UNEXPECTED(zend_ast_evaluate(&tmp, ast, scope) != SUCCESS)) {
  519. return FAILURE;
  520. }
  521. zval_ptr_dtor_nogc(p);
  522. ZVAL_COPY_VALUE(p, &tmp);
  523. }
  524. }
  525. return SUCCESS;
  526. }
  527. /* }}} */
  528. ZEND_API int zval_update_constant(zval *pp) /* {{{ */
  529. {
  530. return zval_update_constant_ex(pp, EG(current_execute_data) ? zend_get_executed_scope() : CG(active_class_entry));
  531. }
  532. /* }}} */
  533. int _call_user_function_ex(zval *object, zval *function_name, zval *retval_ptr, uint32_t param_count, zval params[], int no_separation) /* {{{ */
  534. {
  535. zend_fcall_info fci;
  536. fci.size = sizeof(fci);
  537. fci.object = object ? Z_OBJ_P(object) : NULL;
  538. ZVAL_COPY_VALUE(&fci.function_name, function_name);
  539. fci.retval = retval_ptr;
  540. fci.param_count = param_count;
  541. fci.params = params;
  542. fci.no_separation = (zend_bool) no_separation;
  543. return zend_call_function(&fci, NULL);
  544. }
  545. /* }}} */
  546. int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache) /* {{{ */
  547. {
  548. uint32_t i;
  549. zend_execute_data *call, dummy_execute_data;
  550. zend_fcall_info_cache fci_cache_local;
  551. zend_function *func;
  552. uint32_t call_info;
  553. void *object_or_called_scope;
  554. ZVAL_UNDEF(fci->retval);
  555. if (!EG(active)) {
  556. return FAILURE; /* executor is already inactive */
  557. }
  558. if (EG(exception)) {
  559. return FAILURE; /* we would result in an instable executor otherwise */
  560. }
  561. ZEND_ASSERT(fci->size == sizeof(zend_fcall_info));
  562. /* Initialize execute_data */
  563. if (!EG(current_execute_data)) {
  564. /* This only happens when we're called outside any execute()'s
  565. * It shouldn't be strictly necessary to NULL execute_data out,
  566. * but it may make bugs easier to spot
  567. */
  568. memset(&dummy_execute_data, 0, sizeof(zend_execute_data));
  569. EG(current_execute_data) = &dummy_execute_data;
  570. } else if (EG(current_execute_data)->func &&
  571. ZEND_USER_CODE(EG(current_execute_data)->func->common.type) &&
  572. EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL &&
  573. EG(current_execute_data)->opline->opcode != ZEND_DO_ICALL &&
  574. EG(current_execute_data)->opline->opcode != ZEND_DO_UCALL &&
  575. EG(current_execute_data)->opline->opcode != ZEND_DO_FCALL_BY_NAME) {
  576. /* Insert fake frame in case of include or magic calls */
  577. dummy_execute_data = *EG(current_execute_data);
  578. dummy_execute_data.prev_execute_data = EG(current_execute_data);
  579. dummy_execute_data.call = NULL;
  580. dummy_execute_data.opline = NULL;
  581. dummy_execute_data.func = NULL;
  582. EG(current_execute_data) = &dummy_execute_data;
  583. }
  584. if (!fci_cache || !fci_cache->function_handler) {
  585. char *error = NULL;
  586. if (!fci_cache) {
  587. fci_cache = &fci_cache_local;
  588. }
  589. if (!zend_is_callable_ex(&fci->function_name, fci->object, IS_CALLABLE_CHECK_SILENT, NULL, fci_cache, &error)) {
  590. if (error) {
  591. zend_string *callable_name
  592. = zend_get_callable_name_ex(&fci->function_name, fci->object);
  593. zend_error(E_WARNING, "Invalid callback %s, %s", ZSTR_VAL(callable_name), error);
  594. efree(error);
  595. zend_string_release_ex(callable_name, 0);
  596. }
  597. if (EG(current_execute_data) == &dummy_execute_data) {
  598. EG(current_execute_data) = dummy_execute_data.prev_execute_data;
  599. }
  600. return FAILURE;
  601. }
  602. ZEND_ASSERT(!error);
  603. }
  604. func = fci_cache->function_handler;
  605. if ((func->common.fn_flags & ZEND_ACC_STATIC) || !fci_cache->object) {
  606. fci->object = NULL;
  607. object_or_called_scope = fci_cache->called_scope;
  608. call_info = ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC;
  609. } else {
  610. fci->object = fci_cache->object;
  611. object_or_called_scope = fci->object;
  612. call_info = ZEND_CALL_TOP_FUNCTION | ZEND_CALL_DYNAMIC | ZEND_CALL_HAS_THIS;
  613. }
  614. call = zend_vm_stack_push_call_frame(call_info,
  615. func, fci->param_count, object_or_called_scope);
  616. if (UNEXPECTED(func->common.fn_flags & ZEND_ACC_DEPRECATED)) {
  617. zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
  618. func->common.scope ? ZSTR_VAL(func->common.scope->name) : "",
  619. func->common.scope ? "::" : "",
  620. ZSTR_VAL(func->common.function_name));
  621. if (UNEXPECTED(EG(exception))) {
  622. zend_vm_stack_free_call_frame(call);
  623. if (EG(current_execute_data) == &dummy_execute_data) {
  624. EG(current_execute_data) = dummy_execute_data.prev_execute_data;
  625. zend_rethrow_exception(EG(current_execute_data));
  626. }
  627. return FAILURE;
  628. }
  629. }
  630. for (i=0; i<fci->param_count; i++) {
  631. zval *param;
  632. zval *arg = &fci->params[i];
  633. if (ARG_SHOULD_BE_SENT_BY_REF(func, i + 1)) {
  634. if (UNEXPECTED(!Z_ISREF_P(arg))) {
  635. if (!fci->no_separation) {
  636. /* Separation is enabled -- create a ref */
  637. ZVAL_NEW_REF(arg, arg);
  638. } else if (!ARG_MAY_BE_SENT_BY_REF(func, i + 1)) {
  639. /* By-value send is not allowed -- emit a warning,
  640. * but still perform the call with a by-value send. */
  641. const char *arg_name = get_function_arg_name(func, i + 1);
  642. zend_error(E_WARNING,
  643. "%s%s%s(): Argument #%d%s%s%s must be passed by reference, value given",
  644. func->common.scope ? ZSTR_VAL(func->common.scope->name) : "",
  645. func->common.scope ? "::" : "",
  646. ZSTR_VAL(func->common.function_name),
  647. i+1,
  648. arg_name ? " ($" : "",
  649. arg_name ? arg_name : "",
  650. arg_name ? ")" : ""
  651. );
  652. if (UNEXPECTED(EG(exception))) {
  653. ZEND_CALL_NUM_ARGS(call) = i;
  654. zend_vm_stack_free_args(call);
  655. zend_vm_stack_free_call_frame(call);
  656. if (EG(current_execute_data) == &dummy_execute_data) {
  657. EG(current_execute_data) = dummy_execute_data.prev_execute_data;
  658. }
  659. return FAILURE;
  660. }
  661. }
  662. }
  663. } else {
  664. if (Z_ISREF_P(arg) &&
  665. !(func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  666. /* don't separate references for __call */
  667. arg = Z_REFVAL_P(arg);
  668. }
  669. }
  670. param = ZEND_CALL_ARG(call, i+1);
  671. ZVAL_COPY(param, arg);
  672. }
  673. if (UNEXPECTED(func->op_array.fn_flags & ZEND_ACC_CLOSURE)) {
  674. uint32_t call_info;
  675. GC_ADDREF(ZEND_CLOSURE_OBJECT(func));
  676. call_info = ZEND_CALL_CLOSURE;
  677. if (func->common.fn_flags & ZEND_ACC_FAKE_CLOSURE) {
  678. call_info |= ZEND_CALL_FAKE_CLOSURE;
  679. }
  680. ZEND_ADD_CALL_FLAG(call, call_info);
  681. }
  682. if (func->type == ZEND_USER_FUNCTION) {
  683. int call_via_handler = (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  684. const zend_op *current_opline_before_exception = EG(opline_before_exception);
  685. zend_init_func_execute_data(call, &func->op_array, fci->retval);
  686. zend_execute_ex(call);
  687. EG(opline_before_exception) = current_opline_before_exception;
  688. if (call_via_handler) {
  689. /* We must re-initialize function again */
  690. fci_cache->function_handler = NULL;
  691. }
  692. } else {
  693. int call_via_handler = (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) != 0;
  694. ZEND_ASSERT(func->type == ZEND_INTERNAL_FUNCTION);
  695. ZVAL_NULL(fci->retval);
  696. call->prev_execute_data = EG(current_execute_data);
  697. EG(current_execute_data) = call;
  698. if (EXPECTED(zend_execute_internal == NULL)) {
  699. /* saves one function call if zend_execute_internal is not used */
  700. func->internal_function.handler(call, fci->retval);
  701. } else {
  702. zend_execute_internal(call, fci->retval);
  703. }
  704. EG(current_execute_data) = call->prev_execute_data;
  705. zend_vm_stack_free_args(call);
  706. if (EG(exception)) {
  707. zval_ptr_dtor(fci->retval);
  708. ZVAL_UNDEF(fci->retval);
  709. }
  710. if (call_via_handler) {
  711. /* We must re-initialize function again */
  712. fci_cache->function_handler = NULL;
  713. }
  714. }
  715. zend_vm_stack_free_call_frame(call);
  716. if (EG(current_execute_data) == &dummy_execute_data) {
  717. EG(current_execute_data) = dummy_execute_data.prev_execute_data;
  718. }
  719. if (UNEXPECTED(EG(exception))) {
  720. if (UNEXPECTED(!EG(current_execute_data))) {
  721. zend_throw_exception_internal(NULL);
  722. } else if (EG(current_execute_data)->func &&
  723. ZEND_USER_CODE(EG(current_execute_data)->func->common.type)) {
  724. zend_rethrow_exception(EG(current_execute_data));
  725. }
  726. }
  727. return SUCCESS;
  728. }
  729. /* }}} */
  730. ZEND_API zend_class_entry *zend_lookup_class_ex(zend_string *name, zend_string *key, uint32_t flags) /* {{{ */
  731. {
  732. zend_class_entry *ce = NULL;
  733. zval args[1], *zv;
  734. zval local_retval;
  735. zend_string *lc_name;
  736. zend_fcall_info fcall_info;
  737. zend_fcall_info_cache fcall_cache;
  738. zend_class_entry *orig_fake_scope;
  739. if (key) {
  740. lc_name = key;
  741. } else {
  742. if (name == NULL || !ZSTR_LEN(name)) {
  743. return NULL;
  744. }
  745. if (ZSTR_VAL(name)[0] == '\\') {
  746. lc_name = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
  747. zend_str_tolower_copy(ZSTR_VAL(lc_name), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  748. } else {
  749. lc_name = zend_string_tolower(name);
  750. }
  751. }
  752. zv = zend_hash_find(EG(class_table), lc_name);
  753. if (zv) {
  754. if (!key) {
  755. zend_string_release_ex(lc_name, 0);
  756. }
  757. ce = (zend_class_entry*)Z_PTR_P(zv);
  758. if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_LINKED))) {
  759. if ((flags & ZEND_FETCH_CLASS_ALLOW_UNLINKED) ||
  760. ((flags & ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED) &&
  761. (ce->ce_flags & ZEND_ACC_NEARLY_LINKED))) {
  762. ce->ce_flags |= ZEND_ACC_HAS_UNLINKED_USES;
  763. return ce;
  764. }
  765. return NULL;
  766. }
  767. return ce;
  768. }
  769. /* The compiler is not-reentrant. Make sure we __autoload() only during run-time
  770. * (doesn't impact functionality of __autoload()
  771. */
  772. if ((flags & ZEND_FETCH_CLASS_NO_AUTOLOAD) || zend_is_compiling()) {
  773. if (!key) {
  774. zend_string_release_ex(lc_name, 0);
  775. }
  776. return NULL;
  777. }
  778. if (!EG(autoload_func)) {
  779. if (!key) {
  780. zend_string_release_ex(lc_name, 0);
  781. }
  782. return NULL;
  783. }
  784. /* Verify class name before passing it to __autoload() */
  785. if (!key && strspn(ZSTR_VAL(name), "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377\\") != ZSTR_LEN(name)) {
  786. zend_string_release_ex(lc_name, 0);
  787. return NULL;
  788. }
  789. if (EG(in_autoload) == NULL) {
  790. ALLOC_HASHTABLE(EG(in_autoload));
  791. zend_hash_init(EG(in_autoload), 8, NULL, NULL, 0);
  792. }
  793. if (zend_hash_add_empty_element(EG(in_autoload), lc_name) == NULL) {
  794. if (!key) {
  795. zend_string_release_ex(lc_name, 0);
  796. }
  797. return NULL;
  798. }
  799. ZVAL_UNDEF(&local_retval);
  800. if (ZSTR_VAL(name)[0] == '\\') {
  801. ZVAL_STRINGL(&args[0], ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  802. } else {
  803. ZVAL_STR_COPY(&args[0], name);
  804. }
  805. fcall_info.size = sizeof(fcall_info);
  806. ZVAL_STR_COPY(&fcall_info.function_name, EG(autoload_func)->common.function_name);
  807. fcall_info.retval = &local_retval;
  808. fcall_info.param_count = 1;
  809. fcall_info.params = args;
  810. fcall_info.object = NULL;
  811. fcall_info.no_separation = 1;
  812. fcall_cache.function_handler = EG(autoload_func);
  813. fcall_cache.called_scope = NULL;
  814. fcall_cache.object = NULL;
  815. orig_fake_scope = EG(fake_scope);
  816. EG(fake_scope) = NULL;
  817. zend_exception_save();
  818. if ((zend_call_function(&fcall_info, &fcall_cache) == SUCCESS) && !EG(exception)) {
  819. ce = zend_hash_find_ptr(EG(class_table), lc_name);
  820. }
  821. zend_exception_restore();
  822. EG(fake_scope) = orig_fake_scope;
  823. zval_ptr_dtor(&args[0]);
  824. zval_ptr_dtor_str(&fcall_info.function_name);
  825. zend_hash_del(EG(in_autoload), lc_name);
  826. zval_ptr_dtor(&local_retval);
  827. if (!key) {
  828. zend_string_release_ex(lc_name, 0);
  829. }
  830. return ce;
  831. }
  832. /* }}} */
  833. ZEND_API zend_class_entry *zend_lookup_class(zend_string *name) /* {{{ */
  834. {
  835. return zend_lookup_class_ex(name, NULL, 0);
  836. }
  837. /* }}} */
  838. ZEND_API zend_class_entry *zend_get_called_scope(zend_execute_data *ex) /* {{{ */
  839. {
  840. while (ex) {
  841. if (Z_TYPE(ex->This) == IS_OBJECT) {
  842. return Z_OBJCE(ex->This);
  843. } else if (Z_CE(ex->This)) {
  844. return Z_CE(ex->This);
  845. } else if (ex->func) {
  846. if (ex->func->type != ZEND_INTERNAL_FUNCTION || ex->func->common.scope) {
  847. return NULL;
  848. }
  849. }
  850. ex = ex->prev_execute_data;
  851. }
  852. return NULL;
  853. }
  854. /* }}} */
  855. ZEND_API zend_object *zend_get_this_object(zend_execute_data *ex) /* {{{ */
  856. {
  857. while (ex) {
  858. if (Z_TYPE(ex->This) == IS_OBJECT) {
  859. return Z_OBJ(ex->This);
  860. } else if (ex->func) {
  861. if (ex->func->type != ZEND_INTERNAL_FUNCTION || ex->func->common.scope) {
  862. return NULL;
  863. }
  864. }
  865. ex = ex->prev_execute_data;
  866. }
  867. return NULL;
  868. }
  869. /* }}} */
  870. ZEND_API int zend_eval_stringl(const char *str, size_t str_len, zval *retval_ptr, const char *string_name) /* {{{ */
  871. {
  872. zval pv;
  873. zend_op_array *new_op_array;
  874. uint32_t original_compiler_options;
  875. int retval;
  876. if (retval_ptr) {
  877. ZVAL_NEW_STR(&pv, zend_string_alloc(str_len + sizeof("return ;")-1, 0));
  878. memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
  879. memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
  880. Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
  881. Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
  882. } else {
  883. ZVAL_STRINGL(&pv, str, str_len);
  884. }
  885. /*printf("Evaluating '%s'\n", pv.value.str.val);*/
  886. original_compiler_options = CG(compiler_options);
  887. CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
  888. new_op_array = zend_compile_string(&pv, string_name);
  889. CG(compiler_options) = original_compiler_options;
  890. if (new_op_array) {
  891. zval local_retval;
  892. EG(no_extensions)=1;
  893. new_op_array->scope = zend_get_executed_scope();
  894. zend_try {
  895. ZVAL_UNDEF(&local_retval);
  896. zend_execute(new_op_array, &local_retval);
  897. } zend_catch {
  898. destroy_op_array(new_op_array);
  899. efree_size(new_op_array, sizeof(zend_op_array));
  900. zend_bailout();
  901. } zend_end_try();
  902. if (Z_TYPE(local_retval) != IS_UNDEF) {
  903. if (retval_ptr) {
  904. ZVAL_COPY_VALUE(retval_ptr, &local_retval);
  905. } else {
  906. zval_ptr_dtor(&local_retval);
  907. }
  908. } else {
  909. if (retval_ptr) {
  910. ZVAL_NULL(retval_ptr);
  911. }
  912. }
  913. EG(no_extensions)=0;
  914. destroy_op_array(new_op_array);
  915. efree_size(new_op_array, sizeof(zend_op_array));
  916. retval = SUCCESS;
  917. } else {
  918. retval = FAILURE;
  919. }
  920. zval_ptr_dtor_str(&pv);
  921. return retval;
  922. }
  923. /* }}} */
  924. ZEND_API int zend_eval_string(const char *str, zval *retval_ptr, const char *string_name) /* {{{ */
  925. {
  926. return zend_eval_stringl(str, strlen(str), retval_ptr, string_name);
  927. }
  928. /* }}} */
  929. ZEND_API int zend_eval_stringl_ex(const char *str, size_t str_len, zval *retval_ptr, const char *string_name, int handle_exceptions) /* {{{ */
  930. {
  931. int result;
  932. result = zend_eval_stringl(str, str_len, retval_ptr, string_name);
  933. if (handle_exceptions && EG(exception)) {
  934. zend_exception_error(EG(exception), E_ERROR);
  935. result = FAILURE;
  936. }
  937. return result;
  938. }
  939. /* }}} */
  940. ZEND_API int zend_eval_string_ex(const char *str, zval *retval_ptr, const char *string_name, int handle_exceptions) /* {{{ */
  941. {
  942. return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions);
  943. }
  944. /* }}} */
  945. static void zend_set_timeout_ex(zend_long seconds, int reset_signals);
  946. ZEND_API ZEND_NORETURN void ZEND_FASTCALL zend_timeout(int dummy) /* {{{ */
  947. {
  948. #if defined(PHP_WIN32)
  949. # ifndef ZTS
  950. /* No action is needed if we're timed out because zero seconds are
  951. just ignored. Also, the hard timeout needs to be respected. If the
  952. timer is not restarted properly, it could hang in the shutdown
  953. function. */
  954. if (EG(hard_timeout) > 0) {
  955. EG(timed_out) = 0;
  956. zend_set_timeout_ex(EG(hard_timeout), 1);
  957. /* XXX Abused, introduce an additional flag if the value needs to be kept. */
  958. EG(hard_timeout) = 0;
  959. }
  960. # endif
  961. #else
  962. EG(timed_out) = 0;
  963. zend_set_timeout_ex(0, 1);
  964. #endif
  965. zend_error_noreturn(E_ERROR, "Maximum execution time of " ZEND_LONG_FMT " second%s exceeded", EG(timeout_seconds), EG(timeout_seconds) == 1 ? "" : "s");
  966. }
  967. /* }}} */
  968. #ifndef ZEND_WIN32
  969. static void zend_timeout_handler(int dummy) /* {{{ */
  970. {
  971. #ifndef ZTS
  972. if (EG(timed_out)) {
  973. /* Die on hard timeout */
  974. const char *error_filename = NULL;
  975. uint32_t error_lineno = 0;
  976. char log_buffer[2048];
  977. int output_len = 0;
  978. if (zend_is_compiling()) {
  979. error_filename = ZSTR_VAL(zend_get_compiled_filename());
  980. error_lineno = zend_get_compiled_lineno();
  981. } else if (zend_is_executing()) {
  982. error_filename = zend_get_executed_filename();
  983. if (error_filename[0] == '[') { /* [no active file] */
  984. error_filename = NULL;
  985. error_lineno = 0;
  986. } else {
  987. error_lineno = zend_get_executed_lineno();
  988. }
  989. }
  990. if (!error_filename) {
  991. error_filename = "Unknown";
  992. }
  993. output_len = snprintf(log_buffer, sizeof(log_buffer), "\nFatal error: Maximum execution time of " ZEND_LONG_FMT "+" ZEND_LONG_FMT " seconds exceeded (terminated) in %s on line %d\n", EG(timeout_seconds), EG(hard_timeout), error_filename, error_lineno);
  994. if (output_len > 0) {
  995. zend_quiet_write(2, log_buffer, MIN(output_len, sizeof(log_buffer)));
  996. }
  997. _exit(124);
  998. }
  999. #endif
  1000. if (zend_on_timeout) {
  1001. zend_on_timeout(EG(timeout_seconds));
  1002. }
  1003. EG(timed_out) = 1;
  1004. EG(vm_interrupt) = 1;
  1005. #ifndef ZTS
  1006. if (EG(hard_timeout) > 0) {
  1007. /* Set hard timeout */
  1008. zend_set_timeout_ex(EG(hard_timeout), 1);
  1009. }
  1010. #endif
  1011. }
  1012. /* }}} */
  1013. #endif
  1014. #ifdef ZEND_WIN32
  1015. VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out)
  1016. {
  1017. zend_executor_globals *eg;
  1018. /* The doc states it'll be always true, however it theoretically
  1019. could be FALSE when the thread was signaled. */
  1020. if (!timed_out) {
  1021. return;
  1022. }
  1023. eg = (zend_executor_globals *)arg;
  1024. eg->timed_out = 1;
  1025. eg->vm_interrupt = 1;
  1026. }
  1027. #endif
  1028. /* This one doesn't exists on QNX */
  1029. #ifndef SIGPROF
  1030. #define SIGPROF 27
  1031. #endif
  1032. static void zend_set_timeout_ex(zend_long seconds, int reset_signals) /* {{{ */
  1033. {
  1034. #ifdef ZEND_WIN32
  1035. zend_executor_globals *eg;
  1036. if (!seconds) {
  1037. return;
  1038. }
  1039. /* Don't use ChangeTimerQueueTimer() as it will not restart an expired
  1040. * timer, so we could end up with just an ignored timeout. Instead
  1041. * delete and recreate. */
  1042. if (NULL != tq_timer) {
  1043. if (!DeleteTimerQueueTimer(NULL, tq_timer, INVALID_HANDLE_VALUE)) {
  1044. tq_timer = NULL;
  1045. zend_error_noreturn(E_ERROR, "Could not delete queued timer");
  1046. return;
  1047. }
  1048. tq_timer = NULL;
  1049. }
  1050. /* XXX passing NULL means the default timer queue provided by the system is used */
  1051. eg = ZEND_MODULE_GLOBALS_BULK(executor);
  1052. if (!CreateTimerQueueTimer(&tq_timer, NULL, (WAITORTIMERCALLBACK)tq_timer_cb, (VOID*)eg, seconds*1000, 0, WT_EXECUTEONLYONCE)) {
  1053. tq_timer = NULL;
  1054. zend_error_noreturn(E_ERROR, "Could not queue new timer");
  1055. return;
  1056. }
  1057. #elif defined(HAVE_SETITIMER)
  1058. {
  1059. struct itimerval t_r; /* timeout requested */
  1060. int signo;
  1061. if(seconds) {
  1062. t_r.it_value.tv_sec = seconds;
  1063. t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
  1064. # ifdef __CYGWIN__
  1065. setitimer(ITIMER_REAL, &t_r, NULL);
  1066. }
  1067. signo = SIGALRM;
  1068. # else
  1069. setitimer(ITIMER_PROF, &t_r, NULL);
  1070. }
  1071. signo = SIGPROF;
  1072. # endif
  1073. if (reset_signals) {
  1074. # ifdef ZEND_SIGNALS
  1075. zend_signal(signo, zend_timeout_handler);
  1076. # else
  1077. sigset_t sigset;
  1078. # ifdef HAVE_SIGACTION
  1079. struct sigaction act;
  1080. act.sa_handler = zend_timeout_handler;
  1081. sigemptyset(&act.sa_mask);
  1082. act.sa_flags = SA_RESETHAND | SA_NODEFER;
  1083. sigaction(signo, &act, NULL);
  1084. # else
  1085. signal(signo, zend_timeout_handler);
  1086. # endif /* HAVE_SIGACTION */
  1087. sigemptyset(&sigset);
  1088. sigaddset(&sigset, signo);
  1089. sigprocmask(SIG_UNBLOCK, &sigset, NULL);
  1090. # endif /* ZEND_SIGNALS */
  1091. }
  1092. }
  1093. #endif /* HAVE_SETITIMER */
  1094. }
  1095. /* }}} */
  1096. void zend_set_timeout(zend_long seconds, int reset_signals) /* {{{ */
  1097. {
  1098. EG(timeout_seconds) = seconds;
  1099. zend_set_timeout_ex(seconds, reset_signals);
  1100. EG(timed_out) = 0;
  1101. }
  1102. /* }}} */
  1103. void zend_unset_timeout(void) /* {{{ */
  1104. {
  1105. #ifdef ZEND_WIN32
  1106. if (NULL != tq_timer) {
  1107. if (!DeleteTimerQueueTimer(NULL, tq_timer, INVALID_HANDLE_VALUE)) {
  1108. EG(timed_out) = 0;
  1109. tq_timer = NULL;
  1110. zend_error_noreturn(E_ERROR, "Could not delete queued timer");
  1111. return;
  1112. }
  1113. tq_timer = NULL;
  1114. }
  1115. #elif defined(HAVE_SETITIMER)
  1116. if (EG(timeout_seconds)) {
  1117. struct itimerval no_timeout;
  1118. no_timeout.it_value.tv_sec = no_timeout.it_value.tv_usec = no_timeout.it_interval.tv_sec = no_timeout.it_interval.tv_usec = 0;
  1119. # ifdef __CYGWIN__
  1120. setitimer(ITIMER_REAL, &no_timeout, NULL);
  1121. # else
  1122. setitimer(ITIMER_PROF, &no_timeout, NULL);
  1123. # endif
  1124. }
  1125. #endif
  1126. EG(timed_out) = 0;
  1127. }
  1128. /* }}} */
  1129. zend_class_entry *zend_fetch_class(zend_string *class_name, int fetch_type) /* {{{ */
  1130. {
  1131. zend_class_entry *ce, *scope;
  1132. int fetch_sub_type = fetch_type & ZEND_FETCH_CLASS_MASK;
  1133. check_fetch_type:
  1134. switch (fetch_sub_type) {
  1135. case ZEND_FETCH_CLASS_SELF:
  1136. scope = zend_get_executed_scope();
  1137. if (UNEXPECTED(!scope)) {
  1138. zend_throw_or_error(fetch_type, NULL, "Cannot access self:: when no class scope is active");
  1139. }
  1140. return scope;
  1141. case ZEND_FETCH_CLASS_PARENT:
  1142. scope = zend_get_executed_scope();
  1143. if (UNEXPECTED(!scope)) {
  1144. zend_throw_or_error(fetch_type, NULL, "Cannot access parent:: when no class scope is active");
  1145. return NULL;
  1146. }
  1147. if (UNEXPECTED(!scope->parent)) {
  1148. zend_throw_or_error(fetch_type, NULL, "Cannot access parent:: when current class scope has no parent");
  1149. }
  1150. return scope->parent;
  1151. case ZEND_FETCH_CLASS_STATIC:
  1152. ce = zend_get_called_scope(EG(current_execute_data));
  1153. if (UNEXPECTED(!ce)) {
  1154. zend_throw_or_error(fetch_type, NULL, "Cannot access static:: when no class scope is active");
  1155. return NULL;
  1156. }
  1157. return ce;
  1158. case ZEND_FETCH_CLASS_AUTO: {
  1159. fetch_sub_type = zend_get_class_fetch_type(class_name);
  1160. if (UNEXPECTED(fetch_sub_type != ZEND_FETCH_CLASS_DEFAULT)) {
  1161. goto check_fetch_type;
  1162. }
  1163. }
  1164. break;
  1165. }
  1166. if (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) {
  1167. return zend_lookup_class_ex(class_name, NULL, fetch_type);
  1168. } else if ((ce = zend_lookup_class_ex(class_name, NULL, fetch_type)) == NULL) {
  1169. if (!(fetch_type & ZEND_FETCH_CLASS_SILENT) && !EG(exception)) {
  1170. if (fetch_sub_type == ZEND_FETCH_CLASS_INTERFACE) {
  1171. zend_throw_or_error(fetch_type, NULL, "Interface '%s' not found", ZSTR_VAL(class_name));
  1172. } else if (fetch_sub_type == ZEND_FETCH_CLASS_TRAIT) {
  1173. zend_throw_or_error(fetch_type, NULL, "Trait '%s' not found", ZSTR_VAL(class_name));
  1174. } else {
  1175. zend_throw_or_error(fetch_type, NULL, "Class '%s' not found", ZSTR_VAL(class_name));
  1176. }
  1177. }
  1178. return NULL;
  1179. }
  1180. return ce;
  1181. }
  1182. /* }}} */
  1183. zend_class_entry *zend_fetch_class_by_name(zend_string *class_name, zend_string *key, int fetch_type) /* {{{ */
  1184. {
  1185. zend_class_entry *ce;
  1186. if (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) {
  1187. return zend_lookup_class_ex(class_name, key, fetch_type);
  1188. } else if ((ce = zend_lookup_class_ex(class_name, key, fetch_type)) == NULL) {
  1189. if (fetch_type & ZEND_FETCH_CLASS_SILENT) {
  1190. return NULL;
  1191. }
  1192. if (EG(exception)) {
  1193. if (!(fetch_type & ZEND_FETCH_CLASS_EXCEPTION)) {
  1194. zend_string *exception_str;
  1195. zval exception_zv;
  1196. ZVAL_OBJ(&exception_zv, EG(exception));
  1197. Z_ADDREF(exception_zv);
  1198. zend_clear_exception();
  1199. exception_str = zval_get_string(&exception_zv);
  1200. zend_error_noreturn(E_ERROR,
  1201. "During class fetch: Uncaught %s", ZSTR_VAL(exception_str));
  1202. }
  1203. return NULL;
  1204. }
  1205. if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_INTERFACE) {
  1206. zend_throw_or_error(fetch_type, NULL, "Interface '%s' not found", ZSTR_VAL(class_name));
  1207. } else if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_TRAIT) {
  1208. zend_throw_or_error(fetch_type, NULL, "Trait '%s' not found", ZSTR_VAL(class_name));
  1209. } else {
  1210. zend_throw_or_error(fetch_type, NULL, "Class '%s' not found", ZSTR_VAL(class_name));
  1211. }
  1212. return NULL;
  1213. }
  1214. return ce;
  1215. }
  1216. /* }}} */
  1217. ZEND_API int zend_delete_global_variable(zend_string *name) /* {{{ */
  1218. {
  1219. return zend_hash_del_ind(&EG(symbol_table), name);
  1220. }
  1221. /* }}} */
  1222. ZEND_API zend_array *zend_rebuild_symbol_table(void) /* {{{ */
  1223. {
  1224. zend_execute_data *ex;
  1225. zend_array *symbol_table;
  1226. /* Search for last called user function */
  1227. ex = EG(current_execute_data);
  1228. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->common.type))) {
  1229. ex = ex->prev_execute_data;
  1230. }
  1231. if (!ex) {
  1232. return NULL;
  1233. }
  1234. if (ZEND_CALL_INFO(ex) & ZEND_CALL_HAS_SYMBOL_TABLE) {
  1235. return ex->symbol_table;
  1236. }
  1237. ZEND_ADD_CALL_FLAG(ex, ZEND_CALL_HAS_SYMBOL_TABLE);
  1238. if (EG(symtable_cache_ptr) > EG(symtable_cache)) {
  1239. symbol_table = ex->symbol_table = *(--EG(symtable_cache_ptr));
  1240. if (!ex->func->op_array.last_var) {
  1241. return symbol_table;
  1242. }
  1243. zend_hash_extend(symbol_table, ex->func->op_array.last_var, 0);
  1244. } else {
  1245. symbol_table = ex->symbol_table = zend_new_array(ex->func->op_array.last_var);
  1246. if (!ex->func->op_array.last_var) {
  1247. return symbol_table;
  1248. }
  1249. zend_hash_real_init_mixed(symbol_table);
  1250. /*printf("Cache miss! Initialized %x\n", EG(active_symbol_table));*/
  1251. }
  1252. if (EXPECTED(ex->func->op_array.last_var)) {
  1253. zend_string **str = ex->func->op_array.vars;
  1254. zend_string **end = str + ex->func->op_array.last_var;
  1255. zval *var = ZEND_CALL_VAR_NUM(ex, 0);
  1256. do {
  1257. _zend_hash_append_ind(symbol_table, *str, var);
  1258. str++;
  1259. var++;
  1260. } while (str != end);
  1261. }
  1262. return symbol_table;
  1263. }
  1264. /* }}} */
  1265. ZEND_API void zend_attach_symbol_table(zend_execute_data *execute_data) /* {{{ */
  1266. {
  1267. zend_op_array *op_array = &execute_data->func->op_array;
  1268. HashTable *ht = execute_data->symbol_table;
  1269. /* copy real values from symbol table into CV slots and create
  1270. INDIRECT references to CV in symbol table */
  1271. if (EXPECTED(op_array->last_var)) {
  1272. zend_string **str = op_array->vars;
  1273. zend_string **end = str + op_array->last_var;
  1274. zval *var = EX_VAR_NUM(0);
  1275. do {
  1276. zval *zv = zend_hash_find_ex(ht, *str, 1);
  1277. if (zv) {
  1278. if (Z_TYPE_P(zv) == IS_INDIRECT) {
  1279. zval *val = Z_INDIRECT_P(zv);
  1280. ZVAL_COPY_VALUE(var, val);
  1281. } else {
  1282. ZVAL_COPY_VALUE(var, zv);
  1283. }
  1284. } else {
  1285. ZVAL_UNDEF(var);
  1286. zv = zend_hash_add_new(ht, *str, var);
  1287. }
  1288. ZVAL_INDIRECT(zv, var);
  1289. str++;
  1290. var++;
  1291. } while (str != end);
  1292. }
  1293. }
  1294. /* }}} */
  1295. ZEND_API void zend_detach_symbol_table(zend_execute_data *execute_data) /* {{{ */
  1296. {
  1297. zend_op_array *op_array = &execute_data->func->op_array;
  1298. HashTable *ht = execute_data->symbol_table;
  1299. /* copy real values from CV slots into symbol table */
  1300. if (EXPECTED(op_array->last_var)) {
  1301. zend_string **str = op_array->vars;
  1302. zend_string **end = str + op_array->last_var;
  1303. zval *var = EX_VAR_NUM(0);
  1304. do {
  1305. if (Z_TYPE_P(var) == IS_UNDEF) {
  1306. zend_hash_del(ht, *str);
  1307. } else {
  1308. zend_hash_update(ht, *str, var);
  1309. ZVAL_UNDEF(var);
  1310. }
  1311. str++;
  1312. var++;
  1313. } while (str != end);
  1314. }
  1315. }
  1316. /* }}} */
  1317. ZEND_API int zend_set_local_var(zend_string *name, zval *value, int force) /* {{{ */
  1318. {
  1319. zend_execute_data *execute_data = EG(current_execute_data);
  1320. while (execute_data && (!execute_data->func || !ZEND_USER_CODE(execute_data->func->common.type))) {
  1321. execute_data = execute_data->prev_execute_data;
  1322. }
  1323. if (execute_data) {
  1324. if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1325. zend_ulong h = zend_string_hash_val(name);
  1326. zend_op_array *op_array = &execute_data->func->op_array;
  1327. if (EXPECTED(op_array->last_var)) {
  1328. zend_string **str = op_array->vars;
  1329. zend_string **end = str + op_array->last_var;
  1330. do {
  1331. if (ZSTR_H(*str) == h &&
  1332. zend_string_equal_content(*str, name)) {
  1333. zval *var = EX_VAR_NUM(str - op_array->vars);
  1334. ZVAL_COPY_VALUE(var, value);
  1335. return SUCCESS;
  1336. }
  1337. str++;
  1338. } while (str != end);
  1339. }
  1340. if (force) {
  1341. zend_array *symbol_table = zend_rebuild_symbol_table();
  1342. if (symbol_table) {
  1343. zend_hash_update(symbol_table, name, value);
  1344. return SUCCESS;
  1345. }
  1346. }
  1347. } else {
  1348. zend_hash_update_ind(execute_data->symbol_table, name, value);
  1349. return SUCCESS;
  1350. }
  1351. }
  1352. return FAILURE;
  1353. }
  1354. /* }}} */
  1355. ZEND_API int zend_set_local_var_str(const char *name, size_t len, zval *value, int force) /* {{{ */
  1356. {
  1357. zend_execute_data *execute_data = EG(current_execute_data);
  1358. while (execute_data && (!execute_data->func || !ZEND_USER_CODE(execute_data->func->common.type))) {
  1359. execute_data = execute_data->prev_execute_data;
  1360. }
  1361. if (execute_data) {
  1362. if (!(EX_CALL_INFO() & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1363. zend_ulong h = zend_hash_func(name, len);
  1364. zend_op_array *op_array = &execute_data->func->op_array;
  1365. if (EXPECTED(op_array->last_var)) {
  1366. zend_string **str = op_array->vars;
  1367. zend_string **end = str + op_array->last_var;
  1368. do {
  1369. if (ZSTR_H(*str) == h &&
  1370. ZSTR_LEN(*str) == len &&
  1371. memcmp(ZSTR_VAL(*str), name, len) == 0) {
  1372. zval *var = EX_VAR_NUM(str - op_array->vars);
  1373. zval_ptr_dtor(var);
  1374. ZVAL_COPY_VALUE(var, value);
  1375. return SUCCESS;
  1376. }
  1377. str++;
  1378. } while (str != end);
  1379. }
  1380. if (force) {
  1381. zend_array *symbol_table = zend_rebuild_symbol_table();
  1382. if (symbol_table) {
  1383. zend_hash_str_update(symbol_table, name, len, value);
  1384. return SUCCESS;
  1385. }
  1386. }
  1387. } else {
  1388. zend_hash_str_update_ind(execute_data->symbol_table, name, len, value);
  1389. return SUCCESS;
  1390. }
  1391. }
  1392. return FAILURE;
  1393. }
  1394. /* }}} */