diff --git a/inc/bool_matcher.h b/inc/bool_matcher.h index 195d78c..33457de 100644 --- a/inc/bool_matcher.h +++ b/inc/bool_matcher.h @@ -7,7 +7,7 @@ * All rights reserved * * Written by: LIU YANBING (liuyanbing@iie.ac.cn) - * Last modification: 2018-12-31 + * Last modification: 2021-01-01 * * This code is the exclusive and proprietary property of IIE-CAS and NELIST. * Usage for direct or indirect commercial advantage is not allowed without @@ -25,27 +25,35 @@ extern "C" #endif #define MAX_ITEMS_PER_BOOL_EXPR 8 - /*not_flag=0表示布尔项item_id必须出现;not_flag=1表示布尔项item_id不能出现*/ + /* not_flag=0表示布尔项item_id必须出现;not_flag=1表示布尔项item_id不能出现 */ struct bool_item { unsigned long long item_id; unsigned char not_flag; }; - /*注意:不支持布尔项全“非”的情形*/ + /* At least one item's not_flag should be 0. */ struct bool_expr { + unsigned long long expr_id; void * user_tag; size_t item_num; struct bool_item items[MAX_ITEMS_PER_BOOL_EXPR]; }; + struct bool_expr_match + { + unsigned long long expr_id; + void * user_tag; + }; + struct bool_matcher; - /*注意:本函数调用会交换bool_exprs中元素的位置*/ + /* The elements in exprs array will be swapped to different indexes. */ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num, unsigned int max_thread_num, size_t * mem_size); - int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, void ** result, size_t size); + /* Returned results are sorted by expr_id in descending order. */ + int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, struct bool_expr_match * results, size_t n_result); void bool_matcher_free(struct bool_matcher * matcher); diff --git a/src/entry/Maat_hierarchy.cpp b/src/entry/Maat_hierarchy.cpp index 5c1b953..ae40428 100644 --- a/src/entry/Maat_hierarchy.cpp +++ b/src/entry/Maat_hierarchy.cpp @@ -895,6 +895,7 @@ static struct bool_matcher* Maat_hierarchy_build_bool_matcher(struct Maat_hierar //some compile may have zero groups, e.g. default policy. if(j==(size_t)compile->declared_clause_num&&j>0) { + bool_expr_array[expr_cnt].expr_id=compile->compile_id; bool_expr_array[expr_cnt].user_tag=compile; bool_expr_array[expr_cnt].item_num=j; expr_cnt++; @@ -1338,6 +1339,7 @@ int Maat_hierarchy_region_compile(struct Maat_hierarchy_compile_mid* mid, int is int bool_match_ret=0, i=0; struct Maat_hierarchy* hier=mid->ref_hier; struct Maat_hierarchy_compile* compile_array[ud_array_sz]; + struct bool_expr_match expr_match[ud_array_sz]; size_t r_in_c_cnt=0, this_scan_region_hits=mid->this_scan_region_hit_cnt; size_t ud_result_cnt=0; @@ -1349,9 +1351,11 @@ int Maat_hierarchy_region_compile(struct Maat_hierarchy_compile_mid* mid, int is pthread_rwlock_rdlock(&hier->rwlock); bool_match_ret=bool_matcher_match(hier->bm, mid->thread_num, (unsigned long long*)utarray_eltptr(mid->_all_hit_clause_array, 0), utarray_len(mid->_all_hit_clause_array), - (void**)compile_array, ud_array_sz); + expr_match, ud_array_sz); for(i=0; icompile_id==expr_match[i].expr_id); r_in_c_cnt=Maat_hierarchy_compile_mid_update_by_compile(mid, compile_array[i]); if(compile_array[i]->not_clause_cnt>0 && !is_last_compile) { diff --git a/src/entry/bool_matcher.cpp b/src/entry/bool_matcher.cpp index 01ceaa1..c391d5d 100644 --- a/src/entry/bool_matcher.cpp +++ b/src/entry/bool_matcher.cpp @@ -12,7 +12,7 @@ struct thread_local_data_t { unsigned int mapped_ids[MAX_ARRAY_SIZE]; unsigned int used_cells[MAX_ARRAY_SIZE]; - void * cached_results[MAX_ARRAY_SIZE]; + bool_expr_match cached_results[MAX_ARRAY_SIZE]; unsigned char * multiexpr_bitmap; unsigned int * singlexpr_bitmap; }; @@ -22,7 +22,7 @@ struct bool_matcher unsigned int max_thread_num; unsigned int bool_expr_num; unsigned int multi_expr_num; - void ** bool_expr_ids; + bool_expr_match * bool_expr_ids; unsigned char * multi_expr_size; unsigned char * multi_expr_mask; unsigned int bool_item_id_num; @@ -78,8 +78,8 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num matcher->bool_expr_num=(unsigned int)expr_num; matcher->multi_expr_num=I; - matcher->bool_expr_ids=new void *[expr_num]; - mem_bytes+=(unsigned int)expr_num*sizeof(void *); + matcher->bool_expr_ids=new bool_expr_match[expr_num]; + mem_bytes+=(unsigned int)expr_num*sizeof(bool_expr_match); matcher->multi_expr_size=new unsigned char[matcher->multi_expr_num+1]; mem_bytes+=(matcher->multi_expr_num+1)*sizeof(unsigned char); @@ -105,7 +105,8 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num unsigned int count=0; for(unsigned int i=0; ibool_expr_ids[i]=exprs[i].user_tag; + matcher->bool_expr_ids[i].expr_id=exprs[i].expr_id; + matcher->bool_expr_ids[i].user_tag=exprs[i].user_tag; if(imulti_expr_num) { matcher->multi_expr_size[i]=(unsigned int)exprs[i].item_num; @@ -160,7 +161,14 @@ struct bool_matcher * bool_matcher_new(struct bool_expr * exprs, size_t expr_num return matcher; } -int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, void ** result, size_t size) +int res_comp(const void * lhs, const void * rhs) +{ + bool_expr_match * _lhs=(bool_expr_match *)lhs; + bool_expr_match * _rhs=(bool_expr_match *)rhs; + return (_lhs->expr_id<_rhs->expr_id) ? 1 : -1; +} + +int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, const unsigned long long * item_ids, size_t item_num, struct bool_expr_match * results, size_t n_result) { if(matcher==NULL) return -1; if(thread_id>=matcher->max_thread_num) return -1; @@ -238,7 +246,7 @@ int bool_matcher_match(struct bool_matcher * matcher, unsigned int thread_id, co } unsigned int r=0; - void ** cached_results=matcher->thread_data[thread_id].cached_results; + bool_expr_match * cached_results=matcher->thread_data[thread_id].cached_results; for(unsigned int i=0; i