[WHLSL] The lexer should not choke on a single '/'
authorrmorisset@apple.com <rmorisset@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Jul 2019 02:29:54 +0000 (02:29 +0000)
committerrmorisset@apple.com <rmorisset@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Jul 2019 02:29:54 +0000 (02:29 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199886

Reviewed by Myles Maxfield.

Source/WebCore:

The bug is trivial: just recognize that a single '/' without a '*' or '/' afterwards is not the beginning of a comment.

While investigating this bug, I also found that part of the parser fails to properly propagate errors, so I fixed it.

New test in LayoutTests/webgpu/whlsl/float-math.html
I also added a new flag to WHLSLPrepare.cpp. It can be used to parse the full standard library (this is how I found this bug in the first place).

* Modules/webgpu/WHLSL/WHLSLLexer.cpp:
(WebCore::WHLSL::Lexer::skipWhitespaceAndComments):
* Modules/webgpu/WHLSL/WHLSLParser.cpp:
(WebCore::WHLSL::Parser::parsePossibleTernaryConditional):
* Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
(WebCore::WHLSL::prepareShared):
* Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.cpp:
(WebCore::WHLSL::includeStandardLibrary):
* Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.h:

LayoutTests:

Added a new basic test of division.

* webgpu/whlsl/float-math.html:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247554 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/webgpu/whlsl/float-math.html
Source/WebCore/ChangeLog
Source/WebCore/Modules/webgpu/WHLSL/WHLSLLexer.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLParser.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLPrepare.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.cpp
Source/WebCore/Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.h

index d439d3b..98424ba 100644 (file)
@@ -1,3 +1,14 @@
+2019-07-17  Robin Morisset  <rmorisset@apple.com>
+
+        [WHLSL] The lexer should not choke on a single '/'
+        https://bugs.webkit.org/show_bug.cgi?id=199886
+
+        Reviewed by Myles Maxfield.
+
+        Added a new basic test of division.
+
+        * webgpu/whlsl/float-math.html:
+
 2019-07-17  Antoine Quint  <graouts@apple.com>
 
         Unable to bring up custom media controls on iOS for video.sina.cn
index f98097c..b0717f0 100644 (file)
@@ -37,6 +37,10 @@ whlslTests.floatMath = async () => {
             float x = 7.5;
             return float(x);
         }
+        bool div()
+        {
+            return 0.5 / 2 == 0.25;
+        }
     `;
 
     assert_equals(await callBoolFunction(program,  "foo", []), true);
@@ -44,6 +48,7 @@ whlslTests.floatMath = async () => {
     assert_equals(await callFloatFunction(program, "foo9", []), 7.5);
     assert_equals(await callFloatFunction(program, "foo12", []), 7);
     assert_equals(await callFloatFunction(program, "foo13", []), 7.5);
+    assert_equals(await callBoolFunction(program, "div", []), true);
 
     await checkFail(
         `
index 11ee7bd..1895525 100644 (file)
@@ -1,3 +1,27 @@
+2019-07-17  Robin Morisset  <rmorisset@apple.com>
+
+        [WHLSL] The lexer should not choke on a single '/'
+        https://bugs.webkit.org/show_bug.cgi?id=199886
+
+        Reviewed by Myles Maxfield.
+
+        The bug is trivial: just recognize that a single '/' without a '*' or '/' afterwards is not the beginning of a comment.
+
+        While investigating this bug, I also found that part of the parser fails to properly propagate errors, so I fixed it.
+
+        New test in LayoutTests/webgpu/whlsl/float-math.html
+        I also added a new flag to WHLSLPrepare.cpp. It can be used to parse the full standard library (this is how I found this bug in the first place).
+
+        * Modules/webgpu/WHLSL/WHLSLLexer.cpp:
+        (WebCore::WHLSL::Lexer::skipWhitespaceAndComments):
+        * Modules/webgpu/WHLSL/WHLSLParser.cpp:
+        (WebCore::WHLSL::Parser::parsePossibleTernaryConditional):
+        * Modules/webgpu/WHLSL/WHLSLPrepare.cpp:
+        (WebCore::WHLSL::prepareShared):
+        * Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.cpp:
+        (WebCore::WHLSL::includeStandardLibrary):
+        * Modules/webgpu/WHLSL/WHLSLStandardLibraryUtilities.h:
+
 2019-07-17  Simon Fraser  <simon.fraser@apple.com>
 
         Unreviewed build fix after r247549.
index e4aba8c..93be968 100644 (file)
@@ -558,7 +558,8 @@ void Lexer::skipWhitespaceAndComments()
                         break;
                     }
                 }
-            }
+            } else
+                break;
         } else
             break;
     }
