forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreterTest.cpp
More file actions
513 lines (444 loc) · 16 KB
/
Copy pathInterpreterTest.cpp
File metadata and controls
513 lines (444 loc) · 16 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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* 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 "TestHelpers.h"
#include "hermes/BCGen/HBC/BytecodeGenerator.h"
#include "hermes/VM/Callable.h"
#include "hermes/VM/CodeBlock.h"
#include "hermes/VM/Operations.h"
#include "hermes/VM/Runtime.h"
#include "hermes/VM/SmallXString.h"
#include "hermes/VM/StringView.h"
#include "gtest/gtest.h"
#include "llvh/Support/raw_ostream.h"
using namespace hermes::vm;
using namespace hermes::hbc;
/// Associate a label with an instruction. Use it like this:
/// \begincode
/// LABEL(L(1), builder.emitMov(1, 2));
/// \endcode
#define LABEL(L, emit) \
do { \
auto ofs = emit; \
if (pass == 0) \
labels[L] = ofs; \
} while (0)
/// Emit a conditional jump to label. Use it like this:
/// \begincode
/// JCOND(builder.emitJLessEqualN, L(2), 0, 2);
/// \endcode
#define JCOND(name, L, op2, op3) \
do { \
int line = __LINE__; \
if (pass == 0) \
jmps[line] = name(0, op2, op3); \
else \
name(labels[L] - jmps[line], op2, op3); \
} while (0);
/// Emit an unconditional jump to label. Use it like this:
/// \begincode
/// JMP(builder.emitJmp, L(2));
/// \endcode
#define JMP(name, L) \
do { \
int line = __LINE__; \
if (pass == 0) \
jmps[line] = name(0); \
else \
name(labels[L] - jmps[line]); \
} while (0);
/// Make labels more noticeable in the source by always using this macro to
/// refer to them.
#define L(x) x
namespace {
/// Convert all arguments to string and print them followed by new line.
static CallResult<HermesValue>
print(void *, Runtime *runtime, NativeArgs args) {
GCScope scope(runtime);
bool first = true;
for (Handle<> arg : args.handles()) {
auto res = toString_RJS(runtime, arg);
if (res != ExecutionStatus::RETURNED)
return ExecutionStatus::EXCEPTION;
if (!first)
llvh::outs() << " ";
SmallU16String<32> tmp;
llvh::outs() << StringPrimitive::createStringView(
runtime, runtime->makeHandle(std::move(*res)))
.getUTF16Ref(tmp);
first = false;
}
llvh::outs() << "\n";
return HermesValue::encodeUndefinedValue();
}
class InterpreterFunctionTest : public RuntimeTestFixture {
private:
Handle<Domain> domain;
RuntimeModule *runtimeModule;
BytecodeModuleGenerator BMG;
bool hasRun = false;
CallResult<HermesValue> result{ExecutionStatus::EXCEPTION};
protected:
std::unique_ptr<BytecodeFunctionGenerator> BFG;
InterpreterFunctionTest()
: RuntimeTestFixture(),
domain(runtime->makeHandle(Domain::create(runtime))),
runtimeModule(RuntimeModule::createUninitialized(runtime, domain)) {
BFG = BytecodeFunctionGenerator::create(BMG, 1);
}
CallResult<HermesValue> run() {
assert(!hasRun);
BFG->bytecodeGenerationComplete();
auto *codeBlock = createCodeBlock(runtimeModule, runtime, BFG.get());
ScopedNativeCallFrame frame(
runtime,
0,
HermesValue::encodeNativePointer(codeBlock),
HermesValue::encodeUndefinedValue(),
HermesValue::encodeUndefinedValue());
assert(!frame.overflowed());
result = runtime->interpretFunction(codeBlock);
hasRun = true;
return result;
}
bool getResultAsBool() const {
assert(hasRun);
assert(result != ExecutionStatus::EXCEPTION);
return result->getBool();
}
Handle<StringPrimitive> getResultAsString() {
assert(hasRun);
assert(result != ExecutionStatus::EXCEPTION);
return runtime->makeHandle(result->getString());
}
};
using InterpreterTest = RuntimeTestFixture;
TEST_F(InterpreterTest, SimpleSmokeTest) {
auto *runtimeModule = RuntimeModule::createUninitialized(runtime, domain);
/*
; calculate 10 - 2 and print "result =" the value.
load_imm reg0, 10
load_imm reg1, 2
subn reg2, reg0, reg1
get_global reg0
get_named reg1, reg0, "print"
load_imm reg3, #undefined
push reg3
load_string reg3, "result="
push reg3
push reg2
call reg3, reg1, 3
ret reg2
*/
StringID printID = 1;
StringID resultID = 2;
const unsigned FRAME_SIZE = 16;
const unsigned LAST_REG = FRAME_SIZE - 1;
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, FRAME_SIZE);
BFG->emitLoadConstDoubleDirect(0, 10);
BFG->emitLoadConstDoubleDirect(1, 2);
BFG->emitSubN(2, 0, 1);
BFG->emitGetGlobalObject(0);
BFG->emitGetById(1, 0, 1, printID);
BFG->emitLoadConstUndefined(3);
BFG->emitMov(LAST_REG - StackFrameLayout::ThisArg, 3);
BFG->emitLoadConstString(LAST_REG - StackFrameLayout::FirstArg, resultID);
BFG->emitMov(LAST_REG - StackFrameLayout::FirstArg - 1, 2);
BFG->emitCall(3, 1, 3);
BFG->emitRet(2);
BFG->setHighestReadCacheIndex(1);
BFG->setHighestWriteCacheIndex(0);
BFG->bytecodeGenerationComplete();
auto codeBlock = createCodeBlock(runtimeModule, runtime, BFG.get());
ASSERT_EQ(detail::mapStringMayAllocate(*runtimeModule, "print"), printID);
ASSERT_EQ(detail::mapStringMayAllocate(*runtimeModule, "result="), resultID);
auto printFn = runtime->makeHandle<NativeFunction>(
*NativeFunction::createWithoutPrototype(
runtime,
nullptr,
print,
Predefined::getSymbolID(Predefined::emptyString),
0));
// Define the 'print' function.
(void)JSObject::putNamed_RJS(
runtime->getGlobal(),
runtime,
runtimeModule->getSymbolIDFromStringIDMayAllocate(printID),
printFn);
CallResult<HermesValue> status{ExecutionStatus::EXCEPTION};
{
ScopedNativeCallFrame frame(
runtime, 0, nullptr, false, HermesValue::encodeUndefinedValue());
ASSERT_FALSE(frame.overflowed());
status = runtime->interpretFunction(codeBlock);
}
auto frames = runtime->getStackFrames();
ASSERT_TRUE(frames.begin() == frames.end());
ASSERT_EQ(
StackFrameLayout::CalleeExtraRegistersAtStart, runtime->getStackLevel());
ASSERT_EQ(ExecutionStatus::RETURNED, status.getStatus());
ASSERT_EQ(8.0, status.getValue().getDouble());
}
TEST_F(InterpreterTest, IterativeFactorialTest) {
auto runtimeModule = RuntimeModule::createUninitialized(runtime, domain);
/*
get_arg reg0, 1 ; load n
mov reg1, reg0 ; res = n
load_imm reg2, 1 ; constant for reuse
to_number reg0, reg0 ; --n
L1:
subn reg0, reg0, reg2
jlen L2, reg0, reg2 ; if n <= 1 goto L2
mul reg1, reg1, reg0 ; res *= n
jmp L1
L2:
ret reg1 ; return res
*/
std::map<int, int> labels{};
std::map<int, int> jmps{};
auto emit = [&](BytecodeFunctionGenerator &builder, int pass) {
builder.emitLoadParam(0, 1);
builder.emitMov(1, 0);
builder.emitLoadConstDoubleDirect(2, 1);
builder.emitToNumber(0, 0);
LABEL(L(1), builder.emitSubN(0, 0, 2));
JCOND(builder.emitJLessEqualN, L(2), 0, 2);
builder.emitMul(1, 1, 0);
JMP(builder.emitJmp, L(1));
LABEL(L(2), builder.emitRet(1));
};
// Pass 0 - resolve labels.
{
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, 3);
emit(*BFG, 0);
}
// Pass 1 - build the actual code.
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, 3);
emit(*BFG, 1);
BFG->bytecodeGenerationComplete();
auto codeBlock = createCodeBlock(runtimeModule, runtime, BFG.get());
CallResult<HermesValue> status{ExecutionStatus::EXCEPTION};
{
ScopedNativeCallFrame newFrame(
runtime, 1, nullptr, false, HermesValue::encodeUndefinedValue());
ASSERT_FALSE(newFrame.overflowed());
newFrame->getArgRef(0) = HermesValue::encodeDoubleValue(5);
status = runtime->interpretFunction(codeBlock);
}
auto frames = runtime->getStackFrames();
ASSERT_TRUE(frames.begin() == frames.end());
ASSERT_EQ(
StackFrameLayout::CalleeExtraRegistersAtStart, runtime->getStackLevel());
ASSERT_EQ(ExecutionStatus::RETURNED, status.getStatus());
ASSERT_EQ(120.0, status.getValue().getDouble());
}
TEST_F(InterpreterTest, RecursiveFactorialTest) {
auto runtimeModule = RuntimeModule::createUninitialized(runtime, domain);
auto factID = detail::mapStringMayAllocate(*runtimeModule, "fact");
/*
get_arg reg0, 1 ; load n
load_imm reg1, 2 ; load constant 2
jg L1, reg0, reg1 ; if n > 2 goto L1
ret reg0 ; return n
L1:
load_imm reg1, 1 ; load constant 1
sub reg2, reg0, reg1 ; reg2 = n-1
load_imm reg1, #undefined ; fact(n-1)
push reg1
push reg2
get_global reg1
get_named reg1, reg1, "fact"
call reg1, reg1, 2
mul reg0, reg0, reg1 ; return n*fact(n-1)
ret reg0
*/
std::map<int, int> labels{};
std::map<int, int> jmps{};
const unsigned FRAME_SIZE = 16;
const unsigned LAST_REG = FRAME_SIZE - 1;
auto emit = [&](BytecodeFunctionGenerator &builder, int pass) {
builder.emitLoadParam(0, 1);
builder.emitLoadConstDoubleDirect(1, 2);
JCOND(builder.emitJGreater, L(1), 0, 1);
builder.emitRet(0);
LABEL(L(1), builder.emitLoadConstDoubleDirect(1, 1));
builder.emitLoadConstUndefined(LAST_REG - StackFrameLayout::ThisArg);
builder.emitSub(LAST_REG - StackFrameLayout::FirstArg, 0, 1);
builder.emitGetGlobalObject(1);
builder.emitGetById(1, 1, 0, factID);
builder.emitCall(1, 1, 2);
builder.emitMul(0, 0, 1);
builder.emitRet(0);
};
// Pass 0 - resolve labels.
{
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, FRAME_SIZE);
emit(*BFG, 0);
}
// Pass 1 - build the actual code.
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, FRAME_SIZE);
emit(*BFG, 1);
BFG->setHighestReadCacheIndex(255);
BFG->setHighestWriteCacheIndex(255);
BFG->bytecodeGenerationComplete();
auto codeBlock = createCodeBlock(runtimeModule, runtime, BFG.get());
Handle<JSFunction> factFn = runtime->makeHandle(JSFunction::create(
runtime,
runtimeModule->getDomain(runtime),
Handle<JSObject>(runtime),
Handle<Environment>(runtime),
codeBlock));
// Define the 'fact' function.
(void)JSObject::putNamed_RJS(
runtime->getGlobal(),
runtime,
runtimeModule->getSymbolIDFromStringIDMayAllocate(factID),
factFn);
{
CallResult<HermesValue> status{ExecutionStatus::EXCEPTION};
{
ScopedNativeCallFrame newFrame(
runtime, 1, nullptr, false, HermesValue::encodeUndefinedValue());
ASSERT_FALSE(newFrame.overflowed());
newFrame->getArgRef(0) = HermesValue::encodeDoubleValue(2);
status = runtime->interpretFunction(codeBlock);
}
auto frames = runtime->getStackFrames();
ASSERT_TRUE(frames.begin() == frames.end());
ASSERT_EQ(
StackFrameLayout::CalleeExtraRegistersAtStart,
runtime->getStackLevel());
ASSERT_EQ(ExecutionStatus::RETURNED, status.getStatus());
ASSERT_EQ(2.0, status.getValue().getDouble());
}
{
CallResult<HermesValue> status{ExecutionStatus::EXCEPTION};
{
ScopedNativeCallFrame newFrame(
runtime, 1, nullptr, false, HermesValue::encodeUndefinedValue());
ASSERT_FALSE(newFrame.overflowed());
newFrame->getArgRef(0) = HermesValue::encodeDoubleValue(5);
status = runtime->interpretFunction(codeBlock);
}
auto frames = runtime->getStackFrames();
ASSERT_TRUE(frames.begin() == frames.end());
ASSERT_EQ(
StackFrameLayout::CalleeExtraRegistersAtStart,
runtime->getStackLevel());
ASSERT_EQ(ExecutionStatus::RETURNED, status.getStatus());
ASSERT_EQ(120.0, status.getValue().getDouble());
}
}
TEST_F(InterpreterFunctionTest, GetByIdSlowPathChecksForExceptions) {
BFG->emitLoadConstUndefined(0);
BFG->emitGetById(0, 0, 0, 0);
BFG->emitRet(0);
ASSERT_EQ(ExecutionStatus::EXCEPTION, run());
}
TEST_F(InterpreterFunctionTest, PutByIdSlowPathChecksForExceptions) {
BFG->emitLoadConstUndefined(0);
BFG->emitPutById(0, 0, 0, 0);
BFG->emitRet(0);
ASSERT_EQ(ExecutionStatus::EXCEPTION, run());
}
TEST_F(InterpreterFunctionTest, TestNot) {
BFG->emitLoadConstFalse(0);
BFG->emitNot(0, 0);
BFG->emitRet(0);
ASSERT_EQ(ExecutionStatus::RETURNED, run());
EXPECT_TRUE(getResultAsBool());
}
TEST_F(InterpreterFunctionTest, TestToString) {
BFG->emitLoadConstFalse(0);
BFG->emitAddEmptyString(0, 0);
BFG->emitRet(0);
ASSERT_EQ(ExecutionStatus::RETURNED, run());
SmallU16String<8> tmp;
getResultAsString()->appendUTF16String(tmp);
ASSERT_EQ(createUTF16Ref(u"false"), tmp.arrayRef());
}
#if defined(NDEBUG) && !defined(HERMES_UBSAN) && \
!LLVM_THREAD_SANITIZER_BUILD && !LLVM_ADDRESS_SANITIZER_BUILD
// Returns the native stack pointer of the callee frame.
static CallResult<HermesValue>
getSP(void *, Runtime *runtime, NativeArgs args) {
int dummy;
return HermesValue::encodeNativePointer(&dummy);
}
// Use a non-inline function to perform the stack size measurement so that it
// takes place in a new stack frame. This ensures that location of dummy on the
// stack really is right before the stack frame for interpretFunction.
LLVM_ATTRIBUTE_NOINLINE static void testInterpreterStackSize(
Runtime *runtime,
CodeBlock *codeBlock) {
// Check that inner and outer stack pointer differ by at most a set threshold.
int dummy;
const auto outerStackPointer = reinterpret_cast<uintptr_t>(&dummy);
auto status = runtime->interpretFunction(codeBlock);
ASSERT_EQ(ExecutionStatus::RETURNED, status.getStatus());
const auto innerStackPointer =
reinterpret_cast<uintptr_t>(status.getValue().getNativePointer<void>());
// Increase this only if you have a reason to grow the interpreter's frame.
#ifdef _MSC_VER
// TODO(T42117517) Understand why stack frame size is large on Windows
uintptr_t kStackFrameSizeLimit = 3000;
#else
uintptr_t kStackFrameSizeLimit = 1500;
#endif
ASSERT_LE(outerStackPointer - innerStackPointer, kStackFrameSizeLimit);
}
// In release mode, we test the size of the interpreter's stack frame.
// "getSP" is installed as a native function and called from JS. The
// distance from its "inner" frame to the "outer" frame that invoked
// the interpreter is our approximate measure of the stack size used
// by the interpreter. We set a limit that will catch severe
// regressions, e.g., due to compiler quirks.
TEST_F(InterpreterTest, FrameSizeTest) {
auto runtimeModule = RuntimeModule::createUninitialized(runtime, domain);
/*
get_global reg0
get_named reg1, reg0, "getSP"
call reg0, reg1, 0
ret reg0
*/
StringID getSPID = 1;
const unsigned FRAME_SIZE = 16;
BytecodeModuleGenerator BMG;
auto BFG = BytecodeFunctionGenerator::create(BMG, FRAME_SIZE);
BFG->emitGetGlobalObject(0);
BFG->emitGetById(1, 0, 1, getSPID);
BFG->emitCall(0, 1, 0);
BFG->emitRet(0);
BFG->setHighestReadCacheIndex(1);
BFG->setHighestWriteCacheIndex(0);
BFG->bytecodeGenerationComplete();
auto codeBlock = createCodeBlock(runtimeModule, runtime, BFG.get());
ASSERT_EQ(detail::mapStringMayAllocate(*runtimeModule, "getSP"), getSPID);
auto getSPFn = runtime->makeHandle<NativeFunction>(
*NativeFunction::createWithoutPrototype(
runtime,
nullptr,
getSP,
Predefined::getSymbolID(Predefined::emptyString),
0));
// Define the 'getSP' function.
(void)JSObject::putNamed_RJS(
runtime->getGlobal(),
runtime,
runtimeModule->getSymbolIDFromStringIDMayAllocate(getSPID),
getSPFn);
ScopedNativeCallFrame frame(
runtime, 0, nullptr, false, HermesValue::encodeUndefinedValue());
ASSERT_FALSE(frame.overflowed());
testInterpreterStackSize(runtime, codeBlock);
}
#endif // NDEBUG
} // anonymous namespace