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/ast-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 typedef Result<const WasmModule*> ModuleResult;
18 typedef Result<WasmFunction*> FunctionResult;
19 typedef std::vector<std::pair<int, int>> FunctionOffsets;
20 typedef Result<FunctionOffsets> FunctionOffsetsResult;
21 typedef std::vector<std::vector<std::pair<int, int>>> AsmJsOffsets;
22 typedef Result<AsmJsOffsets> AsmJsOffsetsResult;
23 
24 // Decodes the bytes of a WASM module between {module_start} and {module_end}.
25 V8_EXPORT_PRIVATE ModuleResult DecodeWasmModule(Isolate* isolate,
26                                                 const byte* module_start,
27                                                 const byte* module_end,
28                                                 bool verify_functions,
29                                                 ModuleOrigin origin);
30 
31 // Exposed for testing. Decodes a single function signature, allocating it
32 // in the given zone. Returns {nullptr} upon failure.
33 V8_EXPORT_PRIVATE FunctionSig* DecodeWasmSignatureForTesting(Zone* zone,
34                                                              const byte* start,
35                                                              const byte* end);
36 
37 // Decodes the bytes of a WASM function between
38 // {function_start} and {function_end}.
39 V8_EXPORT_PRIVATE FunctionResult DecodeWasmFunction(Isolate* isolate,
40                                                     Zone* zone, ModuleEnv* env,
41                                                     const byte* function_start,
42                                                     const byte* function_end);
43 
44 // Extracts the function offset table from the wasm module bytes.
45 // Returns a vector with <offset, length> entries, or failure if the wasm bytes
46 // are detected as invalid. Note that this validation is not complete.
47 FunctionOffsetsResult DecodeWasmFunctionOffsets(const byte* module_start,
48                                                 const byte* module_end);
49 
50 V8_EXPORT_PRIVATE WasmInitExpr DecodeWasmInitExprForTesting(const byte* start,
51                                                             const byte* end);
52 
53 // Extracts the mapping from wasm byte offset to asm.js source position per
54 // function.
55 // Returns a vector of vectors with <byte_offset, source_position> entries, or
56 // failure if the wasm bytes are detected as invalid. Note that this validation
57 // is not complete.
58 AsmJsOffsetsResult DecodeAsmJsOffsets(const byte* module_start,
59                                       const byte* module_end);
60 
61 }  // namespace wasm
62 }  // namespace internal
63 }  // namespace v8
64 
65 #endif  // V8_WASM_MODULE_DECODER_H_
66