zend.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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 "zend.h"
  21. #include "zend_extensions.h"
  22. #include "zend_modules.h"
  23. #include "zend_constants.h"
  24. #include "zend_list.h"
  25. #include "zend_API.h"
  26. #include "zend_exceptions.h"
  27. #include "zend_builtin_functions.h"
  28. #include "zend_ini.h"
  29. #include "zend_vm.h"
  30. #include "zend_dtrace.h"
  31. #include "zend_virtual_cwd.h"
  32. #ifdef ZTS
  33. # define GLOBAL_FUNCTION_TABLE global_function_table
  34. # define GLOBAL_CLASS_TABLE global_class_table
  35. # define GLOBAL_CONSTANTS_TABLE global_constants_table
  36. # define GLOBAL_AUTO_GLOBALS_TABLE global_auto_globals_table
  37. #else
  38. # define GLOBAL_FUNCTION_TABLE CG(function_table)
  39. # define GLOBAL_CLASS_TABLE CG(class_table)
  40. # define GLOBAL_AUTO_GLOBALS_TABLE CG(auto_globals)
  41. # define GLOBAL_CONSTANTS_TABLE EG(zend_constants)
  42. #endif
  43. /* true multithread-shared globals */
  44. ZEND_API zend_class_entry *zend_standard_class_def = NULL;
  45. ZEND_API int (*zend_printf)(const char *format, ...);
  46. ZEND_API zend_write_func_t zend_write;
  47. ZEND_API FILE *(*zend_fopen)(const char *filename, char **opened_path TSRMLS_DC);
  48. ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle TSRMLS_DC);
  49. ZEND_API void (*zend_block_interruptions)(void);
  50. ZEND_API void (*zend_unblock_interruptions)(void);
  51. ZEND_API void (*zend_ticks_function)(int ticks);
  52. ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint error_lineno, const char *format, va_list args);
  53. int (*zend_vspprintf)(char **pbuf, size_t max_len, const char *format, va_list ap);
  54. ZEND_API char *(*zend_getenv)(char *name, size_t name_len TSRMLS_DC);
  55. ZEND_API char *(*zend_resolve_path)(const char *filename, int filename_len TSRMLS_DC);
  56. void (*zend_on_timeout)(int seconds TSRMLS_DC);
  57. static void (*zend_message_dispatcher_p)(long message, const void *data TSRMLS_DC);
  58. static int (*zend_get_configuration_directive_p)(const char *name, uint name_length, zval *contents);
  59. static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
  60. {
  61. if (!new_value) {
  62. EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
  63. } else {
  64. EG(error_reporting) = atoi(new_value);
  65. }
  66. return SUCCESS;
  67. }
  68. /* }}} */
  69. static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
  70. {
  71. OnUpdateBool(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
  72. if (GC_G(gc_enabled)) {
  73. gc_init(TSRMLS_C);
  74. }
  75. return SUCCESS;
  76. }
  77. /* }}} */
  78. static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
  79. {
  80. if (!CG(multibyte)) {
  81. return FAILURE;
  82. }
  83. if (!zend_multibyte_get_functions(TSRMLS_C)) {
  84. return SUCCESS;
  85. }
  86. return zend_multibyte_set_script_encoding_by_string(new_value, new_value_length TSRMLS_CC);
  87. }
  88. /* }}} */
  89. ZEND_INI_BEGIN()
  90. ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting)
  91. STD_ZEND_INI_BOOLEAN("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, gc_enabled, zend_gc_globals, gc_globals)
  92. STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals)
  93. ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding)
  94. STD_ZEND_INI_BOOLEAN("zend.detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
  95. #ifdef ZEND_SIGNALS
  96. STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
  97. #endif
  98. ZEND_INI_END()
  99. #ifdef ZTS
  100. ZEND_API int compiler_globals_id;
  101. ZEND_API int executor_globals_id;
  102. static HashTable *global_function_table = NULL;
  103. static HashTable *global_class_table = NULL;
  104. static HashTable *global_constants_table = NULL;
  105. static HashTable *global_auto_globals_table = NULL;
  106. static HashTable *global_persistent_list = NULL;
  107. #endif
  108. ZEND_API zend_utility_values zend_uv;
  109. ZEND_API zval zval_used_for_init; /* True global variable */
  110. /* version information */
  111. static char *zend_version_info;
  112. static uint zend_version_info_length;
  113. #define ZEND_CORE_VERSION_INFO "Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2016 Zend Technologies\n"
  114. #define PRINT_ZVAL_INDENT 4
  115. static void print_hash(zend_write_func_t write_func, HashTable *ht, int indent, zend_bool is_object TSRMLS_DC) /* {{{ */
  116. {
  117. zval **tmp;
  118. char *string_key;
  119. HashPosition iterator;
  120. ulong num_key;
  121. uint str_len;
  122. int i;
  123. for (i = 0; i < indent; i++) {
  124. ZEND_PUTS_EX(" ");
  125. }
  126. ZEND_PUTS_EX("(\n");
  127. indent += PRINT_ZVAL_INDENT;
  128. zend_hash_internal_pointer_reset_ex(ht, &iterator);
  129. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
  130. for (i = 0; i < indent; i++) {
  131. ZEND_PUTS_EX(" ");
  132. }
  133. ZEND_PUTS_EX("[");
  134. switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
  135. case HASH_KEY_IS_STRING:
  136. if (is_object) {
  137. const char *prop_name, *class_name;
  138. int prop_len;
  139. int mangled = zend_unmangle_property_name_ex(string_key, str_len - 1, &class_name, &prop_name, &prop_len);
  140. ZEND_WRITE_EX(prop_name, prop_len);
  141. if (class_name && mangled == SUCCESS) {
  142. if (class_name[0]=='*') {
  143. ZEND_PUTS_EX(":protected");
  144. } else {
  145. ZEND_PUTS_EX(":");
  146. ZEND_PUTS_EX(class_name);
  147. ZEND_PUTS_EX(":private");
  148. }
  149. }
  150. } else {
  151. ZEND_WRITE_EX(string_key, str_len-1);
  152. }
  153. break;
  154. case HASH_KEY_IS_LONG:
  155. {
  156. char key[25];
  157. snprintf(key, sizeof(key), "%ld", num_key);
  158. ZEND_PUTS_EX(key);
  159. }
  160. break;
  161. }
  162. ZEND_PUTS_EX("] => ");
  163. zend_print_zval_r_ex(write_func, *tmp, indent+PRINT_ZVAL_INDENT TSRMLS_CC);
  164. ZEND_PUTS_EX("\n");
  165. zend_hash_move_forward_ex(ht, &iterator);
  166. }
  167. indent -= PRINT_ZVAL_INDENT;
  168. for (i = 0; i < indent; i++) {
  169. ZEND_PUTS_EX(" ");
  170. }
  171. ZEND_PUTS_EX(")\n");
  172. }
  173. /* }}} */
  174. static void print_flat_hash(HashTable *ht TSRMLS_DC) /* {{{ */
  175. {
  176. zval **tmp;
  177. char *string_key;
  178. HashPosition iterator;
  179. ulong num_key;
  180. uint str_len;
  181. int i = 0;
  182. zend_hash_internal_pointer_reset_ex(ht, &iterator);
  183. while (zend_hash_get_current_data_ex(ht, (void **) &tmp, &iterator) == SUCCESS) {
  184. if (i++ > 0) {
  185. ZEND_PUTS(",");
  186. }
  187. ZEND_PUTS("[");
  188. switch (zend_hash_get_current_key_ex(ht, &string_key, &str_len, &num_key, 0, &iterator)) {
  189. case HASH_KEY_IS_STRING:
  190. ZEND_PUTS(string_key);
  191. break;
  192. case HASH_KEY_IS_LONG:
  193. zend_printf("%ld", num_key);
  194. break;
  195. }
  196. ZEND_PUTS("] => ");
  197. zend_print_flat_zval_r(*tmp TSRMLS_CC);
  198. zend_hash_move_forward_ex(ht, &iterator);
  199. }
  200. }
  201. /* }}} */
  202. ZEND_API void zend_make_printable_zval(zval *expr, zval *expr_copy, int *use_copy) /* {{{ */
  203. {
  204. if (Z_TYPE_P(expr)==IS_STRING) {
  205. *use_copy = 0;
  206. return;
  207. }
  208. switch (Z_TYPE_P(expr)) {
  209. case IS_NULL:
  210. Z_STRLEN_P(expr_copy) = 0;
  211. Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
  212. break;
  213. case IS_BOOL:
  214. if (Z_LVAL_P(expr)) {
  215. Z_STRLEN_P(expr_copy) = 1;
  216. Z_STRVAL_P(expr_copy) = estrndup("1", 1);
  217. } else {
  218. Z_STRLEN_P(expr_copy) = 0;
  219. Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
  220. }
  221. break;
  222. case IS_RESOURCE:
  223. Z_STRVAL_P(expr_copy) = (char *) emalloc(sizeof("Resource id #") - 1 + MAX_LENGTH_OF_LONG);
  224. Z_STRLEN_P(expr_copy) = snprintf(Z_STRVAL_P(expr_copy), sizeof("Resource id #") - 1 + MAX_LENGTH_OF_LONG, "Resource id #%ld", Z_LVAL_P(expr));
  225. break;
  226. case IS_ARRAY:
  227. zend_error(E_NOTICE, "Array to string conversion");
  228. Z_STRLEN_P(expr_copy) = sizeof("Array") - 1;
  229. Z_STRVAL_P(expr_copy) = estrndup("Array", Z_STRLEN_P(expr_copy));
  230. break;
  231. case IS_OBJECT:
  232. {
  233. TSRMLS_FETCH();
  234. if (zend_std_cast_object_tostring(expr, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
  235. break;
  236. }
  237. if (Z_OBJ_HANDLER_P(expr, cast_object)) {
  238. zval *val;
  239. ALLOC_ZVAL(val);
  240. INIT_PZVAL_COPY(val, expr);
  241. zval_copy_ctor(val);
  242. if (Z_OBJ_HANDLER_P(expr, cast_object)(val, expr_copy, IS_STRING TSRMLS_CC) == SUCCESS) {
  243. zval_ptr_dtor(&val);
  244. break;
  245. }
  246. zval_ptr_dtor(&val);
  247. }
  248. if (!Z_OBJ_HANDLER_P(expr, cast_object) && Z_OBJ_HANDLER_P(expr, get)) {
  249. zval *z = Z_OBJ_HANDLER_P(expr, get)(expr TSRMLS_CC);
  250. Z_ADDREF_P(z);
  251. if (Z_TYPE_P(z) != IS_OBJECT) {
  252. zend_make_printable_zval(z, expr_copy, use_copy);
  253. if (*use_copy) {
  254. zval_ptr_dtor(&z);
  255. } else {
  256. ZVAL_ZVAL(expr_copy, z, 0, 1);
  257. *use_copy = 1;
  258. }
  259. return;
  260. }
  261. zval_ptr_dtor(&z);
  262. }
  263. zend_error(EG(exception) ? E_ERROR : E_RECOVERABLE_ERROR, "Object of class %s could not be converted to string", Z_OBJCE_P(expr)->name);
  264. Z_STRLEN_P(expr_copy) = 0;
  265. Z_STRVAL_P(expr_copy) = STR_EMPTY_ALLOC();
  266. }
  267. break;
  268. case IS_DOUBLE:
  269. *expr_copy = *expr;
  270. zval_copy_ctor(expr_copy);
  271. zend_locale_sprintf_double(expr_copy ZEND_FILE_LINE_CC);
  272. break;
  273. default:
  274. *expr_copy = *expr;
  275. zval_copy_ctor(expr_copy);
  276. convert_to_string(expr_copy);
  277. break;
  278. }
  279. Z_TYPE_P(expr_copy) = IS_STRING;
  280. *use_copy = 1;
  281. }
  282. /* }}} */
  283. ZEND_API int zend_print_zval(zval *expr, int indent) /* {{{ */
  284. {
  285. return zend_print_zval_ex(zend_write, expr, indent);
  286. }
  287. /* }}} */
  288. ZEND_API int zend_print_zval_ex(zend_write_func_t write_func, zval *expr, int indent) /* {{{ */
  289. {
  290. zval expr_copy;
  291. int use_copy;
  292. zend_make_printable_zval(expr, &expr_copy, &use_copy);
  293. if (use_copy) {
  294. expr = &expr_copy;
  295. }
  296. if (Z_STRLEN_P(expr) == 0) { /* optimize away empty strings */
  297. if (use_copy) {
  298. zval_dtor(expr);
  299. }
  300. return 0;
  301. }
  302. write_func(Z_STRVAL_P(expr), Z_STRLEN_P(expr));
  303. if (use_copy) {
  304. zval_dtor(expr);
  305. }
  306. return Z_STRLEN_P(expr);
  307. }
  308. /* }}} */
  309. ZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC) /* {{{ */
  310. {
  311. switch (Z_TYPE_P(expr)) {
  312. case IS_ARRAY:
  313. ZEND_PUTS("Array (");
  314. if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
  315. ZEND_PUTS(" *RECURSION*");
  316. Z_ARRVAL_P(expr)->nApplyCount--;
  317. return;
  318. }
  319. print_flat_hash(Z_ARRVAL_P(expr) TSRMLS_CC);
  320. ZEND_PUTS(")");
  321. Z_ARRVAL_P(expr)->nApplyCount--;
  322. break;
  323. case IS_OBJECT:
  324. {
  325. HashTable *properties = NULL;
  326. const char *class_name = NULL;
  327. zend_uint clen;
  328. if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
  329. Z_OBJ_HANDLER_P(expr, get_class_name)(expr, &class_name, &clen, 0 TSRMLS_CC);
  330. }
  331. if (class_name) {
  332. zend_printf("%s Object (", class_name);
  333. } else {
  334. zend_printf("%s Object (", "Unknown Class");
  335. }
  336. if (class_name) {
  337. efree((char*)class_name);
  338. }
  339. if (Z_OBJ_HANDLER_P(expr, get_properties)) {
  340. properties = Z_OBJPROP_P(expr);
  341. }
  342. if (properties) {
  343. if (++properties->nApplyCount>1) {
  344. ZEND_PUTS(" *RECURSION*");
  345. properties->nApplyCount--;
  346. return;
  347. }
  348. print_flat_hash(properties TSRMLS_CC);
  349. properties->nApplyCount--;
  350. }
  351. ZEND_PUTS(")");
  352. break;
  353. }
  354. default:
  355. zend_print_variable(expr);
  356. break;
  357. }
  358. }
  359. /* }}} */
  360. ZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC) /* {{{ */
  361. {
  362. zend_print_zval_r_ex(zend_write, expr, indent TSRMLS_CC);
  363. }
  364. /* }}} */
  365. ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC) /* {{{ */
  366. {
  367. switch (Z_TYPE_P(expr)) {
  368. case IS_ARRAY:
  369. ZEND_PUTS_EX("Array\n");
  370. if (++Z_ARRVAL_P(expr)->nApplyCount>1) {
  371. ZEND_PUTS_EX(" *RECURSION*");
  372. Z_ARRVAL_P(expr)->nApplyCount--;
  373. return;
  374. }
  375. print_hash(write_func, Z_ARRVAL_P(expr), indent, 0 TSRMLS_CC);
  376. Z_ARRVAL_P(expr)->nApplyCount--;
  377. break;
  378. case IS_OBJECT:
  379. {
  380. HashTable *properties;
  381. const char *class_name = NULL;
  382. zend_uint clen;
  383. int is_temp;
  384. if (Z_OBJ_HANDLER_P(expr, get_class_name)) {
  385. Z_OBJ_HANDLER_P(expr, get_class_name)(expr, &class_name, &clen, 0 TSRMLS_CC);
  386. }
  387. if (class_name) {
  388. ZEND_PUTS_EX(class_name);
  389. } else {
  390. ZEND_PUTS_EX("Unknown Class");
  391. }
  392. ZEND_PUTS_EX(" Object\n");
  393. if (class_name) {
  394. efree((char*)class_name);
  395. }
  396. if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
  397. break;
  398. }
  399. if (++properties->nApplyCount>1) {
  400. ZEND_PUTS_EX(" *RECURSION*");
  401. properties->nApplyCount--;
  402. return;
  403. }
  404. print_hash(write_func, properties, indent, 1 TSRMLS_CC);
  405. properties->nApplyCount--;
  406. if (is_temp) {
  407. zend_hash_destroy(properties);
  408. efree(properties);
  409. }
  410. break;
  411. }
  412. default:
  413. zend_print_zval_ex(write_func, expr, indent);
  414. break;
  415. }
  416. }
  417. /* }}} */
  418. static FILE *zend_fopen_wrapper(const char *filename, char **opened_path TSRMLS_DC) /* {{{ */
  419. {
  420. if (opened_path) {
  421. *opened_path = estrdup(filename);
  422. }
  423. return fopen(filename, "rb");
  424. }
  425. /* }}} */
  426. #ifdef ZTS
  427. static zend_bool asp_tags_default = 0;
  428. static zend_bool short_tags_default = 1;
  429. static zend_uint compiler_options_default = ZEND_COMPILE_DEFAULT;
  430. #else
  431. # define asp_tags_default 0
  432. # define short_tags_default 1
  433. # define compiler_options_default ZEND_COMPILE_DEFAULT
  434. #endif
  435. static void zend_set_default_compile_time_values(TSRMLS_D) /* {{{ */
  436. {
  437. /* default compile-time values */
  438. CG(asp_tags) = asp_tags_default;
  439. CG(short_tags) = short_tags_default;
  440. CG(compiler_options) = compiler_options_default;
  441. }
  442. /* }}} */
  443. static void zend_init_exception_op(TSRMLS_D) /* {{{ */
  444. {
  445. memset(EG(exception_op), 0, sizeof(EG(exception_op)));
  446. EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
  447. EG(exception_op)[0].op1_type = IS_UNUSED;
  448. EG(exception_op)[0].op2_type = IS_UNUSED;
  449. EG(exception_op)[0].result_type = IS_UNUSED;
  450. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
  451. EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
  452. EG(exception_op)[1].op1_type = IS_UNUSED;
  453. EG(exception_op)[1].op2_type = IS_UNUSED;
  454. EG(exception_op)[1].result_type = IS_UNUSED;
  455. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
  456. EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
  457. EG(exception_op)[2].op1_type = IS_UNUSED;
  458. EG(exception_op)[2].op2_type = IS_UNUSED;
  459. EG(exception_op)[2].result_type = IS_UNUSED;
  460. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
  461. }
  462. /* }}} */
  463. #ifdef ZTS
  464. static void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
  465. {
  466. zend_function tmp_func;
  467. zend_class_entry *tmp_class;
  468. compiler_globals->compiled_filename = NULL;
  469. compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
  470. zend_hash_init_ex(compiler_globals->function_table, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
  471. zend_hash_copy(compiler_globals->function_table, global_function_table, NULL, &tmp_func, sizeof(zend_function));
  472. compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
  473. zend_hash_init_ex(compiler_globals->class_table, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
  474. zend_hash_copy(compiler_globals->class_table, global_class_table, (copy_ctor_func_t) zend_class_add_ref, &tmp_class, sizeof(zend_class_entry *));
  475. zend_set_default_compile_time_values(TSRMLS_C);
  476. CG(interactive) = 0;
  477. compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
  478. zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, NULL, 1, 0);
  479. zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, NULL, NULL, sizeof(zend_auto_global) /* empty element */);
  480. compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
  481. if (compiler_globals->last_static_member) {
  482. compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval**));
  483. } else {
  484. compiler_globals->static_members_table = NULL;
  485. }
  486. compiler_globals->script_encoding_list = NULL;
  487. }
  488. /* }}} */
  489. static void compiler_globals_dtor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */
  490. {
  491. if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
  492. zend_hash_destroy(compiler_globals->function_table);
  493. free(compiler_globals->function_table);
  494. }
  495. if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
  496. zend_hash_destroy(compiler_globals->class_table);
  497. free(compiler_globals->class_table);
  498. }
  499. if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
  500. zend_hash_destroy(compiler_globals->auto_globals);
  501. free(compiler_globals->auto_globals);
  502. }
  503. if (compiler_globals->static_members_table) {
  504. free(compiler_globals->static_members_table);
  505. }
  506. if (compiler_globals->script_encoding_list) {
  507. pefree(compiler_globals->script_encoding_list, 1);
  508. }
  509. compiler_globals->last_static_member = 0;
  510. }
  511. /* }}} */
  512. static void executor_globals_ctor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
  513. {
  514. zend_startup_constants(TSRMLS_C);
  515. zend_copy_constants(EG(zend_constants), GLOBAL_CONSTANTS_TABLE);
  516. zend_init_rsrc_plist(TSRMLS_C);
  517. zend_init_exception_op(TSRMLS_C);
  518. EG(lambda_count) = 0;
  519. EG(user_error_handler) = NULL;
  520. EG(user_exception_handler) = NULL;
  521. EG(in_execution) = 0;
  522. EG(in_autoload) = NULL;
  523. EG(current_execute_data) = NULL;
  524. EG(current_module) = NULL;
  525. EG(exit_status) = 0;
  526. #if XPFPA_HAVE_CW
  527. EG(saved_fpu_cw) = 0;
  528. #endif
  529. EG(saved_fpu_cw_ptr) = NULL;
  530. EG(active) = 0;
  531. }
  532. /* }}} */
  533. static void executor_globals_dtor(zend_executor_globals *executor_globals TSRMLS_DC) /* {{{ */
  534. {
  535. zend_ini_shutdown(TSRMLS_C);
  536. if (&executor_globals->persistent_list != global_persistent_list) {
  537. zend_destroy_rsrc_list(&executor_globals->persistent_list TSRMLS_CC);
  538. }
  539. if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
  540. zend_hash_destroy(executor_globals->zend_constants);
  541. free(executor_globals->zend_constants);
  542. }
  543. }
  544. /* }}} */
  545. static void zend_new_thread_end_handler(THREAD_T thread_id TSRMLS_DC) /* {{{ */
  546. {
  547. if (zend_copy_ini_directives(TSRMLS_C) == SUCCESS) {
  548. zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP TSRMLS_CC);
  549. }
  550. }
  551. /* }}} */
  552. #endif
  553. #if defined(__FreeBSD__) || defined(__DragonFly__)
  554. /* FreeBSD and DragonFly floating point precision fix */
  555. #include <floatingpoint.h>
  556. #endif
  557. static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
  558. {
  559. memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
  560. }
  561. /* }}} */
  562. static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p TSRMLS_DC) /* {{{ */
  563. {
  564. memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
  565. }
  566. /* }}} */
  567. void zend_init_opcodes_handlers(void);
  568. static zend_bool php_auto_globals_create_globals(const char *name, uint name_len TSRMLS_DC) /* {{{ */
  569. {
  570. zval *globals;
  571. ALLOC_ZVAL(globals);
  572. Z_SET_REFCOUNT_P(globals, 1);
  573. Z_SET_ISREF_P(globals);
  574. Z_TYPE_P(globals) = IS_ARRAY;
  575. Z_ARRVAL_P(globals) = &EG(symbol_table);
  576. zend_hash_update(&EG(symbol_table), name, name_len + 1, &globals, sizeof(zval *), NULL);
  577. return 0;
  578. }
  579. /* }}} */
  580. int zend_startup(zend_utility_functions *utility_functions, char **extensions TSRMLS_DC) /* {{{ */
  581. {
  582. #ifdef ZTS
  583. zend_compiler_globals *compiler_globals;
  584. zend_executor_globals *executor_globals;
  585. extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
  586. extern ZEND_API ts_rsrc_id language_scanner_globals_id;
  587. #else
  588. extern zend_ini_scanner_globals ini_scanner_globals;
  589. extern zend_php_scanner_globals language_scanner_globals;
  590. #endif
  591. start_memory_manager(TSRMLS_C);
  592. virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
  593. #if defined(__FreeBSD__) || defined(__DragonFly__)
  594. /* FreeBSD and DragonFly floating point precision fix */
  595. fpsetmask(0);
  596. #endif
  597. zend_startup_strtod();
  598. zend_startup_extensions_mechanism();
  599. /* Set up utility functions and values */
  600. zend_error_cb = utility_functions->error_function;
  601. zend_printf = utility_functions->printf_function;
  602. zend_write = (zend_write_func_t) utility_functions->write_function;
  603. zend_fopen = utility_functions->fopen_function;
  604. if (!zend_fopen) {
  605. zend_fopen = zend_fopen_wrapper;
  606. }
  607. zend_stream_open_function = utility_functions->stream_open_function;
  608. zend_message_dispatcher_p = utility_functions->message_handler;
  609. #ifndef ZEND_SIGNALS
  610. zend_block_interruptions = utility_functions->block_interruptions;
  611. zend_unblock_interruptions = utility_functions->unblock_interruptions;
  612. #endif
  613. zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
  614. zend_ticks_function = utility_functions->ticks_function;
  615. zend_on_timeout = utility_functions->on_timeout;
  616. zend_vspprintf = utility_functions->vspprintf_function;
  617. zend_getenv = utility_functions->getenv_function;
  618. zend_resolve_path = utility_functions->resolve_path_function;
  619. #if HAVE_DTRACE
  620. /* build with dtrace support */
  621. zend_compile_file = dtrace_compile_file;
  622. zend_execute_ex = dtrace_execute_ex;
  623. zend_execute_internal = dtrace_execute_internal;
  624. #else
  625. zend_compile_file = compile_file;
  626. zend_execute_ex = execute_ex;
  627. zend_execute_internal = NULL;
  628. #endif /* HAVE_SYS_SDT_H */
  629. zend_compile_string = compile_string;
  630. zend_throw_exception_hook = NULL;
  631. zend_init_opcodes_handlers();
  632. /* set up version */
  633. zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
  634. zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
  635. GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
  636. GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  637. GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  638. GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  639. zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 100, NULL, ZEND_FUNCTION_DTOR, 1, 0);
  640. zend_hash_init_ex(GLOBAL_CLASS_TABLE, 10, NULL, ZEND_CLASS_DTOR, 1, 0);
  641. zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, NULL, 1, 0);
  642. zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 20, NULL, ZEND_CONSTANT_DTOR, 1, 0);
  643. zend_hash_init_ex(&module_registry, 50, NULL, ZEND_MODULE_DTOR, 1, 0);
  644. zend_init_rsrc_list_dtors();
  645. /* This zval can be used to initialize allocate zval's to an uninit'ed value */
  646. Z_UNSET_ISREF(zval_used_for_init);
  647. Z_SET_REFCOUNT(zval_used_for_init, 1);
  648. Z_TYPE(zval_used_for_init) = IS_NULL;
  649. #ifdef ZTS
  650. ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
  651. ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
  652. ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
  653. ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
  654. compiler_globals = ts_resource(compiler_globals_id);
  655. executor_globals = ts_resource(executor_globals_id);
  656. compiler_globals_dtor(compiler_globals TSRMLS_CC);
  657. compiler_globals->in_compilation = 0;
  658. compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
  659. compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
  660. *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
  661. *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
  662. compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
  663. zend_hash_destroy(executor_globals->zend_constants);
  664. *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
  665. #else
  666. ini_scanner_globals_ctor(&ini_scanner_globals TSRMLS_CC);
  667. php_scanner_globals_ctor(&language_scanner_globals TSRMLS_CC);
  668. zend_set_default_compile_time_values(TSRMLS_C);
  669. EG(user_error_handler) = NULL;
  670. EG(user_exception_handler) = NULL;
  671. #endif
  672. zend_interned_strings_init(TSRMLS_C);
  673. zend_startup_builtin_functions(TSRMLS_C);
  674. zend_register_standard_constants(TSRMLS_C);
  675. zend_register_auto_global("GLOBALS", sizeof("GLOBALS") - 1, 1, php_auto_globals_create_globals TSRMLS_CC);
  676. #ifndef ZTS
  677. zend_init_rsrc_plist(TSRMLS_C);
  678. zend_init_exception_op(TSRMLS_C);
  679. #endif
  680. zend_ini_startup(TSRMLS_C);
  681. #ifdef ZTS
  682. tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
  683. #endif
  684. return SUCCESS;
  685. }
  686. /* }}} */
  687. void zend_register_standard_ini_entries(TSRMLS_D) /* {{{ */
  688. {
  689. int module_number = 0;
  690. REGISTER_INI_ENTRIES();
  691. }
  692. /* }}} */
  693. /* Unlink the global (r/o) copies of the class, function and constant tables,
  694. * and use a fresh r/w copy for the startup thread
  695. */
  696. void zend_post_startup(TSRMLS_D) /* {{{ */
  697. {
  698. #ifdef ZTS
  699. zend_encoding **script_encoding_list;
  700. zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
  701. zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
  702. *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
  703. *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
  704. *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
  705. asp_tags_default = CG(asp_tags);
  706. short_tags_default = CG(short_tags);
  707. compiler_options_default = CG(compiler_options);
  708. zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
  709. free(compiler_globals->function_table);
  710. free(compiler_globals->class_table);
  711. if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
  712. compiler_globals_ctor(compiler_globals, tsrm_ls);
  713. compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
  714. } else {
  715. compiler_globals_ctor(compiler_globals, tsrm_ls);
  716. }
  717. free(EG(zend_constants));
  718. virtual_cwd_deactivate(TSRMLS_C);
  719. executor_globals_ctor(executor_globals, tsrm_ls);
  720. global_persistent_list = &EG(persistent_list);
  721. zend_copy_ini_directives(TSRMLS_C);
  722. #else
  723. virtual_cwd_deactivate(TSRMLS_C);
  724. #endif
  725. }
  726. /* }}} */
  727. void zend_shutdown(TSRMLS_D) /* {{{ */
  728. {
  729. #ifdef ZEND_SIGNALS
  730. zend_signal_shutdown(TSRMLS_C);
  731. #endif
  732. zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
  733. if (EG(active))
  734. {
  735. /*
  736. * The order of destruction is important here.
  737. * See bugs #65463 and 66036.
  738. */
  739. zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) zend_cleanup_function_data_full TSRMLS_CC);
  740. zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) zend_cleanup_user_class_data TSRMLS_CC);
  741. zend_cleanup_internal_classes(TSRMLS_C);
  742. zend_hash_reverse_apply(GLOBAL_FUNCTION_TABLE, (apply_func_t) clean_non_persistent_function_full TSRMLS_CC);
  743. zend_hash_reverse_apply(GLOBAL_CLASS_TABLE, (apply_func_t) clean_non_persistent_class_full TSRMLS_CC);
  744. }
  745. zend_destroy_modules();
  746. virtual_cwd_deactivate(TSRMLS_C);
  747. virtual_cwd_shutdown();
  748. zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
  749. zend_hash_destroy(GLOBAL_CLASS_TABLE);
  750. zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
  751. free(GLOBAL_AUTO_GLOBALS_TABLE);
  752. zend_shutdown_extensions(TSRMLS_C);
  753. free(zend_version_info);
  754. free(GLOBAL_FUNCTION_TABLE);
  755. free(GLOBAL_CLASS_TABLE);
  756. zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
  757. free(GLOBAL_CONSTANTS_TABLE);
  758. zend_shutdown_strtod();
  759. #ifdef ZTS
  760. GLOBAL_FUNCTION_TABLE = NULL;
  761. GLOBAL_CLASS_TABLE = NULL;
  762. GLOBAL_AUTO_GLOBALS_TABLE = NULL;
  763. GLOBAL_CONSTANTS_TABLE = NULL;
  764. #endif
  765. zend_destroy_rsrc_list_dtors();
  766. zend_interned_strings_dtor(TSRMLS_C);
  767. }
  768. /* }}} */
  769. void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
  770. {
  771. zend_uv = *utility_values;
  772. zend_uv.import_use_extension_length = strlen(zend_uv.import_use_extension);
  773. }
  774. /* }}} */
  775. /* this should be compatible with the standard zenderror */
  776. void zenderror(const char *error) /* {{{ */
  777. {
  778. zend_error(E_PARSE, "%s", error);
  779. }
  780. /* }}} */
  781. BEGIN_EXTERN_C()
  782. ZEND_API void _zend_bailout(char *filename, uint lineno) /* {{{ */
  783. {
  784. TSRMLS_FETCH();
  785. if (!EG(bailout)) {
  786. zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
  787. exit(-1);
  788. }
  789. CG(unclean_shutdown) = 1;
  790. CG(active_class_entry) = NULL;
  791. CG(in_compilation) = EG(in_execution) = 0;
  792. EG(current_execute_data) = NULL;
  793. LONGJMP(*EG(bailout), FAILURE);
  794. }
  795. /* }}} */
  796. END_EXTERN_C()
  797. ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
  798. {
  799. char *new_info;
  800. uint new_info_length;
  801. new_info_length = sizeof(" with v, , by \n")
  802. + strlen(extension->name)
  803. + strlen(extension->version)
  804. + strlen(extension->copyright)
  805. + strlen(extension->author);
  806. new_info = (char *) malloc(new_info_length + 1);
  807. snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
  808. zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
  809. strncat(zend_version_info, new_info, new_info_length);
  810. zend_version_info_length += new_info_length;
  811. free(new_info);
  812. }
  813. /* }}} */
  814. ZEND_API char *get_zend_version(void) /* {{{ */
  815. {
  816. return zend_version_info;
  817. }
  818. /* }}} */
  819. ZEND_API void zend_activate(TSRMLS_D) /* {{{ */
  820. {
  821. #ifdef ZTS
  822. virtual_cwd_activate(TSRMLS_C);
  823. #endif
  824. gc_reset(TSRMLS_C);
  825. init_compiler(TSRMLS_C);
  826. init_executor(TSRMLS_C);
  827. startup_scanner(TSRMLS_C);
  828. }
  829. /* }}} */
  830. void zend_call_destructors(TSRMLS_D) /* {{{ */
  831. {
  832. zend_try {
  833. shutdown_destructors(TSRMLS_C);
  834. } zend_end_try();
  835. }
  836. /* }}} */
  837. ZEND_API void zend_deactivate(TSRMLS_D) /* {{{ */
  838. {
  839. /* we're no longer executing anything */
  840. EG(opline_ptr) = NULL;
  841. EG(active_symbol_table) = NULL;
  842. zend_try {
  843. shutdown_scanner(TSRMLS_C);
  844. } zend_end_try();
  845. /* shutdown_executor() takes care of its own bailout handling */
  846. shutdown_executor(TSRMLS_C);
  847. zend_try {
  848. shutdown_compiler(TSRMLS_C);
  849. } zend_end_try();
  850. zend_destroy_rsrc_list(&EG(regular_list) TSRMLS_CC);
  851. #if ZEND_DEBUG
  852. if (GC_G(gc_enabled) && !CG(unclean_shutdown)) {
  853. gc_collect_cycles(TSRMLS_C);
  854. }
  855. #endif
  856. #if GC_BENCH
  857. fprintf(stderr, "GC Statistics\n");
  858. fprintf(stderr, "-------------\n");
  859. fprintf(stderr, "Runs: %d\n", GC_G(gc_runs));
  860. fprintf(stderr, "Collected: %d\n", GC_G(collected));
  861. fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
  862. fprintf(stderr, "Root buffer peak: %d\n\n", GC_G(root_buf_peak));
  863. fprintf(stderr, " Possible Remove from Marked\n");
  864. fprintf(stderr, " Root Buffered buffer grey\n");
  865. fprintf(stderr, " -------- -------- ----------- ------\n");
  866. fprintf(stderr, "ZVAL %8d %8d %9d %8d\n", GC_G(zval_possible_root), GC_G(zval_buffered), GC_G(zval_remove_from_buffer), GC_G(zval_marked_grey));
  867. fprintf(stderr, "ZOBJ %8d %8d %9d %8d\n", GC_G(zobj_possible_root), GC_G(zobj_buffered), GC_G(zobj_remove_from_buffer), GC_G(zobj_marked_grey));
  868. #endif
  869. zend_try {
  870. zend_ini_deactivate(TSRMLS_C);
  871. } zend_end_try();
  872. }
  873. /* }}} */
  874. BEGIN_EXTERN_C()
  875. ZEND_API void zend_message_dispatcher(long message, const void *data TSRMLS_DC) /* {{{ */
  876. {
  877. if (zend_message_dispatcher_p) {
  878. zend_message_dispatcher_p(message, data TSRMLS_CC);
  879. }
  880. }
  881. /* }}} */
  882. END_EXTERN_C()
  883. ZEND_API int zend_get_configuration_directive(const char *name, uint name_length, zval *contents) /* {{{ */
  884. {
  885. if (zend_get_configuration_directive_p) {
  886. return zend_get_configuration_directive_p(name, name_length, contents);
  887. } else {
  888. return FAILURE;
  889. }
  890. }
  891. /* }}} */
  892. #define SAVE_STACK(stack) do { \
  893. if (CG(stack).top) { \
  894. memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
  895. CG(stack).top = CG(stack).max = 0; \
  896. CG(stack).elements = NULL; \
  897. } else { \
  898. stack.top = 0; \
  899. } \
  900. } while (0)
  901. #define RESTORE_STACK(stack) do { \
  902. if (stack.top) { \
  903. zend_stack_destroy(&CG(stack)); \
  904. memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
  905. } \
  906. } while (0)
  907. ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
  908. {
  909. va_list args;
  910. va_list usr_copy;
  911. zval ***params;
  912. zval *retval;
  913. zval *z_error_type, *z_error_message, *z_error_filename, *z_error_lineno, *z_context;
  914. const char *error_filename;
  915. uint error_lineno;
  916. zval *orig_user_error_handler;
  917. zend_bool in_compilation;
  918. zend_class_entry *saved_class_entry;
  919. zend_stack bp_stack;
  920. zend_stack function_call_stack;
  921. zend_stack switch_cond_stack;
  922. zend_stack foreach_copy_stack;
  923. zend_stack object_stack;
  924. zend_stack declare_stack;
  925. zend_stack list_stack;
  926. zend_stack context_stack;
  927. TSRMLS_FETCH();
  928. /* Report about uncaught exception in case of fatal errors */
  929. if (EG(exception)) {
  930. switch (type) {
  931. case E_CORE_ERROR:
  932. case E_ERROR:
  933. case E_RECOVERABLE_ERROR:
  934. case E_PARSE:
  935. case E_COMPILE_ERROR:
  936. case E_USER_ERROR:
  937. if (zend_is_executing(TSRMLS_C)) {
  938. error_lineno = zend_get_executed_lineno(TSRMLS_C);
  939. }
  940. zend_exception_error(EG(exception), E_WARNING TSRMLS_CC);
  941. EG(exception) = NULL;
  942. if (zend_is_executing(TSRMLS_C) && EG(opline_ptr)) {
  943. active_opline->lineno = error_lineno;
  944. }
  945. break;
  946. default:
  947. break;
  948. }
  949. }
  950. /* Obtain relevant filename and lineno */
  951. switch (type) {
  952. case E_CORE_ERROR:
  953. case E_CORE_WARNING:
  954. error_filename = NULL;
  955. error_lineno = 0;
  956. break;
  957. case E_PARSE:
  958. case E_COMPILE_ERROR:
  959. case E_COMPILE_WARNING:
  960. case E_ERROR:
  961. case E_NOTICE:
  962. case E_STRICT:
  963. case E_DEPRECATED:
  964. case E_WARNING:
  965. case E_USER_ERROR:
  966. case E_USER_WARNING:
  967. case E_USER_NOTICE:
  968. case E_USER_DEPRECATED:
  969. case E_RECOVERABLE_ERROR:
  970. if (zend_is_compiling(TSRMLS_C)) {
  971. error_filename = zend_get_compiled_filename(TSRMLS_C);
  972. error_lineno = zend_get_compiled_lineno(TSRMLS_C);
  973. } else if (zend_is_executing(TSRMLS_C)) {
  974. error_filename = zend_get_executed_filename(TSRMLS_C);
  975. error_lineno = zend_get_executed_lineno(TSRMLS_C);
  976. } else {
  977. error_filename = NULL;
  978. error_lineno = 0;
  979. }
  980. break;
  981. default:
  982. error_filename = NULL;
  983. error_lineno = 0;
  984. break;
  985. }
  986. if (!error_filename) {
  987. error_filename = "Unknown";
  988. }
  989. #ifdef HAVE_DTRACE
  990. if(DTRACE_ERROR_ENABLED()) {
  991. char *dtrace_error_buffer;
  992. va_start(args, format);
  993. zend_vspprintf(&dtrace_error_buffer, 0, format, args);
  994. DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
  995. efree(dtrace_error_buffer);
  996. va_end(args);
  997. }
  998. #endif /* HAVE_DTRACE */
  999. va_start(args, format);
  1000. /* if we don't have a user defined error handler */
  1001. if (!EG(user_error_handler)
  1002. || !(EG(user_error_handler_error_reporting) & type)
  1003. || EG(error_handling) != EH_NORMAL) {
  1004. zend_error_cb(type, error_filename, error_lineno, format, args);
  1005. } else switch (type) {
  1006. case E_ERROR:
  1007. case E_PARSE:
  1008. case E_CORE_ERROR:
  1009. case E_CORE_WARNING:
  1010. case E_COMPILE_ERROR:
  1011. case E_COMPILE_WARNING:
  1012. /* The error may not be safe to handle in user-space */
  1013. zend_error_cb(type, error_filename, error_lineno, format, args);
  1014. break;
  1015. default:
  1016. /* Handle the error in user space */
  1017. ALLOC_INIT_ZVAL(z_error_message);
  1018. ALLOC_INIT_ZVAL(z_error_type);
  1019. ALLOC_INIT_ZVAL(z_error_filename);
  1020. ALLOC_INIT_ZVAL(z_error_lineno);
  1021. ALLOC_INIT_ZVAL(z_context);
  1022. /* va_copy() is __va_copy() in old gcc versions.
  1023. * According to the autoconf manual, using
  1024. * memcpy(&dst, &src, sizeof(va_list))
  1025. * gives maximum portability. */
  1026. #ifndef va_copy
  1027. # ifdef __va_copy
  1028. # define va_copy(dest, src) __va_copy((dest), (src))
  1029. # else
  1030. # define va_copy(dest, src) memcpy(&(dest), &(src), sizeof(va_list))
  1031. # endif
  1032. #endif
  1033. va_copy(usr_copy, args);
  1034. Z_STRLEN_P(z_error_message) = zend_vspprintf(&Z_STRVAL_P(z_error_message), 0, format, usr_copy);
  1035. #ifdef va_copy
  1036. va_end(usr_copy);
  1037. #endif
  1038. Z_TYPE_P(z_error_message) = IS_STRING;
  1039. Z_LVAL_P(z_error_type) = type;
  1040. Z_TYPE_P(z_error_type) = IS_LONG;
  1041. if (error_filename) {
  1042. ZVAL_STRING(z_error_filename, error_filename, 1);
  1043. }
  1044. Z_LVAL_P(z_error_lineno) = error_lineno;
  1045. Z_TYPE_P(z_error_lineno) = IS_LONG;
  1046. if (!EG(active_symbol_table)) {
  1047. zend_rebuild_symbol_table(TSRMLS_C);
  1048. }
  1049. /* during shutdown the symbol table table can be still null */
  1050. if (!EG(active_symbol_table)) {
  1051. Z_TYPE_P(z_context) = IS_NULL;
  1052. } else {
  1053. Z_ARRVAL_P(z_context) = EG(active_symbol_table);
  1054. Z_TYPE_P(z_context) = IS_ARRAY;
  1055. zval_copy_ctor(z_context);
  1056. }
  1057. params = (zval ***) emalloc(sizeof(zval **)*5);
  1058. params[0] = &z_error_type;
  1059. params[1] = &z_error_message;
  1060. params[2] = &z_error_filename;
  1061. params[3] = &z_error_lineno;
  1062. params[4] = &z_context;
  1063. orig_user_error_handler = EG(user_error_handler);
  1064. EG(user_error_handler) = NULL;
  1065. /* User error handler may include() additinal PHP files.
  1066. * If an error was generated during comilation PHP will compile
  1067. * such scripts recursivly, but some CG() variables may be
  1068. * inconsistent. */
  1069. in_compilation = CG(in_compilation);
  1070. if (in_compilation) {
  1071. saved_class_entry = CG(active_class_entry);
  1072. CG(active_class_entry) = NULL;
  1073. SAVE_STACK(bp_stack);
  1074. SAVE_STACK(function_call_stack);
  1075. SAVE_STACK(switch_cond_stack);
  1076. SAVE_STACK(foreach_copy_stack);
  1077. SAVE_STACK(object_stack);
  1078. SAVE_STACK(declare_stack);
  1079. SAVE_STACK(list_stack);
  1080. SAVE_STACK(context_stack);
  1081. CG(in_compilation) = 0;
  1082. }
  1083. if (call_user_function_ex(CG(function_table), NULL, orig_user_error_handler, &retval, 5, params, 1, NULL TSRMLS_CC) == SUCCESS) {
  1084. if (retval) {
  1085. if (Z_TYPE_P(retval) == IS_BOOL && Z_LVAL_P(retval) == 0) {
  1086. zend_error_cb(type, error_filename, error_lineno, format, args);
  1087. }
  1088. zval_ptr_dtor(&retval);
  1089. }
  1090. } else if (!EG(exception)) {
  1091. /* The user error handler failed, use built-in error handler */
  1092. zend_error_cb(type, error_filename, error_lineno, format, args);
  1093. }
  1094. if (in_compilation) {
  1095. CG(active_class_entry) = saved_class_entry;
  1096. RESTORE_STACK(bp_stack);
  1097. RESTORE_STACK(function_call_stack);
  1098. RESTORE_STACK(switch_cond_stack);
  1099. RESTORE_STACK(foreach_copy_stack);
  1100. RESTORE_STACK(object_stack);
  1101. RESTORE_STACK(declare_stack);
  1102. RESTORE_STACK(list_stack);
  1103. RESTORE_STACK(context_stack);
  1104. CG(in_compilation) = 1;
  1105. }
  1106. if (!EG(user_error_handler)) {
  1107. EG(user_error_handler) = orig_user_error_handler;
  1108. }
  1109. else {
  1110. zval_ptr_dtor(&orig_user_error_handler);
  1111. }
  1112. efree(params);
  1113. zval_ptr_dtor(&z_error_message);
  1114. zval_ptr_dtor(&z_error_type);
  1115. zval_ptr_dtor(&z_error_filename);
  1116. zval_ptr_dtor(&z_error_lineno);
  1117. zval_ptr_dtor(&z_context);
  1118. break;
  1119. }
  1120. va_end(args);
  1121. if (type == E_PARSE) {
  1122. /* eval() errors do not affect exit_status */
  1123. if (!(EG(current_execute_data) &&
  1124. EG(current_execute_data)->opline &&
  1125. EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
  1126. EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
  1127. EG(exit_status) = 255;
  1128. }
  1129. zend_init_compiler_data_structures(TSRMLS_C);
  1130. }
  1131. }
  1132. /* }}} */
  1133. #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(__INTEL_COMPILER) && !defined(DARWIN) && !defined(__hpux) && !defined(_AIX) && !defined(__osf__)
  1134. void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
  1135. #endif
  1136. ZEND_API void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
  1137. {
  1138. #if ZEND_DEBUG
  1139. va_list args;
  1140. va_start(args, format);
  1141. # ifdef ZEND_WIN32
  1142. {
  1143. char output_buf[1024];
  1144. vsnprintf(output_buf, 1024, format, args);
  1145. OutputDebugString(output_buf);
  1146. OutputDebugString("\n");
  1147. if (trigger_break && IsDebuggerPresent()) {
  1148. DebugBreak();
  1149. }
  1150. }
  1151. # else
  1152. vfprintf(stderr, format, args);
  1153. fprintf(stderr, "\n");
  1154. # endif
  1155. va_end(args);
  1156. #endif
  1157. }
  1158. /* }}} */
  1159. ZEND_API int zend_execute_scripts(int type TSRMLS_DC, zval **retval, int file_count, ...) /* {{{ */
  1160. {
  1161. va_list files;
  1162. int i;
  1163. zend_file_handle *file_handle;
  1164. zend_op_array *orig_op_array = EG(active_op_array);
  1165. zval **orig_retval_ptr_ptr = EG(return_value_ptr_ptr);
  1166. long orig_interactive = CG(interactive);
  1167. va_start(files, file_count);
  1168. for (i = 0; i < file_count; i++) {
  1169. file_handle = va_arg(files, zend_file_handle *);
  1170. if (!file_handle) {
  1171. continue;
  1172. }
  1173. if (orig_interactive) {
  1174. if (file_handle->filename[0] != '-' || file_handle->filename[1]) {
  1175. CG(interactive) = 0;
  1176. } else {
  1177. CG(interactive) = 1;
  1178. }
  1179. }
  1180. EG(active_op_array) = zend_compile_file(file_handle, type TSRMLS_CC);
  1181. if (file_handle->opened_path) {
  1182. int dummy = 1;
  1183. zend_hash_add(&EG(included_files), file_handle->opened_path, strlen(file_handle->opened_path) + 1, (void *)&dummy, sizeof(int), NULL);
  1184. }
  1185. zend_destroy_file_handle(file_handle TSRMLS_CC);
  1186. if (EG(active_op_array)) {
  1187. EG(return_value_ptr_ptr) = retval ? retval : NULL;
  1188. zend_execute(EG(active_op_array) TSRMLS_CC);
  1189. zend_exception_restore(TSRMLS_C);
  1190. if (EG(exception)) {
  1191. if (EG(user_exception_handler)) {
  1192. zval *orig_user_exception_handler;
  1193. zval **params[1], *retval2, *old_exception;
  1194. old_exception = EG(exception);
  1195. EG(exception) = NULL;
  1196. params[0] = &old_exception;
  1197. orig_user_exception_handler = EG(user_exception_handler);
  1198. if (call_user_function_ex(CG(function_table), NULL, orig_user_exception_handler, &retval2, 1, params, 1, NULL TSRMLS_CC) == SUCCESS) {
  1199. if (retval2 != NULL) {
  1200. zval_ptr_dtor(&retval2);
  1201. }
  1202. if (EG(exception)) {
  1203. zval_ptr_dtor(&EG(exception));
  1204. EG(exception) = NULL;
  1205. }
  1206. zval_ptr_dtor(&old_exception);
  1207. } else {
  1208. EG(exception) = old_exception;
  1209. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  1210. }
  1211. } else {
  1212. zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
  1213. }
  1214. }
  1215. destroy_op_array(EG(active_op_array) TSRMLS_CC);
  1216. efree(EG(active_op_array));
  1217. } else if (type==ZEND_REQUIRE) {
  1218. va_end(files);
  1219. EG(active_op_array) = orig_op_array;
  1220. EG(return_value_ptr_ptr) = orig_retval_ptr_ptr;
  1221. CG(interactive) = orig_interactive;
  1222. return FAILURE;
  1223. }
  1224. }
  1225. va_end(files);
  1226. EG(active_op_array) = orig_op_array;
  1227. EG(return_value_ptr_ptr) = orig_retval_ptr_ptr;
  1228. CG(interactive) = orig_interactive;
  1229. return SUCCESS;
  1230. }
  1231. /* }}} */
  1232. #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
  1233. ZEND_API char *zend_make_compiled_string_description(const char *name TSRMLS_DC) /* {{{ */
  1234. {
  1235. const char *cur_filename;
  1236. int cur_lineno;
  1237. char *compiled_string_description;
  1238. if (zend_is_compiling(TSRMLS_C)) {
  1239. cur_filename = zend_get_compiled_filename(TSRMLS_C);
  1240. cur_lineno = zend_get_compiled_lineno(TSRMLS_C);
  1241. } else if (zend_is_executing(TSRMLS_C)) {
  1242. cur_filename = zend_get_executed_filename(TSRMLS_C);
  1243. cur_lineno = zend_get_executed_lineno(TSRMLS_C);
  1244. } else {
  1245. cur_filename = "Unknown";
  1246. cur_lineno = 0;
  1247. }
  1248. zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
  1249. return compiled_string_description;
  1250. }
  1251. /* }}} */
  1252. void free_estring(char **str_p) /* {{{ */
  1253. {
  1254. efree(*str_p);
  1255. }
  1256. /* }}} */
  1257. /*
  1258. * Local variables:
  1259. * tab-width: 4
  1260. * c-basic-offset: 4
  1261. * indent-tabs-mode: t
  1262. * End:
  1263. */