Commit graph

387 commits

Author SHA1 Message Date
Max Kellermann
73cea5dcad Type: use class IntrusivePtr in TypeList 2020-03-06 09:06:38 +01:00
Max Kellermann
de0289125b Type: use class IntrusivePtr in IndexType 2020-03-06 09:06:38 +01:00
Max Kellermann
674e141a15 Val: use class IntrusivePtr in class TableVal 2020-03-06 09:06:38 +01:00
Max Kellermann
ba35ebec4c Type: return IntrusivePtr 2020-03-06 09:06:38 +01:00
Max Kellermann
0a6ddfb6b5 Val: add TableVal::Assign() overload with IntrusivePtr
Prepare the transition to IntrusivePtr for various callers.
2020-03-06 09:06:38 +01:00
Max Kellermann
a0c831a1bd Func: use class IntrusivePtr 2020-03-06 09:06:37 +01:00
Max Kellermann
df7a47510a Expr: reference id_list before passing to new BroFunc
Passing the `id_list` pointer to `BroFunc` transfers ownership of the
contained `ID` instances, because `~BroFunc()` unreferences them.
Therefore, we need to increase the reference counters for each
`BroFunc` instance to fix the use-after-free bug.

Closes https://github.com/zeek/zeek/issues/845
2020-03-06 09:06:37 +01:00
Jon Siwek
cf196bb148 Merge branch 'leaks' of https://github.com/MaxKellermann/zeek
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
  ...
2020-02-28 00:48:20 -08:00
Jon Siwek
646a2313ae Merge remote-tracking branch 'origin/topic/timw/cleanup'
* origin/topic/timw/cleanup:
  Expr: use fmt instead of sprintf
  Expr: other minor initialization cleanup
  Expr: use List::empty()
  Expr: Convert a bunch of methods returning ints to return bools
  IPAddr: minor cleanup
  PriorityQueue: initialization cleanup
  IP: Cleanup initialization, make a few functions consistent with others
2020-02-27 14:25:37 -08:00
Tim Wojtulewicz
33aa192cec Expr: use fmt instead of sprintf 2020-02-27 14:21:18 -07:00
Tim Wojtulewicz
c466e63493 Expr: other minor initialization cleanup 2020-02-27 14:21:18 -07:00
Tim Wojtulewicz
8e27cc0a0d Expr: use List::empty() 2020-02-27 14:21:18 -07:00
Tim Wojtulewicz
1d9968d393 Expr: Convert a bunch of methods returning ints to return bools 2020-02-27 14:21:17 -07:00
Max Kellermann
3331abb4f2 Val: cast_value_to_type() returns IntrusivePtr 2020-02-27 14:08:00 +01:00
Max Kellermann
96951c1300 Val: use IntrusivePtr in check_and_promote() 2020-02-27 14:08:00 +01:00
Max Kellermann
edde591748 ID: use class IntrusivePtr 2020-02-27 14:08:00 +01:00
Max Kellermann
c3ea246237 Expr: use class IntrusivePtr
As a side effect, this fixes lots of memory leaks in
`*Expr::InitVal()` because in most implementations, the `aggr`
parameter leaks.
2020-02-27 12:06:34 +01:00
Max Kellermann
528cf11a5c Scope: lookup_ID() and install_ID() return IntrusivePtr<ID>
This fixes several memory leaks and double free bugs.
2020-02-27 12:02:55 +01:00
Jon Siwek
597407a708 Merge branch 'refactor' of https://github.com/MaxKellermann/zeek
- Minor code style/format tweaks in merge

* 'refactor' of https://github.com/MaxKellermann/zeek:
  IntrusivePtr: overload std::swap()
  IntrusivePtr: eliminate setPtr()
  IntrusivePtr: optimize release() using std::exchange()
  Expr: remove unused default constructors and `friend` declarations
  Expr: remove useless override
  Type: don't pass reference to pointer to MatchesIndex()
  Expr: don't pass reference to pointer to check_and_promote*()
  ID: remove unused default constructor
  Scope: simplify PList access, use pop_back() and back()
  List: add method empty()