index 3e28996..2c80957 100644 (file)
@@ -1618,10 +1618,24 @@ auto Parser::parsePossibleTernaryConditional() -> Expected<UniqueRef<AST::Expres
     }
 
     expression = completePossibleShift(WTFMove(*expression));
+    if (!expression)
+        return Unexpected<Error>(expression.error());
+
     expression = completePossibleMultiply(WTFMove(*expression));
+    if (!expression)
+        return Unexpected<Error>(expression.error());
+
     expression = completePossibleAdd(WTFMove(*expression));
+    if (!expression)
+        return Unexpected<Error>(expression.error());
+
     expression = completePossibleRelationalBinaryOperation(WTFMove(*expression));
+    if (!expression)
+        return Unexpected<Error>(expression.error());
+
     expression = completePossibleLogicalBinaryOperation(WTFMove(*expression));
+    if (!expression)
+        return Unexpected<Error>(expression.error());
 
     PEEK(nextToken);
     if (nextToken->type == Token::Type::QuestionMark)
index 514ec35..6e7fd75 100644 (file)
@@ -64,6 +64,8 @@ static constexpr bool dumpASTAtEnd = false;
 static constexpr bool alwaysDumpPassFailures = false;
 static constexpr bool dumpPassFailure = dumpASTBeforeEachPass || dumpASTAfterParsing || dumpASTAtEnd || alwaysDumpPassFailures;
 
+static constexpr bool parseFullStandardLibrary = false;
+
 static bool dumpASTIfNeeded(bool shouldDump, Program& program, const char* message)
 {
     if (shouldDump) {
@@ -115,7 +117,7 @@ static Optional<Program> prepareShared(String& whlslSource)
             dataLogLn("failed to parse the program: ", *parseFailure);
         return WTF::nullopt;
     }
-    includeStandardLibrary(program, parser);
+    includeStandardLibrary(program, parser, parseFullStandardLibrary);
 
     if (!dumpASTBetweenEachPassIfNeeded(program, "AST after parsing"))
         dumpASTAfterParsingIfNeeded(program);
index 93cb3d1..0481b58 100644 (file)
@@ -33,6 +33,7 @@
 #include "WHLSLStandardLibrary.h"
 #include "WHLSLStandardLibraryFunctionMap.h"
 #include "WHLSLVisitor.h"
+#include <wtf/DataLog.h>
 #include <wtf/HashSet.h>
 #include <wtf/NeverDestroyed.h>
 #include <pal/Gunzip.h>
@@ -74,9 +75,17 @@ private:
     HashSet<String> m_functionNames;
 };
 
-void includeStandardLibrary(Program& program, Parser& parser)
+void includeStandardLibrary(Program& program, Parser& parser, bool parseFullStandardLibrary)
 {
     static NeverDestroyed<String> standardLibrary(decompressAndDecodeStandardLibrary());
+    if (parseFullStandardLibrary) {
+        if (auto parseFailure = parser.parse(program, standardLibrary.get(), Parser::Mode::StandardLibrary)) {
+            dataLogLn("failed to parse the (full) standard library: ", *parseFailure);
+            ASSERT_NOT_REACHED();
+        }
+        return;
+    }
+
     static NeverDestroyed<HashMap<String, SubstringLocation>> standardLibraryFunctionMap(computeStandardLibraryFunctionMap());
 
     auto stringView = StringView(standardLibrary.get()).substring(0, firstFunctionOffsetInStandardLibrary());
@@ -100,8 +109,11 @@ void includeStandardLibrary(Program& program, Parser& parser)
             if (iterator == standardLibraryFunctionMap.get().end())
                 continue;
             auto stringView = StringView(standardLibrary.get()).substring(iterator->value.start, iterator->value.end - iterator->value.start);
-            auto parseFailure = parser.parse(program, stringView, Parser::Mode::StandardLibrary);
-            ASSERT_UNUSED(parseFailure, !parseFailure);
+            if (auto parseFailure = parser.parse(program, stringView, Parser::Mode::StandardLibrary)) {
+                dataLogLn("failed to parse the (partial) standard library: ", *parseFailure);
+                ASSERT_NOT_REACHED();
+                return;
+            }
             allFunctionNames.add(name);
         }
         for ( ; nativeFunctionDeclarationsCount < program.nativeFunctionDeclarations().size(); ++nativeFunctionDeclarationsCount)
index 63f16c5..caf6a41 100644 (file)
@@ -36,7 +36,7 @@ namespace WHLSL {
 class Parser;
 class Program;
 
-void includeStandardLibrary(Program&, Parser&);
+void includeStandardLibrary(Program&, Parser&, bool parseFullStandardLibrary);
 
 }