Merge remote-tracking branch 'origin/topic/awelzel/plugin-init-pre-execution'

* origin/topic/awelzel/plugin-init-pre-execution:
  plugin: Add InitPreExecution()
This commit is contained in:
Arne Welzel 2024-11-21 14:40:53 +01:00
commit 94ffcd6535
10 changed files with 52 additions and 8 deletions

20
CHANGES
View file

@ -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 7.1.0-dev.588 | 2024-11-21 10:22:21 +0100
* Bump zeekjs to v0.14.0 (Arne Welzel, Corelight) * Bump zeekjs to v0.14.0 (Arne Welzel, Corelight)

4
NEWS
View file

@ -77,6 +77,10 @@ New Functionality
event signature_match(state: signature_state, msg: string, data: string, end_of_match: count); 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 Changed Functionality
--------------------- ---------------------

View file

@ -1 +1 @@
7.1.0-dev.588 7.1.0-dev.590

View file

@ -484,6 +484,14 @@ void Manager::InitPostScript() {
(*i)->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() { void Manager::FinishPlugins() {
assert(init); assert(init);

View file

@ -122,6 +122,13 @@ public:
*/ */
void InitPostScript(); 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 * Finalizes all plugins at termination time. This forwards to the
* corresponding Plugin \a Done() methods. * corresponding Plugin \a Done() methods.

View file

@ -249,6 +249,8 @@ void Plugin::InitPreScript() {}
void Plugin::InitPostScript() {} void Plugin::InitPostScript() {}
void Plugin::InitPreExecution() {}
Plugin::bif_item_list Plugin::BifItems() const { return bif_items; } Plugin::bif_item_list Plugin::BifItems() const { return bif_items; }
void Plugin::Done() { void Plugin::Done() {

View file

@ -748,6 +748,14 @@ protected:
*/ */
virtual void InitPostScript(); 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 * Finalizer method that derived classes can override for performing
* custom tasks at shutdown. This can be overridden by derived * custom tasks at shutdown. This can be overridden by derived

View file

@ -26,7 +26,6 @@ AnalyOpt analysis_options;
std::unordered_set<const Func*> non_recursive_funcs; std::unordered_set<const Func*> non_recursive_funcs;
void (*CPP_init_hook)() = nullptr; void (*CPP_init_hook)() = nullptr;
void (*CPP_activation_hook)() = nullptr;
// Tracks all of the loaded functions (including event handlers and hooks). // Tracks all of the loaded functions (including event handlers and hooks).
static std::vector<FuncInfo> funcs; static std::vector<FuncInfo> funcs;

View file

@ -271,8 +271,4 @@ extern bool IsZAM_BuiltInCond(const CallExpr* c);
// to a non-empty value. // to a non-empty value.
extern void (*CPP_init_hook)(); 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 } // namespace zeek::detail

View file

@ -992,8 +992,8 @@ SetupResult setup(int argc, char** argv, Options* zopts) {
// we don't have any other source for it. // we don't have any other source for it.
run_state::detail::update_network_time(util::current_time()); run_state::detail::update_network_time(util::current_time());
if ( CPP_activation_hook ) // Plugin pre-execution hook.
(*CPP_activation_hook)(); plugin_mgr->InitPreExecution();
if ( zeek_init ) if ( zeek_init )
event_mgr.Enqueue(zeek_init, Args{}); event_mgr.Enqueue(zeek_init, Args{});