2 * Copyright © 2020 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
26 * This is a reproducer for issue 1258.
27 * When using separable programs and ssbo a use-after-free can occur in
28 * st_bind_ssbos if these 2 conditions are met:
29 * - the program has been relinked while its pipeline is not bound
30 * - a new ssbo has been attached
32 * Based on rendering.c test.
35 #include "piglit-util-gl.h"
37 PIGLIT_GL_TEST_CONFIG_BEGIN
39 config
.supports_gl_compat_version
= 32;
40 config
.supports_gl_core_version
= 32;
41 config
.window_visual
= PIGLIT_GL_VISUAL_DOUBLE
| PIGLIT_GL_VISUAL_RGBA
;
42 config
.khr_no_error_support
= PIGLIT_NO_ERRORS
;
44 PIGLIT_GL_TEST_CONFIG_END
46 static const char *vert_shader_text
=
48 "#extension GL_ARB_separate_shader_objects: require\n"
50 "in vec4 piglit_vertex;\n"
54 " gl_Position = piglit_vertex;\n"
57 static const char *frag_shader_text
=
59 "#extension GL_ARB_shader_storage_buffer_object: require\n"
60 "#extension GL_ARB_separate_shader_objects: require\n"
62 "buffer ssbo_color { float color; };\n"
66 " gl_FragColor = vec4(color);\n"
69 static GLuint pipeline
, prog_vs
, prog_fs
;
70 static GLuint buffers
[2];
71 static GLint ssbo_size
;
72 static GLint ssbo_index
;
76 piglit_init(int argc
, char **argv
)
78 piglit_require_extension("GL_ARB_shader_storage_buffer_object");
79 piglit_require_extension("GL_ARB_separate_shader_objects");
81 color
= (float*) malloc(piglit_width
* piglit_height
* 4 * sizeof(float));
82 for (int i
= 0; i
< (piglit_width
* piglit_height
* 4); i
++)
85 glViewport(0, 0, piglit_width
, piglit_height
);
86 prog_vs
= piglit_build_simple_program_unlinked(vert_shader_text
, NULL
);
87 glProgramParameteri(prog_vs
, GL_PROGRAM_SEPARABLE
, GL_TRUE
);
88 glLinkProgram(prog_vs
);
89 piglit_link_check_status(prog_vs
);
90 prog_fs
= piglit_build_simple_program(NULL
, frag_shader_text
);
91 glProgramParameteri(prog_fs
, GL_PROGRAM_SEPARABLE
, GL_TRUE
);
92 glLinkProgram(prog_fs
);
93 piglit_link_check_status(prog_fs
);
95 glGenProgramPipelines(1, &pipeline
);
96 glUseProgramStages(pipeline
, GL_VERTEX_SHADER_BIT
, prog_vs
);
97 glUseProgramStages(pipeline
, GL_FRAGMENT_SHADER_BIT
, prog_fs
);
98 piglit_program_pipeline_check_status(pipeline
);
100 ssbo_index
= glGetProgramResourceIndex(prog_fs
,
101 GL_SHADER_STORAGE_BLOCK
,
103 GLenum prop
= GL_BUFFER_DATA_SIZE
;
104 glGetProgramResourceiv(prog_fs
, GL_SHADER_STORAGE_BLOCK
, ssbo_index
,
105 1, &prop
, 1, NULL
, &ssbo_size
);
108 glGenBuffers(ARRAY_SIZE(buffers
), buffers
);
109 for (int i
= 0; i
< ARRAY_SIZE(buffers
); i
++) {
110 glBindBuffer(GL_SHADER_STORAGE_BUFFER
, buffers
[i
]);
111 glBufferData(GL_SHADER_STORAGE_BUFFER
, ssbo_size
,
112 &color
[0], GL_DYNAMIC_DRAW
);
116 if (!piglit_check_gl_error(0))
117 piglit_report_result(PIGLIT_FAIL
);
119 glClearColor(0.2, 0.2, 0.2, 0.2);
125 glViewport(0, 0, piglit_width
, piglit_height
);
126 glClear(GL_COLOR_BUFFER_BIT
);
128 if (!piglit_check_gl_error(GL_NO_ERROR
))
131 piglit_draw_rect(-1, -1, 2, 2);
132 glBindBuffer(GL_SHADER_STORAGE_BUFFER
, 0);
134 return piglit_probe_image_color(
136 piglit_width
, piglit_height
,
144 glBindProgramPipeline(pipeline
);
145 glBindBufferRange(GL_SHADER_STORAGE_BUFFER
, 0, buffers
[0], 0, ssbo_size
);
146 glShaderStorageBlockBinding(prog_fs
, ssbo_index
, 0);
151 glBindProgramPipeline(0);
152 glLinkProgram(prog_fs
);
153 glBindProgramPipeline(pipeline
);
155 glBindBufferRange(GL_SHADER_STORAGE_BUFFER
, 0, buffers
[1], 0, ssbo_size
);
156 glShaderStorageBlockBinding(prog_fs
, ssbo_index
, 0);
161 piglit_present_results();