zend_execute_API.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  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_ptr_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. #ifdef HAVE_SYS_TIME_H
  35. #include <sys/time.h>
  36. #endif
  37. ZEND_API void (*zend_execute_ex)(zend_execute_data *execute_data TSRMLS_DC);
  38. ZEND_API void (*zend_execute_internal)(zend_execute_data *execute_data_ptr, zend_fcall_info *fci, int return_value_used TSRMLS_DC);
  39. /* true globals */
  40. ZEND_API const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0, NULL, NULL, 0 };
  41. ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, NULL };
  42. #ifdef ZEND_WIN32
  43. #ifdef ZTS
  44. __declspec(thread)
  45. #endif
  46. HANDLE tq_timer = NULL;
  47. #endif
  48. #if 0&&ZEND_DEBUG
  49. static void (*original_sigsegv_handler)(int);
  50. static void zend_handle_sigsegv(int dummy) /* {{{ */
  51. {
  52. fflush(stdout);
  53. fflush(stderr);
  54. if (original_sigsegv_handler == zend_handle_sigsegv) {
  55. signal(SIGSEGV, original_sigsegv_handler);
  56. } else {
  57. signal(SIGSEGV, SIG_DFL);
  58. }
  59. {
  60. TSRMLS_FETCH();
  61. fprintf(stderr, "SIGSEGV caught on opcode %d on opline %d of %s() at %s:%d\n\n",
  62. active_opline->opcode,
  63. active_opline-EG(active_op_array)->opcodes,
  64. get_active_function_name(TSRMLS_C),
  65. zend_get_executed_filename(TSRMLS_C),
  66. zend_get_executed_lineno(TSRMLS_C));
  67. /* See http://support.microsoft.com/kb/190351 */
  68. #ifdef PHP_WIN32
  69. fflush(stderr);
  70. #endif
  71. }
  72. if (original_sigsegv_handler!=zend_handle_sigsegv) {
  73. original_sigsegv_handler(dummy);
  74. }
  75. }
  76. /* }}} */
  77. #endif
  78. static void zend_extension_activator(zend_extension *extension TSRMLS_DC) /* {{{ */
  79. {
  80. if (extension->activate) {
  81. extension->activate();
  82. }
  83. }
  84. /* }}} */
  85. static void zend_extension_deactivator(zend_extension *extension TSRMLS_DC) /* {{{ */
  86. {
  87. if (extension->deactivate) {
  88. extension->deactivate();
  89. }
  90. }
  91. /* }}} */
  92. static int clean_non_persistent_function(zend_function *function TSRMLS_DC) /* {{{ */
  93. {
  94. return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
  95. }
  96. /* }}} */
  97. ZEND_API int clean_non_persistent_function_full(zend_function *function TSRMLS_DC) /* {{{ */
  98. {
  99. return (function->type == ZEND_INTERNAL_FUNCTION) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  100. }
  101. /* }}} */
  102. static int clean_non_persistent_class(zend_class_entry **ce TSRMLS_DC) /* {{{ */
  103. {
  104. return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_STOP : ZEND_HASH_APPLY_REMOVE;
  105. }
  106. /* }}} */
  107. ZEND_API int clean_non_persistent_class_full(zend_class_entry **ce TSRMLS_DC) /* {{{ */
  108. {
  109. return ((*ce)->type == ZEND_INTERNAL_CLASS) ? ZEND_HASH_APPLY_KEEP : ZEND_HASH_APPLY_REMOVE;
  110. }
  111. /* }}} */
  112. void init_executor(TSRMLS_D) /* {{{ */
  113. {
  114. zend_init_fpu(TSRMLS_C);
  115. INIT_ZVAL(EG(uninitialized_zval));
  116. /* trick to make uninitialized_zval never be modified, passed by ref, etc. */
  117. Z_ADDREF(EG(uninitialized_zval));
  118. INIT_ZVAL(EG(error_zval));
  119. EG(uninitialized_zval_ptr)=&EG(uninitialized_zval);
  120. EG(error_zval_ptr)=&EG(error_zval);
  121. /* destroys stack frame, therefore makes core dumps worthless */
  122. #if 0&&ZEND_DEBUG
  123. original_sigsegv_handler = signal(SIGSEGV, zend_handle_sigsegv);
  124. #endif
  125. EG(return_value_ptr_ptr) = NULL;
  126. EG(symtable_cache_ptr) = EG(symtable_cache) - 1;
  127. EG(symtable_cache_limit) = EG(symtable_cache) + SYMTABLE_CACHE_SIZE - 1;
  128. EG(no_extensions) = 0;
  129. EG(function_table) = CG(function_table);
  130. EG(class_table) = CG(class_table);
  131. EG(in_execution) = 0;
  132. EG(in_autoload) = NULL;
  133. EG(autoload_func) = NULL;
  134. EG(error_handling) = EH_NORMAL;
  135. zend_vm_stack_init(TSRMLS_C);
  136. zend_vm_stack_push((void *) NULL TSRMLS_CC);
  137. zend_hash_init(&EG(symbol_table), 50, NULL, ZVAL_PTR_DTOR, 0);
  138. EG(active_symbol_table) = &EG(symbol_table);
  139. zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_activator TSRMLS_CC);
  140. EG(opline_ptr) = NULL;
  141. zend_hash_init(&EG(included_files), 5, NULL, NULL, 0);
  142. EG(ticks_count) = 0;
  143. EG(user_error_handler) = NULL;
  144. EG(current_execute_data) = NULL;
  145. zend_stack_init(&EG(user_error_handlers_error_reporting));
  146. zend_ptr_stack_init(&EG(user_error_handlers));
  147. zend_ptr_stack_init(&EG(user_exception_handlers));
  148. zend_objects_store_init(&EG(objects_store), 1024);
  149. EG(full_tables_cleanup) = 0;
  150. #ifdef ZEND_WIN32
  151. EG(timed_out) = 0;
  152. #endif
  153. EG(exception) = NULL;
  154. EG(prev_exception) = NULL;
  155. EG(scope) = NULL;
  156. EG(called_scope) = NULL;
  157. EG(This) = NULL;
  158. EG(active_op_array) = NULL;
  159. EG(active) = 1;
  160. EG(start_op) = NULL;
  161. }
  162. /* }}} */
  163. static int zval_call_destructor(zval **zv TSRMLS_DC) /* {{{ */
  164. {
  165. if (Z_TYPE_PP(zv) == IS_OBJECT && Z_REFCOUNT_PP(zv) == 1) {
  166. return ZEND_HASH_APPLY_REMOVE;
  167. } else {
  168. return ZEND_HASH_APPLY_KEEP;
  169. }
  170. }
  171. /* }}} */
  172. void shutdown_destructors(TSRMLS_D) /* {{{ */
  173. {
  174. zend_try {
  175. int symbols;
  176. do {
  177. symbols = zend_hash_num_elements(&EG(symbol_table));
  178. zend_hash_reverse_apply(&EG(symbol_table), (apply_func_t) zval_call_destructor TSRMLS_CC);
  179. } while (symbols != zend_hash_num_elements(&EG(symbol_table)));
  180. zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC);
  181. } zend_catch {
  182. /* if we couldn't destruct cleanly, mark all objects as destructed anyway */
  183. zend_objects_store_mark_destructed(&EG(objects_store) TSRMLS_CC);
  184. } zend_end_try();
  185. }
  186. /* }}} */
  187. void shutdown_executor(TSRMLS_D) /* {{{ */
  188. {
  189. zend_try {
  190. /* Removed because this can not be safely done, e.g. in this situation:
  191. Object 1 creates object 2
  192. Object 3 holds reference to object 2.
  193. Now when 1 and 2 are destroyed, 3 can still access 2 in its destructor, with
  194. very problematic results */
  195. /* zend_objects_store_call_destructors(&EG(objects_store) TSRMLS_CC); */
  196. /* Moved after symbol table cleaners, because some of the cleaners can call
  197. destructors, which would use EG(symtable_cache_ptr) and thus leave leaks */
  198. /* while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
  199. zend_hash_destroy(*EG(symtable_cache_ptr));
  200. efree(*EG(symtable_cache_ptr));
  201. EG(symtable_cache_ptr)--;
  202. }
  203. */
  204. zend_llist_apply(&zend_extensions, (llist_apply_func_t) zend_extension_deactivator TSRMLS_CC);
  205. zend_hash_graceful_reverse_destroy(&EG(symbol_table));
  206. } zend_end_try();
  207. zend_try {
  208. zval *zeh;
  209. /* remove error handlers before destroying classes and functions,
  210. * so that if handler used some class, crash would not happen */
  211. if (EG(user_error_handler)) {
  212. zeh = EG(user_error_handler);
  213. EG(user_error_handler) = NULL;
  214. zval_ptr_dtor(&zeh);
  215. }
  216. if (EG(user_exception_handler)) {
  217. zeh = EG(user_exception_handler);
  218. EG(user_exception_handler) = NULL;
  219. zval_ptr_dtor(&zeh);
  220. }
  221. zend_stack_destroy(&EG(user_error_handlers_error_reporting));
  222. zend_stack_init(&EG(user_error_handlers_error_reporting));
  223. zend_ptr_stack_clean(&EG(user_error_handlers), ZVAL_DESTRUCTOR, 1);
  224. zend_ptr_stack_clean(&EG(user_exception_handlers), ZVAL_DESTRUCTOR, 1);
  225. } zend_end_try();
  226. zend_try {
  227. /* Cleanup static data for functions and arrays.
  228. * We need a separate cleanup stage because of the following problem:
  229. * Suppose we destroy class X, which destroys the class's function table,
  230. * and in the function table we have function foo() that has static $bar.
  231. * Now if an object of class X is assigned to $bar, its destructor will be
  232. * called and will fail since X's function table is in mid-destruction.
  233. * So we want first of all to clean up all data and then move to tables destruction.
  234. * Note that only run-time accessed data need to be cleaned up, pre-defined data can
  235. * not contain objects and thus are not probelmatic */
  236. if (EG(full_tables_cleanup)) {
  237. zend_hash_apply(EG(function_table), (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
  238. zend_hash_apply(EG(class_table), (apply_func_t) zend_cleanup_class_data TSRMLS_CC);
  239. } else {
  240. zend_hash_reverse_apply(EG(function_table), (apply_func_t) zend_cleanup_function_data TSRMLS_CC);
  241. zend_hash_reverse_apply(EG(class_table), (apply_func_t) zend_cleanup_user_class_data TSRMLS_CC);
  242. zend_cleanup_internal_classes(TSRMLS_C);
  243. }
  244. } zend_end_try();
  245. zend_try {
  246. zend_objects_store_free_object_storage(&EG(objects_store) TSRMLS_CC);
  247. zend_vm_stack_destroy(TSRMLS_C);
  248. /* Destroy all op arrays */
  249. if (EG(full_tables_cleanup)) {
  250. zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function_full TSRMLS_CC);
  251. zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class_full TSRMLS_CC);
  252. } else {
  253. zend_hash_reverse_apply(EG(function_table), (apply_func_t) clean_non_persistent_function TSRMLS_CC);
  254. zend_hash_reverse_apply(EG(class_table), (apply_func_t) clean_non_persistent_class TSRMLS_CC);
  255. }
  256. while (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
  257. zend_hash_destroy(*EG(symtable_cache_ptr));
  258. FREE_HASHTABLE(*EG(symtable_cache_ptr));
  259. EG(symtable_cache_ptr)--;
  260. }
  261. } zend_end_try();
  262. zend_try {
  263. clean_non_persistent_constants(TSRMLS_C);
  264. } zend_end_try();
  265. zend_try {
  266. #if 0&&ZEND_DEBUG
  267. signal(SIGSEGV, original_sigsegv_handler);
  268. #endif
  269. zend_hash_destroy(&EG(included_files));
  270. zend_stack_destroy(&EG(user_error_handlers_error_reporting));
  271. zend_ptr_stack_destroy(&EG(user_error_handlers));
  272. zend_ptr_stack_destroy(&EG(user_exception_handlers));
  273. zend_objects_store_destroy(&EG(objects_store));
  274. if (EG(in_autoload)) {
  275. zend_hash_destroy(EG(in_autoload));
  276. FREE_HASHTABLE(EG(in_autoload));
  277. }
  278. } zend_end_try();
  279. zend_shutdown_fpu(TSRMLS_C);
  280. EG(active) = 0;
  281. }
  282. /* }}} */
  283. /* return class name and "::" or "". */
  284. ZEND_API const char *get_active_class_name(const char **space TSRMLS_DC) /* {{{ */
  285. {
  286. if (!zend_is_executing(TSRMLS_C)) {
  287. if (space) {
  288. *space = "";
  289. }
  290. return "";
  291. }
  292. switch (EG(current_execute_data)->function_state.function->type) {
  293. case ZEND_USER_FUNCTION:
  294. case ZEND_INTERNAL_FUNCTION:
  295. {
  296. zend_class_entry *ce = EG(current_execute_data)->function_state.function->common.scope;
  297. if (space) {
  298. *space = ce ? "::" : "";
  299. }
  300. return ce ? ce->name : "";
  301. }
  302. default:
  303. if (space) {
  304. *space = "";
  305. }
  306. return "";
  307. }
  308. }
  309. /* }}} */
  310. ZEND_API const char *get_active_function_name(TSRMLS_D) /* {{{ */
  311. {
  312. if (!zend_is_executing(TSRMLS_C)) {
  313. return NULL;
  314. }
  315. switch (EG(current_execute_data)->function_state.function->type) {
  316. case ZEND_USER_FUNCTION: {
  317. const char *function_name = ((zend_op_array *) EG(current_execute_data)->function_state.function)->function_name;
  318. if (function_name) {
  319. return function_name;
  320. } else {
  321. return "main";
  322. }
  323. }
  324. break;
  325. case ZEND_INTERNAL_FUNCTION:
  326. return ((zend_internal_function *) EG(current_execute_data)->function_state.function)->function_name;
  327. break;
  328. default:
  329. return NULL;
  330. }
  331. }
  332. /* }}} */
  333. ZEND_API const char *zend_get_executed_filename(TSRMLS_D) /* {{{ */
  334. {
  335. if (EG(active_op_array)) {
  336. return EG(active_op_array)->filename;
  337. } else {
  338. return "[no active file]";
  339. }
  340. }
  341. /* }}} */
  342. ZEND_API uint zend_get_executed_lineno(TSRMLS_D) /* {{{ */
  343. {
  344. if(EG(exception) && EG(opline_ptr) && active_opline->opcode == ZEND_HANDLE_EXCEPTION &&
  345. active_opline->lineno == 0 && EG(opline_before_exception)) {
  346. return EG(opline_before_exception)->lineno;
  347. }
  348. if (EG(opline_ptr)) {
  349. return active_opline->lineno;
  350. } else {
  351. return 0;
  352. }
  353. }
  354. /* }}} */
  355. ZEND_API zend_bool zend_is_executing(TSRMLS_D) /* {{{ */
  356. {
  357. return EG(in_execution);
  358. }
  359. /* }}} */
  360. ZEND_API void _zval_ptr_dtor(zval **zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
  361. {
  362. TSRMLS_FETCH();
  363. i_zval_ptr_dtor(*zval_ptr ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
  364. }
  365. /* }}} */
  366. ZEND_API void _zval_internal_ptr_dtor(zval **zval_ptr ZEND_FILE_LINE_DC) /* {{{ */
  367. {
  368. #if DEBUG_ZEND>=2
  369. printf("Reducing refcount for %x (%x): %d->%d\n", *zval_ptr, zval_ptr, Z_REFCOUNT_PP(zval_ptr), Z_REFCOUNT_PP(zval_ptr) - 1);
  370. #endif
  371. Z_DELREF_PP(zval_ptr);
  372. if (Z_REFCOUNT_PP(zval_ptr) == 0) {
  373. zval_internal_dtor(*zval_ptr);
  374. free(*zval_ptr);
  375. } else if (Z_REFCOUNT_PP(zval_ptr) == 1) {
  376. Z_UNSET_ISREF_PP(zval_ptr);
  377. }
  378. }
  379. /* }}} */
  380. ZEND_API int zend_is_true(zval *op) /* {{{ */
  381. {
  382. return i_zend_is_true(op);
  383. }
  384. /* }}} */
  385. #define IS_VISITED_CONSTANT 0x80
  386. #define IS_CONSTANT_VISITED(p) (Z_TYPE_P(p) & IS_VISITED_CONSTANT)
  387. #define Z_REAL_TYPE_P(p) (Z_TYPE_P(p) & ~IS_VISITED_CONSTANT)
  388. #define MARK_CONSTANT_VISITED(p) Z_TYPE_P(p) |= IS_VISITED_CONSTANT
  389. ZEND_API int zval_update_constant_ex(zval **pp, zend_bool inline_change, zend_class_entry *scope TSRMLS_DC) /* {{{ */
  390. {
  391. zval *p = *pp;
  392. zval const_value;
  393. char *colon;
  394. if (IS_CONSTANT_VISITED(p)) {
  395. zend_error(E_ERROR, "Cannot declare self-referencing constant '%s'", Z_STRVAL_P(p));
  396. } else if ((Z_TYPE_P(p) & IS_CONSTANT_TYPE_MASK) == IS_CONSTANT) {
  397. int refcount;
  398. zend_uchar is_ref;
  399. SEPARATE_ZVAL_IF_NOT_REF(pp);
  400. p = *pp;
  401. MARK_CONSTANT_VISITED(p);
  402. refcount = Z_REFCOUNT_P(p);
  403. is_ref = Z_ISREF_P(p);
  404. if (!zend_get_constant_ex(p->value.str.val, p->value.str.len, &const_value, scope, Z_REAL_TYPE_P(p) TSRMLS_CC)) {
  405. char *actual = Z_STRVAL_P(p);
  406. if ((colon = (char*)zend_memrchr(Z_STRVAL_P(p), ':', Z_STRLEN_P(p)))) {
  407. zend_error(E_ERROR, "Undefined class constant '%s'", Z_STRVAL_P(p));
  408. Z_STRLEN_P(p) -= ((colon - Z_STRVAL_P(p)) + 1);
  409. if (inline_change) {
  410. colon = estrndup(colon, Z_STRLEN_P(p));
  411. str_efree(Z_STRVAL_P(p));
  412. Z_STRVAL_P(p) = colon;
  413. } else {
  414. Z_STRVAL_P(p) = colon + 1;
  415. }
  416. } else {
  417. char *save = actual, *slash;
  418. int actual_len = Z_STRLEN_P(p);
  419. if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) && (slash = (char *)zend_memrchr(actual, '\\', actual_len))) {
  420. actual = slash + 1;
  421. actual_len -= (actual - Z_STRVAL_P(p));
  422. if (inline_change) {
  423. actual = estrndup(actual, actual_len);
  424. Z_STRVAL_P(p) = actual;
  425. Z_STRLEN_P(p) = actual_len;
  426. }
  427. }
  428. if (actual[0] == '\\') {
  429. if (inline_change) {
  430. memmove(Z_STRVAL_P(p), Z_STRVAL_P(p)+1, Z_STRLEN_P(p));
  431. --Z_STRLEN_P(p);
  432. } else {
  433. ++actual;
  434. }
  435. --actual_len;
  436. }
  437. if ((Z_TYPE_P(p) & IS_CONSTANT_UNQUALIFIED) == 0) {
  438. int fix_save = 0;
  439. if (save[0] == '\\') {
  440. save++;
  441. fix_save = 1;
  442. }
  443. zend_error(E_ERROR, "Undefined constant '%s'", save);
  444. if (fix_save) {
  445. save--;
  446. }
  447. if (inline_change) {
  448. str_efree(save);
  449. }
  450. save = NULL;
  451. }
  452. if (inline_change && save && save != actual) {
  453. str_efree(save);
  454. }
  455. zend_error(E_NOTICE, "Use of undefined constant %s - assumed '%s'", actual, actual);
  456. p->type = IS_STRING;
  457. if (!inline_change) {
  458. Z_STRVAL_P(p) = actual;
  459. Z_STRLEN_P(p) = actual_len;
  460. zval_copy_ctor(p);
  461. }
  462. }
  463. } else {
  464. if (inline_change) {
  465. str_efree(Z_STRVAL_P(p));
  466. }
  467. *p = const_value;
  468. }
  469. Z_SET_REFCOUNT_P(p, refcount);
  470. Z_SET_ISREF_TO_P(p, is_ref);
  471. } else if (Z_TYPE_P(p) == IS_CONSTANT_AST) {
  472. SEPARATE_ZVAL_IF_NOT_REF(pp);
  473. p = *pp;
  474. zend_ast_evaluate(&const_value, Z_AST_P(p), scope TSRMLS_CC);
  475. if (inline_change) {
  476. zend_ast_destroy(Z_AST_P(p));
  477. }
  478. ZVAL_COPY_VALUE(p, &const_value);
  479. }
  480. return 0;
  481. }
  482. /* }}} */
  483. ZEND_API int zval_update_constant_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
  484. {
  485. return zval_update_constant_ex(pp, 1, scope TSRMLS_CC);
  486. }
  487. /* }}} */
  488. ZEND_API int zval_update_constant_no_inline_change(zval **pp, zend_class_entry *scope TSRMLS_DC) /* {{{ */
  489. {
  490. return zval_update_constant_ex(pp, 0, scope TSRMLS_CC);
  491. }
  492. /* }}} */
  493. ZEND_API int zval_update_constant(zval **pp, zend_bool inline_change TSRMLS_DC) /* {{{ */
  494. {
  495. return zval_update_constant_ex(pp, inline_change, NULL TSRMLS_CC);
  496. }
  497. /* }}} */
  498. int call_user_function(HashTable *function_table, zval **object_pp, zval *function_name, zval *retval_ptr, zend_uint param_count, zval *params[] TSRMLS_DC) /* {{{ */
  499. {
  500. zval ***params_array;
  501. zend_uint i;
  502. int ex_retval;
  503. zval *local_retval_ptr = NULL;
  504. if (param_count) {
  505. params_array = (zval ***) emalloc(sizeof(zval **)*param_count);
  506. for (i=0; i<param_count; i++) {
  507. params_array[i] = &params[i];
  508. }
  509. } else {
  510. params_array = NULL;
  511. }
  512. ex_retval = call_user_function_ex(function_table, object_pp, function_name, &local_retval_ptr, param_count, params_array, 1, NULL TSRMLS_CC);
  513. if (local_retval_ptr) {
  514. COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
  515. } else {
  516. INIT_ZVAL(*retval_ptr);
  517. }
  518. if (params_array) {
  519. efree(params_array);
  520. }
  521. return ex_retval;
  522. }
  523. /* }}} */
  524. int call_user_function_ex(HashTable *function_table, zval **object_pp, zval *function_name, zval **retval_ptr_ptr, zend_uint param_count, zval **params[], int no_separation, HashTable *symbol_table TSRMLS_DC) /* {{{ */
  525. {
  526. zend_fcall_info fci;
  527. fci.size = sizeof(fci);
  528. fci.function_table = function_table;
  529. fci.object_ptr = object_pp ? *object_pp : NULL;
  530. fci.function_name = function_name;
  531. fci.retval_ptr_ptr = retval_ptr_ptr;
  532. fci.param_count = param_count;
  533. fci.params = params;
  534. fci.no_separation = (zend_bool) no_separation;
  535. fci.symbol_table = symbol_table;
  536. return zend_call_function(&fci, NULL TSRMLS_CC);
  537. }
  538. /* }}} */
  539. int zend_call_function(zend_fcall_info *fci, zend_fcall_info_cache *fci_cache TSRMLS_DC) /* {{{ */
  540. {
  541. zend_uint i;
  542. zval **original_return_value;
  543. HashTable *calling_symbol_table;
  544. zend_op_array *original_op_array;
  545. zend_op **original_opline_ptr;
  546. zend_class_entry *current_scope;
  547. zend_class_entry *current_called_scope;
  548. zend_class_entry *calling_scope = NULL;
  549. zend_class_entry *called_scope = NULL;
  550. zval *current_this;
  551. zend_execute_data execute_data;
  552. zend_fcall_info_cache fci_cache_local;
  553. *fci->retval_ptr_ptr = NULL;
  554. if (!EG(active)) {
  555. return FAILURE; /* executor is already inactive */
  556. }
  557. if (EG(exception)) {
  558. return FAILURE; /* we would result in an instable executor otherwise */
  559. }
  560. switch (fci->size) {
  561. case sizeof(zend_fcall_info):
  562. break; /* nothing to do currently */
  563. default:
  564. zend_error(E_ERROR, "Corrupted fcall_info provided to zend_call_function()");
  565. break;
  566. }
  567. /* Initialize execute_data */
  568. if (EG(current_execute_data)) {
  569. execute_data = *EG(current_execute_data);
  570. EX(op_array) = NULL;
  571. EX(opline) = NULL;
  572. EX(object) = NULL;
  573. } else {
  574. /* This only happens when we're called outside any execute()'s
  575. * It shouldn't be strictly necessary to NULL execute_data out,
  576. * but it may make bugs easier to spot
  577. */
  578. memset(&execute_data, 0, sizeof(zend_execute_data));
  579. }
  580. if (!fci_cache || !fci_cache->initialized) {
  581. char *callable_name;
  582. char *error = NULL;
  583. if (!fci_cache) {
  584. fci_cache = &fci_cache_local;
  585. }
  586. if (!zend_is_callable_ex(fci->function_name, fci->object_ptr, IS_CALLABLE_CHECK_SILENT, &callable_name, NULL, fci_cache, &error TSRMLS_CC)) {
  587. if (error) {
  588. zend_error(E_WARNING, "Invalid callback %s, %s", callable_name, error);
  589. efree(error);
  590. }
  591. if (callable_name) {
  592. efree(callable_name);
  593. }
  594. return FAILURE;
  595. } else if (error) {
  596. /* Capitalize the first latter of the error message */
  597. if (error[0] >= 'a' && error[0] <= 'z') {
  598. error[0] += ('A' - 'a');
  599. }
  600. zend_error(E_STRICT, "%s", error);
  601. efree(error);
  602. }
  603. efree(callable_name);
  604. }
  605. EX(function_state).function = fci_cache->function_handler;
  606. calling_scope = fci_cache->calling_scope;
  607. called_scope = fci_cache->called_scope;
  608. fci->object_ptr = fci_cache->object_ptr;
  609. EX(object) = fci->object_ptr;
  610. if (fci->object_ptr && Z_TYPE_P(fci->object_ptr) == IS_OBJECT &&
  611. (!EG(objects_store).object_buckets || !EG(objects_store).object_buckets[Z_OBJ_HANDLE_P(fci->object_ptr)].valid)) {
  612. return FAILURE;
  613. }
  614. if (EX(function_state).function->common.fn_flags & (ZEND_ACC_ABSTRACT|ZEND_ACC_DEPRECATED)) {
  615. if (EX(function_state).function->common.fn_flags & ZEND_ACC_ABSTRACT) {
  616. zend_error_noreturn(E_ERROR, "Cannot call abstract method %s::%s()", EX(function_state).function->common.scope->name, EX(function_state).function->common.function_name);
  617. }
  618. if (EX(function_state).function->common.fn_flags & ZEND_ACC_DEPRECATED) {
  619. zend_error(E_DEPRECATED, "Function %s%s%s() is deprecated",
  620. EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
  621. EX(function_state).function->common.scope ? "::" : "",
  622. EX(function_state).function->common.function_name);
  623. }
  624. }
  625. ZEND_VM_STACK_GROW_IF_NEEDED(fci->param_count + 1);
  626. for (i=0; i<fci->param_count; i++) {
  627. zval *param;
  628. if (ARG_SHOULD_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
  629. if (!PZVAL_IS_REF(*fci->params[i]) && Z_REFCOUNT_PP(fci->params[i]) > 1) {
  630. zval *new_zval;
  631. if (fci->no_separation &&
  632. !ARG_MAY_BE_SENT_BY_REF(EX(function_state).function, i + 1)) {
  633. if (i || UNEXPECTED(ZEND_VM_STACK_ELEMETS(EG(argument_stack)) == (EG(argument_stack)->top))) {
  634. /* hack to clean up the stack */
  635. zend_vm_stack_push((void *) (zend_uintptr_t)i TSRMLS_CC);
  636. zend_vm_stack_clear_multiple(0 TSRMLS_CC);
  637. }
  638. zend_error(E_WARNING, "Parameter %d to %s%s%s() expected to be a reference, value given",
  639. i+1,
  640. EX(function_state).function->common.scope ? EX(function_state).function->common.scope->name : "",
  641. EX(function_state).function->common.scope ? "::" : "",
  642. EX(function_state).function->common.function_name);
  643. return FAILURE;
  644. }
  645. ALLOC_ZVAL(new_zval);
  646. *new_zval = **fci->params[i];
  647. zval_copy_ctor(new_zval);
  648. Z_SET_REFCOUNT_P(new_zval, 1);
  649. Z_DELREF_PP(fci->params[i]);
  650. *fci->params[i] = new_zval;
  651. }
  652. Z_ADDREF_PP(fci->params[i]);
  653. Z_SET_ISREF_PP(fci->params[i]);
  654. param = *fci->params[i];
  655. } else if (PZVAL_IS_REF(*fci->params[i]) &&
  656. /* don't separate references for __call */
  657. (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) == 0 ) {
  658. ALLOC_ZVAL(param);
  659. *param = **(fci->params[i]);
  660. INIT_PZVAL(param);
  661. zval_copy_ctor(param);
  662. } else if (*fci->params[i] != &EG(uninitialized_zval)) {
  663. Z_ADDREF_PP(fci->params[i]);
  664. param = *fci->params[i];
  665. } else {
  666. ALLOC_ZVAL(param);
  667. *param = **(fci->params[i]);
  668. INIT_PZVAL(param);
  669. }
  670. zend_vm_stack_push(param TSRMLS_CC);
  671. }
  672. EX(function_state).arguments = zend_vm_stack_top(TSRMLS_C);
  673. zend_vm_stack_push((void*)(zend_uintptr_t)fci->param_count TSRMLS_CC);
  674. current_scope = EG(scope);
  675. EG(scope) = calling_scope;
  676. current_this = EG(This);
  677. current_called_scope = EG(called_scope);
  678. if (called_scope) {
  679. EG(called_scope) = called_scope;
  680. } else if (EX(function_state).function->type != ZEND_INTERNAL_FUNCTION) {
  681. EG(called_scope) = NULL;
  682. }
  683. if (fci->object_ptr) {
  684. if ((EX(function_state).function->common.fn_flags & ZEND_ACC_STATIC)) {
  685. EG(This) = NULL;
  686. } else {
  687. EG(This) = fci->object_ptr;
  688. if (!PZVAL_IS_REF(EG(This))) {
  689. Z_ADDREF_P(EG(This)); /* For $this pointer */
  690. } else {
  691. zval *this_ptr;
  692. ALLOC_ZVAL(this_ptr);
  693. *this_ptr = *EG(This);
  694. INIT_PZVAL(this_ptr);
  695. zval_copy_ctor(this_ptr);
  696. EG(This) = this_ptr;
  697. }
  698. }
  699. } else {
  700. EG(This) = NULL;
  701. }
  702. EX(prev_execute_data) = EG(current_execute_data);
  703. EG(current_execute_data) = &execute_data;
  704. if (EX(function_state).function->type == ZEND_USER_FUNCTION) {
  705. calling_symbol_table = EG(active_symbol_table);
  706. EG(scope) = EX(function_state).function->common.scope;
  707. if (fci->symbol_table) {
  708. EG(active_symbol_table) = fci->symbol_table;
  709. } else {
  710. EG(active_symbol_table) = NULL;
  711. }
  712. original_return_value = EG(return_value_ptr_ptr);
  713. original_op_array = EG(active_op_array);
  714. EG(return_value_ptr_ptr) = fci->retval_ptr_ptr;
  715. EG(active_op_array) = (zend_op_array *) EX(function_state).function;
  716. original_opline_ptr = EG(opline_ptr);
  717. if (EG(active_op_array)->fn_flags & ZEND_ACC_GENERATOR) {
  718. *fci->retval_ptr_ptr = zend_generator_create_zval(EG(active_op_array) TSRMLS_CC);
  719. } else {
  720. const zend_op *current_opline_before_exception = EG(opline_before_exception);
  721. zend_execute(EG(active_op_array) TSRMLS_CC);
  722. EG(opline_before_exception) = current_opline_before_exception;
  723. }
  724. if (!fci->symbol_table && EG(active_symbol_table)) {
  725. zend_clean_and_cache_symbol_table(EG(active_symbol_table) TSRMLS_CC);
  726. }
  727. EG(active_symbol_table) = calling_symbol_table;
  728. EG(active_op_array) = original_op_array;
  729. EG(return_value_ptr_ptr)=original_return_value;
  730. EG(opline_ptr) = original_opline_ptr;
  731. } else if (EX(function_state).function->type == ZEND_INTERNAL_FUNCTION) {
  732. int call_via_handler = (EX(function_state).function->common.fn_flags & ZEND_ACC_CALL_VIA_HANDLER) != 0;
  733. ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
  734. if (EX(function_state).function->common.scope) {
  735. EG(scope) = EX(function_state).function->common.scope;
  736. }
  737. if (EXPECTED(zend_execute_internal == NULL)) {
  738. /* saves one function call if zend_execute_internal is not used */
  739. EX(function_state).function->internal_function.handler(fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, 1 TSRMLS_CC);
  740. } else {
  741. zend_execute_internal(&execute_data, fci, 1 TSRMLS_CC);
  742. }
  743. /* We shouldn't fix bad extensions here,
  744. because it can break proper ones (Bug #34045)
  745. if (!EX(function_state).function->common.return_reference)
  746. {
  747. INIT_PZVAL(*fci->retval_ptr_ptr);
  748. }*/
  749. if (EG(exception) && fci->retval_ptr_ptr) {
  750. zval_ptr_dtor(fci->retval_ptr_ptr);
  751. *fci->retval_ptr_ptr = NULL;
  752. }
  753. if (call_via_handler) {
  754. /* We must re-initialize function again */
  755. fci_cache->initialized = 0;
  756. }
  757. } else { /* ZEND_OVERLOADED_FUNCTION */
  758. ALLOC_INIT_ZVAL(*fci->retval_ptr_ptr);
  759. /* Not sure what should be done here if it's a static method */
  760. if (fci->object_ptr) {
  761. Z_OBJ_HT_P(fci->object_ptr)->call_method(EX(function_state).function->common.function_name, fci->param_count, *fci->retval_ptr_ptr, fci->retval_ptr_ptr, fci->object_ptr, 1 TSRMLS_CC);
  762. } else {
  763. zend_error_noreturn(E_ERROR, "Cannot call overloaded function for non-object");
  764. }
  765. if (EX(function_state).function->type == ZEND_OVERLOADED_FUNCTION_TEMPORARY) {
  766. efree((char*)EX(function_state).function->common.function_name);
  767. }
  768. efree(EX(function_state).function);
  769. if (EG(exception) && fci->retval_ptr_ptr) {
  770. zval_ptr_dtor(fci->retval_ptr_ptr);
  771. *fci->retval_ptr_ptr = NULL;
  772. }
  773. }
  774. zend_vm_stack_clear_multiple(0 TSRMLS_CC);
  775. if (EG(This)) {
  776. zval_ptr_dtor(&EG(This));
  777. }
  778. EG(called_scope) = current_called_scope;
  779. EG(scope) = current_scope;
  780. EG(This) = current_this;
  781. EG(current_execute_data) = EX(prev_execute_data);
  782. if (EG(exception)) {
  783. zend_throw_exception_internal(NULL TSRMLS_CC);
  784. }
  785. return SUCCESS;
  786. }
  787. /* }}} */
  788. ZEND_API int zend_lookup_class_ex(const char *name, int name_length, const zend_literal *key, int use_autoload, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
  789. {
  790. zval **args[1];
  791. zval autoload_function;
  792. zval *class_name_ptr;
  793. zval *retval_ptr = NULL;
  794. int retval, lc_length;
  795. char *lc_name;
  796. char *lc_free;
  797. zend_fcall_info fcall_info;
  798. zend_fcall_info_cache fcall_cache;
  799. char dummy = 1;
  800. ulong hash;
  801. ALLOCA_FLAG(use_heap)
  802. if (key) {
  803. lc_name = Z_STRVAL(key->constant);
  804. lc_length = Z_STRLEN(key->constant) + 1;
  805. hash = key->hash_value;
  806. } else {
  807. if (name == NULL || !name_length) {
  808. return FAILURE;
  809. }
  810. lc_free = lc_name = do_alloca(name_length + 1, use_heap);
  811. zend_str_tolower_copy(lc_name, name, name_length);
  812. lc_length = name_length + 1;
  813. if (lc_name[0] == '\\') {
  814. lc_name += 1;
  815. lc_length -= 1;
  816. }
  817. hash = zend_inline_hash_func(lc_name, lc_length);
  818. }
  819. if (zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce) == SUCCESS) {
  820. if (!key) {
  821. free_alloca(lc_free, use_heap);
  822. }
  823. return SUCCESS;
  824. }
  825. /* The compiler is not-reentrant. Make sure we __autoload() only during run-time
  826. * (doesn't impact functionality of __autoload()
  827. */
  828. if (!use_autoload || zend_is_compiling(TSRMLS_C)) {
  829. if (!key) {
  830. free_alloca(lc_free, use_heap);
  831. }
  832. return FAILURE;
  833. }
  834. /* Verify class name before passing it to __autoload() */
  835. if (strspn(name, "0123456789_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\177\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\\") != name_length) {
  836. if (!key) {
  837. free_alloca(lc_free, use_heap);
  838. }
  839. return FAILURE;
  840. }
  841. if (EG(in_autoload) == NULL) {
  842. ALLOC_HASHTABLE(EG(in_autoload));
  843. zend_hash_init(EG(in_autoload), 0, NULL, NULL, 0);
  844. }
  845. if (zend_hash_quick_add(EG(in_autoload), lc_name, lc_length, hash, (void**)&dummy, sizeof(char), NULL) == FAILURE) {
  846. if (!key) {
  847. free_alloca(lc_free, use_heap);
  848. }
  849. return FAILURE;
  850. }
  851. ZVAL_STRINGL(&autoload_function, ZEND_AUTOLOAD_FUNC_NAME, sizeof(ZEND_AUTOLOAD_FUNC_NAME) - 1, 0);
  852. ALLOC_ZVAL(class_name_ptr);
  853. INIT_PZVAL(class_name_ptr);
  854. if (name[0] == '\\') {
  855. ZVAL_STRINGL(class_name_ptr, name+1, name_length-1, 1);
  856. } else {
  857. ZVAL_STRINGL(class_name_ptr, name, name_length, 1);
  858. }
  859. args[0] = &class_name_ptr;
  860. fcall_info.size = sizeof(fcall_info);
  861. fcall_info.function_table = EG(function_table);
  862. fcall_info.function_name = &autoload_function;
  863. fcall_info.symbol_table = NULL;
  864. fcall_info.retval_ptr_ptr = &retval_ptr;
  865. fcall_info.param_count = 1;
  866. fcall_info.params = args;
  867. fcall_info.object_ptr = NULL;
  868. fcall_info.no_separation = 1;
  869. fcall_cache.initialized = EG(autoload_func) ? 1 : 0;
  870. fcall_cache.function_handler = EG(autoload_func);
  871. fcall_cache.calling_scope = NULL;
  872. fcall_cache.called_scope = NULL;
  873. fcall_cache.object_ptr = NULL;
  874. zend_exception_save(TSRMLS_C);
  875. retval = zend_call_function(&fcall_info, &fcall_cache TSRMLS_CC);
  876. zend_exception_restore(TSRMLS_C);
  877. EG(autoload_func) = fcall_cache.function_handler;
  878. zval_ptr_dtor(&class_name_ptr);
  879. zend_hash_quick_del(EG(in_autoload), lc_name, lc_length, hash);
  880. if (retval_ptr) {
  881. zval_ptr_dtor(&retval_ptr);
  882. }
  883. if (retval == SUCCESS) {
  884. retval = zend_hash_quick_find(EG(class_table), lc_name, lc_length, hash, (void **) ce);
  885. }
  886. if (!key) {
  887. free_alloca(lc_free, use_heap);
  888. }
  889. return retval;
  890. }
  891. /* }}} */
  892. ZEND_API int zend_lookup_class(const char *name, int name_length, zend_class_entry ***ce TSRMLS_DC) /* {{{ */
  893. {
  894. return zend_lookup_class_ex(name, name_length, NULL, 1, ce TSRMLS_CC);
  895. }
  896. /* }}} */
  897. ZEND_API int zend_eval_stringl(char *str, int str_len, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
  898. {
  899. zval pv;
  900. zend_op_array *new_op_array;
  901. zend_op_array *original_active_op_array = EG(active_op_array);
  902. zend_uint original_compiler_options;
  903. int retval;
  904. if (retval_ptr) {
  905. Z_STRLEN(pv) = str_len + sizeof("return ;") - 1;
  906. Z_STRVAL(pv) = emalloc(Z_STRLEN(pv) + 1);
  907. memcpy(Z_STRVAL(pv), "return ", sizeof("return ") - 1);
  908. memcpy(Z_STRVAL(pv) + sizeof("return ") - 1, str, str_len);
  909. Z_STRVAL(pv)[Z_STRLEN(pv) - 1] = ';';
  910. Z_STRVAL(pv)[Z_STRLEN(pv)] = '\0';
  911. } else {
  912. Z_STRLEN(pv) = str_len;
  913. Z_STRVAL(pv) = str;
  914. }
  915. Z_TYPE(pv) = IS_STRING;
  916. /*printf("Evaluating '%s'\n", pv.value.str.val);*/
  917. original_compiler_options = CG(compiler_options);
  918. CG(compiler_options) = ZEND_COMPILE_DEFAULT_FOR_EVAL;
  919. new_op_array = zend_compile_string(&pv, string_name TSRMLS_CC);
  920. CG(compiler_options) = original_compiler_options;
  921. if (new_op_array) {
  922. zval *local_retval_ptr=NULL;
  923. zval **original_return_value_ptr_ptr = EG(return_value_ptr_ptr);
  924. zend_op **original_opline_ptr = EG(opline_ptr);
  925. int orig_interactive = CG(interactive);
  926. EG(return_value_ptr_ptr) = &local_retval_ptr;
  927. EG(active_op_array) = new_op_array;
  928. EG(no_extensions)=1;
  929. if (!EG(active_symbol_table)) {
  930. zend_rebuild_symbol_table(TSRMLS_C);
  931. }
  932. CG(interactive) = 0;
  933. zend_try {
  934. zend_execute(new_op_array TSRMLS_CC);
  935. } zend_catch {
  936. destroy_op_array(new_op_array TSRMLS_CC);
  937. efree(new_op_array);
  938. zend_bailout();
  939. } zend_end_try();
  940. CG(interactive) = orig_interactive;
  941. if (local_retval_ptr) {
  942. if (retval_ptr) {
  943. COPY_PZVAL_TO_ZVAL(*retval_ptr, local_retval_ptr);
  944. } else {
  945. zval_ptr_dtor(&local_retval_ptr);
  946. }
  947. } else {
  948. if (retval_ptr) {
  949. INIT_ZVAL(*retval_ptr);
  950. }
  951. }
  952. EG(no_extensions)=0;
  953. EG(opline_ptr) = original_opline_ptr;
  954. EG(active_op_array) = original_active_op_array;
  955. destroy_op_array(new_op_array TSRMLS_CC);
  956. efree(new_op_array);
  957. EG(return_value_ptr_ptr) = original_return_value_ptr_ptr;
  958. retval = SUCCESS;
  959. } else {
  960. retval = FAILURE;
  961. }
  962. if (retval_ptr) {
  963. zval_dtor(&pv);
  964. }
  965. return retval;
  966. }
  967. /* }}} */
  968. ZEND_API int zend_eval_string(char *str, zval *retval_ptr, char *string_name TSRMLS_DC) /* {{{ */
  969. {
  970. return zend_eval_stringl(str, strlen(str), retval_ptr, string_name TSRMLS_CC);
  971. }
  972. /* }}} */
  973. ZEND_API int zend_eval_stringl_ex(char *str, int str_len, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
  974. {
  975. int result;
  976. result = zend_eval_stringl(str, str_len, retval_ptr, string_name TSRMLS_CC);
  977. if (handle_exceptions && EG(exception)) {
  978. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  979. result = FAILURE;
  980. }
  981. return result;
  982. }
  983. /* }}} */
  984. ZEND_API int zend_eval_string_ex(char *str, zval *retval_ptr, char *string_name, int handle_exceptions TSRMLS_DC) /* {{{ */
  985. {
  986. return zend_eval_stringl_ex(str, strlen(str), retval_ptr, string_name, handle_exceptions TSRMLS_CC);
  987. }
  988. /* }}} */
  989. void execute_new_code(TSRMLS_D) /* {{{ */
  990. {
  991. zend_op *opline, *end;
  992. zend_op *ret_opline;
  993. int orig_interactive;
  994. if (!(CG(active_op_array)->fn_flags & ZEND_ACC_INTERACTIVE)
  995. || CG(context).backpatch_count>0
  996. || CG(active_op_array)->function_name
  997. || CG(active_op_array)->type!=ZEND_USER_FUNCTION) {
  998. return;
  999. }
  1000. ret_opline = get_next_op(CG(active_op_array) TSRMLS_CC);
  1001. ret_opline->opcode = ZEND_RETURN;
  1002. ret_opline->op1_type = IS_CONST;
  1003. ret_opline->op1.constant = zend_add_literal(CG(active_op_array), &EG(uninitialized_zval) TSRMLS_CC);
  1004. SET_UNUSED(ret_opline->op2);
  1005. if (!EG(start_op)) {
  1006. EG(start_op) = CG(active_op_array)->opcodes;
  1007. }
  1008. opline=EG(start_op);
  1009. end=CG(active_op_array)->opcodes+CG(active_op_array)->last;
  1010. while (opline<end) {
  1011. if (opline->op1_type == IS_CONST) {
  1012. opline->op1.zv = &CG(active_op_array)->literals[opline->op1.constant].constant;
  1013. }
  1014. if (opline->op2_type == IS_CONST) {
  1015. opline->op2.zv = &CG(active_op_array)->literals[opline->op2.constant].constant;
  1016. }
  1017. switch (opline->opcode) {
  1018. case ZEND_GOTO:
  1019. if (Z_TYPE_P(opline->op2.zv) != IS_LONG) {
  1020. zend_resolve_goto_label(CG(active_op_array), opline, 1 TSRMLS_CC);
  1021. }
  1022. /* break omitted intentionally */
  1023. case ZEND_JMP:
  1024. opline->op1.jmp_addr = &CG(active_op_array)->opcodes[opline->op1.opline_num];
  1025. break;
  1026. case ZEND_JMPZ:
  1027. case ZEND_JMPNZ:
  1028. case ZEND_JMPZ_EX:
  1029. case ZEND_JMPNZ_EX:
  1030. case ZEND_JMP_SET:
  1031. case ZEND_JMP_SET_VAR:
  1032. opline->op2.jmp_addr = &CG(active_op_array)->opcodes[opline->op2.opline_num];
  1033. break;
  1034. }
  1035. ZEND_VM_SET_OPCODE_HANDLER(opline);
  1036. opline++;
  1037. }
  1038. zend_release_labels(1 TSRMLS_CC);
  1039. EG(return_value_ptr_ptr) = NULL;
  1040. EG(active_op_array) = CG(active_op_array);
  1041. orig_interactive = CG(interactive);
  1042. CG(interactive) = 0;
  1043. zend_execute(CG(active_op_array) TSRMLS_CC);
  1044. CG(interactive) = orig_interactive;
  1045. if (EG(exception)) {
  1046. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  1047. }
  1048. CG(active_op_array)->last -= 1; /* get rid of that ZEND_RETURN */
  1049. EG(start_op) = CG(active_op_array)->opcodes+CG(active_op_array)->last;
  1050. }
  1051. /* }}} */
  1052. ZEND_API void zend_timeout(int dummy) /* {{{ */
  1053. {
  1054. TSRMLS_FETCH();
  1055. if (zend_on_timeout) {
  1056. #ifdef ZEND_SIGNALS
  1057. /*
  1058. We got here because we got a timeout signal, so we are in a signal handler
  1059. at this point. However, we want to be able to timeout any user-supplied
  1060. shutdown functions, so pretend we are not in a signal handler while we are
  1061. calling these
  1062. */
  1063. SIGG(running) = 0;
  1064. #endif
  1065. zend_on_timeout(EG(timeout_seconds) TSRMLS_CC);
  1066. }
  1067. zend_error(E_ERROR, "Maximum execution time of %d second%s exceeded", EG(timeout_seconds), EG(timeout_seconds) == 1 ? "" : "s");
  1068. }
  1069. /* }}} */
  1070. #ifdef ZEND_WIN32
  1071. VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out)
  1072. {
  1073. zend_bool *php_timed_out;
  1074. /* The doc states it'll be always true, however it theoretically
  1075. could be FALSE when the thread was signaled. */
  1076. if (!timed_out) {
  1077. return;
  1078. }
  1079. php_timed_out = (zend_bool *)arg;
  1080. *php_timed_out = 1;
  1081. }
  1082. #endif
  1083. /* This one doesn't exists on QNX */
  1084. #ifndef SIGPROF
  1085. #define SIGPROF 27
  1086. #endif
  1087. void zend_set_timeout(long seconds, int reset_signals) /* {{{ */
  1088. {
  1089. TSRMLS_FETCH();
  1090. EG(timeout_seconds) = seconds;
  1091. #ifdef ZEND_WIN32
  1092. if(!seconds) {
  1093. return;
  1094. }
  1095. /* Don't use ChangeTimerQueueTimer() as it will not restart an expired
  1096. timer, so we could end up with just an ignored timeout. Instead
  1097. delete and recreate. */
  1098. if (NULL != tq_timer) {
  1099. if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
  1100. EG(timed_out) = 0;
  1101. tq_timer = NULL;
  1102. zend_error(E_ERROR, "Could not delete queued timer");
  1103. return;
  1104. }
  1105. tq_timer = NULL;
  1106. }
  1107. /* XXX passing NULL means the default timer queue provided by the system is used */
  1108. if (!CreateTimerQueueTimer(&tq_timer, NULL, (WAITORTIMERCALLBACK)tq_timer_cb, (VOID*)&EG(timed_out), seconds*1000, 0, WT_EXECUTEONLYONCE)) {
  1109. EG(timed_out) = 0;
  1110. tq_timer = NULL;
  1111. zend_error(E_ERROR, "Could not queue new timer");
  1112. return;
  1113. }
  1114. EG(timed_out) = 0;
  1115. #else
  1116. # ifdef HAVE_SETITIMER
  1117. {
  1118. struct itimerval t_r; /* timeout requested */
  1119. int signo;
  1120. if(seconds) {
  1121. t_r.it_value.tv_sec = seconds;
  1122. t_r.it_value.tv_usec = t_r.it_interval.tv_sec = t_r.it_interval.tv_usec = 0;
  1123. # ifdef __CYGWIN__
  1124. setitimer(ITIMER_REAL, &t_r, NULL);
  1125. }
  1126. signo = SIGALRM;
  1127. # else
  1128. setitimer(ITIMER_PROF, &t_r, NULL);
  1129. }
  1130. signo = SIGPROF;
  1131. # endif
  1132. if (reset_signals) {
  1133. # ifdef ZEND_SIGNALS
  1134. zend_signal(signo, zend_timeout TSRMLS_CC);
  1135. # else
  1136. sigset_t sigset;
  1137. signal(signo, zend_timeout);
  1138. sigemptyset(&sigset);
  1139. sigaddset(&sigset, signo);
  1140. sigprocmask(SIG_UNBLOCK, &sigset, NULL);
  1141. # endif
  1142. }
  1143. }
  1144. # endif /* HAVE_SETITIMER */
  1145. #endif
  1146. }
  1147. /* }}} */
  1148. void zend_unset_timeout(TSRMLS_D) /* {{{ */
  1149. {
  1150. #ifdef ZEND_WIN32
  1151. if (NULL != tq_timer) {
  1152. if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
  1153. EG(timed_out) = 0;
  1154. tq_timer = NULL;
  1155. zend_error(E_ERROR, "Could not delete queued timer");
  1156. return;
  1157. }
  1158. tq_timer = NULL;
  1159. }
  1160. EG(timed_out) = 0;
  1161. #else
  1162. # ifdef HAVE_SETITIMER
  1163. if (EG(timeout_seconds)) {
  1164. struct itimerval no_timeout;
  1165. 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;
  1166. #ifdef __CYGWIN__
  1167. setitimer(ITIMER_REAL, &no_timeout, NULL);
  1168. #else
  1169. setitimer(ITIMER_PROF, &no_timeout, NULL);
  1170. #endif
  1171. }
  1172. # endif
  1173. #endif
  1174. }
  1175. /* }}} */
  1176. zend_class_entry *zend_fetch_class(const char *class_name, uint class_name_len, int fetch_type TSRMLS_DC) /* {{{ */
  1177. {
  1178. zend_class_entry **pce;
  1179. int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
  1180. int silent = (fetch_type & ZEND_FETCH_CLASS_SILENT) != 0;
  1181. fetch_type &= ZEND_FETCH_CLASS_MASK;
  1182. check_fetch_type:
  1183. switch (fetch_type) {
  1184. case ZEND_FETCH_CLASS_SELF:
  1185. if (!EG(scope)) {
  1186. zend_error(E_ERROR, "Cannot access self:: when no class scope is active");
  1187. }
  1188. return EG(scope);
  1189. case ZEND_FETCH_CLASS_PARENT:
  1190. if (!EG(scope)) {
  1191. zend_error(E_ERROR, "Cannot access parent:: when no class scope is active");
  1192. }
  1193. if (!EG(scope)->parent) {
  1194. zend_error(E_ERROR, "Cannot access parent:: when current class scope has no parent");
  1195. }
  1196. return EG(scope)->parent;
  1197. case ZEND_FETCH_CLASS_STATIC:
  1198. if (!EG(called_scope)) {
  1199. zend_error(E_ERROR, "Cannot access static:: when no class scope is active");
  1200. }
  1201. return EG(called_scope);
  1202. case ZEND_FETCH_CLASS_AUTO: {
  1203. fetch_type = zend_get_class_fetch_type(class_name, class_name_len);
  1204. if (fetch_type!=ZEND_FETCH_CLASS_DEFAULT) {
  1205. goto check_fetch_type;
  1206. }
  1207. }
  1208. break;
  1209. }
  1210. if (zend_lookup_class_ex(class_name, class_name_len, NULL, use_autoload, &pce TSRMLS_CC) == FAILURE) {
  1211. if (use_autoload) {
  1212. if (!silent && !EG(exception)) {
  1213. if (fetch_type == ZEND_FETCH_CLASS_INTERFACE) {
  1214. zend_error(E_ERROR, "Interface '%s' not found", class_name);
  1215. } else if (fetch_type == ZEND_FETCH_CLASS_TRAIT) {
  1216. zend_error(E_ERROR, "Trait '%s' not found", class_name);
  1217. } else {
  1218. zend_error(E_ERROR, "Class '%s' not found", class_name);
  1219. }
  1220. }
  1221. }
  1222. return NULL;
  1223. }
  1224. return *pce;
  1225. }
  1226. /* }}} */
  1227. zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC) /* {{{ */
  1228. {
  1229. zend_class_entry **pce;
  1230. int use_autoload = (fetch_type & ZEND_FETCH_CLASS_NO_AUTOLOAD) == 0;
  1231. if (zend_lookup_class_ex(class_name, class_name_len, key, use_autoload, &pce TSRMLS_CC) == FAILURE) {
  1232. if (use_autoload) {
  1233. if ((fetch_type & ZEND_FETCH_CLASS_SILENT) == 0 && !EG(exception)) {
  1234. if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_INTERFACE) {
  1235. zend_error(E_ERROR, "Interface '%s' not found", class_name);
  1236. } else if ((fetch_type & ZEND_FETCH_CLASS_MASK) == ZEND_FETCH_CLASS_TRAIT) {
  1237. zend_error(E_ERROR, "Trait '%s' not found", class_name);
  1238. } else {
  1239. zend_error(E_ERROR, "Class '%s' not found", class_name);
  1240. }
  1241. }
  1242. }
  1243. return NULL;
  1244. }
  1245. return *pce;
  1246. }
  1247. /* }}} */
  1248. #define MAX_ABSTRACT_INFO_CNT 3
  1249. #define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
  1250. #define DISPLAY_ABSTRACT_FN(idx) \
  1251. ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
  1252. ai.afn[idx] ? "::" : "", \
  1253. ai.afn[idx] ? ai.afn[idx]->common.function_name : "", \
  1254. ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
  1255. typedef struct _zend_abstract_info {
  1256. zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
  1257. int cnt;
  1258. int ctor;
  1259. } zend_abstract_info;
  1260. static int zend_verify_abstract_class_function(zend_function *fn, zend_abstract_info *ai TSRMLS_DC) /* {{{ */
  1261. {
  1262. if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
  1263. if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
  1264. ai->afn[ai->cnt] = fn;
  1265. }
  1266. if (fn->common.fn_flags & ZEND_ACC_CTOR) {
  1267. if (!ai->ctor) {
  1268. ai->cnt++;
  1269. ai->ctor = 1;
  1270. } else {
  1271. ai->afn[ai->cnt] = NULL;
  1272. }
  1273. } else {
  1274. ai->cnt++;
  1275. }
  1276. }
  1277. return 0;
  1278. }
  1279. /* }}} */
  1280. void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC) /* {{{ */
  1281. {
  1282. zend_abstract_info ai;
  1283. if ((ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) && !(ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) {
  1284. memset(&ai, 0, sizeof(ai));
  1285. zend_hash_apply_with_argument(&ce->function_table, (apply_func_arg_t) zend_verify_abstract_class_function, &ai TSRMLS_CC);
  1286. if (ai.cnt) {
  1287. zend_error(E_ERROR, "Class %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
  1288. ce->name, ai.cnt,
  1289. ai.cnt > 1 ? "s" : "",
  1290. DISPLAY_ABSTRACT_FN(0),
  1291. DISPLAY_ABSTRACT_FN(1),
  1292. DISPLAY_ABSTRACT_FN(2)
  1293. );
  1294. }
  1295. }
  1296. }
  1297. /* }}} */
  1298. ZEND_API void zend_reset_all_cv(HashTable *symbol_table TSRMLS_DC) /* {{{ */
  1299. {
  1300. zend_execute_data *ex;
  1301. int i;
  1302. for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
  1303. if (ex->op_array && ex->symbol_table == symbol_table) {
  1304. for (i = 0; i < ex->op_array->last_var; i++) {
  1305. *EX_CV_NUM(ex, i) = NULL;
  1306. }
  1307. }
  1308. }
  1309. }
  1310. /* }}} */
  1311. ZEND_API void zend_delete_variable(zend_execute_data *ex, HashTable *ht, const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
  1312. {
  1313. if (zend_hash_quick_del(ht, name, name_len, hash_value) == SUCCESS) {
  1314. name_len--;
  1315. while (ex && ex->symbol_table == ht) {
  1316. int i;
  1317. if (ex->op_array) {
  1318. for (i = 0; i < ex->op_array->last_var; i++) {
  1319. if (ex->op_array->vars[i].hash_value == hash_value &&
  1320. ex->op_array->vars[i].name_len == name_len &&
  1321. !memcmp(ex->op_array->vars[i].name, name, name_len)) {
  1322. *EX_CV_NUM(ex, i) = NULL;
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. ex = ex->prev_execute_data;
  1328. }
  1329. }
  1330. }
  1331. /* }}} */
  1332. ZEND_API int zend_delete_global_variable_ex(const char *name, int name_len, ulong hash_value TSRMLS_DC) /* {{{ */
  1333. {
  1334. zend_execute_data *ex;
  1335. if (zend_hash_quick_exists(&EG(symbol_table), name, name_len + 1, hash_value)) {
  1336. for (ex = EG(current_execute_data); ex; ex = ex->prev_execute_data) {
  1337. if (ex->op_array && ex->symbol_table == &EG(symbol_table)) {
  1338. int i;
  1339. for (i = 0; i < ex->op_array->last_var; i++) {
  1340. if (ex->op_array->vars[i].hash_value == hash_value &&
  1341. ex->op_array->vars[i].name_len == name_len &&
  1342. !memcmp(ex->op_array->vars[i].name, name, name_len)
  1343. ) {
  1344. *EX_CV_NUM(ex, i) = NULL;
  1345. break;
  1346. }
  1347. }
  1348. }
  1349. }
  1350. return zend_hash_quick_del(&EG(symbol_table), name, name_len + 1, hash_value);
  1351. }
  1352. return FAILURE;
  1353. }
  1354. /* }}} */
  1355. ZEND_API int zend_delete_global_variable(const char *name, int name_len TSRMLS_DC) /* {{{ */
  1356. {
  1357. return zend_delete_global_variable_ex(name, name_len, zend_inline_hash_func(name, name_len + 1) TSRMLS_CC);
  1358. }
  1359. /* }}} */
  1360. ZEND_API void zend_rebuild_symbol_table(TSRMLS_D) /* {{{ */
  1361. {
  1362. zend_uint i;
  1363. zend_execute_data *ex;
  1364. if (!EG(active_symbol_table)) {
  1365. /* Search for last called user function */
  1366. ex = EG(current_execute_data);
  1367. while (ex && !ex->op_array) {
  1368. ex = ex->prev_execute_data;
  1369. }
  1370. if (ex && ex->symbol_table) {
  1371. EG(active_symbol_table) = ex->symbol_table;
  1372. return;
  1373. }
  1374. if (ex && ex->op_array) {
  1375. if (EG(symtable_cache_ptr)>=EG(symtable_cache)) {
  1376. /*printf("Cache hit! Reusing %x\n", symtable_cache[symtable_cache_ptr]);*/
  1377. EG(active_symbol_table) = *(EG(symtable_cache_ptr)--);
  1378. } else {
  1379. ALLOC_HASHTABLE(EG(active_symbol_table));
  1380. zend_hash_init(EG(active_symbol_table), ex->op_array->last_var, NULL, ZVAL_PTR_DTOR, 0);
  1381. /*printf("Cache miss! Initialized %x\n", EG(active_symbol_table));*/
  1382. }
  1383. ex->symbol_table = EG(active_symbol_table);
  1384. for (i = 0; i < ex->op_array->last_var; i++) {
  1385. if (*EX_CV_NUM(ex, i)) {
  1386. if (UNEXPECTED(**EX_CV_NUM(ex, i) == &EG(uninitialized_zval))) {
  1387. Z_DELREF(EG(uninitialized_zval));
  1388. ALLOC_INIT_ZVAL(**EX_CV_NUM(ex, i));
  1389. }
  1390. zend_hash_quick_update(EG(active_symbol_table),
  1391. ex->op_array->vars[i].name,
  1392. ex->op_array->vars[i].name_len + 1,
  1393. ex->op_array->vars[i].hash_value,
  1394. (void**)*EX_CV_NUM(ex, i),
  1395. sizeof(zval*),
  1396. (void**)EX_CV_NUM(ex, i));
  1397. }
  1398. }
  1399. }
  1400. }
  1401. }
  1402. /* }}} */
  1403. /*
  1404. * Local variables:
  1405. * tab-width: 4
  1406. * c-basic-offset: 4
  1407. * indent-tabs-mode: t
  1408. * End:
  1409. */