RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

Message Queue (cib) breaks on write counter overflow

Open mlohse opened this issue 9 years ago • 4 comments

After running for ~4days a thread reproducibly keeps loosing messages sent from an interrupt handler ('msg_send_int') and from itself. When debugging I noticed, that a message comes through when the target thread was waiting in 'msg_receive' (state 'STATUS_RECEIVE_BLOCKED'). However, when it was to be queued (e.g. because the thread was already active) it gets lost.

Digging deeper I found that the "Circular integer buffer interface" (core/include/cib.h) which is used by the message queue does not handle integer overflows correctly. Write and read counters are incremented but never decremented or reset to 0. So it is just a matter of time until they overflow. The overflow itself is not a problem if handled correctly, like in 'cib_avail'. However 'cib_get' and 'cib_peek' are using a simple "greater then"-comparison instead of calling 'cib_avail', so the buffer breaks when 'write_count' overflows:

  1. Consider the following state for cib_t (buffer is empty, size = 4): read_count = 0xFFFFFFFF write_count=0xFFFFFFFF mask = 0x00000003

  2. Call 'cib_put' -> returns write index: 0x00000003 (now 'write_count' overflows to 0)

  3. Call 'cib_get' -> returns read index: -1 (expected: 0x00000003) Now the buffer is broken :(

Suggested fix: Replace if (cib->write_count > cib->read_count) { with if (cib_avail(cib)) { in both 'cib_get' and 'cib_peek'.

mlohse avatar Nov 22 '17 16:11 mlohse

nice catch!

kaspar030 avatar Nov 22 '17 16:11 kaspar030

@mlohse I guess it takes 4 days to verify that #8119 solves the issue? ;)

kaspar030 avatar Nov 22 '17 16:11 kaspar030

@kaspar030 Testing for this particular bug on a release-build will take 5 days minimum + another 3 weeks to run a full regression test with different hardware configurations - if everything goes smooth. I'll keep you posted.... ;)

mlohse avatar Nov 22 '17 21:11 mlohse

Ok, tests against this bug have run through successfully. Regression tests are still in progress, but I doubt that there will be any side-effects... fixed for me :)

mlohse avatar Dec 05 '17 14:12 mlohse