Arduino 2.2.1 unexpected function calling for lolin32 lite

Hello, I have made many apps on Arduino Uno for my projects, always with a smooth experience.
I have written a few for various ESP32 boards as well.
I would like to report a problem that I am having with a new app for the lolin32 lite board.
Hopefully, someone can indicate where I go wrong, doing something that I shouldn't,
or not doing something I should.

The symptoms seem to indicate that the call stack is being mistreated somehow.
I have simplified the code as much as I could, in an attempt to figure out myself what is going on.
Now it contains only a few functions, uses no libraries, and does nothing except print debug info.
Here it is:

uint16_t ReadWord()
{
  Serial.println("ReadWord exec");
}

void Dummy(uint16_t k)
{
  Serial.printf("Dummy! %i\n",k);
  
  if (k==20) return;

  while(true);
}

void ReadBlock()
{
  Serial.println("ReadWord call");
  ReadWord();
  Serial.println("ReadWord return");
}

void setup() 
{
  Serial.begin(115200);
//  Dummy(20);
}

void loop() 
{
  Serial.println("loop entry");
  ReadBlock();
  Serial.println("loop exit");
}

The code compiles without a warning, and after uploading (no problems there either),
the output is:

17:13:58.961 -> ets Jun  8 2016 00:22:57
17:13:58.961 -> 
17:13:58.961 -> rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
17:13:58.961 -> configsip: 0, SPIWP:0xee
17:13:58.961 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
17:13:58.961 -> mode:DIO, clock div:1
17:13:58.961 -> load:0x3fff0030,len:1184
17:13:58.961 -> load:0x40078000,len:13260
17:13:58.990 -> load:0x40080400,len:3028
17:13:58.990 -> entry 0x400805e4
17:13:59.119 -> loop entry
17:13:59.119 -> ReadWord call
17:13:59.119 -> ReadWord exec
17:13:59.119 -> ReadWord call
17:13:59.119 -> ReadWord exec
17:13:59.119 -> ReadWord call
17:13:59.119 -> ReadWord exec
17:13:59.119 -> ReadWord call
......
17:13:59.440 -> ReadWord call
17:13:59.440 -> ReadWord exec
Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception). 
17:13:59.440 -> Debug exception reason: Stack canary watchpoint triggered (loopTask) 
17:13:59.440 -> Core  1 register dump:
17:13:59.440 -> PC      : 0x40087f68  PS      : 0x00060036  A0      : 0x4008458e  A1      : 0x3ffc3210  
17:13:59.440 -> A2      : 0x00000001  A3      : 0xffffffff  A4      : 0x3ffc5530  A5      : 0x3ffc54f4  
17:13:59.473 -> A6      : 0x3ffc5508  A7      : 0x00000000  A8      : 0x8008328c  A9      : 0x3ffc32b0  
17:13:59.473 -> A10     : 0x3ff000e0  A11     : 0x00000001  A12     : 0x3ffbdc78  A13     : 0x00060023  
17:13:59.473 -> A14     : 0x00060023  A15     : 0x00000000  SAR     : 0x00000003  EXCCAUSE: 0x00000001  
17:13:59.473 -> EXCVADDR: 0x00000000  LBEG    : 0x400862d9  LEND    : 0x400862e9  LCOUNT  : 0xffffffff  
17:13:59.505 -> 
17:13:59.505 -> 
17:13:59.505 -> Backtrace: 0x40087f65:0x3ffc3210 0x4008458b:0x3ffc32f0 0x400d362d:0x3ffc3330 0x400d3df9:0x3ffc3380 0x400d2084:0x3ffc33b0 0x400d1491:0x3ffc33d0 0x400d180b:0x3ffc33f0 0x400d181d:0x3ffc3410 0x400d1839:0x3ffc3430 0x400d131d:0x3ffc3450 0x400d132c:0x3ffc3470 0x400d131d:0x3ffc3490 0x400d132c:0x3ffc34b0 0x400d131d:0x3ffc34d0 0x400d132c:0x3ffc34f0 0x400d131d:0x3ffc3510 0x400d132c:0x3ffc3530 0x400d131d:0x3ffc3550 0x400d132c:0x3ffc3570 0x400d131d:0x3ffc3590 0x400d132c:0x3ffc35b0 0x400d131d:0x3ffc35d0 0x400d132c:0x3ffc35f0 0x400d131d:0x3ffc3610 0x400d132c:0x3ffc3630 0x400d131d:0x3ffc3650 0x400d132c:0x3ffc3670 0x400d131d:0x3ffc3690 0x400d132c:0x3ffc36b0 0x400d131d:0x3ffc36d0 0x400d132c:0x3ffc36f0 0x400d131d:0x3ffc3710 0x400d132c:0x3ffc3730 0x400d131d:0x3ffc3750 0x400d132c:0x3ffc3770 0x400d131d:0x3ffc3790 0x400d132c:0x3ffc37b0 0x400d131d:0x3ffc37d0 0x400d132c:0x3ffc37f0 0x400d131d:0x3ffc3810 0x400d132c:0x3ffc3830 0x400d131d:0x3ffc3850 0x400d132c:0x3ffc3870 0x400d131d:0x3ffc3890 0x400d132c:0x3ffc38b0 0x400d131d:0x3ffc38d0 0x400d132c:0x3ffc38f0 0x400d131d:0x3ffc3910 0x400d132c:0x3ffc3930 0x400d131d:0x3ffc3950 0x400d132c:0x3ffc3970 0x400d131d:0x3ffc3990 0x400d132c:0x3ffc39b0 0x400d131d:0x3ffc39d0 0x400d132c:0x3ffc39f0 0x400d131d:0x3ffc3a10 0x400d132c:0x3ffc3a30 0x400d131d:0x3ffc3a50 0x400d132c:0x3ffc3a70 0x400d131d:0x3ffc3a90 0x400d132c:0x3ffc3ab0 0x400d131d:0x3ffc3ad0 0x400d132c:0x3ffc3af0 0x400d131d:0x3ffc3b10 0x400d132c:0x3ffc3b30 0x400d131d:0x3ffc3b50 0x400d132c:0x3ffc3b70 0x400d131d:0x3ffc3b90 0x400d132c:0x3ffc3bb0 0x400d131d:0x3ffc3bd0 0x400d132c:0x3ffc3bf0 0x400d131d:0x3ffc3c10 0x400d132c:0x3ffc3c30 0x400d131d:0x3ffc3c50 0x400d132c:0x3ffc3c70 0x400d131d:0x3ffc3c90 0x400d132c:0x3ffc3cb0 0x400d131d:0x3ffc3cd0 0x400d132c:0x3ffc3cf0 0x400d131d:0x3ffc3d10 0x400d132c:0x3ffc3d30 0x400d131d:0x3ffc3d50 0x400d132c:0x3ffc3d70 0x400d131d:0x3ffc3d90 0x400d132c:0x3ffc3db0 0x400d131d:0x3ffc3dd0 0x400d132c:0x3ffc3df0 0x400d131d:0x3ffc3e10 0x400d132c:0x3ffc3e30 0x400d131d:0x3ffc3e50 0x400d132c:0x3ffc3e70 0x400d131d:0x3ffc3e90 0x400d132c:0x3ffc3eb0 0x400d131d:0x3ffc3ed0 0x400d132c:0x3ffc3ef0 0x400d131d:0x3ffc3f10 0x400d132c:0x3ffc3f30 0x400d131d:0x3ffc3f50 0x400d132c:0x3ffc3f70 0x400d131d:0x3ffc3f90 0x400d132c:0x3ffc3fb0 |<-CONTINUES
17:13:59.699 -> 
17:13:59.699 -> 
17:13:59.699 -> 
17:13:59.699 -> 
17:13:59.699 -> ELF file SHA256: 68b8bfc699fa330e
17:13:59.699 -> 
17:13:59.860 -> Rebooting...

