Skip to content

Commit

Permalink
console: fix a crash when initializing usb_serial_jtag console
Browse files Browse the repository at this point in the history
The crash occurred when calling setvbuf(stdin,...) with stdin==NULL.
This happened because esp_console_repl_task started running before
its args->uart_channel was initialized; then esp_console_repl_task
went into the code path 'uart_channel != CONFIG_ESP_CONSOLE_UART_NUM',
and tried to 'fopen("/dev/uart/0");`
Since the UART VFS is not registered when ESP_CONSOLE_USB_SERIAL_JTAG
option is enabled, fopen failed and 'stdin' was NULL.
Fix by moving the initialization of repl task arguments before the
start of the task, same as it is done for the usb_cdcacm case.

The crash started happening after the commit 287ab75. I haven’t
verified this, but I guess the reason why it wasn’t happening before
was that xTaskCreate was not correctly yielding to the newly created
higher-priority 'repl' task, therefore the code which was setting
the repl task arguments after xTaskCreate had time to execute.

It should be noted that the 'uart_channel' argument is a bit hacky,
in the first place. The code should be refactored to pass a callback
function to the repl task, and let this callback initialize stdin and
stdout based on the chosen console channel. Then esp_console_repl_task
does not require assumptions about the specific interface used.

Closes #9662
  • Loading branch information
igrr committed Aug 30, 2022
1 parent c7696fb commit c1d5717
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions components/console/esp_console_repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,18 @@ esp_err_t esp_console_new_repl_usb_serial_jtag(const esp_console_dev_usb_serial_
// setup prompt
esp_console_setup_prompt(repl_config->prompt, &usb_serial_jtag_repl->repl_com);

/* Fill the structure here as it will be used directly by the created task. */
usb_serial_jtag_repl->uart_channel = CONFIG_ESP_CONSOLE_UART_NUM;
usb_serial_jtag_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
usb_serial_jtag_repl->repl_com.repl_core.del = esp_console_repl_usb_serial_jtag_delete;

/* spawn a single thread to run REPL */
if (xTaskCreate(esp_console_repl_task, "console_repl", repl_config->task_stack_size,
&usb_serial_jtag_repl->repl_com, repl_config->task_priority, &usb_serial_jtag_repl->repl_com.task_hdl) != pdTRUE) {
ret = ESP_FAIL;
goto _exit;
}

usb_serial_jtag_repl->uart_channel = CONFIG_ESP_CONSOLE_UART_NUM;
usb_serial_jtag_repl->repl_com.state = CONSOLE_REPL_STATE_INIT;
usb_serial_jtag_repl->repl_com.repl_core.del = esp_console_repl_usb_serial_jtag_delete;
*ret_repl = &usb_serial_jtag_repl->repl_com.repl_core;
return ESP_OK;
_exit:
Expand Down

0 comments on commit c1d5717

Please sign in to comment.