fixes for doing "script validation" to check for ZAM compile-ability

This commit is contained in:
Vern Paxson 2023-06-14 17:39:47 -07:00
parent cae5d30c62
commit 2c5b5bb41f
3 changed files with 20 additions and 12 deletions

View file

@ -22,11 +22,11 @@ public:
stmt_depths[stmt->Tag()] += 1; stmt_depths[stmt->Tag()] += 1;
if ( stmt->Tag() == STMT_BREAK && ! BreakStmtIsValid() ) if ( stmt->Tag() == STMT_BREAK && ! BreakStmtIsValid() )
Error(stmt, "break statement used outside of for, while or " Report(stmt, "break statement used outside of for, while or "
"switch statement and not within a hook."); "switch statement and not within a hook.");
if ( stmt->Tag() == STMT_NEXT && ! NextStmtIsValid() ) if ( stmt->Tag() == STMT_NEXT && ! NextStmtIsValid() )
Error(stmt, "next statement used outside of for or while statement."); Report(stmt, "next statement used outside of for or while statement.");
return TC_CONTINUE; return TC_CONTINUE;
} }
@ -63,6 +63,8 @@ public:
return TC_CONTINUE; return TC_CONTINUE;
} }
void SetHookDepth(int hd) { hook_depth = hd; }
bool IsValid() const { return valid_script; } bool IsValid() const { return valid_script; }
private: private:
@ -84,11 +86,7 @@ private:
void Report(const Stmt* stmt, const char* msg) void Report(const Stmt* stmt, const char* msg)
{ {
if ( report ) if ( report )
{ Error(stmt, msg);
zeek::reporter->PushLocation(stmt->GetLocationInfo());
zeek::reporter->Warning("%s", msg);
zeek::reporter->PopLocation();
}
valid_script = false; valid_script = false;
} }
@ -105,10 +103,15 @@ void script_validation()
traverse_all(&bn_cb); traverse_all(&bn_cb);
} }
bool script_is_valid(const Stmt* stmt) bool script_is_valid(const Stmt* stmt, bool is_in_hook)
{ {
BreakNextScriptValidation bn_cb(false); BreakNextScriptValidation bn_cb(false);
if ( is_in_hook )
bn_cb.SetHookDepth(1);
stmt->Traverse(&bn_cb); stmt->Traverse(&bn_cb);
return bn_cb.IsValid(); return bn_cb.IsValid();
} }

View file

@ -13,8 +13,12 @@ class Stmt;
void script_validation(); void script_validation();
/** /**
* Returns true if the given script statement (body) is valid. * Returns true if the given script statement (body) is valid. The
* second argument indicates whether the statement is the body of a hook.
*
* Unlike script_validation(), does not report any errors, just returns
* whether they are present.
*/ */
bool script_is_valid(const Stmt* s); bool script_is_valid(const Stmt* s, bool is_in_hook);
} }

View file

@ -35,7 +35,8 @@ bool is_ZAM_compilable(const ProfileFunc* pf, const char** reason)
} }
auto b = pf->ProfiledBody(); auto b = pf->ProfiledBody();
if ( b && ! script_is_valid(b) ) auto is_hook = pf->ProfiledFunc()->Flavor() == FUNC_FLAVOR_HOOK;
if ( b && ! script_is_valid(b, is_hook) )
{ {
if ( reason ) if ( reason )
*reason = "invalid script body"; *reason = "invalid script body";