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:
committed by
neelimap
co-authored by
subhamgdeb
parent
bd330e941f
commit
d6e76bcf3d
@@ -38,15 +38,11 @@ int tss_symlist_add_new(TASK_ID taskid,
|
|||||||
tss_t *tss_key,
|
tss_t *tss_key,
|
||||||
tss_sym_state state);
|
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
|
* Check and populate keys for current task based on run_once_arg_str
|
||||||
*/
|
*/
|
||||||
void
|
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 */
|
/* Display the tss symbol table */
|
||||||
int
|
int
|
||||||
|
|||||||
@@ -378,15 +378,7 @@ int ossl_init_thread_start(const void *index, void *arg,
|
|||||||
THREAD_EVENT_HANDLER **hands;
|
THREAD_EVENT_HANDLER **hands;
|
||||||
THREAD_EVENT_HANDLER *hand;
|
THREAD_EVENT_HANDLER *hand;
|
||||||
|
|
||||||
TASK_ID tid = tss_symlist_get_first_tid("ossl_init_base");
|
tss_symlist_clone_entries_for_new_task("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");
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef FIPS_MODULE
|
#ifdef FIPS_MODULE
|
||||||
OSSL_LIB_CTX *ctx = arg;
|
OSSL_LIB_CTX *ctx = arg;
|
||||||
|
|||||||
@@ -49,11 +49,7 @@ crypto_once(CRYPTO_ONCE *once_control, void (*init_routine)(void))
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Per Task initialization */
|
/* Per Task initialization */
|
||||||
TASK_ID tid = tss_symlist_get_first_tid(once_control->run_once_arg);
|
tss_symlist_clone_entries_for_new_task(once_control->run_once_arg);
|
||||||
|
|
||||||
if (tid != ERROR) {
|
|
||||||
tss_symlist_clone_entries_for_new_task(tid, once_control->run_once_arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
semGive(once_control->sem);
|
semGive(once_control->sem);
|
||||||
return OK;
|
return OK;
|
||||||
|
|||||||
@@ -10,22 +10,30 @@
|
|||||||
#include <vxtss_symlist.h>
|
#include <vxtss_symlist.h>
|
||||||
|
|
||||||
#define MAX_TSS_SYM_ENTRY 256
|
#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 */
|
/* Sym table entry */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
long taskid;
|
long taskid;
|
||||||
char run_once_arg_str[MAX_STR_LEN];
|
tss_sym_info info;
|
||||||
char tss_key_name[MAX_STR_LEN];
|
|
||||||
tss_t *tss_key;
|
|
||||||
tss_sym_state state;
|
tss_sym_state state;
|
||||||
|
BOOL cloned;
|
||||||
} tss_sym_entry;
|
} tss_sym_entry;
|
||||||
|
|
||||||
/* Globals */
|
/* Globals */
|
||||||
static CRYPTO_RWLOCK *tss_sym_table_lock = NULL;
|
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 */
|
/* Helper functions */
|
||||||
static const char*
|
static const char*
|
||||||
@@ -33,13 +41,42 @@ tss_symlist_get_entry_state_str(tss_sym_state state)
|
|||||||
{
|
{
|
||||||
switch(state) {
|
switch(state) {
|
||||||
case TSS_SYMLIST_STATE_INVALID: return "INVALID";
|
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_ADDED: return "ADDED";
|
||||||
case TSS_SYMLIST_STATE_FAILED: return "FAILED";
|
case TSS_SYMLIST_STATE_FAILED: return "FAILED";
|
||||||
default: return "UNKNOWN";
|
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 */
|
/* Display the symbol table */
|
||||||
int
|
int
|
||||||
tss_symlist_show(void)
|
tss_symlist_show(void)
|
||||||
@@ -51,9 +88,9 @@ tss_symlist_show(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
printf("\n=== TSS Symbol Table ===\n");
|
printf("\n=== TSS Symbol Table ===\n");
|
||||||
printf("%-20s %-32s %-32s %-16s %-8s\n",
|
printf("%-32s %-32s %-32s %-16s %-8s %-8s\n",
|
||||||
"TNAME", "ONCE_ARGSTR", "TSSKEYNAME", "TSSKEYVAL", "STATE");
|
"TINFO", "ONCE_ARGSTR", "TSSKEYNAME", "TSSKEYVAL", "STATE", "CLONE");
|
||||||
printf("------------------------------------------------------------------------------------------------------------\n");
|
printf("-------------------------------------------------------------------------------------------------------------------------------------\n");
|
||||||
|
|
||||||
/* Acquire lock for reading */
|
/* Acquire lock for reading */
|
||||||
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
|
CRYPTO_THREAD_read_lock(tss_sym_table_lock);
|
||||||
@@ -63,20 +100,30 @@ tss_symlist_show(void)
|
|||||||
continue; /* Skip invalid entries */
|
continue; /* Skip invalid entries */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char tid_buf[64]; /* enough for name + hex TID */
|
||||||
const char *tname = taskName((int)tss_sym_table[i].taskid);
|
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);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
printf("=== End of Table ===\n\n");
|
/* 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;
|
return OK;
|
||||||
}
|
}
|
||||||
@@ -95,14 +142,13 @@ tss_symlist_find_free_slot(void)
|
|||||||
|
|
||||||
/* Helper function to check if entry exists for a task */
|
/* Helper function to check if entry exists for a task */
|
||||||
static int
|
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;
|
int i;
|
||||||
|
|
||||||
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
|
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
|
||||||
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
|
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
|
||||||
tss_sym_table[i].taskid == (long)tid &&
|
tss_sym_table[i].taskid == (long)tid) {
|
||||||
strncmp(tss_sym_table[i].run_once_arg_str, run_once_arg_string, MAX_STR_LEN) == 0) {
|
|
||||||
return i; /* Return index of matching entry */
|
return i; /* Return index of matching entry */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,9 +156,53 @@ tss_symlist_find_entry_by_tid_and_arg(TASK_ID tid, char *run_once_arg_string)
|
|||||||
return ERROR; /* No matching entry found */
|
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
|
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;
|
int idx;
|
||||||
|
|
||||||
@@ -125,54 +215,18 @@ tss_symlist_add_new_internal(TASK_ID taskId, char *run_once_arg_str, char *tss_k
|
|||||||
|
|
||||||
/* Add new entry */
|
/* Add new entry */
|
||||||
tss_sym_table[idx].taskid = (long)taskId;
|
tss_sym_table[idx].taskid = (long)taskId;
|
||||||
strncpy(tss_sym_table[idx].run_once_arg_str, run_once_arg_str, MAX_STR_LEN - 1);
|
strncpy(tss_sym_table[idx].info.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';
|
tss_sym_table[idx].info.run_once_arg_str[MAX_STR_LEN - 1] = '\0';
|
||||||
strncpy(tss_sym_table[idx].tss_key_name, tss_key_name, MAX_STR_LEN - 1);
|
strncpy(tss_sym_table[idx].info.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].info.tss_key_name[MAX_STR_LEN - 1] = '\0';
|
||||||
tss_sym_table[idx].tss_key = (tss_t*)key;
|
tss_sym_table[idx].info.tss_key = (tss_t*)key;
|
||||||
tss_sym_table[idx].state = state;
|
tss_sym_table[idx].state = state;
|
||||||
|
tss_sym_table[idx].cloned = cloned;
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* taskHook cleanup */
|
/* Initialize the TSS symbol table and clone list */
|
||||||
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 */
|
|
||||||
int
|
int
|
||||||
tss_sym_init(void)
|
tss_sym_init(void)
|
||||||
{
|
{
|
||||||
@@ -186,36 +240,45 @@ tss_sym_init(void)
|
|||||||
memset(&tss_sym_table[i], 0, sizeof(tss_sym_entry));
|
memset(&tss_sym_table[i], 0, sizeof(tss_sym_entry));
|
||||||
tss_sym_table[i].state = TSS_SYMLIST_STATE_INVALID;
|
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 */
|
/* Create new lock */
|
||||||
tss_sym_table_lock = (CRYPTO_RWLOCK *)CRYPTO_THREAD_lock_new();
|
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;
|
g_tss_sym_initialized = 1;
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Add new entry to the symbol table */
|
/* Add new entry to the symbol table and (if appropriate) to the clone list */
|
||||||
int
|
int
|
||||||
tss_symlist_add_new(TASK_ID taskId, char *run_once_arg_str, char *tss_key_name, tss_t *key, tss_sym_state state)
|
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) {
|
if (!g_tss_sym_initialized) {
|
||||||
tss_sym_init();
|
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 */
|
/* Acquire lock */
|
||||||
CRYPTO_THREAD_write_lock(tss_sym_table_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 */
|
/* Add this entry to the clone list if it isn't already added */
|
||||||
if (g_first_tid == -1 && result == OK) {
|
if (result == OK) {
|
||||||
g_first_tid = taskId;
|
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 */
|
/* Release lock */
|
||||||
@@ -224,45 +287,18 @@ tss_symlist_add_new(TASK_ID taskId, char *run_once_arg_str, char *tss_key_name,
|
|||||||
return result;
|
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 */
|
/* 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_create_and_update_table(tss_t *key, char *tss_key_name, char *run_once_arg_str)
|
||||||
{
|
{
|
||||||
tss_entry_t *entry;
|
tss_entry_t *entry;
|
||||||
TASK_ID selfTaskId;
|
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));
|
entry = (tss_entry_t *)malloc(sizeof(tss_entry_t));
|
||||||
if (entry == NULL) {
|
if (entry == NULL) {
|
||||||
return ERROR;
|
return ERROR;
|
||||||
@@ -281,27 +317,62 @@ tss_create_and_update_table(tss_t *key, char *tss_key_name, char *run_once_arg_s
|
|||||||
*key = (tss_t)entry;
|
*key = (tss_t)entry;
|
||||||
|
|
||||||
/* Add to symbol table for tracking */
|
/* Add to symbol table for tracking */
|
||||||
tss_symlist_add_new_internal(selfTaskId, run_once_arg_str, tss_key_name, key, TSS_SYMLIST_STATE_ADDED);
|
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;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Clone entries for a new task - correctly passes the global key address */
|
/* check if entry exists for current task by tid, key name, and arg.
|
||||||
void
|
If not found, clone from the clone list. Assumed lock held by caller */
|
||||||
tss_symlist_clone_entries_for_new_task(TASK_ID tid, char *run_once_arg_string)
|
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 i = 0;
|
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)
|
||||||
|
{
|
||||||
if (!g_tss_sym_initialized) {
|
if (!g_tss_sym_initialized) {
|
||||||
tss_sym_init();
|
tss_sym_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
TASK_ID current_tid = taskIdSelf();
|
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 */
|
/* 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) {
|
if (existing_idx != ERROR) {
|
||||||
/* Entry already exists for this task, no need to clone */
|
/* Entry already exists for this task, no need to clone */
|
||||||
@@ -309,23 +380,18 @@ tss_symlist_clone_entries_for_new_task(TASK_ID tid, char *run_once_arg_string)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
CRYPTO_THREAD_unlock(tss_sym_table_lock);
|
int i;
|
||||||
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
|
/* Iterate through clone list entries matching the argument string */
|
||||||
|
for (i = 0; i < MAX_CLONE_ENTRIES; i++) {
|
||||||
/* Find entries with matching run_once_arg_str */
|
if (tss_clone_list[i].tss_key == NULL)
|
||||||
while (i < MAX_TSS_SYM_ENTRY) {
|
continue;
|
||||||
if (tss_sym_table[i].state != TSS_SYMLIST_STATE_INVALID &&
|
int result = tss_symlist_check_and_clone(current_tid,
|
||||||
tss_sym_table[i].taskid == tid &&
|
tss_clone_list[i].tss_key_name,
|
||||||
strncmp(tss_sym_table[i].run_once_arg_str, run_once_arg_string, MAX_STR_LEN) == 0) {
|
tss_clone_list[i].run_once_arg_str,
|
||||||
tss_create_and_update_table(tss_sym_table[i].tss_key,
|
tss_clone_list[i].tss_key);
|
||||||
tss_sym_table[i].tss_key_name,
|
|
||||||
run_once_arg_string);
|
|
||||||
}
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CRYPTO_THREAD_unlock(tss_sym_table_lock);
|
CRYPTO_THREAD_unlock(tss_sym_table_lock);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* De-initialize the TSS symbol table */
|
/* De-initialize the TSS symbol table */
|
||||||
@@ -337,44 +403,52 @@ tss_sym_deinit(void)
|
|||||||
if (!g_tss_sym_initialized)
|
if (!g_tss_sym_initialized)
|
||||||
return OK;
|
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 */
|
/* Acquire write lock before tearing down */
|
||||||
if (!CRYPTO_THREAD_write_lock(tss_sym_table_lock))
|
CRYPTO_THREAD_write_lock(tss_sym_table_lock);
|
||||||
return ERROR;
|
|
||||||
|
|
||||||
/* Free any remaining entries */
|
/* Free any remaining per-task entries in the symbol table */
|
||||||
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
|
for (i = 0; i < MAX_TSS_SYM_ENTRY; i++) {
|
||||||
tss_entry_t *entry = NULL;
|
|
||||||
|
|
||||||
if (tss_sym_table[i].state == TSS_SYMLIST_STATE_INVALID)
|
if (tss_sym_table[i].state == TSS_SYMLIST_STATE_INVALID)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* If tss_key points to a slot, clear it */
|
if (tss_sym_table[i].info.tss_key) {
|
||||||
if (tss_sym_table[i].tss_key) {
|
tss_entry_t *entry = (tss_entry_t *)(*tss_sym_table[i].info.tss_key);
|
||||||
entry = (tss_entry_t *)(*tss_sym_table[i].tss_key);
|
|
||||||
if (entry) {
|
if (entry) {
|
||||||
if (entry->dtor)
|
if (entry->dtor)
|
||||||
entry->dtor(entry->var);
|
entry->dtor(entry->var);
|
||||||
free(entry);
|
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));
|
memset(&tss_sym_table[i], 0, sizeof(tss_sym_entry));
|
||||||
tss_sym_table[i].state = TSS_SYMLIST_STATE_INVALID;
|
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);
|
CRYPTO_THREAD_unlock(tss_sym_table_lock);
|
||||||
|
|
||||||
/* Free the lock */
|
/* Free the lock and clear pointer */
|
||||||
CRYPTO_THREAD_lock_free(tss_sym_table_lock);
|
CRYPTO_THREAD_lock_free(tss_sym_table_lock);
|
||||||
tss_sym_table_lock = NULL;
|
tss_sym_table_lock = NULL;
|
||||||
|
|
||||||
/* Remove cleanup hook from task delete chain */
|
|
||||||
taskDeleteHookDelete((FUNCPTR)tss_symlist_task_cleanup_hook);
|
|
||||||
|
|
||||||
g_tss_sym_initialized = 0;
|
g_tss_sym_initialized = 0;
|
||||||
|
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user