ivmarkov / edge-net

async + no_std + no-alloc implementations of various network protocols
Apache License 2.0
88 stars 15 forks source link

Core panic with edge-captive #28

Closed mawoka-myblock closed 3 days ago

mawoka-myblock commented 3 days ago

ESP's logs:

I (611) wifi_init: rx ba win: 6
I (611) wifi_init: tcpip mbox: 32
I (621) wifi_init: udp mbox: 6
I (621) wifi_init: tcp mbox: 6
I (621) wifi_init: tcp tx win: 5760
I (631) wifi_init: tcp rx win: 5760
I (631) wifi_init: tcp mss: 1440
I (641) wifi_init: WiFi IRAM OP enabled
I (641) wifi_init: WiFi RX IRAM OP enabled
Guru Meditation Error: Core  0 panic'ed (Stack protection fault). 

Detected in task "main" at 0x42007dda
0x42007dda - async_lock::once_cell::OnceCell<T>::get
    at /home/mawoka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-lock-3.4.0/src/once_cell.rs:173
Stack pointer: 0x3fc9b380
Stack bounds: 0x3fc9b388 - 0x3fc9d4c0

Core  0 register dump:
MEPC    : 0x42007ddc  RA      : 0x42007d30  SP      : 0x3fc9b380  GP      : 0x3fc92c00  
0x42007ddc - async_lock::once_cell::OnceCell<T>::get
    at /home/mawoka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-lock-3.4.0/src/once_cell.rs:173
0x42007d30 - async_lock::once_cell::OnceCell<T>::get_or_try_init_blocking
    at /home/mawoka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-lock-3.4.0/src/once_cell.rs:445
0x3fc92c00 - __global_pointer$
    at ??:??
TP      : 0x3fc56c98  T0      : 0x00000000  T1      : 0x42007cba  T2      : 0x00000000  
0x42007cba - core::result::Result<T,E>::expect
    at /var/home/mawoka/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/result.rs:1059
S0/FP   : 0x3fc95240  S1      : 0x00000001  A0      : 0x3fc95240  A1      : 0x00000001  
0x3fc95240 - $d
    at ??:??
0x3fc95240 - $d
    at ??:??
A2      : 0x00000000  A3      : 0x00000004  A4      : 0x00000000  A5      : 0x600c2194  
A6      : 0x00000001  A7      : 0x3fc9be10  S2      : 0x3fc9b414  S3      : 0x3fc9c5c8  
S4      : 0x00000000  S5      : 0x00000000  S6      : 0x3fc9c118  S7      : 0x3fc9c100  
S8      : 0x3fc9c090  S9      : 0x3fc9c09c  S10     : 0x3fc9c1f0  S11     : 0x00000001  
T3      : 0x3fc95dac  T4      : 0x00000036  T5      : 0x00000000  T6      : 0x420790a2  
0x3fc95dac - s_vfs
    at ??:??
0x420790a2 - sys_mutex_unlock
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/port/freertos/sys_arch.c:79
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x0000001b  MTVAL   : 0x0000c606  
0x40380001 - _vector_table
    at ??:??
MHARTID : 0x00000000  

Stack memory:
3fc9b380: 0x3fc9dd78 0x00002140 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc9b3a0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0x3fc9c124 0x3fc95040 0x3fc9bb80 0x42007ab8
0x42007ab8 - async_io::reactor::Reactor::get::{{closure}}
    at /home/mawoka/.cargo/registry/src/index.crates.io-6f17d22bba15001f/async-io-2.3.4/src/reactor.rs:104
3fc9b3c0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc9b3e0: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc9b400: 0x3fc95040 0x3fc9bdb3 0x00000000 0x3fc95040 0x3fc9bdb3 0xa5000000 0x3fc95040 0xa5a5a5a5
3fc9b420: 0x40387b9c 0x403872da 0x3fc9bc20 0x3fc92c00 0x40387b9c 0x403872da 0x3fc9bc30 0x3fc92c00
0x40387b9c - vPortClearInterruptMaskFromISR
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:520
0x403872da - xQueueGenericSend
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:1057
0x3fc92c00 - __global_pointer$
    at ??:??
