Skip to content

Commit

Permalink
Prevent execution of install_hook() hooks after disabling tracer (#2016)
Browse files Browse the repository at this point in the history
This led to crashes in combination with the database integrations, if database calls happened after the tracer was disabled.

Signed-off-by: Bob Weinand <bob.weinand@datadoghq.com>
  • Loading branch information
bwoebi authored Apr 14, 2023
1 parent 3bddfc8 commit 00b3721
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions ext/hook/uhook.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ static bool dd_uhook_begin(zend_ulong invocation, zend_execute_data *execute_dat
dd_uhook_def *def = auxiliary;
dd_uhook_dynamic *dyn = dynamic;

if (def->closure && def->closure != ZEND_CLOSURE_OBJECT(EX(func))) {
if ((def->closure && def->closure != ZEND_CLOSURE_OBJECT(EX(func))) || !get_DD_TRACE_ENABLED()) {
dyn->hook_data = NULL;
return true;
}
Expand Down Expand Up @@ -219,7 +219,7 @@ static void dd_uhook_end(zend_ulong invocation, zend_execute_data *execute_data,
dd_trace_stop_span_time(span);
}

if (def->end && !def->running) {
if (def->end && !def->running && get_DD_TRACE_ENABLED()) {
zval tmp;

/* If the profiler doesn't handle a potential pending interrupt before
Expand Down Expand Up @@ -480,7 +480,7 @@ void dd_uhook_span(INTERNAL_FUNCTION_PARAMETERS, bool unlimited) {
}

// pre-hook check
if (!hookData->execute_data || (!unlimited && ddtrace_tracer_is_limited())) {
if (!hookData->execute_data || (!unlimited && ddtrace_tracer_is_limited()) || !get_DD_TRACE_ENABLED()) {
// dummy span, which never gets pushed
hookData->span = ddtrace_init_dummy_span();
RETURN_OBJ_COPY(&hookData->span->std);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Ensure hooks are not invoked after the tracer has been disabled
--FILE--
<?php

function foo() {}
DDTrace\install_hook("foo", function($h) {
print "Invoked\n";
ini_set("datadog.trace.enabled", 0);
$h->span(); // must not crash
}, function() {
print "Skipped\n";
});
foo();
foo();

?>
--EXPECT--
Invoked

0 comments on commit 00b3721

Please sign in to comment.