SkSL: Only allow 'discard' in fragment shaders

Allowing this in runtime effects lets people break our contracts around
SkShaders on the color side of the paint altering coverage, etc.

Bug: skia:11085
Change-Id: I1ec8e71581c8d50f681cb0ca6ca8416375b3f43f
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/344760
Auto-Submit: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
This commit is contained in:
Brian Osman 2020-12-15 15:40:32 -05:00 committed by Skia Commit-Bot
parent d059005b93
commit f2876b0b9e
2 changed files with 7 additions and 0 deletions

View File

@ -697,6 +697,10 @@ std::unique_ptr<Statement> IRGenerator::convertContinue(const ASTNode& c) {
std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) { std::unique_ptr<Statement> IRGenerator::convertDiscard(const ASTNode& d) {
SkASSERT(d.fKind == ASTNode::Kind::kDiscard); SkASSERT(d.fKind == ASTNode::Kind::kDiscard);
if (fKind != Program::kFragment_Kind && fKind != Program::kFragmentProcessor_Kind) {
fErrors.error(d.fOffset, "discard statement is only permitted in fragment shaders");
return nullptr;
}
return std::make_unique<DiscardStatement>(d.fOffset); return std::make_unique<DiscardStatement>(d.fOffset);
} }

View File

@ -58,6 +58,9 @@ DEF_TEST(SkRuntimeEffectInvalid, r) {
test("half4 missing(); half4 main() { return missing(); }", "undefined function"); test("half4 missing(); half4 main() { return missing(); }", "undefined function");
// No use of 'discard' is permitted
test("half4 main() { discard; }", "discard");
// Shouldn't be possible to create an SkRuntimeEffect without "main" // Shouldn't be possible to create an SkRuntimeEffect without "main"
test("", "main"); test("", "main");