0x40387b9c - vPortClearInterruptMaskFromISR
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:520
0x403872da - xQueueGenericSend
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:1057
0x3fc92c00 - __global_pointer$
    at ??:??
3fc9b440: 0x3fc56c98 0x3fc9dfe0 0x00000008 0x403822e6 0x3fca9178 0x00000000 0x00000001 0x00000001
0x403822e6 - heap_caps_malloc
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/heap/heap_caps.c:202
3fc9b460: 0x3fc99000 0x3fc99000 0x00000000 0x600c2194 0x00000006 0x00000006 0x00000000 0x3fc9bc7c
0x3fc99000 - phy_version_str
    at ??:??
0x3fc99000 - phy_version_str
    at ??:??
3fc9b480: 0xffffffff 0x00000002 0x3fca9188 0x3fc9c100 0x3fc9c090 0x3fc9c09c 0x3fc9c1f0 0x00000001
3fc9b4a0: 0x00000000 0x4205b792 0x00000000 0x3fc9dfb0 0x3fc9c1f0 0x3fc9c09c 0x3fc9c090 0x4038780c
0x4205b792 - pthread_setspecific
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/pthread/pthread_local_storage.c:230
0x4038780c - xQueueSemaphoreTake
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:1731
3fc9b4c0: 0x00000004 0x00000002 0x3fc9bdc2 0xffffffff 0x00000004 0x00000002 0x3fc9bdc2 0x3fc9bdc0
3fc9b4e0: 0x0000001c 0x4207ad84 0x3fc9bdc2 0x3fc9bdc0 0x0000001c 0x00000000 0x3fca8fa8 0x40386d10
0x4207ad84 - lwip_netconn_do_bind
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/lwip/src/api/api_msg.c:1281
0x40386d10 - prvCopyDataToQueue
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:2470
3fc9b500: 0x00000000 0x00000000 0x3fca8fa8 0x40387284 0x00000000 0x4207ad84 0x3fc9bcc8 0x00000000
0x40387284 - xQueueGenericSend
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:966
0x4207ad84 - lwip_netconn_do_bind
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/lwip/src/api/api_msg.c:1281
3fc9b520: 0x00000004 0x00000002 0x3fc9bdc2 0x3fc9bdc0 0x00000004 0x00000002 0x3fc9bdc2 0x3fc9bdc0
3fc9b540: 0x0000001c 0x00000000 0x00000000 0x420790a2 0x3c117bc4 0x3fc90035 0x3fc95dac 0x42067ce6
0x420790a2 - sys_mutex_unlock
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/port/freertos/sys_arch.c:79
0x3c117bc4 - __func__.9
    at ??:??
0x3fc95dac - s_vfs
    at ??:??
0x42067ce6 - done_socket
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/lwip/src/api/sockets.c:398
3fc9b560: 0x3fc9bde0 0x00000036 0x3fcaa780 0x00000000 0x0000001c 0x00000000 0x3fca8fa8 0x40386d10
0x40386d10 - prvCopyDataToQueue
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:2470
3fc9b580: 0x00000000 0x00000000 0x3fca8fa8 0x40386d10 0x00000000 0x00000000 0x3fca8fa8 0x40387284
0x40386d10 - prvCopyDataToQueue
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:2470
0x40387284 - xQueueGenericSend
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/freertos/FreeRTOS-Kernel/queue.c:966
3fc9b5a0: 0x3fc9bde0 0x00000036 0x00000000 0x00000000 0x3fc9c118 0x00000000 0x00000000 0x3fc9c5c8
3fc9b5c0: 0x3fc9c118 0x00000000 0x00000000 0x3fc9c5c8 0x00004002 0x00000000 0x00000000 0x420790a2
0x420790a2 - sys_mutex_unlock
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/port/freertos/sys_arch.c:79
3fc9b5e0: 0x00000000 0x00000036 0x3fc95dac 0x42067ce6 0x3fc9be10 0x00000001 0x3fc9be40 0x00000000
0x3fc95dac - s_vfs
    at ??:??
