diff --git a/CHANGES b/CHANGES index 8f029325b7..6f8f4b2de5 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,23 @@ +7.1.0-dev.590 | 2024-11-21 14:40:53 +0100 + + * plugin: Add InitPreExecution() (Arne Welzel, Corelight) + + Currently, plugins do not have a way to further inspect or even mutate + script functions after ZAM optimization ran. One use-case here is + zeek-perf-support [1]. This plugin wraps Stmt instances of functions, + events and hooks hooks with a small assembly stub to support JIT map + files [2] and for integration with perf tools. + + This change introduces a new InitPreExecution() hook that runs after + ZAM optimization completed, just before the zeek_init() event is enqueued. + + Additionally, remove the existing CPP_activation_hook. It doesn't seem + to be used. If it becomes necessary in the future, the new + InitPreExecution() hook can be leveraged instead. + + [1] https://github.com/zeek/zeek-perf-support + [2] https://github.com/torvalds/linux/blob/master/tools/perf/Documentation/jit-interface.txt + 7.1.0-dev.588 | 2024-11-21 10:22:21 +0100 * Bump zeekjs to v0.14.0 (Arne Welzel, Corelight) diff --git a/NEWS b/NEWS index 70450f07e3..8b5382f528 100644 --- a/NEWS +++ b/NEWS @@ -77,6 +77,10 @@ New Functionality event signature_match(state: signature_state, msg: string, data: string, end_of_match: count); +* A we plugin hook ``InitPreExecution()`` has been added to allow introspection + of Zeek's AST after ZAM optimizations ran. This hook executes right before + the ``zeek_init()`` event is enqueued. + Changed Functionality --------------------- diff --git a/VERSION b/VERSION index 18551f1692..472c511dcc 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -7.1.0-dev.588 +7.1.0-dev.590 diff --git a/src/plugin/Manager.cc b/src/plugin/Manager.cc index aae835839a..c8018a395f 100644 --- a/src/plugin/Manager.cc +++ b/src/plugin/Manager.cc @@ -484,6 +484,14 @@ void Manager::InitPostScript() { (*i)->InitPostScript(); } +void Manager::InitPreExecution() { + assert(init); + + for ( plugin_list::iterator i = Manager::ActivePluginsInternal()->begin(); + i != Manager::ActivePluginsInternal()->end(); i++ ) + (*i)->InitPreExecution(); +} + void Manager::FinishPlugins() { assert(init); diff --git a/src/plugin/Manager.h b/src/plugin/Manager.h index 54cfd8a514..f430774241 100644 --- a/src/plugin/Manager.h +++ b/src/plugin/Manager.h @@ -122,6 +122,13 @@ public: */ void InitPostScript(); + /** + * Fourth-stage initialization of the manager. This is called just before + * enqueuing zeek_init(), after script analysis and optimization completed. + * It forwards to the corresponding Plugin methods. + */ + void InitPreExecution(); + /** * Finalizes all plugins at termination time. This forwards to the * corresponding Plugin \a Done() methods. diff --git a/src/plugin/Plugin.cc b/src/plugin/Plugin.cc index 153f5fc983..fa0338ef0f 100644 --- a/src/plugin/Plugin.cc +++ b/src/plugin/Plugin.cc @@ -249,6 +249,8 @@ void Plugin::InitPreScript() {} void Plugin::InitPostScript() {} +void Plugin::InitPreExecution() {} + Plugin::bif_item_list Plugin::BifItems() const { return bif_items; } void Plugin::Done() { diff --git a/src/plugin/Plugin.h b/src/plugin/Plugin.h index 78304d4483..3cea1d14d4 100644 --- a/src/plugin/Plugin.h +++ b/src/plugin/Plugin.h @@ -748,6 +748,14 @@ protected: */ virtual void InitPostScript(); + /** + * Third-stage initialization of the plugin called just before enqueueing + * zeek_init(), after script analysis and optimization completed. + * This can be overridden by derived classes; they must however call the + * parent's implementation. + */ + virtual void InitPreExecution(); + /** * Finalizer method that derived classes can override for performing * custom tasks at shutdown. This can be overridden by derived diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index f515a4f16e..47effda307 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -26,7 +26,6 @@ AnalyOpt analysis_options; std::unordered_set non_recursive_funcs; void (*CPP_init_hook)() = nullptr; -void (*CPP_activation_hook)() = nullptr; // Tracks all of the loaded functions (including event handlers and hooks). static std::vector funcs; diff --git a/src/script_opt/ScriptOpt.h b/src/script_opt/ScriptOpt.h index 1a8c4c25fe..441c8d6ec2 100644 --- a/src/script_opt/ScriptOpt.h +++ b/src/script_opt/ScriptOpt.h @@ -271,8 +271,4 @@ extern bool IsZAM_BuiltInCond(const CallExpr* c); // to a non-empty value. extern void (*CPP_init_hook)(); -// Used for "standalone" C++-compiled scripts to complete their activation; -// called after parsing and BiF initialization, but before zeek_init. -extern void (*CPP_activation_hook)(); - } // namespace zeek::detail diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index ad7091ccf1..8fdfd5111a 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -992,8 +992,8 @@ SetupResult setup(int argc, char** argv, Options* zopts) { // we don't have any other source for it. run_state::detail::update_network_time(util::current_time()); - if ( CPP_activation_hook ) - (*CPP_activation_hook)(); + // Plugin pre-execution hook. + plugin_mgr->InitPreExecution(); if ( zeek_init ) event_mgr.Enqueue(zeek_init, Args{});