Commit graph

195 commits

Author SHA1 Message Date
Jon Siwek
f032885085 Change record field anonymous functions to use lambda expressions
There was an alternate syntax to assign anonymous functions to record
fields that was never migrated to use the new lambda expression
machinery (and so didn't allow referencing variables in outer scope):

    type myrec: record {
        foo: function(a: string);
    };

    local o = "o";
    local mr = myrec($foo(a: string) = { print a + o; });
2020-03-26 15:48:18 -07:00
Jon Siwek
b62727a7fa Merge branch 'intrusive_ptr' of https://github.com/MaxKellermann/zeek
* 'intrusive_ptr' of https://github.com/MaxKellermann/zeek: (32 commits)
  Scope: store IntrusivePtr in `local`
  Scope: pass IntrusivePtr to AddInit()
  DNS_Mgr: use class IntrusivePtr
  Scope: use class IntrusivePtr
  Attr: use class IntrusivePtr
  Expr: check_and_promote_expr() returns IntrusivePtr
  Frame: use class IntrusivePtr
  Val: RecordVal::LookupWithDefault() returns IntrusivePtr
  Type: RecordType::FieldDefault() returns IntrusivePtr
  Val: TableVal::Delete() returns IntrusivePtr
  Type: base_type() returns IntrusivePtr
  Type: init_type() returns IntrusivePtr
  Type: merge_types() returns IntrusivePtr
  Type: use class IntrusivePtr in VectorType
  Type: use class IntrusivePtr in EnumType
  Type: use class IntrusivePtr in FileType
  Type: use class IntrusivePtr in TypeDecl
  Type: make TypeDecl `final` and the dtor non-`virtual`
  Type: use class IntrusivePtr in TypeType
  Type: use class IntrusivePtr in FuncType
  ...
2020-03-17 22:51:46 -07:00
Max Kellermann
6e0d331267 Scope: pass IntrusivePtr to AddInit() 2020-03-06 13:02:15 +01:00
Max Kellermann
dc518c0fb4 Scope: use class IntrusivePtr 2020-03-06 13:00:45 +01:00
Max Kellermann
36a26a7b43 Attr: use class IntrusivePtr 2020-03-06 12:59:47 +01:00
Max Kellermann
6495193aae Type: base_type() returns IntrusivePtr 2020-03-06 09:06:38 +01:00
Max Kellermann
f0a357cadf Type: init_type() returns IntrusivePtr 2020-03-06 09:06:38 +01:00
Max Kellermann
43af5f8349 Type: use class IntrusivePtr in TypeDecl 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
a0c831a1bd Func: use class IntrusivePtr 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
Max Kellermann
46397de5c0 Stmt: use class IntrusivePtr 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
7be3641f1d Var: copy Location to stack, to fix use-after-free crash bug
The AssignExpr constructor may free the "init" pointer via
AssignExpr::TypeCheck(), resulting in a crash due to use-after-free.
To fix the crash bug, this patch copies the `Location` to the stack
instead of using a potentially-dangling pointer.
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
Max Kellermann
875bfc09a2 Scope: pop_scope() returns IntrusivePtr<>
Make sure unused scopes are freed to fix memory leaks.

The comment inside pop_scope() is now obsolete and I deleted it,
because this commit implements the real solution.

Note that this requires us to add a reference to the
push_existing_scope() call in dbg_eval_expr(), because it never owned
the reference.
2020-02-27 12:02:55 +01:00
Max Kellermann
2c0ece7376 Var: pass IntrusivePtr to add_global(), add_local() etc.
Those functions don't have a well-defined reference passing API, and
we had lots of memory leaks here.  By using IntrusivePtr, reference
ownership is well-defined.
2020-02-27 11:59:37 +01:00
Max Kellermann
7c80b577f1 Var: fix crash when redeclaring a function with different args
The following source code:

 function foo(foo: int) {}
 function foo() {}

... first produces this error:

 error in crash.zeek, line 1 and crash.zeek, line 2: incompatible types (function(foo:int;) : void and function() : void)

... and then crashes:

 Thread 1 "zeek" received signal SIGSEGV, Segmentation fault.
 0x000055555617d970 in RecordType::FieldDecl (this=0x555557cbdd80, field=0) at ../src/Type.cc:735
 735		return (*types)[field];
 (gdb) bt
 #0  0x000055555617d970 in RecordType::FieldDecl (this=0x555557cbdd80, field=0) at ../src/Type.cc:735
 #1  0x000055555619c0e2 in transfer_arg_defaults (args=0x555557cbf270, recv=0x555557cbdd80) at ../src/Var.cc:315
 #2  0x000055555619c40c in begin_func (id=0x555557cbf070, module_name=0x5555579dd4a0 "GLOBAL", flavor=FUNC_FLAVOR_FUNCTION, is_redef=0, t=0x555557cbde00,
     attrs=0x0) at ../src/Var.cc:371
 #3  0x0000555555f5723b in yyparse () at parse.y:1174
 #4  0x0000555556038bf6 in main (argc=5, argv=0x7fffffffe658) at ../src/main.cc:646

