Hash :
67d8d8c8
Author :
Date :
2024-07-29T13:44:37
WebGL: Reject shaders with infinite loops Bug: chromium:350528343 Change-Id: I1b2fc152cf285b0e69c4c294351c1cf2389cc234 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5746714 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org> Auto-Submit: Shahbaz Youssefi <syoussefi@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//
// Copyright 2024 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// PruneInfiniteLoops.cpp: The PruneInfiniteLoops function prunes:
//
// 1. while (true) { ... }
//
// 2. bool variable = true; /* variable is never accessed */
// while (variable) { ... }
//
// In all cases, the loop must not have EOpBreak or EOpReturn inside to be allowed to prune.
//
// In all cases, for (...; condition; ...) is treated the same as while (condition).
//
// It quickly gets error-prone when trying to detect more complicated cases. For example, it's
// temping to reject any |while (expression involving variable with no side effects)| because that's
// either while(true) or while(false), which is prune-able either way. That detects loops like
// while(variable == false), while(variable + 2 != 4). But for example
// while(coherent_buffer[variable]) may indeed not result in an infinite loop. For now, we stick to
// the basic case.
#include "compiler/translator/tree_ops/PruneInfiniteLoops.h"
#include "compiler/translator/Symbol.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include <stack>
namespace sh
{
namespace
{
using VariableSet = TUnorderedSet<const TVariable *>;
class FindConstantVariablesTraverser : public TIntermTraverser
{
public:
FindConstantVariablesTraverser(TSymbolTable *symbolTable)
: TIntermTraverser(true, false, false, symbolTable)
{}
const VariableSet &getConstVariables() const { return mConstVariables; }
private:
bool visitDeclaration(Visit, TIntermDeclaration *decl) override
{
// Initially, assume every variable is a constant
TIntermSequence *sequence = decl->getSequence();
for (TIntermNode *node : *sequence)
{
TIntermSymbol *symbol = node->getAsSymbolNode();
if (symbol == nullptr)
{
TIntermBinary *assign = node->getAsBinaryNode();
ASSERT(assign != nullptr && assign->getOp() == EOpInitialize);
symbol = assign->getLeft()->getAsSymbolNode();
ASSERT(symbol != nullptr);
}
ASSERT(mConstVariables.find(&symbol->variable()) == mConstVariables.end());
mConstVariables.insert(&symbol->variable());
}
return false;
}
bool visitLoop(Visit visit, TIntermLoop *loop) override
{
ASSERT(visit == PreVisit);
// For simplicity, for now only consider conditions that are just |variable|. In that case,
// the condition is not visited, so that `visitSymbol` doesn't consider this a write.
if (loop->getInit() != nullptr)
{
loop->getInit()->traverse(this);
}
if (loop->getExpression() != nullptr)
{
loop->getExpression()->traverse(this);
}
loop->getBody()->traverse(this);
TIntermTyped *condition = loop->getCondition();
if (condition != nullptr &&
(condition->getAsSymbolNode() == nullptr || loop->getType() == ELoopDoWhile))
{
condition->traverse(this);
}
return false;
}
void visitSymbol(TIntermSymbol *symbol) override
{
// Assume write for simplicity. AST makes it difficult to tell if this is read or write.
mConstVariables.erase(&symbol->variable());
}
VariableSet mConstVariables;
};
struct LoopStats
{
bool hasBreak = false;
bool hasReturn = false;
};
class PruneInfiniteLoopsTraverser : public TIntermTraverser
{
public:
PruneInfiniteLoopsTraverser(TSymbolTable *symbolTable, const VariableSet &constVariables)
: TIntermTraverser(true, false, false, symbolTable),
mConstVariables(constVariables),
mAnyLoopsPruned(false)
{}
bool anyLoopsPruned() const { return mAnyLoopsPruned; }
private:
bool visitLoop(Visit visit, TIntermLoop *loop) override;
bool visitSwitch(Visit visit, TIntermSwitch *node) override;
bool visitBranch(Visit visit, TIntermBranch *node) override;
void onScopeBegin() { mLoopStats.push({}); }
void onScopeEnd()
{
// Propagate |hasReturn| up the stack, it escapes every loop.
ASSERT(!mLoopStats.empty());
bool hasReturn = mLoopStats.top().hasReturn;
mLoopStats.pop();
if (!mLoopStats.empty())
{
mLoopStats.top().hasReturn = mLoopStats.top().hasReturn || hasReturn;
}
}
bool hasLoopEscape()
{
ASSERT(!mLoopStats.empty());
return mLoopStats.top().hasBreak || mLoopStats.top().hasReturn;
}
const VariableSet &mConstVariables;
std::stack<LoopStats> mLoopStats;
bool mAnyLoopsPruned;
};
bool PruneInfiniteLoopsTraverser::visitLoop(Visit visit, TIntermLoop *loop)
{
onScopeBegin();
// Nothing in the init, condition or expression of loops can alter the control flow, just visit
// the body.
loop->getBody()->traverse(this);
// Prune the loop if it has no breaks or returns, it's not do-while, and the condition is a
// constant variable.
TIntermTyped *condition = loop->getCondition();
TIntermConstantUnion *constCondition = condition ? condition->getAsConstantUnion() : nullptr;
TIntermSymbol *conditionSymbol = condition ? loop->getCondition()->getAsSymbolNode() : nullptr;
const bool isConditionConstant =
condition == nullptr || constCondition != nullptr ||
(conditionSymbol != nullptr &&
mConstVariables.find(&conditionSymbol->variable()) != mConstVariables.end());
if (isConditionConstant && loop->getType() != ELoopDoWhile && !hasLoopEscape())
{
mMultiReplacements.emplace_back(getParentNode()->getAsBlock(), loop, TIntermSequence{});
mAnyLoopsPruned = true;
}
onScopeEnd();
return false;
}
bool PruneInfiniteLoopsTraverser::visitSwitch(Visit visit, TIntermSwitch *node)
{
// Insert a LoopStats node for switch, just so that breaks inside the switch are not considered
// loop breaks.
onScopeBegin();
// Nothing in the switch expression that can alter the control flow, just visit the body.
node->getStatementList()->traverse(this);
onScopeEnd();
return false;
}
bool PruneInfiniteLoopsTraverser::visitBranch(Visit visit, TIntermBranch *node)
{
if (!mLoopStats.empty())
{
switch (node->getFlowOp())
{
case EOpReturn:
mLoopStats.top().hasReturn = true;
break;
case EOpBreak:
mLoopStats.top().hasBreak = true;
break;
case EOpContinue:
case EOpKill:
// Kill and continue don't let control flow escape from the loop
break;
default:
UNREACHABLE();
}
}
// Only possible child is the value of a return statement, which has no significance.
return false;
}
} // namespace
bool PruneInfiniteLoops(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
bool *anyLoopsPruned)
{
*anyLoopsPruned = false;
FindConstantVariablesTraverser constVarTransverser(symbolTable);
root->traverse(&constVarTransverser);
PruneInfiniteLoopsTraverser pruneTraverser(symbolTable,
constVarTransverser.getConstVariables());
root->traverse(&pruneTraverser);
*anyLoopsPruned = pruneTraverser.anyLoopsPruned();
return pruneTraverser.updateTree(compiler, root);
}
} // namespace sh