I see. I think I probably had the wrong expectations about the stack overflow detection in uLisp. If I’m reading you correctly, it isn’t necessarily intended to guarantee that the system is left in a fully intact state, but is meant as more of a “best effort” check to make the error condition clear to the user, as well as prevent data loss in some cases?
I would say that stack overflows are probably uniquely a problem only when using uLisp as an interactive development environment (such as one might do on something like the PicoCalc). I did run into this issue organically while writing some code, because I had made a mistake in the base case of a recursive function. I kept working on the problem for a bit, but then later found that I couldn’t save my program to the SD card anymore. To be fair, pprint was still working, so I could have copied it out over the serial port, or copied the program to some other medium manually.
I think it matters much less when pre-writing software and then using uLisp as just a runtime for running it on a microcontroller.
I was also under the impression that there wouldn’t be any heap / dynamic memory allocation when running uLisp, but I think - even though uLisp itself doesn’t make any heap memory allocations at runtime, some of the libraries that are used on the more powerful controllers, such as the Pi Pico (or Pico 2), do still utilize the heap (such as the filesystem implementation for SD card support, and it seems also some of the GFX routines). At least that’s the only explanation I can think of that would be consistent with what I’ve observed when I write close to (but not below) ENDSTACK.
Either way, I’m perfectly happy with just setting STACKDIFF to something generous (like 2 or 4 KB) for my personal use. This gives me the additional bit of safety, and the space left for the stack on the Pi Pico 2 that I’m using is way more than I’d ever need for typical programs anyway.
Happy to close this thread.
(fwiw, I think if we wanted to have a more general solution for this problem, we could check if there’s a way to limit the heap allocator used by the RP2040 Arduino SDK to a maximum heap size - then we could use that limit to have a well defined upper bound of the heap address space, and could determine ENDSTACK accordingly…)