From a8be640a853012b4836dd4fd408c2fc385273fc1 Mon Sep 17 00:00:00 2001 From: Hilko Bengen Date: Sat, 20 Aug 2022 22:42:46 +0200 Subject: [PATCH] Make module implementations self-contained Individual modules' YR_MODULE structs are now defined alongside the rest of the module code. --- libyara/include/yara/modules.h | 13 +++++++++++++ libyara/modules.c | 19 +------------------ libyara/modules/console/console.c | 2 ++ libyara/modules/cuckoo/cuckoo.c | 2 ++ libyara/modules/demo/demo.c | 2 ++ libyara/modules/dex/dex.c | 2 +- libyara/modules/dotnet/dotnet.c | 2 ++ libyara/modules/elf/elf.c | 2 ++ libyara/modules/hash/hash.c | 2 ++ libyara/modules/macho/macho.c | 2 ++ libyara/modules/magic/magic.c | 2 ++ libyara/modules/math/math.c | 2 ++ libyara/modules/pb_tests/pb_tests.c | 2 ++ libyara/modules/pe/pe.c | 2 ++ libyara/modules/tests/tests.c | 2 ++ libyara/modules/time/time.c | 2 ++ 16 files changed, 41 insertions(+), 19 deletions(-) diff --git a/libyara/include/yara/modules.h b/libyara/include/yara/modules.h index d11c714178..62f62cf454 100644 --- a/libyara/include/yara/modules.h +++ b/libyara/include/yara/modules.h @@ -303,6 +303,19 @@ struct YR_MODULE YR_EXT_FINALIZE_FUNC finalize; }; +#define _yr_module_define_named(name) \ + YR_MODULE name##__module = { \ + #name, \ + name##__declarations, \ + name##__load, \ + name##__unload, \ + name##__initialize, \ + name##__finalize} + +#define yr_module_define_named(name) _yr_module_define_named(name) + +#define yr_module_define() yr_module_define_named(MODULE_NAME) + struct YR_MODULE_IMPORT { const char* module_name; diff --git a/libyara/modules.c b/libyara/modules.c index 7db0aeea15..a7a8fda006 100644 --- a/libyara/modules.c +++ b/libyara/modules.c @@ -31,24 +31,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -#define MODULE(name) \ - int name##__declarations(YR_OBJECT* module); \ - int name##__load( \ - YR_SCAN_CONTEXT* context, \ - YR_OBJECT* module, \ - void* module_data, \ - size_t module_data_size); \ - int name##__unload(YR_OBJECT* main_structure); \ - int name##__initialize(YR_MODULE* module); \ - int name##__finalize(YR_MODULE* module); \ - \ - static YR_MODULE name##__module = { \ - #name, \ - name##__declarations, \ - name##__load, \ - name##__unload, \ - name##__initialize, \ - name##__finalize}; +#define MODULE(name) extern YR_MODULE name##__module; #include diff --git a/libyara/modules/console/console.c b/libyara/modules/console/console.c index a5c46acc11..18cd0787b3 100644 --- a/libyara/modules/console/console.c +++ b/libyara/modules/console/console.c @@ -270,3 +270,5 @@ int module_unload(YR_OBJECT* module_object) { return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/cuckoo/cuckoo.c b/libyara/modules/cuckoo/cuckoo.c index cc69dc77f4..8bc181b1c6 100644 --- a/libyara/modules/cuckoo/cuckoo.c +++ b/libyara/modules/cuckoo/cuckoo.c @@ -464,3 +464,5 @@ int module_unload(YR_OBJECT* module) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/demo/demo.c b/libyara/modules/demo/demo.c index 4ebca6bdf7..5e846b8049 100644 --- a/libyara/modules/demo/demo.c +++ b/libyara/modules/demo/demo.c @@ -64,3 +64,5 @@ int module_unload(YR_OBJECT* module_object) { return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/dex/dex.c b/libyara/modules/dex/dex.c index 6f302e5f9d..a50ec1dd0a 100644 --- a/libyara/modules/dex/dex.c +++ b/libyara/modules/dex/dex.c @@ -1504,4 +1504,4 @@ int module_unload(YR_OBJECT* module_object) return ERROR_SUCCESS; } -#undef MODULE_NAME +yr_module_define(); diff --git a/libyara/modules/dotnet/dotnet.c b/libyara/modules/dotnet/dotnet.c index 718d5be25a..a8323ff1a1 100644 --- a/libyara/modules/dotnet/dotnet.c +++ b/libyara/modules/dotnet/dotnet.c @@ -3536,3 +3536,5 @@ int module_unload(YR_OBJECT* module_object) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/elf/elf.c b/libyara/modules/elf/elf.c index cbfc2a57c9..a4939d8cd0 100644 --- a/libyara/modules/elf/elf.c +++ b/libyara/modules/elf/elf.c @@ -1253,3 +1253,5 @@ int module_unload(YR_OBJECT* module_object) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/hash/hash.c b/libyara/modules/hash/hash.c index 84feb4ede6..58a7b23341 100644 --- a/libyara/modules/hash/hash.c +++ b/libyara/modules/hash/hash.c @@ -867,3 +867,5 @@ int module_unload(YR_OBJECT* module_object) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/macho/macho.c b/libyara/modules/macho/macho.c index e42c69c0c6..188ecf9037 100644 --- a/libyara/modules/macho/macho.c +++ b/libyara/modules/macho/macho.c @@ -1373,3 +1373,5 @@ int module_unload(YR_OBJECT* module_object) { return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/magic/magic.c b/libyara/modules/magic/magic.c index 5e435d8f41..025551e04f 100644 --- a/libyara/modules/magic/magic.c +++ b/libyara/modules/magic/magic.c @@ -201,3 +201,5 @@ int module_unload(YR_OBJECT* module) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/math/math.c b/libyara/modules/math/math.c index bd549dd362..873f6432f2 100644 --- a/libyara/modules/math/math.c +++ b/libyara/modules/math/math.c @@ -826,3 +826,5 @@ int module_unload(YR_OBJECT* module_object) { return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/pb_tests/pb_tests.c b/libyara/modules/pb_tests/pb_tests.c index c9b5e0a079..7d34e052e0 100644 --- a/libyara/modules/pb_tests/pb_tests.c +++ b/libyara/modules/pb_tests/pb_tests.c @@ -317,3 +317,5 @@ int module_load( return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/pe/pe.c b/libyara/modules/pe/pe.c index 0c4c832c79..3b2a0857f8 100644 --- a/libyara/modules/pe/pe.c +++ b/libyara/modules/pe/pe.c @@ -4075,3 +4075,5 @@ int module_unload(YR_OBJECT* module_object) return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/tests/tests.c b/libyara/modules/tests/tests.c index 69e5d3b39c..eb72ded94d 100644 --- a/libyara/modules/tests/tests.c +++ b/libyara/modules/tests/tests.c @@ -215,3 +215,5 @@ int module_unload(YR_OBJECT* module_object) module_object->data = (void*) 0xFABADA; return ERROR_SUCCESS; } + +yr_module_define(); diff --git a/libyara/modules/time/time.c b/libyara/modules/time/time.c index badf8f4687..6a233b17b0 100644 --- a/libyara/modules/time/time.c +++ b/libyara/modules/time/time.c @@ -73,3 +73,5 @@ int module_unload(YR_OBJECT* module_object) { return ERROR_SUCCESS; } + +yr_module_define();