mirror of https://github.com/wolfSSL/wolfssl.git
Set the global heap hint in the static memory examples
With WOLFSSL_NO_MALLOC there is no allocator behind a NULL-heap XMALLOC, so an allocation made outside any CTX or SSL object can only be served from the global heap hint. wolfSSL_Init() makes such an allocation: under OPENSSL_EXTRA it seeds the compatibility-layer RNG, whose _InitRng() call allocates with a NULL heap. That returned NULL, wolfSSL_Init() reported WC_INIT_E, and every wolfSSL_CTX_new_ex() that triggered the lazy init failed, so neither example could establish a connection even though the pool it had loaded was large enough. Have each example nominate its own pool, which is what the hint is for. The server could not do that before: it created its CTX through wolfSSL_CTX_load_static_memory(), which loads the pool and creates the CTX in one call, so wolfSSL_Init() ran before the caller ever saw the hint. Load the pool with wc_LoadStaticMemory() and create the CTX afterwards, as the client already does. Claim the hint only when none is set, and drop it again on the way out. Both pools are local to the example's own function, and testsuite runs the wolfCrypt test, both examples and the echo server in one process, so an example that overwrote the hint would leave it pointing at a pool that dies the moment the example returns. scripts/resume.test, scripts/tls13.test and testsuite/testsuite.test go from failing to passing with --enable-staticmemory -DWOLFSSL_NO_MALLOC. Restrict the claim to the standalone programs (!NO_MAIN_DRIVER). testsuite and unit.test compile both examples with NO_MAIN_DRIVER and run server_test on a spawned thread beside client_test, so an in-harness claim would publish one thread's automatic-storage pool as the process allocator, let the other thread allocate from it, and then revoke it when the owning frame unwound. In those builds the harness's own long-lived pool is the one that belongs in the hint. Check ctx after wolfSSL_CTX_new_ex() rather than letting the following IO-pool load report a CTX allocation failure as "unable to load static memory".pull/11432/head
parent
00b8e2e5a9
commit
cb049aebcb
|
|
@ -3465,6 +3465,12 @@ THREAD_RETURN WOLFSSL_THREAD client_test(void* args)
|
|||
err_sys("unable to load static memory");
|
||||
}
|
||||
|
||||
#if defined(WOLFSSL_NO_MALLOC) && !defined(NO_MAIN_DRIVER)
|
||||
/* only the standalone program may publish a pool of its own */
|
||||
if (wolfSSL_GetGlobalHeapHint() == NULL)
|
||||
wolfSSL_SetGlobalHeapHint(heap);
|
||||
#endif
|
||||
|
||||
#if defined(WOLFSSL_STATIC_MEMORY) && \
|
||||
defined(WOLFSSL_STATIC_MEMORY_DEBUG_CALLBACK)
|
||||
wolfSSL_SetDebugMemoryCb(ExampleDebugMemoryCb);
|
||||
|
|
@ -5156,6 +5162,13 @@ exit:
|
|||
(void) useVerifyCb;
|
||||
(void) customVerifyCert;
|
||||
|
||||
#if defined(WOLFSSL_STATIC_MEMORY) && defined(WOLFSSL_NO_MALLOC) && \
|
||||
!defined(NO_MAIN_DRIVER)
|
||||
/* the pool backing the hint is on this function's stack */
|
||||
if (wolfSSL_GetGlobalHeapHint() == (void*)heap)
|
||||
wolfSSL_SetGlobalHeapHint(NULL);
|
||||
#endif
|
||||
|
||||
WOLFSSL_RETURN_FROM_THREAD(0);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1835,6 +1835,7 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
|
|||
WOLFSSL_MEM_STATS mem_stats;
|
||||
#endif
|
||||
#endif
|
||||
WOLFSSL_HEAP_HINT *heap = NULL;
|
||||
#endif
|
||||
#if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
|
||||
int onlyKeyShare = 0;
|
||||
|
|
@ -2826,9 +2827,19 @@ THREAD_RETURN WOLFSSL_THREAD server_test(void* args)
|
|||
WOLFMEM_IO_POOL_FIXED));
|
||||
#endif /* DEBUG_WOLFSSL */
|
||||
|
||||
if (wolfSSL_CTX_load_static_memory(&ctx, method, memory, sizeof(memory),0,1)
|
||||
!= WOLFSSL_SUCCESS)
|
||||
err_sys_ex(catastrophic, "unable to load static memory and create ctx");
|
||||
if (wc_LoadStaticMemory(&heap, memory, sizeof(memory), 0, 1) != 0)
|
||||
err_sys_ex(catastrophic, "unable to load static memory");
|
||||
|
||||
#if defined(WOLFSSL_NO_MALLOC) && !defined(NO_MAIN_DRIVER)
|
||||
/* only the standalone program may publish a pool of its own */
|
||||
if (wolfSSL_GetGlobalHeapHint() == NULL)
|
||||
wolfSSL_SetGlobalHeapHint(heap);
|
||||
#endif
|
||||
|
||||
if (method != NULL)
|
||||
ctx = wolfSSL_CTX_new_ex(method(heap), heap);
|
||||
if (ctx == NULL)
|
||||
err_sys_ex(catastrophic, "unable to get ctx");
|
||||
|
||||
/* load in a buffer for IO */
|
||||
if (wolfSSL_CTX_load_static_memory(&ctx, NULL, memoryIO, sizeof(memoryIO),
|
||||
|
|
@ -4249,6 +4260,13 @@ exit:
|
|||
#if defined(WOLFSSL_CALLBACKS) && defined(WOLFSSL_EARLY_DATA)
|
||||
(void) earlyData;
|
||||
#endif
|
||||
#if defined(WOLFSSL_STATIC_MEMORY) && defined(WOLFSSL_NO_MALLOC) && \
|
||||
!defined(NO_MAIN_DRIVER)
|
||||
/* the pool backing the hint is on this function's stack */
|
||||
if (wolfSSL_GetGlobalHeapHint() == (void*)heap)
|
||||
wolfSSL_SetGlobalHeapHint(NULL);
|
||||
#endif
|
||||
|
||||
WOLFSSL_RETURN_FROM_THREAD(0);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue