From: Jason Ekstrand Date: Wed, 18 Sep 2019 19:32:00 +0000 (-0500) Subject: Move blob from compiler/ to util/ X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=0c4e89ad5b9cc9a3e2afdab86602f643e69e9412;p=mesa.git Move blob from compiler/ to util/ There's nothing whatsoever compiler-specific about it other than that's currently where it's used. Reviewed-by: Kristian H. Kristensen Reviewed-by: Lionel Landwerlin --- diff --git a/src/compiler/Makefile.sources b/src/compiler/Makefile.sources index ac6c0670444..c4d2c2be7cb 100644 --- a/src/compiler/Makefile.sources +++ b/src/compiler/Makefile.sources @@ -1,6 +1,4 @@ LIBCOMPILER_FILES = \ - blob.c \ - blob.h \ builtin_type_macros.h \ glsl_types.cpp \ glsl_types.h \ diff --git a/src/compiler/blob.c b/src/compiler/blob.c deleted file mode 100644 index c89092e1cf3..00000000000 --- a/src/compiler/blob.c +++ /dev/null @@ -1,391 +0,0 @@ -/* - * Copyright © 2014 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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 - -#include "main/macros.h" -#include "blob.h" - -#ifdef HAVE_VALGRIND -#include -#include -#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; - uint8_t *new_data; - - if (blob->out_of_memory) - return false; - - if (blob->size + additional <= blob->allocated) - return true; - - if (blob->fixed_allocation) { - blob->out_of_memory = true; - return false; - } - - if (blob->allocated == 0) - to_allocate = BLOB_INITIAL_SIZE; - else - to_allocate = blob->allocated * 2; - - to_allocate = MAX2(to_allocate, blob->allocated + additional); - - new_data = realloc(blob->data, to_allocate); - if (new_data == NULL) { - blob->out_of_memory = true; - return false; - } - - blob->data = new_data; - blob->allocated = to_allocate; - - return true; -} - -/* Align the blob->size so that reading or writing a value at (blob->data + - * blob->size) will result in an access aligned to a granularity of \alignment - * bytes. - * - * \return True unless allocation fails - */ -static bool -align_blob(struct blob *blob, size_t alignment) -{ - const size_t new_size = ALIGN(blob->size, alignment); - - if (blob->size < new_size) { - if (!grow_to_fit(blob, new_size - blob->size)) - return false; - - if (blob->data) - memset(blob->data + blob->size, 0, new_size - blob->size); - blob->size = new_size; - } - - return true; -} - -static void -align_blob_reader(struct blob_reader *blob, size_t alignment) -{ - blob->current = blob->data + ALIGN(blob->current - blob->data, alignment); -} - -void -blob_init(struct blob *blob) -{ - blob->data = NULL; - blob->allocated = 0; - blob->size = 0; - blob->fixed_allocation = false; - blob->out_of_memory = false; -} - -void -blob_init_fixed(struct blob *blob, void *data, size_t size) -{ - blob->data = data; - blob->allocated = size; - blob->size = 0; - blob->fixed_allocation = true; - blob->out_of_memory = false; -} - -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 (offset + to_write < offset || blob->size < offset + to_write) - return false; - - VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); - - if (blob->data) - 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)); - - if (blob->data) - memcpy(blob->data + blob->size, bytes, to_write); - blob->size += to_write; - - return true; -} - -intptr_t -blob_reserve_bytes(struct blob *blob, size_t to_write) -{ - intptr_t ret; - - if (! grow_to_fit (blob, to_write)) - return -1; - - ret = blob->size; - blob->size += to_write; - - return ret; -} - -intptr_t -blob_reserve_uint32(struct blob *blob) -{ - align_blob(blob, sizeof(uint32_t)); - return blob_reserve_bytes(blob, sizeof(uint32_t)); -} - -intptr_t -blob_reserve_intptr(struct blob *blob) -{ - align_blob(blob, sizeof(intptr_t)); - return blob_reserve_bytes(blob, sizeof(intptr_t)); -} - -bool -blob_write_uint32(struct blob *blob, uint32_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -#define ASSERT_ALIGNED(_offset, _align) \ - assert(ALIGN((_offset), (_align)) == (_offset)) - -bool -blob_overwrite_uint32 (struct blob *blob, - size_t offset, - uint32_t value) -{ - ASSERT_ALIGNED(offset, sizeof(value)); - return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); -} - -bool -blob_write_uint64(struct blob *blob, uint64_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -bool -blob_write_intptr(struct blob *blob, intptr_t value) -{ - align_blob(blob, sizeof(value)); - - return blob_write_bytes(blob, &value, sizeof(value)); -} - -bool -blob_overwrite_intptr (struct blob *blob, - size_t offset, - intptr_t value) -{ - ASSERT_ALIGNED(offset, sizeof(value)); - return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); -} - -bool -blob_write_string(struct blob *blob, const char *str) -{ - return blob_write_bytes(blob, str, strlen(str) + 1); -} - -void -blob_reader_init(struct blob_reader *blob, const void *data, size_t size) -{ - blob->data = data; - blob->end = blob->data + size; - blob->current = data; - blob->overrun = false; -} - -/* Check that an object of size \size can be read from this blob. - * - * If not, set blob->overrun to indicate that we attempted to read too far. - */ -static bool -ensure_can_read(struct blob_reader *blob, size_t size) -{ - if (blob->overrun) - return false; - - if (blob->current <= blob->end && blob->end - blob->current >= size) - return true; - - blob->overrun = true; - - return false; -} - -const void * -blob_read_bytes(struct blob_reader *blob, size_t size) -{ - const void *ret; - - if (! ensure_can_read (blob, size)) - return NULL; - - ret = blob->current; - - blob->current += size; - - return ret; -} - -void -blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size) -{ - const void *bytes; - - bytes = blob_read_bytes(blob, size); - if (bytes == NULL) - return; - - memcpy(dest, bytes, size); -} - -void -blob_skip_bytes(struct blob_reader *blob, size_t size) -{ - if (ensure_can_read (blob, size)) - blob->current += size; -} - -/* These next three read functions have identical form. If we add any beyond - * these first three we should probably switch to generating these with a - * preprocessor macro. -*/ -uint32_t -blob_read_uint32(struct blob_reader *blob) -{ - uint32_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((uint32_t*) blob->current); - - blob->current += size; - - return ret; -} - -uint64_t -blob_read_uint64(struct blob_reader *blob) -{ - uint64_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((uint64_t*) blob->current); - - blob->current += size; - - return ret; -} - -intptr_t -blob_read_intptr(struct blob_reader *blob) -{ - intptr_t ret; - int size = sizeof(ret); - - align_blob_reader(blob, size); - - if (! ensure_can_read(blob, size)) - return 0; - - ret = *((intptr_t *) blob->current); - - blob->current += size; - - return ret; -} - -char * -blob_read_string(struct blob_reader *blob) -{ - int size; - char *ret; - uint8_t *nul; - - /* If we're already at the end, then this is an overrun. */ - if (blob->current >= blob->end) { - blob->overrun = true; - return NULL; - } - - /* Similarly, if there is no zero byte in the data remaining in this blob, - * we also consider that an overrun. - */ - nul = memchr(blob->current, 0, blob->end - blob->current); - - if (nul == NULL) { - blob->overrun = true; - return NULL; - } - - size = nul - blob->current + 1; - - assert(ensure_can_read(blob, size)); - - ret = (char *) blob->current; - - blob->current += size; - - return ret; -} diff --git a/src/compiler/blob.h b/src/compiler/blob.h deleted file mode 100644 index b56fa4b2fe0..00000000000 --- a/src/compiler/blob.h +++ /dev/null @@ -1,358 +0,0 @@ -/* - * Copyright © 2014 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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. - */ - -#ifndef BLOB_H -#define BLOB_H - -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* The blob functions implement a simple, low-level API for serializing and - * deserializing. - * - * All objects written to a blob will be serialized directly, (without any - * additional meta-data to describe the data written). Therefore, it is the - * caller's responsibility to ensure that any data can be read later, (either - * by knowing exactly what data is expected, or by writing to the blob - * sufficient meta-data to describe what has been written). - * - * A blob is efficient in that it dynamically grows by doubling in size, so - * allocation costs are logarithmic. - */ - -struct blob { - /* The data actually written to the blob. */ - uint8_t *data; - - /** Number of bytes that have been allocated for \c data. */ - size_t allocated; - - /** The number of bytes that have actual data written to them. */ - size_t size; - - /** True if \c data a fixed allocation that we cannot resize - * - * \see blob_init_fixed - */ - bool fixed_allocation; - - /** - * True if we've ever failed to realloc or if we go pas the end of a fixed - * allocation blob. - */ - bool out_of_memory; -}; - -/* When done reading, the caller can ensure that everything was consumed by - * checking the following: - * - * 1. blob->current should be equal to blob->end, (if not, too little was - * read). - * - * 2. blob->overrun should be false, (otherwise, too much was read). - */ -struct blob_reader { - const uint8_t *data; - const uint8_t *end; - const uint8_t *current; - bool overrun; -}; - -/** - * Init a new, empty blob. - */ -void -blob_init(struct blob *blob); - -/** - * Init a new, fixed-size blob. - * - * A fixed-size blob has a fixed block of data that will not be freed on - * blob_finish and will never be grown. If we hit the end, we simply start - * returning false from the write functions. - * - * If a fixed-size blob has a NULL data pointer then the data is written but - * it otherwise operates normally. This can be used to determine the size - * that will be required to write a given data structure. - */ -void -blob_init_fixed(struct blob *blob, void *data, size_t size); - -/** - * Finish a blob and free its memory. - * - * If \blob was initialized with blob_init_fixed, the data pointer is - * considered to be owned by the user and will not be freed. - */ -static inline void -blob_finish(struct blob *blob) -{ - if (!blob->fixed_allocation) - free(blob->data); -} - -/** - * Add some unstructured, fixed-size data to a blob. - * - * \return True unless allocation failed. - */ -bool -blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write); - -/** - * Reserve space in \blob for a number of bytes. - * - * Space will be allocated within the blob for these byes, but the bytes will - * be left uninitialized. The caller is expected to use \sa - * blob_overwrite_bytes to write to these bytes. - * - * \return An offset to space allocated within \blob to which \to_write bytes - * can be written, (or -1 in case of any allocation error). - */ -intptr_t -blob_reserve_bytes(struct blob *blob, size_t to_write); - -/** - * Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of - * space. Note that this must be used if later reading with \sa - * blob_read_uint32, since it aligns the offset correctly. - */ -intptr_t -blob_reserve_uint32(struct blob *blob); - -/** - * Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of - * space. Note that this must be used if later reading with \sa - * blob_read_intptr, since it aligns the offset correctly. - */ -intptr_t -blob_reserve_intptr(struct blob *blob); - -/** - * Overwrite some data previously written to the blob. - * - * Writes data to an existing portion of the blob at an offset of \offset. - * This data range must have previously been written to the blob by one of the - * blob_write_* calls. - * - * For example usage, see blob_overwrite_uint32 - * - * \return True unless the requested offset or offset+to_write lie outside - * the current blob's size. - */ -bool -blob_overwrite_bytes(struct blob *blob, - size_t offset, - const void *bytes, - size_t to_write); - -/** - * Add a uint32_t to a blob. - * - * \note This function will only write to a uint32_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_uint32(struct blob *blob, uint32_t value); - -/** - * Overwrite a uint32_t previously written to the blob. - * - * Writes a uint32_t value to an existing portion of the blob at an offset of - * \offset. This data range must have previously been written to the blob by - * one of the blob_write_* calls. - * - * - * The expected usage is something like the following pattern: - * - * size_t offset; - * - * offset = blob_reserve_uint32(blob); - * ... various blob write calls, writing N items ... - * blob_overwrite_uint32 (blob, offset, N); - * - * \return True unless the requested position or position+to_write lie outside - * the current blob's size. - */ -bool -blob_overwrite_uint32(struct blob *blob, - size_t offset, - uint32_t value); - -/** - * Add a uint64_t to a blob. - * - * \note This function will only write to a uint64_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_uint64(struct blob *blob, uint64_t value); - -/** - * Add an intptr_t to a blob. - * - * \note This function will only write to an intptr_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be added to the - * blob if this write follows some unaligned write (such as - * blob_write_string). - * - * \return True unless allocation failed. - */ -bool -blob_write_intptr(struct blob *blob, intptr_t value); - -/** - * Overwrite an intptr_t previously written to the blob. - * - * Writes a intptr_t value to an existing portion of the blob at an offset of - * \offset. This data range must have previously been written to the blob by - * one of the blob_write_* calls. - * - * For example usage, see blob_overwrite_uint32 - * - * \return True unless the requested position or position+to_write lie outside - * the current blob's size. - */ -bool -blob_overwrite_intptr(struct blob *blob, - size_t offset, - intptr_t value); - -/** - * Add a NULL-terminated string to a blob, (including the NULL terminator). - * - * \return True unless allocation failed. - */ -bool -blob_write_string(struct blob *blob, const char *str); - -/** - * Start reading a blob, (initializing the contents of \blob for reading). - * - * After this call, the caller can use the various blob_read_* functions to - * read elements from the data array. - * - * For all of the blob_read_* functions, if there is insufficient data - * remaining, the functions will do nothing, (perhaps returning default values - * such as 0). The caller can detect this by noting that the blob_reader's - * current value is unchanged before and after the call. - */ -void -blob_reader_init(struct blob_reader *blob, const void *data, size_t size); - -/** - * Read some unstructured, fixed-size data from the current location, (and - * update the current location to just past this data). - * - * \note The memory returned belongs to the data underlying the blob reader. The - * caller must copy the data in order to use it after the lifetime of the data - * underlying the blob reader. - * - * \return The bytes read (see note above about memory lifetime). - */ -const void * -blob_read_bytes(struct blob_reader *blob, size_t size); - -/** - * Read some unstructured, fixed-size data from the current location, copying - * it to \dest (and update the current location to just past this data) - */ -void -blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size); - -/** - * Skip \size bytes within the blob. - */ -void -blob_skip_bytes(struct blob_reader *blob, size_t size); - -/** - * Read a uint32_t from the current location, (and update the current location - * to just past this uint32_t). - * - * \note This function will only read from a uint32_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The uint32_t read - */ -uint32_t -blob_read_uint32(struct blob_reader *blob); - -/** - * Read a uint64_t from the current location, (and update the current location - * to just past this uint64_t). - * - * \note This function will only read from a uint64_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The uint64_t read - */ -uint64_t -blob_read_uint64(struct blob_reader *blob); - -/** - * Read an intptr_t value from the current location, (and update the - * current location to just past this intptr_t). - * - * \note This function will only read from an intptr_t-aligned offset from the - * beginning of the blob's data, so some padding bytes may be skipped. - * - * \return The intptr_t read - */ -intptr_t -blob_read_intptr(struct blob_reader *blob); - -/** - * Read a NULL-terminated string from the current location, (and update the - * current location to just past this string). - * - * \note The memory returned belongs to the data underlying the blob reader. The - * caller must copy the string in order to use the string after the lifetime - * of the data underlying the blob reader. - * - * \return The string read (see note above about memory lifetime). However, if - * there is no NULL byte remaining within the blob, this function returns - * NULL. - */ -char * -blob_read_string(struct blob_reader *blob); - -#ifdef __cplusplus -} -#endif - -#endif /* BLOB_H */ diff --git a/src/compiler/glsl/tests/blob_test.c b/src/compiler/glsl/tests/blob_test.c deleted file mode 100644 index 21b8b1efdc1..00000000000 --- a/src/compiler/glsl/tests/blob_test.c +++ /dev/null @@ -1,328 +0,0 @@ -/* - * Copyright © 2014 Intel Corporation - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * 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. - */ - -/* A collection of unit tests for blob.c */ - -#include -#include -#include -#include -#include -#ifdef _MSC_VER -#include -typedef SSIZE_T ssize_t; -#endif - -#include "util/ralloc.h" -#include "blob.h" - -#define bytes_test_str "bytes_test" -#define reserve_test_str "reserve_test" - -/* This placeholder must be the same length as the next overwrite_test_str */ -#define placeholder_str "XXXXXXXXXXXXXX" -#define overwrite_test_str "overwrite_test" -#define uint32_test 0x12345678 -#define uint32_placeholder 0xDEADBEEF -#define uint32_overwrite 0xA1B2C3D4 -#define uint64_test 0x1234567890ABCDEF -#define string_test_str "string_test" - -bool error = false; - -static void -expect_equal(uint64_t expected, uint64_t actual, const char *test) -{ - if (actual != expected) { - fprintf(stderr, - "Error: Test '%s' failed: " - "Expected=%" PRIu64 ", " - "Actual=%" PRIu64 "\n", - test, expected, actual); - error = true; - } -} - -static void -expect_unequal(uint64_t expected, uint64_t actual, const char *test) -{ - if (actual == expected) { - fprintf(stderr, - "Error: Test '%s' failed: Result=%" PRIu64 ", " - "but expected something different.\n", - test, actual); - error = true; - } -} - -static void -expect_equal_str(const char *expected, const char *actual, const char *test) -{ - if (strcmp(expected, actual)) { - fprintf (stderr, "Error: Test '%s' failed:\n\t" - "Expected=\"%s\", Actual=\"%s\"\n", - test, expected, actual); - error = true; - } -} - -static void -expect_equal_bytes(uint8_t *expected, const uint8_t *actual, - size_t num_bytes, const char *test) -{ - size_t i; - - if (memcmp(expected, actual, num_bytes)) { - fprintf (stderr, "Error: Test '%s' failed:\n\t", test); - - fprintf (stderr, "Expected=["); - for (i = 0; i < num_bytes; i++) { - if (i != 0) - fprintf(stderr, ", "); - fprintf(stderr, "0x%02x", expected[i]); - } - fprintf (stderr, "]"); - - fprintf (stderr, "Actual=["); - for (i = 0; i < num_bytes; i++) { - if (i != 0) - fprintf(stderr, ", "); - fprintf(stderr, "0x%02x", actual[i]); - } - fprintf (stderr, "]\n"); - - error = true; - } -} - -/* Test at least one call of each blob_write_foo and blob_read_foo function, - * verifying that we read out everything we wrote, that every bytes is - * consumed, and that the overrun bit is not set. - */ -static void -test_write_and_read_functions (void) -{ - struct blob blob; - struct blob_reader reader; - ssize_t reserved; - size_t str_offset, uint_offset; - uint8_t reserve_buf[sizeof(reserve_test_str)]; - - blob_init(&blob); - - /*** Test blob by writing one of every possible kind of value. */ - - blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str)); - - reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str)); - blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str)); - - /* Write a placeholder, (to be replaced later via overwrite_bytes) */ - str_offset = blob.size; - blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str)); - - blob_write_uint32(&blob, uint32_test); - - /* Write a placeholder, (to be replaced later via overwrite_uint32) */ - uint_offset = blob.size; - blob_write_uint32(&blob, uint32_placeholder); - - blob_write_uint64(&blob, uint64_test); - - blob_write_intptr(&blob, (intptr_t) &blob); - - blob_write_string(&blob, string_test_str); - - /* Finally, overwrite our placeholders. */ - blob_overwrite_bytes(&blob, str_offset, overwrite_test_str, - sizeof(overwrite_test_str)); - blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite); - - /*** Now read each value and verify. */ - blob_reader_init(&reader, blob.data, blob.size); - - expect_equal_str(bytes_test_str, - blob_read_bytes(&reader, sizeof(bytes_test_str)), - "blob_write/read_bytes"); - - blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf)); - expect_equal_str(reserve_test_str, (char *) reserve_buf, - "blob_reserve_bytes/blob_copy_bytes"); - - expect_equal_str(overwrite_test_str, - blob_read_bytes(&reader, sizeof(overwrite_test_str)), - "blob_overwrite_bytes"); - - expect_equal(uint32_test, blob_read_uint32(&reader), - "blob_write/read_uint32"); - expect_equal(uint32_overwrite, blob_read_uint32(&reader), - "blob_overwrite_uint32"); - expect_equal(uint64_test, blob_read_uint64(&reader), - "blob_write/read_uint64"); - expect_equal((intptr_t) &blob, blob_read_intptr(&reader), - "blob_write/read_intptr"); - expect_equal_str(string_test_str, blob_read_string(&reader), - "blob_write/read_string"); - - expect_equal(reader.end - reader.data, reader.current - reader.data, - "read_consumes_all_bytes"); - expect_equal(false, reader.overrun, "read_does_not_overrun"); - - blob_finish(&blob); -} - -/* Test that data values are written and read with proper alignment. */ -static void -test_alignment(void) -{ - struct blob blob; - struct blob_reader reader; - uint8_t bytes[] = "ABCDEFGHIJKLMNOP"; - size_t delta, last, num_bytes; - - blob_init(&blob); - - /* First, write an intptr value to the blob and capture that size. This is - * the expected offset between any pair of intptr values (if written with - * alignment). - */ - blob_write_intptr(&blob, (intptr_t) &blob); - - delta = blob.size; - last = blob.size; - - /* Then loop doing the following: - * - * 1. Write an unaligned number of bytes - * 2. Verify that write results in an unaligned size - * 3. Write an intptr_t value - * 2. Verify that that write results in an aligned size - */ - for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { - blob_write_bytes(&blob, bytes, num_bytes); - - expect_unequal(delta, blob.size - last, "unaligned write of bytes"); - - blob_write_intptr(&blob, (intptr_t) &blob); - - expect_equal(2 * delta, blob.size - last, "aligned write of intptr"); - - last = blob.size; - } - - /* Finally, test that reading also does proper alignment. Since we know - * that values were written with all the right alignment, all we have to do - * here is verify that correct values are read. - */ - blob_reader_init(&reader, blob.data, blob.size); - - expect_equal((intptr_t) &blob, blob_read_intptr(&reader), - "read of initial, aligned intptr_t"); - - for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { - expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes), - num_bytes, "unaligned read of bytes"); - expect_equal((intptr_t) &blob, blob_read_intptr(&reader), - "aligned read of intptr_t"); - } - - blob_finish(&blob); -} - -/* Test that we detect overrun. */ -static void -test_overrun(void) -{ - struct blob blob; - struct blob_reader reader; - uint32_t value = 0xdeadbeef; - - blob_init(&blob); - - blob_write_uint32(&blob, value); - - blob_reader_init(&reader, blob.data, blob.size); - - expect_equal(value, blob_read_uint32(&reader), "read before overrun"); - expect_equal(false, reader.overrun, "overrun flag not set"); - expect_equal(0, blob_read_uint32(&reader), "read at overrun"); - expect_equal(true, reader.overrun, "overrun flag set"); - - blob_finish(&blob); -} - -/* Test that we can read and write some large objects, (exercising the code in - * the blob_write functions to realloc blob->data. - */ -static void -test_big_objects(void) -{ - void *ctx = ralloc_context(NULL); - struct blob blob; - struct blob_reader reader; - int size = 1000; - int count = 1000; - size_t i; - char *buf; - - blob_init(&blob); - - /* Initialize our buffer. */ - buf = ralloc_size(ctx, size); - for (i = 0; i < size; i++) { - buf[i] = i % 256; - } - - /* Write it many times. */ - for (i = 0; i < count; i++) { - blob_write_bytes(&blob, buf, size); - } - - blob_reader_init(&reader, blob.data, blob.size); - - /* Read and verify it many times. */ - for (i = 0; i < count; i++) { - expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size, - "read of large objects"); - } - - expect_equal(reader.end - reader.data, reader.current - reader.data, - "number of bytes read reading large objects"); - - expect_equal(false, reader.overrun, - "overrun flag not set reading large objects"); - - blob_finish(&blob); - ralloc_free(ctx); -} - -int -main (void) -{ - test_write_and_read_functions (); - test_alignment (); - test_overrun (); - test_big_objects (); - - return error ? 1 : 0; -} diff --git a/src/compiler/glsl/tests/meson.build b/src/compiler/glsl/tests/meson.build index dc017ca4072..e9272fe5fbe 100644 --- a/src/compiler/glsl/tests/meson.build +++ b/src/compiler/glsl/tests/meson.build @@ -18,18 +18,6 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -test( - 'blob_test', - executable( - 'blob_test', - 'blob_test.c', - c_args : [c_vis_args, c_msvc_compat_args, no_override_init_args], - include_directories : [inc_common, inc_compiler], - link_with : [libglsl], - ), - suite : ['compiler', 'glsl'], -) - if with_shader_cache test( 'cache_test', diff --git a/src/compiler/glsl_types.h b/src/compiler/glsl_types.h index dcd7eea6dc0..10596603887 100644 --- a/src/compiler/glsl_types.h +++ b/src/compiler/glsl_types.h @@ -29,8 +29,8 @@ #include #include "shader_enums.h" -#include "blob.h" #include "c11/threads.h" +#include "util/blob.h" #include "util/macros.h" #ifdef __cplusplus diff --git a/src/compiler/meson.build b/src/compiler/meson.build index 108dc7eb10c..d4a40846fc7 100644 --- a/src/compiler/meson.build +++ b/src/compiler/meson.build @@ -24,8 +24,6 @@ inc_glsl = include_directories('glsl') inc_spirv = include_directories('spirv') files_libcompiler = files( - 'blob.c', - 'blob.h', 'builtin_type_macros.h', 'glsl_types.cpp', 'glsl_types.h', diff --git a/src/compiler/nir/nir_serialize.h b/src/compiler/nir/nir_serialize.h index f77d8e367ff..528988f5e4a 100644 --- a/src/compiler/nir/nir_serialize.h +++ b/src/compiler/nir/nir_serialize.h @@ -25,7 +25,7 @@ #define _NIR_SERIALIZE_H #include "nir.h" -#include "compiler/blob.h" +#include "util/blob.h" #ifdef __cplusplus extern "C" { diff --git a/src/gallium/drivers/iris/iris_disk_cache.c b/src/gallium/drivers/iris/iris_disk_cache.c index 325903c9861..2b5889c49e9 100644 --- a/src/gallium/drivers/iris/iris_disk_cache.c +++ b/src/gallium/drivers/iris/iris_disk_cache.c @@ -31,8 +31,8 @@ #include #include -#include "compiler/blob.h" #include "compiler/nir/nir.h" +#include "util/blob.h" #include "util/build_id.h" #include "util/disk_cache.h" #include "util/mesa-sha1.h" diff --git a/src/intel/vulkan/anv_pipeline_cache.c b/src/intel/vulkan/anv_pipeline_cache.c index 7c9f8a6a8cd..9c315d5f44c 100644 --- a/src/intel/vulkan/anv_pipeline_cache.c +++ b/src/intel/vulkan/anv_pipeline_cache.c @@ -21,7 +21,7 @@ * IN THE SOFTWARE. */ -#include "compiler/blob.h" +#include "util/blob.h" #include "util/hash_table.h" #include "util/debug.h" #include "util/disk_cache.h" diff --git a/src/mesa/drivers/dri/i965/brw_disk_cache.c b/src/mesa/drivers/dri/i965/brw_disk_cache.c index ccf389cfd79..1b4cb437cbc 100644 --- a/src/mesa/drivers/dri/i965/brw_disk_cache.c +++ b/src/mesa/drivers/dri/i965/brw_disk_cache.c @@ -21,10 +21,10 @@ * IN THE SOFTWARE. */ -#include "compiler/blob.h" #include "compiler/glsl/ir_uniform.h" #include "compiler/glsl/shader_cache.h" #include "main/mtypes.h" +#include "util/blob.h" #include "util/build_id.h" #include "util/debug.h" #include "util/disk_cache.h" diff --git a/src/mesa/main/program_binary.c b/src/mesa/main/program_binary.c index 39537cfccce..8c2db255246 100644 --- a/src/mesa/main/program_binary.c +++ b/src/mesa/main/program_binary.c @@ -29,12 +29,12 @@ */ -#include "compiler/blob.h" #include "compiler/glsl/serialize.h" #include "main/errors.h" #include "main/mtypes.h" #include "main/shaderapi.h" #include "util/bitscan.h" +#include "util/blob.h" #include "util/crc32.h" #include "program_binary.h" #include "program/prog_parameter.h" diff --git a/src/mesa/state_tracker/st_shader_cache.h b/src/mesa/state_tracker/st_shader_cache.h index 67cc084d600..2a74ea1e90a 100644 --- a/src/mesa/state_tracker/st_shader_cache.h +++ b/src/mesa/state_tracker/st_shader_cache.h @@ -25,9 +25,9 @@ #define ST_SHADER_CACHE_H #include "st_context.h" -#include "compiler/blob.h" #include "main/mtypes.h" #include "pipe/p_state.h" +#include "util/blob.h" #include "util/disk_cache.h" #include "util/mesa-sha1.h" diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index cf8c848ea6d..109de1ebf6e 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -5,6 +5,8 @@ MESA_UTIL_FILES := \ bitscan.c \ bitscan.h \ bitset.h \ + blob.c \ + blob.h \ build_id.c \ build_id.h \ crc32.c \ diff --git a/src/util/blob.c b/src/util/blob.c new file mode 100644 index 00000000000..c89092e1cf3 --- /dev/null +++ b/src/util/blob.c @@ -0,0 +1,391 @@ +/* + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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 + +#include "main/macros.h" +#include "blob.h" + +#ifdef HAVE_VALGRIND +#include +#include +#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; + uint8_t *new_data; + + if (blob->out_of_memory) + return false; + + if (blob->size + additional <= blob->allocated) + return true; + + if (blob->fixed_allocation) { + blob->out_of_memory = true; + return false; + } + + if (blob->allocated == 0) + to_allocate = BLOB_INITIAL_SIZE; + else + to_allocate = blob->allocated * 2; + + to_allocate = MAX2(to_allocate, blob->allocated + additional); + + new_data = realloc(blob->data, to_allocate); + if (new_data == NULL) { + blob->out_of_memory = true; + return false; + } + + blob->data = new_data; + blob->allocated = to_allocate; + + return true; +} + +/* Align the blob->size so that reading or writing a value at (blob->data + + * blob->size) will result in an access aligned to a granularity of \alignment + * bytes. + * + * \return True unless allocation fails + */ +static bool +align_blob(struct blob *blob, size_t alignment) +{ + const size_t new_size = ALIGN(blob->size, alignment); + + if (blob->size < new_size) { + if (!grow_to_fit(blob, new_size - blob->size)) + return false; + + if (blob->data) + memset(blob->data + blob->size, 0, new_size - blob->size); + blob->size = new_size; + } + + return true; +} + +static void +align_blob_reader(struct blob_reader *blob, size_t alignment) +{ + blob->current = blob->data + ALIGN(blob->current - blob->data, alignment); +} + +void +blob_init(struct blob *blob) +{ + blob->data = NULL; + blob->allocated = 0; + blob->size = 0; + blob->fixed_allocation = false; + blob->out_of_memory = false; +} + +void +blob_init_fixed(struct blob *blob, void *data, size_t size) +{ + blob->data = data; + blob->allocated = size; + blob->size = 0; + blob->fixed_allocation = true; + blob->out_of_memory = false; +} + +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 (offset + to_write < offset || blob->size < offset + to_write) + return false; + + VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write)); + + if (blob->data) + 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)); + + if (blob->data) + memcpy(blob->data + blob->size, bytes, to_write); + blob->size += to_write; + + return true; +} + +intptr_t +blob_reserve_bytes(struct blob *blob, size_t to_write) +{ + intptr_t ret; + + if (! grow_to_fit (blob, to_write)) + return -1; + + ret = blob->size; + blob->size += to_write; + + return ret; +} + +intptr_t +blob_reserve_uint32(struct blob *blob) +{ + align_blob(blob, sizeof(uint32_t)); + return blob_reserve_bytes(blob, sizeof(uint32_t)); +} + +intptr_t +blob_reserve_intptr(struct blob *blob) +{ + align_blob(blob, sizeof(intptr_t)); + return blob_reserve_bytes(blob, sizeof(intptr_t)); +} + +bool +blob_write_uint32(struct blob *blob, uint32_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +#define ASSERT_ALIGNED(_offset, _align) \ + assert(ALIGN((_offset), (_align)) == (_offset)) + +bool +blob_overwrite_uint32 (struct blob *blob, + size_t offset, + uint32_t value) +{ + ASSERT_ALIGNED(offset, sizeof(value)); + return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); +} + +bool +blob_write_uint64(struct blob *blob, uint64_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +bool +blob_write_intptr(struct blob *blob, intptr_t value) +{ + align_blob(blob, sizeof(value)); + + return blob_write_bytes(blob, &value, sizeof(value)); +} + +bool +blob_overwrite_intptr (struct blob *blob, + size_t offset, + intptr_t value) +{ + ASSERT_ALIGNED(offset, sizeof(value)); + return blob_overwrite_bytes(blob, offset, &value, sizeof(value)); +} + +bool +blob_write_string(struct blob *blob, const char *str) +{ + return blob_write_bytes(blob, str, strlen(str) + 1); +} + +void +blob_reader_init(struct blob_reader *blob, const void *data, size_t size) +{ + blob->data = data; + blob->end = blob->data + size; + blob->current = data; + blob->overrun = false; +} + +/* Check that an object of size \size can be read from this blob. + * + * If not, set blob->overrun to indicate that we attempted to read too far. + */ +static bool +ensure_can_read(struct blob_reader *blob, size_t size) +{ + if (blob->overrun) + return false; + + if (blob->current <= blob->end && blob->end - blob->current >= size) + return true; + + blob->overrun = true; + + return false; +} + +const void * +blob_read_bytes(struct blob_reader *blob, size_t size) +{ + const void *ret; + + if (! ensure_can_read (blob, size)) + return NULL; + + ret = blob->current; + + blob->current += size; + + return ret; +} + +void +blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size) +{ + const void *bytes; + + bytes = blob_read_bytes(blob, size); + if (bytes == NULL) + return; + + memcpy(dest, bytes, size); +} + +void +blob_skip_bytes(struct blob_reader *blob, size_t size) +{ + if (ensure_can_read (blob, size)) + blob->current += size; +} + +/* These next three read functions have identical form. If we add any beyond + * these first three we should probably switch to generating these with a + * preprocessor macro. +*/ +uint32_t +blob_read_uint32(struct blob_reader *blob) +{ + uint32_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((uint32_t*) blob->current); + + blob->current += size; + + return ret; +} + +uint64_t +blob_read_uint64(struct blob_reader *blob) +{ + uint64_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((uint64_t*) blob->current); + + blob->current += size; + + return ret; +} + +intptr_t +blob_read_intptr(struct blob_reader *blob) +{ + intptr_t ret; + int size = sizeof(ret); + + align_blob_reader(blob, size); + + if (! ensure_can_read(blob, size)) + return 0; + + ret = *((intptr_t *) blob->current); + + blob->current += size; + + return ret; +} + +char * +blob_read_string(struct blob_reader *blob) +{ + int size; + char *ret; + uint8_t *nul; + + /* If we're already at the end, then this is an overrun. */ + if (blob->current >= blob->end) { + blob->overrun = true; + return NULL; + } + + /* Similarly, if there is no zero byte in the data remaining in this blob, + * we also consider that an overrun. + */ + nul = memchr(blob->current, 0, blob->end - blob->current); + + if (nul == NULL) { + blob->overrun = true; + return NULL; + } + + size = nul - blob->current + 1; + + assert(ensure_can_read(blob, size)); + + ret = (char *) blob->current; + + blob->current += size; + + return ret; +} diff --git a/src/util/blob.h b/src/util/blob.h new file mode 100644 index 00000000000..b56fa4b2fe0 --- /dev/null +++ b/src/util/blob.h @@ -0,0 +1,358 @@ +/* + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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. + */ + +#ifndef BLOB_H +#define BLOB_H + +#include +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* The blob functions implement a simple, low-level API for serializing and + * deserializing. + * + * All objects written to a blob will be serialized directly, (without any + * additional meta-data to describe the data written). Therefore, it is the + * caller's responsibility to ensure that any data can be read later, (either + * by knowing exactly what data is expected, or by writing to the blob + * sufficient meta-data to describe what has been written). + * + * A blob is efficient in that it dynamically grows by doubling in size, so + * allocation costs are logarithmic. + */ + +struct blob { + /* The data actually written to the blob. */ + uint8_t *data; + + /** Number of bytes that have been allocated for \c data. */ + size_t allocated; + + /** The number of bytes that have actual data written to them. */ + size_t size; + + /** True if \c data a fixed allocation that we cannot resize + * + * \see blob_init_fixed + */ + bool fixed_allocation; + + /** + * True if we've ever failed to realloc or if we go pas the end of a fixed + * allocation blob. + */ + bool out_of_memory; +}; + +/* When done reading, the caller can ensure that everything was consumed by + * checking the following: + * + * 1. blob->current should be equal to blob->end, (if not, too little was + * read). + * + * 2. blob->overrun should be false, (otherwise, too much was read). + */ +struct blob_reader { + const uint8_t *data; + const uint8_t *end; + const uint8_t *current; + bool overrun; +}; + +/** + * Init a new, empty blob. + */ +void +blob_init(struct blob *blob); + +/** + * Init a new, fixed-size blob. + * + * A fixed-size blob has a fixed block of data that will not be freed on + * blob_finish and will never be grown. If we hit the end, we simply start + * returning false from the write functions. + * + * If a fixed-size blob has a NULL data pointer then the data is written but + * it otherwise operates normally. This can be used to determine the size + * that will be required to write a given data structure. + */ +void +blob_init_fixed(struct blob *blob, void *data, size_t size); + +/** + * Finish a blob and free its memory. + * + * If \blob was initialized with blob_init_fixed, the data pointer is + * considered to be owned by the user and will not be freed. + */ +static inline void +blob_finish(struct blob *blob) +{ + if (!blob->fixed_allocation) + free(blob->data); +} + +/** + * Add some unstructured, fixed-size data to a blob. + * + * \return True unless allocation failed. + */ +bool +blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write); + +/** + * Reserve space in \blob for a number of bytes. + * + * Space will be allocated within the blob for these byes, but the bytes will + * be left uninitialized. The caller is expected to use \sa + * blob_overwrite_bytes to write to these bytes. + * + * \return An offset to space allocated within \blob to which \to_write bytes + * can be written, (or -1 in case of any allocation error). + */ +intptr_t +blob_reserve_bytes(struct blob *blob, size_t to_write); + +/** + * Similar to \sa blob_reserve_bytes, but only reserves an uint32_t worth of + * space. Note that this must be used if later reading with \sa + * blob_read_uint32, since it aligns the offset correctly. + */ +intptr_t +blob_reserve_uint32(struct blob *blob); + +/** + * Similar to \sa blob_reserve_bytes, but only reserves an intptr_t worth of + * space. Note that this must be used if later reading with \sa + * blob_read_intptr, since it aligns the offset correctly. + */ +intptr_t +blob_reserve_intptr(struct blob *blob); + +/** + * Overwrite some data previously written to the blob. + * + * Writes data to an existing portion of the blob at an offset of \offset. + * This data range must have previously been written to the blob by one of the + * blob_write_* calls. + * + * For example usage, see blob_overwrite_uint32 + * + * \return True unless the requested offset or offset+to_write lie outside + * the current blob's size. + */ +bool +blob_overwrite_bytes(struct blob *blob, + size_t offset, + const void *bytes, + size_t to_write); + +/** + * Add a uint32_t to a blob. + * + * \note This function will only write to a uint32_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_uint32(struct blob *blob, uint32_t value); + +/** + * Overwrite a uint32_t previously written to the blob. + * + * Writes a uint32_t value to an existing portion of the blob at an offset of + * \offset. This data range must have previously been written to the blob by + * one of the blob_write_* calls. + * + * + * The expected usage is something like the following pattern: + * + * size_t offset; + * + * offset = blob_reserve_uint32(blob); + * ... various blob write calls, writing N items ... + * blob_overwrite_uint32 (blob, offset, N); + * + * \return True unless the requested position or position+to_write lie outside + * the current blob's size. + */ +bool +blob_overwrite_uint32(struct blob *blob, + size_t offset, + uint32_t value); + +/** + * Add a uint64_t to a blob. + * + * \note This function will only write to a uint64_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_uint64(struct blob *blob, uint64_t value); + +/** + * Add an intptr_t to a blob. + * + * \note This function will only write to an intptr_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be added to the + * blob if this write follows some unaligned write (such as + * blob_write_string). + * + * \return True unless allocation failed. + */ +bool +blob_write_intptr(struct blob *blob, intptr_t value); + +/** + * Overwrite an intptr_t previously written to the blob. + * + * Writes a intptr_t value to an existing portion of the blob at an offset of + * \offset. This data range must have previously been written to the blob by + * one of the blob_write_* calls. + * + * For example usage, see blob_overwrite_uint32 + * + * \return True unless the requested position or position+to_write lie outside + * the current blob's size. + */ +bool +blob_overwrite_intptr(struct blob *blob, + size_t offset, + intptr_t value); + +/** + * Add a NULL-terminated string to a blob, (including the NULL terminator). + * + * \return True unless allocation failed. + */ +bool +blob_write_string(struct blob *blob, const char *str); + +/** + * Start reading a blob, (initializing the contents of \blob for reading). + * + * After this call, the caller can use the various blob_read_* functions to + * read elements from the data array. + * + * For all of the blob_read_* functions, if there is insufficient data + * remaining, the functions will do nothing, (perhaps returning default values + * such as 0). The caller can detect this by noting that the blob_reader's + * current value is unchanged before and after the call. + */ +void +blob_reader_init(struct blob_reader *blob, const void *data, size_t size); + +/** + * Read some unstructured, fixed-size data from the current location, (and + * update the current location to just past this data). + * + * \note The memory returned belongs to the data underlying the blob reader. The + * caller must copy the data in order to use it after the lifetime of the data + * underlying the blob reader. + * + * \return The bytes read (see note above about memory lifetime). + */ +const void * +blob_read_bytes(struct blob_reader *blob, size_t size); + +/** + * Read some unstructured, fixed-size data from the current location, copying + * it to \dest (and update the current location to just past this data) + */ +void +blob_copy_bytes(struct blob_reader *blob, void *dest, size_t size); + +/** + * Skip \size bytes within the blob. + */ +void +blob_skip_bytes(struct blob_reader *blob, size_t size); + +/** + * Read a uint32_t from the current location, (and update the current location + * to just past this uint32_t). + * + * \note This function will only read from a uint32_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The uint32_t read + */ +uint32_t +blob_read_uint32(struct blob_reader *blob); + +/** + * Read a uint64_t from the current location, (and update the current location + * to just past this uint64_t). + * + * \note This function will only read from a uint64_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The uint64_t read + */ +uint64_t +blob_read_uint64(struct blob_reader *blob); + +/** + * Read an intptr_t value from the current location, (and update the + * current location to just past this intptr_t). + * + * \note This function will only read from an intptr_t-aligned offset from the + * beginning of the blob's data, so some padding bytes may be skipped. + * + * \return The intptr_t read + */ +intptr_t +blob_read_intptr(struct blob_reader *blob); + +/** + * Read a NULL-terminated string from the current location, (and update the + * current location to just past this string). + * + * \note The memory returned belongs to the data underlying the blob reader. The + * caller must copy the string in order to use the string after the lifetime + * of the data underlying the blob reader. + * + * \return The string read (see note above about memory lifetime). However, if + * there is no NULL byte remaining within the blob, this function returns + * NULL. + */ +char * +blob_read_string(struct blob_reader *blob); + +#ifdef __cplusplus +} +#endif + +#endif /* BLOB_H */ diff --git a/src/util/blob_test.c b/src/util/blob_test.c new file mode 100644 index 00000000000..21b8b1efdc1 --- /dev/null +++ b/src/util/blob_test.c @@ -0,0 +1,328 @@ +/* + * Copyright © 2014 Intel Corporation + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * 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. + */ + +/* A collection of unit tests for blob.c */ + +#include +#include +#include +#include +#include +#ifdef _MSC_VER +#include +typedef SSIZE_T ssize_t; +#endif + +#include "util/ralloc.h" +#include "blob.h" + +#define bytes_test_str "bytes_test" +#define reserve_test_str "reserve_test" + +/* This placeholder must be the same length as the next overwrite_test_str */ +#define placeholder_str "XXXXXXXXXXXXXX" +#define overwrite_test_str "overwrite_test" +#define uint32_test 0x12345678 +#define uint32_placeholder 0xDEADBEEF +#define uint32_overwrite 0xA1B2C3D4 +#define uint64_test 0x1234567890ABCDEF +#define string_test_str "string_test" + +bool error = false; + +static void +expect_equal(uint64_t expected, uint64_t actual, const char *test) +{ + if (actual != expected) { + fprintf(stderr, + "Error: Test '%s' failed: " + "Expected=%" PRIu64 ", " + "Actual=%" PRIu64 "\n", + test, expected, actual); + error = true; + } +} + +static void +expect_unequal(uint64_t expected, uint64_t actual, const char *test) +{ + if (actual == expected) { + fprintf(stderr, + "Error: Test '%s' failed: Result=%" PRIu64 ", " + "but expected something different.\n", + test, actual); + error = true; + } +} + +static void +expect_equal_str(const char *expected, const char *actual, const char *test) +{ + if (strcmp(expected, actual)) { + fprintf (stderr, "Error: Test '%s' failed:\n\t" + "Expected=\"%s\", Actual=\"%s\"\n", + test, expected, actual); + error = true; + } +} + +static void +expect_equal_bytes(uint8_t *expected, const uint8_t *actual, + size_t num_bytes, const char *test) +{ + size_t i; + + if (memcmp(expected, actual, num_bytes)) { + fprintf (stderr, "Error: Test '%s' failed:\n\t", test); + + fprintf (stderr, "Expected=["); + for (i = 0; i < num_bytes; i++) { + if (i != 0) + fprintf(stderr, ", "); + fprintf(stderr, "0x%02x", expected[i]); + } + fprintf (stderr, "]"); + + fprintf (stderr, "Actual=["); + for (i = 0; i < num_bytes; i++) { + if (i != 0) + fprintf(stderr, ", "); + fprintf(stderr, "0x%02x", actual[i]); + } + fprintf (stderr, "]\n"); + + error = true; + } +} + +/* Test at least one call of each blob_write_foo and blob_read_foo function, + * verifying that we read out everything we wrote, that every bytes is + * consumed, and that the overrun bit is not set. + */ +static void +test_write_and_read_functions (void) +{ + struct blob blob; + struct blob_reader reader; + ssize_t reserved; + size_t str_offset, uint_offset; + uint8_t reserve_buf[sizeof(reserve_test_str)]; + + blob_init(&blob); + + /*** Test blob by writing one of every possible kind of value. */ + + blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str)); + + reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str)); + blob_overwrite_bytes(&blob, reserved, reserve_test_str, sizeof(reserve_test_str)); + + /* Write a placeholder, (to be replaced later via overwrite_bytes) */ + str_offset = blob.size; + blob_write_bytes(&blob, placeholder_str, sizeof(placeholder_str)); + + blob_write_uint32(&blob, uint32_test); + + /* Write a placeholder, (to be replaced later via overwrite_uint32) */ + uint_offset = blob.size; + blob_write_uint32(&blob, uint32_placeholder); + + blob_write_uint64(&blob, uint64_test); + + blob_write_intptr(&blob, (intptr_t) &blob); + + blob_write_string(&blob, string_test_str); + + /* Finally, overwrite our placeholders. */ + blob_overwrite_bytes(&blob, str_offset, overwrite_test_str, + sizeof(overwrite_test_str)); + blob_overwrite_uint32(&blob, uint_offset, uint32_overwrite); + + /*** Now read each value and verify. */ + blob_reader_init(&reader, blob.data, blob.size); + + expect_equal_str(bytes_test_str, + blob_read_bytes(&reader, sizeof(bytes_test_str)), + "blob_write/read_bytes"); + + blob_copy_bytes(&reader, reserve_buf, sizeof(reserve_buf)); + expect_equal_str(reserve_test_str, (char *) reserve_buf, + "blob_reserve_bytes/blob_copy_bytes"); + + expect_equal_str(overwrite_test_str, + blob_read_bytes(&reader, sizeof(overwrite_test_str)), + "blob_overwrite_bytes"); + + expect_equal(uint32_test, blob_read_uint32(&reader), + "blob_write/read_uint32"); + expect_equal(uint32_overwrite, blob_read_uint32(&reader), + "blob_overwrite_uint32"); + expect_equal(uint64_test, blob_read_uint64(&reader), + "blob_write/read_uint64"); + expect_equal((intptr_t) &blob, blob_read_intptr(&reader), + "blob_write/read_intptr"); + expect_equal_str(string_test_str, blob_read_string(&reader), + "blob_write/read_string"); + + expect_equal(reader.end - reader.data, reader.current - reader.data, + "read_consumes_all_bytes"); + expect_equal(false, reader.overrun, "read_does_not_overrun"); + + blob_finish(&blob); +} + +/* Test that data values are written and read with proper alignment. */ +static void +test_alignment(void) +{ + struct blob blob; + struct blob_reader reader; + uint8_t bytes[] = "ABCDEFGHIJKLMNOP"; + size_t delta, last, num_bytes; + + blob_init(&blob); + + /* First, write an intptr value to the blob and capture that size. This is + * the expected offset between any pair of intptr values (if written with + * alignment). + */ + blob_write_intptr(&blob, (intptr_t) &blob); + + delta = blob.size; + last = blob.size; + + /* Then loop doing the following: + * + * 1. Write an unaligned number of bytes + * 2. Verify that write results in an unaligned size + * 3. Write an intptr_t value + * 2. Verify that that write results in an aligned size + */ + for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { + blob_write_bytes(&blob, bytes, num_bytes); + + expect_unequal(delta, blob.size - last, "unaligned write of bytes"); + + blob_write_intptr(&blob, (intptr_t) &blob); + + expect_equal(2 * delta, blob.size - last, "aligned write of intptr"); + + last = blob.size; + } + + /* Finally, test that reading also does proper alignment. Since we know + * that values were written with all the right alignment, all we have to do + * here is verify that correct values are read. + */ + blob_reader_init(&reader, blob.data, blob.size); + + expect_equal((intptr_t) &blob, blob_read_intptr(&reader), + "read of initial, aligned intptr_t"); + + for (num_bytes = 1; num_bytes < sizeof(intptr_t); num_bytes++) { + expect_equal_bytes(bytes, blob_read_bytes(&reader, num_bytes), + num_bytes, "unaligned read of bytes"); + expect_equal((intptr_t) &blob, blob_read_intptr(&reader), + "aligned read of intptr_t"); + } + + blob_finish(&blob); +} + +/* Test that we detect overrun. */ +static void +test_overrun(void) +{ + struct blob blob; + struct blob_reader reader; + uint32_t value = 0xdeadbeef; + + blob_init(&blob); + + blob_write_uint32(&blob, value); + + blob_reader_init(&reader, blob.data, blob.size); + + expect_equal(value, blob_read_uint32(&reader), "read before overrun"); + expect_equal(false, reader.overrun, "overrun flag not set"); + expect_equal(0, blob_read_uint32(&reader), "read at overrun"); + expect_equal(true, reader.overrun, "overrun flag set"); + + blob_finish(&blob); +} + +/* Test that we can read and write some large objects, (exercising the code in + * the blob_write functions to realloc blob->data. + */ +static void +test_big_objects(void) +{ + void *ctx = ralloc_context(NULL); + struct blob blob; + struct blob_reader reader; + int size = 1000; + int count = 1000; + size_t i; + char *buf; + + blob_init(&blob); + + /* Initialize our buffer. */ + buf = ralloc_size(ctx, size); + for (i = 0; i < size; i++) { + buf[i] = i % 256; + } + + /* Write it many times. */ + for (i = 0; i < count; i++) { + blob_write_bytes(&blob, buf, size); + } + + blob_reader_init(&reader, blob.data, blob.size); + + /* Read and verify it many times. */ + for (i = 0; i < count; i++) { + expect_equal_bytes((uint8_t *) buf, blob_read_bytes(&reader, size), size, + "read of large objects"); + } + + expect_equal(reader.end - reader.data, reader.current - reader.data, + "number of bytes read reading large objects"); + + expect_equal(false, reader.overrun, + "overrun flag not set reading large objects"); + + blob_finish(&blob); + ralloc_free(ctx); +} + +int +main (void) +{ + test_write_and_read_functions (); + test_alignment (); + test_overrun (); + test_big_objects (); + + return error ? 1 : 0; +} diff --git a/src/util/meson.build b/src/util/meson.build index c981a146de1..60989c924ea 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -29,6 +29,8 @@ files_mesa_util = files( 'bitscan.c', 'bitscan.h', 'bitset.h', + 'blob.c', + 'blob.h', 'build_id.c', 'build_id.h', 'crc32.c', @@ -144,6 +146,7 @@ deps_for_libmesa_util = [ dep_thread, dep_atomic, dep_m, + dep_valgrind, ] if with_platform_android @@ -202,6 +205,18 @@ if with_tests suite : ['util'], ) + test( + 'blob', + executable( + 'blob_test', + files('blob_test.c'), + include_directories : inc_common, + dependencies : idep_mesautil, + c_args : [c_msvc_compat_args], + ), + suite : ['util'], + ) + test( 'roundeven', executable(