Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 2143104

Browse files
authored
Fixing Segfault/garbage data for attributes (#93)
* Add failing test for incorrect gc count for attributes * Fix the GC count for the returned attributes array. When returning our internal HashTable as a zval to user-space, we need to increment the GC count. If the user-space variable is destroyed, then our HashTable with our attributes could be destroyed. References to these values could work, could segfault, or could have garbage data.
1 parent b0717a0 commit 2143104

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

ext/opencensus_trace_span.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ int opencensus_trace_span_to_zval(opencensus_trace_span_t *trace_span, zval *spa
533533
zend_update_property_double(opencensus_trace_span_ce, span, "endTime", sizeof("endTime") - 1, trace_span->stop);
534534

535535
array_init(&attributes);
536-
zend_hash_copy(Z_ARRVAL(attributes), trace_span->attributes, NULL);
536+
zend_hash_copy(Z_ARRVAL(attributes), trace_span->attributes, zval_add_ref);
537537
zend_update_property(opencensus_trace_span_ce, span, "attributes", sizeof("attributes") - 1, &attributes);
538538

539539
zend_update_property(opencensus_trace_span_ce, span, "stackTrace", sizeof("stackTrace") - 1, &trace_span->stackTrace);

ext/tests/bug_001.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
--TEST--
2+
OpenCensus Trace: Bug 001: Attributes incorrect GC count yields garbage data.
3+
--FILE--
4+
<?php
5+
6+
function myEcho ($var) {
7+
echo $var . PHP_EOL;
8+
}
9+
10+
function execute() {
11+
$str = "Hello World!";
12+
myEcho($str);
13+
}
14+
15+
function allocate_strings($count) {
16+
$foo = "asdf";
17+
for ($i = 0; $i < $count; $i++) {
18+
$foo .= ",$i";
19+
}
20+
}
21+
22+
function inspect() {
23+
$span = opencensus_trace_list()[0];
24+
print_r($span->attributes());
25+
}
26+
27+
// 1: Sanity test a simple profile run
28+
opencensus_trace_function("myEcho", function ($x) {
29+
return ['name' => 'foo', 'startTime' => 0.1, 'attributes' => ['text' => $x]];
30+
});
31+
32+
execute();
33+
execute();
34+
35+
inspect();
36+
inspect();
37+
38+
?>
39+
--EXPECT--
40+
Hello World!
41+
Hello World!
42+
Array
43+
(
44+
[text] => Hello World!
45+
)
46+
Array
47+
(
48+
[text] => Hello World!
49+
)

0 commit comments

Comments
 (0)