This is because `begin_func()` checks if the old and new functions
have the same type via same_type(), but continues anyway, and then
transfer_arg_defaults() crashes because both `Args()` have different
lengths.
2020-02-27 11:50:25 +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
6c263cbce5 Var: fix memory leaks in add_global() and add_local()
The `init` parameter always leaks.
2020-02-24 13:00:22 +01:00
Max Kellermann
d49e709bfd Var: add missing references to init in add{,_and_assign}_local()
The reference to `init` is consumed by make_var() and again by the
AssignExpr constructor.
2020-02-24 12:59:08 +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
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
Jon Siwek
7b9a27c96a Merge remote-tracking branch 'origin/topic/johanna/remove-build-unique'
* origin/topic/johanna/remove-build-unique:
  Replace build_unique with make_unique
2019-10-29 09:42:55 -07:00
Johanna Amann
e2a8dd4db1 Replace build_unique with make_unique
This was a rarely used convenience function from when we did not yet
have c++17 support.
2019-10-29 11:50:30 +01:00
Tim Wojtulewicz
aacd2134fa Add check for null init variable during make_var. (Coverity 1403419) 2019-09-16 10:56:41 -07:00
Tim Wojtulewicz
a3deb0446c Scope: remove uses of PDict 2019-08-13 19:57:42 -07:00
Zeke Medley
cef94832f1 Frame merge and cleanup for merge. 2019-07-25 11:19:17 -07:00
Robin Sommer
1bf0cd29fd Edit pass over changes before merge. 2019-07-20 00:00:51 +00:00
Zeke Medley
1692be4c2d Fix bug in selective cloning. 2019-07-16 15:15:05 -07:00
Jon Siwek
8c45937798 Merge branch 'topic/jsiwek/template-containers-merge'
* topic/jsiwek/template-containers-merge:
  Fix a potential usage of List::remove_nth(-1)
  Change List::remote(const T&) to return a bool
  Fix debug build due to old int_list usage within assert
  Convert uses of loop_over_list to ranged-for loops
  Remove loop_over_queue (as an example for later removing loop_over_list)
  Change int_list in CCL.h to be a vector, fix uses of int_list to match
  Remove List<> usage from strings.bif
  Replace uses of the old Queue/PQueue generation code with new template versions
  Convert BaseQueue/Queue/PQueue into templates, including iterator support
  Replace uses of the old Dict generation code with new template versions
  Convert PDict into template
  Replace uses of the old List generation code with new template versions
  Convert BaseList/List/PList into templates, including iterator support

* Generally squashed fixups from topic/timw/template-containers

