Thu, 05 Jun 2008 15:57:56 -0700
6711316: Open source the Garbage-First garbage collector
Summary: First mercurial integration of the code for the Garbage-First garbage collector.
Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr
1 /*
2 * Copyright 1997-2007 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
632 JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name,
633 jboolean init, jobject loader,
634 jboolean throwError))
635 JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name,
636 throwError ? "error" : "exception");
637 // Java libraries should ensure that name is never null...
638 if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
639 // It's impossible to create this class; the name cannot fit
640 // into the constant pool.
641 if (throwError) {
642 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
643 } else {
644 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
645 }
646 }
647 symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
648 Handle h_loader(THREAD, JNIHandles::resolve(loader));
649 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
650 Handle(), throwError, thread);
652 if (TraceClassResolution && result != NULL) {
653 trace_class_resolution(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(result)));
654 }
656 return result;
657 JVM_END
660 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
661 jboolean init, jclass from))
662 JVMWrapper2("JVM_FindClassFromClass %s", name);
663 if (name == NULL || (int)strlen(name) > symbolOopDesc::max_length()) {
664 // It's impossible to create this class; the name cannot fit
665 // into the constant pool.
666 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
667 }
668 symbolHandle h_name = oopFactory::new_symbol_handle(name, CHECK_NULL);
669 oop from_class_oop = JNIHandles::resolve(from);
670 klassOop from_class = (from_class_oop == NULL)
671 ? (klassOop)NULL
672 : java_lang_Class::as_klassOop(from_class_oop);
673 oop class_loader = NULL;
674 oop protection_domain = NULL;
675 if (from_class != NULL) {
676 class_loader = Klass::cast(from_class)->class_loader();
677 protection_domain = Klass::cast(from_class)->protection_domain();
678 }
679 Handle h_loader(THREAD, class_loader);
680 Handle h_prot (THREAD, protection_domain);
681 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
682 h_prot, true, thread);
684 if (TraceClassResolution && result != NULL) {
685 // this function is generally only used for class loading during verification.
686 ResourceMark rm;
687 oop from_mirror = JNIHandles::resolve_non_null(from);
688 klassOop from_class = java_lang_Class::as_klassOop(from_mirror);
689 const char * from_name = Klass::cast(from_class)->external_name();
691 oop mirror = JNIHandles::resolve_non_null(result);
692 klassOop to_class = java_lang_Class::as_klassOop(mirror);
693 const char * to = Klass::cast(to_class)->external_name();
694 tty->print("RESOLVE %s %s (verification)\n", from_name, to);
695 }
697 return result;
698 JVM_END
700 static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) {
701 if (loader.is_null()) {
702 return;
703 }
705 // check whether the current caller thread holds the lock or not.
706 // If not, increment the corresponding counter
707 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) !=
708 ObjectSynchronizer::owner_self) {
709 counter->inc();
710 }
711 }
713 // common code for JVM_DefineClass() and JVM_DefineClassWithSource()
714 static jclass jvm_define_class_common(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source, TRAPS) {
716 // Since exceptions can be thrown, class initialization can take place
717 // if name is NULL no check for class name in .class stream has to be made.
718 symbolHandle class_name;
719 if (name != NULL) {
720 const int str_len = (int)strlen(name);
721 if (str_len > symbolOopDesc::max_length()) {
722 // It's impossible to create this class; the name cannot fit
723 // into the constant pool.
724 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
725 }
726 class_name = oopFactory::new_symbol_handle(name, str_len, CHECK_NULL);
727 }
729 ResourceMark rm(THREAD);
730 ClassFileStream st((u1*) buf, len, (char *)source);
731 Handle class_loader (THREAD, JNIHandles::resolve(loader));
732 if (UsePerfData) {
733 is_lock_held_by_thread(class_loader,
734 ClassLoader::sync_JVMDefineClassLockFreeCounter(),
735 THREAD);
736 }
737 Handle protection_domain (THREAD, JNIHandles::resolve(pd));
738 klassOop k = SystemDictionary::resolve_from_stream(class_name, class_loader,
739 protection_domain, &st,
740 CHECK_NULL);
742 if (TraceClassResolution && k != NULL) {
743 trace_class_resolution(k);
744 }
746 return (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
747 }
750 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
751 JVMWrapper2("JVM_DefineClass %s", name);
753 return jvm_define_class_common(env, name, loader, buf, len, pd, "__JVM_DefineClass__", THREAD);
754 JVM_END
757 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
758 JVMWrapper2("JVM_DefineClassWithSource %s", name);
760 return jvm_define_class_common(env, name, loader, buf, len, pd, source, THREAD);
761 JVM_END
764 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
765 JVMWrapper("JVM_FindLoadedClass");
766 ResourceMark rm(THREAD);
768 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
769 Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
771 const char* str = java_lang_String::as_utf8_string(string());
772 // Sanity check, don't expect null
773 if (str == NULL) return NULL;
775 const int str_len = (int)strlen(str);
776 if (str_len > symbolOopDesc::max_length()) {
777 // It's impossible to create this class; the name cannot fit
778 // into the constant pool.
779 return NULL;
780 }
781 symbolHandle klass_name = oopFactory::new_symbol_handle(str, str_len,CHECK_NULL);
783 // Security Note:
784 // The Java level wrapper will perform the necessary security check allowing
785 // us to pass the NULL as the initiating class loader.
786 Handle h_loader(THREAD, JNIHandles::resolve(loader));
787 if (UsePerfData) {
788 is_lock_held_by_thread(h_loader,
789 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
790 THREAD);
791 }
793 klassOop k = SystemDictionary::find_instance_or_array_klass(klass_name,
794 h_loader,
795 Handle(),
796 CHECK_NULL);
798 return (k == NULL) ? NULL :
799 (jclass) JNIHandles::make_local(env, Klass::cast(k)->java_mirror());
800 JVM_END
803 // Reflection support //////////////////////////////////////////////////////////////////////////////
805 JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls))
806 assert (cls != NULL, "illegal class");
807 JVMWrapper("JVM_GetClassName");
808 JvmtiVMObjectAllocEventCollector oam;
809 ResourceMark rm(THREAD);
810 const char* name;
811 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
812 name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls)));
813 } else {
814 // Consider caching interned string in Klass
815 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
816 assert(k->is_klass(), "just checking");
817 name = Klass::cast(k)->external_name();
818 }
819 oop result = StringTable::intern((char*) name, CHECK_NULL);
820 return (jstring) JNIHandles::make_local(env, result);
821 JVM_END
824 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
825 JVMWrapper("JVM_GetClassInterfaces");
826 JvmtiVMObjectAllocEventCollector oam;
827 oop mirror = JNIHandles::resolve_non_null(cls);
829 // Special handling for primitive objects
830 if (java_lang_Class::is_primitive(mirror)) {
831 // Primitive objects does not have any interfaces
832 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
833 return (jobjectArray) JNIHandles::make_local(env, r);
834 }
836 KlassHandle klass(thread, java_lang_Class::as_klassOop(mirror));
837 // Figure size of result array
838 int size;
839 if (klass->oop_is_instance()) {
840 size = instanceKlass::cast(klass())->local_interfaces()->length();
841 } else {
842 assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass");
843 size = 2;
844 }
846 // Allocate result array
847 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), size, CHECK_NULL);
848 objArrayHandle result (THREAD, r);
849 // Fill in result
850 if (klass->oop_is_instance()) {
851 // Regular instance klass, fill in all local interfaces
852 for (int index = 0; index < size; index++) {
853 klassOop k = klassOop(instanceKlass::cast(klass())->local_interfaces()->obj_at(index));
854 result->obj_at_put(index, Klass::cast(k)->java_mirror());
855 }
856 } else {
857 // All arrays implement java.lang.Cloneable and java.io.Serializable
858 result->obj_at_put(0, Klass::cast(SystemDictionary::cloneable_klass())->java_mirror());
859 result->obj_at_put(1, Klass::cast(SystemDictionary::serializable_klass())->java_mirror());
860 }
861 return (jobjectArray) JNIHandles::make_local(env, result());
862 JVM_END
865 JVM_ENTRY(jobject, JVM_GetClassLoader(JNIEnv *env, jclass cls))
866 JVMWrapper("JVM_GetClassLoader");
867 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
868 return NULL;
869 }
870 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
871 oop loader = Klass::cast(k)->class_loader();
872 return JNIHandles::make_local(env, loader);
873 JVM_END
876 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
877 JVMWrapper("JVM_IsInterface");
878 oop mirror = JNIHandles::resolve_non_null(cls);
879 if (java_lang_Class::is_primitive(mirror)) {
880 return JNI_FALSE;
881 }
882 klassOop k = java_lang_Class::as_klassOop(mirror);
883 jboolean result = Klass::cast(k)->is_interface();
884 assert(!result || Klass::cast(k)->oop_is_instance(),
885 "all interfaces are instance types");
886 // The compiler intrinsic for isInterface tests the
887 // Klass::_access_flags bits in the same way.
888 return result;
889 JVM_END
892 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
893 JVMWrapper("JVM_GetClassSigners");
894 JvmtiVMObjectAllocEventCollector oam;
895 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
896 // There are no signers for primitive types
897 return NULL;
898 }
900 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
901 objArrayOop signers = NULL;
902 if (Klass::cast(k)->oop_is_instance()) {
903 signers = instanceKlass::cast(k)->signers();
904 }
906 // If there are no signers set in the class, or if the class
907 // is an array, return NULL.
908 if (signers == NULL) return NULL;
910 // copy of the signers array
911 klassOop element = objArrayKlass::cast(signers->klass())->element_klass();
912 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
913 for (int index = 0; index < signers->length(); index++) {
914 signers_copy->obj_at_put(index, signers->obj_at(index));
915 }
917 // return the copy
918 return (jobjectArray) JNIHandles::make_local(env, signers_copy);
919 JVM_END
922 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
923 JVMWrapper("JVM_SetClassSigners");
924 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
925 // This call is ignored for primitive types and arrays.
926 // Signers are only set once, ClassLoader.java, and thus shouldn't
927 // be called with an array. Only the bootstrap loader creates arrays.
928 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
929 if (Klass::cast(k)->oop_is_instance()) {
930 instanceKlass::cast(k)->set_signers(objArrayOop(JNIHandles::resolve(signers)));
931 }
932 }
933 JVM_END
936 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
937 JVMWrapper("JVM_GetProtectionDomain");
938 if (JNIHandles::resolve(cls) == NULL) {
939 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
940 }
942 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
943 // Primitive types does not have a protection domain.
944 return NULL;
945 }
947 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
948 return (jobject) JNIHandles::make_local(env, Klass::cast(k)->protection_domain());
949 JVM_END
952 // Obsolete since 1.2 (Class.setProtectionDomain removed), although
953 // still defined in core libraries as of 1.5.
954 JVM_ENTRY(void, JVM_SetProtectionDomain(JNIEnv *env, jclass cls, jobject protection_domain))
955 JVMWrapper("JVM_SetProtectionDomain");
956 if (JNIHandles::resolve(cls) == NULL) {
957 THROW(vmSymbols::java_lang_NullPointerException());
958 }
959 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
960 // Call is ignored for primitive types
961 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
963 // cls won't be an array, as this called only from ClassLoader.defineClass
964 if (Klass::cast(k)->oop_is_instance()) {
965 oop pd = JNIHandles::resolve(protection_domain);
966 assert(pd == NULL || pd->is_oop(), "just checking");
967 instanceKlass::cast(k)->set_protection_domain(pd);
968 }
969 }
970 JVM_END
973 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
974 JVMWrapper("JVM_DoPrivileged");
976 if (action == NULL) {
977 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
978 }
980 // Stack allocated list of privileged stack elements
981 PrivilegedElement pi;
983 // Check that action object understands "Object run()"
984 Handle object (THREAD, JNIHandles::resolve(action));
986 // get run() method
987 methodOop m_oop = Klass::cast(object->klass())->uncached_lookup_method(
988 vmSymbols::run_method_name(),
989 vmSymbols::void_object_signature());
990 methodHandle m (THREAD, m_oop);
991 if (m.is_null() || !m->is_method() || !methodOop(m())->is_public() || methodOop(m())->is_static()) {
992 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method");
993 }
995 // Compute the frame initiating the do privileged operation and setup the privileged stack
996 vframeStream vfst(thread);
997 vfst.security_get_caller_frame(1);
999 if (!vfst.at_end()) {
1000 pi.initialize(&vfst, JNIHandles::resolve(context), thread->privileged_stack_top(), CHECK_NULL);
1001 thread->set_privileged_stack_top(&pi);
1002 }
1005 // invoke the Object run() in the action object. We cannot use call_interface here, since the static type
1006 // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction
1007 Handle pending_exception;
1008 JavaValue result(T_OBJECT);
1009 JavaCallArguments args(object);
1010 JavaCalls::call(&result, m, &args, THREAD);
1012 // done with action, remove ourselves from the list
1013 if (!vfst.at_end()) {
1014 assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element");
1015 thread->set_privileged_stack_top(thread->privileged_stack_top()->next());
1016 }
1018 if (HAS_PENDING_EXCEPTION) {
1019 pending_exception = Handle(THREAD, PENDING_EXCEPTION);
1020 CLEAR_PENDING_EXCEPTION;
1022 if ( pending_exception->is_a(SystemDictionary::exception_klass()) &&
1023 !pending_exception->is_a(SystemDictionary::runtime_exception_klass())) {
1024 // Throw a java.security.PrivilegedActionException(Exception e) exception
1025 JavaCallArguments args(pending_exception);
1026 THROW_ARG_0(vmSymbolHandles::java_security_PrivilegedActionException(),
1027 vmSymbolHandles::exception_void_signature(),
1028 &args);
1029 }
1030 }
1032 if (pending_exception.not_null()) THROW_OOP_0(pending_exception());
1033 return JNIHandles::make_local(env, (oop) result.get_jobject());
1034 JVM_END
1037 // Returns the inherited_access_control_context field of the running thread.
1038 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
1039 JVMWrapper("JVM_GetInheritedAccessControlContext");
1040 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
1041 return JNIHandles::make_local(env, result);
1042 JVM_END
1044 class RegisterArrayForGC {
1045 private:
1046 JavaThread *_thread;
1047 public:
1048 RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array) {
1049 _thread = thread;
1050 _thread->register_array_for_gc(array);
1051 }
1053 ~RegisterArrayForGC() {
1054 _thread->register_array_for_gc(NULL);
1055 }
1056 };
1059 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
1060 JVMWrapper("JVM_GetStackAccessControlContext");
1061 if (!UsePrivilegedStack) return NULL;
1063 ResourceMark rm(THREAD);
1064 GrowableArray<oop>* local_array = new GrowableArray<oop>(12);
1065 JvmtiVMObjectAllocEventCollector oam;
1067 // count the protection domains on the execution stack. We collapse
1068 // duplicate consecutive protection domains into a single one, as
1069 // well as stopping when we hit a privileged frame.
1071 // Use vframeStream to iterate through Java frames
1072 vframeStream vfst(thread);
1074 oop previous_protection_domain = NULL;
1075 Handle privileged_context(thread, NULL);
1076 bool is_privileged = false;
1077 oop protection_domain = NULL;
1079 for(; !vfst.at_end(); vfst.next()) {
1080 // get method of frame
1081 methodOop method = vfst.method();
1082 intptr_t* frame_id = vfst.frame_id();
1084 // check the privileged frames to see if we have a match
1085 if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) {
1086 // this frame is privileged
1087 is_privileged = true;
1088 privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context());
1089 protection_domain = thread->privileged_stack_top()->protection_domain();
1090 } else {
1091 protection_domain = instanceKlass::cast(method->method_holder())->protection_domain();
1092 }
1094 if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
1095 local_array->push(protection_domain);
1096 previous_protection_domain = protection_domain;
1097 }
1099 if (is_privileged) break;
1100 }
1103 // either all the domains on the stack were system domains, or
1104 // we had a privileged system domain
1105 if (local_array->is_empty()) {
1106 if (is_privileged && privileged_context.is_null()) return NULL;
1108 oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
1109 return JNIHandles::make_local(env, result);
1110 }
1112 // the resource area must be registered in case of a gc
1113 RegisterArrayForGC ragc(thread, local_array);
1114 objArrayOop context = oopFactory::new_objArray(SystemDictionary::protectionDomain_klass(),
1115 local_array->length(), CHECK_NULL);
1116 objArrayHandle h_context(thread, context);
1117 for (int index = 0; index < local_array->length(); index++) {
1118 h_context->obj_at_put(index, local_array->at(index));
1119 }
1121 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
1123 return JNIHandles::make_local(env, result);
1124 JVM_END
1127 JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
1128 JVMWrapper("JVM_IsArrayClass");
1129 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1130 return (k != NULL) && Klass::cast(k)->oop_is_javaArray() ? true : false;
1131 JVM_END
1134 JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
1135 JVMWrapper("JVM_IsPrimitiveClass");
1136 oop mirror = JNIHandles::resolve_non_null(cls);
1137 return (jboolean) java_lang_Class::is_primitive(mirror);
1138 JVM_END
1141 JVM_ENTRY(jclass, JVM_GetComponentType(JNIEnv *env, jclass cls))
1142 JVMWrapper("JVM_GetComponentType");
1143 oop mirror = JNIHandles::resolve_non_null(cls);
1144 oop result = Reflection::array_component_type(mirror, CHECK_NULL);
1145 return (jclass) JNIHandles::make_local(env, result);
1146 JVM_END
1149 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
1150 JVMWrapper("JVM_GetClassModifiers");
1151 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1152 // Primitive type
1153 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1154 }
1156 Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
1157 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
1158 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
1159 return k->modifier_flags();
1160 JVM_END
1163 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
1165 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
1166 const int inner_class_info_index = 0;
1167 const int outer_class_info_index = 1;
1169 JvmtiVMObjectAllocEventCollector oam;
1170 // ofClass is a reference to a java_lang_Class object. The mirror object
1171 // of an instanceKlass
1173 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1174 ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
1175 oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
1176 return (jobjectArray)JNIHandles::make_local(env, result);
1177 }
1179 instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1181 if (k->inner_classes()->length() == 0) {
1182 // Neither an inner nor outer class
1183 oop result = oopFactory::new_objArray(SystemDictionary::class_klass(), 0, CHECK_NULL);
1184 return (jobjectArray)JNIHandles::make_local(env, result);
1185 }
1187 // find inner class info
1188 typeArrayHandle icls(thread, k->inner_classes());
1189 constantPoolHandle cp(thread, k->constants());
1190 int length = icls->length();
1192 // Allocate temp. result array
1193 objArrayOop r = oopFactory::new_objArray(SystemDictionary::class_klass(), length/4, CHECK_NULL);
1194 objArrayHandle result (THREAD, r);
1195 int members = 0;
1197 for(int i = 0; i < length; i += 4) {
1198 int ioff = icls->ushort_at(i + inner_class_info_index);
1199 int ooff = icls->ushort_at(i + outer_class_info_index);
1201 if (ioff != 0 && ooff != 0) {
1202 // Check to see if the name matches the class we're looking for
1203 // before attempting to find the class.
1204 if (cp->klass_name_at_matches(k, ooff)) {
1205 klassOop outer_klass = cp->klass_at(ooff, CHECK_NULL);
1206 if (outer_klass == k()) {
1207 klassOop ik = cp->klass_at(ioff, CHECK_NULL);
1208 instanceKlassHandle inner_klass (THREAD, ik);
1210 // Throws an exception if outer klass has not declared k as
1211 // an inner klass
1212 Reflection::check_for_inner_class(k, inner_klass, CHECK_NULL);
1214 result->obj_at_put(members, inner_klass->java_mirror());
1215 members++;
1216 }
1217 }
1218 }
1219 }
1221 if (members != length) {
1222 // Return array of right length
1223 objArrayOop res = oopFactory::new_objArray(SystemDictionary::class_klass(), members, CHECK_NULL);
1224 for(int i = 0; i < members; i++) {
1225 res->obj_at_put(i, result->obj_at(i));
1226 }
1227 return (jobjectArray)JNIHandles::make_local(env, res);
1228 }
1230 return (jobjectArray)JNIHandles::make_local(env, result());
1231 JVM_END
1234 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
1235 const int inner_class_info_index = 0;
1236 const int outer_class_info_index = 1;
1238 // ofClass is a reference to a java_lang_Class object.
1239 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1240 ! Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_instance()) {
1241 return NULL;
1242 }
1244 instanceKlassHandle k(thread, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1246 if (k->inner_classes()->length() == 0) {
1247 // No inner class info => no declaring class
1248 return NULL;
1249 }
1251 typeArrayHandle i_icls(thread, k->inner_classes());
1252 constantPoolHandle i_cp(thread, k->constants());
1253 int i_length = i_icls->length();
1255 bool found = false;
1256 klassOop ok;
1257 instanceKlassHandle outer_klass;
1259 // Find inner_klass attribute
1260 for(int i = 0; i < i_length && !found; i+= 4) {
1261 int ioff = i_icls->ushort_at(i + inner_class_info_index);
1262 int ooff = i_icls->ushort_at(i + outer_class_info_index);
1264 if (ioff != 0 && ooff != 0) {
1265 // Check to see if the name matches the class we're looking for
1266 // before attempting to find the class.
1267 if (i_cp->klass_name_at_matches(k, ioff)) {
1268 klassOop inner_klass = i_cp->klass_at(ioff, CHECK_NULL);
1269 if (k() == inner_klass) {
1270 found = true;
1271 ok = i_cp->klass_at(ooff, CHECK_NULL);
1272 outer_klass = instanceKlassHandle(thread, ok);
1273 }
1274 }
1275 }
1276 }
1278 // If no inner class attribute found for this class.
1279 if (!found) return NULL;
1281 // Throws an exception if outer klass has not declared k as an inner klass
1282 Reflection::check_for_inner_class(outer_klass, k, CHECK_NULL);
1284 return (jclass)JNIHandles::make_local(env, outer_klass->java_mirror());
1285 JVM_END
1288 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
1289 assert (cls != NULL, "illegal class");
1290 JVMWrapper("JVM_GetClassSignature");
1291 JvmtiVMObjectAllocEventCollector oam;
1292 ResourceMark rm(THREAD);
1293 // Return null for arrays and primatives
1294 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1295 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
1296 if (Klass::cast(k)->oop_is_instance()) {
1297 symbolHandle sym = symbolHandle(THREAD, instanceKlass::cast(k)->generic_signature());
1298 if (sym.is_null()) return NULL;
1299 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1300 return (jstring) JNIHandles::make_local(env, str());
1301 }
1302 }
1303 return NULL;
1304 JVM_END
1307 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
1308 assert (cls != NULL, "illegal class");
1309 JVMWrapper("JVM_GetClassAnnotations");
1310 ResourceMark rm(THREAD);
1311 // Return null for arrays and primitives
1312 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
1313 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve(cls));
1314 if (Klass::cast(k)->oop_is_instance()) {
1315 return (jbyteArray) JNIHandles::make_local(env,
1316 instanceKlass::cast(k)->class_annotations());
1317 }
1318 }
1319 return NULL;
1320 JVM_END
1323 JVM_ENTRY(jbyteArray, JVM_GetFieldAnnotations(JNIEnv *env, jobject field))
1324 assert(field != NULL, "illegal field");
1325 JVMWrapper("JVM_GetFieldAnnotations");
1327 // some of this code was adapted from from jni_FromReflectedField
1329 // field is a handle to a java.lang.reflect.Field object
1330 oop reflected = JNIHandles::resolve_non_null(field);
1331 oop mirror = java_lang_reflect_Field::clazz(reflected);
1332 klassOop k = java_lang_Class::as_klassOop(mirror);
1333 int slot = java_lang_reflect_Field::slot(reflected);
1334 int modifiers = java_lang_reflect_Field::modifiers(reflected);
1336 fieldDescriptor fd;
1337 KlassHandle kh(THREAD, k);
1338 intptr_t offset = instanceKlass::cast(kh())->offset_from_fields(slot);
1340 if (modifiers & JVM_ACC_STATIC) {
1341 // for static fields we only look in the current class
1342 if (!instanceKlass::cast(kh())->find_local_field_from_offset(offset,
1343 true, &fd)) {
1344 assert(false, "cannot find static field");
1345 return NULL; // robustness
1346 }
1347 } else {
1348 // for instance fields we start with the current class and work
1349 // our way up through the superclass chain
1350 if (!instanceKlass::cast(kh())->find_field_from_offset(offset, false,
1351 &fd)) {
1352 assert(false, "cannot find instance field");
1353 return NULL; // robustness
1354 }
1355 }
1357 return (jbyteArray) JNIHandles::make_local(env, fd.annotations());
1358 JVM_END
1361 static methodOop jvm_get_method_common(jobject method, TRAPS) {
1362 // some of this code was adapted from from jni_FromReflectedMethod
1364 oop reflected = JNIHandles::resolve_non_null(method);
1365 oop mirror = NULL;
1366 int slot = 0;
1368 if (reflected->klass() == SystemDictionary::reflect_constructor_klass()) {
1369 mirror = java_lang_reflect_Constructor::clazz(reflected);
1370 slot = java_lang_reflect_Constructor::slot(reflected);
1371 } else {
1372 assert(reflected->klass() == SystemDictionary::reflect_method_klass(),
1373 "wrong type");
1374 mirror = java_lang_reflect_Method::clazz(reflected);
1375 slot = java_lang_reflect_Method::slot(reflected);
1376 }
1377 klassOop k = java_lang_Class::as_klassOop(mirror);
1379 KlassHandle kh(THREAD, k);
1380 methodOop m = instanceKlass::cast(kh())->method_with_idnum(slot);
1381 if (m == NULL) {
1382 assert(false, "cannot find method");
1383 return NULL; // robustness
1384 }
1386 return m;
1387 }
1390 JVM_ENTRY(jbyteArray, JVM_GetMethodAnnotations(JNIEnv *env, jobject method))
1391 JVMWrapper("JVM_GetMethodAnnotations");
1393 // method is a handle to a java.lang.reflect.Method object
1394 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1395 return (jbyteArray) JNIHandles::make_local(env, m->annotations());
1396 JVM_END
1399 JVM_ENTRY(jbyteArray, JVM_GetMethodDefaultAnnotationValue(JNIEnv *env, jobject method))
1400 JVMWrapper("JVM_GetMethodDefaultAnnotationValue");
1402 // method is a handle to a java.lang.reflect.Method object
1403 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1404 return (jbyteArray) JNIHandles::make_local(env, m->annotation_default());
1405 JVM_END
1408 JVM_ENTRY(jbyteArray, JVM_GetMethodParameterAnnotations(JNIEnv *env, jobject method))
1409 JVMWrapper("JVM_GetMethodParameterAnnotations");
1411 // method is a handle to a java.lang.reflect.Method object
1412 methodOop m = jvm_get_method_common(method, CHECK_NULL);
1413 return (jbyteArray) JNIHandles::make_local(env, m->parameter_annotations());
1414 JVM_END
1417 // New (JDK 1.4) reflection implementation /////////////////////////////////////
1419 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1420 {
1421 JVMWrapper("JVM_GetClassDeclaredFields");
1422 JvmtiVMObjectAllocEventCollector oam;
1424 // Exclude primitive types and array types
1425 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
1426 Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1427 // Return empty array
1428 oop res = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), 0, CHECK_NULL);
1429 return (jobjectArray) JNIHandles::make_local(env, res);
1430 }
1432 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1433 constantPoolHandle cp(THREAD, k->constants());
1435 // Ensure class is linked
1436 k->link_class(CHECK_NULL);
1438 typeArrayHandle fields(THREAD, k->fields());
1439 int fields_len = fields->length();
1441 // 4496456 We need to filter out java.lang.Throwable.backtrace
1442 bool skip_backtrace = false;
1444 // Allocate result
1445 int num_fields;
1447 if (publicOnly) {
1448 num_fields = 0;
1449 for (int i = 0, j = 0; i < fields_len; i += instanceKlass::next_offset, j++) {
1450 int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
1451 if (mods & JVM_ACC_PUBLIC) ++num_fields;
1452 }
1453 } else {
1454 num_fields = fields_len / instanceKlass::next_offset;
1456 if (k() == SystemDictionary::throwable_klass()) {
1457 num_fields--;
1458 skip_backtrace = true;
1459 }
1460 }
1462 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_field_klass(), num_fields, CHECK_NULL);
1463 objArrayHandle result (THREAD, r);
1465 int out_idx = 0;
1466 fieldDescriptor fd;
1467 for (int i = 0; i < fields_len; i += instanceKlass::next_offset) {
1468 if (skip_backtrace) {
1469 // 4496456 skip java.lang.Throwable.backtrace
1470 int offset = k->offset_from_fields(i);
1471 if (offset == java_lang_Throwable::get_backtrace_offset()) continue;
1472 }
1474 int mods = fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
1475 if (!publicOnly || (mods & JVM_ACC_PUBLIC)) {
1476 fd.initialize(k(), i);
1477 oop field = Reflection::new_field(&fd, UseNewReflection, CHECK_NULL);
1478 result->obj_at_put(out_idx, field);
1479 ++out_idx;
1480 }
1481 }
1482 assert(out_idx == num_fields, "just checking");
1483 return (jobjectArray) JNIHandles::make_local(env, result());
1484 }
1485 JVM_END
1487 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1488 {
1489 JVMWrapper("JVM_GetClassDeclaredMethods");
1490 JvmtiVMObjectAllocEventCollector oam;
1492 // Exclude primitive types and array types
1493 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1494 || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1495 // Return empty array
1496 oop res = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), 0, CHECK_NULL);
1497 return (jobjectArray) JNIHandles::make_local(env, res);
1498 }
1500 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1502 // Ensure class is linked
1503 k->link_class(CHECK_NULL);
1505 objArrayHandle methods (THREAD, k->methods());
1506 int methods_length = methods->length();
1507 int num_methods = 0;
1509 int i;
1510 for (i = 0; i < methods_length; i++) {
1511 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1512 if (!method->is_initializer()) {
1513 if (!publicOnly || method->is_public()) {
1514 ++num_methods;
1515 }
1516 }
1517 }
1519 // Allocate result
1520 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_method_klass(), num_methods, CHECK_NULL);
1521 objArrayHandle result (THREAD, r);
1523 int out_idx = 0;
1524 for (i = 0; i < methods_length; i++) {
1525 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1526 if (!method->is_initializer()) {
1527 if (!publicOnly || method->is_public()) {
1528 oop m = Reflection::new_method(method, UseNewReflection, false, CHECK_NULL);
1529 result->obj_at_put(out_idx, m);
1530 ++out_idx;
1531 }
1532 }
1533 }
1534 assert(out_idx == num_methods, "just checking");
1535 return (jobjectArray) JNIHandles::make_local(env, result());
1536 }
1537 JVM_END
1539 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
1540 {
1541 JVMWrapper("JVM_GetClassDeclaredConstructors");
1542 JvmtiVMObjectAllocEventCollector oam;
1544 // Exclude primitive types and array types
1545 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
1546 || Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)))->oop_is_javaArray()) {
1547 // Return empty array
1548 oop res = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), 0 , CHECK_NULL);
1549 return (jobjectArray) JNIHandles::make_local(env, res);
1550 }
1552 instanceKlassHandle k(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(ofClass)));
1554 // Ensure class is linked
1555 k->link_class(CHECK_NULL);
1557 objArrayHandle methods (THREAD, k->methods());
1558 int methods_length = methods->length();
1559 int num_constructors = 0;
1561 int i;
1562 for (i = 0; i < methods_length; i++) {
1563 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1564 if (method->is_initializer() && !method->is_static()) {
1565 if (!publicOnly || method->is_public()) {
1566 ++num_constructors;
1567 }
1568 }
1569 }
1571 // Allocate result
1572 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_constructor_klass(), num_constructors, CHECK_NULL);
1573 objArrayHandle result(THREAD, r);
1575 int out_idx = 0;
1576 for (i = 0; i < methods_length; i++) {
1577 methodHandle method(THREAD, (methodOop) methods->obj_at(i));
1578 if (method->is_initializer() && !method->is_static()) {
1579 if (!publicOnly || method->is_public()) {
1580 oop m = Reflection::new_constructor(method, CHECK_NULL);
1581 result->obj_at_put(out_idx, m);
1582 ++out_idx;
1583 }
1584 }
1585 }
1586 assert(out_idx == num_constructors, "just checking");
1587 return (jobjectArray) JNIHandles::make_local(env, result());
1588 }
1589 JVM_END
1591 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
1592 {
1593 JVMWrapper("JVM_GetClassAccessFlags");
1594 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1595 // Primitive type
1596 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
1597 }
1599 Klass* k = Klass::cast(java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
1600 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
1601 }
1602 JVM_END
1605 // Constant pool access //////////////////////////////////////////////////////////
1607 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
1608 {
1609 JVMWrapper("JVM_GetClassConstantPool");
1610 JvmtiVMObjectAllocEventCollector oam;
1612 // Return null for primitives and arrays
1613 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
1614 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1615 if (Klass::cast(k)->oop_is_instance()) {
1616 instanceKlassHandle k_h(THREAD, k);
1617 Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL);
1618 sun_reflect_ConstantPool::set_cp_oop(jcp(), k_h->constants());
1619 return JNIHandles::make_local(jcp());
1620 }
1621 }
1622 return NULL;
1623 }
1624 JVM_END
1627 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject unused, jobject jcpool))
1628 {
1629 JVMWrapper("JVM_ConstantPoolGetSize");
1630 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1631 return cp->length();
1632 }
1633 JVM_END
1636 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
1637 if (!cp->is_within_bounds(index)) {
1638 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
1639 }
1640 }
1643 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1644 {
1645 JVMWrapper("JVM_ConstantPoolGetClassAt");
1646 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1647 bounds_check(cp, index, CHECK_NULL);
1648 constantTag tag = cp->tag_at(index);
1649 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
1650 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1651 }
1652 klassOop k = cp->klass_at(index, CHECK_NULL);
1653 return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
1654 }
1655 JVM_END
1658 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1659 {
1660 JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded");
1661 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1662 bounds_check(cp, index, CHECK_NULL);
1663 constantTag tag = cp->tag_at(index);
1664 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
1665 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1666 }
1667 klassOop k = constantPoolOopDesc::klass_at_if_loaded(cp, index);
1668 if (k == NULL) return NULL;
1669 return (jclass) JNIHandles::make_local(k->klass_part()->java_mirror());
1670 }
1671 JVM_END
1673 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1674 constantTag tag = cp->tag_at(index);
1675 if (!tag.is_method() && !tag.is_interface_method()) {
1676 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1677 }
1678 int klass_ref = cp->uncached_klass_ref_index_at(index);
1679 klassOop k_o;
1680 if (force_resolution) {
1681 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1682 } else {
1683 k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
1684 if (k_o == NULL) return NULL;
1685 }
1686 instanceKlassHandle k(THREAD, k_o);
1687 symbolOop name = cp->uncached_name_ref_at(index);
1688 symbolOop sig = cp->uncached_signature_ref_at(index);
1689 methodHandle m (THREAD, k->find_method(name, sig));
1690 if (m.is_null()) {
1691 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
1692 }
1693 oop method;
1694 if (!m->is_initializer() || m->is_static()) {
1695 method = Reflection::new_method(m, true, true, CHECK_NULL);
1696 } else {
1697 method = Reflection::new_constructor(m, CHECK_NULL);
1698 }
1699 return JNIHandles::make_local(method);
1700 }
1702 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1703 {
1704 JVMWrapper("JVM_ConstantPoolGetMethodAt");
1705 JvmtiVMObjectAllocEventCollector oam;
1706 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1707 bounds_check(cp, index, CHECK_NULL);
1708 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
1709 return res;
1710 }
1711 JVM_END
1713 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1714 {
1715 JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded");
1716 JvmtiVMObjectAllocEventCollector oam;
1717 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1718 bounds_check(cp, index, CHECK_NULL);
1719 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
1720 return res;
1721 }
1722 JVM_END
1724 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
1725 constantTag tag = cp->tag_at(index);
1726 if (!tag.is_field()) {
1727 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1728 }
1729 int klass_ref = cp->uncached_klass_ref_index_at(index);
1730 klassOop k_o;
1731 if (force_resolution) {
1732 k_o = cp->klass_at(klass_ref, CHECK_NULL);
1733 } else {
1734 k_o = constantPoolOopDesc::klass_at_if_loaded(cp, klass_ref);
1735 if (k_o == NULL) return NULL;
1736 }
1737 instanceKlassHandle k(THREAD, k_o);
1738 symbolOop name = cp->uncached_name_ref_at(index);
1739 symbolOop sig = cp->uncached_signature_ref_at(index);
1740 fieldDescriptor fd;
1741 klassOop target_klass = k->find_field(name, sig, &fd);
1742 if (target_klass == NULL) {
1743 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
1744 }
1745 oop field = Reflection::new_field(&fd, true, CHECK_NULL);
1746 return JNIHandles::make_local(field);
1747 }
1749 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1750 {
1751 JVMWrapper("JVM_ConstantPoolGetFieldAt");
1752 JvmtiVMObjectAllocEventCollector oam;
1753 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1754 bounds_check(cp, index, CHECK_NULL);
1755 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
1756 return res;
1757 }
1758 JVM_END
1760 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1761 {
1762 JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded");
1763 JvmtiVMObjectAllocEventCollector oam;
1764 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1765 bounds_check(cp, index, CHECK_NULL);
1766 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
1767 return res;
1768 }
1769 JVM_END
1771 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1772 {
1773 JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt");
1774 JvmtiVMObjectAllocEventCollector oam;
1775 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1776 bounds_check(cp, index, CHECK_NULL);
1777 constantTag tag = cp->tag_at(index);
1778 if (!tag.is_field_or_method()) {
1779 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1780 }
1781 int klass_ref = cp->uncached_klass_ref_index_at(index);
1782 symbolHandle klass_name (THREAD, cp->klass_name_at(klass_ref));
1783 symbolHandle member_name(THREAD, cp->uncached_name_ref_at(index));
1784 symbolHandle member_sig (THREAD, cp->uncached_signature_ref_at(index));
1785 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::string_klass(), 3, CHECK_NULL);
1786 objArrayHandle dest(THREAD, dest_o);
1787 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
1788 dest->obj_at_put(0, str());
1789 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
1790 dest->obj_at_put(1, str());
1791 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
1792 dest->obj_at_put(2, str());
1793 return (jobjectArray) JNIHandles::make_local(dest());
1794 }
1795 JVM_END
1797 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1798 {
1799 JVMWrapper("JVM_ConstantPoolGetIntAt");
1800 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1801 bounds_check(cp, index, CHECK_0);
1802 constantTag tag = cp->tag_at(index);
1803 if (!tag.is_int()) {
1804 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1805 }
1806 return cp->int_at(index);
1807 }
1808 JVM_END
1810 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1811 {
1812 JVMWrapper("JVM_ConstantPoolGetLongAt");
1813 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1814 bounds_check(cp, index, CHECK_(0L));
1815 constantTag tag = cp->tag_at(index);
1816 if (!tag.is_long()) {
1817 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1818 }
1819 return cp->long_at(index);
1820 }
1821 JVM_END
1823 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1824 {
1825 JVMWrapper("JVM_ConstantPoolGetFloatAt");
1826 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1827 bounds_check(cp, index, CHECK_(0.0f));
1828 constantTag tag = cp->tag_at(index);
1829 if (!tag.is_float()) {
1830 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1831 }
1832 return cp->float_at(index);
1833 }
1834 JVM_END
1836 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1837 {
1838 JVMWrapper("JVM_ConstantPoolGetDoubleAt");
1839 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1840 bounds_check(cp, index, CHECK_(0.0));
1841 constantTag tag = cp->tag_at(index);
1842 if (!tag.is_double()) {
1843 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1844 }
1845 return cp->double_at(index);
1846 }
1847 JVM_END
1849 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1850 {
1851 JVMWrapper("JVM_ConstantPoolGetStringAt");
1852 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1853 bounds_check(cp, index, CHECK_NULL);
1854 constantTag tag = cp->tag_at(index);
1855 if (!tag.is_string() && !tag.is_unresolved_string()) {
1856 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1857 }
1858 oop str = cp->string_at(index, CHECK_NULL);
1859 return (jstring) JNIHandles::make_local(str);
1860 }
1861 JVM_END
1863 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject unused, jobject jcpool, jint index))
1864 {
1865 JVMWrapper("JVM_ConstantPoolGetUTF8At");
1866 JvmtiVMObjectAllocEventCollector oam;
1867 constantPoolHandle cp = constantPoolHandle(THREAD, constantPoolOop(JNIHandles::resolve_non_null(jcpool)));
1868 bounds_check(cp, index, CHECK_NULL);
1869 constantTag tag = cp->tag_at(index);
1870 if (!tag.is_symbol()) {
1871 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
1872 }
1873 symbolOop sym_o = cp->symbol_at(index);
1874 symbolHandle sym(THREAD, sym_o);
1875 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
1876 return (jstring) JNIHandles::make_local(str());
1877 }
1878 JVM_END
1881 // Assertion support. //////////////////////////////////////////////////////////
1883 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
1884 JVMWrapper("JVM_DesiredAssertionStatus");
1885 assert(cls != NULL, "bad class");
1887 oop r = JNIHandles::resolve(cls);
1888 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
1889 if (java_lang_Class::is_primitive(r)) return false;
1891 klassOop k = java_lang_Class::as_klassOop(r);
1892 assert(Klass::cast(k)->oop_is_instance(), "must be an instance klass");
1893 if (! Klass::cast(k)->oop_is_instance()) return false;
1895 ResourceMark rm(THREAD);
1896 const char* name = Klass::cast(k)->name()->as_C_string();
1897 bool system_class = Klass::cast(k)->class_loader() == NULL;
1898 return JavaAssertions::enabled(name, system_class);
1900 JVM_END
1903 // Return a new AssertionStatusDirectives object with the fields filled in with
1904 // command-line assertion arguments (i.e., -ea, -da).
1905 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
1906 JVMWrapper("JVM_AssertionStatusDirectives");
1907 JvmtiVMObjectAllocEventCollector oam;
1908 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
1909 return JNIHandles::make_local(env, asd);
1910 JVM_END
1912 // Verification ////////////////////////////////////////////////////////////////////////////////
1914 // Reflection for the verifier /////////////////////////////////////////////////////////////////
1916 // RedefineClasses support: bug 6214132 caused verification to fail.
1917 // All functions from this section should call the jvmtiThreadSate function:
1918 // klassOop class_to_verify_considering_redefinition(klassOop klass).
1919 // The function returns a klassOop of the _scratch_class if the verifier
1920 // was invoked in the middle of the class redefinition.
1921 // Otherwise it returns its argument value which is the _the_class klassOop.
1922 // Please, refer to the description in the jvmtiThreadSate.hpp.
1924 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
1925 JVMWrapper("JVM_GetClassNameUTF");
1926 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1927 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1928 return Klass::cast(k)->name()->as_utf8();
1929 JVM_END
1932 JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
1933 JVMWrapper("JVM_GetClassCPTypes");
1934 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1935 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1936 // types will have length zero if this is not an instanceKlass
1937 // (length is determined by call to JVM_GetClassCPEntriesCount)
1938 if (Klass::cast(k)->oop_is_instance()) {
1939 constantPoolOop cp = instanceKlass::cast(k)->constants();
1940 for (int index = cp->length() - 1; index >= 0; index--) {
1941 constantTag tag = cp->tag_at(index);
1942 types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class :
1943 (tag.is_unresolved_string()) ? JVM_CONSTANT_String : tag.value();
1944 }
1945 }
1946 JVM_END
1949 JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
1950 JVMWrapper("JVM_GetClassCPEntriesCount");
1951 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1952 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1953 if (!Klass::cast(k)->oop_is_instance())
1954 return 0;
1955 return instanceKlass::cast(k)->constants()->length();
1956 JVM_END
1959 JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
1960 JVMWrapper("JVM_GetClassFieldsCount");
1961 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1962 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1963 if (!Klass::cast(k)->oop_is_instance())
1964 return 0;
1965 return instanceKlass::cast(k)->fields()->length() / instanceKlass::next_offset;
1966 JVM_END
1969 JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
1970 JVMWrapper("JVM_GetClassMethodsCount");
1971 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1972 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1973 if (!Klass::cast(k)->oop_is_instance())
1974 return 0;
1975 return instanceKlass::cast(k)->methods()->length();
1976 JVM_END
1979 // The following methods, used for the verifier, are never called with
1980 // array klasses, so a direct cast to instanceKlass is safe.
1981 // Typically, these methods are called in a loop with bounds determined
1982 // by the results of JVM_GetClass{Fields,Methods}Count, which return
1983 // zero for arrays.
1984 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
1985 JVMWrapper("JVM_GetMethodIxExceptionIndexes");
1986 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
1987 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
1988 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
1989 int length = methodOop(method)->checked_exceptions_length();
1990 if (length > 0) {
1991 CheckedExceptionElement* table= methodOop(method)->checked_exceptions_start();
1992 for (int i = 0; i < length; i++) {
1993 exceptions[i] = table[i].class_cp_index;
1994 }
1995 }
1996 JVM_END
1999 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
2000 JVMWrapper("JVM_GetMethodIxExceptionsCount");
2001 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2002 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2003 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2004 return methodOop(method)->checked_exceptions_length();
2005 JVM_END
2008 JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
2009 JVMWrapper("JVM_GetMethodIxByteCode");
2010 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2011 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2012 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2013 memcpy(code, methodOop(method)->code_base(), methodOop(method)->code_size());
2014 JVM_END
2017 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
2018 JVMWrapper("JVM_GetMethodIxByteCodeLength");
2019 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2020 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2021 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2022 return methodOop(method)->code_size();
2023 JVM_END
2026 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
2027 JVMWrapper("JVM_GetMethodIxExceptionTableEntry");
2028 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2029 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2030 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2031 typeArrayOop extable = methodOop(method)->exception_table();
2032 entry->start_pc = extable->int_at(entry_index * 4);
2033 entry->end_pc = extable->int_at(entry_index * 4 + 1);
2034 entry->handler_pc = extable->int_at(entry_index * 4 + 2);
2035 entry->catchType = extable->int_at(entry_index * 4 + 3);
2036 JVM_END
2039 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
2040 JVMWrapper("JVM_GetMethodIxExceptionTableLength");
2041 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2042 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2043 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2044 return methodOop(method)->exception_table()->length() / 4;
2045 JVM_END
2048 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
2049 JVMWrapper("JVM_GetMethodIxModifiers");
2050 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2051 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2052 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2053 return methodOop(method)->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2054 JVM_END
2057 JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
2058 JVMWrapper("JVM_GetFieldIxModifiers");
2059 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2060 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2061 typeArrayOop fields = instanceKlass::cast(k)->fields();
2062 return fields->ushort_at(field_index * instanceKlass::next_offset + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2063 JVM_END
2066 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
2067 JVMWrapper("JVM_GetMethodIxLocalsCount");
2068 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2069 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2070 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2071 return methodOop(method)->max_locals();
2072 JVM_END
2075 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
2076 JVMWrapper("JVM_GetMethodIxArgsSize");
2077 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2078 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2079 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2080 return methodOop(method)->size_of_parameters();
2081 JVM_END
2084 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
2085 JVMWrapper("JVM_GetMethodIxMaxStack");
2086 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2087 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2088 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2089 return methodOop(method)->max_stack();
2090 JVM_END
2093 JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
2094 JVMWrapper("JVM_IsConstructorIx");
2095 ResourceMark rm(THREAD);
2096 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2097 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2098 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2099 return methodOop(method)->name() == vmSymbols::object_initializer_name();
2100 JVM_END
2103 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
2104 JVMWrapper("JVM_GetMethodIxIxUTF");
2105 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2106 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2107 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2108 return methodOop(method)->name()->as_utf8();
2109 JVM_END
2112 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
2113 JVMWrapper("JVM_GetMethodIxSignatureUTF");
2114 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2115 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2116 oop method = instanceKlass::cast(k)->methods()->obj_at(method_index);
2117 return methodOop(method)->signature()->as_utf8();
2118 JVM_END
2120 /**
2121 * All of these JVM_GetCP-xxx methods are used by the old verifier to
2122 * read entries in the constant pool. Since the old verifier always
2123 * works on a copy of the code, it will not see any rewriting that
2124 * may possibly occur in the middle of verification. So it is important
2125 * that nothing it calls tries to use the cpCache instead of the raw
2126 * constant pool, so we must use cp->uncached_x methods when appropriate.
2127 */
2128 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2129 JVMWrapper("JVM_GetCPFieldNameUTF");
2130 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2131 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2132 constantPoolOop cp = instanceKlass::cast(k)->constants();
2133 switch (cp->tag_at(cp_index).value()) {
2134 case JVM_CONSTANT_Fieldref:
2135 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2136 default:
2137 fatal("JVM_GetCPFieldNameUTF: illegal constant");
2138 }
2139 ShouldNotReachHere();
2140 return NULL;
2141 JVM_END
2144 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2145 JVMWrapper("JVM_GetCPMethodNameUTF");
2146 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2147 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2148 constantPoolOop cp = instanceKlass::cast(k)->constants();
2149 switch (cp->tag_at(cp_index).value()) {
2150 case JVM_CONSTANT_InterfaceMethodref:
2151 case JVM_CONSTANT_Methodref:
2152 return cp->uncached_name_ref_at(cp_index)->as_utf8();
2153 default:
2154 fatal("JVM_GetCPMethodNameUTF: illegal constant");
2155 }
2156 ShouldNotReachHere();
2157 return NULL;
2158 JVM_END
2161 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2162 JVMWrapper("JVM_GetCPMethodSignatureUTF");
2163 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2164 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2165 constantPoolOop cp = instanceKlass::cast(k)->constants();
2166 switch (cp->tag_at(cp_index).value()) {
2167 case JVM_CONSTANT_InterfaceMethodref:
2168 case JVM_CONSTANT_Methodref:
2169 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2170 default:
2171 fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
2172 }
2173 ShouldNotReachHere();
2174 return NULL;
2175 JVM_END
2178 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
2179 JVMWrapper("JVM_GetCPFieldSignatureUTF");
2180 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2181 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2182 constantPoolOop cp = instanceKlass::cast(k)->constants();
2183 switch (cp->tag_at(cp_index).value()) {
2184 case JVM_CONSTANT_Fieldref:
2185 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
2186 default:
2187 fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
2188 }
2189 ShouldNotReachHere();
2190 return NULL;
2191 JVM_END
2194 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2195 JVMWrapper("JVM_GetCPClassNameUTF");
2196 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2197 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2198 constantPoolOop cp = instanceKlass::cast(k)->constants();
2199 symbolOop classname = cp->klass_name_at(cp_index);
2200 return classname->as_utf8();
2201 JVM_END
2204 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2205 JVMWrapper("JVM_GetCPFieldClassNameUTF");
2206 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2207 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2208 constantPoolOop cp = instanceKlass::cast(k)->constants();
2209 switch (cp->tag_at(cp_index).value()) {
2210 case JVM_CONSTANT_Fieldref: {
2211 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2212 symbolOop classname = cp->klass_name_at(class_index);
2213 return classname->as_utf8();
2214 }
2215 default:
2216 fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
2217 }
2218 ShouldNotReachHere();
2219 return NULL;
2220 JVM_END
2223 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
2224 JVMWrapper("JVM_GetCPMethodClassNameUTF");
2225 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2226 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2227 constantPoolOop cp = instanceKlass::cast(k)->constants();
2228 switch (cp->tag_at(cp_index).value()) {
2229 case JVM_CONSTANT_Methodref:
2230 case JVM_CONSTANT_InterfaceMethodref: {
2231 int class_index = cp->uncached_klass_ref_index_at(cp_index);
2232 symbolOop classname = cp->klass_name_at(class_index);
2233 return classname->as_utf8();
2234 }
2235 default:
2236 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
2237 }
2238 ShouldNotReachHere();
2239 return NULL;
2240 JVM_END
2243 JVM_QUICK_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2244 JVMWrapper("JVM_GetCPFieldModifiers");
2245 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2246 klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
2247 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2248 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2249 constantPoolOop cp = instanceKlass::cast(k)->constants();
2250 constantPoolOop cp_called = instanceKlass::cast(k_called)->constants();
2251 switch (cp->tag_at(cp_index).value()) {
2252 case JVM_CONSTANT_Fieldref: {
2253 symbolOop name = cp->uncached_name_ref_at(cp_index);
2254 symbolOop signature = cp->uncached_signature_ref_at(cp_index);
2255 typeArrayOop fields = instanceKlass::cast(k_called)->fields();
2256 int fields_count = fields->length();
2257 for (int i = 0; i < fields_count; i += instanceKlass::next_offset) {
2258 if (cp_called->symbol_at(fields->ushort_at(i + instanceKlass::name_index_offset)) == name &&
2259 cp_called->symbol_at(fields->ushort_at(i + instanceKlass::signature_index_offset)) == signature) {
2260 return fields->ushort_at(i + instanceKlass::access_flags_offset) & JVM_RECOGNIZED_FIELD_MODIFIERS;
2261 }
2262 }
2263 return -1;
2264 }
2265 default:
2266 fatal("JVM_GetCPFieldModifiers: illegal constant");
2267 }
2268 ShouldNotReachHere();
2269 return 0;
2270 JVM_END
2273 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
2274 JVMWrapper("JVM_GetCPMethodModifiers");
2275 klassOop k = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls));
2276 klassOop k_called = java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(called_cls));
2277 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
2278 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
2279 constantPoolOop cp = instanceKlass::cast(k)->constants();
2280 switch (cp->tag_at(cp_index).value()) {
2281 case JVM_CONSTANT_Methodref:
2282 case JVM_CONSTANT_InterfaceMethodref: {
2283 symbolOop name = cp->uncached_name_ref_at(cp_index);
2284 symbolOop signature = cp->uncached_signature_ref_at(cp_index);
2285 objArrayOop methods = instanceKlass::cast(k_called)->methods();
2286 int methods_count = methods->length();
2287 for (int i = 0; i < methods_count; i++) {
2288 methodOop method = methodOop(methods->obj_at(i));
2289 if (method->name() == name && method->signature() == signature) {
2290 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
2291 }
2292 }
2293 return -1;
2294 }
2295 default:
2296 fatal("JVM_GetCPMethodModifiers: illegal constant");
2297 }
2298 ShouldNotReachHere();
2299 return 0;
2300 JVM_END
2303 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
2305 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
2306 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
2307 JVM_END
2310 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
2311 JVMWrapper("JVM_IsSameClassPackage");
2312 oop class1_mirror = JNIHandles::resolve_non_null(class1);
2313 oop class2_mirror = JNIHandles::resolve_non_null(class2);
2314 klassOop klass1 = java_lang_Class::as_klassOop(class1_mirror);
2315 klassOop klass2 = java_lang_Class::as_klassOop(class2_mirror);
2316 return (jboolean) Reflection::is_same_class_package(klass1, klass2);
2317 JVM_END
2320 // IO functions ////////////////////////////////////////////////////////////////////////////////////////
2322 JVM_LEAF(jint, JVM_Open(const char *fname, jint flags, jint mode))
2323 JVMWrapper2("JVM_Open (%s)", fname);
2325 //%note jvm_r6
2326 int result = hpi::open(fname, flags, mode);
2327 if (result >= 0) {
2328 return result;
2329 } else {
2330 switch(errno) {
2331 case EEXIST:
2332 return JVM_EEXIST;
2333 default:
2334 return -1;
2335 }
2336 }
2337 JVM_END
2340 JVM_LEAF(jint, JVM_Close(jint fd))
2341 JVMWrapper2("JVM_Close (0x%x)", fd);
2342 //%note jvm_r6
2343 return hpi::close(fd);
2344 JVM_END
2347 JVM_LEAF(jint, JVM_Read(jint fd, char *buf, jint nbytes))
2348 JVMWrapper2("JVM_Read (0x%x)", fd);
2350 //%note jvm_r6
2351 return (jint)hpi::read(fd, buf, nbytes);
2352 JVM_END
2355 JVM_LEAF(jint, JVM_Write(jint fd, char *buf, jint nbytes))
2356 JVMWrapper2("JVM_Write (0x%x)", fd);
2358 //%note jvm_r6
2359 return (jint)hpi::write(fd, buf, nbytes);
2360 JVM_END
2363 JVM_LEAF(jint, JVM_Available(jint fd, jlong *pbytes))
2364 JVMWrapper2("JVM_Available (0x%x)", fd);
2365 //%note jvm_r6
2366 return hpi::available(fd, pbytes);
2367 JVM_END
2370 JVM_LEAF(jlong, JVM_Lseek(jint fd, jlong offset, jint whence))
2371 JVMWrapper4("JVM_Lseek (0x%x, %Ld, %d)", fd, offset, whence);
2372 //%note jvm_r6
2373 return hpi::lseek(fd, offset, whence);
2374 JVM_END
2377 JVM_LEAF(jint, JVM_SetLength(jint fd, jlong length))
2378 JVMWrapper3("JVM_SetLength (0x%x, %Ld)", fd, length);
2379 return hpi::ftruncate(fd, length);
2380 JVM_END
2383 JVM_LEAF(jint, JVM_Sync(jint fd))
2384 JVMWrapper2("JVM_Sync (0x%x)", fd);
2385 //%note jvm_r6
2386 return hpi::fsync(fd);
2387 JVM_END
2390 // Printing support //////////////////////////////////////////////////
2391 extern "C" {
2393 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
2394 // see bug 4399518, 4417214
2395 if ((intptr_t)count <= 0) return -1;
2396 return vsnprintf(str, count, fmt, args);
2397 }
2400 int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
2401 va_list args;
2402 int len;
2403 va_start(args, fmt);
2404 len = jio_vsnprintf(str, count, fmt, args);
2405 va_end(args);
2406 return len;
2407 }
2410 int jio_fprintf(FILE* f, const char *fmt, ...) {
2411 int len;
2412 va_list args;
2413 va_start(args, fmt);
2414 len = jio_vfprintf(f, fmt, args);
2415 va_end(args);
2416 return len;
2417 }
2420 int jio_vfprintf(FILE* f, const char *fmt, va_list args) {
2421 if (Arguments::vfprintf_hook() != NULL) {
2422 return Arguments::vfprintf_hook()(f, fmt, args);
2423 } else {
2424 return vfprintf(f, fmt, args);
2425 }
2426 }
2429 int jio_printf(const char *fmt, ...) {
2430 int len;
2431 va_list args;
2432 va_start(args, fmt);
2433 len = jio_vfprintf(defaultStream::output_stream(), fmt, args);
2434 va_end(args);
2435 return len;
2436 }
2439 // HotSpot specific jio method
2440 void jio_print(const char* s) {
2441 // Try to make this function as atomic as possible.
2442 if (Arguments::vfprintf_hook() != NULL) {
2443 jio_fprintf(defaultStream::output_stream(), "%s", s);
2444 } else {
2445 ::write(defaultStream::output_fd(), s, (int)strlen(s));
2446 }
2447 }
2449 } // Extern C
2451 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
2453 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
2454 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
2455 // OSThread objects. The exception to this rule is when the target object is the thread
2456 // doing the operation, in which case we know that the thread won't exit until the
2457 // operation is done (all exits being voluntary). There are a few cases where it is
2458 // rather silly to do operations on yourself, like resuming yourself or asking whether
2459 // you are alive. While these can still happen, they are not subject to deadlocks if
2460 // the lock is held while the operation occurs (this is not the case for suspend, for
2461 // instance), and are very unlikely. Because IsAlive needs to be fast and its
2462 // implementation is local to this file, we always lock Threads_lock for that one.
2464 static void thread_entry(JavaThread* thread, TRAPS) {
2465 HandleMark hm(THREAD);
2466 Handle obj(THREAD, thread->threadObj());
2467 JavaValue result(T_VOID);
2468 JavaCalls::call_virtual(&result,
2469 obj,
2470 KlassHandle(THREAD, SystemDictionary::thread_klass()),
2471 vmSymbolHandles::run_method_name(),
2472 vmSymbolHandles::void_method_signature(),
2473 THREAD);
2474 }
2477 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
2478 JVMWrapper("JVM_StartThread");
2479 JavaThread *native_thread = NULL;
2481 // We cannot hold the Threads_lock when we throw an exception,
2482 // due to rank ordering issues. Example: we might need to grab the
2483 // Heap_lock while we construct the exception.
2484 bool throw_illegal_thread_state = false;
2486 // We must release the Threads_lock before we can post a jvmti event
2487 // in Thread::start.
2488 {
2489 // Ensure that the C++ Thread and OSThread structures aren't freed before
2490 // we operate.
2491 MutexLocker mu(Threads_lock);
2493 // Check to see if we're running a thread that's already exited or was
2494 // stopped (is_stillborn) or is still active (thread is not NULL).
2495 if (java_lang_Thread::is_stillborn(JNIHandles::resolve_non_null(jthread)) ||
2496 java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
2497 throw_illegal_thread_state = true;
2498 } else {
2499 jlong size =
2500 java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
2501 // Allocate the C++ Thread structure and create the native thread. The
2502 // stack size retrieved from java is signed, but the constructor takes
2503 // size_t (an unsigned type), so avoid passing negative values which would
2504 // result in really large stacks.
2505 size_t sz = size > 0 ? (size_t) size : 0;
2506 native_thread = new JavaThread(&thread_entry, sz);
2508 // At this point it may be possible that no osthread was created for the
2509 // JavaThread due to lack of memory. Check for this situation and throw
2510 // an exception if necessary. Eventually we may want to change this so
2511 // that we only grab the lock if the thread was created successfully -
2512 // then we can also do this check and throw the exception in the
2513 // JavaThread constructor.
2514 if (native_thread->osthread() != NULL) {
2515 // Note: the current thread is not being used within "prepare".
2516 native_thread->prepare(jthread);
2517 }
2518 }
2519 }
2521 if (throw_illegal_thread_state) {
2522 THROW(vmSymbols::java_lang_IllegalThreadStateException());
2523 }
2525 assert(native_thread != NULL, "Starting null thread?");
2527 if (native_thread->osthread() == NULL) {
2528 // No one should hold a reference to the 'native_thread'.
2529 delete native_thread;
2530 if (JvmtiExport::should_post_resource_exhausted()) {
2531 JvmtiExport::post_resource_exhausted(
2532 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
2533 "unable to create new native thread");
2534 }
2535 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
2536 "unable to create new native thread");
2537 }
2539 Thread::start(native_thread);
2541 JVM_END
2543 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
2544 // before the quasi-asynchronous exception is delivered. This is a little obtrusive,
2545 // but is thought to be reliable and simple. In the case, where the receiver is the
2546 // save thread as the sender, no safepoint is needed.
2547 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
2548 JVMWrapper("JVM_StopThread");
2550 oop java_throwable = JNIHandles::resolve(throwable);
2551 if (java_throwable == NULL) {
2552 THROW(vmSymbols::java_lang_NullPointerException());
2553 }
2554 oop java_thread = JNIHandles::resolve_non_null(jthread);
2555 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2556 Events::log("JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]", receiver, (address)java_thread, throwable);
2557 // First check if thread already exited
2558 if (receiver != NULL) {
2559 // Check if exception is getting thrown at self (use oop equality, since the
2560 // target object might exit)
2561 if (java_thread == thread->threadObj()) {
2562 // This is a change from JDK 1.1, but JDK 1.2 will also do it:
2563 // NOTE (from JDK 1.2): this is done solely to prevent stopped
2564 // threads from being restarted.
2565 // Fix for 4314342, 4145910, perhaps others: it now doesn't have
2566 // any effect on the "liveness" of a thread; see
2567 // JVM_IsThreadAlive, below.
2568 if (java_throwable->is_a(SystemDictionary::threaddeath_klass())) {
2569 java_lang_Thread::set_stillborn(java_thread);
2570 }
2571 THROW_OOP(java_throwable);
2572 } else {
2573 // Enques a VM_Operation to stop all threads and then deliver the exception...
2574 Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable));
2575 }
2576 }
2577 JVM_END
2580 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
2581 JVMWrapper("JVM_IsThreadAlive");
2583 oop thread_oop = JNIHandles::resolve_non_null(jthread);
2584 return java_lang_Thread::is_alive(thread_oop);
2585 JVM_END
2588 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
2589 JVMWrapper("JVM_SuspendThread");
2590 oop java_thread = JNIHandles::resolve_non_null(jthread);
2591 JavaThread* receiver = java_lang_Thread::thread(java_thread);
2593 if (receiver != NULL) {
2594 // thread has run and has not exited (still on threads list)
2596 {
2597 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
2598 if (receiver->is_external_suspend()) {
2599 // Don't allow nested external suspend requests. We can't return
2600 // an error from this interface so just ignore the problem.
2601 return;
2602 }
2603 if (receiver->is_exiting()) { // thread is in the process of exiting
2604 return;
2605 }
2606 receiver->set_external_suspend();
2607 }
2609 // java_suspend() will catch threads in the process of exiting
2610 // and will ignore them.
2611 receiver->java_suspend();
2613 // It would be nice to have the following assertion in all the
2614 // time, but it is possible for a racing resume request to have
2615 // resumed this thread right after we suspended it. Temporarily
2616 // enable this assertion if you are chasing a different kind of
2617 // bug.
2618 //
2619 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
2620 // receiver->is_being_ext_suspended(), "thread is not suspended");
2621 }
2622 JVM_END
2625 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
2626 JVMWrapper("JVM_ResumeThread");
2627 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate.
2628 // We need to *always* get the threads lock here, since this operation cannot be allowed during
2629 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
2630 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
2631 // looks at it.
2632 MutexLocker ml(Threads_lock);
2633 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2634 if (thr != NULL) {
2635 // the thread has run and is not in the process of exiting
2636 thr->java_resume();
2637 }
2638 JVM_END
2641 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
2642 JVMWrapper("JVM_SetThreadPriority");
2643 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2644 MutexLocker ml(Threads_lock);
2645 oop java_thread = JNIHandles::resolve_non_null(jthread);
2646 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
2647 JavaThread* thr = java_lang_Thread::thread(java_thread);
2648 if (thr != NULL) { // Thread not yet started; priority pushed down when it is
2649 Thread::set_priority(thr, (ThreadPriority)prio);
2650 }
2651 JVM_END
2654 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
2655 JVMWrapper("JVM_Yield");
2656 if (os::dont_yield()) return;
2657 // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield.
2658 // Critical for similar threading behaviour
2659 if (ConvertYieldToSleep) {
2660 os::sleep(thread, MinSleepInterval, false);
2661 } else {
2662 os::yield();
2663 }
2664 JVM_END
2667 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
2668 JVMWrapper("JVM_Sleep");
2670 if (millis < 0) {
2671 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
2672 }
2674 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
2675 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2676 }
2678 // Save current thread state and restore it at the end of this block.
2679 // And set new thread state to SLEEPING.
2680 JavaThreadSleepState jtss(thread);
2682 if (millis == 0) {
2683 // When ConvertSleepToYield is on, this matches the classic VM implementation of
2684 // JVM_Sleep. Critical for similar threading behaviour (Win32)
2685 // It appears that in certain GUI contexts, it may be beneficial to do a short sleep
2686 // for SOLARIS
2687 if (ConvertSleepToYield) {
2688 os::yield();
2689 } else {
2690 ThreadState old_state = thread->osthread()->get_state();
2691 thread->osthread()->set_state(SLEEPING);
2692 os::sleep(thread, MinSleepInterval, false);
2693 thread->osthread()->set_state(old_state);
2694 }
2695 } else {
2696 ThreadState old_state = thread->osthread()->get_state();
2697 thread->osthread()->set_state(SLEEPING);
2698 if (os::sleep(thread, millis, true) == OS_INTRPT) {
2699 // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
2700 // us while we were sleeping. We do not overwrite those.
2701 if (!HAS_PENDING_EXCEPTION) {
2702 // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
2703 // to properly restore the thread state. That's likely wrong.
2704 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
2705 }
2706 }
2707 thread->osthread()->set_state(old_state);
2708 }
2709 JVM_END
2711 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
2712 JVMWrapper("JVM_CurrentThread");
2713 oop jthread = thread->threadObj();
2714 assert (thread != NULL, "no current thread!");
2715 return JNIHandles::make_local(env, jthread);
2716 JVM_END
2719 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
2720 JVMWrapper("JVM_CountStackFrames");
2722 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2723 oop java_thread = JNIHandles::resolve_non_null(jthread);
2724 bool throw_illegal_thread_state = false;
2725 int count = 0;
2727 {
2728 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2729 // We need to re-resolve the java_thread, since a GC might have happened during the
2730 // acquire of the lock
2731 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2733 if (thr == NULL) {
2734 // do nothing
2735 } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) {
2736 // Check whether this java thread has been suspended already. If not, throws
2737 // IllegalThreadStateException. We defer to throw that exception until
2738 // Threads_lock is released since loading exception class has to leave VM.
2739 // The correct way to test a thread is actually suspended is
2740 // wait_for_ext_suspend_completion(), but we can't call that while holding
2741 // the Threads_lock. The above tests are sufficient for our purposes
2742 // provided the walkability of the stack is stable - which it isn't
2743 // 100% but close enough for most practical purposes.
2744 throw_illegal_thread_state = true;
2745 } else {
2746 // Count all java activation, i.e., number of vframes
2747 for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) {
2748 // Native frames are not counted
2749 if (!vfst.method()->is_native()) count++;
2750 }
2751 }
2752 }
2754 if (throw_illegal_thread_state) {
2755 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
2756 "this thread is not suspended");
2757 }
2758 return count;
2759 JVM_END
2761 // Consider: A better way to implement JVM_Interrupt() is to acquire
2762 // Threads_lock to resolve the jthread into a Thread pointer, fetch
2763 // Thread->platformevent, Thread->native_thr, Thread->parker, etc.,
2764 // drop Threads_lock, and the perform the unpark() and thr_kill() operations
2765 // outside the critical section. Threads_lock is hot so we want to minimize
2766 // the hold-time. A cleaner interface would be to decompose interrupt into
2767 // two steps. The 1st phase, performed under Threads_lock, would return
2768 // a closure that'd be invoked after Threads_lock was dropped.
2769 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and
2770 // admit spurious wakeups.
2772 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
2773 JVMWrapper("JVM_Interrupt");
2775 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2776 oop java_thread = JNIHandles::resolve_non_null(jthread);
2777 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2778 // We need to re-resolve the java_thread, since a GC might have happened during the
2779 // acquire of the lock
2780 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2781 if (thr != NULL) {
2782 Thread::interrupt(thr);
2783 }
2784 JVM_END
2787 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
2788 JVMWrapper("JVM_IsInterrupted");
2790 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
2791 oop java_thread = JNIHandles::resolve_non_null(jthread);
2792 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
2793 // We need to re-resolve the java_thread, since a GC might have happened during the
2794 // acquire of the lock
2795 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
2796 if (thr == NULL) {
2797 return JNI_FALSE;
2798 } else {
2799 return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0);
2800 }
2801 JVM_END
2804 // Return true iff the current thread has locked the object passed in
2806 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
2807 JVMWrapper("JVM_HoldsLock");
2808 assert(THREAD->is_Java_thread(), "sanity check");
2809 if (obj == NULL) {
2810 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
2811 }
2812 Handle h_obj(THREAD, JNIHandles::resolve(obj));
2813 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
2814 JVM_END
2817 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
2818 JVMWrapper("JVM_DumpAllStacks");
2819 VM_PrintThreads op;
2820 VMThread::execute(&op);
2821 if (JvmtiExport::should_post_data_dump()) {
2822 JvmtiExport::post_data_dump();
2823 }
2824 JVM_END
2827 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
2829 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
2830 assert(jthread->is_Java_thread(), "must be a Java thread");
2831 if (jthread->privileged_stack_top() == NULL) return false;
2832 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
2833 oop loader = jthread->privileged_stack_top()->class_loader();
2834 if (loader == NULL) return true;
2835 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
2836 if (trusted) return true;
2837 }
2838 return false;
2839 }
2841 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
2842 JVMWrapper("JVM_CurrentLoadedClass");
2843 ResourceMark rm(THREAD);
2845 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2846 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
2847 bool trusted = is_trusted_frame(thread, &vfst);
2848 if (trusted) return NULL;
2850 methodOop m = vfst.method();
2851 if (!m->is_native()) {
2852 klassOop holder = m->method_holder();
2853 oop loader = instanceKlass::cast(holder)->class_loader();
2854 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
2855 return (jclass) JNIHandles::make_local(env, Klass::cast(holder)->java_mirror());
2856 }
2857 }
2858 }
2859 return NULL;
2860 JVM_END
2863 JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env))
2864 JVMWrapper("JVM_CurrentClassLoader");
2865 ResourceMark rm(THREAD);
2867 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2869 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
2870 bool trusted = is_trusted_frame(thread, &vfst);
2871 if (trusted) return NULL;
2873 methodOop m = vfst.method();
2874 if (!m->is_native()) {
2875 klassOop holder = m->method_holder();
2876 assert(holder->is_klass(), "just checking");
2877 oop loader = instanceKlass::cast(holder)->class_loader();
2878 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
2879 return JNIHandles::make_local(env, loader);
2880 }
2881 }
2882 }
2883 return NULL;
2884 JVM_END
2887 // Utility object for collecting method holders walking down the stack
2888 class KlassLink: public ResourceObj {
2889 public:
2890 KlassHandle klass;
2891 KlassLink* next;
2893 KlassLink(KlassHandle k) { klass = k; next = NULL; }
2894 };
2897 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
2898 JVMWrapper("JVM_GetClassContext");
2899 ResourceMark rm(THREAD);
2900 JvmtiVMObjectAllocEventCollector oam;
2901 // Collect linked list of (handles to) method holders
2902 KlassLink* first = NULL;
2903 KlassLink* last = NULL;
2904 int depth = 0;
2906 for(vframeStream vfst(thread); !vfst.at_end(); vfst.security_get_caller_frame(1)) {
2907 // Native frames are not returned
2908 if (!vfst.method()->is_native()) {
2909 klassOop holder = vfst.method()->method_holder();
2910 assert(holder->is_klass(), "just checking");
2911 depth++;
2912 KlassLink* l = new KlassLink(KlassHandle(thread, holder));
2913 if (first == NULL) {
2914 first = last = l;
2915 } else {
2916 last->next = l;
2917 last = l;
2918 }
2919 }
2920 }
2922 // Create result array of type [Ljava/lang/Class;
2923 objArrayOop result = oopFactory::new_objArray(SystemDictionary::class_klass(), depth, CHECK_NULL);
2924 // Fill in mirrors corresponding to method holders
2925 int index = 0;
2926 while (first != NULL) {
2927 result->obj_at_put(index++, Klass::cast(first->klass())->java_mirror());
2928 first = first->next;
2929 }
2930 assert(index == depth, "just checking");
2932 return (jobjectArray) JNIHandles::make_local(env, result);
2933 JVM_END
2936 JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name))
2937 JVMWrapper("JVM_ClassDepth");
2938 ResourceMark rm(THREAD);
2939 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
2940 Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0);
2942 const char* str = java_lang_String::as_utf8_string(class_name_str());
2943 symbolHandle class_name_sym =
2944 symbolHandle(THREAD, SymbolTable::probe(str, (int)strlen(str)));
2945 if (class_name_sym.is_null()) {
2946 return -1;
2947 }
2949 int depth = 0;
2951 for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2952 if (!vfst.method()->is_native()) {
2953 klassOop holder = vfst.method()->method_holder();
2954 assert(holder->is_klass(), "just checking");
2955 if (instanceKlass::cast(holder)->name() == class_name_sym()) {
2956 return depth;
2957 }
2958 depth++;
2959 }
2960 }
2961 return -1;
2962 JVM_END
2965 JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env))
2966 JVMWrapper("JVM_ClassLoaderDepth");
2967 ResourceMark rm(THREAD);
2968 int depth = 0;
2969 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
2970 // if a method in a class in a trusted loader is in a doPrivileged, return -1
2971 bool trusted = is_trusted_frame(thread, &vfst);
2972 if (trusted) return -1;
2974 methodOop m = vfst.method();
2975 if (!m->is_native()) {
2976 klassOop holder = m->method_holder();
2977 assert(holder->is_klass(), "just checking");
2978 oop loader = instanceKlass::cast(holder)->class_loader();
2979 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
2980 return depth;
2981 }
2982 depth++;
2983 }
2984 }
2985 return -1;
2986 JVM_END
2989 // java.lang.Package ////////////////////////////////////////////////////////////////
2992 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
2993 JVMWrapper("JVM_GetSystemPackage");
2994 ResourceMark rm(THREAD);
2995 JvmtiVMObjectAllocEventCollector oam;
2996 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
2997 oop result = ClassLoader::get_system_package(str, CHECK_NULL);
2998 return (jstring) JNIHandles::make_local(result);
2999 JVM_END
3002 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
3003 JVMWrapper("JVM_GetSystemPackages");
3004 JvmtiVMObjectAllocEventCollector oam;
3005 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
3006 return (jobjectArray) JNIHandles::make_local(result);
3007 JVM_END
3010 // ObjectInputStream ///////////////////////////////////////////////////////////////
3012 bool force_verify_field_access(klassOop current_class, klassOop field_class, AccessFlags access, bool classloader_only) {
3013 if (current_class == NULL) {
3014 return true;
3015 }
3016 if ((current_class == field_class) || access.is_public()) {
3017 return true;
3018 }
3020 if (access.is_protected()) {
3021 // See if current_class is a subclass of field_class
3022 if (Klass::cast(current_class)->is_subclass_of(field_class)) {
3023 return true;
3024 }
3025 }
3027 return (!access.is_private() && instanceKlass::cast(current_class)->is_same_class_package(field_class));
3028 }
3031 // JVM_AllocateNewObject and JVM_AllocateNewArray are unused as of 1.4
3032 JVM_ENTRY(jobject, JVM_AllocateNewObject(JNIEnv *env, jobject receiver, jclass currClass, jclass initClass))
3033 JVMWrapper("JVM_AllocateNewObject");
3034 JvmtiVMObjectAllocEventCollector oam;
3035 // Receiver is not used
3036 oop curr_mirror = JNIHandles::resolve_non_null(currClass);
3037 oop init_mirror = JNIHandles::resolve_non_null(initClass);
3039 // Cannot instantiate primitive types
3040 if (java_lang_Class::is_primitive(curr_mirror) || java_lang_Class::is_primitive(init_mirror)) {
3041 ResourceMark rm(THREAD);
3042 THROW_0(vmSymbols::java_lang_InvalidClassException());
3043 }
3045 // Arrays not allowed here, must use JVM_AllocateNewArray
3046 if (Klass::cast(java_lang_Class::as_klassOop(curr_mirror))->oop_is_javaArray() ||
3047 Klass::cast(java_lang_Class::as_klassOop(init_mirror))->oop_is_javaArray()) {
3048 ResourceMark rm(THREAD);
3049 THROW_0(vmSymbols::java_lang_InvalidClassException());
3050 }
3052 instanceKlassHandle curr_klass (THREAD, java_lang_Class::as_klassOop(curr_mirror));
3053 instanceKlassHandle init_klass (THREAD, java_lang_Class::as_klassOop(init_mirror));
3055 assert(curr_klass->is_subclass_of(init_klass()), "just checking");
3057 // Interfaces, abstract classes, and java.lang.Class classes cannot be instantiated directly.
3058 curr_klass->check_valid_for_instantiation(false, CHECK_NULL);
3060 // Make sure klass is initialized, since we are about to instantiate one of them.
3061 curr_klass->initialize(CHECK_NULL);
3063 methodHandle m (THREAD,
3064 init_klass->find_method(vmSymbols::object_initializer_name(),
3065 vmSymbols::void_method_signature()));
3066 if (m.is_null()) {
3067 ResourceMark rm(THREAD);
3068 THROW_MSG_0(vmSymbols::java_lang_NoSuchMethodError(),
3069 methodOopDesc::name_and_sig_as_C_string(Klass::cast(init_klass()),
3070 vmSymbols::object_initializer_name(),
3071 vmSymbols::void_method_signature()));
3072 }
3074 if (curr_klass == init_klass && !m->is_public()) {
3075 // Calling the constructor for class 'curr_klass'.
3076 // Only allow calls to a public no-arg constructor.
3077 // This path corresponds to creating an Externalizable object.
3078 THROW_0(vmSymbols::java_lang_IllegalAccessException());
3079 }
3081 if (!force_verify_field_access(curr_klass(), init_klass(), m->access_flags(), false)) {
3082 // subclass 'curr_klass' does not have access to no-arg constructor of 'initcb'
3083 THROW_0(vmSymbols::java_lang_IllegalAccessException());
3084 }
3086 Handle obj = curr_klass->allocate_instance_handle(CHECK_NULL);
3087 // Call constructor m. This might call a constructor higher up in the hierachy
3088 JavaCalls::call_default_constructor(thread, m, obj, CHECK_NULL);
3090 return JNIHandles::make_local(obj());
3091 JVM_END
3094 JVM_ENTRY(jobject, JVM_AllocateNewArray(JNIEnv *env, jobject obj, jclass currClass, jint length))
3095 JVMWrapper("JVM_AllocateNewArray");
3096 JvmtiVMObjectAllocEventCollector oam;
3097 oop mirror = JNIHandles::resolve_non_null(currClass);
3099 if (java_lang_Class::is_primitive(mirror)) {
3100 THROW_0(vmSymbols::java_lang_InvalidClassException());
3101 }
3102 klassOop k = java_lang_Class::as_klassOop(mirror);
3103 oop result;
3105 if (k->klass_part()->oop_is_typeArray()) {
3106 // typeArray
3107 result = typeArrayKlass::cast(k)->allocate(length, CHECK_NULL);
3108 } else if (k->klass_part()->oop_is_objArray()) {
3109 // objArray
3110 objArrayKlassHandle oak(THREAD, k);
3111 oak->initialize(CHECK_NULL); // make sure class is initialized (matches Classic VM behavior)
3112 result = oak->allocate(length, CHECK_NULL);
3113 } else {
3114 THROW_0(vmSymbols::java_lang_InvalidClassException());
3115 }
3116 return JNIHandles::make_local(env, result);
3117 JVM_END
3120 // Return the first non-null class loader up the execution stack, or null
3121 // if only code from the null class loader is on the stack.
3123 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
3124 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
3125 // UseNewReflection
3126 vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection
3127 klassOop holder = vfst.method()->method_holder();
3128 oop loader = instanceKlass::cast(holder)->class_loader();
3129 if (loader != NULL) {
3130 return JNIHandles::make_local(env, loader);
3131 }
3132 }
3133 return NULL;
3134 JVM_END
3137 // Load a class relative to the most recent class on the stack with a non-null
3138 // classloader.
3139 // This function has been deprecated and should not be considered part of the
3140 // specified JVM interface.
3142 JVM_ENTRY(jclass, JVM_LoadClass0(JNIEnv *env, jobject receiver,
3143 jclass currClass, jstring currClassName))
3144 JVMWrapper("JVM_LoadClass0");
3145 // Receiver is not used
3146 ResourceMark rm(THREAD);
3148 // Class name argument is not guaranteed to be in internal format
3149 Handle classname (THREAD, JNIHandles::resolve_non_null(currClassName));
3150 Handle string = java_lang_String::internalize_classname(classname, CHECK_NULL);
3152 const char* str = java_lang_String::as_utf8_string(string());
3154 if (str == NULL || (int)strlen(str) > symbolOopDesc::max_length()) {
3155 // It's impossible to create this class; the name cannot fit
3156 // into the constant pool.
3157 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), str);
3158 }
3160 symbolHandle name = oopFactory::new_symbol_handle(str, CHECK_NULL);
3161 Handle curr_klass (THREAD, JNIHandles::resolve(currClass));
3162 // Find the most recent class on the stack with a non-null classloader
3163 oop loader = NULL;
3164 oop protection_domain = NULL;
3165 if (curr_klass.is_null()) {
3166 for (vframeStream vfst(thread);
3167 !vfst.at_end() && loader == NULL;
3168 vfst.next()) {
3169 if (!vfst.method()->is_native()) {
3170 klassOop holder = vfst.method()->method_holder();
3171 loader = instanceKlass::cast(holder)->class_loader();
3172 protection_domain = instanceKlass::cast(holder)->protection_domain();
3173 }
3174 }
3175 } else {
3176 klassOop curr_klass_oop = java_lang_Class::as_klassOop(curr_klass());
3177 loader = instanceKlass::cast(curr_klass_oop)->class_loader();
3178 protection_domain = instanceKlass::cast(curr_klass_oop)->protection_domain();
3179 }
3180 Handle h_loader(THREAD, loader);
3181 Handle h_prot (THREAD, protection_domain);
3182 return find_class_from_class_loader(env, name, true, h_loader, h_prot,
3183 false, thread);
3184 JVM_END
3187 // Array ///////////////////////////////////////////////////////////////////////////////////////////
3190 // resolve array handle and check arguments
3191 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
3192 if (arr == NULL) {
3193 THROW_0(vmSymbols::java_lang_NullPointerException());
3194 }
3195 oop a = JNIHandles::resolve_non_null(arr);
3196 if (!a->is_javaArray() || (type_array_only && !a->is_typeArray())) {
3197 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
3198 }
3199 return arrayOop(a);
3200 }
3203 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
3204 JVMWrapper("JVM_GetArrayLength");
3205 arrayOop a = check_array(env, arr, false, CHECK_0);
3206 return a->length();
3207 JVM_END
3210 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
3211 JVMWrapper("JVM_Array_Get");
3212 JvmtiVMObjectAllocEventCollector oam;
3213 arrayOop a = check_array(env, arr, false, CHECK_NULL);
3214 jvalue value;
3215 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
3216 oop box = Reflection::box(&value, type, CHECK_NULL);
3217 return JNIHandles::make_local(env, box);
3218 JVM_END
3221 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
3222 JVMWrapper("JVM_GetPrimitiveArrayElement");
3223 jvalue value;
3224 value.i = 0; // to initialize value before getting used in CHECK
3225 arrayOop a = check_array(env, arr, true, CHECK_(value));
3226 assert(a->is_typeArray(), "just checking");
3227 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
3228 BasicType wide_type = (BasicType) wCode;
3229 if (type != wide_type) {
3230 Reflection::widen(&value, type, wide_type, CHECK_(value));
3231 }
3232 return value;
3233 JVM_END
3236 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
3237 JVMWrapper("JVM_SetArrayElement");
3238 arrayOop a = check_array(env, arr, false, CHECK);
3239 oop box = JNIHandles::resolve(val);
3240 jvalue value;
3241 value.i = 0; // to initialize value before getting used in CHECK
3242 BasicType value_type;
3243 if (a->is_objArray()) {
3244 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
3245 value_type = Reflection::unbox_for_regular_object(box, &value);
3246 } else {
3247 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
3248 }
3249 Reflection::array_set(&value, a, index, value_type, CHECK);
3250 JVM_END
3253 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
3254 JVMWrapper("JVM_SetPrimitiveArrayElement");
3255 arrayOop a = check_array(env, arr, true, CHECK);
3256 assert(a->is_typeArray(), "just checking");
3257 BasicType value_type = (BasicType) vCode;
3258 Reflection::array_set(&v, a, index, value_type, CHECK);
3259 JVM_END
3262 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
3263 JVMWrapper("JVM_NewArray");
3264 JvmtiVMObjectAllocEventCollector oam;
3265 oop element_mirror = JNIHandles::resolve(eltClass);
3266 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
3267 return JNIHandles::make_local(env, result);
3268 JVM_END
3271 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
3272 JVMWrapper("JVM_NewMultiArray");
3273 JvmtiVMObjectAllocEventCollector oam;
3274 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
3275 oop element_mirror = JNIHandles::resolve(eltClass);
3276 assert(dim_array->is_typeArray(), "just checking");
3277 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
3278 return JNIHandles::make_local(env, result);
3279 JVM_END
3282 // Networking library support ////////////////////////////////////////////////////////////////////
3284 JVM_LEAF(jint, JVM_InitializeSocketLibrary())
3285 JVMWrapper("JVM_InitializeSocketLibrary");
3286 return hpi::initialize_socket_library();
3287 JVM_END
3290 JVM_LEAF(jint, JVM_Socket(jint domain, jint type, jint protocol))
3291 JVMWrapper("JVM_Socket");
3292 return hpi::socket(domain, type, protocol);
3293 JVM_END
3296 JVM_LEAF(jint, JVM_SocketClose(jint fd))
3297 JVMWrapper2("JVM_SocketClose (0x%x)", fd);
3298 //%note jvm_r6
3299 return hpi::socket_close(fd);
3300 JVM_END
3303 JVM_LEAF(jint, JVM_SocketShutdown(jint fd, jint howto))
3304 JVMWrapper2("JVM_SocketShutdown (0x%x)", fd);
3305 //%note jvm_r6
3306 return hpi::socket_shutdown(fd, howto);
3307 JVM_END
3310 JVM_LEAF(jint, JVM_Recv(jint fd, char *buf, jint nBytes, jint flags))
3311 JVMWrapper2("JVM_Recv (0x%x)", fd);
3312 //%note jvm_r6
3313 return hpi::recv(fd, buf, nBytes, flags);
3314 JVM_END
3317 JVM_LEAF(jint, JVM_Send(jint fd, char *buf, jint nBytes, jint flags))
3318 JVMWrapper2("JVM_Send (0x%x)", fd);
3319 //%note jvm_r6
3320 return hpi::send(fd, buf, nBytes, flags);
3321 JVM_END
3324 JVM_LEAF(jint, JVM_Timeout(int fd, long timeout))
3325 JVMWrapper2("JVM_Timeout (0x%x)", fd);
3326 //%note jvm_r6
3327 return hpi::timeout(fd, timeout);
3328 JVM_END
3331 JVM_LEAF(jint, JVM_Listen(jint fd, jint count))
3332 JVMWrapper2("JVM_Listen (0x%x)", fd);
3333 //%note jvm_r6
3334 return hpi::listen(fd, count);
3335 JVM_END
3338 JVM_LEAF(jint, JVM_Connect(jint fd, struct sockaddr *him, jint len))
3339 JVMWrapper2("JVM_Connect (0x%x)", fd);
3340 //%note jvm_r6
3341 return hpi::connect(fd, him, len);
3342 JVM_END
3345 JVM_LEAF(jint, JVM_Bind(jint fd, struct sockaddr *him, jint len))
3346 JVMWrapper2("JVM_Bind (0x%x)", fd);
3347 //%note jvm_r6
3348 return hpi::bind(fd, him, len);
3349 JVM_END
3352 JVM_LEAF(jint, JVM_Accept(jint fd, struct sockaddr *him, jint *len))
3353 JVMWrapper2("JVM_Accept (0x%x)", fd);
3354 //%note jvm_r6
3355 return hpi::accept(fd, him, (int *)len);
3356 JVM_END
3359 JVM_LEAF(jint, JVM_RecvFrom(jint fd, char *buf, int nBytes, int flags, struct sockaddr *from, int *fromlen))
3360 JVMWrapper2("JVM_RecvFrom (0x%x)", fd);
3361 //%note jvm_r6
3362 return hpi::recvfrom(fd, buf, nBytes, flags, from, fromlen);
3363 JVM_END
3366 JVM_LEAF(jint, JVM_GetSockName(jint fd, struct sockaddr *him, int *len))
3367 JVMWrapper2("JVM_GetSockName (0x%x)", fd);
3368 //%note jvm_r6
3369 return hpi::get_sock_name(fd, him, len);
3370 JVM_END
3373 JVM_LEAF(jint, JVM_SendTo(jint fd, char *buf, int len, int flags, struct sockaddr *to, int tolen))
3374 JVMWrapper2("JVM_SendTo (0x%x)", fd);
3375 //%note jvm_r6
3376 return hpi::sendto(fd, buf, len, flags, to, tolen);
3377 JVM_END
3380 JVM_LEAF(jint, JVM_SocketAvailable(jint fd, jint *pbytes))
3381 JVMWrapper2("JVM_SocketAvailable (0x%x)", fd);
3382 //%note jvm_r6
3383 return hpi::socket_available(fd, pbytes);
3384 JVM_END
3387 JVM_LEAF(jint, JVM_GetSockOpt(jint fd, int level, int optname, char *optval, int *optlen))
3388 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
3389 //%note jvm_r6
3390 return hpi::get_sock_opt(fd, level, optname, optval, optlen);
3391 JVM_END
3394 JVM_LEAF(jint, JVM_SetSockOpt(jint fd, int level, int optname, const char *optval, int optlen))
3395 JVMWrapper2("JVM_GetSockOpt (0x%x)", fd);
3396 //%note jvm_r6
3397 return hpi::set_sock_opt(fd, level, optname, optval, optlen);
3398 JVM_END
3400 JVM_LEAF(int, JVM_GetHostName(char* name, int namelen))
3401 JVMWrapper("JVM_GetHostName");
3402 return hpi::get_host_name(name, namelen);
3403 JVM_END
3405 #ifdef _WINDOWS
3407 JVM_LEAF(struct hostent*, JVM_GetHostByAddr(const char* name, int len, int type))
3408 JVMWrapper("JVM_GetHostByAddr");
3409 return hpi::get_host_by_addr(name, len, type);
3410 JVM_END
3413 JVM_LEAF(struct hostent*, JVM_GetHostByName(char* name))
3414 JVMWrapper("JVM_GetHostByName");
3415 return hpi::get_host_by_name(name);
3416 JVM_END
3419 JVM_LEAF(struct protoent*, JVM_GetProtoByName(char* name))
3420 JVMWrapper("JVM_GetProtoByName");
3421 return hpi::get_proto_by_name(name);
3422 JVM_END
3424 #endif
3426 // Library support ///////////////////////////////////////////////////////////////////////////
3428 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
3429 //%note jvm_ct
3430 JVMWrapper2("JVM_LoadLibrary (%s)", name);
3431 char ebuf[1024];
3432 void *load_result;
3433 {
3434 ThreadToNativeFromVM ttnfvm(thread);
3435 load_result = hpi::dll_load(name, ebuf, sizeof ebuf);
3436 }
3437 if (load_result == NULL) {
3438 char msg[1024];
3439 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
3440 // Since 'ebuf' may contain a string encoded using
3441 // platform encoding scheme, we need to pass
3442 // Exceptions::unsafe_to_utf8 to the new_exception method
3443 // as the last argument. See bug 6367357.
3444 Handle h_exception =
3445 Exceptions::new_exception(thread,
3446 vmSymbols::java_lang_UnsatisfiedLinkError(),
3447 msg, Exceptions::unsafe_to_utf8);
3449 THROW_HANDLE_0(h_exception);
3450 }
3451 return load_result;
3452 JVM_END
3455 JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
3456 JVMWrapper("JVM_UnloadLibrary");
3457 hpi::dll_unload(handle);
3458 JVM_END
3461 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
3462 JVMWrapper2("JVM_FindLibraryEntry (%s)", name);
3463 return hpi::dll_lookup(handle, name);
3464 JVM_END
3466 // Floating point support ////////////////////////////////////////////////////////////////////
3468 JVM_LEAF(jboolean, JVM_IsNaN(jdouble a))
3469 JVMWrapper("JVM_IsNaN");
3470 return g_isnan(a);
3471 JVM_END
3475 // JNI version ///////////////////////////////////////////////////////////////////////////////
3477 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
3478 JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version);
3479 return Threads::is_supported_jni_version_including_1_1(version);
3480 JVM_END
3483 // String support ///////////////////////////////////////////////////////////////////////////
3485 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
3486 JVMWrapper("JVM_InternString");
3487 JvmtiVMObjectAllocEventCollector oam;
3488 if (str == NULL) return NULL;
3489 oop string = JNIHandles::resolve_non_null(str);
3490 oop result = StringTable::intern(string, CHECK_NULL);
3491 return (jstring) JNIHandles::make_local(env, result);
3492 JVM_END
3495 // Raw monitor support //////////////////////////////////////////////////////////////////////
3497 // The lock routine below calls lock_without_safepoint_check in order to get a raw lock
3498 // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because
3499 // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check
3500 // that only works with java threads.
3503 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
3504 VM_Exit::block_if_vm_exited();
3505 JVMWrapper("JVM_RawMonitorCreate");
3506 return new Mutex(Mutex::native, "JVM_RawMonitorCreate");
3507 }
3510 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) {
3511 VM_Exit::block_if_vm_exited();
3512 JVMWrapper("JVM_RawMonitorDestroy");
3513 delete ((Mutex*) mon);
3514 }
3517 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) {
3518 VM_Exit::block_if_vm_exited();
3519 JVMWrapper("JVM_RawMonitorEnter");
3520 ((Mutex*) mon)->jvm_raw_lock();
3521 return 0;
3522 }
3525 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
3526 VM_Exit::block_if_vm_exited();
3527 JVMWrapper("JVM_RawMonitorExit");
3528 ((Mutex*) mon)->jvm_raw_unlock();
3529 }
3532 // Support for Serialization
3534 typedef jfloat (JNICALL *IntBitsToFloatFn )(JNIEnv* env, jclass cb, jint value);
3535 typedef jdouble (JNICALL *LongBitsToDoubleFn)(JNIEnv* env, jclass cb, jlong value);
3536 typedef jint (JNICALL *FloatToIntBitsFn )(JNIEnv* env, jclass cb, jfloat value);
3537 typedef jlong (JNICALL *DoubleToLongBitsFn)(JNIEnv* env, jclass cb, jdouble value);
3539 static IntBitsToFloatFn int_bits_to_float_fn = NULL;
3540 static LongBitsToDoubleFn long_bits_to_double_fn = NULL;
3541 static FloatToIntBitsFn float_to_int_bits_fn = NULL;
3542 static DoubleToLongBitsFn double_to_long_bits_fn = NULL;
3545 void initialize_converter_functions() {
3546 if (JDK_Version::is_gte_jdk14x_version()) {
3547 // These functions only exist for compatibility with 1.3.1 and earlier
3548 return;
3549 }
3551 // called from universe_post_init()
3552 assert(
3553 int_bits_to_float_fn == NULL &&
3554 long_bits_to_double_fn == NULL &&
3555 float_to_int_bits_fn == NULL &&
3556 double_to_long_bits_fn == NULL ,
3557 "initialization done twice"
3558 );
3559 // initialize
3560 int_bits_to_float_fn = CAST_TO_FN_PTR(IntBitsToFloatFn , NativeLookup::base_library_lookup("java/lang/Float" , "intBitsToFloat" , "(I)F"));
3561 long_bits_to_double_fn = CAST_TO_FN_PTR(LongBitsToDoubleFn, NativeLookup::base_library_lookup("java/lang/Double", "longBitsToDouble", "(J)D"));
3562 float_to_int_bits_fn = CAST_TO_FN_PTR(FloatToIntBitsFn , NativeLookup::base_library_lookup("java/lang/Float" , "floatToIntBits" , "(F)I"));
3563 double_to_long_bits_fn = CAST_TO_FN_PTR(DoubleToLongBitsFn, NativeLookup::base_library_lookup("java/lang/Double", "doubleToLongBits", "(D)J"));
3564 // verify
3565 assert(
3566 int_bits_to_float_fn != NULL &&
3567 long_bits_to_double_fn != NULL &&
3568 float_to_int_bits_fn != NULL &&
3569 double_to_long_bits_fn != NULL ,
3570 "initialization failed"
3571 );
3572 }
3575 // Serialization
3576 JVM_ENTRY(void, JVM_SetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
3577 jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
3578 assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
3580 typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
3581 typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data));
3582 typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs));
3583 oop o = JNIHandles::resolve(obj);
3585 if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) {
3586 THROW(vmSymbols::java_lang_NullPointerException());
3587 }
3589 jsize nfids = fids->length();
3590 if (nfids == 0) return;
3592 if (tcodes->length() < nfids) {
3593 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
3594 }
3596 jsize off = 0;
3597 /* loop through fields, setting values */
3598 for (jsize i = 0; i < nfids; i++) {
3599 jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
3600 int field_offset;
3601 if (fid != NULL) {
3602 // NULL is a legal value for fid, but retrieving the field offset
3603 // trigger assertion in that case
3604 field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
3605 }
3607 switch (tcodes->char_at(i)) {
3608 case 'Z':
3609 if (fid != NULL) {
3610 jboolean val = (dbuf->byte_at(off) != 0) ? JNI_TRUE : JNI_FALSE;
3611 o->bool_field_put(field_offset, val);
3612 }
3613 off++;
3614 break;
3616 case 'B':
3617 if (fid != NULL) {
3618 o->byte_field_put(field_offset, dbuf->byte_at(off));
3619 }
3620 off++;
3621 break;
3623 case 'C':
3624 if (fid != NULL) {
3625 jchar val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
3626 + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
3627 o->char_field_put(field_offset, val);
3628 }
3629 off += 2;
3630 break;
3632 case 'S':
3633 if (fid != NULL) {
3634 jshort val = ((dbuf->byte_at(off + 0) & 0xFF) << 8)
3635 + ((dbuf->byte_at(off + 1) & 0xFF) << 0);
3636 o->short_field_put(field_offset, val);
3637 }
3638 off += 2;
3639 break;
3641 case 'I':
3642 if (fid != NULL) {
3643 jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
3644 + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
3645 + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
3646 + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
3647 o->int_field_put(field_offset, ival);
3648 }
3649 off += 4;
3650 break;
3652 case 'F':
3653 if (fid != NULL) {
3654 jint ival = ((dbuf->byte_at(off + 0) & 0xFF) << 24)
3655 + ((dbuf->byte_at(off + 1) & 0xFF) << 16)
3656 + ((dbuf->byte_at(off + 2) & 0xFF) << 8)
3657 + ((dbuf->byte_at(off + 3) & 0xFF) << 0);
3658 jfloat fval = (*int_bits_to_float_fn)(env, NULL, ival);
3659 o->float_field_put(field_offset, fval);
3660 }
3661 off += 4;
3662 break;
3664 case 'J':
3665 if (fid != NULL) {
3666 jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
3667 + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
3668 + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
3669 + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
3670 + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
3671 + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
3672 + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
3673 + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
3674 o->long_field_put(field_offset, lval);
3675 }
3676 off += 8;
3677 break;
3679 case 'D':
3680 if (fid != NULL) {
3681 jlong lval = (((jlong) dbuf->byte_at(off + 0) & 0xFF) << 56)
3682 + (((jlong) dbuf->byte_at(off + 1) & 0xFF) << 48)
3683 + (((jlong) dbuf->byte_at(off + 2) & 0xFF) << 40)
3684 + (((jlong) dbuf->byte_at(off + 3) & 0xFF) << 32)
3685 + (((jlong) dbuf->byte_at(off + 4) & 0xFF) << 24)
3686 + (((jlong) dbuf->byte_at(off + 5) & 0xFF) << 16)
3687 + (((jlong) dbuf->byte_at(off + 6) & 0xFF) << 8)
3688 + (((jlong) dbuf->byte_at(off + 7) & 0xFF) << 0);
3689 jdouble dval = (*long_bits_to_double_fn)(env, NULL, lval);
3690 o->double_field_put(field_offset, dval);
3691 }
3692 off += 8;
3693 break;
3695 default:
3696 // Illegal typecode
3697 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
3698 }
3699 }
3700 JVM_END
3703 JVM_ENTRY(void, JVM_GetPrimitiveFieldValues(JNIEnv *env, jclass cb, jobject obj,
3704 jlongArray fieldIDs, jcharArray typecodes, jbyteArray data))
3705 assert(!JDK_Version::is_gte_jdk14x_version(), "should only be used in 1.3.1 and earlier");
3707 typeArrayOop tcodes = typeArrayOop(JNIHandles::resolve(typecodes));
3708 typeArrayOop dbuf = typeArrayOop(JNIHandles::resolve(data));
3709 typeArrayOop fids = typeArrayOop(JNIHandles::resolve(fieldIDs));
3710 oop o = JNIHandles::resolve(obj);
3712 if (o == NULL || fids == NULL || dbuf == NULL || tcodes == NULL) {
3713 THROW(vmSymbols::java_lang_NullPointerException());
3714 }
3716 jsize nfids = fids->length();
3717 if (nfids == 0) return;
3719 if (tcodes->length() < nfids) {
3720 THROW(vmSymbols::java_lang_ArrayIndexOutOfBoundsException());
3721 }
3723 /* loop through fields, fetching values */
3724 jsize off = 0;
3725 for (jsize i = 0; i < nfids; i++) {
3726 jfieldID fid = (jfieldID)(intptr_t) fids->long_at(i);
3727 if (fid == NULL) {
3728 THROW(vmSymbols::java_lang_NullPointerException());
3729 }
3730 int field_offset = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
3732 switch (tcodes->char_at(i)) {
3733 case 'Z':
3734 {
3735 jboolean val = o->bool_field(field_offset);
3736 dbuf->byte_at_put(off++, (val != 0) ? 1 : 0);
3737 }
3738 break;
3740 case 'B':
3741 dbuf->byte_at_put(off++, o->byte_field(field_offset));
3742 break;
3744 case 'C':
3745 {
3746 jchar val = o->char_field(field_offset);
3747 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3748 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3749 }
3750 break;
3752 case 'S':
3753 {
3754 jshort val = o->short_field(field_offset);
3755 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3756 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3757 }
3758 break;
3760 case 'I':
3761 {
3762 jint val = o->int_field(field_offset);
3763 dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
3764 dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
3765 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3766 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3767 }
3768 break;
3770 case 'F':
3771 {
3772 jfloat fval = o->float_field(field_offset);
3773 jint ival = (*float_to_int_bits_fn)(env, NULL, fval);
3774 dbuf->byte_at_put(off++, (ival >> 24) & 0xFF);
3775 dbuf->byte_at_put(off++, (ival >> 16) & 0xFF);
3776 dbuf->byte_at_put(off++, (ival >> 8) & 0xFF);
3777 dbuf->byte_at_put(off++, (ival >> 0) & 0xFF);
3778 }
3779 break;
3781 case 'J':
3782 {
3783 jlong val = o->long_field(field_offset);
3784 dbuf->byte_at_put(off++, (val >> 56) & 0xFF);
3785 dbuf->byte_at_put(off++, (val >> 48) & 0xFF);
3786 dbuf->byte_at_put(off++, (val >> 40) & 0xFF);
3787 dbuf->byte_at_put(off++, (val >> 32) & 0xFF);
3788 dbuf->byte_at_put(off++, (val >> 24) & 0xFF);
3789 dbuf->byte_at_put(off++, (val >> 16) & 0xFF);
3790 dbuf->byte_at_put(off++, (val >> 8) & 0xFF);
3791 dbuf->byte_at_put(off++, (val >> 0) & 0xFF);
3792 }
3793 break;
3795 case 'D':
3796 {
3797 jdouble dval = o->double_field(field_offset);
3798 jlong lval = (*double_to_long_bits_fn)(env, NULL, dval);
3799 dbuf->byte_at_put(off++, (lval >> 56) & 0xFF);
3800 dbuf->byte_at_put(off++, (lval >> 48) & 0xFF);
3801 dbuf->byte_at_put(off++, (lval >> 40) & 0xFF);
3802 dbuf->byte_at_put(off++, (lval >> 32) & 0xFF);
3803 dbuf->byte_at_put(off++, (lval >> 24) & 0xFF);
3804 dbuf->byte_at_put(off++, (lval >> 16) & 0xFF);
3805 dbuf->byte_at_put(off++, (lval >> 8) & 0xFF);
3806 dbuf->byte_at_put(off++, (lval >> 0) & 0xFF);
3807 }
3808 break;
3810 default:
3811 // Illegal typecode
3812 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "illegal typecode");
3813 }
3814 }
3815 JVM_END
3818 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
3820 jclass find_class_from_class_loader(JNIEnv* env, symbolHandle name, jboolean init, Handle loader, Handle protection_domain, jboolean throwError, TRAPS) {
3821 // Security Note:
3822 // The Java level wrapper will perform the necessary security check allowing
3823 // us to pass the NULL as the initiating class loader.
3824 klassOop klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
3825 KlassHandle klass_handle(THREAD, klass);
3826 // Check if we should initialize the class
3827 if (init && klass_handle->oop_is_instance()) {
3828 klass_handle->initialize(CHECK_NULL);
3829 }
3830 return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
3831 }
3834 // Internal SQE debugging support ///////////////////////////////////////////////////////////
3836 #ifndef PRODUCT
3838 extern "C" {
3839 JNIEXPORT jboolean JNICALL JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get);
3840 JNIEXPORT jboolean JNICALL JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get);
3841 JNIEXPORT void JNICALL JVM_VMBreakPoint(JNIEnv *env, jobject obj);
3842 }
3844 JVM_LEAF(jboolean, JVM_AccessVMBooleanFlag(const char* name, jboolean* value, jboolean is_get))
3845 JVMWrapper("JVM_AccessBoolVMFlag");
3846 return is_get ? CommandLineFlags::boolAt((char*) name, (bool*) value) : CommandLineFlags::boolAtPut((char*) name, (bool*) value, INTERNAL);
3847 JVM_END
3849 JVM_LEAF(jboolean, JVM_AccessVMIntFlag(const char* name, jint* value, jboolean is_get))
3850 JVMWrapper("JVM_AccessVMIntFlag");
3851 intx v;
3852 jboolean result = is_get ? CommandLineFlags::intxAt((char*) name, &v) : CommandLineFlags::intxAtPut((char*) name, &v, INTERNAL);
3853 *value = (jint)v;
3854 return result;
3855 JVM_END
3858 JVM_ENTRY(void, JVM_VMBreakPoint(JNIEnv *env, jobject obj))
3859 JVMWrapper("JVM_VMBreakPoint");
3860 oop the_obj = JNIHandles::resolve(obj);
3861 BREAKPOINT;
3862 JVM_END
3865 #endif
3868 //---------------------------------------------------------------------------
3869 //
3870 // Support for old native code-based reflection (pre-JDK 1.4)
3871 // Disabled by default in the product build.
3872 //
3873 // See reflection.hpp for information on SUPPORT_OLD_REFLECTION
3874 //
3875 //---------------------------------------------------------------------------
3877 #ifdef SUPPORT_OLD_REFLECTION
3879 JVM_ENTRY(jobjectArray, JVM_GetClassFields(JNIEnv *env, jclass cls, jint which))
3880 JVMWrapper("JVM_GetClassFields");
3881 JvmtiVMObjectAllocEventCollector oam;
3882 oop mirror = JNIHandles::resolve_non_null(cls);
3883 objArrayOop result = Reflection::reflect_fields(mirror, which, CHECK_NULL);
3884 return (jobjectArray) JNIHandles::make_local(env, result);
3885 JVM_END
3888 JVM_ENTRY(jobjectArray, JVM_GetClassMethods(JNIEnv *env, jclass cls, jint which))
3889 JVMWrapper("JVM_GetClassMethods");
3890 JvmtiVMObjectAllocEventCollector oam;
3891 oop mirror = JNIHandles::resolve_non_null(cls);
3892 objArrayOop result = Reflection::reflect_methods(mirror, which, CHECK_NULL);
3893 //%note jvm_r4
3894 return (jobjectArray) JNIHandles::make_local(env, result);
3895 JVM_END
3898 JVM_ENTRY(jobjectArray, JVM_GetClassConstructors(JNIEnv *env, jclass cls, jint which))
3899 JVMWrapper("JVM_GetClassConstructors");
3900 JvmtiVMObjectAllocEventCollector oam;
3901 oop mirror = JNIHandles::resolve_non_null(cls);
3902 objArrayOop result = Reflection::reflect_constructors(mirror, which, CHECK_NULL);
3903 //%note jvm_r4
3904 return (jobjectArray) JNIHandles::make_local(env, result);
3905 JVM_END
3908 JVM_ENTRY(jobject, JVM_GetClassField(JNIEnv *env, jclass cls, jstring name, jint which))
3909 JVMWrapper("JVM_GetClassField");
3910 JvmtiVMObjectAllocEventCollector oam;
3911 if (name == NULL) return NULL;
3912 Handle str (THREAD, JNIHandles::resolve_non_null(name));
3914 const char* cstr = java_lang_String::as_utf8_string(str());
3915 symbolHandle field_name =
3916 symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
3917 if (field_name.is_null()) {
3918 THROW_0(vmSymbols::java_lang_NoSuchFieldException());
3919 }
3921 oop mirror = JNIHandles::resolve_non_null(cls);
3922 oop result = Reflection::reflect_field(mirror, field_name(), which, CHECK_NULL);
3923 if (result == NULL) {
3924 THROW_0(vmSymbols::java_lang_NoSuchFieldException());
3925 }
3926 return JNIHandles::make_local(env, result);
3927 JVM_END
3930 JVM_ENTRY(jobject, JVM_GetClassMethod(JNIEnv *env, jclass cls, jstring name, jobjectArray types, jint which))
3931 JVMWrapper("JVM_GetClassMethod");
3932 JvmtiVMObjectAllocEventCollector oam;
3933 if (name == NULL) {
3934 THROW_0(vmSymbols::java_lang_NullPointerException());
3935 }
3936 Handle str (THREAD, JNIHandles::resolve_non_null(name));
3938 const char* cstr = java_lang_String::as_utf8_string(str());
3939 symbolHandle method_name =
3940 symbolHandle(THREAD, SymbolTable::probe(cstr, (int)strlen(cstr)));
3941 if (method_name.is_null()) {
3942 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3943 }
3945 oop mirror = JNIHandles::resolve_non_null(cls);
3946 objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
3947 oop result = Reflection::reflect_method(mirror, method_name, tarray,
3948 which, CHECK_NULL);
3949 if (result == NULL) {
3950 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3951 }
3952 return JNIHandles::make_local(env, result);
3953 JVM_END
3956 JVM_ENTRY(jobject, JVM_GetClassConstructor(JNIEnv *env, jclass cls, jobjectArray types, jint which))
3957 JVMWrapper("JVM_GetClassConstructor");
3958 JvmtiVMObjectAllocEventCollector oam;
3959 oop mirror = JNIHandles::resolve_non_null(cls);
3960 objArrayHandle tarray (THREAD, objArrayOop(JNIHandles::resolve(types)));
3961 oop result = Reflection::reflect_constructor(mirror, tarray, which, CHECK_NULL);
3962 if (result == NULL) {
3963 THROW_0(vmSymbols::java_lang_NoSuchMethodException());
3964 }
3965 return (jobject) JNIHandles::make_local(env, result);
3966 JVM_END
3969 // Instantiation ///////////////////////////////////////////////////////////////////////////////
3971 JVM_ENTRY(jobject, JVM_NewInstance(JNIEnv *env, jclass cls))
3972 JVMWrapper("JVM_NewInstance");
3973 Handle mirror(THREAD, JNIHandles::resolve_non_null(cls));
3975 methodOop resolved_constructor = java_lang_Class::resolved_constructor(mirror());
3976 if (resolved_constructor == NULL) {
3977 klassOop k = java_lang_Class::as_klassOop(mirror());
3978 // The java.lang.Class object caches a resolved constructor if all the checks
3979 // below were done successfully and a constructor was found.
3981 // Do class based checks
3982 if (java_lang_Class::is_primitive(mirror())) {
3983 const char* msg = "";
3984 if (mirror == Universe::bool_mirror()) msg = "java/lang/Boolean";
3985 else if (mirror == Universe::char_mirror()) msg = "java/lang/Character";
3986 else if (mirror == Universe::float_mirror()) msg = "java/lang/Float";
3987 else if (mirror == Universe::double_mirror()) msg = "java/lang/Double";
3988 else if (mirror == Universe::byte_mirror()) msg = "java/lang/Byte";
3989 else if (mirror == Universe::short_mirror()) msg = "java/lang/Short";
3990 else if (mirror == Universe::int_mirror()) msg = "java/lang/Integer";
3991 else if (mirror == Universe::long_mirror()) msg = "java/lang/Long";
3992 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), msg);
3993 }
3995 // Check whether we are allowed to instantiate this class
3996 Klass::cast(k)->check_valid_for_instantiation(false, CHECK_NULL); // Array classes get caught here
3997 instanceKlassHandle klass(THREAD, k);
3998 // Make sure class is initialized (also so all methods are rewritten)
3999 klass->initialize(CHECK_NULL);
4001 // Lookup default constructor
4002 resolved_constructor = klass->find_method(vmSymbols::object_initializer_name(), vmSymbols::void_method_signature());
4003 if (resolved_constructor == NULL) {
4004 ResourceMark rm(THREAD);
4005 THROW_MSG_0(vmSymbols::java_lang_InstantiationException(), klass->external_name());
4006 }
4008 // Cache result in java.lang.Class object. Does not have to be MT safe.
4009 java_lang_Class::set_resolved_constructor(mirror(), resolved_constructor);
4010 }
4012 assert(resolved_constructor != NULL, "sanity check");
4013 methodHandle constructor = methodHandle(THREAD, resolved_constructor);
4015 // We have an initialized instanceKlass with a default constructor
4016 instanceKlassHandle klass(THREAD, java_lang_Class::as_klassOop(JNIHandles::resolve_non_null(cls)));
4017 assert(klass->is_initialized() || klass->is_being_initialized(), "sanity check");
4019 // Do security check
4020 klassOop caller_klass = NULL;
4021 if (UsePrivilegedStack) {
4022 caller_klass = thread->security_get_caller_class(2);
4024 if (!Reflection::verify_class_access(caller_klass, klass(), false) ||
4025 !Reflection::verify_field_access(caller_klass,
4026 klass(),
4027 klass(),
4028 constructor->access_flags(),
4029 false,
4030 true)) {
4031 ResourceMark rm(THREAD);
4032 THROW_MSG_0(vmSymbols::java_lang_IllegalAccessException(), klass->external_name());
4033 }
4034 }
4036 // Allocate object and call constructor
4037 Handle receiver = klass->allocate_instance_handle(CHECK_NULL);
4038 JavaCalls::call_default_constructor(thread, constructor, receiver, CHECK_NULL);
4040 jobject res = JNIHandles::make_local(env, receiver());
4041 if (JvmtiExport::should_post_vm_object_alloc()) {
4042 JvmtiExport::post_vm_object_alloc(JavaThread::current(), receiver());
4043 }
4044 return res;
4045 JVM_END
4048 // Field ////////////////////////////////////////////////////////////////////////////////////////////
4050 JVM_ENTRY(jobject, JVM_GetField(JNIEnv *env, jobject field, jobject obj))
4051 JVMWrapper("JVM_GetField");
4052 JvmtiVMObjectAllocEventCollector oam;
4053 Handle field_mirror(thread, JNIHandles::resolve(field));
4054 Handle receiver (thread, JNIHandles::resolve(obj));
4055 fieldDescriptor fd;
4056 Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_NULL);
4057 jvalue value;
4058 BasicType type = Reflection::field_get(&value, &fd, receiver);
4059 oop box = Reflection::box(&value, type, CHECK_NULL);
4060 return JNIHandles::make_local(env, box);
4061 JVM_END
4064 JVM_ENTRY(jvalue, JVM_GetPrimitiveField(JNIEnv *env, jobject field, jobject obj, unsigned char wCode))
4065 JVMWrapper("JVM_GetPrimitiveField");
4066 Handle field_mirror(thread, JNIHandles::resolve(field));
4067 Handle receiver (thread, JNIHandles::resolve(obj));
4068 fieldDescriptor fd;
4069 jvalue value;
4070 value.j = 0;
4071 Reflection::resolve_field(field_mirror, receiver, &fd, false, CHECK_(value));
4072 BasicType type = Reflection::field_get(&value, &fd, receiver);
4073 BasicType wide_type = (BasicType) wCode;
4074 if (type != wide_type) {
4075 Reflection::widen(&value, type, wide_type, CHECK_(value));
4076 }
4077 return value;
4078 JVM_END // should really be JVM_END, but that doesn't work for union types!
4081 JVM_ENTRY(void, JVM_SetField(JNIEnv *env, jobject field, jobject obj, jobject val))
4082 JVMWrapper("JVM_SetField");
4083 Handle field_mirror(thread, JNIHandles::resolve(field));
4084 Handle receiver (thread, JNIHandles::resolve(obj));
4085 oop box = JNIHandles::resolve(val);
4086 fieldDescriptor fd;
4087 Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
4088 BasicType field_type = fd.field_type();
4089 jvalue value;
4090 BasicType value_type;
4091 if (field_type == T_OBJECT || field_type == T_ARRAY) {
4092 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
4093 value_type = Reflection::unbox_for_regular_object(box, &value);
4094 Reflection::field_set(&value, &fd, receiver, field_type, CHECK);
4095 } else {
4096 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
4097 Reflection::field_set(&value, &fd, receiver, value_type, CHECK);
4098 }
4099 JVM_END
4102 JVM_ENTRY(void, JVM_SetPrimitiveField(JNIEnv *env, jobject field, jobject obj, jvalue v, unsigned char vCode))
4103 JVMWrapper("JVM_SetPrimitiveField");
4104 Handle field_mirror(thread, JNIHandles::resolve(field));
4105 Handle receiver (thread, JNIHandles::resolve(obj));
4106 fieldDescriptor fd;
4107 Reflection::resolve_field(field_mirror, receiver, &fd, true, CHECK);
4108 BasicType value_type = (BasicType) vCode;
4109 Reflection::field_set(&v, &fd, receiver, value_type, CHECK);
4110 JVM_END
4113 // Method ///////////////////////////////////////////////////////////////////////////////////////////
4115 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
4116 JVMWrapper("JVM_InvokeMethod");
4117 Handle method_handle;
4118 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
4119 method_handle = Handle(THREAD, JNIHandles::resolve(method));
4120 Handle receiver(THREAD, JNIHandles::resolve(obj));
4121 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
4122 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
4123 jobject res = JNIHandles::make_local(env, result);
4124 if (JvmtiExport::should_post_vm_object_alloc()) {
4125 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
4126 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
4127 if (java_lang_Class::is_primitive(ret_type)) {
4128 // Only for primitive type vm allocates memory for java object.
4129 // See box() method.
4130 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
4131 }
4132 }
4133 return res;
4134 } else {
4135 THROW_0(vmSymbols::java_lang_StackOverflowError());
4136 }
4137 JVM_END
4140 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
4141 JVMWrapper("JVM_NewInstanceFromConstructor");
4142 oop constructor_mirror = JNIHandles::resolve(c);
4143 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
4144 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
4145 jobject res = JNIHandles::make_local(env, result);
4146 if (JvmtiExport::should_post_vm_object_alloc()) {
4147 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
4148 }
4149 return res;
4150 JVM_END
4152 #endif /* SUPPORT_OLD_REFLECTION */
4154 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
4156 JVM_LEAF(jboolean, JVM_SupportsCX8())
4157 JVMWrapper("JVM_SupportsCX8");
4158 return VM_Version::supports_cx8();
4159 JVM_END
4162 JVM_ENTRY(jboolean, JVM_CX8Field(JNIEnv *env, jobject obj, jfieldID fid, jlong oldVal, jlong newVal))
4163 JVMWrapper("JVM_CX8Field");
4164 jlong res;
4165 oop o = JNIHandles::resolve(obj);
4166 intptr_t fldOffs = jfieldIDWorkaround::from_instance_jfieldID(o->klass(), fid);
4167 volatile jlong* addr = (volatile jlong*)((address)o + fldOffs);
4169 assert(VM_Version::supports_cx8(), "cx8 not supported");
4170 res = Atomic::cmpxchg(newVal, addr, oldVal);
4172 return res == oldVal;
4173 JVM_END
4175 // DTrace ///////////////////////////////////////////////////////////////////
4177 JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env))
4178 JVMWrapper("JVM_DTraceGetVersion");
4179 return (jint)JVM_TRACING_DTRACE_VERSION;
4180 JVM_END
4182 JVM_ENTRY(jlong,JVM_DTraceActivate(
4183 JNIEnv* env, jint version, jstring module_name, jint providers_count,
4184 JVM_DTraceProvider* providers))
4185 JVMWrapper("JVM_DTraceActivate");
4186 return DTraceJSDT::activate(
4187 version, module_name, providers_count, providers, CHECK_0);
4188 JVM_END
4190 JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method))
4191 JVMWrapper("JVM_DTraceIsProbeEnabled");
4192 return DTraceJSDT::is_probe_enabled(method);
4193 JVM_END
4195 JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle))
4196 JVMWrapper("JVM_DTraceDispose");
4197 DTraceJSDT::dispose(handle);
4198 JVM_END
4200 JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env))
4201 JVMWrapper("JVM_DTraceIsSupported");
4202 return DTraceJSDT::is_supported();
4203 JVM_END
4205 // Returns an array of all live Thread objects (VM internal JavaThreads,
4206 // jvmti agent threads, and JNI attaching threads are skipped)
4207 // See CR 6404306 regarding JNI attaching threads
4208 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
4209 ResourceMark rm(THREAD);
4210 ThreadsListEnumerator tle(THREAD, false, false);
4211 JvmtiVMObjectAllocEventCollector oam;
4213 int num_threads = tle.num_threads();
4214 objArrayOop r = oopFactory::new_objArray(SystemDictionary::thread_klass(), num_threads, CHECK_NULL);
4215 objArrayHandle threads_ah(THREAD, r);
4217 for (int i = 0; i < num_threads; i++) {
4218 Handle h = tle.get_threadObj(i);
4219 threads_ah->obj_at_put(i, h());
4220 }
4222 return (jobjectArray) JNIHandles::make_local(env, threads_ah());
4223 JVM_END
4226 // Support for java.lang.Thread.getStackTrace() and getAllStackTraces() methods
4227 // Return StackTraceElement[][], each element is the stack trace of a thread in
4228 // the corresponding entry in the given threads array
4229 JVM_ENTRY(jobjectArray, JVM_DumpThreads(JNIEnv *env, jclass threadClass, jobjectArray threads))
4230 JVMWrapper("JVM_DumpThreads");
4231 JvmtiVMObjectAllocEventCollector oam;
4233 // Check if threads is null
4234 if (threads == NULL) {
4235 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
4236 }
4238 objArrayOop a = objArrayOop(JNIHandles::resolve_non_null(threads));
4239 objArrayHandle ah(THREAD, a);
4240 int num_threads = ah->length();
4241 // check if threads is non-empty array
4242 if (num_threads == 0) {
4243 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
4244 }
4246 // check if threads is not an array of objects of Thread class
4247 klassOop k = objArrayKlass::cast(ah->klass())->element_klass();
4248 if (k != SystemDictionary::thread_klass()) {
4249 THROW_(vmSymbols::java_lang_IllegalArgumentException(), 0);
4250 }
4252 ResourceMark rm(THREAD);
4254 GrowableArray<instanceHandle>* thread_handle_array = new GrowableArray<instanceHandle>(num_threads);
4255 for (int i = 0; i < num_threads; i++) {
4256 oop thread_obj = ah->obj_at(i);
4257 instanceHandle h(THREAD, (instanceOop) thread_obj);
4258 thread_handle_array->append(h);
4259 }
4261 Handle stacktraces = ThreadService::dump_stack_traces(thread_handle_array, num_threads, CHECK_NULL);
4262 return (jobjectArray)JNIHandles::make_local(env, stacktraces());
4264 JVM_END
4266 // JVM monitoring and management support
4267 JVM_ENTRY_NO_ENV(void*, JVM_GetManagement(jint version))
4268 return Management::get_jmm_interface(version);
4269 JVM_END
4271 // com.sun.tools.attach.VirtualMachine agent properties support
4272 //
4273 // Initialize the agent properties with the properties maintained in the VM
4274 JVM_ENTRY(jobject, JVM_InitAgentProperties(JNIEnv *env, jobject properties))
4275 JVMWrapper("JVM_InitAgentProperties");
4276 ResourceMark rm;
4278 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
4280 PUTPROP(props, "sun.java.command", Arguments::java_command());
4281 PUTPROP(props, "sun.jvm.flags", Arguments::jvm_flags());
4282 PUTPROP(props, "sun.jvm.args", Arguments::jvm_args());
4283 return properties;
4284 JVM_END
4286 JVM_ENTRY(jobjectArray, JVM_GetEnclosingMethodInfo(JNIEnv *env, jclass ofClass))
4287 {
4288 JVMWrapper("JVM_GetEnclosingMethodInfo");
4289 JvmtiVMObjectAllocEventCollector oam;
4291 if (ofClass == NULL) {
4292 return NULL;
4293 }
4294 Handle mirror(THREAD, JNIHandles::resolve_non_null(ofClass));
4295 // Special handling for primitive objects
4296 if (java_lang_Class::is_primitive(mirror())) {
4297 return NULL;
4298 }
4299 klassOop k = java_lang_Class::as_klassOop(mirror());
4300 if (!Klass::cast(k)->oop_is_instance()) {
4301 return NULL;
4302 }
4303 instanceKlassHandle ik_h(THREAD, k);
4304 int encl_method_class_idx = ik_h->enclosing_method_class_index();
4305 if (encl_method_class_idx == 0) {
4306 return NULL;
4307 }
4308 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::object_klass(), 3, CHECK_NULL);
4309 objArrayHandle dest(THREAD, dest_o);
4310 klassOop enc_k = ik_h->constants()->klass_at(encl_method_class_idx, CHECK_NULL);
4311 dest->obj_at_put(0, Klass::cast(enc_k)->java_mirror());
4312 int encl_method_method_idx = ik_h->enclosing_method_method_index();
4313 if (encl_method_method_idx != 0) {
4314 symbolOop sym_o = ik_h->constants()->symbol_at(
4315 extract_low_short_from_int(
4316 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
4317 symbolHandle sym(THREAD, sym_o);
4318 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
4319 dest->obj_at_put(1, str());
4320 sym_o = ik_h->constants()->symbol_at(
4321 extract_high_short_from_int(
4322 ik_h->constants()->name_and_type_at(encl_method_method_idx)));
4323 sym = symbolHandle(THREAD, sym_o);
4324 str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
4325 dest->obj_at_put(2, str());
4326 }
4327 return (jobjectArray) JNIHandles::make_local(dest());
4328 }
4329 JVM_END
4331 JVM_ENTRY(jintArray, JVM_GetThreadStateValues(JNIEnv* env,
4332 jint javaThreadState))
4333 {
4334 // If new thread states are added in future JDK and VM versions,
4335 // this should check if the JDK version is compatible with thread
4336 // states supported by the VM. Return NULL if not compatible.
4337 //
4338 // This function must map the VM java_lang_Thread::ThreadStatus
4339 // to the Java thread state that the JDK supports.
4340 //
4342 typeArrayHandle values_h;
4343 switch (javaThreadState) {
4344 case JAVA_THREAD_STATE_NEW : {
4345 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4346 values_h = typeArrayHandle(THREAD, r);
4347 values_h->int_at_put(0, java_lang_Thread::NEW);
4348 break;
4349 }
4350 case JAVA_THREAD_STATE_RUNNABLE : {
4351 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4352 values_h = typeArrayHandle(THREAD, r);
4353 values_h->int_at_put(0, java_lang_Thread::RUNNABLE);
4354 break;
4355 }
4356 case JAVA_THREAD_STATE_BLOCKED : {
4357 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4358 values_h = typeArrayHandle(THREAD, r);
4359 values_h->int_at_put(0, java_lang_Thread::BLOCKED_ON_MONITOR_ENTER);
4360 break;
4361 }
4362 case JAVA_THREAD_STATE_WAITING : {
4363 typeArrayOop r = oopFactory::new_typeArray(T_INT, 2, CHECK_NULL);
4364 values_h = typeArrayHandle(THREAD, r);
4365 values_h->int_at_put(0, java_lang_Thread::IN_OBJECT_WAIT);
4366 values_h->int_at_put(1, java_lang_Thread::PARKED);
4367 break;
4368 }
4369 case JAVA_THREAD_STATE_TIMED_WAITING : {
4370 typeArrayOop r = oopFactory::new_typeArray(T_INT, 3, CHECK_NULL);
4371 values_h = typeArrayHandle(THREAD, r);
4372 values_h->int_at_put(0, java_lang_Thread::SLEEPING);
4373 values_h->int_at_put(1, java_lang_Thread::IN_OBJECT_WAIT_TIMED);
4374 values_h->int_at_put(2, java_lang_Thread::PARKED_TIMED);
4375 break;
4376 }
4377 case JAVA_THREAD_STATE_TERMINATED : {
4378 typeArrayOop r = oopFactory::new_typeArray(T_INT, 1, CHECK_NULL);
4379 values_h = typeArrayHandle(THREAD, r);
4380 values_h->int_at_put(0, java_lang_Thread::TERMINATED);
4381 break;
4382 }
4383 default:
4384 // Unknown state - probably incompatible JDK version
4385 return NULL;
4386 }
4388 return (jintArray) JNIHandles::make_local(env, values_h());
4389 }
4390 JVM_END
4393 JVM_ENTRY(jobjectArray, JVM_GetThreadStateNames(JNIEnv* env,
4394 jint javaThreadState,
4395 jintArray values))
4396 {
4397 // If new thread states are added in future JDK and VM versions,
4398 // this should check if the JDK version is compatible with thread
4399 // states supported by the VM. Return NULL if not compatible.
4400 //
4401 // This function must map the VM java_lang_Thread::ThreadStatus
4402 // to the Java thread state that the JDK supports.
4403 //
4405 ResourceMark rm;
4407 // Check if threads is null
4408 if (values == NULL) {
4409 THROW_(vmSymbols::java_lang_NullPointerException(), 0);
4410 }
4412 typeArrayOop v = typeArrayOop(JNIHandles::resolve_non_null(values));
4413 typeArrayHandle values_h(THREAD, v);
4415 objArrayHandle names_h;
4416 switch (javaThreadState) {
4417 case JAVA_THREAD_STATE_NEW : {
4418 assert(values_h->length() == 1 &&
4419 values_h->int_at(0) == java_lang_Thread::NEW,
4420 "Invalid threadStatus value");
4422 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4423 1, /* only 1 substate */
4424 CHECK_NULL);
4425 names_h = objArrayHandle(THREAD, r);
4426 Handle name = java_lang_String::create_from_str("NEW", CHECK_NULL);
4427 names_h->obj_at_put(0, name());
4428 break;
4429 }
4430 case JAVA_THREAD_STATE_RUNNABLE : {
4431 assert(values_h->length() == 1 &&
4432 values_h->int_at(0) == java_lang_Thread::RUNNABLE,
4433 "Invalid threadStatus value");
4435 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4436 1, /* only 1 substate */
4437 CHECK_NULL);
4438 names_h = objArrayHandle(THREAD, r);
4439 Handle name = java_lang_String::create_from_str("RUNNABLE", CHECK_NULL);
4440 names_h->obj_at_put(0, name());
4441 break;
4442 }
4443 case JAVA_THREAD_STATE_BLOCKED : {
4444 assert(values_h->length() == 1 &&
4445 values_h->int_at(0) == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER,
4446 "Invalid threadStatus value");
4448 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4449 1, /* only 1 substate */
4450 CHECK_NULL);
4451 names_h = objArrayHandle(THREAD, r);
4452 Handle name = java_lang_String::create_from_str("BLOCKED", CHECK_NULL);
4453 names_h->obj_at_put(0, name());
4454 break;
4455 }
4456 case JAVA_THREAD_STATE_WAITING : {
4457 assert(values_h->length() == 2 &&
4458 values_h->int_at(0) == java_lang_Thread::IN_OBJECT_WAIT &&
4459 values_h->int_at(1) == java_lang_Thread::PARKED,
4460 "Invalid threadStatus value");
4461 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4462 2, /* number of substates */
4463 CHECK_NULL);
4464 names_h = objArrayHandle(THREAD, r);
4465 Handle name0 = java_lang_String::create_from_str("WAITING.OBJECT_WAIT",
4466 CHECK_NULL);
4467 Handle name1 = java_lang_String::create_from_str("WAITING.PARKED",
4468 CHECK_NULL);
4469 names_h->obj_at_put(0, name0());
4470 names_h->obj_at_put(1, name1());
4471 break;
4472 }
4473 case JAVA_THREAD_STATE_TIMED_WAITING : {
4474 assert(values_h->length() == 3 &&
4475 values_h->int_at(0) == java_lang_Thread::SLEEPING &&
4476 values_h->int_at(1) == java_lang_Thread::IN_OBJECT_WAIT_TIMED &&
4477 values_h->int_at(2) == java_lang_Thread::PARKED_TIMED,
4478 "Invalid threadStatus value");
4479 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4480 3, /* number of substates */
4481 CHECK_NULL);
4482 names_h = objArrayHandle(THREAD, r);
4483 Handle name0 = java_lang_String::create_from_str("TIMED_WAITING.SLEEPING",
4484 CHECK_NULL);
4485 Handle name1 = java_lang_String::create_from_str("TIMED_WAITING.OBJECT_WAIT",
4486 CHECK_NULL);
4487 Handle name2 = java_lang_String::create_from_str("TIMED_WAITING.PARKED",
4488 CHECK_NULL);
4489 names_h->obj_at_put(0, name0());
4490 names_h->obj_at_put(1, name1());
4491 names_h->obj_at_put(2, name2());
4492 break;
4493 }
4494 case JAVA_THREAD_STATE_TERMINATED : {
4495 assert(values_h->length() == 1 &&
4496 values_h->int_at(0) == java_lang_Thread::TERMINATED,
4497 "Invalid threadStatus value");
4498 objArrayOop r = oopFactory::new_objArray(SystemDictionary::string_klass(),
4499 1, /* only 1 substate */
4500 CHECK_NULL);
4501 names_h = objArrayHandle(THREAD, r);
4502 Handle name = java_lang_String::create_from_str("TERMINATED", CHECK_NULL);
4503 names_h->obj_at_put(0, name());
4504 break;
4505 }
4506 default:
4507 // Unknown state - probably incompatible JDK version
4508 return NULL;
4509 }
4510 return (jobjectArray) JNIHandles::make_local(env, names_h());
4511 }
4512 JVM_END
4514 JVM_ENTRY(void, JVM_GetVersionInfo(JNIEnv* env, jvm_version_info* info, size_t info_size))
4515 {
4516 memset(info, 0, sizeof(info_size));
4518 info->jvm_version = Abstract_VM_Version::jvm_version();
4519 info->update_version = 0; /* 0 in HotSpot Express VM */
4520 info->special_update_version = 0; /* 0 in HotSpot Express VM */
4522 // when we add a new capability in the jvm_version_info struct, we should also
4523 // consider to expose this new capability in the sun.rt.jvmCapabilities jvmstat
4524 // counter defined in runtimeService.cpp.
4525 info->is_attachable = AttachListener::is_attach_supported();
4526 #ifdef KERNEL
4527 info->is_kernel_jvm = 1; // true;
4528 #else // KERNEL
4529 info->is_kernel_jvm = 0; // false;
4530 #endif // KERNEL
4531 }
4532 JVM_END