Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite PicoW LWIP interface, major stability increase #916

Merged
merged 8 commits into from
Oct 15, 2022

Commits on Oct 14, 2022

  1. Disable interrupts while in the memory manager

    Fixes random crashes while on the PicoW with WiFi.
    
    The system malloc/etc. is not re-entrant, so it is not safe to do
    any memory (de)allocation from an interrupt.
    
    The LWIP stack is configured not to use the memory manager, but it may
    end up calling callback functions in userspace which may well use
    new or malloc() (i.e. to manage a new connection info or process a
    UDP packet like in MDNS).
    
    This could cause unpredicatable crashes due to memory corruption at
    some random time after the interrupt/malloc occurred.
    
    Avoid the issue completely by locking interrupts out while in the memory
    manager.  Inter-core will be protected by the malloc_lock already in
    Newlib.
    
    This should not have any effect on absolute performance of the system.
    earlephilhower committed Oct 14, 2022
    Configuration menu
    Copy the full SHA
    4ccc0c4 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    20a3a71 View commit details
    Browse the repository at this point in the history

Commits on Oct 15, 2022

  1. Individually wrap all LWIP calls

    Brute-force protect all LWIP calls from re-rentrancy
    earlephilhower committed Oct 15, 2022
    Configuration menu
    Copy the full SHA
    b5ecb9f View commit details
    Browse the repository at this point in the history
  2. Only stuff WiFi packets if we're not in LWIP

    Was causing memory corruption during long, high frequency, multi-client
    workloads.  It is illegal to call netif->input() from an IRQ while
    in LWIP code.
    
    For now, throw them out and let upper layer handle a re-transmit.
    earlephilhower committed Oct 15, 2022
    Configuration menu
    Copy the full SHA
    84f69a8 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    57bdaac View commit details
    Browse the repository at this point in the history
  4. Restyle

    earlephilhower committed Oct 15, 2022
    Configuration menu
    Copy the full SHA
    8785bab View commit details
    Browse the repository at this point in the history
  5. Skip LWIP callbacks during ClientContext teardown

    During a ClientContext we set TCP connection arguments to nullptr and then
    turn off all the callbacks.  It is possible for an interrupt to occur
    between setting the arg nullptr and the clearing of all CBs.  The the CB
    function will get nullptr as (this), causing a crash like:
    ````
    Thread 1 received signal SIGTRAP, Trace/breakpoint trap.
    isr_hardfault () at /home/earle/Arduino/hardware/pico/rp2040/pico-sdk/src/rp2_common/pico_standard_link/crt0.S:98
    98	decl_isr_bkpt isr_hardfault
    (gdb) where
       e/Arduino/hardware/pico/rp2040/libraries/WiFi/src/include/ClientContext.h:619
       ries/WiFi/src/include/ClientContext.h:612
       raries/WiFi/src/include/ClientContext.h:671
        ) at /home/earle/Arduino/hardware/pico/rp2040/pico-sdk/lib/lwip/src/core/tcp_in.c:541
        ) at /home/earle/Arduino/hardware/pico/rp2040/pico-sdk/lib/lwip/src/core/ipv4/ip4.c:743
       rdware/pico/rp2040/pico-sdk/lib/lwip/src/netif/ethernet.c:186
        buf=0x20001b7a <cyw43_state+226> "(\315\301") at /home/earle/Arduino/hardware/pico/rp2040/libraries/lwIP_CYW43/src/utility/CYW43shim.cpp:132
       2040/pico-sdk/lib/cyw43-driver/src/cyw43_ll.c:1212
       cores/rp2040/lwip_wrap.cpp:163
       e/ClientContext.h:85
       /libraries/WiFi/src/WiFiClient.cpp:81
       /libraries/WiFi/src/WiFiClient.cpp:78
       rver.cpp:284
       ico/rp2040/libraries/WebServer/src/WebServerTemplate.h:86
    ````
    
    Avoid by checking and silently ignoring CBs who have a nullptr arg.
    earlephilhower committed Oct 15, 2022
    Configuration menu
    Copy the full SHA
    ca9a096 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    7a85036 View commit details
    Browse the repository at this point in the history