Skip to content

Commit

Permalink
Implement WebGL2 farbling
Browse files Browse the repository at this point in the history
  • Loading branch information
pilgrim-brave committed Oct 8, 2020
1 parent 2e01ddc commit 3bbf285
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <algorithm>

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
Original file line number Diff line number Diff line change
@@ -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(
Expand Down

0 comments on commit 3bbf285

Please sign in to comment.