From 0a2b5ce42be2e1fd1fd25830ce79f762e01e2b58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jukka=20Jyl=C3=A4nki?= Date: Sat, 21 Nov 2026 18:43:03 +0200 Subject: [PATCH] Raise GL_INVALID_VALUE from an out of range vertex attribute index in -sFULL_ES2 Fixes: #14589 Co-Authored-By: Claude Fable 5.2 --- src/lib/libwebgl.js | 24 ++- src/lib/libwebgl2.js | 8 +- .../webgl_vertex_attrib_invalid_index.c | 147 ++++++++++++++++++ .../test_codesize_hello_dylink_all.json | 4 +- test/test_browser.py | 16 ++ 5 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 test/browser/webgl_vertex_attrib_invalid_index.c diff --git a/src/lib/libwebgl.js b/src/lib/libwebgl.js index 8a09b5fda4580..cd9814524bd4b 100644 --- a/src/lib/libwebgl.js +++ b/src/lib/libwebgl.js @@ -3684,8 +3684,14 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { glVertexAttribPointer: (index, size, type, normalized, stride, ptr) => { #if FULL_ES2 var cb = GL.currentContext.clientBuffers[index]; +#if GL_TRACK_ERRORS + if (!cb) { #if GL_ASSERTIONS - assert(cb, index); + err(`GL_INVALID_VALUE in glVertexAttribPointer(index=${index}): index must be in the range 0 <= index < GL_MAX_VERTEX_ATTRIBS (${GL.currentContext.maxVertexAttribs})!`); +#endif + GL.recordError(GL_INVALID_VALUE); + return; + } #endif if (!GLctx.currentArrayBufferBinding) { cb.size = size; @@ -3710,8 +3716,14 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { glEnableVertexAttribArray: (index) => { #if FULL_ES2 var cb = GL.currentContext.clientBuffers[index]; +#if GL_TRACK_ERRORS + if (!cb) { #if GL_ASSERTIONS - assert(cb, index); + err(`GL_INVALID_VALUE in glEnableVertexAttribArray(index=${index}): index must be in the range 0 <= index < GL_MAX_VERTEX_ATTRIBS (${GL.currentContext.maxVertexAttribs})!`); +#endif + GL.recordError(GL_INVALID_VALUE); + return; + } #endif cb.enabled = true; #endif @@ -3721,8 +3733,14 @@ for (let i = 0; i <= GL_POOL_TEMP_BUFFERS_SIZE; ++i) { glDisableVertexAttribArray: (index) => { #if FULL_ES2 var cb = GL.currentContext.clientBuffers[index]; +#if GL_TRACK_ERRORS + if (!cb) { #if GL_ASSERTIONS - assert(cb, index); + err(`GL_INVALID_VALUE in glDisableVertexAttribArray(index=${index}): index must be in the range 0 <= index < GL_MAX_VERTEX_ATTRIBS (${GL.currentContext.maxVertexAttribs})!`); +#endif + GL.recordError(GL_INVALID_VALUE); + return; + } #endif cb.enabled = false; #endif diff --git a/src/lib/libwebgl2.js b/src/lib/libwebgl2.js index 8bfe09c19da3b..189f47ab30554 100644 --- a/src/lib/libwebgl2.js +++ b/src/lib/libwebgl2.js @@ -888,8 +888,14 @@ var LibraryWebGL2 = { glVertexAttribIPointer: (index, size, type, stride, ptr) => { #if FULL_ES3 var cb = GL.currentContext.clientBuffers[index]; +#if GL_TRACK_ERRORS + if (!cb) { #if GL_ASSERTIONS - assert(cb, index); + err(`GL_INVALID_VALUE in glVertexAttribIPointer(index=${index}): index must be in the range 0 <= index < GL_MAX_VERTEX_ATTRIBS (${GL.currentContext.maxVertexAttribs})!`); +#endif + GL.recordError(GL_INVALID_VALUE); + return; + } #endif if (!GLctx.currentArrayBufferBinding) { cb.size = size; diff --git a/test/browser/webgl_vertex_attrib_invalid_index.c b/test/browser/webgl_vertex_attrib_invalid_index.c new file mode 100644 index 0000000000000..599da34f04ba5 --- /dev/null +++ b/test/browser/webgl_vertex_attrib_invalid_index.c @@ -0,0 +1,147 @@ +// Copyright 2026 The Emscripten Authors. All rights reserved. +// Emscripten is available under two separate licenses, the MIT license and the +// University of Illinois/NCSA Open Source License. Both these licenses can be +// found in the LICENSE file. + +// Tests that calling the vertex attribute array functions with an attribute +// index that is out of range generates GL_INVALID_VALUE, like it does in +// native OpenGL ES, instead of throwing a JavaScript exception. +// See https://github.com/emscripten-core/emscripten/issues/14589 +// +// A common way to end up with such an index is to pass on the -1 that +// glGetAttribLocation() returns for an attribute that the shader declares, but +// does not use. +// +// In -sFULL_ES2 and -sFULL_ES3 builds the Emscripten GL library tracks the +// state of each vertex attribute to emulate client-side vertex arrays, so +// there the index needs to be validated by the GL library itself. + +#include +#include +#include + +#ifdef TEST_WEBGL2 +#include +#define VERSION "#version 300 es\n" +#define ATTRIBUTE "in" +#else +#include +#define VERSION +#define ATTRIBUTE "attribute" +#endif + +static GLuint compile_shader(GLenum type, const char *src) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + GLint compiled = 0; + glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); + assert(compiled && "Shader compile failed"); + return shader; +} + +// Calls each of the vertex attribute array functions with the given invalid +// attribute index. +static void test_invalid_index(GLuint index) { + printf("Testing invalid vertex attribute index %d\n", (int)index); + + glEnableVertexAttribArray(index); + assert(glGetError() == GL_INVALID_VALUE); + // glGetError() clears the error. + assert(glGetError() == GL_NO_ERROR); + + glVertexAttribPointer(index, 2, GL_FLOAT, GL_FALSE, 0, 0); + assert(glGetError() == GL_INVALID_VALUE); + +#ifdef TEST_WEBGL2 + glVertexAttribIPointer(index, 2, GL_INT, 0, 0); + assert(glGetError() == GL_INVALID_VALUE); +#endif + + glDisableVertexAttribArray(index); + assert(glGetError() == GL_INVALID_VALUE); +} + +int main() { + EmscriptenWebGLContextAttributes attr; + emscripten_webgl_init_context_attributes(&attr); +#ifdef TEST_WEBGL2 + attr.majorVersion = 2; +#endif + EMSCRIPTEN_WEBGL_CONTEXT_HANDLE ctx = emscripten_webgl_create_context("#canvas", &attr); + assert(ctx > 0); + emscripten_webgl_make_context_current(ctx); + + // The attribute 'unused' is declared, but not used, so it is not an active + // attribute of the linked program. + GLuint vs = compile_shader(GL_VERTEX_SHADER, + VERSION + ATTRIBUTE " vec2 pos;\n" + ATTRIBUTE " vec2 unused;\n" + "void main() { gl_Position = vec4(pos, 0.0, 1.0); }\n"); + + GLuint fs = compile_shader(GL_FRAGMENT_SHADER, +#ifdef TEST_WEBGL2 + VERSION + "precision mediump float;\n" + "out vec4 color;\n" + "void main() { color = vec4(1.0); }\n"); +#else + "precision mediump float;\n" + "void main() { gl_FragColor = vec4(1.0); }\n"); +#endif + + GLuint program = glCreateProgram(); + glAttachShader(program, vs); + glAttachShader(program, fs); + glLinkProgram(program); + + GLint linked = 0; + glGetProgramiv(program, GL_LINK_STATUS, &linked); + assert(linked && "Program link failed"); + + glUseProgram(program); + + GLint loc_pos = glGetAttribLocation(program, "pos"); + GLint loc_unused = glGetAttribLocation(program, "unused"); + assert(loc_pos != -1); + assert(loc_unused == -1); + + GLint max_vertex_attribs = 0; + glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &max_vertex_attribs); + assert(max_vertex_attribs >= 8); + assert(glGetError() == GL_NO_ERROR); + + static const float vertices[] = { -1.f, -1.f, 1.f, -1.f, 0.f, 1.f }; + GLuint vbo; + glGenBuffers(1, &vbo); + + // Test both when specifying the vertex data from a buffer object, and when + // no buffer is bound (which is the client-side vertex array code path in + // -sFULL_ES2 builds). + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + assert(glGetError() == GL_NO_ERROR); + test_invalid_index(loc_unused); + test_invalid_index(max_vertex_attribs); + + glBindBuffer(GL_ARRAY_BUFFER, 0); + test_invalid_index(loc_unused); + test_invalid_index(max_vertex_attribs); + + // The failed calls above did not disturb the state of the valid vertex + // attributes, so rendering with them still works. + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glEnableVertexAttribArray(loc_pos); + glVertexAttribPointer(loc_pos, 2, GL_FLOAT, GL_FALSE, 0, 0); + glDrawArrays(GL_TRIANGLES, 0, 3); + assert(glGetError() == GL_NO_ERROR); + + // The highest valid attribute index is still accepted. + glEnableVertexAttribArray(max_vertex_attribs - 1); + glDisableVertexAttribArray(max_vertex_attribs - 1); + assert(glGetError() == GL_NO_ERROR); + + printf("Test passed!\n"); + return 0; +} diff --git a/test/codesize/test_codesize_hello_dylink_all.json b/test/codesize/test_codesize_hello_dylink_all.json index f545d480e709e..2af1e10245a21 100644 --- a/test/codesize/test_codesize_hello_dylink_all.json +++ b/test/codesize/test_codesize_hello_dylink_all.json @@ -1,7 +1,7 @@ { - "a.out.js": 268338, + "a.out.js": 268403, "a.out.nodebug.wasm": 535698, - "total": 804036, + "total": 804101, "sent": [ "IMG_Init", "IMG_Load", diff --git a/test/test_browser.py b/test/test_browser.py index 67a220b16b202..a7a2bd0a473e9 100644 --- a/test/test_browser.py +++ b/test/test_browser.py @@ -1773,6 +1773,22 @@ def test_gles2_emulation(self, args): def test_clientside_vertex_arrays_es3(self): self.reftest('clientside_vertex_arrays_es3.c', 'gl_triangle.png', cflags=['-sFULL_ES3', '-sUSE_GLFW=3', '-lglfw', '-lGLESv2']) + # Tests that passing an out-of-range vertex attribute index, e.g. the -1 that + # glGetAttribLocation() returns for an inactive attribute, raises + # GL_INVALID_VALUE instead of throwing an exception (#14589) + @parameterized({ + '': ([],), + 'full_es2': (['-sFULL_ES2'],), + 'full_es2_assertions': (['-sFULL_ES2', '-sGL_ASSERTIONS'],), + 'full_es3': (['-sFULL_ES3', '-DTEST_WEBGL2'],), + 'full_es3_assertions': (['-sFULL_ES3', '-DTEST_WEBGL2', '-sGL_ASSERTIONS'],), + }) + @requires_graphics_hardware + def test_webgl_vertex_attrib_invalid_index(self, args): + if '-DTEST_WEBGL2' in args and webgl2_disabled(): + self.skipTest('This test requires WebGL2 to be available') + self.btest_exit('webgl_vertex_attrib_invalid_index.c', cflags=['-lGL'] + args) + def test_emscripten_api(self): self.btest_exit('emscripten_api_browser.c', cflags=['-lSDL'])