Commit graph

6667 commits

Author SHA1 Message Date
Max Kellermann
6f3e9d2c4c IntrusivePtr: optimize release() using std::exchange() 2020-02-25 21:05:42 +01:00
Max Kellermann
3bc33fe7f6 Expr: remove unused default constructors and friend declarations 2020-02-25 21:05:14 +01:00
Max Kellermann
138868747c Expr: remove useless override
All it does is call the base class.
2020-02-25 21:05:03 +01:00
Max Kellermann
8f3a712f74 Type: don't pass reference to pointer to MatchesIndex()
No implementation modifies the pointer value.  To guard against this,
this commit changes `&` to `const`.
2020-02-25 21:04:29 +01: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
Max Kellermann
3ccda03a54 ID: remove unused default constructor 2020-02-25 21:03:53 +01:00
Max Kellermann
ee21f29173 Scope: simplify PList access, use pop_back() and back() 2020-02-25 21:03:27 +01:00
Max Kellermann
cdf4362c7d List: add method empty() 2020-02-25 21:03:07 +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
Tim Wojtulewicz
7726558151 Add a faster siphash24 implementation than the reference one
- Average of 10 runs of 2009-M57-day11-18.trace (release build at -O3):
  - Master: 6.027s  93650 bytes max RSS
  - Commit: 5.950s  93271 bytes max RSS

- Hashing a fixed 32-byte payload 10 million times with a fixed key:
  - Master: 1.397411s
  - Commit: 0.998211s
2020-02-24 21:03:13 -07:00
Jon Siwek
1f4dca04d4 Merge branch 'ExpandAndInit_IntrusivePtr' of https://github.com/MaxKellermann/zeek
- Minor whitespace tweak in merge

* 'ExpandAndInit_IntrusivePtr' of https://github.com/MaxKellermann/zeek:
  Val: pass IntrusivePtr<> to TableVal::ExpandAndInit()
2020-02-24 18:12:58 -08:00
Tim Wojtulewicz
01df20c782 Merge remote-tracking branch 'origin/topic/jsiwek/deprecated-attribute'
* origin/topic/jsiwek/deprecated-attribute:
  Switch to using [[deprecated]] attribute
2020-02-24 18:53:37 -07:00
Tim Wojtulewicz
822567b3f9 Merge remote-tracking branch 'origin/topic/jsiwek/32bit-compat'
* origin/topic/jsiwek/32bit-compat:
  Improve formatting of doubles that are close to integers
  Improve HTTP version number comparisons
  Add a 32-bit task to Cirrus CI config
  Replace va_list fmt() overload with vfmt()
  Format tables indexed by patterns consistently across 32-bit/64-bit
  Format interval values consistently across 32-bit/64-bit platforms
2020-02-24 18:49:33 -07:00
Jon Siwek
15b4eeb085 Merge branch 'opaqueval_intrusiveptr' of https://github.com/MaxKellermann/zeek
- Minor whitespace changes during merge.

* 'opaqueval_intrusiveptr' of https://github.com/MaxKellermann/zeek:
  OpaqueVal: use class IntrusivePtr
2020-02-24 17:25:03 -08:00
Jon Siwek
e0da9fbe82 Merge branch 'scanned-file-device-and-inode' of https://github.com/awelzel/zeek
- Minor whitespace adjustments
- Converted std::pair usage to anonymous struct to improve readability

* 'scanned-file-device-and-inode' of https://github.com/awelzel/zeek:
  scan.l: Actually add <utility> include, too.
  ScannedFile: Identify already scanned files by device and inode
2020-02-24 17:09:30 -08:00
Max Kellermann
12b6070b2b parse.y: fix memory leak in FieldAssignExpr call
An unmanaged reference is held on `func_id`, but its `val` reference
is now owned by the `FieldAssignExpr` instance.  The `ID` instance
still feels responsible for releasing the `val` reference, but since
nobody ever frees the `ID`, the conflict never causes a crash.
2020-02-24 19:50:56 +01: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
68b965c299 parse.y: fix use-after-free bug in open-ended index_slice
This code passed the "$1" reference to both `SizeExpr::SizeExpr()` and
`IndexExpr::IndexExpr()`.

