Skip to content

Commit af80836

Browse files
committed
Fix double free of esp32 uart driver on close
- Also fix usage of memory_ensure_free in esp32 uart driver - Also remove unnecessary condition in context_destroy Signed-off-by: Paul Guyot <pguyot@kallisys.net>
1 parent dfc3bf1 commit af80836

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ certain VM instructions are used.
2727
- Fixed an issue where a timeout would occur immediately in a race condition
2828
- Fixed SPI close command
2929
- Added missing lock on socket structure
30+
- Fixed a double free when esp32 uart driver was closed, yielding an assert abort
3031

3132
## [0.6.5] - 2024-10-15
3233

src/libAtomVM/context.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,7 @@ void context_destroy(Context *ctx)
178178
// globalcontext_get_process_lock before accessing platform_data.
179179
// Here, the context can no longer be acquired with
180180
// globalcontext_get_process_lock, so it's safe to free the pointer.
181-
if (ctx->platform_data) {
182-
free(ctx->platform_data);
183-
}
181+
free(ctx->platform_data);
184182

185183
free(ctx);
186184
}

src/platforms/esp32/components/avm_builtins/uart_driver.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static void uart_driver_do_read(Context *ctx, GenMessage gen_message)
309309
int local_pid = term_to_local_process_id(pid);
310310

311311
if (uart_data->reader_process_pid != term_invalid_term()) {
312-
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2) * 2 + REF_SIZE) != MEMORY_GC_OK)) {
312+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2) * 2 , 1, &ref, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
313313
ESP_LOGE(TAG, "[uart_driver_do_read] Failed to allocate space for error tuple");
314314
globalcontext_send_message(glb, local_pid, MEMORY_ATOM);
315315
return;
@@ -326,7 +326,7 @@ static void uart_driver_do_read(Context *ctx, GenMessage gen_message)
326326

327327
if (count > 0) {
328328
int bin_size = term_binary_heap_size(count);
329-
if (UNLIKELY(memory_ensure_free(ctx, bin_size + TUPLE_SIZE(2) * 2 + REF_SIZE) != MEMORY_GC_OK)) {
329+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, bin_size + TUPLE_SIZE(2) * 2, 1, &ref, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
330330
ESP_LOGE(TAG, "[uart_driver_do_read] Failed to allocate space for return value");
331331
globalcontext_send_message(glb, local_pid, MEMORY_ATOM);
332332
}
@@ -387,7 +387,7 @@ static void uart_driver_do_write(Context *ctx, GenMessage gen_message)
387387
free(buffer);
388388

389389
int local_pid = term_to_local_process_id(pid);
390-
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2) + REF_SIZE) != MEMORY_GC_OK)) {
390+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 1, &ref, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
391391
ESP_LOGE(TAG, "[uart_driver_do_write] Failed to allocate space for return value");
392392
globalcontext_send_message(glb, local_pid, MEMORY_ATOM);
393393
}
@@ -406,7 +406,7 @@ static void uart_driver_do_close(Context *ctx, GenMessage gen_message)
406406

407407
sys_unregister_listener(glb, &uart_data->listener);
408408

409-
if (UNLIKELY(memory_ensure_free(ctx, TUPLE_SIZE(2) + REF_SIZE) != MEMORY_GC_OK)) {
409+
if (UNLIKELY(memory_ensure_free_with_roots(ctx, TUPLE_SIZE(2), 1, &ref, MEMORY_CAN_SHRINK) != MEMORY_GC_OK)) {
410410
ESP_LOGE(TAG, "[uart_driver_do_close] Failed to allocate space for return value");
411411
globalcontext_send_message(glb, local_pid, MEMORY_ATOM);
412412
}
@@ -419,6 +419,7 @@ static void uart_driver_do_close(Context *ctx, GenMessage gen_message)
419419
}
420420

421421
free(uart_data);
422+
ctx->platform_data = NULL;
422423
}
423424

424425
static NativeHandlerResult uart_driver_consume_mailbox(Context *ctx)

0 commit comments

Comments
 (0)