Przeglądaj źródła

Fix reading inline names in wasm binary reader.
Removed some confusion in the section header.

Michael Ferris 9 lat temu
rodzic
commit
3db4a6238f

+ 11 - 43
lib/WasmReader/WasmBinaryReader.cpp

@@ -75,28 +75,6 @@ void WasmBinaryReader::InitializeReader()
 {
     ValidateModuleHeader();
     m_readerState = READER_STATE_UNKNOWN;
-#if DBG_DUMP
-    if (DO_WASM_TRACE_SECTION)
-    {
-        const byte* startModule = m_pc;
-
-        bool doRead = true;
-        SectionCode prevSect = bSectLimit;
-        while (doRead)
-        {
-            SectionHeader secHeader = ReadSectionHeader();
-            if (secHeader.code <= prevSect)
-            {
-                TRACE_WASM_SECTION(_u("Unknown section order"));
-            }
-            prevSect = secHeader.code;
-            // skip the section
-            m_pc = secHeader.end;
-            doRead = !EndOfModule();
-        }
-        m_pc = startModule;
-    }
-#endif
 }
 
 void
@@ -233,29 +211,17 @@ WasmBinaryReader::ReadSectionHeader()
     CheckBytesLeft(sectionSize);
 
     header.code = sectionId;
-    const char *sectionName = SectionInfo::All[sectionId].id;
-    UINT32 nameLength = SectionInfo::All[sectionId].nameLength;
     if (sectionId == bSectCustom)
     {
-        nameLength = LEB128(len);
-        CheckBytesLeft(nameLength);
-        sectionName = (const char*)(m_pc);
-        m_pc += nameLength;
+        header.name = ReadInlineName(len, header.nameLength);
     }
-
-    header.nameLength = nameLength;
-    header.name = sectionName;
-
-#if ENABLE_DEBUG_CONFIG_OPTIONS
-    if (DO_WASM_TRACE_SECTION)
+    else
     {
-        char16* wstr = nullptr;
-        size_t unused;
-        utf8::NarrowStringToWide<utf8::malloc_allocator>(sectionName, nameLength, &wstr, &unused);
-        TRACE_WASM_SECTION(_u("Section Header: %s, length = %u (0x%x)"), wstr, sectionSize, sectionSize);
-        free(wstr);
+        header.name = SectionInfo::All[sectionId].name;
+        header.nameLength = SectionInfo::All[sectionId].nameLength;
     }
-#endif
+
+    TRACE_WASM_SECTION(_u("Section Header: %s, length = %u (0x%x)"), header.name, sectionSize, sectionSize);
     return header;
 }
 
@@ -945,7 +911,8 @@ WasmBinaryReader::ReadNamesSection()
     {
         UINT fnNameLen = 0;
         WasmFunctionInfo* funsig = m_module->GetWasmFunctionInfo(i);
-        funsig->SetName(ReadInlineName(len, fnNameLen), fnNameLen);
+        const char16* name = ReadInlineName(len, fnNameLen);
+        funsig->SetName(name, fnNameLen);
         UINT numLocals = LEB128(len);
         if (numLocals != funsig->GetLocalCount())
         {
@@ -998,7 +965,8 @@ void
 WasmBinaryReader::ReadCustomSection()
 {
     CustomSection customSection;
-    customSection.name = CvtUtf8Str((LPCUTF8)m_currentSection.name, m_currentSection.nameLength, &customSection.nameLength);
+    customSection.name = m_currentSection.name;
+    customSection.nameLength = m_currentSection.nameLength;
     customSection.payload = m_pc;
 
     size_t size = m_currentSection.end - m_pc;
@@ -1021,7 +989,7 @@ WasmBinaryReader::ReadInlineName(uint32& length, uint32& nameLength)
     m_pc += nameLength;
     length += nameLength;
 
-    return CvtUtf8Str(rawName, nameLength);
+    return CvtUtf8Str(rawName, nameLength, &nameLength);
 }
 
 const char16*

+ 1 - 1
lib/WasmReader/WasmBinaryReader.h

@@ -26,7 +26,7 @@ namespace Wasm
         const byte* start;
         const byte* end;
         uint32 nameLength;
-        const char* name;
+        const char16* name;
     };
 
     static const unsigned int binaryVersion = 0x1;

+ 2 - 3
lib/WasmReader/WasmSection.cpp

@@ -12,12 +12,11 @@ namespace Wasm
         SectionFlag flag,
         SectionCode precedent,
         const char16* name,
-        const char* id,
         const uint32 nameLength
-    ): flag(flag), precedent(precedent), name(name), id(id), nameLength(nameLength) {}
+    ): flag(flag), precedent(precedent), name(name), nameLength(nameLength) {}
 
     SectionInfo SectionInfo::All[bSectLimit] = {
-#define WASM_SECTION(name, id, flag, precedent) {flag, bSect ## precedent, static_cast<const char16*>(_u(#name)), static_cast<const char*>(id), sizeof(#name)},
+#define WASM_SECTION(_, id, flag, precedent) {flag, bSect ## precedent, static_cast<const char16*>(_u(id)), sizeof(id)},
 #include "WasmSections.h"
     };
 }

+ 2 - 3
lib/WasmReader/WasmSection.h

@@ -15,7 +15,7 @@ namespace Wasm
         fSectIgnore,
     };
 