And after rebooting, this process repeats. It is clear from the output
that the ReadWord() function is called in a way that is not intended by the program.

Note that in the setup() function, there is one line in comment, a simple call to Dummy().
When that line is uncommented, the output changes drastically:

17:21:21.810 -> ets Jun  8 2016 00:22:57
17:21:21.810 -> 
17:21:21.810 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
17:21:21.810 -> configsip: 0, SPIWP:0xee
17:21:21.810 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
17:21:21.889 -> mode:DIO, clock div:1
17:21:21.889 -> load:0x3fff0030,len:1184
17:21:21.889 -> load:0x40078000,len:13260
17:21:21.889 -> load:0x40080400,len:3028
17:21:21.889 -> entry 0x400805e4
17:21:21.971 -> Dummy! 20
17:21:21.971 -> loop entry
17:21:21.971 -> ReadWord call
17:21:21.971 -> ReadWord exec
17:21:21.971 -> Dummy! 15

Note that this time, the execution hangs in the (intended infinite) while loop in Dummy(),
because the argument to the function is not as expected: nowhere in the code
Dummy() is called with argument 15, as the output states.

Combining the results from these two variants, It seems like there is a fall-through
from ReadWord() into the next function, without that next function being called.
(In the first variant, the Dummy() function is never called, and presumably is not
included in the executable.) But that is all I can glean from the outputs.

Can anyone explain this behavior please?
Many thanks in advance!

I'm not a ESP32 user but the following stands out; it might not be the cause of the problem but I'm surprised that you do not get at least a warning (maybe you have to crank up the warning level in file → preferences).

You promise to return an uint16_t but you don't.

Ah, that is a left-over from the original code, where the function did do something useful.
I'll make the function void, and see if it makes a change in behavior. Thanks!

And now it works! Now I can go back to debugging my original program.
Again many thanks!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.