Bug introduced by commit 8515d3aa57
2020-02-24 15:51:44 +01:00
Arne Welzel
97cd5c11a4 scan.l: Actually add <utility> include, too. 2020-02-24 15:12:46 +01:00
Max Kellermann
bcac5d3b17 Type: fix use-after-free bug in init_type()
Prior to this, `t` gets assigned from `Expr::InitType()` which returns
a referenced `BroType` to the caller (and init_Type() releases the
reference later).

But `reduce_type()` does not return a referenced `BroType`; so if
`reduce_type()` happens to return a different instance, it will be
released and maybe destroyed, resulting in a use-after-free bug.
2020-02-24 14:28:31 +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
c074122f13 zeekygen/IdentifierInfo: fix memory leak in operator=() 2020-02-24 13:00:22 +01:00
Max Kellermann
b84693e546 Func: fix memory leaks in get_func_priority() 2020-02-24 13:00:22 +01:00
Max Kellermann
80d6bbc4dc parse.y: fix several memory leaks after lookup_ID()
lookup_ID() returns a referenced pointer to the caller.  Quite a lot
of code paths don't release those references.
2020-02-24 13:00:22 +01:00
Max Kellermann
e32a9a61f6 Func: fix memory leaks in check_built_in_call()
All error branches leak the `fmt_str_arg->Eval(0)` return value.
2020-02-24 13:00:22 +01: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
2db1c88e4d parse.y: hold reference on init_expr for zeekygen::Manager::Redef()
The reference is consumed by add_global(), which means we must not use
it after the function returns.
2020-02-24 12:57:28 +01:00
Max Kellermann
3b09bb9e46 Expr: fix two memory leaks in AssignExpr::InitVal() 2020-02-24 12:14:10 +01:00
Max Kellermann
32786c7645 parse.y: fix memory leak after "&derepcated" without string
The "TOK_CONSTANT" leaks.
2020-02-24 12:14:10 +01:00
Max Kellermann
7f97f74203 RuleMatcher: delete PatternSet instances in destructor (memleak) 2020-02-24 12:14:10 +01:00
Max Kellermann
b2072c4c5c option.bif: fix crash bug by referencing Func, not Val
The method `ID::AddOptionHandler()` expects to adopt a reference to
the `callback` parameter from the caller, but the caller references
the containing `Val` instance, not the `Func`.

Later, the `ID` destructor will `Unref()` the `Func`, which will
quickly underflow the reference counter.  The containing `Val` however
will have references nobody will ever release (memory leak).
2020-02-24 12:14:10 +01:00
Arne Welzel
4b4595f5db ScannedFile: Identify already scanned files by device and inode
Zeek scripts located on separate filesystems, but sharing the same inode
number leads to scripts not being loaded. The reason is that a `ScannedFile`
is only identified by `st_ino` which is not enough to uniquely identify a
file in a system.

This problem may be hit when `ZEEKPATH` points to separate filesystems and
two script files happen have the same `st_ino` value - definitely not very
likely, but possibly very confusing when it happens.

The following test case creates two zeek scripts on separate filesystems.
As the filesystems are freshly created and of the same type, the files will
(tested a few times with xfs/ext4) have the same `st_ino` values.

    #!/bin/bash
    ZEEKDIR=${ZEEKDIR:-/home/awelzel/projects/zeek}
    export ZEEKPATH=.:${ZEEKDIR}/build/scripts:${ZEEKDIR}/scripts

    cat << EOF > hello.zeek
    event zeek_init() {
        print("Hello, once or twice?");
    }
    EOF

    for i in 1 2 ; do
        dd if=/dev/urandom of=img${i} count=16 bs=1M 2>/dev/null
        sudo mkfs.xfs -q ./img${i}
        mkdir -p mount${i}
        sudo mount ./img${i} ./mount${i}
        sudo cp hello.zeek ./mount${i}/hello.zeek
    done

    ls ./mount*/*zeek
    stat -c "%n: device=%d inode=%i" ./mount*/hello.zeek

    ${ZEEKDIR}/build/src/zeek -b ./mount1/hello.zeek ./mount2/hello.zeek

    # Cleanup
    for i in 1 2 ; do
        sudo umount ./mount${i}
        rm -rfv ./img${i} ./mount${i}
        rm -rfv hello.zeek
    done

