From 93b8cf6da867d288c2b29529a7770a8bcf62da00 Mon Sep 17 00:00:00 2001 From: Davide Bettio Date: Mon, 29 Jan 2024 19:56:58 +0100 Subject: [PATCH] Fix: add missing mutex init entropy and random mutexes were initialized only on generic_unix, and also destroy was missing. Signed-off-by: Davide Bettio --- CHANGELOG.md | 1 + src/platforms/esp32/components/avm_sys/sys.c | 14 ++++++++++++++ src/platforms/rp2040/src/lib/sys.c | 16 ++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 814ce67cf..00ec0f333 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix several missing memory allocation checks in libAtomVM. - Fixed a possible memory leak in libAtomVM/module.c `module_destroy`. +- Fix possibile bug in random number generator on ESP32 and RPi2040 ## [0.6.0-alpha.2] - 2023-12-10 diff --git a/src/platforms/esp32/components/avm_sys/sys.c b/src/platforms/esp32/components/avm_sys/sys.c index 601f709db..2fc790bff 100644 --- a/src/platforms/esp32/components/avm_sys/sys.c +++ b/src/platforms/esp32/components/avm_sys/sys.c @@ -254,6 +254,15 @@ void sys_init_platform(GlobalContext *glb) if (UNLIKELY(esp_pthread_set_cfg(&esp_pthread_cfg))) { AVM_ABORT(); } + + platform->entropy_mutex = smp_mutex_create(); + if (IS_NULL_PTR(platform->entropy_mutex)) { + AVM_ABORT(); + } + platform->random_mutex = smp_mutex_create(); + if (IS_NULL_PTR(platform->random_mutex)) { + AVM_ABORT(); + } #endif platform->entropy_is_initialized = false; @@ -282,6 +291,11 @@ void sys_free_platform(GlobalContext *glb) mbedtls_entropy_free(&platform->entropy_ctx); } +#ifndef AVM_NO_SMP + smp_mutex_destroy(platform->entropy_mutex); + smp_mutex_destroy(platform->random_mutex); +#endif + free(platform); } diff --git a/src/platforms/rp2040/src/lib/sys.c b/src/platforms/rp2040/src/lib/sys.c index bbc7ee3dd..6f079dd14 100644 --- a/src/platforms/rp2040/src/lib/sys.c +++ b/src/platforms/rp2040/src/lib/sys.c @@ -90,6 +90,17 @@ void sys_init_platform(GlobalContext *glb) otp_socket_init(glb); #endif +#ifndef AVM_NO_SMP + platform->entropy_mutex = smp_mutex_create(); + if (IS_NULL_PTR(platform->entropy_mutex)) { + AVM_ABORT(); + } + platform->random_mutex = smp_mutex_create(); + if (IS_NULL_PTR(platform->random_mutex)) { + AVM_ABORT(); + } +#endif + platform->entropy_is_initialized = false; platform->random_is_initialized = false; } @@ -111,6 +122,11 @@ void sys_free_platform(GlobalContext *glb) mbedtls_entropy_free(&platform->entropy_ctx); } +#ifndef AVM_NO_SMP + smp_mutex_destroy(platform->entropy_mutex); + smp_mutex_destroy(platform->random_mutex); +#endif + free(platform); #ifndef AVM_NO_SMP