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