Only one instance of base_type() getting a NewRef instead of AdoptRef
fixed in merge.  All other changes are superficial formatting and
factoring.

* 'leaks' of https://github.com/MaxKellermann/zeek: (22 commits)
  Stmt: use class IntrusivePtr
  Stmt: remove unused default constructors and `friend` declarations
  Val: remove unimplemented prototype recover_val()
  Val: cast_value_to_type() returns IntrusivePtr
  Val: use IntrusivePtr in check_and_promote()
  Val: use nullptr instead of 0
  zeekygen: use class IntrusivePtr
  ID: use class IntrusivePtr
  Expr: use class IntrusivePtr
  Var: copy Location to stack, to fix use-after-free crash bug
  Scope: lookup_ID() and install_ID() return IntrusivePtr<ID>
  Scope: delete duplicate locals
  EventRegistry: automatically delete EventHandlers
  main: destroy event_registry after iosource_mgr
  zeekygen/IdentifierInfo: delete duplicate fields
  main: free the global scope in terminate_bro()
  Scope: pop_scope() returns IntrusivePtr<>
  Scope: unref all inits in destructor
  Var: pass IntrusivePtr to add_global(), add_local() etc.
  plugin/ComponentManager: hold a reference to the EnumType
  ...
This commit is contained in:
Jon Siwek 2020-02-28 00:42:17 -08:00
commit cf196bb148
41 changed files with 1864 additions and 2095 deletions

View file

