From 6ad14dec3e90c75481c35d95f4d6a1fe56bbe3c3 Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Thu, 13 Jun 2024 09:16:26 -0700 Subject: [PATCH] checking CFT info when doing optimization pruning --- scripts/base/protocols/krb/main.zeek | 3 +- src/script_opt/ZAM/AM-Opt.cc | 54 +++++++++++++++++++++++----- src/script_opt/ZAM/Branches.cc | 7 ++-- src/script_opt/ZAM/Compile.h | 12 ++----- src/script_opt/ZAM/Low-Level.cc | 9 ++++- src/script_opt/ZAM/ZInst.h | 4 ++- 6 files changed, 66 insertions(+), 23 deletions(-) diff --git a/scripts/base/protocols/krb/main.zeek b/scripts/base/protocols/krb/main.zeek index ef0f3e7b2f..0b3066f89a 100644 --- a/scripts/base/protocols/krb/main.zeek +++ b/scripts/base/protocols/krb/main.zeek @@ -190,8 +190,7 @@ event krb_as_response(c: connection, msg: KDC_Response) &priority=-5 event krb_ap_request(c: connection, ticket: KRB::Ticket, opts: KRB::AP_Options) &priority=5 { - if ( set_session(c) ) - return; + set_session(c); } event krb_tgs_request(c: connection, msg: KDC_Request) &priority=5 diff --git a/src/script_opt/ZAM/AM-Opt.cc b/src/script_opt/ZAM/AM-Opt.cc index 29e9d38266..8242dc1d7f 100644 --- a/src/script_opt/ZAM/AM-Opt.cc +++ b/src/script_opt/ZAM/AM-Opt.cc @@ -941,16 +941,54 @@ void ZAMCompiler::KillInst(zeek_uint_t i) { } } - if ( num_labels == 0 ) - // No labels to propagate. - return; + ZInstI* succ = nullptr; - for ( auto j = i + 1; j < insts1.size(); ++j ) { - auto succ = insts1[j]; - if ( succ->live ) { - succ->num_labels += num_labels; - break; + if ( num_labels > 0 ) { + for ( auto j = i + 1; j < insts1.size(); ++j ) { + if ( insts1[j]->live ) { + succ = insts1[j]; + break; + } } + if ( succ ) + succ->num_labels += num_labels; + } + + // Look into propagating control flow info. + if ( inst->aux && ! inst->aux->cft.empty() ) { + auto& cft = inst->aux->cft; + + if ( cft.count(CFT_BLOCK_END) > 0 ) { + // Propagate block-ends backwards. + int j = i; + while ( --j >= 0 ) + if ( insts1[j]->live ) + break; + + ASSERT(j >= 0); + + // Make sure the CFT entry is created. + AddCFT(insts1[j], CFT_BLOCK_END); + + auto be_cnt = cft[CFT_BLOCK_END]; + --be_cnt; // we already did one above + insts1[j]->aux->cft[CFT_BLOCK_END] += be_cnt; + } + + if ( cft.count(CFT_ELSE) > 0 ) { + // Push forward unless this was the end of the block. + if ( cft.count(CFT_BLOCK_END) == 0 ) { + ASSERT(succ); + AddCFT(succ, CFT_ELSE); + } + } + + // If's can be killed because their bodies become empty, + // and break's because they just lead to their following instruction. + // However, loop's and next's should not be killed. + ASSERT(cft.count(CFT_LOOP) == 0); + ASSERT(cft.count(CFT_LOOP_COND) == 0); + ASSERT(cft.count(CFT_NEXT) == 0); } } diff --git a/src/script_opt/ZAM/Branches.cc b/src/script_opt/ZAM/Branches.cc index 717a4a0afd..bbe1ecf957 100644 --- a/src/script_opt/ZAM/Branches.cc +++ b/src/script_opt/ZAM/Branches.cc @@ -10,9 +10,12 @@ namespace zeek::detail { void ZAMCompiler::PushGoTos(GoToSets& gotos) { gotos.emplace_back(); } -void ZAMCompiler::ResolveGoTos(GoToSets& gotos, const InstLabel l) { - for ( auto& gi : gotos.back() ) +void ZAMCompiler::ResolveGoTos(GoToSets& gotos, const InstLabel l, ControlFlowType cft) { + for ( auto& gi : gotos.back() ) { SetGoTo(gi, l); + if ( cft != CFT_NONE ) + AddCFT(insts1[gi.stmt_num], cft); + } gotos.pop_back(); } diff --git a/src/script_opt/ZAM/Compile.h b/src/script_opt/ZAM/Compile.h index a8b262dfac..5cddb776bc 100644 --- a/src/script_opt/ZAM/Compile.h +++ b/src/script_opt/ZAM/Compile.h @@ -167,14 +167,8 @@ private: void PushFallThroughs() { PushGoTos(fallthroughs); } void PushCatchReturns() { PushGoTos(catches); } - void ResolveNexts(const InstLabel l) { - ResolveGoTos(nexts, l); - AddCFT(l, CFT_NEXT); - } - void ResolveBreaks(const InstLabel l) { - ResolveGoTos(breaks, l); - AddCFT(l, CFT_BREAK); - } + void ResolveNexts(const InstLabel l) { ResolveGoTos(nexts, l, CFT_NEXT); } + void ResolveBreaks(const InstLabel l) { ResolveGoTos(breaks, l, CFT_BREAK); } void ResolveFallThroughs(const InstLabel l) { ResolveGoTos(fallthroughs, l); } void ResolveCatchReturns(const InstLabel l) { ResolveGoTos(catches, l); } @@ -280,7 +274,7 @@ private: using GoToSets = std::vector; void PushGoTos(GoToSets& gotos); - void ResolveGoTos(GoToSets& gotos, const InstLabel l); + void ResolveGoTos(GoToSets& gotos, const InstLabel l, ControlFlowType cft = CFT_NONE); ZAMStmt GenGoTo(GoToSet& v); ZAMStmt GoToStub(); diff --git a/src/script_opt/ZAM/Low-Level.cc b/src/script_opt/ZAM/Low-Level.cc index fa6c95c87c..1079a64c31 100644 --- a/src/script_opt/ZAM/Low-Level.cc +++ b/src/script_opt/ZAM/Low-Level.cc @@ -27,7 +27,14 @@ const ZAMStmt ZAMCompiler::LastInst() { return ZAMStmt(insts1.size() - 1); } void ZAMCompiler::AddCFT(ZInstI* inst, ControlFlowType cft) { if ( ! inst->aux ) inst->aux = new ZInstAux(0); - inst->aux->cft.insert(cft); + + auto cft_entry = inst->aux->cft.find(cft); + if ( cft_entry == inst->aux->cft.end() ) + inst->aux->cft[cft] = 1; + else { + ASSERT(cft == CFT_BLOCK_END); + ++cft_entry->second; + } } OpaqueVals* ZAMCompiler::BuildVals(const ListExprPtr& l) { return new OpaqueVals(InternalBuildVals(l.get())); } diff --git a/src/script_opt/ZAM/ZInst.h b/src/script_opt/ZAM/ZInst.h index fc013181e9..a0bb5338f0 100644 --- a/src/script_opt/ZAM/ZInst.h +++ b/src/script_opt/ZAM/ZInst.h @@ -385,6 +385,8 @@ enum ControlFlowType { CFT_LOOP_COND, CFT_NEXT, CFT_BREAK, + + CFT_NONE, }; // Auxiliary information, used when the fixed ZInst layout lacks @@ -506,7 +508,7 @@ public: bool is_BiF_call = false; // Associated control flow information. - std::set cft; + std::map cft; // Used for referring to events. EventHandler* event_handler = nullptr;