-
Notifications
You must be signed in to change notification settings - Fork 109
[Bugfix] fix builtin algo lookup in plugin via unifed register #317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,94 @@ | ||||||||||||
| #include <strings.h> | ||||||||||||
|
|
||||||||||||
| #include "libCacheSim/evictionAlgo.h" | ||||||||||||
|
|
||||||||||||
| // Registry for built-in eviction algorithm init functions. | ||||||||||||
|
|
||||||||||||
| #ifdef __cplusplus | ||||||||||||
| extern "C" { | ||||||||||||
| #endif | ||||||||||||
|
|
||||||||||||
| typedef struct { | ||||||||||||
| const char *name; | ||||||||||||
| cache_init_func_ptr init_func; | ||||||||||||
| } eviction_algo_entry_t; | ||||||||||||
|
|
||||||||||||
| cache_init_func_ptr get_builtin_cache_init(const char *const cache_alg_name) { | ||||||||||||
| static const eviction_algo_entry_t builtin_caches[] = { | ||||||||||||
| {"2q", TwoQ_init}, | ||||||||||||
| {"arc", ARC_init}, | ||||||||||||
| {"arcv0", ARCv0_init}, | ||||||||||||
| {"belady", Belady_init}, | ||||||||||||
| {"beladySize", BeladySize_init}, | ||||||||||||
| {"CAR", CAR_init}, | ||||||||||||
| {"cacheus", Cacheus_init}, | ||||||||||||
| {"clock", Clock_init}, | ||||||||||||
| {"clock2qplus", Clock2QPlus_init}, | ||||||||||||
| {"clockpro", ClockPro_init}, | ||||||||||||
| {"cr-lfu", CR_LFU_init}, | ||||||||||||
| {"cr_lfu", CR_LFU_init}, | ||||||||||||
| {"fifo", FIFO_init}, | ||||||||||||
| {"fifo-merge", FIFO_Merge_init}, | ||||||||||||
| {"fifo-reinsertion", Clock_init}, | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Map Line 32 maps Suggested fix- {"fifo-reinsertion", Clock_init},
+ {"fifo-reinsertion", FIFO_Reinsertion_init},📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||
| {"fifomerge", FIFO_Merge_init}, | ||||||||||||
| {"flashProb", flashProb_init}, | ||||||||||||
| {"gdsf", GDSF_init}, | ||||||||||||
| {"hyperbolic", Hyperbolic_init}, | ||||||||||||
| {"lhd", LHD_init}, | ||||||||||||
| {"lecar", LeCaR_init}, | ||||||||||||
| {"lecarv0", LeCaRv0_init}, | ||||||||||||
| {"lfu", LFU_init}, | ||||||||||||
| {"lfucpp", LFUCpp_init}, | ||||||||||||
| {"lfuda", LFUDA_init}, | ||||||||||||
| {"lirs", LIRS_init}, | ||||||||||||
| {"lru", LRU_init}, | ||||||||||||
| {"lru-k", LRU_K_init}, | ||||||||||||
| {"lru-prob", LRU_Prob_init}, | ||||||||||||
| {"lru_prob", LRU_Prob_init}, | ||||||||||||
| {"lruv0", LRUv0_init}, | ||||||||||||
| {"mru", MRU_init}, | ||||||||||||
| {"nop", nop_init}, | ||||||||||||
| {"pluginCache", pluginCache_init}, | ||||||||||||
| {"qdlp", QDLP_init}, | ||||||||||||
| {"random", Random_init}, | ||||||||||||
| {"RandomLRU", RandomLRU_init}, | ||||||||||||
| {"randomTwo", RandomTwo_init}, | ||||||||||||
| {"s3-fifo", S3FIFO_init}, | ||||||||||||
| {"s3fifo", S3FIFO_init}, | ||||||||||||
| {"s3fifod", S3FIFOd_init}, | ||||||||||||
| {"s3-fifov0", S3FIFOv0_init}, | ||||||||||||
| {"s3fifov0", S3FIFOv0_init}, | ||||||||||||
| {"s3lru", S3LRU_init}, | ||||||||||||
| {"sieve", Sieve_init}, | ||||||||||||
| {"size", Size_init}, | ||||||||||||
| {"slru", SLRU_init}, | ||||||||||||
| {"slruv0", SLRUv0_init}, | ||||||||||||
| {"sr-lru", SR_LRU_init}, | ||||||||||||
| {"sr_lru", SR_LRU_init}, | ||||||||||||
| {"twoq", TwoQ_init}, | ||||||||||||
| {"wtinyLFU", WTinyLFU_init}, | ||||||||||||
|
Comment on lines
+68
to
+69
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add
Suggested change
|
||||||||||||
| #ifdef ENABLE_3L_CACHE | ||||||||||||
| {"3LCache", ThreeLCache_init}, | ||||||||||||
| #endif | ||||||||||||
| #ifdef ENABLE_GLCACHE | ||||||||||||
| {"GLCache", GLCache_init}, | ||||||||||||
| {"gl-cache", GLCache_init}, | ||||||||||||
| #endif | ||||||||||||
| #ifdef ENABLE_LRB | ||||||||||||
| {"lrb", LRB_init}, | ||||||||||||
| #endif | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| for (size_t i = 0; i < sizeof(builtin_caches) / sizeof(builtin_caches[0]); | ||||||||||||
| i++) { | ||||||||||||
| if (strcasecmp(cache_alg_name, builtin_caches[i].name) == 0) { | ||||||||||||
| return builtin_caches[i].init_func; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return NULL; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| #ifdef __cplusplus | ||||||||||||
| } | ||||||||||||
| #endif | ||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,11 +59,16 @@ cache_t *create_cache_external(const char *const cache_alg_name, | |
| cache_t *create_cache_internal(const char *const cache_alg_name, | ||
| common_cache_params_t cc_params, | ||
| void *cache_specific_params) { | ||
| cache_t *(*cache_init)(common_cache_params_t, void *) = NULL; | ||
| cache_init_func_ptr cache_init = get_builtin_cache_init(cache_alg_name); | ||
|
Comment on lines
59
to
+62
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a defensive check to ensure cache_t *create_cache_internal(const char *const cache_alg_name,
common_cache_params_t cc_params,
void *cache_specific_params) {
if (cache_alg_name == NULL) {
return NULL;
}
cache_init_func_ptr cache_init = get_builtin_cache_init(cache_alg_name); |
||
| if (cache_init != NULL) { | ||
| INFO("internal cache %s loaded\n", cache_alg_name); | ||
| return cache_init(cc_params, (const char *)cache_specific_params); | ||
| } | ||
|
|
||
| char *err = NULL; | ||
|
|
||
| char cache_init_func_name[256]; | ||
| void *handle = dlopen(NULL, RTLD_GLOBAL); | ||
| void *handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL); | ||
| /* should not check err here, otherwise ubuntu will report err even though | ||
| * everything is OK */ | ||
|
Comment on lines
+71
to
73
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a defensive check to ensure void *handle = dlopen(NULL, RTLD_LAZY | RTLD_GLOBAL);
if (handle == NULL) {
return NULL;
}
/* should not check err here, otherwise ubuntu will report err even though
* everything is OK */ |
||
|
|
||
|
|
@@ -72,21 +77,22 @@ cache_t *create_cache_internal(const char *const cache_alg_name, | |
| // ISO C compliant way to convert void* to function pointer | ||
| union { | ||
| void *obj_ptr; | ||
| cache_t *(*func_ptr)(common_cache_params_t, void *); | ||
| cache_init_func_ptr func_ptr; | ||
| } dlsym_ptr; | ||
|
|
||
| dlerror(); | ||
| dlsym_ptr.obj_ptr = dlsym(handle, cache_init_func_name); | ||
| cache_init = dlsym_ptr.func_ptr; | ||
|
|
||
| err = dlerror(); | ||
|
|
||
| if (cache_init == NULL) { | ||
| WARN("cannot load internal cache %s: error %s\n", cache_alg_name, err); | ||
| abort(); | ||
| return NULL; | ||
| } | ||
|
Comment on lines
89
to
92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle the case where if (cache_init == NULL) {
WARN("cannot load internal cache %s: error %s\n", cache_alg_name, err ? err : "symbol not found");
return NULL;
} |
||
|
|
||
| INFO("internal cache %s loaded\n", cache_alg_name); | ||
| cache_t *cache = cache_init(cc_params, cache_specific_params); | ||
| cache_t *cache = cache_init(cc_params, (const char *)cache_specific_params); | ||
| return cache; | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a defensive check to ensure
cache_alg_nameis notNULLbefore callingstrcasecmp. Ifcache_alg_nameisNULL,strcasecmpwill dereference a null pointer and cause a segmentation fault.