Skip to content

Commit 8532a5b

Browse files
committed
Fix debug calls for runtime loading
Now that the tests have a verbose mode, the debug calls need to be loaded in at runtime for non-static builds.
1 parent a50cd04 commit 8532a5b

File tree

4 files changed

+118
-10
lines changed

4 files changed

+118
-10
lines changed

tests/debug_test.c

Lines changed: 81 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,13 @@
4141

4242
#ifndef HAVE_PKCS11_STATIC
4343
#include <dlfcn.h>
44+
static void* dlib = NULL;
45+
void (*wolfPKCS11_Debugging_On_fp)(void) = NULL;
46+
void (*wolfPKCS11_Debugging_Off_fp)(void) = NULL;
4447
#endif
4548

49+
static CK_FUNCTION_LIST_PTR pFunctionList = NULL;
50+
4651
#ifdef DEBUG_WOLFPKCS11
4752
static FILE* original_stdout = NULL;
4853
static FILE* capture_file = NULL;
@@ -80,6 +85,67 @@ static int check_debug_output(void)
8085
fclose(capture_file);
8186
return found_debug;
8287
}
88+
89+
/* Wrapper functions for debugging */
90+
static void call_wolfPKCS11_Debugging_On(void) {
91+
#ifndef HAVE_PKCS11_STATIC
92+
if (wolfPKCS11_Debugging_On_fp != NULL) {
93+
wolfPKCS11_Debugging_On_fp();
94+
}
95+
#else
96+
wolfPKCS11_Debugging_On();
97+
#endif
98+
}
99+
100+
static void call_wolfPKCS11_Debugging_Off(void) {
101+
#ifndef HAVE_PKCS11_STATIC
102+
if (wolfPKCS11_Debugging_Off_fp != NULL) {
103+
wolfPKCS11_Debugging_Off_fp();
104+
}
105+
#else
106+
wolfPKCS11_Debugging_Off();
107+
#endif
108+
}
109+
110+
static CK_RV debug_init(const char* library) {
111+
CK_RV ret = CKR_OK;
112+
#ifndef HAVE_PKCS11_STATIC
113+
void* func;
114+
115+
dlib = dlopen(library, RTLD_NOW | RTLD_LOCAL);
116+
if (dlib == NULL) {
117+
fprintf(stderr, "dlopen error: %s\n", dlerror());
118+
return -1;
119+
}
120+
121+
func = (void*)(CK_C_GetFunctionList)dlsym(dlib, "C_GetFunctionList");
122+
if (func == NULL) {
123+
fprintf(stderr, "Failed to get function list function\n");
124+
dlclose(dlib);
125+
return -1;
126+
}
127+
128+
wolfPKCS11_Debugging_On_fp = (void (*)(void))dlsym(dlib,
129+
"wolfPKCS11_Debugging_On");
130+
wolfPKCS11_Debugging_Off_fp = (void (*)(void))dlsym(dlib,
131+
"wolfPKCS11_Debugging_Off");
132+
133+
ret = ((CK_C_GetFunctionList)func)(&pFunctionList);
134+
#else
135+
ret = C_GetFunctionList(&pFunctionList);
136+
(void)library;
137+
#endif
138+
return ret;
139+
}
140+
141+
static void debug_cleanup(void) {
142+
#ifndef HAVE_PKCS11_STATIC
143+
if (dlib) {
144+
dlclose(dlib);
145+
dlib = NULL;
146+
}
147+
#endif
148+
}
83149
#endif
84150

85151
int main(void)
@@ -90,8 +156,8 @@ int main(void)
90156
return 77;
91157
#else
92158
CK_RV rv;
93-
CK_FUNCTION_LIST_PTR pFunctionList;
94159
int debug_found;
160+
const char* library = "./src/.libs/libwolfpkcs11.so";
95161

