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

fix: Allow hooks to be removed after class binding #2422

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
remove_hook() with class argument
--FILE--
<?php

interface Elder {
function foo();
}

$id = DDTrace\install_hook("Elder::foo", function () { print "HOOKED: " . static::class . "\n"; });

if (time()) {
abstract class Child implements Elder {}

class GrandChild extends Child {
function foo() {
print static::class . "\n";
}
}
}

DDTrace\remove_hook($id, "GrandChild");

(new GrandChild)->foo(); // no hook

?>
--EXPECT--
GrandChild
43 changes: 43 additions & 0 deletions tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
--TEST--
Remove hook
--ENV--
DD_TRACE_LOGS_ENABLED=false
--FILE--
<?php

namespace Psr\Log {
interface LoggerInterface {
public function log($level, $message, array $context = array());
}

abstract class AbstractLogger implements LoggerInterface {
public function log($level, $message, array $context = array()) {
echo static::class . "\n";
}
}

class NullLogger extends AbstractLogger {
public function log($level, $message, array $context = array()) {
echo static::class . "\n";
}
}
}


namespace {
$hook = \DDTrace\install_hook("Psr\Log\LoggerInterface::log", function () {
echo "HOOKED: " . static::class . "\n";
});
\DDTrace\remove_hook($hook, \Psr\Log\NullLogger::class);

$logger = new \Psr\Log\NullLogger();
for ($i = 0; $i < 3; $i++) {
$logger->log("info", "Hello World!");
}
}

?>
--EXPECTF--
Psr\Log\NullLogger
Psr\Log\NullLogger
Psr\Log\NullLogger
11 changes: 11 additions & 0 deletions zend_abstract_interface/hook/hook.c
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,17 @@ zai_hook_continued zai_hook_continue(zend_execute_data *ex, zai_hook_memory_t *m
}

if (check_scope) {
if (zai_hook_is_excluded(hook, ex->func->common.scope)) {
// hook is no longer valid, remove it
zai_hook_remove_from_entry(hooks, hook->id);

zend_hash_iterator_del(ht_iter);
memory->hook_count = (zend_ulong)hook_num;
zai_hook_finish(ex, NULL, memory);

return ZAI_HOOK_BAILOUT;
}

if (!(hook->resolved_scope->ce_flags & ZEND_ACC_TRAIT) && !instanceof_function(zend_get_called_scope(ex), hook->resolved_scope)) {
continue;
}
Expand Down