From d81bfed45da04d0e77496ea4487832d5aa1f95ba Mon Sep 17 00:00:00 2001 From: Robin Sommer Date: Thu, 6 Feb 2014 17:52:41 -0800 Subject: [PATCH] Fixing memory leaks in input framework. --- CHANGES | 7 +++ VERSION | 2 +- aux/btest | 2 +- src/input/Manager.cc | 18 ++++-- .../btest/core/leaks/input-with-remove.bro | 63 +++++++++++++++++++ 5 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 testing/btest/core/leaks/input-with-remove.bro diff --git a/CHANGES b/CHANGES index ce57bfc99e..345e945207 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,11 @@ +2.2-140 | 2014-02-06 17:58:04 -0800 + + * Fixing memory leaks in input framework. (Robin Sommer) + + * Add script to detect filtered TCP traces. Addresses BIT-1119. (Jon + Siwek) + 2.2-137 | 2014-02-04 09:09:55 -0800 * Minor unified2 script documentation fix. (Jon Siwek) diff --git a/VERSION b/VERSION index c869973493..8611c50ec0 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.2-137 +2.2-140 diff --git a/aux/btest b/aux/btest index 23ff11bf0e..808fd764b6 160000 --- a/aux/btest +++ b/aux/btest @@ -1 +1 @@ -Subproject commit 23ff11bf0edbad2c6f1acbeb3f9a029ff4b61785 +Subproject commit 808fd764b6f5198264177822db3f902f747c21cc diff --git a/src/input/Manager.cc b/src/input/Manager.cc index 7af80892c6..95983faf26 100644 --- a/src/input/Manager.cc +++ b/src/input/Manager.cc @@ -397,7 +397,9 @@ bool Manager::CreateEventStream(RecordVal* fval) string stream_name = name_val->AsString()->CheckString(); Unref(name_val); - RecordType *fields = fval->Lookup("fields", true)->AsType()->AsTypeType()->Type()->AsRecordType(); + Val* fields_val = fval->Lookup("fields", true); + RecordType *fields = fields_val->AsType()->AsTypeType()->Type()->AsRecordType(); + Unref(fields_val); Val *want_record = fval->Lookup("want_record", true); @@ -548,13 +550,17 @@ bool Manager::CreateTableStream(RecordVal* fval) Val* pred = fval->Lookup("pred", true); - RecordType *idx = fval->Lookup("idx", true)->AsType()->AsTypeType()->Type()->AsRecordType(); + Val* idx_val = fval->Lookup("idx", true); + RecordType *idx = idx_val->AsType()->AsTypeType()->Type()->AsRecordType(); + Unref(idx_val); + RecordType *val = 0; - if ( fval->Lookup("val", true) != 0 ) + Val* val_val = fval->Lookup("val", true); + if ( val_val ) { - val = fval->Lookup("val", true)->AsType()->AsTypeType()->Type()->AsRecordType(); - Unref(val); // The lookupwithdefault in the if-clause ref'ed val. + val = val_val->AsType()->AsTypeType()->Type()->AsRecordType(); + Unref(val_val); } TableVal *dst = fval->Lookup("destination", true)->AsTableVal(); @@ -729,7 +735,7 @@ bool Manager::CreateTableStream(RecordVal* fval) stream->pred = pred ? pred->AsFunc() : 0; stream->num_idx_fields = idxfields; stream->num_val_fields = valfields; - stream->tab = dst->AsTableVal(); + stream->tab = dst->AsTableVal(); // ref'd by lookupwithdefault stream->rtype = val ? val->AsRecordType() : 0; stream->itype = idx->AsRecordType(); stream->event = event ? event_registry->Lookup(event->Name()) : 0; diff --git a/testing/btest/core/leaks/input-with-remove.bro b/testing/btest/core/leaks/input-with-remove.bro new file mode 100644 index 0000000000..62fcfa0a4e --- /dev/null +++ b/testing/btest/core/leaks/input-with-remove.bro @@ -0,0 +1,63 @@ +# Needs perftools support. +# +# @TEST-GROUP: leaks +# +# @TEST-REQUIRES: bro --help 2>&1 | grep -q mem-leaks +# +# @TEST-EXEC: HEAP_CHECK_DUMP_DIRECTORY=. HEAPCHECK=local btest-bg-run bro bro -b -m -r $TRACES/wikipedia.trace %INPUT +# @TEST-EXEC: btest-bg-wait 15 + +@load base/frameworks/input + +redef exit_only_after_terminate = T; + +global c: count = 0; + + +type OneLine: record { + s: string; +}; + +event line(description: Input::EventDescription, tpe: Input::Event, s: string) + { + print "1", "Line"; + } + +event InputRaw::process_finished(name: string, source:string, exit_code:count, signal_exit:bool) + { + Input::remove(name); + print "2", name; + } + +function run(): count + { + Input::add_event([$name=unique_id(""), + $source=fmt("%s |", "date"), + $reader=Input::READER_RAW, + $mode=Input::STREAM, + $fields=OneLine, + $ev=line, + $want_record=F]); + + return 1; + } + + +event do() + { + run(); + } + +event do_term() { + terminate(); +} + +event bro_init() { + schedule 1sec { + do() + }; + schedule 3sec { + do_term() + }; +} +