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
|
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/runtime/runtime.h"
#include "src/base/hashmap.h"
#include "src/codegen/reloc-info.h"
#include "src/execution/isolate.h"
#include "src/handles/handles-inl.h"
#include "src/heap/heap.h"
#include "src/objects/contexts.h"
#include "src/objects/objects-inl.h"
#include "src/runtime/runtime-utils.h"
namespace v8 {
namespace internal {
// Header of runtime functions.
#define F(name, number_of_args, result_size) \
Address Runtime_##name(int args_length, Address* args_object, \
Isolate* isolate);
FOR_EACH_INTRINSIC_RETURN_OBJECT(F)
#undef F
#define P(name, number_of_args, result_size) \
ObjectPair Runtime_##name(int args_length, Address* args_object, \
Isolate* isolate);
FOR_EACH_INTRINSIC_RETURN_PAIR(P)
#undef P
#define F(name, number_of_args, result_size) \
{ \
Runtime::k##name, Runtime::RUNTIME, #name, FUNCTION_ADDR(Runtime_##name), \
number_of_args, result_size \
} \
,
#define I(name, number_of_args, result_size) \
{ \
Runtime::kInline##name, Runtime::INLINE, "_" #name, \
FUNCTION_ADDR(Runtime_##name), number_of_args, result_size \
} \
,
static const Runtime::Function kIntrinsicFunctions[] = {
FOR_EACH_INTRINSIC(F) FOR_EACH_INLINE_INTRINSIC(I)};
#undef I
#undef F
namespace {
V8_DECLARE_ONCE(initialize_function_name_map_once);
static const base::CustomMatcherHashMap* kRuntimeFunctionNameMap;
struct IntrinsicFunctionIdentifier {
IntrinsicFunctionIdentifier(const unsigned char* data, const int length)
: data_(data), length_(length) {}
static bool Match(void* key1, void* key2) {
const IntrinsicFunctionIdentifier* lhs =
static_cast<IntrinsicFunctionIdentifier*>(key1);
const IntrinsicFunctionIdentifier* rhs =
static_cast<IntrinsicFunctionIdentifier*>(key2);
if (lhs->length_ != rhs->length_) return false;
return CompareCharsUnsigned(reinterpret_cast<const uint8_t*>(lhs->data_),
reinterpret_cast<const uint8_t*>(rhs->data_),
rhs->length_) == 0;
}
uint32_t Hash() {
return StringHasher::HashSequentialString<uint8_t>(
data_, length_, v8::internal::kZeroHashSeed);
}
const unsigned char* data_;
const int length_;
};
void InitializeIntrinsicFunctionNames() {
base::CustomMatcherHashMap* function_name_map =
new base::CustomMatcherHashMap(IntrinsicFunctionIdentifier::Match);
for (size_t i = 0; i < arraysize(kIntrinsicFunctions); ++i) {
const Runtime::Function* function = &kIntrinsicFunctions[i];
IntrinsicFunctionIdentifier* identifier = new IntrinsicFunctionIdentifier(
reinterpret_cast<const unsigned char*>(function->name),
static_cast<int>(strlen(function->name)));
base::HashMap::Entry* entry =
function_name_map->InsertNew(identifier, identifier->Hash());
entry->value = const_cast<Runtime::Function*>(function);
}
kRuntimeFunctionNameMap = function_name_map;
}
} // namespace
bool Runtime::NeedsExactContext(FunctionId id) {
switch (id) {
case Runtime::kInlineAsyncFunctionReject:
case Runtime::kInlineAsyncFunctionResolve:
// For %_AsyncFunctionReject and %_AsyncFunctionResolve we don't
// really need the current context, which in particular allows
// us to usually eliminate the catch context for the implicit
// try-catch in async function.
return false;
case Runtime::kAddPrivateField:
case Runtime::kAddPrivateBrand:
case Runtime::kCreatePrivateAccessors:
case Runtime::kCopyDataProperties:
case Runtime::kCreateDataProperty:
case Runtime::kCreatePrivateNameSymbol:
case Runtime::kCreatePrivateBrandSymbol:
case Runtime::kLoadPrivateGetter:
case Runtime::kLoadPrivateSetter:
case Runtime::kReThrow:
case Runtime::kThrow:
case Runtime::kThrowApplyNonFunction:
case Runtime::kThrowCalledNonCallable:
case Runtime::kThrowConstAssignError:
case Runtime::kThrowConstructorNonCallableError:
case Runtime::kThrowConstructedNonConstructable:
case Runtime::kThrowConstructorReturnedNonObject:
case Runtime::kThrowInvalidStringLength:
case Runtime::kThrowInvalidTypedArrayAlignment:
case Runtime::kThrowIteratorError:
case Runtime::kThrowIteratorResultNotAnObject:
case Runtime::kThrowNotConstructor:
case Runtime::kThrowRangeError:
case Runtime::kThrowReferenceError:
case Runtime::kThrowAccessedUninitializedVariable:
case Runtime::kThrowStackOverflow:
case Runtime::kThrowStaticPrototypeError:
case Runtime::kThrowSuperAlreadyCalledError:
case Runtime::kThrowSuperNotCalled:
case Runtime::kThrowSymbolAsyncIteratorInvalid:
case Runtime::kThrowSymbolIteratorInvalid:
case Runtime::kThrowThrowMethodMissing:
case Runtime::kThrowTypeError:
case Runtime::kThrowUnsupportedSuperError:
case Runtime::kThrowWasmError:
case Runtime::kThrowWasmStackOverflow:
return false;
default:
return true;
}
}
bool Runtime::IsNonReturning(FunctionId id) {
switch (id) {
case Runtime::kThrowUnsupportedSuperError:
case Runtime::kThrowConstructorNonCallableError:
case Runtime::kThrowStaticPrototypeError:
case Runtime::kThrowSuperAlreadyCalledError:
case Runtime::kThrowSuperNotCalled:
case Runtime::kReThrow:
case Runtime::kThrow:
case Runtime::kThrowApplyNonFunction:
case Runtime::kThrowCalledNonCallable:
case Runtime::kThrowConstructedNonConstructable:
case Runtime::kThrowConstructorReturnedNonObject:
case Runtime::kThrowInvalidStringLength:
case Runtime::kThrowInvalidTypedArrayAlignment:
case Runtime::kThrowIteratorError:
case Runtime::kThrowIteratorResultNotAnObject:
case Runtime::kThrowThrowMethodMissing:
case Runtime::kThrowSymbolIteratorInvalid:
case Runtime::kThrowNotConstructor:
case Runtime::kThrowRangeError:
case Runtime::kThrowReferenceError:
case Runtime::kThrowAccessedUninitializedVariable:
case Runtime::kThrowStackOverflow:
case Runtime::kThrowSymbolAsyncIteratorInvalid:
case Runtime::kThrowTypeError:
case Runtime::kThrowConstAssignError:
case Runtime::kThrowWasmError:
case Runtime::kThrowWasmStackOverflow:
return true;
default:
return false;
}
}
bool Runtime::MayAllocate(FunctionId id) {
switch (id) {
case Runtime::kCompleteInobjectSlackTracking:
case Runtime::kCompleteInobjectSlackTrackingForMap:
return false;
default:
return true;
}
}
bool Runtime::IsWhitelistedForFuzzing(FunctionId id) {
CHECK(FLAG_allow_natives_for_fuzzing);
switch (id) {
// Runtime functions whitelisted for all fuzzers. Only add functions that
// help increase coverage.
case Runtime::kArrayBufferDetach:
case Runtime::kDeoptimizeFunction:
case Runtime::kDeoptimizeNow:
case Runtime::kEnableCodeLoggingForTesting:
case Runtime::kGetUndetectable:
case Runtime::kNeverOptimizeFunction:
case Runtime::kOptimizeFunctionOnNextCall:
case Runtime::kOptimizeOsr:
case Runtime::kPrepareFunctionForOptimization:
case Runtime::kSetAllocationTimeout:
case Runtime::kSimulateNewspaceFull:
return true;
// Runtime functions only permitted for non-differential fuzzers.
// This list may contain functions performing extra checks or returning
// different values in the context of different flags passed to V8.
case Runtime::kHeapObjectVerify:
case Runtime::kIsBeingInterpreted:
return !FLAG_allow_natives_for_differential_fuzzing;
default:
return false;
}
}
const Runtime::Function* Runtime::FunctionForName(const unsigned char* name,
int length) {
base::CallOnce(&initialize_function_name_map_once,
&InitializeIntrinsicFunctionNames);
IntrinsicFunctionIdentifier identifier(name, length);
base::HashMap::Entry* entry =
kRuntimeFunctionNameMap->Lookup(&identifier, identifier.Hash());
if (entry) {
return reinterpret_cast<Function*>(entry->value);
}
return nullptr;
}
const Runtime::Function* Runtime::FunctionForEntry(Address entry) {
for (size_t i = 0; i < arraysize(kIntrinsicFunctions); ++i) {
if (entry == kIntrinsicFunctions[i].entry) {
return &(kIntrinsicFunctions[i]);
}
}
return nullptr;
}
const Runtime::Function* Runtime::FunctionForId(Runtime::FunctionId id) {
return &(kIntrinsicFunctions[static_cast<int>(id)]);
}
const Runtime::Function* Runtime::RuntimeFunctionTable(Isolate* isolate) {
#ifdef USE_SIMULATOR
// When running with the simulator we need to provide a table which has
// redirected runtime entry addresses.
if (!isolate->runtime_state()->redirected_intrinsic_functions()) {
size_t function_count = arraysize(kIntrinsicFunctions);
Function* redirected_functions = new Function[function_count];
memcpy(redirected_functions, kIntrinsicFunctions,
sizeof(kIntrinsicFunctions));
for (size_t i = 0; i < function_count; i++) {
ExternalReference redirected_entry =
ExternalReference::Create(static_cast<Runtime::FunctionId>(i));
redirected_functions[i].entry = redirected_entry.address();
}
isolate->runtime_state()->set_redirected_intrinsic_functions(
redirected_functions);
}
return isolate->runtime_state()->redirected_intrinsic_functions();
#else
return kIntrinsicFunctions;
#endif
}
std::ostream& operator<<(std::ostream& os, Runtime::FunctionId id) {
return os << Runtime::FunctionForId(id)->name;
}
} // namespace internal
} // namespace v8
|