Skip to content

Commit

Permalink
Merge pull request #7438 from julek-wolfssl/zephr-no-malloc
Browse files Browse the repository at this point in the history
zephyr no malloc
  • Loading branch information
douzzer authored May 9, 2024
2 parents 10b31cc + 14ce8ce commit f7e1e37
Show file tree
Hide file tree
Showing 30 changed files with 520 additions and 80 deletions.
23 changes: 19 additions & 4 deletions .github/workflows/zephyr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ jobs:
zephyr-sdk: 0.16.1
- zephyr-ref: v3.5.0
zephyr-sdk: 0.16.3
- zephyr-ref: v2.7.4
zephyr-sdk: 0.16.3
runs-on: ubuntu-latest
# This should be a safe limit for the tests to run.
timeout-minutes: 15
timeout-minutes: 25
steps:
- name: Install dependencies
run: |
Expand Down Expand Up @@ -78,25 +80,38 @@ jobs:
cd zephyr-sdk-${{ matrix.config.zephyr-sdk }}
./setup.sh -h -c -t x86_64-zephyr-elf
- name: Fix options for 2.7.4
if: ${{ matrix.config.zephyr-ref == 'v2.7.4' }}
working-directory: zephyr/modules/crypto/wolfssl
run: |
sed -i -e 's/CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE/CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE/g' $(find -name prj.conf)
- name: Run wolfssl test
id: wolfssl-test
working-directory: zephyr
run: |
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test -vvv
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test -vvv
rm -rf zephyr/twister-out
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test_no_malloc -vvv
rm -rf zephyr/twister-out
- name: Run wolfssl TLS sock test
# Results in a page fault that I can't trace
if: ${{ matrix.config.zephyr-ref != 'v2.7.4' }}
id: wolfssl-tls-sock
working-directory: zephyr
run: |
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock -vvv
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock -vvv
rm -rf zephyr/twister-out
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock_no_malloc -vvv
rm -rf zephyr/twister-out
- name: Run wolfssl TLS thread test
if: ${{ matrix.config.zephyr-ref != 'v2.7.4' }}
id: wolfssl-tls-thread
working-directory: zephyr
run: |
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_thread/sample.crypto.wolfssl_tls_thread -vvv
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_thread/sample.crypto.wolfssl_tls_thread -vvv
rm -rf zephyr/twister-out
- name: Zip failure logs
Expand Down
2 changes: 1 addition & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -37860,7 +37860,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
#endif
if (sess == NULL) {
ret = TlsSessionCacheGetAndRdLock(id, &sess, &freeCtx->row,
ssl->options.side);
(byte)ssl->options.side);
if (ret != 0)
sess = NULL;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ssl_p7p12.c
Original file line number Diff line number Diff line change
Expand Up @@ -1684,7 +1684,7 @@ WC_PKCS12* wolfSSL_d2i_PKCS12_bio(WOLFSSL_BIO* bio, WC_PKCS12** pkcs12)
}

if (mem != NULL) {
localPkcs12 = wc_PKCS12_new();
localPkcs12 = wc_PKCS12_new_ex(bio->heap);
if (localPkcs12 == NULL) {
WOLFSSL_MSG("Memory error");
}
Expand Down
5 changes: 3 additions & 2 deletions src/x509.c
Original file line number Diff line number Diff line change
Expand Up @@ -11678,8 +11678,9 @@ int wolfSSL_i2d_X509_NAME(WOLFSSL_X509_NAME* name, unsigned char** out)
"-----BEGIN X509 CRL-----")) {
/* We have a crl */
WOLFSSL_MSG("Parsing crl");
if((PemToDer((const unsigned char*) header, footerEnd - header,
CRL_TYPE, &der, NULL, NULL, NULL)) < 0) {
if((PemToDer((const unsigned char*) header,
(long)(footerEnd - header), CRL_TYPE, &der, NULL, NULL,
NULL)) < 0) {
WOLFSSL_MSG("PemToDer error");
goto err;
}
Expand Down
5 changes: 3 additions & 2 deletions wolfcrypt/src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
}
#ifdef WOLFSSL_DEBUG_STATIC_MEMORY
else {
fprintf(stderr, "Size: %ld, Empty: %d\n", size,
fprintf(stderr, "Size: %lu, Empty: %d\n", (unsigned long) size,
mem->sizeList[i]);
}
#endif
Expand Down Expand Up @@ -1029,7 +1029,8 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
else {
WOLFSSL_MSG("ERROR ran out of static memory");
#ifdef WOLFSSL_DEBUG_MEMORY
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", size, func, line);
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", (unsigned long) size, func,
line);
#endif
}

Expand Down
21 changes: 10 additions & 11 deletions wolfcrypt/src/pkcs12.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,22 @@ typedef struct WC_PKCS12_ATTRIBUTE {


WC_PKCS12* wc_PKCS12_new(void)
{
return wc_PKCS12_new_ex(NULL);
}


WC_PKCS12* wc_PKCS12_new_ex(void* heap)
{
WC_PKCS12* pkcs12 = (WC_PKCS12*)XMALLOC(sizeof(WC_PKCS12),
NULL, DYNAMIC_TYPE_PKCS);
heap, DYNAMIC_TYPE_PKCS);
if (pkcs12 == NULL) {
WOLFSSL_MSG("Memory issue when creating WC_PKCS12 struct");
return NULL;
}

XMEMSET(pkcs12, 0, sizeof(WC_PKCS12));
pkcs12->heap = heap;

return pkcs12;
}
Expand Down Expand Up @@ -202,7 +209,7 @@ void wc_PKCS12_free(WC_PKCS12* pkcs12)
}
#endif

