1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
|
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) 1998-2015 Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@zend.com> |
| Marcus Boerger <helly@php.net> |
| Sterling Hughes <sterling@php.net> |
| Zeev Suraski <zeev@zend.com> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#include "zend.h"
#include "zend_API.h"
#include "zend_builtin_functions.h"
#include "zend_interfaces.h"
#include "zend_exceptions.h"
#include "zend_vm.h"
#include "zend_dtrace.h"
#include "zend_smart_str.h"
ZEND_API zend_class_entry *zend_ce_throwable;
ZEND_API zend_class_entry *zend_exception_ce;
ZEND_API zend_class_entry *zend_error_exception_ce;
ZEND_API zend_class_entry *zend_error_ce;
ZEND_API zend_class_entry *zend_parse_error_ce;
ZEND_API zend_class_entry *zend_type_error_ce;
ZEND_API zend_class_entry *zend_ce_arithmetic_error;
ZEND_API zend_class_entry *zend_ce_division_by_zero_error;
ZEND_API void (*zend_throw_exception_hook)(zval *ex);
static zend_object_handlers default_exception_handlers;
/* {{{ zend_implement_throwable */
static int zend_implement_throwable(zend_class_entry *interface, zend_class_entry *class_type)
{
if (instanceof_function(class_type, zend_exception_ce) || instanceof_function(class_type, zend_error_ce)) {
return SUCCESS;
}
zend_error_noreturn(E_ERROR, "Class %s cannot implement interface %s, extend %s or %s instead",
ZSTR_VAL(class_type->name),
ZSTR_VAL(interface->name),
ZSTR_VAL(zend_exception_ce->name),
ZSTR_VAL(zend_error_ce->name));
return FAILURE;
}
/* }}} */
static inline zend_class_entry *i_get_exception_base(zval *object)
{
return instanceof_function(Z_OBJCE_P(object), zend_exception_ce) ? zend_exception_ce : zend_error_ce;
}
ZEND_API zend_class_entry *zend_get_exception_base(zval *object)
{
return i_get_exception_base(object);
}
void zend_exception_set_previous(zend_object *exception, zend_object *add_previous)
{
zval tmp, *previous, zv, *pzv, rv;
zend_class_entry *base_ce;
if (exception == add_previous || !add_previous || !exception) {
return;
}
ZVAL_OBJ(&tmp, add_previous);
if (!instanceof_function(Z_OBJCE(tmp), zend_ce_throwable)) {
zend_error_noreturn(E_CORE_ERROR, "Previous exception must implement Throwable");
return;
}
ZVAL_OBJ(&zv, exception);
pzv = &zv;
do {
base_ce = i_get_exception_base(pzv);
previous = zend_read_property(base_ce, pzv, "previous", sizeof("previous")-1, 1, &rv);
if (Z_TYPE_P(previous) == IS_NULL) {
zend_update_property(base_ce, pzv, "previous", sizeof("previous")-1, &tmp);
GC_REFCOUNT(add_previous)--;
return;
}
pzv = previous;
} while (pzv && Z_OBJ_P(pzv) != add_previous);
}
void zend_exception_save(void) /* {{{ */
{
if (EG(prev_exception)) {
zend_exception_set_previous(EG(exception), EG(prev_exception));
}
if (EG(exception)) {
EG(prev_exception) = EG(exception);
}
EG(exception) = NULL;
}
/* }}} */
void zend_exception_restore(void) /* {{{ */
{
if (EG(prev_exception)) {
if (EG(exception)) {
zend_exception_set_previous(EG(exception), EG(prev_exception));
} else {
EG(exception) = EG(prev_exception);
}
EG(prev_exception) = NULL;
}
}
/* }}} */
ZEND_API void zend_throw_exception_internal(zval *exception) /* {{{ */
{
#ifdef HAVE_DTRACE
if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
if (exception != NULL) {
DTRACE_EXCEPTION_THROWN(ZSTR_VAL(Z_OBJ_P(exception)->ce->name));
} else {
DTRACE_EXCEPTION_THROWN(NULL);
}
}
#endif /* HAVE_DTRACE */
if (exception != NULL) {
zend_object *previous = EG(exception);
zend_exception_set_previous(Z_OBJ_P(exception), EG(exception));
EG(exception) = Z_OBJ_P(exception);
if (previous) {
return;
}
}
if (!EG(current_execute_data)) {
if (exception && Z_OBJCE_P(exception) == zend_parse_error_ce) {
return;
}
if(EG(exception)) {
zend_exception_error(EG(exception), E_ERROR);
}
zend_error_noreturn(E_CORE_ERROR, "Exception thrown without a stack frame");
}
if (zend_throw_exception_hook) {
zend_throw_exception_hook(exception);
}
if (!EG(current_execute_data)->func ||
!ZEND_USER_CODE(EG(current_execute_data)->func->common.type) ||
EG(current_execute_data)->opline->opcode == ZEND_HANDLE_EXCEPTION) {
/* no need to rethrow the exception */
return;
}
EG(opline_before_exception) = EG(current_execute_data)->opline;
EG(current_execute_data)->opline = EG(exception_op);
}
/* }}} */
ZEND_API void zend_clear_exception(void) /* {{{ */
{
if (EG(prev_exception)) {
OBJ_RELEASE(EG(prev_exception));
EG(prev_exception) = NULL;
}
if (!EG(exception)) {
return;
}
OBJ_RELEASE(EG(exception));
EG(exception) = NULL;
EG(current_execute_data)->opline = EG(opline_before_exception);
#if ZEND_DEBUG
EG(opline_before_exception) = NULL;
#endif
}
/* }}} */
static zend_object *zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces) /* {{{ */
{
zval obj;
zend_object *object;
zval trace;
zend_class_entry *base_ce;
zend_string *filename;
Z_OBJ(obj) = object = zend_objects_new(class_type);
Z_OBJ_HT(obj) = &default_exception_handlers;
object_properties_init(object, class_type);
if (EG(current_execute_data)) {
zend_fetch_debug_backtrace(&trace, skip_top_traces, 0, 0);
} else {
array_init(&trace);
}
Z_SET_REFCOUNT(trace, 0);
base_ce = i_get_exception_base(&obj);
if (EXPECTED(class_type != zend_parse_error_ce || !(filename = zend_get_compiled_filename()))) {
zend_update_property_string(base_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename());
zend_update_property_long(base_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno());
} else {
zend_update_property_string(base_ce, &obj, "file", sizeof("file")-1, ZSTR_VAL(filename));
zend_update_property_long(base_ce, &obj, "line", sizeof("line")-1, zend_get_compiled_lineno());
}
zend_update_property(base_ce, &obj, "trace", sizeof("trace")-1, &trace);
return object;
}
/* }}} */
static zend_object *zend_default_exception_new(zend_class_entry *class_type) /* {{{ */
{
return zend_default_exception_new_ex(class_type, 0);
}
/* }}} */
static zend_object *zend_error_exception_new(zend_class_entry *class_type) /* {{{ */
{
return zend_default_exception_new_ex(class_type, 2);
}
/* }}} */
/* {{{ proto Exception|Error Exception|Error::__clone()
Clone the exception object */
ZEND_METHOD(exception, __clone)
{
/* Should never be executable */
zend_throw_exception(NULL, "Cannot clone object using __clone()", 0);
}
/* }}} */
/* {{{ proto Exception|Error::__construct(string message, int code [, Throwable previous])
Exception constructor */
ZEND_METHOD(exception, __construct)
{
zend_string *message = NULL;
zend_long code = 0;
zval *object, *previous = NULL;
zend_class_entry *base_ce;
int argc = ZEND_NUM_ARGS();
object = getThis();
base_ce = i_get_exception_base(object);
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|SlO!", &message, &code, &previous, zend_ce_throwable) == FAILURE) {
zend_class_entry *ce;
if (execute_data->called_scope) {
ce = execute_data->called_scope;
} else {
ce = base_ce;
}
zend_error(E_EXCEPTION | E_ERROR, "Wrong parameters for %s([string $message [, long $code [, Throwable $previous = NULL]]])", ZSTR_VAL(ce->name));
return;
}
if (message) {
zend_update_property_str(base_ce, object, "message", sizeof("message")-1, message);
}
if (code) {
zend_update_property_long(base_ce, object, "code", sizeof("code")-1, code);
}
if (previous) {
zend_update_property(base_ce, object, "previous", sizeof("previous")-1, previous);
}
}
/* }}} */
/* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Throwable previous]]])
ErrorException constructor */
ZEND_METHOD(error_exception, __construct)
{
char *message = NULL, *filename = NULL;
zend_long code = 0, severity = E_ERROR, lineno;
zval *object, *previous = NULL;
int argc = ZEND_NUM_ARGS();
size_t message_len, filename_len;
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc, "|sllslO!", &message, &message_len, &code, &severity, &filename, &filename_len, &lineno, &previous, zend_ce_throwable) == FAILURE) {
zend_class_entry *ce;
if (execute_data->called_scope) {
ce = execute_data->called_scope;
} else {
ce = zend_error_exception_ce;
}
zend_error(E_EXCEPTION | E_ERROR, "Wrong parameters for %s([string $message [, long $code, [ long $severity, [ string $filename, [ long $lineno [, Throwable $previous = NULL]]]]]])", ZSTR_VAL(ce->name));
return;
}
object = getThis();
if (message) {
zend_update_property_string(zend_exception_ce, object, "message", sizeof("message")-1, message);
}
if (code) {
zend_update_property_long(zend_exception_ce, object, "code", sizeof("code")-1, code);
}
if (previous) {
zend_update_property(zend_exception_ce, object, "previous", sizeof("previous")-1, previous);
}
zend_update_property_long(zend_error_exception_ce, object, "severity", sizeof("severity")-1, severity);
if (argc >= 4) {
zend_update_property_string(zend_exception_ce, object, "file", sizeof("file")-1, filename);
if (argc < 5) {
lineno = 0; /* invalidate lineno */
}
zend_update_property_long(zend_exception_ce, object, "line", sizeof("line")-1, lineno);
}
}
/* }}} */
#define DEFAULT_0_PARAMS \
if (zend_parse_parameters_none() == FAILURE) { \
return; \
}
#define GET_PROPERTY(object, name) \
zend_read_property(i_get_exception_base(object), (object), name, sizeof(name) - 1, 0, &rv)
#define GET_PROPERTY_SILENT(object, name) \
zend_read_property(i_get_exception_base(object), (object), name, sizeof(name) - 1, 1, &rv)
/* {{{ proto string Exception|Error::getFile()
Get the file in which the exception occurred */
ZEND_METHOD(exception, getFile)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "file"));
}
/* }}} */
/* {{{ proto int Exception|Error::getLine()
Get the line in which the exception occurred */
ZEND_METHOD(exception, getLine)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "line"));
}
/* }}} */
/* {{{ proto string Exception|Error::getMessage()
Get the exception message */
ZEND_METHOD(exception, getMessage)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "message"));
}
/* }}} */
/* {{{ proto int Exception|Error::getCode()
Get the exception code */
ZEND_METHOD(exception, getCode)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "code"));
}
/* }}} */
/* {{{ proto array Exception|Error::getTrace()
Get the stack trace for the location in which the exception occurred */
ZEND_METHOD(exception, getTrace)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "trace"));
}
/* }}} */
/* {{{ proto int ErrorException::getSeverity()
Get the exception severity */
ZEND_METHOD(error_exception, getSeverity)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY(getThis(), "severity"));
}
/* }}} */
#define TRACE_APPEND_KEY(key) do { \
tmp = zend_hash_str_find(ht, key, sizeof(key)-1); \
if (tmp) { \
if (Z_TYPE_P(tmp) != IS_STRING) { \
zend_error(E_WARNING, "Value for %s is no string", key); \
smart_str_appends(str, "[unknown]"); \
} else { \
smart_str_append(str, Z_STR_P(tmp)); \
} \
} \
} while (0)
/* Windows uses VK_ESCAPE instead of \e */
#ifndef VK_ESCAPE
#define VK_ESCAPE '\e'
#endif
static size_t compute_escaped_string_len(const char *s, size_t l) {
size_t i, len = l;
for (i = 0; i < l; ++i) {
char c = s[i];
if (c == '\n' || c == '\r' || c == '\t' ||
c == '\f' || c == '\v' || c == '\\' || c == VK_ESCAPE) {
len += 1;
} else if (c < 32 || c > 126) {
len += 3;
}
}
return len;
}
static void smart_str_append_escaped(smart_str *str, const char *s, size_t l) {
char *res;
size_t i, len = compute_escaped_string_len(s, l);
smart_str_alloc(str, len, 0);
res = &ZSTR_VAL(str->s)[ZSTR_LEN(str->s)];
ZSTR_LEN(str->s) += len;
for (i = 0; i < l; ++i) {
unsigned char c = s[i];
if (c < 32 || c == '\\' || c > 126) {
*res++ = '\\';
switch (c) {
case '\n': *res++ = 'n'; break;
case '\r': *res++ = 'r'; break;
case '\t': *res++ = 't'; break;
case '\f': *res++ = 'f'; break;
case '\v': *res++ = 'v'; break;
case '\\': *res++ = '\\'; break;
case VK_ESCAPE: *res++ = 'e'; break;
default:
*res++ = 'x';
if ((c >> 4) < 10) {
*res++ = (c >> 4) + '0';
} else {
*res++ = (c >> 4) + 'A' - 10;
}
if ((c & 0xf) < 10) {
*res++ = (c & 0xf) + '0';
} else {
*res++ = (c & 0xf) + 'A' - 10;
}
}
} else {
*res++ = c;
}
}
}
static void _build_trace_args(zval *arg, smart_str *str) /* {{{ */
{
/* the trivial way would be to do
* convert_to_string_ex(arg);
* append it and kill the now tmp arg.
* but that could cause some E_NOTICE and also damn long lines.
*/
ZVAL_DEREF(arg);
switch (Z_TYPE_P(arg)) {
case IS_NULL:
smart_str_appends(str, "NULL, ");
break;
case IS_STRING:
smart_str_appendc(str, '\'');
smart_str_append_escaped(str, Z_STRVAL_P(arg), MIN(Z_STRLEN_P(arg), 15));
if (Z_STRLEN_P(arg) > 15) {
smart_str_appends(str, "...', ");
} else {
smart_str_appends(str, "', ");
}
break;
case IS_FALSE:
smart_str_appends(str, "false, ");
break;
case IS_TRUE:
smart_str_appends(str, "true, ");
break;
case IS_RESOURCE:
smart_str_appends(str, "Resource id #");
smart_str_append_long(str, Z_RES_HANDLE_P(arg));
smart_str_appends(str, ", ");
break;
case IS_LONG:
smart_str_append_long(str, Z_LVAL_P(arg));
smart_str_appends(str, ", ");
break;
case IS_DOUBLE: {
double dval = Z_DVAL_P(arg);
char *s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
int l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval); /* SAFE */
smart_str_appendl(str, s_tmp, l_tmp);
smart_str_appends(str, ", ");
efree(s_tmp);
break;
}
case IS_ARRAY:
smart_str_appends(str, "Array, ");
break;
case IS_OBJECT:
smart_str_appends(str, "Object(");
smart_str_append(str, Z_OBJCE_P(arg)->name);
smart_str_appends(str, "), ");
break;
}
}
/* }}} */
static void _build_trace_string(smart_str *str, HashTable *ht, uint32_t num) /* {{{ */
{
zval *file, *tmp;
smart_str_appendc(str, '#');
smart_str_append_long(str, num);
smart_str_appendc(str, ' ');
file = zend_hash_str_find(ht, "file", sizeof("file")-1);
if (file) {
if (Z_TYPE_P(file) != IS_STRING) {
zend_error(E_WARNING, "Function name is no string");
smart_str_appends(str, "[unknown function]");
} else{
zend_long line;
tmp = zend_hash_str_find(ht, "line", sizeof("line")-1);
if (tmp) {
if (Z_TYPE_P(tmp) == IS_LONG) {
line = Z_LVAL_P(tmp);
} else {
zend_error(E_WARNING, "Line is no long");
line = 0;
}
} else {
line = 0;
}
smart_str_append(str, Z_STR_P(file));
smart_str_appendc(str, '(');
smart_str_append_long(str, line);
smart_str_appends(str, "): ");
}
} else {
smart_str_appends(str, "[internal function]: ");
}
TRACE_APPEND_KEY("class");
TRACE_APPEND_KEY("type");
TRACE_APPEND_KEY("function");
smart_str_appendc(str, '(');
tmp = zend_hash_str_find(ht, "args", sizeof("args")-1);
if (tmp) {
if (Z_TYPE_P(tmp) == IS_ARRAY) {
size_t last_len = ZSTR_LEN(str->s);
zval *arg;
ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(tmp), arg) {
_build_trace_args(arg, str);
} ZEND_HASH_FOREACH_END();
if (last_len != ZSTR_LEN(str->s)) {
ZSTR_LEN(str->s) -= 2; /* remove last ', ' */
}
} else {
zend_error(E_WARNING, "args element is no array");
}
}
smart_str_appends(str, ")\n");
}
/* }}} */
/* {{{ proto string Exception|Error::getTraceAsString()
Obtain the backtrace for the exception as a string (instead of an array) */
ZEND_METHOD(exception, getTraceAsString)
{
zval *trace, *frame, rv;
zend_ulong index;
zval *object;
zend_class_entry *base_ce;
smart_str str = {0};
uint32_t num = 0;
DEFAULT_0_PARAMS;
object = getThis();
base_ce = i_get_exception_base(object);
trace = zend_read_property(base_ce, object, "trace", sizeof("trace")-1, 1, &rv);
if (Z_TYPE_P(trace) != IS_ARRAY) {
RETURN_FALSE;
}
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(trace), index, frame) {
if (Z_TYPE_P(frame) != IS_ARRAY) {
zend_error(E_WARNING, "Expected array for frame %pu", index);
continue;
}
_build_trace_string(&str, Z_ARRVAL_P(frame), num++);
} ZEND_HASH_FOREACH_END();
smart_str_appendc(&str, '#');
smart_str_append_long(&str, num);
smart_str_appends(&str, " {main}");
smart_str_0(&str);
RETURN_NEW_STR(str.s);
}
/* }}} */
/* {{{ proto Throwable Exception|Error::getPrevious()
Return previous Throwable or NULL. */
ZEND_METHOD(exception, getPrevious)
{
zval rv;
DEFAULT_0_PARAMS;
ZVAL_COPY(return_value, GET_PROPERTY_SILENT(getThis(), "previous"));
} /* }}} */
size_t zend_spprintf(char **message, size_t max_len, const char *format, ...) /* {{{ */
{
va_list arg;
size_t len;
va_start(arg, format);
len = zend_vspprintf(message, max_len, format, arg);
va_end(arg);
return len;
}
/* }}} */
zend_string *zend_strpprintf(size_t max_len, const char *format, ...) /* {{{ */
{
va_list arg;
zend_string *str;
va_start(arg, format);
str = zend_vstrpprintf(max_len, format, arg);
va_end(arg);
return str;
}
/* }}} */
/* {{{ proto string Exception|Error::__toString()
Obtain the string representation of the Exception object */
ZEND_METHOD(exception, __toString)
{
zval trace, *exception;
zend_class_entry *base_ce;
zend_string *str;
zend_fcall_info fci;
zval fname, rv;
DEFAULT_0_PARAMS;
str = ZSTR_EMPTY_ALLOC();
exception = getThis();
ZVAL_STRINGL(&fname, "gettraceasstring", sizeof("gettraceasstring")-1);
while (exception && Z_TYPE_P(exception) == IS_OBJECT) {
zend_string *prev_str = str;
zend_string *message = zval_get_string(GET_PROPERTY(exception, "message"));
zend_string *file = zval_get_string(GET_PROPERTY(exception, "file"));
zend_long line = zval_get_long(GET_PROPERTY(exception, "line"));
fci.size = sizeof(fci);
fci.function_table = &Z_OBJCE_P(exception)->function_table;
ZVAL_COPY_VALUE(&fci.function_name, &fname);
fci.symbol_table = NULL;
fci.object = Z_OBJ_P(exception);
fci.retval = &trace;
fci.param_count = 0;
fci.params = NULL;
fci.no_separation = 1;
zend_call_function(&fci, NULL);
if (Z_TYPE(trace) != IS_STRING) {
zval_ptr_dtor(&trace);
ZVAL_UNDEF(&trace);
}
if (Z_OBJCE_P(exception) == zend_type_error_ce && strstr(ZSTR_VAL(message), ", called in ")) {
zend_string *real_message = zend_strpprintf(0, "%s and defined", ZSTR_VAL(message));
zend_string_release(message);
message = real_message;
}
if (ZSTR_LEN(message) > 0) {
str = zend_strpprintf(0, "%s: %s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(message), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
} else {
str = zend_strpprintf(0, "%s in %s:" ZEND_LONG_FMT
"\nStack trace:\n%s%s%s",
ZSTR_VAL(Z_OBJCE_P(exception)->name), ZSTR_VAL(file), line,
(Z_TYPE(trace) == IS_STRING && Z_STRLEN(trace)) ? Z_STRVAL(trace) : "#0 {main}\n",
ZSTR_LEN(prev_str) ? "\n\nNext " : "", ZSTR_VAL(prev_str));
}
zend_string_release(prev_str);
zend_string_release(message);
zend_string_release(file);
zval_ptr_dtor(&trace);
exception = GET_PROPERTY(exception, "previous");
}
zval_dtor(&fname);
exception = getThis();
base_ce = i_get_exception_base(exception);
/* We store the result in the private property string so we can access
* the result in uncaught exception handlers without memleaks. */
zend_update_property_str(base_ce, exception, "string", sizeof("string")-1, str);
RETURN_STR(str);
}
/* }}} */
/** {{{ Throwable method definition */
const zend_function_entry zend_funcs_throwable[] = {
ZEND_ABSTRACT_ME(throwable, getMessage, NULL)
ZEND_ABSTRACT_ME(throwable, getCode, NULL)
ZEND_ABSTRACT_ME(throwable, getFile, NULL)
ZEND_ABSTRACT_ME(throwable, getLine, NULL)
ZEND_ABSTRACT_ME(throwable, getTrace, NULL)
ZEND_ABSTRACT_ME(throwable, getPrevious, NULL)
ZEND_ABSTRACT_ME(throwable, getTraceAsString, NULL)
ZEND_ABSTRACT_ME(throwable, __toString, NULL)
ZEND_FE_END
};
/* }}} */
/* {{{ internal structs */
/* All functions that may be used in uncaught exception handlers must be final
* and must not throw exceptions. Otherwise we would need a facility to handle
* such exceptions in that handler.
* Also all getXY() methods are final because thy serve as read only access to
* their corresponding properties, no more, no less. If after all you need to
* override somthing then it is method __toString().
* And never try to change the state of exceptions and never implement anything
* that gives the user anything to accomplish this.
*/
ZEND_BEGIN_ARG_INFO_EX(arginfo_exception___construct, 0, 0, 0)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, code)
ZEND_ARG_INFO(0, previous)
ZEND_END_ARG_INFO()
static const zend_function_entry default_exception_functions[] = {
ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
ZEND_ME(exception, __construct, arginfo_exception___construct, ZEND_ACC_PUBLIC)
ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getPrevious, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_ME(exception, __toString, NULL, 0)
ZEND_FE_END
};
ZEND_BEGIN_ARG_INFO_EX(arginfo_error_exception___construct, 0, 0, 0)
ZEND_ARG_INFO(0, message)
ZEND_ARG_INFO(0, code)
ZEND_ARG_INFO(0, severity)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, lineno)
ZEND_ARG_INFO(0, previous)
ZEND_END_ARG_INFO()
static const zend_function_entry error_exception_functions[] = {
ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, ZEND_ACC_PUBLIC)
ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
ZEND_FE_END
};
/* }}} */
void zend_register_default_exception(void) /* {{{ */
{
zend_class_entry ce;
REGISTER_MAGIC_INTERFACE(throwable, Throwable);
memcpy(&default_exception_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
default_exception_handlers.clone_obj = NULL;
INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
zend_exception_ce = zend_register_internal_class_ex(&ce, NULL);
zend_exception_ce->create_object = zend_default_exception_new;
zend_class_implements(zend_exception_ce, 1, zend_ce_throwable);
zend_declare_property_string(zend_exception_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
zend_declare_property_string(zend_exception_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
zend_declare_property_long(zend_exception_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
zend_declare_property_null(zend_exception_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
zend_error_exception_ce = zend_register_internal_class_ex(&ce, zend_exception_ce);
zend_error_exception_ce->create_object = zend_error_exception_new;
zend_declare_property_long(zend_error_exception_ce, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED);
INIT_CLASS_ENTRY(ce, "Error", default_exception_functions);
zend_error_ce = zend_register_internal_class_ex(&ce, NULL);
zend_error_ce->create_object = zend_default_exception_new;
zend_class_implements(zend_error_ce, 1, zend_ce_throwable);
zend_declare_property_string(zend_error_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED);
zend_declare_property_string(zend_error_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE);
zend_declare_property_long(zend_error_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_error_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_error_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED);
zend_declare_property_null(zend_error_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE);
zend_declare_property_null(zend_error_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE);
INIT_CLASS_ENTRY(ce, "ParseError", NULL);
zend_parse_error_ce = zend_register_internal_class_ex(&ce, zend_error_ce);
zend_parse_error_ce->create_object = zend_default_exception_new;
INIT_CLASS_ENTRY(ce, "TypeError", NULL);
zend_type_error_ce = zend_register_internal_class_ex(&ce, zend_error_ce);
zend_type_error_ce->create_object = zend_default_exception_new;
INIT_CLASS_ENTRY(ce, "ArithmeticError", NULL);
zend_ce_arithmetic_error = zend_register_internal_class_ex(&ce, zend_error_ce);
zend_ce_arithmetic_error->create_object = zend_default_exception_new;
INIT_CLASS_ENTRY(ce, "DivisionByZeroError", NULL);
zend_ce_division_by_zero_error = zend_register_internal_class_ex(&ce, zend_ce_arithmetic_error);
zend_ce_division_by_zero_error->create_object = zend_default_exception_new;
}
/* }}} */
/* {{{ Deprecated - Use zend_exception_ce directly instead */
ZEND_API zend_class_entry *zend_exception_get_default(void)
{
return zend_exception_ce;
}
/* }}} */
/* {{{ Deprecated - Use zend_error_exception_ce directly instead */
ZEND_API zend_class_entry *zend_get_error_exception(void)
{
return zend_error_exception_ce;
}
/* }}} */
ZEND_API zend_object *zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code) /* {{{ */
{
zval ex;
if (exception_ce) {
if (!instanceof_function(exception_ce, zend_ce_throwable)) {
zend_error(E_NOTICE, "Exceptions must implement Throwable");
exception_ce = zend_exception_ce;
}
} else {
exception_ce = zend_exception_ce;
}
object_init_ex(&ex, exception_ce);
if (message) {
zend_update_property_string(exception_ce, &ex, "message", sizeof("message")-1, message);
}
if (code) {
zend_update_property_long(exception_ce, &ex, "code", sizeof("code")-1, code);
}
zend_throw_exception_internal(&ex);
return Z_OBJ(ex);
}
/* }}} */
ZEND_API zend_object *zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format, ...) /* {{{ */
{
va_list arg;
char *message;
zend_object *obj;
va_start(arg, format);
zend_vspprintf(&message, 0, format, arg);
va_end(arg);
obj = zend_throw_exception(exception_ce, message, code);
efree(message);
return obj;
}
/* }}} */
ZEND_API zend_object *zend_throw_error_exception(zend_class_entry *exception_ce, const char *message, zend_long code, int severity) /* {{{ */
{
zval ex;
zend_object *obj = zend_throw_exception(exception_ce, message, code);
ZVAL_OBJ(&ex, obj);
zend_update_property_long(zend_error_exception_ce, &ex, "severity", sizeof("severity")-1, severity);
return obj;
}
/* }}} */
static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) /* {{{ */
{
va_list args;
va_start(args, format);
zend_error_cb(type, file, lineno, format, args);
va_end(args);
}
/* }}} */
static void zend_error_helper(int type, const char *filename, const uint lineno, const char *format, ...)
{
va_list va;
va_start(va, format);
zend_error_cb(type, filename, lineno, format, va);
va_end(va);
}
/* This function doesn't return if it uses E_ERROR */
ZEND_API void zend_exception_error(zend_object *ex, int severity) /* {{{ */
{
zval exception, rv;
zend_class_entry *ce_exception;
ZVAL_OBJ(&exception, ex);
ce_exception = Z_OBJCE(exception);
EG(exception) = NULL;
if (ce_exception == zend_parse_error_ce) {
zend_string *message = zval_get_string(GET_PROPERTY(&exception, "message"));
zend_string *file = zval_get_string(GET_PROPERTY_SILENT(&exception, "file"));
zend_long line = zval_get_long(GET_PROPERTY_SILENT(&exception, "line"));
zend_long code = zval_get_long(GET_PROPERTY_SILENT(&exception, "code"));
zend_error_helper(code? code : E_ERROR, ZSTR_VAL(file), line, "%s", ZSTR_VAL(message));
zend_string_release(file);
zend_string_release(message);
} else if (instanceof_function(ce_exception, zend_ce_throwable)) {
zval tmp, rv;
zend_string *str, *file = NULL;
zend_long line = 0;
zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &tmp);
if (!EG(exception)) {
if (Z_TYPE(tmp) != IS_STRING) {
zend_error(E_WARNING, "%s::__toString() must return a string", ZSTR_VAL(ce_exception->name));
} else {
zend_update_property_string(i_get_exception_base(&exception), &exception, "string", sizeof("string")-1, EG(exception) ? ZSTR_VAL(ce_exception->name) : Z_STRVAL(tmp));
}
}
zval_ptr_dtor(&tmp);
if (EG(exception)) {
zval zv;
ZVAL_OBJ(&zv, EG(exception));
/* do the best we can to inform about the inner exception */
if (instanceof_function(ce_exception, zend_exception_ce) || instanceof_function(ce_exception, zend_error_ce)) {
file = zval_get_string(GET_PROPERTY_SILENT(&zv, "file"));
line = zval_get_long(GET_PROPERTY_SILENT(&zv, "line"));
}
zend_error_va(E_WARNING, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
"Uncaught %s in exception handling during call to %s::__tostring()",
ZSTR_VAL(Z_OBJCE(zv)->name), ZSTR_VAL(ce_exception->name));
if (file) {
zend_string_release(file);
}
}
str = zval_get_string(GET_PROPERTY_SILENT(&exception, "string"));
file = zval_get_string(GET_PROPERTY_SILENT(&exception, "file"));
line = zval_get_long(GET_PROPERTY_SILENT(&exception, "line"));
zend_error_va(severity, (file && ZSTR_LEN(file) > 0) ? ZSTR_VAL(file) : NULL, line,
"Uncaught %s\n thrown", ZSTR_VAL(str));
zend_string_release(str);
zend_string_release(file);
} else {
zend_error(severity, "Uncaught exception '%s'", ZSTR_VAL(ce_exception->name));
}
OBJ_RELEASE(ex);
}
/* }}} */
ZEND_API void zend_throw_exception_object(zval *exception) /* {{{ */
{
zend_class_entry *exception_ce;
if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
zend_error_noreturn(E_CORE_ERROR, "Need to supply an object when throwing an exception");
}
exception_ce = Z_OBJCE_P(exception);
if (!exception_ce || !instanceof_function(exception_ce, zend_ce_throwable)) {
zend_error(E_EXCEPTION | E_ERROR, "Cannot throw objects that do not implement Throwable");
return;
}
zend_throw_exception_internal(exception);
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
*/
|