* Add missing include file in List.h: <cassert>
2019-07-15 19:51:27 -07:00
Tim Wojtulewicz
e51f02737b Convert uses of loop_over_list to ranged-for loops 2019-07-15 19:00:24 -07:00
Tim Wojtulewicz
ec4913ac18 GH-157: Mark some attributes as not allowed for global variables 2019-07-11 13:34:39 -07:00
Zeke Medley
409f27955b Call parent constructor from LambdaExpr. 2019-07-01 13:36:28 -07:00
Zeke Medley
28253b24f9 Table defaults capture closures. 2019-06-27 14:38:38 -07:00
Zeke Medley
8ed18ca194 Make ClosureFrame safe & cleanup
TODO: make anonymous-funcs associated with tables capture closures,
implement copy constructor for Frame, & other cleanup.
2019-06-26 15:05:57 -07:00
Zeke Medley
f27209e84c Merge branch 'master' of https://github.com/zeek/zeek into topic/zeke/closures 2019-06-21 12:31:34 -07:00
Zeke Medley
8257a644d3 Lambdas selectively clone from the closure. 2019-06-21 12:02:41 -07:00
Zeke Medley
1ed672287b Merge branch 'master' of https://github.com/zeek/zeek into topic/zeke/closures 2019-06-21 09:41:41 -07:00
Tim Wojtulewicz
a5e03d25d4 GH-171: support warning messages alongside deprecated attributes 2019-06-20 22:57:15 -07:00
Zeke Medley
a3001f1b2b Add lambda expressions with closures to Zeek.
This allows anonymous functions in Zeek to capture their closures.
they do so by creating a copy of their enclosing frame and joining
that with their own frame.

There is no way to specify what specific items to capture from the
closure like C++, nor is there a nonlocal keyword like Python.
Attemptying to declare a local variable that has already been caught
by the closure will error nicely. At the worst this is an inconvenience
for people who are using lambdas which use the same variable names
as their closures.

As a result of functions copying their enclosing frames there is no
way for a function with a closure to reach back up and modify the
state of the frame that it was created in. This lets functions that
generate functions work as expected. The function can reach back and
modify its copy of the frame that it is captured in though.

Implementation wise this is done by creating two new subclasses in
Zeek. The first is a LambdaExpression which can be thought of as a
function generator. It gathers all of the ingredients for a function
at parse time, and then when evaluated creats a new version of that
function with the frame it is being evaluated in as a closure. The
second subclass is a ClosureFrame. This acts for most intents and
purposes like a regular Frame, but it routes lookups of values to its
closure as needed.
2019-06-20 18:43:56 -07:00
Johanna Amann
824ccde6fc Merge remote-tracking branch 'origin/master' into topic/johanna/243 2019-05-20 10:14:11 -07:00
Johanna Amann
ffa173abc0 Implement a Shallow Clone operation for types.
This is needed to track name changes for the documentation.

With this things, which do not need val-cloning, generally seem to work
again. There are a whole bunch of test failures at the moment.
2019-05-17 11:13:04 -07:00
Jon Siwek
6ad7099f7e Merge remote-tracking branch 'origin/topic/robin/gh-239'
* origin/topic/robin/gh-239:
  Undo a change to btest.cfg from a recent commit
  Updating submodule.
  Fix zeek-wrapper
  Update for renaming BroControl to ZeekControl.
  Updating submodule.
  GH-239: Rename bro to zeek, bro-config to zeek-config, and bro-path-dev to zeek-path-dev.
2019-05-14 13:27:40 -07:00
Johanna Amann
474efe9e69 Remove value serialization.
Note - this compiles, but you cannot run Bro anymore - it crashes
immediately with a 0-pointer access. The reason behind it is that the
required clone functionality does not work anymore.
2019-05-09 11:54:38 -07:00
Johanna Amann
dcd6454530 Remove RemoteSerializer and related code/types.
Also removes broccoli from the source tree.
2019-05-03 15:00:13 -07:00
Johanna Amann
61c84a0a40 Remove synchrnized and persistent attributes.
Code that was used by them is still there.
2019-05-02 13:10:37 -07:00
Robin Sommer
789cb376fd GH-239: Rename bro to zeek, bro-config to zeek-config, and bro-path-dev to zeek-path-dev.
This also installs symlinks from "zeek" and "bro-config" to a wrapper
script that prints a deprecation warning.

The btests pass, but this is still WIP. broctl renaming is still
missing.

#239
2019-05-01 21:43:45 +00:00
Jon Siwek
8bc65f09ec Cleanup/improve PList usage and Event API
Majority of PLists are now created as automatic/stack objects,
rather than on heap and initialized either with the known-capacity
reserved upfront or directly from an initializer_list (so there's no
wasted slack in the memory that gets allocated for lists containing
a fixed/known number of elements).

Added versions of the ConnectionEvent/QueueEvent methods that take
a val_list by value.

Added a move ctor/assign-operator to Plists to allow passing them
around without having to copy the underlying array of pointers.
2019-04-11 20:30:25 -07:00