1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/external-reference-table.h"
6 
7 #include "src/accessors.h"
8 #include "src/assembler.h"
9 #include "src/builtins/builtins.h"
10 #include "src/counters.h"
11 #include "src/deoptimizer.h"
12 #include "src/ic/stub-cache.h"
13 
14 #if defined(DEBUG) && defined(V8_OS_LINUX) && !defined(V8_OS_ANDROID)
15 #define SYMBOLIZE_FUNCTION
16 #include <execinfo.h>
17 #endif  // DEBUG && V8_OS_LINUX && !V8_OS_ANDROID
18 
19 namespace v8 {
20 namespace internal {
21 
22 // Forward declarations for C++ builtins.
23 #define FORWARD_DECLARE(Name) \
24   Object* Builtin_##Name(int argc, Object** args, Isolate* isolate);
BUILTIN_LIST_C(FORWARD_DECLARE)25 BUILTIN_LIST_C(FORWARD_DECLARE)
26 #undef FORWARD_DECLARE
27 
28 ExternalReferenceTable* ExternalReferenceTable::instance(Isolate* isolate) {
29   ExternalReferenceTable* external_reference_table =
30       isolate->external_reference_table();
31   if (external_reference_table == NULL) {
32     external_reference_table = new ExternalReferenceTable(isolate);
33     isolate->set_external_reference_table(external_reference_table);
34   }
35   return external_reference_table;
36 }
37 
ExternalReferenceTable(Isolate * isolate)38 ExternalReferenceTable::ExternalReferenceTable(Isolate* isolate) {
39   // nullptr is preserved through serialization/deserialization.
40   Add(nullptr, "nullptr");
41   AddReferences(isolate);
42   AddBuiltins(isolate);
43   AddRuntimeFunctions(isolate);
44   AddIsolateAddresses(isolate);
45   AddAccessors(isolate);
46   AddStubCache(isolate);
47   AddDeoptEntries(isolate);
48   AddApiReferences(isolate);
49 }
50 
51 #ifdef DEBUG
ResetCount()52 void ExternalReferenceTable::ResetCount() {
53   for (ExternalReferenceEntry& entry : refs_) entry.count = 0;
54 }
55 
PrintCount()56 void ExternalReferenceTable::PrintCount() {
57   for (int i = 0; i < refs_.length(); i++) {
58     v8::base::OS::Print("index=%5d count=%5d  %-60s\n", i, refs_[i].count,
59                         refs_[i].name);
60   }
61 }
62 #endif  // DEBUG
63 
64 // static
ResolveSymbol(void * address)65 const char* ExternalReferenceTable::ResolveSymbol(void* address) {
66 #ifdef SYMBOLIZE_FUNCTION
67   return backtrace_symbols(&address, 1)[0];
68 #else
69   return "<unresolved>";
70 #endif  // SYMBOLIZE_FUNCTION
71 }
72 
AddReferences(Isolate * isolate)73 void ExternalReferenceTable::AddReferences(Isolate* isolate) {
74   // Miscellaneous
75   Add(ExternalReference::roots_array_start(isolate).address(),
76       "Heap::roots_array_start()");
77   Add(ExternalReference::address_of_stack_limit(isolate).address(),
78       "StackGuard::address_of_jslimit()");
79   Add(ExternalReference::address_of_real_stack_limit(isolate).address(),
80       "StackGuard::address_of_real_jslimit()");
81   Add(ExternalReference::new_space_allocation_limit_address(isolate).address(),
82       "Heap::NewSpaceAllocationLimitAddress()");
83   Add(ExternalReference::new_space_allocation_top_address(isolate).address(),
84       "Heap::NewSpaceAllocationTopAddress()");
85   Add(ExternalReference::mod_two_doubles_operation(isolate).address(),
86       "mod_two_doubles");
87   Add(ExternalReference::handle_scope_next_address(isolate).address(),
88       "HandleScope::next");
89   Add(ExternalReference::handle_scope_limit_address(isolate).address(),
90       "HandleScope::limit");
91   Add(ExternalReference::handle_scope_level_address(isolate).address(),
92       "HandleScope::level");
93   Add(ExternalReference::new_deoptimizer_function(isolate).address(),
94       "Deoptimizer::New()");
95   Add(ExternalReference::compute_output_frames_function(isolate).address(),
96       "Deoptimizer::ComputeOutputFrames()");
97   Add(ExternalReference::address_of_min_int().address(),
98       "LDoubleConstant::min_int");
99   Add(ExternalReference::address_of_one_half().address(),
100       "LDoubleConstant::one_half");
101   Add(ExternalReference::isolate_address(isolate).address(), "isolate");
102   Add(ExternalReference::interpreter_dispatch_table_address(isolate).address(),
103       "Interpreter::dispatch_table_address");
104   Add(ExternalReference::address_of_negative_infinity().address(),
105       "LDoubleConstant::negative_infinity");
106   Add(ExternalReference::power_double_double_function(isolate).address(),
107       "power_double_double_function");
108   Add(ExternalReference::ieee754_acos_function(isolate).address(),
109       "base::ieee754::acos");
110   Add(ExternalReference::ieee754_acosh_function(isolate).address(),
111       "base::ieee754::acosh");
112   Add(ExternalReference::ieee754_asin_function(isolate).address(),
113       "base::ieee754::asin");
114   Add(ExternalReference::ieee754_asinh_function(isolate).address(),
115       "base::ieee754::asinh");
116   Add(ExternalReference::ieee754_atan_function(isolate).address(),
117       "base::ieee754::atan");
118   Add(ExternalReference::ieee754_atanh_function(isolate).address(),
119       "base::ieee754::atanh");
120   Add(ExternalReference::ieee754_atan2_function(isolate).address(),
121       "base::ieee754::atan2");
122   Add(ExternalReference::ieee754_cbrt_function(isolate).address(),
123       "base::ieee754::cbrt");
124   Add(ExternalReference::ieee754_cos_function(isolate).address(),
125       "base::ieee754::cos");
126   Add(ExternalReference::ieee754_cosh_function(isolate).address(),
127       "base::ieee754::cosh");
128   Add(ExternalReference::ieee754_exp_function(isolate).address(),
129       "base::ieee754::exp");
130   Add(ExternalReference::ieee754_expm1_function(isolate).address(),
131       "base::ieee754::expm1");
132   Add(ExternalReference::ieee754_log_function(isolate).address(),
133       "base::ieee754::log");
134   Add(ExternalReference::ieee754_log1p_function(isolate).address(),
135       "base::ieee754::log1p");
136   Add(ExternalReference::ieee754_log10_function(isolate).address(),
137       "base::ieee754::log10");
138   Add(ExternalReference::ieee754_log2_function(isolate).address(),
139       "base::ieee754::log2");
140   Add(ExternalReference::ieee754_sin_function(isolate).address(),
141       "base::ieee754::sin");
142   Add(ExternalReference::ieee754_sinh_function(isolate).address(),
143       "base::ieee754::sinh");
144   Add(ExternalReference::ieee754_tan_function(isolate).address(),
145       "base::ieee754::tan");
146   Add(ExternalReference::ieee754_tanh_function(isolate).address(),
147       "base::ieee754::tanh");
148   Add(ExternalReference::store_buffer_top(isolate).address(),
149       "store_buffer_top");
150   Add(ExternalReference::address_of_the_hole_nan().address(), "the_hole_nan");
151   Add(ExternalReference::get_date_field_function(isolate).address(),
152       "JSDate::GetField");
153   Add(ExternalReference::date_cache_stamp(isolate).address(),
154       "date_cache_stamp");
155   Add(ExternalReference::address_of_pending_message_obj(isolate).address(),
156       "address_of_pending_message_obj");
157   Add(ExternalReference::get_make_code_young_function(isolate).address(),
158       "Code::MakeCodeYoung");
159   Add(ExternalReference::cpu_features().address(), "cpu_features");
160   Add(ExternalReference::old_space_allocation_top_address(isolate).address(),
161       "Heap::OldSpaceAllocationTopAddress");
162   Add(ExternalReference::old_space_allocation_limit_address(isolate).address(),
163       "Heap::OldSpaceAllocationLimitAddress");
164   Add(ExternalReference::allocation_sites_list_address(isolate).address(),
165       "Heap::allocation_sites_list_address()");
166   Add(ExternalReference::address_of_uint32_bias().address(), "uint32_bias");
167   Add(ExternalReference::get_mark_code_as_executed_function(isolate).address(),
168       "Code::MarkCodeAsExecuted");
169   Add(ExternalReference::is_profiling_address(isolate).address(),
170       "Isolate::is_profiling");
171   Add(ExternalReference::scheduled_exception_address(isolate).address(),
172       "Isolate::scheduled_exception");
173   Add(ExternalReference::invoke_function_callback(isolate).address(),
174       "InvokeFunctionCallback");
175   Add(ExternalReference::invoke_accessor_getter_callback(isolate).address(),
176       "InvokeAccessorGetterCallback");
177   Add(ExternalReference::wasm_f32_trunc(isolate).address(),
178       "wasm::f32_trunc_wrapper");
179   Add(ExternalReference::wasm_f32_floor(isolate).address(),
180       "wasm::f32_floor_wrapper");
181   Add(ExternalReference::wasm_f32_ceil(isolate).address(),
182       "wasm::f32_ceil_wrapper");
183   Add(ExternalReference::wasm_f32_nearest_int(isolate).address(),
184       "wasm::f32_nearest_int_wrapper");
185   Add(ExternalReference::wasm_f64_trunc(isolate).address(),
186       "wasm::f64_trunc_wrapper");
187   Add(ExternalReference::wasm_f64_floor(isolate).address(),
188       "wasm::f64_floor_wrapper");
189   Add(ExternalReference::wasm_f64_ceil(isolate).address(),
190       "wasm::f64_ceil_wrapper");
191   Add(ExternalReference::wasm_f64_nearest_int(isolate).address(),
192       "wasm::f64_nearest_int_wrapper");
193   Add(ExternalReference::wasm_int64_to_float32(isolate).address(),
194       "wasm::int64_to_float32_wrapper");
195   Add(ExternalReference::wasm_uint64_to_float32(isolate).address(),
196       "wasm::uint64_to_float32_wrapper");
197   Add(ExternalReference::wasm_int64_to_float64(isolate).address(),
198       "wasm::int64_to_float64_wrapper");
199   Add(ExternalReference::wasm_uint64_to_float64(isolate).address(),
200       "wasm::uint64_to_float64_wrapper");
201   Add(ExternalReference::wasm_float32_to_int64(isolate).address(),
202       "wasm::float32_to_int64_wrapper");
203   Add(ExternalReference::wasm_float32_to_uint64(isolate).address(),
204       "wasm::float32_to_uint64_wrapper");
205   Add(ExternalReference::wasm_float64_to_int64(isolate).address(),
206       "wasm::float64_to_int64_wrapper");
207   Add(ExternalReference::wasm_float64_to_uint64(isolate).address(),
208       "wasm::float64_to_uint64_wrapper");
209   Add(ExternalReference::wasm_float64_pow(isolate).address(),
210       "wasm::float64_pow");
211   Add(ExternalReference::wasm_int64_div(isolate).address(), "wasm::int64_div");
212   Add(ExternalReference::wasm_int64_mod(isolate).address(), "wasm::int64_mod");
213   Add(ExternalReference::wasm_uint64_div(isolate).address(),
214       "wasm::uint64_div");
215   Add(ExternalReference::wasm_uint64_mod(isolate).address(),
216       "wasm::uint64_mod");
217   Add(ExternalReference::wasm_word32_ctz(isolate).address(),
218       "wasm::word32_ctz");
219   Add(ExternalReference::wasm_word64_ctz(isolate).address(),
220       "wasm::word64_ctz");
221   Add(ExternalReference::wasm_word32_popcnt(isolate).address(),
222       "wasm::word32_popcnt");
223   Add(ExternalReference::wasm_word64_popcnt(isolate).address(),
224       "wasm::word64_popcnt");
225   Add(ExternalReference::f64_acos_wrapper_function(isolate).address(),
226       "f64_acos_wrapper");
227   Add(ExternalReference::f64_asin_wrapper_function(isolate).address(),
228       "f64_asin_wrapper");
229   Add(ExternalReference::f64_mod_wrapper_function(isolate).address(),
230       "f64_mod_wrapper");
231   Add(ExternalReference::log_enter_external_function(isolate).address(),
232       "Logger::EnterExternal");
233   Add(ExternalReference::log_leave_external_function(isolate).address(),
234       "Logger::LeaveExternal");
235   Add(ExternalReference::address_of_minus_one_half().address(),
236       "double_constants.minus_one_half");
237   Add(ExternalReference::stress_deopt_count(isolate).address(),
238       "Isolate::stress_deopt_count_address()");
239   Add(ExternalReference::runtime_function_table_address(isolate).address(),
240       "Runtime::runtime_function_table_address()");
241   Add(ExternalReference::is_tail_call_elimination_enabled_address(isolate)
242           .address(),
243       "Isolate::is_tail_call_elimination_enabled_address()");
244   Add(ExternalReference::address_of_float_abs_constant().address(),
245       "float_absolute_constant");
246   Add(ExternalReference::address_of_float_neg_constant().address(),
247       "float_negate_constant");
248   Add(ExternalReference::address_of_double_abs_constant().address(),
249       "double_absolute_constant");
250   Add(ExternalReference::address_of_double_neg_constant().address(),
251       "double_negate_constant");
252 
253   // Debug addresses
254   Add(ExternalReference::debug_after_break_target_address(isolate).address(),
255       "Debug::after_break_target_address()");
256   Add(ExternalReference::debug_is_active_address(isolate).address(),
257       "Debug::is_active_address()");
258   Add(ExternalReference::debug_last_step_action_address(isolate).address(),
259       "Debug::step_in_enabled_address()");
260   Add(ExternalReference::debug_suspended_generator_address(isolate).address(),
261       "Debug::step_suspended_generator_address()");
262 
263 #ifndef V8_INTERPRETED_REGEXP
264   Add(ExternalReference::re_case_insensitive_compare_uc16(isolate).address(),
265       "NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()");
266   Add(ExternalReference::re_check_stack_guard_state(isolate).address(),
267       "RegExpMacroAssembler*::CheckStackGuardState()");
268   Add(ExternalReference::re_grow_stack(isolate).address(),
269       "NativeRegExpMacroAssembler::GrowStack()");
270   Add(ExternalReference::re_word_character_map().address(),
271       "NativeRegExpMacroAssembler::word_character_map");
272   Add(ExternalReference::address_of_regexp_stack_limit(isolate).address(),
273       "RegExpStack::limit_address()");
274   Add(ExternalReference::address_of_regexp_stack_memory_address(isolate)
275           .address(),
276       "RegExpStack::memory_address()");
277   Add(ExternalReference::address_of_regexp_stack_memory_size(isolate).address(),
278       "RegExpStack::memory_size()");
279   Add(ExternalReference::address_of_static_offsets_vector(isolate).address(),
280       "OffsetsVector::static_offsets_vector");
281 #endif  // V8_INTERPRETED_REGEXP
282 
283   // Runtime entries
284   Add(ExternalReference::delete_handle_scope_extensions(isolate).address(),
285       "HandleScope::DeleteExtensions");
286   Add(ExternalReference::incremental_marking_record_write_function(isolate)
287           .address(),
288       "IncrementalMarking::RecordWrite");
289   Add(ExternalReference::incremental_marking_record_write_code_entry_function(
290           isolate)
291           .address(),
292       "IncrementalMarking::RecordWriteOfCodeEntryFromCode");
293   Add(ExternalReference::store_buffer_overflow_function(isolate).address(),
294       "StoreBuffer::StoreBufferOverflow");
295 }
296 
AddBuiltins(Isolate * isolate)297 void ExternalReferenceTable::AddBuiltins(Isolate* isolate) {
298   struct CBuiltinEntry {
299     Address address;
300     const char* name;
301   };
302   static const CBuiltinEntry c_builtins[] = {
303 #define DEF_ENTRY(Name, ...) {FUNCTION_ADDR(&Builtin_##Name), "Builtin_" #Name},
304       BUILTIN_LIST_C(DEF_ENTRY)
305 #undef DEF_ENTRY
306   };
307   for (unsigned i = 0; i < arraysize(c_builtins); ++i) {
308     Add(ExternalReference(c_builtins[i].address, isolate).address(),
309         c_builtins[i].name);
310   }
311 
312   struct BuiltinEntry {
313     Builtins::Name id;
314     const char* name;
315   };
316   static const BuiltinEntry builtins[] = {
317 #define DEF_ENTRY(Name, ...) {Builtins::k##Name, "Builtin_" #Name},
318       BUILTIN_LIST_C(DEF_ENTRY) BUILTIN_LIST_A(DEF_ENTRY)
319 #undef DEF_ENTRY
320   };
321   for (unsigned i = 0; i < arraysize(builtins); ++i) {
322     Add(isolate->builtins()->builtin_address(builtins[i].id), builtins[i].name);
323   }
324 }
325 
AddRuntimeFunctions(Isolate * isolate)326 void ExternalReferenceTable::AddRuntimeFunctions(Isolate* isolate) {
327   struct RuntimeEntry {
328     Runtime::FunctionId id;
329     const char* name;
330   };
331 
332   static const RuntimeEntry runtime_functions[] = {
333 #define RUNTIME_ENTRY(name, i1, i2) {Runtime::k##name, "Runtime::" #name},
334       FOR_EACH_INTRINSIC(RUNTIME_ENTRY)
335 #undef RUNTIME_ENTRY
336   };
337 
338   for (unsigned i = 0; i < arraysize(runtime_functions); ++i) {
339     ExternalReference ref(runtime_functions[i].id, isolate);
340     Add(ref.address(), runtime_functions[i].name);
341   }
342 }
343 
AddIsolateAddresses(Isolate * isolate)344 void ExternalReferenceTable::AddIsolateAddresses(Isolate* isolate) {
345   // Top addresses
346   static const char* address_names[] = {
347 #define BUILD_NAME_LITERAL(Name, name) "Isolate::" #name "_address",
348       FOR_EACH_ISOLATE_ADDRESS_NAME(BUILD_NAME_LITERAL) NULL
349 #undef BUILD_NAME_LITERAL
350   };
351 
352   for (int i = 0; i < Isolate::kIsolateAddressCount; ++i) {
353     Add(isolate->get_address_from_id(static_cast<Isolate::AddressId>(i)),
354         address_names[i]);
355   }
356 }
357 
AddAccessors(Isolate * isolate)358 void ExternalReferenceTable::AddAccessors(Isolate* isolate) {
359   // Accessors
360   struct AccessorRefTable {
361     Address address;
362     const char* name;
363   };
364 
365   static const AccessorRefTable getters[] = {
366 #define ACCESSOR_INFO_DECLARATION(name)     \
367   { FUNCTION_ADDR(&Accessors::name##Getter), \
368     "Redirect to Accessors::" #name "Getter"},
369       ACCESSOR_INFO_LIST(ACCESSOR_INFO_DECLARATION)
370 #undef ACCESSOR_INFO_DECLARATION
371   };
372   static const AccessorRefTable setters[] = {
373 #define ACCESSOR_SETTER_DECLARATION(name) \
374   { FUNCTION_ADDR(&Accessors::name), "Accessors::" #name},
375       ACCESSOR_SETTER_LIST(ACCESSOR_SETTER_DECLARATION)
376 #undef ACCESSOR_INFO_DECLARATION
377   };
378 
379   for (unsigned i = 0; i < arraysize(getters); ++i) {
380     const char* name = getters[i].name + 12;  // Skip "Redirect to " prefix.
381     Add(getters[i].address, name);
382     Add(AccessorInfo::redirect(isolate, getters[i].address, ACCESSOR_GETTER),
383         getters[i].name);
384   }
385 
386   for (unsigned i = 0; i < arraysize(setters); ++i) {
387     Add(setters[i].address, setters[i].name);
388   }
389 }
390 
AddStubCache(Isolate * isolate)391 void ExternalReferenceTable::AddStubCache(Isolate* isolate) {
392   StubCache* load_stub_cache = isolate->load_stub_cache();
393 
394   // Stub cache tables
395   Add(load_stub_cache->key_reference(StubCache::kPrimary).address(),
396       "Load StubCache::primary_->key");
397   Add(load_stub_cache->value_reference(StubCache::kPrimary).address(),
398       "Load StubCache::primary_->value");
399   Add(load_stub_cache->map_reference(StubCache::kPrimary).address(),
400       "Load StubCache::primary_->map");
401   Add(load_stub_cache->key_reference(StubCache::kSecondary).address(),
402       "Load StubCache::secondary_->key");
403   Add(load_stub_cache->value_reference(StubCache::kSecondary).address(),
404       "Load StubCache::secondary_->value");
405   Add(load_stub_cache->map_reference(StubCache::kSecondary).address(),
406       "Load StubCache::secondary_->map");
407 
408   StubCache* store_stub_cache = isolate->store_stub_cache();
409 
410   // Stub cache tables
411   Add(store_stub_cache->key_reference(StubCache::kPrimary).address(),
412       "Store StubCache::primary_->key");
413   Add(store_stub_cache->value_reference(StubCache::kPrimary).address(),
414       "Store StubCache::primary_->value");
415   Add(store_stub_cache->map_reference(StubCache::kPrimary).address(),
416       "Store StubCache::primary_->map");
417   Add(store_stub_cache->key_reference(StubCache::kSecondary).address(),
418       "Store StubCache::secondary_->key");
419   Add(store_stub_cache->value_reference(StubCache::kSecondary).address(),
420       "Store StubCache::secondary_->value");
421   Add(store_stub_cache->map_reference(StubCache::kSecondary).address(),
422       "Store StubCache::secondary_->map");
423 }
424 
AddDeoptEntries(Isolate * isolate)425 void ExternalReferenceTable::AddDeoptEntries(Isolate* isolate) {
426   // Add a small set of deopt entry addresses to encoder without generating
427   // the
428   // deopt table code, which isn't possible at deserialization time.
429   HandleScope scope(isolate);
430   for (int entry = 0; entry < kDeoptTableSerializeEntryCount; ++entry) {
431     Address address = Deoptimizer::GetDeoptimizationEntry(
432         isolate, entry, Deoptimizer::LAZY,
433         Deoptimizer::CALCULATE_ENTRY_ADDRESS);
434     Add(address, "lazy_deopt");
435   }
436 }
437 
AddApiReferences(Isolate * isolate)438 void ExternalReferenceTable::AddApiReferences(Isolate* isolate) {
439   // Add external references provided by the embedder (a null-terminated
440   // array).
441   intptr_t* api_external_references = isolate->api_external_references();
442   if (api_external_references != nullptr) {
443     while (*api_external_references != 0) {
444       Address address = reinterpret_cast<Address>(*api_external_references);
445       Add(address, ResolveSymbol(address));
446       api_external_references++;
447     }
448   }
449 }
450 
451 }  // namespace internal
452 }  // namespace v8
453