2020-02-26 10:07:31 -08:00
Max Kellermann
0520d715c3 Expr: don't pass reference to pointer to check_and_promote*()
The function never modifies the pointer value.

check_and_promote_expr() is left untouched because it really does
modify the pointer.
2020-02-25 21:04:20 +01:00
Jon Siwek
4c7b1fa619 Merge branch 'leaks' of https://github.com/MaxKellermann/zeek
- Various minor code formatting/styling during the merge

* 'leaks' of https://github.com/MaxKellermann/zeek:
  parse.y: fix memory leak in FieldAssignExpr call
  parse.y: fix use-after-free bug in open-ended index_slice
  Type: fix use-after-free bug in init_type()
  Expr: fix memory leak in RecordCoerceExpr::Fold()
  Expr: fix memory leak in RecordCoerceExpr::InitVal()
  zeekygen/IdentifierInfo: fix memory leak in operator=()
  Func: fix memory leaks in get_func_priority()
  parse.y: fix several memory leaks after lookup_ID()
  Func: fix memory leaks in check_built_in_call()
  Var: fix memory leaks in add_global() and add_local()
  Var: add missing references to `init` in add{,_and_assign}_local()
  parse.y: hold reference on init_expr for zeekygen::Manager::Redef()
  Expr: fix two memory leaks in AssignExpr::InitVal()
  parse.y: fix memory leak after "&derepcated" without string
  RuleMatcher: delete PatternSet instances in destructor (memleak)
  option.bif: fix crash bug by referencing `Func`, not `Val`
2020-02-24 20:51:31 -08:00
Max Kellermann
e2bf12d54a Val: pass IntrusivePtr<> to TableVal::ExpandAndInit()
Clarifies ownership and fixes memory leaks.

Closes https://github.com/zeek/zeek/issues/811
2020-02-24 15:55:52 +01:00
Max Kellermann
e557563c69 Expr: fix memory leak in RecordCoerceExpr::Fold()
Don't add a second reference if the `rhs` variable was assigned from
`def->AttrExpr()->Eval(0)`.

Caused by commit af3267acc3
2020-02-24 13:44:59 +01:00
Max Kellermann
46ecbd400e Expr: fix memory leak in RecordCoerceExpr::InitVal()
Caused by commit b1fd161274
2020-02-24 13:37:51 +01:00
Max Kellermann
3b09bb9e46 Expr: fix two memory leaks in AssignExpr::InitVal() 2020-02-24 12:14:10 +01:00
Jon Siwek
38b25cf38d Merge branch 'smart_ptr' of https://github.com/MaxKellermann/zeek
Minor whitespace fixes during merge.

* 'smart_ptr' of https://github.com/MaxKellermann/zeek:
  OpaqueVal: remove misplaced `virtual` keywords
  CompHash: use class IntrusivePtr for the `type` field
  IntrusivePtr: replace the "add_ref" parameter with tag structs
  IntrusivePtr: remove reset(), nobody uses it
  IntrusivePtr: remove ordering operators
  IntrusivePtr: rename detach() to release()
  IntrusivePtr: move nullptr initializer to field declaration
2020-02-21 10:15:24 -08:00
Jon Siwek
678115803b Merge branch 'expr_missing_ref' of https://github.com/MaxKellermann/zeek
Updated the Ref() to happen inline with Assign() call for clarity.

* 'expr_missing_ref' of https://github.com/MaxKellermann/zeek:
  Expr: add missing reference in AssignExpr::InitVal()
2020-02-20 14:59:04 -08:00
Max Kellermann
31b3a56740 IntrusivePtr: replace the "add_ref" parameter with tag structs
Using a runtime parameter is obscure and error-prone.  Avoiding
error-prone code and getting reference counting right is the whole
point of this class.
2020-02-20 15:56:50 +01:00
Max Kellermann
35d97a24f0 Expr: add missing reference in AssignExpr::InitVal()
The one reference returned by `op2->InitVal()` is given to
`aggr_r->Assign()` and returned to the caller, which may result in a
use-after-free crash bug.  This patch adds the missing reference.

