-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathCodeBlock.cpp
More file actions
394 lines (344 loc) · 13.5 KB
/
CodeBlock.cpp
File metadata and controls
394 lines (344 loc) · 13.5 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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#define DEBUG_TYPE "codeblock"
#include "hermes/VM/CodeBlock.h"
#include "hermes/BCGen/HBC/Bytecode.h"
#include "hermes/BCGen/HBC/HBC.h"
#include "hermes/Support/Conversions.h"
#include "hermes/Support/PerfSection.h"
#include "hermes/Support/SimpleDiagHandler.h"
#include "hermes/VM/GCPointer-inline.h"
#include "hermes/VM/Runtime.h"
#include "hermes/VM/RuntimeModule.h"
#include "llvh/Support/Debug.h"
#include "llvh/Support/ErrorHandling.h"
namespace hermes {
namespace vm {
using namespace hermes::inst;
#ifdef HERMES_SLOW_DEBUG
static void validateInstructions(ArrayRef<uint8_t> list, unsigned frameSize) {
const OperandAddr32 listSize = (OperandAddr32)list.size();
assert((size_t)listSize == list.size() && "more than 2GB instructions!");
auto validateUInt8 = [](...) {};
auto validateUInt16 = [](...) {};
auto validateUInt32 = [](...) {};
auto validateImm32 = [](...) {};
auto validateDouble = [](...) {};
auto validateReg8 = [&](OperandAddr32, OperandReg8 reg8) {
assert(reg8 < frameSize && "invalid register index");
};
auto validateReg32 = [&](OperandAddr32, OperandReg32 reg32) {
assert(reg32 < frameSize && "invalid register index");
};
auto validateAddr32 = [&](OperandAddr32 ip, OperandAddr32 offset) {
// Check the offset while avoiding overflow.
assert(
(offset < 0 ? ip + offset >= 0 : offset < listSize - ip) &&
"invalid jmp offset");
};
auto validateAddr8 = [&](OperandAddr32 ip, OperandAddr8 offset) {
validateAddr32(ip, offset);
};
for (OperandAddr32 ip = 0; ip != listSize;) {
assert(ip < listSize);
auto *inst = reinterpret_cast<const Inst *>(&list[ip]);
switch (inst->opCode) {
#define DEFINE_OPCODE_0(name) \
case OpCode::name: \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_1(name, op1type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_2(name, op1type, op2type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
validate##op2type(ip, inst->i##name.op2); \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_3(name, op1type, op2type, op3type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
validate##op2type(ip, inst->i##name.op2); \
validate##op3type(ip, inst->i##name.op3); \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_4(name, op1type, op2type, op3type, op4type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
validate##op2type(ip, inst->i##name.op2); \
validate##op3type(ip, inst->i##name.op3); \
validate##op4type(ip, inst->i##name.op4); \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_5(name, op1type, op2type, op3type, op4type, op5type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
validate##op2type(ip, inst->i##name.op2); \
validate##op3type(ip, inst->i##name.op3); \
validate##op4type(ip, inst->i##name.op4); \
validate##op5type(ip, inst->i##name.op5); \
ip += sizeof(inst->i##name); \
break;
#define DEFINE_OPCODE_6( \
name, op1type, op2type, op3type, op4type, op5type, op6type) \
case OpCode::name: \
validate##op1type(ip, inst->i##name.op1); \
validate##op2type(ip, inst->i##name.op2); \
validate##op3type(ip, inst->i##name.op3); \
validate##op4type(ip, inst->i##name.op4); \
validate##op5type(ip, inst->i##name.op5); \
validate##op6type(ip, inst->i##name.op6); \
ip += sizeof(inst->i##name); \
break;
#include "hermes/BCGen/HBC/BytecodeList.def"
default:
llvm_unreachable("invalid opcode");
}
}
}
#endif
std::unique_ptr<CodeBlock> CodeBlock::createCodeBlock(
RuntimeModule *runtimeModule,
hbc::RuntimeFunctionHeader header,
const uint8_t *bytecode,
uint32_t functionID) {
#ifdef HERMES_SLOW_DEBUG
if (bytecode)
validateInstructions(
{bytecode, header.getBytecodeSizeInBytes()}, header.getFrameSize());
#endif
// Compute size needed for caches. The bytecode instructions have
// one byte for cache indices. If the number of cache entries
// needed in a function reaches 256, we reserve index 255 to mean
// "overflow", don't cache -- the cache size won't grow beyond 256.
// But we only have one byte in the function header for the size.
// So we encode 256 as 255: a size of 255 could mean that there are
// actually 255 elements, or that there are 256. Here we make sure
// that the cache is big enough in that case, by conservatively
// adding one element when the size is 255.
auto sizeComputer = [](uint8_t size) -> uint32_t {
static_assert(hbc::PROPERTY_CACHING_DISABLED == 255);
return size == hbc::PROPERTY_CACHING_DISABLED ? 256 : size;
};
uint32_t readCacheSize = sizeComputer(header.getReadCacheSize());
uint32_t writeCacheSize = sizeComputer(header.getWriteCacheSize());
uint32_t privateNameCacheSize =
sizeComputer(header.getPrivateNameCacheSize());
bool isCodeBlockLazy = !bytecode;
if (isCodeBlockLazy) {
readCacheSize = sizeComputer(std::numeric_limits<uint8_t>::max());
writeCacheSize = sizeComputer(std::numeric_limits<uint8_t>::max());
privateNameCacheSize = sizeComputer(std::numeric_limits<uint8_t>::max());
}
auto allocSize = totalSizeToAlloc<
ReadPropertyCacheEntry,
WritePropertyCacheEntry,
PrivateNameCacheEntry>(
readCacheSize, writeCacheSize, privateNameCacheSize);
void *mem = checkedMalloc(allocSize);
return std::unique_ptr<CodeBlock>(new (mem) CodeBlock(
runtimeModule,
header,
bytecode,
functionID,
readCacheSize,
writeCacheSize,
privateNameCacheSize));
}
int32_t CodeBlock::findCatchTargetOffset(uint32_t exceptionOffset) {
return runtimeModule_->getBytecode()->findCatchTargetOffset(
functionID_, exceptionOffset);
}
SymbolID CodeBlock::getNameMayAllocate() const {
return runtimeModule_->getSymbolIDFromStringIDMayAllocate(
functionHeader_.getFunctionName());
}
std::string CodeBlock::getNameString() const {
return runtimeModule_->getStringFromStringID(
functionHeader_.getFunctionName());
}
OptValue<uint32_t> CodeBlock::getDebugSourceLocationsOffset() const {
auto *debugOffsets =
runtimeModule_->getBytecode()->getDebugOffsets(functionID_);
if (!debugOffsets)
return llvh::None;
uint32_t ret = debugOffsets->sourceLocations;
if (ret == hbc::DebugOffsets::NO_OFFSET)
return llvh::None;
return ret;
}
OptValue<hbc::DebugSourceLocation> CodeBlock::getSourceLocation(
uint32_t offset) const {
auto debugLocsOffset = getDebugSourceLocationsOffset();
if (!debugLocsOffset) {
return llvh::None;
}
return getRuntimeModule()
->getBytecode()
->getDebugInfo()
->getLocationForAddress(*debugLocsOffset, offset);
}
OptValue<hbc::DebugSourceLocation> CodeBlock::getSourceLocationForFunction()
const {
auto debugLocsOffset = getDebugSourceLocationsOffset();
if (!debugLocsOffset) {
return llvh::None;
}
return getRuntimeModule()
->getBytecode()
->getDebugInfo()
->getLocationForFunction(*debugLocsOffset);
}
ExecutionStatus CodeBlock::compileLazyFunction(Runtime &runtime) {
assert(isLazy() && "Laziness has not been checked");
auto *provider = runtimeModule_->getBytecode();
bool success;
llvh::StringRef errMsg;
executeInStack(
runtime.getStackExecutor(), [&success, &errMsg, &provider, this]() {
std::tie(success, errMsg) =
hbc::compileLazyFunction(provider, functionID_);
});
if (!success) {
// Raise a SyntaxError to be consistent with eval().
return runtime.raiseSyntaxError(llvh::StringRef{errMsg});
}
functionHeader_ =
runtimeModule_->getBytecode()->getFunctionHeader(functionID_);
bytecode_ = runtimeModule_->getBytecode()->getBytecode(functionID_);
runtimeModule_->initAfterLazyCompilation();
return ExecutionStatus::RETURNED;
}
bool CodeBlock::coordsInLazyFunction(SMLoc loc, OptValue<SMLoc> end) const {
assert(isLazy() && "function is not lazy");
return hbc::coordsInLazyFunction(
runtimeModule_->getBytecode(), functionID_, loc, end);
}
std::vector<uint32_t> CodeBlock::getVariableCounts(
uint32_t lexicalScopeIdxInParentFunction) const {
return hbc::getVariableCounts(
runtimeModule_->getBytecode(),
functionID_,
lexicalScopeIdxInParentFunction);
}
hbc::VariableInfoAtDepth CodeBlock::getVariableInfoAtDepth(
uint32_t depth,
uint32_t variableIndex,
uint32_t lexicalScopeIdxInParentFunction) const {
return hbc::getVariableInfoAtDepth(
runtimeModule_->getBytecode(),
functionID_,
depth,
variableIndex,
lexicalScopeIdxInParentFunction);
}
OptValue<uint32_t> CodeBlock::getFunctionSourceID() const {
// Note that for the case of lazy compilation, the function sources had been
// reserved into the function source table of the root bytecode module.
// For non-lazy module, the lazy root module is itself.
llvh::ArrayRef<std::pair<uint32_t, uint32_t>> table =
runtimeModule_->getBytecode()->getFunctionSourceTable();
// Performs a binary search since the function source table is sorted by the
// 1st value. We could further optimize the lookup by loading it as a map in
// the RuntimeModule, but the table is expected to be small.
auto it = std::lower_bound(
table.begin(),
table.end(),
functionID_,
[](std::pair<uint32_t, uint32_t> entry, uint32_t id) {
return entry.first < id;
});
if (it == table.end() || it->first != functionID_) {
return llvh::None;
} else {
return it->second;
}
}
void CodeBlock::markWeakElementsInCaches(
Runtime &runtime,
WeakRootAcceptor &acceptor) {
for (auto &prop :
llvh::makeMutableArrayRef(readPropertyCache(), readPropertyCacheSize_)) {
if (prop.clazz) {
acceptor.acceptWeak(prop.clazz);
}
if (prop.negMatchClazz) {
acceptor.acceptWeak(prop.negMatchClazz);
}
}
for (auto &prop : llvh::makeMutableArrayRef(
writePropertyCache(), writePropertyCacheSize_)) {
if (prop.clazz) {
acceptor.acceptWeak(prop.clazz);
}
}
for (auto &prop :
llvh::makeMutableArrayRef(privateNameCache(), privateNameCacheSize_)) {
if (prop.clazz) {
acceptor.acceptWeak(prop.clazz);
}
acceptor.acceptWeakSym(prop.nameVal);
}
}
uint32_t CodeBlock::getVirtualOffset() const {
return getRuntimeModule()->getBytecode()->getVirtualOffsetForFunction(
functionID_);
}
#ifdef HERMES_ENABLE_DEBUGGER
/// Makes the page that \p address is in writable.
/// \return true on success, false if the page cannot be made writable (e.g.
/// the bytecode lives in a read-only segment of the binary that the OS
/// refuses to remap, such as macOS __DATA_CONST under hardened runtime).
static bool makeWritable(void *address, size_t length) {
void *endAddress = static_cast<void *>(static_cast<char *>(address) + length);
// Align the address to page size before setting the pagesize.
void *alignedAddress = reinterpret_cast<void *>(llvh::alignDown(
reinterpret_cast<uintptr_t>(address), hermes::oscompat::page_size()));
size_t totalLength =
static_cast<char *>(endAddress) - static_cast<char *>(alignedAddress);
return oscompat::vm_protect(
alignedAddress, totalLength, oscompat::ProtectMode::ReadWrite);
}
bool CodeBlock::installBreakpointAtOffset(uint32_t offset) {
auto opcodes = getOpcodeArray();
assert(offset < opcodes.size() && "patch offset out of bounds");
hbc::opcode_atom_t *address =
const_cast<hbc::opcode_atom_t *>(opcodes.begin() + offset);
hbc::opcode_atom_t debuggerOpcode =
static_cast<hbc::opcode_atom_t>(OpCode::Debugger);
static_assert(
sizeof(inst::DebuggerInst) == 1,
"debugger instruction can only be a single opcode atom");
if (!makeWritable(address, sizeof(inst::DebuggerInst))) {
return false;
}
*address = debuggerOpcode;
++numInstalledBreakpoints_;
return true;
}
void CodeBlock::uninstallBreakpointAtOffset(
uint32_t offset,
hbc::opcode_atom_t opCode) {
auto opcodes = getOpcodeArray();
assert(offset < opcodes.size() && "unpatch offset out of bounds");
hbc::opcode_atom_t *address =
const_cast<hbc::opcode_atom_t *>(opcodes.begin() + offset);
assert(
*address == static_cast<hbc::opcode_atom_t>(OpCode::Debugger) &&
"can't uninstall a non-debugger instruction");
// This is valid because we can only uninstall breakpoints that we installed.
// Therefore, the page here must be writable.
*address = opCode;
--numInstalledBreakpoints_;
}
#endif
} // namespace vm
} // namespace hermes
#undef DEBUG_TYPE