summaryrefslogtreecommitdiff
path: root/tests/spec/glsl-1.20/recursion/recursion.c
blob: 214f565ab6e18e51c3d1f701a2944e52d1be8d0d (plain)
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
317
318
319
320
/*
 * 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.
 */

/**
 * \file glsl-recursion.c
 * Verify that shaders containing static recursion are rejected.
 *
 * From page 44 (page 50 of the PDF) of the GLSL 1.20 spec:
 *
 *     "Recursion is not allowed, not even statically. Static recursion is
 *     present if the static function call graph of the program contains
 *     cycles."
 *
 * This language leaves a lot of questions unanswered.
 *
 *     - Is the error generated at compile-time or link-time?
 *
 *     - Is it an error to have a recursive function that is never statically
 *       called by main or any function called directly or indirectly by main?
 *       Technically speaking, such a function is not in the "static function
 *       call graph of the program" at all.
 *
 * This set of tests checks for a variety of forms of recursion in shaders.
 * Logs are dumped at both compile-time and link-time.  Errors are only
 * checked at link time.  However, a compile error will also generate a link
 * error (linking an uncompiled shader).
 *
 * \author Ian Romanick <ian.d.romanick@intel.com>
 */
#include "piglit-util-gl.h"

PIGLIT_GL_TEST_CONFIG_BEGIN

	config.supports_gl_compat_version = 10;

	config.window_visual = PIGLIT_GL_VISUAL_RGB | PIGLIT_GL_VISUAL_DOUBLE;

PIGLIT_GL_TEST_CONFIG_END

static const char simple_text[] =
	"#version 120\n"
	"int A(void) { return A(); }\n"
	"\n"
	"void main() {\n"
	"  A();\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char unreachable_text[] =
	"#version 120\n"
	"int A(void) { return A(); }\n"
	"\n"
	"void main() {\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char unreachable_opt_text[] =
	"#version 120\n"
	"int A(void) { return A(); }\n"
	"\n"
	"void main() {\n"
	"  if (false) A();\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char indirect_text[] =
	"#version 120\n"
	"int A(void);\n"
	"int B(void) { return A(); }\n"
	"int A(void) { return B(); }\n"
	"\n"
	"void main() {\n"
	"  A();\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char indirect_sep1_text[] =
	"#version 120\n"
	"int B(void);\n"
	"int A(void) { return B(); }\n"
	"\n"
	"void main() {\n"
	"  A();\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char indirect_sep2_text[] =
	"#version 120\n"
	"int A(void);\n"
	"int B(void) { return A(); }\n"
	;

static const char indirect_complex_text[] =
	"#version 120\n"
	"int A(bool);\n"
	"int B(bool from_a) { if (!from_a) return A(true); return 0; }\n"
	"int A(bool from_b) { if (!from_b) return B(true); return 0; }\n"
	"\n"
	"void main() {\n"
	"  A(false);\n"
	"  B(false);\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char indirect_complex1_text[] =
	"#version 120\n"
	"int B(bool);\n"
	"int A(bool from_b) { if (!from_b) return B(true); return 0; }\n"
	"\n"
	"void main() {\n"
	"  A(false);\n"
	"  B(false);\n"
	"  gl_Position = gl_Vertex;\n"
	"}"
	;

static const char indirect_complex2_text[] =
	"#version 120\n"
	"int A(bool);\n"
	"int B(bool from_a) { if (!from_a) return A(true); return 0; }\n"
	;

struct test_vector {
	const char *name;
	const char *description;
	const char *shader_source[4];
};

