1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
* Copyright (c) 2009 Nicolai Hähnle
*
* 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:
* Nicolai Hähnle <nhaehnle@gmail.com>
*
*/
/**
* \file
* Test that out-of-bound writes to uniform locations are caught properly.
*/
#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include "piglit-util-gl-common.h"
PIGLIT_GL_TEST_CONFIG_BEGIN
config.window_width = 100;
config.window_height = 100;
config.window_visual = PIGLIT_GL_VISUAL_RGB;
PIGLIT_GL_TEST_CONFIG_END
static void expect_error(GLenum expect, const char * where, ...)
{
GLenum error = glGetError();
if (error != expect) {
va_list va;
fprintf(stderr, "Expected OpenGL error 0x%04x, got 0x%04x\nat: ", expect, error);
va_start(va, where);
vfprintf(stderr, where, va);
va_end(va);
fprintf(stderr, "\n");
piglit_report_result(PIGLIT_FAIL);
}
}
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_FAIL);
}
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_FAIL);
}
return program;
}
static const GLfloat lots_of_zeros[16*1024] = { 0.0f, };
static const char vs_vector_template[] =
"uniform %s a;\n"
"uniform %s b[4];\n"
"uniform %s c[4];\n"
"varying %s v;\n"
"void main() {\n"
" v = a + b[3] + c[0] + c[1] + c[2] + c[3];\n"
" gl_Position = vec4(0,0,0,1);\n"
"}\n";
static const char fs_vector_template[] =
"varying %s v;\n"
"void main() {\n"
" gl_FragColor = vec4(v%s);\n"
"}\n";
static void test_vector(const char *glsl_type, const char * suffix,
void (GLAPIENTRY *uniform)(GLint, GLsizei, const GLfloat*))
{
char buffer[2*sizeof(vs_vector_template)];
GLuint vs, fs;
GLuint program;
GLint loc_a, loc_b, loc_c;
GLint loc_b2;
snprintf(buffer, sizeof(buffer), vs_vector_template,
glsl_type, glsl_type, glsl_type, glsl_type);
vs = compile_shader(GL_VERTEX_SHADER_ARB, buffer);
snprintf(buffer, sizeof(buffer), fs_vector_template,
glsl_type, suffix);
fs = compile_shader(GL_FRAGMENT_SHADER_ARB, buffer);
program = link_program(vs, fs);
glUseProgramObjectARB(program);
loc_a = glGetUniformLocationARB(program, "a");
loc_b = glGetUniformLocationARB(program, "b");
loc_c = glGetUniformLocationARB(program, "c");
loc_b2 = glGetUniformLocationARB(program, "b[2]");
printf("locations: a: %i b: %i c: %i b[2]: %i\n", loc_a, loc_b, loc_c, loc_b);
expect_error(GL_NO_ERROR, "Type %s: Sanity check", glsl_type);
uniform(loc_a, 0, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to a", glsl_type);
uniform(loc_a, 1, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to a", glsl_type);
uniform(loc_a, 2, lots_of_zeros);
expect_error(GL_INVALID_OPERATION, "Type %s: Write count = 2 to a", glsl_type);
uniform(loc_a, 1024, lots_of_zeros);
expect_error(GL_INVALID_OPERATION, "Type %s: Write count = 1024 to a", glsl_type);
uniform(loc_b, 0, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to b", glsl_type);
uniform(loc_b, 1, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to b", glsl_type);
uniform(loc_b, 4, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 4 to b", glsl_type);
/* Note: The following are out of bound for the array,
* but the spec situation is a bit unclear as to whether errors
* should be generated.
*
* Issue #32 of the ARB_shader_objects spec suggests errors
* should be generated when writing out-of-bounds on arrays,
* but this is not reflected in the OpenGL spec.
*
* The main point of these tests is to make sure the driver
* does not introduce memory errors by accessing internal arrays
* out of bounds.
*/
uniform(loc_b, 5, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
uniform(loc_c, 0, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to c", glsl_type);
uniform(loc_c, 1, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to c", glsl_type);
uniform(loc_c, 4, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 4 to c", glsl_type);
/* Out of bounds; see comment above */
uniform(loc_c, 5, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
uniform(loc_b2, 0, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to b[2]", glsl_type);
uniform(loc_b2, 2, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 2 to b[2]", glsl_type);
/* Out of bounds; see comment above */
uniform(loc_b2, 1024, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
glDeleteObjectARB(fs);
glDeleteObjectARB(vs);
glDeleteObjectARB(program);
}
static const char vs_matrix_template[] =
"uniform mat4 a;\n"
"uniform mat4 b[4];\n"
"uniform mat4 c[4];\n"
"varying vec4 v;\n"
"void main() {\n"
" mat4 m = a + b[3] + c[0] + c[1] + c[2] + c[3];\n"
" v = m * vec4(1.0, 1.0, 1.0, 1.0);\n"
" gl_Position = vec4(0,0,0,1);\n"
"}\n";
static const char fs_matrix_template[] =
"varying vec4 v;\n"
"void main() {\n"
" gl_FragColor = v;\n"
"}\n";
static void test_matrix(void)
{
GLuint vs, fs;
GLuint program;
GLint loc_a, loc_b, loc_c;
GLint loc_b2;
const char * glsl_type = "mat4";
vs = compile_shader(GL_VERTEX_SHADER_ARB, vs_matrix_template);
fs = compile_shader(GL_FRAGMENT_SHADER_ARB, fs_matrix_template);
program = link_program(vs, fs);
glUseProgramObjectARB(program);
loc_a = glGetUniformLocationARB(program, "a");
loc_b = glGetUniformLocationARB(program, "b");
loc_c = glGetUniformLocationARB(program, "c");
loc_b2 = glGetUniformLocationARB(program, "b[2]");
printf("locations: a: %i b: %i c: %i b[2]: %i\n", loc_a, loc_b, loc_c, loc_b);
expect_error(GL_NO_ERROR, "Type %s: Sanity check", glsl_type);
glUniformMatrix4fvARB(loc_b, 0, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to b", glsl_type);
glUniformMatrix4fvARB(loc_b, 1, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to b", glsl_type);
glUniformMatrix4fvARB(loc_b, 4, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 4 to b", glsl_type);
/* Out of bounds; see comment above */
glUniformMatrix4fvARB(loc_b, 5, GL_FALSE, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
glUniformMatrix4fvARB(loc_c, 0, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to c", glsl_type);
glUniformMatrix4fvARB(loc_c, 1, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to c", glsl_type);
glUniformMatrix4fvARB(loc_c, 4, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 4 to c", glsl_type);
/* Out of bounds; see comment above */
glUniformMatrix4fvARB(loc_c, 5, GL_FALSE, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
glUniformMatrix4fvARB(loc_b2, 0, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to b[2]", glsl_type);
glUniformMatrix4fvARB(loc_b2, 2, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 2 to b[2]", glsl_type);
/* Out of bounds; see comment above */
glUniformMatrix4fvARB(loc_b2, INT_MAX, GL_FALSE, lots_of_zeros);
(void) glGetError(); /* Eat generated error, if any */
glUniformMatrix4fvARB(loc_a, 0, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 0 to a", glsl_type);
glUniformMatrix4fvARB(loc_a, 1, GL_FALSE, lots_of_zeros);
expect_error(GL_NO_ERROR, "Type %s: Write count = 1 to a", glsl_type);
glUniformMatrix4fvARB(loc_a, 2, GL_FALSE, lots_of_zeros);
expect_error(GL_INVALID_OPERATION, "Type %s: Write count = 2 to a", glsl_type);
glUniformMatrix4fvARB(loc_a, INT_MAX, GL_FALSE, lots_of_zeros);
expect_error(GL_INVALID_OPERATION, "Type %s: Write count = INT_MAX to a", glsl_type);
glDeleteObjectARB(fs);
glDeleteObjectARB(vs);
glDeleteObjectARB(program);
}
enum piglit_result
piglit_display(void)
{
piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE);
test_matrix();
test_vector("float", ", 0, 0, 0", glUniform1fvARB);
test_vector("vec2", ", 0, 0", glUniform2fvARB);
test_vector("vec3", ", 0", glUniform3fvARB);
test_vector("vec4", "", glUniform4fvARB);
return PIGLIT_PASS;
}
void
piglit_init(int argc, char **argv)
{
if (!piglit_is_extension_supported("GL_ARB_shader_objects") || !piglit_is_extension_supported("GL_ARB_vertex_shader") || !piglit_is_extension_supported("GL_ARB_fragment_shader")) {
printf("Requires ARB_shader_objects and ARB_{vertex,fragment}_shader\n");
piglit_report_result(PIGLIT_SKIP);
}
}
|