From e4e8744d40ed37ff15c0c8dc1879ba260bc52e13 Mon Sep 17 00:00:00 2001 From: Jian Zhao Date: Fri, 21 Jan 2011 13:27:40 +0800 Subject: [PATCH] Add test on the glsl uniform array read and free. In our mesa core, there will be a out of bound operation when operate on a uniform array, which will cause program crash when free them. --- tests/all.tests | 1 + tests/shaders/CMakeLists.txt | 1 + .../glsl-uniform-array-init-out-of-bounds.c | 156 ++++++++++++++++++++ 3 files changed, 158 insertions(+), 0 deletions(-) create mode 100644 tests/shaders/glsl-uniform-array-init-out-of-bounds.c diff --git a/tests/all.tests b/tests/all.tests index bc95e97..a9b8d76 100644 --- a/tests/all.tests +++ b/tests/all.tests @@ -328,6 +328,7 @@ add_plain_test(shaders, 'glsl-novertexdata') add_plain_test(shaders, 'glsl-preprocessor-comments') add_plain_test(shaders, 'glsl-reload-source') add_plain_test(shaders, 'glsl-uniform-out-of-bounds') +add_plain_test(shaders, 'glsl-uniform-array-init-out-of-bounds') add_plain_test(shaders, 'glsl-uniform-update') add_plain_test(shaders, 'glsl-unused-varying') add_shader_generic(shaders, 'glsl-floating-constant-120') diff --git a/tests/shaders/CMakeLists.txt b/tests/shaders/CMakeLists.txt index f34e6f7..de22e5c 100644 --- a/tests/shaders/CMakeLists.txt +++ b/tests/shaders/CMakeLists.txt @@ -74,6 +74,7 @@ add_executable (glsl-reload-source glsl-reload-source.c) add_executable (glsl-unused-varying glsl-unused-varying.c) add_executable (glsl-uniform-update glsl-uniform-update.c) add_executable (glsl-uniform-out-of-bounds glsl-uniform-out-of-bounds.c) +add_executable (glsl-uniform-array-init-out-of-bounds glsl-uniform-array-init-out-of-bounds.c) add_executable (glsl-fs-bug25902 glsl-fs-bug25902.c) add_executable (glsl-fs-exp2 glsl-fs-exp2.c) add_executable (glsl-fs-flat-color glsl-fs-flat-color.c) diff --git a/tests/shaders/glsl-uniform-array-init-out-of-bounds.c b/tests/shaders/glsl-uniform-array-init-out-of-bounds.c new file mode 100644 index 0000000..e296df0 --- /dev/null +++ b/tests/shaders/glsl-uniform-array-init-out-of-bounds.c @@ -0,0 +1,156 @@ +/* + * Copyright © 2011 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. + * + * Authors: + * Jian Zhao + * + */ + +/** @file glsl-uniform-array-init-out-of-bounds.c + * + * Tests that init uniforms arrays will doing some out of bound operation which may lead to program abort when free them. + * + */ + +#include "piglit-util.h" +#include "piglit-framework.h" + +int piglit_width = 100, piglit_height = 100; +int piglit_window_mode = GLUT_RGBA | GLUT_DOUBLE; + +static int color_location; +static GLint prog; + +static const char vs_vector_template[] = +"#version 120\n" +"uniform float pos[4] = float[4](5.0, 5.0, 0.0, 1.0);\n" +"uniform float color[4] = float[4](1.0, 0.0, 0.0, 0.0);\n" +"void main() {\n" +" gl_Position = vec4(pos[0], pos[1], 0, 1);\n" +"}\n"; + +static const char fs_vector_template[] = +"#version 120\n" +"uniform float color[4] = float[4](1.0, 0.0, 0.0, 0.0);\n" +"void main() {\n" +" gl_FragColor = vec4(color[0], color[1], color[2], color[3]);\n" +"}\n"; + + +enum piglit_result +piglit_display(void) +{ + GLboolean pass = GL_TRUE; + void *uniformVal; + + + glClearColor(0.5, 0.5, 0.5, 0.5); + glClear(GL_COLOR_BUFFER_BIT); + + uniformVal = malloc( 4 * sizeof(GLfloat) * 4); + memset(uniformVal, 0, 4 * sizeof(GLfloat) * 4); + glGetUniformfv(prog, color_location, (GLfloat *)uniformVal); + + + if(uniformVal) + free(uniformVal); + + return pass ? PIGLIT_SUCCESS : PIGLIT_FAILURE; +} + +static GLuint compile_shader(GLenum shaderType, const char * text) +{ + GLuint shader; + GLint status; + + shader = glCreateShaderObjectARB(shaderType); + glShaderSourceARB(shader, 1, (const GLchar **)&text, NULL); + glCompileShaderARB(shader); + + glGetObjectParameterivARB(shader, GL_OBJECT_COMPILE_STATUS_ARB, &status); + if (!status) { + GLchar log[1000]; + GLsizei len; + glGetInfoLogARB(shader, 1000, &len, log); + fprintf(stderr, "Error: problem compiling shader: %s\n", log); + piglit_report_result(PIGLIT_FAILURE); + } + + return shader; +} + +static GLuint link_program(GLuint vs, GLuint fs) +{ + GLuint program; + GLint status; + + program = glCreateProgramObjectARB(); + if (vs) + glAttachObjectARB(program, vs); + if (fs) + glAttachObjectARB(program, fs); + + glLinkProgramARB(program); + glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &status); + if (!status) { + GLchar log[1000]; + GLsizei len; + glGetInfoLogARB(program, 1000, &len, log); + fprintf(stderr, "Error: problem linking program: %s\n", log); + piglit_report_result(PIGLIT_FAILURE); + } + + return program; +} + +void +piglit_init(int argc, char **argv) +{ + GLint vs, fs; + char *vs_buffer; + char *fs_buffer; + + if (!GLEW_ARB_shader_objects || !GLEW_ARB_vertex_shader || !GLEW_ARB_fragment_shader) { + printf("Requires ARB_shader_objects and ARB_{vertex,fragment}_shader\n"); + piglit_report_result(PIGLIT_SKIP); + } + + piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE); + vs_buffer = (char *) malloc(sizeof(char) * (strlen(vs_vector_template)+1)); + strcpy(vs_buffer, vs_vector_template); + vs = compile_shader(GL_VERTEX_SHADER_ARB, vs_buffer); + + fs_buffer = (char *) malloc(sizeof(char) * (strlen(fs_vector_template)+1)); + strcpy(fs_buffer, fs_vector_template); + fs = compile_shader(GL_FRAGMENT_SHADER_ARB, fs_buffer); + + prog = link_program(vs, fs); + + glUseProgramObjectARB(prog); + + color_location = glGetUniformLocation(prog, "color"); + + if(vs_buffer) + free(vs_buffer); + if(fs_buffer) + free(fs_buffer); +} -- 1.6.0.6