src/share/vm/runtime/sharedRuntime.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File JDK-8026478 Sdiff src/share/vm/runtime

src/share/vm/runtime/sharedRuntime.cpp

Print this page




2357 }
2358 
2359 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(methodHandle method) {
2360   // Use customized signature handler.  Need to lock around updates to
2361   // the AdapterHandlerTable (it is not safe for concurrent readers
2362   // and a single writer: this could be fixed if it becomes a
2363   // problem).
2364 
2365   // Get the address of the ic_miss handlers before we grab the
2366   // AdapterHandlerLibrary_lock. This fixes bug 6236259 which
2367   // was caused by the initialization of the stubs happening
2368   // while we held the lock and then notifying jvmti while
2369   // holding it. This just forces the initialization to be a little
2370   // earlier.
2371   address ic_miss = SharedRuntime::get_ic_miss_stub();
2372   assert(ic_miss != NULL, "must have handler");
2373 
2374   ResourceMark rm;
2375 
2376   NOT_PRODUCT(int insts_size);
2377   AdapterBlob* B = NULL;
2378   AdapterHandlerEntry* entry = NULL;
2379   AdapterFingerPrint* fingerprint = NULL;
2380   {
2381     MutexLocker mu(AdapterHandlerLibrary_lock);
2382     // make sure data structure is initialized
2383     initialize();
2384 
2385     if (method->is_abstract()) {
2386       return _abstract_method_handler;
2387     }
2388 
2389     // Fill in the signature array, for the calling-convention call.
2390     int total_args_passed = method->size_of_parameters(); // All args on stack
2391 
2392     BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2393     VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2394     int i = 0;
2395     if (!method->is_static())  // Pass in receiver first
2396       sig_bt[i++] = T_OBJECT;
2397     for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2398       sig_bt[i++] = ss.type();  // Collect remaining bits of signature
2399       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2400         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
2401     }
2402     assert(i == total_args_passed, "");
2403 
2404     // Lookup method signature's fingerprint
2405     entry = _adapters->lookup(total_args_passed, sig_bt);
2406 
2407 #ifdef ASSERT
2408     AdapterHandlerEntry* shared_entry = NULL;
2409     if (VerifyAdapterSharing && entry != NULL) {

2410       shared_entry = entry;
2411       entry = NULL;
2412     }
2413 #endif
2414 
2415     if (entry != NULL) {
2416       return entry;
2417     }
2418 
2419     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2420     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
2421 
2422     // Make a C heap allocated version of the fingerprint to store in the adapter
2423     fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
2424 
2425     // Create I2C & C2I handlers



2426 

2427     BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2428     if (buf != NULL) {
2429       CodeBuffer buffer(buf);
2430       short buffer_locs[20];
2431       buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2432                                              sizeof(buffer_locs)/sizeof(relocInfo));
2433       MacroAssembler _masm(&buffer);
2434 

2435       entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2436                                                      total_args_passed,
2437                                                      comp_args_on_stack,
2438                                                      sig_bt,
2439                                                      regs,
2440                                                      fingerprint);
2441 
2442 #ifdef ASSERT
2443       if (VerifyAdapterSharing) {
2444         if (shared_entry != NULL) {
2445           assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size(), total_args_passed, sig_bt),
2446                  "code must match");
2447           // Release the one just created and return the original
2448           _adapters->free_entry(entry);
2449           return shared_entry;
2450         } else  {
2451           entry->save_code(buf->code_begin(), buffer.insts_size(), total_args_passed, sig_bt);
2452         }
2453       }
2454 #endif
2455 
2456       B = AdapterBlob::create(&buffer);
2457       NOT_PRODUCT(insts_size = buffer.insts_size());
2458     }
2459     if (B == NULL) {
2460       // CodeCache is full, disable compilation
2461       // Ought to log this but compile log is only per compile thread
2462       // and we're some non descript Java thread.
2463       MutexUnlocker mu(AdapterHandlerLibrary_lock);
2464       CompileBroker::handle_full_code_cache();
2465       return NULL; // Out of CodeCache space
2466     }
2467     entry->relocate(B->content_begin());
2468 #ifndef PRODUCT
2469     // debugging suppport
2470     if (PrintAdapterHandlers || PrintStubCode) {
2471       ttyLocker ttyl;
2472       entry->print_adapter_on(tty);
2473       tty->print_cr("i2c argument handler #%d for: %s %s (%d bytes generated)",
2474                     _adapters->number_of_entries(), (method->is_static() ? "static" : "receiver"),
2475                     method->signature()->as_C_string(), insts_size);
2476       tty->print_cr("c2i argument handler starts at %p",entry->get_c2i_entry());
2477       if (Verbose || PrintStubCode) {
2478         address first_pc = entry->base_address();
2479         if (first_pc != NULL) {
2480           Disassembler::decode(first_pc, first_pc + insts_size);
2481           tty->cr();
2482         }
2483       }
2484     }
2485 #endif
2486 


