Skip to content

Commit f7e1e37

Browse files
authored
Merge pull request #7438 from julek-wolfssl/zephr-no-malloc
zephyr no malloc
2 parents 10b31cc + 14ce8ce commit f7e1e37

30 files changed

Lines changed: 520 additions & 80 deletions

File tree

.github/workflows/zephyr.yml

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ jobs:
2323
zephyr-sdk: 0.16.1
2424
- zephyr-ref: v3.5.0
2525
zephyr-sdk: 0.16.3
26+
- zephyr-ref: v2.7.4
27+
zephyr-sdk: 0.16.3
2628
runs-on: ubuntu-latest
2729
# This should be a safe limit for the tests to run.
28-
timeout-minutes: 15
30+
timeout-minutes: 25
2931
steps:
3032
- name: Install dependencies
3133
run: |
@@ -78,25 +80,38 @@ jobs:
7880
cd zephyr-sdk-${{ matrix.config.zephyr-sdk }}
7981
./setup.sh -h -c -t x86_64-zephyr-elf
8082
83+
- name: Fix options for 2.7.4
84+
if: ${{ matrix.config.zephyr-ref == 'v2.7.4' }}
85+
working-directory: zephyr/modules/crypto/wolfssl
86+
run: |
87+
sed -i -e 's/CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE/CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE/g' $(find -name prj.conf)
88+
8189
- name: Run wolfssl test
8290
id: wolfssl-test
8391
working-directory: zephyr
8492
run: |
85-
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test -vvv
93+
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test -vvv
94+
rm -rf zephyr/twister-out
95+
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_test/sample.crypto.wolfssl_test_no_malloc -vvv
8696
rm -rf zephyr/twister-out
8797
8898
- name: Run wolfssl TLS sock test
99+
# Results in a page fault that I can't trace
100+
if: ${{ matrix.config.zephyr-ref != 'v2.7.4' }}
89101
id: wolfssl-tls-sock
90102
working-directory: zephyr
91103
run: |
92-
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock -vvv
104+
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock -vvv
105+
rm -rf zephyr/twister-out
106+
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_sock/sample.crypto.wolfssl_tls_sock_no_malloc -vvv
93107
rm -rf zephyr/twister-out
94108
95109
- name: Run wolfssl TLS thread test
110+
if: ${{ matrix.config.zephyr-ref != 'v2.7.4' }}
96111
id: wolfssl-tls-thread
97112
working-directory: zephyr
98113
run: |
99-
./zephyr/scripts/twister --testsuite-root modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_thread/sample.crypto.wolfssl_tls_thread -vvv
114+
./zephyr/scripts/twister -T modules/crypto/wolfssl --test zephyr/samples/wolfssl_tls_thread/sample.crypto.wolfssl_tls_thread -vvv
100115
rm -rf zephyr/twister-out
101116
102117
- name: Zip failure logs

src/internal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37860,7 +37860,7 @@ static int DoSessionTicket(WOLFSSL* ssl, const byte* input, word32* inOutIdx,
3786037860
#endif
3786137861
if (sess == NULL) {
3786237862
ret = TlsSessionCacheGetAndRdLock(id, &sess, &freeCtx->row,
37863-
ssl->options.side);
37863+
(byte)ssl->options.side);
3786437864
if (ret != 0)
3786537865
sess = NULL;
3786637866
}

src/ssl_p7p12.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ WC_PKCS12* wolfSSL_d2i_PKCS12_bio(WOLFSSL_BIO* bio, WC_PKCS12** pkcs12)
16841684
}
16851685

16861686
if (mem != NULL) {
1687-
localPkcs12 = wc_PKCS12_new();
1687+
localPkcs12 = wc_PKCS12_new_ex(bio->heap);
16881688
if (localPkcs12 == NULL) {
16891689
WOLFSSL_MSG("Memory error");
16901690
}

src/x509.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11678,8 +11678,9 @@ int wolfSSL_i2d_X509_NAME(WOLFSSL_X509_NAME* name, unsigned char** out)
1167811678
"-----BEGIN X509 CRL-----")) {
1167911679
/* We have a crl */
1168011680
WOLFSSL_MSG("Parsing crl");
11681-
if((PemToDer((const unsigned char*) header, footerEnd - header,
11682-
CRL_TYPE, &der, NULL, NULL, NULL)) < 0) {
11681+
if((PemToDer((const unsigned char*) header,
11682+
(long)(footerEnd - header), CRL_TYPE, &der, NULL, NULL,
11683+
NULL)) < 0) {
1168311684
WOLFSSL_MSG("PemToDer error");
1168411685
goto err;
1168511686
}

wolfcrypt/src/memory.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
992992
}
993993
#ifdef WOLFSSL_DEBUG_STATIC_MEMORY
994994
else {
995-
fprintf(stderr, "Size: %ld, Empty: %d\n", size,
995+
fprintf(stderr, "Size: %lu, Empty: %d\n", (unsigned long) size,
996996
mem->sizeList[i]);
997997
}
998998
#endif
@@ -1029,7 +1029,8 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
10291029
else {
10301030
WOLFSSL_MSG("ERROR ran out of static memory");
10311031
#ifdef WOLFSSL_DEBUG_MEMORY
1032-
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", size, func, line);
1032+
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", (unsigned long) size, func,
1033+
line);
10331034
#endif
10341035
}
10351036

wolfcrypt/src/pkcs12.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,22 @@ typedef struct WC_PKCS12_ATTRIBUTE {
130130

131131

132132
WC_PKCS12* wc_PKCS12_new(void)
133+
{
134+
return wc_PKCS12_new_ex(NULL);
135+
}
136+
137+
138+
WC_PKCS12* wc_PKCS12_new_ex(void* heap)
133139
{
134140
WC_PKCS12* pkcs12 = (WC_PKCS12*)XMALLOC(sizeof(WC_PKCS12),
135-
NULL, DYNAMIC_TYPE_PKCS);
141+
heap, DYNAMIC_TYPE_PKCS);
136142
if (pkcs12 == NULL) {
137143
WOLFSSL_MSG("Memory issue when creating WC_PKCS12 struct");
138144
return NULL;
139145
}
140146

141147
XMEMSET(pkcs12, 0, sizeof(WC_PKCS12));
148+
pkcs12->heap = heap;
142149

143150
return pkcs12;
144151
}
@@ -202,7 +209,7 @@ void wc_PKCS12_free(WC_PKCS12* pkcs12)
202209
}
203210
#endif
204211

