crownstone / bluenet

Bluenet is the in-house firmware on Crownstone hardware. Functions: switching, dimming, energy monitoring, presence detection, indoor localization, switchcraft.
https://crownstone.rocks
91 stars 62 forks source link

Bug: microapp & meshing has memory issues #104

Closed mrquincle closed 3 years ago

mrquincle commented 3 years ago

Describe the bug With both meshing and microapp support enabled, there are a lot of invalid commands coming from the microapp.

[croapp/cs_MicroappProtocol.cpp : 219 ] Unknown command 9 of length 15: [0x9 0x3 0x1 0x0 ...,...]   

To Reproduce First run with BUILD_MESHING=0 and you will see proper microapp behaviour. Then run with BUILD_MESHING=1.

Additional context Something is overwriting something. To debug this it is important to get some more grip on the memory layout. The following are the default configuration parameters for the microapp.

# The start of microapps in flash
FLASH_MICROAPP_BASE=0x68000

# The number of pages for microapps (each page is 0x1000 / 4096 bytes)
FLASH_MICROAPP_PAGES=2

# The start of where the microapp is allowed to put its memory (start of its .bss .data and then the heap)
RAM_MICROAPP_BASE=0x2000B000

# The amount of RAM it is allowed to use (stack grows down from RAM_MICROAPP_BASE + RAM_MICROAPP_AMOUNT)
RAM_MICROAPP_AMOUNT=0x2000

If we look at the output we'll find this:

[t/source/src/cs_Crownstone.cpp : 920 ] heapEnd=0x2000B1B8 maxHeapEnd=0x2000B1B8 minStackEnd=0x2000CC48 minFree=6800 sb0

Hence, it looks like the heap (which is growing upwards) overflows into the memory for the microapp.

mrquincle commented 3 years ago

Adjust https://github.com/crownstone/bluenet/blob/master/docs/FLASH_MEMORY_LAYOUT.md so it contains some info on intended RAM usage.

mrquincle commented 3 years ago

Added some warnings to commit in https://github.com/crownstone/bluenet/commit/81c6210b7632d8895f5c61244c6e3b1aaf9f6739 when heap or stack goes beyond microapp settings.

mrquincle commented 3 years ago

Also added some compilation checks on flash in https://github.com/crownstone/bluenet/commit/81c6210b7632d8895f5c61244c6e3b1aaf9f6739. Will close this for now.