[Mesa-dev] [PATCH 6/9] glsl/blob: add valgrind checks that written data is defined

Nicolai Hähnle nhaehnle at gmail.com
Mon Jun 26 09:40:44 UTC 2017


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

Undefined data will eventually trigger a valgrind error while computing
its CRC32 while writing it into the disk cache, but at that point, it is
basically impossible to track down where the undefined data came from.

With this change, finding the origin of undefined data becomes easy.
---
 src/compiler/Makefile.am |  2 ++
 src/compiler/glsl/blob.c | 12 ++++++++++++
 2 files changed, 14 insertions(+)

diff --git a/src/compiler/Makefile.am b/src/compiler/Makefile.am
index d52da91..dcbd63e 100644
--- a/src/compiler/Makefile.am
+++ b/src/compiler/Makefile.am
@@ -32,24 +32,26 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir)/src/compiler/glsl\
 	-I$(top_srcdir)/src/compiler/glsl/glcpp\
 	-I$(top_builddir)/src/compiler/nir \
 	-I$(top_srcdir)/src/compiler/nir \
 	-I$(top_srcdir)/src/gallium/include \
 	-I$(top_srcdir)/src/gallium/auxiliary \
 	-I$(top_srcdir)/src/gtest/include \
 	$(DEFINES)
 
 AM_CFLAGS = \
+	$(VALGRIND_CFLAGS) \
 	$(VISIBILITY_CFLAGS) \
 	$(MSVC2013_COMPAT_CFLAGS)
 
 AM_CXXFLAGS = \
+	$(VALGRIND_CFLAGS) \
 	$(VISIBILITY_CXXFLAGS) \
 	$(MSVC2013_COMPAT_CXXFLAGS)
 
 noinst_LTLIBRARIES = libcompiler.la
 
 libcompiler_la_SOURCES = $(LIBCOMPILER_FILES)
 
 check_PROGRAMS =
 TESTS =
 BUILT_SOURCES =
diff --git a/src/compiler/glsl/blob.c b/src/compiler/glsl/blob.c
index db36252..3c4aed8 100644
--- a/src/compiler/glsl/blob.c
+++ b/src/compiler/glsl/blob.c
@@ -19,20 +19,28 @@
  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  * IN THE SOFTWARE.
  */
 
 #include <string.h>
 
 #include "main/macros.h"
 #include "blob.h"
 
+#ifdef HAVE_VALGRIND
+#include <valgrind.h>
+#include <memcheck.h>
+#define VG(x) x
+#else
+#define VG(x)
+#endif
+
 #define BLOB_INITIAL_SIZE 4096
 
 /* Ensure that \blob will be able to fit an additional object of size
  * \additional.  The growing (if any) will occur by doubling the existing
  * allocation.
  */
 static bool
 grow_to_fit(struct blob *blob, size_t additional)
 {
    size_t to_allocate;
@@ -103,31 +111,35 @@ blob_create()
 bool
 blob_overwrite_bytes(struct blob *blob,
                      size_t offset,
                      const void *bytes,
                      size_t to_write)
 {
    /* Detect an attempt to overwrite data out of bounds. */
    if (blob->size < offset + to_write)
       return false;
 
+   VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
+
    memcpy(blob->data + offset, bytes, to_write);
 
    return true;
 }
 
 bool
 blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
 {
    if (! grow_to_fit(blob, to_write))
        return false;
 
+   VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
+
    memcpy(blob->data + blob->size, bytes, to_write);
    blob->size += to_write;
 
    return true;
 }
 
 uint8_t *
 blob_reserve_bytes(struct blob *blob, size_t to_write)
 {
    uint8_t *ret;
-- 
2.9.3



More information about the mesa-dev mailing list