zend.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2018 Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_extensions.h"
  21. #include "zend_modules.h"
  22. #include "zend_constants.h"
  23. #include "zend_list.h"
  24. #include "zend_API.h"
  25. #include "zend_exceptions.h"
  26. #include "zend_builtin_functions.h"
  27. #include "zend_ini.h"
  28. #include "zend_vm.h"
  29. #include "zend_dtrace.h"
  30. #include "zend_virtual_cwd.h"
  31. #include "zend_smart_str.h"
  32. #include "zend_smart_string.h"
  33. #include "zend_cpuinfo.h"
  34. #ifdef ZTS
  35. ZEND_API int compiler_globals_id;
  36. ZEND_API int executor_globals_id;
  37. static HashTable *global_function_table = NULL;
  38. static HashTable *global_class_table = NULL;
  39. static HashTable *global_constants_table = NULL;
  40. static HashTable *global_auto_globals_table = NULL;
  41. static HashTable *global_persistent_list = NULL;
  42. ZEND_TSRMLS_CACHE_DEFINE()
  43. # define GLOBAL_FUNCTION_TABLE global_function_table
  44. # define GLOBAL_CLASS_TABLE global_class_table
  45. # define GLOBAL_CONSTANTS_TABLE global_constants_table
  46. # define GLOBAL_AUTO_GLOBALS_TABLE global_auto_globals_table
  47. #else
  48. # define GLOBAL_FUNCTION_TABLE CG(function_table)
  49. # define GLOBAL_CLASS_TABLE CG(class_table)
  50. # define GLOBAL_AUTO_GLOBALS_TABLE CG(auto_globals)
  51. # define GLOBAL_CONSTANTS_TABLE EG(zend_constants)
  52. #endif
  53. ZEND_API zend_utility_values zend_uv;
  54. ZEND_API zend_bool zend_dtrace_enabled;
  55. /* version information */
  56. static char *zend_version_info;
  57. static uint32_t zend_version_info_length;
  58. #define ZEND_CORE_VERSION_INFO "Zend Engine v" ZEND_VERSION ", Copyright (c) 1998-2018 Zend Technologies\n"
  59. #define PRINT_ZVAL_INDENT 4
  60. /* true multithread-shared globals */
  61. ZEND_API zend_class_entry *zend_standard_class_def = NULL;
  62. ZEND_API size_t (*zend_printf)(const char *format, ...);
  63. ZEND_API zend_write_func_t zend_write;
  64. ZEND_API FILE *(*zend_fopen)(const char *filename, zend_string **opened_path);
  65. ZEND_API int (*zend_stream_open_function)(const char *filename, zend_file_handle *handle);
  66. ZEND_API void (*zend_ticks_function)(int ticks);
  67. ZEND_API void (*zend_interrupt_function)(zend_execute_data *execute_data);
  68. ZEND_API void (*zend_error_cb)(int type, const char *error_filename, const uint32_t error_lineno, const char *format, va_list args);
  69. void (*zend_printf_to_smart_string)(smart_string *buf, const char *format, va_list ap);
  70. void (*zend_printf_to_smart_str)(smart_str *buf, const char *format, va_list ap);
  71. ZEND_API char *(*zend_getenv)(char *name, size_t name_len);
  72. ZEND_API zend_string *(*zend_resolve_path)(const char *filename, size_t filename_len);
  73. ZEND_API int (*zend_post_startup_cb)(void) = NULL;
  74. void (*zend_on_timeout)(int seconds);
  75. static void (*zend_message_dispatcher_p)(zend_long message, const void *data);
  76. static zval *(*zend_get_configuration_directive_p)(zend_string *name);
  77. #if ZEND_RC_DEBUG
  78. ZEND_API zend_bool zend_rc_debug = 0;
  79. #endif
  80. static ZEND_INI_MH(OnUpdateErrorReporting) /* {{{ */
  81. {
  82. if (!new_value) {
  83. EG(error_reporting) = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED;
  84. } else {
  85. EG(error_reporting) = atoi(ZSTR_VAL(new_value));
  86. }
  87. return SUCCESS;
  88. }
  89. /* }}} */
  90. static ZEND_INI_MH(OnUpdateGCEnabled) /* {{{ */
  91. {
  92. zend_bool val;
  93. val = zend_ini_parse_bool(new_value);
  94. gc_enable(val);
  95. return SUCCESS;
  96. }
  97. /* }}} */
  98. static ZEND_INI_DISP(zend_gc_enabled_displayer_cb) /* {{{ */
  99. {
  100. if (gc_enabled()) {
  101. ZEND_PUTS("On");
  102. } else {
  103. ZEND_PUTS("Off");
  104. }
  105. }
  106. /* }}} */
  107. static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */
  108. {
  109. if (!CG(multibyte)) {
  110. return FAILURE;
  111. }
  112. if (!zend_multibyte_get_functions()) {
  113. return SUCCESS;
  114. }
  115. return zend_multibyte_set_script_encoding_by_string(new_value ? ZSTR_VAL(new_value) : NULL, new_value ? ZSTR_LEN(new_value) : 0);
  116. }
  117. /* }}} */
  118. static ZEND_INI_MH(OnUpdateAssertions) /* {{{ */
  119. {
  120. zend_long *p, val;
  121. #ifndef ZTS
  122. char *base = (char *) mh_arg2;
  123. #else
  124. char *base;
  125. base = (char *) ts_resource(*((int *) mh_arg2));
  126. #endif
  127. p = (zend_long *) (base+(size_t) mh_arg1);
  128. val = zend_atol(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
  129. if (stage != ZEND_INI_STAGE_STARTUP &&
  130. stage != ZEND_INI_STAGE_SHUTDOWN &&
  131. *p != val &&
  132. (*p < 0 || val < 0)) {
  133. zend_error(E_WARNING, "zend.assertions may be completely enabled or disabled only in php.ini");
  134. return FAILURE;
  135. }
  136. *p = val;
  137. return SUCCESS;
  138. }
  139. /* }}} */
  140. ZEND_INI_BEGIN()
  141. ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting)
  142. STD_ZEND_INI_ENTRY("zend.assertions", "1", ZEND_INI_ALL, OnUpdateAssertions, assertions, zend_executor_globals, executor_globals)
  143. ZEND_INI_ENTRY3_EX("zend.enable_gc", "1", ZEND_INI_ALL, OnUpdateGCEnabled, NULL, NULL, NULL, zend_gc_enabled_displayer_cb)
  144. STD_ZEND_INI_BOOLEAN("zend.multibyte", "0", ZEND_INI_PERDIR, OnUpdateBool, multibyte, zend_compiler_globals, compiler_globals)
  145. ZEND_INI_ENTRY("zend.script_encoding", NULL, ZEND_INI_ALL, OnUpdateScriptEncoding)
  146. STD_ZEND_INI_BOOLEAN("zend.detect_unicode", "1", ZEND_INI_ALL, OnUpdateBool, detect_unicode, zend_compiler_globals, compiler_globals)
  147. #ifdef ZEND_SIGNALS
  148. STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals)
  149. #endif
  150. ZEND_INI_END()
  151. ZEND_API size_t zend_vspprintf(char **pbuf, size_t max_len, const char *format, va_list ap) /* {{{ */
  152. {
  153. smart_string buf = {0};
  154. /* since there are places where (v)spprintf called without checking for null,
  155. a bit of defensive coding here */
  156. if (!pbuf) {
  157. return 0;
  158. }
  159. zend_printf_to_smart_string(&buf, format, ap);
  160. if (max_len && buf.len > max_len) {
  161. buf.len = max_len;
  162. }
  163. smart_string_0(&buf);
  164. if (buf.c) {
  165. *pbuf = buf.c;
  166. return buf.len;
  167. } else {
  168. *pbuf = estrndup("", 0);
  169. return 0;
  170. }
  171. }
  172. /* }}} */
  173. ZEND_API size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
  174. {
  175. va_list arg;
  176. size_t len;
  177. va_start(arg, format);
  178. len = zend_vspprintf(message, max_len, format, arg);
  179. va_end(arg);
  180. return len;
  181. }
  182. /* }}} */
  183. ZEND_API size_t zend_spprintf_unchecked(char **message, size_t max_len, const char *format, ...) /* {{{ */
  184. {
  185. va_list arg;
  186. size_t len;
  187. va_start(arg, format);
  188. len = zend_vspprintf(message, max_len, format, arg);
  189. va_end(arg);
  190. return len;
  191. }
  192. /* }}} */
  193. ZEND_API zend_string *zend_vstrpprintf(size_t max_len, const char *format, va_list ap) /* {{{ */
  194. {
  195. smart_str buf = {0};
  196. zend_printf_to_smart_str(&buf, format, ap);
  197. if (!buf.s) {
  198. return ZSTR_EMPTY_ALLOC();
  199. }
  200. if (max_len && ZSTR_LEN(buf.s) > max_len) {
  201. ZSTR_LEN(buf.s) = max_len;
  202. }
  203. smart_str_0(&buf);
  204. return buf.s;
  205. }
  206. /* }}} */
  207. ZEND_API zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
  208. {
  209. va_list arg;
  210. zend_string *str;
  211. va_start(arg, format);
  212. str = zend_vstrpprintf(max_len, format, arg);
  213. va_end(arg);
  214. return str;
  215. }
  216. /* }}} */
  217. ZEND_API zend_string *zend_strpprintf_unchecked(size_t max_len, const char *format, ...) /* {{{ */
  218. {
  219. va_list arg;
  220. zend_string *str;
  221. va_start(arg, format);
  222. str = zend_vstrpprintf(max_len, format, arg);
  223. va_end(arg);
  224. return str;
  225. }
  226. /* }}} */
  227. static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent);
  228. static void print_hash(smart_str *buf, HashTable *ht, int indent, zend_bool is_object) /* {{{ */
  229. {
  230. zval *tmp;
  231. zend_string *string_key;
  232. zend_ulong num_key;
  233. int i;
  234. for (i = 0; i < indent; i++) {
  235. smart_str_appendc(buf, ' ');
  236. }
  237. smart_str_appends(buf, "(\n");
  238. indent += PRINT_ZVAL_INDENT;
  239. ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
  240. for (i = 0; i < indent; i++) {
  241. smart_str_appendc(buf, ' ');
  242. }
  243. smart_str_appendc(buf, '[');
  244. if (string_key) {
  245. if (is_object) {
  246. const char *prop_name, *class_name;
  247. size_t prop_len;
  248. int mangled = zend_unmangle_property_name_ex(string_key, &class_name, &prop_name, &prop_len);
  249. smart_str_appendl(buf, prop_name, prop_len);
  250. if (class_name && mangled == SUCCESS) {
  251. if (class_name[0] == '*') {
  252. smart_str_appends(buf, ":protected");
  253. } else {
  254. smart_str_appends(buf, ":");
  255. smart_str_appends(buf, class_name);
  256. smart_str_appends(buf, ":private");
  257. }
  258. }
  259. } else {
  260. smart_str_append(buf, string_key);
  261. }
  262. } else {
  263. smart_str_append_long(buf, num_key);
  264. }
  265. smart_str_appends(buf, "] => ");
  266. zend_print_zval_r_to_buf(buf, tmp, indent+PRINT_ZVAL_INDENT);
  267. smart_str_appends(buf, "\n");
  268. } ZEND_HASH_FOREACH_END();
  269. indent -= PRINT_ZVAL_INDENT;
  270. for (i = 0; i < indent; i++) {
  271. smart_str_appendc(buf, ' ');
  272. }
  273. smart_str_appends(buf, ")\n");
  274. }
  275. /* }}} */
  276. static void print_flat_hash(HashTable *ht) /* {{{ */
  277. {
  278. zval *tmp;
  279. zend_string *string_key;
  280. zend_ulong num_key;
  281. int i = 0;
  282. ZEND_HASH_FOREACH_KEY_VAL_IND(ht, num_key, string_key, tmp) {
  283. if (i++ > 0) {
  284. ZEND_PUTS(",");
  285. }
  286. ZEND_PUTS("[");
  287. if (string_key) {
  288. ZEND_WRITE(ZSTR_VAL(string_key), ZSTR_LEN(string_key));
  289. } else {
  290. zend_printf(ZEND_ULONG_FMT, num_key);
  291. }
  292. ZEND_PUTS("] => ");
  293. zend_print_flat_zval_r(tmp);
  294. } ZEND_HASH_FOREACH_END();
  295. }
  296. /* }}} */
  297. ZEND_API int zend_make_printable_zval(zval *expr, zval *expr_copy) /* {{{ */
  298. {
  299. if (Z_TYPE_P(expr) == IS_STRING) {
  300. return 0;
  301. } else {
  302. ZVAL_STR(expr_copy, zval_get_string_func(expr));
  303. return 1;
  304. }
  305. }
  306. /* }}} */
  307. ZEND_API size_t zend_print_zval(zval *expr, int indent) /* {{{ */
  308. {
  309. zend_string *tmp_str;
  310. zend_string *str = zval_get_tmp_string(expr, &tmp_str);
  311. size_t len = ZSTR_LEN(str);
  312. if (len != 0) {
  313. zend_write(ZSTR_VAL(str), len);
  314. }
  315. zend_tmp_string_release(tmp_str);
  316. return len;
  317. }
  318. /* }}} */
  319. ZEND_API void zend_print_flat_zval_r(zval *expr) /* {{{ */
  320. {
  321. switch (Z_TYPE_P(expr)) {
  322. case IS_ARRAY:
  323. ZEND_PUTS("Array (");
  324. if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
  325. if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
  326. ZEND_PUTS(" *RECURSION*");
  327. return;
  328. }
  329. GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
  330. }
  331. print_flat_hash(Z_ARRVAL_P(expr));
  332. ZEND_PUTS(")");
  333. if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
  334. GC_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
  335. }
  336. break;
  337. case IS_OBJECT:
  338. {
  339. HashTable *properties = NULL;
  340. zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
  341. zend_printf("%s Object (", ZSTR_VAL(class_name));
  342. zend_string_release_ex(class_name, 0);
  343. if (GC_IS_RECURSIVE(Z_OBJ_P(expr))) {
  344. ZEND_PUTS(" *RECURSION*");
  345. return;
  346. }
  347. if (Z_OBJ_HANDLER_P(expr, get_properties)) {
  348. properties = Z_OBJPROP_P(expr);
  349. }
  350. if (properties) {
  351. GC_PROTECT_RECURSION(Z_OBJ_P(expr));
  352. print_flat_hash(properties);
  353. GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
  354. }
  355. ZEND_PUTS(")");
  356. break;
  357. }
  358. case IS_REFERENCE:
  359. zend_print_flat_zval_r(Z_REFVAL_P(expr));
  360. break;
  361. default:
  362. zend_print_zval(expr, 0);
  363. break;
  364. }
  365. }
  366. /* }}} */
  367. static void zend_print_zval_r_to_buf(smart_str *buf, zval *expr, int indent) /* {{{ */
  368. {
  369. switch (Z_TYPE_P(expr)) {
  370. case IS_ARRAY:
  371. smart_str_appends(buf, "Array\n");
  372. if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
  373. if (GC_IS_RECURSIVE(Z_ARRVAL_P(expr))) {
  374. smart_str_appends(buf, " *RECURSION*");
  375. return;
  376. }
  377. GC_PROTECT_RECURSION(Z_ARRVAL_P(expr));
  378. }
  379. print_hash(buf, Z_ARRVAL_P(expr), indent, 0);
  380. if (!(GC_FLAGS(Z_ARRVAL_P(expr)) & GC_IMMUTABLE)) {
  381. GC_UNPROTECT_RECURSION(Z_ARRVAL_P(expr));
  382. }
  383. break;
  384. case IS_OBJECT:
  385. {
  386. HashTable *properties;
  387. int is_temp;
  388. zend_string *class_name = Z_OBJ_HANDLER_P(expr, get_class_name)(Z_OBJ_P(expr));
  389. smart_str_appends(buf, ZSTR_VAL(class_name));
  390. zend_string_release_ex(class_name, 0);
  391. smart_str_appends(buf, " Object\n");
  392. if (GC_IS_RECURSIVE(Z_OBJ_P(expr))) {
  393. smart_str_appends(buf, " *RECURSION*");
  394. return;
  395. }
  396. if ((properties = Z_OBJDEBUG_P(expr, is_temp)) == NULL) {
  397. break;
  398. }
  399. GC_PROTECT_RECURSION(Z_OBJ_P(expr));
  400. print_hash(buf, properties, indent, 1);
  401. GC_UNPROTECT_RECURSION(Z_OBJ_P(expr));
  402. if (is_temp) {
  403. zend_hash_destroy(properties);
  404. FREE_HASHTABLE(properties);
  405. }
  406. break;
  407. }
  408. case IS_LONG:
  409. smart_str_append_long(buf, Z_LVAL_P(expr));
  410. break;
  411. case IS_REFERENCE:
  412. zend_print_zval_r_to_buf(buf, Z_REFVAL_P(expr), indent);
  413. break;
  414. case IS_STRING:
  415. smart_str_append(buf, Z_STR_P(expr));
  416. break;
  417. default:
  418. {
  419. zend_string *str = zval_get_string_func(expr);
  420. smart_str_append(buf, str);
  421. zend_string_release_ex(str, 0);
  422. }
  423. break;
  424. }
  425. }
  426. /* }}} */
  427. ZEND_API zend_string *zend_print_zval_r_to_str(zval *expr, int indent) /* {{{ */
  428. {
  429. smart_str buf = {0};
  430. zend_print_zval_r_to_buf(&buf, expr, indent);
  431. smart_str_0(&buf);
  432. return buf.s;
  433. }
  434. /* }}} */
  435. ZEND_API void zend_print_zval_r(zval *expr, int indent) /* {{{ */
  436. {
  437. zend_string *str = zend_print_zval_r_to_str(expr, indent);
  438. zend_write(ZSTR_VAL(str), ZSTR_LEN(str));
  439. zend_string_release_ex(str, 0);
  440. }
  441. /* }}} */
  442. static FILE *zend_fopen_wrapper(const char *filename, zend_string **opened_path) /* {{{ */
  443. {
  444. if (opened_path) {
  445. *opened_path = zend_string_init(filename, strlen(filename), 0);
  446. }
  447. return fopen(filename, "rb");
  448. }
  449. /* }}} */
  450. #ifdef ZTS
  451. static zend_bool short_tags_default = 1;
  452. static uint32_t compiler_options_default = ZEND_COMPILE_DEFAULT;
  453. #else
  454. # define short_tags_default 1
  455. # define compiler_options_default ZEND_COMPILE_DEFAULT
  456. #endif
  457. static void zend_set_default_compile_time_values(void) /* {{{ */
  458. {
  459. /* default compile-time values */
  460. CG(short_tags) = short_tags_default;
  461. CG(compiler_options) = compiler_options_default;
  462. }
  463. /* }}} */
  464. #ifdef ZEND_WIN32
  465. static void zend_get_windows_version_info(OSVERSIONINFOEX *osvi) /* {{{ */
  466. {
  467. ZeroMemory(osvi, sizeof(OSVERSIONINFOEX));
  468. osvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
  469. if(!GetVersionEx((OSVERSIONINFO *) osvi)) {
  470. ZEND_ASSERT(0); /* Should not happen as sizeof is used. */
  471. }
  472. }
  473. /* }}} */
  474. #endif
  475. static void zend_init_exception_op(void) /* {{{ */
  476. {
  477. memset(EG(exception_op), 0, sizeof(EG(exception_op)));
  478. EG(exception_op)[0].opcode = ZEND_HANDLE_EXCEPTION;
  479. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op));
  480. EG(exception_op)[1].opcode = ZEND_HANDLE_EXCEPTION;
  481. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+1);
  482. EG(exception_op)[2].opcode = ZEND_HANDLE_EXCEPTION;
  483. ZEND_VM_SET_OPCODE_HANDLER(EG(exception_op)+2);
  484. }
  485. /* }}} */
  486. static void zend_init_call_trampoline_op(void) /* {{{ */
  487. {
  488. memset(&EG(call_trampoline_op), 0, sizeof(EG(call_trampoline_op)));
  489. EG(call_trampoline_op).opcode = ZEND_CALL_TRAMPOLINE;
  490. ZEND_VM_SET_OPCODE_HANDLER(&EG(call_trampoline_op));
  491. }
  492. /* }}} */
  493. static void auto_global_dtor(zval *zv) /* {{{ */
  494. {
  495. free(Z_PTR_P(zv));
  496. }
  497. /* }}} */
  498. #ifdef ZTS
  499. static void function_copy_ctor(zval *zv) /* {{{ */
  500. {
  501. zend_function *old_func = Z_FUNC_P(zv);
  502. zend_function *func = pemalloc(sizeof(zend_internal_function), 1);
  503. Z_FUNC_P(zv) = func;
  504. memcpy(func, old_func, sizeof(zend_internal_function));
  505. function_add_ref(func);
  506. if ((old_func->common.fn_flags & (ZEND_ACC_HAS_RETURN_TYPE|ZEND_ACC_HAS_TYPE_HINTS))
  507. && old_func->common.arg_info) {
  508. uint32_t i;
  509. uint32_t num_args = old_func->common.num_args + 1;
  510. zend_arg_info *arg_info = old_func->common.arg_info - 1;
  511. zend_arg_info *new_arg_info;
  512. if (old_func->common.fn_flags & ZEND_ACC_VARIADIC) {
  513. num_args++;
  514. }
  515. new_arg_info = pemalloc(sizeof(zend_arg_info) * num_args, 1);
  516. memcpy(new_arg_info, arg_info, sizeof(zend_arg_info) * num_args);
  517. for (i = 0 ; i < num_args; i++) {
  518. if (ZEND_TYPE_IS_CLASS(arg_info[i].type)) {
  519. zend_string *name = zend_string_dup(ZEND_TYPE_NAME(arg_info[i].type), 1);
  520. new_arg_info[i].type =
  521. ZEND_TYPE_ENCODE_CLASS(
  522. name, ZEND_TYPE_ALLOW_NULL(arg_info[i].type));
  523. }
  524. }
  525. func->common.arg_info = new_arg_info + 1;
  526. }
  527. }
  528. /* }}} */
  529. static void auto_global_copy_ctor(zval *zv) /* {{{ */
  530. {
  531. zend_auto_global *old_ag = (zend_auto_global *) Z_PTR_P(zv);
  532. zend_auto_global *new_ag = pemalloc(sizeof(zend_auto_global), 1);
  533. new_ag->name = old_ag->name;
  534. new_ag->auto_global_callback = old_ag->auto_global_callback;
  535. new_ag->jit = old_ag->jit;
  536. Z_PTR_P(zv) = new_ag;
  537. }
  538. /* }}} */
  539. static void compiler_globals_ctor(zend_compiler_globals *compiler_globals) /* {{{ */
  540. {
  541. compiler_globals->compiled_filename = NULL;
  542. compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
  543. zend_hash_init_ex(compiler_globals->function_table, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
  544. zend_hash_copy(compiler_globals->function_table, global_function_table, function_copy_ctor);
  545. compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
  546. zend_hash_init_ex(compiler_globals->class_table, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
  547. zend_hash_copy(compiler_globals->class_table, global_class_table, zend_class_add_ref);
  548. zend_set_default_compile_time_values();
  549. compiler_globals->auto_globals = (HashTable *) malloc(sizeof(HashTable));
  550. zend_hash_init_ex(compiler_globals->auto_globals, 8, NULL, auto_global_dtor, 1, 0);
  551. zend_hash_copy(compiler_globals->auto_globals, global_auto_globals_table, auto_global_copy_ctor);
  552. compiler_globals->last_static_member = zend_hash_num_elements(compiler_globals->class_table);
  553. if (compiler_globals->last_static_member) {
  554. compiler_globals->static_members_table = calloc(compiler_globals->last_static_member, sizeof(zval*));
  555. } else {
  556. compiler_globals->static_members_table = NULL;
  557. }
  558. compiler_globals->script_encoding_list = NULL;
  559. }
  560. /* }}} */
  561. static void compiler_globals_dtor(zend_compiler_globals *compiler_globals) /* {{{ */
  562. {
  563. if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) {
  564. zend_hash_destroy(compiler_globals->function_table);
  565. free(compiler_globals->function_table);
  566. }
  567. if (compiler_globals->class_table != GLOBAL_CLASS_TABLE) {
  568. zend_hash_destroy(compiler_globals->class_table);
  569. free(compiler_globals->class_table);
  570. }
  571. if (compiler_globals->auto_globals != GLOBAL_AUTO_GLOBALS_TABLE) {
  572. zend_hash_destroy(compiler_globals->auto_globals);
  573. free(compiler_globals->auto_globals);
  574. }
  575. if (compiler_globals->static_members_table) {
  576. free(compiler_globals->static_members_table);
  577. }
  578. if (compiler_globals->script_encoding_list) {
  579. pefree((char*)compiler_globals->script_encoding_list, 1);
  580. }
  581. compiler_globals->last_static_member = 0;
  582. }
  583. /* }}} */
  584. static void executor_globals_ctor(zend_executor_globals *executor_globals) /* {{{ */
  585. {
  586. ZEND_TSRMLS_CACHE_UPDATE();
  587. zend_startup_constants();
  588. zend_copy_constants(executor_globals->zend_constants, GLOBAL_CONSTANTS_TABLE);
  589. zend_init_rsrc_plist();
  590. zend_init_exception_op();
  591. zend_init_call_trampoline_op();
  592. memset(&executor_globals->trampoline, 0, sizeof(zend_op_array));
  593. executor_globals->lambda_count = 0;
  594. ZVAL_UNDEF(&executor_globals->user_error_handler);
  595. ZVAL_UNDEF(&executor_globals->user_exception_handler);
  596. executor_globals->in_autoload = NULL;
  597. executor_globals->current_execute_data = NULL;
  598. executor_globals->current_module = NULL;
  599. executor_globals->exit_status = 0;
  600. #if XPFPA_HAVE_CW
  601. executor_globals->saved_fpu_cw = 0;
  602. #endif
  603. executor_globals->saved_fpu_cw_ptr = NULL;
  604. executor_globals->active = 0;
  605. executor_globals->bailout = NULL;
  606. executor_globals->error_handling = EH_NORMAL;
  607. executor_globals->exception_class = NULL;
  608. executor_globals->exception = NULL;
  609. executor_globals->objects_store.object_buckets = NULL;
  610. #ifdef ZEND_WIN32
  611. zend_get_windows_version_info(&executor_globals->windows_version_info);
  612. #endif
  613. executor_globals->flags = EG_FLAGS_INITIAL;
  614. }
  615. /* }}} */
  616. static void executor_globals_dtor(zend_executor_globals *executor_globals) /* {{{ */
  617. {
  618. zend_ini_dtor(executor_globals->ini_directives);
  619. if (&executor_globals->persistent_list != global_persistent_list) {
  620. zend_destroy_rsrc_list(&executor_globals->persistent_list);
  621. }
  622. if (executor_globals->zend_constants != GLOBAL_CONSTANTS_TABLE) {
  623. zend_hash_destroy(executor_globals->zend_constants);
  624. free(executor_globals->zend_constants);
  625. }
  626. }
  627. /* }}} */
  628. static void zend_new_thread_end_handler(THREAD_T thread_id) /* {{{ */
  629. {
  630. if (zend_copy_ini_directives() == SUCCESS) {
  631. zend_ini_refresh_caches(ZEND_INI_STAGE_STARTUP);
  632. }
  633. }
  634. /* }}} */
  635. #endif
  636. #if defined(__FreeBSD__) || defined(__DragonFly__)
  637. /* FreeBSD and DragonFly floating point precision fix */
  638. #include <floatingpoint.h>
  639. #endif
  640. static void ini_scanner_globals_ctor(zend_ini_scanner_globals *scanner_globals_p) /* {{{ */
  641. {
  642. memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
  643. }
  644. /* }}} */
  645. static void php_scanner_globals_ctor(zend_php_scanner_globals *scanner_globals_p) /* {{{ */
  646. {
  647. memset(scanner_globals_p, 0, sizeof(*scanner_globals_p));
  648. }
  649. /* }}} */
  650. static void module_destructor_zval(zval *zv) /* {{{ */
  651. {
  652. zend_module_entry *module = (zend_module_entry*)Z_PTR_P(zv);
  653. module_destructor(module);
  654. free(module);
  655. }
  656. /* }}} */
  657. static zend_bool php_auto_globals_create_globals(zend_string *name) /* {{{ */
  658. {
  659. zval globals;
  660. /* IS_ARRAY, but with ref-counter 1 and not IS_TYPE_REFCOUNTED */
  661. ZVAL_ARR(&globals, &EG(symbol_table));
  662. Z_TYPE_FLAGS_P(&globals) = 0;
  663. ZVAL_NEW_REF(&globals, &globals);
  664. zend_hash_update(&EG(symbol_table), name, &globals);
  665. return 0;
  666. }
  667. /* }}} */
  668. int zend_startup(zend_utility_functions *utility_functions, char **extensions) /* {{{ */
  669. {
  670. #ifdef ZTS
  671. zend_compiler_globals *compiler_globals;
  672. zend_executor_globals *executor_globals;
  673. extern ZEND_API ts_rsrc_id ini_scanner_globals_id;
  674. extern ZEND_API ts_rsrc_id language_scanner_globals_id;
  675. ZEND_TSRMLS_CACHE_UPDATE();
  676. #else
  677. extern zend_ini_scanner_globals ini_scanner_globals;
  678. extern zend_php_scanner_globals language_scanner_globals;
  679. #endif
  680. zend_cpu_startup();
  681. #ifdef ZEND_WIN32
  682. php_win32_cp_set_by_id(65001);
  683. #endif
  684. start_memory_manager();
  685. virtual_cwd_startup(); /* Could use shutdown to free the main cwd but it would just slow it down for CGI */
  686. #if defined(__FreeBSD__) || defined(__DragonFly__)
  687. /* FreeBSD and DragonFly floating point precision fix */
  688. fpsetmask(0);
  689. #endif
  690. zend_startup_strtod();
  691. zend_startup_extensions_mechanism();
  692. /* Set up utility functions and values */
  693. zend_error_cb = utility_functions->error_function;
  694. zend_printf = utility_functions->printf_function;
  695. zend_write = (zend_write_func_t) utility_functions->write_function;
  696. zend_fopen = utility_functions->fopen_function;
  697. if (!zend_fopen) {
  698. zend_fopen = zend_fopen_wrapper;
  699. }
  700. zend_stream_open_function = utility_functions->stream_open_function;
  701. zend_message_dispatcher_p = utility_functions->message_handler;
  702. zend_get_configuration_directive_p = utility_functions->get_configuration_directive;
  703. zend_ticks_function = utility_functions->ticks_function;
  704. zend_on_timeout = utility_functions->on_timeout;
  705. zend_printf_to_smart_string = utility_functions->printf_to_smart_string_function;
  706. zend_printf_to_smart_str = utility_functions->printf_to_smart_str_function;
  707. zend_getenv = utility_functions->getenv_function;
  708. zend_resolve_path = utility_functions->resolve_path_function;
  709. zend_interrupt_function = NULL;
  710. #if HAVE_DTRACE
  711. /* build with dtrace support */
  712. {
  713. char *tmp = getenv("USE_ZEND_DTRACE");
  714. if (tmp && zend_atoi(tmp, 0)) {
  715. zend_dtrace_enabled = 1;
  716. zend_compile_file = dtrace_compile_file;
  717. zend_execute_ex = dtrace_execute_ex;
  718. zend_execute_internal = dtrace_execute_internal;
  719. } else {
  720. zend_compile_file = compile_file;
  721. zend_execute_ex = execute_ex;
  722. zend_execute_internal = NULL;
  723. }
  724. }
  725. #else
  726. zend_compile_file = compile_file;
  727. zend_execute_ex = execute_ex;
  728. zend_execute_internal = NULL;
  729. #endif /* HAVE_DTRACE */
  730. zend_compile_string = compile_string;
  731. zend_throw_exception_hook = NULL;
  732. /* Set up the default garbage collection implementation. */
  733. gc_collect_cycles = zend_gc_collect_cycles;
  734. zend_vm_init();
  735. /* set up version */
  736. zend_version_info = strdup(ZEND_CORE_VERSION_INFO);
  737. zend_version_info_length = sizeof(ZEND_CORE_VERSION_INFO) - 1;
  738. GLOBAL_FUNCTION_TABLE = (HashTable *) malloc(sizeof(HashTable));
  739. GLOBAL_CLASS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  740. GLOBAL_AUTO_GLOBALS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  741. GLOBAL_CONSTANTS_TABLE = (HashTable *) malloc(sizeof(HashTable));
  742. zend_hash_init_ex(GLOBAL_FUNCTION_TABLE, 1024, NULL, ZEND_FUNCTION_DTOR, 1, 0);
  743. zend_hash_init_ex(GLOBAL_CLASS_TABLE, 64, NULL, ZEND_CLASS_DTOR, 1, 0);
  744. zend_hash_init_ex(GLOBAL_AUTO_GLOBALS_TABLE, 8, NULL, auto_global_dtor, 1, 0);
  745. zend_hash_init_ex(GLOBAL_CONSTANTS_TABLE, 128, NULL, ZEND_CONSTANT_DTOR, 1, 0);
  746. zend_hash_init_ex(&module_registry, 32, NULL, module_destructor_zval, 1, 0);
  747. zend_init_rsrc_list_dtors();
  748. #ifdef ZTS
  749. ts_allocate_id(&compiler_globals_id, sizeof(zend_compiler_globals), (ts_allocate_ctor) compiler_globals_ctor, (ts_allocate_dtor) compiler_globals_dtor);
  750. ts_allocate_id(&executor_globals_id, sizeof(zend_executor_globals), (ts_allocate_ctor) executor_globals_ctor, (ts_allocate_dtor) executor_globals_dtor);
  751. ts_allocate_id(&language_scanner_globals_id, sizeof(zend_php_scanner_globals), (ts_allocate_ctor) php_scanner_globals_ctor, NULL);
  752. ts_allocate_id(&ini_scanner_globals_id, sizeof(zend_ini_scanner_globals), (ts_allocate_ctor) ini_scanner_globals_ctor, NULL);
  753. compiler_globals = ts_resource(compiler_globals_id);
  754. executor_globals = ts_resource(executor_globals_id);
  755. compiler_globals_dtor(compiler_globals);
  756. compiler_globals->in_compilation = 0;
  757. compiler_globals->function_table = (HashTable *) malloc(sizeof(HashTable));
  758. compiler_globals->class_table = (HashTable *) malloc(sizeof(HashTable));
  759. *compiler_globals->function_table = *GLOBAL_FUNCTION_TABLE;
  760. *compiler_globals->class_table = *GLOBAL_CLASS_TABLE;
  761. compiler_globals->auto_globals = GLOBAL_AUTO_GLOBALS_TABLE;
  762. zend_hash_destroy(executor_globals->zend_constants);
  763. *executor_globals->zend_constants = *GLOBAL_CONSTANTS_TABLE;
  764. #else
  765. ini_scanner_globals_ctor(&ini_scanner_globals);
  766. php_scanner_globals_ctor(&language_scanner_globals);
  767. zend_set_default_compile_time_values();
  768. #ifdef ZEND_WIN32
  769. zend_get_windows_version_info(&EG(windows_version_info));
  770. #endif
  771. #endif
  772. EG(error_reporting) = E_ALL & ~E_NOTICE;
  773. zend_interned_strings_init();
  774. zend_startup_builtin_functions();
  775. zend_register_standard_constants();
  776. zend_register_auto_global(zend_string_init_interned("GLOBALS", sizeof("GLOBALS") - 1, 1), 1, php_auto_globals_create_globals);
  777. #ifndef ZTS
  778. zend_init_rsrc_plist();
  779. zend_init_exception_op();
  780. zend_init_call_trampoline_op();
  781. #endif
  782. zend_ini_startup();
  783. #ifdef ZEND_WIN32
  784. /* Uses INI settings, so needs to be run after it. */
  785. php_win32_cp_setup();
  786. #endif
  787. #ifdef ZTS
  788. tsrm_set_new_thread_end_handler(zend_new_thread_end_handler);
  789. tsrm_set_shutdown_handler(zend_interned_strings_dtor);
  790. #endif
  791. return SUCCESS;
  792. }
  793. /* }}} */
  794. void zend_register_standard_ini_entries(void) /* {{{ */
  795. {
  796. int module_number = 0;
  797. REGISTER_INI_ENTRIES();
  798. }
  799. /* }}} */
  800. /* Unlink the global (r/o) copies of the class, function and constant tables,
  801. * and use a fresh r/w copy for the startup thread
  802. */
  803. int zend_post_startup(void) /* {{{ */
  804. {
  805. #ifdef ZTS
  806. zend_encoding **script_encoding_list;
  807. zend_compiler_globals *compiler_globals = ts_resource(compiler_globals_id);
  808. zend_executor_globals *executor_globals = ts_resource(executor_globals_id);
  809. *GLOBAL_FUNCTION_TABLE = *compiler_globals->function_table;
  810. *GLOBAL_CLASS_TABLE = *compiler_globals->class_table;
  811. *GLOBAL_CONSTANTS_TABLE = *executor_globals->zend_constants;
  812. short_tags_default = CG(short_tags);
  813. compiler_options_default = CG(compiler_options);
  814. zend_destroy_rsrc_list(&EG(persistent_list));
  815. free(compiler_globals->function_table);
  816. free(compiler_globals->class_table);
  817. if ((script_encoding_list = (zend_encoding **)compiler_globals->script_encoding_list)) {
  818. compiler_globals_ctor(compiler_globals);
  819. compiler_globals->script_encoding_list = (const zend_encoding **)script_encoding_list;
  820. } else {
  821. compiler_globals_ctor(compiler_globals);
  822. }
  823. free(EG(zend_constants));
  824. executor_globals_ctor(executor_globals);
  825. global_persistent_list = &EG(persistent_list);
  826. zend_copy_ini_directives();
  827. #endif
  828. if (zend_post_startup_cb) {
  829. int (*cb)(void) = zend_post_startup_cb;
  830. zend_post_startup_cb = NULL;
  831. if (cb() != SUCCESS) {
  832. return FAILURE;
  833. }
  834. }
  835. return SUCCESS;
  836. }
  837. /* }}} */
  838. void zend_shutdown(void) /* {{{ */
  839. {
  840. zend_vm_dtor();
  841. zend_destroy_rsrc_list(&EG(persistent_list));
  842. zend_destroy_modules();
  843. virtual_cwd_deactivate();
  844. virtual_cwd_shutdown();
  845. zend_hash_destroy(GLOBAL_FUNCTION_TABLE);
  846. zend_hash_destroy(GLOBAL_CLASS_TABLE);
  847. zend_hash_destroy(GLOBAL_AUTO_GLOBALS_TABLE);
  848. free(GLOBAL_AUTO_GLOBALS_TABLE);
  849. zend_shutdown_extensions();
  850. free(zend_version_info);
  851. free(GLOBAL_FUNCTION_TABLE);
  852. free(GLOBAL_CLASS_TABLE);
  853. zend_hash_destroy(GLOBAL_CONSTANTS_TABLE);
  854. free(GLOBAL_CONSTANTS_TABLE);
  855. zend_shutdown_strtod();
  856. #ifdef ZTS
  857. GLOBAL_FUNCTION_TABLE = NULL;
  858. GLOBAL_CLASS_TABLE = NULL;
  859. GLOBAL_AUTO_GLOBALS_TABLE = NULL;
  860. GLOBAL_CONSTANTS_TABLE = NULL;
  861. #endif
  862. zend_destroy_rsrc_list_dtors();
  863. }
  864. /* }}} */
  865. void zend_set_utility_values(zend_utility_values *utility_values) /* {{{ */
  866. {
  867. zend_uv = *utility_values;
  868. zend_uv.import_use_extension_length = (uint32_t)strlen(zend_uv.import_use_extension);
  869. }
  870. /* }}} */
  871. /* this should be compatible with the standard zenderror */
  872. ZEND_COLD void zenderror(const char *error) /* {{{ */
  873. {
  874. CG(parse_error) = 0;
  875. if (EG(exception)) {
  876. /* An exception was thrown in the lexer, don't throw another in the parser. */
  877. return;
  878. }
  879. zend_throw_exception(zend_ce_parse_error, error, 0);
  880. }
  881. /* }}} */
  882. BEGIN_EXTERN_C()
  883. ZEND_API ZEND_COLD void _zend_bailout(const char *filename, uint32_t lineno) /* {{{ */
  884. {
  885. if (!EG(bailout)) {
  886. zend_output_debug_string(1, "%s(%d) : Bailed out without a bailout address!", filename, lineno);
  887. exit(-1);
  888. }
  889. gc_protect(1);
  890. CG(unclean_shutdown) = 1;
  891. CG(active_class_entry) = NULL;
  892. CG(in_compilation) = 0;
  893. EG(current_execute_data) = NULL;
  894. LONGJMP(*EG(bailout), FAILURE);
  895. }
  896. /* }}} */
  897. END_EXTERN_C()
  898. ZEND_API void zend_append_version_info(const zend_extension *extension) /* {{{ */
  899. {
  900. char *new_info;
  901. uint32_t new_info_length;
  902. new_info_length = (uint32_t)(sizeof(" with v, , by \n")
  903. + strlen(extension->name)
  904. + strlen(extension->version)
  905. + strlen(extension->copyright)
  906. + strlen(extension->author));
  907. new_info = (char *) malloc(new_info_length + 1);
  908. snprintf(new_info, new_info_length, " with %s v%s, %s, by %s\n", extension->name, extension->version, extension->copyright, extension->author);
  909. zend_version_info = (char *) realloc(zend_version_info, zend_version_info_length+new_info_length + 1);
  910. strncat(zend_version_info, new_info, new_info_length);
  911. zend_version_info_length += new_info_length;
  912. free(new_info);
  913. }
  914. /* }}} */
  915. ZEND_API char *get_zend_version(void) /* {{{ */
  916. {
  917. return zend_version_info;
  918. }
  919. /* }}} */
  920. ZEND_API void zend_activate(void) /* {{{ */
  921. {
  922. #ifdef ZTS
  923. virtual_cwd_activate();
  924. #endif
  925. gc_reset();
  926. init_compiler();
  927. init_executor();
  928. startup_scanner();
  929. }
  930. /* }}} */
  931. void zend_call_destructors(void) /* {{{ */
  932. {
  933. zend_try {
  934. shutdown_destructors();
  935. } zend_end_try();
  936. }
  937. /* }}} */
  938. ZEND_API void zend_deactivate(void) /* {{{ */
  939. {
  940. /* we're no longer executing anything */
  941. EG(current_execute_data) = NULL;
  942. zend_try {
  943. shutdown_scanner();
  944. } zend_end_try();
  945. /* shutdown_executor() takes care of its own bailout handling */
  946. shutdown_executor();
  947. zend_try {
  948. zend_ini_deactivate();
  949. } zend_end_try();
  950. zend_try {
  951. shutdown_compiler();
  952. } zend_end_try();
  953. zend_destroy_rsrc_list(&EG(regular_list));
  954. #if GC_BENCH
  955. fprintf(stderr, "GC Statistics\n");
  956. fprintf(stderr, "-------------\n");
  957. fprintf(stderr, "Runs: %d\n", GC_G(gc_runs));
  958. fprintf(stderr, "Collected: %d\n", GC_G(collected));
  959. fprintf(stderr, "Root buffer length: %d\n", GC_G(root_buf_length));
  960. fprintf(stderr, "Root buffer peak: %d\n\n", GC_G(root_buf_peak));
  961. fprintf(stderr, " Possible Remove from Marked\n");
  962. fprintf(stderr, " Root Buffered buffer grey\n");
  963. fprintf(stderr, " -------- -------- ----------- ------\n");
  964. 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));
  965. #endif
  966. }
  967. /* }}} */
  968. BEGIN_EXTERN_C()
  969. ZEND_API void zend_message_dispatcher(zend_long message, const void *data) /* {{{ */
  970. {
  971. if (zend_message_dispatcher_p) {
  972. zend_message_dispatcher_p(message, data);
  973. }
  974. }
  975. /* }}} */
  976. END_EXTERN_C()
  977. ZEND_API zval *zend_get_configuration_directive(zend_string *name) /* {{{ */
  978. {
  979. if (zend_get_configuration_directive_p) {
  980. return zend_get_configuration_directive_p(name);
  981. } else {
  982. return NULL;
  983. }
  984. }
  985. /* }}} */
  986. #define SAVE_STACK(stack) do { \
  987. if (CG(stack).top) { \
  988. memcpy(&stack, &CG(stack), sizeof(zend_stack)); \
  989. CG(stack).top = CG(stack).max = 0; \
  990. CG(stack).elements = NULL; \
  991. } else { \
  992. stack.top = 0; \
  993. } \
  994. } while (0)
  995. #define RESTORE_STACK(stack) do { \
  996. if (stack.top) { \
  997. zend_stack_destroy(&CG(stack)); \
  998. memcpy(&CG(stack), &stack, sizeof(zend_stack)); \
  999. } \
  1000. } while (0)
  1001. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1002. ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) /* {{{ */
  1003. #else
  1004. static ZEND_COLD void zend_error_va_list(int type, const char *format, va_list args)
  1005. #endif
  1006. {
  1007. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1008. va_list args;
  1009. #endif
  1010. va_list usr_copy;
  1011. zval params[5];
  1012. zval retval;
  1013. const char *error_filename;
  1014. uint32_t error_lineno = 0;
  1015. zval orig_user_error_handler;
  1016. zend_bool in_compilation;
  1017. zend_class_entry *saved_class_entry;
  1018. zend_stack loop_var_stack;
  1019. zend_stack delayed_oplines_stack;
  1020. zend_array *symbol_table;
  1021. zend_class_entry *orig_fake_scope;
  1022. /* Report about uncaught exception in case of fatal errors */
  1023. if (EG(exception)) {
  1024. zend_execute_data *ex;
  1025. const zend_op *opline;
  1026. switch (type) {
  1027. case E_CORE_ERROR:
  1028. case E_ERROR:
  1029. case E_RECOVERABLE_ERROR:
  1030. case E_PARSE:
  1031. case E_COMPILE_ERROR:
  1032. case E_USER_ERROR:
  1033. ex = EG(current_execute_data);
  1034. opline = NULL;
  1035. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  1036. ex = ex->prev_execute_data;
  1037. }
  1038. if (ex && ex->opline->opcode == ZEND_HANDLE_EXCEPTION &&
  1039. EG(opline_before_exception)) {
  1040. opline = EG(opline_before_exception);
  1041. }
  1042. zend_exception_error(EG(exception), E_WARNING);
  1043. EG(exception) = NULL;
  1044. if (opline) {
  1045. ex->opline = opline;
  1046. }
  1047. break;
  1048. default:
  1049. break;
  1050. }
  1051. }
  1052. /* Obtain relevant filename and lineno */
  1053. switch (type) {
  1054. case E_CORE_ERROR:
  1055. case E_CORE_WARNING:
  1056. error_filename = NULL;
  1057. error_lineno = 0;
  1058. break;
  1059. case E_PARSE:
  1060. case E_COMPILE_ERROR:
  1061. case E_COMPILE_WARNING:
  1062. case E_ERROR:
  1063. case E_NOTICE:
  1064. case E_STRICT:
  1065. case E_DEPRECATED:
  1066. case E_WARNING:
  1067. case E_USER_ERROR:
  1068. case E_USER_WARNING:
  1069. case E_USER_NOTICE:
  1070. case E_USER_DEPRECATED:
  1071. case E_RECOVERABLE_ERROR:
  1072. if (zend_is_compiling()) {
  1073. error_filename = ZSTR_VAL(zend_get_compiled_filename());
  1074. error_lineno = zend_get_compiled_lineno();
  1075. } else if (zend_is_executing()) {
  1076. error_filename = zend_get_executed_filename();
  1077. if (error_filename[0] == '[') { /* [no active file] */
  1078. error_filename = NULL;
  1079. error_lineno = 0;
  1080. } else {
  1081. error_lineno = zend_get_executed_lineno();
  1082. }
  1083. } else {
  1084. error_filename = NULL;
  1085. error_lineno = 0;
  1086. }
  1087. break;
  1088. default:
  1089. error_filename = NULL;
  1090. error_lineno = 0;
  1091. break;
  1092. }
  1093. if (!error_filename) {
  1094. error_filename = "Unknown";
  1095. }
  1096. #ifdef HAVE_DTRACE
  1097. if (DTRACE_ERROR_ENABLED()) {
  1098. char *dtrace_error_buffer;
  1099. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1100. va_start(args, format);
  1101. #endif
  1102. zend_vspprintf(&dtrace_error_buffer, 0, format, args);
  1103. DTRACE_ERROR(dtrace_error_buffer, (char *)error_filename, error_lineno);
  1104. efree(dtrace_error_buffer);
  1105. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1106. va_end(args);
  1107. #endif
  1108. }
  1109. #endif /* HAVE_DTRACE */
  1110. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1111. va_start(args, format);
  1112. #endif
  1113. /* if we don't have a user defined error handler */
  1114. if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF
  1115. || !(EG(user_error_handler_error_reporting) & type)
  1116. || EG(error_handling) != EH_NORMAL) {
  1117. zend_error_cb(type, error_filename, error_lineno, format, args);
  1118. } else switch (type) {
  1119. case E_ERROR:
  1120. case E_PARSE:
  1121. case E_CORE_ERROR:
  1122. case E_CORE_WARNING:
  1123. case E_COMPILE_ERROR:
  1124. case E_COMPILE_WARNING:
  1125. /* The error may not be safe to handle in user-space */
  1126. zend_error_cb(type, error_filename, error_lineno, format, args);
  1127. break;
  1128. default:
  1129. /* Handle the error in user space */
  1130. va_copy(usr_copy, args);
  1131. ZVAL_STR(&params[1], zend_vstrpprintf(0, format, usr_copy));
  1132. va_end(usr_copy);
  1133. ZVAL_LONG(&params[0], type);
  1134. if (error_filename) {
  1135. ZVAL_STRING(&params[2], error_filename);
  1136. } else {
  1137. ZVAL_NULL(&params[2]);
  1138. }
  1139. ZVAL_LONG(&params[3], error_lineno);
  1140. symbol_table = zend_rebuild_symbol_table();
  1141. /* during shutdown the symbol table table can be still null */
  1142. if (!symbol_table) {
  1143. ZVAL_NULL(&params[4]);
  1144. } else {
  1145. ZVAL_ARR(&params[4], zend_array_dup(symbol_table));
  1146. }
  1147. ZVAL_COPY_VALUE(&orig_user_error_handler, &EG(user_error_handler));
  1148. ZVAL_UNDEF(&EG(user_error_handler));
  1149. /* User error handler may include() additinal PHP files.
  1150. * If an error was generated during comilation PHP will compile
  1151. * such scripts recursivly, but some CG() variables may be
  1152. * inconsistent. */
  1153. in_compilation = CG(in_compilation);
  1154. if (in_compilation) {
  1155. saved_class_entry = CG(active_class_entry);
  1156. CG(active_class_entry) = NULL;
  1157. SAVE_STACK(loop_var_stack);
  1158. SAVE_STACK(delayed_oplines_stack);
  1159. CG(in_compilation) = 0;
  1160. }
  1161. orig_fake_scope = EG(fake_scope);
  1162. EG(fake_scope) = NULL;
  1163. if (call_user_function(CG(function_table), NULL, &orig_user_error_handler, &retval, 5, params) == SUCCESS) {
  1164. if (Z_TYPE(retval) != IS_UNDEF) {
  1165. if (Z_TYPE(retval) == IS_FALSE) {
  1166. zend_error_cb(type, error_filename, error_lineno, format, args);
  1167. }
  1168. zval_ptr_dtor(&retval);
  1169. }
  1170. } else if (!EG(exception)) {
  1171. /* The user error handler failed, use built-in error handler */
  1172. zend_error_cb(type, error_filename, error_lineno, format, args);
  1173. }
  1174. EG(fake_scope) = orig_fake_scope;
  1175. if (in_compilation) {
  1176. CG(active_class_entry) = saved_class_entry;
  1177. RESTORE_STACK(loop_var_stack);
  1178. RESTORE_STACK(delayed_oplines_stack);
  1179. CG(in_compilation) = 1;
  1180. }
  1181. zval_ptr_dtor(&params[4]);
  1182. zval_ptr_dtor(&params[2]);
  1183. zval_ptr_dtor(&params[1]);
  1184. if (Z_TYPE(EG(user_error_handler)) == IS_UNDEF) {
  1185. ZVAL_COPY_VALUE(&EG(user_error_handler), &orig_user_error_handler);
  1186. } else {
  1187. zval_ptr_dtor(&orig_user_error_handler);
  1188. }
  1189. break;
  1190. }
  1191. #if !defined(HAVE_NORETURN) || defined(HAVE_NORETURN_ALIAS)
  1192. va_end(args);
  1193. #endif
  1194. if (type == E_PARSE) {
  1195. /* eval() errors do not affect exit_status */
  1196. if (!(EG(current_execute_data) &&
  1197. EG(current_execute_data)->func &&
  1198. ZEND_USER_CODE(EG(current_execute_data)->func->type) &&
  1199. EG(current_execute_data)->opline->opcode == ZEND_INCLUDE_OR_EVAL &&
  1200. EG(current_execute_data)->opline->extended_value == ZEND_EVAL)) {
  1201. EG(exit_status) = 255;
  1202. }
  1203. }
  1204. }
  1205. /* }}} */
  1206. #ifdef HAVE_NORETURN
  1207. # ifdef HAVE_NORETURN_ALIAS
  1208. ZEND_COLD void zend_error_noreturn(int type, const char *format, ...) __attribute__ ((alias("zend_error"),noreturn));
  1209. # else
  1210. ZEND_API ZEND_COLD void zend_error(int type, const char *format, ...) /* {{{ */
  1211. {
  1212. va_list va;
  1213. va_start(va, format);
  1214. zend_error_va_list(type, format, va);
  1215. va_end(va);
  1216. }
  1217. ZEND_API ZEND_COLD ZEND_NORETURN void zend_error_noreturn(int type, const char *format, ...)
  1218. {
  1219. va_list va;
  1220. va_start(va, format);
  1221. zend_error_va_list(type, format, va);
  1222. va_end(va);
  1223. }
  1224. /* }}} */
  1225. # endif
  1226. #endif
  1227. ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format, ...) /* {{{ */
  1228. {
  1229. va_list va;
  1230. char *message = NULL;
  1231. if (exception_ce) {
  1232. if (!instanceof_function(exception_ce, zend_ce_error)) {
  1233. zend_error(E_NOTICE, "Error exceptions must be derived from Error");
  1234. exception_ce = zend_ce_error;
  1235. }
  1236. } else {
  1237. exception_ce = zend_ce_error;
  1238. }
  1239. va_start(va, format);
  1240. zend_vspprintf(&message, 0, format, va);
  1241. //TODO: we can't convert compile-time errors to exceptions yet???
  1242. if (EG(current_execute_data) && !CG(in_compilation)) {
  1243. zend_throw_exception(exception_ce, message, 0);
  1244. } else {
  1245. zend_error(E_ERROR, "%s", message);
  1246. }
  1247. efree(message);
  1248. va_end(va);
  1249. }
  1250. /* }}} */
  1251. ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
  1252. {
  1253. va_list va;
  1254. char *message = NULL;
  1255. va_start(va, format);
  1256. zend_vspprintf(&message, 0, format, va);
  1257. zend_throw_exception(zend_ce_type_error, message, 0);
  1258. efree(message);
  1259. va_end(va);
  1260. } /* }}} */
  1261. ZEND_API ZEND_COLD void zend_internal_type_error(zend_bool throw_exception, const char *format, ...) /* {{{ */
  1262. {
  1263. va_list va;
  1264. char *message = NULL;
  1265. va_start(va, format);
  1266. zend_vspprintf(&message, 0, format, va);
  1267. if (throw_exception) {
  1268. zend_throw_exception(zend_ce_type_error, message, 0);
  1269. } else {
  1270. zend_error(E_WARNING, "%s", message);
  1271. }
  1272. efree(message);
  1273. va_end(va);
  1274. } /* }}} */
  1275. ZEND_API ZEND_COLD void zend_internal_argument_count_error(zend_bool throw_exception, const char *format, ...) /* {{{ */
  1276. {
  1277. va_list va;
  1278. char *message = NULL;
  1279. va_start(va, format);
  1280. zend_vspprintf(&message, 0, format, va);
  1281. if (throw_exception) {
  1282. zend_throw_exception(zend_ce_argument_count_error, message, 0);
  1283. } else {
  1284. zend_error(E_WARNING, "%s", message);
  1285. }
  1286. efree(message);
  1287. va_end(va);
  1288. } /* }}} */
  1289. ZEND_API ZEND_COLD void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) /* {{{ */
  1290. {
  1291. #if ZEND_DEBUG
  1292. va_list args;
  1293. va_start(args, format);
  1294. # ifdef ZEND_WIN32
  1295. {
  1296. char output_buf[1024];
  1297. vsnprintf(output_buf, 1024, format, args);
  1298. OutputDebugString(output_buf);
  1299. OutputDebugString("\n");
  1300. if (trigger_break && IsDebuggerPresent()) {
  1301. DebugBreak();
  1302. }
  1303. }
  1304. # else
  1305. vfprintf(stderr, format, args);
  1306. fprintf(stderr, "\n");
  1307. # endif
  1308. va_end(args);
  1309. #endif
  1310. }
  1311. /* }}} */
  1312. ZEND_API void zend_try_exception_handler() /* {{{ */
  1313. {
  1314. if (EG(exception)) {
  1315. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  1316. zval orig_user_exception_handler;
  1317. zval params[1], retval2;
  1318. zend_object *old_exception;
  1319. old_exception = EG(exception);
  1320. EG(exception) = NULL;
  1321. ZVAL_OBJ(&params[0], old_exception);
  1322. ZVAL_COPY_VALUE(&orig_user_exception_handler, &EG(user_exception_handler));
  1323. if (call_user_function(CG(function_table), NULL, &orig_user_exception_handler, &retval2, 1, params) == SUCCESS) {
  1324. zval_ptr_dtor(&retval2);
  1325. if (EG(exception)) {
  1326. OBJ_RELEASE(EG(exception));
  1327. EG(exception) = NULL;
  1328. }
  1329. OBJ_RELEASE(old_exception);
  1330. } else {
  1331. EG(exception) = old_exception;
  1332. }
  1333. }
  1334. }
  1335. } /* }}} */
  1336. ZEND_API int zend_execute_scripts(int type, zval *retval, int file_count, ...) /* {{{ */
  1337. {
  1338. va_list files;
  1339. int i;
  1340. zend_file_handle *file_handle;
  1341. zend_op_array *op_array;
  1342. va_start(files, file_count);
  1343. for (i = 0; i < file_count; i++) {
  1344. file_handle = va_arg(files, zend_file_handle *);
  1345. if (!file_handle) {
  1346. continue;
  1347. }
  1348. op_array = zend_compile_file(file_handle, type);
  1349. if (file_handle->opened_path) {
  1350. zend_hash_add_empty_element(&EG(included_files), file_handle->opened_path);
  1351. }
  1352. zend_destroy_file_handle(file_handle);
  1353. if (op_array) {
  1354. zend_execute(op_array, retval);
  1355. zend_exception_restore();
  1356. zend_try_exception_handler();
  1357. if (EG(exception)) {
  1358. zend_exception_error(EG(exception), E_ERROR);
  1359. }
  1360. destroy_op_array(op_array);
  1361. efree_size(op_array, sizeof(zend_op_array));
  1362. } else if (type==ZEND_REQUIRE) {
  1363. va_end(files);
  1364. return FAILURE;
  1365. }
  1366. }
  1367. va_end(files);
  1368. return SUCCESS;
  1369. }
  1370. /* }}} */
  1371. #define COMPILED_STRING_DESCRIPTION_FORMAT "%s(%d) : %s"
  1372. ZEND_API char *zend_make_compiled_string_description(const char *name) /* {{{ */
  1373. {
  1374. const char *cur_filename;
  1375. int cur_lineno;
  1376. char *compiled_string_description;
  1377. if (zend_is_compiling()) {
  1378. cur_filename = ZSTR_VAL(zend_get_compiled_filename());
  1379. cur_lineno = zend_get_compiled_lineno();
  1380. } else if (zend_is_executing()) {
  1381. cur_filename = zend_get_executed_filename();
  1382. cur_lineno = zend_get_executed_lineno();
  1383. } else {
  1384. cur_filename = "Unknown";
  1385. cur_lineno = 0;
  1386. }
  1387. zend_spprintf(&compiled_string_description, 0, COMPILED_STRING_DESCRIPTION_FORMAT, cur_filename, cur_lineno, name);
  1388. return compiled_string_description;
  1389. }
  1390. /* }}} */
  1391. void free_estring(char **str_p) /* {{{ */
  1392. {
  1393. efree(*str_p);
  1394. }
  1395. /* }}} */
  1396. /*
  1397. * Local variables:
  1398. * tab-width: 4
  1399. * c-basic-offset: 4
  1400. * indent-tabs-mode: t
  1401. * End:
  1402. * vim600: sw=4 ts=4 fdm=marker
  1403. * vim<600: sw=4 ts=4
  1404. */