-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathfunctions.c
More file actions
3060 lines (2597 loc) · 107 KB
/
Copy pathfunctions.c
File metadata and controls
3060 lines (2597 loc) · 107 KB
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 "ddtrace.h"
#include "configuration.h"
#include "span.h"
#include <Zend/zend_exceptions.h>
#include <exceptions/exceptions.h>
#include "auto_flush.h"
#include "code_origins.h"
#include "engine_hooks.h"
#include "ffe.h"
#include "handlers_exception.h"
#include "memory_limit.h"
#include "random.h"
#include "serializer.h"
#include "weak_resources.h"
#include <ext/startup_logging.h>
#include "handlers_http.h"
#include "ip_extraction.h"
#include "tracer_telemetry.h"
#include "tracer_tag_propagation/tracer_tag_propagation.h"
#include <ext/agent_info.h>
#include <ext/ffi_utils.h>
#include <ext/process_tags.h>
#include <ext/remote_config.h>
#include <ext/sidecar.h>
#include <ext/telemetry.h>
#include <ext/standard/php_string.h>
#include <ext/json/php_json.h>
#include <json/json.h>
#ifndef _WIN32
#include "comms_php.h"
#include "coms.h"
#endif
#include <ext/zend_hrtime.h>
#define DDTRACE_FFE_TYPE_STRING 0
#define DDTRACE_FFE_TYPE_INT 1
#define DDTRACE_FFE_TYPE_FLOAT 2
#define DDTRACE_FFE_TYPE_BOOL 3
#define DDTRACE_FFE_TYPE_OBJECT 4
// On PHP 7 we cannot declare arrays as internal values. Assign null and handle in create_object where necessary.
#if PHP_VERSION_ID < 80000
#pragma push_macro("ZVAL_EMPTY_ARRAY")
#undef ZVAL_EMPTY_ARRAY
#define ZVAL_EMPTY_ARRAY ZVAL_NULL
#endif
// CG(empty_string) is not accessible during MINIT (in ZTS at least)
#if PHP_VERSION_ID < 70200
#pragma push_macro("ZVAL_EMPTY_STRING")
#undef ZVAL_EMPTY_STRING
#define ZVAL_EMPTY_STRING(z) ZVAL_NEW_STR(z, zend_string_init("", 0, 1))
#endif
#include "ddtrace_arginfo.h"
#if PHP_VERSION_ID < 70200
#pragma pop_macro("ZVAL_EMPTY_STRING")
#endif
#if PHP_VERSION_ID < 80000
#pragma pop_macro("ZVAL_EMPTY_ARRAY")
#endif
// For manual ZPP
#if PHP_VERSION_ID < 70400
#define _error_code error_code
#endif
ZEND_EXTERN_MODULE_GLOBALS(datadog);
static void dd_span_event_construct(ddtrace_span_event *event, zend_string *name, zend_long timestamp, zval *attributes)
{
zval garbage_name, garbage_timestamp, garbage_attributes;
// Copy current values to temporary zval variables
ZVAL_COPY_VALUE(&garbage_name, &event->property_name);
ZVAL_COPY_VALUE(&garbage_timestamp, &event->property_timestamp);
ZVAL_COPY_VALUE(&garbage_attributes, &event->property_attributes);
ZVAL_STR_COPY(&event->property_name, name);
// Use the provided timestamp or the current time in nanoseconds
if (timestamp == 0) {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
timestamp = ts.tv_sec * ZEND_NANO_IN_SEC + ts.tv_nsec;
}
ZVAL_LONG(&event->property_timestamp, timestamp);
// Initialize attributes
if (attributes) {
ZVAL_COPY(&event->property_attributes, attributes);
} else {
array_init(&event->property_attributes);
}
// Free the copied values after replacement
zval_ptr_dtor(&garbage_name);
zval_ptr_dtor(&garbage_timestamp);
zval_ptr_dtor(&garbage_attributes);
}
/* DDTrace\SpanEvent */
zend_class_entry *ddtrace_ce_span_event;
PHP_METHOD(DDTrace_SpanEvent, jsonSerialize) {
ddtrace_span_event *event = (ddtrace_span_event*)Z_OBJ_P(ZEND_THIS);
zval array;
array_init(&array);
Z_TRY_ADDREF(event->property_name);
add_assoc_zval_ex(&array, ZEND_STRL("name"), &event->property_name);
Z_TRY_ADDREF(event->property_timestamp);
add_assoc_zval_ex(&array, ZEND_STRL("time_unix_nano"), &event->property_timestamp);
// Handle attributes dynamically
zval *attributes = &event->property_attributes;
zval combined_attributes;
array_init(&combined_attributes);
if (instanceof_function(event->std.ce, ddtrace_ce_exception_span_event)) {
// Handle exception attributes dynamically if an exception property exists
ddtrace_exception_span_event *exception_event = (ddtrace_exception_span_event *) event;
zval *exception = &exception_event->property_exception;
if (Z_TYPE_P(exception) == IS_OBJECT && instanceof_function(Z_OBJCE_P(exception), zend_ce_throwable)) {
// Get exception message, type, and stack trace directly
zend_string *message = zai_exception_message(Z_OBJ_P(exception));
if (ZSTR_LEN(message)) {
add_assoc_str_ex(&combined_attributes, ZEND_STRL("exception.message"), zend_string_copy(message));
}
add_assoc_str_ex(&combined_attributes, ZEND_STRL("exception.type"), zend_string_copy(Z_OBJCE_P(exception)->name));
// Get the exception stack trace using zai_get_trace_without_args_from_exception
zend_string *stacktrace = zai_get_trace_without_args_from_exception(Z_OBJ_P(exception));
add_assoc_str_ex(&combined_attributes, ZEND_STRL("exception.stacktrace"), stacktrace);
}
}
if (Z_TYPE_P(attributes) == IS_ARRAY) {
zend_hash_copy(Z_ARRVAL(combined_attributes), Z_ARRVAL_P(attributes), (copy_ctor_func_t)zval_add_ref);
}
if (zend_hash_num_elements(Z_ARRVAL(combined_attributes)) > 0) {
add_assoc_zval_ex(&array, ZEND_STRL("attributes"), &combined_attributes);
} else {
zval_ptr_dtor(&combined_attributes); // Clean up if no elements
}
RETURN_ARR(Z_ARR(array)); // Return the array
}
PHP_METHOD(DDTrace_SpanEvent, __construct)
{
UNUSED(return_value);
zend_string *name;
zval *attributes = NULL;
zend_long timestamp = 0;
ZEND_PARSE_PARAMETERS_START(1, 3)
Z_PARAM_STR(name)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_EX(attributes, 1, 0)
Z_PARAM_LONG(timestamp)
ZEND_PARSE_PARAMETERS_END();
ddtrace_span_event *event = (ddtrace_span_event*)Z_OBJ_P(ZEND_THIS);
// Use the static function to set properties and handle cleanup
dd_span_event_construct(event, name, timestamp, attributes);
}
/* DDTrace\ExceptionSpanEvent */
zend_class_entry *ddtrace_ce_exception_span_event;
PHP_METHOD(DDTrace_ExceptionSpanEvent, __construct)
{
UNUSED(return_value);
zval *exception;
zval *attributes = NULL;
ZEND_PARSE_PARAMETERS_START(1, 2)
Z_PARAM_OBJECT_OF_CLASS(exception, zend_ce_throwable)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_EX(attributes, 1, 0)
ZEND_PARSE_PARAMETERS_END();
ddtrace_exception_span_event *event = (ddtrace_exception_span_event*)Z_OBJ_P(ZEND_THIS);
// Use the static function to set properties and handle cleanup
zend_string *name = zend_string_init(ZEND_STRL("exception"), 0);
dd_span_event_construct(&event->span_event, name, 0, attributes);
zend_string_release(name);
zval garbage;
ZVAL_COPY_VALUE(&garbage, &event->property_exception);
ZVAL_COPY(&event->property_exception, exception);
zval_ptr_dtor(&garbage);
}
/* DDTrace\SpanLink */
zend_class_entry *ddtrace_ce_span_link;
PHP_METHOD(DDTrace_SpanLink, jsonSerialize) {
ddtrace_span_link *link = (ddtrace_span_link *)Z_OBJ_P(ZEND_THIS);
zend_array *array = zend_new_array(5);
zend_string *trace_id = zend_string_init("trace_id", sizeof("trace_id") - 1, 0);
zend_string *span_id = zend_string_init("span_id", sizeof("span_id") - 1, 0);
zend_string *trace_state = zend_string_init("trace_state", sizeof("trace_state") - 1, 0);
zend_string *attributes = zend_string_init("attributes", sizeof("attributes") - 1, 0);
zend_string *dropped_attributes_count = zend_string_init("dropped_attributes_count", sizeof("dropped_attributes_count") - 1, 0);
Z_TRY_ADDREF(link->property_trace_id);
zend_hash_add(array, trace_id, &link->property_trace_id);
Z_TRY_ADDREF(link->property_span_id);
zend_hash_add(array, span_id, &link->property_span_id);
Z_TRY_ADDREF(link->property_trace_state);
zend_hash_add(array, trace_state, &link->property_trace_state);
Z_TRY_ADDREF(link->property_attributes);
zend_hash_add(array, attributes, &link->property_attributes);
Z_TRY_ADDREF(link->property_dropped_attributes_count);
zend_hash_add(array, dropped_attributes_count, &link->property_dropped_attributes_count);
zend_string_release(trace_id);
zend_string_release(span_id);
zend_string_release(trace_state);
zend_string_release(attributes);
zend_string_release(dropped_attributes_count);
RETURN_ARR(array);
}
static ddtrace_distributed_tracing_result dd_parse_distributed_tracing_headers_function(INTERNAL_FUNCTION_PARAMETERS, bool *success);
ZEND_METHOD(DDTrace_SpanLink, fromHeaders) {
bool success;
ddtrace_distributed_tracing_result result = dd_parse_distributed_tracing_headers_function(INTERNAL_FUNCTION_PARAM_PASSTHRU, &success);
if (!success) {
RETURN_NULL();
}
object_init_ex(return_value, ddtrace_ce_span_link);
ddtrace_span_link *link = (ddtrace_span_link *)Z_OBJ_P(return_value);
if (!get_DD_TRACE_ENABLED()) {
return;
}
ZVAL_STR(&link->property_trace_id, datadog_trace_id_as_hex_string(result.trace_id));
ZVAL_STR(&link->property_span_id, ddtrace_span_id_as_hex_string(result.parent_id));
array_init(&link->property_attributes);
zend_hash_copy(Z_ARR(link->property_attributes), &result.meta_tags, NULL);
zend_string *propagated_tags = ddtrace_format_propagated_tags(&result.propagated_tags, &result.meta_tags);
zend_string *full_tracestate = ddtrace_format_tracestate(result.tracestate, 0, result.origin, result.priority_sampling, propagated_tags, &result.tracestate_unknown_dd_keys);
if (propagated_tags) {
zend_string_release(propagated_tags);
}
if (full_tracestate) {
ZVAL_STR(&link->property_trace_state, full_tracestate);
}
result.meta_tags.pDestructor = NULL; // we moved values directly
zend_hash_destroy(&result.meta_tags);
zend_hash_destroy(&result.propagated_tags);
zend_hash_destroy(&result.tracestate_unknown_dd_keys);
zend_hash_destroy(&result.baggage);
if (result.origin) {
zend_string_release(result.origin);
}
if (result.tracestate) {
zend_string_release(result.tracestate);
}
}
/* DDTrace\SpanData */
zend_class_entry *ddtrace_ce_span_data;
zend_class_entry *ddtrace_ce_inferred_span_data;
zend_class_entry *ddtrace_ce_root_span_data;
#if PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100
HashTable dd_root_span_data_duplicated_properties_table;
#endif
zend_class_entry *ddtrace_ce_span_stack;
static zend_class_entry *ddtrace_ce_ffe_result;
zend_object_handlers ddtrace_span_data_handlers;
zend_object_handlers ddtrace_inferred_span_data_handlers;
zend_object_handlers ddtrace_root_span_data_handlers;
zend_object_handlers ddtrace_span_stack_handlers;
static zend_object *dd_init_span_data_object(zend_class_entry *class_type, ddtrace_span_data *span, zend_object_handlers *handlers) {
zend_object_std_init(&span->std, class_type);
span->std.handlers = handlers;
object_properties_init(&span->std, class_type);
ZVAL_NULL(&span->property_parent); // readonly prop cannot be initialized in stub
#if PHP_VERSION_ID < 80000
// Not handled in arginfo on these old versions
array_init(&span->property_meta);
array_init(&span->property_metrics);
array_init(&span->property_meta_struct);
array_init(&span->property_links);
array_init(&span->property_events);
array_init(&span->property_peer_service_sources);
array_init(&span->property_on_close);
#endif
// Explicitly assign property-mapped NULLs
span->stack = NULL;
span->parent = NULL;
return &span->std;
}
static zend_object *ddtrace_span_data_create(zend_class_entry *class_type) {
ddtrace_span_data *span = ecalloc(1, sizeof(*span));
dd_init_span_data_object(class_type, span, &ddtrace_span_data_handlers);
#if PHP_VERSION_ID < 80000
// Not handled in arginfo on these old versions
array_init(&span->property_baggage);
#endif
return &span->std;
}
static zend_object *ddtrace_inferred_span_data_create(zend_class_entry *class_type) {
ddtrace_inferred_span_data *span = ecalloc(1, sizeof(*span));
return dd_init_span_data_object(class_type, &span->span, &ddtrace_inferred_span_data_handlers);
}
static zend_object *ddtrace_root_span_data_create(zend_class_entry *class_type) {
ddtrace_root_span_data *span = ecalloc(1, sizeof(*span));
dd_init_span_data_object(class_type, &span->span, &ddtrace_root_span_data_handlers);
#if PHP_VERSION_ID < 80000
// Not handled in arginfo on these old versions
array_init(&span->property_propagated_tags);
array_init(&span->property_tracestate_tags);
array_init(&span->property_baggage);
#endif
return &span->std;
}
static zend_object *ddtrace_span_stack_create(zend_class_entry *class_type) {
ddtrace_span_stack *stack = ecalloc(1, sizeof(*stack));
zend_object_std_init(&stack->std, class_type);
stack->root_stack = stack;
stack->std.handlers = &ddtrace_span_stack_handlers;
object_properties_init(&stack->std, class_type);
ZVAL_NULL(&stack->property_parent); // readonly prop cannot be initialized in stub
// Explicitly assign property-mapped NULLs
stack->active = NULL;
stack->parent_stack = NULL;
#if PHP_VERSION_ID < 80000
// Not handled in arginfo on these old versions
array_init(&stack->property_span_creation_observers);
#endif
return &stack->std;
}
// Init with empty span stack if directly allocated via new()
static zend_function *ddtrace_span_data_get_constructor(zend_object *object) {
object_init_ex(&OBJ_SPANDATA(object)->property_stack, ddtrace_ce_span_stack);
return NULL;
}
static void ddtrace_span_stack_dtor_obj(zend_object *object) {
// We must not invoke span stack destructors during zend_objects_store_call_destructors to avoid them not being present for appsec rshutdown
if (EG(current_execute_data) == NULL && !DDTRACE_G(in_shutdown)) {
GC_DEL_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
return;
}
ddtrace_span_stack *stack = (ddtrace_span_stack *)object;
ddtrace_span_data *top;
while (stack->active && (top = SPANDATA(stack->active)) && top->stack == stack) {
dd_trace_stop_span_time(top);
// let's not stack swap to a) avoid side effects in destructors and b) avoid a crash on PHP 7.3 and older
ddtrace_close_top_span_without_stack_swap(top);
}
if (stack->closed_ring || stack->closed_ring_flush) {
// ensure dtor can be called again
GC_DEL_FLAGS(object, IS_OBJ_DESTRUCTOR_CALLED);
}
zend_objects_destroy_object(object);
}
// span stacks have intrinsic properties (managing other spans, can be switched to), unlike trivial span data which are just value objects
// thus we need to cleanup a little what exactly we can copy
#if PHP_VERSION_ID < 80000
static zend_object *ddtrace_span_stack_clone_obj(zval *old_zv) {
zend_object *old_obj = Z_OBJ_P(old_zv);
#else
static zend_object *ddtrace_span_stack_clone_obj(zend_object *old_obj) {
#endif
zend_object *new_obj = ddtrace_span_stack_create(old_obj->ce);
zend_objects_clone_members(new_obj, old_obj);
ddtrace_span_stack *stack = (ddtrace_span_stack *)new_obj;
ddtrace_span_stack *oldstack = (ddtrace_span_stack *)old_obj;
if (oldstack->parent_stack) { // if this is false, we're copying an initial stack
stack->root_stack = stack->parent_stack->root_stack;
stack->root_span = stack->parent_stack->root_span;
}
if (oldstack->root_stack == oldstack) {
stack->root_stack = stack;
}
ddtrace_span_properties *pspan = stack->active;
zval_ptr_dtor(&stack->property_active);
while (pspan && pspan->stack == oldstack) {
pspan = pspan->parent;
}
if (pspan) {
ZVAL_OBJ_COPY(&stack->property_active, &pspan->std);
} else {
if (oldstack->root_span && oldstack->root_span->stack == oldstack) {
stack->root_span = NULL;
}
stack->active = NULL;
ZVAL_NULL(&stack->property_active);
}
return new_obj;
}
// A span stack keeps a non-owning pointer to its root span (see ddtrace_init_span_stack, which
// copies it without taking a reference). Normally the parent-stack reference chain keeps the root
// span alive for as long as any stack aliases it. Out-of-order root span drops break that
// invariant: ddtrace_span_alter_root_span_config() (runtime config change) and the rejection
// branch of ddtrace_drop_span() free the root span while only NULLing the *current* stack's
// pointer, leaving sibling/descendant stacks dangling. The next cross-stack inherit
// (ddtrace_inherit_span_properties, reached from ddtrace_set_root_span_properties) then
// dereferences the freed span. It crashes on the baggage copy specifically once baggage is
// populated; an empty baggage stays the immutable ZVAL_EMPTY_ARRAY default whose pointer survives
// the freed read, which is why disabling baggage extraction masks the bug. Scrub every alias when
// the root span object is actually freed so the weak pointer can never outlive it, regardless of
// which drop path freed it.
static void dd_clear_dangling_root_span_aliases(ddtrace_root_span_data *root_span) {
zend_objects_store *objects = &EG(objects_store);
if (!objects->object_buckets) {
return;
}
zend_object **end = objects->object_buckets + 1;
zend_object **obj_ptr = objects->object_buckets + objects->top;
do {
obj_ptr--;
zend_object *obj = *obj_ptr;
if (IS_OBJ_VALID(obj) && obj->ce == ddtrace_ce_span_stack) {
ddtrace_span_stack *stack = (ddtrace_span_stack *)obj;
if (stack->root_span == root_span) {
stack->root_span = NULL;
}
}
} while (obj_ptr != end);
}
static void ddtrace_span_data_free_storage(zend_object *object) {
if (object->ce == ddtrace_ce_root_span_data) {
dd_clear_dangling_root_span_aliases((ddtrace_root_span_data *)object);
}
zend_object_std_dtor(object);
// Prevent use after free after zend_objects_store_free_object_storage is called (e.g. preloading) [PHP < 8.1]
memset(object->properties_table, 0, sizeof(ddtrace_span_data) - XtOffsetOf(ddtrace_span_data, std.properties_table));
}
#if PHP_VERSION_ID < 80000
static zend_object *ddtrace_span_data_clone_obj(zval *old_zv) {
zend_object *old_obj = Z_OBJ_P(old_zv);
#else
static zend_object *ddtrace_span_data_clone_obj(zend_object *old_obj) {
#endif
zend_object *new_obj = ddtrace_span_data_create(old_obj->ce);
zend_objects_clone_members(new_obj, old_obj);
return new_obj;
}
#if PHP_VERSION_ID < 80000
static zend_object *ddtrace_inferred_span_data_clone_obj(zval *old_zv) {
zend_object *old_obj = Z_OBJ_P(old_zv);
#else
static zend_object *ddtrace_inferred_span_data_clone_obj(zend_object *old_obj) {
#endif
zend_object *new_obj = ddtrace_inferred_span_data_create(old_obj->ce);
zend_objects_clone_members(new_obj, old_obj);
return new_obj;
}
#if PHP_VERSION_ID < 80000
static zend_object *ddtrace_root_span_data_clone_obj(zval *old_zv) {
zend_object *old_obj = Z_OBJ_P(old_zv);
#else
static zend_object *ddtrace_root_span_data_clone_obj(zend_object *old_obj) {
#endif
zend_object *new_obj = ddtrace_root_span_data_create(old_obj->ce);
zend_objects_clone_members(new_obj, old_obj);
return new_obj;
}
#if PHP_VERSION_ID < 80000
#if PHP_VERSION_ID >= 70400
static zval *ddtrace_span_data_readonly(zval *object, zval *member, zval *value, void **cache_slot) {
#else
static void ddtrace_span_data_readonly(zval *object, zval *member, zval *value, void **cache_slot) {
#endif
zend_object *obj = Z_OBJ_P(object);
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
#else
static zval *ddtrace_span_data_readonly(zend_object *object, zend_string *member, zval *value, void **cache_slot) {
zend_object *obj = object;
zend_string *prop_name = member;
#endif
if (zend_string_equals_literal(prop_name, "parent")
|| zend_string_equals_literal(prop_name, "id")
|| zend_string_equals_literal(prop_name, "stack")) {
zend_throw_error(zend_ce_error, "Cannot modify readonly property %s::$%s", ZSTR_VAL(obj->ce->name), ZSTR_VAL(prop_name));
#if PHP_VERSION_ID >= 70400
return &EG(uninitialized_zval);
#else
return;
#endif
}
ddtrace_span_data *span = OBJ_SPANDATA(obj);
// As per unified service tagging spec if a span is created with a service name different from the global
// service name it will not inherit the global version value, unless it has no ancestor traces.
if (zend_string_equals_literal(prop_name, "service")) {
cache_slot = NULL;
bool is_top_level_root = span->std.ce == ddtrace_ce_root_span_data;
if (is_top_level_root) {
for (ddtrace_span_stack *s = span->stack->parent_stack; s != NULL; s = s->parent_stack) {
if (s->active) {
is_top_level_root = false;
break;
}
}
}
if (ZSTR_LEN(get_DD_SERVICE()) || !is_top_level_root) {
if (!zend_is_identical(&span->property_service, value)) {
zval_ptr_dtor(&span->property_version);
ZVAL_EMPTY_STRING(&span->property_version);
}
}
}
#if PHP_VERSION_ID >= 70400
return zend_std_write_property(object, member, value, cache_slot);
#else
zend_std_write_property(object, member, value, cache_slot);
#endif
}
#if PHP_VERSION_ID < 80000
#if PHP_VERSION_ID >= 70400
static zval *ddtrace_root_span_data_write(zval *object, zval *member, zval *value, void **cache_slot) {
#else
static void ddtrace_root_span_data_write(zval *object, zval *member, zval *value, void **cache_slot) {
#endif
zend_object *obj = Z_OBJ_P(object);
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
#else
static zval *ddtrace_root_span_data_write(zend_object *object, zend_string *member, zval *value, void **cache_slot) {
zend_object *obj = object;
zend_string *prop_name = member;
#endif
ddtrace_root_span_data *span = ROOTSPANDATA(obj);
zval zv;
bool root_span_data_changed = false;
if (zend_string_equals_literal(prop_name, "parentId")) {
if (Z_TYPE_P(value) == IS_LONG && Z_LVAL_P(value)) {
span->parent_id = (uint64_t) Z_LVAL_P(value);
ZVAL_STR(&zv, zend_strpprintf(0, "%" PRIu64, span->parent_id));
Z_DELREF(zv); // zend_std_write_property will incref itself
value = &zv;
} else {
span->parent_id = ddtrace_parse_userland_span_id(value);
if (!span->parent_id) {
ZVAL_EMPTY_STRING(&zv);
Z_TRY_DELREF(zv);
value = &zv;
}
}
cache_slot = NULL;
} else if (zend_string_equals_literal(prop_name, "traceId")) {
span->trace_id = Z_TYPE_P(value) == IS_STRING ? ddtrace_parse_hex_trace_id(Z_STRVAL_P(value), Z_STRLEN_P(value)) : (datadog_trace_id){ 0 };
if (!span->trace_id.low && !span->trace_id.high) {
span->trace_id = (datadog_trace_id) {
.low = span->span_id,
.time = get_DD_TRACE_128_BIT_TRACEID_GENERATION_ENABLED() ? span->start / ZEND_NANO_IN_SEC : 0,
};
value = &span->property_id;
}
cache_slot = NULL;
} else if (zend_string_equals_literal(prop_name, "service")) {
if (ddtrace_span_is_entrypoint_root(&span->span) && !zend_is_identical(&span->property_service, value)) {
root_span_data_changed = true;
}
cache_slot = NULL;
} else if (zend_string_equals_literal(prop_name, "env")) {
if (ddtrace_span_is_entrypoint_root(&span->span) && !zend_is_identical(&span->property_env, value)) {
root_span_data_changed = true;
}
cache_slot = NULL;
} else if (zend_string_equals_literal(prop_name, "version")) {
if (ddtrace_span_is_entrypoint_root(&span->span) && !zend_is_identical(&span->property_version, value)) {
root_span_data_changed = true;
}
cache_slot = NULL;
} else if (zend_string_equals_literal(prop_name, "samplingPriority")) {
span->explicit_sampling_priority = zval_get_long(value) != DDTRACE_PRIORITY_SAMPLING_UNKNOWN;
cache_slot = NULL;
}
#if PHP_VERSION_ID >= 70400
zval *ret = ddtrace_span_data_readonly(object, member, value, cache_slot);
#else
ddtrace_span_data_readonly(object, member, value, cache_slot);
#endif
if (root_span_data_changed) {
ddtrace_sidecar_submit_root_span_data();
}
#if PHP_VERSION_ID >= 70400
return ret;
#endif
}
static bool ddtrace_span_stack_is_context_property(zend_string *prop_name) {
return zend_string_equals_literal(prop_name, "active")
|| zend_string_equals_literal(prop_name, "parent");
}
#if PHP_VERSION_ID < 80000
static zval *ddtrace_span_stack_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv) {
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
ddtrace_span_stack *stack = (ddtrace_span_stack *)Z_OBJ_P(object);
#else
static zval *ddtrace_span_stack_read_property(zend_object *object, zend_string *member, int type, void **cache_slot, zval *rv) {
zend_string *prop_name = member;
ddtrace_span_stack *stack = (ddtrace_span_stack *)object;
#endif
if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
&& ddtrace_span_stack_is_context_property(prop_name)) {
if (zend_string_equals_literal(prop_name, "active")) {
ZVAL_COPY(rv, &stack->property_active);
} else {
ZVAL_COPY(rv, &stack->property_parent);
}
return rv;
}
return zend_std_read_property(object, member, type, cache_slot, rv);
}
#if PHP_VERSION_ID < 80000
static zval *ddtrace_span_stack_get_property_ptr_ptr(zval *object, zval *member, int type, void **cache_slot) {
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
#else
static zval *ddtrace_span_stack_get_property_ptr_ptr(zend_object *object, zend_string *member, int type, void **cache_slot) {
zend_string *prop_name = member;
#endif
if ((type == BP_VAR_W || type == BP_VAR_RW || type == BP_VAR_UNSET)
&& ddtrace_span_stack_is_context_property(prop_name)) {
return NULL; // prevent cache fill; read_property handles the copy
}
return zend_std_get_property_ptr_ptr(object, member, type, cache_slot);
}
#if PHP_VERSION_ID < 80000
static void ddtrace_span_stack_unset_property(zval *object, zval *member, void **cache_slot) {
zend_object *obj = Z_OBJ_P(object);
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
#else
static void ddtrace_span_stack_unset_property(zend_object *object, zend_string *member, void **cache_slot) {
zend_object *obj = object;
zend_string *prop_name = member;
#endif
if (ddtrace_span_stack_is_context_property(prop_name)) {
zend_throw_error(zend_ce_error, "Cannot unset readonly property %s::$%s", ZSTR_VAL(obj->ce->name), ZSTR_VAL(prop_name));
return;
}
zend_std_unset_property(object, member, cache_slot);
}
#if PHP_VERSION_ID < 80000
#if PHP_VERSION_ID >= 70400
static zval *ddtrace_span_stack_readonly(zval *object, zval *member, zval *value, void **cache_slot) {
#else
static void ddtrace_span_stack_readonly(zval *object, zval *member, zval *value, void **cache_slot) {
#endif
zend_object *obj = Z_OBJ_P(object);
zend_string *prop_name = Z_TYPE_P(member) == IS_STRING ? Z_STR_P(member) : ZSTR_EMPTY_ALLOC();
#else
static zval *ddtrace_span_stack_readonly(zend_object *object, zend_string *member, zval *value, void **cache_slot) {
zend_object *obj = object;
zend_string *prop_name = member;
#endif
if (ddtrace_span_stack_is_context_property(prop_name)) {
zend_throw_error(zend_ce_error, "Cannot modify readonly property %s::$%s", ZSTR_VAL(obj->ce->name), ZSTR_VAL(prop_name));
#if PHP_VERSION_ID >= 70400
return &EG(uninitialized_zval);
#else
return;
#endif
}
#if PHP_VERSION_ID >= 70400
return zend_std_write_property(object, member, value, cache_slot);
#else
zend_std_write_property(object, member, value, cache_slot);
#endif
}
PHP_METHOD(DDTrace_SpanData, getDuration) {
ddtrace_span_data *span = OBJ_SPANDATA(Z_OBJ_P(ZEND_THIS));
RETURN_LONG(span->duration);
}
PHP_METHOD(DDTrace_SpanData, getStartTime) {
ddtrace_span_data *span = OBJ_SPANDATA(Z_OBJ_P(ZEND_THIS));
RETURN_LONG(span->start);
}
PHP_METHOD(DDTrace_SpanData, getLink) {
ddtrace_span_data *span = OBJ_SPANDATA(Z_OBJ_P(ZEND_THIS));
span->flags |= DDTRACE_SPAN_FLAG_NOT_DROPPABLE;
zval fci_zv;
object_init_ex(&fci_zv, ddtrace_ce_span_link);
ddtrace_span_link *link = (ddtrace_span_link *)Z_OBJ_P(&fci_zv);
ZVAL_STR(&link->property_trace_id, datadog_trace_id_as_hex_string(span->root ? span->root->trace_id : (datadog_trace_id){ .low = span->span_id, .high = 0 }));
ZVAL_STR(&link->property_span_id, ddtrace_span_id_as_hex_string(span->span_id));
RETURN_OBJ(Z_OBJ(fci_zv));
}
PHP_METHOD(DDTrace_SpanData, hexId) {
ddtrace_span_data *span = OBJ_SPANDATA(Z_OBJ_P(ZEND_THIS));
RETURN_STR(ddtrace_span_id_as_hex_string(span->span_id));
}
static void dd_register_span_data_ce(void) {
ddtrace_ce_span_data = register_class_DDTrace_SpanData();
ddtrace_ce_span_data->create_object = ddtrace_span_data_create;
memcpy(&ddtrace_span_data_handlers, &std_object_handlers, sizeof(zend_object_handlers));
ddtrace_span_data_handlers.offset = XtOffsetOf(ddtrace_span_data, std);
ddtrace_span_data_handlers.clone_obj = ddtrace_span_data_clone_obj;
ddtrace_span_data_handlers.free_obj = ddtrace_span_data_free_storage;
ddtrace_span_data_handlers.write_property = ddtrace_span_data_readonly;
ddtrace_span_data_handlers.get_constructor = ddtrace_span_data_get_constructor;
ddtrace_ce_inferred_span_data = register_class_DDTrace_InferredSpanData(ddtrace_ce_span_data);
ddtrace_ce_inferred_span_data->create_object = ddtrace_inferred_span_data_create;
memcpy(&ddtrace_inferred_span_data_handlers, &ddtrace_span_data_handlers, sizeof(zend_object_handlers));
ddtrace_inferred_span_data_handlers.offset = XtOffsetOf(ddtrace_inferred_span_data, std);
ddtrace_inferred_span_data_handlers.clone_obj = ddtrace_inferred_span_data_clone_obj;
ddtrace_ce_root_span_data = register_class_DDTrace_RootSpanData(ddtrace_ce_span_data);
ddtrace_ce_root_span_data->create_object = ddtrace_root_span_data_create;
#if PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100
// Work around wrong reference source for typed internal properties by preventing duplication of them
// php -d extension=zend_test -r '$c = new _ZendTestChildClass; $i = &$c->intProp;'
// php: /usr/local/src/php/Zend/zend_execute.c:3390: zend_ref_del_type_source: Assertion `source_list->ptr == prop' failed.
zend_hash_init(&dd_root_span_data_duplicated_properties_table, zend_hash_num_elements(&ddtrace_ce_span_data->properties_info), NULL, NULL, true);
for (uint32_t i = 0; i < zend_hash_num_elements(&ddtrace_ce_span_data->properties_info); ++i) {
Bucket *bucket = &ddtrace_ce_root_span_data->properties_info.arData[i];
zend_hash_add_ptr(&dd_root_span_data_duplicated_properties_table, bucket->key, Z_PTR(bucket->val));
Z_PTR(bucket->val) = ddtrace_ce_root_span_data->properties_info_table[i] = Z_PTR(ddtrace_ce_span_data->properties_info.arData[i].val);
}
#endif
memcpy(&ddtrace_root_span_data_handlers, &ddtrace_span_data_handlers, sizeof(zend_object_handlers));
ddtrace_root_span_data_handlers.offset = XtOffsetOf(ddtrace_root_span_data, std);
ddtrace_root_span_data_handlers.clone_obj = ddtrace_root_span_data_clone_obj;
ddtrace_root_span_data_handlers.write_property = ddtrace_root_span_data_write;
ddtrace_ce_span_stack = register_class_DDTrace_SpanStack();
ddtrace_ce_span_stack->create_object = ddtrace_span_stack_create;
memcpy(&ddtrace_span_stack_handlers, &std_object_handlers, sizeof(zend_object_handlers));
ddtrace_span_stack_handlers.clone_obj = ddtrace_span_stack_clone_obj;
ddtrace_span_stack_handlers.dtor_obj = ddtrace_span_stack_dtor_obj;
ddtrace_span_stack_handlers.read_property = ddtrace_span_stack_read_property;
ddtrace_span_stack_handlers.get_property_ptr_ptr = ddtrace_span_stack_get_property_ptr_ptr;
ddtrace_span_stack_handlers.unset_property = ddtrace_span_stack_unset_property;
ddtrace_span_stack_handlers.write_property = ddtrace_span_stack_readonly;
}
/* DDTrace\FatalError */
zend_class_entry *ddtrace_ce_fatal_error;
static void dd_register_fatal_error_ce(void) {
zend_class_entry ce;
INIT_NS_CLASS_ENTRY(ce, "DDTrace", "FatalError", NULL);
ddtrace_ce_fatal_error = zend_register_internal_class_ex(&ce, zend_ce_exception);
}
zend_class_entry *ddtrace_ce_integration;
zend_class_entry *ddtrace_ce_git_metadata;
zend_object_handlers datadog_git_metadata_handlers;
static zend_object *datadog_git_metadata_create(zend_class_entry *class_type) {
zend_object *object = zend_objects_new(class_type);
object_properties_init(object, class_type);
object->handlers = &datadog_git_metadata_handlers;
return object;
}
static void ddtrace_free_obj_wrapper(zend_object *object) {
zend_object_std_dtor(object);
}
void ddtrace_register_functions_and_classes(int module_number) {
register_ddtrace_symbols(module_number);
dd_register_span_data_ce();
dd_register_fatal_error_ce();
ddtrace_ce_integration = register_class_DDTrace_Integration();
ddtrace_ce_ffe_result = register_class_DDTrace_FfeResult();
ddtrace_ce_span_link = register_class_DDTrace_SpanLink(php_json_serializable_ce);
ddtrace_ce_span_event = register_class_DDTrace_SpanEvent(php_json_serializable_ce);
ddtrace_ce_exception_span_event = register_class_DDTrace_ExceptionSpanEvent(ddtrace_ce_span_event);
ddtrace_ce_git_metadata = register_class_DDTrace_GitMetadata();
ddtrace_ce_git_metadata->create_object = datadog_git_metadata_create;
memcpy(&datadog_git_metadata_handlers, &std_object_handlers, sizeof(zend_object_handlers));
// We need a free_obj wrapper as zend_objects_store_free_object_storage will skip freeing of classes with the default free_obj handler when fast_shutdown is active. This will mess with our refcount and leak cached git metadata.
datadog_git_metadata_handlers.free_obj = ddtrace_free_obj_wrapper;
zend_register_functions(NULL, ext_functions, NULL, datadog_module_entry.type);
}
void ddtrace_unregister_functions_and_classes() {
#if PHP_VERSION_ID < 80300
zend_unregister_functions(ext_functions, sizeof(ext_functions) / sizeof(zend_function_entry) - 1, NULL);
#endif
#if PHP_VERSION_ID >= 80000 && PHP_VERSION_ID < 80100
// See dd_register_span_data_ce for explanation
zend_string *key;
void *prop_info;
ZEND_HASH_FOREACH_STR_KEY_PTR(&dd_root_span_data_duplicated_properties_table, key, prop_info) {
ZVAL_PTR(zend_hash_find(&ddtrace_ce_root_span_data->properties_info, key), prop_info); // no update to avoid dtor
} ZEND_HASH_FOREACH_END();
#endif
}
/* {{{ proto string DDTrace\add_global_tag(string $key, string $value) */
PHP_FUNCTION(DDTrace_add_global_tag) {
UNUSED(execute_data);
zend_string *key, *val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
RETURN_NULL();
}
if (!get_DD_TRACE_ENABLED()) {
RETURN_NULL();
}
zval value_zv;
ZVAL_STR_COPY(&value_zv, val);
zend_hash_update(DDTRACE_G(additional_global_tags), key, &value_zv);
RETURN_NULL();
}
/* {{{ proto string DDTrace\add_distributed_tag(string $key, string $value) */
PHP_FUNCTION(DDTrace_add_distributed_tag) {
UNUSED(execute_data);
zend_string *key, *val;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS", &key, &val) == FAILURE) {
RETURN_NULL();
}
if (!get_DD_TRACE_ENABLED()) {
RETURN_NULL();
}
zend_string *prefixed_key = zend_strpprintf(0, "_dd.p.%s", ZSTR_VAL(key));
zend_array *target_table, *propagated;
if (DDTRACE_G(active_stack)->root_span) {
target_table = ddtrace_property_array(&DDTRACE_G(active_stack)->root_span->property_meta);
propagated = ddtrace_property_array(&DDTRACE_G(active_stack)->root_span->property_propagated_tags);
} else {
target_table = &DDTRACE_G(root_span_tags_preset);
propagated = &DDTRACE_G(propagated_root_span_tags);
}
zval value_zv;
ZVAL_STR_COPY(&value_zv, val);
zend_hash_update(target_table, prefixed_key, &value_zv);
zend_hash_add_empty_element(propagated, prefixed_key);
zend_string_release(prefixed_key);
RETURN_NULL();
}
static void _ddtrace_set_user(zend_string *user_id, zend_array *metadata, zend_bool propagate) {
zend_array *target_table, *propagated;
if (DDTRACE_G(active_stack)->root_span) {
target_table = ddtrace_property_array(&DDTRACE_G(active_stack)->root_span->property_meta);
propagated = ddtrace_property_array(&DDTRACE_G(active_stack)->root_span->property_propagated_tags);
} else {
target_table = &DDTRACE_G(root_span_tags_preset);
propagated = &DDTRACE_G(propagated_root_span_tags);
}
zval user_id_zv;
ZVAL_STR_COPY(&user_id_zv, user_id);
zend_hash_str_update(target_table, ZEND_STRL("usr.id"), &user_id_zv);
if (propagate) {
zval value_zv;
zend_string *encoded_user_id = php_base64_encode_str(user_id);
ZVAL_STR(&value_zv, encoded_user_id);
zend_hash_str_update(target_table, ZEND_STRL("_dd.p.usr.id"), &value_zv);
zend_hash_str_add_empty_element(propagated, ZEND_STRL("_dd.p.usr.id"));
}
if (metadata != NULL) {
zend_string *key;
zval *value;
ZEND_HASH_FOREACH_STR_KEY_VAL(metadata, key, value)
{
if (!key || Z_TYPE_P(value) != IS_STRING) {
continue;
}
zend_string *prefixed_key = zend_strpprintf(0, "usr.%s", ZSTR_VAL(key));
zval value_copy;
ZVAL_COPY(&value_copy, value);
zend_hash_update(target_table, prefixed_key, &value_copy);
zend_string_release(prefixed_key);
}
ZEND_HASH_FOREACH_END();
}
}
PHP_FUNCTION(DDTrace_set_user) {
UNUSED(execute_data);
zend_string *user_id;
HashTable *metadata = NULL;
zend_bool propagate = get_DD_TRACE_PROPAGATE_USER_ID_DEFAULT();
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|hb", &user_id, &metadata, &propagate) == FAILURE) {
RETURN_NULL();
}
if (!get_DD_TRACE_ENABLED()) {
RETURN_NULL();
}
if (user_id == NULL || ZSTR_LEN(user_id) == 0) {
LOG_LINE(WARN, "Unexpected empty user id in DDTrace\\set_user");
RETURN_NULL();
}
_ddtrace_set_user(user_id, metadata, propagate);
RETURN_NULL();
}
PHP_FUNCTION(datadog_appsec_v2_track_user_login_success) {
UNUSED(execute_data);
zend_string *login;
zval *user = NULL;
zend_array *metadata = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|zh", &login, &user, &metadata) == FAILURE) {
RETURN_NULL();
}
if (!get_DD_TRACE_ENABLED()) {
RETURN_NULL();
}
zend_array *target_table;
if (DDTRACE_G(active_stack)->root_span) {
target_table = ddtrace_property_array(&DDTRACE_G(active_stack)->root_span->property_meta);
} else {
target_table = &DDTRACE_G(root_span_tags_preset);
}
if (ZSTR_LEN(login) == 0) {
LOG_LINE(WARN, "Unexpected empty login in datadog\\appsec\\v2\\track_user_login_success");
RETURN_NULL();
}
#define DDTRACE_ATO_V2_EVENT_USERS_LOGIN_SUCCESS "appsec.events.users.login.success"
zend_string *user_id = NULL;