Closes https://github.com/zeek/zeek/issues/805
2020-02-20 09:27:06 +01:00
Max Kellermann
08128b244f IntrusivePtr: rename detach() to release()
Follow the C++ standard library conventions (here: `std::unique_ptr`).
2020-02-20 08:58:21 +01:00
Jon Siwek
a20dd12117 Merge branch 'memleaks' of https://github.com/MaxKellermann/zeek
The merge commit fixes reference counting issues introduced with the
changes to ListExpr::Assign() and IndexExpr::Assign(), but then also
several other pre-existing reference counting confusions in other
Assign() implementations/calls, some which were now directly observable
via new crashing behavior, others just from a cursory code audit.

* 'memleaks' of https://github.com/MaxKellermann/zeek:
  input/Manager: fix memory leak in UnrollRecordType()
  OpaqueVal: fix two memory leaks in BloomFilterVal::Merge()
  DbgBreakpoint: fix memory leak
  DebugCmds: fix memory leak
  scan.l: fix crash bug in do_atif()
  Expr: fix memory leak in RecordConstructorExpr::InitVal()
  Expr: fix memory leaks in AssignExpr::EvalIntoAggregate()
  Expr: fix memory leaks in CondExpr::Eval()
  Expr: fix several memory leaks in BoolExpr::Eval()
  Expr: fix various memory leaks in Assign()
  Expr: fix memory leaks in BinaryExpr::Eval()
  analyzer/protocol/http: fix potential memory leak
2020-02-19 18:59:04 -08:00
Max Kellermann
0a0884edb4 Expr: fix memory leak in RecordConstructorExpr::InitVal() 2020-02-19 15:12:28 +01:00
Max Kellermann
5468fae8e0 Expr: fix memory leaks in AssignExpr::EvalIntoAggregate() 2020-02-19 15:12:28 +01:00
Max Kellermann
65c4f34385 Expr: fix memory leaks in CondExpr::Eval()
No code path had any cleanup code, leaking all the local references.

More weird was however the result building code: it took elements from
one of the existing vectors without referencing them, and passed them
to VectorVal::Assign() which assumes that the caller-owned reference
is now owned by that VectorVal.

Even in the successful code path, no references were freed.

Everything was wrong with this method!
2020-02-19 15:12:28 +01:00
Max Kellermann
862f48da45 Expr: fix several memory leaks in BoolExpr::Eval() 2020-02-19 15:12:28 +01:00
Max Kellermann
6ce1081b38 Expr: fix various memory leaks in Assign() 2020-02-19 15:12:28 +01:00
Max Kellermann
593ebc1d62 Expr: fix memory leaks in BinaryExpr::Eval() 2020-02-19 15:12:28 +01:00
Max Kellermann
6a815b4b06 UID, ..: un-inline methods to reduce header dependencies
Only 1% build time speedup, but still, it declutters the headers a bit.

Before this patch:

 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k
 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps

After this patch:

 2537.19user 142.94system 2:26.90elapsed 1824%CPU (0avgtext+0avgdata 1434268maxresident)k
 16240inputs+8887152outputs (1931major+48728888minor)pagefaults 0swaps
2020-02-04 20:51:02 +01:00
Max Kellermann
0db61f3094 include cleanup
The Zeek code base has very inconsistent #includes.  Many sources
included a few headers, and those headers included other headers, and
in the end, nearly everything is included everywhere, so missing
#includes were never noticed.  Another side effect was a lot of header
bloat which slows down the build.

First step to fix it: in each source file, its own header should be
included first to verify that each header's includes are correct, and
none is missing.

After adding the missing #includes, I replaced lots of #includes
inside headers with class forward declarations.  In most headers,
object pointers are never referenced, so declaring the function
prototypes with forward-declared classes is just fine.