205-
XFREE(pkcs12, NULL, DYNAMIC_TYPE_PKCS);
212+
XFREE(pkcs12, heap, DYNAMIC_TYPE_PKCS);
206213
}
207214

208215

@@ -2604,20 +2611,12 @@ WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz, char* name,
26042611
return NULL;
26052612
}
26062613

2607-
if ((pkcs12 = wc_PKCS12_new()) == NULL) {
2614+
if ((pkcs12 = wc_PKCS12_new_ex(heap)) == NULL) {
26082615
wc_FreeRng(&rng);
26092616
WOLFSSL_LEAVE("wc_PKCS12_create", MEMORY_E);
26102617
return NULL;
26112618
}
26122619

2613-
if ((ret = wc_PKCS12_SetHeap(pkcs12, heap)) != 0) {
2614-
wc_PKCS12_free(pkcs12);
2615-
wc_FreeRng(&rng);
2616-
WOLFSSL_LEAVE("wc_PKCS12_create", ret);
2617-
(void)ret;
2618-
return NULL;
2619-
}
2620-
26212620
if (iter <= 0) {
26222621
iter = WC_PKCS12_ITT_DEFAULT;
26232622
}

wolfcrypt/src/random.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,25 +3731,33 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
37313731

37323732
#elif defined(WOLFSSL_ZEPHYR)
37333733

3734-
#include <version.h>
3734+
#include <version.h>
37353735

37363736
#if KERNEL_VERSION_NUMBER >= 0x30500
37373737
#include <zephyr/random/random.h>
37383738
#else
3739-
#include <zephyr/random/rand32.h>
3739+
#if KERNEL_VERSION_NUMBER >= 0x30100
3740+
#include <zephyr/random/rand32.h>
3741+
#else
3742+
#include <random/rand32.h>
3743+
#endif
37403744
#endif
37413745

37423746
#ifndef _POSIX_C_SOURCE
3743-
#include <zephyr/posix/time.h>
3747+
#if KERNEL_VERSION_NUMBER >= 0x30100
3748+
#include <zephyr/posix/time.h>
3749+
#else
3750+
#include <posix/time.h>
3751+
#endif
37443752
#else
37453753
#include <time.h>
37463754
#endif
37473755

3748-
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3749-
{
3750-
sys_rand_get(output, sz);
3751-
return 0;
3752-
}
3756+
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3757+
{
3758+
sys_rand_get(output, sz);
3759+
return 0;
3760+
}
37533761

37543762
#elif defined(WOLFSSL_TELIT_M2MB)
37553763

wolfcrypt/src/wc_port.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,11 +3668,13 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
36683668

36693669
#elif defined(WOLFSSL_ZEPHYR)
36703670

3671+
void* wolfsslThreadHeapHint = NULL;
3672+
36713673
int wolfSSL_NewThread(THREAD_TYPE* thread,
36723674
THREAD_CB cb, void* arg)
36733675
{
36743676
#ifndef WOLFSSL_ZEPHYR_STACK_SZ
3675-
#define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
3677+
#define WOLFSSL_ZEPHYR_STACK_SZ (48*1024)
36763678
#endif
36773679

36783680
if (thread == NULL || cb == NULL)
@@ -3686,10 +3688,12 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
36863688
* 0);
36873689
*/
36883690
thread->threadStack = (void*)XMALLOC(
3689-
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
3690-
DYNAMIC_TYPE_TMP_BUFFER);
3691-
if (thread->threadStack == NULL)
3691+
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ),
3692+
wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
3693+
if (thread->threadStack == NULL) {
3694+
WOLFSSL_MSG("error: XMALLOC failed");
36923695
return MEMORY_E;
3696+
}
36933697

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

37223727
/* No thread resources to free. Everything is stored in thread.tid */

0 commit comments

Comments
 (0)