diff --git a/src/compiler/translator/Compiler.cpp b/src/compiler/translator/Compiler.cpp index a42ef6205..36b313e22 100644 --- a/src/compiler/translator/Compiler.cpp +++ b/src/compiler/translator/Compiler.cpp @@ -180,6 +180,10 @@ bool TCompiler::compile(const char* const shaderStrings[], TIntermNode* root = parseContext.treeRoot; success = intermediate.postProcess(root); + // Disallow expressions deemed too complex. + if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY)) + success = limitExpressionComplexity(root); + if (success) success = detectCallDepth(root, infoSink, (compileOptions & SH_LIMIT_CALL_STACK_DEPTH) != 0); @@ -221,10 +225,6 @@ bool TCompiler::compile(const char* const shaderStrings[], if (success && (compileOptions & SH_CLAMP_INDIRECT_ARRAY_BOUNDS)) arrayBoundsClamper.MarkIndirectArrayBoundsForClamping(root); - // Disallow expressions deemed too complex. - if (success && (compileOptions & SH_LIMIT_EXPRESSION_COMPLEXITY)) - success = limitExpressionComplexity(root); - if (success && shaderType == SH_VERTEX_SHADER && (compileOptions & SH_INIT_GL_POSITION)) initializeGLPosition(root); @@ -413,8 +413,15 @@ bool TCompiler::enforceTimingRestrictions(TIntermNode* root, bool outputGraph) bool TCompiler::limitExpressionComplexity(TIntermNode* root) { - TIntermTraverser traverser; + TMaxDepthTraverser traverser(maxExpressionComplexity+1); root->traverse(&traverser); + + if (traverser.getMaxDepth() > maxExpressionComplexity) + { + infoSink.info << "Expression too complex."; + return false; + } + TDependencyGraph graph(root); for (TFunctionCallVector::const_iterator iter = graph.beginUserDefinedFunctionCalls(); @@ -426,11 +433,6 @@ bool TCompiler::limitExpressionComplexity(TIntermNode* root) samplerSymbol->traverse(&graphTraverser); } - if (traverser.getMaxDepth() > maxExpressionComplexity) - { - infoSink.info << "Expression too complex."; - return false; - } return true; } diff --git a/src/compiler/translator/intermediate.h b/src/compiler/translator/intermediate.h index b09fc9e41..7c72099fe 100644 --- a/src/compiler/translator/intermediate.h +++ b/src/compiler/translator/intermediate.h @@ -664,4 +664,30 @@ protected: TVector path; }; +// +// For traversing the tree, and computing max depth. +// Takes a maximum depth limit to prevent stack overflow. +// +class TMaxDepthTraverser : public TIntermTraverser +{ +public: + POOL_ALLOCATOR_NEW_DELETE(); + TMaxDepthTraverser(int depthLimit) + : TIntermTraverser(true, true, false, false), + depthLimit(depthLimit) + {} + + virtual bool visitBinary(Visit visit, TIntermBinary*) { return depthCheck(); } + virtual bool visitUnary(Visit visit, TIntermUnary*) { return depthCheck(); } + virtual bool visitSelection(Visit visit, TIntermSelection*) { return depthCheck(); } + virtual bool visitAggregate(Visit visit, TIntermAggregate*) { return depthCheck(); } + virtual bool visitLoop(Visit visit, TIntermLoop*) { return depthCheck(); } + virtual bool visitBranch(Visit visit, TIntermBranch*) { return depthCheck(); } + +protected: + int depthLimit; + + bool depthCheck() const { return maxDepth < depthLimit; } +}; + #endif // __INTERMEDIATE_H