-
Notifications
You must be signed in to change notification settings - Fork 863
Expand file tree
/
Copy pathsource-map.cpp
More file actions
314 lines (277 loc) · 9.62 KB
/
Copy pathsource-map.cpp
File metadata and controls
314 lines (277 loc) · 9.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright 2024 WebAssembly Community Group participants
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "source-map.h"
#include "ir/module-utils.h"
#include "print-test.h"
#include "wasm-builder.h"
#include "gmock/gmock-matchers.h"
#include "gtest/gtest.h"
using namespace wasm;
class SourceMapTest : public PrintTest {
std::vector<char> buffer;
protected:
Module wasm;
std::unique_ptr<SourceMapReader> reader;
void SetUp() override {
PrintTest::SetUp();
reader.reset();
parseWast(wasm, "(module)");
}
void parseMap(std::string& sourceMap) {
buffer = {sourceMap.begin(), sourceMap.end()};
reader.reset(new SourceMapReader(buffer));
reader->parse(wasm);
}
void ExpectDbgLocEq(size_t location,
BinaryLocation file,
BinaryLocation line,
BinaryLocation col,
std::optional<BinaryLocation> sym) {
auto loc_str = std::stringstream() << "location: " << location;
SCOPED_TRACE(loc_str.str());
auto loc = reader->readDebugLocationAt(location);
ASSERT_TRUE(loc.has_value());
EXPECT_EQ(loc->fileIndex, file);
EXPECT_EQ(loc->lineNumber, line);
EXPECT_EQ(loc->columnNumber, col);
EXPECT_EQ(loc->symbolNameIndex, sym);
}
void ExpectParseError(std::string& mapString, const char* expectedError) {
SCOPED_TRACE(mapString);
EXPECT_THROW(parseMap(mapString), MapParseException);
try {
parseMap(mapString);
} catch (MapParseException ex) {
EXPECT_THAT(ex.errorText, ::testing::HasSubstr(expectedError));
}
}
};
// Check that debug location parsers can handle single-segment mappings.
TEST_F(SourceMapTest, SourceMappingSingleSegment) {
// A single-segment mapping starting at offset 0.
std::string sourceMap = R"(
{
"version": 3,
"sources": [],
"sourcesContent": [],
"names": [],
"mappings": "A"
}
)";
parseMap(sourceMap);
auto loc = reader->readDebugLocationAt(0);
EXPECT_FALSE(loc.has_value());
}
TEST_F(SourceMapTest, BadSourceMaps) {
// Test that a malformed JSON string throws rather than asserting.
std::string sourceMap = R"(
{
"version": 3,
"sources": ["foo.c"],
"mappings": ""
malformed
}
)";
ExpectParseError(sourceMap, "malformed value in JSON object");
// Valid JSON, but missing the version field.
sourceMap = R"(
{
"sources": [],
"names": [],
"mappings": "A"
}
)";
ExpectParseError(sourceMap, "Source map version missing");
// Valid JSON, but a bad "sources" field.
sourceMap = R"(
{
"version": 3,
"sources": 123,
"mappings": ""
}
)";
ExpectParseError(sourceMap, "Source map sources missing or not an array");
sourceMap = R"(
{
"version": 3,
"sources": ["foo.c"],
"mappings": "C;A"
}
)";
parseMap(sourceMap);
// Mapping strings are parsed incrementally, so errors don't show up until a
// sufficiently far-advanced location is requested to reach the problem.
EXPECT_THROW(reader->readDebugLocationAt(1), MapParseException);
// VLQ with too many continuation digits. 7 continuation characters ('g')
// push the shift to 35, exceeding the uint32_t width. This is a malformed
// VLQ that should be rejected rather than causing undefined behavior.
sourceMap = R"(
{
"version": 3,
"sources": ["foo.c"],
"mappings": "gggggggA"
}
)";
ExpectParseError(sourceMap, "VLQ value too large");
}
TEST_F(SourceMapTest, SourcesAndNames) {
std::string sourceMap = R"(
{
"version": 3,
"sources": ["foo.c", "bar.c"],
"names": ["foo", "bar"],
"mappings": ""
}
)";
parseMap(sourceMap);
EXPECT_EQ(wasm.debugInfoFileNames.size(), 2);
EXPECT_EQ(wasm.debugInfoFileNames[0], "foo.c");
EXPECT_EQ(wasm.debugInfoFileNames[1], "bar.c");
EXPECT_EQ(wasm.debugInfoSymbolNames.size(), 2);
EXPECT_EQ(wasm.debugInfoSymbolNames[0], "foo");
EXPECT_EQ(wasm.debugInfoSymbolNames[1], "bar");
}
TEST_F(SourceMapTest, OptionalFields) {
// The "names" field is optional.
std::string sourceMap = R"(
{
"version": 3,
"sources": [],
"mappings": "A"
}
)";
parseMap(sourceMap);
}
// This map is taken from test/fib-dbg.wasm
TEST_F(SourceMapTest, Fibonacci) {
// Test mapping parsing and debug locs
std::string sourceMap = R"(
{
"version":3,
"sources":["fib.c"],
"names":[],
"mappings": "moBAEA,4BAKA,QAJA,OADA,OAAA,uCAKA"
}
)";
parseMap(sourceMap);
// Location before the first record has no value
auto loc = reader->readDebugLocationAt(642);
EXPECT_FALSE(loc.has_value());
// TODO: These column numbers show as 0 but emsymbolizer.py prints them as 1.
// Figure out which is right.
// First location
ExpectDbgLocEq(643, 0, 3, 0, std::nullopt);
// locations in between records have the same value as the previous one.
for (size_t l = 644; l < 671; l++) {
ExpectDbgLocEq(l, 0, 3, 0, std::nullopt);
}
// Subsequent ones are on record boundaries
ExpectDbgLocEq(671, 0, 8, 0, std::nullopt);
ExpectDbgLocEq(679, 0, 4, 0, std::nullopt);
ExpectDbgLocEq(686, 0, 3, 0, std::nullopt);
ExpectDbgLocEq(693, 0, 3, 0, std::nullopt);
ExpectDbgLocEq(732, 0, 8, 0, std::nullopt);
// Entries after the last record have the same value as the last one.
ExpectDbgLocEq(733, 0, 8, 0, std::nullopt);
// Should we return empty values for locations that are past the end of the
// program?
ExpectDbgLocEq(9999, 0, 8, 0, std::nullopt);
}
TEST_F(SourceMapTest, SourceMapSourceRootFile) {
std::string sourceMap = R"(
{
"version":3,
"file": "foo.wasm",
"sources":[],
"names":[],
"mappings": "",
"sourceRoot": "/foo/bar"
}
)";
parseMap(sourceMap);
EXPECT_EQ(wasm.debugInfoSourceRoot, "/foo/bar");
EXPECT_EQ(wasm.debugInfoFile, "foo.wasm");
}
TEST_F(SourceMapTest, SourcesContent) {
// The backslash escapes appear in the JSON encoding, and are preserved in
// the internal representation. The string values are uninterpreted in
// Binaryen, and they are written directly back out without re-encoding.
std::string sourceMap = R"(
{
"version": 3,
"sources": ["foo.c"],
"sourcesContent": ["#include <stdio.h> int main()\n{ printf(\"Gr\u00fc\u00df Gott, Welt!\"); return 0;}"],
"mappings" : ""
}
)";
parseMap(sourceMap);
ASSERT_EQ(wasm.debugInfoSourcesContent.size(), 1);
EXPECT_EQ(wasm.debugInfoSourcesContent[0],
"#include <stdio.h> int main()\\n{ printf(\\\"Gr\\u00fc\\u00df "
"Gott, Welt!\\\"); return 0;}");
}
// Regression test: updateSymbol calls for prologLocation/epilogLocation were
// inside the debugLocations loop instead of outside. When debugLocations is
// empty, the loop never executes and the locations are not updated.
TEST(SourceMapCopyTest, UpdateSymbolOutsideLoop) {
Module srcModule;
Module dstModule;
Builder builder(srcModule);
auto func = builder.makeFunction(
"test", {}, Signature(Type::none, Type::none), {}, builder.makeNop());
// Set prologLocation and epilogLocation with symbolNameIndex values,
// but leave debugLocations empty.
func->prologLocation =
Function::DebugLocation{0, 1, 1, std::optional<BinaryLocation>(0)};
func->epilogLocation =
Function::DebugLocation{0, 2, 1, std::optional<BinaryLocation>(1)};
// Symbol name index map: 0->10, 1->11.
std::vector<Index> symbolNameIndexMap = {10, 11};
auto copied = ModuleUtils::copyFunctionWithoutAdd(
func.get(), dstModule, Name(), std::nullopt, symbolNameIndexMap);
ASSERT_TRUE(copied->prologLocation.has_value());
ASSERT_TRUE(copied->prologLocation->symbolNameIndex.has_value());
EXPECT_EQ(*copied->prologLocation->symbolNameIndex, 10u);
ASSERT_TRUE(copied->epilogLocation.has_value());
ASSERT_TRUE(copied->epilogLocation->symbolNameIndex.has_value());
EXPECT_EQ(*copied->epilogLocation->symbolNameIndex, 11u);
}
// When debugLocations has multiple entries, updateSymbol should only run once
// per location, not once per loop iteration (which would double-remap).
TEST(SourceMapCopyTest, UpdateSymbolNotDoubleRemapped) {
Module srcModule;
Module dstModule;
Builder builder(srcModule);
auto func = builder.makeFunction(
"test", {}, Signature(Type::none, Type::none), {}, builder.makeNop());
func->prologLocation =
Function::DebugLocation{0, 1, 1, std::optional<BinaryLocation>(0)};
// Add multiple debug locations so the loop runs multiple times.
auto* nop1 = srcModule.allocator.alloc<Nop>();
auto* nop2 = srcModule.allocator.alloc<Nop>();
func->debugLocations[nop1] =
Function::DebugLocation{0, 10, 1, std::optional<BinaryLocation>(0)};
func->debugLocations[nop2] =
Function::DebugLocation{0, 11, 1, std::optional<BinaryLocation>(1)};
// Map: 0->5, 1->6.
std::vector<Index> symbolNameIndexMap = {5, 6};
auto copied = ModuleUtils::copyFunctionWithoutAdd(
func.get(), dstModule, Name(), std::nullopt, symbolNameIndexMap);
// Should be remapped exactly once: 0 -> 5.
ASSERT_TRUE(copied->prologLocation.has_value());
ASSERT_TRUE(copied->prologLocation->symbolNameIndex.has_value());
EXPECT_EQ(*copied->prologLocation->symbolNameIndex, 5u);
}