LCOV - code coverage report
Current view: top level - test/unittests/wasm - streaming-decoder-unittest.cc (source / functions) Hit Total Coverage
Test: app.info Lines: 207 210 98.6 %
Date: 2019-04-18 Functions: 79 115 68.7 %

          Line data    Source code
       1             : // Copyright 2017 the V8 project authors. All rights reserved.
       2             : // Use of this source code is governed by a BSD-style license that can be
       3             : // found in the LICENSE file.
       4             : 
       5             : #include "test/unittests/test-utils.h"
       6             : 
       7             : #include "src/objects-inl.h"
       8             : 
       9             : #include "src/wasm/module-decoder.h"
      10             : #include "src/wasm/streaming-decoder.h"
      11             : 
      12             : #include "src/objects/descriptor-array.h"
      13             : #include "src/objects/dictionary.h"
      14             : #include "test/common/wasm/wasm-macro-gen.h"
      15             : 
      16             : namespace v8 {
      17             : namespace internal {
      18             : namespace wasm {
      19             : 
      20        2260 : struct MockStreamingResult {
      21             :   size_t num_sections = 0;
      22             :   size_t num_functions = 0;
      23             :   WasmError error;
      24             :   OwnedVector<uint8_t> received_bytes;
      25             : 
      26         554 :   bool ok() const { return !error.has_error(); }
      27             : 
      28        1130 :   MockStreamingResult() = default;
      29             : };
      30             : 
      31        2260 : class MockStreamingProcessor : public StreamingProcessor {
      32             :  public:
      33             :   explicit MockStreamingProcessor(MockStreamingResult* result)
      34        1130 :       : result_(result) {}
      35             : 
      36        1093 :   bool ProcessModuleHeader(Vector<const uint8_t> bytes,
      37             :                            uint32_t offset) override {
      38             :     Decoder decoder(bytes.begin(), bytes.end());
      39             :     uint32_t magic_word = decoder.consume_u32("wasm magic");
      40        1093 :     if (decoder.failed() || magic_word != kWasmMagic) {
      41         576 :       result_->error = WasmError(0, "expected wasm magic");
      42         288 :       return false;
      43             :     }
      44             :     uint32_t magic_version = decoder.consume_u32("wasm version");
      45         805 :     if (decoder.failed() || magic_version != kWasmVersion) {
      46         576 :       result_->error = WasmError(4, "expected wasm version");
      47         288 :       return false;
      48             :     }
      49             :     return true;
      50             :   }
      51             : 
      52             :   // Process all sections but the code section.
      53         269 :   bool ProcessSection(SectionCode section_code, Vector<const uint8_t> bytes,
      54             :                       uint32_t offset) override {
      55         269 :     ++result_->num_sections;
      56         269 :     return true;
      57             :   }
      58             : 
      59         268 :   bool ProcessCodeSectionHeader(int num_functions, uint32_t offset,
      60             :                                 std::shared_ptr<WireBytesStorage>) override {
      61         268 :     return true;
      62             :   }
      63             : 
      64             :   // Process a function body.
      65         341 :   bool ProcessFunctionBody(Vector<const uint8_t> bytes,
      66             :                            uint32_t offset) override {
      67         341 :     ++result_->num_functions;
      68         341 :     return true;
      69             :   }
      70             : 
      71        1371 :   void OnFinishedChunk() override {}
      72             : 
      73             :   // Finish the processing of the stream.
      74         260 :   void OnFinishedStream(OwnedVector<uint8_t> bytes) override {
      75         260 :     result_->received_bytes = std::move(bytes);
      76         260 :   }
      77             : 
      78             :   // Report an error detected in the StreamingDecoder.
      79         294 :   void OnError(const WasmError& error) override {
      80         294 :     result_->error = error;
      81         588 :     CHECK(!result_->ok());
      82         294 :   }
      83             : 
      84           0 :   void OnAbort() override {}
      85             : 
      86           0 :   bool Deserialize(Vector<const uint8_t> module_bytes,
      87             :                    Vector<const uint8_t> wire_bytes) override {
      88           0 :     return false;
      89             :   }
      90             : 
      91             :  private:
      92             :   MockStreamingResult* const result_;
      93             : };
      94             : 
      95          66 : class WasmStreamingDecoderTest : public ::testing::Test {
      96             :  public:
      97          15 :   void ExpectVerifies(Vector<const uint8_t> data, size_t expected_sections,
      98             :                       size_t expected_functions) {
      99         535 :     for (int split = 0; split <= data.length(); ++split) {
     100         260 :       MockStreamingResult result;
     101             :       StreamingDecoder stream(
     102         780 :           base::make_unique<MockStreamingProcessor>(&result));
     103         260 :       stream.OnBytesReceived(data.SubVector(0, split));
     104         520 :       stream.OnBytesReceived(data.SubVector(split, data.length()));
     105         260 :       stream.Finish();
     106         260 :       EXPECT_TRUE(result.ok());
     107         260 :       EXPECT_EQ(expected_sections, result.num_sections);
     108         260 :       EXPECT_EQ(expected_functions, result.num_functions);
     109         520 :       EXPECT_EQ(data, result.received_bytes.as_vector());
     110             :     }
     111          15 :   }
     112             : 
     113          85 :   void ExpectFailure(Vector<const uint8_t> data, uint32_t error_offset,
     114             :                      const char* message) {
     115        1821 :     for (int split = 0; split <= data.length(); ++split) {
     116         868 :       MockStreamingResult result;
     117             :       StreamingDecoder stream(
     118        2604 :           base::make_unique<MockStreamingProcessor>(&result));
     119         868 :       stream.OnBytesReceived(data.SubVector(0, split));
     120        1736 :       stream.OnBytesReceived(data.SubVector(split, data.length()));
     121         868 :       stream.Finish();
     122        1736 :       EXPECT_FALSE(result.ok());
     123        1736 :       EXPECT_EQ(error_offset, result.error.offset());
     124         868 :       EXPECT_EQ(message, result.error.message());
     125             :     }
     126          85 :   }
     127             : };
     128             : 
     129       15419 : TEST_F(WasmStreamingDecoderTest, EmptyStream) {
     130           1 :   MockStreamingResult result;
     131           3 :   StreamingDecoder stream(base::make_unique<MockStreamingProcessor>(&result));
     132           1 :   stream.Finish();
     133           2 :   EXPECT_FALSE(result.ok());
     134           1 : }
     135             : 
     136       15419 : TEST_F(WasmStreamingDecoderTest, IncompleteModuleHeader) {
     137           1 :   const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion)};
     138             :   {
     139           1 :     MockStreamingResult result;
     140           3 :     StreamingDecoder stream(base::make_unique<MockStreamingProcessor>(&result));
     141           1 :     stream.OnBytesReceived(VectorOf(data, 1));
     142           1 :     stream.Finish();
     143           2 :     EXPECT_FALSE(result.ok());
     144             :   }
     145          15 :   for (uint32_t length = 1; length < sizeof(data); ++length) {
     146           7 :     ExpectFailure(VectorOf(data, length), length - 1,
     147           7 :                   "unexpected end of stream");
     148             :   }
     149           1 : }
     150             : 
     151       15419 : TEST_F(WasmStreamingDecoderTest, MagicAndVersion) {
     152           1 :   const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion)};
     153           1 :   ExpectVerifies(ArrayVector(data), 0, 0);
     154           1 : }
     155             : 
     156       15419 : TEST_F(WasmStreamingDecoderTest, BadMagic) {
     157          65 :   for (uint32_t x = 1; x; x <<= 1) {
     158          32 :     const uint8_t data[] = {U32_LE(kWasmMagic ^ x), U32_LE(kWasmVersion)};
     159          32 :     ExpectFailure(ArrayVector(data), 0, "expected wasm magic");
     160             :   }
     161           1 : }
     162             : 
     163       15419 : TEST_F(WasmStreamingDecoderTest, BadVersion) {
     164          65 :   for (uint32_t x = 1; x; x <<= 1) {
     165          32 :     const uint8_t data[] = {U32_LE(kWasmMagic), U32_LE(kWasmVersion ^ x)};
     166          32 :     ExpectFailure(ArrayVector(data), 4, "expected wasm version");
     167             :   }
     168           1 : }
     169             : 
     170       15419 : TEST_F(WasmStreamingDecoderTest, OneSection) {
     171             :   const uint8_t data[] = {
     172             :       U32_LE(kWasmMagic),    // --
     173             :       U32_LE(kWasmVersion),  // --
     174             :       0x1,                   // Section ID
     175             :       0x6,                   // Section Length
     176             :       0x0,                   // Payload
     177             :       0x0,                   // 2
     178             :       0x0,                   // 3
     179             :       0x0,                   // 4
     180             :       0x0,                   // 5
     181             :       0x0                    // 6
     182           1 :   };
     183           1 :   ExpectVerifies(ArrayVector(data), 1, 0);
     184           1 : }
     185             : 
     186       15419 : TEST_F(WasmStreamingDecoderTest, OneSection_b) {
     187             :   const uint8_t data[] = {
     188             :       U32_LE(kWasmMagic),    // --
     189             :       U32_LE(kWasmVersion),  // --
     190             :       0x1,                   // Section ID
     191             :       0x86,                  // Section Length = 6 (LEB)
     192             :       0x0,                   // --
     193             :       0x0,                   // Payload
     194             :       0x0,                   // 2
     195             :       0x0,                   // 3
     196             :       0x0,                   // 4
     197             :       0x0,                   // 5
     198             :       0x0                    // 6
     199           1 :   };
     200           1 :   ExpectVerifies(ArrayVector(data), 1, 0);
     201           1 : }
     202             : 
     203       15419 : TEST_F(WasmStreamingDecoderTest, OneShortSection) {
     204             :   // Short section means that section length + payload is less than 5 bytes,
     205             :   // which is the maximum size of the length field.
     206             :   const uint8_t data[] = {
     207             :       U32_LE(kWasmMagic),    // --
     208             :       U32_LE(kWasmVersion),  // --
     209             :       0x1,                   // Section ID
     210             :       0x2,                   // Section Length
     211             :       0x0,                   // Payload
     212             :       0x0                    // 2
     213           1 :   };
     214           1 :   ExpectVerifies(ArrayVector(data), 1, 0);
     215           1 : }
     216             : 
     217       15419 : TEST_F(WasmStreamingDecoderTest, OneShortSection_b) {
     218             :   const uint8_t data[] = {
     219             :       U32_LE(kWasmMagic),    // --
     220             :       U32_LE(kWasmVersion),  // --
     221             :       0x1,                   // Section ID
     222             :       0x82,                  // Section Length = 2 (LEB)
     223             :       0x80,                  // --
     224             :       0x0,                   // --
     225             :       0x0,                   // Payload
     226             :       0x0                    // 2
     227           1 :   };
     228           1 :   ExpectVerifies(ArrayVector(data), 1, 0);
     229           1 : }
     230             : 
     231       15419 : TEST_F(WasmStreamingDecoderTest, OneEmptySection) {
     232             :   const uint8_t data[] = {
     233             :       U32_LE(kWasmMagic),    // --
     234             :       U32_LE(kWasmVersion),  // --
     235             :       0x1,                   // Section ID
     236             :       0x0                    // Section Length
     237           1 :   };
     238           1 :   ExpectVerifies(ArrayVector(data), 1, 0);
     239           1 : }
     240             : 
     241       15419 : TEST_F(WasmStreamingDecoderTest, OneSectionNotEnoughPayload1) {
     242             :   const uint8_t data[] = {
     243             :       U32_LE(kWasmMagic),    // --
     244             :       U32_LE(kWasmVersion),  // --
     245             :       0x1,                   // Section ID
     246             :       0x6,                   // Section Length
     247             :       0x0,                   // Payload
     248             :       0x0,                   // 2
     249             :       0x0,                   // 3
     250             :       0x0,                   // 4
     251             :       0x0                    // 5
     252           1 :   };
     253           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     254           1 :                 "unexpected end of stream");
     255           1 : }
     256             : 
     257       15419 : TEST_F(WasmStreamingDecoderTest, OneSectionNotEnoughPayload2) {
     258             :   const uint8_t data[] = {
     259             :       U32_LE(kWasmMagic),    // --
     260             :       U32_LE(kWasmVersion),  // --
     261             :       0x1,                   // Section ID
     262             :       0x6,                   // Section Length
     263             :       0x0                    // Payload
     264           1 :   };
     265           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     266           1 :                 "unexpected end of stream");
     267           1 : }
     268             : 
     269       15419 : TEST_F(WasmStreamingDecoderTest, OneSectionInvalidLength) {
     270             :   const uint8_t data[] = {
     271             :       U32_LE(kWasmMagic),    // --
     272             :       U32_LE(kWasmVersion),  // --
     273             :       0x1,                   // Section ID
     274             :       0x80,                  // Section Length (invalid LEB)
     275             :       0x80,                  // --
     276             :       0x80,                  // --
     277             :       0x80,                  // --
     278             :       0x80,                  // --
     279           1 :   };
     280           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1, "expected section length");
     281           1 : }
     282             : 
     283       15419 : TEST_F(WasmStreamingDecoderTest, TwoLongSections) {
     284             :   const uint8_t data[] = {
     285             :       U32_LE(kWasmMagic),    // --
     286             :       U32_LE(kWasmVersion),  // --
     287             :       0x1,                   // Section ID
     288             :       0x6,                   // Section Length
     289             :       0x0,                   // Payload
     290             :       0x0,                   // 2
     291             :       0x0,                   // 3
     292             :       0x0,                   // 4
     293             :       0x0,                   // 5
     294             :       0x0,                   // 6
     295             :       0x2,                   // Section ID
     296             :       0x7,                   // Section Length
     297             :       0x0,                   // Payload
     298             :       0x0,                   // 2
     299             :       0x0,                   // 3
     300             :       0x0,                   // 4
     301             :       0x0,                   // 5
     302             :       0x0,                   // 6
     303             :       0x0                    // 7
     304           1 :   };
     305           1 :   ExpectVerifies(ArrayVector(data), 2, 0);
     306           1 : }
     307             : 
     308       15419 : TEST_F(WasmStreamingDecoderTest, TwoShortSections) {
     309             :   const uint8_t data[] = {
     310             :       U32_LE(kWasmMagic),    // --
     311             :       U32_LE(kWasmVersion),  // --
     312             :       0x1,                   // Section ID
     313             :       0x1,                   // Section Length
     314             :       0x0,                   // Payload
     315             :       0x2,                   // Section ID
     316             :       0x2,                   // Section Length
     317             :       0x0,                   // Payload
     318             :       0x0,                   // 2
     319           1 :   };
     320           1 :   ExpectVerifies(ArrayVector(data), 2, 0);
     321           1 : }
     322             : 
     323       15419 : TEST_F(WasmStreamingDecoderTest, TwoSectionsShortLong) {
     324             :   const uint8_t data[] = {
     325             :       U32_LE(kWasmMagic),    // --
     326             :       U32_LE(kWasmVersion),  // --
     327             :       0x1,                   // Section ID
     328             :       0x1,                   // Section Length
     329             :       0x0,                   // Payload
     330             :       0x2,                   // Section ID
     331             :       0x7,                   // Section Length
     332             :       0x0,                   // Payload
     333             :       0x0,                   // 2
     334             :       0x0,                   // 3
     335             :       0x0,                   // 4
     336             :       0x0,                   // 5
     337             :       0x0,                   // 6
     338             :       0x0                    // 7
     339           1 :   };
     340           1 :   ExpectVerifies(ArrayVector(data), 2, 0);
     341           1 : }
     342             : 
     343       15419 : TEST_F(WasmStreamingDecoderTest, TwoEmptySections) {
     344             :   const uint8_t data[] = {
     345             :       U32_LE(kWasmMagic),    // --
     346             :       U32_LE(kWasmVersion),  // --
     347             :       0x1,                   // Section ID
     348             :       0x0,                   // Section Length
     349             :       0x2,                   // Section ID
     350             :       0x0                    // Section Length
     351           1 :   };
     352           1 :   ExpectVerifies(ArrayVector(data), 2, 0);
     353           1 : }
     354             : 
     355       15419 : TEST_F(WasmStreamingDecoderTest, OneFunction) {
     356             :   const uint8_t data[] = {
     357             :       U32_LE(kWasmMagic),    // --
     358             :       U32_LE(kWasmVersion),  // --
     359             :       kCodeSectionCode,      // Section ID
     360             :       0x8,                   // Section Length
     361             :       0x1,                   // Number of Functions
     362             :       0x6,                   // Function Length
     363             :       0x0,                   // Function
     364             :       0x0,                   // 2
     365             :       0x0,                   // 3
     366             :       0x0,                   // 4
     367             :       0x0,                   // 5
     368             :       0x0,                   // 6
     369           1 :   };
     370           1 :   ExpectVerifies(ArrayVector(data), 0, 1);
     371           1 : }
     372             : 
     373       15419 : TEST_F(WasmStreamingDecoderTest, OneShortFunction) {
     374             :   const uint8_t data[] = {
     375             :       U32_LE(kWasmMagic),    // --
     376             :       U32_LE(kWasmVersion),  // --
     377             :       kCodeSectionCode,      // Section ID
     378             :       0x3,                   // Section Length
     379             :       0x1,                   // Number of Functions
     380             :       0x1,                   // Function Length
     381             :       0x0,                   // Function
     382           1 :   };
     383           1 :   ExpectVerifies(ArrayVector(data), 0, 1);
     384           1 : }
     385             : 
     386       15419 : TEST_F(WasmStreamingDecoderTest, EmptyFunction) {
     387             :   const uint8_t data[] = {
     388             :       U32_LE(kWasmMagic),    // --
     389             :       U32_LE(kWasmVersion),  // --
     390             :       kCodeSectionCode,      // Section ID
     391             :       0x2,                   // Section Length
     392             :       0x1,                   // Number of Functions
     393             :       0x0,                   // Function Length  -- ERROR
     394           1 :   };
     395           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     396           1 :                 "invalid function length (0)");
     397           1 : }
     398             : 
     399       15419 : TEST_F(WasmStreamingDecoderTest, TwoFunctions) {
     400             :   const uint8_t data[] = {
     401             :       U32_LE(kWasmMagic),    // --
     402             :       U32_LE(kWasmVersion),  // --
     403             :       kCodeSectionCode,      // Section ID
     404             :       0x10,                  // Section Length
     405             :       0x2,                   // Number of Functions
     406             :       0x6,                   // Function Length
     407             :       0x0,                   // Function
     408             :       0x0,                   // 2
     409             :       0x0,                   // 3
     410             :       0x0,                   // 4
     411             :       0x0,                   // 5
     412             :       0x0,                   // 6
     413             :       0x7,                   // Function Length
     414             :       0x0,                   // Function
     415             :       0x0,                   // 2
     416             :       0x0,                   // 3
     417             :       0x0,                   // 4
     418             :       0x0,                   // 5
     419             :       0x0,                   // 6
     420             :       0x0,                   // 7
     421           1 :   };
     422           1 :   ExpectVerifies(ArrayVector(data), 0, 2);
     423           1 : }
     424             : 
     425       15419 : TEST_F(WasmStreamingDecoderTest, TwoFunctions_b) {
     426             :   const uint8_t data[] = {
     427             :       U32_LE(kWasmMagic),    // --
     428             :       U32_LE(kWasmVersion),  // --
     429             :       kCodeSectionCode,      // Section ID
     430             :       0xB,                   // Section Length
     431             :       0x2,                   // Number of Functions
     432             :       0x1,                   // Function Length
     433             :       0x0,                   // Function
     434             :       0x7,                   // Function Length
     435             :       0x0,                   // Function
     436             :       0x0,                   // 2
     437             :       0x0,                   // 3
     438             :       0x0,                   // 4
     439             :       0x0,                   // 5
     440             :       0x0,                   // 6
     441             :       0x0,                   // 7
     442           1 :   };
     443           1 :   ExpectVerifies(ArrayVector(data), 0, 2);
     444           1 : }
     445             : 
     446       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthZero) {
     447             :   const uint8_t data[] = {
     448             :       U32_LE(kWasmMagic),    // --
     449             :       U32_LE(kWasmVersion),  // --
     450             :       kCodeSectionCode,      // Section ID
     451             :       0x0,                   // Section Length
     452           1 :   };
     453           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     454           1 :                 "code section cannot have size 0");
     455           1 : }
     456             : 
     457       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHigh) {
     458             :   const uint8_t data[] = {
     459             :       U32_LE(kWasmMagic),    // --
     460             :       U32_LE(kWasmVersion),  // --
     461             :       kCodeSectionCode,      // Section ID
     462             :       0xD,                   // Section Length
     463             :       0x2,                   // Number of Functions
     464             :       0x7,                   // Function Length
     465             :       0x0,                   // Function
     466             :       0x0,                   // 2
     467             :       0x0,                   // 3
     468             :       0x0,                   // 4
     469             :       0x0,                   // 5
     470             :       0x0,                   // 6
     471             :       0x0,                   // 7
     472             :       0x1,                   // Function Length
     473             :       0x0,                   // Function
     474           1 :   };
     475           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     476           1 :                 "not all code section bytes were used");
     477           1 : }
     478             : 
     479       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooHighZeroFunctions) {
     480             :   const uint8_t data[] = {
     481             :       U32_LE(kWasmMagic),    // --
     482             :       U32_LE(kWasmVersion),  // --
     483             :       kCodeSectionCode,      // Section ID
     484             :       0xD,                   // Section Length
     485             :       0x0,                   // Number of Functions
     486           1 :   };
     487           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     488           1 :                 "not all code section bytes were used");
     489           1 : }
     490             : 
     491       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLow) {
     492             :   const uint8_t data[] = {
     493             :       U32_LE(kWasmMagic),    // --
     494             :       U32_LE(kWasmVersion),  // --
     495             :       kCodeSectionCode,      // Section ID
     496             :       0x9,                   // Section Length
     497             :       0x2,                   // Number of Functions  <0>
     498             :       0x7,                   // Function Length      <1>
     499             :       0x0,                   // Function             <2>
     500             :       0x0,                   // 2                    <3>
     501             :       0x0,                   // 3                    <3>
     502             :       0x0,                   // 4                    <4>
     503             :       0x0,                   // 5                    <5>
     504             :       0x0,                   // 6                    <6>
     505             :       0x0,                   // 7                    <7>
     506             :       0x1,                   // Function Length      <8> -- ERROR
     507             :       0x0,                   // Function
     508           1 :   };
     509           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 2,
     510           1 :                 "read past code section end");
     511           1 : }
     512             : 
     513       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInNumFunctions) {
     514             :   const uint8_t data[] = {
     515             :       U32_LE(kWasmMagic),    // --
     516             :       U32_LE(kWasmVersion),  // --
     517             :       kCodeSectionCode,      // Section ID
     518             :       0x1,                   // Section Length
     519             :       0x82,                  // Number of Functions  <0>
     520             :       0x80,                  // --                   <1> -- ERROR
     521             :       0x00,                  // --
     522             :       0x7,                   // Function Length
     523             :       0x0,                   // Function
     524             :       0x0,                   // 2
     525             :       0x0,                   // 3
     526             :       0x0,                   // 4
     527             :       0x0,                   // 5
     528             :       0x0,                   // 6
     529             :       0x0,                   // 7
     530             :       0x1,                   // Function Length
     531             :       0x0,                   // Function
     532           1 :   };
     533           1 :   ExpectFailure(ArrayVector(data), 12, "invalid code section length");
     534           1 : }
     535             : 
     536       15419 : TEST_F(WasmStreamingDecoderTest, CodeSectionLengthTooLowEndsInFunctionLength) {
     537             :   const uint8_t data[] = {
     538             :       U32_LE(kWasmMagic),    // --
     539             :       U32_LE(kWasmVersion),  // --
     540             :       kCodeSectionCode,      // Section ID
     541             :       0x5,                   // Section Length
     542             :       0x82,                  // Number of Functions  <0>
     543             :       0x80,                  // --                   <1>
     544             :       0x00,                  // --                   <2>
     545             :       0x87,                  // Function Length      <3>
     546             :       0x80,                  // --                   <4>
     547             :       0x00,                  // --                   <5> -- ERROR
     548             :       0x0,                   // Function
     549             :       0x0,                   // 2
     550             :       0x0,                   // 3
     551             :       0x0,                   // 4
     552             :       0x0,                   // 5
     553             :       0x0,                   // 6
     554             :       0x0,                   // 7
     555             :       0x1,                   // Function Length
     556             :       0x0,                   // Function
     557           1 :   };
     558           1 :   ExpectFailure(ArrayVector(data), 15, "read past code section end");
     559           1 : }
     560             : 
     561       15419 : TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooHigh) {
     562             :   const uint8_t data[] = {
     563             :       U32_LE(kWasmMagic),    // --
     564             :       U32_LE(kWasmVersion),  // --
     565             :       kCodeSectionCode,      // Section ID
     566             :       0xB,                   // Section Length
     567             :       0x4,                   // Number of Functions
     568             :       0x7,                   // Function Length
     569             :       0x0,                   // Function
     570             :       0x0,                   // 2
     571             :       0x0,                   // 3
     572             :       0x0,                   // 4
     573             :       0x0,                   // 5
     574             :       0x0,                   // 6
     575             :       0x0,                   // 7
     576             :       0x1,                   // Function Length
     577             :       0x0,                   // Function
     578           1 :   };
     579           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 1,
     580           1 :                 "unexpected end of stream");
     581           1 : }
     582             : 
     583       15419 : TEST_F(WasmStreamingDecoderTest, NumberOfFunctionsTooLow) {
     584             :   const uint8_t data[] = {
     585             :       U32_LE(kWasmMagic),    // --
     586             :       U32_LE(kWasmVersion),  // --
     587             :       kCodeSectionCode,      // Section ID
     588             :       0x8,                   // Section Length
     589             :       0x2,                   // Number of Functions
     590             :       0x1,                   // Function Length
     591             :       0x0,                   // Function
     592             :       0x2,                   // Function Length
     593             :       0x0,                   // Function byte#0
     594             :       0x0,                   // Function byte#1   -- ERROR
     595             :       0x1,                   // Function Length
     596             :       0x0                    // Function
     597           1 :   };
     598           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 3,
     599           1 :                 "not all code section bytes were used");
     600           1 : }
     601             : 
     602       15419 : TEST_F(WasmStreamingDecoderTest, TwoCodeSections) {
     603             :   const uint8_t data[] = {
     604             :       U32_LE(kWasmMagic),    // --
     605             :       U32_LE(kWasmVersion),  // --
     606             :       kCodeSectionCode,      // Section ID
     607             :       0x3,                   // Section Length
     608             :       0x1,                   // Number of Functions
     609             :       0x1,                   // Function Length
     610             :       0x0,                   // Function
     611             :       kCodeSectionCode,      // Section ID      -- ERROR (where it should be)
     612             :       0x3,                   // Section Length  -- ERROR (where it is reported)
     613             :       0x1,                   // Number of Functions
     614             :       0x1,                   // Function Length
     615             :       0x0,                   // Function
     616           1 :   };
     617             :   // TODO(wasm): This should report at the second kCodeSectionCode.
     618           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 4,
     619           1 :                 "code section can only appear once");
     620           1 : }
     621             : 
     622       15419 : TEST_F(WasmStreamingDecoderTest, UnknownSection) {
     623             :   const uint8_t data[] = {
     624             :       U32_LE(kWasmMagic),    // --
     625             :       U32_LE(kWasmVersion),  // --
     626             :       kCodeSectionCode,      // Section ID
     627             :       0x3,                   // Section Length
     628             :       0x1,                   // Number of Functions
     629             :       0x1,                   // Function Length
     630             :       0x0,                   // Function
     631             :       kUnknownSectionCode,   // Section ID
     632             :       0x3,                   // Section Length
     633             :       0x1,                   // Name Length
     634             :       0x1,                   // Name
     635             :       0x0,                   // Content
     636           1 :   };
     637           1 :   ExpectVerifies(ArrayVector(data), 1, 1);
     638           1 : }
     639             : 
     640       15419 : TEST_F(WasmStreamingDecoderTest, UnknownSectionSandwich) {
     641             :   const uint8_t data[] = {
     642             :       U32_LE(kWasmMagic),    // --
     643             :       U32_LE(kWasmVersion),  // --
     644             :       kCodeSectionCode,      // Section ID
     645             :       0x3,                   // Section Length
     646             :       0x1,                   // Number of Functions
     647             :       0x1,                   // Function Length
     648             :       0x0,                   // Function
     649             :       kUnknownSectionCode,   // Section ID
     650             :       0x3,                   // Section Length
     651             :       0x1,                   // Name Length
     652             :       0x1,                   // Name
     653             :       0x0,                   // Content
     654             :       kCodeSectionCode,      // Section ID     -- ERROR (where it should be)
     655             :       0x3,                   // Section Length -- ERROR (where it is reported)
     656             :       0x1,                   // Number of Functions
     657             :       0x1,                   // Function Length
     658             :       0x0,                   // Function
     659           1 :   };
     660             :   // TODO(wasm): This should report at the second kCodeSectionCode.
     661           1 :   ExpectFailure(ArrayVector(data), sizeof(data) - 4,
     662           1 :                 "code section can only appear once");
     663           1 : }
     664             : 
     665             : }  // namespace wasm
     666             : }  // namespace internal
     667        9249 : }  // namespace v8

Generated by: LCOV version 1.10