This patch speeds up the build by 19%, because each compilation unit
gets smaller.  Here are the "time" numbers for a fresh build (with a
warm page cache but without ccache):

Before this patch:

 3144.94user 161.63system 3:02.87elapsed 1808%CPU (0avgtext+0avgdata 2168608maxresident)k
 760inputs+12008400outputs (1511major+57747204minor)pagefaults 0swaps

After this patch:

 2565.17user 141.83system 2:25.46elapsed 1860%CPU (0avgtext+0avgdata 1489076maxresident)k
 72576inputs+9130920outputs (1667major+49400430minor)pagefaults 0swaps
2020-02-04 20:51:02 +01:00
Tim Wojtulewicz
a159d075cf Add Trigger manager for managing triggers created by things like 'when' statements
- Adds new trigger namespace
- Adds trigger::Manager class as a new IOSource for keeping track of triggers and integrating them into the loop. Previously the loop relied on the event manager Drain() method to process all triggers on every loop, but now that the loop actively waits for events to occur, triggers would not fire when they needed to. Adding them as part of the loop ensures they're checked.
2020-01-31 10:13:09 -07:00
Jon Siwek
44d922c4b5 Fix reference counting issues related to lambdas/closures
For example, circular references between a lambda function the frame
it's stored within and/or its closure could cause memory leaks.

This also fixes other various reference-count ownership issues that
could lead to memory errors.

There may still be some potential/undiscovered issues because the "outer
ID" finding logic doesn't look quite right as the AST traversal descends
within nested lambdas and considers their locals as "outer", but
possibly the other logic for locating values in closures or cloning
closures just works around that behavior.
2020-01-02 23:04:22 -08:00
Tim Wojtulewicz
0140098adb Add null check for results of dynamic_cast in AssignExpr::TypeCheck. Fixes coverity findings 1403416 and 1403417 2019-09-16 10:56:41 -07:00
Jon Siwek
47235b57a6 Merge remote-tracking branch 'origin/topic/timw/deprecate-int-types'
* origin/topic/timw/deprecate-int-types:
  Deprecate the internal int/uint types in favor of the cstdint types they were based on

Merge adjustments:
  * A bpf type mistakenly got replaced (inside an unlikely #ifdef)
  * Did a few substitutions that got missed (likely due to
    pre-processing out of DEBUG macros)
2019-08-14 15:49:24 -07:00
Jon Siwek
94c380297e Merge remote-tracking branch 'origin/topic/timw/easy-pdict-replacements'
* origin/topic/timw/easy-pdict-replacements:
  Cleanups related to PDict -> std::map replacements
  Remove other simple uses of PDict
  Protocols: Remove uses of PDict
  g_dbgfilemaps: Remove uses of PDict
  Scope: remove uses of PDict
  DFA: remove uses of PDict
  EventRegistry: remove uses of PDict
2019-08-13 20:16:02 -07:00
Jon Siwek
87f85ecca1 Cleanups related to PDict -> std::map replacements 2019-08-13 19:57:42 -07:00
Tim Wojtulewicz
54752ef9a1 Deprecate the internal int/uint types in favor of the cstdint types they were based on 2019-08-12 13:50:07 -07:00
Jon Siwek
f6f4e3e8bc GH-527: fix LambdaExpr::Traverse
Coverity CID 1403966
2019-08-06 14:47:03 -07:00
Jon Siwek
a1d8a21005 Guarantee unique internal name for each lambda function
By dealing with hash collisions.
2019-07-31 14:10:29 -07:00
Jon Siwek
8575c9daed Use consistent hashing method for internal lambda function names
The results of std::hash<std::string> may vary depending on platform.
E.g. test suite failed on macOS due to Linux generating different lambda
function names.
2019-07-31 12:06:27 -07:00
Robin Sommer
13c373086d Merge remote-tracking branch 'origin/topic/zeke/closures' 2019-07-30 02:32:34 +00:00