Before this patch, `Hello, once or twice?` is printed only once,
afterwards twice:

    $ sh testcase.sh
    [sudo] password for awelzel:
    ./mount1/hello.zeek  ./mount2/hello.zeek
    ./mount1/hello.zeek: device=1794 inode=6915
    ./mount2/hello.zeek: device=1795 inode=6915
    Hello, once or twice?
    Hello, once or twice?
2020-02-23 17:26:08 +01:00
Jon Siwek
0d5e395c6c Switch to using [[deprecated]] attribute 2020-02-22 11:58:38 -08:00
Jon Siwek
f0c713046c Merge branch 'refactor_obj' of https://github.com/MaxKellermann/zeek
Minor formatting/style changes in merge.

* 'refactor_obj' of https://github.com/MaxKellermann/zeek:
  Val: use C++ initializers
  Val: add BroValUnion constructors
  Val: reduce duplicate code by using delegating constructors
  Val: remove unused default constructors and `friend` declarations
  Val: remove the unnecessary BroValUnion typedef
  Type: remove unnecessary enum typedefs
  Type: use C++ initializers
  Type: move code from BroType::BroType() to constexpr functions
  Type: remove useless BroType destructor
  Obj: disallow copying BroObj
  Obj: use C++ initializers
  Obj: make `no_location` constexpr
2020-02-21 20:06:09 -08:00
Jon Siwek
3996a54302 Merge branch 'noreturn' of https://github.com/MaxKellermann/zeek
Minor formatting change in merge so [[noreturn]] is consistently
on same line as function declarations.

* 'noreturn' of https://github.com/MaxKellermann/zeek:
  threading/MsgThread: add [[noreturn]] to InternalError()
  Flare: add [[noreturn]] to bad_pipe_op()
  Obj: add [[noreturn]] attributes to Internal(),  bad_ref()
  Reporter: add [[noreturn]] attribute to several methods
2020-02-21 15:37:33 -08:00
Max Kellermann
55398ed5d8 OpaqueVal: use class IntrusivePtr 2020-02-21 20:50:11 +01:00
Max Kellermann
ccfafde17e threading/MsgThread: add [[noreturn]] to InternalError() 2020-02-21 20:45:09 +01:00
Max Kellermann
3d44864c79 Flare: add [[noreturn]] to bad_pipe_op() 2020-02-21 20:45:09 +01:00
Max Kellermann
c9af57aff8 Obj: add [[noreturn]] attributes to Internal(), bad_ref() 2020-02-21 20:45:09 +01:00
Max Kellermann
a4cfd86f47 Reporter: add [[noreturn]] attribute to several methods
Fixes this compiler warning:

 src/digest.cc: In function ‘EVP_MD_CTX* hash_init(HashAlgorithm)’:
 src/digest.cc:44:26: warning: ‘md’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  if ( ! EVP_DigestInit_ex(c, md, NULL) )
         ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~
2020-02-21 20:43:09 +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
c0164c42d5 OpaqueVal: remove misplaced virtual keywords
These methods are not meant to be overridden.
2020-02-20 15:57:57 +01:00
Max Kellermann
53f49e0057 CompHash: use class IntrusivePtr for the type field 2020-02-20 15:57:39 +01: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
7c0863dccf IntrusivePtr: remove reset(), nobody uses it
This method mimicks std::unique_ptr::reset(), but adds an obscure
"add_ref" parameter which is error prone.  Since nobody uses this
method, and this method is all about dealing with raw pointers which
we shouldn't be doing, let's remove it.
2020-02-20 15:56:48 +01:00
Max Kellermann
98b27a2ae8 IntrusivePtr: remove ordering operators
These violate the C++ standard because comparing pointers to unrelated
objects is undefined behavior.
2020-02-20 15:56:32 +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