[BUGFIX]fix compile gc bug

This commit is contained in:
刘文坛
2023-06-19 12:30:25 +00:00
parent df36b8987b
commit d3aed20bfa
4 changed files with 76 additions and 11 deletions

View File

@@ -76,6 +76,8 @@ void *compile_runtime_get_ex_data(struct compile_runtime *compile_rt,
void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt, void compile_runtime_ex_data_iterate(struct compile_runtime *compile_rt,
struct compile_schema *compile_schema); struct compile_schema *compile_schema);
void compile_runtime_garbage_collect_routine(void *compile_runtime);
/* group2compile runtime API */ /* group2compile runtime API */
void *group2compile_runtime_new(void *g2c_schema, size_t max_thread_num, void *group2compile_runtime_new(void *g2c_schema, size_t max_thread_num,
struct maat_garbage_bin *garbage_bin, struct maat_garbage_bin *garbage_bin,

View File

@@ -580,8 +580,7 @@ void *compile_runtime_new(void *compile_schema, size_t max_thread_num,
compile_rt->expr_match_buff = ALLOC(struct bool_expr_match, compile_rt->expr_match_buff = ALLOC(struct bool_expr_match,
max_thread_num * MAX_SCANNER_HIT_COMPILE_NUM); max_thread_num * MAX_SCANNER_HIT_COMPILE_NUM);
compile_rt->version = time(NULL); compile_rt->version = time(NULL);
compile_rt->cfg_hash = rcu_hash_new(rcu_compile_cfg_free, NULL, compile_rt->cfg_hash = rcu_hash_new(rcu_compile_cfg_free, NULL, schema->gc_timeout_s);
schema->gc_timeout_s);
compile_rt->clause_by_literals_hash = NULL; compile_rt->clause_by_literals_hash = NULL;
compile_rt->literal2clause_hash = NULL; compile_rt->literal2clause_hash = NULL;
compile_rt->logger = logger; compile_rt->logger = logger;
@@ -2265,3 +2264,15 @@ size_t maat_compile_state_get_internal_hit_paths(struct maat_compile_state *comp
return hit_path_cnt; return hit_path_cnt;
} }
void compile_runtime_garbage_collect_routine(void *compile_runtime)
{
if (NULL == compile_runtime) {
return;
}
struct compile_runtime *compile_rt = (struct compile_runtime *)compile_runtime;
if (compile_rt->cfg_hash != NULL) {
rcu_hash_garbage_collect_routine(compile_rt->cfg_hash);
}
}

View File

@@ -25,6 +25,9 @@
#include "maat_table.h" #include "maat_table.h"
#include "maat_compile.h" #include "maat_compile.h"
#include "maat_plugin.h" #include "maat_plugin.h"
#include "maat_ip_plugin.h"
#include "maat_fqdn_plugin.h"
#include "maat_bool_plugin.h"
#include "maat_stat.h" #include "maat_stat.h"
#include "ip_matcher.h" #include "ip_matcher.h"
#include "alignment.h" #include "alignment.h"
@@ -170,16 +173,41 @@ void maat_plugin_table_garbage_collect_routine(struct table_manager *tbl_mgr)
{ {
size_t max_table_cnt = table_manager_table_size(tbl_mgr); size_t max_table_cnt = table_manager_table_size(tbl_mgr);
enum table_type table_type = TABLE_TYPE_INVALID; enum table_type table_type = TABLE_TYPE_INVALID;
void *runtime = NULL;
struct ex_data_runtime *ex_data_rt = NULL;
for (size_t i = 0; i < max_table_cnt; i++) { for (size_t i = 0; i < max_table_cnt; i++) {
table_type = table_manager_get_table_type(tbl_mgr, i); table_type = table_manager_get_table_type(tbl_mgr, i);
if (table_type != TABLE_TYPE_PLUGIN) {
continue;
}
void *plugin_runtime = table_manager_get_runtime(tbl_mgr, i); switch (table_type) {
struct ex_data_runtime *ex_data_rt = plugin_runtime_get_ex_data_rt(plugin_runtime); case TABLE_TYPE_COMPILE:
ex_data_runtime_garbage_collect_routine(ex_data_rt); runtime = table_manager_get_runtime(tbl_mgr, i);
compile_runtime_garbage_collect_routine(runtime);
break;
case TABLE_TYPE_PLUGIN:
runtime = table_manager_get_runtime(tbl_mgr, i);
ex_data_rt = plugin_runtime_get_ex_data_rt(runtime);
break;
case TABLE_TYPE_IP_PLUGIN:
runtime = table_manager_get_runtime(tbl_mgr, i);
ex_data_rt = ip_plugin_runtime_get_ex_data_rt(runtime);
break;
case TABLE_TYPE_FQDN_PLUGIN:
runtime = table_manager_get_runtime(tbl_mgr, i);
ex_data_rt = fqdn_plugin_runtime_get_ex_data_rt(runtime);
break;
case TABLE_TYPE_BOOL_PLUGIN:
runtime = table_manager_get_runtime(tbl_mgr, i);
ex_data_rt = bool_plugin_runtime_get_ex_data_rt(runtime);
break;
default:
break;
}
if (ex_data_rt != NULL) {
ex_data_runtime_garbage_collect_routine(ex_data_rt);
ex_data_rt = NULL;
}
} }
} }

View File

@@ -2798,7 +2798,10 @@ void ip_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{ {
struct ip_plugin_ud *ud = (struct ip_plugin_ud *)(*ad); struct ip_plugin_ud *ud = (struct ip_plugin_ud *)(*ad);
ud->rule_id = 0;
memset(ud->buffer, 0, ud->buf_len); memset(ud->buffer, 0, ud->buf_len);
ud->buf_len = 0;
free(ud->buffer); free(ud->buffer);
free(ud); free(ud);
*ad = NULL; *ad = NULL;
@@ -2931,6 +2934,9 @@ void fqdn_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{ {
struct fqdn_plugin_ud *u = (struct fqdn_plugin_ud *)(*ad); struct fqdn_plugin_ud *u = (struct fqdn_plugin_ud *)(*ad);
u->rule_id = 0;
u->catid = 0;
free(u); free(u);
*ad = NULL; *ad = NULL;
} }
@@ -3003,7 +3009,10 @@ void bool_plugin_ex_free_cb(int table_id, void **ad, long argl, void *argp)
{ {
struct bool_plugin_ud *u = (struct bool_plugin_ud *)(*ad); struct bool_plugin_ud *u = (struct bool_plugin_ud *)(*ad);
u->id = 0;
memset(u->name, 0, u->name_len); memset(u->name, 0, u->name_len);
u->name_len = 0;
free(u->name); free(u->name);
free(u); free(u);
*ad = NULL; *ad = NULL;
@@ -5059,12 +5068,18 @@ TEST_F(MaatCmdTest, UpdateIPPlugin) {
EXPECT_GT(ret, 0); EXPECT_GT(ret, 0);
} }
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S); //gc_timeout_s == 3 which configured in table_info
memset(results, 0, sizeof(results));
ret = maat_ip_plugin_table_get_ex_data(maat_inst, table_id, &ipv4, ret = maat_ip_plugin_table_get_ex_data(maat_inst, table_id, &ipv4,
(void **)results, ARRAY_SIZE); (void **)results, ARRAY_SIZE);
EXPECT_EQ(ret, 0); EXPECT_EQ(ret, 0);
//the data pointed by results[idx] has in garbage queue, but not be freed yet
EXPECT_EQ(results[0]->rule_id, 104);
EXPECT_EQ(results[1]->rule_id, 103);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
//excced gc_timeout_s, the data pointed by results[idx] has been freed
} }
TEST_F(MaatCmdTest, UpdateFQDNPlugin) { TEST_F(MaatCmdTest, UpdateFQDNPlugin) {
@@ -5122,6 +5137,7 @@ TEST_F(MaatCmdTest, UpdateFQDNPlugin) {
"r3---sn-i3belne6.example2.com", "r3---sn-i3belne6.example2.com",
(void**)results, ARRAY_SIZE); (void**)results, ARRAY_SIZE);
ASSERT_EQ(ret, 2); ASSERT_EQ(ret, 2);
EXPECT_EQ(results[0]->catid, 3);
//del lines //del lines
for (i = 3; i < TEST_CMD_LINE_NUM; i++) { for (i = 3; i < TEST_CMD_LINE_NUM; i++) {
@@ -5140,6 +5156,10 @@ TEST_F(MaatCmdTest, UpdateFQDNPlugin) {
"r3---sn-i3belne6.example2.com", "r3---sn-i3belne6.example2.com",
(void**)results, ARRAY_SIZE); (void**)results, ARRAY_SIZE);
ASSERT_EQ(ret, 0); ASSERT_EQ(ret, 0);
EXPECT_EQ(results[0]->catid, 3);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
//excced gc_timeout_s, the data pointed by results[idx] has been freed
} }
TEST_F(MaatCmdTest, UpdateBoolPlugin) { TEST_F(MaatCmdTest, UpdateBoolPlugin) {
@@ -5198,6 +5218,7 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
ret = maat_bool_plugin_table_get_ex_data(maat_inst, table_id, items, 3, ret = maat_bool_plugin_table_get_ex_data(maat_inst, table_id, items, 3,
(void **)results, ARRAY_SIZE); (void **)results, ARRAY_SIZE);
EXPECT_EQ(ret, 4); EXPECT_EQ(ret, 4);
EXPECT_EQ(results[0]->name_len, 8);
for (i = 3; i < TEST_CMD_LINE_NUM; i++) { for (i = 3; i < TEST_CMD_LINE_NUM; i++) {
memset(&line_rule, 0, sizeof(line_rule)); memset(&line_rule, 0, sizeof(line_rule));
@@ -5211,10 +5232,13 @@ TEST_F(MaatCmdTest, UpdateBoolPlugin) {
} }
sleep(WAIT_FOR_EFFECTIVE_S); sleep(WAIT_FOR_EFFECTIVE_S);
memset(results, 0, sizeof(results));
ret = maat_bool_plugin_table_get_ex_data(maat_inst, table_id, items, 3, ret = maat_bool_plugin_table_get_ex_data(maat_inst, table_id, items, 3,
(void **)results, ARRAY_SIZE); (void **)results, ARRAY_SIZE);
EXPECT_EQ(ret, 2); EXPECT_EQ(ret, 2);
EXPECT_EQ(results[0]->name_len, 8);
sleep(WAIT_FOR_EFFECTIVE_S * 2);
//excced gc_timeout_s, the data pointed by results[idx] has been freed
} }
#define COMPILE_ID_NUMS 1000 #define COMPILE_ID_NUMS 1000