Skip to content

Commit 6750f4a

Browse files
ljd42cfriedt
authored andcommitted
doc: services: logging: add limitation when using logging thread name
Add a note about a limitation when using deferred logging + thread name together with dynamically allocated struct k_thread. This limitation has been raised in issue #95077. Signed-off-by: Loic Domaigne <tech@domaigne.com>
1 parent 1b09500 commit 6750f4a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/services/logging/index.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,26 @@ There are following limitations:
887887

888888
* Logging does not support string format specifier with width (e.g., ``%.*s`` or ``%8s``). That
889889
is because format string content is not used to build a log message, only argument types.
890+
* If deferred logging is used and log messages are prefixed with the thread name
891+
(Kconfig option ``CONFIG_LOG_THREAD_ID_PREFIX=y`` and ``CONFIG_THREAD_NAME=y``), it is assumed that
892+
the corresponding :c:struct:`k_thread` structure is still valid when the log message is
893+
formatted. This can be an issue when that structure is allocated dynamically, using :c:func:`k_malloc` or
894+
:c:func:`malloc` for instance. In this case, if the thread logs some messages and then gets
895+
stopped and its ``struct k_thread`` is freed, the log system will still try to access that
896+
structure when handling the message later. This creates a use-after-free scenario.
897+
To avoid this, a solution consists in calling :c:func:`log_flush` before freeing the structure.
898+
899+
.. code-block:: c
900+
901+
struct k_thread *thread = k_malloc(sizeof(*thread)); /* struct allocated dynamically */
902+
k_thread_create(thread, ...);
903+
k_thread_name_set(thread, "foobar");
904+
905+
/* Thread calls LOG_*(...) */
906+
907+
k_thread_join(thread, K_FOREVER);
908+
log_flush(); /* flush log buffer before freeing the struct k_thread */
909+
k_free(thread); /* avoid a potential use-after-free scenario if deferred logging is used */
890910
891911
Benchmark
892912
*********

0 commit comments

Comments
 (0)