]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Merge pull request #2427 in SNORT/snort3 from ~MIALTIZE/snort3:cpputest_leaks to...
authorMichael Altizer (mialtize) <mialtize@cisco.com>
Tue, 1 Sep 2020 01:45:47 +0000 (01:45 +0000)
committerMichael Altizer (mialtize) <mialtize@cisco.com>
Tue, 1 Sep 2020 01:45:47 +0000 (01:45 +0000)
Squashed commit of the following:

commit f1f70793fa2f9bfa46a9f48245372df2899bcc5a
Author: Michael Altizer <mialtize@cisco.com>
Date:   Mon Aug 31 15:35:04 2020 -0400

    host_tracker: Fix allocator unit test to work on 32-bit systems again

commit 85a78101fe8bfb9d3055488bcba029ec4e430f02
Author: Michael Altizer <mialtize@cisco.com>
Date:   Thu Aug 27 13:43:28 2020 -0400

    cmake: Restore accidentally removed caching of static DAQ modules

commit a89ffd26d712ca56ee8fafe24e1d64f616fc9c8b
Author: Michael Altizer <mialtize@cisco.com>
Date:   Thu Aug 27 11:15:02 2020 -0400

    utils: Add sys/time.h to util.h for struct timeval definition

    This fixes the Alpine Linux build issue where the forward declaration
    of struct timval from ts_print() ended up in the snort namespace.

commit d3c78c5f6ec5fbc21231043512a3d90e30488e64
Author: Michael Altizer <mialtize@cisco.com>
Date:   Thu Aug 27 11:12:32 2020 -0400

    rna: Remove redefinition of USHRT_MAX

commit 9393c5e3621b1900acf15d07e08020c98be763cd
Author: Michael Altizer <mialtize@cisco.com>
Date:   Tue Aug 25 12:41:00 2020 -0400

    tests: Fix the majority of memory leaks in CppUTest unit tests

    Additionally, this allows us to use the finally released CppUTest 4.0.

commit 7d363fe48a6a11836bd9e44f2fd8d54f936acafc
Author: Michael Altizer <mialtize@cisco.com>
Date:   Tue Aug 25 17:18:19 2020 -0400

    style: Replace some tabs that snuck in with proper spaces

28 files changed:
cmake/FindDAQ.cmake
src/connectors/tcp_connector/test/tcp_connector_test.cc
src/detection/detect_trace.cc
src/helpers/test/hyper_search_test.cc
src/host_tracker/test/host_cache_allocator_ht_test.cc
src/host_tracker/test/host_cache_allocator_test.cc
src/host_tracker/test/host_cache_module_test.cc
src/host_tracker/test/host_tracker_module_test.cc
src/ips_options/test/ips_regex_test.cc
src/network_inspectors/appid/test/app_info_table_test.cc
src/network_inspectors/appid/test/appid_api_test.cc
src/network_inspectors/appid/test/appid_detector_test.cc
src/network_inspectors/appid/test/appid_http_event_test.cc
src/network_inspectors/appid/test/appid_http_session_test.cc
src/network_inspectors/appid/test/appid_mock_definitions.h
src/network_inspectors/appid/test/appid_mock_http_session.h
src/network_inspectors/appid/test/appid_session_api_test.cc
src/network_inspectors/rna/rna_pnd.h
src/network_inspectors/rna/test/rna_module_mock.h
src/network_inspectors/rna/test/rna_module_test.cc
src/search_engines/test/hyperscan_test.cc
src/side_channel/side_channel.cc
src/side_channel/test/side_channel_test.cc
src/stream/ip/ip_defrag.cc
src/stream/tcp/segment_overlap_editor.cc
src/stream/tcp/tcp_segment_descriptor.cc
src/stream/tcp/test/tcp_normalizer_test.cc
src/utils/util.h

index a420ddcaf752f064674612c4804254723806b93f..907c655836011813ce4084c25eb8710e50a986fc 100644 (file)
@@ -67,6 +67,6 @@ if (PKG_CONFIG_EXECUTABLE AND ENABLE_STATIC_DAQ)
     endif()
     if (DAQ_STATIC_MODULES)
         list(SORT DAQ_STATIC_MODULES)
-       # set(DAQ_STATIC_MODULES ${DAQ_STATIC_MODULES} CACHE INTERNAL "Static DAQ modules")
+        set(DAQ_STATIC_MODULES ${DAQ_STATIC_MODULES} CACHE INTERNAL "Static DAQ modules")
     endif()
 endif()