@ -26,7 +26,7 @@ public:
~Stmt() override;
virtual Val* Exec(Frame* f, stmt_flow_type& flow) const = 0;
virtual IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const = 0;
Stmt* Ref() { ::Ref(this); return this; }
@ -85,116 +85,105 @@ protected:
class ExprListStmt : public Stmt {
public:
const ListExpr* ExprList() const { return l; }
const ListExpr* ExprList() const { return l.get(); }
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
ExprListStmt() { l = 0; }
ExprListStmt(BroStmtTag t, ListExpr* arg_l);
ExprListStmt(BroStmtTag t, IntrusivePtr<ListExpr> arg_l);
~ExprListStmt() override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
virtual Val* DoExec(val_list* vals, stmt_flow_type& flow) const = 0;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
virtual IntrusivePtr<Val> DoExec(val_list* vals, stmt_flow_type& flow) const = 0;
void Describe(ODesc* d) const override;
void PrintVals(ODesc* d, val_list* vals, int offset) const;
ListExpr* l;
IntrusivePtr<ListExpr> l;
};
class PrintStmt : public ExprListStmt {
public:
explicit PrintStmt(ListExpr* l) : ExprListStmt(STMT_PRINT, l) { }
template<typename L>
explicit PrintStmt(L&& l) : ExprListStmt(STMT_PRINT, std::forward<L>(l)) { }
protected:
friend class Stmt;
PrintStmt() {}
Val* DoExec(val_list* vals, stmt_flow_type& flow) const override;
IntrusivePtr<Val> DoExec(val_list* vals, stmt_flow_type& flow) const override;
};
class ExprStmt : public Stmt {
public:
explicit ExprStmt(Expr* e);
explicit ExprStmt(IntrusivePtr<Expr> e);
~ExprStmt() override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
const Expr* StmtExpr() const { return e; }
const Expr* StmtExpr() const { return e.get(); }
void Describe(ODesc* d) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
ExprStmt() { e = 0; }
ExprStmt(BroStmtTag t, Expr* e);
ExprStmt(BroStmtTag t, IntrusivePtr<Expr> e);
virtual Val* DoExec(Frame* f, Val* v, stmt_flow_type& flow) const;
virtual IntrusivePtr<Val> DoExec(Frame* f, Val* v, stmt_flow_type& flow) const;
int IsPure() const override;
Expr* e;
IntrusivePtr<Expr> e;
};
class IfStmt : public ExprStmt {
public:
IfStmt(Expr* test, Stmt* s1, Stmt* s2);
IfStmt(IntrusivePtr<Expr> test, IntrusivePtr<Stmt> s1, IntrusivePtr<Stmt> s2);
~IfStmt() override;
const Stmt* TrueBranch() const { return s1; }
const Stmt* FalseBranch() const { return s2; }
const Stmt* TrueBranch() const { return s1.get(); }
const Stmt* FalseBranch() const { return s2.get(); }
void Describe(ODesc* d) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
IfStmt() { s1 = s2 = 0; }
Val* DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
IntrusivePtr<Val> DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
int IsPure() const override;
Stmt* s1;
Stmt* s2;
IntrusivePtr<Stmt> s1;
IntrusivePtr<Stmt> s2;
};
class Case : public BroObj {
public:
Case(ListExpr* c, id_list* types, Stmt* arg_s);
Case(IntrusivePtr<ListExpr> c, id_list* types, IntrusivePtr<Stmt> arg_s);
~Case() override;
const ListExpr* ExprCases() const { return expr_cases; }
ListExpr* ExprCases() { return expr_cases; }
const ListExpr* ExprCases() const { return expr_cases.get(); }
ListExpr* ExprCases() { return expr_cases.get(); }
const id_list* TypeCases() const { return type_cases; }
id_list* TypeCases() { return type_cases; }
const Stmt* Body() const { return s; }
Stmt* Body() { return s; }
const Stmt* Body() const { return s.get(); }
Stmt* Body() { return s.get(); }
void Describe(ODesc* d) const override;
TraversalCode Traverse(TraversalCallback* cb) const;
protected:
friend class Stmt;
Case() { expr_cases = 0; type_cases = 0; s = 0; }
ListExpr* expr_cases;
IntrusivePtr<ListExpr> expr_cases;
id_list* type_cases;
Stmt* s;
IntrusivePtr<Stmt> s;
};
typedef PList<Case> case_list;
class SwitchStmt : public ExprStmt {
public:
SwitchStmt(Expr* index, case_list* cases);
SwitchStmt(IntrusivePtr<Expr> index, case_list* cases);
~SwitchStmt() override;
const case_list* Cases() const { return cases; }
@ -204,10 +193,7 @@ public:
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
SwitchStmt() { cases = 0; default_case_idx = -1; comp_hash = 0; }
Val* DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
IntrusivePtr<Val> DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
int IsPure() const override;
// Initialize composite hash and case label map.
@ -238,51 +224,40 @@ protected:
class AddStmt : public ExprStmt {
public:
explicit AddStmt(Expr* e);
explicit AddStmt(IntrusivePtr<Expr> e);
int IsPure() const override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
AddStmt() {}
};
class DelStmt : public ExprStmt {
public:
explicit DelStmt(Expr* e);
explicit DelStmt(IntrusivePtr<Expr> e);
int IsPure() const override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
DelStmt() {}
};
class EventStmt : public ExprStmt {
public:
explicit EventStmt(EventExpr* e);
explicit EventStmt(IntrusivePtr<EventExpr> e);
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
EventStmt() { event_expr = 0; }
EventExpr* event_expr;
IntrusivePtr<EventExpr> event_expr;
};
class WhileStmt : public Stmt {
public:
WhileStmt(Expr* loop_condition, Stmt* body);
WhileStmt(IntrusivePtr<Expr> loop_condition, IntrusivePtr<Stmt> body);
~WhileStmt() override;
int IsPure() const override;
@ -292,29 +267,24 @@ public:
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
WhileStmt()
{ loop_condition = 0; body = 0; }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
Expr* loop_condition;
Stmt* body;
IntrusivePtr<Expr> loop_condition;
IntrusivePtr<Stmt> body;
};
class ForStmt : public ExprStmt {
public:
ForStmt(id_list* loop_vars, Expr* loop_expr);
ForStmt(id_list* loop_vars, IntrusivePtr<Expr> loop_expr);
// Special constructor for key value for loop.
ForStmt(id_list* loop_vars, Expr* loop_expr, ID* val_var);
ForStmt(id_list* loop_vars, IntrusivePtr<Expr> loop_expr, IntrusivePtr<ID> val_var);
~ForStmt() override;
void AddBody(Stmt* arg_body) { body = arg_body; }
void AddBody(IntrusivePtr<Stmt> arg_body) { body = std::move(arg_body); }
const id_list* LoopVar() const { return loop_vars; }
const Expr* LoopExpr() const { return e; }
const Stmt* LoopBody() const { return body; }
const Expr* LoopExpr() const { return e.get(); }
const Stmt* LoopBody() const { return body.get(); }
int IsPure() const override;
@ -323,23 +293,20 @@ public:
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
ForStmt() { loop_vars = 0; body = 0; }
Val* DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
IntrusivePtr<Val> DoExec(Frame* f, Val* v, stmt_flow_type& flow) const override;
id_list* loop_vars;
Stmt* body;
IntrusivePtr<Stmt> body;
// Stores the value variable being used for a key value for loop.
// Always set to nullptr unless special constructor is called.
ID* value_var = nullptr;
IntrusivePtr<ID> value_var;
};
class NextStmt : public Stmt {
public:
NextStmt() : Stmt(STMT_NEXT) { }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
int IsPure() const override;
void Describe(ODesc* d) const override;
@ -353,7 +320,7 @@ class BreakStmt : public Stmt {
public:
BreakStmt() : Stmt(STMT_BREAK) { }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
int IsPure() const override;
void Describe(ODesc* d) const override;
@ -367,7 +334,7 @@ class FallthroughStmt : public Stmt {
public:
FallthroughStmt() : Stmt(STMT_FALLTHROUGH) { }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
int IsPure() const override;
void Describe(ODesc* d) const override;
@ -379,15 +346,11 @@ protected:
class ReturnStmt : public ExprStmt {
public:
explicit ReturnStmt(Expr* e);
explicit ReturnStmt(IntrusivePtr<Expr> e);
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
void Describe(ODesc* d) const override;
protected:
friend class Stmt;
ReturnStmt() {}
};
class StmtList : public Stmt {
@ -395,7 +358,7 @@ public:
StmtList();
~StmtList() override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
const stmt_list& Stmts() const { return stmts; }
stmt_list& Stmts() { return stmts; }
@ -415,7 +378,7 @@ public:
EventBodyList() : StmtList()
{ topmost = false; tag = STMT_EVENT_BODY_LIST; }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
void Describe(ODesc* d) const override;
@ -433,7 +396,7 @@ public:
~InitStmt() override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
const id_list* Inits() const { return inits; }
@ -442,9 +405,6 @@ public:
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
friend class Stmt;
InitStmt() { inits = 0; }
id_list* inits;
};
@ -452,7 +412,7 @@ class NullStmt : public Stmt {
public:
NullStmt() : Stmt(STMT_NULL) { }
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
int IsPure() const override;
void Describe(ODesc* d) const override;
@ -463,27 +423,27 @@ public:
class WhenStmt : public Stmt {
public:
// s2 is null if no timeout block given.
WhenStmt(Expr* cond, Stmt* s1, Stmt* s2, Expr* timeout, bool is_return);
WhenStmt(IntrusivePtr<Expr> cond,
IntrusivePtr<Stmt> s1, IntrusivePtr<Stmt> s2,
IntrusivePtr<Expr> timeout, bool is_return);
~WhenStmt() override;
Val* Exec(Frame* f, stmt_flow_type& flow) const override;
IntrusivePtr<Val> Exec(Frame* f, stmt_flow_type& flow) const override;
int IsPure() const override;
const Expr* Cond() const { return cond; }
const Stmt* Body() const { return s1; }
const Expr* TimeoutExpr() const { return timeout; }
const Stmt* TimeoutBody() const { return s2; }
const Expr* Cond() const { return cond.get(); }
const Stmt* Body() const { return s1.get(); }
const Expr* TimeoutExpr() const { return timeout.get(); }
const Stmt* TimeoutBody() const { return s2.get(); }
void Describe(ODesc* d) const override;
TraversalCode Traverse(TraversalCallback* cb) const override;
protected:
WhenStmt() { cond = 0; s1 = s2 = 0; timeout = 0; is_return = 0; }
Expr* cond;
Stmt* s1;
Stmt* s2;
Expr* timeout;
IntrusivePtr<Expr> cond;
IntrusivePtr<Stmt> s1;
IntrusivePtr<Stmt> s2;
IntrusivePtr<Expr> timeout;
bool is_return;
};