From 95b89be57160c54efb1adc35d5aa71f5fd43af0d Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Wed, 24 Mar 2021 21:22:03 -0700 Subject: [PATCH 1/3] put global statements into a quasi-function to support script optimization --- src/parse.y | 2 +- src/script_opt/ScriptOpt.cc | 23 +++++++++++++++++++++++ src/script_opt/ScriptOpt.h | 5 +++++ src/zeek-setup.cc | 8 +++++--- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/parse.y b/src/parse.y index ac7a924e40..03a9f4cb68 100644 --- a/src/parse.y +++ b/src/parse.y @@ -266,7 +266,7 @@ static bool expr_is_table_type_name(const zeek::detail::Expr* expr) %% -bro: +zeek: decl_list stmt_list { if ( zeek::detail::stmts ) diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index 78d99ff0e5..3873b43386 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -3,6 +3,7 @@ #include "zeek/Options.h" #include "zeek/Reporter.h" #include "zeek/Desc.h" +#include "zeek/module_util.h" #include "zeek/script_opt/ScriptOpt.h" #include "zeek/script_opt/ProfileFunc.h" #include "zeek/script_opt/Inline.h" @@ -153,6 +154,28 @@ void analyze_func(ScriptFuncPtr f) funcs.emplace_back(f, ScopePtr{NewRef{}, f->GetScope()}, f->CurrentBody()); } +const FuncInfo* analyze_global_stmts(Stmt* stmts) + { + // We ignore analysis_options.only_func - if it's in use, later + // logic will keep this function from being compiled, but it's handy + // now to enter it into "funcs" so we have a FuncInfo to return. + + auto id = install_ID("", GLOBAL_MODULE_NAME, true, false); + auto empty_args_t = make_intrusive(nullptr); + auto func_t = make_intrusive(empty_args_t, nullptr, FUNC_FLAVOR_FUNCTION); + id->SetType(func_t); + + auto sc = current_scope(); + std::vector empty_inits; + StmtPtr stmts_p{NewRef{}, stmts}; + auto sf = make_intrusive(id, stmts_p, empty_inits, + sc->Length(), 0); + + funcs.emplace_back(sf, ScopePtr{NewRef{}, sc}, stmts_p); + + return &funcs.back(); + } + static void check_env_opt(const char* opt, bool& opt_flag) { if ( getenv(opt) ) diff --git a/src/script_opt/ScriptOpt.h b/src/script_opt/ScriptOpt.h index 242f9d987a..4c56bbc52f 100644 --- a/src/script_opt/ScriptOpt.h +++ b/src/script_opt/ScriptOpt.h @@ -96,6 +96,11 @@ extern std::unordered_set non_recursive_funcs; // Analyze a given function for optimization. extern void analyze_func(ScriptFuncPtr f); +// Analyze the given top-level statement(s) for optimization. Returns +// a pointer to a FuncInfo for an argument-less quasi-function that can +// be Invoked, or its body executed directly, to execute the statements. +extern const FuncInfo* analyze_global_stmts(Stmt* stmts); + // Analyze all of the parsed scripts collectively for optimization. extern void analyze_scripts(); diff --git a/src/zeek-setup.cc b/src/zeek-setup.cc index 51e3162c2b..a03f9b3696 100644 --- a/src/zeek-setup.cc +++ b/src/zeek-setup.cc @@ -752,6 +752,8 @@ SetupResult setup(int argc, char** argv, Options* zopts) } } + auto init_stmts = stmts ? analyze_global_stmts(stmts) : nullptr; + analyze_scripts(); if ( analysis_options.report_recursive ) @@ -838,15 +840,15 @@ SetupResult setup(int argc, char** argv, Options* zopts) // cause more severe problems. ZEEK_LSAN_ENABLE(); - if ( stmts ) + if ( init_stmts ) { StmtFlowType flow; - Frame f(current_scope()->Length(), nullptr, nullptr); + Frame f(init_stmts->Scope()->Length(), nullptr, nullptr); g_frame_stack.push_back(&f); try { - stmts->Exec(&f, flow); + init_stmts->Body()->Exec(&f, flow); } catch ( InterpreterException& ) { From 3139cf259441524ab7c443122312706f86cc5eba Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Wed, 24 Mar 2021 21:37:26 -0700 Subject: [PATCH 2/3] "balance" tests with multiple Zeek scripts to load the same elements --- testing/btest/scripts/base/frameworks/netcontrol/acld-hook.zeek | 1 + testing/btest/scripts/base/frameworks/openflow/broker-basic.zeek | 1 + 2 files changed, 2 insertions(+) diff --git a/testing/btest/scripts/base/frameworks/netcontrol/acld-hook.zeek b/testing/btest/scripts/base/frameworks/netcontrol/acld-hook.zeek index 2698a3bfab..0b0bab2e6b 100644 --- a/testing/btest/scripts/base/frameworks/netcontrol/acld-hook.zeek +++ b/testing/btest/scripts/base/frameworks/netcontrol/acld-hook.zeek @@ -9,6 +9,7 @@ @TEST-START-FILE send.zeek @load base/frameworks/netcontrol +@load base/frameworks/broker redef exit_only_after_terminate = T; global have_peer = F; diff --git a/testing/btest/scripts/base/frameworks/openflow/broker-basic.zeek b/testing/btest/scripts/base/frameworks/openflow/broker-basic.zeek index e1d05db7a5..f6f166013b 100644 --- a/testing/btest/scripts/base/frameworks/openflow/broker-basic.zeek +++ b/testing/btest/scripts/base/frameworks/openflow/broker-basic.zeek @@ -82,6 +82,7 @@ event OpenFlow::flow_mod_failure(name: string, match: OpenFlow::ofp_match, flow_ @TEST-START-FILE recv.zeek +@load base/protocols/conn @load base/frameworks/openflow redef exit_only_after_terminate = T; From e5a0b2c04c4463aaf74cbf437a5e899985644a6f Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Wed, 24 Mar 2021 21:52:20 -0700 Subject: [PATCH 3/3] whitespace adjustment --- src/script_opt/ScriptOpt.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/script_opt/ScriptOpt.cc b/src/script_opt/ScriptOpt.cc index 3873b43386..65e2cd9266 100644 --- a/src/script_opt/ScriptOpt.cc +++ b/src/script_opt/ScriptOpt.cc @@ -168,8 +168,7 @@ const FuncInfo* analyze_global_stmts(Stmt* stmts) auto sc = current_scope(); std::vector empty_inits; StmtPtr stmts_p{NewRef{}, stmts}; - auto sf = make_intrusive(id, stmts_p, empty_inits, - sc->Length(), 0); + auto sf = make_intrusive(id, stmts_p, empty_inits, sc->Length(), 0); funcs.emplace_back(sf, ScopePtr{NewRef{}, sc}, stmts_p);