XFREE(pkcs12, NULL, DYNAMIC_TYPE_PKCS);
XFREE(pkcs12, heap, DYNAMIC_TYPE_PKCS);
}


Expand Down Expand Up @@ -2604,20 +2611,12 @@ WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz, char* name,
return NULL;
}

if ((pkcs12 = wc_PKCS12_new()) == NULL) {
if ((pkcs12 = wc_PKCS12_new_ex(heap)) == NULL) {
wc_FreeRng(&rng);
WOLFSSL_LEAVE("wc_PKCS12_create", MEMORY_E);
return NULL;
}

if ((ret = wc_PKCS12_SetHeap(pkcs12, heap)) != 0) {
wc_PKCS12_free(pkcs12);
wc_FreeRng(&rng);
WOLFSSL_LEAVE("wc_PKCS12_create", ret);
(void)ret;
return NULL;
}

if (iter <= 0) {
iter = WC_PKCS12_ITT_DEFAULT;
}
Expand Down
24 changes: 16 additions & 8 deletions wolfcrypt/src/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -3731,25 +3731,33 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)

#elif defined(WOLFSSL_ZEPHYR)

#include <version.h>
#include <version.h>

#if KERNEL_VERSION_NUMBER >= 0x30500
#include <zephyr/random/random.h>
#else
#include <zephyr/random/rand32.h>
#if KERNEL_VERSION_NUMBER >= 0x30100
#include <zephyr/random/rand32.h>
#else
#include <random/rand32.h>
#endif
#endif

#ifndef _POSIX_C_SOURCE
#include <zephyr/posix/time.h>
#if KERNEL_VERSION_NUMBER >= 0x30100
#include <zephyr/posix/time.h>
#else
#include <posix/time.h>
#endif
#else
#include <time.h>
#endif

int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
sys_rand_get(output, sz);
return 0;
}
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
sys_rand_get(output, sz);
return 0;
}

#elif defined(WOLFSSL_TELIT_M2MB)

Expand Down
15 changes: 10 additions & 5 deletions wolfcrypt/src/wc_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -3668,11 +3668,13 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)

#elif defined(WOLFSSL_ZEPHYR)

void* wolfsslThreadHeapHint = NULL;

int wolfSSL_NewThread(THREAD_TYPE* thread,
THREAD_CB cb, void* arg)
{
#ifndef WOLFSSL_ZEPHYR_STACK_SZ
#define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
#define WOLFSSL_ZEPHYR_STACK_SZ (48*1024)
#endif

if (thread == NULL || cb == NULL)
Expand All @@ -3686,10 +3688,12 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
* 0);
*/
thread->threadStack = (void*)XMALLOC(
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
DYNAMIC_TYPE_TMP_BUFFER);
if (thread->threadStack == NULL)
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ),
wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
if (thread->threadStack == NULL) {
WOLFSSL_MSG("error: XMALLOC failed");
return MEMORY_E;
}

/* k_thread_create does not return any error codes */
/* Casting to k_thread_entry_t should be fine since we just ignore the
Expand All @@ -3716,7 +3720,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
* if (err != 0)
* ret = MEMORY_E;
*/
XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_TMP_BUFFER);
XFREE(thread.threadStack, wolfsslThreadHeapHint,
DYNAMIC_TYPE_TMP_BUFFER);
thread.threadStack = NULL;

/* No thread resources to free. Everything is stored in thread.tid */
Expand Down
Loading

0 comments on commit f7e1e37

Please sign in to comment.