Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid resolving unresolved functions and classes #2450

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/ext/traced_attribute.phpt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--TEST--
Set DDTrace\start_span() properties
Test tracing via attributes
--SKIPIF--
<?php if (PHP_VERSION_ID < 80000) die('skip: No attributes pre-PHP 8'); ?>
--ENV--
Expand Down
57 changes: 57 additions & 0 deletions tests/ext/traced_attribute_delayed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
--TEST--
Test delayed resolution of tracing attributes
--SKIPIF--
<?php if (PHP_VERSION_ID < 80000) die('skip: No attributes pre-PHP 8'); ?>
--ENV--
DD_TRACE_GENERATE_ROOT_SPAN=0
--FILE--
<?php

include __DIR__ . '/sandbox/dd_dumper.inc';

$eval = <<<'EVAL'
eval("class Bar {}");

class Foo extends Bar {
#[DDTrace\Trace(name: "simpleclass")]
static function simple($arg) { return $arg; }
}
EVAL;

$oldId = DDTrace\install_hook("somefunction", function() {});
eval($eval);
$newId = DDTrace\install_hook("somefunction", function() {});

Foo::simple(1);

var_dump($newId - $oldId - 1); // we check how many hooks were installed in between, to ensure that it was installed only once

$eval = <<<'EVAL'
if (time()) {
#[DDTrace\Trace(name: "simplefunc")]
function simple($arg) { return $arg; }
}
EVAL;

$oldId = DDTrace\install_hook("somefunction", function() {});
eval($eval);
$newId = DDTrace\install_hook("somefunction", function() {});

simple(1);

var_dump($newId - $oldId - 1); // we check how many hooks were installed in between, to ensure that it was installed only once

dd_dump_spans();

?>
--EXPECTF--
int(1)
int(1)
spans(\DDTrace\SpanData) (2) {
simpleclass (traced_attribute_delayed.php, simpleclass, cli)
_dd.p.dm => -0
_dd.p.tid => %s
simplefunc (traced_attribute_delayed.php, simplefunc, cli)
_dd.p.dm => -0
_dd.p.tid => %s
}
15 changes: 12 additions & 3 deletions zend_abstract_interface/interceptor/php8/resolver_pre-8_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ static void zai_interceptor_add_new_entries(HashPosition classpos, HashPosition
for (zend_class_entry *ce;
(ce = zend_hash_get_current_data_ptr_ex(CG(class_table), &classpos));
zend_hash_move_forward_ex(CG(class_table), &classpos)) {
zend_hash_get_current_key_ex(CG(class_table), &lcname, &index, &classpos);
zai_hook_resolve_class(ce, lcname);
if (ce->ce_flags & ZEND_ACC_LINKED) {
zend_hash_get_current_key_ex(CG(class_table), &lcname, &index, &classpos);
zai_hook_resolve_class(ce, lcname);
}
}

zend_hash_move_forward_ex(CG(function_table), &funcpos); // move past previous end
for (zend_function *func;
(func = zend_hash_get_current_data_ptr_ex(CG(function_table), &funcpos));
zend_hash_move_forward_ex(CG(function_table), &funcpos)) {
zend_hash_get_current_key_ex(CG(function_table), &lcname, &index, &funcpos);
zai_hook_resolve_function(func, lcname);
#if PHP_VERSION_ID < 80100
// check for unlinked function: "rtd_key" (i.e. lcname) starts with NUL-byte
// On PHP 8.1+ these are attached to the op_array instead and not present in CG(function_table).
if (ZSTR_VAL(lcname)[0])
#endif
{
zai_hook_resolve_function(func, lcname);
}
}
}

Expand Down
Loading