From 91b6032a71c42c9029c8b86e66aac44268d7080c Mon Sep 17 00:00:00 2001 From: Johannes Schlatow Date: Fri, 18 Feb 2022 09:26:28 +0100 Subject: [PATCH] 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 --- repos/base/include/base/trace/buffer.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/repos/base/include/base/trace/buffer.h b/repos/base/include/base/trace/buffer.h index 439d672b1d..605b11b2f9 100644 --- a/repos/base/include/base/trace/buffer.h +++ b/repos/base/include/base/trace/buffer.h @@ -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; }