test buffer overflow with drawElements offset TypedArrays25ms ‣
const {gl, tagObject} = createContext();
const vs = `
attribute vec4 position;
void main() {
gl_Position = position;
}
`;
const fs = `
precision mediump float;
void main() {
gl_FragColor = vec4(1, 0, 0, 1);
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
gl.useProgram(prg);
function makeBuffer(gl, name, data, target = gl.ARRAY_BUFFER) {
const buf = gl.createBuffer();
tagObject(buf, name);
gl.bindBuffer(target, buf);
gl.bufferData(target, data, gl.STATIC_DRAW);
}
function makeBufferAndSetAttrib(gl, prg, name, size, data) {
makeBuffer(gl, name, data);
const loc = gl.getAttribLocation(prg, name);
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, size, gl.FLOAT, false, 0, 0);
}
const arrayBuffer = new ArrayBuffer(12 * 4 + 6 * 2);
const positions = new Float32Array(arrayBuffer, 0, 12);
const indices = new Uint16Array(arrayBuffer, 12 * 4, 6);
positions.set([-1, 1, 0, 1, 1, 0, 1, -1, 0, -1, -1, 0]);
indices.set([0, 1, 2, 0, 2, 3]);
makeBufferAndSetAttrib(gl, prg, 'position', 3, positions);
makeBuffer(gl, 'indices', indices, gl.ELEMENT_ARRAY_BUFFER);
gl.useProgram(prg);
assertDoesNotThrow(() => {
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
});
test buffer overflow with drawElements offset TypedArrays using bufferSubData ‣
Error: pixel at 0,0 expected: 0,255,0,255, was 0,0,0,0
at checkDest (https://greggman.github.io/webgl-lint/test/webgl.js:67:17)
at Context.<anonymous> (https://greggman.github.io/webgl-lint/test/tests/buffer-overflow-tests.js:273:5)
at callFn (https://greggman.github.io/webgl-lint/test/mocha.js:13124:23)
at Runnable$3.run (https://greggman.github.io/webgl-lint/test/mocha.js:13112:7)
at Runner.runTest (https://greggman.github.io/webgl-lint/test/mocha.js:14657:12)
at https://greggman.github.io/webgl-lint/test/mocha.js:14780:14
at next (https://greggman.github.io/webgl-lint/test/mocha.js:14572:16)
at https://greggman.github.io/webgl-lint/test/mocha.js:14582:9
at next (https://greggman.github.io/webgl-lint/test/mocha.js:14465:16)
at https://greggman.github.io/webgl-lint/test/mocha.js:14550:7
const {gl, tagObject} = createContext();
const vs = `
attribute vec4 position;
void main() {
gl_Position = position;
}
`;
const fs = `
precision mediump float;
void main() {
gl_FragColor = vec4(0, 1, 0, 1);
}
`;
const prg = twgl.createProgram(gl, [vs, fs]);
gl.useProgram(prg);
function makeBuffer(gl, name, data, target = gl.ARRAY_BUFFER) {
const buf = gl.createBuffer();
tagObject(buf, name);
gl.bindBuffer(target, buf);
gl.bufferData(target, data.byteLength, gl.STATIC_DRAW);
gl.bufferSubData(target, 0, data);
}
function makeBufferAndSetAttrib(gl, prg, name, size, data) {
makeBuffer(gl, name, data);
const loc = gl.getAttribLocation(prg, name);
gl.enableVertexAttribArray(loc);
gl.vertexAttribPointer(loc, size, gl.FLOAT, false, 0, 0);
}
const arrayBuffer = new ArrayBuffer(12 * 4 + 6 * 2);
const positions = new Float32Array(arrayBuffer, 0, 12);
const indices = new Uint16Array(arrayBuffer, 12 * 4, 6);
positions.set([
-1, 1, 0,
1, 1, 0,
1, -1, 0,
-1, -1, 0,
]);
indices.set([0, 1, 2, 0, 2, 3]);
makeBufferAndSetAttrib(gl, prg, 'position', 3, positions);
makeBuffer(gl, 'indices', indices, gl.ELEMENT_ARRAY_BUFFER);
gl.useProgram(prg);
gl.clearColor(1, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0);
checkDest(gl, [0, 255, 0, 255]);