Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions src/lib/libwebgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the GL_TRACK_ERRORS just wrao the call to recordError?

i.e. My understanding is that turning on or off GL_TRACK_ERRORS doesn't effect whether a given call will crash or not, just whether or not glGetError will report the errors?

So maybe we should still return early if cb is missing here?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the GL_TRACK_ERRORS just wrao the call to recordError?

That would be nice if we did require Closure compiler.. but for example Unity is not currently able to use it, so there would be tons of GL.recordError()s still left behind.

My understanding is that turning on or off GL_TRACK_ERRORS doesn't effect whether a given call will crash or not, just whether or not glGetError will report the errors?

GL_TRACK_ERRORS=1 would be that "I want benign failures" mode, and GL_TRACK_ERRORS=0 would be a "I've tested my code to not need all that pedantic error tracking" mode.

So in the GL_TRACK_ERRORS=0 mode, we'd generally assume that user doesn't run into these types of errors in the first place, so a if (!cb) return; would then be dead code.

}
#endif
cb.enabled = true;
#endif
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion src/lib/libwebgl2.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
147 changes: 147 additions & 0 deletions test/browser/webgl_vertex_attrib_invalid_index.c
Original file line number Diff line number Diff line change
@@ -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 <assert.h>
#include <stdio.h>
#include <emscripten/html5.h>

#ifdef TEST_WEBGL2
#include <GLES3/gl3.h>
#define VERSION "#version 300 es\n"
#define ATTRIBUTE "in"
#else
#include <GLES2/gl2.h>
#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");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test program seem fairly involved for something that is just checking if a given call errors on a bad index.

Can it be simplified? Maybe just a few lines of code and achieve the same result?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could test just bad API calls before compiling a program, but then it would be only testing a scenario where user doesn't even have a valid program set up. It does seem more robust to actually test that having a valid program, and then asking for invalid indices in the code.

return 0;
}
4 changes: 2 additions & 2 deletions test/codesize/test_codesize_hello_dylink_all.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 16 additions & 0 deletions test/test_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'],),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check the -sGL_TRACK_ERRORS=0 too and maybe sure we don't just crash/trap in that case?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to throw an exception in that case. We do have other locations that use GL_TRACK_ERRORS like that, assuming that the errors don't occur in the first place.

})
@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'])

Expand Down
Loading