Valgrind: suppress warnings with MemPool & MEM_* trailing padding

Suppress false positive Valgrind warnings which flooded the output.

- BLI_mempool alloc/free & iteration.
- Set alignment padding bytes at the end of MEM_* allocations
  as "defined" since this causes many false positive warnings
  in blend file writing and MEMFILE comparisons.
- Set MEM_* allocations as undefined when `--debug-memory`
  is passed in to account for debug initialization.
- Initialize pad bytes in TextLine allocations.
This commit is contained in:
Campbell Barton
2023-12-05 16:31:58 +11:00
parent d48b9d32c6
commit 20fd012adb
5 changed files with 164 additions and 27 deletions

View File

@@ -6,6 +6,10 @@ if(HAVE_MALLOC_STATS_H)
add_definitions(-DHAVE_MALLOC_STATS_H)
endif()
if(WITH_MEM_VALGRIND)
add_definitions(-DWITH_MEM_VALGRIND)
endif()
set(INC
PUBLIC .
)

View File

@@ -19,6 +19,12 @@
#include "MEM_guardedalloc.h"
/* Quiet warnings when dealing with allocated data written into the blend file.
* This also rounds up and causes warnings which we don't consider bugs in practice. */
#ifdef WITH_MEM_VALGRIND
# include "valgrind/memcheck.h"
#endif
/* to ensure strict conversions */
#include "../../source/blender/blenlib/BLI_strict_flags.h"
@@ -445,14 +451,28 @@ void *MEM_guarded_mallocN(size_t len, const char *str)
{
MemHead *memh;
#ifdef WITH_MEM_VALGRIND
const size_t len_unaligned = len;
#endif
len = SIZET_ALIGN_4(len);
memh = (MemHead *)malloc(len + sizeof(MemHead) + sizeof(MemTail));
if (LIKELY(memh)) {
make_memhead_header(memh, len, str);
if (UNLIKELY(malloc_debug_memset && len)) {
memset(memh + 1, 255, len);
if (LIKELY(len)) {
if (UNLIKELY(malloc_debug_memset)) {
memset(memh + 1, 255, len);
}
#ifdef WITH_MEM_VALGRIND
if (malloc_debug_memset) {
VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
}
else {
VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
}
#endif /* WITH_MEM_VALGRIND */
}
#ifdef DEBUG_MEMCOUNTER
@@ -510,6 +530,9 @@ void *MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str)
*/
assert(alignment < 1024);
#ifdef WITH_MEM_VALGRIND
const size_t len_unaligned = len;
#endif
len = SIZET_ALIGN_4(len);
MemHead *memh = (MemHead *)aligned_malloc(
@@ -524,8 +547,18 @@ void *MEM_guarded_mallocN_aligned(size_t len, size_t alignment, const char *str)
make_memhead_header(memh, len, str);
memh->alignment = (short)alignment;
if (UNLIKELY(malloc_debug_memset && len)) {
memset(memh + 1, 255, len);
if (LIKELY(len)) {
if (UNLIKELY(malloc_debug_memset)) {
memset(memh + 1, 255, len);
}
#ifdef WITH_MEM_VALGRIND
if (malloc_debug_memset) {
VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
}
else {
VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
}
#endif /* WITH_MEM_VALGRIND */
}
#ifdef DEBUG_MEMCOUNTER

View File

@@ -16,6 +16,12 @@
#include "MEM_guardedalloc.h"
/* Quiet warnings when dealing with allocated data written into the blend file.
* This also rounds up and causes warnings which we don't consider bugs in practice. */
#ifdef WITH_MEM_VALGRIND
# include "valgrind/memcheck.h"
#endif
/* to ensure strict conversions */
#include "../../source/blender/blenlib/BLI_strict_flags.h"
@@ -244,13 +250,27 @@ void *MEM_lockfree_mallocN(size_t len, const char *str)
{
MemHead *memh;
#ifdef WITH_MEM_VALGRIND
const size_t len_unaligned = len;
#endif
len = SIZET_ALIGN_4(len);
memh = (MemHead *)malloc(len + sizeof(MemHead));
if (LIKELY(memh)) {
if (UNLIKELY(malloc_debug_memset && len)) {
memset(memh + 1, 255, len);
if (LIKELY(len)) {
if (UNLIKELY(malloc_debug_memset)) {
memset(memh + 1, 255, len);
}
#ifdef WITH_MEM_VALGRIND
if (malloc_debug_memset) {
VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
}
else {
VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
}
#endif /* WITH_MEM_VALGRIND */
}
memh->len = len;
@@ -305,6 +325,9 @@ void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str
*/
size_t extra_padding = MEMHEAD_ALIGN_PADDING(alignment);
#ifdef WITH_MEM_VALGRIND
const size_t len_unaligned = len;
#endif
len = SIZET_ALIGN_4(len);
MemHeadAligned *memh = (MemHeadAligned *)aligned_malloc(
@@ -317,8 +340,18 @@ void *MEM_lockfree_mallocN_aligned(size_t len, size_t alignment, const char *str
*/
memh = (MemHeadAligned *)((char *)memh + extra_padding);
if (UNLIKELY(malloc_debug_memset && len)) {
memset(memh + 1, 255, len);
if (LIKELY(len)) {
if (UNLIKELY(malloc_debug_memset)) {
memset(memh + 1, 255, len);
}
#ifdef WITH_MEM_VALGRIND
if (malloc_debug_memset) {
VALGRIND_MAKE_MEM_UNDEFINED(memh + 1, len_unaligned);
}
else {
VALGRIND_MAKE_MEM_DEFINED((const char *)(memh + 1) + len_unaligned, len - len_unaligned);
}
#endif /* WITH_MEM_VALGRIND */
}
memh->len = len | (size_t)MEMHEAD_ALIGN_FLAG;