index af9576284221e8c3d7fcad2fb992fda7a5efc487..a5581bc3706591981fbf539b55b0742f4053f382 100644 (file)
@@ -344,8 +344,6 @@ TEST_GROUP(tcp_connector_tinit_tterm_thread_call)
 {
     void setup() override
     {
-        // FIXIT-RC workaround for CppUTest mem leak detector issue
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         tcpc_api = (const ConnectorApi*) tcp_connector;
         s_instance = 0;
         set_normal_status();
@@ -370,7 +368,6 @@ TEST_GROUP(tcp_connector_tinit_tterm_thread_call)
         tcpc_api->tterm(connector);
         tcpc_api->dtor(connector_common);
         tcp_connector->mod_dtor(mod);
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
index 16eaae4a2a829b5d46678d16f8e05fbf62b0828b..5ae1ba132372a32b8229436169263a83654bafc5 100644 (file)
@@ -130,7 +130,7 @@ void node_eval_trace(const detection_option_tree_node_t* node, const Cursor& cur
         return;
 
     if ( trace_enabled(detection_trace, TRACE_BUFFER, 5) )
-               dump_buffer(cursor.buffer() + pos, cursor.length(), p);
+        dump_buffer(cursor.buffer() + pos, cursor.length(), p);
     else if ((pos != cursor_pos) || strcmp(cursor_name, name))
     {
         cursor_pos = pos;
index bf4e692548a47f9e2515dd1dc392f3a95b47d2b5..41e20e52c11d931467a3c174003aec265d882e2e 100644 (file)
@@ -66,7 +66,11 @@ int SnortConfig::request_scratch(ScratchAllocator* s)
 }
 
 void SnortConfig::release_scratch(int)
-{ s_state.clear(); }
+{
+    scratcher = nullptr;
+    s_state.clear();
+    s_state.shrink_to_fit();
+}
 
 const SnortConfig* SnortConfig::get_conf()
 { return snort_conf; }
@@ -253,7 +257,6 @@ TEST(hyper_search_test_group, not_found4)
 
 int main(int argc, char** argv)
 {
-    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
 
index cf1f94c7806f7f1d23de4de477ae45538d388d6c..d7746ca36d4281cd2e7484e8ec862ca2cd4db4d3 100644 (file)
@@ -33,7 +33,6 @@
 #include <CppUTest/CommandLineTestRunner.h>
 #include <CppUTest/TestHarness.h>
 
-using namespace std;
 using namespace snort;
 
 namespace snort
@@ -59,13 +58,18 @@ TEST(host_cache_allocator_ht, allocate)
     const size_t hc_item_sz = sizeof(HostCacheIp::Data) + sizeof(HostTracker);
     const size_t ht_item_sz = sizeof(HostApplication);
 
-    // room for n host trackers in the cache and m host applications in ht
-    const size_t max_size = n * hc_item_sz + m * ht_item_sz;
+    // room for n host trackers in the cache and 2^floor(log2(3))+2^ceil(log2(3))-1 host
+    // applications in ht
+    // FIXIT-L this makes a questionable assumption about the STL vector implementation
+    // that it will double the allocation each time it needs to increase its size, so
+    // going from 2 to 3 will allocate 4 and then release 2, meaning in order to exactly
+    // induce pruning, the max size should be just one <ht_item_sz> short of holding 6
+    const size_t max_size = n * hc_item_sz + 5 * ht_item_sz;
 
     host_cache.set_max_size(max_size);
 
     // insert n empty host trackers:
-    for (size_t i=0; i<n; i++)
+    for (size_t i = 0; i < n; i++)
     {
         memset(hk, 0, 16);
         hk[i] = (uint8_t) i;
@@ -78,7 +82,7 @@ TEST(host_cache_allocator_ht, allocate)
 
     // insert m host tracker items (host applications) into the most recent
     // host tracker
-    size_t i = n-1;
+    size_t i = n - 1;
     memset(hk, 0, 16);
     hk[i] = (uint8_t) i;
     ip.set(hk);
@@ -91,20 +95,19 @@ TEST(host_cache_allocator_ht, allocate)
         auto ht_ptr = host_cache[ip];
         CHECK(ht_ptr != nullptr);
 
-        for (size_t port = 0; port+1<m; port++)
+        for (size_t port = 0; port + 1 < m; port++)
             CHECK(true == ht_ptr->add_service(port, IpProtocol::TCP, 676, true));
 
         // Insert a new host tracker item. The sequence of operations is this:
-        // - host tracker vector is holding 2 * ht_item_sz = 24 and wants to
-        //   double in size.
-        // - the allocator honors the vector's request, allocating 48 bytes and
-        //   informs the host_cache about the new size, which exceeds max_size now
+        // - host tracker vector is holding 2 * <ht_item_sz> bytes and wants to double in size.
+        // - the allocator honors the vector's request, allocating 4 * <ht_item_sz> bytes and
+        //   informs the host_cache about the new size, which exceeds max_size
         // - host_cache prunes, removing the least recent host tracker.
-        //   Since this ht is empty, precisely hc_item_sz = 88 bytes are freed.
-        // - the host tracker vector destructor frees up an additional 24 bytes
+        //   Since this ht is empty, precisely <hc_item_sz> bytes are freed.
+        // - the host tracker vector destructor frees up an additional 2 * <ht_item_sz> bytes
         //   that it reallocated.
         // Hence, after the next insert, the math is this:
-        size_t sz = host_cache.mem_size() + 4*ht_item_sz - hc_item_sz - 2*ht_item_sz;
+        size_t sz = host_cache.mem_size() + 4 * ht_item_sz - hc_item_sz - 2 * ht_item_sz;
 
         CHECK(true == ht_ptr->add_service(m, IpProtocol::TCP, 676, true));
         CHECK(sz == host_cache.mem_size());
@@ -138,13 +141,12 @@ TEST(host_cache_allocator_ht, allocate)
     res = host_cache.remove(ip, ht_ptr);
     CHECK(res == true);
     CHECK(ht_ptr != nullptr);
-
 }
 
 int main(int argc, char** argv)
 {
-    //  Use this if you want to turn off memory checks entirely:
+    // FIXIT-L There is currently no external way to fully release the memory from the global host
+    //   cache unordered_map in host_cache.cc
     MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
index 49bf94aee575e01f9b4738ffb10b9dc57ed9d9d4..1497262c2a5893392c31c6c82e38f7f8a5050440 100644 (file)
@@ -121,8 +121,8 @@ TEST(cache_allocator, allocate)
 
 int main(int argc, char** argv)
 {
-    //  Use this if you want to turn off memory checks entirely:
+    // FIXIT-L There is currently no external way to fully release the memory from the global host
+    //   cache unordered_map in host_cache.cc
     MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
index 62d145260c70a501d79e5d00ebb8070a80eb29f3..b7b8391e50adf42c9848fef29678cbab792768af 100644 (file)
@@ -37,7 +37,6 @@
 #include "sfip/sf_ip.h"
 
 using namespace snort;
-using namespace std;
 
 // All tests here use the same module since host_cache is global. Creating a local module for each
 // test will cause host_cache PegCount testing to be dependent on the order of running these tests.
@@ -84,15 +83,6 @@ HostCacheAllocIp<T>::HostCacheAllocIp()
 
 TEST_GROUP(host_cache_module)
 {
-    void setup() override
-    {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-    }
-
-    void teardown() override
-    {
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
-    }
 };
 
 static void try_reload_prune(bool is_not_locked)
@@ -111,8 +101,6 @@ static void try_reload_prune(bool is_not_locked)
 // This method is a friend of LruCacheSharedMemcap class.
 TEST(host_cache_module, misc)
 {
-    Value size_val((double)2112);
-    Parameter size_param = { "size", Parameter::PT_INT, nullptr, nullptr, "cache size" };
     const PegInfo* ht_pegs = module.get_pegs();
     const PegCount* ht_stats = module.get_counts();
 
@@ -165,13 +153,6 @@ TEST(host_cache_module, misc)
     CHECK(ht_stats[4] == 2); // 2 reload_prunes
     CHECK(ht_stats[5] == 1); // 1 remove
 
-    size_val.set(&size_param);
-
-    // Set up the host_cache max size.
-    module.begin("host_cache", 0, nullptr);
-    module.set(nullptr, size_val, nullptr);
-    module.end("host_cache", 0, nullptr);
-
     ht_stats = module.get_counts();
     CHECK(ht_stats[0] == 4);
 }
@@ -194,5 +175,8 @@ TEST(host_cache_module, log_host_cache_messages)
 
 int main(int argc, char** argv)
 {
+    // FIXIT-L There is currently no external way to fully release the memory from the global host
+    //   cache unordered_map in host_cache.cc
+    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
index ad74160c989b03071e50a516f14e7320f51407f9..3522af22c67ee4274fc18d951ed6ab5a6b2dadab 100644 (file)
@@ -100,6 +100,8 @@ TEST(host_tracker_module, host_tracker_module_test_basic)
 
 int main(int argc, char** argv)
 {
+    // FIXIT-L There is currently no external way to fully release the memory from the global host
+    //   cache unordered_map in host_cache.cc
     MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
index e408e2fcb7917bb60b85dd3867e551d23f59e734..13a82e20b13223051cb3c525e69d70361ef6ca7e 100644 (file)
@@ -71,7 +71,12 @@ int SnortConfig::request_scratch(ScratchAllocator* s)
     return 0;
 }
 
-void SnortConfig::release_scratch(int) { }
+void SnortConfig::release_scratch(int)
+{
+    scratcher = nullptr;
+    s_state.clear();
+    s_state.shrink_to_fit();
+}
 
 const SnortConfig* SnortConfig::get_conf()
 { return snort_conf; }
@@ -373,8 +378,6 @@ TEST(ips_regex_option_relative, no_match)
 
 int main(int argc, char** argv)
 {
-    // FIXIT-L cpputest hangs or crashes in the leak detector
-    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
 
index 088ae2671dce0ce93a275a7ee2abbd29ad3041c6..607db7cd0b9854ad25451efe6f04453109236b8d 100644 (file)
@@ -68,15 +68,9 @@ AppInfoTableEntry* add_static_entry(AppId id, const char* name)
 
 TEST_GROUP(app_info_table)
 {
-    void setup() override
-    {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-    }
-
     void teardown() override
     {
         app_info_mgr.cleanup_appid_info_table();
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
index 7057aa74ff230a061db5b8bc0bdbc93d0f215679..2873fcd5a1cab3d53d21a50850664e4c1d982e33 100644 (file)
@@ -200,7 +200,10 @@ TEST_GROUP(appid_api)
     char test_log[256];
     void setup() override
     {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
+        mock_init_appid_pegs();
+        SfIp ip;
+        mock_session = new AppIdSession(IpProtocol::TCP, &ip, 1492, dummy_appid_inspector,
+        dummy_appid_inspector.get_ctxt().get_odp_ctxt());
         flow = new Flow;
         flow->set_flow_data(mock_session);
         mock().setDataObject("test_log", "char", test_log);
@@ -210,7 +213,9 @@ TEST_GROUP(appid_api)
     {
         delete flow;
         mock().clear();
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
+        mock_cleanup_appid_pegs();
+        delete &mock_session->get_api();
+        delete mock_session;
     }
 };
 
@@ -382,13 +387,6 @@ TEST(appid_api, is_service_http_type)
 
 int main(int argc, char** argv)
 {
-    mock_init_appid_pegs();
-    SfIp ip;
-    mock_session = new AppIdSession(IpProtocol::TCP, &ip, 1492, dummy_appid_inspector,
-        dummy_appid_inspector.get_ctxt().get_odp_ctxt());
     int rc = CommandLineTestRunner::RunAllTests(argc, argv);
-    mock_cleanup_appid_pegs();
-    delete &mock_session->get_api();
-    delete mock_session;
     return rc;
 }
index e38a03dd7c646e479b5bf24b0f177fc9eb6c0afa..557dfb33aa75e4ddec40b6ec1225db9eb3234ad2 100644 (file)
@@ -66,7 +66,6 @@ TEST_GROUP(appid_detector_tests)
 
     void setup() override
     {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         SfIp ip;
         mock_session = new AppIdSession(IpProtocol::TCP, &ip, 1492, dummy_appid_inspector,
             dummy_appid_inspector.get_ctxt().get_odp_ctxt());
@@ -79,7 +78,6 @@ TEST_GROUP(appid_detector_tests)
         delete flow;
         delete &mock_session->get_api();
         delete mock_session;
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
index 378a2dc1cd76e3417115f3973e4937925bdee31f..e4fc3766afbfd3be101c281b45d729cef86d3dfc 100644 (file)
@@ -245,7 +245,6 @@ TEST_GROUP(appid_http_event)
 {
     void setup() override
     {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         flow = new Flow;
         SfIp ip;
         mock_session = new AppIdSession(IpProtocol::TCP, &ip, 1492, dummy_appid_inspector, stub_odp_ctxt);
@@ -263,7 +262,6 @@ TEST_GROUP(appid_http_event)
         delete flow;
         mock().clear();
         delete appidDebug;
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
index a21073293e34cd45fca057aca21bf7bf5aad175e..b64de9c0fab199c9c93c8cf8e66a2c4745e19dd8 100644 (file)
@@ -45,6 +45,7 @@
 
 #include <CppUTest/CommandLineTestRunner.h>
 #include <CppUTest/TestHarness.h>
+
 using namespace snort;
 
 namespace snort
@@ -113,7 +114,9 @@ AppIdSession::AppIdSession(IpProtocol, const SfIp* ip, uint16_t, AppIdInspector&
 {}
 
 AppIdSession::~AppIdSession()
-{}
+{
+    delete &api;
+}
 
 void AppIdSession::set_client_appid_data(AppId, AppidChangeBits&, char*)
 {
@@ -162,8 +165,6 @@ void Profiler::consolidate_stats() { }
 void Profiler::reset_stats() { }
 void Profiler::show_stats() { }
 
-MemoryContext::MemoryContext(MemoryTracker&) { }
-MemoryContext::~MemoryContext() { }
 void memory::MemoryCap::update_allocations(size_t) { }
 void memory::MemoryCap::update_deallocations(size_t) { }
 
@@ -175,22 +176,24 @@ AppIdConfig::~AppIdConfig() { }
 unsigned AppIdSession::inspector_id = 0;
 THREAD_LOCAL AppIdDebug* appidDebug = nullptr;
 
-SfIp sfip;
-AppIdSession session(IpProtocol::IP, &sfip, 0, dummy_appid_inspector, stub_odp_ctxt);
-AppIdHttpSession mock_hsession(session, 0);
-
 TEST_GROUP(appid_http_session)
 {
+    AppIdHttpSession* mock_hsession;
+    AppIdSession* session;
+
     void setup() override
     {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
+        SfIp sfip;
+        session = new AppIdSession(IpProtocol::IP, &sfip, 0, dummy_appid_inspector, stub_odp_ctxt);
+        mock_hsession = new AppIdHttpSession(*session, 0);
         appidDebug = new AppIdDebug();
     }
 
     void teardown() override
     {
         delete appidDebug;
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
+        delete mock_hsession;
+        delete session;
     }
 };
 
@@ -201,49 +204,49 @@ TEST(appid_http_session, http_field_ids_enum_order)
     // in appid_http_session.h.
     AppidChangeBits change_bits;
 
-    mock_hsession.set_field( (HttpFieldIds)0, new std::string("agent"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)1, new std::string("host"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)2, new std::string("referer"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)3, new std::string("uri"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)4, new std::string("cookie"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)5, new std::string("req_body"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)6, new std::string("content_type"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)7, new std::string("location"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)8, new std::string("rsp_body"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)9, new std::string("via"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)10, new std::string("response_code"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)11, new std::string("server"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)12, new std::string("xww"), change_bits );
-    mock_hsession.set_field( (HttpFieldIds)13, new std::string("url"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)0, new std::string("agent"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)1, new std::string("host"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)2, new std::string("referer"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)3, new std::string("uri"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)4, new std::string("cookie"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)5, new std::string("req_body"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)6, new std::string("content_type"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)7, new std::string("location"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)8, new std::string("rsp_body"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)9, new std::string("via"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)10, new std::string("response_code"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)11, new std::string("server"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)12, new std::string("xww"), change_bits );
+    mock_hsession->set_field( (HttpFieldIds)13, new std::string("url"), change_bits );
 
     const std::string* field;
-    field = mock_hsession.get_field(REQ_AGENT_FID);
+    field = mock_hsession->get_field(REQ_AGENT_FID);
     STRCMP_EQUAL(field->c_str(), "agent");
-    field = mock_hsession.get_field(REQ_HOST_FID);
+    field = mock_hsession->get_field(REQ_HOST_FID);
     STRCMP_EQUAL(field->c_str(), "host");
-    field = mock_hsession.get_field(REQ_REFERER_FID);
+    field = mock_hsession->get_field(REQ_REFERER_FID);
     STRCMP_EQUAL(field->c_str(), "referer");
-    field = mock_hsession.get_field(REQ_URI_FID);
+    field = mock_hsession->get_field(REQ_URI_FID);
     STRCMP_EQUAL(field->c_str(), "uri");
-    field = mock_hsession.get_field(REQ_COOKIE_FID);
+    field = mock_hsession->get_field(REQ_COOKIE_FID);
     STRCMP_EQUAL(field->c_str(), "cookie");
-    field = mock_hsession.get_field(REQ_BODY_FID);
+    field = mock_hsession->get_field(REQ_BODY_FID);
     STRCMP_EQUAL(field->c_str(), "req_body");
-    field = mock_hsession.get_field(RSP_CONTENT_TYPE_FID);
+    field = mock_hsession->get_field(RSP_CONTENT_TYPE_FID);
     STRCMP_EQUAL(field->c_str(), "content_type");
-    field = mock_hsession.get_field(RSP_LOCATION_FID);
+    field = mock_hsession->get_field(RSP_LOCATION_FID);
     STRCMP_EQUAL(field->c_str(), "location");
-    field = mock_hsession.get_field(RSP_BODY_FID);
+    field = mock_hsession->get_field(RSP_BODY_FID);
     STRCMP_EQUAL(field->c_str(), "rsp_body");
-    field = mock_hsession.get_field(MISC_VIA_FID);
+    field = mock_hsession->get_field(MISC_VIA_FID);
     STRCMP_EQUAL(field->c_str(), "via");
-    field = mock_hsession.get_field(MISC_RESP_CODE_FID);
+    field = mock_hsession->get_field(MISC_RESP_CODE_FID);
     STRCMP_EQUAL(field->c_str(), "response_code");
-    field = mock_hsession.get_field(MISC_SERVER_FID);
+    field = mock_hsession->get_field(MISC_SERVER_FID);
     STRCMP_EQUAL(field->c_str(), "server");
-    field = mock_hsession.get_field(MISC_XWW_FID);
+    field = mock_hsession->get_field(MISC_XWW_FID);
     STRCMP_EQUAL(field->c_str(), "xww");
-    field = mock_hsession.get_field(MISC_URL_FID);
+    field = mock_hsession->get_field(MISC_URL_FID);
     STRCMP_EQUAL(field->c_str(), "url");
 
     // Detect changes in host, url, user agent, response, and referer fields
@@ -261,9 +264,9 @@ TEST(appid_http_session, set_tun_dest)
     SfIp ipv6;
     ipv6.set("2001:db8:85a3::8a2e:370:7334");
     AppidChangeBits change_bits;
-    mock_hsession.set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:7334]:51413"), change_bits);
-    mock_hsession.set_tun_dest();
-    tun_dest = mock_hsession.get_tun_dest();
+    mock_hsession->set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:7334]:51413"), change_bits);
+    mock_hsession->set_tun_dest();
+    tun_dest = mock_hsession->get_tun_dest();
     CHECK(tun_dest != nullptr);
     CHECK_EQUAL(tun_dest->port, 51413);
     CHECK_EQUAL((ipv6 == tun_dest->ip), true);
@@ -273,16 +276,16 @@ TEST(appid_http_session, set_tun_dest_bad_uri)
 {
     const TunnelDest* tun_dest  = nullptr;
     AppidChangeBits change_bits;
-    mock_hsession.set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:1234]:51413"), change_bits);
-    mock_hsession.set_tun_dest();
-    tun_dest = mock_hsession.get_tun_dest();
+    mock_hsession->set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:1234]:51413"), change_bits);
+    mock_hsession->set_tun_dest();
+    tun_dest = mock_hsession->get_tun_dest();
     CHECK(tun_dest != nullptr);
 
     // Testing with bad URL
-    mock_hsession.free_tun_dest();
-    mock_hsession.set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:1235]"), change_bits);
-    mock_hsession.set_tun_dest();
-    tun_dest = mock_hsession.get_tun_dest();
+    mock_hsession->free_tun_dest();
+    mock_hsession->set_field(REQ_URI_FID, new std::string("[2001:db8:85a3::8a2e:370:1235]"), change_bits);
+    mock_hsession->set_tun_dest();
+    tun_dest = mock_hsession->get_tun_dest();
     CHECK(tun_dest == nullptr);
 }
 
@@ -290,14 +293,13 @@ TEST(appid_http_session, change_bits_for_referred_appid)
 {
     // Testing set_referred_payload_app_id_data
     AppidChangeBits change_bits;
-    AppIdPegCounts::init_pegs();
     AppIdConfig config;
     OdpContext odp_ctxt(config, nullptr);
-    session.set_service_id(APP_ID_HTTP, odp_ctxt);
-    session.scan_flags |= SCAN_HTTP_HOST_URL_FLAG;
-    mock_hsession.set_skip_simple_detect(false);
-    mock_hsession.set_field( (HttpFieldIds)2, new std::string("referer"), change_bits );
-    mock_hsession.process_http_packet(APP_ID_FROM_INITIATOR, change_bits, odp_ctxt.get_http_matchers());
+    session->set_service_id(APP_ID_HTTP, odp_ctxt);
+    session->scan_flags |= SCAN_HTTP_HOST_URL_FLAG;
+    mock_hsession->set_skip_simple_detect(false);
+    mock_hsession->set_field( (HttpFieldIds)2, new std::string("referer"), change_bits );
+    mock_hsession->process_http_packet(APP_ID_FROM_INITIATOR, change_bits, odp_ctxt.get_http_matchers());
 
     // Detect changes in referred appid
     CHECK_EQUAL(change_bits.test(APPID_REFERRED_BIT), true);
index 2e343a153cdb44663388004de7ee718868b2e267..05fd3426ab124d4778e0612e1a41b8db163c994f 100644 (file)
@@ -35,7 +35,9 @@ namespace snort
 {
 char* snort_strndup(const char* src, size_t dst_size)
 {
-    return strndup(src, dst_size);
+    char* dup = (char*)snort_calloc(dst_size + 1);
+    strncpy(dup, src, dst_size + 1);
+    return dup;
 }
 
 char* snort_strdup(const char* str)
index 151020b165f08685de47f425f4adf05a520646af..aadf2ef3b23bfa356d13393cc0b4e27d81347b5c 100644 (file)
@@ -35,10 +35,9 @@ AppIdHttpSession::AppIdHttpSession(AppIdSession& session, uint32_t http2_stream_
 AppIdHttpSession::~AppIdHttpSession()
 {
     for ( int i = 0; i < NUM_METADATA_FIELDS; i++)
-    {
-        if ( meta_data[i] )
-            delete meta_data[i];
-    }
+        delete meta_data[i];
+    if (tun_dest)
+        delete tun_dest;
 }
 
 int AppIdHttpSession::process_http_packet(AppidSessionDirection, AppidChangeBits&, HttpPatternMatchers&) { return 0; }
index 093d86a09331ba40839d07addf39691f742dd9b0..caafc38d7e082698e40577263c4ad25c770d791b 100644 (file)
@@ -66,7 +66,6 @@ TEST_GROUP(appid_session_api)
     {
         AppidChangeBits change_bits;
 
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         SfIp ip;
         mock_session = new AppIdSession(IpProtocol::TCP, &ip, 1492, dummy_appid_inspector, odpctxt);
         mock_session->set_ss_application_ids(APPID_UT_ID, APPID_UT_ID, APPID_UT_ID,
@@ -77,7 +76,6 @@ TEST_GROUP(appid_session_api)
     {
         delete &mock_session->get_api();
         delete mock_session;
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
index 6bd5c29cdeea37cb20d35abfb1042158eb358264..101af99ce28aabb18912a458bea8a1aa65e9b2df 100644 (file)
@@ -20,7 +20,7 @@
 #ifndef RNA_PND_H
 #define RNA_PND_H
 
-#include <limits>
+#include <climits>
 
 #include "helpers/discovery_filter.h"
 #include "host_tracker/host_tracker.h"
@@ -36,8 +36,6 @@
 #include "rna_logger.h"
 #include "rna_mac_cache.h"
 
-#define USHRT_MAX std::numeric_limits<unsigned short>::max()
-
 enum class TcpPacketType
 {
     SYN, SYN_ACK, MIDSTREAM
index 74aa50520349988e16db9e7c93a6df1f9638100f..02ae13a7b20431720221e0c695abfdc311b22ca0 100644 (file)
 bool Swapper::reload_in_progress = false;
 THREAD_LOCAL RnaStats rna_stats;
 THREAD_LOCAL ProfileStats rna_perf_stats;
-static std::string message;
 static Request mock_request;
 
 const char* luaL_optlstring(lua_State*, int, const char*, size_t*) { return nullptr; }
 
-void Request::respond(const char* msg, bool, bool)
-{
-    message = msg;
-}
 Request& get_current_request()
 { return mock_request; }
 
index e41a146e3fa61d8bce6936593b3d247cb3606b30..231ef880f9f087d9c00e7e6b7f90f34c2eaa5cdb 100644 (file)
 
 #include <CppUTest/CommandLineTestRunner.h>
 #include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
+
+void Request::respond(const char* msg, bool, bool)
+{
+    mock().actualCall("respond").onObject(this).withParameter("msg", msg);
+}
 
 TEST_GROUP(rna_module_test)
 {
-    void setup() override
-    {
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-    }
-    void teardown() override
-    {
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
-    }
 };
 
 TEST(rna_module_test, reload_fingerprint)
 {
     // When another reload is pending
+    mock().expectOneCall("respond").onObject(&mock_request).withParameter("msg", "== reload pending; retry\n");
     Swapper::set_reload_in_progress(true);
     reload_fingerprint(nullptr);
     Swapper::set_reload_in_progress(false);
-    CHECK_TRUE(message == "== reload pending; retry\n");
+    mock().checkExpectations();
 
     // When rna is not configured
+    mock().expectOneCall("respond").onObject(&mock_request).withParameter("msg", "== reload fingerprint failed - rna not enabled\n");
     reload_fingerprint(nullptr);
-    CHECK_TRUE(message == "== reload fingerprint failed - rna not enabled\n");
+    mock().checkExpectations();
 
     // Reload in progress flag should remain unset at the end
     CHECK_FALSE(Swapper::get_reload_in_progress());
+
+    mock().clear();
 }
 
 TEST(rna_module_test, push_tcp_fingerprints)
index e509d2062733ffa21866ad94084f21271ae93e1d..fd7a21addb7023d8d43e7af037b3fd9cac6bcfbf 100644 (file)
@@ -116,7 +116,12 @@ int SnortConfig::request_scratch(ScratchAllocator* s)
     return 0;
 }
 
-void SnortConfig::release_scratch(int) { }
+void SnortConfig::release_scratch(int)
+{
+    scratcher = nullptr;
+    s_state.clear();
+    s_state.shrink_to_fit();
+}
 
 const SnortConfig* SnortConfig::get_conf()
 { return snort_conf; }
@@ -224,8 +229,6 @@ TEST_GROUP(mpse_hs_match)
 
     void setup() override
     {
-        // FIXIT-L cpputest hangs or crashes in the leak detector
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         CHECK(se_hyperscan);
         mod = mpse_api->base.mod_ctor();
         hs = mpse_api->ctor(snort_conf, nullptr, &s_agent);
@@ -239,7 +242,6 @@ TEST_GROUP(mpse_hs_match)
         if ( do_cleanup )
             scratcher->cleanup(snort_conf);
         mpse_api->base.mod_dtor(mod);
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
@@ -373,8 +375,6 @@ TEST_GROUP(mpse_hs_multi)
 
     void setup() override
     {
-        // FIXIT-L cpputest hangs or crashes in the leak detector
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         CHECK(se_hyperscan);
 
         mod = mpse_api->base.mod_ctor();
@@ -395,7 +395,6 @@ TEST_GROUP(mpse_hs_multi)
         if ( do_cleanup )
             scratcher->cleanup(snort_conf);
         mpse_api->base.mod_dtor(mod);
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
@@ -431,6 +430,9 @@ TEST(mpse_hs_multi, single)
 
 int main(int argc, char** argv)
 {
+    // FIXIT-L There is currently no external way to fully release the memory from the static
+    //   s_scratch vector in hyperscan.cc
+    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
 
index 331fc6fb1beca5e86c992715f1d10b78970eb99e..0b83c15968aaede3a6078deeea2eab0a967a739e 100644 (file)
@@ -181,6 +181,7 @@ void SideChannelManager::term()
         delete scm;
 
     s_maps.clear();
+    s_maps.shrink_to_fit();
 }
 
 // receive at most max_messages.  Zero indicates unlimited.
index acefe8d74bc57c6df3aaff9ba0f5a9466a15ec54..780e73cba5678c04ac493dfbd605fd5413a893da 100644 (file)
@@ -146,10 +146,7 @@ TEST_GROUP(side_channel)
 {
     void setup() override
     {
-        // FIXIT-L workaround for issue with CppUTest memory leak detection
-        MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
         SideChannelManager::pre_config_init();
-
         SCConnectors test_connectors;
         PortBitSet test_ports;
 
@@ -200,7 +197,6 @@ TEST_GROUP(side_channel)
     {
         SideChannelManager::thread_term();
         SideChannelManager::term();
-        MemoryLeakWarningPlugin::turnOnNewDeleteOverloads();
     }
 };
 
@@ -283,7 +279,6 @@ TEST(side_channel, test_connector_receive_process_dispatch_discard)
 {
     SideChannel* sc = SideChannelManager::get_side_channel(1);
     CHECK(sc != nullptr);
-
     sc->register_receive_handler(receive_handler);
 
     bool success = sc->process(1);
index 8703e5fb13ccfffba4a4e4d738e91e03ca9de759..b704283b353c98948b1ff5267389e788e9980b4d 100644 (file)
@@ -1300,7 +1300,7 @@ int Defrag::insert(Packet* p, FragTracker* ft, FragEngine* fe)
                 }
 
                 debug_logf(stream_ip_trace, p, "left overlap, truncating new pkt (slide: %d)\n",
-                                       slide);
+                    slide);
 
                 break;
 
@@ -1947,7 +1947,7 @@ int Defrag::add_frag_node(
     ft->frag_bytes += newfrag->size;
 
     debug_logf(stream_ip_trace, nullptr, "[#] accumulated bytes on FragTracker %u, count %d\n",
-               ft->frag_bytes, ft->fraglist_count);
+        ft->frag_bytes, ft->fraglist_count);
 
     *retFrag = newfrag;
     return FRAG_INSERT_OK;
@@ -1981,7 +1981,7 @@ int Defrag::dup_frag_node( FragTracker* ft, Fragment* left, Fragment** retFrag)
     ft->frag_bytes += newfrag->size;
 
     debug_logf(stream_ip_trace, nullptr, "[#] accumulated bytes on FragTracker %u, count %d\n",
-               ft->frag_bytes, ft->fraglist_count);
+        ft->frag_bytes, ft->fraglist_count);
 
     *retFrag = newfrag;
     return FRAG_INSERT_OK;
index dd1422138bfa2b9e694441f4193e209095812247..318025e6e4153ab1ecc36bc9129e2cce01e5be27 100644 (file)
@@ -396,7 +396,7 @@ void SegmentOverlapEditor::full_right_overlap_os1(TcpReassemblerState& trs)
         drop_old_segment(trs);
     }
     else
-       full_right_overlap_truncate_new(trs);
+        full_right_overlap_truncate_new(trs);
 }
 
 // REASSEMBLY_POLICY_LINUX:
@@ -415,7 +415,7 @@ void SegmentOverlapEditor::full_right_overlap_os2(TcpReassemblerState& trs)
         drop_old_segment(trs);
     }
     else
-       full_right_overlap_truncate_new(trs);
+        full_right_overlap_truncate_new(trs);
 }
 
 // REASSEMBLY_POLICY_HPUX11:
@@ -447,7 +447,7 @@ void SegmentOverlapEditor::full_right_overlap_os4(TcpReassemblerState& trs)
 
 void SegmentOverlapEditor::full_right_overlap_os5(TcpReassemblerState& trs)
 {
-       full_right_overlap_truncate_new(trs);
+    full_right_overlap_truncate_new(trs);
 }
 
 void SegmentOverlapEditor::print(TcpReassemblerState& trs)
index b756bb7e7874ed4080f38dd1382b96e07858886b..125b32c8ee348198939082a413a2dabec54692a3 100644 (file)
@@ -61,8 +61,8 @@ TcpSegmentDescriptor::TcpSegmentDescriptor(Flow* f, Packet* p, TcpEventLogger& t
 
 TcpSegmentDescriptor::TcpSegmentDescriptor
     (snort::Flow* f, snort::Packet* p, uint32_t meta_ack, uint16_t window)
-       : flow(f), pkt(ma_pseudo_packet), tcph(&ma_pseudo_tcph),
-         packet_number(p->context->packet_number)
+    : flow(f), pkt(ma_pseudo_packet), tcph(&ma_pseudo_tcph),
+      packet_number(p->context->packet_number)
 {
     // init tcp header fields for meta-ack packet
     ma_pseudo_tcph.th_dport = p->ptrs.tcph->raw_src_port();
@@ -76,7 +76,7 @@ TcpSegmentDescriptor::TcpSegmentDescriptor
     ma_pseudo_tcph.th_urp = 0;
 
     // init meta-ack Packet fields stream cares about for TCP ack processing
-       pkt->flow = p->flow;
+    pkt->flow = p->flow;
     pkt->context = p->context;
     pkt->dsize = 0;
 
index 4c584e547d0fee7986d6f49bf140609e47836ec6..ac50a1b6a62f31463b2253f449a53fcafcd46a27 100644 (file)
@@ -229,7 +229,6 @@ TEST(tcp_normalizers, norm_options_enabled)
 
 int main(int argc, char** argv)
 {
-    //MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
     return CommandLineTestRunner::RunAllTests(argc, argv);
 }
 
index b96299674b2990d0540f19dad29370ad80ef6d07..ab439d91da35bbcfa41e67ba46dac2a401daa385 100644 (file)
@@ -27,6 +27,7 @@
 #if defined(__linux__)
 #include <sys/syscall.h>
 #endif
+#include <sys/time.h>
 #include <unistd.h>
 
 #include <cerrno>