
From: Longfang Liu <liulongfang@huawei.com> In UADK's algorithm registration framework, the specific algorithm driver name should be decoupled from the framework to prevent compatibility issues. Additionally, the filtering rules when the framework opens the driver SO file need to be updated to prevent errors caused by extremely short file names. Signed-off-by: Longfang Liu <liulongfang@huawei.com> Signed-off-by: Qi Tao <taoqi10@huawei.com> --- drv/isa_ce_sm3.c | 1 + wd_alg.c | 17 ++++++++------ wd_util.c | 58 ++++++++++++++++++++++++------------------------ 3 files changed, 40 insertions(+), 36 deletions(-) diff --git a/drv/isa_ce_sm3.c b/drv/isa_ce_sm3.c index 84ae2854..0e88b614 100644 --- a/drv/isa_ce_sm3.c +++ b/drv/isa_ce_sm3.c @@ -57,6 +57,7 @@ static void __attribute__((constructor)) sm3_ce_probe(void) static void __attribute__((destructor)) sm3_ce_remove(void) { + WD_INFO("Info: unregister SM3 CE alg drivers!\n"); wd_alg_driver_unregister(&sm3_ce_alg_driver); } diff --git a/wd_alg.c b/wd_alg.c index a33f5d66..a2999e36 100644 --- a/wd_alg.c +++ b/wd_alg.c @@ -91,7 +91,7 @@ static bool wd_check_accel_dev(const char *dev_name) return false; } -static bool wd_check_ce_support(const char *dev_name) +static bool wd_check_ce_support(const char *alg_name) { unsigned long hwcaps = 0; @@ -100,10 +100,10 @@ static bool wd_check_ce_support(const char *dev_name) #elif defined(__aarch64__) hwcaps = getauxval(AT_HWCAP); #endif - if (!strcmp("isa_ce_sm3", dev_name) && (hwcaps & HWCAP_CE_SM3)) + if (!strcmp("sm3", alg_name) && (hwcaps & HWCAP_CE_SM3)) return true; - if (!strcmp("isa_ce_sm4", dev_name) && (hwcaps & HWCAP_CE_SM4)) + if (!strcmp("sm4", alg_name) && (hwcaps & HWCAP_CE_SM4)) return true; return false; @@ -122,7 +122,8 @@ static bool wd_check_sve_support(void) return false; } -static bool wd_alg_check_available(int calc_type, const char *dev_name) +static bool wd_alg_check_available(int calc_type, + const char *alg_name, const char *dev_name) { bool ret = false; @@ -131,7 +132,7 @@ static bool wd_alg_check_available(int calc_type, const char *dev_name) break; /* Should find the CPU if not support CE */ case UADK_ALG_CE_INSTR: - ret = wd_check_ce_support(dev_name); + ret = wd_check_ce_support(alg_name); break; /* Should find the CPU if not support SVE */ case UADK_ALG_SVE_INSTR: @@ -217,7 +218,8 @@ int wd_alg_driver_register(struct wd_alg_driver *drv) new_alg->refcnt = 0; new_alg->next = NULL; - new_alg->available = wd_alg_check_available(drv->calc_type, drv->drv_name); + new_alg->available = wd_alg_check_available(drv->calc_type, + drv->alg_name, drv->drv_name); if (!new_alg->available) { free(new_alg); return -WD_ENODEV; @@ -307,7 +309,8 @@ void wd_enable_drv(struct wd_alg_driver *drv) } if (pnext) - pnext->available = wd_alg_check_available(drv->calc_type, drv->drv_name); + pnext->available = wd_alg_check_available(drv->calc_type, + drv->alg_name, drv->drv_name); pthread_mutex_unlock(&mutex); } diff --git a/wd_util.c b/wd_util.c index af4d949b..01536f4c 100644 --- a/wd_util.c +++ b/wd_util.c @@ -1901,35 +1901,6 @@ static void wd_get_alg_type(const char *alg_name, char *alg_type) } } -/** - * There are many other .so files in this file directory (/root/lib/), - * and it is necessary to screen out valid uadk driver files - * through this function. - */ -static int file_check_valid(char *lib_file) -{ -#define FILE_HEAD_SZ 4 -#define FILE_TAIL_SZ 4 - int file_len = strlen(lib_file); - char file_head[FILE_HEAD_SZ] = "lib"; - char file_tail[FILE_TAIL_SZ] = ".so"; - int i, j; - - /* Lib file name is libxx_xxx.so */ - for (i = 0; i < FILE_HEAD_SZ - 1; i++) { - if (lib_file[i] != file_head[i]) - return -EINVAL; - } - - for (i = file_len - (FILE_TAIL_SZ - 1), j = 0; - i < file_len && j < FILE_TAIL_SZ; i++, j++) { - if (lib_file[i] != file_tail[j]) - return -EINVAL; - } - - return 0; -} - static int wd_alg_init_fallback(struct wd_alg_driver *fb_driver) { if (!fb_driver->init) { @@ -2222,6 +2193,34 @@ int wd_get_lib_file_path(const char *lib_file, char *lib_path, bool is_dir) return 0; } +/** + * There are many other .so files in this file directory (/root/lib/), + * and it is necessary to screen out valid uadk driver files + * through this function. + */ +static int file_check_valid(const char *lib_file) +{ +#define MIN_FILE_LEN 6 +#define FILE_TAIL_LEN 3 + const char *dot = strrchr(lib_file, '.'); + size_t len; + + /* Check if the filename length is sufficient. */ + len = strlen(lib_file); + if (len < MIN_FILE_LEN) + return -EINVAL; + + /* Check if it starts with "lib". */ + if (strncmp(lib_file, "lib", FILE_TAIL_LEN) != 0) + return -EINVAL; + + /* Check if it ends with ".so". */ + if (!dot || strcmp(dot, ".so") != 0) + return -EINVAL; + + return 0; +} + void *wd_dlopen_drv(const char *cust_lib_dir) { typedef int (*alg_ops)(struct wd_alg_driver *drv); @@ -2243,6 +2242,7 @@ void *wd_dlopen_drv(const char *cust_lib_dir) return NULL; } strncpy(lib_dir_path, cust_lib_dir, PATH_MAX - 1); + lib_dir_path[PATH_MAX - 1] = '\0'; } wd_dir = opendir(lib_dir_path); -- 2.33.0