Commit ce39f6ff by Olli Etuaho

Disallow user-defined function calls in global variable init

Generate an error message when an user-defined function call is found in a global variable initializer. Even before this patch, the call graph already marked functions that were only called from the global scope as unused. This change was tested extensively with popular WebGL content, with no regressions found. TEST=angle_unittests BUG=angleproject:988 Change-Id: Iec1b16d2af386f1e5c383f86926d80cef553b694 Reviewed-on: https://chromium-review.googlesource.com/283291Tested-by: 's avatarOlli Etuaho <oetuaho@nvidia.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarZhenyao Mo <zmo@chromium.org>
parent 8fc55c8e
...@@ -17,6 +17,7 @@ class ValidateGlobalInitializerTraverser : public TIntermTraverser ...@@ -17,6 +17,7 @@ class ValidateGlobalInitializerTraverser : public TIntermTraverser
ValidateGlobalInitializerTraverser(const TParseContext *context); ValidateGlobalInitializerTraverser(const TParseContext *context);
void visitSymbol(TIntermSymbol *node) override; void visitSymbol(TIntermSymbol *node) override;
bool visitAggregate(Visit visit, TIntermAggregate *node) override;
bool isValid() const { return mIsValid; } bool isValid() const { return mIsValid; }
bool issueWarning() const { return mIssueWarning; } bool issueWarning() const { return mIssueWarning; }
...@@ -59,6 +60,16 @@ void ValidateGlobalInitializerTraverser::visitSymbol(TIntermSymbol *node) ...@@ -59,6 +60,16 @@ void ValidateGlobalInitializerTraverser::visitSymbol(TIntermSymbol *node)
} }
} }
bool ValidateGlobalInitializerTraverser::visitAggregate(Visit visit, TIntermAggregate *node)
{
// Disallow calls to user-defined functions in global variable initializers.
if (node->getOp() == EOpFunctionCall && node->isUserDefined())
{
mIsValid = false;
}
return true;
}
ValidateGlobalInitializerTraverser::ValidateGlobalInitializerTraverser(const TParseContext *context) ValidateGlobalInitializerTraverser::ValidateGlobalInitializerTraverser(const TParseContext *context)
: TIntermTraverser(true, false, false), : TIntermTraverser(true, false, false),
mContext(context), mContext(context),
......
...@@ -405,12 +405,9 @@ TEST_F(ExpressionLimitTest, Recursion) ...@@ -405,12 +405,9 @@ TEST_F(ExpressionLimitTest, Recursion)
return vec3(float(r) / 255.0, float(g) / 255.0, float(b) / 255.0); return vec3(float(r) / 255.0, float(g) / 255.0, float(b) / 255.0);
} }
// these external calls used to incorrectly trigger
// recursion detection.
vec3 hairColor0 = rgb(151, 200, 234);
vec3 faceColor2 = rgb(183, 148, 133);
void main() { void main() {
vec3 hairColor0 = rgb(151, 200, 234);
vec3 faceColor2 = rgb(183, 148, 133);
gl_FragColor = u_color + vec4(hairColor0 + faceColor2, 0); gl_FragColor = u_color + vec4(hairColor0 + faceColor2, 0);
} }
); );
......
...@@ -508,6 +508,23 @@ TEST_F(MalformedShaderTest, AssignUniformToGlobalESSL1) ...@@ -508,6 +508,23 @@ TEST_F(MalformedShaderTest, AssignUniformToGlobalESSL1)
} }
} }
// Global variable initializers need to be constant expressions (ESSL 1.00 section 4.3)
// Initializing with an user-defined function call should be an error.
TEST_F(MalformedShaderTest, AssignFunctionCallToGlobal)
{
const std::string &shaderString =
"precision mediump float;\n"
"float foo() { return 1.0; }\n"
"float b = foo();\n"
"void main() {\n"
" gl_FragColor = vec4(b);\n"
"}\n";
if (compile(shaderString))
{
FAIL() << "Shader compilation succeeded, expecting failure " << mInfoLog;
}
}
// Global variable initializers need to be constant expressions (ESSL 3.00 section 4.3) // Global variable initializers need to be constant expressions (ESSL 3.00 section 4.3)
// Initializing with a non-constant global should be an error. // Initializing with a non-constant global should be an error.
TEST_F(MalformedShaderTest, AssignNonConstGlobalToGlobal) TEST_F(MalformedShaderTest, AssignNonConstGlobalToGlobal)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment