diff --git a/src/maat_api.c b/src/maat_api.c index 33388a9..c565eb1 100644 --- a/src/maat_api.c +++ b/src/maat_api.c @@ -1789,20 +1789,20 @@ size_t maat_get_hit_paths(struct maat *maat_instance, struct maat_state *state, compile_table_cnt = state->n_compile_table; } - size_t hit_path_index = 0; - //size_t sum_hit_paths = 0; + size_t sum_hit_path = 0; + for (size_t i = 0; i < compile_table_cnt; i++) { void *compile_rt = table_manager_get_runtime(maat_instance->tbl_mgr, compile_table_ids[i]); void *g2g_runtime = table_manager_get_runtime(maat_instance->tbl_mgr, maat_instance->g2g_table_id); assert(NULL != compile_rt && NULL != g2g_runtime); size_t n_hit_path = compile_runtime_get_hit_paths((struct compile_runtime *)compile_rt, (struct group2group_runtime *)g2g_runtime, - state->compile_state, paths, hit_path_index, + state->compile_state, paths, sum_hit_path, n_path); - hit_path_index += n_hit_path; + sum_hit_path += n_hit_path; } - return hit_path_index; + return sum_hit_path; } size_t maat_get_hit_objects(struct maat_compile_state *compile_state, diff --git a/src/maat_compile.c b/src/maat_compile.c index 1dd01ad..a14d640 100644 --- a/src/maat_compile.c +++ b/src/maat_compile.c @@ -137,6 +137,7 @@ struct maat_internal_hit_path { struct maat_compile_state { int thread_id; int Nth_scan; + int Nth_get_hit_path; time_t hier_ver; size_t this_scan_hit_item_cnt; int not_clause_hitted_flag; @@ -1277,41 +1278,44 @@ size_t compile_runtime_get_hit_paths(struct compile_runtime *compile_rt, size_t hit_path_cnt = hit_path_index; size_t new_hit_path_cnt = 0; - for (i = 0; i < utarray_len(compile_state->internal_hit_paths); i++) { - internal_path = (struct maat_internal_hit_path *)utarray_eltptr(compile_state->internal_hit_paths, i); - /* - NOTE: maybe one item has been deleted, but it's item_id still exist in internal_hit_paths - */ - long long top_group_ids[MAX_SCANNER_HIT_GROUP_NUM]; - memset(top_group_ids, 0, sizeof(top_group_ids)); - int top_group_cnt = group2group_runtime_get_top_groups(g2g_rt, &(internal_path->group_id), - 1, top_group_ids); - if (top_group_cnt <= 0) { - /* - item->group_id has no top group, this group can only be referenced by compile - ------------------------------------------------------------------------------ - for example: - compile1 -> group1 -> group2 -> item1 - group3 -> item2 - - group1 and group3 has no top group - group1 is referenced by compile1, group3 is not referenced by any compile - - NOTE: Add the hit path as long as the item is hit - + size_t tmp_path_cnt = utarray_len(compile_state->internal_hit_paths); + if (0 == compile_state->Nth_get_hit_path) { + for (i = 0; i < utarray_len(compile_state->internal_hit_paths); i++) { + internal_path = (struct maat_internal_hit_path *)utarray_eltptr(compile_state->internal_hit_paths, i); + /* + NOTE: maybe one item has been deleted, but it's item_id still exist in internal_hit_paths */ - top_group_cnt = 1; //add one hit path which top_group_ids[0] = -1 - } + long long top_group_ids[MAX_SCANNER_HIT_GROUP_NUM]; + memset(top_group_ids, 0, sizeof(top_group_ids)); + int top_group_cnt = group2group_runtime_get_top_groups(g2g_rt, &(internal_path->group_id), + 1, top_group_ids); + if (top_group_cnt <= 0) { + /* + item->group_id has no top group, this group can only be referenced by compile + ------------------------------------------------------------------------------ + for example: + compile1 -> group1 -> group2 -> item1 + group3 -> item2 - for (int j = 0; j < top_group_cnt && hit_path_cnt < hit_path_size; j++, hit_path_cnt++) { - hit_paths[hit_path_cnt].Nth_scan = internal_path->Nth_scan; - hit_paths[hit_path_cnt].item_id = internal_path->item_id; - hit_paths[hit_path_cnt].sub_group_id = internal_path->group_id; - hit_paths[hit_path_cnt].top_group_id = top_group_ids[j]; //top_group_id may be -1 - hit_paths[hit_path_cnt].vtable_id = internal_path->vtable_id; - hit_paths[hit_path_cnt].compile_id = -1; + group1 and group3 has no top group + group1 is referenced by compile1, group3 is not referenced by any compile + + NOTE: Add the hit path as long as the item is hit + + */ + top_group_cnt = 1; // add one hit path which top_group_ids[0] = -1 + } + + for (int j = 0; j < top_group_cnt && hit_path_cnt < hit_path_size; j++, hit_path_cnt++) { + hit_paths[hit_path_cnt].Nth_scan = internal_path->Nth_scan; + hit_paths[hit_path_cnt].item_id = internal_path->item_id; + hit_paths[hit_path_cnt].sub_group_id = internal_path->group_id; + hit_paths[hit_path_cnt].top_group_id = top_group_ids[j]; // top_group_id may be -1 + hit_paths[hit_path_cnt].vtable_id = internal_path->vtable_id; + hit_paths[hit_path_cnt].compile_id = -1; + } } - } + } /* assign hit_paths[].compile_id */ struct maat_compile *compile = NULL; @@ -1353,7 +1357,14 @@ size_t compile_runtime_get_hit_paths(struct compile_runtime *compile_rt, } } - return hit_path_cnt + new_hit_path_cnt; + if (0 == compile_state->Nth_get_hit_path) { + hit_path_cnt += new_hit_path_cnt; + } else { + hit_path_cnt = new_hit_path_cnt; + } + compile_state->Nth_get_hit_path++; + + return hit_path_cnt; } void maat_compile_state_update_hit_path(struct maat_compile_state *compile_state, diff --git a/src/maat_table.c b/src/maat_table.c index 7c79f10..c57295d 100644 --- a/src/maat_table.c +++ b/src/maat_table.c @@ -617,7 +617,7 @@ void table_manager_runtime_destroy(struct table_manager *tbl_mgr) if (NULL == runtime) { continue; } - + enum table_type table_type = table_manager_get_table_type(tbl_mgr, i); maat_table_runtime_free(runtime, table_type); tbl_mgr->tbl[i]->runtime = NULL; diff --git a/test/maat_framework_gtest.cpp b/test/maat_framework_gtest.cpp index e42f02a..ea3f7f4 100644 --- a/test/maat_framework_gtest.cpp +++ b/test/maat_framework_gtest.cpp @@ -1445,6 +1445,11 @@ TEST_F(CompileTable, Conjunction) { EXPECT_EQ(n_hit_result, 2); EXPECT_EQ(results[0], 197); EXPECT_EQ(results[1], 141); + + struct maat_hit_path hit_path[HIT_PATH_SIZE] = {0}; + int n_read = maat_state_get_hit_paths(g_maat_instance, &state, hit_path, HIT_PATH_SIZE); + EXPECT_EQ(n_read, 2); + maat_state_free(&state); } @@ -1665,35 +1670,64 @@ protected: } }; #if 0 +TEST_F(MaatCmdTest, HitPath) { + const char *g2g_table_name = "GROUP2GROUP"; + const char *g2c_table_name = "GROUP2COMPILE"; + const char *compile_table_name = "COMPILE"; + const char *http_sig_table_name = "HTTP_SIGNATURE"; + const char *ip_table_name = "IP_CONFIG"; + const char *keywords_table_name = "KEYWORDS_TABLE"; + char table_line_add[128] = {0}; + struct maat_cmd_line line_rule; + + /* compile table add line */ + long long compile_id = maat_cmd_incrby(g_maat_instance, "TEST_SEQ", 1); + sprintf(table_line_add, "%lld\t0\t0\t0\t0\t0\tanything\t1\t1\t0.0", compile_id); + line_rule.rule_id = compile_id; + line_rule.table_line = table_line_add; + line_rule.table_name = compile_table_name; + int ret = maat_cmd_set_line(g_maat_instance, &line_rule); + EXPECT_EQ(ret, 1); + + /* group2compile table add line */ + memset(&line_rule, 0, sizeof(line_rule)); + memset(table_line_add, 0, sizeof(table_line_add)); + + long long group_id = maat_cmd_incrby(g_maat_instance, "SEQUENCE_GROUP", 1); + sprintf(table_line_add, "%lld\t%lld\t1\t0\tnull\t1", group_id, compile_id); + line_rule.rule_id = group_id; + line_rule.table_line = table_line_add; + line_rule.table_name = g2c_table_name; + ret = maat_cmd_set_line(g_maat_instance, &line_rule); + EXPECT_EQ(ret, 1); + + /* item table add line */ + memset(&line_rule, 0, sizeof(line_rule)); + memset(table_line_add, 0, sizeof(table_line_add)); + + long long item_id = maat_cmd_incrby(g_maat_instance, "SEQUENCE_REGION", 1); + sprintf(table_line_add, "%lld\t%lld\t%s\t%s\t", item_id, group_id, "URL", "graph_theory"); +} +#endif +#if 0 TEST_F(MaatCmdTest, SetIP) { long long version_before = 0; const char *ip_table_name = "IP_CONFIG"; const char *compile_table_name = "COMPILE"; - char table_line[128] = {0}; + char compile_table_line_add[128] = {0}; long long compile_id = 0; compile_id = maat_cmd_incrby(g_maat_instance, "TEST_SEQ", 1); - sprintf(table_line, "%lld\t") + sprintf(compile_table_line_add, "%lld\t0\t0\t0\t0\t0\tanything\t1\t1\t0.0", compile_id); struct maat_cmd_line line_rule; + line_rule.rule_id = compile_id; + line_rule.table_line = compile_table_line_add; line_rule.table_name = compile_table_name; + int ret = maat_cmd_set_line(g_maat_instance, &line_rule); + EXPECT_EQ(ret, 1); - - strcpy(compile.service_defined, "maat_command"); - struct Maat_command_batch* batch=NULL; - batch=Maat_command_batch_new(feather); - //MUST acqire by function, because Maat_cmd_t has some hidden members. - Maat_command_batch_set_compile(batch, MAAT_OP_ADD, &compile, "COMPILE", NULL, 1, 0, timeout); - - struct Maat_cmd_group2compile g2c; - memset(&g2c, 0, sizeof(g2c)); - g2c.group_id=Maat_command_get_new_group_id(feather); - g2c.compile_id=compile.config_id; - g2c.clause_index=1; - g2c.table_name="GROUP2COMPILE"; - Maat_command_batch_set_group2compile(batch, MAAT_OP_ADD, &g2c); - - - + long long group_id = maat_cmd_incrby(g_maat_instance, "SEQUENCE_GROUP", 1); + memset(&line_rule, 0, sizeof(line_rule)); struct Maat_cmd_region region;