0x42067ce6 - done_socket
    at /var/home/mawoka/projects/td-free-rs/firmware/.embuild/espressif/esp-idf/v5.2.2/components/lwip/lwip/src/api/sockets.c:398
3fc9b600: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc9b620: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fc9b640: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5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T)
Saved PC:0x40381fea
main.rs ```rust #![feature(iter_intersperse)] use std::borrow::Cow; use std::net::Ipv4Addr; use std::str::FromStr; use core::time::Duration; use embedded_hal::delay::DelayNs; use esp_idf_svc::hal::sys::nvs_flash_init; use esp_idf_svc::hal::task::block_on; use esp_idf_svc::http::server::EspHttpServer; use esp_idf_svc::http::Method; use esp_idf_svc::io::Write; use esp_idf_svc::ipv4::{ ClientConfiguration as IpClientConfiguration, ClientSettings as IpClientSettings, Configuration as IpConfiguration, Mask, RouterConfiguration, Subnet, }; use esp_idf_svc::netif::{EspNetif, NetifConfiguration, NetifStack}; use esp_idf_svc::nvs::EspDefaultNvsPartition; use esp_idf_svc::timer::EspTaskTimerService; use esp_idf_svc::wifi::{ AccessPointConfiguration, AsyncWifi, BlockingWifi, Configuration, EspWifi, WifiDeviceId, WifiDriver, }; use esp_idf_svc::{ eventloop::EspSystemEventLoop, hal::{ delay::FreeRtos, i2c::{I2cConfig, I2cDriver}, peripherals::Peripherals, prelude::*, reset, }, }; use log::{info, warn}; use smol::block_on as smol_block_on; use smol::io; use smol_macros::main; // use wifi::get; mod dns; mod led; mod wifi; main! { async fn main() -> io::Result<()> { // It is necessary to call this function once. Otherwise some patches to the runtime // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71 esp_idf_svc::sys::link_patches(); // Bind the log crate to the ESP Logging facilities esp_idf_svc::log::EspLogger::initialize_default(); let peripherals = Peripherals::take().unwrap(); let sysloop = EspSystemEventLoop::take().unwrap(); // let app_config = CONFIG; let nvs = EspDefaultNvsPartition::take().unwrap(); let timer_service = EspTaskTimerService::new().unwrap(); let driver = EspWifi::new(peripherals.modem, sysloop.clone(), Some(nvs)).unwrap(); let mut wifi = AsyncWifi::wrap(driver, sysloop, timer_service).unwrap(); let stack = edge_nal_std::Stack::new(); let mut tx = [0; 1500]; let mut rx = [0; 1500]; // let http_server = block_on(start_http()).unwrap(); smol_block_on(edge_captive::io::run( &stack, edge_captive::io::DEFAULT_SOCKET, &mut tx, &mut rx, Ipv4Addr::new(192, 168, 0, 1), Duration::from_secs(60), )).unwrap(); // block_on(start_ap(&mut wifi)).unwrap(); loop { FreeRtos.delay_ms(500); } } ```
Cargo.toml ```toml [package] name = "td-free-rs" version = "0.1.0" authors = ["Mawoka "] edition = "2021" resolver = "2" rust-version = "1.77" [[bin]] name = "td-free-rs" harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors [profile.release] opt-level = "s" [profile.dev] debug = true # Symbols are nice and they don't increase the size on Flash opt-level = "z" [features] default = ["std", "embassy", "esp-idf-svc/native"] #critical-section-impl = ["critical-section/restore-state-none"] pio = ["esp-idf-svc/pio"] std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"] alloc = ["esp-idf-svc/alloc"] nightly = ["esp-idf-svc/nightly"] experimental = ["esp-idf-svc/experimental"] embassy = [ "esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver", ] [dependencies] log = { version = "0.4", default-features = false } esp-idf-svc = { version = "0.49", default-features = false, features= ["experimental"] } veml7700 = "0.2.0" embedded-hal = "1" smol = { version = "2" } smol-macros = {version = "0.1"} toml-cfg = "0.2" anyhow = "1" embedded-svc = "0.28" ws2812-esp32-rmt-driver = { version = "0.9", features = ["smart-leds-trait"] } smart-leds = "0.4" enumset = "1" heapless = "0.8" edge-captive = { version = "0.3", features = ["std", "io"] } edge-nal-std = "0.3" [build-dependencies] embuild = "0.32.0" ```

