zend_exceptions.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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. | Marcus Boerger <helly@php.net> |
  17. | Sterling Hughes <sterling@php.net> |
  18. | Zeev Suraski <zeev@php.net> |
  19. +----------------------------------------------------------------------+
  20. */
  21. #include "zend.h"
  22. #include "zend_API.h"
  23. #include "zend_builtin_functions.h"
  24. #include "zend_interfaces.h"
  25. #include "zend_exceptions.h"
  26. #include "zend_vm.h"
  27. #include "zend_dtrace.h"
  28. #include "zend_smart_str.h"
  29. #include "zend_exceptions_arginfo.h"
  30. #include "zend_observer.h"
  31. ZEND_API zend_class_entry *zend_ce_throwable;
  32. ZEND_API zend_class_entry *zend_ce_exception;
  33. ZEND_API zend_class_entry *zend_ce_error_exception;
  34. ZEND_API zend_class_entry *zend_ce_error;
  35. ZEND_API zend_class_entry *zend_ce_compile_error;
  36. ZEND_API zend_class_entry *zend_ce_parse_error;
  37. ZEND_API zend_class_entry *zend_ce_type_error;
  38. ZEND_API zend_class_entry *zend_ce_argument_count_error;
  39. ZEND_API zend_class_entry *zend_ce_value_error;
  40. ZEND_API zend_class_entry *zend_ce_arithmetic_error;
  41. ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
  42. ZEND_API zend_class_entry *zend_ce_unhandled_match_error;
  43. /* Internal pseudo-exception that is not exposed to userland. Throwing this exception *does not* execute finally blocks. */
  44. static zend_class_entry zend_ce_unwind_exit;
  45. /* Internal pseudo-exception that is not exposed to userland. Throwing this exception *does* execute finally blocks. */
  46. static zend_class_entry zend_ce_graceful_exit;
  47. ZEND_API void (*zend_throw_exception_hook)(zend_object *ex);
  48. static zend_object_handlers default_exception_handlers;
  49. /* {{{ zend_implement_throwable */
  50. static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
  51. {
  52. /* zend_ce_exception and zend_ce_error may not be initialized yet when this is called (e.g when
  53. * implementing Throwable for Exception itself). Perform a manual inheritance check. */
  54. zend_class_entry *root = class_type;
  55. while (root->parent) {
  56. root = root->parent;
  57. }
  58. if (zend_string_equals_literal(root->name, "Exception")
  59. || zend_string_equals_literal(root->name, "Error")) {
  60. return SUCCESS;
  61. }
  62. zend_error_noreturn(E_ERROR,
  63. "Class %s cannot implement interface %s, extend Exception or Error instead",
  64. ZSTR_VAL(class_type->name),
  65. ZSTR_VAL(interface->name));
  66. return FAILURE;
  67. }
  68. /* }}} */
  69. static inline zend_class_entry *i_get_exception_base(zend_object *object) /* {{{ */
  70. {
  71. return instanceof_function(object->ce, zend_ce_exception) ? zend_ce_exception : zend_ce_error;
  72. }
  73. /* }}} */
  74. ZEND_API zend_class_entry *zend_get_exception_base(zend_object *object) /* {{{ */
  75. {
  76. return i_get_exception_base(object);
  77. }
  78. /* }}} */
  79. void zend_exception_set_previous(zend_object *exception, zend_object *add_previous) /* {{{ */
  80. {
  81. zval *previous, *ancestor, *ex;
  82. zval pv, zv, rv;
  83. zend_class_entry *base_ce;
  84. if (!exception || !add_previous) {
  85. return;
  86. }
  87. if (exception == add_previous || zend_is_unwind_exit(add_previous) || zend_is_graceful_exit(add_previous)) {
  88. OBJ_RELEASE(add_previous);
  89. return;
  90. }
  91. ZEND_ASSERT(instanceof_function(add_previous->ce, zend_ce_throwable)
  92. && "Previous exception must implement Throwable");
  93. ZVAL_OBJ(&pv, add_previous);
  94. ZVAL_OBJ(&zv, exception);
  95. ex = &zv;
  96. do {
  97. ancestor = zend_read_property_ex(i_get_exception_base(add_previous), add_previous, ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
  98. while (Z_TYPE_P(ancestor) == IS_OBJECT) {
  99. if (Z_OBJ_P(ancestor) == Z_OBJ_P(ex)) {
  100. OBJ_RELEASE(add_previous);
  101. return;
  102. }
  103. ancestor = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(ancestor)), Z_OBJ_P(ancestor), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
  104. }
  105. base_ce = i_get_exception_base(Z_OBJ_P(ex));
  106. previous = zend_read_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), 1, &rv);
  107. if (Z_TYPE_P(previous) == IS_NULL) {
  108. zend_update_property_ex(base_ce, Z_OBJ_P(ex), ZSTR_KNOWN(ZEND_STR_PREVIOUS), &pv);
  109. GC_DELREF(add_previous);
  110. return;
  111. }
  112. ex = previous;
  113. } while (Z_OBJ_P(ex) != add_previous);
  114. }
  115. /* }}} */
  116. void zend_exception_save(void) /* {{{ */
  117. {
  118. if (EG(prev_exception)) {
  119. zend_exception_set_previous(EG(exception), EG(prev_exception));
  120. }
  121. if (EG(exception)) {
  122. EG(prev_exception) = EG(exception);
  123. }
  124. EG(exception) = NULL;
  125. }
  126. /* }}} */
  127. void zend_exception_restore(void) /* {{{ */
  128. {
  129. if (EG(prev_exception)) {
  130. if (EG(exception)) {
  131. zend_exception_set_previous(EG(exception), EG(prev_exception));
  132. } else {
  133. EG(exception) = EG(prev_exception);
  134. }
  135. EG(prev_exception) = NULL;
  136. }
  137. }
  138. /* }}} */
  139. static zend_always_inline bool is_handle_exception_set(void) {
  140. zend_execute_data *execute_data = EG(current_execute_data);
  141. return !execute_data->func
  142. || !ZEND_USER_CODE(execute_data->func->common.type)
  143. || execute_data->opline->opcode == ZEND_HANDLE_EXCEPTION;
  144. }
  145. ZEND_API ZEND_COLD void zend_throw_exception_internal(zend_object *exception) /* {{{ */
  146. {
  147. #ifdef HAVE_DTRACE
  148. if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
  149. if (exception != NULL) {
  150. DTRACE_EXCEPTION_THROWN(ZSTR_VAL(exception->ce->name));
  151. } else {
  152. DTRACE_EXCEPTION_THROWN(NULL);
  153. }
  154. }
  155. #endif /* HAVE_DTRACE */
  156. if (exception != NULL) {
  157. zend_object *previous = EG(exception);
  158. if (previous && zend_is_unwind_exit(previous)) {
  159. /* Don't replace unwinding exception with different exception. */
  160. OBJ_RELEASE(exception);
  161. return;
  162. }
  163. zend_exception_set_previous(exception, EG(exception));
  164. EG(exception) = exception;
  165. if (previous) {
  166. ZEND_ASSERT(is_handle_exception_set() && "HANDLE_EXCEPTION not set?");
  167. return;
  168. }
  169. }
  170. if (!EG(current_execute_data)) {
  171. if (exception && (exception->ce == zend_ce_parse_error || exception->ce == zend_ce_compile_error)) {
  172. return;
  173. }
  174. if (EG(exception)) {
  175. zend_exception_error(EG(exception), E_ERROR);
  176. zend_bailout();
  177. }
  178. zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
  179. }
  180. if (zend_throw_exception_hook) {
  181. zend_throw_exception_hook(exception);
  182. }
  183. if (is_handle_exception_set()) {
  184. /* no need to rethrow the exception */
  185. return;
  186. }
  187. EG(opline_before_exception) = EG(current_execute_data)->opline;
  188. EG(current_execute_data)->opline = EG(exception_op);
  189. }
  190. /* }}} */
  191. ZEND_API void zend_clear_exception(void) /* {{{ */
  192. {
  193. zend_object *exception;
  194. if (EG(prev_exception)) {
  195. OBJ_RELEASE(EG(prev_exception));
  196. EG(prev_exception) = NULL;
  197. }
  198. if (!EG(exception)) {
  199. return;
  200. }
  201. /* exception may have destructor */
  202. exception = EG(exception);
  203. EG(exception) = NULL;
  204. OBJ_RELEASE(exception);
  205. if (EG(current_execute_data)) {
  206. EG(current_execute_data)->opline = EG(opline_before_exception);
  207. }
  208. #if ZEND_DEBUG
  209. EG(opline_before_exception) = NULL;
  210. #endif
  211. }
  212. /* }}} */
  213. static zend_object *zend_default_exception_new_ex(zend_class_entry *class_type, bool skip_top_traces) /* {{{ */
  214. {
  215. zval tmp;
  216. zval trace;
  217. zend_class_entry *base_ce;
  218. zend_string *filename;
  219. zend_object *object = zend_objects_new(class_type);
  220. object->handlers = &default_exception_handlers;
  221. object_properties_init(object, class_type);
  222. if (EG(current_execute_data)) {
  223. zend_fetch_debug_backtrace(&trace,
  224. skip_top_traces,
  225. EG(exception_ignore_args) ? DEBUG_BACKTRACE_IGNORE_ARGS : 0, 0);
  226. } else {
  227. array_init(&trace);
  228. }
  229. Z_SET_REFCOUNT(trace, 0);
  230. base_ce = i_get_exception_base(object);
  231. if (EXPECTED((class_type != zend_ce_parse_error && class_type != zend_ce_compile_error)
  232. || !(filename = zend_get_compiled_filename()))) {
  233. ZVAL_STRING(&tmp, zend_get_executed_filename());
  234. zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
  235. zval_ptr_dtor(&tmp);
  236. ZVAL_LONG(&tmp, zend_get_executed_lineno());
  237. zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  238. } else {
  239. ZVAL_STR(&tmp, filename);
  240. zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
  241. ZVAL_LONG(&tmp, zend_get_compiled_lineno());
  242. zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  243. }
  244. zend_update_property_ex(base_ce, object, ZSTR_KNOWN(ZEND_STR_TRACE), &trace);
  245. return object;
  246. }
  247. /* }}} */
  248. static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
  249. {
  250. return zend_default_exception_new_ex(class_type, 0);
  251. }
  252. /* }}} */
  253. static zend_object *zend_error_exception_new(zend_class_entry *class_type) /* {{{ */
  254. {
  255. return zend_default_exception_new_ex(class_type, 0);
  256. }
  257. /* }}} */
  258. /* {{{ Clone the exception object */
  259. ZEND_COLD ZEND_METHOD(Exception, __clone)
  260. {
  261. /* Should never be executable */
  262. zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
  263. }
  264. /* }}} */
  265. /* {{{ Exception constructor */
  266. ZEND_METHOD(Exception, __construct)
  267. {
  268. zend_string *message = NULL;
  269. zend_long code = 0;
  270. zval tmp, *object, *previous = NULL;
  271. zend_class_entry *base_ce;
  272. object = ZEND_THIS;
  273. base_ce = i_get_exception_base(Z_OBJ_P(object));
  274. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
  275. RETURN_THROWS();
  276. }
  277. if (message) {
  278. ZVAL_STR(&tmp, message);
  279. zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
  280. }
  281. if (code) {
  282. ZVAL_LONG(&tmp, code);
  283. zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
  284. }
  285. if (previous) {
  286. zend_update_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
  287. }
  288. }
  289. /* }}} */
  290. /* {{{ Exception unserialize checks */
  291. #define CHECK_EXC_TYPE(id, type) \
  292. pvalue = zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &value); \
  293. if (Z_TYPE_P(pvalue) != IS_NULL && Z_TYPE_P(pvalue) != type) { \
  294. zend_unset_property(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_VAL(ZSTR_KNOWN(id)), ZSTR_LEN(ZSTR_KNOWN(id))); \
  295. }
  296. ZEND_METHOD(Exception, __wakeup)
  297. {
  298. ZEND_PARSE_PARAMETERS_NONE();
  299. zval value, *pvalue;
  300. zval *object = ZEND_THIS;
  301. CHECK_EXC_TYPE(ZEND_STR_MESSAGE, IS_STRING);
  302. CHECK_EXC_TYPE(ZEND_STR_CODE, IS_LONG);
  303. /* The type of all other properties is enforced through typed properties. */
  304. }
  305. /* }}} */
  306. /* {{{ ErrorException constructor */
  307. ZEND_METHOD(ErrorException, __construct)
  308. {
  309. zend_string *message = NULL, *filename = NULL;
  310. zend_long code = 0, severity = E_ERROR, lineno;
  311. bool lineno_is_null = 1;
  312. zval tmp, *object, *previous = NULL;
  313. if (zend_parse_parameters(ZEND_NUM_ARGS(), "|SllS!l!O!", &message, &code, &severity, &filename, &lineno, &lineno_is_null, &previous, zend_ce_throwable) == FAILURE) {
  314. RETURN_THROWS();
  315. }
  316. object = ZEND_THIS;
  317. if (message) {
  318. ZVAL_STR_COPY(&tmp, message);
  319. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
  320. zval_ptr_dtor(&tmp);
  321. }
  322. if (code) {
  323. ZVAL_LONG(&tmp, code);
  324. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
  325. }
  326. if (previous) {
  327. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_PREVIOUS), previous);
  328. }
  329. ZVAL_LONG(&tmp, severity);
  330. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
  331. if (filename) {
  332. ZVAL_STR_COPY(&tmp, filename);
  333. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_FILE), &tmp);
  334. zval_ptr_dtor(&tmp);
  335. }
  336. if (!lineno_is_null) {
  337. ZVAL_LONG(&tmp, lineno);
  338. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  339. } else if (filename) {
  340. ZVAL_LONG(&tmp, 0);
  341. zend_update_property_ex(zend_ce_exception, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_LINE), &tmp);
  342. }
  343. }
  344. /* }}} */
  345. #define GET_PROPERTY(object, id) \
  346. zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 0, &rv)
  347. #define GET_PROPERTY_SILENT(object, id) \
  348. zend_read_property_ex(i_get_exception_base(Z_OBJ_P(object)), Z_OBJ_P(object), ZSTR_KNOWN(id), 1, &rv)
  349. /* {{{ Get the file in which the exception occurred */
  350. ZEND_METHOD(Exception, getFile)
  351. {
  352. zval *prop, rv;
  353. ZEND_PARSE_PARAMETERS_NONE();
  354. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_FILE);
  355. RETURN_STR(zval_get_string(prop));
  356. }
  357. /* }}} */
  358. /* {{{ Get the line in which the exception occurred */
  359. ZEND_METHOD(Exception, getLine)
  360. {
  361. zval *prop, rv;
  362. ZEND_PARSE_PARAMETERS_NONE();
  363. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_LINE);
  364. RETURN_LONG(zval_get_long(prop));
  365. }
  366. /* }}} */
  367. /* {{{ Get the exception message */
  368. ZEND_METHOD(Exception, getMessage)
  369. {
  370. zval *prop, rv;
  371. ZEND_PARSE_PARAMETERS_NONE();
  372. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_MESSAGE);
  373. RETURN_STR(zval_get_string(prop));
  374. }
  375. /* }}} */
  376. /* {{{ Get the exception code */
  377. ZEND_METHOD(Exception, getCode)
  378. {
  379. zval *prop, rv;
  380. ZEND_PARSE_PARAMETERS_NONE();
  381. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_CODE);
  382. ZVAL_DEREF(prop);
  383. ZVAL_COPY(return_value, prop);
  384. }
  385. /* }}} */
  386. /* {{{ Get the stack trace for the location in which the exception occurred */
  387. ZEND_METHOD(Exception, getTrace)
  388. {
  389. zval *prop, rv;
  390. ZEND_PARSE_PARAMETERS_NONE();
  391. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_TRACE);
  392. ZVAL_DEREF(prop);
  393. ZVAL_COPY(return_value, prop);
  394. }
  395. /* }}} */
  396. /* {{{ Get the exception severity */
  397. ZEND_METHOD(ErrorException, getSeverity)
  398. {
  399. zval *prop, rv;
  400. ZEND_PARSE_PARAMETERS_NONE();
  401. prop = GET_PROPERTY(ZEND_THIS, ZEND_STR_SEVERITY);
  402. ZVAL_DEREF(prop);
  403. ZVAL_COPY(return_value, prop);
  404. }
  405. /* }}} */
  406. #define TRACE_APPEND_KEY(key) do { \
  407. tmp = zend_hash_find(ht, key); \
  408. if (tmp) { \
  409. if (Z_TYPE_P(tmp) != IS_STRING) { \
  410. zend_error(E_WARNING, "Value for %s is not a string", \
  411. ZSTR_VAL(key)); \
  412. smart_str_appends(str, "[unknown]"); \
  413. } else { \
  414. smart_str_appends(str, Z_STRVAL_P(tmp)); \
  415. } \
  416. } \
  417. } while (0)
  418. static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
  419. {
  420. /* the trivial way would be to do
  421. * convert_to_string(arg);
  422. * append it and kill the now tmp arg.
  423. * but that could cause some E_NOTICE and also damn long lines.
  424. */
  425. ZVAL_DEREF(arg);
  426. if (Z_TYPE_P(arg) <= IS_STRING) {
  427. smart_str_append_scalar(str, arg, EG(exception_string_param_max_len));
  428. smart_str_appends(str, ", ");
  429. } else {
  430. switch (Z_TYPE_P(arg)) {
  431. case IS_RESOURCE:
  432. smart_str_appends(str, "Resource id #");
  433. smart_str_append_long(str, Z_RES_HANDLE_P(arg));
  434. smart_str_appends(str, ", ");
  435. break;
  436. case IS_ARRAY:
  437. smart_str_appends(str, "Array, ");
  438. break;
  439. case IS_OBJECT: {
  440. zend_string *class_name = Z_OBJ_HANDLER_P(arg, get_class_name)(Z_OBJ_P(arg));
  441. smart_str_appends(str, "Object(");
  442. smart_str_appends(str, ZSTR_VAL(class_name));
  443. smart_str_appends(str, "), ");
  444. zend_string_release_ex(class_name, 0);
  445. break;
  446. }
  447. }
  448. }
  449. }
  450. /* }}} */
  451. static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
  452. {
  453. zval *file, *tmp;
  454. smart_str_appendc(str, '#');
  455. smart_str_append_long(str, num);
  456. smart_str_appendc(str, ' ');
  457. file = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_FILE));
  458. if (file) {
  459. if (Z_TYPE_P(file) != IS_STRING) {
  460. zend_error(E_WARNING, "File name is not a string");
  461. smart_str_appends(str, "[unknown file]: ");
  462. } else{
  463. zend_long line = 0;
  464. tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_LINE));
  465. if (tmp) {
  466. if (Z_TYPE_P(tmp) == IS_LONG) {
  467. line = Z_LVAL_P(tmp);
  468. } else {
  469. zend_error(E_WARNING, "Line is not an int");
  470. }
  471. }
  472. smart_str_append(str, Z_STR_P(file));
  473. smart_str_appendc(str, '(');
  474. smart_str_append_long(str, line);
  475. smart_str_appends(str, "): ");
  476. }
  477. } else {
  478. smart_str_appends(str, "[internal function]: ");
  479. }
  480. TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_CLASS));
  481. TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_TYPE));
  482. TRACE_APPEND_KEY(ZSTR_KNOWN(ZEND_STR_FUNCTION));
  483. smart_str_appendc(str, '(');
  484. tmp = zend_hash_find_known_hash(ht, ZSTR_KNOWN(ZEND_STR_ARGS));
  485. if (tmp) {
  486. if (Z_TYPE_P(tmp) == IS_ARRAY) {
  487. size_t last_len = ZSTR_LEN(str->s);
  488. zend_string *name;
  489. zval *arg;
  490. ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(tmp), name, arg) {
  491. if (name) {
  492. smart_str_append(str, name);
  493. smart_str_appends(str, ": ");
  494. }
  495. _build_trace_args(arg, str);
  496. } ZEND_HASH_FOREACH_END();
  497. if (last_len != ZSTR_LEN(str->s)) {
  498. ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
  499. }
  500. } else {
  501. zend_error(E_WARNING, "args element is not an array");
  502. }
  503. }
  504. smart_str_appends(str, ")\n");
  505. }
  506. /* }}} */
  507. ZEND_API zend_string *zend_trace_to_string(HashTable *trace, bool include_main) {
  508. zend_ulong index;
  509. zval *frame;
  510. uint32_t num = 0;
  511. smart_str str = {0};
  512. ZEND_HASH_FOREACH_NUM_KEY_VAL(trace, index, frame) {
  513. if (Z_TYPE_P(frame) != IS_ARRAY) {
  514. zend_error(E_WARNING, "Expected array for frame " ZEND_ULONG_FMT, index);
  515. continue;
  516. }
  517. _build_trace_string(&str, Z_ARRVAL_P(frame), num++);
  518. } ZEND_HASH_FOREACH_END();
  519. if (include_main) {
  520. smart_str_appendc(&str, '#');
  521. smart_str_append_long(&str, num);
  522. smart_str_appends(&str, " {main}");
  523. }
  524. smart_str_0(&str);
  525. return str.s ? str.s : ZSTR_EMPTY_ALLOC();
  526. }
  527. /* {{{ Obtain the backtrace for the exception as a string (instead of an array) */
  528. ZEND_METHOD(Exception, getTraceAsString)
  529. {
  530. ZEND_PARSE_PARAMETERS_NONE();
  531. zval *object = ZEND_THIS;
  532. zend_class_entry *base_ce = i_get_exception_base(Z_OBJ_P(object));
  533. zval rv;
  534. zval *trace = zend_read_property_ex(base_ce, Z_OBJ_P(object), ZSTR_KNOWN(ZEND_STR_TRACE), 1, &rv);
  535. if (EG(exception)) {
  536. RETURN_THROWS();
  537. }
  538. /* Type should be guaranteed by property type. */
  539. ZEND_ASSERT(Z_TYPE_P(trace) == IS_ARRAY);
  540. RETURN_NEW_STR(zend_trace_to_string(Z_ARRVAL_P(trace), /* include_main */ true));
  541. }
  542. /* }}} */
  543. /* {{{ Return previous Throwable or NULL. */
  544. ZEND_METHOD(Exception, getPrevious)
  545. {
  546. zval rv;
  547. ZEND_PARSE_PARAMETERS_NONE();
  548. ZVAL_COPY(return_value, GET_PROPERTY_SILENT(ZEND_THIS, ZEND_STR_PREVIOUS));
  549. } /* }}} */
  550. /* {{{ Obtain the string representation of the Exception object */
  551. ZEND_METHOD(Exception, __toString)
  552. {
  553. zval trace, *exception;
  554. zend_class_entry *base_ce;
  555. zend_string *str;
  556. zend_fcall_info fci;
  557. zval rv, tmp;
  558. zend_string *fname;
  559. ZEND_PARSE_PARAMETERS_NONE();
  560. str = ZSTR_EMPTY_ALLOC();
  561. exception = ZEND_THIS;
  562. fname = zend_string_init("gettraceasstring", sizeof("gettraceasstring")-1, 0);
  563. while (exception && Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
  564. zend_string *prev_str = str;
  565. zend_string *message = zval_get_string(GET_PROPERTY(exception, ZEND_STR_MESSAGE));
  566. zend_string *file = zval_get_string(GET_PROPERTY(exception, ZEND_STR_FILE));
  567. zend_long line = zval_get_long(GET_PROPERTY(exception, ZEND_STR_LINE));
  568. fci.size = sizeof(fci);
  569. ZVAL_STR(&fci.function_name, fname);
  570. fci.object = Z_OBJ_P(exception);
  571. fci.retval = &trace;
  572. fci.param_count = 0;
  573. fci.params = NULL;
  574. fci.named_params = NULL;
  575. zend_call_function(&fci, NULL);
  576. if (Z_TYPE(trace) != IS_STRING) {
  577. zval_ptr_dtor(&trace);
  578. ZVAL_UNDEF(&trace);
  579. }
  580. if ((Z_OBJCE_P(exception) == zend_ce_type_error || Z_OBJCE_P(exception) == zend_ce_argument_count_error) && strstr(ZSTR_VAL(message), ", called in ")) {
  581. zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
  582. zend_string_release_ex(message, 0);
  583. message = real_message;
  584. }
  585. if (ZSTR_LEN(message) > 0) {
  586. str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
  587. "\nStack trace:\n%s%s%s",
  588. ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
  589. (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
  590. ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
  591. } else {
  592. str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
  593. "\nStack trace:\n%s%s%s",
  594. ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
  595. (Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
  596. ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
  597. }
  598. zend_string_release_ex(prev_str, 0);
  599. zend_string_release_ex(message, 0);
  600. zend_string_release_ex(file, 0);
  601. zval_ptr_dtor(&trace);
  602. Z_PROTECT_RECURSION_P(exception);
  603. exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
  604. if (exception && Z_TYPE_P(exception) == IS_OBJECT && Z_IS_RECURSIVE_P(exception)) {
  605. break;
  606. }
  607. }
  608. zend_string_release_ex(fname, 0);
  609. exception = ZEND_THIS;
  610. /* Reset apply counts */
  611. while (exception && Z_TYPE_P(exception) == IS_OBJECT && (base_ce = i_get_exception_base(Z_OBJ_P(exception))) && instanceof_function(Z_OBJCE_P(exception), base_ce)) {
  612. if (Z_IS_RECURSIVE_P(exception)) {
  613. Z_UNPROTECT_RECURSION_P(exception);
  614. } else {
  615. break;
  616. }
  617. exception = GET_PROPERTY(exception, ZEND_STR_PREVIOUS);
  618. }
  619. exception = ZEND_THIS;
  620. base_ce = i_get_exception_base(Z_OBJ_P(exception));
  621. /* We store the result in the private property string so we can access
  622. * the result in uncaught exception handlers without memleaks. */
  623. ZVAL_STR(&tmp, str);
  624. zend_update_property_ex(base_ce, Z_OBJ_P(exception), ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
  625. RETURN_STR(str);
  626. }
  627. /* }}} */
  628. void zend_register_default_exception(void) /* {{{ */
  629. {
  630. zend_ce_throwable = register_class_Throwable(zend_ce_stringable);
  631. zend_ce_throwable->interface_gets_implemented = zend_implement_throwable;
  632. memcpy(&default_exception_handlers, &std_object_handlers, sizeof(zend_object_handlers));
  633. default_exception_handlers.clone_obj = NULL;
  634. zend_ce_exception = register_class_Exception(zend_ce_throwable);
  635. zend_ce_exception->create_object = zend_default_exception_new;
  636. zend_ce_error_exception = register_class_ErrorException(zend_ce_exception);
  637. zend_ce_error_exception->create_object = zend_error_exception_new;
  638. /* Declared manually because it uses constant E_ERROR. */
  639. zval severity_default_value;
  640. ZVAL_LONG(&severity_default_value, E_ERROR);
  641. zend_declare_typed_property(zend_ce_error_exception, ZSTR_KNOWN(ZEND_STR_SEVERITY), &severity_default_value, ZEND_ACC_PROTECTED, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG));
  642. zend_ce_error = register_class_Error(zend_ce_throwable);
  643. zend_ce_error->create_object = zend_default_exception_new;
  644. zend_ce_compile_error = register_class_CompileError(zend_ce_error);
  645. zend_ce_compile_error->create_object = zend_default_exception_new;
  646. zend_ce_parse_error = register_class_ParseError(zend_ce_compile_error);
  647. zend_ce_parse_error->create_object = zend_default_exception_new;
  648. zend_ce_type_error = register_class_TypeError(zend_ce_error);
  649. zend_ce_type_error->create_object = zend_default_exception_new;
  650. zend_ce_argument_count_error = register_class_ArgumentCountError(zend_ce_type_error);
  651. zend_ce_argument_count_error->create_object = zend_default_exception_new;
  652. zend_ce_value_error = register_class_ValueError(zend_ce_error);
  653. zend_ce_value_error->create_object = zend_default_exception_new;
  654. zend_ce_arithmetic_error = register_class_ArithmeticError(zend_ce_error);
  655. zend_ce_arithmetic_error->create_object = zend_default_exception_new;
  656. zend_ce_division_by_zero_error = register_class_DivisionByZeroError(zend_ce_arithmetic_error);
  657. zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
  658. zend_ce_unhandled_match_error = register_class_UnhandledMatchError(zend_ce_error);
  659. zend_ce_unhandled_match_error->create_object = zend_default_exception_new;
  660. INIT_CLASS_ENTRY(zend_ce_unwind_exit, "UnwindExit", NULL);
  661. INIT_CLASS_ENTRY(zend_ce_graceful_exit, "GracefulExit", NULL);
  662. }
  663. /* }}} */
  664. /* {{{ Deprecated - Use zend_ce_exception directly instead */
  665. ZEND_API zend_class_entry *zend_exception_get_default(void)
  666. {
  667. return zend_ce_exception;
  668. }
  669. /* }}} */
  670. /* {{{ Deprecated - Use zend_ce_error_exception directly instead */
  671. ZEND_API zend_class_entry *zend_get_error_exception(void)
  672. {
  673. return zend_ce_error_exception;
  674. }
  675. /* }}} */
  676. static zend_object *zend_throw_exception_zstr(zend_class_entry *exception_ce, zend_string *message, zend_long code) /* {{{ */
  677. {
  678. zval ex, tmp;
  679. if (!exception_ce) {
  680. exception_ce = zend_ce_exception;
  681. }
  682. ZEND_ASSERT(instanceof_function(exception_ce, zend_ce_throwable)
  683. && "Exceptions must implement Throwable");
  684. object_init_ex(&ex, exception_ce);
  685. if (message) {
  686. ZVAL_STR(&tmp, message);
  687. zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_MESSAGE), &tmp);
  688. }
  689. if (code) {
  690. ZVAL_LONG(&tmp, code);
  691. zend_update_property_ex(exception_ce, Z_OBJ(ex), ZSTR_KNOWN(ZEND_STR_CODE), &tmp);
  692. }
  693. zend_throw_exception_internal(Z_OBJ(ex));
  694. return Z_OBJ(ex);
  695. }
  696. /* }}} */
  697. ZEND_API ZEND_COLD zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
  698. {
  699. zend_string *msg_str = message ? zend_string_init(message, strlen(message), 0) : NULL;
  700. zend_object *ex = zend_throw_exception_zstr(exception_ce, msg_str, code);
  701. if (msg_str) {
  702. zend_string_release(msg_str);
  703. }
  704. return ex;
  705. }
  706. /* }}} */
  707. ZEND_API ZEND_COLD zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
  708. {
  709. va_list arg;
  710. char *message;
  711. zend_object *obj;
  712. va_start(arg, format);
  713. zend_vspprintf(&message, 0, format, arg);
  714. va_end(arg);
  715. obj = zend_throw_exception(exception_ce, message, code);
  716. efree(message);
  717. return obj;
  718. }
  719. /* }}} */
  720. ZEND_API ZEND_COLD zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, zend_string *message, zend_long code, int severity) /* {{{ */
  721. {
  722. zend_object *obj = zend_throw_exception_zstr(exception_ce, message, code);
  723. if (exception_ce && instanceof_function(exception_ce, zend_ce_error_exception)) {
  724. zval tmp;
  725. ZVAL_LONG(&tmp, severity);
  726. zend_update_property_ex(zend_ce_error_exception, obj, ZSTR_KNOWN(ZEND_STR_SEVERITY), &tmp);
  727. }
  728. return obj;
  729. }
  730. /* }}} */
  731. static void zend_error_va(int type, zend_string *file, uint32_t lineno, const char *format, ...) /* {{{ */
  732. {
  733. va_list args;
  734. va_start(args, format);
  735. zend_string *message = zend_vstrpprintf(0, format, args);
  736. zend_observer_error_notify(type, file, lineno, message);
  737. zend_error_cb(type, file, lineno, message);
  738. zend_string_release(message);
  739. va_end(args);
  740. }
  741. /* }}} */
  742. /* This function doesn't return if it uses E_ERROR */
  743. ZEND_API ZEND_COLD zend_result zend_exception_error(zend_object *ex, int severity) /* {{{ */
  744. {
  745. zval exception, rv;
  746. zend_class_entry *ce_exception;
  747. zend_result result = FAILURE;
  748. ZVAL_OBJ(&exception, ex);
  749. ce_exception = ex->ce;
  750. EG(exception) = NULL;
  751. if (ce_exception == zend_ce_parse_error || ce_exception == zend_ce_compile_error) {
  752. zend_string *message = zval_get_string(GET_PROPERTY(&exception, ZEND_STR_MESSAGE));
  753. zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
  754. zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
  755. int type = (ce_exception == zend_ce_parse_error ? E_PARSE : E_COMPILE_ERROR) | E_DONT_BAIL;
  756. zend_observer_error_notify(type, file, line, message);
  757. zend_error_cb(type, file, line, message);
  758. zend_string_release_ex(file, 0);
  759. zend_string_release_ex(message, 0);
  760. } else if (instanceof_function(ce_exception, zend_ce_throwable)) {
  761. zval tmp;
  762. zend_string *str, *file = NULL;
  763. zend_long line = 0;
  764. zend_call_known_instance_method_with_0_params(ex->ce->__tostring, ex, &tmp);
  765. if (!EG(exception)) {
  766. if (Z_TYPE(tmp) != IS_STRING) {
  767. zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
  768. } else {
  769. zend_update_property_ex(i_get_exception_base(ex), ex, ZSTR_KNOWN(ZEND_STR_STRING), &tmp);
  770. }
  771. }
  772. zval_ptr_dtor(&tmp);
  773. if (EG(exception)) {
  774. zval zv;
  775. ZVAL_OBJ(&zv, EG(exception));
  776. /* do the best we can to inform about the inner exception */
  777. if (instanceof_function(ce_exception, zend_ce_exception) || instanceof_function(ce_exception, zend_ce_error)) {
  778. file = zval_get_string(GET_PROPERTY_SILENT(&zv, ZEND_STR_FILE));
  779. line = zval_get_long(GET_PROPERTY_SILENT(&zv, ZEND_STR_LINE));
  780. }
  781. zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
  782. "Uncaught %s in exception handling during call to %s::__toString()",
  783. ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
  784. if (file) {
  785. zend_string_release_ex(file, 0);
  786. }
  787. }
  788. str = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_STRING));
  789. file = zval_get_string(GET_PROPERTY_SILENT(&exception, ZEND_STR_FILE));
  790. line = zval_get_long(GET_PROPERTY_SILENT(&exception, ZEND_STR_LINE));
  791. zend_error_va(severity | E_DONT_BAIL,
  792. (file && ZSTR_LEN(file) > 0) ? file : NULL, line,
  793. "Uncaught %s\n thrown", ZSTR_VAL(str));
  794. zend_string_release_ex(str, 0);
  795. zend_string_release_ex(file, 0);
  796. } else if (ce_exception == &zend_ce_unwind_exit || ce_exception == &zend_ce_graceful_exit) {
  797. /* We successfully unwound, nothing more to do.
  798. * We still return FAILURE in this case, as further execution should still be aborted. */
  799. } else {
  800. zend_error(severity, "Uncaught exception %s", ZSTR_VAL(ce_exception->name));
  801. }
  802. OBJ_RELEASE(ex);
  803. return result;
  804. }
  805. /* }}} */
  806. ZEND_NORETURN void zend_exception_uncaught_error(const char *format, ...) {
  807. va_list va;
  808. va_start(va, format);
  809. zend_string *prefix = zend_vstrpprintf(0, format, va);
  810. va_end(va);
  811. ZEND_ASSERT(EG(exception));
  812. zval exception_zv;
  813. ZVAL_OBJ_COPY(&exception_zv, EG(exception));
  814. zend_clear_exception();
  815. zend_string *exception_str = zval_get_string(&exception_zv);
  816. zend_error_noreturn(E_ERROR,
  817. "%s: Uncaught %s", ZSTR_VAL(prefix), ZSTR_VAL(exception_str));
  818. }
  819. ZEND_API ZEND_COLD void zend_throw_exception_object(zval *exception) /* {{{ */
  820. {
  821. if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
  822. zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
  823. }
  824. zend_class_entry *exception_ce = Z_OBJCE_P(exception);
  825. if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
  826. zend_throw_error(NULL, "Cannot throw objects that do not implement Throwable");
  827. zval_ptr_dtor(exception);
  828. return;
  829. }
  830. zend_throw_exception_internal(Z_OBJ_P(exception));
  831. }
  832. /* }}} */
  833. ZEND_API ZEND_COLD zend_object *zend_create_unwind_exit(void)
  834. {
  835. return zend_objects_new(&zend_ce_unwind_exit);
  836. }
  837. ZEND_API ZEND_COLD zend_object *zend_create_graceful_exit(void)
  838. {
  839. return zend_objects_new(&zend_ce_graceful_exit);
  840. }
  841. ZEND_API ZEND_COLD void zend_throw_unwind_exit(void)
  842. {
  843. ZEND_ASSERT(!EG(exception));
  844. EG(exception) = zend_create_unwind_exit();
  845. EG(opline_before_exception) = EG(current_execute_data)->opline;
  846. EG(current_execute_data)->opline = EG(exception_op);
  847. }
  848. ZEND_API ZEND_COLD void zend_throw_graceful_exit(void)
  849. {
  850. ZEND_ASSERT(!EG(exception));
  851. EG(exception) = zend_create_graceful_exit();
  852. EG(opline_before_exception) = EG(current_execute_data)->opline;
  853. EG(current_execute_data)->opline = EG(exception_op);
  854. }
  855. ZEND_API bool zend_is_unwind_exit(const zend_object *ex)
  856. {
  857. return ex->ce == &zend_ce_unwind_exit;
  858. }
  859. ZEND_API bool zend_is_graceful_exit(const zend_object *ex)
  860. {
  861. return ex->ce == &zend_ce_graceful_exit;
  862. }