From b30178700fef4ea9f6111c251d83d876eb52d25c Mon Sep 17 00:00:00 2001 From: Jason Dellaluce Date: Fri, 24 Jan 2025 14:56:56 +0000 Subject: [PATCH] fix(libsinsp/test): remove illegal threat manager memory access in plugin tests Signed-off-by: Jason Dellaluce --- userspace/libsinsp/test/plugins.ut.cpp | 69 +++++++++++-------- .../test/plugins/syscall_subtables.cpp | 2 +- .../test/plugins/syscall_subtables_array.cpp | 2 +- .../plugins/syscall_subtables_array_pair.cpp | 2 +- 4 files changed, 42 insertions(+), 33 deletions(-) diff --git a/userspace/libsinsp/test/plugins.ut.cpp b/userspace/libsinsp/test/plugins.ut.cpp index 06a4abde57..b5c7a00e12 100644 --- a/userspace/libsinsp/test/plugins.ut.cpp +++ b/userspace/libsinsp/test/plugins.ut.cpp @@ -862,9 +862,17 @@ TEST_F(sinsp_with_test_input, plugin_subtables) { ASSERT_EQ(table->entries_count(), 0); + // start the event capture + // we coordinate with the plugin by sending open events: for each one received, + // the plugin will take a subsequent action on which we then assert the status + // note: we need to do this before adding our artificial threadinfo because + // the threads table is cleared up upon inspectors being opened + open_inspector(); + // add a new entry to the thread table - ASSERT_NE(table->add_entry(5, table->new_entry()), nullptr); - auto entry = table->get_entry(5); + int64_t tid = 5; + ASSERT_NE(table->add_entry(tid, table->new_entry()), nullptr); + auto entry = table->get_entry(tid); ASSERT_NE(entry, nullptr); ASSERT_EQ(table->entries_count(), 1); @@ -890,14 +898,9 @@ TEST_F(sinsp_with_test_input, plugin_subtables) { ASSERT_NE(dfield, subtable->dynamic_fields()->fields().end()); auto dfieldacc = dfield->second.new_accessor(); - // start the event capture - // we coordinate with the plugin by sending open events: for each one received, - // the plugin will take a subsequent action on which we then assert the status - open_inspector(); - // step #0: the plugin should populate the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -918,7 +921,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables) { // step #1: the plugin should remove one entry from the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -928,7 +931,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables) { // step #2: the plugin should cleae the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -960,9 +963,17 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array) { ASSERT_EQ(table->entries_count(), 0); + // start the event capture + // we coordinate with the plugin by sending open events: for each one received, + // the plugin will take a subsequent action on which we then assert the status + // note: we need to do this before adding our artificial threadinfo because + // the threads table is cleared up upon inspectors being opened + open_inspector(); + // add a new entry to the thread table - ASSERT_NE(table->add_entry(5, table->new_entry()), nullptr); - auto entry = table->get_entry(5); + int64_t tid = 5; + ASSERT_NE(table->add_entry(tid, table->new_entry()), nullptr); + auto entry = table->get_entry(tid); ASSERT_NE(entry, nullptr); ASSERT_EQ(table->entries_count(), 1); @@ -985,14 +996,9 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array) { ASSERT_EQ(dfield->second.info(), libsinsp::state::typeinfo::of()); auto dfieldacc = dfield->second.new_accessor(); - // start the event capture - // we coordinate with the plugin by sending open events: for each one received, - // the plugin will take a subsequent action on which we then assert the status - open_inspector(); - // step #0: the plugin should populate the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -1010,7 +1016,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array) { // step #1: the plugin should remove one entry from the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -1020,7 +1026,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array) { // step #2: the plugin should cleae the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -1053,9 +1059,17 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array_pair) { ASSERT_EQ(table->entries_count(), 0); + // start the event capture + // we coordinate with the plugin by sending open events: for each one received, + // the plugin will take a subsequent action on which we then assert the status + // note: we need to do this before adding our artificial threadinfo because + // the threads table is cleared up upon inspectors being opened + open_inspector(); + // add a new entry to the thread table - ASSERT_NE(table->add_entry(5, table->new_entry()), nullptr); - auto entry = table->get_entry(5); + int64_t tid = 5; + ASSERT_NE(table->add_entry(tid, table->new_entry()), nullptr); + auto entry = table->get_entry(tid); ASSERT_NE(entry, nullptr); ASSERT_EQ(table->entries_count(), 1); @@ -1087,14 +1101,9 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array_pair) { ASSERT_EQ(dfield_second->second.info(), libsinsp::state::typeinfo::of()); auto dfield_second_acc = dfield_second->second.new_accessor(); - // start the event capture - // we coordinate with the plugin by sending open events: for each one received, - // the plugin will take a subsequent action on which we then assert the status - open_inspector(); - // step #0: the plugin should populate the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -1114,7 +1123,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array_pair) { // step #1: the plugin should remove one entry from the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", @@ -1124,7 +1133,7 @@ TEST_F(sinsp_with_test_input, plugin_subtables_array_pair) { // step #2: the plugin should cleae the fdtable add_event_advance_ts(increasing_ts(), - 0, + tid, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", diff --git a/userspace/libsinsp/test/plugins/syscall_subtables.cpp b/userspace/libsinsp/test/plugins/syscall_subtables.cpp index 16b235a026..b69e2dac9c 100644 --- a/userspace/libsinsp/test/plugins/syscall_subtables.cpp +++ b/userspace/libsinsp/test/plugins/syscall_subtables.cpp @@ -178,7 +178,7 @@ ss_plugin_rc plugin_parse_event(ss_plugin_t* s, ss_plugin_table_t* fdtable = nullptr; - key.s64 = 0; + key.s64 = ev->evt->tid; entry = in->table_reader_ext->get_table_entry(ps->thread_table, &key); if(!entry) { ps->lasterr = "can't get table entry"; diff --git a/userspace/libsinsp/test/plugins/syscall_subtables_array.cpp b/userspace/libsinsp/test/plugins/syscall_subtables_array.cpp index 44daca24ee..a5317b120d 100644 --- a/userspace/libsinsp/test/plugins/syscall_subtables_array.cpp +++ b/userspace/libsinsp/test/plugins/syscall_subtables_array.cpp @@ -150,7 +150,7 @@ ss_plugin_rc plugin_parse_event(ss_plugin_t* s, ss_plugin_state_data out; ss_plugin_state_data data; - key.s64 = 0; + key.s64 = ev->evt->tid; ss_plugin_table_entry_t* tinfo = in->table_reader_ext->get_table_entry(ps->thread_table, &key); if(!tinfo) { ps->lasterr = "can't get table entry"; diff --git a/userspace/libsinsp/test/plugins/syscall_subtables_array_pair.cpp b/userspace/libsinsp/test/plugins/syscall_subtables_array_pair.cpp index f1223f0008..032bc80530 100644 --- a/userspace/libsinsp/test/plugins/syscall_subtables_array_pair.cpp +++ b/userspace/libsinsp/test/plugins/syscall_subtables_array_pair.cpp @@ -161,7 +161,7 @@ ss_plugin_rc plugin_parse_event(ss_plugin_t* s, ss_plugin_state_data out; ss_plugin_state_data data; - key.s64 = 0; + key.s64 = ev->evt->tid; ss_plugin_table_entry_t* tinfo = in->table_reader_ext->get_table_entry(ps->thread_table, &key); if(!tinfo) { ps->lasterr = "can't get table entry";