1 /*
2  * Mesa 3-D graphics library
3  *
4  * Copyright (c) 2017 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \file program_binary.c
27  *
28  * Helper functions for serializing a binary program.
29  */
30 
31 
32 #include "compiler/glsl/serialize.h"
33 #include "main/errors.h"
34 #include "main/mtypes.h"
35 #include "main/shaderapi.h"
36 #include "util/bitscan.h"
37 #include "util/blob.h"
38 #include "util/crc32.h"
39 #include "program_binary.h"
40 #include "program/prog_parameter.h"
41 
42 /**
43  * Mesa supports one binary format, but it must differentiate between formats
44  * produced by different drivers and different Mesa versions.
45  *
46  * Mesa uses a uint32_t value to specify an internal format. The only format
47  * defined has one uint32_t value of 0, followed by 20 bytes specifying a sha1
48  * that uniquely identifies the Mesa driver type and version.
49  */
50 
51 struct program_binary_header {
52    /* If internal_format is 0, it must be followed by the 20 byte sha1 that
53     * identifies the Mesa driver and version supported. If we want to support
54     * something besides a sha1, then a new internal_format value can be added.
55     */
56    uint32_t internal_format;
57    uint8_t sha1[20];
58    /* Fields following sha1 can be changed since the sha1 will guarantee that
59     * the binary only works with the same Mesa version.
60     */
61    uint32_t size;
62    uint32_t crc32;
63 };
64 
65 /**
66  * Returns the header size needed for a binary
67  */
68 static unsigned
get_program_binary_header_size(void)69 get_program_binary_header_size(void)
70 {
71    return sizeof(struct program_binary_header);
72 }
73 
74 static bool
write_program_binary(const void * payload,unsigned payload_size,const void * sha1,void * binary,unsigned binary_size,GLenum * binary_format)75 write_program_binary(const void *payload, unsigned payload_size,
76                      const void *sha1, void *binary, unsigned binary_size,
77                      GLenum *binary_format)
78 {
79    struct program_binary_header *hdr = binary;
80 
81    if (binary_size < sizeof(*hdr))
82       return false;
83 
84    /* binary_size is the size of the buffer provided by the application.
85     * Make sure our program (payload) will fit in the buffer.
86     */
87    if (payload_size > binary_size - sizeof(*hdr))
88       return false;
89 
90    hdr->internal_format = 0;
91    memcpy(hdr->sha1, sha1, sizeof(hdr->sha1));
92    memcpy(hdr + 1, payload, payload_size);
93    hdr->size = payload_size;
94 
95    hdr->crc32 = util_hash_crc32(hdr + 1, payload_size);
96    *binary_format = GL_PROGRAM_BINARY_FORMAT_MESA;
97 
98    return true;
99 }
100 
101 static bool
simple_header_checks(const struct program_binary_header * hdr,unsigned length)102 simple_header_checks(const struct program_binary_header *hdr, unsigned length)
103 {
104    if (hdr == NULL || length < sizeof(*hdr))
105       return false;
106 
107    if (hdr->internal_format != 0)
108       return false;
109 
110    return true;
111 }
112 
113 static bool
check_crc32(const struct program_binary_header * hdr,unsigned length)114 check_crc32(const struct program_binary_header *hdr, unsigned length)
115 {
116    uint32_t crc32;
117    unsigned crc32_len;
118 
119    crc32_len = hdr->size;
120    if (crc32_len > length - sizeof(*hdr))
121       return false;
122 
123    crc32 = util_hash_crc32(hdr + 1, crc32_len);
124    if (hdr->crc32 != crc32)
125       return false;
126 
127    return true;
128 }
129 
130 static bool
is_program_binary_valid(GLenum binary_format,const void * sha1,const struct program_binary_header * hdr,unsigned length)131 is_program_binary_valid(GLenum binary_format, const void *sha1,
132                         const struct program_binary_header *hdr,
133                         unsigned length)
134 {
135    if (binary_format != GL_PROGRAM_BINARY_FORMAT_MESA)
136       return false;
137 
138    if (!simple_header_checks(hdr, length))
139       return false;
140 
141    if (memcmp(hdr->sha1, sha1, sizeof(hdr->sha1)) != 0)
142       return false;
143 
144    if (!check_crc32(hdr, length))
145       return false;
146 
147    return true;
148 }
149 
150 /**
151  * Returns the payload within the binary.
152  *
153  * If NULL is returned, then the binary not supported. If non-NULL is
154  * returned, it will be a pointer contained within the specified `binary`
155  * buffer.
156  *
157  * This can be used to access the payload of `binary` during the
158  * glProgramBinary call.
159  */
160 static const void*
get_program_binary_payload(GLenum binary_format,const void * sha1,const void * binary,unsigned length)161 get_program_binary_payload(GLenum binary_format, const void *sha1,
162                            const void *binary, unsigned length)
163 {
164    const struct program_binary_header *hdr = binary;
165    if (!is_program_binary_valid(binary_format, sha1, hdr, length))
166       return NULL;
167    return (const uint8_t*)binary + sizeof(*hdr);
168 }
169 
170 static void
write_program_payload(struct gl_context * ctx,struct blob * blob,struct gl_shader_program * sh_prog)171 write_program_payload(struct gl_context *ctx, struct blob *blob,
172                       struct gl_shader_program *sh_prog)
173 {
174    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
175       struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
176       if (shader)
177          ctx->Driver.ProgramBinarySerializeDriverBlob(ctx, sh_prog,
178                                                       shader->Program);
179    }
180 
181    blob_write_uint32(blob, sh_prog->SeparateShader);
182 
183    serialize_glsl_program(blob, ctx, sh_prog);
184 
185    for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
186       struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
187       if (shader) {
188          struct gl_program *prog = sh_prog->_LinkedShaders[stage]->Program;
189          ralloc_free(prog->driver_cache_blob);
190          prog->driver_cache_blob = NULL;
191          prog->driver_cache_blob_size = 0;
192       }
193    }
194 }
195 
196 static bool
read_program_payload(struct gl_context * ctx,struct blob_reader * blob,GLenum binary_format,struct gl_shader_program * sh_prog)197 read_program_payload(struct gl_context *ctx, struct blob_reader *blob,
198                      GLenum binary_format, struct gl_shader_program *sh_prog)
199 {
200    sh_prog->SeparateShader = blob_read_uint32(blob);
201 
202    if (!deserialize_glsl_program(blob, ctx, sh_prog))
203       return false;
204 
205    unsigned int stage;
206    for (stage = 0; stage < ARRAY_SIZE(sh_prog->_LinkedShaders); stage++) {
207       struct gl_linked_shader *shader = sh_prog->_LinkedShaders[stage];
208       if (!shader)
209          continue;
210 
211       ctx->Driver.ProgramBinaryDeserializeDriverBlob(ctx, sh_prog,
212                                                      shader->Program);
213    }
214 
215    return true;
216 }
217 
218 void
_mesa_get_program_binary_length(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLint * length)219 _mesa_get_program_binary_length(struct gl_context *ctx,
220                                 struct gl_shader_program *sh_prog,
221                                 GLint *length)
222 {
223    struct blob blob;
224    blob_init_fixed(&blob, NULL, SIZE_MAX);
225    write_program_payload(ctx, &blob, sh_prog);
226    *length = get_program_binary_header_size() + blob.size;
227    blob_finish(&blob);
228 }
229 
230 void
_mesa_get_program_binary(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLsizei buf_size,GLsizei * length,GLenum * binary_format,GLvoid * binary)231 _mesa_get_program_binary(struct gl_context *ctx,
232                        struct gl_shader_program *sh_prog,
233                        GLsizei buf_size, GLsizei *length,
234                        GLenum *binary_format, GLvoid *binary)
235 {
236    struct blob blob;
237    uint8_t driver_sha1[20];
238    unsigned header_size = get_program_binary_header_size();
239 
240    ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
241 
242    blob_init(&blob);
243 
244    if (buf_size < header_size)
245       goto fail;
246 
247    write_program_payload(ctx, &blob, sh_prog);
248    if (blob.size + header_size > buf_size ||
249        blob.out_of_memory)
250       goto fail;
251 
252    bool written = write_program_binary(blob.data, blob.size, driver_sha1,
253                                       binary, buf_size, binary_format);
254    if (!written || blob.out_of_memory)
255       goto fail;
256 
257    *length = header_size + blob.size;
258 
259    blob_finish(&blob);
260    return;
261 
262 fail:
263    _mesa_error(ctx, GL_INVALID_OPERATION,
264                "glGetProgramBinary(buffer too small)");
265    *length = 0;
266    blob_finish(&blob);
267 }
268 
269 void
_mesa_program_binary(struct gl_context * ctx,struct gl_shader_program * sh_prog,GLenum binary_format,const GLvoid * binary,GLsizei length)270 _mesa_program_binary(struct gl_context *ctx, struct gl_shader_program *sh_prog,
271                      GLenum binary_format, const GLvoid *binary,
272                      GLsizei length)
273 {
274    uint8_t driver_sha1[20];
275    unsigned header_size = get_program_binary_header_size();
276 
277    ctx->Driver.GetProgramBinaryDriverSHA1(ctx, driver_sha1);
278 
279    const void *payload = get_program_binary_payload(binary_format, driver_sha1,
280                                                     binary, length);
281 
282    if (payload == NULL) {
283       sh_prog->data->LinkStatus = LINKING_FAILURE;
284       return;
285    }
286 
287    struct blob_reader blob;
288    blob_reader_init(&blob, payload, length - header_size);
289 
290    unsigned programs_in_use = 0;
291    if (ctx->_Shader)
292       for (unsigned stage = 0; stage < MESA_SHADER_STAGES; stage++) {
293          if (ctx->_Shader->CurrentProgram[stage] &&
294              ctx->_Shader->CurrentProgram[stage]->Id == sh_prog->Name) {
295             programs_in_use |= 1 << stage;
296          }
297    }
298 
299    if (!read_program_payload(ctx, &blob, binary_format, sh_prog)) {
300       sh_prog->data->LinkStatus = LINKING_FAILURE;
301       return;
302    }
303 
304    /* From section 7.3 (Program Objects) of the OpenGL 4.5 spec:
305     *
306     *    "If LinkProgram or ProgramBinary successfully re-links a program
307     *     object that is active for any shader stage, then the newly generated
308     *     executable code will be installed as part of the current rendering
309     *     state for all shader stages where the program is active.
310     *     Additionally, the newly generated executable code is made part of
311     *     the state of any program pipeline for all stages where the program
312     *     is attached."
313     */
314    while (programs_in_use) {
315       const int stage = u_bit_scan(&programs_in_use);
316 
317       struct gl_program *prog = NULL;
318       if (sh_prog->_LinkedShaders[stage])
319          prog = sh_prog->_LinkedShaders[stage]->Program;
320 
321       _mesa_use_program(ctx, stage, sh_prog, prog, ctx->_Shader);
322    }
323 
324    sh_prog->data->LinkStatus = LINKING_SKIPPED;
325 }
326