96162
#ifndef WOLFPKCS11_NO_ENV
97163
if (!XGETENV("WOLFPKCS11_TOKEN_PATH")) {
@@ -102,22 +168,27 @@ int main(void)
102168
printf("=== wolfPKCS11 Debug Test Program ===\n");
103169
printf("Debug mode is ENABLED (DEBUG_WOLFPKCS11 defined)\n");
104170

171+
printf("\nInitializing library:\n");
172+
rv = debug_init(library);
173+
if (rv != CKR_OK) {
174+
printf("Failed to initialize library: %lu\n", (unsigned long)rv);
175+
return 1;
176+
}
177+
105178
printf("\nTesting debug control functions:\n");
106-
wolfPKCS11_Debugging_On();
179+
call_wolfPKCS11_Debugging_On();
107180
printf("Debug enabled\n");
108181

109-
wolfPKCS11_Debugging_Off();
182+
call_wolfPKCS11_Debugging_Off();
110183
printf("Debug disabled\n");
111184

112-
wolfPKCS11_Debugging_On();
185+
call_wolfPKCS11_Debugging_On();
113186
printf("Debug re-enabled\n");
114187

115188
printf("\nTesting PKCS#11 functions with debug output capture:\n");
116189

117190
setup_output_capture();
118191

119-
rv = C_GetFunctionList(&pFunctionList);
120-
121192
if (rv == CKR_OK && pFunctionList != NULL) {
122193
rv = pFunctionList->C_Initialize(NULL);
123194

@@ -133,13 +204,15 @@ int main(void)
133204
printf("C_GetFunctionList returned: %lu\n", (unsigned long)rv);
134205
printf("Debug output detection: %s\n", debug_found ? "PASS" : "FAIL");
135206

136-
wolfPKCS11_Debugging_Off();
207+
call_wolfPKCS11_Debugging_Off();
137208
printf("Debug disabled at end\n");
138209

210+
debug_cleanup();
139211
printf("\n=== Test Complete ===\n");
140212

141213
if (!debug_found) {
142-
printf("ERROR: No debug output was detected during PKCS#11 function calls\n");
214+
printf("ERROR: No debug output was detected during "
215+
"PKCS#11 function calls\n");
143216
return 1;
144217
}
145218

tests/include.am

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ tests_rsa_session_persistence_test_LDADD += src/libwolfpkcs11.la
4545
tests_debug_test_LDADD += src/libwolfpkcs11.la
4646
tests_object_id_uniqueness_test_LDADD += src/libwolfpkcs11.la
4747
else
48-
tests_debug_test_LDADD += src/libwolfpkcs11.la
4948
tests_object_id_uniqueness_test_LDADD += src/libwolfpkcs11.la
5049
endif
5150

tests/pkcs11test.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@
6464
static void* dlib;
6565
#endif
6666
static CK_FUNCTION_LIST* funcList;
67+
68+
#ifdef DEBUG_WOLFPKCS11
69+
#ifndef HAVE_PKCS11_STATIC
70+
void (*wolfPKCS11_Debugging_On_fp)(void) = NULL;
71+
void (*wolfPKCS11_Debugging_Off_fp)(void) = NULL;
72+
#endif
73+
#endif
6774
static int slot = 0;
6875
static const char* tokenName = "wolfpkcs11";
6976

@@ -13948,6 +13955,16 @@ static CK_RV pkcs11_init(const char* library)
1394813955
}
1394913956
}
1395013957

13958+
#ifdef DEBUG_WOLFPKCS11
13959+
if (ret == CKR_OK) {
13960+
wolfPKCS11_Debugging_On_fp = (void (*)(void))dlsym(dlib,
13961+
"wolfPKCS11_Debugging_On");
13962+
wolfPKCS11_Debugging_Off_fp = (void (*)(void))dlsym(dlib,
13963+
"wolfPKCS11_Debugging_Off");
13964+
/* These functions are optional, so don't fail if they're not found */
13965+
}
13966+
#endif
13967+
1395113968
if (ret == CKR_OK) {
1395213969
ret = ((CK_C_GetFunctionList)func)(&funcList);
1395313970
CHECK_CKR(ret, "Get Function List call");

tests/unit.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,25 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2020
*/
2121

22+
/* Forward declarations for debugging function pointers */
23+
#ifdef DEBUG_WOLFPKCS11
24+
#ifndef HAVE_PKCS11_STATIC
25+
extern void (*wolfPKCS11_Debugging_On_fp)(void);
26+
extern void (*wolfPKCS11_Debugging_Off_fp)(void);
27+
#endif
28+
29+
/* Wrapper function to handle both static and dynamic debugging calls */
30+
static inline void call_wolfPKCS11_Debugging_On(void) {
31+
#ifndef HAVE_PKCS11_STATIC
32+
if (wolfPKCS11_Debugging_On_fp != NULL) {
33+
wolfPKCS11_Debugging_On_fp();
34+
}
35+
#else
36+
wolfPKCS11_Debugging_On();
37+
#endif
38+
}
39+
#endif
40+
2241
#ifdef DEBUG_WOLFPKCS11
2342
#define CHECK_COND(cond, ret, msg) \
2443
do { \
@@ -323,7 +342,7 @@ static CK_RV run_tests(TEST_FUNC* testFunc, int testFuncCnt, int onlySet,
323342

324343
#ifdef DEBUG_WOLFPKCS11
325344
if (verbose) {
326-
wolfPKCS11_Debugging_On();
345+
call_wolfPKCS11_Debugging_On();
327346
}
328347
#endif
329348

0 commit comments

Comments
 (0)