Replace global variable and first task reference for cloning with tss_clone_list (#63)

Task Id of first task in vxtss symbol list is updated with 0 after it is terminated.

Co-authored-by: subhamgdeb <subhamgdeb>
Reviewed-on: http://13.234.246.121:3000/Neominds/ossl35vx69/pulls/63
Co-authored-by: Subham Gangdeb <subham.gangdeb@neominds.in>
Co-committed-by: Subham Gangdeb <subham.gangdeb@neominds.in>
This commit is contained in:
2026-07-30 20:45:01 +05:30
committed by neelimap
co-authored by subhamgdeb
parent bd330e941f
commit d6e76bcf3d
4 changed files with 245 additions and 187 deletions
+1 -5
View File
@@ -38,15 +38,11 @@ int tss_symlist_add_new(TASK_ID taskid,
tss_t *tss_key,
tss_sym_state state);
/* Get the first task ID with matching run_once_arg_string */
TASK_ID
tss_symlist_get_first_tid(char *run_once_arg_string);
/*
* Check and populate keys for current task based on run_once_arg_str
*/
void
tss_symlist_clone_entries_for_new_task(TASK_ID tid, char *run_once_arg_string);
tss_symlist_clone_entries_for_new_task(char *run_once_arg_string);
/* Display the tss symbol table */
int
@@ -378,15 +378,7 @@ int ossl_init_thread_start(const void *index, void *arg,
THREAD_EVENT_HANDLER **hands;
THREAD_EVENT_HANDLER *hand;
TASK_ID tid = tss_symlist_get_first_tid("ossl_init_base");
if (tid != ERROR) {
tss_symlist_clone_entries_for_new_task(tid,"ossl_init_base");
}
else
{
printf ("ERROR: ossl_init_thread_start(): tss_symlist_get_first_tid failed for ossl_init_base\n");
}
tss_symlist_clone_entries_for_new_task("ossl_init_base");
#ifdef FIPS_MODULE
OSSL_LIB_CTX *ctx = arg;
+1 -5
View File
@@ -49,11 +49,7 @@ crypto_once(CRYPTO_ONCE *once_control, void (*init_routine)(void))
}
/* Per Task initialization */
TASK_ID tid = tss_symlist_get_first_tid(once_control->run_once_arg);
if (tid != ERROR) {
tss_symlist_clone_entries_for_new_task(tid, once_control->run_once_arg);
}
tss_symlist_clone_entries_for_new_task(once_control->run_once_arg);
semGive(once_control->sem);
return OK;
+242 -168
View File
@@ -10,22 +10,30 @@
#include <vxtss_symlist.h>
#define MAX_TSS_SYM_ENTRY 256
#define MAX_CLONE_ENTRIES 32
/* Symbol entry info structure */
typedef struct {
char run_once_arg_str[MAX_STR_LEN];
char tss_key_name[MAX_STR_LEN];
tss_t *tss_key;
} tss_sym_info;
/* Sym table entry */
typedef struct {
long taskid;
char run_once_arg_str[MAX_STR_LEN];
char tss_key_name[MAX_STR_LEN];
tss_t *tss_key;
long taskid;
tss_sym_info info;
tss_sym_state state;
BOOL cloned;
} tss_sym_entry;
/* Globals */
static CRYPTO_RWLOCK *tss_sym_table_lock = NULL;
static tss_sym_entry tss_sym_table[MAX_TSS_SYM_ENTRY];
static int g_tss_sym_initialized = 0;
static TASK_ID g_first_tid = -1;
static tss_sym_entry tss_sym_table[MAX_TSS_SYM_ENTRY];
static tss_sym_info tss_clone_list[MAX_CLONE_ENTRIES];
static int g_tss_sym_initialized = 0;
/* Helper functions */
static const char*
@@ -33,28 +41,57 @@ tss_symlist_get_entry_state_str(tss_sym_state state)
{
switch(state) {
case TSS_SYMLIST_STATE_INVALID: return "INVALID";
case TSS_SYMLIST_STATE_NEW: return "NEW";
case TSS_SYMLIST_STATE_ADDED: return "ADDED";
case TSS_SYMLIST_STATE_FAILED: return "FAILED";
default: return "UNKNOWN";
}
}
/* Display the clone list */
int
tss_clone_list_show(void)
{
int i;
/* Acquire read lock for safe iteration */
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
printf("\n=== TSS Clone List ===\n");
printf("%-32s %-32s %-16s\n",
"ONCE_ARGSTR", "TSSKEYNAME", "TSSKEYVAL");
printf("----------------------------------------------------------------------------\n");
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
if (tss_clone_list[i].tss_key == NULL)
continue;
printf("%-32s %-32s %-16p\n",
tss_clone_list[i].run_once_arg_str ?: "(null)",
tss_clone_list[i].tss_key_name ?: "(null)",
(void *)tss_clone_list[i].tss_key);
}
printf("=== End of Clone List ===\n\n");
CRYPTO_THREAD_unlock(tss_sym_table_lock);
return OK;
}
/* Display the symbol table */
int
tss_symlist_show(void)
{
int i;
if (!g_tss_sym_initialized) {
return ERROR;
}
printf("\n=== TSS Symbol Table ===\n");
printf("%-20s %-32s %-32s %-16s %-8s\n",
"TNAME", "ONCE_ARGSTR", "TSSKEYNAME", "TSSKEYVAL", "STATE");
printf("------------------------------------------------------------------------------------------------------------\n");
printf("%-32s %-32s %-32s %-16s %-8s %-8s\n",
"TINFO", "ONCE_ARGSTR", "TSSKEYNAME", "TSSKEYVAL", "STATE", "CLONE");
printf("-------------------------------------------------------------------------------------------------------------------------------------\n");
/* Acquire lock for reading */
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
@@ -62,21 +99,31 @@ tss_symlist_show(void)
if (tss_sym_table[i].state == TSS_SYMLIST_STATE_INVALID) {
continue; /* Skip invalid entries */
}
const char *tname = taskName((int)tss_sym_table[i].taskid);
if (!tname) tname = "(null)";
/* Print entry in the specified format */
printf("%-20s %-32s %-32s %-16p %-8s\n",
tname,
tss_sym_table[i].run_once_arg_str ?: "(null)",
tss_sym_table[i].tss_key_name ?: "(null)",
(void *)tss_sym_table[i].tss_key,
tss_symlist_get_entry_state_str(tss_sym_table[i].state));
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
printf("=== End of Table ===\n\n");
char tid_buf[64]; /* enough for name + hex TID */
const char *tname = taskName((int)tss_sym_table[i].taskid);
if (!tname) {
snprintf(tid_buf, sizeof(tid_buf), "(null)(0x%lx)",
(unsigned long)tss_sym_table[i].taskid);
} else {
snprintf(tid_buf, sizeof(tid_buf), "%s(0x%lx)",
tname, (unsigned long)tss_sym_table[i].taskid);
}
/* Print entry in the specified format */
printf("%-32s %-32s %-32s %-16p %-8s %-8s\n",
tid_buf,
tss_sym_table[i].info.run_once_arg_str ?: "(null)",
tss_sym_table[i].info.tss_key_name ?: "(null)",
(void *)tss_sym_table[i].info.tss_key,
tss_symlist_get_entry_state_str(tss_sym_table[i].state),
tss_sym_table[i].cloned ? "CLONED":"");
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
printf("=== End of Table ===\n\n");
return OK;
}
@@ -95,89 +142,96 @@ tss_symlist_find_free_slot(void)
/* Helper function to check if entry exists for a task */
static int
tss_symlist_find_entry_by_tid_and_arg(TASK_ID tid, char *run_once_arg_string)
tss_symlist_find_entry_by_tid(TASK_ID tid)
{
int i;
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
tss_sym_table[i].taskid == (long)tid &&
strncmp(tss_sym_table[i].run_once_arg_str, run_once_arg_string, MAX_STR_LEN) == 0) {
tss_sym_table[i].taskid == (long)tid) {
return i; /* Return index of matching entry */
}
}
return ERROR; /* No matching entry found */
}
/* Internal add function - assumes lock is already held */
/* Clone-list helper: find an entry in the clone list */
static int
tss_symlist_add_new_internal(TASK_ID taskId, char *run_once_arg_str, char *tss_key_name, tss_t *key, tss_sym_state state)
tss_clone_list_find(tss_t *key, const char *run_once_arg_str)
{
int i;
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
if (tss_clone_list[i].tss_key != NULL &&
tss_clone_list[i].tss_key == key &&
strncmp(tss_clone_list[i].run_once_arg_str,
run_once_arg_str, MAX_STR_LEN) == 0) {
return i;
}
}
return ERROR;
}
/* Clone-list helper: add entry if it doesn't already exist */
static int
tss_clone_list_add(const tss_sym_info *entry)
{
int idx;
/* Caller must hold write lock */
if (tss_clone_list_find(entry->tss_key, entry->run_once_arg_str) != ERROR)
return OK;
/* Find a free slot in clone list */
for (idx = 0; idx < MAX_CLONE_ENTRIES; idx++) {
if (tss_clone_list[idx].tss_key == NULL)
break;
}
if (idx == MAX_CLONE_ENTRIES)
return ERROR;
/* Copy over the template entry */
memcpy(&tss_clone_list[idx], entry, sizeof(tss_sym_info));
return OK;
}
/* Internal add function, assumes lock is already held */
static int
tss_symlist_add_new_internal(TASK_ID taskId,
char *run_once_arg_str,
char *tss_key_name,
tss_t *key,
tss_sym_state state,
int cloned)
{
int idx;
/* Find free slot */
idx = tss_symlist_find_free_slot();
if (idx == ERROR) {
return ERROR;
}
/* Add new entry */
tss_sym_table[idx].taskid = (long)taskId;
strncpy(tss_sym_table[idx].run_once_arg_str, run_once_arg_str, MAX_STR_LEN - 1);
tss_sym_table[idx].run_once_arg_str[MAX_STR_LEN - 1] = '\0';
strncpy(tss_sym_table[idx].tss_key_name, tss_key_name, MAX_STR_LEN - 1);
tss_sym_table[idx].tss_key_name[MAX_STR_LEN - 1] = '\0';
tss_sym_table[idx].tss_key = (tss_t*)key;
strncpy(tss_sym_table[idx].info.run_once_arg_str, run_once_arg_str, MAX_STR_LEN - 1);
tss_sym_table[idx].info.run_once_arg_str[MAX_STR_LEN - 1] = '\0';
strncpy(tss_sym_table[idx].info.tss_key_name, tss_key_name, MAX_STR_LEN - 1);
tss_sym_table[idx].info.tss_key_name[MAX_STR_LEN - 1] = '\0';
tss_sym_table[idx].info.tss_key = (tss_t*)key;
tss_sym_table[idx].state = state;
tss_sym_table[idx].cloned = cloned;
return OK;
}
/* taskHook cleanup */
void
tss_symlist_task_cleanup_hook(int taskId)
{
if (!CRYPTO_THREAD_write_lock(tss_sym_table_lock))
return;
int i;
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
if (tss_sym_table[i].state == TSS_SYMLIST_STATE_INVALID ||
tss_sym_table[i].taskid != taskId)
continue;
tss_t *thread_local_ptr = tss_sym_table[i].tss_key;
tss_entry_t *entry = (tss_entry_t *)(*thread_local_ptr);
if (entry) {
taskVarDelete(taskId, (void *)&entry->var);
if (entry->dtor)
entry->dtor(entry->var);
if (taskId != g_first_tid) {
free(entry);
*thread_local_ptr = (tss_t)0;
tss_sym_table[i].tss_key = NULL;
tss_sym_table[i].state = TSS_SYMLIST_STATE_INVALID;
} else {
/* First task: keep metadata */
*thread_local_ptr = (tss_t)0; /* clear slot for this task */
}
}
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
}
/* Initialize the TSS symbol table */
/* Initialize the TSS symbol table and clone list */
int
tss_sym_init(void)
{
int i;
if (g_tss_sym_initialized)
return OK;
@@ -186,90 +240,72 @@ tss_sym_init(void)
memset(&tss_sym_table[i], 0, sizeof(tss_sym_entry));
tss_sym_table[i].state = TSS_SYMLIST_STATE_INVALID;
}
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
memset(&tss_clone_list[i], 0, sizeof(tss_sym_info));
}
/* Create new lock */
tss_sym_table_lock = (CRYPTO_RWLOCK *)CRYPTO_THREAD_lock_new();
/* Attach cleanup routine to task delete hook chain */
taskDeleteHookAdd((FUNCPTR)tss_symlist_task_cleanup_hook);
g_tss_sym_initialized = 1;
return OK;
}
/* Add new entry to the symbol table */
/* Add new entry to the symbol table and (if appropriate) to the clone list */
int
tss_symlist_add_new(TASK_ID taskId, char *run_once_arg_str, char *tss_key_name, tss_t *key, tss_sym_state state)
{
int result;
if (!g_tss_sym_initialized) {
tss_sym_init();
}
if (key == NULL ||
tss_key_name == NULL || tss_key_name[0] == '\0' ||
run_once_arg_str == NULL || run_once_arg_str[0] == '\0')
return ERROR;
/* Acquire lock */
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
result = tss_symlist_add_new_internal(taskId, run_once_arg_str, tss_key_name, key, state);
int result = tss_symlist_add_new_internal(taskId, run_once_arg_str,
tss_key_name, key, TSS_SYMLIST_STATE_ADDED, FALSE);
/* Store this as the first tid if not already set */
if (g_first_tid == -1 && result == OK) {
g_first_tid = taskId;
/* Add this entry to the clone list if it isn't already added */
if (result == OK) {
tss_sym_info tmp;
memset(&tmp, 0, sizeof(tmp));
strncpy(tmp.run_once_arg_str, run_once_arg_str, MAX_STR_LEN - 1);
strncpy(tmp.tss_key_name, tss_key_name, MAX_STR_LEN - 1);
tmp.tss_key = key;
tss_clone_list_add(&tmp);
}
/* Release lock */
CRYPTO_THREAD_unlock(tss_sym_table_lock);
return result;
}
/* Get the first task ID with matching run_once_arg_string */
TASK_ID
tss_symlist_get_first_tid(char *run_once_arg_string)
{
int i;
int found = 0;
if (!g_tss_sym_initialized) {
tss_sym_init();
}
/* Acquire lock for reading */
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
/* Find the first entry with run_once_arg_str */
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
strncmp(tss_sym_table[i].run_once_arg_str, run_once_arg_string, MAX_STR_LEN) == 0) {
found = 1;
break;
}
}
if (!found) {
CRYPTO_THREAD_unlock(tss_sym_table_lock);
return (TASK_ID)ERROR;
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
return (TASK_ID)tss_sym_table[i].taskid;
}
/* Create entry and update the global key (thread_local) for the current task */
int
static int
tss_create_and_update_table(tss_t *key, char *tss_key_name, char *run_once_arg_str)
{
tss_entry_t *entry;
TASK_ID selfTaskId;
if (key == NULL ||
tss_key_name == NULL || tss_key_name[0] == '\0' ||
run_once_arg_str == NULL || run_once_arg_str[0] == '\0')
return ERROR;
entry = (tss_entry_t *)malloc(sizeof(tss_entry_t));
if (entry == NULL) {
return ERROR;
}
memset(entry, 0, sizeof(tss_entry_t));
selfTaskId = taskIdSelf();
if (taskVarAdd(0, (int *)&entry->var) != OK) {
@@ -279,53 +315,83 @@ tss_create_and_update_table(tss_t *key, char *tss_key_name, char *run_once_arg_s
/* Update the global key (thread_local) to point to this entry */
*key = (tss_t)entry;
/* Add to symbol table for tracking */
tss_symlist_add_new_internal(selfTaskId, run_once_arg_str, tss_key_name, key, TSS_SYMLIST_STATE_ADDED);
/* Add to symbol table for tracking */
int result = tss_symlist_add_new_internal(selfTaskId, run_once_arg_str,
tss_key_name, key, TSS_SYMLIST_STATE_ADDED, TRUE);
if (result != OK) {
/* Rollback on failure */
taskVarDelete(selfTaskId, (void *)&entry->var);
free(entry);
*key = (tss_t)0;
return ERROR;
}
return OK;
}
/* Clone entries for a new task - correctly passes the global key address */
void
tss_symlist_clone_entries_for_new_task(TASK_ID tid, char *run_once_arg_string)
/* check if entry exists for current task by tid, key name, and arg.
If not found, clone from the clone list. Assumed lock held by caller */
static int
tss_symlist_check_and_clone(TASK_ID tid,
const char *tss_key_name,
const char *run_once_arg_str,
tss_t *key)
{
int idx;
/* First, check if entry already exists in the symbol table */
for (idx = 0; idx < MAX_TSS_SYM_ENTRY; idx++) {
if (tss_sym_table[idx].state == TSS_SYMLIST_STATE_INVALID)
continue;
if (tss_sym_table[idx].taskid == (long)tid &&
strncmp(tss_sym_table[idx].info.tss_key_name, tss_key_name, MAX_STR_LEN) == 0 &&
strncmp(tss_sym_table[idx].info.run_once_arg_str, run_once_arg_str, MAX_STR_LEN) == 0) {
/* Found existing entry, no need to clone */
return ERROR;
}
}
/* Not found, clone entry - lock already held by caller */
return tss_create_and_update_table(key,
(char *)tss_key_name,
(char *)run_once_arg_str);
}
/* Clone missing entries for a new task */
void
tss_symlist_clone_entries_for_new_task(char *run_once_arg_string)
{
int i = 0;
if (!g_tss_sym_initialized) {
tss_sym_init();
}
TASK_ID current_tid = taskIdSelf();
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
/* write lock to avoid race */
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
/* Check if entry already added for current task */
int existing_idx = tss_symlist_find_entry_by_tid_and_arg(current_tid, run_once_arg_string);
int existing_idx = tss_symlist_find_entry_by_tid(current_tid);
if (existing_idx != ERROR) {
/* Entry already exists for this task, no need to clone */
CRYPTO_THREAD_unlock(tss_sym_table_lock);
return;
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
/* Find entries with matching run_once_arg_str */
while (i < MAX_TSS_SYM_ENTRY) {
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
tss_sym_table[i].taskid == tid &&
strncmp(tss_sym_table[i].run_once_arg_str, run_once_arg_string, MAX_STR_LEN) == 0) {
tss_create_and_update_table(tss_sym_table[i].tss_key,
tss_sym_table[i].tss_key_name,
run_once_arg_string);
}
i++;
int i;
/* Iterate through clone list entries matching the argument string */
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
if (tss_clone_list[i].tss_key == NULL)
continue;
int result = tss_symlist_check_and_clone(current_tid,
tss_clone_list[i].tss_key_name,
tss_clone_list[i].run_once_arg_str,
tss_clone_list[i].tss_key);
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
}
/* De-initialize the TSS symbol table */
@@ -337,44 +403,52 @@ tss_sym_deinit(void)
if (!g_tss_sym_initialized)
return OK;
/* If lock is not created, just mark uninitialized and return */
if (tss_sym_table_lock == NULL) {
g_tss_sym_initialized = 0;
return OK;
}
/* Acquire write lock before tearing down */
if (!CRYPTO_THREAD_write_lock(tss_sym_table_lock))
return ERROR;
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
/* Free any remaining entries */
/* Free any remaining per-task entries in the symbol table */
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
tss_entry_t *entry = NULL;
if (tss_sym_table[i].state == TSS_SYMLIST_STATE_INVALID)
continue;
/* If tss_key points to a slot, clear it */
if (tss_sym_table[i].tss_key) {
entry = (tss_entry_t *)(*tss_sym_table[i].tss_key);
if (tss_sym_table[i].info.tss_key) {
tss_entry_t *entry = (tss_entry_t *)(*tss_sym_table[i].info.tss_key);
if (entry) {
if (entry->dtor)
entry->dtor(entry->var);
free(entry);
*tss_sym_table[i].tss_key = (tss_t)0; /* clear slot */
*tss_sym_table[i].info.tss_key = (tss_t)0; /* clear per-task slot */
}
tss_sym_table[i].tss_key = NULL;
/* Clear the stored pointer to the key variable itself */
tss_sym_table[i].info.tss_key = NULL;
}
/* Clear the table slot */
memset(&tss_sym_table[i], 0, sizeof(tss_sym_entry));
tss_sym_table[i].state = TSS_SYMLIST_STATE_INVALID;
}
/* Clear clone list entries */
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
if (tss_clone_list[i].tss_key == NULL)
continue;
memset(&tss_clone_list[i], 0, sizeof(tss_sym_info));
tss_clone_list[i].tss_key = NULL;
}
CRYPTO_THREAD_unlock(tss_sym_table_lock);
/* Free the lock */
/* Free the lock and clear pointer */
CRYPTO_THREAD_lock_free(tss_sym_table_lock);
tss_sym_table_lock = NULL;
/* Remove cleanup hook from task delete chain */
taskDeleteHookDelete((FUNCPTR)tss_symlist_task_cleanup_hook);
g_tss_sym_initialized = 0;
return OK;
}
}