From 3bbf2850a0317e257a3833346d05f534305b4864 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 8 Oct 2020 14:51:31 -0400 Subject: [PATCH] Implement WebGL2 farbling --- .../webgl/webgl2_rendering_context_base.cc | 110 ++++++++++++++++++ ...bgl-webgl2_rendering_context_base.cc.patch | 4 +- 2 files changed, 112 insertions(+), 2 deletions(-) diff --git a/chromium_src/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc b/chromium_src/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc index 07b6f0f76ccf..2a65a9c22836 100644 --- a/chromium_src/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc +++ b/chromium_src/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc @@ -3,11 +3,121 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this file, * You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.h" #include "brave/components/content_settings/renderer/brave_content_settings_agent_impl_helper.h" +#include "third_party/blink/renderer/bindings/modules/v8/webgl_any.h" +#include "third_party/blink/renderer/core/execution_context/execution_context.h" + +#include + +using blink::ScriptState; +using blink::ScriptValue; +using blink::WebGL2RenderingContextBase; +using blink::WebGLAny; + +namespace { + +ScriptValue FarbleGLIntParameter(WebGL2RenderingContextBase* owner, + ScriptState* script_state, + GLenum pname, + int discard) { + GLint value = 0; + if (!owner->isContextLost()) + owner->ContextGL()->GetIntegerv(pname, &value); + if (value > 0) { + std::mt19937_64 prng = + brave::BraveSessionCache::From(*ExecutionContext::From(script_state)) + .MakePseudoRandomGenerator(); + prng.discard(discard); + if (prng() % 2 != 0) { + value = value - 1; + } + } + return WebGLAny(script_state, value); +} + +ScriptValue FarbleGLInt64Parameter(WebGL2RenderingContextBase* owner, + ScriptState* script_state, + GLenum pname, + int discard) { + GLint64 value = 0; + if (!owner->isContextLost()) + owner->ContextGL()->GetInteger64v(pname, &value); + if (value > 0) { + std::mt19937_64 prng = + brave::BraveSessionCache::From(*ExecutionContext::From(script_state)) + .MakePseudoRandomGenerator(); + prng.discard(discard); + if (prng() % 2 != 0) { + value = value - 1; + } + } + return WebGLAny(script_state, value); +} + +} // namespace #define BRAVE_WEBGL2_RENDERING_CONTEXT_BASE \ if (canvas() && !AllowFingerprinting(canvas()->GetDocument().GetFrame())) \ return ScriptValue::CreateNull(script_state->GetIsolate()); +#define BRAVE_WEBGL2_RENDERING_CONTEXT_BASE_GETPARAMETER \ + if (ExecutionContext* context = ExecutionContext::From(script_state)) { \ + if (WebContentSettingsClient* settings = \ + brave::GetContentSettingsClientFor(context)) { \ + if (settings->GetBraveFarblingLevel() == BraveFarblingLevel::MAXIMUM) { \ + switch (pname) { \ + case GL_SHADING_LANGUAGE_VERSION: \ + case GL_VERSION: \ + case GL_COPY_READ_BUFFER_BINDING: \ + case GL_COPY_WRITE_BUFFER_BINDING: \ + case GL_DRAW_FRAMEBUFFER_BINDING: \ + case GL_MAX_VERTEX_UNIFORM_COMPONENTS: \ + case GL_MAX_VERTEX_UNIFORM_BLOCKS: \ + case GL_MAX_VERTEX_OUTPUT_COMPONENTS: \ + case GL_MAX_VARYING_COMPONENTS: \ + case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS: \ + case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS: \ + case GL_MAX_FRAGMENT_UNIFORM_BLOCKS: \ + case GL_MAX_FRAGMENT_INPUT_COMPONENTS: \ + case GL_MAX_UNIFORM_BUFFER_BINDINGS: \ + case GL_MAX_COMBINED_UNIFORM_BLOCKS: \ + case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS: \ + case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS: \ + return ScriptValue::CreateNull(script_state->GetIsolate()); \ + } \ + } else if (settings->GetBraveFarblingLevel() == \ + BraveFarblingLevel::BALANCED) { \ + switch (pname) { \ + case GL_MAX_VERTEX_UNIFORM_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 1); \ + case GL_MAX_VERTEX_UNIFORM_BLOCKS: \ + return FarbleGLIntParameter(this, script_state, pname, 2); \ + case GL_MAX_VERTEX_OUTPUT_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 3); \ + case GL_MAX_VARYING_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 4); \ + case GL_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 5); \ + case GL_MAX_FRAGMENT_UNIFORM_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 6); \ + case GL_MAX_FRAGMENT_UNIFORM_BLOCKS: \ + return FarbleGLIntParameter(this, script_state, pname, 7); \ + case GL_MAX_FRAGMENT_INPUT_COMPONENTS: \ + return FarbleGLIntParameter(this, script_state, pname, 8); \ + case GL_MAX_UNIFORM_BUFFER_BINDINGS: \ + return FarbleGLIntParameter(this, script_state, pname, 9); \ + case GL_MAX_COMBINED_UNIFORM_BLOCKS: \ + return FarbleGLIntParameter(this, script_state, pname, 10); \ + case GL_MAX_COMBINED_VERTEX_UNIFORM_COMPONENTS: \ + return FarbleGLInt64Parameter(this, script_state, pname, 11); \ + case GL_MAX_COMBINED_FRAGMENT_UNIFORM_COMPONENTS: \ + return FarbleGLInt64Parameter(this, script_state, pname, 12); \ + } \ + } \ + } \ + } + #include "../../../../../../../third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc" #undef BRAVE_WEBGL2_RENDERING_CONTEXT_BASE +#undef BRAVE_WEBGL2_RENDERING_CONTEXT_BASE_GETPARAMETER diff --git a/patches/third_party-blink-renderer-modules-webgl-webgl2_rendering_context_base.cc.patch b/patches/third_party-blink-renderer-modules-webgl-webgl2_rendering_context_base.cc.patch index f90441271506..48143ab1494a 100644 --- a/patches/third_party-blink-renderer-modules-webgl-webgl2_rendering_context_base.cc.patch +++ b/patches/third_party-blink-renderer-modules-webgl-webgl2_rendering_context_base.cc.patch @@ -1,12 +1,12 @@ diff --git a/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc b/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc -index 2d8068bf9f28ed7c5e33e7dee7003963aca89a0e..5d7d75d6ff5085f21fe40f79411e82fa01570617 100644 +index 2d8068bf9f28ed7c5e33e7dee7003963aca89a0e..f71dd39a2070fdcb111be4a61087a9ff95f4d68a 100644 --- a/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc +++ b/third_party/blink/renderer/modules/webgl/webgl2_rendering_context_base.cc @@ -5062,6 +5062,7 @@ ScriptValue WebGL2RenderingContextBase::getParameter(ScriptState* script_state, GLenum pname) { if (isContextLost()) return ScriptValue::CreateNull(script_state->GetIsolate()); -+ BRAVE_WEBGL2_RENDERING_CONTEXT_BASE ++ BRAVE_WEBGL2_RENDERING_CONTEXT_BASE_GETPARAMETER switch (pname) { case GL_SHADING_LANGUAGE_VERSION: { return WebGLAny(