It doesn't matter whether I use smol::block_on or esp_idf_svc::hal::task::block_on

Vollbrecht commented 3 days ago

Did you increase the default stack size of your main task? You can do that by changing the according value in your sdkconfig file. Otherwise you spawn a thread that has a appropriated stack_size set on spawning with the std::thread api. Keep in mind that the default stack size is only ~3kb

mawoka-myblock commented 3 days ago

Sorry for annoying, but how do I do that, how's the parameter called?

mawoka-myblock commented 3 days ago

Oh, nevermind, it's set to CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000. I've also increased it to CONFIG_ESP_MAIN_TASK_STACK_SIZE=20000 but it didn't make any difference.

ivmarkov commented 3 days ago

Apparently you either did not increase it, or you have to increase it even more.

But the error can't be any clearer than that: Guru Meditation Error: Core 0 panic'ed (Stack protection fault)

Two more suggestions:

ivmarkov commented 3 days ago

Oh, one more thing: if you stick with the stock async-io, you need to increase the pthreads' stack sizes too, as async-io spins a hidden pthread thread too. Look at the esp-idf-template generated project for the pthread stack size conf.

mawoka-myblock commented 3 days ago

Okay, I now got this code:

    let handle = thread::Builder::new()
        .stack_size(100 * 1024)
        .spawn(move ||{smol_block_on(edge_captive::io::run(
        &stack,
        edge_captive::io::DEFAULT_SOCKET,
        &mut tx,
        &mut rx,
        Ipv4Addr::new(192, 168, 0, 1),
        Duration::from_secs(60),
    )).unwrap()}).unwrap();

To my understanding, this spawns a new thread with 100KB memory, right? If I go above the 100KB, It throws an OutOfMemory. Like this, it's still a core panic. How high does the stack size has to be? This seems to me like it's way too much memory used for the captive portal part.

ivmarkov commented 3 days ago

Oh, one more thing: if you stick with the stock async-io, you need to increase the pthreads' stack sizes too, as async-io spins a hidden pthread thread too. Look at the esp-idf-template generated project for the pthread stack size conf.

Did you also read this?

mawoka-myblock commented 3 days ago

I did, though I misread it. Turns out, setting CONFIG_PTHREAD_TASK_STACK_SIZE_DEFAULT=10000 made the code above work with adding vfs::initialize_eventfd(5).unwrap();. This is fixed, though a note about increasing the PTHREAD stack size would be awesome! Still many thanks for the lightning fast replies here!

ivmarkov commented 3 days ago

This is fixed, though a note about increasing the PTHREAD stack size would be awesome!

I'm a bit unsure how far deep the esp-rs rabbit hole we have to go in the edge-* crates. The thing is, they are supposed to work on any MCU, not just Espressif's, and not just with ESP-IDF. But maybe I can add something generic in the readme w.r.t. stack-sizes, and then refer to the ESP-IDF docu or something

Still many thanks for the lightning fast replies here!

Always welcome!