Signed-off-by: Jordan Justen <jordan.l.jus...@intel.com> --- src/util/Makefile.sources | 2 + src/util/meson.build | 2 + src/util/program_binary.c | 322 ++++++++++++++++++++++++++++++++++++++++++++++ src/util/program_binary.h | 91 +++++++++++++ 4 files changed, 417 insertions(+) create mode 100644 src/util/program_binary.c create mode 100644 src/util/program_binary.h
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index c7f6516a992..d9048bbd182 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -21,6 +21,8 @@ MESA_UTIL_FILES := \ macros.h \ mesa-sha1.c \ mesa-sha1.h \ + program_binary.c \ + program_binary.h \ sha1/sha1.c \ sha1/sha1.h \ ralloc.c \ diff --git a/src/util/meson.build b/src/util/meson.build index c9cb3e861e9..9bc10222a72 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -45,6 +45,8 @@ files_mesa_util = files( 'macros.h', 'mesa-sha1.c', 'mesa-sha1.h', + 'program_binary.c', + 'program_binary.h', 'sha1/sha1.c', 'sha1/sha1.h', 'ralloc.c', diff --git a/src/util/program_binary.c b/src/util/program_binary.c new file mode 100644 index 00000000000..4447dd632d9 --- /dev/null +++ b/src/util/program_binary.c @@ -0,0 +1,322 @@ +/* + * Mesa 3-D graphics library + * + * Copyright (c) 2017 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 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. + */ + +/** + * \file program_binary.c + * + * Helper functions for serializing a binary program. + */ + + +#include "main/mtypes.h" +#include "crc32.h" +#include "program_binary.h" +#include "zlib.h" + +/** + * Mesa supports one binary format, but it must differentiate between formats + * produced by different drivers and different Mesa versions. + * + * Mesa uses a uint32_t value to specify an internal format. The only format + * defined has one uint32_t value of 0, followed by 20 bytes specifying a sha1 + * that uniquely identifies the Mesa driver type and version. + */ + +struct program_binary_header { + /* If internal_format is 0, it must be followed by the 20 byte sha1 that + * identifies the Mesa driver and version supported. If we want to support + * something besides a sha1, then a new internal_format value can be added. + */ + uint32_t internal_format; + uint8_t sha1[20]; + /* Fields following sha1 can be changed since the sha1 will guarantee that + * the binary only works with the same Mesa version. + */ + uint32_t deflated_size; + uint32_t inflated_size; + uint32_t crc32; +}; + +unsigned +get_program_binary_max_size(unsigned payload_size) +{ + return sizeof(struct program_binary_header) + payload_size; +} + +/* From the zlib docs: + * "If the memory is available, buffers sizes on the order of 128K or 256K + * bytes should be used." + */ +#define BUFSIZE 256 * 1024 + +/** + * Compresses buffer + */ +static size_t +deflate_and_write_to_buf(const void *in_data, size_t in_data_size, + uint8_t *dest, size_t max_dest_size) +{ + unsigned char out[BUFSIZE]; + size_t written = 0; + + /* allocate deflate state */ + z_stream strm; + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.next_in = (uint8_t *) in_data; + strm.avail_in = in_data_size; + + int ret = deflateInit(&strm, Z_BEST_COMPRESSION); + if (ret != Z_OK) + return 0; + + /* compress until end of in_data */ + size_t compressed_size = 0; + int flush; + do { + int remaining = in_data_size - BUFSIZE; + flush = remaining > 0 ? Z_NO_FLUSH : Z_FINISH; + in_data_size -= BUFSIZE; + + /* Run deflate() on input until the output buffer is not full (which + * means there is no more data to deflate). + */ + do { + strm.avail_out = BUFSIZE; + strm.next_out = out; + + ret = deflate(&strm, flush); /* no bad return value */ + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + + size_t have = BUFSIZE - strm.avail_out; + compressed_size += have; + + if (written + have > max_dest_size) { + (void)deflateEnd(&strm); + return 0; + } + + memcpy(dest, out, have); + dest += have; + written += have; + } while (strm.avail_out == 0); + + /* all input should be used */ + assert(strm.avail_in == 0); + + } while (flush != Z_FINISH); + + /* stream should be complete */ + assert(ret == Z_STREAM_END); + + /* clean up and return */ + (void)deflateEnd(&strm); + return compressed_size; +} + +bool +get_program_binary(const void *payload, unsigned payload_size, + const void *sha1, void *binary, unsigned max_binary_size, + unsigned *final_binary_size, GLenum *binary_format) +{ + struct program_binary_header *hdr = binary; + unsigned final_payload_size; + unsigned max_payload_size = max_binary_size - sizeof(*hdr); + size_t deflated_size; + + if (max_binary_size < sizeof(*hdr)) + return false; + + deflated_size = + deflate_and_write_to_buf(payload, payload_size, (uint8_t*)(hdr + 1), + max_payload_size); + + if (deflated_size == 0 && payload_size > max_payload_size) + return false; + + hdr->internal_format = 0; + memcpy(hdr->sha1, sha1, sizeof(hdr->sha1)); + if (deflated_size == 0) { + memcpy(hdr + 1, payload, payload_size); + hdr->deflated_size = 0; + final_payload_size = payload_size; + } else { + hdr->deflated_size = deflated_size; + final_payload_size = deflated_size; + } + hdr->inflated_size = payload_size; + + hdr->crc32 = util_hash_crc32(hdr + 1, final_payload_size); + *binary_format = GL_PROGRAM_BINARY_FORMAT_MESA; + if (final_binary_size) + *final_binary_size = final_payload_size + sizeof(*hdr); + + return true; +} + +static bool +simple_header_checks(const void *binary, unsigned length) +{ + const struct program_binary_header *hdr = binary; + if (binary == NULL || length < sizeof(*hdr)) + return false; + + if (hdr->internal_format != 0) + return false; + + return true; +} + +static bool +check_crc32(const void *binary, unsigned length) +{ + const struct program_binary_header *hdr = binary; + uint32_t crc32; + unsigned crc32_len; + + crc32_len = hdr->deflated_size ? hdr->deflated_size : hdr->inflated_size; + if (crc32_len > length - sizeof(*hdr)) + return false; + + crc32 = util_hash_crc32(hdr + 1, crc32_len); + if (hdr->crc32 != crc32) + return false; + + return true; +} + +/** + * The driver should have used is_program_binary_valid and rejected the + * binary. + */ +static void +assert_is_program_binary_valid(const void *binary, unsigned length) +{ + assert(simple_header_checks(binary, length)); + assert(check_crc32(binary, length)); +} + +bool +is_program_binary_valid(GLenum binary_format, const void *sha1, + const void *binary, unsigned length) +{ + const struct program_binary_header *hdr = binary; + + if (binary_format != GL_PROGRAM_BINARY_FORMAT_MESA) + return false; + + if (!simple_header_checks(binary, length)) + return false; + + if (memcmp(hdr->sha1, sha1, sizeof(hdr->sha1)) != 0) + return false; + + if (!check_crc32(binary, length)) + return false; + + return true; +} + +int +program_binary_payload_offset(const void *binary, unsigned length) +{ + const struct program_binary_header *hdr = binary; + assert_is_program_binary_valid(binary, length); + return (hdr->deflated_size == 0) ? sizeof(*hdr) : -1; +} + +/** + * Decompresses payload, returns true if successful. + */ +static bool +inflate_payload(uint8_t *in_data, size_t in_data_size, + uint8_t *out_data, size_t out_data_size) +{ + z_stream strm; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.next_in = in_data; + strm.avail_in = in_data_size; + strm.next_out = out_data; + strm.avail_out = out_data_size; + + int ret = inflateInit(&strm); + if (ret != Z_OK) + return false; + + ret = inflate(&strm, Z_NO_FLUSH); + assert(ret != Z_STREAM_ERROR); /* state not clobbered */ + + /* Unless there was an error we should have decompressed everything in one + * go as we know the uncompressed file size. + */ + if (ret != Z_STREAM_END) { + (void)inflateEnd(&strm); + return false; + } + assert(strm.avail_out == 0); + + /* clean up and return */ + (void)inflateEnd(&strm); + return true; +} + +void +extract_program_binary_payload(const void *binary, unsigned length, + void **payload, unsigned *payload_length) +{ + const struct program_binary_header *hdr = binary; + void *payload_buf = NULL; + + assert_is_program_binary_valid(binary, length); + + /* This function should only be used if program_binary_payload_offset + * returns -1. */ + assert(program_binary_payload_offset(binary, length) < 0); + + if (hdr->deflated_size == 0) + goto fail; + + payload_buf = ralloc_size(NULL, hdr->inflated_size); + if (!payload_buf) + goto fail; + + if (!inflate_payload((uint8_t*)(hdr + 1), length - sizeof(*hdr), + payload_buf, hdr->inflated_size)) + goto fail; + + *payload = payload_buf; + *payload_length = hdr->inflated_size; + return; + + fail: + if (payload_buf) + ralloc_free(payload_buf); + *payload = NULL; + *payload_length = 0; +} diff --git a/src/util/program_binary.h b/src/util/program_binary.h new file mode 100644 index 00000000000..6897298232d --- /dev/null +++ b/src/util/program_binary.h @@ -0,0 +1,91 @@ +/* + * Mesa 3-D graphics library + * + * Copyright (C) 2004-2007 Brian Paul All Rights Reserved. + * Copyright (C) 2010 VMware, Inc. All Rights Reserved. + * + * 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 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 PROGRAM_BINARY_H +#define PROGRAM_BINARY_H + +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Returns the largest binary size required for a given payload size. + * + * The final size of the binary may be smaller if compression reduces the + * payload size. + */ +extern unsigned +get_program_binary_max_size(unsigned payload_size); + +/** + * Returns the program binary for a given payload. + * + * This binary can be used as a return for the glGetProgramBinary call. + */ +extern bool +get_program_binary(const void *payload, unsigned payload_size, + const void *sha1, void *binary, unsigned max_binary_size, + unsigned *final_binary_size, GLenum *binary_format); + +/** + * Performs basic checks on a binary to see if it is valid. + * + * The contents of the payload are not checked. + */ +extern bool +is_program_binary_valid(GLenum binary_format, const void *sha1, + const void *binary, unsigned length); + +/** + * Finds the offset of the payload within the binary if the payload is + * uncompressed. + * + * If -1 is returned, then the payload was compressed, and + * extract_program_binary_payload must be used. + */ +extern int +program_binary_payload_offset(const void *binary, unsigned length); + +/** + * Decompresses the compressed payload within a binary. + * + * Before using this function, call program_binary_payload_offset to see if + * the payload data is uncompressed in the binary. + * + * If the data is decompressed, then the returned buffer is must be freed by + * the caller with ralloc_free. + */ +extern void +extract_program_binary_payload(const void *binary, unsigned length, + void **payload, unsigned *payload_length); + +#ifdef __cplusplus +} +#endif + +#endif /* PROGRAM_BINARY_H */ -- 2.14.1 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev