v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
module-decoder.h
Go to the documentation of this file.
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#if !V8_ENABLE_WEBASSEMBLY
9#error This header should only be included if WebAssembly is enabled.
10#endif // !V8_ENABLE_WEBASSEMBLY
11
12#include <memory>
13
14#include "src/common/globals.h"
15#include "src/logging/metrics.h"
21
22namespace v8 {
23namespace internal {
24
25class Counters;
26
27namespace wasm {
28
29struct CompilationEnv;
30
31inline bool IsValidSectionCode(uint8_t byte) {
32 // Allow everything within [kUnknownSectionCode, kLastKnownModuleSection].
33 static_assert(kUnknownSectionCode == 0);
34 return byte <= kLastKnownModuleSection;
35}
36
38
41using FunctionOffsets = std::vector<std::pair<int, int>>;
43
52 std::vector<AsmJsOffsetEntry> entries;
53};
55 std::vector<AsmJsOffsetFunctionEntries> functions;
56};
58
78
79enum class DecodingMethod {
80 kSync,
81 kAsync,
85};
86
87// Decodes the bytes of a wasm module in {wire_bytes} while recording events and
88// updating counters.
90 WasmEnabledFeatures enabled_features,
91 base::Vector<const uint8_t> wire_bytes, bool validate_functions,
92 ModuleOrigin origin, Counters* counters,
93 std::shared_ptr<metrics::Recorder> metrics_recorder,
94 v8::metrics::Recorder::ContextId context_id, DecodingMethod decoding_method,
95 WasmDetectedFeatures* detected_features);
96// Decodes the bytes of a wasm module in {wire_bytes} without recording events
97// or updating counters.
99 WasmEnabledFeatures enabled_features,
100 base::Vector<const uint8_t> wire_bytes, bool validate_functions,
101 ModuleOrigin origin, WasmDetectedFeatures* detected_features);
102// Stripped down version for disassembler needs.
104 base::Vector<const uint8_t> wire_bytes, ITracer* tracer);
105
106// Exposed for testing. Decodes a single function signature, allocating it
107// in the given zone.
108V8_EXPORT_PRIVATE Result<const FunctionSig*> DecodeWasmSignatureForTesting(
109 WasmEnabledFeatures enabled_features, Zone* zone,
111
112// Decodes the bytes of a wasm function in {function_bytes} (part of
113// {wire_bytes}).
115 WasmEnabledFeatures enabled, Zone* zone, ModuleWireBytes wire_bytes,
116 const WasmModule* module, base::Vector<const uint8_t> function_bytes);
117
119 WasmEnabledFeatures enabled_features, base::Vector<const uint8_t> bytes,
120 ValueType expected);
121
127
128V8_EXPORT_PRIVATE std::vector<CustomSectionOffset> DecodeCustomSections(
129 base::Vector<const uint8_t> wire_bytes);
130
131// Extracts the mapping from wasm byte offset to asm.js source position per
132// function.
134 base::Vector<const uint8_t> encoded_offsets);
135
136// Decode the function names from the name section. Returns the result as an
137// unordered map. Only names with valid utf8 encoding are stored and conflicts
138// are resolved by choosing the last name read.
140 NameMap& names);
141// Decode the type names from the type section, store them in the provided
142// vector/map indexed by *canonical* index.
143// The vector {typenames} must have sufficient size.
144// Existing non-empty names won't be overwritten.
145// The number of allocated characters will be added to {total_allocated_size}.
147 base::Vector<const uint8_t> wire_bytes, const WasmModule* module,
148 std::vector<base::OwnedVector<char>>& typenames,
149 std::map<uint32_t, std::vector<base::OwnedVector<char>>>& fieldnames,
150 size_t* total_allocated_size);
151
152// Validate specific functions in the module. Return the first validation error
153// (deterministically), or an empty {WasmError} if all validated functions are
154// valid. {filter} determines which functions are validated. Pass an empty
155// function for "all functions". The {filter} callback needs to be thread-safe.
157 const WasmModule*, WasmEnabledFeatures enabled_features,
158 base::Vector<const uint8_t> wire_bytes, std::function<bool(int)> filter,
159 WasmDetectedFeatures* detected_features);
160
162 int func_index, const WasmModule* module,
163 WasmError error);
164
166
168 public:
169 explicit ModuleDecoder(WasmEnabledFeatures enabled_features,
170 WasmDetectedFeatures* detected_features);
172
174
175 void DecodeSection(SectionCode section_code,
176 base::Vector<const uint8_t> bytes, uint32_t offset);
177
178 void StartCodeSection(WireBytesRef section_bytes);
179
180 bool CheckFunctionsCount(uint32_t functions_count, uint32_t error_offset);
181
182 void DecodeFunctionBody(uint32_t index, uint32_t size, uint32_t offset);
183
185
186 const std::shared_ptr<WasmModule>& shared_module() const;
187
188 WasmModule* module() const { return shared_module().get(); }
189
190 bool ok() const;
191
192 // Translates the unknown section that decoder is pointing to to an extended
193 // SectionCode if the unknown section is known to decoder.
194 // The decoder is expected to point after the section length and just before
195 // the identifier string of the unknown section.
196 // The return value is the number of bytes that were consumed.
197 static size_t IdentifyUnknownSection(ModuleDecoder* decoder,
199 uint32_t offset, SectionCode* result);
200
201 private:
202 std::unique_ptr<ModuleDecoderImpl> impl_;
203};
204
205} // namespace wasm
206} // namespace internal
207} // namespace v8
208
209#endif // V8_WASM_MODULE_DECODER_H_
DecodedNameSection(base::Vector< const uint8_t > wire_bytes, WireBytesRef name_section)
void DecodeSection(SectionCode section_code, base::Vector< const uint8_t > bytes, uint32_t offset)
void StartCodeSection(WireBytesRef section_bytes)
const std::shared_ptr< WasmModule > & shared_module() const
ModuleDecoder(WasmEnabledFeatures enabled_features, WasmDetectedFeatures *detected_features)
void DecodeModuleHeader(base::Vector< const uint8_t > bytes)
static size_t IdentifyUnknownSection(ModuleDecoder *decoder, base::Vector< const uint8_t > bytes, uint32_t offset, SectionCode *result)
std::unique_ptr< ModuleDecoderImpl > impl_
void DecodeFunctionBody(uint32_t index, uint32_t size, uint32_t offset)
bool CheckFunctionsCount(uint32_t functions_count, uint32_t error_offset)
int32_t offset
ZoneVector< RpoNumber > & result
std::vector< std::pair< int, int > > FunctionOffsets
const char * SectionName(SectionCode code)
void DecodeFunctionNames(base::Vector< const uint8_t > wire_bytes, NameMap &names)
void DecodeCanonicalTypeNames(base::Vector< const uint8_t > wire_bytes, const WasmModule *module, std::vector< base::OwnedVector< char > > &typenames, std::map< uint32_t, std::vector< base::OwnedVector< char > > > &fieldnames, size_t *total_allocated_size)
ModuleResult DecodeWasmModuleForDisassembler(base::Vector< const uint8_t > wire_bytes, ITracer *tracer)
AsmJsOffsetsResult DecodeAsmJsOffsets(base::Vector< const uint8_t > encoded_offsets)
bool IsValidSectionCode(uint8_t byte)
ModuleResult DecodeWasmModule(WasmEnabledFeatures enabled_features, base::Vector< const uint8_t > wire_bytes, bool validate_functions, ModuleOrigin origin, Counters *counters, std::shared_ptr< metrics::Recorder > metrics_recorder, v8::metrics::Recorder::ContextId context_id, DecodingMethod decoding_method, WasmDetectedFeatures *detected_features)
Result< const FunctionSig * > DecodeWasmSignatureForTesting(WasmEnabledFeatures enabled_features, Zone *zone, base::Vector< const uint8_t > bytes)
WasmError ValidateFunctions(const WasmModule *module, WasmEnabledFeatures enabled_features, base::Vector< const uint8_t > wire_bytes, std::function< bool(int)> filter, WasmDetectedFeatures *detected_features_out)
Result< std::unique_ptr< WasmFunction > > FunctionResult
std::vector< CustomSectionOffset > DecodeCustomSections(base::Vector< const uint8_t > bytes)
ConstantExpression DecodeWasmInitExprForTesting(WasmEnabledFeatures enabled_features, base::Vector< const uint8_t > bytes, ValueType expected)
Result< std::shared_ptr< WasmModule > > ModuleResult
FunctionResult DecodeWasmFunctionForTesting(WasmEnabledFeatures enabled_features, Zone *zone, ModuleWireBytes wire_bytes, const WasmModule *module, base::Vector< const uint8_t > function_bytes)
WasmError GetWasmErrorWithName(base::Vector< const uint8_t > wire_bytes, int func_index, const WasmModule *module, WasmError error)
Definition c-api.cc:87
#define V8_EXPORT_PRIVATE
Definition macros.h:460
std::vector< AsmJsOffsetEntry > entries
std::vector< AsmJsOffsetFunctionEntries > functions