1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

core/thread: "fix" valgrind erros in thread_measure_stack_free()

The dark magic used used in thread_measure_stack_free() is frowned upon
by valgrind. E.g. valgrind may deduce (by monitoring the stack pointer)
that a specific value was at some point allocated on the stack, but has
gone out of scope. When that value is now read again to estimate stack
usage, it does look a lot like someone passed a pointer to a stack
allocated value, and that pointer is referenced after that value has
gone out of scope.

This is "fixed" by temporarily disabling valgrind error reporting while
iterating over the stack.
This commit is contained in:
Marian Buschsieweke 2024-06-04 20:21:45 +02:00
parent 571754fcd9
commit 11e273c4db
No known key found for this signature in database
GPG Key ID: 77AA882EC78084E6

View File

@ -35,6 +35,15 @@
#define ENABLE_DEBUG 0
#include "debug.h"
#if defined(HAVE_VALGRIND_H)
# include <valgrind.h>
#elif defined(HAVE_VALGRIND_VALGRIND_H)
# include <valgrind/valgrind.h>
#else
# define VALGRIND_DISABLE_ERROR_REPORTING (void)0
# define VALGRIND_ENABLE_ERROR_REPORTING (void)0
#endif
thread_status_t thread_getstatus(kernel_pid_t pid)
{
thread_t *thread = thread_get(pid);
@ -178,11 +187,23 @@ uintptr_t measure_stack_free_internal(const char *stack, size_t size)
uintptr_t *stackp = (uintptr_t *)(uintptr_t)stack;
uintptr_t end = (uintptr_t)stack + size;
/* HACK: This will affect native/native64 only.
*
* The dark magic used here is frowned upon by valgrind. E.g. valgrind may
* deduce that a specific value was at some point allocated on the stack,
* but has gone out of scope. When that value is now read again to
* estimate stack usage, it does look a lot like someone passed a pointer
* to a stack allocated value, and that pointer is referenced after that
* value has gone out of scope. */
VALGRIND_DISABLE_ERROR_REPORTING;
/* assume that the stack grows "downwards" */
while (((uintptr_t)stackp < end) && (*stackp == (uintptr_t)stackp)) {
stackp++;
}
VALGRIND_ENABLE_ERROR_REPORTING;
uintptr_t space_free = (uintptr_t)stackp - (uintptr_t)stack;
return space_free;