From 7884add24da89487cf53d6286116d477f6b2c98d Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Wed, 2 Oct 2024 08:54:46 -0700 Subject: [PATCH 1/3] Add a compile_output::clone() method that can be overridden Add a clone() method that can be overridden by subclasses. This allows copying compile state when needed in a way that preserves polymorphism. Signed-off-by: Mark Stemm --- userspace/engine/rule_loader_compile_output.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/userspace/engine/rule_loader_compile_output.h b/userspace/engine/rule_loader_compile_output.h index 6aed9389a30..edfb6aeedef 100644 --- a/userspace/engine/rule_loader_compile_output.h +++ b/userspace/engine/rule_loader_compile_output.h @@ -20,6 +20,8 @@ limitations under the License. #include "indexed_vector.h" #include "falco_rule.h" +#include + namespace rule_loader { struct compile_output { compile_output() = default; @@ -29,6 +31,10 @@ struct compile_output { compile_output(const compile_output&) = default; compile_output& operator=(const compile_output&) = default; + virtual std::unique_ptr clone() const { + return std::make_unique(*this); + }; + indexed_vector lists; indexed_vector macros; indexed_vector rules; From e10d54b95b90d7fd25ab37d0cd8bc28d35a31e0d Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Wed, 2 Oct 2024 08:55:27 -0700 Subject: [PATCH 2/3] Add equality operators for indexed_vector/falco_{list,macro,rule} Add an equality operator for indexed_vector. As indexed_vectors commonly hold falco lists/macros/rules, also add equality operators for those structs. For condition/sinsp_filter shared_ptrs, the operator checks that the shared_ptrs point to the same underlying memory. Signed-off-by: Mark Stemm --- userspace/engine/falco_rule.h | 24 ++++++++++++++++++++++++ userspace/engine/indexed_vector.h | 3 +++ 2 files changed, 27 insertions(+) diff --git a/userspace/engine/falco_rule.h b/userspace/engine/falco_rule.h index 2ed166d57d5..b247c430f2d 100644 --- a/userspace/engine/falco_rule.h +++ b/userspace/engine/falco_rule.h @@ -35,6 +35,11 @@ struct falco_list { falco_list& operator=(const falco_list&) = default; ~falco_list() = default; + bool operator==(const falco_list& rhs) const { + return (this->used == rhs.used && this->id == rhs.id && this->name == rhs.name && + this->items == rhs.items); + } + bool used; std::size_t id; std::string name; @@ -53,6 +58,14 @@ struct falco_macro { falco_macro& operator=(const falco_macro&) = default; ~falco_macro() = default; + bool operator==(const falco_macro& rhs) const { + // Note this only ensures that the shared_ptrs are + // pointing to the same underlying memory, not that + // they are logically equal. + return (this->used == rhs.used && this->id == rhs.id && this->name == rhs.name && + this->condition.get() == rhs.condition.get()); + } + bool used; std::size_t id; std::string name; @@ -71,6 +84,17 @@ struct falco_rule { falco_rule& operator=(const falco_rule&) = default; ~falco_rule() = default; + bool operator==(const falco_rule& rhs) const { + // Note this only ensures that the shared_ptrs are + // pointing to the same underlying memory, not that + // they are logically equal. + return (this->id == rhs.id && this->source == rhs.source && this->name == rhs.name && + this->description == rhs.description && this->output == rhs.output && + this->tags == rhs.tags && this->exception_fields == rhs.exception_fields && + this->priority == rhs.priority && this->condition.get() == rhs.condition.get() && + this->filter.get() == rhs.filter.get()); + } + std::size_t id; std::string source; std::string name; diff --git a/userspace/engine/indexed_vector.h b/userspace/engine/indexed_vector.h index 21d3cbf643f..0c24af2f03f 100644 --- a/userspace/engine/indexed_vector.h +++ b/userspace/engine/indexed_vector.h @@ -34,6 +34,9 @@ class indexed_vector { indexed_vector& operator=(indexed_vector&&) = default; indexed_vector(const indexed_vector&) = default; indexed_vector& operator=(const indexed_vector&) = default; + bool operator==(const indexed_vector& rhs) const { + return (this->m_entries == rhs.m_entries && this->m_index == rhs.m_index); + } /*! \brief Returns the number of elements From 42edde139ea7156d706700c982f5f75725b9aef2 Mon Sep 17 00:00:00 2001 From: Mark Stemm Date: Wed, 2 Oct 2024 08:55:59 -0700 Subject: [PATCH 3/3] Add a test for compile_output::clone() for derived type Add a test for checking that compile_output::clone() returns equal values, specifically in the case of derived values. Signed-off-by: Mark Stemm --- unit_tests/engine/test_alt_rule_loader.cpp | 36 +++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/unit_tests/engine/test_alt_rule_loader.cpp b/unit_tests/engine/test_alt_rule_loader.cpp index a6eae9e89cb..95e5a1b88a3 100644 --- a/unit_tests/engine/test_alt_rule_loader.cpp +++ b/unit_tests/engine/test_alt_rule_loader.cpp @@ -15,6 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include #include #include @@ -41,7 +42,10 @@ struct test_object_info { struct test_compile_output : public rule_loader::compile_output { test_compile_output() = default; - ~test_compile_output() = default; + virtual ~test_compile_output() = default; + virtual std::unique_ptr clone() const override { + return std::make_unique(*this); + } std::set defined_test_properties; }; @@ -320,3 +324,33 @@ TEST(engine_loader_alt_loader, falco_engine_alternate_loader) { EXPECT_TRUE(defined_properties.find("other-value") != defined_properties.end()); EXPECT_TRUE(defined_properties.find("not-exists-value") == defined_properties.end()); }; + +TEST(engine_loader_alt_loader, clone_compile_output) { + sinsp inspector; + sinsp_filter_check_list filterchecks; + indexed_vector sources; + + std::shared_ptr cfg = + create_configuration(inspector, filterchecks, sources); + + test_reader reader; + test_collector collector; + test_compiler compiler; + + EXPECT_TRUE(reader.read(*cfg, collector)); + + std::unique_ptr compile_output = compiler.new_compile_output(); + + compiler.compile(*cfg, collector, *compile_output); + + const test_compile_output& original_ref = + dynamic_cast(*(compile_output.get())); + + std::unique_ptr copy = compile_output->clone(); + const test_compile_output& copy_ref = dynamic_cast(*(copy.get())); + + EXPECT_EQ(copy_ref.lists, original_ref.lists); + EXPECT_EQ(copy_ref.macros, original_ref.macros); + EXPECT_EQ(copy_ref.rules, original_ref.rules); + EXPECT_EQ(copy_ref.defined_test_properties, original_ref.defined_test_properties); +}