static const struct test_vector all_tests[] = {
	{
		"simple",
		"Trivial test of recursion.  main calls A, and A calls A.\n",
		{ simple_text, NULL }
	},
	{
		"unreachable",
		"Shader contains a function A that calls itself, but A is\n"
		"trivially unreachable from main.\n",
		{ unreachable_text, NULL }
	},
	{
		"unreachable-constant-folding",
		"Shader contains a function A that calls itself, but A is\n"
		"unreachable from main if a constant folding is performed\n"
		"before the check for recursion.\n",
		{ unreachable_opt_text, NULL }
	},
	{
		"indirect",
		"Trivial test of indirect recursion.  main calls A, A calls\n"
		"B, and B calls A.\n",
		{ indirect_text, NULL }
	},
	{
		"indirect-separate",
		"Trivial test of indirect recursion.  main calls A, A calls\n"
		"B, and B calls A.  A and B are in separate compilation\n"
		"units.\n",
		{ indirect_sep1_text, indirect_sep2_text, NULL }
	},
	{
		"indirect-complex",
		"Two functions A and B are statically mutually recursive,\n"
		"but the parameters passed to the functions ensure that no\n"
		"recursion actually occurs.  This is still an error.\n",
		{ indirect_complex_text, NULL }
	},
	{
		"indirect-complex-separate",
		"Two functions A and B are statically mutually recursive,\n"
		"but the parameters passed to the functions ensure that no\n"
		"recursion actually occurs.  This is still an error.  A and\n"
		"B are in separate compilation units.\n",
		{ indirect_complex1_text, indirect_complex2_text, NULL }
	},
};

enum piglit_result
piglit_display(void)
{
	return PIGLIT_FAIL;
}

bool
do_named_test(const char *name)
{
	bool pass = true;
	unsigned i;
	unsigned j;

	for (i = 0; i < ARRAY_SIZE(all_tests); i++) {
		GLint ok;
		GLuint prog;
		GLint size;

		if (name != NULL && strcmp(name, all_tests[i].name) != 0)
			continue;

		printf("Starting test \"%s\":\n", all_tests[i].name);

		prog = glCreateProgram();

		for (j = 0; all_tests[i].shader_source[j] != NULL; j++) {
			GLuint vs;

			vs = glCreateShader(GL_VERTEX_SHADER);
			glShaderSource(vs, 1,
					    (const GLchar **)
					    & all_tests[i].shader_source[j],
					    NULL);
			glCompileShader(vs);

			/* Some drivers return a size of 1 for an empty log.
			 * This is the size of a log that contains only a
			 * terminating NUL character.
			 */
			printf("Compilation info log for shader %u:\n", j);
			glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &size);
			if (size > 1) {
				GLchar *info = malloc(size);

				glGetShaderInfoLog(vs, size, NULL, info);
				printf("%s\n", info);
				free(info);
			} else {
				printf("<empty log>\n\n");
			}

			glAttachShader(prog, vs);
			glDeleteShader(vs);
		}

		glLinkProgram(prog);

		/* Some drivers return a size of 1 for an empty log.  This is
		 * the size of a log that contains only a terminating NUL
		 * character.
		 */
		printf("Link info log:\n");
		glGetProgramiv(prog, GL_INFO_LOG_LENGTH, &size);
		if (size > 1) {
			GLchar *info = malloc(size);
			glGetProgramInfoLog(prog, size, NULL, info);
			printf("%s\n", info);
			free(info);
		} else {
			printf("<empty log>\n\n");
		}

		glGetProgramiv(prog, GL_LINK_STATUS, &ok);
		if (ok) {
			fprintf(stderr,
				"Shader with recursion compiled and linked, "
				"but it should have failed.\n");
			pass = false;
		}
		printf("Done with test \"%s\".\n\n", all_tests[i].name);

		glDeleteProgram(prog);

		if (name != NULL)
			break;
	}

	return pass;
}

void
piglit_init(int argc, char **argv)
{
	bool pass = true;
	const char *glsl_version_string;

	piglit_require_vertex_shader();

	glsl_version_string = (const char *)
		glGetString(GL_SHADING_LANGUAGE_VERSION);
	if (strtod(glsl_version_string, NULL) < 1.2) {
		printf("Requires GLSL 1.20 (have version `%s')\n",
		       glsl_version_string);
		piglit_report_result(PIGLIT_SKIP);
	}

	if (argc == 1) {
		pass = do_named_test(NULL);
	} else {
		int i;
		for (i = 1; i < argc; i++) {
			pass = do_named_test(argv[i]) && pass;
		}
	}

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}