forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHBC.cpp
More file actions
474 lines (388 loc) · 15.9 KB
/
HBC.cpp
File metadata and controls
474 lines (388 loc) · 15.9 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
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
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "llvh/Support/raw_ostream.h"
#include "hermes/BCGen/HBC/BytecodeDataProvider.h"
#include "hermes/BCGen/HBC/BytecodeDisassembler.h"
#include "hermes/BCGen/HBC/BytecodeGenerator.h"
#include "hermes/BCGen/HBC/BytecodeStream.h"
#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/BCGen/HBC/Passes.h"
#include "hermes/BCGen/HBC/UniquingStringLiteralTable.h"
#include "hermes/IR/IR.h"
#include "hermes/IR/IRBuilder.h"
#include "hermes/Public/Buffer.h"
#include "hermes/SourceMap/SourceMapGenerator.h"
#include "TestHelpers.h"
#include "gtest/gtest.h"
#include <initializer_list>
#define DEBUG_TYPE "hbc-unittests"
using namespace hermes;
using namespace hermes::hbc;
namespace {
class StringBuffer : public Buffer {
public:
StringBuffer(std::string buffer) : string_(std::move(buffer)) {
data_ = reinterpret_cast<const uint8_t *>(string_.c_str());
size_ = string_.size();
}
private:
std::string string_;
};
/// Create a string table appropriate (i.e. that contains all the necessary
/// strings) for use with these tests. Additionally adds the strings in
/// \p strs if the test requires extra
StringLiteralTable stringsForTest(
std::initializer_list<llvh::StringRef> strs = {}) {
UniquingStringLiteralAccumulator strings;
strings.addString("global", /* isIdentifier */ false);
for (auto &str : strs) {
strings.addString(str, /* isIdentifier */ false);
}
return UniquingStringLiteralAccumulator::toTable(std::move(strings));
}
TEST(HBCBytecodeGen, IntegrationTest) {
std::string Result;
llvh::raw_string_ostream OS(Result);
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder Builder(&M);
Ctx->setDebugInfoSetting(DebugInfoSetting::ALL);
BytecodeModuleGenerator BMG;
BMG.initializeStringTable(stringsForTest({"f1"}));
BMG.addFilename("main.js");
Function *globalFunction = Builder.createTopLevelFunction({});
auto BFG1 = BytecodeFunctionGenerator::create(BMG, 3);
BFG1->emitMov(1, 2);
BMG.setEntryPointIndex(BMG.addFunction(globalFunction));
BMG.setFunctionGenerator(globalFunction, std::move(BFG1));
Function *f1 =
Builder.createFunction("f1", Function::DefinitionKind::ES5Function, true);
auto BFG2 = BytecodeFunctionGenerator::create(BMG, 10);
BFG2->setSourceLocation(DebugSourceLocation(0, 0, 1, 1, 0));
const DebugSourceLocation debugSourceLoc(0, 1, 20, 300, 0);
BFG2->addDebugSourceLocation(debugSourceLoc);
BFG2->emitCall(9, 8, 7);
BMG.addFunction(f1);
BMG.setFunctionGenerator(f1, std::move(BFG2));
BytecodeSerializer BS{OS};
std::shared_ptr<BytecodeModule> BM = BMG.generate();
BS.serialize(*BM, SHA1{});
int globalFunctionIndex = BM->getGlobalFunctionIndex();
int globalFunctionOffsetExpected =
BM->getFunction(globalFunctionIndex).getOffset();
EXPECT_GE(globalFunctionOffsetExpected, 0);
auto bytecode = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<StringBuffer>(OS.str()))
.first;
int functionCnt = bytecode->getFunctionCount();
EXPECT_EQ(functionCnt, 2);
globalFunctionIndex = bytecode->getGlobalFunctionIndex();
EXPECT_TRUE(globalFunctionIndex == 0 || globalFunctionIndex == 1);
int globalFunctionOffsetActual =
bytecode->getFunctionHeader(globalFunctionIndex).offset();
EXPECT_EQ(globalFunctionOffsetExpected, globalFunctionOffsetActual);
// We permit globalFunctionIndex to be 0 or 1; f1 must be the other value.
int f1Index = 1 - globalFunctionIndex;
const BytecodeFunction &oldF1 = BM->getFunction(f1Index);
EXPECT_EQ(
oldF1.getDebugOffsets()->sourceLocations,
bytecode->getDebugOffsets(f1Index)->sourceLocations);
EXPECT_EQ(
oldF1.getDebugOffsets()->lexicalData,
bytecode->getDebugOffsets(f1Index)->lexicalData);
auto optionalSourceLoc = bytecode->getDebugInfo()->getLocationForAddress(
bytecode->getDebugOffsets(f1Index)->sourceLocations, 0);
EXPECT_TRUE(optionalSourceLoc.hasValue());
EXPECT_EQ(*optionalSourceLoc, debugSourceLoc);
// Verify basic properties of the source map.
// We have two functions. Each function has one segment per debug location.
SourceMapGenerator sourceMap;
sourceMap.addSource("main.js");
BM->populateSourceMap(&sourceMap);
const auto &mappings = sourceMap.getMappingsLines();
EXPECT_EQ(mappings.size(), 1u);
EXPECT_EQ(mappings[0].size(), 2u);
}
TEST(HBCBytecodeGen, StripDebugInfo) {
// Test that stripping debug info is successful.
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder Builder(&M);
Ctx->setDebugInfoSetting(DebugInfoSetting::ALL);
BytecodeModuleGenerator BMG;
BMG.initializeStringTable(stringsForTest());
Function *globalFunction = Builder.createTopLevelFunction({});
auto BFG1 = BytecodeFunctionGenerator::create(BMG, 3);
BFG1->addDebugSourceLocation(DebugSourceLocation{0, 1, 20, 300, 0});
BFG1->emitMov(1, 2);
BMG.setEntryPointIndex(BMG.addFunction(globalFunction));
BMG.setFunctionGenerator(globalFunction, std::move(BFG1));
std::shared_ptr<BytecodeModule> BM = BMG.generate();
BytecodeGenerationOptions opts = BytecodeGenerationOptions::defaults();
std::string unstrippedStorage;
llvh::raw_string_ostream unstrippedOS(unstrippedStorage);
opts.stripDebugInfoSection = false;
BytecodeSerializer{unstrippedOS, opts}.serialize(*BM, SHA1{});
std::string strippedStorage;
llvh::raw_string_ostream strippedOS(strippedStorage);
opts.stripDebugInfoSection = true;
BytecodeSerializer{strippedOS, opts}.serialize(*BM, SHA1{});
// Stripping should reduce the size.
EXPECT_LT(strippedOS.str().size(), unstrippedOS.str().size());
// Verify debug info absent from decoded BM.
std::unique_ptr<hbc::BCProvider> strippedBC =
hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<StringBuffer>(strippedOS.str()))
.first;
ASSERT_EQ(strippedBC->getFunctionCount(), BM->getNumFunctions());
for (uint32_t i = 0, max = BM->getNumFunctions(); i < max; i++) {
EXPECT_TRUE(BM->getFunction(i).hasDebugInfo());
EXPECT_FALSE(strippedBC->getFunctionHeader(i).flags().hasDebugInfo);
}
}
TEST(HBCBytecodeGen, StringTableTest) {
std::string Result;
llvh::raw_string_ostream OS(Result);
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder Builder(&M);
BytecodeModuleGenerator BMG;
BMG.initializeStringTable(
stringsForTest({"foo", "bar", /* Ā */ "\xc4\x80", /* å */ "\xc3\xa5"}));
Function *F = Builder.createTopLevelFunction(true);
auto BFG = BytecodeFunctionGenerator::create(BMG, 2);
auto fooIdx1 = BFG->getStringID(Builder.getLiteralString("foo"));
auto barIdx1 = BFG->getStringID(Builder.getLiteralString("bar"));
auto fooIdx2 = BFG->getStringID(Builder.getLiteralString("foo"));
auto barIdx2 = BFG->getStringID(Builder.getLiteralString("bar"));
auto unicodeIdx1 =
BFG->getStringID(Builder.getLiteralString("\xc4\x80")); // Ā
auto unicodeIdx2 =
BFG->getStringID(Builder.getLiteralString("\xc3\xa5")); // å
BFG->emitLoadConstString(1, fooIdx1);
BFG->emitLoadConstString(1, barIdx1);
BFG->emitLoadConstString(1, fooIdx2);
BFG->emitLoadConstString(1, barIdx2);
BFG->emitLoadConstString(1, unicodeIdx1);
BFG->emitLoadConstString(1, unicodeIdx2);
BFG->bytecodeGenerationComplete();
BMG.setEntryPointIndex(BMG.addFunction(F));
BMG.setFunctionGenerator(F, std::move(BFG));
std::shared_ptr<BytecodeModule> BM = BMG.generate();
// 4 strings + function name.
EXPECT_EQ(BM->getStringTableSize(), 5u);
// function name "global" + 'foobar' + 2 chars per unicode char
EXPECT_EQ(BM->getStringStorageSize(), 16u);
Result.clear();
BytecodeSerializer BS{OS};
BS.serialize(*BM, SHA1{});
auto bytecode = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<StringBuffer>(OS.str()))
.first;
EXPECT_EQ(bytecode->getStringCount(), 5u);
EXPECT_EQ(bytecode->getStringStorage().size(), 16u);
// bar
EXPECT_EQ(bytecode->getStringTableEntry(0).getOffset(), 0u);
EXPECT_EQ(bytecode->getStringTableEntry(0).getLength(), 3u);
EXPECT_FALSE(bytecode->getStringTableEntry(0).isUTF16());
// foo
EXPECT_EQ(bytecode->getStringTableEntry(1).getOffset(), 3u);
EXPECT_EQ(bytecode->getStringTableEntry(1).getLength(), 3u);
EXPECT_FALSE(bytecode->getStringTableEntry(1).isUTF16());
// global
EXPECT_EQ(bytecode->getStringTableEntry(2).getOffset(), 6u);
EXPECT_EQ(bytecode->getStringTableEntry(2).getLength(), 6u);
EXPECT_FALSE(bytecode->getStringTableEntry(2).isUTF16());
// UTF16
EXPECT_EQ(bytecode->getStringTableEntry(3).getOffset(), 12u);
EXPECT_EQ(bytecode->getStringTableEntry(3).getLength(), 1u);
EXPECT_TRUE(bytecode->getStringTableEntry(3).isUTF16());
// UTF16
EXPECT_EQ(bytecode->getStringTableEntry(4).getOffset(), 14u);
EXPECT_EQ(bytecode->getStringTableEntry(4).getLength(), 1u);
EXPECT_TRUE(bytecode->getStringTableEntry(4).isUTF16());
}
TEST(HBCBytecodeGen, ExceptionTableTest) {
std::string Result;
llvh::raw_string_ostream OS(Result);
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder Builder(&M);
BytecodeModuleGenerator BMG;
BMG.initializeStringTable(stringsForTest());
Function *F = Builder.createTopLevelFunction(true);
auto BFG = BytecodeFunctionGenerator::create(BMG, 3);
BFG->emitMov(1, 2);
BFG->addExceptionHandler(HBCExceptionHandlerInfo{0, 10, 100});
BFG->addExceptionHandler(HBCExceptionHandlerInfo{0, 20, 200});
BFG->addExceptionHandler(HBCExceptionHandlerInfo{50, 60, 300});
BMG.setEntryPointIndex(BMG.addFunction(F));
BMG.setFunctionGenerator(F, std::move(BFG));
std::shared_ptr<BytecodeModule> BM = BMG.generate();
auto &BF = BM->getGlobalCode();
ASSERT_EQ(BF.getExceptionHandlerCount(), 3u);
BytecodeSerializer BS{OS};
BS.serialize(*BM, SHA1{});
auto bytecode = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<StringBuffer>(OS.str()))
.first;
ASSERT_EQ(bytecode->getExceptionTable(0).size(), 3u);
EXPECT_EQ(bytecode->findCatchTargetOffset(0, 5), 100);
EXPECT_EQ(bytecode->findCatchTargetOffset(0, 15), 200);
EXPECT_EQ(bytecode->findCatchTargetOffset(0, 25), -1);
EXPECT_EQ(bytecode->findCatchTargetOffset(0, 55), 300);
}
TEST(HBCBytecodeGen, ArrayBufferTest) {
// Since deserialization of the array buffer now requires a codeblock,
// the only thing that can be checked at BCGen time is that it uses
// the proper number of bytes for the serialization format.
std::string Result;
llvh::raw_string_ostream OS(Result);
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder Builder(&M);
BytecodeModuleGenerator BMG;
BMG.initializeStringTable(stringsForTest({"abc"}));
Function *F = Builder.createTopLevelFunction(true);
auto BFG = BytecodeFunctionGenerator::create(BMG, 3);
BFG->emitMov(1, 2);
std::vector<Literal *> arr1{
Builder.getLiteralNumber(1),
Builder.getLiteralBool(true),
Builder.getLiteralBool(false),
Builder.getLiteralNull(),
Builder.getLiteralNull(),
Builder.getLiteralString("abc"),
};
BMG.addArrayBuffer(llvh::ArrayRef<Literal *>{arr1});
BMG.setEntryPointIndex(BMG.addFunction(F));
BMG.setFunctionGenerator(F, std::move(BFG));
std::shared_ptr<BytecodeModule> BM = BMG.generate();
// auto &BF = BM->getGlobalCode();
ASSERT_EQ(BM->getArrayBufferSize(), 10u);
BytecodeSerializer BS{OS};
BS.serialize(*BM, SHA1{});
auto bytecode = hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<StringBuffer>(OS.str()))
.first;
ASSERT_EQ(bytecode->getArrayBuffer().size(), 10u);
}
TEST(SpillRegisterTest, SpillsParameters) {
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder builder(&M);
auto *F = builder.createTopLevelFunction(true);
auto *BB = builder.createBasicBlock(F);
builder.setInsertionBlock(BB);
auto *undef = builder.getLiteralUndefined();
// Create a 200 LoadConstInsts, requiring 200 registers
std::vector<Value *> values;
for (int i = 0; i < 200; i++) {
values.push_back(builder.createHBCLoadConstInst(undef));
}
// Use them in a call to require 200 parameter registers.
builder.createCallInst(undef, undef, values);
builder.createReturnInst(undef);
HVMRegisterAllocator RA(F);
PostOrderAnalysis PO(F);
llvh::SmallVector<BasicBlock *, 16> order(PO.rbegin(), PO.rend());
RA.allocate(order);
PassManager PM;
PM.addPass(new LowerCalls(RA));
// Due to Mov elimination, many LoadConstInsts will be reallocated
PM.addPass(new MovElimination(RA));
PM.addPass(new SpillRegisters(RA));
PM.run(F);
// Ensure that spilling takes care of that
for (auto &inst : *BB) {
auto *load = llvh::dyn_cast<HBCLoadConstInst>(&inst);
if (!load)
continue;
EXPECT_LT(RA.getRegister(load).getIndex(), 256u);
}
}
TEST(SpillRegisterTest, NoStoreUnspilling) {
auto Ctx = std::make_shared<Context>();
Module M(Ctx);
IRBuilder builder(&M);
auto *F = builder.createTopLevelFunction(true);
auto *BB = builder.createBasicBlock(F);
builder.setInsertionBlock(BB);
// Create a simple store
auto *store = builder.createStorePropertyInst(
builder.getLiteralUndefined(),
builder.getLiteralUndefined(),
builder.getLiteralUndefined());
builder.createReturnInst(builder.getLiteralUndefined());
// Allocate that store to a high register
HVMRegisterAllocator RA(F);
PostOrderAnalysis PO(F);
llvh::SmallVector<BasicBlock *, 16> order(PO.rbegin(), PO.rend());
RA.allocate(order);
RA.allocateParameterCount(256);
RA.updateRegister(store, Register(256));
// Ensure that spilling doesn't insert any additional instructions
unsigned sizeBefore = BB->size();
PassManager PM;
PM.addPass(new SpillRegisters(RA));
PM.run(F);
EXPECT_EQ(sizeBefore, BB->size());
}
TEST(HBCBytecodeGen, BytecodeFields) {
std::string error;
auto bytecode = bytecodeForSource("print('Hello World');");
ConstBytecodeFileFields fields;
bool populated = fields.populateFromBuffer(bytecode, &error);
ASSERT_TRUE(populated);
ASSERT_TRUE(error.empty());
EXPECT_EQ(fields.header->magic, hbc::MAGIC);
ASSERT_EQ(fields.functionHeaders.size(), 1); // global function
// Three functions: print, 'Hello World', file name
ASSERT_EQ(fields.stringTableEntries.size(), 3);
ASSERT_TRUE(fields.arrayBuffer.empty());
ASSERT_TRUE(fields.objValueBuffer.empty());
ASSERT_TRUE(fields.regExpTable.empty());
ASSERT_TRUE(fields.regExpStorage.empty());
}
TEST(HBCBytecodeGen, BytecodeFieldsFail) {
std::string error;
std::vector<uint8_t> bytecode = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
ConstBytecodeFileFields fields;
bool populated = fields.populateFromBuffer(bytecode, &error);
EXPECT_FALSE(populated);
EXPECT_FALSE(error.empty());
}
TEST(HBCBytecodeGen, SerializeBytecodeOptions) {
TestCompileFlags flags;
flags.staticBuiltins = false;
auto bytecodeVecDefault = bytecodeForSource("print('hello world');", flags);
flags.staticBuiltins = true;
auto bytecodeVecStaticBuiltins =
bytecodeForSource("print('hello world');", flags);
class VectorBuffer : public Buffer {
public:
VectorBuffer(std::vector<uint8_t> buffer) : vec_(std::move(buffer)) {
data_ = reinterpret_cast<const uint8_t *>(vec_.data());
size_ = vec_.size();
}
private:
std::vector<uint8_t> vec_;
};
auto bytecodeDefault =
hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<VectorBuffer>(bytecodeVecDefault))
.first;
auto bytecodeStaticBuiltins =
hbc::BCProviderFromBuffer::createBCProviderFromBuffer(
llvh::make_unique<VectorBuffer>(bytecodeVecStaticBuiltins))
.first;
ASSERT_TRUE(bytecodeDefault);
ASSERT_TRUE(bytecodeStaticBuiltins);
EXPECT_FALSE(bytecodeDefault->getBytecodeOptions().staticBuiltins);
EXPECT_TRUE(bytecodeStaticBuiltins->getBytecodeOptions().staticBuiltins);
}
} // end anonymous namespace