pellepl / spiffs

Wear-leveled SPI flash file system for embedded devices
MIT License
1.52k stars 402 forks source link

Forcing garbage collection by creating files result in NMI handler, fixable by making some fields volatile #291

Open basprins opened 2 years ago

basprins commented 2 years ago

Hi, I ran into some strange issue, which I managed to reproduce by creating a lot of file entries (by overwriting the same one over and over again). At some point the garbage collector will kick in, and call the erase callback. That call fails, and my program jumps to the NMI handler straight away.

Debugging and many attempts further, I found out that it only happens when I run my optimized build. After some struggles forcing GDB to do what I want, I managed to located the function where the pointer fs->cfg gets corrupted. The function pointers somehow end up pointing to garbage (while the memory which is allocated by the spiffs member variable still holds the correct addresses of the function pointers).

The function which breaks stuff is spiffs_gc_find_candidate. So I cheated my way out, I made all stack variables which are used in the while loops volatile, and then the problem disappears. Now I can execute my for loop of 1000 file (re)creates, and the garbage collector kicks in many times, without mangling pointers.

Not sure if this is something you want to further investigate or maybe fix, but thought to at least drop you a message.

. image

image

image

I am a huge fan of your library still, it saved me a great deal of hard work! Keep up the good work :)