-#define WASM_SECTION(name, id, flag, precendent) bSect ## name,
+#define WASM_SECTION(intName, id, flag, precendent) bSect ## intName,
     enum SectionCode : uint8
     {
 #include "WasmSections.h"
@@ -25,11 +25,10 @@ namespace Wasm
 
     struct SectionInfo
     {
-        SectionInfo(SectionFlag, SectionCode, const char16*, const char*, const uint32);
+        SectionInfo(SectionFlag, SectionCode, const char16*, const uint32);
         SectionFlag flag;
         SectionCode precedent;
         const char16* name;
-        const char* id;
         const uint32 nameLength;
         static SectionInfo All[bSectLimit];
     };

+ 1 - 1
lib/WasmReader/WasmSections.h

@@ -3,7 +3,7 @@
 // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
 //-------------------------------------------------------------------------------------------------------
 
-//          (name                 , ID                   , SectionFlag, Precedent         )
+//          (internalName         , ID                   , SectionFlag, Precedent         )
 WASM_SECTION(Custom               , ""                   , fSectNone  , Limit             )
 WASM_SECTION(Signatures           , "type"               , fSectNone  , Limit             )
 WASM_SECTION(ImportTable          , "import"             , fSectNone  , Limit             )

+ 39 - 11
test/WasmSpec/baselines/jsapi.baseline

@@ -1,5 +1,5 @@
 Harness Status: OK
-Found 61 tests: Pass = 29 Fail = 32
+Found 89 tests: Pass = 67 Fail = 22
 Pass 'WebAssembly' data property on global object  
 Fail 'WebAssembly' object  assert_equals: expected "[object WebAssembly]" but got "[object Object]"
 Pass 'WebAssembly.(Compile|Link|Runtime)Error' data property  
@@ -48,16 +48,44 @@ Pass 'WebAssembly.Table.prototype.grow' data property
 Fail 'WebAssembly.Table.prototype.grow' method  assert_equals: expected (number) 1 but got (undefined) undefined
 Pass 'WebAssembly.validate' method  
 Pass 'WebAssembly.compile' data property  
-Fail 'WebAssembly.compile' function  assert_equals: expected 1 but got 2
-Fail assertCompileError 1  BufferSource expected
-Fail assertCompileError 2  BufferSource expected
-Fail assertCompileError 3  BufferSource expected
-Fail assertCompileError 4  BufferSource expected
-Fail assertCompileError 5  Out of file: Needed: 4, Left: 0
-Fail assertCompileError 6  Out of file: Needed: 4, Left: 0
-Fail assertCompileError 7  Out of file: Needed: 4, Left: 0
-Fail assertCompileError 8  Out of file: Needed: 4, Left: 0
+Pass 'WebAssembly.compile' function  
+Pass assertCompileError 1  
+Pass assertCompileError 2  
+Pass assertCompileError 3  
+Pass assertCompileError 4  
+Pass assertCompileError 5  
+Pass assertCompileError 6  
+Pass assertCompileError 7  
+Pass assertCompileError 8  
 Pass assertCompileSuccess 1  
 Pass assertCompileSuccess 2  
 Pass 'WebAssembly.instantiate' data property  
-Fail 'WebAssembly.instantiate' function  assert_equals: expected 1 but got 2
+Pass 'WebAssembly.instantiate' function  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected success in assertInstantiateError  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  
+Pass unexpected failure in assertInstantiateSuccess  

+ 7 - 19
test/WasmSpec/jsapi.js

@@ -12,7 +12,7 @@ const verbose = options.indexOf("-verbose") !== -1;
 const self = this;
 const setTimeout = WScript.SetTimeout;
 const clearTimeout = WScript.ClearTimeout;
-self.addEventListener = function(){};
+self.addEventListener = function() {};
 
 class ConsoleTestEnvironment {
   on_tests_ready() {
@@ -26,7 +26,7 @@ class ConsoleTestEnvironment {
 
   // Should return a new unique default test name.
   next_default_test_name() {
-    return this.nextName = this.nextName|0 + 1;
+    return this.nextName = (this.nextName | 0) + 1;
   }
 
   // Should return the test harness timeout duration in milliseconds.
@@ -74,27 +74,15 @@ function reportResult(tests, harness_status) {
   function get_assertion(test) {
     if (test.properties.hasOwnProperty("assert")) {
       if (Array.isArray(test.properties.assert)) {
-        return test.properties.assert.join(' ');
+        return test.properties.assert.join(" ");
       }
       return test.properties.assert;
     }
-    return '';
+    return "";
   }
   const testsReport = tests.map(test => {
-    let stack = "";
-    if (verbose) {
-      const lines = (test.stack||"").split("at");
-      let firstLine = lines[0].trim();
-      if (firstLine.length > 0) {
-        firstLine = "\n" + firstLine;
-      }
-      stack = [firstLine, ...lines
-        .slice(1)
-        .filter(line => line.includes(testfile))
-        .map(line => line.replace(/\(.+\\([^\\]+:\d+:\d+)\)/, "$1").trim())
-      ].join("\n  at ");
-    }
-    return `${status_text[test.status]} ${test.name} ${get_assertion(test)} ${test.message||""}${stack}`
+    const stack = verbose ? test.stack : "";
+    return `${status_text[test.status]} ${test.name} ${get_assertion(test)} ${test.message || ""}${stack}`;
   });
   console.log(`Harness Status: ${status_text_harness[harness_status.status]}
 Found ${tests.length} tests: ${Object.keys(status_number).map(key => `${key} = ${status_number[key]}`).join(" ")}
@@ -111,4 +99,4 @@ function runTests() {
   });
 
   WScript.LoadScriptFile(testfile);
-}
+}