1 // Copyright 2019 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_WASM_MODULE_SOURCEMAP_H_ 6 #define V8_WASM_WASM_MODULE_SOURCEMAP_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "include/v8.h" 12 #include "src/base/macros.h" 13 14 namespace v8 { 15 namespace internal { 16 namespace wasm { 17 // The class is for decoding and managing source map generated by a WebAssembly 18 // toolchain (e.g. Emscripten). This implementation mostly complies with the 19 // specification (https://sourcemaps.info/spec.html), with the following 20 // accommodations: 21 // 1. "names" field is an empty array in current source maps of Wasm, hence it 22 // is not handled; 23 // 2. The semicolons divides "mappings" field into groups, each of which 24 // represents a line in the generated code. As *.wasm is in binary format, there 25 // is one "line" of generated code, and ";" is treated as illegal symbol in 26 // "mappings". 27 // 3. Though each comma-separated section may contains 1, 4 or 5 fields, we only 28 // consider "mappings" with 4 fields, i.e. start line of generated code, index 29 // into "sources" fields, start line of source code and start column of source 30 // code. 31 class V8_EXPORT_PRIVATE WasmModuleSourceMap { 32 public: 33 WasmModuleSourceMap(v8::Isolate* v8_isolate, 34 v8::Local<v8::String> src_map_str); 35 36 // Member valid_ is true only if the source map complies with specification 37 // and can be correctly decoded. IsValid()38 bool IsValid() const { return valid_; } 39 40 // Given a function located at [start, end) in Wasm Module, this function 41 // checks if this function has its corresponding source code. 42 bool HasSource(size_t start, size_t end) const; 43 44 // Given a function's base address start and an address addr within, this 45 // function checks if the address can be mapped to an offset in this function. 46 // For example, we have the following memory layout for Wasm functions, foo 47 // and bar, and O1, O2, O3 and O4 are the decoded offsets of source map: 48 // 49 // O1 --- O2 ----- O3 ----- O4 50 // --->|<-foo->|<--bar->|<----- 51 // --------------A------------- 52 // 53 // Address A of function bar should be mapped to its nearest lower offset, O2. 54 // However, O2 is an address of function foo, thus, this mapping is treated as 55 // invalid. 56 bool HasValidEntry(size_t start, size_t addr) const; 57 58 // This function is responsible for looking up an offset's corresponding line 59 // number in source file. It should only be called when current function is 60 // checked with IsValid, HasSource and HasValidEntry. 61 size_t GetSourceLine(size_t wasm_offset) const; 62 63 // This function is responsible for looking up an offset's corresponding 64 // source file name. It should only be called when current function is checked 65 // with IsValid, HasSource and HasValidEntry. 66 std::string GetFilename(size_t wasm_offset) const; 67 68 private: 69 std::vector<size_t> offsets; 70 std::vector<std::string> filenames; 71 std::vector<size_t> file_idxs; 72 std::vector<size_t> source_row; 73 // As column number in source file is always 0 in source map generated by 74 // WebAssembly toolchain, we will not store this value. 75 76 bool valid_ = false; 77 78 bool DecodeMapping(const std::string& s); 79 }; 80 } // namespace wasm 81 } // namespace internal 82 } // namespace v8 83 #endif // V8_WASM_WASM_MODULE_SOURCEMAP_H_ 84