-
Notifications
You must be signed in to change notification settings - Fork 23
/
otel_observer.c
1173 lines (1031 loc) · 42.7 KB
/
otel_observer.c
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
#include "php.h"
#include "otel_observer.h"
#include "zend_observer.h"
#include "zend_execute.h"
#include "zend_extensions.h"
#include "zend_exceptions.h"
#include "zend_attributes.h"
#include "php_opentelemetry.h"
static int op_array_extension = -1;
const char *withspan_fqn_lc = "opentelemetry\\api\\instrumentation\\withspan";
const char *spanattribute_fqn_lc =
"opentelemetry\\api\\instrumentation\\spanattribute";
static char *with_span_attribute_args_keys[] = {"name", "span_kind"};
typedef struct otel_observer {
zend_llist pre_hooks;
zend_llist post_hooks;
} otel_observer;
typedef struct otel_exception_state {
zend_object *exception;
zend_object *prev_exception;
const zend_op *opline_before_exception;
bool has_opline;
const zend_op *opline;
} otel_exception_state;
#define STACK_EXTENSION_LIMIT 16
typedef struct otel_arg_locator {
zend_execute_data *execute_data;
// Number of argument slots reserved in execute_data, any arguments beyond
// this limit will be stored after auxiliary slots
uint32_t reserved;
// Number of arguments provided at the call site. May exceed the number of
// arguments in the function definition
uint32_t provided;
// Number of slots between reserved and "extra" argument slots
uint32_t auxiliary_slots;
uint32_t extended_start;
uint32_t extended_max;
uint32_t extended_used;
zval extended_slots[STACK_EXTENSION_LIMIT];
} otel_arg_locator;
static inline void
func_get_this_or_called_scope(zval *zv, zend_execute_data *execute_data) {
if (execute_data->func->op_array.scope) {
if (execute_data->func->op_array.fn_flags & ZEND_ACC_STATIC) {
zend_class_entry *called_scope =
zend_get_called_scope(execute_data);
ZVAL_STR(zv, called_scope->name);
} else {
zend_object *this = zend_get_this_object(execute_data);
ZVAL_OBJ_COPY(zv, this);
}
} else {
ZVAL_NULL(zv);
}
}
static inline void func_get_function_name(zval *zv, zend_execute_data *ex) {
ZVAL_STR_COPY(zv, ex->func->op_array.function_name);
}
static zend_function *find_function(zend_class_entry *ce, zend_string *name) {
zend_function *func;
ZEND_HASH_FOREACH_PTR(&ce->function_table, func) {
if (zend_string_equals(func->common.function_name, name)) {
return func;
}
}
ZEND_HASH_FOREACH_END();
return NULL;
}
// find SpanAttribute attribute on a parameter, or on a parameter of
// an interface
static zend_attribute *find_spanattribute_attribute(zend_function *func,
uint32_t i) {
zend_attribute *attr = zend_get_parameter_attribute_str(
func->common.attributes, spanattribute_fqn_lc,
strlen(spanattribute_fqn_lc), i);
if (attr != NULL) {
return attr;
}
zend_class_entry *ce = func->common.scope;
if (ce && ce->num_interfaces > 0) {
zend_class_entry *interface_ce;
for (uint32_t i = 0; i < ce->num_interfaces; i++) {
interface_ce = ce->interfaces[i];
if (interface_ce != NULL) {
// does the interface have the function we are looking for?
zend_function *iface_func =
find_function(interface_ce, func->common.function_name);
if (iface_func != NULL) {
// method found, check positional arg for attribute
attr = zend_get_parameter_attribute_str(
iface_func->common.attributes, spanattribute_fqn_lc,
strlen(spanattribute_fqn_lc), i);
if (attr != NULL) {
return attr;
}
}
}
}
}
return NULL;
}
// find WithSpan in attributes, or in interface method attributes
static zend_attribute *find_withspan_attribute(zend_function *func) {
zend_attribute *attr;
attr = zend_get_attribute_str(func->common.attributes, withspan_fqn_lc,
strlen(withspan_fqn_lc));
if (attr != NULL) {
return attr;
}
zend_class_entry *ce = func->common.scope;
// if a method, check interfaces
if (ce && ce->num_interfaces > 0) {
zend_class_entry *interface_ce;
for (uint32_t i = 0; i < ce->num_interfaces; i++) {
interface_ce = ce->interfaces[i];
if (interface_ce != NULL) {
// does the interface have the function we are looking for?
zend_function *iface_func =
find_function(interface_ce, func->common.function_name);
if (iface_func != NULL) {
// Method found in the interface, now check for attributes
attr = zend_get_attribute_str(iface_func->common.attributes,
withspan_fqn_lc,
strlen(withspan_fqn_lc));
if (attr) {
return attr;
}
}
}
}
}
return NULL;
}
static bool func_has_withspan_attribute(zend_execute_data *ex) {
zend_attribute *attr = find_withspan_attribute(ex->func);
return attr != NULL;
}
/*
* OpenTelemetry attribute values may only be of limited types
*/
static bool is_valid_attribute_value(zval *val) {
switch (Z_TYPE_P(val)) {
case IS_STRING:
case IS_LONG:
case IS_DOUBLE:
case IS_TRUE:
case IS_FALSE:
case IS_ARRAY:
return true;
default:
return false;
}
}
// get function args. any args with the
// SpanAttributes attribute are added to the attributes HashTable
static void func_get_args(zval *zv, HashTable *attributes,
zend_execute_data *ex, bool check_for_attributes) {
zval *p, *q;
uint32_t i, first_extra_arg;
uint32_t arg_count = ZEND_CALL_NUM_ARGS(ex);
// @see
// https://github.com/php/php-src/blob/php-8.1.0/Zend/zend_builtin_functions.c#L235
if (arg_count) {
array_init_size(zv, arg_count);
if (ex->func->type == ZEND_INTERNAL_FUNCTION) {
first_extra_arg = arg_count;
} else {
first_extra_arg = ex->func->op_array.num_args;
}
zend_hash_real_init_packed(Z_ARRVAL_P(zv));
ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(zv)) {
i = 0;
p = ZEND_CALL_ARG(ex, 1);
if (arg_count > first_extra_arg) {
while (i < first_extra_arg) {
q = p;
if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
ZVAL_DEREF(q);
if (Z_OPT_REFCOUNTED_P(q)) {
Z_ADDREF_P(q);
}
ZEND_HASH_FILL_SET(q);
} else {
ZEND_HASH_FILL_SET_NULL();
}
ZEND_HASH_FILL_NEXT();
p++;
i++;
}
p = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var +
ex->func->op_array.T);
}
while (i < arg_count) {
if (check_for_attributes &&
ex->func->type != ZEND_INTERNAL_FUNCTION) {
zend_string *arg_name = ex->func->op_array.vars[i];
zend_attribute *attribute =
find_spanattribute_attribute(ex->func, i);
if (attribute != NULL && is_valid_attribute_value(p)) {
if (attribute->argc) {
zend_string *key = Z_STR(attribute->args[0].value);
zend_hash_del(attributes, key);
zend_hash_add(attributes, key, p);
} else {
zend_hash_del(attributes, arg_name);
zend_hash_add(attributes, arg_name, p);
}
}
}
q = p;
if (EXPECTED(Z_TYPE_INFO_P(q) != IS_UNDEF)) {
ZVAL_DEREF(q);
if (Z_OPT_REFCOUNTED_P(q)) {
Z_ADDREF_P(q);
}
ZEND_HASH_FILL_SET(q);
} else {
ZEND_HASH_FILL_SET_NULL();
}
ZEND_HASH_FILL_NEXT();
p++;
i++;
}
}
ZEND_HASH_FILL_END();
Z_ARRVAL_P(zv)->nNumOfElements = arg_count;
} else {
ZVAL_EMPTY_ARRAY(zv);
}
}
static uint32_t func_get_arg_index_by_name(zend_execute_data *execute_data,
zend_string *arg_name) {
// @see
// https://github.com/php/php-src/blob/php-8.1.0/Zend/zend_execute.c#L4515
zend_function *fbc = execute_data->func;
uint32_t num_args = fbc->common.num_args;
if (EXPECTED(fbc->type == ZEND_USER_FUNCTION) ||
EXPECTED(fbc->common.fn_flags & ZEND_ACC_USER_ARG_INFO)) {
for (uint32_t i = 0; i < num_args; i++) {
zend_arg_info *arg_info = &fbc->op_array.arg_info[i];
if (zend_string_equals(arg_name, arg_info->name)) {
return i;
}
}
} else {
for (uint32_t i = 0; i < num_args; i++) {
zend_internal_arg_info *arg_info =
&fbc->internal_function.arg_info[i];
size_t len = strlen(arg_info->name);
if (len == ZSTR_LEN(arg_name) &&
!memcmp(arg_info->name, ZSTR_VAL(arg_name), len)) {
return i;
}
}
}
return (uint32_t)-1;
}
static inline void func_get_retval(zval *zv, zval *retval) {
if (UNEXPECTED(!retval || Z_ISUNDEF_P(retval))) {
ZVAL_NULL(zv);
} else {
ZVAL_COPY(zv, retval);
}
}
static inline void func_get_exception(zval *zv) {
zend_object *exception = EG(exception);
if (exception && zend_is_unwind_exit(exception)) {
ZVAL_NULL(zv);
} else if (UNEXPECTED(exception)) {
ZVAL_OBJ_COPY(zv, exception);
} else {
ZVAL_NULL(zv);
}
}
static inline void func_get_declaring_scope(zval *zv, zend_execute_data *ex) {
if (ex->func->op_array.scope) {
ZVAL_STR_COPY(zv, ex->func->op_array.scope->name);
} else {
ZVAL_NULL(zv);
}
}
static inline void func_get_filename(zval *zv, zend_execute_data *ex) {
if (ex->func->type != ZEND_INTERNAL_FUNCTION) {
ZVAL_STR_COPY(zv, ex->func->op_array.filename);
} else {
ZVAL_NULL(zv);
}
}
static inline void func_get_lineno(zval *zv, zend_execute_data *ex) {
if (ex->func->type != ZEND_INTERNAL_FUNCTION) {
ZVAL_LONG(zv, ex->func->op_array.line_start);
} else {
ZVAL_NULL(zv);
}
}
static inline void func_get_attribute_args(zval *zv, HashTable *attributes,
zend_execute_data *ex) {
if (!OTEL_G(attr_hooks_enabled)) {
ZVAL_EMPTY_ARRAY(zv);
return;
}
zend_attribute *attr = find_withspan_attribute(ex->func);
if (attr == NULL || attr->argc == 0) {
ZVAL_EMPTY_ARRAY(zv);
return;
}
HashTable *ht;
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, attr->argc, NULL, ZVAL_PTR_DTOR, 0);
zend_attribute_arg arg;
zend_string *key;
for (uint32_t i = 0; i < attr->argc; i++) {
arg = attr->args[i];
if (i == 2 ||
(arg.name && zend_string_equals_literal(arg.name, "attributes"))) {
// attributes, append to a separate HashTable
if (Z_TYPE(arg.value) == IS_ARRAY) {
zend_hash_clean(attributes); // should already be empty
HashTable *array_ht = Z_ARRVAL_P(&arg.value);
zend_hash_copy(attributes, array_ht, zval_add_ref);
}
} else {
if (arg.name != NULL) {
zend_hash_add(ht, arg.name, &arg.value);
} else {
key = zend_string_init(with_span_attribute_args_keys[i],
strlen(with_span_attribute_args_keys[i]),
0);
zend_hash_add(ht, key, &arg.value);
zend_string_release(key);
}
}
}
ZVAL_ARR(zv, ht);
}
/**
* Check if the object implements or extends the specified class
*/
bool is_object_compatible_with_type_hint(zval *object_zval,
zend_class_entry *type_hint) {
zend_class_entry *object_ce = Z_OBJCE_P(object_zval);
return instanceof_function(object_ce, type_hint);
}
/**
* Check if pre/post function callback's signature is compatible with
* the expected function signature.
* This is a runtime check, since some parameters are only known at runtime.
* Can be disabled via the opentelemetry.check_hook_functions ini value.
*/
static inline bool is_valid_signature(zend_fcall_info fci,
zend_fcall_info_cache fcc) {
if (OTEL_G(validate_hook_functions) == 0) {
return 1;
}
zend_function *func = fcc.function_handler;
zend_arg_info *arg_info;
zend_type *arg_type;
uint32_t type;
uint32_t type_mask;
for (uint32_t i = 0; i < func->common.num_args; i++) {
// get type mask of callback argument
arg_info = &func->common.arg_info[i];
arg_type = &arg_info->type;
type_mask = arg_type->type_mask;
// get actual value + type
zval param = fci.params[i];
type = Z_TYPE(fci.params[i]);
if (type_mask == IS_UNDEF) {
// no type mask -> ok
} else if (Z_TYPE(param) == IS_OBJECT) {
// object special-case handling (check for interfaces, subclasses)
zend_class_entry *ce = Z_OBJCE(param);
if (!is_object_compatible_with_type_hint(¶m, ce)) {
return false;
}
} else if ((type_mask & (1 << type)) == 0) {
// type is not compatible with mask
return false;
}
}
return true;
}
static void exception_isolation_start(otel_exception_state *save_state) {
save_state->exception = EG(exception);
save_state->prev_exception = EG(prev_exception);
save_state->opline_before_exception = EG(opline_before_exception);
EG(exception) = NULL;
EG(prev_exception) = NULL;
EG(opline_before_exception) = NULL;
// If the hook handler throws an exception, the execute_data of the outer
// frame may have its opline set to an exception handler too. This is done
// before the chance to clear the exception, so opline has to be restored
// to original value.
zend_execute_data *execute_data = EG(current_execute_data);
if (execute_data != NULL) {
save_state->has_opline = true;
save_state->opline = execute_data->opline;
} else {
save_state->has_opline = false;
save_state->opline = NULL;
}
}
static zend_object *exception_isolation_end(otel_exception_state *save_state) {
zend_object *suppressed = EG(exception);
// NULL this before call to zend_clear_exception, as it would try to jump
// to exception handler then.
EG(exception) = NULL;
// this clears prev_exception if it was set for any reason
zend_clear_exception();
EG(exception) = save_state->exception;
EG(prev_exception) = save_state->prev_exception;
EG(opline_before_exception) = save_state->opline_before_exception;
zend_execute_data *execute_data = EG(current_execute_data);
if (execute_data != NULL && save_state->has_opline) {
execute_data->opline = save_state->opline;
}
return suppressed;
}
static const char *zval_get_chars(zval *zv) {
if (zv != NULL && Z_TYPE_P(zv) == IS_STRING) {
return Z_STRVAL_P(zv);
}
return "null";
}
static void exception_isolation_handle_exception(zend_object *suppressed,
zval *class_name,
zval *function_name,
const char *type) {
if (suppressed == NULL) {
return;
}
zend_class_entry *exception_base = zend_get_exception_base(suppressed);
zval return_value;
zval *message =
zend_read_property_ex(exception_base, suppressed,
ZSTR_KNOWN(ZEND_STR_MESSAGE), 1, &return_value);
php_error_docref(NULL, E_CORE_WARNING,
"OpenTelemetry: %s threw exception,"
" class=%s function=%s message=%s",
type, zval_get_chars(class_name),
zval_get_chars(function_name), zval_get_chars(message));
if (message != NULL) {
ZVAL_DEREF(message);
}
OBJ_RELEASE(suppressed);
}
static void arg_locator_initialize(otel_arg_locator *arg_locator,
zend_execute_data *execute_data) {
arg_locator->execute_data = execute_data;
if (execute_data->func->type == ZEND_INTERNAL_FUNCTION) {
// For internal functions, rather than having reserved number of slots
// before auxiliary slots and extra ones after that, internal functions
// have all (and only) arguments provided by call site before auxiliary
// slots, and there is nothing after auxiliary slots.
arg_locator->reserved = ZEND_CALL_NUM_ARGS(execute_data);
} else {
arg_locator->reserved = execute_data->func->op_array.last_var;
}
arg_locator->provided = ZEND_CALL_NUM_ARGS(execute_data);
arg_locator->auxiliary_slots = execute_data->func->op_array.T;
arg_locator->extended_used = 0;
arg_locator->extended_start = arg_locator->provided > arg_locator->reserved
? arg_locator->provided
: arg_locator->reserved;
if (OTEL_G(allow_stack_extension)) {
arg_locator->extended_max = STACK_EXTENSION_LIMIT;
size_t slots_left_in_stack = EG(vm_stack_end) - EG(vm_stack_top);
if (slots_left_in_stack < arg_locator->extended_max) {
arg_locator->extended_max = slots_left_in_stack;
}
} else {
arg_locator->extended_max = 0;
}
}
static zval *arg_locator_get_slot(otel_arg_locator *arg_locator, uint32_t index,
const char **failure_reason) {
if (index < arg_locator->reserved) {
return ZEND_CALL_ARG(arg_locator->execute_data, index + 1);
} else if (index < arg_locator->provided) {
return ZEND_CALL_ARG(arg_locator->execute_data,
index + arg_locator->auxiliary_slots + 1);
}
uint32_t extended_index = index - arg_locator->extended_start;
if (extended_index < arg_locator->extended_max) {
uint32_t extended_index = index - arg_locator->extended_start;
if (extended_index >= arg_locator->extended_used) {
arg_locator->extended_used = extended_index + 1;
}
return &arg_locator->extended_slots[extended_index];
}
if (failure_reason != NULL) {
// Having a hardcoded upper limit allows performing stack
// extension as one step in the end, rather than moving slots around in
// the stack each time a new argument is discovered
if (extended_index >= STACK_EXTENSION_LIMIT) {
*failure_reason = "exceeds built-in stack extension limit";
} else if (!OTEL_G(allow_stack_extension)) {
*failure_reason = "stack extension must be enabled with "
"opentelemetry.allow_stack_extension option";
} else {
*failure_reason = "not enough room left in stack page";
}
}
return NULL;
}
static void arg_locator_store_extended(otel_arg_locator *arg_locator) {
if (arg_locator->extended_used == 0) {
return;
}
// This is safe because extended_max is adjusted to not exceed current stack
// page end
EG(vm_stack_top) += arg_locator->extended_used;
if (arg_locator->execute_data->func->type == ZEND_INTERNAL_FUNCTION) {
// For internal functions, the additional arguments need to go before
// the auxiliary slots, therefore the auxiliary slots need to be moved
// ahead
zval *target =
ZEND_CALL_ARG(arg_locator->execute_data, arg_locator->provided + 1);
zval *aux_target = target + arg_locator->extended_used;
memmove(aux_target, target,
sizeof(*aux_target) * arg_locator->auxiliary_slots);
memcpy(target, arg_locator->extended_slots,
sizeof(*target) * arg_locator->extended_used);
} else {
// For PHP functions, the additional arguments go to the end of the
// frame, so nothing else needs to be moved around
zval *target = ZEND_CALL_ARG(arg_locator->execute_data,
arg_locator->extended_start +
arg_locator->auxiliary_slots + 1);
memcpy(target, arg_locator->extended_slots,
sizeof(*target) * arg_locator->extended_used);
}
}
static void observer_begin(zend_execute_data *execute_data, zend_llist *hooks) {
if (!zend_llist_count(hooks)) {
return;
}
zval params[8];
uint32_t param_count = 8;
HashTable *attributes;
ALLOC_HASHTABLE(attributes);
zend_hash_init(attributes, 0, NULL, ZVAL_PTR_DTOR, 0);
bool check_for_attributes =
OTEL_G(attr_hooks_enabled) && func_has_withspan_attribute(execute_data);
func_get_this_or_called_scope(¶ms[0], execute_data);
func_get_attribute_args(¶ms[6], attributes, execute_data);
func_get_args(¶ms[1], attributes, execute_data, check_for_attributes);
func_get_declaring_scope(¶ms[2], execute_data);
func_get_function_name(¶ms[3], execute_data);
func_get_filename(¶ms[4], execute_data);
func_get_lineno(¶ms[5], execute_data);
ZVAL_ARR(¶ms[7], attributes);
for (zend_llist_element *element = hooks->head; element;
element = element->next) {
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
if (UNEXPECTED(zend_fcall_info_init((zval *)element->data, 0, &fci,
&fcc, NULL, NULL) != SUCCESS)) {
php_error_docref(NULL, E_WARNING,
"Failed to initialize pre hook callable");
continue;
}
zval ret = {.u1.type_info = IS_UNDEF};
fci.param_count = param_count;
fci.params = params;
fci.named_params = NULL;
fci.retval = &ret;
if (!is_valid_signature(fci, fcc)) {
php_error_docref(NULL, E_CORE_WARNING,
"OpenTelemetry: pre hook invalid signature,"
" class=%s function=%s",
(Z_TYPE_P(¶ms[2]) == IS_NULL)
? "null"
: Z_STRVAL_P(¶ms[2]),
Z_STRVAL_P(¶ms[3]));
continue;
}
otel_exception_state save_state;
exception_isolation_start(&save_state);
if (zend_call_function(&fci, &fcc) == SUCCESS) {
if (Z_TYPE(ret) == IS_ARRAY &&
!zend_is_identical(&ret, ¶ms[1])) {
zend_ulong idx;
zend_string *str_idx;
zval *val;
bool invalid_arg_warned = false;
otel_arg_locator arg_locator;
arg_locator_initialize(&arg_locator, execute_data);
uint32_t args_initialized = arg_locator.provided;
ZEND_HASH_FOREACH_KEY_VAL(Z_ARR(ret), idx, str_idx, val) {
const char *failure_reason = "";
if (str_idx != NULL) {
idx = func_get_arg_index_by_name(execute_data, str_idx);
if (idx == (uint32_t)-1) {
php_error_docref(
NULL, E_CORE_WARNING,
"OpenTelemetry: pre hook unknown "
"named arg %s, class=%s function=%s",
ZSTR_VAL(str_idx), zval_get_chars(¶ms[2]),
zval_get_chars(¶ms[3]));
continue;
}
}
zval *target = arg_locator_get_slot(&arg_locator, idx,
&failure_reason);
if (target == NULL) {
if (invalid_arg_warned) {
continue;
}
php_error_docref(NULL, E_CORE_WARNING,
"OpenTelemetry: pre hook invalid "
"argument index " ZEND_ULONG_FMT
" - %s, class=%s function=%s",
idx, failure_reason,
zval_get_chars(¶ms[2]),
zval_get_chars(¶ms[3]));
invalid_arg_warned = true;
continue;
}
if (idx >= args_initialized) {
// This slot was not initialized, need to initialize
// all slots between current and the last initialized
// one
for (uint32_t i = args_initialized; i < idx; i++) {
ZVAL_UNDEF(
arg_locator_get_slot(&arg_locator, i, NULL));
ZEND_ADD_CALL_FLAG(execute_data,
ZEND_CALL_MAY_HAVE_UNDEF);
}
args_initialized = idx + 1;
} else {
// This slot was already initialized, need to
// decrement refcount before overwriting
zval_dtor(target);
}
if (idx >= arg_locator.reserved && Z_REFCOUNTED_P(val)) {
// If there are any "extra parameters" that are
// refcounted, then this flag must be set. While we
// cannot add any new extra parameter slots, this flag
// may not have been present because all the values
// were previously not refcounted
ZEND_ADD_CALL_FLAG(execute_data,
ZEND_CALL_FREE_EXTRA_ARGS);
}
ZVAL_COPY(target, val);
if (idx < arg_locator.provided &&
Z_TYPE(params[1]) == IS_ARRAY) {
// This index is present in the array provided to begin
// hook, update it in that array as well
Z_TRY_ADDREF_P(val);
zend_hash_index_update(Z_ARR(params[1]), idx, val);
}
}
ZEND_HASH_FOREACH_END();
arg_locator_store_extended(&arg_locator);
// Update provided argument count if begin hook added arguments
// that were not provided originally
if (args_initialized > arg_locator.provided) {
ZEND_CALL_NUM_ARGS(execute_data) = args_initialized;
}
}
}
zend_object *suppressed = exception_isolation_end(&save_state);
exception_isolation_handle_exception(suppressed, ¶ms[2], ¶ms[3],
"pre hook");
zval_dtor(&ret);
}
if (UNEXPECTED(ZEND_CALL_INFO(execute_data) & ZEND_CALL_MAY_HAVE_UNDEF)) {
zend_object *exception = EG(exception);
EG(exception) = (void *)(uintptr_t)-1;
if (zend_handle_undef_args(execute_data) == FAILURE) {
uint32_t arg_count = ZEND_CALL_NUM_ARGS(execute_data);
for (uint32_t i = 0; i < arg_count; i++) {
zval *arg = ZEND_CALL_VAR_NUM(execute_data, i);
if (!Z_ISUNDEF_P(arg)) {
continue;
}
ZVAL_NULL(arg);
}
}
EG(exception) = exception;
}
for (size_t i = 0; i < param_count; i++) {
zval_dtor(¶ms[i]);
}
}
static void observer_end(zend_execute_data *execute_data, zval *retval,
zend_llist *hooks) {
if (!zend_llist_count(hooks)) {
return;
}
zval params[8];
uint32_t param_count = 8;
func_get_this_or_called_scope(¶ms[0], execute_data);
func_get_args(¶ms[1], NULL, execute_data, false);
func_get_retval(¶ms[2], retval);
func_get_exception(¶ms[3]);
func_get_declaring_scope(¶ms[4], execute_data);
func_get_function_name(¶ms[5], execute_data);
func_get_filename(¶ms[6], execute_data);
func_get_lineno(¶ms[7], execute_data);
for (zend_llist_element *element = hooks->tail; element;
element = element->prev) {
zend_fcall_info fci = empty_fcall_info;
zend_fcall_info_cache fcc = empty_fcall_info_cache;
if (UNEXPECTED(zend_fcall_info_init((zval *)element->data, 0, &fci,
&fcc, NULL, NULL) != SUCCESS)) {
php_error_docref(NULL, E_WARNING,
"Failed to initialize post hook callable");
continue;
}
zval ret = {.u1.type_info = IS_UNDEF};
fci.param_count = param_count;
fci.params = params;
fci.named_params = NULL;
fci.retval = &ret;
if (!is_valid_signature(fci, fcc)) {
php_error_docref(NULL, E_CORE_WARNING,
"OpenTelemetry: post hook invalid signature, "
"class=%s function=%s",
(Z_TYPE_P(¶ms[4]) == IS_NULL)
? "null"
: Z_STRVAL_P(¶ms[4]),
Z_STRVAL_P(¶ms[5]));
continue;
}
otel_exception_state save_state;
exception_isolation_start(&save_state);
if (zend_call_function(&fci, &fcc) == SUCCESS) {
/* TODO rather than ignoring return value if post callback doesn't
have a return type-hint, could we check whether the types are
compatible and allow if they are? */
if (!Z_ISUNDEF(ret) &&
(fcc.function_handler->op_array.fn_flags &
ZEND_ACC_HAS_RETURN_TYPE) &&
!(ZEND_TYPE_PURE_MASK(
fcc.function_handler->common.arg_info[-1].type) &
MAY_BE_VOID)) {
if (execute_data->return_value) {
zval_ptr_dtor(execute_data->return_value);
ZVAL_COPY(execute_data->return_value, &ret);
zval_ptr_dtor(¶ms[2]);
ZVAL_COPY_VALUE(¶ms[2], &ret);
ZVAL_UNDEF(&ret);
}
}
}
zend_object *suppressed = exception_isolation_end(&save_state);
exception_isolation_handle_exception(suppressed, ¶ms[4], ¶ms[5],
"post hook");
zval_dtor(&ret);
}
for (size_t i = 0; i < param_count; i++) {
zval_dtor(¶ms[i]);
}
}
static void observer_begin_handler(zend_execute_data *execute_data) {
otel_observer *observer = ZEND_OP_ARRAY_EXTENSION(
&execute_data->func->op_array, op_array_extension);
if (!observer || !zend_llist_count(&observer->pre_hooks)) {
return;
}
observer_begin(execute_data, &observer->pre_hooks);
}
static void observer_end_handler(zend_execute_data *execute_data,
zval *retval) {
otel_observer *observer = ZEND_OP_ARRAY_EXTENSION(
&execute_data->func->op_array, op_array_extension);
if (!observer || !zend_llist_count(&observer->post_hooks)) {
return;
}
observer_end(execute_data, retval, &observer->post_hooks);
}
static void free_observer(otel_observer *observer) {
zend_llist_destroy(&observer->pre_hooks);
zend_llist_destroy(&observer->post_hooks);
efree(observer);
}
static void init_observer(otel_observer *observer) {
zend_llist_init(&observer->pre_hooks, sizeof(zval),
(llist_dtor_func_t)zval_ptr_dtor, 0);
zend_llist_init(&observer->post_hooks, sizeof(zval),
(llist_dtor_func_t)zval_ptr_dtor, 0);
}
static otel_observer *create_observer() {
otel_observer *observer = emalloc(sizeof(otel_observer));
init_observer(observer);
return observer;
}
static void copy_observer(otel_observer *source, otel_observer *destination) {
destination->pre_hooks = source->pre_hooks;
destination->post_hooks = source->post_hooks;
}
static bool find_observers(HashTable *ht, zend_string *n, zend_llist *pre_hooks,
zend_llist *post_hooks) {
otel_observer *observer = zend_hash_find_ptr_lc(ht, n);
if (observer) {
for (zend_llist_element *element = observer->pre_hooks.head; element;
element = element->next) {
zval_add_ref((zval *)&element->data);
zend_llist_add_element(pre_hooks, &element->data);
}
for (zend_llist_element *element = observer->post_hooks.head; element;
element = element->next) {
zval_add_ref((zval *)&element->data);
zend_llist_add_element(post_hooks, &element->data);
}
return true;
}
return false;
}
static void find_class_observers(HashTable *ht, HashTable *type_visited_lookup,
zend_class_entry *ce, zend_llist *pre_hooks,
zend_llist *post_hooks) {
for (; ce; ce = ce->parent) {
// Omit type if it was already visited
if (zend_hash_exists(type_visited_lookup, ce->name)) {
continue;
}
if (find_observers(ht, ce->name, pre_hooks, post_hooks)) {
zend_hash_add_empty_element(type_visited_lookup, ce->name);
}
for (uint32_t i = 0; i < ce->num_interfaces; i++) {
find_class_observers(ht, type_visited_lookup, ce->interfaces[i],
pre_hooks, post_hooks);
}
}
}
static void find_method_observers(HashTable *ht, zend_class_entry *ce,
zend_string *fn, zend_llist *pre_hooks,
zend_llist *post_hooks) {
// Below hashtable stores information
// whether type was already visited
// This information is used to prevent
// adding hooks more than once in the case
// of extensive class hierarchy
HashTable type_visited_lookup;
zend_hash_init(&type_visited_lookup, 8, NULL, NULL, 0);
HashTable *lookup = zend_hash_find_ptr_lc(ht, fn);
if (lookup) {
find_class_observers(lookup, &type_visited_lookup, ce, pre_hooks,
post_hooks);
}
zend_hash_destroy(&type_visited_lookup);
}
static zval create_attribute_observer_handler(char *fn) {
zval callable;
ZVAL_STRING(&callable, fn);
return callable;
}
static otel_observer *resolve_observer(zend_execute_data *execute_data) {
zend_function *fbc = execute_data->func;
if (!fbc->common.function_name) {
return NULL;
}
bool has_withspan_attribute = func_has_withspan_attribute(execute_data);
if (OTEL_G(attr_hooks_enabled) == false && has_withspan_attribute) {
php_error_docref(NULL, E_CORE_WARNING,
"OpenTelemetry: WithSpan attribute found but "
"attribute hooks disabled");
}
otel_observer observer_instance;
init_observer(&observer_instance);
if (fbc->op_array.scope) {
find_method_observers(OTEL_G(observer_class_lookup),
fbc->op_array.scope, fbc->common.function_name,
&observer_instance.pre_hooks,
&observer_instance.post_hooks);
} else {
find_observers(OTEL_G(observer_function_lookup),
fbc->common.function_name, &observer_instance.pre_hooks,
&observer_instance.post_hooks);
}
if (!zend_llist_count(&observer_instance.pre_hooks) &&
!zend_llist_count(&observer_instance.post_hooks)) {
if (OTEL_G(attr_hooks_enabled) && has_withspan_attribute) {