trace: Correct transfer box size calculation.
authorJose Fonseca <jfonseca@vmware.com>
Mon, 24 Jul 2017 13:20:03 +0000 (14:20 +0100)
committerJose Fonseca <jfonseca@vmware.com>
Tue, 25 Jul 2017 16:18:04 +0000 (17:18 +0100)
For textures we must not approximate the calculation with `stride *
height`, or `slice_stride * depth`, as that can easily lead to buffer
overflows, particularly for partial transfers.

This should address the issue that Bruce Cherniak found and diagnosed.

Reviewed-by: Roland Scheidegger <sroland@vmware.com>
src/gallium/drivers/trace/tr_dump.c

index 78c72492dca38ccc63a273170f8c2b80d9e101ea..2003222cc1cf6af7e19c458de1ef6929174e3eeb 100644 (file)
@@ -448,23 +448,22 @@ void trace_dump_box_bytes(const void *data,
                          unsigned stride,
                          unsigned slice_stride)
 {
+   enum pipe_format format = resource->format;
    size_t size;
 
+   assert(box->height > 0);
+   assert(box->depth > 0);
+
+   size =  util_format_get_nblocksx(format, box->width )      * util_format_get_blocksize(format)
+        + (util_format_get_nblocksy(format, box->height) - 1) * stride
+        +                                  (box->depth   - 1) * slice_stride;
+
    /*
     * Only dump buffer transfers to avoid huge files.
     * TODO: Make this run-time configurable
     */
    if (resource->target != PIPE_BUFFER) {
       size = 0;
-   } else {
-      enum pipe_format format = resource->format;
-      if (slice_stride)
-         size = box->depth * slice_stride;
-      else if (stride)
-         size = util_format_get_nblocksy(format, box->height) * stride;
-      else {
-         size = util_format_get_nblocksx(format, box->width) * util_format_get_blocksize(format);
-      }
    }
 
    trace_dump_bytes(data, size);