Skip to content

Commit

Permalink
Merge pull request atomvm#1020 from bettio/add-missing-mutex-init
Browse files Browse the repository at this point in the history
Fix: add missing mutex init/destroy

entropy and random mutexes were initialized only on generic_unix.

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
  • Loading branch information
bettio committed Feb 1, 2024
2 parents f26a643 + 93b8cf6 commit bed726e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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

Expand Down
14 changes: 14 additions & 0 deletions src/platforms/esp32/components/avm_sys/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
16 changes: 16 additions & 0 deletions src/platforms/rp2040/src/lib/sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand Down

0 comments on commit bed726e

Please sign in to comment.