diff --git a/CHANGES b/CHANGES index 86c6aabca4..81aad55382 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,10 @@ +4.1.0-dev.438 | 2021-03-29 15:42:25 -0700 + + * "balance" tests with multiple Zeek scripts to load the same elements (Vern Paxson, Corelight) + + * put global statements into a quasi-function to support script optimization (Vern Paxson, Corelight) + 4.1.0-dev.434 | 2021-03-29 13:18:18 -0700 * Fix sign-compare compiler warning in coerce_to_record() (Jon Siwek, Corelight) diff --git a/VERSION b/VERSION index 5fd68827bc..812002b3ed 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -4.1.0-dev.434 +4.1.0-dev.438 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..65e2cd9266 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,27 @@ 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& ) { 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;