Compare commits

...
2 changed files with 365 additions and 0 deletions

No files matched your search

+84
View File
@@ -15,6 +15,9 @@ known-bad:
same text canvas: what separates two machines is how they draw, never a
per-seed change at readback.
* A toDataURL PNG carries the deBG chunk retail 151 writes.
* A WebGL canvas that is not premultiplied (premultipliedAlpha:false, half
transparent) reads the same pixels through toDataURL and toBlob, noised with the
engine on (hw_seed > 0); the control is the engine off (hw_seed 0: as drawn).
Runs against about:blank and a local http page, no network/proxy. Part of the
e2e release gate.
@@ -154,3 +157,84 @@ def test_two_seeds_on_the_same_machine_draw_the_same_text(firefox_binary):
assert hashlib.sha256(bytes(a)).digest() == hashlib.sha256(bytes(b)).digest(), (
"two seeds declaring the same display scale and ClearType tuning read "
"different text pixels: something per-seed still changes a 2D canvas")
_NON_PREMULTIPLIED_JS = """async () => {
const c = document.createElement('canvas'); c.width = 128; c.height = 128;
const gl = c.getContext('webgl', {premultipliedAlpha: false, alpha: true,
preserveDrawingBuffer: true, antialias: true});
if (!gl) return null;
const alpha = 128 / 255;
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, 'attribute vec2 p; attribute vec3 c; varying vec3 v;'
+ ' void main(){v=c;gl_Position=vec4(p,0.,1.);}');
gl.compileShader(vs);
const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, 'precision mediump float; varying vec3 v; void main(){gl_FragColor=vec4(v,'
+ alpha.toFixed(8) + ');}');
gl.compileShader(fs);
const pr = gl.createProgram(); gl.attachShader(pr, vs); gl.attachShader(pr, fs);
gl.linkProgram(pr); gl.useProgram(pr);
const b = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(
[-0.9, -0.8, 1, 0, 0, 0.85, -0.6, 0, 1, 0, 0.1, 0.9, 0, 0, 1]), gl.STATIC_DRAW);
const pl = gl.getAttribLocation(pr, 'p'), cl = gl.getAttribLocation(pr, 'c');
gl.enableVertexAttribArray(pl); gl.vertexAttribPointer(pl, 2, gl.FLOAT, false, 20, 0);
gl.enableVertexAttribArray(cl); gl.vertexAttribPointer(cl, 3, gl.FLOAT, false, 20, 8);
gl.clearColor(1, 1, 1, alpha); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.finish();
const url = c.toDataURL();
const blob = await new Promise(resolve => c.toBlob(resolve, 'image/png'));
const bytes = new Uint8Array(await blob.arrayBuffer());
let binary = '';
for (let i = 0; i < bytes.length; i += 8192) binary += String.fromCharCode.apply(null, bytes.subarray(i, i + 8192));
return {url, blob: btoa(binary), premultipliedAlpha: gl.getContextAttributes().premultipliedAlpha};
}"""
def _non_premultiplied(firefox_binary, hw_seed):
"""(the pixels of toDataURL, the pixels of toBlob) of one half-transparent, non-premultiplied WebGL canvas,
as 128x128 RGBA bytes, with the engine on (hw_seed > 0) or off (0)."""
import base64
from _page_bytes import unfiltered
with InvisiblePlaywright(
seed=7, binary_path=firefox_binary, headless=True,
extra_prefs={"zoom.stealth.fpp.hw_seed": hw_seed},
) as b:
p = b.new_context().new_page()
p.goto("about:blank", timeout=30_000)
got = p.evaluate(_NON_PREMULTIPLIED_JS)
assert got is not None, "webgl is unavailable: a WebGL canvas cannot be read"
assert got["premultipliedAlpha"] is False, "the context is premultiplied: %r" % got["premultipliedAlpha"]
images = [unfiltered(base64.b64decode(got["url"].split(",", 1)[1])), unfiltered(base64.b64decode(got["blob"]))]
for png in images:
assert (png["w"], png["h"], png["channels"]) == (128, 128, 4), png
return images[0]["data"], images[1]["data"]
@pytest.mark.e2e
def test_a_non_premultiplied_webgl_canvas_reads_the_same_through_todataurl_and_toblob(firefox_binary):
"""toDataURL and toBlob of a premultipliedAlpha:false WebGL canvas give the same pixels, noised with the engine on.
The canvas noise is applied to the snapshot with its alpha type: a context that is not premultiplied stores
the colours as drawn, and a reader that took it for premultiplied would noise (or convert) them wrongly.
Both PNG readers go through the same hook (GetImageBuffer, GetInputStream in ClientWebGLContext.cpp). The
canvas is half transparent (alpha 128/255, colours as drawn), so a premultiplied reading changes the bytes.
Negative control, in the same test: with the engine off the two readers are the bytes as drawn, their alpha is
the 128 drawn, and the engine-on PNG differs from them."""
on_url, on_blob = _non_premultiplied(firefox_binary, 12345)
off_url, off_blob = _non_premultiplied(firefox_binary, 0)
alphas = {off_url[i] for i in range(3, len(off_url), 4)}
assert alphas <= set(range(126, 131)), (
"engine off: the alpha of the PNG is %s, not the 128 that was drawn" % sorted(alphas))
differing = lambda a, b: sum(1 for x, y in zip(a, b) if x != y)
assert off_blob == off_url, (
"engine off: %d of %d bytes of toBlob differ from toDataURL" % (differing(off_blob, off_url), len(off_url)))
assert differing(on_url, off_url), (
"toDataURL of a non-premultiplied WebGL canvas is the same with the engine on and off: no noise reaches "
"the page, so comparing the two readers says nothing")
assert on_blob == on_url, (
"engine on: %d of %d bytes of toBlob differ from toDataURL of the same non-premultiplied WebGL canvas"
% (differing(on_blob, on_url), len(on_url)))
+281
View File
@@ -22,16 +22,35 @@ The clear colours are v/255 for integer v, so the expected byte is exactly v
whatever the GPU's rounding: a k/64 colour lands on 127.5 at k=32, where two
correct drivers may disagree.
Three more tests characterise the roads by which a page reads a WebGL canvas (the
engine's WebGL readback hooks: ClientWebGLContext.cpp and StealthWebGLReadback.h
of the fingerprint/canvas/noise-projection folder). Each reads with the engine on
(zoom.stealth.fpp.hw_seed > 0) and, as the negative control in the same test, with the
canvas noise declared off (zoom.stealth.canvas.substitute_pixels false, the engine still
on: the bytes as drawn), and asserts the two differ, or no noise reached
the road the test compares:
* readPixels into a PIXEL_PACK_BUFFER (WebGL2), read back with getBufferSubData,
gives the bytes of readPixels into an array.
* A frame of canvas.captureStream() has the bytes of toDataURL of the same canvas.
* readPixels of a rectangle partly outside the framebuffer, and one with
PACK_SKIP_PIXELS / PACK_SKIP_ROWS / PACK_ROW_LENGTH, change only the bytes the
read wrote: the rest of the page's array stays what the page passed in, and the
bytes written are those of a plain read of the same pixels.
Kept in its own file: it launches its own short-lived browsers, so it must not
run alongside another module's shared browser (the Playwright sync API cannot
nest). Run: pytest tests/test_webgl_noise_active.py -m e2e -v
"""
from __future__ import annotations
import contextlib
import hashlib
import pytest
from _page_bytes import unfiltered
from _waits import wait_until
from invisible_playwright import InvisiblePlaywright
_GRADIENT_JS = """() => {
@@ -118,3 +137,265 @@ def test_a_flat_clear_reads_back_exactly_as_cleared(firefox_binary, seed):
"%d of 4096 cleared pixels do not read back as cleared (column, row, "
"cleared, read): %s. A clear is the same bytes on every GPU, so noise "
"on it is a tell" % (len(moved), moved[:5]))
#: A hw_seed no runner has by accident (the engine is on when it is positive). The control of the tests below keeps the engine ON, with the
#: same declared GPU, and declares only the canvas noise off (``zoom.stealth.canvas.substitute_pixels`` false): a launch with hw_seed 0 would
#: change the declared GPU too, and a Windows runner without the engine has no WebGL2 at all, so the comparison would say nothing.
_ENGINE_ON = 12345
_ENGINE_OFF = 0 # "the noise is off": the engine stays on (see _page)
#: Draws what the tests below read: a triangle with per-vertex colours and antialiased edges on a white clear,
#: the render of _GRADIENT_JS, where a device varies and so where the noise lands. ``scene(canvas, kind, attrs, alpha)``
#: returns the context, drawn and finished, or null when there is no such context.
_SCENE_JS = """
const scene = (c, kind, attrs, alpha) => {
const gl = c.getContext(kind, attrs);
if (!gl) return null;
const vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, 'attribute vec2 p; attribute vec3 c; varying vec3 v;'
+ ' void main(){v=c;gl_Position=vec4(p,0.,1.);}');
gl.compileShader(vs);
const fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, 'precision mediump float; varying vec3 v; void main(){gl_FragColor=vec4(v,'
+ alpha.toFixed(8) + ');}');
gl.compileShader(fs);
const pr = gl.createProgram(); gl.attachShader(pr, vs); gl.attachShader(pr, fs);
gl.linkProgram(pr); gl.useProgram(pr);
const b = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, b);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(
[-0.9, -0.8, 1, 0, 0, 0.85, -0.6, 0, 1, 0, 0.1, 0.9, 0, 0, 1]), gl.STATIC_DRAW);
const pl = gl.getAttribLocation(pr, 'p'), cl = gl.getAttribLocation(pr, 'c');
gl.enableVertexAttribArray(pl); gl.vertexAttribPointer(pl, 2, gl.FLOAT, false, 20, 0);
gl.enableVertexAttribArray(cl); gl.vertexAttribPointer(cl, 3, gl.FLOAT, false, 20, 8);
gl.clearColor(1, 1, 1, alpha); gl.clear(gl.COLOR_BUFFER_BIT); gl.drawArrays(gl.TRIANGLES, 0, 3);
gl.finish();
return gl;
};
"""
@contextlib.contextmanager
def _page(firefox_binary, hw_seed: int):
"""One short-lived browser at a blank page, the engine always on: with the noise on (hw_seed > 0) or with only the canvas noise
declared off (``_ENGINE_OFF``: ``zoom.stealth.canvas.substitute_pixels`` false, the bytes as drawn by the same declared GPU)."""
prefs = {"zoom.stealth.fpp.hw_seed": _ENGINE_ON}
if hw_seed == _ENGINE_OFF:
prefs["zoom.stealth.canvas.substitute_pixels"] = False
with InvisiblePlaywright(
seed=7, binary_path=firefox_binary, headless=True,
extra_prefs=prefs,
) as b:
p = b.new_context().new_page()
p.goto("about:blank", timeout=30_000)
yield p
def _differing(a, b) -> int:
"""How many bytes of two equally long sequences differ."""
assert len(a) == len(b), (len(a), len(b))
return sum(1 for x, y in zip(a, b) if x != y)
_PBO_JS = "() => {" + _SCENE_JS + """
const W = 128, H = 128, OFFSET = 16;
const c = document.createElement('canvas'); c.width = W; c.height = H;
const gl = scene(c, 'webgl2', {preserveDrawingBuffer: true, antialias: true}, 1);
if (!gl) return null;
while (gl.getError()) {}
const into = new Uint8Array(W * H * 4);
gl.readPixels(0, 0, W, H, gl.RGBA, gl.UNSIGNED_BYTE, into);
const pbo = gl.createBuffer();
gl.bindBuffer(gl.PIXEL_PACK_BUFFER, pbo);
gl.bufferData(gl.PIXEL_PACK_BUFFER, OFFSET + W * H * 4, gl.STREAM_READ);
gl.readPixels(0, 0, W, H, gl.RGBA, gl.UNSIGNED_BYTE, OFFSET);
const error = gl.getError();
const back = new Uint8Array(OFFSET + W * H * 4);
gl.getBufferSubData(gl.PIXEL_PACK_BUFFER, 0, back);
gl.bindBuffer(gl.PIXEL_PACK_BUFFER, null);
return {error, into: Array.from(into), pbo: Array.from(back.subarray(OFFSET)),
head: Array.from(back.subarray(0, OFFSET))};
}"""
def _pbo(firefox_binary, hw_seed: int):
with _page(firefox_binary, hw_seed) as p:
got = p.evaluate(_PBO_JS)
assert got is not None, "webgl2 is unavailable: readPixels into a PIXEL_PACK_BUFFER cannot be read"
assert got["error"] == 0, "readPixels into a PIXEL_PACK_BUFFER raised 0x%x" % got["error"]
assert got["head"] == [0] * 16, "the 16 bytes before the offset of the read were written: %r" % got["head"]
return bytes(got["into"]), bytes(got["pbo"])
@pytest.mark.e2e
def test_readpixels_into_a_pixel_pack_buffer_reads_as_into_an_array(firefox_binary):
"""The two roads a page reads WebGL2 pixels by give the same bytes, with the engine on and off.
With the engine on the PIXEL_PACK_BUFFER road (the host writes the buffer, the engine rewrites it with the noise
row by row) must give what readPixels into an array gives. With the engine off both are the bytes as drawn, and
the array differs from the engine-on array: the noise reaches the page on the array road, so the equality above is
not two untouched reads."""
on_array, on_pbo = _pbo(firefox_binary, _ENGINE_ON)
off_array, off_pbo = _pbo(firefox_binary, _ENGINE_OFF)
assert off_pbo == off_array, (
"engine off: %d of %d bytes of the buffer read differ from the array read of the same render"
% (_differing(off_pbo, off_array), len(off_array)))
assert _differing(on_array, off_array), (
"the array read is the same with the engine on and off: no noise reaches readPixels, so the comparison "
"of the two roads says nothing")
assert on_pbo == on_array, (
"engine on: %d of %d bytes read from a PIXEL_PACK_BUFFER differ from readPixels into an array; "
"off, the buffer differs from the array by %d bytes"
% (_differing(on_pbo, on_array), len(on_array), _differing(off_pbo, off_array)))
_CAPTURE_START_JS = "() => {" + _SCENE_JS + """
const c = document.createElement('canvas'); c.width = 64; c.height = 64;
const gl = scene(c, 'webgl', {preserveDrawingBuffer: true, antialias: true}, 1);
if (!gl) return false;
window.__png = c.toDataURL();
const stream = c.captureStream();
const v = document.createElement('video');
v.muted = true; v.autoplay = true; v.srcObject = stream;
document.body.appendChild(v);
window.__video = v; window.__track = stream.getVideoTracks()[0]; window.__frame = null;
v.play().catch(() => {});
return true;
}"""
#: One look at the video: when it holds a frame with something on it, copy it through a 2D canvas (which reads as
#: drawn) and say so; otherwise ask the track for a frame again.
_CAPTURE_POLL_JS = """() => {
if (window.__frame) return true;
const v = window.__video;
if (v.readyState >= 2 && v.videoWidth === 64 && v.videoHeight === 64) {
const c = document.createElement('canvas'); c.width = 64; c.height = 64;
const x = c.getContext('2d'); x.drawImage(v, 0, 0);
const d = x.getImageData(0, 0, 64, 64).data;
if (d.some(b => b !== 0)) { window.__frame = Array.from(d); return true; }
}
window.__track.requestFrame();
return false;
}"""
def _capture(firefox_binary, hw_seed: int):
"""(a frame of canvas.captureStream() as 64x64 RGBA bytes, the pixels of toDataURL of the same canvas)."""
import base64
with _page(firefox_binary, hw_seed) as p:
assert p.evaluate(_CAPTURE_START_JS), "webgl is unavailable: captureStream of a WebGL canvas cannot be read"
got = wait_until(lambda: p.evaluate(_CAPTURE_POLL_JS), 60, lambda: p.wait_for_timeout(100))
assert got, "the video of canvas.captureStream() never held a frame with something on it"
frame, url = p.evaluate("() => [window.__frame, window.__png]")
png = unfiltered(base64.b64decode(url.split(",", 1)[1]))
assert (png["w"], png["h"], png["channels"]) == (64, 64, 4), png
return bytes(frame), png["data"]
@pytest.mark.e2e
def test_a_capturestream_frame_reads_as_todataurl(firefox_binary):
"""A frame a page takes from canvas.captureStream() has the bytes toDataURL of the same canvas has.
captureStream is the one reader of the async snapshot, and a road of its own: the engine must apply the noise
there too, once. With the engine off the frame is the render as drawn and still equals toDataURL; with it on
the frame differs from the one of the engine off (the noise reaches this road) and equals toDataURL."""
on_frame, on_url = _capture(firefox_binary, _ENGINE_ON)
off_frame, off_url = _capture(firefox_binary, _ENGINE_OFF)
assert off_frame == off_url, (
"engine off: %d of %d bytes of a captureStream frame differ from toDataURL of the same canvas"
% (_differing(off_frame, off_url), len(off_url)))
assert _differing(on_url, off_url), (
"toDataURL is the same with the engine on and off: no noise reaches the page, so comparing the two "
"roads says nothing")
assert _differing(on_frame, off_frame), (
"a captureStream frame is the same with the engine on and off: the noise does not reach captureStream")
assert on_frame == on_url, (
"engine on: %d of %d bytes of a captureStream frame differ from toDataURL of the same canvas"
% (_differing(on_frame, on_url), len(on_url)))
_PARTIAL_JS = "() => {" + _SCENE_JS + """
const W = 128, H = 128;
const c = document.createElement('canvas'); c.width = W; c.height = H;
const gl = scene(c, 'webgl2', {preserveDrawingBuffer: true, antialias: true}, 1);
if (!gl) return null;
while (gl.getError()) {}
const errors = [];
const filled = n => { const a = new Uint8Array(n); for (let i = 0; i < n; i++) a[i] = 1 + (i * 7) % 251; return a; };
const full = new Uint8Array(W * H * 4);
gl.readPixels(0, 0, W, H, gl.RGBA, gl.UNSIGNED_BYTE, full);
errors.push(gl.getError());
// A rectangle that starts 16 pixels left of and below the framebuffer: its first 16 columns and rows are outside.
const part = filled(96 * 96 * 4), partPassed = part.slice();
gl.readPixels(-16, -16, 96, 96, gl.RGBA, gl.UNSIGNED_BYTE, part);
errors.push(gl.getError());
// The same pixels as a plain read, put 5 pixels right and 3 rows down in rows of 80 pixels.
gl.pixelStorei(gl.PACK_ROW_LENGTH, 80);
gl.pixelStorei(gl.PACK_SKIP_PIXELS, 5);
gl.pixelStorei(gl.PACK_SKIP_ROWS, 3);
const skip = filled((3 + 64) * 80 * 4), skipPassed = skip.slice();
gl.readPixels(0, 0, 64, 64, gl.RGBA, gl.UNSIGNED_BYTE, skip);
errors.push(gl.getError());
gl.pixelStorei(gl.PACK_ROW_LENGTH, 0);
gl.pixelStorei(gl.PACK_SKIP_PIXELS, 0);
gl.pixelStorei(gl.PACK_SKIP_ROWS, 0);
return {errors, full: Array.from(full), part: Array.from(part), partPassed: Array.from(partPassed),
skip: Array.from(skip), skipPassed: Array.from(skipPassed)};
}"""
def _partial(firefox_binary, hw_seed: int):
"""Reads of one render: the whole framebuffer, a rectangle partly outside it, and a packed read. Returns the
bytes of the whole read and the pixels of the other two that are not where they should be (empty when none)."""
with _page(firefox_binary, hw_seed) as p:
got = p.evaluate(_PARTIAL_JS)
assert got is not None, "webgl2 is unavailable: readPixels with PACK_SKIP_* cannot be read"
assert got["errors"] == [0, 0, 0], "readPixels raised %r" % got["errors"]
full = got["full"]
pixel = lambda arr, i: tuple(arr[4 * i:4 * i + 4])
problems = []
# The rectangle at (-16, -16), 96x96: request pixel (c, r) is framebuffer pixel (c - 16, r - 16); written when
# that is inside the 128x128 framebuffer, that is for c, r in [16, 96).
for r in range(96):
for c in range(96):
i = r * 96 + c
if c >= 16 and r >= 16:
want, what = pixel(full, (r - 16) * 128 + (c - 16)), "a plain read"
else:
want, what = pixel(got["partPassed"], i), "what the page passed in"
if pixel(got["part"], i) != want:
problems.append(("rectangle", (c, r), pixel(got["part"], i), want, what))
# 64x64 at (0, 0) with skip 5 / 3 and rows of 80: request pixel (c, r) is at column c + 5 of row r + 3.
for row in range(3 + 64):
for col in range(80):
i = row * 80 + col
c, r = col - 5, row - 3
if 0 <= c < 64 and 0 <= r < 64:
want, what = pixel(full, r * 128 + c), "a plain read"
else:
want, what = pixel(got["skipPassed"], i), "what the page passed in"
if pixel(got["skip"], i) != want:
problems.append(("packed", (c, r), pixel(got["skip"], i), want, what))
return bytes(full), problems
@pytest.mark.e2e
def test_readpixels_of_a_partial_rectangle_projects_only_what_was_written(firefox_binary):
"""readPixels changes the bytes it wrote and no other, and writes what a plain read of those pixels gives.
Two reads of one render: a 96x96 rectangle that starts 16 pixels outside the framebuffer (only its inside part
is written), and a 64x64 read with PACK_ROW_LENGTH 80, PACK_SKIP_PIXELS 5 and PACK_SKIP_ROWS 3. The bytes the
read did not write must be what the page passed in, and the ones it wrote those of a plain read of the same
framebuffer pixels (noised, with the engine on). Engine off, the same holds with the bytes as drawn; and the
plain read differs from the engine-on one, so the noise is in what is compared."""
on_full, on_problems = _partial(firefox_binary, _ENGINE_ON)
off_full, off_problems = _partial(firefox_binary, _ENGINE_OFF)
assert not off_problems, "engine off: %d pixels are not where they should be (read, pixel, got, want, from): %s" % (
len(off_problems), off_problems[:5])
assert _differing(on_full, off_full), (
"the plain read is the same with the engine on and off: no noise reaches readPixels, so the comparison "
"says nothing")
assert not on_problems, (
"engine on: %d pixels are not where they should be (read, pixel, got, want, from): %s. A pixel outside "
"the rectangle that was written must stay what the page passed in, and one inside it must be what a "
"plain read of the same pixel gives" % (len(on_problems), on_problems[:5]))