From 26acc6adfde179518b38bb286f3a47931a8e067a Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Mon, 13 Dec 2021 23:40:49 -0500 Subject: [PATCH] bytevector: introduce BEES_VALGRIND to help work around valgrind valgrind doesn't understand ioctl arguments, so it does not know if or when they initialize memory, and it complains about conditionals depending on data that comes out of ioctls. That's a problem for bees, where every decision we ever make is based on data an ioctl gave us. Fix the initialization issue by using calloc instead of malloc for ByteVectors when we are building for valgrind. Don't enable this by default because all the callocs aren't necessary (assuming the rest of the code is correct) and hurt performance. Define BEES_VALGRIND in localconf to activate, e.g. echo CCFLAGS += -DBEES_VALGRIND=1 >> localconf Signed-off-by: Zygo Blaxell --- lib/bytevector.cc | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/bytevector.cc b/lib/bytevector.cc index 5424120..083aa8e 100644 --- a/lib/bytevector.cc +++ b/lib/bytevector.cc @@ -65,9 +65,21 @@ namespace crucible { return m_ptr.get()[size]; } + static + void * + bv_allocate(size_t size) + { +#ifdef BEES_VALGRIND + // XXX: only do this to shut up valgrind + return calloc(1, size); +#else + return malloc(size); +#endif + } + ByteVector::ByteVector(size_t size) { - m_ptr = Pointer(static_cast(malloc(size)), free); + m_ptr = Pointer(static_cast(bv_allocate(size)), free); // bad_alloc doesn't fit THROW_CHECK's template THROW_CHECK0(runtime_error, m_ptr); m_size = size; @@ -77,7 +89,7 @@ namespace crucible { { const size_t size = end - begin; const size_t alloc_size = max(size, min_size); - m_ptr = Pointer(static_cast(malloc(alloc_size)), free); + m_ptr = Pointer(static_cast(bv_allocate(alloc_size)), free); THROW_CHECK0(runtime_error, m_ptr); m_size = alloc_size; memcpy(m_ptr.get(), begin, size);