From 14dccc7dc992d5f83805b80170579726f64420b0 Mon Sep 17 00:00:00 2001 From: Alexandre Choura Date: Wed, 13 Dec 2023 13:18:13 +0100 Subject: [PATCH 1/2] fix: Exclude hook after class binding --- .../exclude_inherited_hook_after_binding.phpt | 28 ++++++++++++ .../install_hook/remove_hook_null_logger.phpt | 44 +++++++++++++++++++ zend_abstract_interface/hook/hook.c | 11 +++++ 3 files changed, 83 insertions(+) create mode 100644 tests/ext/sandbox/install_hook/exclude_inherited_hook_after_binding.phpt create mode 100644 tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt diff --git a/tests/ext/sandbox/install_hook/exclude_inherited_hook_after_binding.phpt b/tests/ext/sandbox/install_hook/exclude_inherited_hook_after_binding.phpt new file mode 100644 index 0000000000..8f3c8ed383 --- /dev/null +++ b/tests/ext/sandbox/install_hook/exclude_inherited_hook_after_binding.phpt @@ -0,0 +1,28 @@ +--TEST-- +remove_hook() with class argument +--FILE-- +foo(); // no hook + +?> +--EXPECT-- +GrandChild diff --git a/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt b/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt new file mode 100644 index 0000000000..718a29a6d8 --- /dev/null +++ b/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt @@ -0,0 +1,44 @@ +--TEST-- +Remove hook +--ENV-- +DD_TRACE_LOGS_ENABLED=false +DD_TRACE_DEBUG=1 +--FILE-- +log("info", "Hello World!"); + } +} + +?> +--EXPECTF-- +Psr\Log\NullLogger +Psr\Log\NullLogger +Psr\Log\NullLogger diff --git a/zend_abstract_interface/hook/hook.c b/zend_abstract_interface/hook/hook.c index 0b064cd6ea..eceda5ed46 100644 --- a/zend_abstract_interface/hook/hook.c +++ b/zend_abstract_interface/hook/hook.c @@ -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; } From 10f1b5b8ccb97ab52fbef8d7f62fc7b4622b0129 Mon Sep 17 00:00:00 2001 From: Alexandre Choura Date: Wed, 13 Dec 2023 13:36:51 +0100 Subject: [PATCH 2/2] tests: Remove debug logs --- tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt b/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt index 718a29a6d8..7d56cd3d1b 100644 --- a/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt +++ b/tests/ext/sandbox/install_hook/remove_hook_null_logger.phpt @@ -2,7 +2,6 @@ Remove hook --ENV-- DD_TRACE_LOGS_ENABLED=false -DD_TRACE_DEBUG=1 --FILE--