2487     _adapters->add(entry);
2488   }

2489   // Outside of the lock
2490   if (B != NULL) {
2491     char blob_id[256];
2492     jio_snprintf(blob_id,
2493                  sizeof(blob_id),
2494                  "%s(%s)@" PTR_FORMAT,
2495                  B->name(),
2496                  fingerprint->as_string(),
2497                  B->content_begin());
2498     Forte::register_stub(blob_id, B->content_begin(), B->content_end());
2499 
2500     if (JvmtiExport::should_post_dynamic_code_generated()) {
2501       JvmtiExport::post_dynamic_code_generated(blob_id, B->content_begin(), B->content_end());
2502     }
2503   }
2504   return entry;
2505 }
2506 
2507 address AdapterHandlerEntry::base_address() {
2508   address base = _i2c_entry;
2509   if (base == NULL)  base = _c2i_entry;
2510   assert(base <= _c2i_entry || _c2i_entry == NULL, "");
2511   assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == NULL, "");
2512   return base;
2513 }
2514 
2515 void AdapterHandlerEntry::relocate(address new_base) {
2516   address old_base = base_address();
2517   assert(old_base != NULL, "");
2518   ptrdiff_t delta = new_base - old_base;
2519   if (_i2c_entry != NULL)
2520     _i2c_entry += delta;
2521   if (_c2i_entry != NULL)
2522     _c2i_entry += delta;
2523   if (_c2i_unverified_entry != NULL)
2524     _c2i_unverified_entry += delta;
2525   assert(base_address() == new_base, "");
2526 }
2527 
2528 
2529 void AdapterHandlerEntry::deallocate() {
2530   delete _fingerprint;
2531 #ifdef ASSERT
2532   if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code, mtCode);
2533   if (_saved_sig)  FREE_C_HEAP_ARRAY(Basictype, _saved_sig, mtCode);
2534 #endif
2535 }
2536 
2537 
2538 #ifdef ASSERT
2539 // Capture the code before relocation so that it can be compared
2540 // against other versions.  If the code is captured after relocation
2541 // then relative instructions won't be equivalent.
2542 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
2543   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2544   _code_length = length;
2545   memcpy(_saved_code, buffer, length);
2546   _total_args_passed = total_args_passed;
2547   _saved_sig = NEW_C_HEAP_ARRAY(BasicType, _total_args_passed, mtCode);
2548   memcpy(_saved_sig, sig_bt, _total_args_passed * sizeof(BasicType));
2549 }
2550 
2551 
2552 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length, int total_args_passed, BasicType* sig_bt) {
2553   if (length != _code_length) {
2554     return false;
2555   }
2556   for (int i = 0; i < length; i++) {
2557     if (buffer[i] != _saved_code[i]) {
2558       return false;
2559     }
2560   }
2561   return true;
2562 }
2563 #endif
2564 
2565 
2566 // Create a native wrapper for this native method.  The wrapper converts the
2567 // java compiled calling convention to the native convention, handlizes
2568 // arguments, and transitions to native.  On return from the native we transition
2569 // back to java blocking if a safepoint is in progress.
2570 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) {
2571   ResourceMark rm;
2572   nmethod* nm = NULL;
2573 
2574   assert(method->is_native(), "must be native");
2575   assert(method->is_method_handle_intrinsic() ||
2576          method->has_native_function(), "must have something valid to call!");
2577 
2578   {
2579     // perform the work while holding the lock, but perform any printing outside the lock
2580     MutexLocker mu(AdapterHandlerLibrary_lock);
2581     // See if somebody beat us to it




2357 }
2358 
2359 AdapterHandlerEntry* AdapterHandlerLibrary::get_adapter(methodHandle method) {
2360   // Use customized signature handler.  Need to lock around updates to
2361   // the AdapterHandlerTable (it is not safe for concurrent readers
2362   // and a single writer: this could be fixed if it becomes a
2363   // problem).
2364 
2365   // Get the address of the ic_miss handlers before we grab the
2366   // AdapterHandlerLibrary_lock. This fixes bug 6236259 which
2367   // was caused by the initialization of the stubs happening
2368   // while we held the lock and then notifying jvmti while
2369   // holding it. This just forces the initialization to be a little
2370   // earlier.
2371   address ic_miss = SharedRuntime::get_ic_miss_stub();
2372   assert(ic_miss != NULL, "must have handler");
2373 
2374   ResourceMark rm;
2375 
2376   NOT_PRODUCT(int insts_size);
2377   AdapterBlob* new_adapter = NULL;
2378   AdapterHandlerEntry* entry = NULL;
2379   AdapterFingerPrint* fingerprint = NULL;
2380   {
2381     MutexLocker mu(AdapterHandlerLibrary_lock);
2382     // make sure data structure is initialized
2383     initialize();
2384 
2385     if (method->is_abstract()) {
2386       return _abstract_method_handler;
2387     }
2388 
2389     // Fill in the signature array, for the calling-convention call.
2390     int total_args_passed = method->size_of_parameters(); // All args on stack
2391 
2392     BasicType* sig_bt = NEW_RESOURCE_ARRAY(BasicType, total_args_passed);
2393     VMRegPair* regs   = NEW_RESOURCE_ARRAY(VMRegPair, total_args_passed);
2394     int i = 0;
2395     if (!method->is_static())  // Pass in receiver first
2396       sig_bt[i++] = T_OBJECT;
2397     for (SignatureStream ss(method->signature()); !ss.at_return_type(); ss.next()) {
2398       sig_bt[i++] = ss.type();  // Collect remaining bits of signature
2399       if (ss.type() == T_LONG || ss.type() == T_DOUBLE)
2400         sig_bt[i++] = T_VOID;   // Longs & doubles take 2 Java slots
2401     }
2402     assert(i == total_args_passed, "");
2403 
2404     // Lookup method signature's fingerprint
2405     entry = _adapters->lookup(total_args_passed, sig_bt);
2406 
2407 #ifdef ASSERT
2408     AdapterHandlerEntry* shared_entry = NULL;
2409     // Start adapter sharing verification only after the VM is booted.
2410     if (VerifyAdapterSharing && (entry != NULL)) {
2411       shared_entry = entry;
2412       entry = NULL;
2413     }
2414 #endif
2415 
2416     if (entry != NULL) {
2417       return entry;
2418     }
2419 
2420     // Get a description of the compiled java calling convention and the largest used (VMReg) stack slot usage
2421     int comp_args_on_stack = SharedRuntime::java_calling_convention(sig_bt, regs, total_args_passed, false);
2422 
2423     // Make a C heap allocated version of the fingerprint to store in the adapter
2424     fingerprint = new AdapterFingerPrint(total_args_passed, sig_bt);
2425 
2426     // StubRoutines::code2() is initialized after this function is called. As a result,
2427     // VerifyAdapterCalls and VerifyAdapterSharing can fail if we re-use code that generated
2428     // prior to StubRoutines::code2() being set.
2429     bool contains_all_checks = (StubRoutines::code2() != NULL) || !VerifyAdapterCalls;
2430 
2431     // Create I2C & C2I handlers
2432     BufferBlob* buf = buffer_blob(); // the temporary code buffer in CodeCache
2433     if (buf != NULL) {
2434       CodeBuffer buffer(buf);
2435       short buffer_locs[20];
2436       buffer.insts()->initialize_shared_locs((relocInfo*)buffer_locs,
2437                                              sizeof(buffer_locs)/sizeof(relocInfo));

2438 
2439       MacroAssembler _masm(&buffer);
2440       entry = SharedRuntime::generate_i2c2i_adapters(&_masm,
2441                                                      total_args_passed,
2442                                                      comp_args_on_stack,
2443                                                      sig_bt,
2444                                                      regs,
2445                                                      fingerprint);

2446 #ifdef ASSERT
2447       if (VerifyAdapterSharing) {
2448         if (shared_entry != NULL) {
2449           assert(shared_entry->compare_code(buf->code_begin(), buffer.insts_size()), "code must match");

2450           // Release the one just created and return the original
2451           _adapters->free_entry(entry);
2452           return shared_entry;
2453         } else  {
2454           entry->save_code(buf->code_begin(), buffer.insts_size());
2455         }
2456       }
2457 #endif
2458 
2459       new_adapter = AdapterBlob::create(&buffer);
2460       NOT_PRODUCT(insts_size = buffer.insts_size());
2461     }
2462     if (new_adapter == NULL) {
2463       // CodeCache is full, disable compilation
2464       // Ought to log this but compile log is only per compile thread
2465       // and we're some non descript Java thread.
2466       MutexUnlocker mu(AdapterHandlerLibrary_lock);
2467       CompileBroker::handle_full_code_cache();
2468       return NULL; // Out of CodeCache space
2469     }
2470     entry->relocate(new_adapter->content_begin());
2471 #ifndef PRODUCT
2472     // debugging suppport
2473     if (PrintAdapterHandlers || PrintStubCode) {
2474       ttyLocker ttyl;
2475       entry->print_adapter_on(tty);
2476       tty->print_cr("i2c argument handler #%d for: %s %s (%d bytes generated)",
2477                     _adapters->number_of_entries(), (method->is_static() ? "static" : "receiver"),
2478                     method->signature()->as_C_string(), insts_size);
2479       tty->print_cr("c2i argument handler starts at %p",entry->get_c2i_entry());
2480       if (Verbose || PrintStubCode) {
2481         address first_pc = entry->base_address();
2482         if (first_pc != NULL) {
2483           Disassembler::decode(first_pc, first_pc + insts_size);
2484           tty->cr();
2485         }
2486       }
2487     }
2488 #endif
2489     // Add the entry only if the entry contains all required checks (see sharedRuntime_xxx.cpp)
2490     // The checks are inserted only if -XX:+VerifyAdapterCalls is specified.
2491     if (contains_all_checks) {
2492       _adapters->add(entry);
2493     }
2494   }
2495   // Outside of the lock
2496   if (new_adapter != NULL) {
2497     char blob_id[256];
2498     jio_snprintf(blob_id,
2499                  sizeof(blob_id),
2500                  "%s(%s)@" PTR_FORMAT,
2501                  new_adapter->name(),
2502                  fingerprint->as_string(),
2503                  new_adapter->content_begin());
2504     Forte::register_stub(blob_id, new_adapter->content_begin(),new_adapter->content_end());
2505 
2506     if (JvmtiExport::should_post_dynamic_code_generated()) {
2507       JvmtiExport::post_dynamic_code_generated(blob_id, new_adapter->content_begin(), new_adapter->content_end());
2508     }
2509   }
2510   return entry;
2511 }
2512 
2513 address AdapterHandlerEntry::base_address() {
2514   address base = _i2c_entry;
2515   if (base == NULL)  base = _c2i_entry;
2516   assert(base <= _c2i_entry || _c2i_entry == NULL, "");
2517   assert(base <= _c2i_unverified_entry || _c2i_unverified_entry == NULL, "");
2518   return base;
2519 }
2520 
2521 void AdapterHandlerEntry::relocate(address new_base) {
2522   address old_base = base_address();
2523   assert(old_base != NULL, "");
2524   ptrdiff_t delta = new_base - old_base;
2525   if (_i2c_entry != NULL)
2526     _i2c_entry += delta;
2527   if (_c2i_entry != NULL)
2528     _c2i_entry += delta;
2529   if (_c2i_unverified_entry != NULL)
2530     _c2i_unverified_entry += delta;
2531   assert(base_address() == new_base, "");
2532 }
2533 
2534 
2535 void AdapterHandlerEntry::deallocate() {
2536   delete _fingerprint;
2537 #ifdef ASSERT
2538   if (_saved_code) FREE_C_HEAP_ARRAY(unsigned char, _saved_code, mtCode);

2539 #endif
2540 }
2541 
2542 
2543 #ifdef ASSERT
2544 // Capture the code before relocation so that it can be compared
2545 // against other versions.  If the code is captured after relocation
2546 // then relative instructions won't be equivalent.
2547 void AdapterHandlerEntry::save_code(unsigned char* buffer, int length) {
2548   _saved_code = NEW_C_HEAP_ARRAY(unsigned char, length, mtCode);
2549   _saved_code_length = length;
2550   memcpy(_saved_code, buffer, length);



2551 }
2552 
2553 
2554 bool AdapterHandlerEntry::compare_code(unsigned char* buffer, int length) {
2555   if (length != _saved_code_length) {




2556     return false;
2557   }
2558 
2559   return (memcmp(buffer, _saved_code, length) == 0) ? true : false;
2560 }
2561 #endif
2562 
2563 
2564 // Create a native wrapper for this native method.  The wrapper converts the
2565 // java compiled calling convention to the native convention, handlizes
2566 // arguments, and transitions to native.  On return from the native we transition
2567 // back to java blocking if a safepoint is in progress.
2568 nmethod *AdapterHandlerLibrary::create_native_wrapper(methodHandle method, int compile_id) {
2569   ResourceMark rm;
2570   nmethod* nm = NULL;
2571 
2572   assert(method->is_native(), "must be native");
2573   assert(method->is_method_handle_intrinsic() ||
2574          method->has_native_function(), "must have something valid to call!");
2575 
2576   {
2577     // perform the work while holding the lock, but perform any printing outside the lock
2578     MutexLocker mu(AdapterHandlerLibrary_lock);
2579     // See if somebody beat us to it


src/share/vm/runtime/sharedRuntime.cpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File