From 5bacbb8eef8a5cbf0fb20c3f689d4ff782135e55 Mon Sep 17 00:00:00 2001 From: Winford Date: Wed, 21 Feb 2024 14:40:26 -0800 Subject: [PATCH 1/2] Add missing memory check to ESP32 GPIO port driver Adds missing `NULL` check when `struct GPIOData *gpio_data` is malloc'd. Signed-off-by: Winford --- src/platforms/esp32/components/avm_builtins/gpio_driver.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/platforms/esp32/components/avm_builtins/gpio_driver.c b/src/platforms/esp32/components/avm_builtins/gpio_driver.c index b7728378ba..d7bc23258b 100644 --- a/src/platforms/esp32/components/avm_builtins/gpio_driver.c +++ b/src/platforms/esp32/components/avm_builtins/gpio_driver.c @@ -235,6 +235,11 @@ Context *gpio_driver_create_port(GlobalContext *global, term opts) Context *ctx = context_new(global); struct GPIOData *gpio_data = malloc(sizeof(struct GPIOData)); + if (IS_NULL_PTR(gpio_data)) { + ESP_LOGE(TAG, "Not enough free memory to initialize GPIO port driver!"); + scheduler_terminate(ctx); + return NULL; + } list_init(&gpio_data->gpio_listeners); ctx->native_handler = consume_gpio_mailbox; From b61cc87c985e2d273605c88a2be7e732d5c6e81d Mon Sep 17 00:00:00 2001 From: Winford Date: Fri, 23 Feb 2024 07:50:01 -0800 Subject: [PATCH 2/2] Fix crash when gpio:stop/0 or gpio:close/1 are used on ESP32 Fixes a hard vm crash when either `gpio:stop/0` or `gpio:close/1` are used on ESP32 platform, which emits the following error before dumping a stacktrace (ESP-IDF native, not an erlang stacktrace) in `idf.py monitor`: assert failed: tlsf_free tlsf.c:1119 (!block_is_free(block) && "block already marked as free") closes #816 Signed-off-by: Winford --- CHANGELOG.md | 1 + src/platforms/esp32/components/avm_builtins/gpio_driver.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f812c11d3b..e619823a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - ESP32: fix i2c_driver_acquire and i2c_driver_release functions, that were working only once. - Sending messages to registered processes using the `!` operator now works. - Fixed bug in `OP_SEND` that would accept sending a message to any integer or term without raising an error. +- ESP32: fixed bug in `gpio:stop/0` and `gpio:close/1` that would cause the VM to crash. ## [0.6.0-beta.0] - 2024-02-08 diff --git a/src/platforms/esp32/components/avm_builtins/gpio_driver.c b/src/platforms/esp32/components/avm_builtins/gpio_driver.c index d7bc23258b..cd0967a20e 100644 --- a/src/platforms/esp32/components/avm_builtins/gpio_driver.c +++ b/src/platforms/esp32/components/avm_builtins/gpio_driver.c @@ -275,7 +275,7 @@ static term gpiodriver_close(Context *ctx) struct GPIOListenerData *gpio_listener = GET_LIST_ENTRY(item, struct GPIOListenerData, gpio_listener_list_head); gpio_num = gpio_listener->gpio; list_remove(&gpio_listener->gpio_listener_list_head); - list_remove(&gpio_listener->listener.listeners_list_head); + sys_unregister_listener(ctx->global, &gpio_listener->listener); free(gpio_listener); gpio_set_intr_type(gpio_num, GPIO_INTR_DISABLE); @@ -287,6 +287,7 @@ static term gpiodriver_close(Context *ctx) } } + ctx->platform_data = NULL; globalcontext_unregister_process(glb, gpio_atom_index); free(gpio_data);