Wed, 08 Oct 2008 08:10:51 -0700
6755845: JVM_FindClassFromBoot triggers assertions
Summary: Fixes assertions caused by one jvm_entry calling another, solved by refactoring code and modified gamma test.
Reviewed-by: dholmes, xlu
1 /*
2 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_jvm.cpp.incl"
27 #include <errno.h>
29 /*
30 NOTE about use of any ctor or function call that can trigger a safepoint/GC:
31 such ctors and calls MUST NOT come between an oop declaration/init and its
32 usage because if objects are move this may cause various memory stomps, bus
33 errors and segfaults. Here is a cookbook for causing so called "naked oop
34 failures":
36 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> {
37 JVMWrapper("JVM_GetClassDeclaredFields");
39 // Object address to be held directly in mirror & not visible to GC
40 oop mirror = JNIHandles::resolve_non_null(ofClass);
42 // If this ctor can hit a safepoint, moving objects around, then
43 ComplexConstructor foo;
45 // Boom! mirror may point to JUNK instead of the intended object
46 (some dereference of mirror)
48 // Here's another call that may block for GC, making mirror stale
49 MutexLocker ml(some_lock);
51 // And here's an initializer that can result in a stale oop
52 // all in one step.
53 oop o = call_that_can_throw_exception(TRAPS);
56 The solution is to keep the oop declaration BELOW the ctor or function
57 call that might cause a GC, do another resolve to reassign the oop, or
58 consider use of a Handle instead of an oop so there is immunity from object
59 motion. But note that the "QUICK" entries below do not have a handlemark
60 and thus can only support use of handles passed in.
61 */
63 static void trace_class_resolution_impl(klassOop to_class, TRAPS) {
64 ResourceMark rm;
65 int line_number = -1;
66 const char * source_file = NULL;
67 klassOop caller = NULL;
68 JavaThread* jthread = JavaThread::current();
69 if (jthread->has_last_Java_frame()) {
70 vframeStream vfst(jthread);
72 // scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames
73 symbolHandle access_controller = oopFactory::new_symbol_handle("java/security/AccessController", CHECK);
74 klassOop access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK);
75 symbolHandle privileged_action = oopFactory::new_symbol_handle("java/security/PrivilegedAction", CHECK);
76 klassOop privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK);
78 methodOop last_caller = NULL;
80 while (!vfst.at_end()) {
81 methodOop m = vfst.method();
82 if (!vfst.method()->method_holder()->klass_part()->is_subclass_of(SystemDictionary::classloader_klass())&&
83 !vfst.method()->method_holder()->klass_part()->is_subclass_of(access_controller_klass) &&
84 !vfst.method()->method_holder()->klass_part()->is_subclass_of(privileged_action_klass)) {
85 break;
86 }
87 last_caller = m;
88 vfst.next();
89 }
90 // if this is called from Class.forName0 and that is called from Class.forName,
91 // then print the caller of Class.forName. If this is Class.loadClass, then print
92 // that caller, otherwise keep quiet since this should be picked up elsewhere.
93 bool found_it = false;
94 if (!vfst.at_end() &&
95 instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class() &&
96 vfst.method()->name() == vmSymbols::forName0_name()) {
97 vfst.next();
98 if (!vfst.at_end() &&
99 instanceKlass::cast(vfst.method()->method_holder())->name() == vmSymbols::java_lang_Class() &&
100 vfst.method()->name() == vmSymbols::forName_name()) {
101 vfst.next();
102 found_it = true;
103 }
104 } else if (last_caller != NULL &&
105 instanceKlass::cast(last_caller->method_holder())->name() ==
106 vmSymbols::java_lang_ClassLoader() &&
107 (last_caller->name() == vmSymbols::loadClassInternal_name() ||
108 last_caller->name() == vmSymbols::loadClass_name())) {
109 found_it = true;
110 }
111 if (found_it && !vfst.at_end()) {
112 // found the caller
113 caller = vfst.method()->method_holder();
114 line_number = vfst.method()->line_number_from_bci(vfst.bci());
115 symbolOop s = instanceKlass::cast(vfst.method()->method_holder())->source_file_name();
116 if (s != NULL) {
117 source_file = s->as_C_string();
118 }
119 }
120 }
121 if (caller != NULL) {
122 if (to_class != caller) {
123 const char * from = Klass::cast(caller)->external_name();
124 const char * to = Klass::cast(to_class)->external_name();
125 // print in a single call to reduce interleaving between threads
126 if (source_file != NULL) {
127 tty->print("RESOLVE %s %s %s:%d (explicit)\n", from, to, source_file, line_number);
128 } else {
129 tty->print("RESOLVE %s %s (explicit)\n", from, to);
130 }
131 }
132 }
133 }
135 static void trace_class_resolution(klassOop to_class) {
136 EXCEPTION_MARK;
137 trace_class_resolution_impl(to_class, THREAD);
138 if (HAS_PENDING_EXCEPTION) {
139 CLEAR_PENDING_EXCEPTION;
140 }
141 }
143 // Wrapper to trace JVM functions
145 #ifdef ASSERT
146 class JVMTraceWrapper : public StackObj {
147 public:
148 JVMTraceWrapper(const char* format, ...) {
149 if (TraceJVMCalls) {
150 va_list ap;
151 va_start(ap, format);
152 tty->print("JVM ");
153 tty->vprint_cr(format, ap);
154 va_end(ap);
155 }
156 }
157 };
159 Histogram* JVMHistogram;
160 volatile jint JVMHistogram_lock = 0;
162 class JVMHistogramElement : public HistogramElement {
163 public:
164 JVMHistogramElement(const char* name);
165 };
167 JVMHistogramElement::JVMHistogramElement(const char* elementName) {
168 _name = elementName;
169 uintx count = 0;
171 while (Atomic::cmpxchg(1, &JVMHistogram_lock, 0) != 0) {
172 while (OrderAccess::load_acquire(&JVMHistogram_lock) != 0) {
173 count +=1;
174 if ( (WarnOnStalledSpinLock > 0)
175 && (count % WarnOnStalledSpinLock == 0)) {
176 warning("JVMHistogram_lock seems to be stalled");
177 }
178 }
179 }
181 if(JVMHistogram == NULL)
182 JVMHistogram = new Histogram("JVM Call Counts",100);
184 JVMHistogram->add_element(this);
185 Atomic::dec(&JVMHistogram_lock);
186 }
188 #define JVMCountWrapper(arg) \
189 static JVMHistogramElement* e = new JVMHistogramElement(arg); \
190 if (e != NULL) e->increment_count(); // Due to bug in VC++, we need a NULL check here eventhough it should never happen!
192 #define JVMWrapper(arg1) JVMCountWrapper(arg1); JVMTraceWrapper(arg1)
193 #define JVMWrapper2(arg1, arg2) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2)
194 #define JVMWrapper3(arg1, arg2, arg3) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3)
195 #define JVMWrapper4(arg1, arg2, arg3, arg4) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3, arg4)
196 #else
197 #define JVMWrapper(arg1)
198 #define JVMWrapper2(arg1, arg2)
199 #define JVMWrapper3(arg1, arg2, arg3)
200 #define JVMWrapper4(arg1, arg2, arg3, arg4)
201 #endif
204 // Interface version /////////////////////////////////////////////////////////////////////
207 JVM_LEAF(jint, JVM_GetInterfaceVersion())
208 return JVM_INTERFACE_VERSION;
209 JVM_END
212 // java.lang.System //////////////////////////////////////////////////////////////////////
215 JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
216 JVMWrapper("JVM_CurrentTimeMillis");
217 return os::javaTimeMillis();
218 JVM_END
220 JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
221 JVMWrapper("JVM_NanoTime");
222 return os::javaTimeNanos();
223 JVM_END
226 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
227 jobject dst, jint dst_pos, jint length))
228 JVMWrapper("JVM_ArrayCopy");
229 // Check if we have null pointers
230 if (src == NULL || dst == NULL) {
231 THROW(vmSymbols::java_lang_NullPointerException());
232 }
233 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
234 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
235 assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
236 assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
237 // Do copy
238 Klass::cast(s->klass())->copy_array(s, src_pos, d, dst_pos, length, thread);
239 JVM_END
242 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
243 JavaValue r(T_OBJECT);
244 // public synchronized Object put(Object key, Object value);
245 HandleMark hm(THREAD);
246 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
247 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
248 JavaCalls::call_virtual(&r,
249 props,
250 KlassHandle(THREAD, SystemDictionary::properties_klass()),
251 vmSymbolHandles::put_name(),
252 vmSymbolHandles::object_object_object_signature(),
253 key_str,
254 value_str,
255 THREAD);
256 }
259 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
262 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties))
263 JVMWrapper("JVM_InitProperties");
264 ResourceMark rm;
266 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
268 // System property list includes both user set via -D option and
269 // jvm system specific properties.
270 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
271 PUTPROP(props, p->key(), p->value());
272 }
274 // Convert the -XX:MaxDirectMemorySize= command line flag
275 // to the sun.nio.MaxDirectMemorySize property.
276 // Do this after setting user properties to prevent people
277 // from setting the value with a -D option, as requested.
278 {
279 char as_chars[256];
280 jio_snprintf(as_chars, sizeof(as_chars), INTX_FORMAT, MaxDirectMemorySize);
281 PUTPROP(props, "sun.nio.MaxDirectMemorySize", as_chars);
282 }
284 // JVM monitoring and management support
285 // Add the sun.management.compiler property for the compiler's name
286 {
287 #undef CSIZE
288 #if defined(_LP64) || defined(_WIN64)
289 #define CSIZE "64-Bit "
290 #else
291 #define CSIZE
292 #endif // 64bit
294 #ifdef TIERED
295 const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers";
296 #else
297 #if defined(COMPILER1)
298 const char* compiler_name = "HotSpot " CSIZE "Client Compiler";
299 #elif defined(COMPILER2)
300 const char* compiler_name = "HotSpot " CSIZE "Server Compiler";
301 #else
302 const char* compiler_name = "";
303 #endif // compilers
304 #endif // TIERED
306 if (*compiler_name != '\0' &&
307 (Arguments::mode() != Arguments::_int)) {
308 PUTPROP(props, "sun.management.compiler", compiler_name);
309 }
310 }
312 return properties;
313 JVM_END
316 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
318 extern volatile jint vm_created;
320 JVM_ENTRY_NO_ENV(void, JVM_Exit(jint code))
321 if (vm_created != 0 && (code == 0)) {
322 // The VM is about to exit. We call back into Java to check whether finalizers should be run
323 Universe::run_finalizers_on_exit();
324 }
325 before_exit(thread);
326 vm_exit(code);
327 JVM_END
330 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
331 before_exit(thread);
332 vm_exit(code);
333 JVM_END
336 JVM_LEAF(void, JVM_OnExit(void (*func)(void)))
337 register_on_exit_function(func);
338 JVM_END
341 JVM_ENTRY_NO_ENV(void, JVM_GC(void))
342 JVMWrapper("JVM_GC");
343 if (!DisableExplicitGC) {
344 Universe::heap()->collect(GCCause::_java_lang_system_gc);
345 }
346 JVM_END
349 JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
350 JVMWrapper("JVM_MaxObjectInspectionAge");
351 return Universe::heap()->millis_since_last_gc();
352 JVM_END
355 JVM_LEAF(void, JVM_TraceInstructions(jboolean on))
356 if (PrintJVMWarnings) warning("JVM_TraceInstructions not supported");
357 JVM_END
360 JVM_LEAF(void, JVM_TraceMethodCalls(jboolean on))
361 if (PrintJVMWarnings) warning("JVM_TraceMethodCalls not supported");
362 JVM_END
364 static inline jlong convert_size_t_to_jlong(size_t val) {
365 // In the 64-bit vm, a size_t can overflow a jlong (which is signed).
366 NOT_LP64 (return (jlong)val;)
367 LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
368 }
370 JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
371 JVMWrapper("JVM_TotalMemory");
372 size_t n = Universe::heap()->capacity();
373 return convert_size_t_to_jlong(n);
374 JVM_END
377 JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
378 JVMWrapper("JVM_FreeMemory");
379 CollectedHeap* ch = Universe::heap();
380 size_t n;
381 {
382 MutexLocker x(Heap_lock);
383 n = ch->capacity() - ch->used();
384 }
385 return convert_size_t_to_jlong(n);
386 JVM_END
389 JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
390 JVMWrapper("JVM_MaxMemory");
391 size_t n = Universe::heap()->max_capacity();
392 return convert_size_t_to_jlong(n);
393 JVM_END
396 JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
397 JVMWrapper("JVM_ActiveProcessorCount");
398 return os::active_processor_count();
399 JVM_END
403 // java.lang.Throwable //////////////////////////////////////////////////////
406 JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
407 JVMWrapper("JVM_FillInStackTrace");
408 Handle exception(thread, JNIHandles::resolve_non_null(receiver));
409 java_lang_Throwable::fill_in_stack_trace(exception);
410 JVM_END
413 JVM_ENTRY(void, JVM_PrintStackTrace(JNIEnv *env, jobject receiver, jobject printable))
414 JVMWrapper("JVM_PrintStackTrace");
415 // Note: This is no longer used in Merlin, but we still support it for compatibility.
416 oop exception = JNIHandles::resolve_non_null(receiver);
417 oop stream = JNIHandles::resolve_non_null(printable);
418 java_lang_Throwable::print_stack_trace(exception, stream);
419 JVM_END
422 JVM_ENTRY(jint, JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable))
423 JVMWrapper("JVM_GetStackTraceDepth");
424 oop exception = JNIHandles::resolve(throwable);
425 return java_lang_Throwable::get_stack_trace_depth(exception, THREAD);
426 JVM_END
429 JVM_ENTRY(jobject, JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index))
430 JVMWrapper("JVM_GetStackTraceElement");
431 JvmtiVMObjectAllocEventCollector oam; // This ctor (throughout this module) may trigger a safepoint/GC
432 oop exception = JNIHandles::resolve(throwable);
433 oop element = java_lang_Throwable::get_stack_trace_element(exception, index, CHECK_NULL);
434 return JNIHandles::make_local(env, element);
435 JVM_END
438 // java.lang.Object ///////////////////////////////////////////////
441 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
442 JVMWrapper("JVM_IHashCode");
443 // as implemented in the classic virtual machine; return 0 if object is NULL
444 return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
445 JVM_END
448 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
449 JVMWrapper("JVM_MonitorWait");
450 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
451 assert(obj->is_instance() || obj->is_array(), "JVM_MonitorWait must apply to an object");
452 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
453 if (JvmtiExport::should_post_monitor_wait()) {
454 JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms);
455 }
456 ObjectSynchronizer::wait(obj, ms, CHECK);
457 JVM_END
460 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
461 JVMWrapper("JVM_MonitorNotify");
462 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
463 assert(obj->is_instance() || obj->is_array(), "JVM_MonitorNotify must apply to an object");
464 ObjectSynchronizer::notify(obj, CHECK);
465 JVM_END
468 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
469 JVMWrapper("JVM_MonitorNotifyAll");
470 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
471 assert(obj->is_instance() || obj->is_array(), "JVM_MonitorNotifyAll must apply to an object");
472 ObjectSynchronizer::notifyall(obj, CHECK);
473 JVM_END
476 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
477 JVMWrapper("JVM_Clone");
478 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
479 const KlassHandle klass (THREAD, obj->klass());
480 JvmtiVMObjectAllocEventCollector oam;
482 #ifdef ASSERT
483 // Just checking that the cloneable flag is set correct
484 if (obj->is_javaArray()) {
485 guarantee(klass->is_cloneable(), "all arrays are cloneable");
486 } else {
487 guarantee(obj->is_instance(), "should be instanceOop");
488 bool cloneable = klass->is_subtype_of(SystemDictionary::cloneable_klass());
489 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
490 }
491 #endif
493 // Check if class of obj supports the Cloneable interface.
494 // All arrays are considered to be cloneable (See JLS 20.1.5)
495 if (!klass->is_cloneable()) {
496 ResourceMark rm(THREAD);
497 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
498 }
500 // Make shallow object copy
501 const int size = obj->size();
502 oop new_obj = NULL;
503 if (obj->is_javaArray()) {
504 const int length = ((arrayOop)obj())->length();
505 new_obj = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
506 } else {
507 new_obj = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
508 }
509 // 4839641 (4840070): We must do an oop-atomic copy, because if another thread
510 // is modifying a reference field in the clonee, a non-oop-atomic copy might
511 // be suspended in the middle of copying the pointer and end up with parts
512 // of two different pointers in the field. Subsequent dereferences will crash.
513 // 4846409: an oop-copy of objects with long or double fields or arrays of same
514 // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead
515 // of oops. We know objects are aligned on a minimum of an jlong boundary.
516 // The same is true of StubRoutines::object_copy and the various oop_copy
517 // variants, and of the code generated by the inline_native_clone intrinsic.
518 assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");
519 Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj,
520 (size_t)align_object_size(size) / HeapWordsPerLong);
521 // Clear the header
522 new_obj->init_mark();
524 // Store check (mark entire object and let gc sort it out)
525 BarrierSet* bs = Universe::heap()->barrier_set();
526 assert(bs->has_write_region_opt(), "Barrier set does not have write_region");
527 bs->write_region(MemRegion((HeapWord*)new_obj, size));
529 // Caution: this involves a java upcall, so the clone should be
530 // "gc-robust" by this stage.
531 if (klass->has_finalizer()) {
532 assert(obj->is_instance(), "should be instanceOop");
533 new_obj = instanceKlass::register_finalizer(instanceOop(new_obj), CHECK_NULL);
534 }
536 return JNIHandles::make_local(env, oop(new_obj));
537 JVM_END
539 // java.lang.Compiler ////////////////////////////////////////////////////
541 // The initial cuts of the HotSpot VM will not support JITs, and all existing
542 // JITs would need extensive changes to work with HotSpot. The JIT-related JVM
543 // functions are all silently ignored unless JVM warnings are printed.
545 JVM_LEAF(void, JVM_InitializeCompiler (JNIEnv *env, jclass compCls))
546 if (PrintJVMWarnings) warning("JVM_InitializeCompiler not supported");
547 JVM_END
550 JVM_LEAF(jboolean, JVM_IsSilentCompiler(JNIEnv *env, jclass compCls))
551 if (PrintJVMWarnings) warning("JVM_IsSilentCompiler not supported");
552 return JNI_FALSE;
553 JVM_END
556 JVM_LEAF(jboolean, JVM_CompileClass(JNIEnv *env, jclass compCls, jclass cls))
557 if (PrintJVMWarnings) warning("JVM_CompileClass not supported");
558 return JNI_FALSE;
559 JVM_END
562 JVM_LEAF(jboolean, JVM_CompileClasses(JNIEnv *env, jclass cls, jstring jname))
563 if (PrintJVMWarnings) warning("JVM_CompileClasses not supported");
564 return JNI_FALSE;
565 JVM_END
568 JVM_LEAF(jobject, JVM_CompilerCommand(JNIEnv *env, jclass compCls, jobject arg))
569 if (PrintJVMWarnings) warning("JVM_CompilerCommand not supported");
570 return NULL;
571 JVM_END
574 JVM_LEAF(void, JVM_EnableCompiler(JNIEnv *env, jclass compCls))
575 if (PrintJVMWarnings) warning("JVM_EnableCompiler not supported");
576 JVM_END
579 JVM_LEAF(void, JVM_DisableCompiler(JNIEnv *env, jclass compCls))
580 if (PrintJVMWarnings) warning("JVM_DisableCompiler not supported");
581 JVM_END
585 // Error message support //////////////////////////////////////////////////////
587 JVM_LEAF(jint, JVM_GetLastErrorString(char *buf, int len))
588 JVMWrapper("JVM_GetLastErrorString");
589 return hpi::lasterror(buf, len);
590 JVM_END
593 // java.io.File ///////////////////////////////////////////////////////////////
595 JVM_LEAF(char*, JVM_NativePath(char* path))
596 JVMWrapper2("JVM_NativePath (%s)", path);
597 return hpi::native_path(path);
598 JVM_END
601 // Misc. class handling ///////////////////////////////////////////////////////////
604 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth))
605 JVMWrapper("JVM_GetCallerClass");
606 klassOop k = thread->security_get_caller_class(depth);
607 return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
608 JVM_END
611 JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
612 JVMWrapper("JVM_FindPrimitiveClass");
613 oop mirror = NULL;
614 BasicType t = name2type(utf);
615 if (t != T_ILLEGAL && t != T_OBJECT && t != T_ARRAY) {
616 mirror = Universe::java_mirror(t);
617 }
618 if (mirror == NULL) {
619 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
620 } else {
621 return (jclass) JNIHandles::make_local(env, mirror);
622 }
623 JVM_END
626 JVM_ENTRY(void, JVM_ResolveClass(JNIEnv* env, jclass cls))
627 JVMWrapper("JVM_ResolveClass");
628 if (PrintJVMWarnings) warning("JVM_ResolveClass not implemented");
629 JVM_END
631 // Common implementation for JVM_FindClassFromBootLoader and
632 // JVM_FindClassFromLoader
633 static jclass jvm_find_class_from_class_loader(JNIEnv* env, const char* name,
634 jboolean init, jobject loader,
635 jboolean throwError, TRAPS) {
636 // Java libraries should ensure that name is never null...
637 if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
638 // It's impossible to create this class; the name cannot fit
639 // into the constant pool.
640 if (throwError) {
641 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
642 } else {
643 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
644 }
645 }
646 symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
647 Handle h_loader(THREAD, JNIHandles::resolve(loader));
648 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
649 Handle(), throwError, THREAD);
651 if (TraceClassResolution && result != NULL) {
652 trace_class_resolution(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(result)));
653 }
654 return result;
655 }
657 // Rationale behind JVM_FindClassFromBootLoader
658 // a> JVM_FindClassFromClassLoader was never exported in the export tables.
659 // b> because of (a) java.dll has a direct dependecy on the unexported
660 // private symbol "_JVM_FindClassFromClassLoader@20".
661 // c> the launcher cannot use the private symbol as it dynamically opens
662 // the entry point, so if something changes, the launcher will fail
663 // unexpectedly at runtime, it is safest for the launcher to dlopen a
664 // stable exported interface.
665 // d> re-exporting JVM_FindClassFromClassLoader as public, will cause its
666 // signature to change from _JVM_FindClassFromClassLoader@20 to
667 // JVM_FindClassFromClassLoader and will not be backward compatible
668 // with older JDKs.
669 // Thus a public/stable exported entry point is the right solution,
670 // public here means public in linker semantics, and is exported only
671 // to the JDK, and is not intended to be a public API.
673 JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
674 const char* name,
675 jboolean throwError))
676 JVMWrapper3("JVM_FindClassFromBootLoader %s throw %s", name,
677 throwError ? "error" : "exception");
678 return jvm_find_class_from_class_loader(env, name, JNI_FALSE,
679 (jobject)NULL, throwError, THREAD);
680 JVM_END
682 JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name,
683 jboolean init, jobject loader,
684 jboolean throwError))
685 JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name,
686 throwError ? "error" : "exception");
687 return jvm_find_class_from_class_loader(env, name, init, loader,
688 throwError, THREAD);
689 JVM_END
692 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
693 jboolean init, jclass from))
694 JVMWrapper2("JVM_FindClassFromClass %s", name);
695 if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
696 // It's impossible to create this class; the name cannot fit
697 // into the constant pool.
698 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
699 }
700 symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
701 oop from_class_oop = JNIHandles::resolve(from);
702 klassOop from_class = (from_class_oop == NULL)
703 ? (klassOop)NULL
704 : java_lang_Class::as_klassOop(from_class_oop);
705 oop class_loader = NULL;
706 oop protection_domain = NULL;
707 if (from_class != NULL) {
708 class_loader = Klass::cast(from_class)->class_loader();
709 protection_domain = Klass::cast(from_class)->protection_domain();
710 }
711 Handle h_loader(THREAD, class_loader);
712 Handle h_prot (THREAD, protection_domain);
713 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
714 h_prot, true, thread);
716 if (TraceClassResolution && result != NULL) {
717 // this function is generally only used for class loading during verification.
718 ResourceMark rm;
719 oop from_mirror = JNIHandles::resolve_non_null(from);
720 klassOop from_class = java_lang_Class::as_klassOop(from_mirror);
721 const char * from_name = Klass::cast(from_class)->external_name();
723 oop mirror = JNIHandles::resolve_non_null(result);
724 klassOop to_class = java_lang_Class::as_klassOop(mirror);
725 const char * to = Klass::cast(to_class)->external_name();
726 tty->print("RESOLVE %s %s (verification)\n", from_name, to);
727 }
729 return result;
730 JVM_END
732 static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) {
733 if (loader.is_null()) {
734 return;
735 }
737 // check whether the current caller thread holds the lock or not.
738 // If not, increment the corresponding counter
739 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) !=
740 ObjectSynchronizer::owner_self) {
741 counter->inc();
742 }
743 }
745 // common code for JVM_DefineClass() and JVM_DefineClassWithSource()
746 static jclass jvm_define_class_common(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source, TRAPS) {
748 // Since exceptions can be thrown, class initialization can take place
749 // if name is NULL no check for class name in .class stream has to be made.
750 symbolHandle class_name;
751 if (name != NULL) {
752 const int str_len = (int)strlen(name);
753 if (str_len > symbolOopDesc::max_length()) {
754 // It's impossible to create this class; the name cannot fit
755 // into the constant pool.
756 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
757 }
758 class_name = oopFactory::new_symbol_handle(name, str_len, CHECK_NULL);
759 }
761 ResourceMark rm(THREAD);
762 ClassFileStream st((u1*) buf, len, (char *)source);
763 Handle class_loader (THREAD, JNIHandles::resolve(loader));
764 if (UsePerfData) {
765 is_lock_held_by_thread(class_loader,
766 ClassLoader::sync_JVMDefineClassLockFreeCounter(),
767 THREAD);
768 }
769 Handle protection_domain (THREAD, JNIHandles::resolve(pd));
770 klassOop k = SystemDictionary::resolve_from_stream(class_name, class_loader,
771 protection_domain, &st,
772 CHECK_NULL);
774 if (TraceClassResolution && k != NULL) {
775 trace_class_resolution(k);
776 }
778 return (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
779 }
782 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
783 JVMWrapper2("JVM_DefineClass %s", name);
785 return jvm_define_class_common(env, name, loader, buf, len, pd, "__JVM_DefineClass__", THREAD);
786 JVM_END
789 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
790 JVMWrapper2("JVM_DefineClassWithSource %s", name);
792 return jvm_define_class_common(env, name, loader, buf, len, pd, source, THREAD);
793 JVM_END
796 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
797 JVMWrapper("JVM_FindLoadedClass");
798 ResourceMark rm(THREAD);
800 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
801 Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
803 const char* str = java_lang_String::as_utf8_string(string());
804 // Sanity check, don't expect null
805 if (str == NULL) return NULL;
807 const int str_len = (int)strlen(str);
808 if (str_len > symbolOopDesc::max_length()) {
809 // It's impossible to create this class; the name cannot fit
810 // into the constant pool.
811 return NULL;
812 }
813 symbolHandle klass_name = oopFactory::new_symbol_handle(str, str_len,CHECK_NULL);
815 // Security Note:
816 // The Java level wrapper will perform the necessary security check allowing
817 // us to pass the NULL as the initiating class loader.
818 Handle h_loader(THREAD, JNIHandles::resolve(loader));
819 if (UsePerfData) {
820 is_lock_held_by_thread(h_loader,
821 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
822 THREAD);
823 }
825 klassOop k = SystemDictionary::find_instance_or_array_klass(klass_name,
826 h_loader,
827 Handle(),
828 CHECK_NULL);
830 return (k == NULL) ? NULL :
831 (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
832 JVM_END
835 // Reflection support //////////////////////////////////////////////////////////////////////////////
837 JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls))
838 assert (cls != NULL, "illegal class");
839 JVMWrapper("JVM_GetClassName");
840 JvmtiVMObjectAllocEventCollector oam;
841 ResourceMark rm(THREAD);
842 const char* name;
843 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
844 name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls)));
845 } else {
846 // Consider caching interned string in Klass
847 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
848 assert(k->is_klass(), "just checking");
849 name = Klass::cast(k)->external_name();
850 }
851 oop result = StringTable::intern((char*) name, CHECK_NULL);
852 return (jstring) JNIHandles::make_local(env, result);
853 JVM_END
856 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
857 JVMWrapper("JVM_GetClassInterfaces");
858 JvmtiVMObjectAllocEventCollector oam;
859 oop mirror = JNIHandles::resolve_non_null(cls);
861 // Special handling for primitive objects
862 if (java_lang_Class::is_primitive(mirror)) {
863 // Primitive objects does not have any interfaces
864 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
865 return (jobjectArray) JNIHandles::make_local(env, r);
866 }
868 KlassHandle klass(thread, java_lang_Class::as_klassOop(mirror));
869 // Figure size of result array
870 int size;
871 if (klass->oop_is_instance()) {
872 size = instanceKlass::cast(klass())->local_interfaces()->length();
873 } else {
874 assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass");
875 size = 2;
876 }
878 // Allocate result array
879 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), size, CHECK_NULL);
880 objArrayHandle result (THREAD, r);
881 // Fill in result
882 if (klass->oop_is_instance()) {
883 // Regular instance klass, fill in all local interfaces
884 for (int index = 0; index < size; index++) {
885 klassOop k = klassOop(instanceKlass::cast(klass())->local_interfaces()->obj_at(index));
886 result->obj_at_put(index, Klass::cast(k)->java_mirror());
887 }
888 } else {
889 // All arrays implement java.lang.Cloneable and java.io.Serializable
890 result->obj_at_put(0, Klass::cast(SystemDictionary::cloneable_klass())->java_mirror());
891 result->obj_at_put(1, Klass::cast(SystemDictionary::serializable_klass())->java_mirror());
892 }
893 return (jobjectArray) JNIHandles::make_local(env, result());
894 JVM_END
897 JVM_ENTRY(jobject, JVM_GetClassLoader(JNIEnv *env, jclass cls))
898 JVMWrapper("JVM_GetClassLoader");
899 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
900 return NULL;
901 }
902 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
903 oop loader = Klass::cast(k)->class_loader();
904 return JNIHandles::make_local(env, loader);
905 JVM_END
908 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
909 JVMWrapper("JVM_IsInterface");
910 oop mirror = JNIHandles::resolve_non_null(cls);
911 if (java_lang_Class::is_primitive(mirror)) {
912 return JNI_FALSE;
913 }
914 klassOop k = java_lang_Class::as_klassOop(mirror);
915 jboolean result = Klass::cast(k)->is_interface();
916 assert(!result || Klass::cast(k)->oop_is_instance(),
917 "all interfaces are instance types");
918 // The compiler intrinsic for isInterface tests the
919 // Klass::_access_flags bits in the same way.
920 return result;
921 JVM_END
924 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
925 JVMWrapper("JVM_GetClassSigners");
926 JvmtiVMObjectAllocEventCollector oam;
927 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
928 // There are no signers for primitive types
929 return NULL;
930 }
932 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
933 objArrayOop signers = NULL;
934 if (Klass::cast(k)->oop_is_instance()) {
935 signers = instanceKlass::cast(k)->signers();
936 }
938 // If there are no signers set in the class, or if the class
939 // is an array, return NULL.
940 if (signers == NULL) return NULL;
942 // copy of the signers array
943 klassOop element = objArrayKlass::cast(signers->klass())->element_klass();
944 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
945 for (int index = 0; index < signers->length(); index++) {
946 signers_copy->obj_at_put(index, signers->obj_at(index));
947 }
949 // return the copy
950 return (jobjectArray) JNIHandles::make_local(env, signers_copy);
951 JVM_END
954 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
955 JVMWrapper("JVM_SetClassSigners");
956 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
957 // This call is ignored for primitive types and arrays.
958 // Signers are only set once, ClassLoader.java, and thus shouldn't
959 // be called with an array. Only the bootstrap loader creates arrays.
960 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
961 if (Klass::cast(k)->oop_is_instance()) {
962 instanceKlass::cast(k)->set_signers(objArrayOop(JNIHandles::resolve(signers)));
963 }
964 }
965 JVM_END
968 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
969 JVMWrapper("JVM_GetProtectionDomain");
970 if (JNIHandles::resolve(cls) == NULL) {
971 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
972 }
974 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
975 // Primitive types does not have a protection domain.
976 return NULL;
977 }
979 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
980 return (jobject) JNIHandles::make_local(env, Klass::cast(k)->protection_domain());
981 JVM_END
984 // Obsolete since 1.2 (Class.setProtectionDomain removed), although
985 // still defined in core libraries as of 1.5.
986 JVM_ENTRY(void, JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain))
987 JVMWrapper("JVM_SetProtectionDomain");
988 if (JNIHandles::resolve(cls) == NULL) {
989 THROW(vmSymbols::java_lang_NullPointerException());
990 }
991 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
992 // Call is ignored for primitive types
993 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
995 // cls won't be an array, as this called only from ClassLoader.defineClass
996 if (Klass::cast(k)->oop_is_instance()) {
997 oop pd = JNIHandles::resolve(protection_domain);
998 assert(pd == NULL || pd->is_oop(), "just checking");
999 instanceKlass::cast(k)->set_protection_domain(pd);
1000 }
1001 }
1002 JVM_END
1005 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
1006 JVMWrapper("JVM_DoPrivileged");
1008 if (action == NULL) {
1009 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
1010 }
1012 // Stack allocated list of privileged stack elements
1013 PrivilegedElement pi;
1015 // Check that action object understands "Object run()"
1016 Handle object (THREAD, JNIHandles::resolve(action));
1018 // get run() method
1019 methodOop m_oop = Klass::cast(object->klass())->uncached_lookup_method(
1020 vmSymbols::run_method_name(),
1021 vmSymbols::void_object_signature());
1022 methodHandle m (THREAD, m_oop);
1023 if (m.is_null() || !m->is_method() || !methodOop(m())->is_public() || methodOop(m())->is_static()) {
1024 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method");
1025 }
1027 // Compute the frame initiating the do privileged operation and setup the privileged stack
1028 vframeStream vfst(thread);
1029 vfst.security_get_caller_frame(1);
1031 if (!vfst.at_end()) {
1032 pi.initialize(&vfst, JNIHandles::resolve(context), thread->privileged_stack_top(), CHECK_NULL);
1033 thread->set_privileged_stack_top(&pi);
1034 }
1037 // invoke the Object run() in the action object. We cannot use call_interface here, since the static type
1038 // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction
1039 Handle pending_exception;
1040 JavaValue result(T_OBJECT);
1041 JavaCallArguments args(object);
1042 JavaCalls::call(&result, m, &args, THREAD);
1044 // done with action, remove ourselves from the list
1045 if (!vfst.at_end()) {
1046 assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element");
1047 thread->set_privileged_stack_top(thread->privileged_stack_top()->next());
1048 }
1050 if (HAS_PENDING_EXCEPTION) {
1051 pending_exception = Handle(THREAD, PENDING_EXCEPTION);
1052 CLEAR_PENDING_EXCEPTION;
1054 if ( pending_exception->is_a(SystemDictionary::exception_klass()) &&
1055 !pending_exception->is_a(SystemDictionary::runtime_exception_klass())) {
1056 // Throw a java.security.PrivilegedActionException(Exception e) exception
1057 JavaCallArguments args(pending_exception);
1058 THROW_ARG_0(vmSymbolHandles::java_security_PrivilegedActionException(),
1059 vmSymbolHandles::exception_void_signature(),
1060 &args);
1061 }
1062 }
1064 if (pending_exception.not_null()) THROW_OOP_0(pending_exception());
1065 return JNIHandles::make_local(env, (oop) result.get_jobject());
1066 JVM_END
1069 // Returns the inherited_access_control_context field of the running thread.
1070 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1071 JVMWrapper("JVM_GetInheritedAccessControlContext");
1072 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1073 return JNIHandles::make_local(env, result);
1074 JVM_END
1076 class RegisterArrayForGC {
1077 private:
1078 JavaThread *_thread;
1079 public:
1080 RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array) {
1081 _thread = thread;
1082 _thread->register_array_for_gc(array);
1083 }
1085 ~RegisterArrayForGC() {
1086 _thread->register_array_for_gc(NULL);
1087 }
1088 };
1091 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
1092 JVMWrapper("JVM_GetStackAccessControlContext");
1093 if (!UsePrivilegedStack) return NULL;
1095 ResourceMark rm(THREAD);
1096 GrowableArray<oop>* local_array = new GrowableArray<oop>(12);
1097 JvmtiVMObjectAllocEventCollector oam;
1099 // count the protection domains on the execution stack. We collapse
1100 // duplicate consecutive protection domains into a single one, as
1101 // well as stopping when we hit a privileged frame.
1103 // Use vframeStream to iterate through Java frames
1104 vframeStream vfst(thread);
1106 oop previous_protection_domain = NULL;
1107 Handle privileged_context(thread, NULL);
1108 bool is_privileged = false;
1109 oop protection_domain = NULL;
1111 for(; !vfst.at_end(); vfst.next()) {
1112 // get method of frame
1113 methodOop method = vfst.method();
1114 intptr_t* frame_id = vfst.frame_id();
1116 // check the privileged frames to see if we have a match
1117 if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) {
1118 // this frame is privileged
1119 is_privileged = true;
1120 privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context());
1121 protection_domain = thread->privileged_stack_top()->protection_domain();
1122 } else {
1123 protection_domain = instanceKlass::cast(method->method_holder())->protection_domain();
1124 }
1126 if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
1127 local_array->push(protection_domain);
1128 previous_protection_domain = protection_domain;
1129 }
1131 if (is_privileged) break;
1132 }
1135 // either all the domains on the stack were system domains, or
1136 // we had a privileged system domain
1137 if (local_array->is_empty()) {
1138 if (is_privileged && privileged_context.is_null()) return NULL;
1140 oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
1141 return JNIHandles::make_local(env, result);
1142 }
1144 // the resource area must be registered in case of a gc
1145 RegisterArrayForGC ragc(thread, local_array);
1146 objArrayOop context = oopFactory::new_objArray(SystemDictionary::protectionDomain_klass(),
1147 local_array->length(), CHECK_NULL);
1148 objArrayHandle h_context(thread, context);
1149 for (int index = 0; index < local_array->length(); index++) {
1150 h_context->obj_at_put(index, local_array->at(index));
1151 }
1153 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
1155 return JNIHandles::make_local(env, result);
1156 JVM_END
1159 JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
1160 JVMWrapper("JVM_IsArrayClass");
1161 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1162 return (k != NULL) && Klass::cast(k)->oop_is_javaArray() ? true : false;
1163 JVM_END
1166 JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
1167 JVMWrapper("JVM_IsPrimitiveClass");
1168 oop mirror = JNIHandles::resolve_non_null(cls);
1169 return (jboolean) java_lang_Class::is_primitive(mirror);
1170 JVM_END
1173 JVM_ENTRY(jclass, JVM_GetComponentType(JNIEnv *env, jclass cls))
1174 JVMWrapper("JVM_GetComponentType");
1175 oop mirror = JNIHandles::resolve_non_null(cls);
1176 oop result = Reflection::array_component_type(mirror, CHECK_NULL);
1177 return (jclass) JNIHandles::make_local(env, result);
1178 JVM_END
1181 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
1182 JVMWrapper("JVM_GetClassModifiers");
1183 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1184 // Primitive type
1185 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1186 }
1188 Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
1189 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
1190 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1191 return k->modifier_flags();
1192 JVM_END
1195 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1197 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1198 const int inner_class_info_index = 0;
1199 const int outer_class_info_index = 1;
1201 JvmtiVMObjectAllocEventCollector oam;
1202 // ofClass is a reference to a java_lang_Class object. The mirror object
1203 // of an instanceKlass
1205 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1206 ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
1207 oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
1208 return (jobjectArray)JNIHandles::make_local(env, result);
1209 }
1211 instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1213 if (k->inner_classes()->length() == 0) {
1214 // Neither an inner nor outer class
1215 oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
1216 return (jobjectArray)JNIHandles::make_local(env, result);
1217 }
1219 // find inner class info
1220 typeArrayHandle icls(thread, k->inner_classes());
1221 constantPoolHandle cp(thread, k->constants());
1222 int length = icls->length();
1224 // Allocate temp. result array
1225 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), length/4, CHECK_NULL);
1226 objArrayHandle result (THREAD, r);
1227 int members = 0;
1229 for(int i = 0; i < length; i += 4) {
1230 int ioff = icls->ushort_at(i + inner_class_info_index);
1231 int ooff = icls->ushort_at(i + outer_class_info_index);
1233 if (ioff != 0 && ooff != 0) {
1234 // Check to see if the name matches the class we're looking for
1235 // before attempting to find the class.
1236 if (cp->klass_name_at_matches(k, ooff)) {
1237 klassOop outer_klass = cp->klass_at(ooff, CHECK_NULL);
1238 if (outer_klass == k()) {
1239 klassOop ik = cp->klass_at(ioff, CHECK_NULL);
1240 instanceKlassHandle inner_klass (THREAD, ik);
1242 // Throws an exception if outer klass has not declared k as
1243 // an inner klass
1244 Reflection::check_for_inner_class(k, inner_klass, CHECK_NULL);
1246 result->obj_at_put(members, inner_klass->java_mirror());
1247 members++;
1248 }
1249 }
1250 }
1251 }
1253 if (members != length) {
1254 // Return array of right length
1255 objArrayOop res = oopFactory::new_objArray(SystemDictionary::class_klass(), members, CHECK_NULL);
1256 for(int i = 0; i < members; i++) {
1257 res->obj_at_put(i, result->obj_at(i));
1258 }
1259 return (jobjectArray)JNIHandles::make_local(env, res);
1260 }
1262 return (jobjectArray)JNIHandles::make_local(env, result());
1263 JVM_END
1266 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1267 const int inner_class_info_index = 0;
1268 const int outer_class_info_index = 1;
1270 // ofClass is a reference to a java_lang_Class object.
1271 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1272 ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
1273 return NULL;
1274 }
1276 instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1278 if (k->inner_classes()->length() == 0) {
1279 // No inner class info => no declaring class
1280 return NULL;
1281 }
1283 typeArrayHandle i_icls(thread, k->inner_classes());
1284 constantPoolHandle i_cp(thread, k->constants());
1285 int i_length = i_icls->length();
1287 bool found = false;
1288 klassOop ok;
1289 instanceKlassHandle outer_klass;
1291 // Find inner_klass attribute
1292 for(int i = 0; i < i_length && !found; i+= 4) {
1293 int ioff = i_icls->ushort_at(i + inner_class_info_index);
1294 int ooff = i_icls->ushort_at(i + outer_class_info_index);
1296 if (ioff != 0 && ooff != 0) {
1297 // Check to see if the name matches the class we're looking for
1298 // before attempting to find the class.
1299 if (i_cp->klass_name_at_matches(k, ioff)) {
1300 klassOop inner_klass = i_cp->klass_at(ioff, CHECK_NULL);
1301 if (k() == inner_klass) {
1302 found = true;
1303 ok = i_cp->klass_at(ooff, CHECK_NULL);
1304 outer_klass = instanceKlassHandle(thread, ok);
1305 }
1306 }
1307 }
1308 }
1310 // If no inner class attribute found for this class.
1311 if (!found) return NULL;
1313 // Throws an exception if outer klass has not declared k as an inner klass
1314 Reflection::check_for_inner_class(outer_klass, k, CHECK_NULL);
1316 return (jclass)JNIHandles::make_local(env, outer_klass->java_mirror());
1317 JVM_END
1320 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1321 assert (cls != NULL, "illegal class");
1322 JVMWrapper("JVM_GetClassSignature");
1323 JvmtiVMObjectAllocEventCollector oam;
1324 ResourceMark rm(THREAD);
1325 // Return null for arrays and primatives
1326 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1327 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
1328 if (Klass::cast(k)->oop_is_instance()) {
1329 symbolHandle sym = symbolHandle(THREAD, instanceKlass::cast(k)->generic_signature());
1330 if (sym.is_null()) return NULL;
1331 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1332 return (jstring) JNIHandles::make_local(env, str());
1333 }
1334 }
1335 return NULL;
1336 JVM_END
1339 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1340 assert (cls != NULL, "illegal class");
1341 JVMWrapper("JVM_GetClassAnnotations");
1342 ResourceMark rm(THREAD);
1343 // Return null for arrays and primitives
1344 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1345 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
1346 if (Klass::cast(k)->oop_is_instance()) {
1347 return (jbyteArray) JNIHandles::make_local(env,
1348 instanceKlass::cast(k)->class_annotations());
1349 }
1350 }
1351 return NULL;
1352 JVM_END
1355 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field))
1356 assert(field != NULL, "illegal field");
1357 JVMWrapper("JVM_GetFieldAnnotations");
1359 // some of this code was adapted from from jni_FromReflectedField
1361 // field is a handle to a java.lang.reflect.Field object
1362 oop reflected = JNIHandles::resolve_non_null(field);
1363 oop mirror = java_lang_reflect_Field::clazz(reflected);
1364 klassOop k = java_lang_Class::as_klassOop(mirror);
1365 int slot = java_lang_reflect_Field::slot(reflected);
1366 int modifiers = java_lang_reflect_Field::modifiers(reflected);
1368 fieldDescriptor fd;
1369 KlassHandle kh(THREAD, k);
1370 intptr_t offset = instanceKlass::cast(kh())->offset_from_fields(slot);
1372 if (modifiers & JVM_ACC_STATIC) {
1373 // for static fields we only look in the current class
1374 if (!instanceKlass::cast(kh())->find_local_field_from_offset(offset,
1375 true, &fd)) {
1376 assert(false, "cannot find static field");
1377 return NULL; // robustness
1378 }
1379 } else {
1380 // for instance fields we start with the current class and work
1381 // our way up through the superclass chain
1382 if (!instanceKlass::cast(kh())->find_field_from_offset(offset, false,
1383 &fd)) {
1384 assert(false, "cannot find instance field");
1385 return NULL; // robustness
1386 }
1387 }
1389 return (jbyteArray) JNIHandles::make_local(env, fd.annotations());
1390 JVM_END
1393 static methodOop jvm_get_method_common(jobject method, TRAPS) {
1394 // some of this code was adapted from from jni_FromReflectedMethod
1396 oop reflected = JNIHandles::resolve_non_null(method);
1397 oop mirror = NULL;
1398 int slot = 0;
1400 if (reflected->klass() == SystemDictionary::reflect_constructor_klass()) {
1401 mirror = java_lang_reflect_Constructor::clazz(reflected);
1402 slot = java_lang_reflect_Constructor::slot(reflected);
1403 } else {
1404 assert(reflected->klass() == SystemDictionary::reflect_method_klass(),
1405 "wrong type");
1406 mirror = java_lang_reflect_Method::clazz(reflected);
1407 slot = java_lang_reflect_Method::slot(reflected);
1408 }
1409 klassOop k = java_lang_Class::as_klassOop(mirror);
1411 KlassHandle kh(THREAD, k);
1412 methodOop m = instanceKlass::cast(kh())->method_with_idnum(slot);
1413 if (m == NULL) {
1414 assert(false, "cannot find method");
1415 return NULL; // robustness
1416 }
1418 return m;
1419 }
1422 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
1423 JVMWrapper("JVM_GetMethodAnnotations");
1425 // method is a handle to a java.lang.reflect.Method object
1426 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1427 return (jbyteArray) JNIHandles::make_local(env, m->annotations());
1428 JVM_END
1431 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
1432 JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
1434 // method is a handle to a java.lang.reflect.Method object
1435 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1436 return (jbyteArray) JNIHandles::make_local(env, m->annotation_default());
1437 JVM_END
1440 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
1441 JVMWrapper("JVM_GetMethodParameterAnnotations");
1443 // method is a handle to a java.lang.reflect.Method object
1444 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1445 return (jbyteArray) JNIHandles::make_local(env, m->parameter_annotations());
1446 JVM_END
1449 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1451 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1452 {
1453 JVMWrapper("JVM_GetClassDeclaredFields");
1454 JvmtiVMObjectAllocEventCollector oam;
1456 // Exclude primitive types and array types
1457 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1458 Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1459 // Return empty array
1460 oop res = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), 0, CHECK_NULL);
1461 return (jobjectArray) JNIHandles::make_local(env, res);
1462 }
1464 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1465 constantPoolHandle cp(THREAD, k->constants());
1467 // Ensure class is linked
1468 k->link_class(CHECK_NULL);
1470 typeArrayHandle fields(THREAD, k->fields());
1471 int fields_len = fields->length();
1473 // 4496456 We need to filter out java.lang.Throwable.backtrace
1474 bool skip_backtrace = false;
1476 // Allocate result
1477 int num_fields;
1479 if (publicOnly) {
1480 num_fields = 0;
1481 for (int i = 0, j = 0; i < fields_len; i += instanceKlass::next_offset, j++) {
1482 int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
1483 if (mods & JVM_ACC_PUBLIC) ++num_fields;
1484 }
1485 } else {
1486 num_fields = fields_len / instanceKlass::next_offset;
1488 if (k() == SystemDictionary::throwable_klass()) {
1489 num_fields--;
1490 skip_backtrace = true;
1491 }
1492 }
1494 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), num_fields, CHECK_NULL);
1495 objArrayHandle result (THREAD, r);
1497 int out_idx = 0;
1498 fieldDescriptor fd;
1499 for (int i = 0; i < fields_len; i += instanceKlass::next_offset) {
1500 if (skip_backtrace) {
1501 // 4496456 skip java.lang.Throwable.backtrace
1502 int offset = k->offset_from_fields(i);
1503 if (offset == java_lang_Throwable::get_backtrace_offset()) continue;
1504 }
1506 int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
1507 if (!publicOnly || (mods & JVM_ACC_PUBLIC)) {
1508 fd.initialize(k(), i);
1509 oop field = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL);
1510 result->obj_at_put(out_idx, field);
1511 ++out_idx;
1512 }
1513 }
1514 assert(out_idx == num_fields, "just checking");
1515 return (jobjectArray) JNIHandles::make_local(env, result());
1516 }
1517 JVM_END
1519 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1520 {
1521 JVMWrapper("JVM_GetClassDeclaredMethods");
1522 JvmtiVMObjectAllocEventCollector oam;
1524 // Exclude primitive types and array types
1525 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1526 || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1527 // Return empty array
1528 oop res = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), 0, CHECK_NULL);
1529 return (jobjectArray) JNIHandles::make_local(env, res);
1530 }
1532 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1534 // Ensure class is linked
1535 k->link_class(CHECK_NULL);
1537 objArrayHandle methods (THREAD, k->methods());
1538 int methods_length = methods->length();
1539 int num_methods = 0;
1541 int i;
1542 for (i = 0; i < methods_length; i++) {
1543 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1544 if (!method->is_initializer()) {
1545 if (!publicOnly || method->is_public()) {
1546 ++num_methods;
1547 }
1548 }
1549 }
1551 // Allocate result
1552 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), num_methods, CHECK_NULL);
1553 objArrayHandle result (THREAD, r);
1555 int out_idx = 0;
1556 for (i = 0; i < methods_length; i++) {
1557 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1558 if (!method->is_initializer()) {
1559 if (!publicOnly || method->is_public()) {
1560 oop m = Reflection::new_method(method, UseNewReflection, false, CHECK_NULL);
1561 result->obj_at_put(out_idx, m);
1562 ++out_idx;
1563 }
1564 }
1565 }
1566 assert(out_idx == num_methods, "just checking");
1567 return (jobjectArray) JNIHandles::make_local(env, result());
1568 }
1569 JVM_END
1571 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1572 {
1573 JVMWrapper("JVM_GetClassDeclaredConstructors");
1574 JvmtiVMObjectAllocEventCollector oam;
1576 // Exclude primitive types and array types
1577 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1578 || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1579 // Return empty array
1580 oop res = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0 , CHECK_NULL);
1581 return (jobjectArray) JNIHandles::make_local(env, res);
1582 }
1584 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1586 // Ensure class is linked
1587 k->link_class(CHECK_NULL);
1589 objArrayHandle methods (THREAD, k->methods());
1590 int methods_length = methods->length();
1591 int num_constructors = 0;
1593 int i;
1594 for (i = 0; i < methods_length; i++) {
1595 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1596 if (method->is_initializer() && !method->is_static()) {
1597 if (!publicOnly || method->is_public()) {
1598 ++num_constructors;
1599 }
1600 }
1601 }
1603 // Allocate result
1604 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), num_constructors, CHECK_NULL);
1605 objArrayHandle result(THREAD, r);
1607 int out_idx = 0;
1608 for (i = 0; i < methods_length; i++) {
1609 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1610 if (method->is_initializer() && !method->is_static()) {
1611 if (!publicOnly || method->is_public()) {
1612 oop m = Reflection::new_constructor(method, CHECK_NULL);
1613 result->obj_at_put(out_idx, m);
1614 ++out_idx;
1615 }
1616 }
1617 }
1618 assert(out_idx == num_constructors, "just checking");
1619 return (jobjectArray) JNIHandles::make_local(env, result());
1620 }
1621 JVM_END
1623 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
1624 {
1625 JVMWrapper("JVM_GetClassAccessFlags");
1626 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1627 // Primitive type
1628 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1629 }
1631 Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
1632 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1633 }
1634 JVM_END
1637 // Constant pool access //////////////////////////////////////////////////////////
1639 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
1640 {
1641 JVMWrapper("JVM_GetClassConstantPool");
1642 JvmtiVMObjectAllocEventCollector oam;
1644 // Return null for primitives and arrays
1645 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1646 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1647 if (Klass::cast(k)->oop_is_instance()) {
1648 instanceKlassHandle k_h(THREAD, k);
1649 Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL);
1650 sun_reflect_ConstantPool::set_cp_oop(jcp(), k_h->constants());
1651 return JNIHandles::make_local(jcp());
1652 }
1653 }
1654 return NULL;
1655 }
1656 JVM_END
1659 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool))
1660 {
1661 JVMWrapper("JVM_ConstantPoolGetSize");
1662 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1663 return cp->length();
1664 }
1665 JVM_END
1668 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
1669 if (!cp->is_within_bounds(index)) {
1670 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1671 }
1672 }
1675 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1676 {
1677 JVMWrapper("JVM_ConstantPoolGetClassAt");
1678 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1679 bounds_check(cp, index, CHECK_NULL);
1680 constantTag tag = cp->tag_at(index);
1681 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
1682 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1683 }
1684 klassOop k = cp->klass_at(index, CHECK_NULL);
1685 return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
1686 }
1687 JVM_END
1690 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1691 {
1692 JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded");
1693 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1694 bounds_check(cp, index, CHECK_NULL);
1695 constantTag tag = cp->tag_at(index);
1696 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
1697 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1698 }
1699 klassOop k = constantPoolOopDesc::klass_at_if_loaded(cp, index);
1700 if (k == NULL) return NULL;
1701 return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
1702 }
1703 JVM_END
1705 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1706 constantTag tag = cp->tag_at(index);
1707 if (!tag.is_method() && !tag.is_interface_method()) {
1708 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1709 }
1710 int klass_ref = cp->uncached_klass_ref_index_at(index);
1711 klassOop k_o;
1712 if (force_resolution) {
1713 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1714 } else {
1715 k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
1716 if (k_o == NULL) return NULL;
1717 }
1718 instanceKlassHandle k(THREAD, k_o);
1719 symbolOop name = cp->uncached_name_ref_at(index);
1720 symbolOop sig = cp->uncached_signature_ref_at(index);
1721 methodHandle m (THREAD, k->find_method(name, sig));
1722 if (m.is_null()) {
1723 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1724 }
1725 oop method;
1726 if (!m->is_initializer() || m->is_static()) {
1727 method = Reflection::new_method(m, true, true, CHECK_NULL);
1728 } else {
1729 method = Reflection::new_constructor(m, CHECK_NULL);
1730 }
1731 return JNIHandles::make_local(method);
1732 }
1734 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1735 {
1736 JVMWrapper("JVM_ConstantPoolGetMethodAt");
1737 JvmtiVMObjectAllocEventCollector oam;
1738 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1739 bounds_check(cp, index, CHECK_NULL);
1740 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
1741 return res;
1742 }
1743 JVM_END
1745 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1746 {
1747 JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded");
1748 JvmtiVMObjectAllocEventCollector oam;
1749 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1750 bounds_check(cp, index, CHECK_NULL);
1751 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
1752 return res;
1753 }
1754 JVM_END
1756 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1757 constantTag tag = cp->tag_at(index);
1758 if (!tag.is_field()) {
1759 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1760 }
1761 int klass_ref = cp->uncached_klass_ref_index_at(index);
1762 klassOop k_o;
1763 if (force_resolution) {
1764 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1765 } else {
1766 k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
1767 if (k_o == NULL) return NULL;
1768 }
1769 instanceKlassHandle k(THREAD, k_o);
1770 symbolOop name = cp->uncached_name_ref_at(index);
1771 symbolOop sig = cp->uncached_signature_ref_at(index);
1772 fieldDescriptor fd;
1773 klassOop target_klass = k->find_field(name, sig, &fd);
1774 if (target_klass == NULL) {
1775 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
1776 }
1777 oop field = Reflection::new_field(&fd, true, CHECK_NULL);
1778 return JNIHandles::make_local(field);
1779 }
1781 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1782 {
1783 JVMWrapper("JVM_ConstantPoolGetFieldAt");
1784 JvmtiVMObjectAllocEventCollector oam;
1785 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1786 bounds_check(cp, index, CHECK_NULL);
1787 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
1788 return res;
1789 }
1790 JVM_END
1792 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1793 {
1794 JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded");
1795 JvmtiVMObjectAllocEventCollector oam;
1796 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1797 bounds_check(cp, index, CHECK_NULL);
1798 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
1799 return res;
1800 }
1801 JVM_END
1803 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1804 {
1805 JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt");
1806 JvmtiVMObjectAllocEventCollector oam;
1807 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1808 bounds_check(cp, index, CHECK_NULL);
1809 constantTag tag = cp->tag_at(index);
1810 if (!tag.is_field_or_method()) {
1811 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1812 }
1813 int klass_ref = cp->uncached_klass_ref_index_at(index);
1814 symbolHandle klass_name (THREAD, cp->klass_name_at(klass_ref));
1815 symbolHandle member_name(THREAD, cp->uncached_name_ref_at(index));
1816 symbolHandle member_sig (THREAD, cp->uncached_signature_ref_at(index));
1817 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::string_klass(), 3, CHECK_NULL);
1818 objArrayHandle dest(THREAD, dest_o);
1819 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
1820 dest->obj_at_put(0, str());
1821 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
1822 dest->obj_at_put(1, str());
1823 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
1824 dest->obj_at_put(2, str());
1825 return (jobjectArray) JNIHandles::make_local(dest());
1826 }
1827 JVM_END
1829 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1830 {
1831 JVMWrapper("JVM_ConstantPoolGetIntAt");
1832 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1833 bounds_check(cp, index, CHECK_0);
1834 constantTag tag = cp->tag_at(index);
1835 if (!tag.is_int()) {
1836 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1837 }
1838 return cp->int_at(index);
1839 }
1840 JVM_END
1842 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1843 {
1844 JVMWrapper("JVM_ConstantPoolGetLongAt");
1845 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1846 bounds_check(cp, index, CHECK_(0L));
1847 constantTag tag = cp->tag_at(index);
1848 if (!tag.is_long()) {
1849 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1850 }
1851 return cp->long_at(index);
1852 }
1853 JVM_END
1855 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1856 {
1857 JVMWrapper("JVM_ConstantPoolGetFloatAt");
1858 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1859 bounds_check(cp, index, CHECK_(0.0f));
1860 constantTag tag = cp->tag_at(index);
1861 if (!tag.is_float()) {
1862 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1863 }
1864 return cp->float_at(index);
1865 }
1866 JVM_END
1868 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1869 {
1870 JVMWrapper("JVM_ConstantPoolGetDoubleAt");
1871 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1872 bounds_check(cp, index, CHECK_(0.0));
1873 constantTag tag = cp->tag_at(index);
1874 if (!tag.is_double()) {
1875 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1876 }
1877 return cp->double_at(index);
1878 }
1879 JVM_END
1881 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1882 {
1883 JVMWrapper("JVM_ConstantPoolGetStringAt");
1884 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1885 bounds_check(cp, index, CHECK_NULL);
1886 constantTag tag = cp->tag_at(index);
1887 if (!tag.is_string() && !tag.is_unresolved_string()) {
1888 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1889 }
1890 oop str = cp->string_at(index, CHECK_NULL);
1891 return (jstring) JNIHandles::make_local(str);
1892 }
1893 JVM_END
1895 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1896 {
1897 JVMWrapper("JVM_ConstantPoolGetUTF8At");
1898 JvmtiVMObjectAllocEventCollector oam;
1899 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1900 bounds_check(cp, index, CHECK_NULL);
1901 constantTag tag = cp->tag_at(index);
1902 if (!tag.is_symbol()) {
1903 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1904 }
1905 symbolOop sym_o = cp->symbol_at(index);
1906 symbolHandle sym(THREAD, sym_o);
1907 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1908 return (jstring) JNIHandles::make_local(str());
1909 }
1910 JVM_END
1913 // Assertion support. //////////////////////////////////////////////////////////
1915 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
1916 JVMWrapper("JVM_DesiredAssertionStatus");
1917 assert(cls != NULL, "bad class");
1919 oop r = JNIHandles::resolve(cls);
1920 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
1921 if (java_lang_Class::is_primitive(r)) return false;
1923 klassOop k = java_lang_Class::as_klassOop(r);
1924 assert(Klass::cast(k)->oop_is_instance(), "must be an instance klass");
1925 if (! Klass::cast(k)->oop_is_instance()) return false;
1927 ResourceMark rm(THREAD);
1928 const char* name = Klass::cast(k)->name()->as_C_string();
1929 bool system_class = Klass::cast(k)->class_loader() == NULL;
1930 return JavaAssertions::enabled(name, system_class);
1932 JVM_END
1935 // Return a new AssertionStatusDirectives object with the fields filled in with
1936 // command-line assertion arguments (i.e., -ea, -da).
1937 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
1938 JVMWrapper("JVM_AssertionStatusDirectives");
1939 JvmtiVMObjectAllocEventCollector oam;
1940 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
1941 return JNIHandles::make_local(env, asd);
1942 JVM_END
1944 // Verification ////////////////////////////////////////////////////////////////////////////////
1946 // Reflection for the verifier /////////////////////////////////////////////////////////////////
1948 // RedefineClasses support: bug 6214132 caused verification to fail.
1949 // All functions from this section should call the jvmtiThreadSate function:
1950 // klassOop class_to_verify_considering_redefinition(klassOop klass).
1951 // The function returns a klassOop of the _scratch_class if the verifier
1952 // was invoked in the middle of the class redefinition.
1953 // Otherwise it returns its argument value which is the _the_class klassOop.
1954 // Please, refer to the description in the jvmtiThreadSate.hpp.
1956 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
1957 JVMWrapper("JVM_GetClassNameUTF");
1958 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1959 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1960 return Klass::cast(k)->name()->as_utf8();
1961 JVM_END
1964 JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
1965 JVMWrapper("JVM_GetClassCPTypes");
1966 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1967 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1968 // types will have length zero if this is not an instanceKlass
1969 // (length is determined by call to JVM_GetClassCPEntriesCount)
1970 if (Klass::cast(k)->oop_is_instance()) {
1971 constantPoolOop cp = instanceKlass::cast(k)->constants();
1972 for (int index = cp->length() - 1; index >= 0; index--) {
1973 constantTag tag = cp->tag_at(index);
1974 types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class :
1975 (tag.is_unresolved_string()) ? JVM_CONSTANT_String : tag.value();
1976 }
1977 }
1978 JVM_END
1981 JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
1982 JVMWrapper("JVM_GetClassCPEntriesCount");
1983 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1984 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1985 if (!Klass::cast(k)->oop_is_instance())
1986 return 0;
1987 return instanceKlass::cast(k)->constants()->length();
1988 JVM_END
1991 JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
1992 JVMWrapper("JVM_GetClassFieldsCount");
1993 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1994 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1995 if (!Klass::cast(k)->oop_is_instance())
1996 return 0;
1997 return instanceKlass::cast(k)->fields()->length() / instanceKlass::next_offset;
1998 JVM_END
2001 JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
2002 JVMWrapper("JVM_GetClassMethodsCount");
2003 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2004 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2005 if (!Klass::cast(k)->oop_is_instance())
2006 return 0;
2007 return instanceKlass::cast(k)->methods()->length();
2008 JVM_END
2011 // The following methods, used for the verifier, are never called with
2012 // array klasses, so a direct cast to instanceKlass is safe.
2013 // Typically, these methods are called in a loop with bounds determined
2014 // by the results of JVM_GetClass{Fields,Methods}Count, which return
2015 // zero for arrays.
2016 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
2017 JVMWrapper("JVM_GetMethodIxExceptionIndexes");
2018 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2019 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2020 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2021 int length = methodOop(method)->checked_exceptions_length();
2022 if (length > 0) {
2023 CheckedExceptionElement* table= methodOop(method)->checked_exceptions_start();
2024 for (int i = 0; i < length; i++) {
2025 exceptions[i] = table[i].class_cp_index;
2026 }
2027 }
2028 JVM_END
2031 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
2032 JVMWrapper("JVM_GetMethodIxExceptionsCount");
2033 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2034 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2035 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2036 return methodOop(method)->checked_exceptions_length();
2037 JVM_END
2040 JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
2041 JVMWrapper("JVM_GetMethodIxByteCode");
2042 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2043 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2044 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2045 memcpy(code, methodOop(method)->code_base(), methodOop(method)->code_size());
2046 JVM_END
2049 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
2050 JVMWrapper("JVM_GetMethodIxByteCodeLength");
2051 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2052 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2053 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2054 return methodOop(method)->code_size();
2055 JVM_END
2058 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
2059 JVMWrapper("JVM_GetMethodIxExceptionTableEntry");
2060 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2061 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2062 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2063 typeArrayOop extable = methodOop(method)->exception_table();
2064 entry->start_pc = extable->int_at(entry_index * 4);
2065 entry->end_pc = extable->int_at(entry_index * 4 + 1);
2066 entry->handler_pc = extable->int_at(entry_index * 4 + 2);
2067 entry->catchType = extable->int_at(entry_index * 4 + 3);
2068 JVM_END
2071 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2072 JVMWrapper("JVM_GetMethodIxExceptionTableLength");
2073 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2074 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2075 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2076 return methodOop(method)->exception_table()->length() / 4;
2077 JVM_END
2080 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2081 JVMWrapper("JVM_GetMethodIxModifiers");
2082 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2083 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2084 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2085 return methodOop(method)->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2086 JVM_END
2089 JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2090 JVMWrapper("JVM_GetFieldIxModifiers");
2091 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2092 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2093 typeArrayOop fields = instanceKlass::cast(k)->fields();
2094 return fields->ushort_at(field_index * instanceKlass::next_offset + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2095 JVM_END
2098 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2099 JVMWrapper("JVM_GetMethodIxLocalsCount");
2100 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2101 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2102 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2103 return methodOop(method)->max_locals();
2104 JVM_END
2107 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2108 JVMWrapper("JVM_GetMethodIxArgsSize");
2109 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2110 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2111 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2112 return methodOop(method)->size_of_parameters();
2113 JVM_END
2116 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2117 JVMWrapper("JVM_GetMethodIxMaxStack");
2118 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2119 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2120 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2121 return methodOop(method)->max_stack();
2122 JVM_END
2125 JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2126 JVMWrapper("JVM_IsConstructorIx");
2127 ResourceMark rm(THREAD);
2128 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2129 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2130 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2131 return methodOop(method)->name() == vmSymbols::object_initializer_name();
2132 JVM_END
2135 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2136 JVMWrapper("JVM_GetMethodIxIxUTF");
2137 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2138 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2139 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2140 return methodOop(method)->name()->as_utf8();
2141 JVM_END
2144 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2145 JVMWrapper("JVM_GetMethodIxSignatureUTF");
2146 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2147 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2148 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2149 return methodOop(method)->signature()->as_utf8();
2150 JVM_END
2152 /**
2153 * All of these JVM_GetCP-xxx methods are used by the old verifier to
2154 * read entries in the constant pool. Since the old verifier always
2155 * works on a copy of the code, it will not see any rewriting that
2156 * may possibly occur in the middle of verification. So it is important
2157 * that nothing it calls tries to use the cpCache instead of the raw
2158 * constant pool, so we must use cp->uncached_x methods when appropriate.
2159 */
2160 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2161 JVMWrapper("JVM_GetCPFieldNameUTF");
2162 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2163 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2164 constantPoolOop cp = instanceKlass::cast(k)->constants();
2165 switch (cp->tag_at(cp_index).value()) {
2166 case JVM_CONSTANT_Fieldref:
2167 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2168 default:
2169 fatal("JVM_GetCPFieldNameUTF: illegal constant");
2170 }
2171 ShouldNotReachHere();
2172 return NULL;
2173 JVM_END
2176 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2177 JVMWrapper("JVM_GetCPMethodNameUTF");
2178 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2179 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2180 constantPoolOop cp = instanceKlass::cast(k)->constants();
2181 switch (cp->tag_at(cp_index).value()) {
2182 case JVM_CONSTANT_InterfaceMethodref:
2183 case JVM_CONSTANT_Methodref:
2184 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2185 default:
2186 fatal("JVM_GetCPMethodNameUTF: illegal constant");
2187 }
2188 ShouldNotReachHere();
2189 return NULL;
2190 JVM_END
2193 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2194 JVMWrapper("JVM_GetCPMethodSignatureUTF");
2195 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2196 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2197 constantPoolOop cp = instanceKlass::cast(k)->constants();
2198 switch (cp->tag_at(cp_index).value()) {
2199 case JVM_CONSTANT_InterfaceMethodref:
2200 case JVM_CONSTANT_Methodref:
2201 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2202 default:
2203 fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
2204 }
2205 ShouldNotReachHere();
2206 return NULL;
2207 JVM_END
2210 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2211 JVMWrapper("JVM_GetCPFieldSignatureUTF");
2212 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2213 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2214 constantPoolOop cp = instanceKlass::cast(k)->constants();
2215 switch (cp->tag_at(cp_index).value()) {
2216 case JVM_CONSTANT_Fieldref:
2217 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2218 default:
2219 fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
2220 }
2221 ShouldNotReachHere();
2222 return NULL;
2223 JVM_END
2226 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2227 JVMWrapper("JVM_GetCPClassNameUTF");
2228 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2229 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2230 constantPoolOop cp = instanceKlass::cast(k)->constants();
2231 symbolOop classname = cp->klass_name_at(cp_index);
2232 return classname->as_utf8();
2233 JVM_END
2236 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2237 JVMWrapper("JVM_GetCPFieldClassNameUTF");
2238 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2239 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2240 constantPoolOop cp = instanceKlass::cast(k)->constants();
2241 switch (cp->tag_at(cp_index).value()) {
2242 case JVM_CONSTANT_Fieldref: {
2243 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2244 symbolOop classname = cp->klass_name_at(class_index);
2245 return classname->as_utf8();
2246 }
2247 default:
2248 fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
2249 }
2250 ShouldNotReachHere();
2251 return NULL;
2252 JVM_END
2255 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2256 JVMWrapper("JVM_GetCPMethodClassNameUTF");
2257 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2258 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2259 constantPoolOop cp = instanceKlass::cast(k)->constants();
2260 switch (cp->tag_at(cp_index).value()) {
2261 case JVM_CONSTANT_Methodref:
2262 case JVM_CONSTANT_InterfaceMethodref: {
2263 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2264 symbolOop classname = cp->klass_name_at(class_index);
2265 return classname->as_utf8();
2266 }
2267 default:
2268 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2269 }
2270 ShouldNotReachHere();
2271 return NULL;
2272 JVM_END
2275 JVM_QUICK_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2276 JVMWrapper("JVM_GetCPFieldModifiers");
2277 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2278 klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
2279 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2280 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2281 constantPoolOop cp = instanceKlass::cast(k)->constants();
2282 constantPoolOop cp_called = instanceKlass::cast(k_called)->constants();
2283 switch (cp->tag_at(cp_index).value()) {
2284 case JVM_CONSTANT_Fieldref: {
2285 symbolOop name = cp->uncached_name_ref_at(cp_index);
2286 symbolOop signature = cp->uncached_signature_ref_at(cp_index);
2287 typeArrayOop fields = instanceKlass::cast(k_called)->fields();
2288 int fields_count = fields->length();
2289 for (int i = 0; i < fields_count; i += instanceKlass::next_offset) {
2290 if (cp_called->symbol_at(fields->ushort_at(i + instanceKlass::name_index_offset)) == name &&
2291 cp_called->symbol_at(fields->ushort_at(i + instanceKlass::signature_index_offset)) == signature) {
2292 return fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2293 }
2294 }
2295 return -1;
2296 }
2297 default:
2298 fatal("JVM_GetCPFieldModifiers: illegal constant");
2299 }
2300 ShouldNotReachHere();
2301 return 0;
2302 JVM_END
2305 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2306 JVMWrapper("JVM_GetCPMethodModifiers");
2307 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2308 klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
2309 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2310 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2311 constantPoolOop cp = instanceKlass::cast(k)->constants();
2312 switch (cp->tag_at(cp_index).value()) {
2313 case JVM_CONSTANT_Methodref:
2314 case JVM_CONSTANT_InterfaceMethodref: {
2315 symbolOop name = cp->uncached_name_ref_at(cp_index);
2316 symbolOop signature = cp->uncached_signature_ref_at(cp_index);
2317 objArrayOop methods = instanceKlass::cast(k_called)->methods();
2318 int methods_count = methods->length();
2319 for (int i = 0; i < methods_count; i++) {
2320 methodOop method = methodOop(methods->obj_at(i));
2321 if (method->name() == name && method->signature() == signature) {
2322 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2323 }
2324 }
2325 return -1;
2326 }
2327 default:
2328 fatal("JVM_GetCPMethodModifiers: illegal constant");
2329 }
2330 ShouldNotReachHere();
2331 return 0;
2332 JVM_END
2335 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
2337 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
2338 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
2339 JVM_END
2342 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
2343 JVMWrapper("JVM_IsSameClassPackage");
2344 oop class1_mirror = JNIHandles::resolve_non_null(class1);
2345 oop class2_mirror = JNIHandles::resolve_non_null(class2);
2346 klassOop klass1 = java_lang_Class::as_klassOop(class1_mirror);
2347 klassOop klass2 = java_lang_Class::as_klassOop(class2_mirror);
2348 return (jboolean) Reflection::is_same_class_package(klass1, klass2);
2349 JVM_END
2352 // IO functions ////////////////////////////////////////////////////////////////////////////////////////
2354 JVM_LEAF(jint, JVM_Open(const char *fname, jint flags, jint mode))
2355 JVMWrapper2("JVM_Open (%s)", fname);
2357 //%note jvm_r6
2358 int result = hpi::open(fname, flags, mode);
2359 if (result >= 0) {
2360 return result;
2361 } else {
2362 switch(errno) {
2363 case EEXIST:
2364 return JVM_EEXIST;
2365 default:
2366 return -1;
2367 }
2368 }
2369 JVM_END
2372 JVM_LEAF(jint, JVM_Close(jint fd))
2373 JVMWrapper2("JVM_Close (0x%x)", fd);
2374 //%note jvm_r6
2375 return hpi::close(fd);
2376 JVM_END
2379 JVM_LEAF(jint, JVM_Read(jint fd, char *buf, jint nbytes))
2380 JVMWrapper2("JVM_Read (0x%x)", fd);
2382 //%note jvm_r6
2383 return (jint)hpi::read(fd, buf, nbytes);
2384 JVM_END
2387 JVM_LEAF(jint, JVM_Write(jint fd, char *buf, jint nbytes))
2388 JVMWrapper2("JVM_Write (0x%x)", fd);
2390 //%note jvm_r6
2391 return (jint)hpi::write(fd, buf, nbytes);
2392 JVM_END
2395 JVM_LEAF(jint, JVM_Available(jint fd, jlong *pbytes))
2396 JVMWrapper2("JVM_Available (0x%x)", fd);
2397 //%note jvm_r6
2398 return hpi::available(fd, pbytes);
2399 JVM_END
2402 JVM_LEAF(jlong, JVM_Lseek(jint fd, jlong offset, jint whence))
2403 JVMWrapper4("JVM_Lseek (0x%x, %Ld, %d)", fd, offset, whence);
2404 //%note jvm_r6
2405 return hpi::lseek(fd, offset, whence);
2406 JVM_END
2409 JVM_LEAF(jint, JVM_SetLength(jint fd, jlong length))
2410 JVMWrapper3("JVM_SetLength (0x%x, %Ld)", fd, length);
2411 return hpi::ftruncate(fd, length);
2412 JVM_END
2415 JVM_LEAF(jint, JVM_Sync(jint fd))
2416 JVMWrapper2("JVM_Sync (0x%x)", fd);
2417 //%note jvm_r6
2418 return hpi::fsync(fd);
2419 JVM_END
2422 // Printing support //////////////////////////////////////////////////
2423 extern "C" {
2425 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
2426 // see bug 4399518, 4417214
2427 if ((intptr_t)count <= 0) return -1;
2428 return vsnprintf(str, count, fmt, args);
2429 }
2432 int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
2433 va_list args;
2434 int len;
2435 va_start(args, fmt);
2436 len = jio_vsnprintf(str, count, fmt, args);
2437 va_end(args);
2438 return len;
2439 }
2442 int jio_fprintf(FILE* f, const char *fmt, ...) {
2443 int len;
2444 va_list args;
2445 va_start(args, fmt);
2446 len = jio_vfprintf(f, fmt, args);
2447 va_end(args);
2448 return len;
2449 }
2452 int jio_vfprintf(FILE* f, const char *fmt, va_list args) {
2453 if (Arguments::vfprintf_hook() != NULL) {
2454 return Arguments::vfprintf_hook()(f, fmt, args);
2455 } else {
2456 return vfprintf(f, fmt, args);
2457 }
2458 }
2461 int jio_printf(const char *fmt, ...) {
2462 int len;
2463 va_list args;
2464 va_start(args, fmt);
2465 len = jio_vfprintf(defaultStream::output_stream(), fmt, args);
2466 va_end(args);
2467 return len;
2468 }
2471 // HotSpot specific jio method
2472 void jio_print(const char* s) {
2473 // Try to make this function as atomic as possible.
2474 if (Arguments::vfprintf_hook() != NULL) {
2475 jio_fprintf(defaultStream::output_stream(), "%s", s);
2476 } else {
2477 ::write(defaultStream::output_fd(), s, (int)strlen(s));
2478 }
2479 }
2481 } // Extern C
2483 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2485 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
2486 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
2487 // OSThread objects. The exception to this rule is when the target object is the thread
2488 // doing the operation, in which case we know that the thread won't exit until the
2489 // operation is done (all exits being voluntary). There are a few cases where it is
2490 // rather silly to do operations on yourself, like resuming yourself or asking whether
2491 // you are alive. While these can still happen, they are not subject to deadlocks if
2492 // the lock is held while the operation occurs (this is not the case for suspend, for
2493 // instance), and are very unlikely. Because IsAlive needs to be fast and its
2494 // implementation is local to this file, we always lock Threads_lock for that one.
2496 static void thread_entry(JavaThread* thread, TRAPS) {
2497 HandleMark hm(THREAD);
2498 Handle obj(THREAD, thread->threadObj());
2499 JavaValue result(T_VOID);
2500 JavaCalls::call_virtual(&result,
2501 obj,
2502 KlassHandle(THREAD, SystemDictionary::thread_klass()),
2503 vmSymbolHandles::run_method_name(),
2504 vmSymbolHandles::void_method_signature(),
2505 THREAD);
2506 }
2509 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2510 JVMWrapper("JVM_StartThread");
2511 JavaThread *native_thread = NULL;
2513 // We cannot hold the Threads_lock when we throw an exception,
2514 // due to rank ordering issues. Example: we might need to grab the
2515 // Heap_lock while we construct the exception.
2516 bool throw_illegal_thread_state = false;
2518 // We must release the Threads_lock before we can post a jvmti event
2519 // in Thread::start.
2520 {
2521 // Ensure that the C++ Thread and OSThread structures aren't freed before
2522 // we operate.
2523 MutexLocker mu(Threads_lock);
2525 // Check to see if we're running a thread that's already exited or was
2526 // stopped (is_stillborn) or is still active (thread is not NULL).
2527 if (java_lang_Thread::is_stillborn(JNIHandles::resolve_non_null(jthread)) ||
2528 java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
2529 throw_illegal_thread_state = true;
2530 } else {
2531 jlong size =
2532 java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
2533 // Allocate the C++ Thread structure and create the native thread. The
2534 // stack size retrieved from java is signed, but the constructor takes
2535 // size_t (an unsigned type), so avoid passing negative values which would
2536 // result in really large stacks.
2537 size_t sz = size > 0 ? (size_t) size : 0;
2538 native_thread = new JavaThread(&thread_entry, sz);
2540 // At this point it may be possible that no osthread was created for the
2541 // JavaThread due to lack of memory. Check for this situation and throw
2542 // an exception if necessary. Eventually we may want to change this so
2543 // that we only grab the lock if the thread was created successfully -
2544 // then we can also do this check and throw the exception in the
2545 // JavaThread constructor.
2546 if (native_thread->osthread() != NULL) {
2547 // Note: the current thread is not being used within "prepare".
2548 native_thread->prepare(jthread);
2549 }
2550 }
2551 }
2553 if (throw_illegal_thread_state) {
2554 THROW(vmSymbols::java_lang_IllegalThreadStateException());
2555 }
2557 assert(native_thread != NULL, "Starting null thread?");
2559 if (native_thread->osthread() == NULL) {
2560 // No one should hold a reference to the 'native_thread'.
2561 delete native_thread;
2562 if (JvmtiExport::should_post_resource_exhausted()) {
2563 JvmtiExport::post_resource_exhausted(
2564 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
2565 "unable to create new native thread");
2566 }
2567 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
2568 "unable to create new native thread");
2569 }
2571 Thread::start(native_thread);
2573 JVM_END
2575 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
2576 // before the quasi-asynchronous exception is delivered. This is a little obtrusive,
2577 // but is thought to be reliable and simple. In the case, where the receiver is the
2578 // save thread as the sender, no safepoint is needed.
2579 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
2580 JVMWrapper("JVM_StopThread");
2582 oop java_throwable = JNIHandles::resolve(throwable);
2583 if (java_throwable == NULL) {
2584 THROW(vmSymbols::java_lang_NullPointerException());
2585 }
2586 oop java_thread = JNIHandles::resolve_non_null(jthread);
2587 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2588 Events::log("JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", receiver, (address)java_thread, throwable);
2589 // First check if thread already exited
2590 if (receiver != NULL) {
2591 // Check if exception is getting thrown at self (use oop equality, since the
2592 // target object might exit)
2593 if (java_thread == thread->threadObj()) {
2594 // This is a change from JDK 1.1, but JDK 1.2 will also do it:
2595 // NOTE (from JDK 1.2): this is done solely to prevent stopped
2596 // threads from being restarted.
2597 // Fix for 4314342, 4145910, perhaps others: it now doesn't have
2598 // any effect on the "liveness" of a thread; see
2599 // JVM_IsThreadAlive, below.
2600 if (java_throwable->is_a(SystemDictionary::threaddeath_klass())) {
2601 java_lang_Thread::set_stillborn(java_thread);
2602 }
2603 THROW_OOP(java_throwable);
2604 } else {
2605 // Enques a VM_Operation to stop all threads and then deliver the exception...
2606 Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable));
2607 }
2608 }
2609 JVM_END
2612 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
2613 JVMWrapper("JVM_IsThreadAlive");
2615 oop thread_oop = JNIHandles::resolve_non_null(jthread);
2616 return java_lang_Thread::is_alive(thread_oop);
2617 JVM_END
2620 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
2621 JVMWrapper("JVM_SuspendThread");
2622 oop java_thread = JNIHandles::resolve_non_null(jthread);
2623 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2625 if (receiver != NULL) {
2626 // thread has run and has not exited (still on threads list)
2628 {
2629 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
2630 if (receiver->is_external_suspend()) {
2631 // Don't allow nested external suspend requests. We can't return
2632 // an error from this interface so just ignore the problem.
2633 return;
2634 }
2635 if (receiver->is_exiting()) { // thread is in the process of exiting
2636 return;
2637 }
2638 receiver->set_external_suspend();
2639 }
2641 // java_suspend() will catch threads in the process of exiting
2642 // and will ignore them.
2643 receiver->java_suspend();
2645 // It would be nice to have the following assertion in all the
2646 // time, but it is possible for a racing resume request to have
2647 // resumed this thread right after we suspended it. Temporarily
2648 // enable this assertion if you are chasing a different kind of
2649 // bug.
2650 //
2651 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
2652 // receiver->is_being_ext_suspended(), "thread is not suspended");
2653 }
2654 JVM_END
2657 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
2658 JVMWrapper("JVM_ResumeThread");
2659 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate.
2660 // We need to *always* get the threads lock here, since this operation cannot be allowed during
2661 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
2662 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
2663 // looks at it.
2664 MutexLocker ml(Threads_lock);
2665 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2666 if (thr != NULL) {
2667 // the thread has run and is not in the process of exiting
2668 thr->java_resume();
2669 }
2670 JVM_END
2673 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
2674 JVMWrapper("JVM_SetThreadPriority");
2675 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2676 MutexLocker ml(Threads_lock);
2677 oop java_thread = JNIHandles::resolve_non_null(jthread);
2678 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
2679 JavaThread* thr = java_lang_Thread::thread(java_thread);
2680 if (thr != NULL) { // Thread not yet started; priority pushed down when it is
2681 Thread::set_priority(thr, (ThreadPriority)prio);
2682 }
2683 JVM_END
2686 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
2687 JVMWrapper("JVM_Yield");
2688 if (os::dont_yield()) return;
2689 // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield.
2690 // Critical for similar threading behaviour
2691 if (ConvertYieldToSleep) {
2692 os::sleep(thread, MinSleepInterval, false);
2693 } else {
2694 os::yield();
2695 }
2696 JVM_END
2699 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
2700 JVMWrapper("JVM_Sleep");
2702 if (millis < 0) {
2703 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
2704 }
2706 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
2707 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2708 }
2710 // Save current thread state and restore it at the end of this block.
2711 // And set new thread state to SLEEPING.
2712 JavaThreadSleepState jtss(thread);
2714 if (millis == 0) {
2715 // When ConvertSleepToYield is on, this matches the classic VM implementation of
2716 // JVM_Sleep. Critical for similar threading behaviour (Win32)
2717 // It appears that in certain GUI contexts, it may be beneficial to do a short sleep
2718 // for SOLARIS
2719 if (ConvertSleepToYield) {
2720 os::yield();
2721 } else {
2722 ThreadState old_state = thread->osthread()->get_state();
2723 thread->osthread()->set_state(SLEEPING);
2724 os::sleep(thread, MinSleepInterval, false);
2725 thread->osthread()->set_state(old_state);
2726 }
2727 } else {
2728 ThreadState old_state = thread->osthread()->get_state();
2729 thread->osthread()->set_state(SLEEPING);
2730 if (os::sleep(thread, millis, true) == OS_INTRPT) {
2731 // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
2732 // us while we were sleeping. We do not overwrite those.
2733 if (!HAS_PENDING_EXCEPTION) {
2734 // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
2735 // to properly restore the thread state. That's likely wrong.
2736 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2737 }
2738 }
2739 thread->osthread()->set_state(old_state);
2740 }
2741 JVM_END
2743 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
2744 JVMWrapper("JVM_CurrentThread");
2745 oop jthread = thread->threadObj();
2746 assert (thread != NULL, "no current thread!");
2747 return JNIHandles::make_local(env, jthread);
2748 JVM_END
2751 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
2752 JVMWrapper("JVM_CountStackFrames");
2754 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2755 oop java_thread = JNIHandles::resolve_non_null(jthread);
2756 bool throw_illegal_thread_state = false;
2757 int count = 0;
2759 {
2760 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2761 // We need to re-resolve the java_thread, since a GC might have happened during the
2762 // acquire of the lock
2763 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2765 if (thr == NULL) {
2766 // do nothing
2767 } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) {
2768 // Check whether this java thread has been suspended already. If not, throws
2769 // IllegalThreadStateException. We defer to throw that exception until
2770 // Threads_lock is released since loading exception class has to leave VM.
2771 // The correct way to test a thread is actually suspended is
2772 // wait_for_ext_suspend_completion(), but we can't call that while holding
2773 // the Threads_lock. The above tests are sufficient for our purposes
2774 // provided the walkability of the stack is stable - which it isn't
2775 // 100% but close enough for most practical purposes.
2776 throw_illegal_thread_state = true;
2777 } else {
2778 // Count all java activation, i.e., number of vframes
2779 for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) {
2780 // Native frames are not counted
2781 if (!vfst.method()->is_native()) count++;
2782 }
2783 }
2784 }
2786 if (throw_illegal_thread_state) {
2787 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
2788 "this thread is not suspended");
2789 }
2790 return count;
2791 JVM_END
2793 // Consider: A better way to implement JVM_Interrupt() is to acquire
2794 // Threads_lock to resolve the jthread into a Thread pointer, fetch
2795 // Thread->platformevent, Thread->native_thr, Thread->parker, etc.,
2796 // drop Threads_lock, and the perform the unpark() and thr_kill() operations
2797 // outside the critical section. Threads_lock is hot so we want to minimize
2798 // the hold-time. A cleaner interface would be to decompose interrupt into
2799 // two steps. The 1st phase, performed under Threads_lock, would return
2800 // a closure that'd be invoked after Threads_lock was dropped.
2801 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and
2802 // admit spurious wakeups.
2804 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
2805 JVMWrapper("JVM_Interrupt");
2807 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2808 oop java_thread = JNIHandles::resolve_non_null(jthread);
2809 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2810 // We need to re-resolve the java_thread, since a GC might have happened during the
2811 // acquire of the lock
2812 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2813 if (thr != NULL) {
2814 Thread::interrupt(thr);
2815 }
2816 JVM_END
2819 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
2820 JVMWrapper("JVM_IsInterrupted");
2822 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2823 oop java_thread = JNIHandles::resolve_non_null(jthread);
2824 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2825 // We need to re-resolve the java_thread, since a GC might have happened during the
2826 // acquire of the lock
2827 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2828 if (thr == NULL) {
2829 return JNI_FALSE;
2830 } else {
2831 return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0);
2832 }
2833 JVM_END
2836 // Return true iff the current thread has locked the object passed in
2838 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
2839 JVMWrapper("JVM_HoldsLock");
2840 assert(THREAD->is_Java_thread(), "sanity check");
2841 if (obj == NULL) {
2842 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
2843 }
2844 Handle h_obj(THREAD, JNIHandles::resolve(obj));
2845 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
2846 JVM_END
2849 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
2850 JVMWrapper("JVM_DumpAllStacks");
2851 VM_PrintThreads op;
2852 VMThread::execute(&op);
2853 if (JvmtiExport::should_post_data_dump()) {
2854 JvmtiExport::post_data_dump();
2855 }
2856 JVM_END
2859 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
2861 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
2862 assert(jthread->is_Java_thread(), "must be a Java thread");
2863 if (jthread->privileged_stack_top() == NULL) return false;
2864 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
2865 oop loader = jthread->privileged_stack_top()->class_loader();
2866 if (loader == NULL) return true;
2867 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
2868 if (trusted) return true;
2869 }
2870 return false;
2871 }
2873 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
2874 JVMWrapper("JVM_CurrentLoadedClass");
2875 ResourceMark rm(THREAD);
2877 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2878 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
2879 bool trusted = is_trusted_frame(thread, &vfst);
2880 if (trusted) return NULL;
2882 methodOop m = vfst.method();
2883 if (!m->is_native()) {
2884 klassOop holder = m->method_holder();
2885 oop loader = instanceKlass::cast(holder)->class_loader();
2886 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
2887 return (jclass) JNIHandles::make_local(env, Klass::cast(holder)->java_mirror());
2888 }
2889 }
2890 }
2891 return NULL;
2892 JVM_END
2895 JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env))
2896 JVMWrapper("JVM_CurrentClassLoader");
2897 ResourceMark rm(THREAD);
2899 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2901 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
2902 bool trusted = is_trusted_frame(thread, &vfst);
2903 if (trusted) return NULL;
2905 methodOop m = vfst.method();
2906 if (!m->is_native()) {
2907 klassOop holder = m->method_holder();
2908 assert(holder->is_klass(), "just checking");
2909 oop loader = instanceKlass::cast(holder)->class_loader();
2910 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
2911 return JNIHandles::make_local(env, loader);
2912 }
2913 }
2914 }
2915 return NULL;
2916 JVM_END
2919 // Utility object for collecting method holders walking down the stack
2920 class KlassLink: public ResourceObj {
2921 public:
2922 KlassHandle klass;
2923 KlassLink* next;
2925 KlassLink(KlassHandle k) { klass = k; next = NULL; }
2926 };
2929 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
2930 JVMWrapper("JVM_GetClassContext");
2931 ResourceMark rm(THREAD);
2932 JvmtiVMObjectAllocEventCollector oam;
2933 // Collect linked list of (handles to) method holders
2934 KlassLink* first = NULL;
2935 KlassLink* last = NULL;
2936 int depth = 0;
2938 for(vframeStream vfst(thread); !vfst.at_end(); vfst.security_get_caller_frame(1)) {
2939 // Native frames are not returned
2940 if (!vfst.method()->is_native()) {
2941 klassOop holder = vfst.method()->method_holder();
2942 assert(holder->is_klass(), "just checking");
2943 depth++;
2944 KlassLink* l = new KlassLink(KlassHandle(thread, holder));
2945 if (first == NULL) {
2946 first = last = l;
2947 } else {
2948 last->next = l;
2949 last = l;
2950 }
2951 }
2952 }
2954 // Create result array of type [Ljava/lang/Class;
2955 objArrayOop result = oopFactory::new_objArray(SystemDictionary::class_klass(), depth, CHECK_NULL);
2956 // Fill in mirrors corresponding to method holders
2957 int index = 0;
2958 while (first != NULL) {
2959 result->obj_at_put(index++, Klass::cast(first->klass())->java_mirror());
2960 first = first->next;
2961 }
2962 assert(index == depth, "just checking");
2964 return (jobjectArray) JNIHandles::make_local(env, result);
2965 JVM_END
2968 JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name))
2969 JVMWrapper("JVM_ClassDepth");
2970 ResourceMark rm(THREAD);
2971 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
2972 Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0);
2974 const char* str = java_lang_String::as_utf8_string(class_name_str());
2975 symbolHandle class_name_sym =
2976 symbolHandle(THREAD, SymbolTable::probe(str, (int)strlen(str)));
2977 if (class_name_sym.is_null()) {
2978 return -1;
2979 }
2981 int depth = 0;
2983 for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2984 if (!vfst.method()->is_native()) {
2985 klassOop holder = vfst.method()->method_holder();
2986 assert(holder->is_klass(), "just checking");
2987 if (instanceKlass::cast(holder)->name() == class_name_sym()) {
2988 return depth;
2989 }
2990 depth++;
2991 }
2992 }
2993 return -1;
2994 JVM_END
2997 JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env))
2998 JVMWrapper("JVM_ClassLoaderDepth");
2999 ResourceMark rm(THREAD);
3000 int depth = 0;
3001 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3002 // if a method in a class in a trusted loader is in a doPrivileged, return -1
3003 bool trusted = is_trusted_frame(thread, &vfst);
3004 if (trusted) return -1;
3006 methodOop m = vfst.method();
3007 if (!m->is_native()) {
3008 klassOop holder = m->method_holder();
3009 assert(holder->is_klass(), "just checking");
3010 oop loader = instanceKlass::cast(holder)->class_loader();
3011 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
3012 return depth;
3013 }
3014 depth++;
3015 }
3016 }
3017 return -1;
3018 JVM_END
3021 // java.lang.Package ////////////////////////////////////////////////////////////////
3024 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
3025 JVMWrapper("JVM_GetSystemPackage");
3026 ResourceMark rm(THREAD);
3027 JvmtiVMObjectAllocEventCollector oam;
3028 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
3029 oop result = ClassLoader::get_system_package(str, CHECK_NULL);
3030 return (jstring) JNIHandles::make_local(result);
3031 JVM_END
3034 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
3035 JVMWrapper("JVM_GetSystemPackages");
3036 JvmtiVMObjectAllocEventCollector oam;
3037 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
3038 return (jobjectArray) JNIHandles::make_local(result);
3039 JVM_END
3042 // ObjectInputStream ///////////////////////////////////////////////////////////////
3044 bool force_verify_field_access(klassOop current_class, klassOop field_class, AccessFlags access, bool classloader_only) {
3045 if (current_class == NULL) {
3046 return true;
3047 }
3048 if ((current_class == field_class) || access.is_public()) {
3049 return true;
3050 }
3052 if (access.is_protected()) {
3053 // See if current_class is a subclass of field_class
3054 if (Klass::cast(current_class)->is_subclass_of(field_class)) {
3055 return true;
3056 }
3057 }
3059 return (!access.is_private() && instanceKlass::cast(current_class)->is_same_class_package(field_class));
3060 }
3063 // JVM_AllocateNewObject and JVM_AllocateNewArray are unused as of 1.4
3064 JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass))
3065 JVMWrapper("JVM_AllocateNewObject");
3066 JvmtiVMObjectAllocEventCollector oam;
3067 // Receiver is not used
3068 oop curr_mirror = JNIHandles::resolve_non_null(currClass);
3069 oop init_mirror = JNIHandles::resolve_non_null(initClass);
3071 // Cannot instantiate primitive types
3072 if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) {
3073 ResourceMark rm(THREAD);
3074 THROW_0(vmSymbols::java_lang_InvalidClassException());
3075 }
3077 // Arrays not allowed here, must use JVM_AllocateNewArray
3078 if (Klass::cast(java_lang_Class::as_klassOop(curr_mirror))->oop_is_javaArray() ||
3079 Klass::cast(java_lang_Class::as_klassOop(init_mirror))->oop_is_javaArray()) {
3080 ResourceMark rm(THREAD);
3081 THROW_0(vmSymbols::java_lang_InvalidClassException());
3082 }
3084 instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_klassOop(curr_mirror));
3085 instanceKlassHandle init_klass (THREAD, java_lang_Class::as_klassOop(init_mirror));
3087 assert(curr_klass->is_subclass_of(init_klass()), "just checking");
3089 // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly.
3090 curr_klass->check_valid_for_instantiation(false, CHECK_NULL);
3092 // Make sure klass is initialized, since we are about to instantiate one of them.
3093 curr_klass->initialize(CHECK_NULL);
3095 methodHandle m (THREAD,
3096 init_klass->find_method(vmSymbols::object_initializer_name(),
3097 vmSymbols::void_method_signature()));
3098 if (m.is_null()) {
3099 ResourceMark rm(THREAD);
3100 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
3101 methodOopDesc::name_and_sig_as_C_string(Klass::cast(init_klass()),
3102 vmSymbols::object_initializer_name(),
3103 vmSymbols::void_method_signature()));
3104 }
3106 if (curr_klass == init_klass && !m->is_public()) {
3107 // Calling the constructor for class 'curr_klass'.
3108 // Only allow calls to a public no-arg constructor.
3109 // This path corresponds to creating an Externalizable object.
3110 THROW_0(vmSymbols::java_lang_IllegalAccessException());
3111 }
3113 if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) {
3114 // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb'
3115 THROW_0(vmSymbols::java_lang_IllegalAccessException());
3116 }
3118 Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL);
3119 // Call constructor m. This might call a constructor higher up in the hierachy
3120 JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL);
3122 return JNIHandles::make_local(obj());
3123 JVM_END
3126 JVM_ENTRY(jobject, JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length))
3127 JVMWrapper("JVM_AllocateNewArray");
3128 JvmtiVMObjectAllocEventCollector oam;
3129 oop mirror = JNIHandles::resolve_non_null(currClass);
3131 if (java_lang_Class::is_primitive(mirror)) {
3132 THROW_0(vmSymbols::java_lang_InvalidClassException());
3133 }
3134 klassOop k = java_lang_Class::as_klassOop(mirror);
3135 oop result;
3137 if (k->klass_part()->oop_is_typeArray()) {
3138 // typeArray
3139 result = typeArrayKlass::cast(k)->allocate(length, CHECK_NULL);
3140 } else if (k->klass_part()->oop_is_objArray()) {
3141 // objArray
3142 objArrayKlassHandle oak(THREAD, k);
3143 oak->initialize(CHECK_NULL); // make sure class is initialized (matches Classic VM behavior)
3144 result = oak->allocate(length, CHECK_NULL);
3145 } else {
3146 THROW_0(vmSymbols::java_lang_InvalidClassException());
3147 }
3148 return JNIHandles::make_local(env, result);
3149 JVM_END
3152 // Return the first non-null class loader up the execution stack, or null
3153 // if only code from the null class loader is on the stack.
3155 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
3156 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3157 // UseNewReflection
3158 vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection
3159 klassOop holder = vfst.method()->method_holder();
3160 oop loader = instanceKlass::cast(holder)->class_loader();
3161 if (loader != NULL) {
3162 return JNIHandles::make_local(env, loader);
3163 }
3164 }
3165 return NULL;
3166 JVM_END
3169 // Load a class relative to the most recent class on the stack with a non-null
3170 // classloader.
3171 // This function has been deprecated and should not be considered part of the
3172 // specified JVM interface.
3174 JVM_ENTRY(jclass, JVM_LoadClass0(JNIEnv *env, jobject receiver,
3175 jclass currClass, jstring currClassName))
3176 JVMWrapper("JVM_LoadClass0");
3177 // Receiver is not used
3178 ResourceMark rm(THREAD);
3180 // Class name argument is not guaranteed to be in internal format
3181 Handle classname (THREAD, JNIHandles::resolve_non_null(currClassName));
3182 Handle string = java_lang_String::internalize_classname(classname, CHECK_NULL);
3184 const char* str = java_lang_String::as_utf8_string(string());
3186 if (str == NULL || (int)strlen(str) > symbolOopDesc::max_length()) {
3187 // It's impossible to create this class; the name cannot fit
3188 // into the constant pool.
3189 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), str);
3190 }
3192 symbolHandle name = oopFactory::new_symbol_handle(str, CHECK_NULL);
3193 Handle curr_klass (THREAD, JNIHandles::resolve(currClass));
3194 // Find the most recent class on the stack with a non-null classloader
3195 oop loader = NULL;
3196 oop protection_domain = NULL;
3197 if (curr_klass.is_null()) {
3198 for (vframeStream vfst(thread);
3199 !vfst.at_end() && loader == NULL;
3200 vfst.next()) {
3201 if (!vfst.method()->is_native()) {
3202 klassOop holder = vfst.method()->method_holder();
3203 loader = instanceKlass::cast(holder)->class_loader();
3204 protection_domain = instanceKlass::cast(holder)->protection_domain();
3205 }
3206 }
3207 } else {
3208 klassOop curr_klass_oop = java_lang_Class::as_klassOop(curr_klass());
3209 loader = instanceKlass::cast(curr_klass_oop)->class_loader();
3210 protection_domain = instanceKlass::cast(curr_klass_oop)->protection_domain();
3211 }
3212 Handle h_loader(THREAD, loader);
3213 Handle h_prot (THREAD, protection_domain);
3214 return find_class_from_class_loader(env, name, true, h_loader, h_prot,
3215 false, thread);
3216 JVM_END
3219 // Array ///////////////////////////////////////////////////////////////////////////////////////////
3222 // resolve array handle and check arguments
3223 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
3224 if (arr == NULL) {
3225 THROW_0(vmSymbols::java_lang_NullPointerException());
3226 }
3227 oop a = JNIHandles::resolve_non_null(arr);
3228 if (!a->is_javaArray() || (type_array_only && !a->is_typeArray())) {
3229 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
3230 }
3231 return arrayOop(a);
3232 }
3235 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
3236 JVMWrapper("JVM_GetArrayLength");
3237 arrayOop a = check_array(env, arr, false, CHECK_0);
3238 return a->length();
3239 JVM_END
3242 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
3243 JVMWrapper("JVM_Array_Get");
3244 JvmtiVMObjectAllocEventCollector oam;
3245 arrayOop a = check_array(env, arr, false, CHECK_NULL);
3246 jvalue value;
3247 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
3248 oop box = Reflection::box(&value, type, CHECK_NULL);
3249 return JNIHandles::make_local(env, box);
3250 JVM_END
3253 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
3254 JVMWrapper("JVM_GetPrimitiveArrayElement");
3255 jvalue value;
3256 value.i = 0; // to initialize value before getting used in CHECK
3257 arrayOop a = check_array(env, arr, true, CHECK_(value));
3258 assert(a->is_typeArray(), "just checking");
3259 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
3260 BasicType wide_type = (BasicType) wCode;
3261 if (type != wide_type) {
3262 Reflection::widen(&value, type, wide_type, CHECK_(value));
3263 }
3264 return value;
3265 JVM_END
3268 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
3269 JVMWrapper("JVM_SetArrayElement");
3270 arrayOop a = check_array(env, arr, false, CHECK);
3271 oop box = JNIHandles::resolve(val);
3272 jvalue value;
3273 value.i = 0; // to initialize value before getting used in CHECK
3274 BasicType value_type;
3275 if (a->is_objArray()) {
3276 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
3277 value_type = Reflection::unbox_for_regular_object(box, &value);
3278 } else {
3279 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
3280 }
3281 Reflection::array_set(&value, a, index, value_type, CHECK);
3282 JVM_END
3285 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
3286 JVMWrapper("JVM_SetPrimitiveArrayElement");
3287 arrayOop a = check_array(env, arr, true, CHECK);
3288 assert(a->is_typeArray(), "just checking");
3289 BasicType value_type = (BasicType) vCode;
3290 Reflection::array_set(&v, a, index, value_type, CHECK);
3291 JVM_END
3294 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
3295 JVMWrapper("JVM_NewArray");
3296 JvmtiVMObjectAllocEventCollector oam;
3297 oop element_mirror = JNIHandles::resolve(eltClass);
3298 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
3299 return JNIHandles::make_local(env, result);
3300 JVM_END
3303 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
3304 JVMWrapper("JVM_NewMultiArray");
3305 JvmtiVMObjectAllocEventCollector oam;
3306 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
3307 oop element_mirror = JNIHandles::resolve(eltClass);
3308 assert(dim_array->is_typeArray(), "just checking");
3309 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
3310 return JNIHandles::make_local(env, result);
3311 JVM_END
3314 // Networking library support ////////////////////////////////////////////////////////////////////
3316 JVM_LEAF(jint, JVM_InitializeSocketLibrary())
3317 JVMWrapper("JVM_InitializeSocketLibrary");
3318 return hpi::initialize_socket_library();
3319 JVM_END
3322 JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol))
3323 JVMWrapper("JVM_Socket");
3324 return hpi::socket(domain, type, protocol);
3325 JVM_END
3328 JVM_LEAF(jint, JVM_SocketClose(jint fd))
3329 JVMWrapper2("JVM_SocketClose (0x%x)", fd);
3330 //%note jvm_r6
3331 return hpi::socket_close(fd);
3332 JVM_END
3335 JVM_LEAF(jint, JVM_SocketShutdown(jint fd, jint howto))
3336 JVMWrapper2("JVM_SocketShutdown (0x%x)", fd);
3337 //%note jvm_r6
3338 return hpi::socket_shutdown(fd, howto);
3339 JVM_END
3342 JVM_LEAF(jint, JVM_Recv(jint fd, char *buf, jint nBytes, jint flags))
3343 JVMWrapper2("JVM_Recv (0x%x)", fd);
3344 //%note jvm_r6
3345 return hpi::recv(fd, buf, nBytes, flags);
3346 JVM_END
3349 JVM_LEAF(jint, JVM_Send(jint fd, char *buf, jint nBytes, jint flags))
3350 JVMWrapper2("JVM_Send (0x%x)", fd);
3351 //%note jvm_r6
3352 return hpi::send(fd, buf, nBytes, flags);
3353 JVM_END
3356 JVM_LEAF(jint, JVM_Timeout(int fd, long timeout))
3357 JVMWrapper2("JVM_Timeout (0x%x)", fd);
3358 //%note jvm_r6
3359 return hpi::timeout(fd, timeout);
3360 JVM_END
3363 JVM_LEAF(jint, JVM_Listen(jint fd, jint count))
3364 JVMWrapper2("JVM_Listen (0x%x)", fd);
3365 //%note jvm_r6
3366 return hpi::listen(fd, count);
3367 JVM_END
3370 JVM_LEAF(jint, JVM_Connect(jint fd, struct sockaddr *him, jint len))
3371 JVMWrapper2("JVM_Connect (0x%x)", fd);
3372 //%note jvm_r6
3373 return hpi::connect(fd, him, len);
3374 JVM_END
3377 JVM_LEAF(jint, JVM_Bind(jint fd, struct sockaddr *him, jint len))
3378 JVMWrapper2("JVM_Bind (0x%x)", fd);
3379 //%note jvm_r6
3380 return hpi::bind(fd, him, len);
3381 JVM_END
3384 JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len))
3385 JVMWrapper2("JVM_Accept (0x%x)", fd);
3386 //%note jvm_r6
3387 return hpi::accept(fd, him, (int *)len);
3388 JVM_END
3391 JVM_LEAF(jint, JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen))
3392 JVMWrapper2("JVM_RecvFrom (0x%x)", fd);
3393 //%note jvm_r6
3394 return hpi::recvfrom(fd, buf, nBytes, flags, from, fromlen);
3395 JVM_END
3398 JVM_LEAF(jint, JVM_GetSockName(jint fd, struct sockaddr *him, int *len))
3399 JVMWrapper2("JVM_GetSockName (0x%x)", fd);
3400 //%note jvm_r6
3401 return hpi::get_sock_name(fd, him, len);
3402 JVM_END
3405 JVM_LEAF(jint, JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen))
3406 JVMWrapper2("JVM_SendTo (0x%x)", fd);
3407 //%note jvm_r6
3408 return hpi::sendto(fd, buf, len, flags, to, tolen);
3409 JVM_END
3412 JVM_LEAF(jint, JVM_SocketAvailable(jint fd, jint *pbytes))
3413 JVMWrapper2("JVM_SocketAvailable (0x%x)", fd);
3414 //%note jvm_r6
3415 return hpi::socket_available(fd, pbytes);
3416 JVM_END
3419 JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen))
3420 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
3421 //%note jvm_r6
3422 return hpi::get_sock_opt(fd, level, optname, optval, optlen);
3423 JVM_END
3426 JVM_LEAF(jint, JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen))
3427 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
3428 //%note jvm_r6
3429 return hpi::set_sock_opt(fd, level, optname, optval, optlen);
3430 JVM_END
3432 JVM_LEAF(int, JVM_GetHostName(char* name, int namelen))
3433 JVMWrapper("JVM_GetHostName");
3434 return hpi::get_host_name(name, namelen);
3435 JVM_END
3437 #ifdef _WINDOWS
3439 JVM_LEAF(struct hostent*, JVM_GetHostByAddr(const char* name, int len, int type))
3440 JVMWrapper("JVM_GetHostByAddr");
3441 return hpi::get_host_by_addr(name, len, type);
3442 JVM_END
3445 JVM_LEAF(struct hostent*, JVM_GetHostByName(char* name))
3446 JVMWrapper("JVM_GetHostByName");
3447 return hpi::get_host_by_name(name);
3448 JVM_END
3451 JVM_LEAF(struct protoent*, JVM_GetProtoByName(char* name))
3452 JVMWrapper("JVM_GetProtoByName");
3453 return hpi::get_proto_by_name(name);
3454 JVM_END
3456 #endif
3458 // Library support ///////////////////////////////////////////////////////////////////////////
3460 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
3461 //%note jvm_ct
3462 JVMWrapper2("JVM_LoadLibrary (%s)", name);
3463 char ebuf[1024];
3464 void *load_result;
3465 {
3466 ThreadToNativeFromVM ttnfvm(thread);
3467 load_result = hpi::dll_load(name, ebuf, sizeof ebuf);
3468 }
3469 if (load_result == NULL) {
3470 char msg[1024];
3471 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
3472 // Since 'ebuf' may contain a string encoded using
3473 // platform encoding scheme, we need to pass
3474 // Exceptions::unsafe_to_utf8 to the new_exception method
3475 // as the last argument. See bug 6367357.
3476 Handle h_exception =
3477 Exceptions::new_exception(thread,
3478 vmSymbols::java_lang_UnsatisfiedLinkError(),
3479 msg, Exceptions::unsafe_to_utf8);
3481 THROW_HANDLE_0(h_exception);
3482 }
3483 return load_result;
3484 JVM_END
3487 JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
3488 JVMWrapper("JVM_UnloadLibrary");
3489 hpi::dll_unload(handle);
3490 JVM_END
3493 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3494 JVMWrapper2("JVM_FindLibraryEntry (%s)", name);
3495 return hpi::dll_lookup(handle, name);
3496 JVM_END
3498 // Floating point support ////////////////////////////////////////////////////////////////////
3500 JVM_LEAF(jboolean, JVM_IsNaN(jdouble a))
3501 JVMWrapper("JVM_IsNaN");
3502 return g_isnan(a);
3503 JVM_END
3507 // JNI version ///////////////////////////////////////////////////////////////////////////////
3509 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3510 JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version);
3511 return Threads::is_supported_jni_version_including_1_1(version);
3512 JVM_END
3515 // String support ///////////////////////////////////////////////////////////////////////////
3517 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3518 JVMWrapper("JVM_InternString");
3519 JvmtiVMObjectAllocEventCollector oam;
3520 if (str == NULL) return NULL;
3521 oop string = JNIHandles::resolve_non_null(str);
3522 oop result = StringTable::intern(string, CHECK_NULL);
3523 return (jstring) JNIHandles::make_local(env, result);
3524 JVM_END
3527 // Raw monitor support //////////////////////////////////////////////////////////////////////
3529 // The lock routine below calls lock_without_safepoint_check in order to get a raw lock
3530 // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because
3531 // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check
3532 // that only works with java threads.
3535 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
3536 VM_Exit::block_if_vm_exited();
3537 JVMWrapper("JVM_RawMonitorCreate");
3538 return new Mutex(Mutex::native, "JVM_RawMonitorCreate");
3539 }
3542 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) {
3543 VM_Exit::block_if_vm_exited();
3544 JVMWrapper("JVM_RawMonitorDestroy");
3545 delete ((Mutex*) mon);
3546 }
3549 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) {
3550 VM_Exit::block_if_vm_exited();
3551 JVMWrapper("JVM_RawMonitorEnter");
3552 ((Mutex*) mon)->jvm_raw_lock();
3553 return 0;
3554 }
3557 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
3558 VM_Exit::block_if_vm_exited();
3559 JVMWrapper("JVM_RawMonitorExit");
3560 ((Mutex*) mon)->jvm_raw_unlock();
3561 }
3564 // Support for Serialization
3566 typedef jfloat (JNICALL *IntBitsToFloatFn )(JNIEnv* env, jclass cb, jint value);
3567 typedef jdouble (JNICALL *LongBitsToDoubleFn)(JNIEnv* env, jclass cb, jlong value);
3568 typedef jint (JNICALL *FloatToIntBitsFn )(JNIEnv* env, jclass cb, jfloat value);
3569 typedef jlong (JNICALL *DoubleToLongBitsFn)(JNIEnv* env, jclass cb, jdouble value);
3571 static IntBitsToFloatFn int_bits_to_float_fn = NULL;
3572 static LongBitsToDoubleFn long_bits_to_double_fn = NULL;
3573 static FloatToIntBitsFn float_to_int_bits_fn = NULL;
3574 static DoubleToLongBitsFn double_to_long_bits_fn = NULL;
3577 void initialize_converter_functions() {
3578 if (JDK_Version::is_gte_jdk14x_version()) {
3579 // These functions only exist for compatibility with 1.3.1 and earlier
3580 return;
3581 }
3583 // called from universe_post_init()
3584 assert(
3585 int_bits_to_float_fn == NULL &&
3586 long_bits_to_double_fn == NULL &&
3587 float_to_int_bits_fn == NULL &&
3588 double_to_long_bits_fn == NULL ,
3589 "initialization done twice"
3590 );
3591 // initialize
3592 int_bits_to_float_fn = CAST_TO_FN_PTR(IntBitsToFloatFn , NativeLookup::base_library_lookup("java/lang/Float" , "intBitsToFloat" , "(I)F"));
3593 long_bits_to_double_fn = CAST_TO_FN_PTR(LongBitsToDoubleFn, NativeLookup::base_library_lookup("java/lang/Double", "longBitsToDouble", "(J)D"));
3594 float_to_int_bits_fn = CAST_TO_FN_PTR(FloatToIntBitsFn , NativeLookup::base_library_lookup("java/lang/Float" , "floatToIntBits" , "(F)I"));
3595 double_to_long_bits_fn = CAST_TO_FN_PTR(DoubleToLongBitsFn, NativeLookup::base_library_lookup("java/lang/Double", "doubleToLongBits", "(D)J"));
3596 // verify
3597 assert(
3598 int_bits_to_float_fn != NULL &&
3599 long_bits_to_double_fn != NULL &&
3600 float_to_int_bits_fn != NULL &&
3601 double_to_long_bits_fn != NULL ,
3602 "initialization failed"
3603 );
3604 }
3607 // Serialization
3608 JVM_ENTRY(void, JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
3609 jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
3610 assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
3612 typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
3613 typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data));
3614 typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs));
3615 oop o = JNIHandles::resolve(obj);
3617 if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) {
3618 THROW(vmSymbols::java_lang_NullPointerException());
3619 }
3621 jsize nfids = fids->length();
3622 if (nfids == 0) return;
3624 if (tcodes->length() < nfids) {
3625 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
3626 }
3628 jsize off = 0;
3629 /* loop through fields, setting values */
3630 for (jsize i = 0; i < nfids; i++) {
3631 jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
3632 int field_offset;
3633 if (fid != NULL) {
3634 // NULL is a legal value for fid, but retrieving the field offset
3635 // trigger assertion in that case
3636 field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
3637 }
3639 switch (tcodes->char_at(i)) {
3640 case 'Z':
3641 if (fid != NULL) {
3642 jboolean val = (dbuf->byte_at(off) != 0) ? JNI_TRUE : JNI_FALSE;
3643 o->bool_field_put(field_offset, val);
3644 }
3645 off++;
3646 break;
3648 case 'B':
3649 if (fid != NULL) {
3650 o->byte_field_put(field_offset, dbuf->byte_at(off));
3651 }
3652 off++;
3653 break;
3655 case 'C':
3656 if (fid != NULL) {
3657 jchar val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
3658 + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
3659 o->char_field_put(field_offset, val);
3660 }
3661 off += 2;
3662 break;
3664 case 'S':
3665 if (fid != NULL) {
3666 jshort val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
3667 + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
3668 o->short_field_put(field_offset, val);
3669 }
3670 off += 2;
3671 break;
3673 case 'I':
3674 if (fid != NULL) {
3675 jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
3676 + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
3677 + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
3678 + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
3679 o->int_field_put(field_offset, ival);
3680 }
3681 off += 4;
3682 break;
3684 case 'F':
3685 if (fid != NULL) {
3686 jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
3687 + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
3688 + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
3689 + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
3690 jfloat fval = (*int_bits_to_float_fn)(env, NULL, ival);
3691 o->float_field_put(field_offset, fval);
3692 }
3693 off += 4;
3694 break;
3696 case 'J':
3697 if (fid != NULL) {
3698 jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
3699 + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
3700 + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
3701 + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
3702 + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
3703 + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
3704 + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
3705 + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
3706 o->long_field_put(field_offset, lval);
3707 }
3708 off += 8;
3709 break;
3711 case 'D':
3712 if (fid != NULL) {
3713 jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
3714 + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
3715 + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
3716 + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
3717 + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
3718 + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
3719 + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
3720 + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
3721 jdouble dval = (*long_bits_to_double_fn)(env, NULL, lval);
3722 o->double_field_put(field_offset, dval);
3723 }
3724 off += 8;
3725 break;
3727 default:
3728 // Illegal typecode
3729 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
3730 }
3731 }
3732 JVM_END
3735 JVM_ENTRY(void, JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
3736 jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
3737 assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
3739 typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
3740 typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data));
3741 typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs));
3742 oop o = JNIHandles::resolve(obj);
3744 if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) {
3745 THROW(vmSymbols::java_lang_NullPointerException());
3746 }
3748 jsize nfids = fids->length();
3749 if (nfids == 0) return;
3751 if (tcodes->length() < nfids) {
3752 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
3753 }
3755 /* loop through fields, fetching values */
3756 jsize off = 0;
3757 for (jsize i = 0; i < nfids; i++) {
3758 jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
3759 if (fid == NULL) {
3760 THROW(vmSymbols::java_lang_NullPointerException());
3761 }
3762 int field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
3764 switch (tcodes->char_at(i)) {
3765 case 'Z':
3766 {
3767 jboolean val = o->bool_field(field_offset);
3768 dbuf->byte_at_put(off++, (val != 0) ? 1 : 0);
3769 }
3770 break;
3772 case 'B':
3773 dbuf->byte_at_put(off++, o->byte_field(field_offset));
3774 break;
3776 case 'C':
3777 {
3778 jchar val = o->char_field(field_offset);
3779 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3780 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3781 }
3782 break;
3784 case 'S':
3785 {
3786 jshort val = o->short_field(field_offset);
3787 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3788 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3789 }
3790 break;
3792 case 'I':
3793 {
3794 jint val = o->int_field(field_offset);
3795 dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
3796 dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
3797 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3798 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3799 }
3800 break;
3802 case 'F':
3803 {
3804 jfloat fval = o->float_field(field_offset);
3805 jint ival = (*float_to_int_bits_fn)(env, NULL, fval);
3806 dbuf->byte_at_put(off++, (ival >> 24) & 0xFF);
3807 dbuf->byte_at_put(off++, (ival >> 16) & 0xFF);
3808 dbuf->byte_at_put(off++, (ival >> 8) & 0xFF);
3809 dbuf->byte_at_put(off++, (ival >> 0) & 0xFF);
3810 }
3811 break;
3813 case 'J':
3814 {
3815 jlong val = o->long_field(field_offset);
3816 dbuf->byte_at_put(off++, (val >> 56) & 0xFF);
3817 dbuf->byte_at_put(off++, (val >> 48) & 0xFF);
3818 dbuf->byte_at_put(off++, (val >> 40) & 0xFF);
3819 dbuf->byte_at_put(off++, (val >> 32) & 0xFF);
3820 dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
3821 dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
3822 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3823 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3824 }
3825 break;
3827 case 'D':
3828 {
3829 jdouble dval = o->double_field(field_offset);
3830 jlong lval = (*double_to_long_bits_fn)(env, NULL, dval);
3831 dbuf->byte_at_put(off++, (lval >> 56) & 0xFF);
3832 dbuf->byte_at_put(off++, (lval >> 48) & 0xFF);
3833 dbuf->byte_at_put(off++, (lval >> 40) & 0xFF);
3834 dbuf->byte_at_put(off++, (lval >> 32) & 0xFF);
3835 dbuf->byte_at_put(off++, (lval >> 24) & 0xFF);
3836 dbuf->byte_at_put(off++, (lval >> 16) & 0xFF);
3837 dbuf->byte_at_put(off++, (lval >> 8) & 0xFF);
3838 dbuf->byte_at_put(off++, (lval >> 0) & 0xFF);
3839 }
3840 break;
3842 default:
3843 // Illegal typecode
3844 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
3845 }
3846 }
3847 JVM_END
3850 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
3852 jclass find_class_from_class_loader(JNIEnv* env, symbolHandle name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS) {
3853 // Security Note:
3854 // The Java level wrapper will perform the necessary security check allowing
3855 // us to pass the NULL as the initiating class loader.
3856 klassOop klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3857 KlassHandle klass_handle(THREAD, klass);
3858 // Check if we should initialize the class
3859 if (init && klass_handle->oop_is_instance()) {
3860 klass_handle->initialize(CHECK_NULL);
3861 }
3862 return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
3863 }
3866 // Internal SQE debugging support ///////////////////////////////////////////////////////////
3868 #ifndef PRODUCT
3870 extern "C" {
3871 JNIEXPORT jboolean JNICALL JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get);
3872 JNIEXPORT jboolean JNICALL JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get);
3873 JNIEXPORT void JNICALL JVM_VMBreakPoint(JNIEnv *env, jobject obj);
3874 }
3876 JVM_LEAF(jboolean, JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get))
3877 JVMWrapper("JVM_AccessBoolVMFlag");
3878 return is_get ? CommandLineFlags::boolAt((char*) name, (bool*) value) : CommandLineFlags::boolAtPut((char*) name, (bool*) value, INTERNAL);
3879 JVM_END
3881 JVM_LEAF(jboolean, JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get))
3882 JVMWrapper("JVM_AccessVMIntFlag");
3883 intx v;
3884 jboolean result = is_get ? CommandLineFlags::intxAt((char*) name, &v) : CommandLineFlags::intxAtPut((char*) name, &v, INTERNAL);
3885 *value = (jint)v;
3886 return result;
3887 JVM_END
3890 JVM_ENTRY(void, JVM_VMBreakPoint(JNIEnv *env, jobject obj))
3891 JVMWrapper("JVM_VMBreakPoint");
3892 oop the_obj = JNIHandles::resolve(obj);
3893 BREAKPOINT;
3894 JVM_END
3897 #endif
3900 //---------------------------------------------------------------------------
3901 //
3902 // Support for old native code-based reflection (pre-JDK 1.4)
3903 // Disabled by default in the product build.
3904 //
3905 // See reflection.hpp for information on SUPPORT_OLD_REFLECTION
3906 //
3907 //---------------------------------------------------------------------------
3909 #ifdef SUPPORT_OLD_REFLECTION
3911 JVM_ENTRY(jobjectArray, JVM_GetClassFields(JNIEnv *env, jclass cls, jint which))
3912 JVMWrapper("JVM_GetClassFields");
3913 JvmtiVMObjectAllocEventCollector oam;
3914 oop mirror = JNIHandles::resolve_non_null(cls);
3915 objArrayOop result = Reflection::reflect_fields(mirror, which, CHECK_NULL);
3916 return (jobjectArray) JNIHandles::make_local(env, result);
3917 JVM_END
3920 JVM_ENTRY(jobjectArray, JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which))
3921 JVMWrapper("JVM_GetClassMethods");
3922 JvmtiVMObjectAllocEventCollector oam;
3923 oop mirror = JNIHandles::resolve_non_null(cls);
3924 objArrayOop result = Reflection::reflect_methods(mirror, which, CHECK_NULL);
3925 //%note jvm_r4
3926 return (jobjectArray) JNIHandles::make_local(env, result);
3927 JVM_END
3930 JVM_ENTRY(jobjectArray, JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which))
3931 JVMWrapper("JVM_GetClassConstructors");
3932 JvmtiVMObjectAllocEventCollector oam;
3933 oop mirror = JNIHandles::resolve_non_null(cls);
3934 objArrayOop result = Reflection::reflect_constructors(mirror, which, CHECK_NULL);
3935 //%note jvm_r4
3936 return (jobjectArray) JNIHandles::make_local(env, result);
3937 JVM_END
3940 JVM_ENTRY(jobject, JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which))
3941 JVMWrapper("JVM_GetClassField");
3942 JvmtiVMObjectAllocEventCollector oam;
3943 if (name == NULL) return NULL;
3944 Handle str (THREAD, JNIHandles::resolve_non_null(name));
3946 const char* cstr = java_lang_String::as_utf8_string(str());
3947 symbolHandle field_name =
3948 symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
3949 if (field_name.is_null()) {
3950 THROW_0(vmSymbols::java_lang_NoSuchFieldException());
3951 }
3953 oop mirror = JNIHandles::resolve_non_null(cls);
3954 oop result = Reflection::reflect_field(mirror, field_name(), which, CHECK_NULL);
3955 if (result == NULL) {
3956 THROW_0(vmSymbols::java_lang_NoSuchFieldException());
3957 }
3958 return JNIHandles::make_local(env, result);
3959 JVM_END
3962 JVM_ENTRY(jobject, JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which))
3963 JVMWrapper("JVM_GetClassMethod");
3964 JvmtiVMObjectAllocEventCollector oam;
3965 if (name == NULL) {
3966 THROW_0(vmSymbols::java_lang_NullPointerException());
3967 }
3968 Handle str (THREAD, JNIHandles::resolve_non_null(name));
3970 const char* cstr = java_lang_String::as_utf8_string(str());
3971 symbolHandle method_name =
3972 symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
3973 if (method_name.is_null()) {
3974 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3975 }
3977 oop mirror = JNIHandles::resolve_non_null(cls);
3978 objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
3979 oop result = Reflection::reflect_method(mirror, method_name, tarray,
3980 which, CHECK_NULL);
3981 if (result == NULL) {
3982 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3983 }
3984 return JNIHandles::make_local(env, result);
3985 JVM_END
3988 JVM_ENTRY(jobject, JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which))
3989 JVMWrapper("JVM_GetClassConstructor");
3990 JvmtiVMObjectAllocEventCollector oam;
3991 oop mirror = JNIHandles::resolve_non_null(cls);
3992 objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
3993 oop result = Reflection::reflect_constructor(mirror, tarray, which, CHECK_NULL);
3994 if (result == NULL) {
3995 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3996 }
3997 return (jobject) JNIHandles::make_local(env, result);
3998 JVM_END
4001 // Instantiation ///////////////////////////////////////////////////////////////////////////////
4003 JVM_ENTRY(jobject, JVM_NewInstance(JNIEnv *env, jclass cls))
4004 JVMWrapper("JVM_NewInstance");
4005 Handle mirror(THREAD, JNIHandles::resolve_non_null(cls));
4007 methodOop resolved_constructor = java_lang_Class::resolved_constructor(mirror());
4008 if (resolved_constructor == NULL) {
4009 klassOop k = java_lang_Class::as_klassOop(mirror());
4010 // The java.lang.Class object caches a resolved constructor if all the checks
4011 // below were done successfully and a constructor was found.
4013 // Do class based checks
4014 if (java_lang_Class::is_primitive(mirror())) {
4015 const char* msg = "";
4016 if (mirror == Universe::bool_mirror()) msg = "java/lang/Boolean";
4017 else if (mirror == Universe::char_mirror()) msg = "java/lang/Character";
4018 else if (mirror == Universe::float_mirror()) msg = "java/lang/Float";
4019 else if (mirror == Universe::double_mirror()) msg = "java/lang/Double";
4020 else if (mirror == Universe::byte_mirror()) msg = "java/lang/Byte";
4021 else if (mirror == Universe::short_mirror()) msg = "java/lang/Short";
4022 else if (mirror == Universe::int_mirror()) msg = "java/lang/Integer";
4023 else if (mirror == Universe::long_mirror()) msg = "java/lang/Long";
4024 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), msg);
4025 }
4027 // Check whether we are allowed to instantiate this class
4028 Klass::cast(k)->check_valid_for_instantiation(false, CHECK_NULL); // Array classes get caught here
4029 instanceKlassHandle klass(THREAD, k);
4030 // Make sure class is initialized (also so all methods are rewritten)
4031 klass->initialize(CHECK_NULL);
4033 // Lookup default constructor
4034 resolved_constructor = klass->find_method(vmSymbols::object_initializer_name(), vmSymbols::void_method_signature());
4035 if (resolved_constructor == NULL) {
4036 ResourceMark rm(THREAD);
4037 THROW_MSG_0(vmSymbols::java_lang_InstantiationException(), klass->external_name());
4038 }
4040 // Cache result in java.lang.Class object. Does not have to be MT safe.
4041 java_lang_Class::set_resolved_constructor(mirror(), resolved_constructor);
4042 }
4044 assert(resolved_constructor != NULL, "sanity check");
4045 methodHandle constructor = methodHandle(THREAD, resolved_constructor);
4047 // We have an initialized instanceKlass with a default constructor
4048 instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
4049 assert(klass->is_initialized() || klass->is_being_initialized(), "sanity check");
4051 // Do security check
4052 klassOop caller_klass = NULL;
4053 if (UsePrivilegedStack) {
4054 caller_klass = thread->security_get_caller_class(2);
4056 if (!Reflection::verify_class_access(caller_klass, klass(), false) ||
4057 !Reflection::verify_field_access(caller_klass,
4058 klass(),
4059 klass(),
4060 constructor->access_flags(),
4061 false,
4062 true)) {
4063 ResourceMark rm(THREAD);
4064 THROW_MSG_0(vmSymbols::java_lang_IllegalAccessException(), klass->external_name());
4065 }
4066 }
4068 // Allocate object and call constructor
4069 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
4070 JavaCalls::call_default_constructor(thread, constructor, receiver, CHECK_NULL);
4072 jobject res = JNIHandles::make_local(env, receiver());
4073 if (JvmtiExport::should_post_vm_object_alloc()) {
4074 JvmtiExport::post_vm_object_alloc(JavaThread::current(), receiver());
4075 }
4076 return res;
4077 JVM_END
4080 // Field ////////////////////////////////////////////////////////////////////////////////////////////
4082 JVM_ENTRY(jobject, JVM_GetField(JNIEnv *env, jobject field, jobject obj))
4083 JVMWrapper("JVM_GetField");
4084 JvmtiVMObjectAllocEventCollector oam;
4085 Handle field_mirror(thread, JNIHandles::resolve(field));
4086 Handle receiver (thread, JNIHandles::resolve(obj));
4087 fieldDescriptor fd;
4088 Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_NULL);
4089 jvalue value;
4090 BasicType type = Reflection::field_get(&value, &fd, receiver);
4091 oop box = Reflection::box(&value, type, CHECK_NULL);
4092 return JNIHandles::make_local(env, box);
4093 JVM_END
4096 JVM_ENTRY(jvalue, JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode))
4097 JVMWrapper("JVM_GetPrimitiveField");
4098 Handle field_mirror(thread, JNIHandles::resolve(field));
4099 Handle receiver (thread, JNIHandles::resolve(obj));
4100 fieldDescriptor fd;
4101 jvalue value;
4102 value.j = 0;
4103 Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_(value));
4104 BasicType type = Reflection::field_get(&value, &fd, receiver);
4105 BasicType wide_type = (BasicType) wCode;
4106 if (type != wide_type) {
4107 Reflection::widen(&value, type, wide_type, CHECK_(value));
4108 }
4109 return value;
4110 JVM_END // should really be JVM_END, but that doesn't work for union types!
4113 JVM_ENTRY(void, JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val))
4114 JVMWrapper("JVM_SetField");
4115 Handle field_mirror(thread, JNIHandles::resolve(field));
4116 Handle receiver (thread, JNIHandles::resolve(obj));
4117 oop box = JNIHandles::resolve(val);
4118 fieldDescriptor fd;
4119 Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
4120 BasicType field_type = fd.field_type();
4121 jvalue value;
4122 BasicType value_type;
4123 if (field_type == T_OBJECT || field_type == T_ARRAY) {
4124 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
4125 value_type = Reflection::unbox_for_regular_object(box, &value);
4126 Reflection::field_set(&value, &fd, receiver, field_type, CHECK);
4127 } else {
4128 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
4129 Reflection::field_set(&value, &fd, receiver, value_type, CHECK);
4130 }
4131 JVM_END
4134 JVM_ENTRY(void, JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode))
4135 JVMWrapper("JVM_SetPrimitiveField");
4136 Handle field_mirror(thread, JNIHandles::resolve(field));
4137 Handle receiver (thread, JNIHandles::resolve(obj));
4138 fieldDescriptor fd;
4139 Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
4140 BasicType value_type = (BasicType) vCode;
4141 Reflection::field_set(&v, &fd, receiver, value_type, CHECK);
4142 JVM_END
4145 // Method ///////////////////////////////////////////////////////////////////////////////////////////
4147 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
4148 JVMWrapper("JVM_InvokeMethod");
4149 Handle method_handle;
4150 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
4151 method_handle = Handle(THREAD, JNIHandles::resolve(method));
4152 Handle receiver(THREAD, JNIHandles::resolve(obj));
4153 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
4154 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
4155 jobject res = JNIHandles::make_local(env, result);
4156 if (JvmtiExport::should_post_vm_object_alloc()) {
4157 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
4158 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
4159 if (java_lang_Class::is_primitive(ret_type)) {
4160 // Only for primitive type vm allocates memory for java object.
4161 // See box() method.
4162 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
4163 }
4164 }
4165 return res;
4166 } else {
4167 THROW_0(vmSymbols::java_lang_StackOverflowError());
4168 }
4169 JVM_END
4172 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
4173 JVMWrapper("JVM_NewInstanceFromConstructor");
4174 oop constructor_mirror = JNIHandles::resolve(c);
4175 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
4176 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
4177 jobject res = JNIHandles::make_local(env, result);
4178 if (JvmtiExport::should_post_vm_object_alloc()) {
4179 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
4180 }
4181 return res;
4182 JVM_END
4184 #endif /* SUPPORT_OLD_REFLECTION */
4186 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
4188 JVM_LEAF(jboolean, JVM_SupportsCX8())
4189 JVMWrapper("JVM_SupportsCX8");
4190 return VM_Version::supports_cx8();
4191 JVM_END
4194 JVM_ENTRY(jboolean, JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal))
4195 JVMWrapper("JVM_CX8Field");
4196 jlong res;
4197 oop o = JNIHandles::resolve(obj);
4198 intptr_t fldOffs = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
4199 volatile jlong* addr = (volatile jlong*)((address)o + fldOffs);
4201 assert(VM_Version::supports_cx8(), "cx8 not supported");
4202 res = Atomic::cmpxchg(newVal, addr, oldVal);
4204 return res == oldVal;
4205 JVM_END
4207 // DTrace ///////////////////////////////////////////////////////////////////
4209 JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env))
4210 JVMWrapper("JVM_DTraceGetVersion");
4211 return (jint)JVM_TRACING_DTRACE_VERSION;
4212 JVM_END
4214 JVM_ENTRY(jlong,JVM_DTraceActivate(
4215 JNIEnv* env, jint version, jstring module_name, jint providers_count,
4216 JVM_DTraceProvider* providers))
4217 JVMWrapper("JVM_DTraceActivate");
4218 return DTraceJSDT::activate(
4219 version, module_name, providers_count, providers, CHECK_0);
4220 JVM_END
4222 JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method))
4223 JVMWrapper("JVM_DTraceIsProbeEnabled");
4224 return DTraceJSDT::is_probe_enabled(method);
4225 JVM_END
4227 JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle))
4228 JVMWrapper("JVM_DTraceDispose");
4229 DTraceJSDT::dispose(handle);
4230 JVM_END
4232 JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env))
4233 JVMWrapper("JVM_DTraceIsSupported");
4234 return DTraceJSDT::is_supported();
4235 JVM_END
4237 // Returns an array of all live Thread objects (VM internal JavaThreads,
4238 // jvmti agent threads, and JNI attaching threads are skipped)
4239 // See CR 6404306 regarding JNI attaching threads
4240 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
4241 ResourceMark rm(THREAD);
4242 ThreadsListEnumerator tle(THREAD, false, false);
4243 JvmtiVMObjectAllocEventCollector oam;
4245 int num_threads = tle.num_threads();
4246 objArrayOop r = oopFactory::new_objArray(SystemDictionary::thread_klass(), num_threads, CHECK_NULL);
4247 objArrayHandle threads_ah(THREAD, r);
4249 for (int i = 0; i < num_threads; i++) {
4250 Handle h = tle.get_threadObj(i);
4251 threads_ah->obj_at_put(i, h());
4252 }
4254 return (jobjectArray) JNIHandles::make_local(env, threads_ah());
4255 JVM_END
4258 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
4259 // Return StackTraceElement[][], each element is the stack trace of a thread in
4260 // the corresponding entry in the given threads array
4261 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
4262 JVMWrapper("JVM_DumpThreads");
4263 JvmtiVMObjectAllocEventCollector oam;
4265 // Check if threads is null
4266 if (threads == NULL) {
4267 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
4268 }
4270 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
4271 objArrayHandle ah(THREAD, a);
4272 int num_threads = ah->length();
4273 // check if threads is non-empty array
4274 if (num_threads == 0) {
4275 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
4276 }
4278 // check if threads is not an array of objects of Thread class
4279 klassOop k = objArrayKlass::cast(ah->klass())->element_klass();
4280 if (k != SystemDictionary::thread_klass()) {
4281 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
4282 }
4284 ResourceMark rm(THREAD);
4286 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
4287 for (int i = 0; i < num_threads; i++) {
4288 oop thread_obj = ah->obj_at(i);
4289 instanceHandle h(THREAD, (instanceOop) thread_obj);
4290 thread_handle_array->append(h);
4291 }
4293 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
4294 return (jobjectArray)JNIHandles::make_local(env, stacktraces());
4296 JVM_END
4298 // JVM monitoring and management support
4299 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
4300 return Management::get_jmm_interface(version);
4301 JVM_END
4303 // com.sun.tools.attach.VirtualMachine agent properties support
4304 //
4305 // Initialize the agent properties with the properties maintained in the VM
4306 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
4307 JVMWrapper("JVM_InitAgentProperties");
4308 ResourceMark rm;
4310 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
4312 PUTPROP(props, "sun.java.command", Arguments::java_command());
4313 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
4314 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
4315 return properties;
4316 JVM_END
4318 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
4319 {
4320 JVMWrapper("JVM_GetEnclosingMethodInfo");
4321 JvmtiVMObjectAllocEventCollector oam;
4323 if (ofClass == NULL) {
4324 return NULL;
4325 }
4326 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
4327 // Special handling for primitive objects
4328 if (java_lang_Class::is_primitive(mirror())) {
4329 return NULL;
4330 }
4331 klassOop k = java_lang_Class::as_klassOop(mirror());
4332 if (!Klass::cast(k)->oop_is_instance()) {
4333 return NULL;
4334 }
4335 instanceKlassHandle ik_h(THREAD, k);
4336 int encl_method_class_idx = ik_h->enclosing_method_class_index();
4337 if (encl_method_class_idx == 0) {
4338 return NULL;
4339 }
4340 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::object_klass(), 3, CHECK_NULL);
4341 objArrayHandle dest(THREAD, dest_o);
4342 klassOop enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
4343 dest->obj_at_put(0, Klass::cast(enc_k)->java_mirror());
4344 int encl_method_method_idx = ik_h->enclosing_method_method_index();
4345 if (encl_method_method_idx != 0) {
4346 symbolOop sym_o = ik_h->constants()->symbol_at(
4347 extract_low_short_from_int(
4348 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
4349 symbolHandle sym(THREAD, sym_o);
4350 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
4351 dest->obj_at_put(1, str());
4352 sym_o = ik_h->constants()->symbol_at(
4353 extract_high_short_from_int(
4354 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
4355 sym = symbolHandle(THREAD, sym_o);
4356 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
4357 dest->obj_at_put(2, str());
4358 }
4359 return (jobjectArray) JNIHandles::make_local(dest());
4360 }
4361 JVM_END
4363 JVM_ENTRY(jintArray, JVM_GetThreadStateValues(JNIEnv* env,
4364 jint javaThreadState))
4365 {
4366 // If new thread states are added in future JDK and VM versions,
4367 // this should check if the JDK version is compatible with thread
4368 // states supported by the VM. Return NULL if not compatible.
4369 //
4370 // This function must map the VM java_lang_Thread::ThreadStatus
4371 // to the Java thread state that the JDK supports.
4372 //
4374 typeArrayHandle values_h;
4375 switch (javaThreadState) {
4376 case JAVA_THREAD_STATE_NEW : {
4377 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4378 values_h = typeArrayHandle(THREAD, r);
4379 values_h->int_at_put(0, java_lang_Thread::NEW);
4380 break;
4381 }
4382 case JAVA_THREAD_STATE_RUNNABLE : {
4383 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4384 values_h = typeArrayHandle(THREAD, r);
4385 values_h->int_at_put(0, java_lang_Thread::RUNNABLE);
4386 break;
4387 }
4388 case JAVA_THREAD_STATE_BLOCKED : {
4389 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4390 values_h = typeArrayHandle(THREAD, r);
4391 values_h->int_at_put(0, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
4392 break;
4393 }
4394 case JAVA_THREAD_STATE_WAITING : {
4395 typeArrayOop r = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL);
4396 values_h = typeArrayHandle(THREAD, r);
4397 values_h->int_at_put(0, java_lang_Thread::IN_OBJECT_WAIT);
4398 values_h->int_at_put(1, java_lang_Thread::PARKED);
4399 break;
4400 }
4401 case JAVA_THREAD_STATE_TIMED_WAITING : {
4402 typeArrayOop r = oopFactory::new_typeArray(T_INT, 3, CHECK_NULL);
4403 values_h = typeArrayHandle(THREAD, r);
4404 values_h->int_at_put(0, java_lang_Thread::SLEEPING);
4405 values_h->int_at_put(1, java_lang_Thread::IN_OBJECT_WAIT_TIMED);
4406 values_h->int_at_put(2, java_lang_Thread::PARKED_TIMED);
4407 break;
4408 }
4409 case JAVA_THREAD_STATE_TERMINATED : {
4410 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4411 values_h = typeArrayHandle(THREAD, r);
4412 values_h->int_at_put(0, java_lang_Thread::TERMINATED);
4413 break;
4414 }
4415 default:
4416 // Unknown state - probably incompatible JDK version
4417 return NULL;
4418 }
4420 return (jintArray) JNIHandles::make_local(env, values_h());
4421 }
4422 JVM_END
4425 JVM_ENTRY(jobjectArray, JVM_GetThreadStateNames(JNIEnv* env,
4426 jint javaThreadState,
4427 jintArray values))
4428 {
4429 // If new thread states are added in future JDK and VM versions,
4430 // this should check if the JDK version is compatible with thread
4431 // states supported by the VM. Return NULL if not compatible.
4432 //
4433 // This function must map the VM java_lang_Thread::ThreadStatus
4434 // to the Java thread state that the JDK supports.
4435 //
4437 ResourceMark rm;
4439 // Check if threads is null
4440 if (values == NULL) {
4441 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
4442 }
4444 typeArrayOop v = typeArrayOop(JNIHandles::resolve_non_null(values));
4445 typeArrayHandle values_h(THREAD, v);
4447 objArrayHandle names_h;
4448 switch (javaThreadState) {
4449 case JAVA_THREAD_STATE_NEW : {
4450 assert(values_h->length() == 1 &&
4451 values_h->int_at(0) == java_lang_Thread::NEW,
4452 "Invalid threadStatus value");
4454 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4455 1, /* only 1 substate */
4456 CHECK_NULL);
4457 names_h = objArrayHandle(THREAD, r);
4458 Handle name = java_lang_String::create_from_str("NEW", CHECK_NULL);
4459 names_h->obj_at_put(0, name());
4460 break;
4461 }
4462 case JAVA_THREAD_STATE_RUNNABLE : {
4463 assert(values_h->length() == 1 &&
4464 values_h->int_at(0) == java_lang_Thread::RUNNABLE,
4465 "Invalid threadStatus value");
4467 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4468 1, /* only 1 substate */
4469 CHECK_NULL);
4470 names_h = objArrayHandle(THREAD, r);
4471 Handle name = java_lang_String::create_from_str("RUNNABLE", CHECK_NULL);
4472 names_h->obj_at_put(0, name());
4473 break;
4474 }
4475 case JAVA_THREAD_STATE_BLOCKED : {
4476 assert(values_h->length() == 1 &&
4477 values_h->int_at(0) == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER,
4478 "Invalid threadStatus value");
4480 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4481 1, /* only 1 substate */
4482 CHECK_NULL);
4483 names_h = objArrayHandle(THREAD, r);
4484 Handle name = java_lang_String::create_from_str("BLOCKED", CHECK_NULL);
4485 names_h->obj_at_put(0, name());
4486 break;
4487 }
4488 case JAVA_THREAD_STATE_WAITING : {
4489 assert(values_h->length() == 2 &&
4490 values_h->int_at(0) == java_lang_Thread::IN_OBJECT_WAIT &&
4491 values_h->int_at(1) == java_lang_Thread::PARKED,
4492 "Invalid threadStatus value");
4493 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4494 2, /* number of substates */
4495 CHECK_NULL);
4496 names_h = objArrayHandle(THREAD, r);
4497 Handle name0 = java_lang_String::create_from_str("WAITING.OBJECT_WAIT",
4498 CHECK_NULL);
4499 Handle name1 = java_lang_String::create_from_str("WAITING.PARKED",
4500 CHECK_NULL);
4501 names_h->obj_at_put(0, name0());
4502 names_h->obj_at_put(1, name1());
4503 break;
4504 }
4505 case JAVA_THREAD_STATE_TIMED_WAITING : {
4506 assert(values_h->length() == 3 &&
4507 values_h->int_at(0) == java_lang_Thread::SLEEPING &&
4508 values_h->int_at(1) == java_lang_Thread::IN_OBJECT_WAIT_TIMED &&
4509 values_h->int_at(2) == java_lang_Thread::PARKED_TIMED,
4510 "Invalid threadStatus value");
4511 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4512 3, /* number of substates */
4513 CHECK_NULL);
4514 names_h = objArrayHandle(THREAD, r);
4515 Handle name0 = java_lang_String::create_from_str("TIMED_WAITING.SLEEPING",
4516 CHECK_NULL);
4517 Handle name1 = java_lang_String::create_from_str("TIMED_WAITING.OBJECT_WAIT",
4518 CHECK_NULL);
4519 Handle name2 = java_lang_String::create_from_str("TIMED_WAITING.PARKED",
4520 CHECK_NULL);
4521 names_h->obj_at_put(0, name0());
4522 names_h->obj_at_put(1, name1());
4523 names_h->obj_at_put(2, name2());
4524 break;
4525 }
4526 case JAVA_THREAD_STATE_TERMINATED : {
4527 assert(values_h->length() == 1 &&
4528 values_h->int_at(0) == java_lang_Thread::TERMINATED,
4529 "Invalid threadStatus value");
4530 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4531 1, /* only 1 substate */
4532 CHECK_NULL);
4533 names_h = objArrayHandle(THREAD, r);
4534 Handle name = java_lang_String::create_from_str("TERMINATED", CHECK_NULL);
4535 names_h->obj_at_put(0, name());
4536 break;
4537 }
4538 default:
4539 // Unknown state - probably incompatible JDK version
4540 return NULL;
4541 }
4542 return (jobjectArray) JNIHandles::make_local(env, names_h());
4543 }
4544 JVM_END
4546 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size))
4547 {
4548 memset(info, 0, sizeof(info_size));
4550 info->jvm_version = Abstract_VM_Version::jvm_version();
4551 info->update_version = 0; /* 0 in HotSpot Express VM */
4552 info->special_update_version = 0; /* 0 in HotSpot Express VM */
4554 // when we add a new capability in the jvm_version_info struct, we should also
4555 // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat
4556 // counter defined in runtimeService.cpp.
4557 info->is_attachable = AttachListener::is_attach_supported();
4558 #ifdef KERNEL
4559 info->is_kernel_jvm = 1; // true;
4560 #else // KERNEL
4561 info->is_kernel_jvm = 0; // false;
4562 #endif // KERNEL
4563 }
4564 JVM_END