blob: 446883fd6be3866358f65b8659f1f816e14a1c7d [file] [log] [blame]
Ben Murdoch014dc512016-03-22 12:00:34 +00001// 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
Ben Murdochc8c1d9e2017-03-08 14:04:23 +00008#include "src/globals.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +01009#include "src/wasm/function-body-decoder.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000010#include "src/wasm/wasm-module.h"
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000011#include "src/wasm/wasm-result.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000012
13namespace v8 {
14namespace internal {
15namespace wasm {
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000016
Ben Murdoch62ed6312017-06-06 11:06:27 +010017const uint32_t kWasmMagic = 0x6d736100;
18const uint32_t kWasmVersion = 0x01;
19const uint8_t kWasmFunctionTypeForm = 0x60;
20const uint8_t kWasmAnyFunctionTypeForm = 0x70;
21const uint8_t kResizableMaximumFlag = 1;
22
23enum WasmSectionCode {
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
39inline bool IsValidSectionCode(uint8_t byte) {
40 return kTypeSectionCode <= byte && byte <= kDataSectionCode;
41}
42
43const char* SectionName(WasmSectionCode code);
44
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000045typedef Result<const WasmModule*> ModuleResult;
46typedef Result<WasmFunction*> FunctionResult;
47typedef std::vector<std::pair<int, int>> FunctionOffsets;
48typedef Result<FunctionOffsets> FunctionOffsetsResult;
Ben Murdoch62ed6312017-06-06 11:06:27 +010049struct AsmJsOffsetEntry {
50 int byte_offset;
51 int source_position_call;
52 int source_position_number_conversion;
53};
54typedef std::vector<std::vector<AsmJsOffsetEntry>> AsmJsOffsets;
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000055typedef Result<AsmJsOffsets> AsmJsOffsetsResult;
56
Ben Murdoch014dc512016-03-22 12:00:34 +000057// Decodes the bytes of a WASM module between {module_start} and {module_end}.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000058V8_EXPORT_PRIVATE ModuleResult DecodeWasmModule(Isolate* isolate,
Ben Murdochf3b273f2017-01-17 12:11:28 +000059 const byte* module_start,
60 const byte* module_end,
61 bool verify_functions,
62 ModuleOrigin origin);
Ben Murdoch014dc512016-03-22 12:00:34 +000063
64// Exposed for testing. Decodes a single function signature, allocating it
65// in the given zone. Returns {nullptr} upon failure.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000066V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone,
67 const byte* start,
68 const byte* end);
Ben Murdoch014dc512016-03-22 12:00:34 +000069
70// Decodes the bytes of a WASM function between
71// {function_start} and {function_end}.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000072V8_EXPORT_PRIVATE FunctionResult DecodeWasmFunction(Isolate* isolate,
Ben Murdoch62ed6312017-06-06 11:06:27 +010073 Zone* zone,
74 ModuleBytesEnv* env,
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000075 const byte* function_start,
76 const byte* function_end);
Ben Murdoch13e2dad2016-09-16 13:49:30 +010077
78// Extracts the function offset table from the wasm module bytes.
79// Returns a vector with <offset, length> entries, or failure if the wasm bytes
80// are detected as invalid. Note that this validation is not complete.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000081FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start,
82 const byte* module_end);
Ben Murdochf3b273f2017-01-17 12:11:28 +000083
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000084V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start,
85 const byte* end);
86
Ben Murdoch62ed6312017-06-06 11:06:27 +010087struct CustomSectionOffset {
88 uint32_t section_start;
89 uint32_t name_offset;
90 uint32_t name_length;
91 uint32_t payload_offset;
92 uint32_t payload_length;
93 uint32_t section_length;
94};
95
96V8_EXPORT_PRIVATE std::vector<CustomSectionOffset> DecodeCustomSections(
97 const byte* start, const byte* end);
98
Ben Murdochc8c1d9e2017-03-08 14:04:23 +000099// Extracts the mapping from wasm byte offset to asm.js source position per
100// function.
101// Returns a vector of vectors with <byte_offset, source_position> entries, or
102// failure if the wasm bytes are detected as invalid. Note that this validation
103// is not complete.
104AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* module_start,
105 const byte* module_end);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100106
Ben Murdoch014dc512016-03-22 12:00:34 +0000107} // namespace wasm
108} // namespace internal
109} // namespace v8
110
111#endif // V8_WASM_MODULE_DECODER_H_