trace_buffer: eliminate race

Since the head of the buffer is marked by a zero-length entry, we must
only write the length field if a new head was set. Otherwise, the
consumer might already read the new entry and not find the new head as a stop
condition.

genodelabs/genode#4430
This commit is contained in:
Johannes Schlatow 2022-02-18 09:26:28 +01:00 committed by Christian Helmuth
parent b57ccf3517
commit 91b6032a71

View File

@ -93,7 +93,11 @@ class Genode::Trace::Buffer
if (len == 0)
return;
_head_entry()->len = len;
/**
* remember current length field so that we can write it after we set
* the new head
*/
size_t *old_head_len = &_head_entry()->len;
/* advance head offset, wrap when reaching buffer boundary */
_head_offset += sizeof(_Entry) + len;
@ -103,6 +107,8 @@ class Genode::Trace::Buffer
/* mark entry next to new entry with len 0 */
else if (_head_offset + sizeof(_Entry) <= _size)
_head_entry()->len = 0;
*old_head_len = len;
}
size_t wrapped() const { return _wrapped; }