duke@435: /* coleenp@4466: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "classfile/classLoader.hpp" stefank@2314: #include "classfile/javaAssertions.hpp" stefank@2314: #include "classfile/javaClasses.hpp" stefank@2314: #include "classfile/symbolTable.hpp" stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "classfile/vmSymbols.hpp" stefank@2314: #include "gc_interface/collectedHeap.inline.hpp" twisti@4866: #include "interpreter/bytecode.hpp" stefank@2314: #include "memory/oopFactory.hpp" stefank@2314: #include "memory/universe.inline.hpp" never@3137: #include "oops/fieldStreams.hpp" stefank@2314: #include "oops/instanceKlass.hpp" stefank@2314: #include "oops/objArrayKlass.hpp" coleenp@4037: #include "oops/method.hpp" stefank@2314: #include "prims/jvm.h" stefank@2314: #include "prims/jvm_misc.hpp" stefank@2314: #include "prims/jvmtiExport.hpp" stefank@2314: #include "prims/jvmtiThreadState.hpp" stefank@2314: #include "prims/nativeLookup.hpp" stefank@2314: #include "prims/privilegedStack.hpp" stefank@2314: #include "runtime/arguments.hpp" stefank@2314: #include "runtime/dtraceJSDT.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/init.hpp" stefank@2314: #include "runtime/interfaceSupport.hpp" stefank@2314: #include "runtime/java.hpp" stefank@2314: #include "runtime/javaCalls.hpp" stefank@2314: #include "runtime/jfieldIDWorkaround.hpp" stefank@2314: #include "runtime/os.hpp" stefank@2314: #include "runtime/perfData.hpp" stefank@2314: #include "runtime/reflection.hpp" stefank@2314: #include "runtime/vframe.hpp" stefank@2314: #include "runtime/vm_operations.hpp" stefank@2314: #include "services/attachListener.hpp" stefank@2314: #include "services/management.hpp" stefank@2314: #include "services/threadService.hpp" stefank@2314: #include "utilities/copy.hpp" stefank@2314: #include "utilities/defaultStream.hpp" stefank@2314: #include "utilities/dtrace.hpp" stefank@2314: #include "utilities/events.hpp" stefank@2314: #include "utilities/histogram.hpp" stefank@2314: #include "utilities/top.hpp" stefank@2314: #include "utilities/utf8.hpp" stefank@2314: #ifdef TARGET_OS_FAMILY_linux stefank@2314: # include "jvm_linux.h" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_solaris stefank@2314: # include "jvm_solaris.h" stefank@2314: #endif stefank@2314: #ifdef TARGET_OS_FAMILY_windows stefank@2314: # include "jvm_windows.h" stefank@2314: #endif never@3156: #ifdef TARGET_OS_FAMILY_bsd never@3156: # include "jvm_bsd.h" never@3156: #endif stefank@2314: duke@435: #include duke@435: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__begin, long long); fparain@1759: HS_DTRACE_PROBE_DECL1(hotspot, thread__sleep__end, int); fparain@1759: HS_DTRACE_PROBE_DECL0(hotspot, thread__yield); dcubed@3202: #endif /* !USDT2 */ fparain@1759: duke@435: /* duke@435: NOTE about use of any ctor or function call that can trigger a safepoint/GC: duke@435: such ctors and calls MUST NOT come between an oop declaration/init and its duke@435: usage because if objects are move this may cause various memory stomps, bus duke@435: errors and segfaults. Here is a cookbook for causing so called "naked oop duke@435: failures": duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields { duke@435: JVMWrapper("JVM_GetClassDeclaredFields"); duke@435: duke@435: // Object address to be held directly in mirror & not visible to GC duke@435: oop mirror = JNIHandles::resolve_non_null(ofClass); duke@435: duke@435: // If this ctor can hit a safepoint, moving objects around, then duke@435: ComplexConstructor foo; duke@435: duke@435: // Boom! mirror may point to JUNK instead of the intended object duke@435: (some dereference of mirror) duke@435: duke@435: // Here's another call that may block for GC, making mirror stale duke@435: MutexLocker ml(some_lock); duke@435: duke@435: // And here's an initializer that can result in a stale oop duke@435: // all in one step. duke@435: oop o = call_that_can_throw_exception(TRAPS); duke@435: duke@435: duke@435: The solution is to keep the oop declaration BELOW the ctor or function duke@435: call that might cause a GC, do another resolve to reassign the oop, or duke@435: consider use of a Handle instead of an oop so there is immunity from object duke@435: motion. But note that the "QUICK" entries below do not have a handlemark duke@435: and thus can only support use of handles passed in. duke@435: */ duke@435: coleenp@4037: static void trace_class_resolution_impl(Klass* to_class, TRAPS) { duke@435: ResourceMark rm; duke@435: int line_number = -1; duke@435: const char * source_file = NULL; acorn@1092: const char * trace = "explicit"; coleenp@4251: InstanceKlass* caller = NULL; duke@435: JavaThread* jthread = JavaThread::current(); duke@435: if (jthread->has_last_Java_frame()) { duke@435: vframeStream vfst(jthread); duke@435: duke@435: // scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames coleenp@2497: TempNewSymbol access_controller = SymbolTable::new_symbol("java/security/AccessController", CHECK); coleenp@4037: Klass* access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK); coleenp@2497: TempNewSymbol privileged_action = SymbolTable::new_symbol("java/security/PrivilegedAction", CHECK); coleenp@4037: Klass* privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK); coleenp@4037: coleenp@4037: Method* last_caller = NULL; duke@435: duke@435: while (!vfst.at_end()) { coleenp@4037: Method* m = vfst.method(); coleenp@4037: if (!vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass())&& coleenp@4037: !vfst.method()->method_holder()->is_subclass_of(access_controller_klass) && coleenp@4037: !vfst.method()->method_holder()->is_subclass_of(privileged_action_klass)) { duke@435: break; duke@435: } duke@435: last_caller = m; duke@435: vfst.next(); duke@435: } duke@435: // if this is called from Class.forName0 and that is called from Class.forName, duke@435: // then print the caller of Class.forName. If this is Class.loadClass, then print duke@435: // that caller, otherwise keep quiet since this should be picked up elsewhere. duke@435: bool found_it = false; duke@435: if (!vfst.at_end() && coleenp@4251: vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() && duke@435: vfst.method()->name() == vmSymbols::forName0_name()) { duke@435: vfst.next(); duke@435: if (!vfst.at_end() && coleenp@4251: vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() && duke@435: vfst.method()->name() == vmSymbols::forName_name()) { duke@435: vfst.next(); duke@435: found_it = true; duke@435: } duke@435: } else if (last_caller != NULL && coleenp@4251: last_caller->method_holder()->name() == duke@435: vmSymbols::java_lang_ClassLoader() && duke@435: (last_caller->name() == vmSymbols::loadClassInternal_name() || duke@435: last_caller->name() == vmSymbols::loadClass_name())) { duke@435: found_it = true; acorn@1092: } else if (!vfst.at_end()) { acorn@1092: if (vfst.method()->is_native()) { acorn@1092: // JNI call acorn@1092: found_it = true; acorn@1092: } duke@435: } duke@435: if (found_it && !vfst.at_end()) { duke@435: // found the caller duke@435: caller = vfst.method()->method_holder(); duke@435: line_number = vfst.method()->line_number_from_bci(vfst.bci()); acorn@1092: if (line_number == -1) { acorn@1092: // show method name if it's a native method acorn@1092: trace = vfst.method()->name_and_sig_as_C_string(); acorn@1092: } coleenp@4251: Symbol* s = caller->source_file_name(); duke@435: if (s != NULL) { duke@435: source_file = s->as_C_string(); duke@435: } duke@435: } duke@435: } duke@435: if (caller != NULL) { duke@435: if (to_class != caller) { coleenp@4251: const char * from = caller->external_name(); coleenp@4251: const char * to = to_class->external_name(); duke@435: // print in a single call to reduce interleaving between threads duke@435: if (source_file != NULL) { acorn@1092: tty->print("RESOLVE %s %s %s:%d (%s)\n", from, to, source_file, line_number, trace); duke@435: } else { acorn@1092: tty->print("RESOLVE %s %s (%s)\n", from, to, trace); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: coleenp@4037: void trace_class_resolution(Klass* to_class) { duke@435: EXCEPTION_MARK; duke@435: trace_class_resolution_impl(to_class, THREAD); duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: CLEAR_PENDING_EXCEPTION; duke@435: } duke@435: } duke@435: duke@435: // Wrapper to trace JVM functions duke@435: duke@435: #ifdef ASSERT duke@435: class JVMTraceWrapper : public StackObj { duke@435: public: duke@435: JVMTraceWrapper(const char* format, ...) { duke@435: if (TraceJVMCalls) { duke@435: va_list ap; duke@435: va_start(ap, format); duke@435: tty->print("JVM "); duke@435: tty->vprint_cr(format, ap); duke@435: va_end(ap); duke@435: } duke@435: } duke@435: }; duke@435: duke@435: Histogram* JVMHistogram; duke@435: volatile jint JVMHistogram_lock = 0; duke@435: duke@435: class JVMHistogramElement : public HistogramElement { duke@435: public: duke@435: JVMHistogramElement(const char* name); duke@435: }; duke@435: duke@435: JVMHistogramElement::JVMHistogramElement(const char* elementName) { duke@435: _name = elementName; duke@435: uintx count = 0; duke@435: duke@435: while (Atomic::cmpxchg(1, &JVMHistogram_lock, 0) != 0) { duke@435: while (OrderAccess::load_acquire(&JVMHistogram_lock) != 0) { duke@435: count +=1; duke@435: if ( (WarnOnStalledSpinLock > 0) duke@435: && (count % WarnOnStalledSpinLock == 0)) { duke@435: warning("JVMHistogram_lock seems to be stalled"); duke@435: } duke@435: } duke@435: } duke@435: duke@435: if(JVMHistogram == NULL) duke@435: JVMHistogram = new Histogram("JVM Call Counts",100); duke@435: duke@435: JVMHistogram->add_element(this); duke@435: Atomic::dec(&JVMHistogram_lock); duke@435: } duke@435: duke@435: #define JVMCountWrapper(arg) \ duke@435: static JVMHistogramElement* e = new JVMHistogramElement(arg); \ duke@435: if (e != NULL) e->increment_count(); // Due to bug in VC++, we need a NULL check here eventhough it should never happen! duke@435: duke@435: #define JVMWrapper(arg1) JVMCountWrapper(arg1); JVMTraceWrapper(arg1) duke@435: #define JVMWrapper2(arg1, arg2) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2) duke@435: #define JVMWrapper3(arg1, arg2, arg3) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3) duke@435: #define JVMWrapper4(arg1, arg2, arg3, arg4) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3, arg4) duke@435: #else duke@435: #define JVMWrapper(arg1) duke@435: #define JVMWrapper2(arg1, arg2) duke@435: #define JVMWrapper3(arg1, arg2, arg3) duke@435: #define JVMWrapper4(arg1, arg2, arg3, arg4) duke@435: #endif duke@435: duke@435: duke@435: // Interface version ///////////////////////////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_GetInterfaceVersion()) duke@435: return JVM_INTERFACE_VERSION; duke@435: JVM_END duke@435: duke@435: duke@435: // java.lang.System ////////////////////////////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored)) duke@435: JVMWrapper("JVM_CurrentTimeMillis"); duke@435: return os::javaTimeMillis(); duke@435: JVM_END duke@435: duke@435: JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored)) duke@435: JVMWrapper("JVM_NanoTime"); duke@435: return os::javaTimeNanos(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos, duke@435: jobject dst, jint dst_pos, jint length)) duke@435: JVMWrapper("JVM_ArrayCopy"); duke@435: // Check if we have null pointers duke@435: if (src == NULL || dst == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: arrayOop s = arrayOop(JNIHandles::resolve_non_null(src)); duke@435: arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst)); duke@435: assert(s->is_oop(), "JVM_ArrayCopy: src not an oop"); duke@435: assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop"); duke@435: // Do copy hseigel@4278: s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread); duke@435: JVM_END duke@435: duke@435: duke@435: static void set_property(Handle props, const char* key, const char* value, TRAPS) { duke@435: JavaValue r(T_OBJECT); duke@435: // public synchronized Object put(Object key, Object value); duke@435: HandleMark hm(THREAD); duke@435: Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK); duke@435: Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK); duke@435: JavaCalls::call_virtual(&r, duke@435: props, never@1577: KlassHandle(THREAD, SystemDictionary::Properties_klass()), coleenp@2497: vmSymbols::put_name(), coleenp@2497: vmSymbols::object_object_object_signature(), duke@435: key_str, duke@435: value_str, duke@435: THREAD); duke@435: } duke@435: duke@435: duke@435: #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties)); duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties)) duke@435: JVMWrapper("JVM_InitProperties"); duke@435: ResourceMark rm; duke@435: duke@435: Handle props(THREAD, JNIHandles::resolve_non_null(properties)); duke@435: duke@435: // System property list includes both user set via -D option and duke@435: // jvm system specific properties. duke@435: for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) { duke@435: PUTPROP(props, p->key(), p->value()); duke@435: } duke@435: duke@435: // Convert the -XX:MaxDirectMemorySize= command line flag duke@435: // to the sun.nio.MaxDirectMemorySize property. duke@435: // Do this after setting user properties to prevent people duke@435: // from setting the value with a -D option, as requested. duke@435: { dholmes@3902: if (FLAG_IS_DEFAULT(MaxDirectMemorySize)) { dholmes@3902: PUTPROP(props, "sun.nio.MaxDirectMemorySize", "-1"); dholmes@3902: } else { dholmes@3902: char as_chars[256]; dholmes@3902: jio_snprintf(as_chars, sizeof(as_chars), UINTX_FORMAT, MaxDirectMemorySize); dholmes@3902: PUTPROP(props, "sun.nio.MaxDirectMemorySize", as_chars); dholmes@3902: } duke@435: } duke@435: duke@435: // JVM monitoring and management support duke@435: // Add the sun.management.compiler property for the compiler's name duke@435: { duke@435: #undef CSIZE duke@435: #if defined(_LP64) || defined(_WIN64) duke@435: #define CSIZE "64-Bit " duke@435: #else duke@435: #define CSIZE duke@435: #endif // 64bit duke@435: duke@435: #ifdef TIERED duke@435: const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers"; duke@435: #else duke@435: #if defined(COMPILER1) duke@435: const char* compiler_name = "HotSpot " CSIZE "Client Compiler"; duke@435: #elif defined(COMPILER2) duke@435: const char* compiler_name = "HotSpot " CSIZE "Server Compiler"; duke@435: #else duke@435: const char* compiler_name = ""; duke@435: #endif // compilers duke@435: #endif // TIERED duke@435: duke@435: if (*compiler_name != '\0' && duke@435: (Arguments::mode() != Arguments::_int)) { duke@435: PUTPROP(props, "sun.management.compiler", compiler_name); duke@435: } duke@435: } duke@435: duke@435: return properties; duke@435: JVM_END duke@435: duke@435: duke@435: // java.lang.Runtime ///////////////////////////////////////////////////////////////////////// duke@435: duke@435: extern volatile jint vm_created; duke@435: duke@435: JVM_ENTRY_NO_ENV(void, JVM_Exit(jint code)) duke@435: if (vm_created != 0 && (code == 0)) { duke@435: // The VM is about to exit. We call back into Java to check whether finalizers should be run duke@435: Universe::run_finalizers_on_exit(); duke@435: } duke@435: before_exit(thread); duke@435: vm_exit(code); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code)) duke@435: before_exit(thread); duke@435: vm_exit(code); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_OnExit(void (*func)(void))) duke@435: register_on_exit_function(func); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY_NO_ENV(void, JVM_GC(void)) duke@435: JVMWrapper("JVM_GC"); duke@435: if (!DisableExplicitGC) { duke@435: Universe::heap()->collect(GCCause::_java_lang_system_gc); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void)) duke@435: JVMWrapper("JVM_MaxObjectInspectionAge"); duke@435: return Universe::heap()->millis_since_last_gc(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_TraceInstructions(jboolean on)) duke@435: if (PrintJVMWarnings) warning("JVM_TraceInstructions not supported"); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_TraceMethodCalls(jboolean on)) duke@435: if (PrintJVMWarnings) warning("JVM_TraceMethodCalls not supported"); duke@435: JVM_END duke@435: duke@435: static inline jlong convert_size_t_to_jlong(size_t val) { duke@435: // In the 64-bit vm, a size_t can overflow a jlong (which is signed). duke@435: NOT_LP64 (return (jlong)val;) duke@435: LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);) duke@435: } duke@435: duke@435: JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void)) duke@435: JVMWrapper("JVM_TotalMemory"); duke@435: size_t n = Universe::heap()->capacity(); duke@435: return convert_size_t_to_jlong(n); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void)) duke@435: JVMWrapper("JVM_FreeMemory"); duke@435: CollectedHeap* ch = Universe::heap(); ysr@777: size_t n; ysr@777: { ysr@777: MutexLocker x(Heap_lock); ysr@777: n = ch->capacity() - ch->used(); ysr@777: } duke@435: return convert_size_t_to_jlong(n); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void)) duke@435: JVMWrapper("JVM_MaxMemory"); duke@435: size_t n = Universe::heap()->max_capacity(); duke@435: return convert_size_t_to_jlong(n); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void)) duke@435: JVMWrapper("JVM_ActiveProcessorCount"); duke@435: return os::active_processor_count(); duke@435: JVM_END duke@435: duke@435: duke@435: duke@435: // java.lang.Throwable ////////////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver)) duke@435: JVMWrapper("JVM_FillInStackTrace"); duke@435: Handle exception(thread, JNIHandles::resolve_non_null(receiver)); duke@435: java_lang_Throwable::fill_in_stack_trace(exception); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable)) duke@435: JVMWrapper("JVM_GetStackTraceDepth"); duke@435: oop exception = JNIHandles::resolve(throwable); duke@435: return java_lang_Throwable::get_stack_trace_depth(exception, THREAD); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index)) duke@435: JVMWrapper("JVM_GetStackTraceElement"); duke@435: JvmtiVMObjectAllocEventCollector oam; // This ctor (throughout this module) may trigger a safepoint/GC duke@435: oop exception = JNIHandles::resolve(throwable); duke@435: oop element = java_lang_Throwable::get_stack_trace_element(exception, index, CHECK_NULL); duke@435: return JNIHandles::make_local(env, element); duke@435: JVM_END duke@435: duke@435: duke@435: // java.lang.Object /////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle)) duke@435: JVMWrapper("JVM_IHashCode"); duke@435: // as implemented in the classic virtual machine; return 0 if object is NULL duke@435: return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms)) duke@435: JVMWrapper("JVM_MonitorWait"); duke@435: Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); duke@435: JavaThreadInObjectWaitState jtiows(thread, ms != 0); duke@435: if (JvmtiExport::should_post_monitor_wait()) { duke@435: JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms); duke@435: } duke@435: ObjectSynchronizer::wait(obj, ms, CHECK); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle)) duke@435: JVMWrapper("JVM_MonitorNotify"); duke@435: Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); duke@435: ObjectSynchronizer::notify(obj, CHECK); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle)) duke@435: JVMWrapper("JVM_MonitorNotifyAll"); duke@435: Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); duke@435: ObjectSynchronizer::notifyall(obj, CHECK); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle)) duke@435: JVMWrapper("JVM_Clone"); duke@435: Handle obj(THREAD, JNIHandles::resolve_non_null(handle)); duke@435: const KlassHandle klass (THREAD, obj->klass()); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: #ifdef ASSERT duke@435: // Just checking that the cloneable flag is set correct coleenp@4037: if (obj->is_array()) { duke@435: guarantee(klass->is_cloneable(), "all arrays are cloneable"); duke@435: } else { duke@435: guarantee(obj->is_instance(), "should be instanceOop"); never@1577: bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass()); duke@435: guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag"); duke@435: } duke@435: #endif duke@435: duke@435: // Check if class of obj supports the Cloneable interface. duke@435: // All arrays are considered to be cloneable (See JLS 20.1.5) duke@435: if (!klass->is_cloneable()) { duke@435: ResourceMark rm(THREAD); duke@435: THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name()); duke@435: } duke@435: duke@435: // Make shallow object copy duke@435: const int size = obj->size(); duke@435: oop new_obj = NULL; coleenp@4037: if (obj->is_array()) { duke@435: const int length = ((arrayOop)obj())->length(); duke@435: new_obj = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL); duke@435: } else { duke@435: new_obj = CollectedHeap::obj_allocate(klass, size, CHECK_NULL); duke@435: } duke@435: // 4839641 (4840070): We must do an oop-atomic copy, because if another thread duke@435: // is modifying a reference field in the clonee, a non-oop-atomic copy might duke@435: // be suspended in the middle of copying the pointer and end up with parts duke@435: // of two different pointers in the field. Subsequent dereferences will crash. duke@435: // 4846409: an oop-copy of objects with long or double fields or arrays of same duke@435: // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead duke@435: // of oops. We know objects are aligned on a minimum of an jlong boundary. duke@435: // The same is true of StubRoutines::object_copy and the various oop_copy duke@435: // variants, and of the code generated by the inline_native_clone intrinsic. duke@435: assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned"); duke@435: Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj, duke@435: (size_t)align_object_size(size) / HeapWordsPerLong); duke@435: // Clear the header duke@435: new_obj->init_mark(); duke@435: duke@435: // Store check (mark entire object and let gc sort it out) duke@435: BarrierSet* bs = Universe::heap()->barrier_set(); duke@435: assert(bs->has_write_region_opt(), "Barrier set does not have write_region"); duke@435: bs->write_region(MemRegion((HeapWord*)new_obj, size)); duke@435: duke@435: // Caution: this involves a java upcall, so the clone should be duke@435: // "gc-robust" by this stage. duke@435: if (klass->has_finalizer()) { duke@435: assert(obj->is_instance(), "should be instanceOop"); coleenp@4037: new_obj = InstanceKlass::register_finalizer(instanceOop(new_obj), CHECK_NULL); duke@435: } duke@435: duke@435: return JNIHandles::make_local(env, oop(new_obj)); duke@435: JVM_END duke@435: duke@435: // java.lang.Compiler //////////////////////////////////////////////////// duke@435: duke@435: // The initial cuts of the HotSpot VM will not support JITs, and all existing duke@435: // JITs would need extensive changes to work with HotSpot. The JIT-related JVM duke@435: // functions are all silently ignored unless JVM warnings are printed. duke@435: duke@435: JVM_LEAF(void, JVM_InitializeCompiler (JNIEnv *env, jclass compCls)) duke@435: if (PrintJVMWarnings) warning("JVM_InitializeCompiler not supported"); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jboolean, JVM_IsSilentCompiler(JNIEnv *env, jclass compCls)) duke@435: if (PrintJVMWarnings) warning("JVM_IsSilentCompiler not supported"); duke@435: return JNI_FALSE; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jboolean, JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls)) duke@435: if (PrintJVMWarnings) warning("JVM_CompileClass not supported"); duke@435: return JNI_FALSE; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jboolean, JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname)) duke@435: if (PrintJVMWarnings) warning("JVM_CompileClasses not supported"); duke@435: return JNI_FALSE; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jobject, JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg)) duke@435: if (PrintJVMWarnings) warning("JVM_CompilerCommand not supported"); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_EnableCompiler(JNIEnv *env, jclass compCls)) duke@435: if (PrintJVMWarnings) warning("JVM_EnableCompiler not supported"); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_DisableCompiler(JNIEnv *env, jclass compCls)) duke@435: if (PrintJVMWarnings) warning("JVM_DisableCompiler not supported"); duke@435: JVM_END duke@435: duke@435: duke@435: duke@435: // Error message support ////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jint, JVM_GetLastErrorString(char *buf, int len)) duke@435: JVMWrapper("JVM_GetLastErrorString"); ikrylov@2322: return (jint)os::lasterror(buf, len); duke@435: JVM_END duke@435: duke@435: duke@435: // java.io.File /////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(char*, JVM_NativePath(char* path)) duke@435: JVMWrapper2("JVM_NativePath (%s)", path); ikrylov@2322: return os::native_path(path); duke@435: JVM_END duke@435: duke@435: duke@435: // Misc. class handling /////////////////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth)) duke@435: JVMWrapper("JVM_GetCallerClass"); twisti@4866: twisti@4866: // Pre-JDK 8 and early builds of JDK 8 don't have a CallerSensitive annotation. twisti@4866: if (SystemDictionary::reflect_CallerSensitive_klass() == NULL) { twisti@4866: Klass* k = thread->security_get_caller_class(depth); twisti@4866: return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, k->java_mirror()); twisti@4866: } else { twisti@4866: // Basic handshaking with Java_sun_reflect_Reflection_getCallerClass twisti@4866: assert(depth == -1, "wrong handshake depth"); twisti@4866: } twisti@4866: twisti@4866: // Getting the class of the caller frame. twisti@4866: // twisti@4866: // The call stack at this point looks something like this: twisti@4866: // twisti@4866: // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ] twisti@4866: // [1] [ @CallerSensitive API.method ] twisti@4866: // [.] [ (skipped intermediate frames) ] twisti@4866: // [n] [ caller ] twisti@4866: vframeStream vfst(thread); twisti@4866: // Cf. LibraryCallKit::inline_native_Reflection_getCallerClass twisti@4866: for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) { twisti@4866: Method* m = vfst.method(); twisti@4866: assert(m != NULL, "sanity"); twisti@4866: switch (n) { twisti@4866: case 0: twisti@4866: // This must only be called from Reflection.getCallerClass twisti@4866: if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) { twisti@4866: THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass"); twisti@4866: } twisti@4866: // fall-through twisti@4866: case 1: twisti@4866: // Frame 0 and 1 must be caller sensitive. twisti@4866: if (!m->caller_sensitive()) { twisti@4866: THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n)); twisti@4866: } twisti@4866: break; twisti@4866: default: twisti@4866: if (!m->is_ignored_by_security_stack_walk()) { twisti@4866: // We have reached the desired frame; return the holder class. twisti@4866: return (jclass) JNIHandles::make_local(env, m->method_holder()->java_mirror()); twisti@4866: } twisti@4866: break; twisti@4866: } twisti@4866: } twisti@4866: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf)) duke@435: JVMWrapper("JVM_FindPrimitiveClass"); duke@435: oop mirror = NULL; duke@435: BasicType t = name2type(utf); duke@435: if (t != T_ILLEGAL && t != T_OBJECT && t != T_ARRAY) { duke@435: mirror = Universe::java_mirror(t); duke@435: } duke@435: if (mirror == NULL) { duke@435: THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf); duke@435: } else { duke@435: return (jclass) JNIHandles::make_local(env, mirror); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_ResolveClass(JNIEnv* env, jclass cls)) duke@435: JVMWrapper("JVM_ResolveClass"); duke@435: if (PrintJVMWarnings) warning("JVM_ResolveClass not implemented"); duke@435: JVM_END duke@435: mchung@1313: mchung@1313: // Returns a class loaded by the bootstrap class loader; or null mchung@1313: // if not found. ClassNotFoundException is not thrown. mchung@1313: // ksrini@661: // Rationale behind JVM_FindClassFromBootLoader ksrini@661: // a> JVM_FindClassFromClassLoader was never exported in the export tables. ksrini@661: // b> because of (a) java.dll has a direct dependecy on the unexported ksrini@661: // private symbol "_JVM_FindClassFromClassLoader@20". ksrini@661: // c> the launcher cannot use the private symbol as it dynamically opens ksrini@661: // the entry point, so if something changes, the launcher will fail ksrini@661: // unexpectedly at runtime, it is safest for the launcher to dlopen a ksrini@661: // stable exported interface. ksrini@661: // d> re-exporting JVM_FindClassFromClassLoader as public, will cause its ksrini@661: // signature to change from _JVM_FindClassFromClassLoader@20 to ksrini@661: // JVM_FindClassFromClassLoader and will not be backward compatible ksrini@661: // with older JDKs. ksrini@661: // Thus a public/stable exported entry point is the right solution, ksrini@661: // public here means public in linker semantics, and is exported only ksrini@661: // to the JDK, and is not intended to be a public API. ksrini@661: ksrini@661: JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env, mchung@1313: const char* name)) mchung@1313: JVMWrapper2("JVM_FindClassFromBootLoader %s", name); mchung@1313: mchung@1313: // Java libraries should ensure that name is never null... coleenp@2497: if (name == NULL || (int)strlen(name) > Symbol::max_length()) { mchung@1313: // It's impossible to create this class; the name cannot fit mchung@1313: // into the constant pool. mchung@1313: return NULL; mchung@1313: } mchung@1313: coleenp@2497: TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); coleenp@4037: Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL); mchung@1313: if (k == NULL) { mchung@1313: return NULL; mchung@1313: } mchung@1313: mchung@1313: if (TraceClassResolution) { mchung@1313: trace_class_resolution(k); mchung@1313: } hseigel@4278: return (jclass) JNIHandles::make_local(env, k->java_mirror()); ksrini@661: JVM_END duke@435: duke@435: JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name, duke@435: jboolean init, jobject loader, duke@435: jboolean throwError)) duke@435: JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name, duke@435: throwError ? "error" : "exception"); mchung@1313: // Java libraries should ensure that name is never null... coleenp@2497: if (name == NULL || (int)strlen(name) > Symbol::max_length()) { mchung@1313: // It's impossible to create this class; the name cannot fit mchung@1313: // into the constant pool. mchung@1313: if (throwError) { mchung@1313: THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); mchung@1313: } else { mchung@1313: THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name); mchung@1313: } mchung@1313: } coleenp@2497: TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); mchung@1313: Handle h_loader(THREAD, JNIHandles::resolve(loader)); mchung@1313: jclass result = find_class_from_class_loader(env, h_name, init, h_loader, mchung@1313: Handle(), throwError, THREAD); mchung@1313: mchung@1313: if (TraceClassResolution && result != NULL) { coleenp@4037: trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result))); mchung@1313: } mchung@1313: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name, duke@435: jboolean init, jclass from)) duke@435: JVMWrapper2("JVM_FindClassFromClass %s", name); coleenp@2497: if (name == NULL || (int)strlen(name) > Symbol::max_length()) { duke@435: // It's impossible to create this class; the name cannot fit duke@435: // into the constant pool. duke@435: THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); duke@435: } coleenp@2497: TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL); duke@435: oop from_class_oop = JNIHandles::resolve(from); coleenp@4037: Klass* from_class = (from_class_oop == NULL) coleenp@4037: ? (Klass*)NULL coleenp@4037: : java_lang_Class::as_Klass(from_class_oop); duke@435: oop class_loader = NULL; duke@435: oop protection_domain = NULL; duke@435: if (from_class != NULL) { hseigel@4278: class_loader = from_class->class_loader(); hseigel@4278: protection_domain = from_class->protection_domain(); duke@435: } duke@435: Handle h_loader(THREAD, class_loader); duke@435: Handle h_prot (THREAD, protection_domain); duke@435: jclass result = find_class_from_class_loader(env, h_name, init, h_loader, duke@435: h_prot, true, thread); duke@435: duke@435: if (TraceClassResolution && result != NULL) { duke@435: // this function is generally only used for class loading during verification. duke@435: ResourceMark rm; duke@435: oop from_mirror = JNIHandles::resolve_non_null(from); coleenp@4037: Klass* from_class = java_lang_Class::as_Klass(from_mirror); hseigel@4278: const char * from_name = from_class->external_name(); duke@435: duke@435: oop mirror = JNIHandles::resolve_non_null(result); coleenp@4037: Klass* to_class = java_lang_Class::as_Klass(mirror); hseigel@4278: const char * to = to_class->external_name(); duke@435: tty->print("RESOLVE %s %s (verification)\n", from_name, to); duke@435: } duke@435: duke@435: return result; duke@435: JVM_END duke@435: duke@435: static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) { duke@435: if (loader.is_null()) { duke@435: return; duke@435: } duke@435: duke@435: // check whether the current caller thread holds the lock or not. duke@435: // If not, increment the corresponding counter duke@435: if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) != duke@435: ObjectSynchronizer::owner_self) { duke@435: counter->inc(); duke@435: } duke@435: } duke@435: duke@435: // common code for JVM_DefineClass() and JVM_DefineClassWithSource() acorn@1408: // and JVM_DefineClassWithSourceCond() acorn@1408: static jclass jvm_define_class_common(JNIEnv *env, const char *name, acorn@1408: jobject loader, const jbyte *buf, acorn@1408: jsize len, jobject pd, const char *source, acorn@1408: jboolean verify, TRAPS) { jrose@866: if (source == NULL) source = "__JVM_DefineClass__"; duke@435: mchung@1310: assert(THREAD->is_Java_thread(), "must be a JavaThread"); mchung@1310: JavaThread* jt = (JavaThread*) THREAD; mchung@1310: mchung@1310: PerfClassTraceTime vmtimer(ClassLoader::perf_define_appclass_time(), mchung@1310: ClassLoader::perf_define_appclass_selftime(), mchung@1310: ClassLoader::perf_define_appclasses(), mchung@1310: jt->get_thread_stat()->perf_recursion_counts_addr(), mchung@1310: jt->get_thread_stat()->perf_timers_addr(), mchung@1310: PerfClassTraceTime::DEFINE_CLASS); mchung@1310: mchung@1310: if (UsePerfData) { mchung@1310: ClassLoader::perf_app_classfile_bytes_read()->inc(len); mchung@1310: } mchung@1310: duke@435: // Since exceptions can be thrown, class initialization can take place duke@435: // if name is NULL no check for class name in .class stream has to be made. coleenp@2497: TempNewSymbol class_name = NULL; duke@435: if (name != NULL) { duke@435: const int str_len = (int)strlen(name); coleenp@2497: if (str_len > Symbol::max_length()) { duke@435: // It's impossible to create this class; the name cannot fit duke@435: // into the constant pool. duke@435: THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name); duke@435: } coleenp@2497: class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL); duke@435: } duke@435: duke@435: ResourceMark rm(THREAD); duke@435: ClassFileStream st((u1*) buf, len, (char *)source); duke@435: Handle class_loader (THREAD, JNIHandles::resolve(loader)); duke@435: if (UsePerfData) { duke@435: is_lock_held_by_thread(class_loader, duke@435: ClassLoader::sync_JVMDefineClassLockFreeCounter(), duke@435: THREAD); duke@435: } duke@435: Handle protection_domain (THREAD, JNIHandles::resolve(pd)); coleenp@4037: Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader, duke@435: protection_domain, &st, acorn@1408: verify != 0, duke@435: CHECK_NULL); duke@435: duke@435: if (TraceClassResolution && k != NULL) { duke@435: trace_class_resolution(k); duke@435: } duke@435: hseigel@4278: return (jclass) JNIHandles::make_local(env, k->java_mirror()); duke@435: } duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd)) duke@435: JVMWrapper2("JVM_DefineClass %s", name); duke@435: acorn@1408: return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, true, THREAD); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source)) duke@435: JVMWrapper2("JVM_DefineClassWithSource %s", name); duke@435: acorn@1408: return jvm_define_class_common(env, name, loader, buf, len, pd, source, true, THREAD); acorn@1408: JVM_END acorn@1408: acorn@1408: JVM_ENTRY(jclass, JVM_DefineClassWithSourceCond(JNIEnv *env, const char *name, acorn@1408: jobject loader, const jbyte *buf, acorn@1408: jsize len, jobject pd, acorn@1408: const char *source, jboolean verify)) acorn@1408: JVMWrapper2("JVM_DefineClassWithSourceCond %s", name); acorn@1408: acorn@1408: return jvm_define_class_common(env, name, loader, buf, len, pd, source, verify, THREAD); acorn@1408: JVM_END duke@435: duke@435: JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name)) duke@435: JVMWrapper("JVM_FindLoadedClass"); duke@435: ResourceMark rm(THREAD); duke@435: duke@435: Handle h_name (THREAD, JNIHandles::resolve_non_null(name)); duke@435: Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL); duke@435: duke@435: const char* str = java_lang_String::as_utf8_string(string()); duke@435: // Sanity check, don't expect null duke@435: if (str == NULL) return NULL; duke@435: duke@435: const int str_len = (int)strlen(str); coleenp@2497: if (str_len > Symbol::max_length()) { duke@435: // It's impossible to create this class; the name cannot fit duke@435: // into the constant pool. duke@435: return NULL; duke@435: } coleenp@2497: TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len, CHECK_NULL); duke@435: duke@435: // Security Note: duke@435: // The Java level wrapper will perform the necessary security check allowing duke@435: // us to pass the NULL as the initiating class loader. duke@435: Handle h_loader(THREAD, JNIHandles::resolve(loader)); duke@435: if (UsePerfData) { duke@435: is_lock_held_by_thread(h_loader, duke@435: ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(), duke@435: THREAD); duke@435: } duke@435: coleenp@4037: Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name, duke@435: h_loader, duke@435: Handle(), duke@435: CHECK_NULL); duke@435: duke@435: return (k == NULL) ? NULL : hseigel@4278: (jclass) JNIHandles::make_local(env, k->java_mirror()); duke@435: JVM_END duke@435: duke@435: duke@435: // Reflection support ////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls)) duke@435: assert (cls != NULL, "illegal class"); duke@435: JVMWrapper("JVM_GetClassName"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: ResourceMark rm(THREAD); duke@435: const char* name; duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { duke@435: name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls))); duke@435: } else { duke@435: // Consider caching interned string in Klass coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); duke@435: assert(k->is_klass(), "just checking"); hseigel@4278: name = k->external_name(); duke@435: } duke@435: oop result = StringTable::intern((char*) name, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassInterfaces"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: oop mirror = JNIHandles::resolve_non_null(cls); duke@435: duke@435: // Special handling for primitive objects duke@435: if (java_lang_Class::is_primitive(mirror)) { duke@435: // Primitive objects does not have any interfaces never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); duke@435: return (jobjectArray) JNIHandles::make_local(env, r); duke@435: } duke@435: coleenp@4037: KlassHandle klass(thread, java_lang_Class::as_Klass(mirror)); duke@435: // Figure size of result array duke@435: int size; duke@435: if (klass->oop_is_instance()) { coleenp@4037: size = InstanceKlass::cast(klass())->local_interfaces()->length(); duke@435: } else { duke@435: assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass"); duke@435: size = 2; duke@435: } duke@435: duke@435: // Allocate result array never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), size, CHECK_NULL); duke@435: objArrayHandle result (THREAD, r); duke@435: // Fill in result duke@435: if (klass->oop_is_instance()) { duke@435: // Regular instance klass, fill in all local interfaces duke@435: for (int index = 0; index < size; index++) { coleenp@4037: Klass* k = InstanceKlass::cast(klass())->local_interfaces()->at(index); hseigel@4278: result->obj_at_put(index, k->java_mirror()); duke@435: } duke@435: } else { duke@435: // All arrays implement java.lang.Cloneable and java.io.Serializable hseigel@4278: result->obj_at_put(0, SystemDictionary::Cloneable_klass()->java_mirror()); hseigel@4278: result->obj_at_put(1, SystemDictionary::Serializable_klass()->java_mirror()); duke@435: } duke@435: return (jobjectArray) JNIHandles::make_local(env, result()); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetClassLoader(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassLoader"); duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { duke@435: return NULL; duke@435: } coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); hseigel@4278: oop loader = k->class_loader(); duke@435: return JNIHandles::make_local(env, loader); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_IsInterface"); duke@435: oop mirror = JNIHandles::resolve_non_null(cls); duke@435: if (java_lang_Class::is_primitive(mirror)) { duke@435: return JNI_FALSE; duke@435: } coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); hseigel@4278: jboolean result = k->is_interface(); hseigel@4278: assert(!result || k->oop_is_instance(), duke@435: "all interfaces are instance types"); duke@435: // The compiler intrinsic for isInterface tests the duke@435: // Klass::_access_flags bits in the same way. duke@435: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassSigners"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { duke@435: // There are no signers for primitive types duke@435: return NULL; duke@435: } duke@435: coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: objArrayOop signers = NULL; hseigel@4278: if (k->oop_is_instance()) { coleenp@4037: signers = InstanceKlass::cast(k)->signers(); duke@435: } duke@435: duke@435: // If there are no signers set in the class, or if the class duke@435: // is an array, return NULL. duke@435: if (signers == NULL) return NULL; duke@435: duke@435: // copy of the signers array coleenp@4142: Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass(); duke@435: objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL); duke@435: for (int index = 0; index < signers->length(); index++) { duke@435: signers_copy->obj_at_put(index, signers->obj_at(index)); duke@435: } duke@435: duke@435: // return the copy duke@435: return (jobjectArray) JNIHandles::make_local(env, signers_copy); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers)) duke@435: JVMWrapper("JVM_SetClassSigners"); duke@435: if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { duke@435: // This call is ignored for primitive types and arrays. duke@435: // Signers are only set once, ClassLoader.java, and thus shouldn't duke@435: // be called with an array. Only the bootstrap loader creates arrays. coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); hseigel@4278: if (k->oop_is_instance()) { coleenp@4037: InstanceKlass::cast(k)->set_signers(objArrayOop(JNIHandles::resolve(signers))); duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetProtectionDomain"); duke@435: if (JNIHandles::resolve(cls) == NULL) { duke@435: THROW_(vmSymbols::java_lang_NullPointerException(), NULL); duke@435: } duke@435: duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { duke@435: // Primitive types does not have a protection domain. duke@435: return NULL; duke@435: } duke@435: coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); hseigel@4278: return (jobject) JNIHandles::make_local(env, k->protection_domain()); duke@435: JVM_END duke@435: duke@435: duke@435: // Obsolete since 1.2 (Class.setProtectionDomain removed), although duke@435: // still defined in core libraries as of 1.5. duke@435: JVM_ENTRY(void, JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain)) duke@435: JVMWrapper("JVM_SetProtectionDomain"); duke@435: if (JNIHandles::resolve(cls) == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { duke@435: // Call is ignored for primitive types coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); duke@435: duke@435: // cls won't be an array, as this called only from ClassLoader.defineClass hseigel@4278: if (k->oop_is_instance()) { duke@435: oop pd = JNIHandles::resolve(protection_domain); duke@435: assert(pd == NULL || pd->is_oop(), "just checking"); coleenp@4037: InstanceKlass::cast(k)->set_protection_domain(pd); duke@435: } duke@435: } duke@435: JVM_END duke@435: mullan@5242: static bool is_authorized(Handle context, instanceKlassHandle klass, TRAPS) { mullan@5242: // If there is a security manager and protection domain, check the access mullan@5242: // in the protection domain, otherwise it is authorized. mullan@5242: if (java_lang_System::has_security_manager()) { mullan@5242: mullan@5242: // For bootstrapping, if pd implies method isn't in the JDK, allow mullan@5242: // this context to revert to older behavior. mullan@5242: // In this case the isAuthorized field in AccessControlContext is also not mullan@5242: // present. mullan@5242: if (Universe::protection_domain_implies_method() == NULL) { mullan@5242: return true; mullan@5242: } mullan@5242: mullan@5242: // Whitelist certain access control contexts mullan@5242: if (java_security_AccessControlContext::is_authorized(context)) { mullan@5242: return true; mullan@5242: } mullan@5242: mullan@5242: oop prot = klass->protection_domain(); mullan@5242: if (prot != NULL) { mullan@5242: // Call pd.implies(new SecurityPermission("createAccessControlContext")) mullan@5242: // in the new wrapper. mullan@5242: methodHandle m(THREAD, Universe::protection_domain_implies_method()); mullan@5242: Handle h_prot(THREAD, prot); mullan@5242: JavaValue result(T_BOOLEAN); mullan@5242: JavaCallArguments args(h_prot); mullan@5242: JavaCalls::call(&result, m, &args, CHECK_false); mullan@5242: return (result.get_jboolean() != 0); mullan@5242: } mullan@5242: } mullan@5242: return true; mullan@5242: } mullan@5242: mullan@5242: // Create an AccessControlContext with a protection domain with null codesource mullan@5242: // and null permissions - which gives no permissions. mullan@5242: oop create_dummy_access_control_context(TRAPS) { mullan@5242: InstanceKlass* pd_klass = InstanceKlass::cast(SystemDictionary::ProtectionDomain_klass()); mullan@5242: // new ProtectionDomain(null,null); mullan@5242: oop null_protection_domain = pd_klass->allocate_instance(CHECK_NULL); mullan@5242: Handle null_pd(THREAD, null_protection_domain); mullan@5242: mullan@5242: // new ProtectionDomain[] {pd}; mullan@5242: objArrayOop context = oopFactory::new_objArray(pd_klass, 1, CHECK_NULL); mullan@5242: context->obj_at_put(0, null_pd()); mullan@5242: mullan@5242: // new AccessControlContext(new ProtectionDomain[] {pd}) mullan@5242: objArrayHandle h_context(THREAD, context); mullan@5242: oop result = java_security_AccessControlContext::create(h_context, false, Handle(), CHECK_NULL); mullan@5242: return result; mullan@5242: } duke@435: duke@435: JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException)) duke@435: JVMWrapper("JVM_DoPrivileged"); duke@435: duke@435: if (action == NULL) { duke@435: THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action"); duke@435: } duke@435: mullan@5242: // Compute the frame initiating the do privileged operation and setup the privileged stack mullan@5242: vframeStream vfst(thread); mullan@5242: vfst.security_get_caller_frame(1); mullan@5242: mullan@5242: if (vfst.at_end()) { mullan@5242: THROW_MSG_0(vmSymbols::java_lang_InternalError(), "no caller?"); mullan@5242: } mullan@5242: mullan@5242: Method* method = vfst.method(); mullan@5242: instanceKlassHandle klass (THREAD, method->method_holder()); mullan@5242: mullan@5242: // Check that action object understands "Object run()" mullan@5242: Handle h_context; mullan@5242: if (context != NULL) { mullan@5242: h_context = Handle(THREAD, JNIHandles::resolve(context)); mullan@5242: bool authorized = is_authorized(h_context, klass, CHECK_NULL); mullan@5242: if (!authorized) { mullan@5242: // Create an unprivileged access control object and call it's run function mullan@5242: // instead. mullan@5242: oop noprivs = create_dummy_access_control_context(CHECK_NULL); mullan@5242: h_context = Handle(THREAD, noprivs); mullan@5242: } mullan@5242: } duke@435: duke@435: // Check that action object understands "Object run()" duke@435: Handle object (THREAD, JNIHandles::resolve(action)); duke@435: duke@435: // get run() method hseigel@4278: Method* m_oop = object->klass()->uncached_lookup_method( duke@435: vmSymbols::run_method_name(), duke@435: vmSymbols::void_object_signature()); duke@435: methodHandle m (THREAD, m_oop); coleenp@4037: if (m.is_null() || !m->is_method() || !m()->is_public() || m()->is_static()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method"); duke@435: } duke@435: mullan@5242: // Stack allocated list of privileged stack elements mullan@5242: PrivilegedElement pi; duke@435: if (!vfst.at_end()) { mullan@5242: pi.initialize(&vfst, h_context(), thread->privileged_stack_top(), CHECK_NULL); duke@435: thread->set_privileged_stack_top(&pi); duke@435: } duke@435: duke@435: duke@435: // invoke the Object run() in the action object. We cannot use call_interface here, since the static type duke@435: // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction duke@435: Handle pending_exception; duke@435: JavaValue result(T_OBJECT); duke@435: JavaCallArguments args(object); duke@435: JavaCalls::call(&result, m, &args, THREAD); duke@435: duke@435: // done with action, remove ourselves from the list duke@435: if (!vfst.at_end()) { duke@435: assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element"); duke@435: thread->set_privileged_stack_top(thread->privileged_stack_top()->next()); duke@435: } duke@435: duke@435: if (HAS_PENDING_EXCEPTION) { duke@435: pending_exception = Handle(THREAD, PENDING_EXCEPTION); duke@435: CLEAR_PENDING_EXCEPTION; duke@435: never@1577: if ( pending_exception->is_a(SystemDictionary::Exception_klass()) && never@1577: !pending_exception->is_a(SystemDictionary::RuntimeException_klass())) { duke@435: // Throw a java.security.PrivilegedActionException(Exception e) exception duke@435: JavaCallArguments args(pending_exception); coleenp@2497: THROW_ARG_0(vmSymbols::java_security_PrivilegedActionException(), coleenp@2497: vmSymbols::exception_void_signature(), duke@435: &args); duke@435: } duke@435: } duke@435: duke@435: if (pending_exception.not_null()) THROW_OOP_0(pending_exception()); duke@435: return JNIHandles::make_local(env, (oop) result.get_jobject()); duke@435: JVM_END duke@435: duke@435: duke@435: // Returns the inherited_access_control_context field of the running thread. duke@435: JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetInheritedAccessControlContext"); duke@435: oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj()); duke@435: return JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: class RegisterArrayForGC { duke@435: private: duke@435: JavaThread *_thread; duke@435: public: duke@435: RegisterArrayForGC(JavaThread *thread, GrowableArray* array) { duke@435: _thread = thread; duke@435: _thread->register_array_for_gc(array); duke@435: } duke@435: duke@435: ~RegisterArrayForGC() { duke@435: _thread->register_array_for_gc(NULL); duke@435: } duke@435: }; duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetStackAccessControlContext"); duke@435: if (!UsePrivilegedStack) return NULL; duke@435: duke@435: ResourceMark rm(THREAD); duke@435: GrowableArray* local_array = new GrowableArray(12); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // count the protection domains on the execution stack. We collapse duke@435: // duplicate consecutive protection domains into a single one, as duke@435: // well as stopping when we hit a privileged frame. duke@435: duke@435: // Use vframeStream to iterate through Java frames duke@435: vframeStream vfst(thread); duke@435: duke@435: oop previous_protection_domain = NULL; duke@435: Handle privileged_context(thread, NULL); duke@435: bool is_privileged = false; duke@435: oop protection_domain = NULL; duke@435: duke@435: for(; !vfst.at_end(); vfst.next()) { duke@435: // get method of frame coleenp@4037: Method* method = vfst.method(); duke@435: intptr_t* frame_id = vfst.frame_id(); duke@435: duke@435: // check the privileged frames to see if we have a match duke@435: if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) { duke@435: // this frame is privileged duke@435: is_privileged = true; duke@435: privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context()); duke@435: protection_domain = thread->privileged_stack_top()->protection_domain(); duke@435: } else { coleenp@4251: protection_domain = method->method_holder()->protection_domain(); duke@435: } duke@435: duke@435: if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) { duke@435: local_array->push(protection_domain); duke@435: previous_protection_domain = protection_domain; duke@435: } duke@435: duke@435: if (is_privileged) break; duke@435: } duke@435: duke@435: duke@435: // either all the domains on the stack were system domains, or duke@435: // we had a privileged system domain duke@435: if (local_array->is_empty()) { duke@435: if (is_privileged && privileged_context.is_null()) return NULL; duke@435: duke@435: oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL); duke@435: return JNIHandles::make_local(env, result); duke@435: } duke@435: duke@435: // the resource area must be registered in case of a gc duke@435: RegisterArrayForGC ragc(thread, local_array); never@1577: objArrayOop context = oopFactory::new_objArray(SystemDictionary::ProtectionDomain_klass(), duke@435: local_array->length(), CHECK_NULL); duke@435: objArrayHandle h_context(thread, context); duke@435: for (int index = 0; index < local_array->length(); index++) { duke@435: h_context->obj_at_put(index, local_array->at(index)); duke@435: } duke@435: duke@435: oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL); duke@435: duke@435: return JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_IsArrayClass"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); hseigel@4278: return (k != NULL) && k->oop_is_array() ? true : false; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_IsPrimitiveClass"); duke@435: oop mirror = JNIHandles::resolve_non_null(cls); duke@435: return (jboolean) java_lang_Class::is_primitive(mirror); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_GetComponentType(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetComponentType"); duke@435: oop mirror = JNIHandles::resolve_non_null(cls); duke@435: oop result = Reflection::array_component_type(mirror, CHECK_NULL); duke@435: return (jclass) JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassModifiers"); duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { duke@435: // Primitive type duke@435: return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; duke@435: } duke@435: hseigel@4278: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0)); duke@435: assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK"); duke@435: return k->modifier_flags(); duke@435: JVM_END duke@435: duke@435: duke@435: // Inner class reflection /////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass)) duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: // ofClass is a reference to a java_lang_Class object. The mirror object coleenp@4037: // of an InstanceKlass duke@435: duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || hseigel@4278: ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) { never@1577: oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); duke@435: return (jobjectArray)JNIHandles::make_local(env, result); duke@435: } duke@435: coleenp@4037: instanceKlassHandle k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); jiangli@3670: InnerClassesIterator iter(k); jiangli@3670: jiangli@3670: if (iter.length() == 0) { duke@435: // Neither an inner nor outer class never@1577: oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL); duke@435: return (jobjectArray)JNIHandles::make_local(env, result); duke@435: } duke@435: duke@435: // find inner class info duke@435: constantPoolHandle cp(thread, k->constants()); jiangli@3670: int length = iter.length(); duke@435: duke@435: // Allocate temp. result array never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL); duke@435: objArrayHandle result (THREAD, r); duke@435: int members = 0; duke@435: jiangli@3670: for (; !iter.done(); iter.next()) { jiangli@3670: int ioff = iter.inner_class_info_index(); jiangli@3670: int ooff = iter.outer_class_info_index(); duke@435: duke@435: if (ioff != 0 && ooff != 0) { duke@435: // Check to see if the name matches the class we're looking for duke@435: // before attempting to find the class. duke@435: if (cp->klass_name_at_matches(k, ooff)) { coleenp@4037: Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL); duke@435: if (outer_klass == k()) { coleenp@4037: Klass* ik = cp->klass_at(ioff, CHECK_NULL); duke@435: instanceKlassHandle inner_klass (THREAD, ik); duke@435: duke@435: // Throws an exception if outer klass has not declared k as duke@435: // an inner klass jrose@1100: Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL); duke@435: duke@435: result->obj_at_put(members, inner_klass->java_mirror()); duke@435: members++; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (members != length) { duke@435: // Return array of right length never@1577: objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL); duke@435: for(int i = 0; i < members; i++) { duke@435: res->obj_at_put(i, result->obj_at(i)); duke@435: } duke@435: return (jobjectArray)JNIHandles::make_local(env, res); duke@435: } duke@435: duke@435: return (jobjectArray)JNIHandles::make_local(env, result()); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass)) jrose@1100: { duke@435: // ofClass is a reference to a java_lang_Class object. duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || hseigel@4278: ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) { duke@435: return NULL; duke@435: } duke@435: xlu@1561: bool inner_is_member = false; coleenp@4037: Klass* outer_klass coleenp@4037: = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)) xlu@1561: )->compute_enclosing_class(&inner_is_member, CHECK_NULL); jrose@1100: if (outer_klass == NULL) return NULL; // already a top-level class xlu@1561: if (!inner_is_member) return NULL; // an anonymous class (inside a method) hseigel@4278: return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror()); jrose@1100: } jrose@1100: JVM_END jrose@1100: coleenp@4037: // should be in InstanceKlass.cpp, but is here for historical reasons coleenp@4037: Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle k, xlu@1561: bool* inner_is_member, xlu@1561: TRAPS) { jrose@1100: Thread* thread = THREAD; jiangli@3670: InnerClassesIterator iter(k); jiangli@3670: if (iter.length() == 0) { duke@435: // No inner class info => no declaring class duke@435: return NULL; duke@435: } duke@435: duke@435: constantPoolHandle i_cp(thread, k->constants()); duke@435: duke@435: bool found = false; coleenp@4037: Klass* ok; duke@435: instanceKlassHandle outer_klass; xlu@1561: *inner_is_member = false; duke@435: duke@435: // Find inner_klass attribute jiangli@3670: for (; !iter.done() && !found; iter.next()) { jiangli@3670: int ioff = iter.inner_class_info_index(); jiangli@3670: int ooff = iter.outer_class_info_index(); jiangli@3670: int noff = iter.inner_name_index(); jrose@1100: if (ioff != 0) { duke@435: // Check to see if the name matches the class we're looking for duke@435: // before attempting to find the class. duke@435: if (i_cp->klass_name_at_matches(k, ioff)) { coleenp@4037: Klass* inner_klass = i_cp->klass_at(ioff, CHECK_NULL); jrose@1100: found = (k() == inner_klass); jrose@1100: if (found && ooff != 0) { duke@435: ok = i_cp->klass_at(ooff, CHECK_NULL); duke@435: outer_klass = instanceKlassHandle(thread, ok); xlu@1561: *inner_is_member = true; duke@435: } duke@435: } duke@435: } duke@435: } duke@435: jrose@1100: if (found && outer_klass.is_null()) { jrose@1100: // It may be anonymous; try for that. jrose@1100: int encl_method_class_idx = k->enclosing_method_class_index(); jrose@1100: if (encl_method_class_idx != 0) { jrose@1100: ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL); jrose@1100: outer_klass = instanceKlassHandle(thread, ok); xlu@1561: *inner_is_member = false; jrose@1100: } jrose@1100: } jrose@1100: duke@435: // If no inner class attribute found for this class. jrose@1100: if (outer_klass.is_null()) return NULL; duke@435: duke@435: // Throws an exception if outer klass has not declared k as an inner klass jrose@1100: // We need evidence that each klass knows about the other, or else jrose@1100: // the system could allow a spoof of an inner class to gain access rights. xlu@1561: Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL); jrose@1100: return outer_klass(); jrose@1100: } duke@435: duke@435: JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls)) duke@435: assert (cls != NULL, "illegal class"); duke@435: JVMWrapper("JVM_GetClassSignature"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: ResourceMark rm(THREAD); duke@435: // Return null for arrays and primatives duke@435: if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); hseigel@4278: if (k->oop_is_instance()) { coleenp@4037: Symbol* sym = InstanceKlass::cast(k)->generic_signature(); coleenp@2497: if (sym == NULL) return NULL; duke@435: Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(env, str()); duke@435: } duke@435: } duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls)) duke@435: assert (cls != NULL, "illegal class"); duke@435: JVMWrapper("JVM_GetClassAnnotations"); rbackman@4818: duke@435: // Return null for arrays and primitives duke@435: if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); hseigel@4278: if (k->oop_is_instance()) { coleenp@4037: typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL); coleenp@4037: return (jbyteArray) JNIHandles::make_local(env, a); duke@435: } duke@435: } duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: rbackman@4818: static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) { duke@435: // some of this code was adapted from from jni_FromReflectedField duke@435: duke@435: oop reflected = JNIHandles::resolve_non_null(field); duke@435: oop mirror = java_lang_reflect_Field::clazz(reflected); coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); duke@435: int slot = java_lang_reflect_Field::slot(reflected); duke@435: int modifiers = java_lang_reflect_Field::modifiers(reflected); duke@435: duke@435: KlassHandle kh(THREAD, k); coleenp@4037: intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot); duke@435: duke@435: if (modifiers & JVM_ACC_STATIC) { duke@435: // for static fields we only look in the current class coleenp@4037: if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) { duke@435: assert(false, "cannot find static field"); rbackman@4818: return false; duke@435: } duke@435: } else { duke@435: // for instance fields we start with the current class and work duke@435: // our way up through the superclass chain coleenp@4037: if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) { duke@435: assert(false, "cannot find instance field"); rbackman@4818: return false; duke@435: } duke@435: } rbackman@4818: return true; rbackman@4818: } rbackman@4818: rbackman@4818: JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field)) rbackman@4818: // field is a handle to a java.lang.reflect.Field object rbackman@4818: assert(field != NULL, "illegal field"); rbackman@4818: JVMWrapper("JVM_GetFieldAnnotations"); rbackman@4818: rbackman@4818: fieldDescriptor fd; rbackman@4818: bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL); rbackman@4818: if (!gotFd) { rbackman@4818: return NULL; rbackman@4818: } duke@435: coleenp@4037: return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.annotations(), THREAD)); duke@435: JVM_END duke@435: duke@435: coleenp@4398: static Method* jvm_get_method_common(jobject method) { duke@435: // some of this code was adapted from from jni_FromReflectedMethod duke@435: duke@435: oop reflected = JNIHandles::resolve_non_null(method); duke@435: oop mirror = NULL; duke@435: int slot = 0; duke@435: never@1577: if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) { duke@435: mirror = java_lang_reflect_Constructor::clazz(reflected); duke@435: slot = java_lang_reflect_Constructor::slot(reflected); duke@435: } else { never@1577: assert(reflected->klass() == SystemDictionary::reflect_Method_klass(), duke@435: "wrong type"); duke@435: mirror = java_lang_reflect_Method::clazz(reflected); duke@435: slot = java_lang_reflect_Method::slot(reflected); duke@435: } coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); duke@435: coleenp@4398: Method* m = InstanceKlass::cast(k)->method_with_idnum(slot); rbackman@4818: assert(m != NULL, "cannot find method"); rbackman@4818: return m; // caller has to deal with NULL in product mode duke@435: } duke@435: duke@435: duke@435: JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method)) duke@435: JVMWrapper("JVM_GetMethodAnnotations"); duke@435: duke@435: // method is a handle to a java.lang.reflect.Method object coleenp@4398: Method* m = jvm_get_method_common(method); rbackman@4818: if (m == NULL) { rbackman@4818: return NULL; rbackman@4818: } rbackman@4818: coleenp@4037: return (jbyteArray) JNIHandles::make_local(env, coleenp@4037: Annotations::make_java_array(m->annotations(), THREAD)); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method)) duke@435: JVMWrapper("JVM_GetMethodDefaultAnnotationValue"); duke@435: duke@435: // method is a handle to a java.lang.reflect.Method object coleenp@4398: Method* m = jvm_get_method_common(method); rbackman@4818: if (m == NULL) { rbackman@4818: return NULL; rbackman@4818: } rbackman@4818: coleenp@4037: return (jbyteArray) JNIHandles::make_local(env, coleenp@4037: Annotations::make_java_array(m->annotation_default(), THREAD)); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method)) duke@435: JVMWrapper("JVM_GetMethodParameterAnnotations"); duke@435: duke@435: // method is a handle to a java.lang.reflect.Method object coleenp@4398: Method* m = jvm_get_method_common(method); rbackman@4818: if (m == NULL) { rbackman@4818: return NULL; rbackman@4818: } rbackman@4818: coleenp@4037: return (jbyteArray) JNIHandles::make_local(env, coleenp@4037: Annotations::make_java_array(m->parameter_annotations(), THREAD)); duke@435: JVM_END duke@435: stefank@4393: /* Type use annotations support (JDK 1.8) */ stefank@4393: stefank@4393: JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls)) stefank@4393: assert (cls != NULL, "illegal class"); stefank@4393: JVMWrapper("JVM_GetClassTypeAnnotations"); stefank@4393: ResourceMark rm(THREAD); stefank@4393: // Return null for arrays and primitives stefank@4393: if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) { stefank@4393: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls)); stefank@4393: if (k->oop_is_instance()) { coleenp@4572: AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations(); stefank@4454: if (type_annotations != NULL) { coleenp@4572: typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); stefank@4454: return (jbyteArray) JNIHandles::make_local(env, a); stefank@4454: } stefank@4393: } stefank@4393: } stefank@4393: return NULL; stefank@4393: JVM_END stefank@4393: rbackman@4818: JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method)) rbackman@4818: assert (method != NULL, "illegal method"); rbackman@4818: JVMWrapper("JVM_GetMethodTypeAnnotations"); rbackman@4818: rbackman@4818: // method is a handle to a java.lang.reflect.Method object rbackman@4818: Method* m = jvm_get_method_common(method); rbackman@4818: if (m == NULL) { rbackman@4818: return NULL; rbackman@4818: } rbackman@4818: rbackman@4818: AnnotationArray* type_annotations = m->type_annotations(); rbackman@4818: if (type_annotations != NULL) { rbackman@4818: typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL); rbackman@4818: return (jbyteArray) JNIHandles::make_local(env, a); rbackman@4818: } rbackman@4818: rbackman@4818: return NULL; rbackman@4818: JVM_END rbackman@4818: rbackman@4818: JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field)) rbackman@4818: assert (field != NULL, "illegal field"); rbackman@4818: JVMWrapper("JVM_GetFieldTypeAnnotations"); rbackman@4818: rbackman@4818: fieldDescriptor fd; rbackman@4818: bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL); rbackman@4818: if (!gotFd) { rbackman@4818: return NULL; rbackman@4818: } rbackman@4818: rbackman@4818: return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.type_annotations(), THREAD)); rbackman@4818: JVM_END rbackman@4818: coleenp@4431: static void bounds_check(constantPoolHandle cp, jint index, TRAPS) { coleenp@4431: if (!cp->is_within_bounds(index)) { coleenp@4431: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds"); coleenp@4431: } coleenp@4431: } coleenp@4431: coleenp@4398: JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method)) coleenp@4398: { coleenp@4398: JVMWrapper("JVM_GetMethodParameters"); coleenp@4398: // method is a handle to a java.lang.reflect.Method object coleenp@4398: Method* method_ptr = jvm_get_method_common(method); coleenp@4398: methodHandle mh (THREAD, method_ptr); coleenp@4398: Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method)); coleenp@4398: const int num_params = mh->method_parameters_length(); coleenp@4398: coleenp@4431: if (0 != num_params) { coleenp@4431: // make sure all the symbols are properly formatted coleenp@4431: for (int i = 0; i < num_params; i++) { coleenp@4431: MethodParametersElement* params = mh->method_parameters_start(); coleenp@4431: int index = params[i].name_cp_index; coleenp@4431: bounds_check(mh->constants(), index, CHECK_NULL); coleenp@4431: coleenp@4431: if (0 != index && !mh->constants()->tag_at(index).is_utf8()) { coleenp@4431: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), coleenp@4431: "Wrong type at constant pool index"); coleenp@4431: } coleenp@4431: coleenp@4431: } coleenp@4431: coleenp@4398: objArrayOop result_oop = oopFactory::new_objArray(SystemDictionary::reflect_Parameter_klass(), num_params, CHECK_NULL); coleenp@4398: objArrayHandle result (THREAD, result_oop); coleenp@4398: coleenp@4431: for (int i = 0; i < num_params; i++) { coleenp@4398: MethodParametersElement* params = mh->method_parameters_start(); coleenp@4431: // For a 0 index, give a NULL symbol coleenp@4431: Symbol* const sym = 0 != params[i].name_cp_index ? coleenp@4431: mh->constants()->symbol_at(params[i].name_cp_index) : NULL; emc@4524: int flags = params[i].flags; coleenp@4398: oop param = Reflection::new_parameter(reflected_method, i, sym, coleenp@4431: flags, CHECK_NULL); coleenp@4398: result->obj_at_put(i, param); coleenp@4398: } coleenp@4398: return (jobjectArray)JNIHandles::make_local(env, result()); coleenp@4398: } else { coleenp@4398: return (jobjectArray)NULL; coleenp@4398: } coleenp@4398: } coleenp@4398: JVM_END duke@435: duke@435: // New (JDK 1.4) reflection implementation ///////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly)) duke@435: { duke@435: JVMWrapper("JVM_GetClassDeclaredFields"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // Exclude primitive types and array types duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) || hseigel@4278: java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) { duke@435: // Return empty array never@1577: oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL); duke@435: return (jobjectArray) JNIHandles::make_local(env, res); duke@435: } duke@435: coleenp@4037: instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); duke@435: constantPoolHandle cp(THREAD, k->constants()); duke@435: duke@435: // Ensure class is linked duke@435: k->link_class(CHECK_NULL); duke@435: duke@435: // 4496456 We need to filter out java.lang.Throwable.backtrace duke@435: bool skip_backtrace = false; duke@435: duke@435: // Allocate result duke@435: int num_fields; duke@435: duke@435: if (publicOnly) { duke@435: num_fields = 0; never@3137: for (JavaFieldStream fs(k()); !fs.done(); fs.next()) { never@3137: if (fs.access_flags().is_public()) ++num_fields; duke@435: } duke@435: } else { never@3137: num_fields = k->java_fields_count(); duke@435: never@1577: if (k() == SystemDictionary::Throwable_klass()) { duke@435: num_fields--; duke@435: skip_backtrace = true; duke@435: } duke@435: } duke@435: never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL); duke@435: objArrayHandle result (THREAD, r); duke@435: duke@435: int out_idx = 0; duke@435: fieldDescriptor fd; never@3137: for (JavaFieldStream fs(k); !fs.done(); fs.next()) { duke@435: if (skip_backtrace) { duke@435: // 4496456 skip java.lang.Throwable.backtrace never@3137: int offset = fs.offset(); duke@435: if (offset == java_lang_Throwable::get_backtrace_offset()) continue; duke@435: } duke@435: never@3137: if (!publicOnly || fs.access_flags().is_public()) { never@3137: fd.initialize(k(), fs.index()); duke@435: oop field = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL); duke@435: result->obj_at_put(out_idx, field); duke@435: ++out_idx; duke@435: } duke@435: } duke@435: assert(out_idx == num_fields, "just checking"); duke@435: return (jobjectArray) JNIHandles::make_local(env, result()); duke@435: } duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly)) duke@435: { duke@435: JVMWrapper("JVM_GetClassDeclaredMethods"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // Exclude primitive types and array types duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) hseigel@4278: || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) { duke@435: // Return empty array never@1577: oop res = oopFactory::new_objArray(SystemDictionary::reflect_Method_klass(), 0, CHECK_NULL); duke@435: return (jobjectArray) JNIHandles::make_local(env, res); duke@435: } duke@435: coleenp@4037: instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); duke@435: duke@435: // Ensure class is linked duke@435: k->link_class(CHECK_NULL); duke@435: coleenp@4037: Array* methods = k->methods(); duke@435: int methods_length = methods->length(); duke@435: int num_methods = 0; duke@435: duke@435: int i; duke@435: for (i = 0; i < methods_length; i++) { coleenp@4037: methodHandle method(THREAD, methods->at(i)); acorn@4805: if (!method->is_initializer() && !method->is_overpass()) { duke@435: if (!publicOnly || method->is_public()) { duke@435: ++num_methods; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Allocate result never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Method_klass(), num_methods, CHECK_NULL); duke@435: objArrayHandle result (THREAD, r); duke@435: duke@435: int out_idx = 0; duke@435: for (i = 0; i < methods_length; i++) { coleenp@4037: methodHandle method(THREAD, methods->at(i)); acorn@4805: if (!method->is_initializer() && !method->is_overpass()) { duke@435: if (!publicOnly || method->is_public()) { duke@435: oop m = Reflection::new_method(method, UseNewReflection, false, CHECK_NULL); duke@435: result->obj_at_put(out_idx, m); duke@435: ++out_idx; duke@435: } duke@435: } duke@435: } duke@435: assert(out_idx == num_methods, "just checking"); duke@435: return (jobjectArray) JNIHandles::make_local(env, result()); duke@435: } duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly)) duke@435: { duke@435: JVMWrapper("JVM_GetClassDeclaredConstructors"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // Exclude primitive types and array types duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) hseigel@4278: || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) { duke@435: // Return empty array never@1577: oop res = oopFactory::new_objArray(SystemDictionary::reflect_Constructor_klass(), 0 , CHECK_NULL); duke@435: return (jobjectArray) JNIHandles::make_local(env, res); duke@435: } duke@435: coleenp@4037: instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))); duke@435: duke@435: // Ensure class is linked duke@435: k->link_class(CHECK_NULL); duke@435: coleenp@4037: Array* methods = k->methods(); duke@435: int methods_length = methods->length(); duke@435: int num_constructors = 0; duke@435: duke@435: int i; duke@435: for (i = 0; i < methods_length; i++) { coleenp@4037: methodHandle method(THREAD, methods->at(i)); duke@435: if (method->is_initializer() && !method->is_static()) { duke@435: if (!publicOnly || method->is_public()) { duke@435: ++num_constructors; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Allocate result never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Constructor_klass(), num_constructors, CHECK_NULL); duke@435: objArrayHandle result(THREAD, r); duke@435: duke@435: int out_idx = 0; duke@435: for (i = 0; i < methods_length; i++) { coleenp@4037: methodHandle method(THREAD, methods->at(i)); duke@435: if (method->is_initializer() && !method->is_static()) { duke@435: if (!publicOnly || method->is_public()) { duke@435: oop m = Reflection::new_constructor(method, CHECK_NULL); duke@435: result->obj_at_put(out_idx, m); duke@435: ++out_idx; duke@435: } duke@435: } duke@435: } duke@435: assert(out_idx == num_constructors, "just checking"); duke@435: return (jobjectArray) JNIHandles::make_local(env, result()); duke@435: } duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls)) duke@435: { duke@435: JVMWrapper("JVM_GetClassAccessFlags"); duke@435: if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { duke@435: // Primitive type duke@435: return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC; duke@435: } duke@435: hseigel@4278: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS; duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: // Constant pool access ////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls)) duke@435: { duke@435: JVMWrapper("JVM_GetClassConstantPool"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // Return null for primitives and arrays duke@435: if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) { coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); hseigel@4278: if (k->oop_is_instance()) { duke@435: instanceKlassHandle k_h(THREAD, k); duke@435: Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL); coleenp@4037: sun_reflect_ConstantPool::set_cp(jcp(), k_h->constants()); duke@435: return JNIHandles::make_local(jcp()); duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: JVM_END duke@435: duke@435: coleenp@4037: JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetSize"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: return cp->length(); duke@435: } duke@435: JVM_END duke@435: duke@435: coleenp@4037: JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetClassAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_klass() && !tag.is_unresolved_klass()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } coleenp@4037: Klass* k = cp->klass_at(index, CHECK_NULL); never@2658: return (jclass) JNIHandles::make_local(k->java_mirror()); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_klass() && !tag.is_unresolved_klass()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } coleenp@4037: Klass* k = ConstantPool::klass_at_if_loaded(cp, index); duke@435: if (k == NULL) return NULL; never@2658: return (jclass) JNIHandles::make_local(k->java_mirror()); duke@435: } duke@435: JVM_END duke@435: duke@435: static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) { duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_method() && !tag.is_interface_method()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: int klass_ref = cp->uncached_klass_ref_index_at(index); coleenp@4037: Klass* k_o; duke@435: if (force_resolution) { duke@435: k_o = cp->klass_at(klass_ref, CHECK_NULL); duke@435: } else { coleenp@4037: k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); duke@435: if (k_o == NULL) return NULL; duke@435: } duke@435: instanceKlassHandle k(THREAD, k_o); coleenp@2497: Symbol* name = cp->uncached_name_ref_at(index); coleenp@2497: Symbol* sig = cp->uncached_signature_ref_at(index); duke@435: methodHandle m (THREAD, k->find_method(name, sig)); duke@435: if (m.is_null()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class"); duke@435: } duke@435: oop method; duke@435: if (!m->is_initializer() || m->is_static()) { duke@435: method = Reflection::new_method(m, true, true, CHECK_NULL); duke@435: } else { duke@435: method = Reflection::new_constructor(m, CHECK_NULL); duke@435: } duke@435: return JNIHandles::make_local(method); duke@435: } duke@435: coleenp@4037: JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetMethodAt"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: jobject res = get_method_at_helper(cp, index, true, CHECK_NULL); duke@435: return res; duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: jobject res = get_method_at_helper(cp, index, false, CHECK_NULL); duke@435: return res; duke@435: } duke@435: JVM_END duke@435: duke@435: static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) { duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_field()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: int klass_ref = cp->uncached_klass_ref_index_at(index); coleenp@4037: Klass* k_o; duke@435: if (force_resolution) { duke@435: k_o = cp->klass_at(klass_ref, CHECK_NULL); duke@435: } else { coleenp@4037: k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref); duke@435: if (k_o == NULL) return NULL; duke@435: } duke@435: instanceKlassHandle k(THREAD, k_o); coleenp@2497: Symbol* name = cp->uncached_name_ref_at(index); coleenp@2497: Symbol* sig = cp->uncached_signature_ref_at(index); duke@435: fieldDescriptor fd; coleenp@4037: Klass* target_klass = k->find_field(name, sig, &fd); duke@435: if (target_klass == NULL) { duke@435: THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class"); duke@435: } duke@435: oop field = Reflection::new_field(&fd, true, CHECK_NULL); duke@435: return JNIHandles::make_local(field); duke@435: } duke@435: coleenp@4037: JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetFieldAt"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: jobject res = get_field_at_helper(cp, index, true, CHECK_NULL); duke@435: return res; duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: jobject res = get_field_at_helper(cp, index, false, CHECK_NULL); duke@435: return res; duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_field_or_method()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: int klass_ref = cp->uncached_klass_ref_index_at(index); coleenp@2497: Symbol* klass_name = cp->klass_name_at(klass_ref); coleenp@2497: Symbol* member_name = cp->uncached_name_ref_at(index); coleenp@2497: Symbol* member_sig = cp->uncached_signature_ref_at(index); never@1577: objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::String_klass(), 3, CHECK_NULL); duke@435: objArrayHandle dest(THREAD, dest_o); duke@435: Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL); duke@435: dest->obj_at_put(0, str()); duke@435: str = java_lang_String::create_from_symbol(member_name, CHECK_NULL); duke@435: dest->obj_at_put(1, str()); duke@435: str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL); duke@435: dest->obj_at_put(2, str()); duke@435: return (jobjectArray) JNIHandles::make_local(dest()); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetIntAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_0); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_int()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: return cp->int_at(index); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetLongAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_(0L)); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_long()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: return cp->long_at(index); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetFloatAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_(0.0f)); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_float()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: return cp->float_at(index); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetDoubleAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_(0.0)); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_double()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: return cp->double_at(index); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetStringAt"); coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: constantTag tag = cp->tag_at(index); coleenp@4037: if (!tag.is_string()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } duke@435: oop str = cp->string_at(index, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(str); duke@435: } duke@435: JVM_END duke@435: coleenp@4037: JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index)) duke@435: { duke@435: JVMWrapper("JVM_ConstantPoolGetUTF8At"); duke@435: JvmtiVMObjectAllocEventCollector oam; coleenp@4037: constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj))); duke@435: bounds_check(cp, index, CHECK_NULL); duke@435: constantTag tag = cp->tag_at(index); duke@435: if (!tag.is_symbol()) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index"); duke@435: } coleenp@2497: Symbol* sym = cp->symbol_at(index); duke@435: Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(str()); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: // Assertion support. ////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls)) duke@435: JVMWrapper("JVM_DesiredAssertionStatus"); duke@435: assert(cls != NULL, "bad class"); duke@435: duke@435: oop r = JNIHandles::resolve(cls); duke@435: assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed"); duke@435: if (java_lang_Class::is_primitive(r)) return false; duke@435: coleenp@4037: Klass* k = java_lang_Class::as_Klass(r); hseigel@4278: assert(k->oop_is_instance(), "must be an instance klass"); hseigel@4278: if (! k->oop_is_instance()) return false; duke@435: duke@435: ResourceMark rm(THREAD); hseigel@4278: const char* name = k->name()->as_C_string(); hseigel@4278: bool system_class = k->class_loader() == NULL; duke@435: return JavaAssertions::enabled(name, system_class); duke@435: duke@435: JVM_END duke@435: duke@435: duke@435: // Return a new AssertionStatusDirectives object with the fields filled in with duke@435: // command-line assertion arguments (i.e., -ea, -da). duke@435: JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused)) duke@435: JVMWrapper("JVM_AssertionStatusDirectives"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL); duke@435: return JNIHandles::make_local(env, asd); duke@435: JVM_END duke@435: duke@435: // Verification //////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: // Reflection for the verifier ///////////////////////////////////////////////////////////////// duke@435: duke@435: // RedefineClasses support: bug 6214132 caused verification to fail. duke@435: // All functions from this section should call the jvmtiThreadSate function: coleenp@4037: // Klass* class_to_verify_considering_redefinition(Klass* klass). coleenp@4037: // The function returns a Klass* of the _scratch_class if the verifier duke@435: // was invoked in the middle of the class redefinition. coleenp@4037: // Otherwise it returns its argument value which is the _the_class Klass*. duke@435: // Please, refer to the description in the jvmtiThreadSate.hpp. duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); hseigel@4278: return k->name()->as_utf8(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types)) duke@435: JVMWrapper("JVM_GetClassCPTypes"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: // types will have length zero if this is not an InstanceKlass duke@435: // (length is determined by call to JVM_GetClassCPEntriesCount) hseigel@4278: if (k->oop_is_instance()) { coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: for (int index = cp->length() - 1; index >= 0; index--) { duke@435: constantTag tag = cp->tag_at(index); coleenp@4037: types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class : tag.value(); duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassCPEntriesCount"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); hseigel@4278: if (!k->oop_is_instance()) duke@435: return 0; coleenp@4037: return InstanceKlass::cast(k)->constants()->length(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassFieldsCount"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); hseigel@4278: if (!k->oop_is_instance()) duke@435: return 0; coleenp@4037: return InstanceKlass::cast(k)->java_fields_count(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls)) duke@435: JVMWrapper("JVM_GetClassMethodsCount"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); hseigel@4278: if (!k->oop_is_instance()) duke@435: return 0; coleenp@4037: return InstanceKlass::cast(k)->methods()->length(); duke@435: JVM_END duke@435: duke@435: duke@435: // The following methods, used for the verifier, are never called with coleenp@4037: // array klasses, so a direct cast to InstanceKlass is safe. duke@435: // Typically, these methods are called in a loop with bounds determined duke@435: // by the results of JVM_GetClass{Fields,Methods}Count, which return duke@435: // zero for arrays. duke@435: JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions)) duke@435: JVMWrapper("JVM_GetMethodIxExceptionIndexes"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: int length = method->checked_exceptions_length(); duke@435: if (length > 0) { coleenp@4037: CheckedExceptionElement* table= method->checked_exceptions_start(); duke@435: for (int i = 0; i < length; i++) { duke@435: exceptions[i] = table[i].class_cp_index; duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index)) duke@435: JVMWrapper("JVM_GetMethodIxExceptionsCount"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->checked_exceptions_length(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code)) duke@435: JVMWrapper("JVM_GetMethodIxByteCode"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: memcpy(code, method->code_base(), method->code_size()); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index)) duke@435: JVMWrapper("JVM_GetMethodIxByteCodeLength"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->code_size(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry)) duke@435: JVMWrapper("JVM_GetMethodIxExceptionTableEntry"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: ExceptionTable extable(method); jiangli@3917: entry->start_pc = extable.start_pc(entry_index); jiangli@3917: entry->end_pc = extable.end_pc(entry_index); jiangli@3917: entry->handler_pc = extable.handler_pc(entry_index); jiangli@3917: entry->catchType = extable.catch_type_index(entry_index); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_GetMethodIxExceptionTableLength"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->exception_table_length(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_GetMethodIxModifiers"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index)) duke@435: JVMWrapper("JVM_GetFieldIxModifiers"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_GetMethodIxLocalsCount"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->max_locals(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_GetMethodIxArgsSize"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->size_of_parameters(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_GetMethodIxMaxStack"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->verifier_max_stack(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index)) duke@435: JVMWrapper("JVM_IsConstructorIx"); duke@435: ResourceMark rm(THREAD); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->name() == vmSymbols::object_initializer_name(); duke@435: JVM_END duke@435: duke@435: acorn@4499: JVM_QUICK_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index)) acorn@4499: JVMWrapper("JVM_IsVMGeneratedMethodIx"); acorn@4499: ResourceMark rm(THREAD); acorn@4499: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); acorn@4499: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); acorn@4499: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); acorn@4499: return method->is_overpass(); acorn@4499: JVM_END acorn@4499: duke@435: JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index)) duke@435: JVMWrapper("JVM_GetMethodIxIxUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->name()->as_utf8(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index)) duke@435: JVMWrapper("JVM_GetMethodIxSignatureUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: Method* method = InstanceKlass::cast(k)->methods()->at(method_index); coleenp@4037: return method->signature()->as_utf8(); duke@435: JVM_END duke@435: duke@435: /** duke@435: * All of these JVM_GetCP-xxx methods are used by the old verifier to duke@435: * read entries in the constant pool. Since the old verifier always duke@435: * works on a copy of the code, it will not see any rewriting that duke@435: * may possibly occur in the middle of verification. So it is important duke@435: * that nothing it calls tries to use the cpCache instead of the raw duke@435: * constant pool, so we must use cp->uncached_x methods when appropriate. duke@435: */ duke@435: JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPFieldNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Fieldref: duke@435: return cp->uncached_name_ref_at(cp_index)->as_utf8(); duke@435: default: duke@435: fatal("JVM_GetCPFieldNameUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPMethodNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_Methodref: jrose@1494: case JVM_CONSTANT_NameAndType: // for invokedynamic duke@435: return cp->uncached_name_ref_at(cp_index)->as_utf8(); duke@435: default: duke@435: fatal("JVM_GetCPMethodNameUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPMethodSignatureUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_InterfaceMethodref: duke@435: case JVM_CONSTANT_Methodref: jrose@1494: case JVM_CONSTANT_NameAndType: // for invokedynamic duke@435: return cp->uncached_signature_ref_at(cp_index)->as_utf8(); duke@435: default: duke@435: fatal("JVM_GetCPMethodSignatureUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPFieldSignatureUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Fieldref: duke@435: return cp->uncached_signature_ref_at(cp_index)->as_utf8(); duke@435: default: duke@435: fatal("JVM_GetCPFieldSignatureUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPClassNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); coleenp@2497: Symbol* classname = cp->klass_name_at(cp_index); duke@435: return classname->as_utf8(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPFieldClassNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Fieldref: { duke@435: int class_index = cp->uncached_klass_ref_index_at(cp_index); coleenp@2497: Symbol* classname = cp->klass_name_at(class_index); duke@435: return classname->as_utf8(); duke@435: } duke@435: default: duke@435: fatal("JVM_GetCPFieldClassNameUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index)) duke@435: JVMWrapper("JVM_GetCPMethodClassNameUTF"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_InterfaceMethodref: { duke@435: int class_index = cp->uncached_klass_ref_index_at(cp_index); coleenp@2497: Symbol* classname = cp->klass_name_at(class_index); duke@435: return classname->as_utf8(); duke@435: } duke@435: default: duke@435: fatal("JVM_GetCPMethodClassNameUTF: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: never@3137: JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) duke@435: JVMWrapper("JVM_GetCPFieldModifiers"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); coleenp@4037: Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); duke@435: k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); coleenp@4037: ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Fieldref: { coleenp@2497: Symbol* name = cp->uncached_name_ref_at(cp_index); coleenp@2497: Symbol* signature = cp->uncached_signature_ref_at(cp_index); never@3137: for (JavaFieldStream fs(k_called); !fs.done(); fs.next()) { never@3137: if (fs.name() == name && fs.signature() == signature) { never@3137: return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS; duke@435: } duke@435: } duke@435: return -1; duke@435: } duke@435: default: duke@435: fatal("JVM_GetCPFieldModifiers: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls)) duke@435: JVMWrapper("JVM_GetCPMethodModifiers"); coleenp@4037: Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls)); coleenp@4037: Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls)); duke@435: k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread); duke@435: k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread); coleenp@4037: ConstantPool* cp = InstanceKlass::cast(k)->constants(); duke@435: switch (cp->tag_at(cp_index).value()) { duke@435: case JVM_CONSTANT_Methodref: duke@435: case JVM_CONSTANT_InterfaceMethodref: { coleenp@2497: Symbol* name = cp->uncached_name_ref_at(cp_index); coleenp@2497: Symbol* signature = cp->uncached_signature_ref_at(cp_index); coleenp@4037: Array* methods = InstanceKlass::cast(k_called)->methods(); duke@435: int methods_count = methods->length(); duke@435: for (int i = 0; i < methods_count; i++) { coleenp@4037: Method* method = methods->at(i); duke@435: if (method->name() == name && method->signature() == signature) { duke@435: return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS; duke@435: } duke@435: } duke@435: return -1; duke@435: } duke@435: default: duke@435: fatal("JVM_GetCPMethodModifiers: illegal constant"); duke@435: } duke@435: ShouldNotReachHere(); duke@435: return 0; duke@435: JVM_END duke@435: duke@435: duke@435: // Misc ////////////////////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(void, JVM_ReleaseUTF(const char *utf)) duke@435: // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)) duke@435: JVMWrapper("JVM_IsSameClassPackage"); duke@435: oop class1_mirror = JNIHandles::resolve_non_null(class1); duke@435: oop class2_mirror = JNIHandles::resolve_non_null(class2); coleenp@4037: Klass* klass1 = java_lang_Class::as_Klass(class1_mirror); coleenp@4037: Klass* klass2 = java_lang_Class::as_Klass(class2_mirror); duke@435: return (jboolean) Reflection::is_same_class_package(klass1, klass2); duke@435: JVM_END duke@435: duke@435: duke@435: // IO functions //////////////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jint, JVM_Open(const char *fname, jint flags, jint mode)) duke@435: JVMWrapper2("JVM_Open (%s)", fname); duke@435: duke@435: //%note jvm_r6 ikrylov@2322: int result = os::open(fname, flags, mode); duke@435: if (result >= 0) { duke@435: return result; duke@435: } else { duke@435: switch(errno) { duke@435: case EEXIST: duke@435: return JVM_EEXIST; duke@435: default: duke@435: return -1; duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Close(jint fd)) duke@435: JVMWrapper2("JVM_Close (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::close(fd); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Read(jint fd, char *buf, jint nbytes)) duke@435: JVMWrapper2("JVM_Read (0x%x)", fd); duke@435: duke@435: //%note jvm_r6 ikrylov@2322: return (jint)os::restartable_read(fd, buf, nbytes); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Write(jint fd, char *buf, jint nbytes)) duke@435: JVMWrapper2("JVM_Write (0x%x)", fd); duke@435: duke@435: //%note jvm_r6 ikrylov@2322: return (jint)os::write(fd, buf, nbytes); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Available(jint fd, jlong *pbytes)) duke@435: JVMWrapper2("JVM_Available (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::available(fd, pbytes); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jlong, JVM_Lseek(jint fd, jlong offset, jint whence)) duke@435: JVMWrapper4("JVM_Lseek (0x%x, %Ld, %d)", fd, offset, whence); duke@435: //%note jvm_r6 ikrylov@2322: return os::lseek(fd, offset, whence); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SetLength(jint fd, jlong length)) duke@435: JVMWrapper3("JVM_SetLength (0x%x, %Ld)", fd, length); ikrylov@2322: return os::ftruncate(fd, length); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Sync(jint fd)) duke@435: JVMWrapper2("JVM_Sync (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::fsync(fd); duke@435: JVM_END duke@435: duke@435: duke@435: // Printing support ////////////////////////////////////////////////// duke@435: extern "C" { duke@435: duke@435: int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) { duke@435: // see bug 4399518, 4417214 duke@435: if ((intptr_t)count <= 0) return -1; duke@435: return vsnprintf(str, count, fmt, args); duke@435: } duke@435: duke@435: duke@435: int jio_snprintf(char *str, size_t count, const char *fmt, ...) { duke@435: va_list args; duke@435: int len; duke@435: va_start(args, fmt); duke@435: len = jio_vsnprintf(str, count, fmt, args); duke@435: va_end(args); duke@435: return len; duke@435: } duke@435: duke@435: duke@435: int jio_fprintf(FILE* f, const char *fmt, ...) { duke@435: int len; duke@435: va_list args; duke@435: va_start(args, fmt); duke@435: len = jio_vfprintf(f, fmt, args); duke@435: va_end(args); duke@435: return len; duke@435: } duke@435: duke@435: duke@435: int jio_vfprintf(FILE* f, const char *fmt, va_list args) { duke@435: if (Arguments::vfprintf_hook() != NULL) { duke@435: return Arguments::vfprintf_hook()(f, fmt, args); duke@435: } else { duke@435: return vfprintf(f, fmt, args); duke@435: } duke@435: } duke@435: duke@435: coleenp@2507: JNIEXPORT int jio_printf(const char *fmt, ...) { duke@435: int len; duke@435: va_list args; duke@435: va_start(args, fmt); duke@435: len = jio_vfprintf(defaultStream::output_stream(), fmt, args); duke@435: va_end(args); duke@435: return len; duke@435: } duke@435: duke@435: duke@435: // HotSpot specific jio method duke@435: void jio_print(const char* s) { duke@435: // Try to make this function as atomic as possible. duke@435: if (Arguments::vfprintf_hook() != NULL) { duke@435: jio_fprintf(defaultStream::output_stream(), "%s", s); duke@435: } else { xlu@948: // Make an unused local variable to avoid warning from gcc 4.x compiler. xlu@948: size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s)); duke@435: } duke@435: } duke@435: duke@435: } // Extern C duke@435: duke@435: // java.lang.Thread ////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock duke@435: // to prevent the target thread from exiting after we have a pointer to the C++ Thread or duke@435: // OSThread objects. The exception to this rule is when the target object is the thread duke@435: // doing the operation, in which case we know that the thread won't exit until the duke@435: // operation is done (all exits being voluntary). There are a few cases where it is duke@435: // rather silly to do operations on yourself, like resuming yourself or asking whether duke@435: // you are alive. While these can still happen, they are not subject to deadlocks if duke@435: // the lock is held while the operation occurs (this is not the case for suspend, for duke@435: // instance), and are very unlikely. Because IsAlive needs to be fast and its duke@435: // implementation is local to this file, we always lock Threads_lock for that one. duke@435: duke@435: static void thread_entry(JavaThread* thread, TRAPS) { duke@435: HandleMark hm(THREAD); duke@435: Handle obj(THREAD, thread->threadObj()); duke@435: JavaValue result(T_VOID); duke@435: JavaCalls::call_virtual(&result, duke@435: obj, never@1577: KlassHandle(THREAD, SystemDictionary::Thread_klass()), coleenp@2497: vmSymbols::run_method_name(), coleenp@2497: vmSymbols::void_method_signature(), duke@435: THREAD); duke@435: } duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_StartThread"); duke@435: JavaThread *native_thread = NULL; duke@435: duke@435: // We cannot hold the Threads_lock when we throw an exception, duke@435: // due to rank ordering issues. Example: we might need to grab the duke@435: // Heap_lock while we construct the exception. duke@435: bool throw_illegal_thread_state = false; duke@435: duke@435: // We must release the Threads_lock before we can post a jvmti event duke@435: // in Thread::start. duke@435: { duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before duke@435: // we operate. duke@435: MutexLocker mu(Threads_lock); duke@435: dholmes@2482: // Since JDK 5 the java.lang.Thread threadStatus is used to prevent dholmes@2482: // re-starting an already started thread, so we should usually find dholmes@2482: // that the JavaThread is null. However for a JNI attached thread dholmes@2482: // there is a small window between the Thread object being created dholmes@2482: // (with its JavaThread set) and the update to its threadStatus, so we dholmes@2482: // have to check for this dholmes@2482: if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) { dholmes@2482: throw_illegal_thread_state = true; duke@435: } else { dholmes@2482: // We could also check the stillborn flag to see if this thread was already stopped, but dholmes@2482: // for historical reasons we let the thread detect that itself when it starts running dholmes@2482: duke@435: jlong size = duke@435: java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread)); duke@435: // Allocate the C++ Thread structure and create the native thread. The duke@435: // stack size retrieved from java is signed, but the constructor takes duke@435: // size_t (an unsigned type), so avoid passing negative values which would duke@435: // result in really large stacks. duke@435: size_t sz = size > 0 ? (size_t) size : 0; duke@435: native_thread = new JavaThread(&thread_entry, sz); duke@435: duke@435: // At this point it may be possible that no osthread was created for the duke@435: // JavaThread due to lack of memory. Check for this situation and throw duke@435: // an exception if necessary. Eventually we may want to change this so duke@435: // that we only grab the lock if the thread was created successfully - duke@435: // then we can also do this check and throw the exception in the duke@435: // JavaThread constructor. duke@435: if (native_thread->osthread() != NULL) { duke@435: // Note: the current thread is not being used within "prepare". duke@435: native_thread->prepare(jthread); duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (throw_illegal_thread_state) { duke@435: THROW(vmSymbols::java_lang_IllegalThreadStateException()); duke@435: } duke@435: duke@435: assert(native_thread != NULL, "Starting null thread?"); duke@435: duke@435: if (native_thread->osthread() == NULL) { duke@435: // No one should hold a reference to the 'native_thread'. duke@435: delete native_thread; duke@435: if (JvmtiExport::should_post_resource_exhausted()) { duke@435: JvmtiExport::post_resource_exhausted( duke@435: JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS, duke@435: "unable to create new native thread"); duke@435: } duke@435: THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(), duke@435: "unable to create new native thread"); duke@435: } duke@435: duke@435: Thread::start(native_thread); duke@435: duke@435: JVM_END duke@435: duke@435: // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints duke@435: // before the quasi-asynchronous exception is delivered. This is a little obtrusive, duke@435: // but is thought to be reliable and simple. In the case, where the receiver is the dholmes@2482: // same thread as the sender, no safepoint is needed. duke@435: JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable)) duke@435: JVMWrapper("JVM_StopThread"); duke@435: duke@435: oop java_throwable = JNIHandles::resolve(throwable); duke@435: if (java_throwable == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: JavaThread* receiver = java_lang_Thread::thread(java_thread); never@3499: Events::log_exception(JavaThread::current(), never@3499: "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", never@3499: receiver, (address)java_thread, throwable); dholmes@2482: // First check if thread is alive duke@435: if (receiver != NULL) { duke@435: // Check if exception is getting thrown at self (use oop equality, since the duke@435: // target object might exit) duke@435: if (java_thread == thread->threadObj()) { duke@435: THROW_OOP(java_throwable); duke@435: } else { duke@435: // Enques a VM_Operation to stop all threads and then deliver the exception... duke@435: Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable)); duke@435: } duke@435: } dholmes@2482: else { dholmes@2482: // Either: dholmes@2482: // - target thread has not been started before being stopped, or dholmes@2482: // - target thread already terminated dholmes@2482: // We could read the threadStatus to determine which case it is dholmes@2482: // but that is overkill as it doesn't matter. We must set the dholmes@2482: // stillborn flag for the first case, and if the thread has already dholmes@2482: // exited setting this flag has no affect dholmes@2482: java_lang_Thread::set_stillborn(java_thread); dholmes@2482: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_IsThreadAlive"); duke@435: duke@435: oop thread_oop = JNIHandles::resolve_non_null(jthread); duke@435: return java_lang_Thread::is_alive(thread_oop); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_SuspendThread"); duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: JavaThread* receiver = java_lang_Thread::thread(java_thread); duke@435: duke@435: if (receiver != NULL) { duke@435: // thread has run and has not exited (still on threads list) duke@435: duke@435: { duke@435: MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag); duke@435: if (receiver->is_external_suspend()) { duke@435: // Don't allow nested external suspend requests. We can't return duke@435: // an error from this interface so just ignore the problem. duke@435: return; duke@435: } duke@435: if (receiver->is_exiting()) { // thread is in the process of exiting duke@435: return; duke@435: } duke@435: receiver->set_external_suspend(); duke@435: } duke@435: duke@435: // java_suspend() will catch threads in the process of exiting duke@435: // and will ignore them. duke@435: receiver->java_suspend(); duke@435: duke@435: // It would be nice to have the following assertion in all the duke@435: // time, but it is possible for a racing resume request to have duke@435: // resumed this thread right after we suspended it. Temporarily duke@435: // enable this assertion if you are chasing a different kind of duke@435: // bug. duke@435: // duke@435: // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL || duke@435: // receiver->is_being_ext_suspended(), "thread is not suspended"); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_ResumeThread"); duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before we operate. duke@435: // We need to *always* get the threads lock here, since this operation cannot be allowed during duke@435: // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other duke@435: // threads randomly resumes threads, then a thread might not be suspended when the safepoint code duke@435: // looks at it. duke@435: MutexLocker ml(Threads_lock); duke@435: JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); duke@435: if (thr != NULL) { duke@435: // the thread has run and is not in the process of exiting duke@435: thr->java_resume(); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio)) duke@435: JVMWrapper("JVM_SetThreadPriority"); duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before we operate duke@435: MutexLocker ml(Threads_lock); duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio); duke@435: JavaThread* thr = java_lang_Thread::thread(java_thread); duke@435: if (thr != NULL) { // Thread not yet started; priority pushed down when it is duke@435: Thread::set_priority(thr, (ThreadPriority)prio); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass)) duke@435: JVMWrapper("JVM_Yield"); duke@435: if (os::dont_yield()) return; dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE0(hotspot, thread__yield); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_YIELD(); dcubed@3202: #endif /* USDT2 */ duke@435: // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield. duke@435: // Critical for similar threading behaviour duke@435: if (ConvertYieldToSleep) { duke@435: os::sleep(thread, MinSleepInterval, false); duke@435: } else { duke@435: os::yield(); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis)) duke@435: JVMWrapper("JVM_Sleep"); duke@435: duke@435: if (millis < 0) { duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative"); duke@435: } duke@435: duke@435: if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) { duke@435: THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); duke@435: } duke@435: duke@435: // Save current thread state and restore it at the end of this block. duke@435: // And set new thread state to SLEEPING. duke@435: JavaThreadSleepState jtss(thread); duke@435: dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE1(hotspot, thread__sleep__begin, millis); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_SLEEP_BEGIN( dcubed@3202: millis); dcubed@3202: #endif /* USDT2 */ fparain@1759: duke@435: if (millis == 0) { duke@435: // When ConvertSleepToYield is on, this matches the classic VM implementation of duke@435: // JVM_Sleep. Critical for similar threading behaviour (Win32) duke@435: // It appears that in certain GUI contexts, it may be beneficial to do a short sleep duke@435: // for SOLARIS duke@435: if (ConvertSleepToYield) { duke@435: os::yield(); duke@435: } else { duke@435: ThreadState old_state = thread->osthread()->get_state(); duke@435: thread->osthread()->set_state(SLEEPING); duke@435: os::sleep(thread, MinSleepInterval, false); duke@435: thread->osthread()->set_state(old_state); duke@435: } duke@435: } else { duke@435: ThreadState old_state = thread->osthread()->get_state(); duke@435: thread->osthread()->set_state(SLEEPING); duke@435: if (os::sleep(thread, millis, true) == OS_INTRPT) { duke@435: // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on duke@435: // us while we were sleeping. We do not overwrite those. duke@435: if (!HAS_PENDING_EXCEPTION) { dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE1(hotspot, thread__sleep__end,1); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_SLEEP_END( dcubed@3202: 1); dcubed@3202: #endif /* USDT2 */ duke@435: // TODO-FIXME: THROW_MSG returns which means we will not call set_state() duke@435: // to properly restore the thread state. That's likely wrong. duke@435: THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted"); duke@435: } duke@435: } duke@435: thread->osthread()->set_state(old_state); duke@435: } dcubed@3202: #ifndef USDT2 fparain@1759: HS_DTRACE_PROBE1(hotspot, thread__sleep__end,0); dcubed@3202: #else /* USDT2 */ dcubed@3202: HOTSPOT_THREAD_SLEEP_END( dcubed@3202: 0); dcubed@3202: #endif /* USDT2 */ duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass)) duke@435: JVMWrapper("JVM_CurrentThread"); duke@435: oop jthread = thread->threadObj(); duke@435: assert (thread != NULL, "no current thread!"); duke@435: return JNIHandles::make_local(env, jthread); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_CountStackFrames"); duke@435: duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before we operate duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: bool throw_illegal_thread_state = false; duke@435: int count = 0; duke@435: duke@435: { duke@435: MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); duke@435: // We need to re-resolve the java_thread, since a GC might have happened during the duke@435: // acquire of the lock duke@435: JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); duke@435: duke@435: if (thr == NULL) { duke@435: // do nothing duke@435: } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) { duke@435: // Check whether this java thread has been suspended already. If not, throws duke@435: // IllegalThreadStateException. We defer to throw that exception until duke@435: // Threads_lock is released since loading exception class has to leave VM. duke@435: // The correct way to test a thread is actually suspended is duke@435: // wait_for_ext_suspend_completion(), but we can't call that while holding duke@435: // the Threads_lock. The above tests are sufficient for our purposes duke@435: // provided the walkability of the stack is stable - which it isn't duke@435: // 100% but close enough for most practical purposes. duke@435: throw_illegal_thread_state = true; duke@435: } else { duke@435: // Count all java activation, i.e., number of vframes duke@435: for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) { duke@435: // Native frames are not counted duke@435: if (!vfst.method()->is_native()) count++; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (throw_illegal_thread_state) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(), duke@435: "this thread is not suspended"); duke@435: } duke@435: return count; duke@435: JVM_END duke@435: duke@435: // Consider: A better way to implement JVM_Interrupt() is to acquire duke@435: // Threads_lock to resolve the jthread into a Thread pointer, fetch duke@435: // Thread->platformevent, Thread->native_thr, Thread->parker, etc., duke@435: // drop Threads_lock, and the perform the unpark() and thr_kill() operations duke@435: // outside the critical section. Threads_lock is hot so we want to minimize duke@435: // the hold-time. A cleaner interface would be to decompose interrupt into duke@435: // two steps. The 1st phase, performed under Threads_lock, would return duke@435: // a closure that'd be invoked after Threads_lock was dropped. duke@435: // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and duke@435: // admit spurious wakeups. duke@435: duke@435: JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread)) duke@435: JVMWrapper("JVM_Interrupt"); duke@435: duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before we operate duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); duke@435: // We need to re-resolve the java_thread, since a GC might have happened during the duke@435: // acquire of the lock duke@435: JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); duke@435: if (thr != NULL) { duke@435: Thread::interrupt(thr); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted)) duke@435: JVMWrapper("JVM_IsInterrupted"); duke@435: duke@435: // Ensure that the C++ Thread and OSThread structures aren't freed before we operate duke@435: oop java_thread = JNIHandles::resolve_non_null(jthread); duke@435: MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock); duke@435: // We need to re-resolve the java_thread, since a GC might have happened during the duke@435: // acquire of the lock duke@435: JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)); duke@435: if (thr == NULL) { duke@435: return JNI_FALSE; duke@435: } else { duke@435: return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: // Return true iff the current thread has locked the object passed in duke@435: duke@435: JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj)) duke@435: JVMWrapper("JVM_HoldsLock"); duke@435: assert(THREAD->is_Java_thread(), "sanity check"); duke@435: if (obj == NULL) { duke@435: THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE); duke@435: } duke@435: Handle h_obj(THREAD, JNIHandles::resolve(obj)); duke@435: return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass)) duke@435: JVMWrapper("JVM_DumpAllStacks"); duke@435: VM_PrintThreads op; duke@435: VMThread::execute(&op); duke@435: if (JvmtiExport::should_post_data_dump()) { duke@435: JvmtiExport::post_data_dump(); duke@435: } duke@435: JVM_END duke@435: dcubed@3202: JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name)) dcubed@3202: JVMWrapper("JVM_SetNativeThreadName"); dcubed@3202: ResourceMark rm(THREAD); dcubed@3202: oop java_thread = JNIHandles::resolve_non_null(jthread); dcubed@3202: JavaThread* thr = java_lang_Thread::thread(java_thread); dcubed@3202: // Thread naming only supported for the current thread, doesn't work for dcubed@3202: // target threads. dcubed@3202: if (Thread::current() == thr && !thr->has_attached_via_jni()) { dcubed@3202: // we don't set the name of an attached thread to avoid stepping dcubed@3202: // on other programs dcubed@3202: const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); dcubed@3202: os::set_native_thread_name(thread_name); dcubed@3202: } dcubed@3202: JVM_END duke@435: duke@435: // java.lang.SecurityManager /////////////////////////////////////////////////////////////////////// duke@435: duke@435: static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) { duke@435: assert(jthread->is_Java_thread(), "must be a Java thread"); duke@435: if (jthread->privileged_stack_top() == NULL) return false; duke@435: if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) { duke@435: oop loader = jthread->privileged_stack_top()->class_loader(); duke@435: if (loader == NULL) return true; duke@435: bool trusted = java_lang_ClassLoader::is_trusted_loader(loader); duke@435: if (trusted) return true; duke@435: } duke@435: return false; duke@435: } duke@435: duke@435: JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env)) duke@435: JVMWrapper("JVM_CurrentLoadedClass"); duke@435: ResourceMark rm(THREAD); duke@435: duke@435: for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { duke@435: // if a method in a class in a trusted loader is in a doPrivileged, return NULL duke@435: bool trusted = is_trusted_frame(thread, &vfst); duke@435: if (trusted) return NULL; duke@435: coleenp@4037: Method* m = vfst.method(); duke@435: if (!m->is_native()) { coleenp@4251: InstanceKlass* holder = m->method_holder(); coleenp@4251: oop loader = holder->class_loader(); duke@435: if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { coleenp@4251: return (jclass) JNIHandles::make_local(env, holder->java_mirror()); duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env)) duke@435: JVMWrapper("JVM_CurrentClassLoader"); duke@435: ResourceMark rm(THREAD); duke@435: duke@435: for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { duke@435: duke@435: // if a method in a class in a trusted loader is in a doPrivileged, return NULL duke@435: bool trusted = is_trusted_frame(thread, &vfst); duke@435: if (trusted) return NULL; duke@435: coleenp@4037: Method* m = vfst.method(); duke@435: if (!m->is_native()) { coleenp@4251: InstanceKlass* holder = m->method_holder(); duke@435: assert(holder->is_klass(), "just checking"); coleenp@4251: oop loader = holder->class_loader(); duke@435: if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { duke@435: return JNIHandles::make_local(env, loader); duke@435: } duke@435: } duke@435: } duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: // Utility object for collecting method holders walking down the stack duke@435: class KlassLink: public ResourceObj { duke@435: public: duke@435: KlassHandle klass; duke@435: KlassLink* next; duke@435: duke@435: KlassLink(KlassHandle k) { klass = k; next = NULL; } duke@435: }; duke@435: duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env)) duke@435: JVMWrapper("JVM_GetClassContext"); duke@435: ResourceMark rm(THREAD); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: // Collect linked list of (handles to) method holders duke@435: KlassLink* first = NULL; duke@435: KlassLink* last = NULL; duke@435: int depth = 0; twisti@4866: vframeStream vfst(thread); twisti@4866: twisti@4866: if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) { twisti@4866: // This must only be called from SecurityManager.getClassContext twisti@4866: Method* m = vfst.method(); twisti@4866: if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() && twisti@4866: m->name() == vmSymbols::getClassContext_name() && twisti@4866: m->signature() == vmSymbols::void_class_array_signature())) { twisti@4866: THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext"); twisti@4866: } twisti@4866: } twisti@4866: twisti@4866: // Collect method holders twisti@4866: for (; !vfst.at_end(); vfst.security_next()) { twisti@4866: Method* m = vfst.method(); duke@435: // Native frames are not returned twisti@4866: if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) { twisti@4866: Klass* holder = m->method_holder(); duke@435: assert(holder->is_klass(), "just checking"); duke@435: depth++; duke@435: KlassLink* l = new KlassLink(KlassHandle(thread, holder)); duke@435: if (first == NULL) { duke@435: first = last = l; duke@435: } else { duke@435: last->next = l; duke@435: last = l; duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Create result array of type [Ljava/lang/Class; never@1577: objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), depth, CHECK_NULL); duke@435: // Fill in mirrors corresponding to method holders duke@435: int index = 0; duke@435: while (first != NULL) { hseigel@4278: result->obj_at_put(index++, first->klass()->java_mirror()); duke@435: first = first->next; duke@435: } duke@435: assert(index == depth, "just checking"); duke@435: duke@435: return (jobjectArray) JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name)) duke@435: JVMWrapper("JVM_ClassDepth"); duke@435: ResourceMark rm(THREAD); duke@435: Handle h_name (THREAD, JNIHandles::resolve_non_null(name)); duke@435: Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0); duke@435: duke@435: const char* str = java_lang_String::as_utf8_string(class_name_str()); coleenp@2497: TempNewSymbol class_name_sym = SymbolTable::probe(str, (int)strlen(str)); coleenp@2497: if (class_name_sym == NULL) { duke@435: return -1; duke@435: } duke@435: duke@435: int depth = 0; duke@435: duke@435: for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { duke@435: if (!vfst.method()->is_native()) { coleenp@4251: InstanceKlass* holder = vfst.method()->method_holder(); duke@435: assert(holder->is_klass(), "just checking"); coleenp@4251: if (holder->name() == class_name_sym) { duke@435: return depth; duke@435: } duke@435: depth++; duke@435: } duke@435: } duke@435: return -1; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env)) duke@435: JVMWrapper("JVM_ClassLoaderDepth"); duke@435: ResourceMark rm(THREAD); duke@435: int depth = 0; duke@435: for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { duke@435: // if a method in a class in a trusted loader is in a doPrivileged, return -1 duke@435: bool trusted = is_trusted_frame(thread, &vfst); duke@435: if (trusted) return -1; duke@435: coleenp@4037: Method* m = vfst.method(); duke@435: if (!m->is_native()) { coleenp@4251: InstanceKlass* holder = m->method_holder(); duke@435: assert(holder->is_klass(), "just checking"); coleenp@4251: oop loader = holder->class_loader(); duke@435: if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) { duke@435: return depth; duke@435: } duke@435: depth++; duke@435: } duke@435: } duke@435: return -1; duke@435: JVM_END duke@435: duke@435: duke@435: // java.lang.Package //////////////////////////////////////////////////////////////// duke@435: duke@435: duke@435: JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name)) duke@435: JVMWrapper("JVM_GetSystemPackage"); duke@435: ResourceMark rm(THREAD); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); duke@435: oop result = ClassLoader::get_system_package(str, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env)) duke@435: JVMWrapper("JVM_GetSystemPackages"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL); duke@435: return (jobjectArray) JNIHandles::make_local(result); duke@435: JVM_END duke@435: duke@435: duke@435: // ObjectInputStream /////////////////////////////////////////////////////////////// duke@435: coleenp@4037: bool force_verify_field_access(Klass* current_class, Klass* field_class, AccessFlags access, bool classloader_only) { duke@435: if (current_class == NULL) { duke@435: return true; duke@435: } duke@435: if ((current_class == field_class) || access.is_public()) { duke@435: return true; duke@435: } duke@435: duke@435: if (access.is_protected()) { duke@435: // See if current_class is a subclass of field_class hseigel@4278: if (current_class->is_subclass_of(field_class)) { duke@435: return true; duke@435: } duke@435: } duke@435: coleenp@4037: return (!access.is_private() && InstanceKlass::cast(current_class)->is_same_class_package(field_class)); duke@435: } duke@435: duke@435: duke@435: // JVM_AllocateNewObject and JVM_AllocateNewArray are unused as of 1.4 duke@435: JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass)) duke@435: JVMWrapper("JVM_AllocateNewObject"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: // Receiver is not used duke@435: oop curr_mirror = JNIHandles::resolve_non_null(currClass); duke@435: oop init_mirror = JNIHandles::resolve_non_null(initClass); duke@435: duke@435: // Cannot instantiate primitive types duke@435: if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) { duke@435: ResourceMark rm(THREAD); duke@435: THROW_0(vmSymbols::java_lang_InvalidClassException()); duke@435: } duke@435: duke@435: // Arrays not allowed here, must use JVM_AllocateNewArray hseigel@4278: if (java_lang_Class::as_Klass(curr_mirror)->oop_is_array() || hseigel@4278: java_lang_Class::as_Klass(init_mirror)->oop_is_array()) { duke@435: ResourceMark rm(THREAD); duke@435: THROW_0(vmSymbols::java_lang_InvalidClassException()); duke@435: } duke@435: coleenp@4037: instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_Klass(curr_mirror)); coleenp@4037: instanceKlassHandle init_klass (THREAD, java_lang_Class::as_Klass(init_mirror)); duke@435: duke@435: assert(curr_klass->is_subclass_of(init_klass()), "just checking"); duke@435: duke@435: // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly. duke@435: curr_klass->check_valid_for_instantiation(false, CHECK_NULL); duke@435: duke@435: // Make sure klass is initialized, since we are about to instantiate one of them. duke@435: curr_klass->initialize(CHECK_NULL); duke@435: duke@435: methodHandle m (THREAD, duke@435: init_klass->find_method(vmSymbols::object_initializer_name(), duke@435: vmSymbols::void_method_signature())); duke@435: if (m.is_null()) { duke@435: ResourceMark rm(THREAD); duke@435: THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(), hseigel@4278: Method::name_and_sig_as_C_string(init_klass(), duke@435: vmSymbols::object_initializer_name(), duke@435: vmSymbols::void_method_signature())); duke@435: } duke@435: duke@435: if (curr_klass == init_klass && !m->is_public()) { duke@435: // Calling the constructor for class 'curr_klass'. duke@435: // Only allow calls to a public no-arg constructor. duke@435: // This path corresponds to creating an Externalizable object. duke@435: THROW_0(vmSymbols::java_lang_IllegalAccessException()); duke@435: } duke@435: duke@435: if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) { duke@435: // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb' duke@435: THROW_0(vmSymbols::java_lang_IllegalAccessException()); duke@435: } duke@435: duke@435: Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL); duke@435: // Call constructor m. This might call a constructor higher up in the hierachy duke@435: JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL); duke@435: duke@435: return JNIHandles::make_local(obj()); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length)) duke@435: JVMWrapper("JVM_AllocateNewArray"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: oop mirror = JNIHandles::resolve_non_null(currClass); duke@435: duke@435: if (java_lang_Class::is_primitive(mirror)) { duke@435: THROW_0(vmSymbols::java_lang_InvalidClassException()); duke@435: } coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror); duke@435: oop result; duke@435: coleenp@4037: if (k->oop_is_typeArray()) { duke@435: // typeArray coleenp@4142: result = TypeArrayKlass::cast(k)->allocate(length, CHECK_NULL); coleenp@4037: } else if (k->oop_is_objArray()) { duke@435: // objArray coleenp@4142: ObjArrayKlass* oak = ObjArrayKlass::cast(k); duke@435: oak->initialize(CHECK_NULL); // make sure class is initialized (matches Classic VM behavior) duke@435: result = oak->allocate(length, CHECK_NULL); duke@435: } else { duke@435: THROW_0(vmSymbols::java_lang_InvalidClassException()); duke@435: } duke@435: return JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: // Return the first non-null class loader up the execution stack, or null duke@435: // if only code from the null class loader is on the stack. duke@435: duke@435: JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env)) duke@435: for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) { duke@435: // UseNewReflection duke@435: vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection coleenp@4251: oop loader = vfst.method()->method_holder()->class_loader(); duke@435: if (loader != NULL) { duke@435: return JNIHandles::make_local(env, loader); duke@435: } duke@435: } duke@435: return NULL; duke@435: JVM_END duke@435: duke@435: duke@435: // Load a class relative to the most recent class on the stack with a non-null duke@435: // classloader. duke@435: // This function has been deprecated and should not be considered part of the duke@435: // specified JVM interface. duke@435: duke@435: JVM_ENTRY(jclass, JVM_LoadClass0(JNIEnv *env, jobject receiver, duke@435: jclass currClass, jstring currClassName)) duke@435: JVMWrapper("JVM_LoadClass0"); duke@435: // Receiver is not used duke@435: ResourceMark rm(THREAD); duke@435: duke@435: // Class name argument is not guaranteed to be in internal format duke@435: Handle classname (THREAD, JNIHandles::resolve_non_null(currClassName)); duke@435: Handle string = java_lang_String::internalize_classname(classname, CHECK_NULL); duke@435: duke@435: const char* str = java_lang_String::as_utf8_string(string()); duke@435: coleenp@2497: if (str == NULL || (int)strlen(str) > Symbol::max_length()) { duke@435: // It's impossible to create this class; the name cannot fit duke@435: // into the constant pool. duke@435: THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), str); duke@435: } duke@435: coleenp@2497: TempNewSymbol name = SymbolTable::new_symbol(str, CHECK_NULL); duke@435: Handle curr_klass (THREAD, JNIHandles::resolve(currClass)); duke@435: // Find the most recent class on the stack with a non-null classloader duke@435: oop loader = NULL; duke@435: oop protection_domain = NULL; duke@435: if (curr_klass.is_null()) { duke@435: for (vframeStream vfst(thread); duke@435: !vfst.at_end() && loader == NULL; duke@435: vfst.next()) { duke@435: if (!vfst.method()->is_native()) { coleenp@4251: InstanceKlass* holder = vfst.method()->method_holder(); coleenp@4251: loader = holder->class_loader(); coleenp@4251: protection_domain = holder->protection_domain(); duke@435: } duke@435: } duke@435: } else { coleenp@4037: Klass* curr_klass_oop = java_lang_Class::as_Klass(curr_klass()); coleenp@4037: loader = InstanceKlass::cast(curr_klass_oop)->class_loader(); coleenp@4037: protection_domain = InstanceKlass::cast(curr_klass_oop)->protection_domain(); duke@435: } duke@435: Handle h_loader(THREAD, loader); duke@435: Handle h_prot (THREAD, protection_domain); acorn@1092: jclass result = find_class_from_class_loader(env, name, true, h_loader, h_prot, acorn@1092: false, thread); acorn@1092: if (TraceClassResolution && result != NULL) { coleenp@4037: trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result))); acorn@1092: } acorn@1092: return result; duke@435: JVM_END duke@435: duke@435: duke@435: // Array /////////////////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: duke@435: // resolve array handle and check arguments duke@435: static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) { duke@435: if (arr == NULL) { duke@435: THROW_0(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: oop a = JNIHandles::resolve_non_null(arr); coleenp@4037: if (!a->is_array() || (type_array_only && !a->is_typeArray())) { duke@435: THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array"); duke@435: } duke@435: return arrayOop(a); duke@435: } duke@435: duke@435: duke@435: JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr)) duke@435: JVMWrapper("JVM_GetArrayLength"); duke@435: arrayOop a = check_array(env, arr, false, CHECK_0); duke@435: return a->length(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index)) duke@435: JVMWrapper("JVM_Array_Get"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: arrayOop a = check_array(env, arr, false, CHECK_NULL); duke@435: jvalue value; duke@435: BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL); duke@435: oop box = Reflection::box(&value, type, CHECK_NULL); duke@435: return JNIHandles::make_local(env, box); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode)) duke@435: JVMWrapper("JVM_GetPrimitiveArrayElement"); duke@435: jvalue value; duke@435: value.i = 0; // to initialize value before getting used in CHECK duke@435: arrayOop a = check_array(env, arr, true, CHECK_(value)); duke@435: assert(a->is_typeArray(), "just checking"); duke@435: BasicType type = Reflection::array_get(&value, a, index, CHECK_(value)); duke@435: BasicType wide_type = (BasicType) wCode; duke@435: if (type != wide_type) { duke@435: Reflection::widen(&value, type, wide_type, CHECK_(value)); duke@435: } duke@435: return value; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val)) duke@435: JVMWrapper("JVM_SetArrayElement"); duke@435: arrayOop a = check_array(env, arr, false, CHECK); duke@435: oop box = JNIHandles::resolve(val); duke@435: jvalue value; duke@435: value.i = 0; // to initialize value before getting used in CHECK duke@435: BasicType value_type; duke@435: if (a->is_objArray()) { duke@435: // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array duke@435: value_type = Reflection::unbox_for_regular_object(box, &value); duke@435: } else { duke@435: value_type = Reflection::unbox_for_primitive(box, &value, CHECK); duke@435: } duke@435: Reflection::array_set(&value, a, index, value_type, CHECK); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode)) duke@435: JVMWrapper("JVM_SetPrimitiveArrayElement"); duke@435: arrayOop a = check_array(env, arr, true, CHECK); duke@435: assert(a->is_typeArray(), "just checking"); duke@435: BasicType value_type = (BasicType) vCode; duke@435: Reflection::array_set(&v, a, index, value_type, CHECK); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length)) duke@435: JVMWrapper("JVM_NewArray"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: oop element_mirror = JNIHandles::resolve(eltClass); duke@435: oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL); duke@435: return JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim)) duke@435: JVMWrapper("JVM_NewMultiArray"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: arrayOop dim_array = check_array(env, dim, true, CHECK_NULL); duke@435: oop element_mirror = JNIHandles::resolve(eltClass); duke@435: assert(dim_array->is_typeArray(), "just checking"); duke@435: oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL); duke@435: return JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: // Networking library support //////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jint, JVM_InitializeSocketLibrary()) duke@435: JVMWrapper("JVM_InitializeSocketLibrary"); ikrylov@2322: return 0; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol)) duke@435: JVMWrapper("JVM_Socket"); ikrylov@2322: return os::socket(domain, type, protocol); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SocketClose(jint fd)) duke@435: JVMWrapper2("JVM_SocketClose (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::socket_close(fd); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SocketShutdown(jint fd, jint howto)) duke@435: JVMWrapper2("JVM_SocketShutdown (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::socket_shutdown(fd, howto); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Recv(jint fd, char *buf, jint nBytes, jint flags)) duke@435: JVMWrapper2("JVM_Recv (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::recv(fd, buf, (size_t)nBytes, (uint)flags); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Send(jint fd, char *buf, jint nBytes, jint flags)) duke@435: JVMWrapper2("JVM_Send (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::send(fd, buf, (size_t)nBytes, (uint)flags); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Timeout(int fd, long timeout)) duke@435: JVMWrapper2("JVM_Timeout (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::timeout(fd, timeout); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Listen(jint fd, jint count)) duke@435: JVMWrapper2("JVM_Listen (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::listen(fd, count); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Connect(jint fd, struct sockaddr *him, jint len)) duke@435: JVMWrapper2("JVM_Connect (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::connect(fd, him, (socklen_t)len); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Bind(jint fd, struct sockaddr *him, jint len)) duke@435: JVMWrapper2("JVM_Bind (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::bind(fd, him, (socklen_t)len); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len)) duke@435: JVMWrapper2("JVM_Accept (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: socklen_t socklen = (socklen_t)(*len); phh@3344: jint result = os::accept(fd, him, &socklen); phh@3344: *len = (jint)socklen; phh@3344: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen)) duke@435: JVMWrapper2("JVM_RecvFrom (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: socklen_t socklen = (socklen_t)(*fromlen); phh@3344: jint result = os::recvfrom(fd, buf, (size_t)nBytes, (uint)flags, from, &socklen); phh@3344: *fromlen = (int)socklen; phh@3344: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_GetSockName(jint fd, struct sockaddr *him, int *len)) duke@435: JVMWrapper2("JVM_GetSockName (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: socklen_t socklen = (socklen_t)(*len); phh@3344: jint result = os::get_sock_name(fd, him, &socklen); phh@3344: *len = (int)socklen; phh@3344: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen)) duke@435: JVMWrapper2("JVM_SendTo (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::sendto(fd, buf, (size_t)len, (uint)flags, to, (socklen_t)tolen); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SocketAvailable(jint fd, jint *pbytes)) duke@435: JVMWrapper2("JVM_SocketAvailable (0x%x)", fd); duke@435: //%note jvm_r6 ikrylov@2322: return os::socket_available(fd, pbytes); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen)) duke@435: JVMWrapper2("JVM_GetSockOpt (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: socklen_t socklen = (socklen_t)(*optlen); phh@3344: jint result = os::get_sock_opt(fd, level, optname, optval, &socklen); phh@3344: *optlen = (int)socklen; phh@3344: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(jint, JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen)) duke@435: JVMWrapper2("JVM_GetSockOpt (0x%x)", fd); duke@435: //%note jvm_r6 phh@3344: return os::set_sock_opt(fd, level, optname, optval, (socklen_t)optlen); duke@435: JVM_END duke@435: phh@3344: duke@435: JVM_LEAF(int, JVM_GetHostName(char* name, int namelen)) duke@435: JVMWrapper("JVM_GetHostName"); ikrylov@2322: return os::get_host_name(name, namelen); ikrylov@2322: JVM_END duke@435: phh@3344: duke@435: // Library support /////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name)) duke@435: //%note jvm_ct duke@435: JVMWrapper2("JVM_LoadLibrary (%s)", name); duke@435: char ebuf[1024]; duke@435: void *load_result; duke@435: { duke@435: ThreadToNativeFromVM ttnfvm(thread); ikrylov@2322: load_result = os::dll_load(name, ebuf, sizeof ebuf); duke@435: } duke@435: if (load_result == NULL) { duke@435: char msg[1024]; duke@435: jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf); duke@435: // Since 'ebuf' may contain a string encoded using duke@435: // platform encoding scheme, we need to pass duke@435: // Exceptions::unsafe_to_utf8 to the new_exception method duke@435: // as the last argument. See bug 6367357. duke@435: Handle h_exception = duke@435: Exceptions::new_exception(thread, duke@435: vmSymbols::java_lang_UnsatisfiedLinkError(), duke@435: msg, Exceptions::unsafe_to_utf8); duke@435: duke@435: THROW_HANDLE_0(h_exception); duke@435: } duke@435: return load_result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void, JVM_UnloadLibrary(void* handle)) duke@435: JVMWrapper("JVM_UnloadLibrary"); ikrylov@2322: os::dll_unload(handle); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name)) duke@435: JVMWrapper2("JVM_FindLibraryEntry (%s)", name); ikrylov@2322: return os::dll_lookup(handle, name); duke@435: JVM_END duke@435: phh@3344: duke@435: // Floating point support //////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jboolean, JVM_IsNaN(jdouble a)) duke@435: JVMWrapper("JVM_IsNaN"); duke@435: return g_isnan(a); duke@435: JVM_END duke@435: duke@435: duke@435: // JNI version /////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version)) duke@435: JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version); duke@435: return Threads::is_supported_jni_version_including_1_1(version); duke@435: JVM_END duke@435: duke@435: duke@435: // String support /////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str)) duke@435: JVMWrapper("JVM_InternString"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: if (str == NULL) return NULL; duke@435: oop string = JNIHandles::resolve_non_null(str); duke@435: oop result = StringTable::intern(string, CHECK_NULL); duke@435: return (jstring) JNIHandles::make_local(env, result); duke@435: JVM_END duke@435: duke@435: duke@435: // Raw monitor support ////////////////////////////////////////////////////////////////////// duke@435: duke@435: // The lock routine below calls lock_without_safepoint_check in order to get a raw lock duke@435: // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because duke@435: // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check duke@435: // that only works with java threads. duke@435: duke@435: duke@435: JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) { duke@435: VM_Exit::block_if_vm_exited(); duke@435: JVMWrapper("JVM_RawMonitorCreate"); duke@435: return new Mutex(Mutex::native, "JVM_RawMonitorCreate"); duke@435: } duke@435: duke@435: duke@435: JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) { duke@435: VM_Exit::block_if_vm_exited(); duke@435: JVMWrapper("JVM_RawMonitorDestroy"); duke@435: delete ((Mutex*) mon); duke@435: } duke@435: duke@435: duke@435: JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) { duke@435: VM_Exit::block_if_vm_exited(); duke@435: JVMWrapper("JVM_RawMonitorEnter"); duke@435: ((Mutex*) mon)->jvm_raw_lock(); duke@435: return 0; duke@435: } duke@435: duke@435: duke@435: JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) { duke@435: VM_Exit::block_if_vm_exited(); duke@435: JVMWrapper("JVM_RawMonitorExit"); duke@435: ((Mutex*) mon)->jvm_raw_unlock(); duke@435: } duke@435: duke@435: duke@435: // Support for Serialization duke@435: duke@435: typedef jfloat (JNICALL *IntBitsToFloatFn )(JNIEnv* env, jclass cb, jint value); duke@435: typedef jdouble (JNICALL *LongBitsToDoubleFn)(JNIEnv* env, jclass cb, jlong value); duke@435: typedef jint (JNICALL *FloatToIntBitsFn )(JNIEnv* env, jclass cb, jfloat value); duke@435: typedef jlong (JNICALL *DoubleToLongBitsFn)(JNIEnv* env, jclass cb, jdouble value); duke@435: duke@435: static IntBitsToFloatFn int_bits_to_float_fn = NULL; duke@435: static LongBitsToDoubleFn long_bits_to_double_fn = NULL; duke@435: static FloatToIntBitsFn float_to_int_bits_fn = NULL; duke@435: static DoubleToLongBitsFn double_to_long_bits_fn = NULL; duke@435: duke@435: duke@435: void initialize_converter_functions() { duke@435: if (JDK_Version::is_gte_jdk14x_version()) { duke@435: // These functions only exist for compatibility with 1.3.1 and earlier duke@435: return; duke@435: } duke@435: duke@435: // called from universe_post_init() duke@435: assert( duke@435: int_bits_to_float_fn == NULL && duke@435: long_bits_to_double_fn == NULL && duke@435: float_to_int_bits_fn == NULL && duke@435: double_to_long_bits_fn == NULL , duke@435: "initialization done twice" duke@435: ); duke@435: // initialize duke@435: int_bits_to_float_fn = CAST_TO_FN_PTR(IntBitsToFloatFn , NativeLookup::base_library_lookup("java/lang/Float" , "intBitsToFloat" , "(I)F")); duke@435: long_bits_to_double_fn = CAST_TO_FN_PTR(LongBitsToDoubleFn, NativeLookup::base_library_lookup("java/lang/Double", "longBitsToDouble", "(J)D")); duke@435: float_to_int_bits_fn = CAST_TO_FN_PTR(FloatToIntBitsFn , NativeLookup::base_library_lookup("java/lang/Float" , "floatToIntBits" , "(F)I")); duke@435: double_to_long_bits_fn = CAST_TO_FN_PTR(DoubleToLongBitsFn, NativeLookup::base_library_lookup("java/lang/Double", "doubleToLongBits", "(D)J")); duke@435: // verify duke@435: assert( duke@435: int_bits_to_float_fn != NULL && duke@435: long_bits_to_double_fn != NULL && duke@435: float_to_int_bits_fn != NULL && duke@435: double_to_long_bits_fn != NULL , duke@435: "initialization failed" duke@435: ); duke@435: } duke@435: duke@435: duke@435: // Serialization duke@435: JVM_ENTRY(void, JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, duke@435: jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)) duke@435: assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier"); duke@435: duke@435: typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes)); duke@435: typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data)); duke@435: typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs)); duke@435: oop o = JNIHandles::resolve(obj); duke@435: duke@435: if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: duke@435: jsize nfids = fids->length(); duke@435: if (nfids == 0) return; duke@435: duke@435: if (tcodes->length() < nfids) { duke@435: THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); duke@435: } duke@435: duke@435: jsize off = 0; duke@435: /* loop through fields, setting values */ duke@435: for (jsize i = 0; i < nfids; i++) { duke@435: jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i); duke@435: int field_offset; duke@435: if (fid != NULL) { duke@435: // NULL is a legal value for fid, but retrieving the field offset duke@435: // trigger assertion in that case duke@435: field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); duke@435: } duke@435: duke@435: switch (tcodes->char_at(i)) { duke@435: case 'Z': duke@435: if (fid != NULL) { duke@435: jboolean val = (dbuf->byte_at(off) != 0) ? JNI_TRUE : JNI_FALSE; duke@435: o->bool_field_put(field_offset, val); duke@435: } duke@435: off++; duke@435: break; duke@435: duke@435: case 'B': duke@435: if (fid != NULL) { duke@435: o->byte_field_put(field_offset, dbuf->byte_at(off)); duke@435: } duke@435: off++; duke@435: break; duke@435: duke@435: case 'C': duke@435: if (fid != NULL) { duke@435: jchar val = ((dbuf->byte_at(off + 0) & 0xFF) << 8) duke@435: + ((dbuf->byte_at(off + 1) & 0xFF) << 0); duke@435: o->char_field_put(field_offset, val); duke@435: } duke@435: off += 2; duke@435: break; duke@435: duke@435: case 'S': duke@435: if (fid != NULL) { duke@435: jshort val = ((dbuf->byte_at(off + 0) & 0xFF) << 8) duke@435: + ((dbuf->byte_at(off + 1) & 0xFF) << 0); duke@435: o->short_field_put(field_offset, val); duke@435: } duke@435: off += 2; duke@435: break; duke@435: duke@435: case 'I': duke@435: if (fid != NULL) { duke@435: jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24) duke@435: + ((dbuf->byte_at(off + 1) & 0xFF) << 16) duke@435: + ((dbuf->byte_at(off + 2) & 0xFF) << 8) duke@435: + ((dbuf->byte_at(off + 3) & 0xFF) << 0); duke@435: o->int_field_put(field_offset, ival); duke@435: } duke@435: off += 4; duke@435: break; duke@435: duke@435: case 'F': duke@435: if (fid != NULL) { duke@435: jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24) duke@435: + ((dbuf->byte_at(off + 1) & 0xFF) << 16) duke@435: + ((dbuf->byte_at(off + 2) & 0xFF) << 8) duke@435: + ((dbuf->byte_at(off + 3) & 0xFF) << 0); duke@435: jfloat fval = (*int_bits_to_float_fn)(env, NULL, ival); duke@435: o->float_field_put(field_offset, fval); duke@435: } duke@435: off += 4; duke@435: break; duke@435: duke@435: case 'J': duke@435: if (fid != NULL) { duke@435: jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56) duke@435: + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48) duke@435: + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40) duke@435: + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32) duke@435: + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24) duke@435: + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16) duke@435: + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8) duke@435: + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0); duke@435: o->long_field_put(field_offset, lval); duke@435: } duke@435: off += 8; duke@435: break; duke@435: duke@435: case 'D': duke@435: if (fid != NULL) { duke@435: jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56) duke@435: + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48) duke@435: + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40) duke@435: + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32) duke@435: + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24) duke@435: + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16) duke@435: + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8) duke@435: + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0); duke@435: jdouble dval = (*long_bits_to_double_fn)(env, NULL, lval); duke@435: o->double_field_put(field_offset, dval); duke@435: } duke@435: off += 8; duke@435: break; duke@435: duke@435: default: duke@435: // Illegal typecode duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode"); duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj, duke@435: jlongArray fieldIDs, jcharArray typecodes, jbyteArray data)) duke@435: assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier"); duke@435: duke@435: typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes)); duke@435: typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data)); duke@435: typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs)); duke@435: oop o = JNIHandles::resolve(obj); duke@435: duke@435: if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: duke@435: jsize nfids = fids->length(); duke@435: if (nfids == 0) return; duke@435: duke@435: if (tcodes->length() < nfids) { duke@435: THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException()); duke@435: } duke@435: duke@435: /* loop through fields, fetching values */ duke@435: jsize off = 0; duke@435: for (jsize i = 0; i < nfids; i++) { duke@435: jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i); duke@435: if (fid == NULL) { duke@435: THROW(vmSymbols::java_lang_NullPointerException()); duke@435: } duke@435: int field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); duke@435: duke@435: switch (tcodes->char_at(i)) { duke@435: case 'Z': duke@435: { duke@435: jboolean val = o->bool_field(field_offset); duke@435: dbuf->byte_at_put(off++, (val != 0) ? 1 : 0); duke@435: } duke@435: break; duke@435: duke@435: case 'B': duke@435: dbuf->byte_at_put(off++, o->byte_field(field_offset)); duke@435: break; duke@435: duke@435: case 'C': duke@435: { duke@435: jchar val = o->char_field(field_offset); duke@435: dbuf->byte_at_put(off++, (val >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: case 'S': duke@435: { duke@435: jshort val = o->short_field(field_offset); duke@435: dbuf->byte_at_put(off++, (val >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: case 'I': duke@435: { duke@435: jint val = o->int_field(field_offset); duke@435: dbuf->byte_at_put(off++, (val >> 24) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 16) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: case 'F': duke@435: { duke@435: jfloat fval = o->float_field(field_offset); duke@435: jint ival = (*float_to_int_bits_fn)(env, NULL, fval); duke@435: dbuf->byte_at_put(off++, (ival >> 24) & 0xFF); duke@435: dbuf->byte_at_put(off++, (ival >> 16) & 0xFF); duke@435: dbuf->byte_at_put(off++, (ival >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (ival >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: case 'J': duke@435: { duke@435: jlong val = o->long_field(field_offset); duke@435: dbuf->byte_at_put(off++, (val >> 56) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 48) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 40) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 32) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 24) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 16) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (val >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: case 'D': duke@435: { duke@435: jdouble dval = o->double_field(field_offset); duke@435: jlong lval = (*double_to_long_bits_fn)(env, NULL, dval); duke@435: dbuf->byte_at_put(off++, (lval >> 56) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 48) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 40) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 32) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 24) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 16) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 8) & 0xFF); duke@435: dbuf->byte_at_put(off++, (lval >> 0) & 0xFF); duke@435: } duke@435: break; duke@435: duke@435: default: duke@435: // Illegal typecode duke@435: THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode"); duke@435: } duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: // Shared JNI/JVM entry points ////////////////////////////////////////////////////////////// duke@435: coleenp@2497: jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS) { duke@435: // Security Note: duke@435: // The Java level wrapper will perform the necessary security check allowing duke@435: // us to pass the NULL as the initiating class loader. coleenp@4037: Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL); mchung@1313: duke@435: KlassHandle klass_handle(THREAD, klass); duke@435: // Check if we should initialize the class duke@435: if (init && klass_handle->oop_is_instance()) { duke@435: klass_handle->initialize(CHECK_NULL); duke@435: } duke@435: return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror()); duke@435: } duke@435: duke@435: duke@435: // Internal SQE debugging support /////////////////////////////////////////////////////////// duke@435: duke@435: #ifndef PRODUCT duke@435: duke@435: extern "C" { duke@435: JNIEXPORT jboolean JNICALL JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get); duke@435: JNIEXPORT jboolean JNICALL JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get); duke@435: JNIEXPORT void JNICALL JVM_VMBreakPoint(JNIEnv *env, jobject obj); duke@435: } duke@435: duke@435: JVM_LEAF(jboolean, JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get)) duke@435: JVMWrapper("JVM_AccessBoolVMFlag"); duke@435: return is_get ? CommandLineFlags::boolAt((char*) name, (bool*) value) : CommandLineFlags::boolAtPut((char*) name, (bool*) value, INTERNAL); duke@435: JVM_END duke@435: duke@435: JVM_LEAF(jboolean, JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get)) duke@435: JVMWrapper("JVM_AccessVMIntFlag"); duke@435: intx v; duke@435: jboolean result = is_get ? CommandLineFlags::intxAt((char*) name, &v) : CommandLineFlags::intxAtPut((char*) name, &v, INTERNAL); duke@435: *value = (jint)v; duke@435: return result; duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(void, JVM_VMBreakPoint(JNIEnv *env, jobject obj)) duke@435: JVMWrapper("JVM_VMBreakPoint"); duke@435: oop the_obj = JNIHandles::resolve(obj); duke@435: BREAKPOINT; duke@435: JVM_END duke@435: duke@435: duke@435: #endif duke@435: duke@435: duke@435: // Method /////////////////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0)) duke@435: JVMWrapper("JVM_InvokeMethod"); duke@435: Handle method_handle; duke@435: if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) { duke@435: method_handle = Handle(THREAD, JNIHandles::resolve(method)); duke@435: Handle receiver(THREAD, JNIHandles::resolve(obj)); duke@435: objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); duke@435: oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL); duke@435: jobject res = JNIHandles::make_local(env, result); duke@435: if (JvmtiExport::should_post_vm_object_alloc()) { duke@435: oop ret_type = java_lang_reflect_Method::return_type(method_handle()); duke@435: assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!"); duke@435: if (java_lang_Class::is_primitive(ret_type)) { duke@435: // Only for primitive type vm allocates memory for java object. duke@435: // See box() method. duke@435: JvmtiExport::post_vm_object_alloc(JavaThread::current(), result); duke@435: } duke@435: } duke@435: return res; duke@435: } else { duke@435: THROW_0(vmSymbols::java_lang_StackOverflowError()); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0)) duke@435: JVMWrapper("JVM_NewInstanceFromConstructor"); duke@435: oop constructor_mirror = JNIHandles::resolve(c); duke@435: objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0))); duke@435: oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL); duke@435: jobject res = JNIHandles::make_local(env, result); duke@435: if (JvmtiExport::should_post_vm_object_alloc()) { duke@435: JvmtiExport::post_vm_object_alloc(JavaThread::current(), result); duke@435: } duke@435: return res; duke@435: JVM_END duke@435: duke@435: // Atomic /////////////////////////////////////////////////////////////////////////////////////////// duke@435: duke@435: JVM_LEAF(jboolean, JVM_SupportsCX8()) duke@435: JVMWrapper("JVM_SupportsCX8"); duke@435: return VM_Version::supports_cx8(); duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jboolean, JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal)) duke@435: JVMWrapper("JVM_CX8Field"); duke@435: jlong res; duke@435: oop o = JNIHandles::resolve(obj); duke@435: intptr_t fldOffs = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid); duke@435: volatile jlong* addr = (volatile jlong*)((address)o + fldOffs); duke@435: duke@435: assert(VM_Version::supports_cx8(), "cx8 not supported"); duke@435: res = Atomic::cmpxchg(newVal, addr, oldVal); duke@435: duke@435: return res == oldVal; duke@435: JVM_END duke@435: kamg@551: // DTrace /////////////////////////////////////////////////////////////////// kamg@551: kamg@551: JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env)) kamg@551: JVMWrapper("JVM_DTraceGetVersion"); kamg@551: return (jint)JVM_TRACING_DTRACE_VERSION; kamg@551: JVM_END kamg@551: kamg@551: JVM_ENTRY(jlong,JVM_DTraceActivate( kamg@551: JNIEnv* env, jint version, jstring module_name, jint providers_count, kamg@551: JVM_DTraceProvider* providers)) kamg@551: JVMWrapper("JVM_DTraceActivate"); kamg@551: return DTraceJSDT::activate( kamg@551: version, module_name, providers_count, providers, CHECK_0); kamg@551: JVM_END kamg@551: kamg@551: JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method)) kamg@551: JVMWrapper("JVM_DTraceIsProbeEnabled"); kamg@551: return DTraceJSDT::is_probe_enabled(method); kamg@551: JVM_END kamg@551: kamg@551: JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle)) kamg@551: JVMWrapper("JVM_DTraceDispose"); kamg@551: DTraceJSDT::dispose(handle); kamg@551: JVM_END kamg@551: kamg@551: JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env)) kamg@551: JVMWrapper("JVM_DTraceIsSupported"); kamg@551: return DTraceJSDT::is_supported(); kamg@551: JVM_END kamg@551: duke@435: // Returns an array of all live Thread objects (VM internal JavaThreads, duke@435: // jvmti agent threads, and JNI attaching threads are skipped) duke@435: // See CR 6404306 regarding JNI attaching threads duke@435: JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy)) duke@435: ResourceMark rm(THREAD); duke@435: ThreadsListEnumerator tle(THREAD, false, false); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: int num_threads = tle.num_threads(); never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NULL); duke@435: objArrayHandle threads_ah(THREAD, r); duke@435: duke@435: for (int i = 0; i < num_threads; i++) { duke@435: Handle h = tle.get_threadObj(i); duke@435: threads_ah->obj_at_put(i, h()); duke@435: } duke@435: duke@435: return (jobjectArray) JNIHandles::make_local(env, threads_ah()); duke@435: JVM_END duke@435: duke@435: duke@435: // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods duke@435: // Return StackTraceElement[][], each element is the stack trace of a thread in duke@435: // the corresponding entry in the given threads array duke@435: JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads)) duke@435: JVMWrapper("JVM_DumpThreads"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: // Check if threads is null duke@435: if (threads == NULL) { duke@435: THROW_(vmSymbols::java_lang_NullPointerException(), 0); duke@435: } duke@435: duke@435: objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads)); duke@435: objArrayHandle ah(THREAD, a); duke@435: int num_threads = ah->length(); duke@435: // check if threads is non-empty array duke@435: if (num_threads == 0) { duke@435: THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); duke@435: } duke@435: duke@435: // check if threads is not an array of objects of Thread class coleenp@4142: Klass* k = ObjArrayKlass::cast(ah->klass())->element_klass(); never@1577: if (k != SystemDictionary::Thread_klass()) { duke@435: THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0); duke@435: } duke@435: duke@435: ResourceMark rm(THREAD); duke@435: duke@435: GrowableArray* thread_handle_array = new GrowableArray(num_threads); duke@435: for (int i = 0; i < num_threads; i++) { duke@435: oop thread_obj = ah->obj_at(i); duke@435: instanceHandle h(THREAD, (instanceOop) thread_obj); duke@435: thread_handle_array->append(h); duke@435: } duke@435: duke@435: Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL); duke@435: return (jobjectArray)JNIHandles::make_local(env, stacktraces()); duke@435: duke@435: JVM_END duke@435: duke@435: // JVM monitoring and management support duke@435: JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version)) duke@435: return Management::get_jmm_interface(version); duke@435: JVM_END duke@435: duke@435: // com.sun.tools.attach.VirtualMachine agent properties support duke@435: // duke@435: // Initialize the agent properties with the properties maintained in the VM duke@435: JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties)) duke@435: JVMWrapper("JVM_InitAgentProperties"); duke@435: ResourceMark rm; duke@435: duke@435: Handle props(THREAD, JNIHandles::resolve_non_null(properties)); duke@435: duke@435: PUTPROP(props, "sun.java.command", Arguments::java_command()); duke@435: PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags()); duke@435: PUTPROP(props, "sun.jvm.args", Arguments::jvm_args()); duke@435: return properties; duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass)) duke@435: { duke@435: JVMWrapper("JVM_GetEnclosingMethodInfo"); duke@435: JvmtiVMObjectAllocEventCollector oam; duke@435: duke@435: if (ofClass == NULL) { duke@435: return NULL; duke@435: } duke@435: Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass)); duke@435: // Special handling for primitive objects duke@435: if (java_lang_Class::is_primitive(mirror())) { duke@435: return NULL; duke@435: } coleenp@4037: Klass* k = java_lang_Class::as_Klass(mirror()); hseigel@4278: if (!k->oop_is_instance()) { duke@435: return NULL; duke@435: } duke@435: instanceKlassHandle ik_h(THREAD, k); duke@435: int encl_method_class_idx = ik_h->enclosing_method_class_index(); duke@435: if (encl_method_class_idx == 0) { duke@435: return NULL; duke@435: } never@1577: objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::Object_klass(), 3, CHECK_NULL); duke@435: objArrayHandle dest(THREAD, dest_o); coleenp@4037: Klass* enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL); hseigel@4278: dest->obj_at_put(0, enc_k->java_mirror()); duke@435: int encl_method_method_idx = ik_h->enclosing_method_method_index(); duke@435: if (encl_method_method_idx != 0) { coleenp@2497: Symbol* sym = ik_h->constants()->symbol_at( duke@435: extract_low_short_from_int( duke@435: ik_h->constants()->name_and_type_at(encl_method_method_idx))); duke@435: Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL); duke@435: dest->obj_at_put(1, str()); coleenp@2497: sym = ik_h->constants()->symbol_at( duke@435: extract_high_short_from_int( duke@435: ik_h->constants()->name_and_type_at(encl_method_method_idx))); duke@435: str = java_lang_String::create_from_symbol(sym, CHECK_NULL); duke@435: dest->obj_at_put(2, str()); duke@435: } duke@435: return (jobjectArray) JNIHandles::make_local(dest()); duke@435: } duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(jintArray, JVM_GetThreadStateValues(JNIEnv* env, duke@435: jint javaThreadState)) duke@435: { duke@435: // If new thread states are added in future JDK and VM versions, duke@435: // this should check if the JDK version is compatible with thread duke@435: // states supported by the VM. Return NULL if not compatible. duke@435: // duke@435: // This function must map the VM java_lang_Thread::ThreadStatus duke@435: // to the Java thread state that the JDK supports. duke@435: // duke@435: duke@435: typeArrayHandle values_h; duke@435: switch (javaThreadState) { duke@435: case JAVA_THREAD_STATE_NEW : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::NEW); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_RUNNABLE : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::RUNNABLE); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_BLOCKED : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_WAITING : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::IN_OBJECT_WAIT); duke@435: values_h->int_at_put(1, java_lang_Thread::PARKED); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_TIMED_WAITING : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 3, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::SLEEPING); duke@435: values_h->int_at_put(1, java_lang_Thread::IN_OBJECT_WAIT_TIMED); duke@435: values_h->int_at_put(2, java_lang_Thread::PARKED_TIMED); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_TERMINATED : { duke@435: typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL); duke@435: values_h = typeArrayHandle(THREAD, r); duke@435: values_h->int_at_put(0, java_lang_Thread::TERMINATED); duke@435: break; duke@435: } duke@435: default: duke@435: // Unknown state - probably incompatible JDK version duke@435: return NULL; duke@435: } duke@435: duke@435: return (jintArray) JNIHandles::make_local(env, values_h()); duke@435: } duke@435: JVM_END duke@435: duke@435: duke@435: JVM_ENTRY(jobjectArray, JVM_GetThreadStateNames(JNIEnv* env, duke@435: jint javaThreadState, duke@435: jintArray values)) duke@435: { duke@435: // If new thread states are added in future JDK and VM versions, duke@435: // this should check if the JDK version is compatible with thread duke@435: // states supported by the VM. Return NULL if not compatible. duke@435: // duke@435: // This function must map the VM java_lang_Thread::ThreadStatus duke@435: // to the Java thread state that the JDK supports. duke@435: // duke@435: duke@435: ResourceMark rm; duke@435: duke@435: // Check if threads is null duke@435: if (values == NULL) { duke@435: THROW_(vmSymbols::java_lang_NullPointerException(), 0); duke@435: } duke@435: duke@435: typeArrayOop v = typeArrayOop(JNIHandles::resolve_non_null(values)); duke@435: typeArrayHandle values_h(THREAD, v); duke@435: duke@435: objArrayHandle names_h; duke@435: switch (javaThreadState) { duke@435: case JAVA_THREAD_STATE_NEW : { duke@435: assert(values_h->length() == 1 && duke@435: values_h->int_at(0) == java_lang_Thread::NEW, duke@435: "Invalid threadStatus value"); duke@435: never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 1, /* only 1 substate */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name = java_lang_String::create_from_str("NEW", CHECK_NULL); duke@435: names_h->obj_at_put(0, name()); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_RUNNABLE : { duke@435: assert(values_h->length() == 1 && duke@435: values_h->int_at(0) == java_lang_Thread::RUNNABLE, duke@435: "Invalid threadStatus value"); duke@435: never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 1, /* only 1 substate */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name = java_lang_String::create_from_str("RUNNABLE", CHECK_NULL); duke@435: names_h->obj_at_put(0, name()); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_BLOCKED : { duke@435: assert(values_h->length() == 1 && duke@435: values_h->int_at(0) == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER, duke@435: "Invalid threadStatus value"); duke@435: never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 1, /* only 1 substate */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name = java_lang_String::create_from_str("BLOCKED", CHECK_NULL); duke@435: names_h->obj_at_put(0, name()); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_WAITING : { duke@435: assert(values_h->length() == 2 && duke@435: values_h->int_at(0) == java_lang_Thread::IN_OBJECT_WAIT && duke@435: values_h->int_at(1) == java_lang_Thread::PARKED, duke@435: "Invalid threadStatus value"); never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 2, /* number of substates */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name0 = java_lang_String::create_from_str("WAITING.OBJECT_WAIT", duke@435: CHECK_NULL); duke@435: Handle name1 = java_lang_String::create_from_str("WAITING.PARKED", duke@435: CHECK_NULL); duke@435: names_h->obj_at_put(0, name0()); duke@435: names_h->obj_at_put(1, name1()); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_TIMED_WAITING : { duke@435: assert(values_h->length() == 3 && duke@435: values_h->int_at(0) == java_lang_Thread::SLEEPING && duke@435: values_h->int_at(1) == java_lang_Thread::IN_OBJECT_WAIT_TIMED && duke@435: values_h->int_at(2) == java_lang_Thread::PARKED_TIMED, duke@435: "Invalid threadStatus value"); never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 3, /* number of substates */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name0 = java_lang_String::create_from_str("TIMED_WAITING.SLEEPING", duke@435: CHECK_NULL); duke@435: Handle name1 = java_lang_String::create_from_str("TIMED_WAITING.OBJECT_WAIT", duke@435: CHECK_NULL); duke@435: Handle name2 = java_lang_String::create_from_str("TIMED_WAITING.PARKED", duke@435: CHECK_NULL); duke@435: names_h->obj_at_put(0, name0()); duke@435: names_h->obj_at_put(1, name1()); duke@435: names_h->obj_at_put(2, name2()); duke@435: break; duke@435: } duke@435: case JAVA_THREAD_STATE_TERMINATED : { duke@435: assert(values_h->length() == 1 && duke@435: values_h->int_at(0) == java_lang_Thread::TERMINATED, duke@435: "Invalid threadStatus value"); never@1577: objArrayOop r = oopFactory::new_objArray(SystemDictionary::String_klass(), duke@435: 1, /* only 1 substate */ duke@435: CHECK_NULL); duke@435: names_h = objArrayHandle(THREAD, r); duke@435: Handle name = java_lang_String::create_from_str("TERMINATED", CHECK_NULL); duke@435: names_h->obj_at_put(0, name()); duke@435: break; duke@435: } duke@435: default: duke@435: // Unknown state - probably incompatible JDK version duke@435: return NULL; duke@435: } duke@435: return (jobjectArray) JNIHandles::make_local(env, names_h()); duke@435: } duke@435: JVM_END duke@435: duke@435: JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size)) duke@435: { duke@435: memset(info, 0, sizeof(info_size)); duke@435: duke@435: info->jvm_version = Abstract_VM_Version::jvm_version(); duke@435: info->update_version = 0; /* 0 in HotSpot Express VM */ duke@435: info->special_update_version = 0; /* 0 in HotSpot Express VM */ duke@435: duke@435: // when we add a new capability in the jvm_version_info struct, we should also duke@435: // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat duke@435: // counter defined in runtimeService.cpp. duke@435: info->is_attachable = AttachListener::is_attach_supported(); duke@435: } duke@435: JVM_END