-
Notifications
You must be signed in to change notification settings - Fork 352
/
Copy pathwebassembly.cc
498 lines (423 loc) · 13.8 KB
/
webassembly.cc
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// Copyright 2018 Google Inc. All Rights Reserved.
//
// 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 "bloaty.h"
#include "source_map.h"
#include "util.h"
#include "absl/strings/substitute.h"
using absl::string_view;
namespace bloaty {
namespace wasm {
uint64_t ReadLEB128Internal(bool is_signed, size_t size, string_view* data) {
uint64_t ret = 0;
int shift = 0;
int maxshift = 70;
const char* ptr = data->data();
const char* limit = ptr + data->size();
while (ptr < limit && shift < maxshift) {
char byte = *(ptr++);
ret |= static_cast<uint64_t>(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0) {
data->remove_prefix(ptr - data->data());
if (is_signed && shift < size && (byte & 0x40)) {
ret |= -(1ULL << shift);
}
return ret;
}
}
THROW("corrupt wasm data, unterminated LEB128");
}
bool ReadVarUInt1(string_view* data) {
return static_cast<bool>(ReadLEB128Internal(false, 1, data));
}
uint8_t ReadVarUInt7(string_view* data) {
return static_cast<char>(ReadLEB128Internal(false, 7, data));
}
uint32_t ReadVarUInt32(string_view* data) {
return static_cast<uint32_t>(ReadLEB128Internal(false, 32, data));
}
uint64_t ReadVarUInt64(string_view* data) {
return static_cast<uint64_t>(ReadLEB128Internal(false, 64, data));
}
int8_t ReadVarint7(string_view* data) {
return static_cast<int8_t>(ReadLEB128Internal(true, 7, data));
}
string_view ReadPiece(size_t bytes, string_view* data) {
if(data->size() < bytes) {
THROW("premature EOF reading variable-length DWARF data");
}
string_view ret = data->substr(0, bytes);
data->remove_prefix(bytes);
return ret;
}
bool ReadMagic(string_view* data) {
const uint32_t wasm_magic = 0x6d736100;
auto magic = ReadFixed<uint32_t>(data);
if (magic != wasm_magic) {
return false;
}
// TODO(haberman): do we need to fail if this is >1?
auto version = ReadFixed<uint32_t>(data);
(void)version;
return true;
}
class Section {
public:
uint32_t id;
std::string name;
string_view data;
string_view contents;
static Section Read(string_view* data_param) {
Section ret;
string_view data = *data_param;
string_view section_data = data;
ret.id = ReadVarUInt7(&data);
uint32_t size = ReadVarUInt32(&data);
ret.contents = ReadPiece(size, &data);
size_t header_size = ret.contents.data() - section_data.data();
ret.data = ReadPiece(size + header_size, §ion_data);
if (ret.id == 0) {
uint32_t name_len = ReadVarUInt32(&ret.contents);
ret.name = std::string(ReadPiece(name_len, &ret.contents));
} else if (ret.id <= 13) {
ret.name = names[ret.id];
} else {
THROWF("Unknown section id: $0", ret.id);
}
*data_param = data;
return ret;
}
enum Name {
kType = 1,
kImport = 2,
kFunction = 3,
kTable = 4,
kMemory = 5,
kGlobal = 6,
kExport = 7,
kStart = 8,
kElement = 9,
kCode = 10,
kData = 11,
kDataCount = 12,
kEvent = 13,
};
static const char* names[];
};
const char* Section::names[] = {
"<none>", // 0
"Type", // 1
"Import", // 2
"Function", // 3
"Table", // 4
"Memory", // 5
"Global", // 6
"Export", // 7
"Start", // 8
"Element", // 9
"Code", // 10
"Data", // 11
"DataCount", // 12
"Event", // 13
};
struct ExternalKind {
enum Kind {
kFunction = 0,
kTable = 1,
kMemory = 2,
kGlobal = 3,
};
};
template <class Func>
void ForEachSection(string_view file, Func&& section_func) {
string_view data = file;
ReadMagic(&data);
while (!data.empty()) {
Section section = Section::Read(&data);
section_func(section);
}
}
template <class Func>
void FindSection(string_view file, std::string name, Func&& section_func) {
string_view data = file;
ReadMagic(&data);
while (!data.empty()) {
Section section = Section::Read(&data);
if (section.name == name) {
section_func(section);
break;
}
}
}
void ParseSections(RangeSink* sink) {
ForEachSection(sink->input_file().data(), [sink](const Section& section) {
sink->AddFileRange("wasm_sections", section.name, section.data);
});
}
typedef std::unordered_map<int, std::string> IndexedNames;
void ReadNames(const Section& section, IndexedNames* func_names,
IndexedNames* dataseg_names, RangeSink* sink) {
enum class NameType {
kModule = 0,
kFunction = 1,
kLocal = 2,
kLabel = 3,
kType = 4,
kTable = 5,
kMemory = 6,
kGlobal = 7,
kElemSegment = 8,
kDataSegment = 9
};
string_view data = section.contents;
while (!data.empty()) {
NameType type = static_cast<NameType>(ReadVarUInt7(&data));
uint32_t size = ReadVarUInt32(&data);
string_view section = ReadPiece(size, &data);
if (type == NameType::kFunction || type == NameType::kDataSegment) {
uint32_t count = ReadVarUInt32(§ion);
for (uint32_t i = 0; i < count; i++) {
string_view entry = section;
uint32_t index = ReadVarUInt32(§ion);
uint32_t name_len = ReadVarUInt32(§ion);
string_view name = ReadPiece(name_len, §ion);
entry = StrictSubstr(entry, 0, name.data() - entry.data() + name.size());
sink->AddFileRange("wasm_funcname", name, entry);
IndexedNames *names = (type == NameType::kFunction ? func_names : dataseg_names);
(*names)[index] = std::string(name);
}
}
}
}
int ReadValueType(string_view* data) {
return ReadVarint7(data);
}
int ReadElemType(string_view* data) {
return ReadVarint7(data);
}
void ReadResizableLimits(string_view* data) {
auto flags = ReadVarUInt1(data);
ReadVarUInt32(data);
if (flags) {
ReadVarUInt32(data);
}
}
void ReadGlobalType(string_view* data) {
ReadValueType(data);
ReadVarUInt1(data);
}
void ReadTableType(string_view* data) {
ReadElemType(data);
ReadResizableLimits(data);
}
void ReadMemoryType(string_view* data) {
ReadResizableLimits(data);
}
uint32_t GetNumFunctionImports(const Section& section) {
assert(section.id == Section::kImport);
string_view data = section.contents;
uint32_t count = ReadVarUInt32(&data);
uint32_t func_count = 0;
for (uint32_t i = 0; i < count; i++) {
uint32_t module_len = ReadVarUInt32(&data);
ReadPiece(module_len, &data);
uint32_t field_len = ReadVarUInt32(&data);
ReadPiece(field_len, &data);
auto kind = ReadFixed<uint8_t>(&data);
switch (kind) {
case ExternalKind::kFunction:
func_count++;
ReadVarUInt32(&data);
break;
case ExternalKind::kTable:
ReadTableType(&data);
break;
case ExternalKind::kMemory:
ReadMemoryType(&data);
break;
case ExternalKind::kGlobal:
ReadGlobalType(&data);
break;
default:
THROWF("Unrecognized import kind: $0", kind);
}
}
return func_count;
}
void ReadCodeSection(const Section& section, const IndexedNames& names,
uint32_t num_imports, RangeSink* sink) {
string_view data = section.contents;
uint32_t count = ReadVarUInt32(&data);
for (uint32_t i = 0; i < count; i++) {
string_view func = data;
uint32_t size = ReadVarUInt32(&data);
uint32_t total_size = size + (data.data() - func.data());
func = StrictSubstr(func, 0, total_size);
data = StrictSubstr(data, size);
auto iter = names.find(num_imports + i);
if (iter == names.end()) {
std::string name = "func[" + std::to_string(i) + "]";
sink->AddFileRange("wasm_function", name, func);
} else {
sink->AddFileRange("wasm_function", ItaniumDemangle(iter->second, sink->data_source()), func);
}
}
}
void SkipInitializerExpression(string_view* data) {
// Read an initializer expression. For now we don't care about the
// offset, only the index (because the name section refers to the index).
// We don't yet support the extended-const proposal, so we only need to
// decode t.const or global.get
uint8_t opcode = ReadFixed<uint8_t>(data);
switch (opcode) {
case 0x23: // global.get
case 0x41: // i32.const
ReadVarUInt32(data);
break;
case 0x42: // i64.const
ReadVarUInt64(data);
break;
case 0x43: // f32.const
ReadFixed<float>(data);
break;
case 0x44: // f64.const
ReadFixed<double>(data);
break;
default:
THROW("Invalid or unsupported opcode in memory initializer expression");
}
uint8_t end = ReadFixed<uint8_t>(data);
if (end != 0x0b) THROW("End opcode not found at end of memory initializer");
}
void ReadDataSection(const Section& section, const IndexedNames& names,
RangeSink* sink) {
string_view data = section.contents;
uint32_t count = ReadVarUInt32(&data);
for (uint32_t i = 0; i < count; i++) {
string_view segment = data;
uint32_t mode = ReadVarUInt32(&data);
if (mode > 1) THROW("multi-memory extension isn't supported");
if (mode == 0) { // Active segment
SkipInitializerExpression(&data);
}
uint32_t segment_size = ReadVarUInt32(&data);
uint32_t total_size = segment_size + (data.data() - segment.data());
segment = StrictSubstr(segment, 0, total_size);
data = StrictSubstr(data, segment_size);
auto iter = names.find(i);
if (iter == names.end()) {
std::string name = "data[" + std::to_string(i) + "]";
sink->AddFileRange("wasm_data", name, segment);
} else {
sink->AddFileRange("wasm_data", iter->second, segment);
}
}
}
void ParseSymbols(RangeSink* sink) {
// First pass: read the custom naming section to get function names.
std::unordered_map<int, std::string> func_names;
std::unordered_map<int, std::string> dataseg_names;
uint32_t num_imports = 0;
ForEachSection(sink->input_file().data(),
[&func_names, &dataseg_names, sink](const Section& section) {
if (section.name == "name") {
ReadNames(section, &func_names, &dataseg_names, sink);
}
});
// Second pass: read the function/code sections.
ForEachSection(sink->input_file().data(),
[&func_names, &dataseg_names, &num_imports, sink](const Section& section) {
if (section.id == Section::kImport) {
num_imports = GetNumFunctionImports(section);
} else if (section.id == Section::kCode) {
ReadCodeSection(section, func_names, num_imports, sink);
} else if (section.id == Section::kData) {
ReadDataSection(section, dataseg_names, sink);
}
});
}
void AddWebAssemblyFallback(RangeSink* sink) {
ForEachSection(sink->input_file().data(), [sink](const Section& section) {
std::string name2 =
std::string("[section ") + std::string(section.name) + std::string("]");
sink->AddFileRange("wasm_overhead", name2, section.data);
});
sink->AddFileRange("wasm_overhead", "[WASM Header]",
StrictSubstr(sink->input_file().data(), 0, 8));
}
class WebAssemblyObjectFile : public ObjectFile {
public:
WebAssemblyObjectFile(std::unique_ptr<InputFile> file_data)
: ObjectFile(std::move(file_data)) {}
std::string GetBuildId() const override {
// Use the sourceMappingURL as the build ID to be able to match to the
// source map.
std::string id;
FindSection(file_data().data(), "sourceMappingURL",
[&id](Section& section) {
uint32_t size = ReadVarUInt32(§ion.contents);
string_view source_mapping_url =
ReadPiece(size, §ion.contents);
id.assign(source_mapping_url);
});
return id;
}
void ProcessFile(const std::vector<RangeSink*>& sinks) const override {
for (auto sink : sinks) {
switch (sink->data_source()) {
case DataSource::kSegments:
case DataSource::kSections:
ParseSections(sink);
break;
case DataSource::kSymbols:
case DataSource::kRawSymbols:
case DataSource::kShortSymbols:
case DataSource::kFullSymbols:
ParseSymbols(sink);
break;
case DataSource::kCompileUnits:
case DataSource::kInlines:
if (const sourcemap::SourceMapObjectFile* source_map =
dynamic_cast<const sourcemap::SourceMapObjectFile*>(
&debug_file())) {
source_map->ProcessFileToSink(sink);
} else {
THROW("Data source requires a source map");
}
break;
case DataSource::kArchiveMembers:
default:
THROW("WebAssembly doesn't support this data source");
}
AddWebAssemblyFallback(sink);
}
}
bool GetDisassemblyInfo(absl::string_view /*symbol*/,
DataSource /*symbol_source*/,
DisassemblyInfo* /*info*/) const override {
WARN("WebAssembly files do not support disassembly yet");
return false;
}
};
} // namespace wasm
std::unique_ptr<ObjectFile> TryOpenWebAssemblyFile(
std::unique_ptr<InputFile>& file) {
string_view data = file->data();
if (wasm::ReadMagic(&data)) {
return std::unique_ptr<ObjectFile>(
new wasm::WebAssemblyObjectFile(std::move(file)));
}
return nullptr;
}
} // namespace bloaty