1 /*
2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
1870 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
1871 _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
1872
1873 // Allocate private object used as system class loader lock
1874 _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
1875 // Initialize basic classes
1876 initialize_preloaded_classes(CHECK);
1877 }
1878
1879 // Compact table of directions on the initialization of klasses:
1880 static const short wk_init_info[] = {
1881 #define WK_KLASS_INIT_INFO(name, symbol, option) \
1882 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
1883 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
1884 | (int)SystemDictionary::option ),
1885 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
1886 #undef WK_KLASS_INIT_INFO
1887 0
1888 };
1889
1890 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
1891 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1892 int info = wk_init_info[id - FIRST_WKID];
1893 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1894 Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
1895 klassOop* klassp = &_well_known_klasses[id];
1896 bool must_load = (init_opt < SystemDictionary::Opt);
1897 bool try_load = true;
1898 if (init_opt == SystemDictionary::Opt_Kernel) {
1899 #ifndef KERNEL
1900 try_load = false;
1901 #endif //KERNEL
1902 }
1903 if ((*klassp) == NULL && try_load) {
1904 if (must_load) {
1905 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
1906 } else {
1907 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
1908 }
1909 }
1910 return ((*klassp) != NULL);
1911 }
1912
1913 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
1914 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
1915 for (int id = (int)start_id; id < (int)limit_id; id++) {
1916 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1917 int info = wk_init_info[id - FIRST_WKID];
1918 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1919 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
1920
1921 initialize_wk_klass((WKID)id, opt, CHECK);
1922
1923 // Update limits, so find_well_known_klass can be very fast:
1924 Symbol* s = vmSymbols::symbol_at((vmSymbols::SID)sid);
1925 if (wk_klass_name_limits[1] == NULL) {
1926 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s;
1927 } else if (wk_klass_name_limits[1] < s) {
1928 wk_klass_name_limits[1] = s;
1929 } else if (wk_klass_name_limits[0] > s) {
1930 wk_klass_name_limits[0] = s;
2331 }
2332
2333
2334 methodOop SystemDictionary::find_method_handle_invoke(Symbol* name,
2335 Symbol* signature,
2336 KlassHandle accessing_klass,
2337 TRAPS) {
2338 if (!EnableMethodHandles) return NULL;
2339 vmSymbols::SID name_id = vmSymbols::find_sid(name);
2340 assert(name_id != vmSymbols::NO_SID, "must be a known name");
2341 unsigned int hash = invoke_method_table()->compute_hash(signature, name_id);
2342 int index = invoke_method_table()->hash_to_index(hash);
2343 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
2344 methodHandle non_cached_result;
2345 if (spe == NULL || spe->property_oop() == NULL) {
2346 spe = NULL;
2347 // Must create lots of stuff here, but outside of the SystemDictionary lock.
2348 if (THREAD->is_Compiler_thread())
2349 return NULL; // do not attempt from within compiler
2350 bool for_invokeGeneric = (name_id == vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name));
2351 bool found_on_bcp = false;
2352 Handle mt = find_method_handle_type(signature, accessing_klass,
2353 for_invokeGeneric,
2354 found_on_bcp, CHECK_NULL);
2355 KlassHandle mh_klass = SystemDictionaryHandles::MethodHandle_klass();
2356 methodHandle m = methodOopDesc::make_invoke_method(mh_klass, name, signature,
2357 mt, CHECK_NULL);
2358 // Now grab the lock. We might have to throw away the new method,
2359 // if a racing thread has managed to install one at the same time.
2360 if (found_on_bcp) {
2361 MutexLocker ml(SystemDictionary_lock, Thread::current());
2362 spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
2363 if (spe == NULL)
2364 spe = invoke_method_table()->add_entry(index, hash, signature, name_id);
2365 if (spe->property_oop() == NULL)
2366 spe->set_property_oop(m());
2367 } else {
2368 non_cached_result = m;
2369 }
2370 }
2514 methodHandle caller_method,
2515 int caller_bci,
2516 TRAPS) {
2517 Handle empty;
2518 guarantee(bootstrap_method.not_null() &&
2519 java_dyn_MethodHandle::is_instance(bootstrap_method()),
2520 "caller must supply a valid BSM");
2521
2522 Handle caller_mname = MethodHandles::new_MemberName(CHECK_(empty));
2523 MethodHandles::init_MemberName(caller_mname(), caller_method());
2524
2525 // call sun.dyn.MethodHandleNatives::makeDynamicCallSite(bootm, name, mtype, info, caller_mname, caller_pos)
2526 oop name_str_oop = StringTable::intern(name, CHECK_(empty)); // not a handle!
2527 JavaCallArguments args(Handle(THREAD, bootstrap_method()));
2528 args.push_oop(name_str_oop);
2529 args.push_oop(signature_invoker->method_handle_type());
2530 args.push_oop(info());
2531 args.push_oop(caller_mname());
2532 args.push_int(caller_bci);
2533 JavaValue result(T_OBJECT);
2534 JavaCalls::call_static(&result,
2535 SystemDictionary::MethodHandleNatives_klass(),
2536 vmSymbols::makeDynamicCallSite_name(),
2537 vmSymbols::makeDynamicCallSite_signature(),
2538 &args, CHECK_(empty));
2539 oop call_site_oop = (oop) result.get_jobject();
2540 assert(call_site_oop->is_oop()
2541 /*&& java_dyn_CallSite::is_instance(call_site_oop)*/, "must be sane");
2542 if (TraceMethodHandles) {
2543 #ifndef PRODUCT
2544 tty->print_cr("Linked invokedynamic bci=%d site="INTPTR_FORMAT":", caller_bci, call_site_oop);
2545 call_site_oop->print();
2546 tty->cr();
2547 #endif //PRODUCT
2548 }
2549 return call_site_oop;
2550 }
2551
2552 Handle SystemDictionary::find_bootstrap_method(methodHandle caller_method, int caller_bci,
2553 int cache_index,
2554 Handle& argument_info_result,
2555 TRAPS) {
2556 Handle empty;
2557
|
1 /*
2 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
1870 _resolution_errors = new ResolutionErrorTable(_resolution_error_size);
1871 _invoke_method_table = new SymbolPropertyTable(_invoke_method_size);
1872
1873 // Allocate private object used as system class loader lock
1874 _system_loader_lock_obj = oopFactory::new_system_objArray(0, CHECK);
1875 // Initialize basic classes
1876 initialize_preloaded_classes(CHECK);
1877 }
1878
1879 // Compact table of directions on the initialization of klasses:
1880 static const short wk_init_info[] = {
1881 #define WK_KLASS_INIT_INFO(name, symbol, option) \
1882 ( ((int)vmSymbols::VM_SYMBOL_ENUM_NAME(symbol) \
1883 << SystemDictionary::CEIL_LG_OPTION_LIMIT) \
1884 | (int)SystemDictionary::option ),
1885 WK_KLASSES_DO(WK_KLASS_INIT_INFO)
1886 #undef WK_KLASS_INIT_INFO
1887 0
1888 };
1889
1890 Symbol* SystemDictionary::find_backup_symbol(Symbol* symbol,
1891 const char* from_prefix,
1892 const char* to_prefix) {
1893 assert(AllowTransitionalJSR292, ""); // delete this subroutine
1894 Symbol* backup_symbol = NULL;
1895 size_t from_len = strlen(from_prefix);
1896 if (strncmp((const char*) symbol->base(), from_prefix, from_len) != 0)
1897 return NULL;
1898 char buf[100];
1899 size_t to_len = strlen(to_prefix);
1900 size_t tail_len = symbol->utf8_length() - from_len;
1901 size_t new_len = to_len + tail_len;
1902 guarantee(new_len < sizeof(buf), "buf too small");
1903 memcpy(buf, to_prefix, to_len);
1904 memcpy(buf + to_len, symbol->base() + from_len, tail_len);
1905 buf[new_len] = '\0';
1906 vmSymbols::SID backup_sid = vmSymbols::find_sid(buf);
1907 if (backup_sid != vmSymbols::NO_SID) {
1908 backup_symbol = vmSymbols::symbol_at(backup_sid);
1909 }
1910 return backup_symbol;
1911 }
1912
1913 Symbol* SystemDictionary::find_backup_class_name(Symbol* symbol) {
1914 assert(AllowTransitionalJSR292, ""); // delete this subroutine
1915 if (symbol == NULL) return NULL;
1916 Symbol* backup_symbol = find_backup_symbol(symbol, "java/lang/invoke/", "java/dyn/"); // AllowTransitionalJSR292 ONLY
1917 if (backup_symbol == NULL)
1918 backup_symbol = find_backup_symbol(symbol, "java/dyn/", "sun/dyn/"); // AllowTransitionalJSR292 ONLY
1919 return backup_symbol;
1920 }
1921
1922 Symbol* SystemDictionary::find_backup_signature(Symbol* symbol) {
1923 assert(AllowTransitionalJSR292, ""); // delete this subroutine
1924 if (symbol == NULL) return NULL;
1925 return find_backup_symbol(symbol, "Ljava/lang/invoke/", "Ljava/dyn/");
1926 }
1927
1928 bool SystemDictionary::initialize_wk_klass(WKID id, int init_opt, TRAPS) {
1929 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1930 int info = wk_init_info[id - FIRST_WKID];
1931 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1932 Symbol* symbol = vmSymbols::symbol_at((vmSymbols::SID)sid);
1933 klassOop* klassp = &_well_known_klasses[id];
1934 bool pre_load = (init_opt < SystemDictionary::Opt);
1935 bool try_load = true;
1936 if (init_opt == SystemDictionary::Opt_Kernel) {
1937 #ifndef KERNEL
1938 try_load = false;
1939 #endif //KERNEL
1940 }
1941 Symbol* backup_symbol = NULL; // symbol to try if the current symbol fails
1942 if (init_opt == SystemDictionary::Pre_JSR292) {
1943 if (!EnableMethodHandles) try_load = false; // do not bother to load such classes
1944 if (AllowTransitionalJSR292) {
1945 backup_symbol = find_backup_class_name(symbol);
1946 if (try_load && PreferTransitionalJSR292) {
1947 while (backup_symbol != NULL) {
1948 (*klassp) = resolve_or_null(backup_symbol, CHECK_0); // try backup early
1949 if (TraceMethodHandles) {
1950 ResourceMark rm;
1951 tty->print_cr("MethodHandles: try backup first for %s => %s (%s)",
1952 symbol->as_C_string(), backup_symbol->as_C_string(),
1953 ((*klassp) == NULL) ? "no such class" : "backup load succeeded");
1954 }
1955 if ((*klassp) != NULL) return true;
1956 backup_symbol = find_backup_class_name(backup_symbol); // find next backup
1957 }
1958 }
1959 }
1960 }
1961 if ((*klassp) != NULL) return true;
1962 if (!try_load) return false;
1963 while (symbol != NULL) {
1964 bool must_load = (pre_load && (backup_symbol == NULL));
1965 if (must_load) {
1966 (*klassp) = resolve_or_fail(symbol, true, CHECK_0); // load required class
1967 } else {
1968 (*klassp) = resolve_or_null(symbol, CHECK_0); // load optional klass
1969 }
1970 if ((*klassp) != NULL) return true;
1971 // Go around again. Example of long backup sequence:
1972 // java.lang.invoke.MemberName, java.dyn.MemberName, sun.dyn.MemberName, ONLY if AllowTransitionalJSR292
1973 if (TraceMethodHandles && (backup_symbol != NULL)) {
1974 ResourceMark rm;
1975 tty->print_cr("MethodHandles: backup for %s => %s",
1976 symbol->as_C_string(), backup_symbol->as_C_string());
1977 }
1978 symbol = backup_symbol;
1979 if (AllowTransitionalJSR292)
1980 backup_symbol = find_backup_class_name(symbol);
1981 }
1982 return false;
1983 }
1984
1985 void SystemDictionary::initialize_wk_klasses_until(WKID limit_id, WKID &start_id, TRAPS) {
1986 assert((int)start_id <= (int)limit_id, "IDs are out of order!");
1987 for (int id = (int)start_id; id < (int)limit_id; id++) {
1988 assert(id >= (int)FIRST_WKID && id < (int)WKID_LIMIT, "oob");
1989 int info = wk_init_info[id - FIRST_WKID];
1990 int sid = (info >> CEIL_LG_OPTION_LIMIT);
1991 int opt = (info & right_n_bits(CEIL_LG_OPTION_LIMIT));
1992
1993 initialize_wk_klass((WKID)id, opt, CHECK);
1994
1995 // Update limits, so find_well_known_klass can be very fast:
1996 Symbol* s = vmSymbols::symbol_at((vmSymbols::SID)sid);
1997 if (wk_klass_name_limits[1] == NULL) {
1998 wk_klass_name_limits[0] = wk_klass_name_limits[1] = s;
1999 } else if (wk_klass_name_limits[1] < s) {
2000 wk_klass_name_limits[1] = s;
2001 } else if (wk_klass_name_limits[0] > s) {
2002 wk_klass_name_limits[0] = s;
2403 }
2404
2405
2406 methodOop SystemDictionary::find_method_handle_invoke(Symbol* name,
2407 Symbol* signature,
2408 KlassHandle accessing_klass,
2409 TRAPS) {
2410 if (!EnableMethodHandles) return NULL;
2411 vmSymbols::SID name_id = vmSymbols::find_sid(name);
2412 assert(name_id != vmSymbols::NO_SID, "must be a known name");
2413 unsigned int hash = invoke_method_table()->compute_hash(signature, name_id);
2414 int index = invoke_method_table()->hash_to_index(hash);
2415 SymbolPropertyEntry* spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
2416 methodHandle non_cached_result;
2417 if (spe == NULL || spe->property_oop() == NULL) {
2418 spe = NULL;
2419 // Must create lots of stuff here, but outside of the SystemDictionary lock.
2420 if (THREAD->is_Compiler_thread())
2421 return NULL; // do not attempt from within compiler
2422 bool for_invokeGeneric = (name_id == vmSymbols::VM_SYMBOL_ENUM_NAME(invokeGeneric_name));
2423 if (AllowInvokeForInvokeGeneric && name_id == vmSymbols::VM_SYMBOL_ENUM_NAME(invoke_name))
2424 for_invokeGeneric = true;
2425 bool found_on_bcp = false;
2426 Handle mt = find_method_handle_type(signature, accessing_klass,
2427 for_invokeGeneric,
2428 found_on_bcp, CHECK_NULL);
2429 KlassHandle mh_klass = SystemDictionaryHandles::MethodHandle_klass();
2430 methodHandle m = methodOopDesc::make_invoke_method(mh_klass, name, signature,
2431 mt, CHECK_NULL);
2432 // Now grab the lock. We might have to throw away the new method,
2433 // if a racing thread has managed to install one at the same time.
2434 if (found_on_bcp) {
2435 MutexLocker ml(SystemDictionary_lock, Thread::current());
2436 spe = invoke_method_table()->find_entry(index, hash, signature, name_id);
2437 if (spe == NULL)
2438 spe = invoke_method_table()->add_entry(index, hash, signature, name_id);
2439 if (spe->property_oop() == NULL)
2440 spe->set_property_oop(m());
2441 } else {
2442 non_cached_result = m;
2443 }
2444 }
2588 methodHandle caller_method,
2589 int caller_bci,
2590 TRAPS) {
2591 Handle empty;
2592 guarantee(bootstrap_method.not_null() &&
2593 java_dyn_MethodHandle::is_instance(bootstrap_method()),
2594 "caller must supply a valid BSM");
2595
2596 Handle caller_mname = MethodHandles::new_MemberName(CHECK_(empty));
2597 MethodHandles::init_MemberName(caller_mname(), caller_method());
2598
2599 // call sun.dyn.MethodHandleNatives::makeDynamicCallSite(bootm, name, mtype, info, caller_mname, caller_pos)
2600 oop name_str_oop = StringTable::intern(name, CHECK_(empty)); // not a handle!
2601 JavaCallArguments args(Handle(THREAD, bootstrap_method()));
2602 args.push_oop(name_str_oop);
2603 args.push_oop(signature_invoker->method_handle_type());
2604 args.push_oop(info());
2605 args.push_oop(caller_mname());
2606 args.push_int(caller_bci);
2607 JavaValue result(T_OBJECT);
2608 Symbol* makeDynamicCallSite_signature = vmSymbols::makeDynamicCallSite_signature();
2609 if (AllowTransitionalJSR292 && SystemDictionaryHandles::MethodHandleNatives_klass()->name() == vmSymbols::sun_dyn_MethodHandleNatives()) {
2610 makeDynamicCallSite_signature = vmSymbols::makeDynamicCallSite_TRANS_signature();
2611 }
2612 JavaCalls::call_static(&result,
2613 SystemDictionary::MethodHandleNatives_klass(),
2614 vmSymbols::makeDynamicCallSite_name(),
2615 makeDynamicCallSite_signature,
2616 &args, CHECK_(empty));
2617 oop call_site_oop = (oop) result.get_jobject();
2618 assert(call_site_oop->is_oop()
2619 /*&& java_dyn_CallSite::is_instance(call_site_oop)*/, "must be sane");
2620 if (TraceMethodHandles) {
2621 #ifndef PRODUCT
2622 tty->print_cr("Linked invokedynamic bci=%d site="INTPTR_FORMAT":", caller_bci, call_site_oop);
2623 call_site_oop->print();
2624 tty->cr();
2625 #endif //PRODUCT
2626 }
2627 return call_site_oop;
2628 }
2629
2630 Handle SystemDictionary::find_bootstrap_method(methodHandle caller_method, int caller_bci,
2631 int cache_index,
2632 Handle& argument_info_result,
2633 TRAPS) {
2634 Handle empty;
2635
|