Line data Source code
1 : // Copyright 2015 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 : #ifndef V8_WASM_MODULE_DECODER_H_
6 : #define V8_WASM_MODULE_DECODER_H_
7 :
8 : #include "src/globals.h"
9 : #include "src/wasm/function-body-decoder.h"
10 : #include "src/wasm/wasm-module.h"
11 : #include "src/wasm/wasm-result.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 : namespace wasm {
16 :
17 : const uint32_t kWasmMagic = 0x6d736100;
18 : const uint32_t kWasmVersion = 0x01;
19 : const uint8_t kWasmFunctionTypeForm = 0x60;
20 : const uint8_t kWasmAnyFunctionTypeForm = 0x70;
21 : const uint8_t kResizableMaximumFlag = 1;
22 :
23 : enum SectionCode {
24 : kUnknownSectionCode = 0, // code for unknown sections
25 : kTypeSectionCode = 1, // Function signature declarations
26 : kImportSectionCode = 2, // Import declarations
27 : kFunctionSectionCode = 3, // Function declarations
28 : kTableSectionCode = 4, // Indirect function table and other tables
29 : kMemorySectionCode = 5, // Memory attributes
30 : kGlobalSectionCode = 6, // Global declarations
31 : kExportSectionCode = 7, // Exports
32 : kStartSectionCode = 8, // Start function declaration
33 : kElementSectionCode = 9, // Elements section
34 : kCodeSectionCode = 10, // Function code
35 : kDataSectionCode = 11, // Data segments
36 : kNameSectionCode = 12, // Name section (encoded as a string)
37 : };
38 :
39 : enum NameSectionType : uint8_t { kFunction = 1, kLocal = 2 };
40 :
41 : inline bool IsValidSectionCode(uint8_t byte) {
42 90874 : return kTypeSectionCode <= byte && byte <= kDataSectionCode;
43 : }
44 :
45 : const char* SectionName(SectionCode code);
46 :
47 : typedef Result<const WasmModule*> ModuleResult;
48 : typedef Result<WasmFunction*> FunctionResult;
49 : typedef std::vector<std::pair<int, int>> FunctionOffsets;
50 : typedef Result<FunctionOffsets> FunctionOffsetsResult;
51 : struct AsmJsOffsetEntry {
52 : int byte_offset;
53 : int source_position_call;
54 : int source_position_number_conversion;
55 : };
56 : typedef std::vector<std::vector<AsmJsOffsetEntry>> AsmJsOffsets;
57 : typedef Result<AsmJsOffsets> AsmJsOffsetsResult;
58 :
59 : // Decodes the bytes of a WASM module between {module_start} and {module_end}.
60 : V8_EXPORT_PRIVATE ModuleResult DecodeWasmModule(Isolate* isolate,
61 : const byte* module_start,
62 : const byte* module_end,
63 : bool verify_functions,
64 : ModuleOrigin origin);
65 :
66 : // Exposed for testing. Decodes a single function signature, allocating it
67 : // in the given zone. Returns {nullptr} upon failure.
68 : V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone,
69 : const byte* start,
70 : const byte* end);
71 :
72 : // Decodes the bytes of a WASM function between
73 : // {function_start} and {function_end}.
74 : V8_EXPORT_PRIVATE FunctionResult DecodeWasmFunction(Isolate* isolate,
75 : Zone* zone,
76 : ModuleBytesEnv* env,
77 : const byte* function_start,
78 : const byte* function_end);
79 :
80 : V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start,
81 : const byte* end);
82 :
83 : struct CustomSectionOffset {
84 : uint32_t section_start;
85 : uint32_t name_offset;
86 : uint32_t name_length;
87 : uint32_t payload_offset;
88 : uint32_t payload_length;
89 : uint32_t section_length;
90 : };
91 :
92 : V8_EXPORT_PRIVATE std::vector<CustomSectionOffset> DecodeCustomSections(
93 : const byte* start, const byte* end);
94 :
95 : // Extracts the mapping from wasm byte offset to asm.js source position per
96 : // function.
97 : // Returns a vector of vectors with <byte_offset, source_position> entries, or
98 : // failure if the wasm bytes are detected as invalid. Note that this validation
99 : // is not complete.
100 : AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* module_start,
101 : const byte* module_end);
102 :
103 : } // namespace wasm
104 : } // namespace internal
105 : } // namespace v8
106 :
107 : #endif // V8_WASM_MODULE_DECODER_H_
|