From a58a525742e24c6d87277bad19cea3ecce567936 Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 15 Dec 2023 14:58:15 +0000 Subject: [PATCH 1/8] Compat: Add FreeRTOS implementation for pthread functions. --- include/compat/freertosthreadcompat.h | 139 ++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 include/compat/freertosthreadcompat.h diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h new file mode 100644 index 0000000000..9184e0a183 --- /dev/null +++ b/include/compat/freertosthreadcompat.h @@ -0,0 +1,139 @@ +#ifndef LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H +#define LIBCRYPTOCOMPAT_FREERTOSTHREADCOMPAT_H + +#ifdef FREERTOS + +#include + +#include +#include +#include +#include + +static inline uid_t getuid() { + return 0; +} + + +// PTHREAD ONCE +#define pthread_once libressl_pthread_once +struct pthread_once { + int is_initialized; + int init_executed; +}; +#define pthread_once_t libressl_pthread_once_t +typedef struct pthread_once pthread_once_t; + +#define PTHREAD_ONCE_INIT { 1, 0 } + +static inline int +pthread_once(pthread_once_t *once, void (*cb) (void)) +{ + if (!once->is_initialized) { + return -1; + } + + if (!once->init_executed) { + once->init_executed = 1; + cb(); + return 0; + } + + return -1; + +} + +// PTHREAD Support +#define pthread_t libressl_pthread_t +typedef TaskHandle_t pthread_t; + +static inline pthread_t +pthread_self(void) +{ + return xTaskGetCurrentTaskHandle(); +} + +static inline int +pthread_equal(pthread_t t1, pthread_t t2) +{ + return t1 == t2; +} + + +// PTHREAD MUTEX Support +#define pthread_mutex libressl_pthread_mutex +struct pthread_mutex { + xSemaphoreHandle handle; +}; +#define pthread_mutex_t libressl_pthread_mutex_t +typedef struct pthread_mutex pthread_mutex_t; + +#define pthread_mutexattr libressl_mutexattr +struct pthread_mutexattr { + +}; +#define pthread_mutexattr_t libressl_mutexattr_t +typedef struct pthread_mutexattr pthread_mutexattr_t; + + +#define PTHREAD_MUTEX_INITIALIZER { NULL } + +static inline int +pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) +{ + xSemaphoreHandle x = xSemaphoreCreateMutex(); + if (x) { + mutex->handle = x; + return 0; + } + + return -1; +} + +static inline int +pthread_mutex_lock(pthread_mutex_t *mutex) +{ + xSemaphoreHandle x = mutex->handle; + if (x == NULL) { + x = xSemaphoreCreateMutex(); + mutex->handle = x; + } + + if ( xSemaphoreTake(x, portMAX_DELAY) == pdTRUE ) { + return 0; + } + else { + return -2; + } +} + +static inline int +pthread_mutex_unlock(pthread_mutex_t *mutex) +{ + xSemaphoreHandle x = mutex->handle; + if (x) { + if ( xSemaphoreGive(x) == pdTRUE ) { + return 0; + } + else { + return -1; + } + } + + return 0; +} + +static inline int +pthread_mutex_destroy(pthread_mutex_t *mutex) +{ + xSemaphoreHandle x = mutex->handle; + if (x) { + vQueueDelete(x); + } + mutex->handle = NULL; + return 0; +} + +#endif + +#endif From a5d536b052b26545d37165d51b4a0413024af63d Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 15 Dec 2023 14:58:42 +0000 Subject: [PATCH 2/8] Compat: pthread.h will use FreeRTOS shim when required. --- include/compat/pthread.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/compat/pthread.h b/include/compat/pthread.h index ed1b9dc51a..0663737eff 100644 --- a/include/compat/pthread.h +++ b/include/compat/pthread.h @@ -6,7 +6,9 @@ #ifndef LIBCRYPTOCOMPAT_PTHREAD_H #define LIBCRYPTOCOMPAT_PTHREAD_H -#ifdef _WIN32 +#if defined(FREERTOS) +#include +#elif defined(_WIN32) #include #include From 7c75b1e56c608ee796451c197bd9f1a010e4526c Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 15 Dec 2023 15:00:46 +0000 Subject: [PATCH 3/8] Compat: Adjust how syslog.h compat shim handles platforms without syslog present. --- include/compat/syslog.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/include/compat/syslog.h b/include/compat/syslog.h index c7a2608bdc..96e0b4ad42 100644 --- a/include/compat/syslog.h +++ b/include/compat/syslog.h @@ -3,7 +3,11 @@ * syslog.h compatibility shim */ -#ifndef _WIN32 +#if defined(_WIN32) +#define NO_SYSLOG +#elif defined(FREERTOS) +#define NO_SYSLOG +#else #include_next #endif @@ -14,7 +18,7 @@ #include -#ifdef _WIN32 +#ifdef NO_SYSLOG #define LOG_CONS LOG_INFO #define LOG_INFO 6 /* informational */ #define LOG_USER (1<<3) /* random user-level messages */ From c60781e73ec3363ad5231573bb830a4a404de769 Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 22 Dec 2023 08:41:51 +0000 Subject: [PATCH 4/8] Compat: Move get_uid to unistd.h --- include/compat/freertosthreadcompat.h | 5 ----- include/compat/unistd.h | 4 ++++ 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h index 9184e0a183..bd6f587b00 100644 --- a/include/compat/freertosthreadcompat.h +++ b/include/compat/freertosthreadcompat.h @@ -10,11 +10,6 @@ #include #include -static inline uid_t getuid() { - return 0; -} - - // PTHREAD ONCE #define pthread_once libressl_pthread_once struct pthread_once { diff --git a/include/compat/unistd.h b/include/compat/unistd.h index 63c07fc3dc..6198615b98 100644 --- a/include/compat/unistd.h +++ b/include/compat/unistd.h @@ -17,6 +17,10 @@ ssize_t pread(int d, void *buf, size_t nbytes, off_t offset); ssize_t pwrite(int d, const void *buf, size_t nbytes, off_t offset); #endif +#elif defined(FREERTOS) +static inline uid_t getuid(void) { + return 0; +} #else #include From dd2cdd8092aa0f1170299a27d0a3f39ddeb07910 Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 22 Dec 2023 08:46:15 +0000 Subject: [PATCH 5/8] Compat: Add libressl prefix namspacing for remaining functions. --- include/compat/freertosthreadcompat.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h index bd6f587b00..130b028578 100644 --- a/include/compat/freertosthreadcompat.h +++ b/include/compat/freertosthreadcompat.h @@ -42,12 +42,14 @@ pthread_once(pthread_once_t *once, void (*cb) (void)) #define pthread_t libressl_pthread_t typedef TaskHandle_t pthread_t; +#define pthread_self libressl_pthread_self static inline pthread_t pthread_self(void) { return xTaskGetCurrentTaskHandle(); } +#define pthread_equal libressl_pthread_equal static inline int pthread_equal(pthread_t t1, pthread_t t2) { @@ -63,16 +65,17 @@ struct pthread_mutex { #define pthread_mutex_t libressl_pthread_mutex_t typedef struct pthread_mutex pthread_mutex_t; -#define pthread_mutexattr libressl_mutexattr +#define pthread_mutexattr libressl_pthread_mutexattr struct pthread_mutexattr { }; -#define pthread_mutexattr_t libressl_mutexattr_t +#define pthread_mutexattr_t libressl_pthread_mutexattr_t typedef struct pthread_mutexattr pthread_mutexattr_t; #define PTHREAD_MUTEX_INITIALIZER { NULL } +#define pthread_mutex_init libressl_pthread_mutex_init static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { @@ -85,6 +88,7 @@ pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) return -1; } +#define pthread_mutex_lock libressl_pthread_mutex_lock static inline int pthread_mutex_lock(pthread_mutex_t *mutex) { @@ -102,6 +106,7 @@ pthread_mutex_lock(pthread_mutex_t *mutex) } } +#define pthread_mutex_unlock libressl_pthread_mutex_unlock static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) { @@ -118,6 +123,7 @@ pthread_mutex_unlock(pthread_mutex_t *mutex) return 0; } +#define pthread_mutex_destroy libressl_pthread_mutex_destroy static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) { From 84aea1d5f563bb5013beb6c3f83155af3f015afa Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 22 Dec 2023 08:58:49 +0000 Subject: [PATCH 6/8] Compat: Review feedback to cleanup internals of mutex_* functions. --- include/compat/freertosthreadcompat.h | 51 +++++++++------------------ 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h index 130b028578..692409f194 100644 --- a/include/compat/freertosthreadcompat.h +++ b/include/compat/freertosthreadcompat.h @@ -79,58 +79,41 @@ typedef struct pthread_mutexattr pthread_mutexattr_t; static inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) { - xSemaphoreHandle x = xSemaphoreCreateMutex(); - if (x) { - mutex->handle = x; - return 0; - } - - return -1; + if ((mutex->handle = xSemaphoreCreateMutex()) == NULL) + return -1; + return 0; } #define pthread_mutex_lock libressl_pthread_mutex_lock static inline int pthread_mutex_lock(pthread_mutex_t *mutex) { - xSemaphoreHandle x = mutex->handle; - if (x == NULL) { - x = xSemaphoreCreateMutex(); - mutex->handle = x; - } - - if ( xSemaphoreTake(x, portMAX_DELAY) == pdTRUE ) { - return 0; - } - else { - return -2; - } + if (mutex->handle == NULL) + mutex->handle = xSemaphoreCreateMutex(); + if (mutex->handle == NULL) + return -1; + if (xSemaphoreTake(mutex->handle, portMAX_DELAY) != pdTRUE) + return -1; + return 0; } #define pthread_mutex_unlock libressl_pthread_mutex_unlock static inline int pthread_mutex_unlock(pthread_mutex_t *mutex) { - xSemaphoreHandle x = mutex->handle; - if (x) { - if ( xSemaphoreGive(x) == pdTRUE ) { - return 0; - } - else { - return -1; - } - } - - return 0; + if (mutex->handle == NULL) + return 0; + if (xSemaphoreGive(mutex->handle) == pdTRUE) + return 0; + return -1; } #define pthread_mutex_destroy libressl_pthread_mutex_destroy static inline int pthread_mutex_destroy(pthread_mutex_t *mutex) { - xSemaphoreHandle x = mutex->handle; - if (x) { - vQueueDelete(x); - } + if (mutex->handle != NULL) + vQueueDelete(mutex->handle); mutex->handle = NULL; return 0; } From c47d69cd1d73c9fc62da2deb6002f964f4a7aee5 Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 22 Dec 2023 09:12:21 +0000 Subject: [PATCH 7/8] Compat: Threadsafe FreeRTOS pthread_once implementation. --- include/compat/freertosthreadcompat.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/include/compat/freertosthreadcompat.h b/include/compat/freertosthreadcompat.h index 692409f194..30b3e1668d 100644 --- a/include/compat/freertosthreadcompat.h +++ b/include/compat/freertosthreadcompat.h @@ -10,32 +10,35 @@ #include #include + // PTHREAD ONCE #define pthread_once libressl_pthread_once struct pthread_once { - int is_initialized; - int init_executed; + xSemaphoreHandle lock; + int init_executed; }; #define pthread_once_t libressl_pthread_once_t typedef struct pthread_once pthread_once_t; -#define PTHREAD_ONCE_INIT { 1, 0 } +#define PTHREAD_ONCE_INIT { .lock = NULL, .init_executed = 0 } static inline int pthread_once(pthread_once_t *once, void (*cb) (void)) { - if (!once->is_initialized) { + if (once->lock == NULL) + once->lock = xSemaphoreCreateMutex(); + if (once->lock == NULL) + return -1; + if (xSemaphoreTake(once->lock, portMAX_DELAY) != pdTRUE) return -1; - } if (!once->init_executed) { once->init_executed = 1; cb(); - return 0; } - return -1; - + xSemaphoreGive(once->lock); + return 0; } // PTHREAD Support From bb69805bb3f16fc50a34f999913f0cd77bd9f7d9 Mon Sep 17 00:00:00 2001 From: Ewan Date: Fri, 22 Dec 2023 09:14:49 +0000 Subject: [PATCH 8/8] Compat: Adjust syslog.h defined checks. --- include/compat/syslog.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/include/compat/syslog.h b/include/compat/syslog.h index 96e0b4ad42..683a60602c 100644 --- a/include/compat/syslog.h +++ b/include/compat/syslog.h @@ -3,9 +3,7 @@ * syslog.h compatibility shim */ -#if defined(_WIN32) -#define NO_SYSLOG -#elif defined(FREERTOS) +#if defined(_WIN32) || defined(FREERTOS) #define NO_SYSLOG #else #include_next