zend_builtin_functions.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Andi Gutmans <andi@php.net> |
  16. | Zeev Suraski <zeev@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "zend.h"
  20. #include "zend_API.h"
  21. #include "zend_gc.h"
  22. #include "zend_builtin_functions.h"
  23. #include "zend_constants.h"
  24. #include "zend_ini.h"
  25. #include "zend_exceptions.h"
  26. #include "zend_extensions.h"
  27. #include "zend_closures.h"
  28. #include "zend_generators.h"
  29. #include "zend_builtin_functions_arginfo.h"
  30. #include "zend_smart_str.h"
  31. /* }}} */
  32. ZEND_MINIT_FUNCTION(core) { /* {{{ */
  33. zend_standard_class_def = register_class_stdClass();
  34. zend_register_default_classes();
  35. return SUCCESS;
  36. }
  37. /* }}} */
  38. zend_module_entry zend_builtin_module = { /* {{{ */
  39. STANDARD_MODULE_HEADER,
  40. "Core",
  41. ext_functions,
  42. ZEND_MINIT(core),
  43. NULL,
  44. NULL,
  45. NULL,
  46. NULL,
  47. ZEND_VERSION,
  48. STANDARD_MODULE_PROPERTIES
  49. };
  50. /* }}} */
  51. zend_result zend_startup_builtin_functions(void) /* {{{ */
  52. {
  53. zend_builtin_module.module_number = 0;
  54. zend_builtin_module.type = MODULE_PERSISTENT;
  55. return (EG(current_module) = zend_register_module_ex(&zend_builtin_module)) == NULL ? FAILURE : SUCCESS;
  56. }
  57. /* }}} */
  58. /* {{{ Get the version of the Zend Engine */
  59. ZEND_FUNCTION(zend_version)
  60. {
  61. ZEND_PARSE_PARAMETERS_NONE();
  62. RETURN_STRINGL(ZEND_VERSION, sizeof(ZEND_VERSION)-1);
  63. }
  64. /* }}} */
  65. /* {{{ Reclaims memory used by MM caches.
  66. Returns number of freed bytes */
  67. ZEND_FUNCTION(gc_mem_caches)
  68. {
  69. ZEND_PARSE_PARAMETERS_NONE();
  70. RETURN_LONG(zend_mm_gc(zend_mm_get_heap()));
  71. }
  72. /* }}} */
  73. /* {{{ Forces collection of any existing garbage cycles.
  74. Returns number of freed zvals */
  75. ZEND_FUNCTION(gc_collect_cycles)
  76. {
  77. ZEND_PARSE_PARAMETERS_NONE();
  78. RETURN_LONG(gc_collect_cycles());
  79. }
  80. /* }}} */
  81. /* {{{ Returns status of the circular reference collector */
  82. ZEND_FUNCTION(gc_enabled)
  83. {
  84. ZEND_PARSE_PARAMETERS_NONE();
  85. RETURN_BOOL(gc_enabled());
  86. }
  87. /* }}} */
  88. /* {{{ Activates the circular reference collector */
  89. ZEND_FUNCTION(gc_enable)
  90. {
  91. zend_string *key;
  92. ZEND_PARSE_PARAMETERS_NONE();
  93. key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
  94. zend_alter_ini_entry_chars(key, "1", sizeof("1")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
  95. zend_string_release_ex(key, 0);
  96. }
  97. /* }}} */
  98. /* {{{ Deactivates the circular reference collector */
  99. ZEND_FUNCTION(gc_disable)
  100. {
  101. zend_string *key;
  102. ZEND_PARSE_PARAMETERS_NONE();
  103. key = zend_string_init("zend.enable_gc", sizeof("zend.enable_gc")-1, 0);
  104. zend_alter_ini_entry_chars(key, "0", sizeof("0")-1, ZEND_INI_USER, ZEND_INI_STAGE_RUNTIME);
  105. zend_string_release_ex(key, 0);
  106. }
  107. /* }}} */
  108. /* {{{ Returns current GC statistics */
  109. ZEND_FUNCTION(gc_status)
  110. {
  111. zend_gc_status status;
  112. ZEND_PARSE_PARAMETERS_NONE();
  113. zend_gc_get_status(&status);
  114. array_init_size(return_value, 3);
  115. add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
  116. add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);
  117. add_assoc_long_ex(return_value, "threshold", sizeof("threshold")-1, (long)status.threshold);
  118. add_assoc_long_ex(return_value, "roots", sizeof("roots")-1, (long)status.num_roots);
  119. }
  120. /* }}} */
  121. /* {{{ Get the number of arguments that were passed to the function */
  122. ZEND_FUNCTION(func_num_args)
  123. {
  124. zend_execute_data *ex = EX(prev_execute_data);
  125. ZEND_PARSE_PARAMETERS_NONE();
  126. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  127. zend_throw_error(NULL, "func_num_args() must be called from a function context");
  128. RETURN_THROWS();
  129. }
  130. if (zend_forbid_dynamic_call("func_num_args()") == FAILURE) {
  131. RETURN_LONG(-1);
  132. }
  133. RETURN_LONG(ZEND_CALL_NUM_ARGS(ex));
  134. }
  135. /* }}} */
  136. /* {{{ Get the $arg_num'th argument that was passed to the function */
  137. ZEND_FUNCTION(func_get_arg)
  138. {
  139. uint32_t arg_count, first_extra_arg;
  140. zval *arg;
  141. zend_long requested_offset;
  142. zend_execute_data *ex;
  143. if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) == FAILURE) {
  144. RETURN_THROWS();
  145. }
  146. if (requested_offset < 0) {
  147. zend_argument_value_error(1, "must be greater than or equal to 0");
  148. RETURN_THROWS();
  149. }
  150. ex = EX(prev_execute_data);
  151. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  152. zend_throw_error(NULL, "func_get_arg() cannot be called from the global scope");
  153. RETURN_THROWS();
  154. }
  155. if (zend_forbid_dynamic_call("func_get_arg()") == FAILURE) {
  156. RETURN_THROWS();
  157. }
  158. arg_count = ZEND_CALL_NUM_ARGS(ex);
  159. if ((zend_ulong)requested_offset >= arg_count) {
  160. zend_argument_value_error(1, "must be less than the number of the arguments passed to the currently executed function");
  161. RETURN_THROWS();
  162. }
  163. first_extra_arg = ex->func->op_array.num_args;
  164. if ((zend_ulong)requested_offset >= first_extra_arg && (ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
  165. arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T) + (requested_offset - first_extra_arg);
  166. } else {
  167. arg = ZEND_CALL_ARG(ex, requested_offset + 1);
  168. }
  169. if (EXPECTED(!Z_ISUNDEF_P(arg))) {
  170. RETURN_COPY_DEREF(arg);
  171. }
  172. }
  173. /* }}} */
  174. /* {{{ Get an array of the arguments that were passed to the function */
  175. ZEND_FUNCTION(func_get_args)
  176. {
  177. zval *p, *q;
  178. uint32_t arg_count, first_extra_arg;
  179. uint32_t i;
  180. zend_execute_data *ex = EX(prev_execute_data);
  181. ZEND_PARSE_PARAMETERS_NONE();
  182. if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
  183. zend_throw_error(NULL, "func_get_args() cannot be called from the global scope");
  184. RETURN_THROWS();
  185. }
  186. if (zend_forbid_dynamic_call("func_get_args()") == FAILURE) {
  187. RETURN_THROWS();
  188. }
  189. arg_count = ZEND_CALL_NUM_ARGS(ex);
  190. if (arg_count) {
  191. array_init_size(return_value, arg_count);
  192. first_extra_arg = ex->func->op_array.num_args;
  193. zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
  194. ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
  195. i = 0;
  196. p = ZEND_CALL_ARG(ex, 1);
  197. if (arg_count > first_extra_arg) {
  198. while (i < first_extra_arg) {
  199. q = p;
  200. if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
  201. ZVAL_DEREF(q);
  202. if (Z_OPT_REFCOUNTED_P(q)) {
  203. Z_ADDREF_P(q);
  204. }
  205. ZEND_HASH_FILL_SET(q);
  206. } else {
  207. ZEND_HASH_FILL_SET_NULL();
  208. }
  209. ZEND_HASH_FILL_NEXT();
  210. p++;
  211. i++;
  212. }
  213. p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var + ex->func->op_array.T);
  214. }
  215. while (i < arg_count) {
  216. q = p;
  217. if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
  218. ZVAL_DEREF(q);
  219. if (Z_OPT_REFCOUNTED_P(q)) {
  220. Z_ADDREF_P(q);
  221. }
  222. ZEND_HASH_FILL_SET(q);
  223. } else {
  224. ZEND_HASH_FILL_SET_NULL();
  225. }
  226. ZEND_HASH_FILL_NEXT();
  227. p++;
  228. i++;
  229. }
  230. } ZEND_HASH_FILL_END();
  231. Z_ARRVAL_P(return_value)->nNumOfElements = arg_count;
  232. } else {
  233. RETURN_EMPTY_ARRAY();
  234. }
  235. }
  236. /* }}} */
  237. /* {{{ Get string length
  238. Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
  239. ZEND_FUNCTION(strlen)
  240. {
  241. zend_string *s;
  242. ZEND_PARSE_PARAMETERS_START(1, 1)
  243. Z_PARAM_STR(s)
  244. ZEND_PARSE_PARAMETERS_END();
  245. RETVAL_LONG(ZSTR_LEN(s));
  246. }
  247. /* }}} */
  248. /* {{{ Binary safe string comparison */
  249. ZEND_FUNCTION(strcmp)
  250. {
  251. zend_string *s1, *s2;
  252. ZEND_PARSE_PARAMETERS_START(2, 2)
  253. Z_PARAM_STR(s1)
  254. Z_PARAM_STR(s2)
  255. ZEND_PARSE_PARAMETERS_END();
  256. RETURN_LONG(zend_binary_strcmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
  257. }
  258. /* }}} */
  259. /* {{{ Binary safe string comparison */
  260. ZEND_FUNCTION(strncmp)
  261. {
  262. zend_string *s1, *s2;
  263. zend_long len;
  264. ZEND_PARSE_PARAMETERS_START(3, 3)
  265. Z_PARAM_STR(s1)
  266. Z_PARAM_STR(s2)
  267. Z_PARAM_LONG(len)
  268. ZEND_PARSE_PARAMETERS_END();
  269. if (len < 0) {
  270. zend_argument_value_error(3, "must be greater than or equal to 0");
  271. RETURN_THROWS();
  272. }
  273. RETURN_LONG(zend_binary_strncmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
  274. }
  275. /* }}} */
  276. /* {{{ Binary safe case-insensitive string comparison */
  277. ZEND_FUNCTION(strcasecmp)
  278. {
  279. zend_string *s1, *s2;
  280. ZEND_PARSE_PARAMETERS_START(2, 2)
  281. Z_PARAM_STR(s1)
  282. Z_PARAM_STR(s2)
  283. ZEND_PARSE_PARAMETERS_END();
  284. RETURN_LONG(zend_binary_strcasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2)));
  285. }
  286. /* }}} */
  287. /* {{{ Binary safe string comparison */
  288. ZEND_FUNCTION(strncasecmp)
  289. {
  290. zend_string *s1, *s2;
  291. zend_long len;
  292. ZEND_PARSE_PARAMETERS_START(3, 3)
  293. Z_PARAM_STR(s1)
  294. Z_PARAM_STR(s2)
  295. Z_PARAM_LONG(len)
  296. ZEND_PARSE_PARAMETERS_END();
  297. if (len < 0) {
  298. zend_argument_value_error(3, "must be greater than or equal to 0");
  299. RETURN_THROWS();
  300. }
  301. RETURN_LONG(zend_binary_strncasecmp(ZSTR_VAL(s1), ZSTR_LEN(s1), ZSTR_VAL(s2), ZSTR_LEN(s2), len));
  302. }
  303. /* }}} */
  304. /* {{{ Return the current error_reporting level, and if an argument was passed - change to the new level */
  305. ZEND_FUNCTION(error_reporting)
  306. {
  307. zend_long err;
  308. bool err_is_null = 1;
  309. int old_error_reporting;
  310. ZEND_PARSE_PARAMETERS_START(0, 1)
  311. Z_PARAM_OPTIONAL
  312. Z_PARAM_LONG_OR_NULL(err, err_is_null)
  313. ZEND_PARSE_PARAMETERS_END();
  314. old_error_reporting = EG(error_reporting);
  315. if (!err_is_null && err != old_error_reporting) {
  316. zend_ini_entry *p = EG(error_reporting_ini_entry);
  317. if (!p) {
  318. zval *zv = zend_hash_find_known_hash(EG(ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING));
  319. if (!zv) {
  320. /* Ini setting does not exist -- can this happen? */
  321. RETURN_LONG(old_error_reporting);
  322. }
  323. p = EG(error_reporting_ini_entry) = (zend_ini_entry*)Z_PTR_P(zv);
  324. }
  325. if (!p->modified) {
  326. if (!EG(modified_ini_directives)) {
  327. ALLOC_HASHTABLE(EG(modified_ini_directives));
  328. zend_hash_init(EG(modified_ini_directives), 8, NULL, NULL, 0);
  329. }
  330. if (EXPECTED(zend_hash_add_ptr(EG(modified_ini_directives), ZSTR_KNOWN(ZEND_STR_ERROR_REPORTING), p) != NULL)) {
  331. p->orig_value = p->value;
  332. p->orig_modifiable = p->modifiable;
  333. p->modified = 1;
  334. }
  335. } else if (p->orig_value != p->value) {
  336. zend_string_release_ex(p->value, 0);
  337. }
  338. p->value = zend_long_to_str(err);
  339. EG(error_reporting) = err;
  340. }
  341. RETURN_LONG(old_error_reporting);
  342. }
  343. /* }}} */
  344. static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
  345. {
  346. bool ret = 1;
  347. zval *val;
  348. GC_PROTECT_RECURSION(ht);
  349. ZEND_HASH_FOREACH_VAL(ht, val) {
  350. ZVAL_DEREF(val);
  351. if (Z_TYPE_P(val) == IS_ARRAY && Z_REFCOUNTED_P(val)) {
  352. if (Z_IS_RECURSIVE_P(val)) {
  353. zend_argument_value_error(argument_number, "cannot be a recursive array");
  354. ret = 0;
  355. break;
  356. } else if (!validate_constant_array_argument(Z_ARRVAL_P(val), argument_number)) {
  357. ret = 0;
  358. break;
  359. }
  360. }
  361. } ZEND_HASH_FOREACH_END();
  362. GC_UNPROTECT_RECURSION(ht);
  363. return ret;
  364. }
  365. /* }}} */
  366. static void copy_constant_array(zval *dst, zval *src) /* {{{ */
  367. {
  368. zend_string *key;
  369. zend_ulong idx;
  370. zval *new_val, *val;
  371. array_init_size(dst, zend_hash_num_elements(Z_ARRVAL_P(src)));
  372. ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(src), idx, key, val) {
  373. /* constant arrays can't contain references */
  374. ZVAL_DEREF(val);
  375. if (key) {
  376. new_val = zend_hash_add_new(Z_ARRVAL_P(dst), key, val);
  377. } else {
  378. new_val = zend_hash_index_add_new(Z_ARRVAL_P(dst), idx, val);
  379. }
  380. if (Z_TYPE_P(val) == IS_ARRAY) {
  381. if (Z_REFCOUNTED_P(val)) {
  382. copy_constant_array(new_val, val);
  383. }
  384. } else {
  385. Z_TRY_ADDREF_P(val);
  386. }
  387. } ZEND_HASH_FOREACH_END();
  388. }
  389. /* }}} */
  390. /* {{{ Define a new constant */
  391. ZEND_FUNCTION(define)
  392. {
  393. zend_string *name;
  394. zval *val, val_free;
  395. bool non_cs = 0;
  396. zend_constant c;
  397. ZEND_PARSE_PARAMETERS_START(2, 3)
  398. Z_PARAM_STR(name)
  399. Z_PARAM_ZVAL(val)
  400. Z_PARAM_OPTIONAL
  401. Z_PARAM_BOOL(non_cs)
  402. ZEND_PARSE_PARAMETERS_END();
  403. if (zend_memnstr(ZSTR_VAL(name), "::", sizeof("::") - 1, ZSTR_VAL(name) + ZSTR_LEN(name))) {
  404. zend_argument_value_error(1, "cannot be a class constant");
  405. RETURN_THROWS();
  406. }
  407. if (non_cs) {
  408. zend_error(E_WARNING, "define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported");
  409. }
  410. ZVAL_UNDEF(&val_free);
  411. if (Z_TYPE_P(val) == IS_ARRAY) {
  412. if (Z_REFCOUNTED_P(val)) {
  413. if (!validate_constant_array_argument(Z_ARRVAL_P(val), 2)) {
  414. RETURN_THROWS();
  415. } else {
  416. copy_constant_array(&c.value, val);
  417. goto register_constant;
  418. }
  419. }
  420. }
  421. ZVAL_COPY(&c.value, val);
  422. zval_ptr_dtor(&val_free);
  423. register_constant:
  424. /* non persistent */
  425. ZEND_CONSTANT_SET_FLAGS(&c, CONST_CS, PHP_USER_CONSTANT);
  426. c.name = zend_string_copy(name);
  427. if (zend_register_constant(&c) == SUCCESS) {
  428. RETURN_TRUE;
  429. } else {
  430. RETURN_FALSE;
  431. }
  432. }
  433. /* }}} */
  434. /* {{{ Check whether a constant exists
  435. Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
  436. ZEND_FUNCTION(defined)
  437. {
  438. zend_string *name;
  439. ZEND_PARSE_PARAMETERS_START(1, 1)
  440. Z_PARAM_STR(name)
  441. ZEND_PARSE_PARAMETERS_END();
  442. if (zend_get_constant_ex(name, zend_get_executed_scope(), ZEND_FETCH_CLASS_SILENT)) {
  443. RETURN_TRUE;
  444. } else {
  445. RETURN_FALSE;
  446. }
  447. }
  448. /* }}} */
  449. /* {{{ Retrieves the class name */
  450. ZEND_FUNCTION(get_class)
  451. {
  452. zval *obj = NULL;
  453. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|o", &obj) == FAILURE) {
  454. RETURN_THROWS();
  455. }
  456. if (!obj) {
  457. zend_class_entry *scope = zend_get_executed_scope();
  458. if (scope) {
  459. RETURN_STR_COPY(scope->name);
  460. } else {
  461. zend_throw_error(NULL, "get_class() without arguments must be called from within a class");
  462. RETURN_THROWS();
  463. }
  464. }
  465. RETURN_STR_COPY(Z_OBJCE_P(obj)->name);
  466. }
  467. /* }}} */
  468. /* {{{ Retrieves the "Late Static Binding" class name */
  469. ZEND_FUNCTION(get_called_class)
  470. {
  471. zend_class_entry *called_scope;
  472. ZEND_PARSE_PARAMETERS_NONE();
  473. called_scope = zend_get_called_scope(execute_data);
  474. if (!called_scope) {
  475. zend_throw_error(NULL, "get_called_class() must be called from within a class");
  476. RETURN_THROWS();
  477. }
  478. RETURN_STR_COPY(called_scope->name);
  479. }
  480. /* }}} */
  481. /* {{{ Retrieves the parent class name for object or class or current scope or false if not in a scope. */
  482. ZEND_FUNCTION(get_parent_class)
  483. {
  484. zend_class_entry *ce = NULL;
  485. ZEND_PARSE_PARAMETERS_START(0, 1)
  486. Z_PARAM_OPTIONAL
  487. Z_PARAM_OBJ_OR_CLASS_NAME(ce)
  488. ZEND_PARSE_PARAMETERS_END();
  489. if (!ce) {
  490. ce = zend_get_executed_scope();
  491. }
  492. if (ce && ce->parent) {
  493. RETURN_STR_COPY(ce->parent->name);
  494. } else {
  495. RETURN_FALSE;
  496. }
  497. }
  498. /* }}} */
  499. static void is_a_impl(INTERNAL_FUNCTION_PARAMETERS, bool only_subclass) /* {{{ */
  500. {
  501. zval *obj;
  502. zend_string *class_name;
  503. zend_class_entry *instance_ce;
  504. zend_class_entry *ce;
  505. bool allow_string = only_subclass;
  506. bool retval;
  507. ZEND_PARSE_PARAMETERS_START(2, 3)
  508. Z_PARAM_ZVAL(obj)
  509. Z_PARAM_STR(class_name)
  510. Z_PARAM_OPTIONAL
  511. Z_PARAM_BOOL(allow_string)
  512. ZEND_PARSE_PARAMETERS_END();
  513. /*
  514. * allow_string - is_a default is no, is_subclass_of is yes.
  515. * if it's allowed, then the autoloader will be called if the class does not exist.
  516. * default behaviour is different, as 'is_a' used to be used to test mixed return values
  517. * and there is no easy way to deprecate this.
  518. */
  519. if (allow_string && Z_TYPE_P(obj) == IS_STRING) {
  520. instance_ce = zend_lookup_class(Z_STR_P(obj));
  521. if (!instance_ce) {
  522. RETURN_FALSE;
  523. }
  524. } else if (Z_TYPE_P(obj) == IS_OBJECT) {
  525. instance_ce = Z_OBJCE_P(obj);
  526. } else {
  527. RETURN_FALSE;
  528. }
  529. if (!only_subclass && EXPECTED(zend_string_equals(instance_ce->name, class_name))) {
  530. retval = 1;
  531. } else {
  532. ce = zend_lookup_class_ex(class_name, NULL, ZEND_FETCH_CLASS_NO_AUTOLOAD);
  533. if (!ce) {
  534. retval = 0;
  535. } else {
  536. if (only_subclass && instance_ce == ce) {
  537. retval = 0;
  538. } else {
  539. retval = instanceof_function(instance_ce, ce);
  540. }
  541. }
  542. }
  543. RETURN_BOOL(retval);
  544. }
  545. /* }}} */
  546. /* {{{ Returns true if the object has this class as one of its parents */
  547. ZEND_FUNCTION(is_subclass_of)
  548. {
  549. is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  550. }
  551. /* }}} */
  552. /* {{{ Returns true if the first argument is an object and is this class or has this class as one of its parents, */
  553. ZEND_FUNCTION(is_a)
  554. {
  555. is_a_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  556. }
  557. /* }}} */
  558. /* {{{ add_class_vars */
  559. static void add_class_vars(zend_class_entry *scope, zend_class_entry *ce, bool statics, zval *return_value)
  560. {
  561. zend_property_info *prop_info;
  562. zval *prop, prop_copy;
  563. zend_string *key;
  564. zval *default_properties_table = CE_DEFAULT_PROPERTIES_TABLE(ce);
  565. ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->properties_info, key, prop_info) {
  566. if (((prop_info->flags & ZEND_ACC_PROTECTED) &&
  567. !zend_check_protected(prop_info->ce, scope)) ||
  568. ((prop_info->flags & ZEND_ACC_PRIVATE) &&
  569. prop_info->ce != scope)) {
  570. continue;
  571. }
  572. prop = NULL;
  573. if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
  574. prop = &ce->default_static_members_table[prop_info->offset];
  575. ZVAL_DEINDIRECT(prop);
  576. } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
  577. prop = &default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
  578. }
  579. if (!prop) {
  580. continue;
  581. }
  582. if (Z_ISUNDEF_P(prop)) {
  583. /* Return uninitialized typed properties as a null value */
  584. ZVAL_NULL(&prop_copy);
  585. } else {
  586. /* copy: enforce read only access */
  587. ZVAL_COPY_OR_DUP(&prop_copy, prop);
  588. }
  589. prop = &prop_copy;
  590. /* this is necessary to make it able to work with default array
  591. * properties, returned to user */
  592. if (Z_OPT_TYPE_P(prop) == IS_CONSTANT_AST) {
  593. if (UNEXPECTED(zval_update_constant_ex(prop, ce) != SUCCESS)) {
  594. return;
  595. }
  596. }
  597. zend_hash_add_new(Z_ARRVAL_P(return_value), key, prop);
  598. } ZEND_HASH_FOREACH_END();
  599. }
  600. /* }}} */
  601. /* {{{ Returns an array of default properties of the class. */
  602. ZEND_FUNCTION(get_class_vars)
  603. {
  604. zend_class_entry *ce = NULL, *scope;
  605. if (zend_parse_parameters(ZEND_NUM_ARGS(), "C", &ce) == FAILURE) {
  606. RETURN_THROWS();
  607. }
  608. array_init(return_value);
  609. if (UNEXPECTED(!(ce->ce_flags & ZEND_ACC_CONSTANTS_UPDATED))) {
  610. if (UNEXPECTED(zend_update_class_constants(ce) != SUCCESS)) {
  611. return;
  612. }
  613. }
  614. scope = zend_get_executed_scope();
  615. add_class_vars(scope, ce, 0, return_value);
  616. add_class_vars(scope, ce, 1, return_value);
  617. }
  618. /* }}} */
  619. /* {{{ Returns an array of object properties */
  620. ZEND_FUNCTION(get_object_vars)
  621. {
  622. zval *value;
  623. HashTable *properties;
  624. zend_string *key;
  625. zend_object *zobj;
  626. zend_ulong num_key;
  627. ZEND_PARSE_PARAMETERS_START(1, 1)
  628. Z_PARAM_OBJ(zobj)
  629. ZEND_PARSE_PARAMETERS_END();
  630. properties = zobj->handlers->get_properties(zobj);
  631. if (properties == NULL) {
  632. RETURN_EMPTY_ARRAY();
  633. }
  634. if (!zobj->ce->default_properties_count && properties == zobj->properties && !GC_IS_RECURSIVE(properties)) {
  635. /* fast copy */
  636. if (EXPECTED(zobj->handlers == &std_object_handlers)) {
  637. RETURN_ARR(zend_proptable_to_symtable(properties, 0));
  638. }
  639. RETURN_ARR(zend_proptable_to_symtable(properties, 1));
  640. } else {
  641. array_init_size(return_value, zend_hash_num_elements(properties));
  642. ZEND_HASH_FOREACH_KEY_VAL(properties, num_key, key, value) {
  643. bool is_dynamic = 1;
  644. if (Z_TYPE_P(value) == IS_INDIRECT) {
  645. value = Z_INDIRECT_P(value);
  646. if (UNEXPECTED(Z_ISUNDEF_P(value))) {
  647. continue;
  648. }
  649. is_dynamic = 0;
  650. }
  651. if (key && zend_check_property_access(zobj, key, is_dynamic) == FAILURE) {
  652. continue;
  653. }
  654. if (Z_ISREF_P(value) && Z_REFCOUNT_P(value) == 1) {
  655. value = Z_REFVAL_P(value);
  656. }
  657. Z_TRY_ADDREF_P(value);
  658. if (UNEXPECTED(!key)) {
  659. /* This case is only possible due to loopholes, e.g. ArrayObject */
  660. zend_hash_index_add(Z_ARRVAL_P(return_value), num_key, value);
  661. } else if (!is_dynamic && ZSTR_VAL(key)[0] == 0) {
  662. const char *prop_name, *class_name;
  663. size_t prop_len;
  664. zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
  665. /* We assume here that a mangled property name is never
  666. * numeric. This is probably a safe assumption, but
  667. * theoretically someone might write an extension with
  668. * private, numeric properties. Well, too bad.
  669. */
  670. zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
  671. } else {
  672. zend_symtable_add_new(Z_ARRVAL_P(return_value), key, value);
  673. }
  674. } ZEND_HASH_FOREACH_END();
  675. }
  676. }
  677. /* }}} */
  678. /* {{{ Returns an array of mangled object properties. Does not respect property visibility. */
  679. ZEND_FUNCTION(get_mangled_object_vars)
  680. {
  681. zend_object *obj;
  682. HashTable *properties;
  683. ZEND_PARSE_PARAMETERS_START(1, 1)
  684. Z_PARAM_OBJ(obj)
  685. ZEND_PARSE_PARAMETERS_END();
  686. properties = obj->handlers->get_properties(obj);
  687. if (!properties) {
  688. ZVAL_EMPTY_ARRAY(return_value);
  689. return;
  690. }
  691. properties = zend_proptable_to_symtable(properties,
  692. (obj->ce->default_properties_count ||
  693. obj->handlers != &std_object_handlers ||
  694. GC_IS_RECURSIVE(properties)));
  695. RETURN_ARR(properties);
  696. }
  697. /* }}} */
  698. /* {{{ Returns an array of method names for class or class instance. */
  699. ZEND_FUNCTION(get_class_methods)
  700. {
  701. zval method_name;
  702. zend_class_entry *ce = NULL;
  703. zend_class_entry *scope;
  704. zend_function *mptr;
  705. ZEND_PARSE_PARAMETERS_START(1, 1)
  706. Z_PARAM_OBJ_OR_CLASS_NAME(ce)
  707. ZEND_PARSE_PARAMETERS_END();
  708. array_init(return_value);
  709. scope = zend_get_executed_scope();
  710. ZEND_HASH_FOREACH_PTR(&ce->function_table, mptr) {
  711. if ((mptr->common.fn_flags & ZEND_ACC_PUBLIC)
  712. || (scope &&
  713. (((mptr->common.fn_flags & ZEND_ACC_PROTECTED) &&
  714. zend_check_protected(mptr->common.scope, scope))
  715. || ((mptr->common.fn_flags & ZEND_ACC_PRIVATE) &&
  716. scope == mptr->common.scope)))
  717. ) {
  718. ZVAL_STR_COPY(&method_name, mptr->common.function_name);
  719. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &method_name);
  720. }
  721. } ZEND_HASH_FOREACH_END();
  722. }
  723. /* }}} */
  724. /* {{{ Checks if the class method exists */
  725. ZEND_FUNCTION(method_exists)
  726. {
  727. zval *klass;
  728. zend_string *method_name;
  729. zend_string *lcname;
  730. zend_class_entry *ce;
  731. zend_function *func;
  732. ZEND_PARSE_PARAMETERS_START(2, 2)
  733. Z_PARAM_ZVAL(klass)
  734. Z_PARAM_STR(method_name)
  735. ZEND_PARSE_PARAMETERS_END();
  736. if (Z_TYPE_P(klass) == IS_OBJECT) {
  737. ce = Z_OBJCE_P(klass);
  738. } else if (Z_TYPE_P(klass) == IS_STRING) {
  739. if ((ce = zend_lookup_class(Z_STR_P(klass))) == NULL) {
  740. RETURN_FALSE;
  741. }
  742. } else {
  743. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(klass));
  744. RETURN_THROWS();
  745. }
  746. lcname = zend_string_tolower(method_name);
  747. func = zend_hash_find_ptr(&ce->function_table, lcname);
  748. zend_string_release_ex(lcname, 0);
  749. if (func) {
  750. /* Exclude shadow properties when checking a method on a specific class. Include
  751. * them when checking an object, as method_exists() generally ignores visibility.
  752. * TODO: Should we use EG(scope) for the object case instead? */
  753. RETURN_BOOL(Z_TYPE_P(klass) == IS_OBJECT
  754. || !(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
  755. }
  756. if (Z_TYPE_P(klass) == IS_OBJECT) {
  757. zend_object *obj = Z_OBJ_P(klass);
  758. func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
  759. if (func != NULL) {
  760. if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
  761. /* Returns true for the fake Closure's __invoke */
  762. RETVAL_BOOL(func->common.scope == zend_ce_closure
  763. && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME));
  764. zend_string_release_ex(func->common.function_name, 0);
  765. zend_free_trampoline(func);
  766. return;
  767. }
  768. RETURN_TRUE;
  769. }
  770. } else {
  771. /* Returns true for fake Closure::__invoke */
  772. if (ce == zend_ce_closure
  773. && zend_string_equals_literal_ci(method_name, ZEND_INVOKE_FUNC_NAME)) {
  774. RETURN_TRUE;
  775. }
  776. }
  777. RETURN_FALSE;
  778. }
  779. /* }}} */
  780. /* {{{ Checks if the object or class has a property */
  781. ZEND_FUNCTION(property_exists)
  782. {
  783. zval *object;
  784. zend_string *property;
  785. zend_class_entry *ce;
  786. zend_property_info *property_info;
  787. if (zend_parse_parameters(ZEND_NUM_ARGS(), "zS", &object, &property) == FAILURE) {
  788. RETURN_THROWS();
  789. }
  790. if (Z_TYPE_P(object) == IS_STRING) {
  791. ce = zend_lookup_class(Z_STR_P(object));
  792. if (!ce) {
  793. RETURN_FALSE;
  794. }
  795. } else if (Z_TYPE_P(object) == IS_OBJECT) {
  796. ce = Z_OBJCE_P(object);
  797. } else {
  798. zend_argument_type_error(1, "must be of type object|string, %s given", zend_zval_type_name(object));
  799. RETURN_THROWS();
  800. }
  801. property_info = zend_hash_find_ptr(&ce->properties_info, property);
  802. if (property_info != NULL
  803. && (!(property_info->flags & ZEND_ACC_PRIVATE)
  804. || property_info->ce == ce)) {
  805. RETURN_TRUE;
  806. }
  807. if (Z_TYPE_P(object) == IS_OBJECT &&
  808. Z_OBJ_HANDLER_P(object, has_property)(Z_OBJ_P(object), property, 2, NULL)) {
  809. RETURN_TRUE;
  810. }
  811. RETURN_FALSE;
  812. }
  813. /* }}} */
  814. static inline void class_exists_impl(INTERNAL_FUNCTION_PARAMETERS, int flags, int skip_flags) /* {{{ */
  815. {
  816. zend_string *name;
  817. zend_string *lcname;
  818. zend_class_entry *ce;
  819. bool autoload = 1;
  820. ZEND_PARSE_PARAMETERS_START(1, 2)
  821. Z_PARAM_STR(name)
  822. Z_PARAM_OPTIONAL
  823. Z_PARAM_BOOL(autoload)
  824. ZEND_PARSE_PARAMETERS_END();
  825. if (ZSTR_HAS_CE_CACHE(name)) {
  826. ce = ZSTR_GET_CE_CACHE(name);
  827. if (ce) {
  828. RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
  829. }
  830. }
  831. if (!autoload) {
  832. if (ZSTR_VAL(name)[0] == '\\') {
  833. /* Ignore leading "\" */
  834. lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
  835. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  836. } else {
  837. lcname = zend_string_tolower(name);
  838. }
  839. ce = zend_hash_find_ptr(EG(class_table), lcname);
  840. zend_string_release_ex(lcname, 0);
  841. } else {
  842. ce = zend_lookup_class(name);
  843. }
  844. if (ce) {
  845. RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
  846. } else {
  847. RETURN_FALSE;
  848. }
  849. }
  850. /* {{{ */
  851. /* {{{ Checks if the class exists */
  852. ZEND_FUNCTION(class_exists)
  853. {
  854. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED, ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT);
  855. }
  856. /* }}} */
  857. /* {{{ Checks if the class exists */
  858. ZEND_FUNCTION(interface_exists)
  859. {
  860. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED|ZEND_ACC_INTERFACE, 0);
  861. }
  862. /* }}} */
  863. /* {{{ Checks if the trait exists */
  864. ZEND_FUNCTION(trait_exists)
  865. {
  866. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT, 0);
  867. }
  868. /* }}} */
  869. ZEND_FUNCTION(enum_exists)
  870. {
  871. class_exists_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ENUM, 0);
  872. }
  873. /* {{{ Checks if the function exists */
  874. ZEND_FUNCTION(function_exists)
  875. {
  876. zend_string *name;
  877. bool exists;
  878. zend_string *lcname;
  879. ZEND_PARSE_PARAMETERS_START(1, 1)
  880. Z_PARAM_STR(name)
  881. ZEND_PARSE_PARAMETERS_END();
  882. if (ZSTR_VAL(name)[0] == '\\') {
  883. /* Ignore leading "\" */
  884. lcname = zend_string_alloc(ZSTR_LEN(name) - 1, 0);
  885. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(name) + 1, ZSTR_LEN(name) - 1);
  886. } else {
  887. lcname = zend_string_tolower(name);
  888. }
  889. exists = zend_hash_exists(EG(function_table), lcname);
  890. zend_string_release_ex(lcname, 0);
  891. RETURN_BOOL(exists);
  892. }
  893. /* }}} */
  894. /* {{{ Creates an alias for user defined class */
  895. ZEND_FUNCTION(class_alias)
  896. {
  897. zend_string *class_name;
  898. zend_string *alias_name;
  899. zend_class_entry *ce;
  900. bool autoload = 1;
  901. ZEND_PARSE_PARAMETERS_START(2, 3)
  902. Z_PARAM_STR(class_name)
  903. Z_PARAM_STR(alias_name)
  904. Z_PARAM_OPTIONAL
  905. Z_PARAM_BOOL(autoload)
  906. ZEND_PARSE_PARAMETERS_END();
  907. ce = zend_lookup_class_ex(class_name, NULL, !autoload ? ZEND_FETCH_CLASS_NO_AUTOLOAD : 0);
  908. if (ce) {
  909. if (ce->type == ZEND_USER_CLASS) {
  910. if (zend_register_class_alias_ex(ZSTR_VAL(alias_name), ZSTR_LEN(alias_name), ce, 0) == SUCCESS) {
  911. RETURN_TRUE;
  912. } else {
  913. zend_error(E_WARNING, "Cannot declare %s %s, because the name is already in use", zend_get_object_type(ce), ZSTR_VAL(alias_name));
  914. RETURN_FALSE;
  915. }
  916. } else {
  917. zend_argument_value_error(1, "must be a user-defined class name, internal class name given");
  918. RETURN_THROWS();
  919. }
  920. } else {
  921. zend_error(E_WARNING, "Class \"%s\" not found", ZSTR_VAL(class_name));
  922. RETURN_FALSE;
  923. }
  924. }
  925. /* }}} */
  926. /* {{{ Returns an array with the file names that were include_once()'d */
  927. ZEND_FUNCTION(get_included_files)
  928. {
  929. zend_string *entry;
  930. ZEND_PARSE_PARAMETERS_NONE();
  931. array_init(return_value);
  932. ZEND_HASH_FOREACH_STR_KEY(&EG(included_files), entry) {
  933. if (entry) {
  934. add_next_index_str(return_value, zend_string_copy(entry));
  935. }
  936. } ZEND_HASH_FOREACH_END();
  937. }
  938. /* }}} */
  939. /* {{{ Generates a user-level error/warning/notice message */
  940. ZEND_FUNCTION(trigger_error)
  941. {
  942. zend_long error_type = E_USER_NOTICE;
  943. char *message;
  944. size_t message_len;
  945. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &message, &message_len, &error_type) == FAILURE) {
  946. RETURN_THROWS();
  947. }
  948. switch (error_type) {
  949. case E_USER_ERROR:
  950. case E_USER_WARNING:
  951. case E_USER_NOTICE:
  952. case E_USER_DEPRECATED:
  953. break;
  954. default:
  955. zend_argument_value_error(2, "must be one of E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE,"
  956. " or E_USER_DEPRECATED");
  957. RETURN_THROWS();
  958. break;
  959. }
  960. zend_error((int)error_type, "%s", message);
  961. // TODO Change to void
  962. RETURN_TRUE;
  963. }
  964. /* }}} */
  965. /* {{{ Sets a user-defined error handler function. Returns the previously defined error handler, or false on error */
  966. ZEND_FUNCTION(set_error_handler)
  967. {
  968. zend_fcall_info fci;
  969. zend_fcall_info_cache fcc;
  970. zend_long error_type = E_ALL;
  971. ZEND_PARSE_PARAMETERS_START(1, 2)
  972. Z_PARAM_FUNC_OR_NULL(fci, fcc)
  973. Z_PARAM_OPTIONAL
  974. Z_PARAM_LONG(error_type)
  975. ZEND_PARSE_PARAMETERS_END();
  976. if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  977. ZVAL_COPY(return_value, &EG(user_error_handler));
  978. }
  979. zend_stack_push(&EG(user_error_handlers_error_reporting), &EG(user_error_handler_error_reporting));
  980. zend_stack_push(&EG(user_error_handlers), &EG(user_error_handler));
  981. if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
  982. ZVAL_UNDEF(&EG(user_error_handler));
  983. return;
  984. }
  985. ZVAL_COPY(&EG(user_error_handler), &(fci.function_name));
  986. EG(user_error_handler_error_reporting) = (int)error_type;
  987. }
  988. /* }}} */
  989. /* {{{ Restores the previously defined error handler function */
  990. ZEND_FUNCTION(restore_error_handler)
  991. {
  992. ZEND_PARSE_PARAMETERS_NONE();
  993. if (Z_TYPE(EG(user_error_handler)) != IS_UNDEF) {
  994. zval zeh;
  995. ZVAL_COPY_VALUE(&zeh, &EG(user_error_handler));
  996. ZVAL_UNDEF(&EG(user_error_handler));
  997. zval_ptr_dtor(&zeh);
  998. }
  999. if (zend_stack_is_empty(&EG(user_error_handlers))) {
  1000. ZVAL_UNDEF(&EG(user_error_handler));
  1001. } else {
  1002. zval *tmp;
  1003. EG(user_error_handler_error_reporting) = zend_stack_int_top(&EG(user_error_handlers_error_reporting));
  1004. zend_stack_del_top(&EG(user_error_handlers_error_reporting));
  1005. tmp = zend_stack_top(&EG(user_error_handlers));
  1006. ZVAL_COPY_VALUE(&EG(user_error_handler), tmp);
  1007. zend_stack_del_top(&EG(user_error_handlers));
  1008. }
  1009. // TODO Change to void
  1010. RETURN_TRUE;
  1011. }
  1012. /* }}} */
  1013. /* {{{ Sets a user-defined exception handler function. Returns the previously defined exception handler, or false on error */
  1014. ZEND_FUNCTION(set_exception_handler)
  1015. {
  1016. zend_fcall_info fci;
  1017. zend_fcall_info_cache fcc;
  1018. ZEND_PARSE_PARAMETERS_START(1, 1)
  1019. Z_PARAM_FUNC_OR_NULL(fci, fcc)
  1020. ZEND_PARSE_PARAMETERS_END();
  1021. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  1022. ZVAL_COPY(return_value, &EG(user_exception_handler));
  1023. }
  1024. zend_stack_push(&EG(user_exception_handlers), &EG(user_exception_handler));
  1025. if (!ZEND_FCI_INITIALIZED(fci)) { /* unset user-defined handler */
  1026. ZVAL_UNDEF(&EG(user_exception_handler));
  1027. return;
  1028. }
  1029. ZVAL_COPY(&EG(user_exception_handler), &(fci.function_name));
  1030. }
  1031. /* }}} */
  1032. /* {{{ Restores the previously defined exception handler function */
  1033. ZEND_FUNCTION(restore_exception_handler)
  1034. {
  1035. ZEND_PARSE_PARAMETERS_NONE();
  1036. if (Z_TYPE(EG(user_exception_handler)) != IS_UNDEF) {
  1037. zval_ptr_dtor(&EG(user_exception_handler));
  1038. }
  1039. if (zend_stack_is_empty(&EG(user_exception_handlers))) {
  1040. ZVAL_UNDEF(&EG(user_exception_handler));
  1041. } else {
  1042. zval *tmp = zend_stack_top(&EG(user_exception_handlers));
  1043. ZVAL_COPY_VALUE(&EG(user_exception_handler), tmp);
  1044. zend_stack_del_top(&EG(user_exception_handlers));
  1045. }
  1046. // TODO Change to void
  1047. RETURN_TRUE;
  1048. }
  1049. /* }}} */
  1050. static inline void get_declared_class_impl(INTERNAL_FUNCTION_PARAMETERS, int flags) /* {{{ */
  1051. {
  1052. zend_string *key;
  1053. zval *zv;
  1054. zend_class_entry *ce;
  1055. ZEND_PARSE_PARAMETERS_NONE();
  1056. array_init(return_value);
  1057. zend_hash_real_init_packed(Z_ARRVAL_P(return_value));
  1058. ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
  1059. ZEND_HASH_FOREACH_STR_KEY_VAL(EG(class_table), key, zv) {
  1060. ce = Z_PTR_P(zv);
  1061. if ((ce->ce_flags & (ZEND_ACC_LINKED|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT)) == flags
  1062. && key
  1063. && ZSTR_VAL(key)[0] != 0) {
  1064. ZEND_HASH_FILL_GROW();
  1065. if (EXPECTED(Z_TYPE_P(zv) == IS_PTR)) {
  1066. ZEND_HASH_FILL_SET_STR_COPY(ce->name);
  1067. } else {
  1068. ZEND_ASSERT(Z_TYPE_P(zv) == IS_ALIAS_PTR);
  1069. ZEND_HASH_FILL_SET_STR_COPY(key);
  1070. }
  1071. ZEND_HASH_FILL_NEXT();
  1072. }
  1073. } ZEND_HASH_FOREACH_END();
  1074. } ZEND_HASH_FILL_END();
  1075. }
  1076. /* {{{ */
  1077. /* {{{ Returns an array of all declared traits. */
  1078. ZEND_FUNCTION(get_declared_traits)
  1079. {
  1080. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_TRAIT);
  1081. }
  1082. /* }}} */
  1083. /* {{{ Returns an array of all declared classes. */
  1084. ZEND_FUNCTION(get_declared_classes)
  1085. {
  1086. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED);
  1087. }
  1088. /* }}} */
  1089. /* {{{ Returns an array of all declared interfaces. */
  1090. ZEND_FUNCTION(get_declared_interfaces)
  1091. {
  1092. get_declared_class_impl(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_LINKED | ZEND_ACC_INTERFACE);
  1093. }
  1094. /* }}} */
  1095. /* {{{ Returns an array of all defined functions */
  1096. ZEND_FUNCTION(get_defined_functions)
  1097. {
  1098. zval internal, user;
  1099. zend_string *key;
  1100. zend_function *func;
  1101. bool exclude_disabled = 1;
  1102. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &exclude_disabled) == FAILURE) {
  1103. RETURN_THROWS();
  1104. }
  1105. if (exclude_disabled == 0) {
  1106. zend_error(E_DEPRECATED,
  1107. "get_defined_functions(): Setting $exclude_disabled to false has no effect");
  1108. }
  1109. array_init(&internal);
  1110. array_init(&user);
  1111. array_init(return_value);
  1112. ZEND_HASH_FOREACH_STR_KEY_PTR(EG(function_table), key, func) {
  1113. if (key && ZSTR_VAL(key)[0] != 0) {
  1114. if (func->type == ZEND_INTERNAL_FUNCTION) {
  1115. add_next_index_str(&internal, zend_string_copy(key));
  1116. } else if (func->type == ZEND_USER_FUNCTION) {
  1117. add_next_index_str(&user, zend_string_copy(key));
  1118. }
  1119. }
  1120. } ZEND_HASH_FOREACH_END();
  1121. zend_hash_str_add_new(Z_ARRVAL_P(return_value), "internal", sizeof("internal")-1, &internal);
  1122. zend_hash_str_add_new(Z_ARRVAL_P(return_value), "user", sizeof("user")-1, &user);
  1123. }
  1124. /* }}} */
  1125. /* {{{ Returns an associative array of names and values of all currently defined variable names (variables in the current scope) */
  1126. ZEND_FUNCTION(get_defined_vars)
  1127. {
  1128. zend_array *symbol_table;
  1129. ZEND_PARSE_PARAMETERS_NONE();
  1130. if (zend_forbid_dynamic_call("get_defined_vars()") == FAILURE) {
  1131. return;
  1132. }
  1133. symbol_table = zend_rebuild_symbol_table();
  1134. if (UNEXPECTED(symbol_table == NULL)) {
  1135. RETURN_EMPTY_ARRAY();
  1136. }
  1137. RETURN_ARR(zend_array_dup(symbol_table));
  1138. }
  1139. /* }}} */
  1140. #if ZEND_DEBUG && defined(ZTS)
  1141. ZEND_FUNCTION(zend_thread_id)
  1142. {
  1143. ZEND_PARSE_PARAMETERS_NONE();
  1144. RETURN_LONG((zend_long)tsrm_thread_id());
  1145. }
  1146. #endif
  1147. /* {{{ Get the resource type name for a given resource */
  1148. ZEND_FUNCTION(get_resource_type)
  1149. {
  1150. const char *resource_type;
  1151. zval *z_resource_type;
  1152. if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &z_resource_type) == FAILURE) {
  1153. RETURN_THROWS();
  1154. }
  1155. resource_type = zend_rsrc_list_get_rsrc_type(Z_RES_P(z_resource_type));
  1156. if (resource_type) {
  1157. RETURN_STRING(resource_type);
  1158. } else {
  1159. RETURN_STRING("Unknown");
  1160. }
  1161. }
  1162. /* }}} */
  1163. /* {{{ Get the resource ID for a given resource */
  1164. ZEND_FUNCTION(get_resource_id)
  1165. {
  1166. zval *resource;
  1167. ZEND_PARSE_PARAMETERS_START(1, 1)
  1168. Z_PARAM_RESOURCE(resource)
  1169. ZEND_PARSE_PARAMETERS_END();
  1170. RETURN_LONG(Z_RES_HANDLE_P(resource));
  1171. }
  1172. /* }}} */
  1173. /* {{{ Get an array with all active resources */
  1174. ZEND_FUNCTION(get_resources)
  1175. {
  1176. zend_string *type = NULL;
  1177. zend_string *key;
  1178. zend_ulong index;
  1179. zval *val;
  1180. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|S!", &type) == FAILURE) {
  1181. RETURN_THROWS();
  1182. }
  1183. if (!type) {
  1184. array_init(return_value);
  1185. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1186. if (!key) {
  1187. Z_ADDREF_P(val);
  1188. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1189. }
  1190. } ZEND_HASH_FOREACH_END();
  1191. } else if (zend_string_equals_literal(type, "Unknown")) {
  1192. array_init(return_value);
  1193. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1194. if (!key && Z_RES_TYPE_P(val) <= 0) {
  1195. Z_ADDREF_P(val);
  1196. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1197. }
  1198. } ZEND_HASH_FOREACH_END();
  1199. } else {
  1200. int id = zend_fetch_list_dtor_id(ZSTR_VAL(type));
  1201. if (id <= 0) {
  1202. zend_argument_value_error(1, "must be a valid resource type");
  1203. RETURN_THROWS();
  1204. }
  1205. array_init(return_value);
  1206. ZEND_HASH_FOREACH_KEY_VAL(&EG(regular_list), index, key, val) {
  1207. if (!key && Z_RES_TYPE_P(val) == id) {
  1208. Z_ADDREF_P(val);
  1209. zend_hash_index_add_new(Z_ARRVAL_P(return_value), index, val);
  1210. }
  1211. } ZEND_HASH_FOREACH_END();
  1212. }
  1213. }
  1214. /* }}} */
  1215. static void add_zendext_info(zend_extension *ext, void *arg) /* {{{ */
  1216. {
  1217. zval *name_array = (zval *)arg;
  1218. add_next_index_string(name_array, ext->name);
  1219. }
  1220. /* }}} */
  1221. /* {{{ Return an array containing names of loaded extensions */
  1222. ZEND_FUNCTION(get_loaded_extensions)
  1223. {
  1224. bool zendext = 0;
  1225. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &zendext) == FAILURE) {
  1226. RETURN_THROWS();
  1227. }
  1228. array_init(return_value);
  1229. if (zendext) {
  1230. zend_llist_apply_with_argument(&zend_extensions, (llist_apply_with_arg_func_t) add_zendext_info, return_value);
  1231. } else {
  1232. zend_module_entry *module;
  1233. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1234. add_next_index_string(return_value, module->name);
  1235. } ZEND_HASH_FOREACH_END();
  1236. }
  1237. }
  1238. /* }}} */
  1239. /* {{{ Return an array containing the names and values of all defined constants */
  1240. ZEND_FUNCTION(get_defined_constants)
  1241. {
  1242. bool categorize = 0;
  1243. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &categorize) == FAILURE) {
  1244. RETURN_THROWS();
  1245. }
  1246. array_init(return_value);
  1247. if (categorize) {
  1248. zend_constant *val;
  1249. int module_number;
  1250. zval *modules, const_val;
  1251. char **module_names;
  1252. zend_module_entry *module;
  1253. int i = 1;
  1254. modules = ecalloc(zend_hash_num_elements(&module_registry) + 2, sizeof(zval));
  1255. module_names = emalloc((zend_hash_num_elements(&module_registry) + 2) * sizeof(char *));
  1256. module_names[0] = "internal";
  1257. ZEND_HASH_FOREACH_PTR(&module_registry, module) {
  1258. module_names[module->module_number] = (char *)module->name;
  1259. i++;
  1260. } ZEND_HASH_FOREACH_END();
  1261. module_names[i] = "user";
  1262. ZEND_HASH_FOREACH_PTR(EG(zend_constants), val) {
  1263. if (!val->name) {
  1264. /* skip special constants */
  1265. continue;
  1266. }
  1267. if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
  1268. module_number = i;
  1269. } else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
  1270. /* should not happen */
  1271. continue;
  1272. } else {
  1273. module_number = ZEND_CONSTANT_MODULE_NUMBER(val);
  1274. }
  1275. if (Z_TYPE(modules[module_number]) == IS_UNDEF) {
  1276. array_init(&modules[module_number]);
  1277. add_assoc_zval(return_value, module_names[module_number], &modules[module_number]);
  1278. }
  1279. ZVAL_COPY_OR_DUP(&const_val, &val->value);
  1280. zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
  1281. } ZEND_HASH_FOREACH_END();
  1282. efree(module_names);
  1283. efree(modules);
  1284. } else {
  1285. zend_constant *constant;
  1286. zval const_val;
  1287. ZEND_HASH_FOREACH_PTR(EG(zend_constants), constant) {
  1288. if (!constant->name) {
  1289. /* skip special constants */
  1290. continue;
  1291. }
  1292. ZVAL_COPY_OR_DUP(&const_val, &constant->value);
  1293. zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
  1294. } ZEND_HASH_FOREACH_END();
  1295. }
  1296. }
  1297. /* }}} */
  1298. static void debug_backtrace_get_args(zend_execute_data *call, zval *arg_array) /* {{{ */
  1299. {
  1300. uint32_t num_args = ZEND_CALL_NUM_ARGS(call);
  1301. if (num_args) {
  1302. uint32_t i = 0;
  1303. zval *p = ZEND_CALL_ARG(call, 1);
  1304. array_init_size(arg_array, num_args);
  1305. zend_hash_real_init_packed(Z_ARRVAL_P(arg_array));
  1306. ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(arg_array)) {
  1307. if (call->func->type == ZEND_USER_FUNCTION) {
  1308. uint32_t first_extra_arg = MIN(num_args, call->func->op_array.num_args);
  1309. if (UNEXPECTED(ZEND_CALL_INFO(call) & ZEND_CALL_HAS_SYMBOL_TABLE)) {
  1310. /* In case of attached symbol_table, values on stack may be invalid
  1311. * and we have to access them through symbol_table
  1312. * See: https://bugs.php.net/bug.php?id=73156
  1313. */
  1314. while (i < first_extra_arg) {
  1315. zend_string *arg_name = call->func->op_array.vars[i];
  1316. zval *arg = zend_hash_find_ex_ind(call->symbol_table, arg_name, 1);
  1317. if (arg) {
  1318. ZVAL_DEREF(arg);
  1319. Z_TRY_ADDREF_P(arg);
  1320. ZEND_HASH_FILL_SET(arg);
  1321. } else {
  1322. ZEND_HASH_FILL_SET_NULL();
  1323. }
  1324. ZEND_HASH_FILL_NEXT();
  1325. i++;
  1326. }
  1327. } else {
  1328. while (i < first_extra_arg) {
  1329. if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
  1330. zval *arg = p;
  1331. ZVAL_DEREF(arg);
  1332. Z_TRY_ADDREF_P(arg);
  1333. ZEND_HASH_FILL_SET(arg);
  1334. } else {
  1335. ZEND_HASH_FILL_SET_NULL();
  1336. }
  1337. ZEND_HASH_FILL_NEXT();
  1338. p++;
  1339. i++;
  1340. }
  1341. }
  1342. p = ZEND_CALL_VAR_NUM(call, call->func->op_array.last_var + call->func->op_array.T);
  1343. }
  1344. while (i < num_args) {
  1345. if (EXPECTED(Z_TYPE_INFO_P(p) != IS_UNDEF)) {
  1346. zval *arg = p;
  1347. ZVAL_DEREF(arg);
  1348. Z_TRY_ADDREF_P(arg);
  1349. ZEND_HASH_FILL_SET(arg);
  1350. } else {
  1351. ZEND_HASH_FILL_SET_NULL();
  1352. }
  1353. ZEND_HASH_FILL_NEXT();
  1354. p++;
  1355. i++;
  1356. }
  1357. } ZEND_HASH_FILL_END();
  1358. Z_ARRVAL_P(arg_array)->nNumOfElements = num_args;
  1359. } else {
  1360. ZVAL_EMPTY_ARRAY(arg_array);
  1361. }
  1362. if (ZEND_CALL_INFO(call) & ZEND_CALL_HAS_EXTRA_NAMED_PARAMS) {
  1363. zend_string *name;
  1364. zval *arg;
  1365. SEPARATE_ARRAY(arg_array);
  1366. ZEND_HASH_FOREACH_STR_KEY_VAL(call->extra_named_params, name, arg) {
  1367. ZVAL_DEREF(arg);
  1368. Z_TRY_ADDREF_P(arg);
  1369. zend_hash_add_new(Z_ARRVAL_P(arg_array), name, arg);
  1370. } ZEND_HASH_FOREACH_END();
  1371. }
  1372. }
  1373. /* }}} */
  1374. /* {{{ */
  1375. ZEND_FUNCTION(debug_print_backtrace)
  1376. {
  1377. zend_long options = 0;
  1378. zend_long limit = 0;
  1379. zval backtrace;
  1380. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
  1381. RETURN_THROWS();
  1382. }
  1383. zend_fetch_debug_backtrace(&backtrace, 1, options, limit);
  1384. ZEND_ASSERT(Z_TYPE(backtrace) == IS_ARRAY);
  1385. zend_string *str = zend_trace_to_string(Z_ARRVAL(backtrace), /* include_main */ false);
  1386. ZEND_WRITE(ZSTR_VAL(str), ZSTR_LEN(str));
  1387. zend_string_release(str);
  1388. zval_ptr_dtor(&backtrace);
  1389. }
  1390. /* }}} */
  1391. ZEND_API void zend_fetch_debug_backtrace(zval *return_value, int skip_last, int options, int limit) /* {{{ */
  1392. {
  1393. zend_execute_data *call;
  1394. zend_object *object;
  1395. bool fake_frame = 0;
  1396. int lineno, frameno = 0;
  1397. zend_function *func;
  1398. zend_string *filename;
  1399. zend_string *include_filename = NULL;
  1400. zval tmp;
  1401. HashTable *stack_frame;
  1402. array_init(return_value);
  1403. call = EG(current_execute_data);
  1404. if (!call) {
  1405. return;
  1406. }
  1407. if (skip_last) {
  1408. /* skip debug_backtrace() */
  1409. call = call->prev_execute_data;
  1410. }
  1411. while (call && (limit == 0 || frameno < limit)) {
  1412. zend_execute_data *prev = call->prev_execute_data;
  1413. if (!prev) {
  1414. /* add frame for a handler call without {main} code */
  1415. if (EXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_TOP_FUNCTION) == 0)) {
  1416. break;
  1417. }
  1418. } else if (UNEXPECTED((ZEND_CALL_INFO(call) & ZEND_CALL_GENERATOR) != 0)) {
  1419. prev = zend_generator_check_placeholder_frame(prev);
  1420. }
  1421. frameno++;
  1422. /* We use _zend_hash_append*() and the array must be preallocated */
  1423. stack_frame = zend_new_array(8);
  1424. zend_hash_real_init_mixed(stack_frame);
  1425. if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
  1426. filename = prev->func->op_array.filename;
  1427. if (prev->opline->opcode == ZEND_HANDLE_EXCEPTION) {
  1428. if (EG(opline_before_exception)) {
  1429. lineno = EG(opline_before_exception)->lineno;
  1430. } else {
  1431. lineno = prev->func->op_array.line_end;
  1432. }
  1433. } else {
  1434. lineno = prev->opline->lineno;
  1435. }
  1436. ZVAL_STR_COPY(&tmp, filename);
  1437. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
  1438. ZVAL_LONG(&tmp, lineno);
  1439. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
  1440. /* try to fetch args only if an FCALL was just made - elsewise we're in the middle of a function
  1441. * and debug_backtrace() might have been called by the error_handler. in this case we don't
  1442. * want to pop anything of the argument-stack */
  1443. } else {
  1444. zend_execute_data *prev_call = prev;
  1445. while (prev_call) {
  1446. zend_execute_data *prev;
  1447. if (prev_call &&
  1448. prev_call->func &&
  1449. !ZEND_USER_CODE(prev_call->func->common.type) &&
  1450. !(prev_call->func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE)) {
  1451. break;
  1452. }
  1453. prev = prev_call->prev_execute_data;
  1454. if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type)) {
  1455. ZVAL_STR_COPY(&tmp, prev->func->op_array.filename);
  1456. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FILE), &tmp, 1);
  1457. ZVAL_LONG(&tmp, prev->opline->lineno);
  1458. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_LINE), &tmp, 1);
  1459. break;
  1460. }
  1461. prev_call = prev;
  1462. }
  1463. filename = NULL;
  1464. }
  1465. func = call->func;
  1466. if (!fake_frame && func->common.function_name) {
  1467. ZVAL_STR_COPY(&tmp, func->common.function_name);
  1468. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
  1469. if (Z_TYPE(call->This) == IS_OBJECT) {
  1470. object = Z_OBJ(call->This);
  1471. /* $this may be passed into regular internal functions */
  1472. if (func->common.scope) {
  1473. ZVAL_STR_COPY(&tmp, func->common.scope->name);
  1474. } else if (object->handlers->get_class_name == zend_std_get_class_name) {
  1475. ZVAL_STR_COPY(&tmp, object->ce->name);
  1476. } else {
  1477. ZVAL_STR(&tmp, object->handlers->get_class_name(object));
  1478. }
  1479. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
  1480. if ((options & DEBUG_BACKTRACE_PROVIDE_OBJECT) != 0) {
  1481. ZVAL_OBJ_COPY(&tmp, object);
  1482. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_OBJECT), &tmp, 1);
  1483. }
  1484. ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_OBJECT_OPERATOR));
  1485. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
  1486. } else if (func->common.scope) {
  1487. ZVAL_STR_COPY(&tmp, func->common.scope->name);
  1488. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_CLASS), &tmp, 1);
  1489. ZVAL_INTERNED_STR(&tmp, ZSTR_KNOWN(ZEND_STR_PAAMAYIM_NEKUDOTAYIM));
  1490. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_TYPE), &tmp, 1);
  1491. }
  1492. if ((options & DEBUG_BACKTRACE_IGNORE_ARGS) == 0 &&
  1493. func->type != ZEND_EVAL_CODE) {
  1494. debug_backtrace_get_args(call, &tmp);
  1495. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &tmp, 1);
  1496. }
  1497. } else {
  1498. /* i know this is kinda ugly, but i'm trying to avoid extra cycles in the main execution loop */
  1499. bool build_filename_arg = 1;
  1500. zend_string *pseudo_function_name;
  1501. uint32_t include_kind = 0;
  1502. if (prev && prev->func && ZEND_USER_CODE(prev->func->common.type) && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
  1503. include_kind = prev->opline->extended_value;
  1504. }
  1505. switch (include_kind) {
  1506. case ZEND_EVAL:
  1507. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_EVAL);
  1508. build_filename_arg = 0;
  1509. break;
  1510. case ZEND_INCLUDE:
  1511. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE);
  1512. break;
  1513. case ZEND_REQUIRE:
  1514. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE);
  1515. break;
  1516. case ZEND_INCLUDE_ONCE:
  1517. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_INCLUDE_ONCE);
  1518. break;
  1519. case ZEND_REQUIRE_ONCE:
  1520. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_REQUIRE_ONCE);
  1521. break;
  1522. default:
  1523. /* Skip dummy frame unless it is needed to preserve filename/lineno info. */
  1524. if (!filename) {
  1525. zend_array_destroy(stack_frame);
  1526. goto skip_frame;
  1527. }
  1528. pseudo_function_name = ZSTR_KNOWN(ZEND_STR_UNKNOWN);
  1529. build_filename_arg = 0;
  1530. break;
  1531. }
  1532. if (build_filename_arg && include_filename) {
  1533. zval arg_array;
  1534. array_init(&arg_array);
  1535. /* include_filename always points to the last filename of the last last called-function.
  1536. if we have called include in the frame above - this is the file we have included.
  1537. */
  1538. ZVAL_STR_COPY(&tmp, include_filename);
  1539. zend_hash_next_index_insert_new(Z_ARRVAL(arg_array), &tmp);
  1540. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_ARGS), &arg_array, 1);
  1541. }
  1542. ZVAL_INTERNED_STR(&tmp, pseudo_function_name);
  1543. _zend_hash_append_ex(stack_frame, ZSTR_KNOWN(ZEND_STR_FUNCTION), &tmp, 1);
  1544. }
  1545. ZVAL_ARR(&tmp, stack_frame);
  1546. zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &tmp);
  1547. skip_frame:
  1548. if (UNEXPECTED(ZEND_CALL_KIND(call) == ZEND_CALL_TOP_FUNCTION)
  1549. && !fake_frame
  1550. && prev
  1551. && prev->func
  1552. && ZEND_USER_CODE(prev->func->common.type)
  1553. && prev->opline->opcode == ZEND_INCLUDE_OR_EVAL) {
  1554. fake_frame = 1;
  1555. } else {
  1556. fake_frame = 0;
  1557. include_filename = filename;
  1558. call = prev;
  1559. }
  1560. }
  1561. }
  1562. /* }}} */
  1563. /* {{{ Return backtrace as array */
  1564. ZEND_FUNCTION(debug_backtrace)
  1565. {
  1566. zend_long options = DEBUG_BACKTRACE_PROVIDE_OBJECT;
  1567. zend_long limit = 0;
  1568. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ll", &options, &limit) == FAILURE) {
  1569. RETURN_THROWS();
  1570. }
  1571. zend_fetch_debug_backtrace(return_value, 1, options, limit);
  1572. }
  1573. /* }}} */
  1574. /* {{{ Returns true if the named extension is loaded */
  1575. ZEND_FUNCTION(extension_loaded)
  1576. {
  1577. zend_string *extension_name;
  1578. zend_string *lcname;
  1579. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
  1580. RETURN_THROWS();
  1581. }
  1582. lcname = zend_string_tolower(extension_name);
  1583. if (zend_hash_exists(&module_registry, lcname)) {
  1584. RETVAL_TRUE;
  1585. } else {
  1586. RETVAL_FALSE;
  1587. }
  1588. zend_string_release_ex(lcname, 0);
  1589. }
  1590. /* }}} */
  1591. /* {{{ Returns an array with the names of functions belonging to the named extension */
  1592. ZEND_FUNCTION(get_extension_funcs)
  1593. {
  1594. zend_string *extension_name;
  1595. zend_string *lcname;
  1596. bool array;
  1597. zend_module_entry *module;
  1598. zend_function *zif;
  1599. if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &extension_name) == FAILURE) {
  1600. RETURN_THROWS();
  1601. }
  1602. if (strncasecmp(ZSTR_VAL(extension_name), "zend", sizeof("zend"))) {
  1603. lcname = zend_string_tolower(extension_name);
  1604. module = zend_hash_find_ptr(&module_registry, lcname);
  1605. zend_string_release_ex(lcname, 0);
  1606. } else {
  1607. module = zend_hash_str_find_ptr(&module_registry, "core", sizeof("core") - 1);
  1608. }
  1609. if (!module) {
  1610. RETURN_FALSE;
  1611. }
  1612. if (module->functions) {
  1613. /* avoid BC break, if functions list is empty, will return an empty array */
  1614. array_init(return_value);
  1615. array = 1;
  1616. } else {
  1617. array = 0;
  1618. }
  1619. ZEND_HASH_FOREACH_PTR(CG(function_table), zif) {
  1620. if (zif->common.type == ZEND_INTERNAL_FUNCTION
  1621. && zif->internal_function.module == module) {
  1622. if (!array) {
  1623. array_init(return_value);
  1624. array = 1;
  1625. }
  1626. add_next_index_str(return_value, zend_string_copy(zif->common.function_name));
  1627. }
  1628. } ZEND_HASH_FOREACH_END();
  1629. if (!array) {
  1630. RETURN_FALSE;
  1631. }
  1632. }
  1633. /* }}} */