compiler/blob: make blob_reserve_bytes() more useful
Despite the name, it could only be used if you immediately wrote to the pointer. Noboby was using it outside of one test, so clearly this behavior wasn't that useful. Instead, make it return an offset into the data buffer so that the result isn't invalidated if you later write to the blob. In conjunction with blob_overwrite_bytes(), this will be useful for leaving a placeholder and then filling it in later, which we'll need to do for handling phi nodes when serializing NIR. v2 (Jason Ekstrand): - Detect overflow in the offset + to_write computation Reviewed-by: Nicolai Hähnle <nicolai.haehnle@amd.com> Reviewed-by: Jordan Justen <jordan.l.justen@intel.com>
This commit is contained in:

committed by
Jason Ekstrand

parent
8ae03af4ed
commit
6935440967
@@ -132,7 +132,7 @@ blob_overwrite_bytes(struct blob *blob,
|
|||||||
size_t to_write)
|
size_t to_write)
|
||||||
{
|
{
|
||||||
/* Detect an attempt to overwrite data out of bounds. */
|
/* Detect an attempt to overwrite data out of bounds. */
|
||||||
if (blob->size < offset + to_write)
|
if (offset + to_write < offset || blob->size < offset + to_write)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
|
VG(VALGRIND_CHECK_MEM_IS_DEFINED(bytes, to_write));
|
||||||
@@ -158,15 +158,15 @@ blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t *
|
ssize_t
|
||||||
blob_reserve_bytes(struct blob *blob, size_t to_write)
|
blob_reserve_bytes(struct blob *blob, size_t to_write)
|
||||||
{
|
{
|
||||||
uint8_t *ret;
|
ssize_t ret;
|
||||||
|
|
||||||
if (! grow_to_fit (blob, to_write))
|
if (! grow_to_fit (blob, to_write))
|
||||||
return NULL;
|
return -1;
|
||||||
|
|
||||||
ret = blob->data + blob->size;
|
ret = blob->size;
|
||||||
blob->size += to_write;
|
blob->size += to_write;
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@@ -129,21 +129,13 @@ blob_write_bytes(struct blob *blob, const void *bytes, size_t to_write);
|
|||||||
* Reserve space in \blob for a number of bytes.
|
* Reserve space in \blob for a number of bytes.
|
||||||
*
|
*
|
||||||
* Space will be allocated within the blob for these byes, but the bytes will
|
* Space will be allocated within the blob for these byes, but the bytes will
|
||||||
* be left uninitialized. The caller is expected to use the return value to
|
* be left uninitialized. The caller is expected to use \sa
|
||||||
* write directly (and immediately) to these bytes.
|
* blob_overwrite_bytes to write to these bytes.
|
||||||
*
|
*
|
||||||
* \note The return value is valid immediately upon return, but can be
|
* \return An offset to space allocated within \blob to which \to_write bytes
|
||||||
* invalidated by any other call to a blob function. So the caller should call
|
* can be written, (or -1 in case of any allocation error).
|
||||||
* blob_reserve_byes immediately before writing through the returned pointer.
|
|
||||||
*
|
|
||||||
* This function is intended to be used when interfacing with an existing API
|
|
||||||
* that is not aware of the blob API, (so that blob_write_bytes cannot be
|
|
||||||
* called).
|
|
||||||
*
|
|
||||||
* \return A pointer to space allocated within \blob to which \to_write bytes
|
|
||||||
* can be written, (or NULL in case of any allocation error).
|
|
||||||
*/
|
*/
|
||||||
uint8_t *
|
ssize_t
|
||||||
blob_reserve_bytes(struct blob *blob, size_t to_write);
|
blob_reserve_bytes(struct blob *blob, size_t to_write);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -120,7 +120,7 @@ test_write_and_read_functions (void)
|
|||||||
{
|
{
|
||||||
struct blob blob;
|
struct blob blob;
|
||||||
struct blob_reader reader;
|
struct blob_reader reader;
|
||||||
uint8_t *reserved;
|
ssize_t reserved;
|
||||||
size_t str_offset, uint_offset;
|
size_t str_offset, uint_offset;
|
||||||
uint8_t reserve_buf[sizeof(reserve_test_str)];
|
uint8_t reserve_buf[sizeof(reserve_test_str)];
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ test_write_and_read_functions (void)
|
|||||||
blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str));
|
blob_write_bytes(&blob, bytes_test_str, sizeof(bytes_test_str));
|
||||||
|
|
||||||
reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str));
|
reserved = blob_reserve_bytes(&blob, sizeof(reserve_test_str));
|
||||||
memcpy(reserved, reserve_test_str, 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) */
|
/* Write a placeholder, (to be replaced later via overwrite_bytes) */
|
||||||
str_offset = blob.size;
|
str_offset = blob.size;
|
||||||
|
Reference in New Issue
Block a user