WasmBinaryReader.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #pragma once
  6. #ifdef ENABLE_WASM
  7. namespace Wasm
  8. {
  9. // Language Types binary encoding with varint7
  10. namespace LanguageTypes
  11. {
  12. const int8 i32 = 0x80 - 0x1;
  13. const int8 i64 = 0x80 - 0x2;
  14. const int8 f32 = 0x80 - 0x3;
  15. const int8 f64 = 0x80 - 0x4;
  16. const int8 anyfunc = 0x80 - 0x10;
  17. const int8 func = 0x80 - 0x20;
  18. const int8 emptyBlock = 0x80 - 0x40;
  19. WasmTypes::WasmType ToWasmType(int8);
  20. }
  21. struct SectionHeader
  22. {
  23. SectionCode code;
  24. const byte* start;
  25. const byte* end;
  26. uint32 nameLength;
  27. const char* name;
  28. };
  29. static const unsigned int experimentalVersion = 0xd;
  30. class WasmBinaryReader : public WasmReaderBase
  31. {
  32. public:
  33. WasmBinaryReader(ArenaAllocator* alloc, Js::WebAssemblyModule * module, const byte* source, size_t length);
  34. void InitializeReader();
  35. bool ReadNextSection(SectionCode nextSection);
  36. // Fully read the section in the reader. Return true if the section fully read
  37. bool ProcessCurrentSection();
  38. virtual void SeekToFunctionBody(FunctionBodyReaderInfo readerInfo) override;
  39. virtual bool IsCurrentFunctionCompleted() const override;
  40. virtual WasmOp ReadExpr() override;
  41. virtual void FunctionEnd() override;
  42. #if DBG_DUMP
  43. void PrintOps();
  44. #endif
  45. intptr_t GetCurrentOffset() const { return m_pc - m_start; }
  46. private:
  47. struct ReaderState
  48. {
  49. UINT32 count; // current entry
  50. size_t size; // number of entries
  51. };
  52. void BlockNode();
  53. void CallNode();
  54. void CallIndirectNode();
  55. void BrNode();
  56. void BrTableNode();
  57. void MemNode();
  58. void VarNode();
  59. // Module readers
  60. void ValidateModuleHeader();
  61. SectionHeader ReadSectionHeader();
  62. void ReadMemorySection(bool isImportSection);
  63. void ReadSignatures();
  64. void ReadFunctionsSignatures();
  65. void ReadFunctionHeaders();
  66. void ReadExportTable();
  67. void ReadTableSection(bool isImportSection);
  68. void ReadDataSegments();
  69. void ReadImportEntries();
  70. void ReadStartFunction();
  71. void ReadNamesSection();
  72. void ReadElementSection();
  73. void ReadGlobalsSection();
  74. void ReadCustomSection();
  75. // Primitive reader
  76. template <WasmTypes::WasmType type> void ConstNode();
  77. template <typename T> T ReadConst();
  78. uint8 ReadVarUInt7();
  79. bool ReadMutableValue();
  80. const char16* ReadInlineName(uint32& length, uint32& nameLength);
  81. const char16* CvtUtf8Str(LPCUTF8 name, uint32 nameLen, charcount_t* dstLength = nullptr);
  82. template<typename MaxAllowedType = UINT>
  83. MaxAllowedType LEB128(UINT &length, bool sgn = false);
  84. template<typename MaxAllowedType = INT>
  85. MaxAllowedType SLEB128(UINT &length);
  86. WasmNode ReadInitExpr(bool isOffset = false);
  87. void CheckBytesLeft(UINT bytesNeeded);
  88. bool EndOfFunc();
  89. bool EndOfModule();
  90. DECLSPEC_NORETURN void ThrowDecodingError(const char16* msg, ...);
  91. Wasm::WasmTypes::WasmType ReadWasmType(uint32& length);
  92. ArenaAllocator* m_alloc;
  93. uint m_funcNumber;
  94. const byte* m_start, *m_end, *m_pc, *m_curFuncEnd;
  95. SectionHeader m_currentSection;
  96. ReaderState m_funcState; // func AST level
  97. private:
  98. enum
  99. {
  100. READER_STATE_UNKNOWN,
  101. READER_STATE_FUNCTION,
  102. READER_STATE_MODULE
  103. } m_readerState;
  104. Js::WebAssemblyModule * m_module;
  105. #if DBG_DUMP
  106. typedef JsUtil::BaseHashSet<WasmOp, ArenaAllocator, PowerOf2SizePolicy> OpSet;
  107. OpSet* m_ops;
  108. #endif
  109. }; // WasmBinaryReader
  110. } // namespace Wasm
  111. #endif // ENABLE_WASM