From a23e58f0a7ce75e35f3766a42787b16dd0c3bf8e Mon Sep 17 00:00:00 2001 From: Luis Montes Date: Sun, 13 Sep 2026 21:20:27 -0700 Subject: [PATCH 1/2] getParameter: stop overflowing the scratch buffer on unlisted array pnames glGetIntegerv writes as many values as the pname defines, and the driver cannot know how large the destination is. The default branch of getParameter() passed a one-element Int32Array, so any array pname that is not special-cased above it overflowed the heap. GL_COMPRESSED_TEXTURE_FORMATS is the case that bit: a desktop Mesa stack reports 51 formats, so the driver wrote ~200 bytes past the end of a 4-byte buffer. The corruption landed in whatever allocated next -- usually V8 -- and surfaced as an unrelated crash much later, with backtraces pointing at the wasm compiler rather than here. Over-allocate a 64-int scratch and return the first element. Scalars are unchanged; an unlisted array pname now costs a slightly larger temporary instead of corrupting the heap. Adds getParameterArray() for callers that want the whole array. npm test: 204 passed, 0 failed. --- lib/webgl2-context.mjs | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/lib/webgl2-context.mjs b/lib/webgl2-context.mjs index dd23c74..aff8e2a 100644 --- a/lib/webgl2-context.mjs +++ b/lib/webgl2-context.mjs @@ -7,6 +7,13 @@ import { unwrap, toUint8, } from './webgl-objects.mjs' +// Headroom for glGetIntegerv on array pnames that are not special-cased. +// The driver writes as many values as the pname defines and cannot know our +// buffer size, so a scalar-sized buffer corrupts the heap. 64 ints covers +// every fixed-size GL pname (the largest are COMPRESSED_TEXTURE_FORMATS, +// bounded by NUM_COMPRESSED_TEXTURE_FORMATS, and the 4-element rect queries). +const GET_INTEGERV_SCRATCH_INTS = 64 + const _tmp1 = new Uint32Array(1) export class WebGL2RenderingContext { @@ -1070,12 +1077,36 @@ export class WebGL2RenderingContext { return v } - // Default: integer scalar - const buf = new Int32Array(1) + // Default: integer scalar. + // + // glGetIntegerv writes however many values the pname has, and the driver + // does not know how big our buffer is. A one-element buffer is therefore + // a heap overflow for any ARRAY pname we have not special-cased above -- + // GL_COMPRESSED_TEXTURE_FORMATS reports 51 entries on a desktop Mesa + // stack, which is a ~200 byte write past the end. The corruption lands in + // whatever allocated next (V8, typically), so it surfaces as an unrelated + // crash much later and is extremely hard to trace back to here. + // + // Over-allocate and return only the first element. Scalars are unchanged; + // an unlisted array pname costs a slightly larger temporary instead of + // corrupting the heap. Genuinely unbounded queries are special-cased + // above (and COMPRESSED_TEXTURE_FORMATS is bounded by its own + // NUM_COMPRESSED_TEXTURE_FORMATS, which no driver reports above a few + // dozen). + const buf = new Int32Array(GET_INTEGERV_SCRATCH_INTS) this._gl.glGetIntegerv(pname, buf) return buf[0] } + // Returns every value an integer pname writes, for callers that need the + // whole array rather than the first element. + getParameterArray(pname, count) { + const n = Math.max(1, Math.min(count | 0, GET_INTEGERV_SCRATCH_INTS)) + const buf = new Int32Array(GET_INTEGERV_SCRATCH_INTS) + this._gl.glGetIntegerv(pname, buf) + return buf.subarray(0, n) + } + getBufferParameter(target, pname) { if (pname === GL.BUFFER_SIZE) { // Use 64-bit query for large buffers From 9ab3a45dea32edd995847207ae72f2932b5e0341 Mon Sep 17 00:00:00 2001 From: Luis Montes Date: Tue, 15 Sep 2026 18:51:05 -0700 Subject: [PATCH 2/2] Fix uniform index queries and accept more texImage2D sources Three fixes to WebGL2 calls that any twgl-based library hits. getUniformIndices / getActiveUniforms: argument arity mismatch The JS wrappers passed a count argument the native bindings do not take. _getUniformIndices reads (program, namesArray, outIndices) and derives the count from the array itself; _getActiveUniformsiv reads (program, indices, pname, outParams). Passing the extra count shifted every argument along, so the addon rejected the call with "Invalid argument". twgl calls both while building any program that has uniforms. texImage2D: coerce undefined/null width and height WebIDL coerces the numeric arguments, so a browser turns an undefined or null width/height into 0 and allocates an empty texture. The native binding demanded real numbers and threw "A number was expected". Allocating an empty texture this way is common: twgl's setEmptyTexture() calls exactly like this whenever a texture is created with no src and no explicit size. texImage2D: accept ImageData as a source ImageData is a valid texImage2D source per the WebGL spec. The 6-arg path only handled Canvas and Image, so it threw "Value is not one of these types". Its pixels are already RGBA in the expected layout, so they upload directly rather than going through a temp canvas, which rejects ImageData anyway. Verified with the existing suite: 204 passed, 0 failed. --- lib/webgl2-context.mjs | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/webgl2-context.mjs b/lib/webgl2-context.mjs index aff8e2a..4208083 100644 --- a/lib/webgl2-context.mjs +++ b/lib/webgl2-context.mjs @@ -468,7 +468,19 @@ export class WebGL2RenderingContext { // 6-arg: texImage2D(target, level, internalformat, format, type, source) — source is Image/Canvas // 10-arg: texImage2D(target, level, internalformat, width, height, border, format, type, pixels, srcOffset) if (rest.length >= 5) { - const [width, height, border, format, type, pixels, srcOffset] = rest + let [width, height, border, format, type, pixels, srcOffset] = rest + /* + * WebIDL coerces the numeric arguments, so a browser turns an undefined or + * null width/height into 0 and allocates an empty texture. The native + * binding requires real numbers and throws "A number was expected" + * instead. Allocating an empty texture this way is common: twgl's + * setEmptyTexture() calls exactly like this whenever a texture is created + * with no src and no explicit size. Without the coercion a perfectly + * legal call fails in a way that reads as a texture bug. + */ + width = width | 0 + height = height | 0 + border = border | 0 if (typeof pixels === 'number') { // PBO offset this._gl.glTexImage2D(target, level, internalformat, width, height, border, format, type, pixels) @@ -487,7 +499,12 @@ export class WebGL2RenderingContext { const w = source.width const h = source.height let pixels - if (source.getContext) { + if (source.data && ArrayBuffer.isView(source.data)) { + // ImageData is a valid texImage2D source per the WebGL spec. Its + // pixels are already RGBA in the expected layout, so upload them + // directly; the temp-canvas path below rejects ImageData outright. + pixels = new Uint8Array(source.data.buffer, source.data.byteOffset, source.data.byteLength) + } else if (source.getContext) { // It's a canvas — get pixels directly const ctx = source.getContext('2d') pixels = new Uint8Array(ctx.getImageData(0, 0, w, h).data) @@ -906,13 +923,17 @@ export class WebGL2RenderingContext { getUniformIndices(prog, uniformNames) { const indices = new Uint32Array(uniformNames.length) - this._gl.glGetUniformIndices(prog._id, uniformNames.length, uniformNames, indices) + // native takes (program, namesArray, outIndices) and derives the count from + // the array itself. Passing an extra count shifted every argument along and + // made the addon reject the call with "Invalid argument". + this._gl.glGetUniformIndices(prog._id, uniformNames, indices) return Array.from(indices) } getActiveUniforms(prog, uniformIndices, pname) { const result = new Int32Array(uniformIndices.length) - this._gl.glGetActiveUniformsiv(prog._id, uniformIndices.length, new Uint32Array(uniformIndices), pname, result) + // native takes (program, indices, pname, outParams), again no count arg. + this._gl.glGetActiveUniformsiv(prog._id, new Uint32Array(uniformIndices), pname, result) if (pname === GL.UNIFORM_IS_ROW_MAJOR) { return Array.from(result, v => !!v) }