annotate src/share/vm/prims/jvm.cpp @ 7511:7f062b70a344

Added tag jdk9-b43 for changeset 65a9747147b8
author lana
date Thu, 18 Dec 2014 19:57:49 -0800
parents 0f6100dde08e a12405f751a8
children 4ea7662ddc22
rev   line source
duke@0 1 /*
dcubed@5891 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/classLoader.hpp"
stefank@1879 27 #include "classfile/javaAssertions.hpp"
stefank@1879 28 #include "classfile/javaClasses.hpp"
gziemski@6414 29 #include "classfile/stringTable.hpp"
stefank@1879 30 #include "classfile/systemDictionary.hpp"
stefank@1879 31 #include "classfile/vmSymbols.hpp"
stefank@1879 32 #include "gc_interface/collectedHeap.inline.hpp"
twisti@4431 33 #include "interpreter/bytecode.hpp"
stefank@1879 34 #include "memory/oopFactory.hpp"
stefank@1879 35 #include "memory/universe.inline.hpp"
never@2702 36 #include "oops/fieldStreams.hpp"
stefank@1879 37 #include "oops/instanceKlass.hpp"
stefank@1879 38 #include "oops/objArrayKlass.hpp"
coleenp@3602 39 #include "oops/method.hpp"
stefank@1879 40 #include "prims/jvm.h"
stefank@1879 41 #include "prims/jvm_misc.hpp"
stefank@1879 42 #include "prims/jvmtiExport.hpp"
stefank@1879 43 #include "prims/jvmtiThreadState.hpp"
stefank@1879 44 #include "prims/nativeLookup.hpp"
stefank@1879 45 #include "prims/privilegedStack.hpp"
stefank@1879 46 #include "runtime/arguments.hpp"
goetz@6646 47 #include "runtime/atomic.inline.hpp"
stefank@1879 48 #include "runtime/dtraceJSDT.hpp"
stefank@1879 49 #include "runtime/handles.inline.hpp"
stefank@1879 50 #include "runtime/init.hpp"
stefank@1879 51 #include "runtime/interfaceSupport.hpp"
stefank@1879 52 #include "runtime/java.hpp"
stefank@1879 53 #include "runtime/javaCalls.hpp"
stefank@1879 54 #include "runtime/jfieldIDWorkaround.hpp"
goetz@6402 55 #include "runtime/orderAccess.inline.hpp"
goetz@6683 56 #include "runtime/os.inline.hpp"
stefank@1879 57 #include "runtime/perfData.hpp"
stefank@1879 58 #include "runtime/reflection.hpp"
goetz@6646 59 #include "runtime/thread.inline.hpp"
stefank@1879 60 #include "runtime/vframe.hpp"
stefank@1879 61 #include "runtime/vm_operations.hpp"
goetz@6760 62 #include "runtime/vm_version.hpp"
stefank@1879 63 #include "services/attachListener.hpp"
stefank@1879 64 #include "services/management.hpp"
stefank@1879 65 #include "services/threadService.hpp"
sla@4802 66 #include "trace/tracing.hpp"
stefank@1879 67 #include "utilities/copy.hpp"
stefank@1879 68 #include "utilities/defaultStream.hpp"
stefank@1879 69 #include "utilities/dtrace.hpp"
stefank@1879 70 #include "utilities/events.hpp"
stefank@1879 71 #include "utilities/histogram.hpp"
stefank@7424 72 #include "utilities/macros.hpp"
stefank@1879 73 #include "utilities/top.hpp"
stefank@1879 74 #include "utilities/utf8.hpp"
stefank@7424 75 #if INCLUDE_CDS
stefank@7424 76 #include "classfile/sharedClassUtil.hpp"
stefank@7424 77 #include "classfile/systemDictionaryShared.hpp"
stefank@7424 78 #endif
stefank@1879 79 #ifdef TARGET_OS_FAMILY_linux
stefank@1879 80 # include "jvm_linux.h"
stefank@1879 81 #endif
stefank@1879 82 #ifdef TARGET_OS_FAMILY_solaris
stefank@1879 83 # include "jvm_solaris.h"
stefank@1879 84 #endif
stefank@1879 85 #ifdef TARGET_OS_FAMILY_windows
stefank@1879 86 # include "jvm_windows.h"
stefank@1879 87 #endif
goetz@5965 88 #ifdef TARGET_OS_FAMILY_aix
goetz@5965 89 # include "jvm_aix.h"
goetz@5965 90 #endif
never@2721 91 #ifdef TARGET_OS_FAMILY_bsd
never@2721 92 # include "jvm_bsd.h"
never@2721 93 #endif
stefank@1879 94
duke@0 95 #include <errno.h>
duke@0 96
duke@0 97 /*
duke@0 98 NOTE about use of any ctor or function call that can trigger a safepoint/GC:
duke@0 99 such ctors and calls MUST NOT come between an oop declaration/init and its
duke@0 100 usage because if objects are move this may cause various memory stomps, bus
duke@0 101 errors and segfaults. Here is a cookbook for causing so called "naked oop
duke@0 102 failures":
duke@0 103
duke@0 104 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields<etc> {
duke@0 105 JVMWrapper("JVM_GetClassDeclaredFields");
duke@0 106
duke@0 107 // Object address to be held directly in mirror & not visible to GC
duke@0 108 oop mirror = JNIHandles::resolve_non_null(ofClass);
duke@0 109
duke@0 110 // If this ctor can hit a safepoint, moving objects around, then
duke@0 111 ComplexConstructor foo;
duke@0 112
duke@0 113 // Boom! mirror may point to JUNK instead of the intended object
duke@0 114 (some dereference of mirror)
duke@0 115
duke@0 116 // Here's another call that may block for GC, making mirror stale
duke@0 117 MutexLocker ml(some_lock);
duke@0 118
duke@0 119 // And here's an initializer that can result in a stale oop
duke@0 120 // all in one step.
duke@0 121 oop o = call_that_can_throw_exception(TRAPS);
duke@0 122
duke@0 123
duke@0 124 The solution is to keep the oop declaration BELOW the ctor or function
duke@0 125 call that might cause a GC, do another resolve to reassign the oop, or
duke@0 126 consider use of a Handle instead of an oop so there is immunity from object
duke@0 127 motion. But note that the "QUICK" entries below do not have a handlemark
duke@0 128 and thus can only support use of handles passed in.
duke@0 129 */
duke@0 130
coleenp@3602 131 static void trace_class_resolution_impl(Klass* to_class, TRAPS) {
duke@0 132 ResourceMark rm;
duke@0 133 int line_number = -1;
duke@0 134 const char * source_file = NULL;
acorn@657 135 const char * trace = "explicit";
coleenp@3816 136 InstanceKlass* caller = NULL;
duke@0 137 JavaThread* jthread = JavaThread::current();
duke@0 138 if (jthread->has_last_Java_frame()) {
duke@0 139 vframeStream vfst(jthread);
duke@0 140
duke@0 141 // scan up the stack skipping ClassLoader, AccessController and PrivilegedAction frames
coleenp@2062 142 TempNewSymbol access_controller = SymbolTable::new_symbol("java/security/AccessController", CHECK);
coleenp@3602 143 Klass* access_controller_klass = SystemDictionary::resolve_or_fail(access_controller, false, CHECK);
coleenp@2062 144 TempNewSymbol privileged_action = SymbolTable::new_symbol("java/security/PrivilegedAction", CHECK);
coleenp@3602 145 Klass* privileged_action_klass = SystemDictionary::resolve_or_fail(privileged_action, false, CHECK);
coleenp@3602 146
coleenp@3602 147 Method* last_caller = NULL;
duke@0 148
duke@0 149 while (!vfst.at_end()) {
coleenp@3602 150 Method* m = vfst.method();
coleenp@3602 151 if (!vfst.method()->method_holder()->is_subclass_of(SystemDictionary::ClassLoader_klass())&&
coleenp@3602 152 !vfst.method()->method_holder()->is_subclass_of(access_controller_klass) &&
coleenp@3602 153 !vfst.method()->method_holder()->is_subclass_of(privileged_action_klass)) {
duke@0 154 break;
duke@0 155 }
duke@0 156 last_caller = m;
duke@0 157 vfst.next();
duke@0 158 }
duke@0 159 // if this is called from Class.forName0 and that is called from Class.forName,
duke@0 160 // then print the caller of Class.forName. If this is Class.loadClass, then print
duke@0 161 // that caller, otherwise keep quiet since this should be picked up elsewhere.
duke@0 162 bool found_it = false;
duke@0 163 if (!vfst.at_end() &&
coleenp@3816 164 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
duke@0 165 vfst.method()->name() == vmSymbols::forName0_name()) {
duke@0 166 vfst.next();
duke@0 167 if (!vfst.at_end() &&
coleenp@3816 168 vfst.method()->method_holder()->name() == vmSymbols::java_lang_Class() &&
duke@0 169 vfst.method()->name() == vmSymbols::forName_name()) {
duke@0 170 vfst.next();
duke@0 171 found_it = true;
duke@0 172 }
duke@0 173 } else if (last_caller != NULL &&
coleenp@3816 174 last_caller->method_holder()->name() ==
duke@0 175 vmSymbols::java_lang_ClassLoader() &&
duke@0 176 (last_caller->name() == vmSymbols::loadClassInternal_name() ||
duke@0 177 last_caller->name() == vmSymbols::loadClass_name())) {
duke@0 178 found_it = true;
acorn@657 179 } else if (!vfst.at_end()) {
acorn@657 180 if (vfst.method()->is_native()) {
acorn@657 181 // JNI call
acorn@657 182 found_it = true;
acorn@657 183 }
duke@0 184 }
duke@0 185 if (found_it && !vfst.at_end()) {
duke@0 186 // found the caller
duke@0 187 caller = vfst.method()->method_holder();
duke@0 188 line_number = vfst.method()->line_number_from_bci(vfst.bci());
acorn@657 189 if (line_number == -1) {
acorn@657 190 // show method name if it's a native method
acorn@657 191 trace = vfst.method()->name_and_sig_as_C_string();
acorn@657 192 }
coleenp@3816 193 Symbol* s = caller->source_file_name();
duke@0 194 if (s != NULL) {
duke@0 195 source_file = s->as_C_string();
duke@0 196 }
duke@0 197 }
duke@0 198 }
duke@0 199 if (caller != NULL) {
duke@0 200 if (to_class != caller) {
coleenp@3816 201 const char * from = caller->external_name();
coleenp@3816 202 const char * to = to_class->external_name();
duke@0 203 // print in a single call to reduce interleaving between threads
duke@0 204 if (source_file != NULL) {
acorn@657 205 tty->print("RESOLVE %s %s %s:%d (%s)\n", from, to, source_file, line_number, trace);
duke@0 206 } else {
acorn@657 207 tty->print("RESOLVE %s %s (%s)\n", from, to, trace);
duke@0 208 }
duke@0 209 }
duke@0 210 }
duke@0 211 }
duke@0 212
coleenp@3602 213 void trace_class_resolution(Klass* to_class) {
duke@0 214 EXCEPTION_MARK;
duke@0 215 trace_class_resolution_impl(to_class, THREAD);
duke@0 216 if (HAS_PENDING_EXCEPTION) {
duke@0 217 CLEAR_PENDING_EXCEPTION;
duke@0 218 }
duke@0 219 }
duke@0 220
duke@0 221 // Wrapper to trace JVM functions
duke@0 222
duke@0 223 #ifdef ASSERT
duke@0 224 class JVMTraceWrapper : public StackObj {
duke@0 225 public:
drchase@6412 226 JVMTraceWrapper(const char* format, ...) ATTRIBUTE_PRINTF(2, 3) {
duke@0 227 if (TraceJVMCalls) {
duke@0 228 va_list ap;
duke@0 229 va_start(ap, format);
duke@0 230 tty->print("JVM ");
duke@0 231 tty->vprint_cr(format, ap);
duke@0 232 va_end(ap);
duke@0 233 }
duke@0 234 }
duke@0 235 };
duke@0 236
duke@0 237 Histogram* JVMHistogram;
duke@0 238 volatile jint JVMHistogram_lock = 0;
duke@0 239
duke@0 240 class JVMHistogramElement : public HistogramElement {
duke@0 241 public:
duke@0 242 JVMHistogramElement(const char* name);
duke@0 243 };
duke@0 244
duke@0 245 JVMHistogramElement::JVMHistogramElement(const char* elementName) {
duke@0 246 _name = elementName;
duke@0 247 uintx count = 0;
duke@0 248
duke@0 249 while (Atomic::cmpxchg(1, &JVMHistogram_lock, 0) != 0) {
duke@0 250 while (OrderAccess::load_acquire(&JVMHistogram_lock) != 0) {
duke@0 251 count +=1;
duke@0 252 if ( (WarnOnStalledSpinLock > 0)
duke@0 253 && (count % WarnOnStalledSpinLock == 0)) {
duke@0 254 warning("JVMHistogram_lock seems to be stalled");
duke@0 255 }
duke@0 256 }
duke@0 257 }
duke@0 258
duke@0 259 if(JVMHistogram == NULL)
duke@0 260 JVMHistogram = new Histogram("JVM Call Counts",100);
duke@0 261
duke@0 262 JVMHistogram->add_element(this);
duke@0 263 Atomic::dec(&JVMHistogram_lock);
duke@0 264 }
duke@0 265
duke@0 266 #define JVMCountWrapper(arg) \
duke@0 267 static JVMHistogramElement* e = new JVMHistogramElement(arg); \
duke@0 268 if (e != NULL) e->increment_count(); // Due to bug in VC++, we need a NULL check here eventhough it should never happen!
duke@0 269
duke@0 270 #define JVMWrapper(arg1) JVMCountWrapper(arg1); JVMTraceWrapper(arg1)
duke@0 271 #define JVMWrapper2(arg1, arg2) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2)
duke@0 272 #define JVMWrapper3(arg1, arg2, arg3) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3)
duke@0 273 #define JVMWrapper4(arg1, arg2, arg3, arg4) JVMCountWrapper(arg1); JVMTraceWrapper(arg1, arg2, arg3, arg4)
duke@0 274 #else
duke@0 275 #define JVMWrapper(arg1)
duke@0 276 #define JVMWrapper2(arg1, arg2)
duke@0 277 #define JVMWrapper3(arg1, arg2, arg3)
duke@0 278 #define JVMWrapper4(arg1, arg2, arg3, arg4)
duke@0 279 #endif
duke@0 280
duke@0 281
duke@0 282 // Interface version /////////////////////////////////////////////////////////////////////
duke@0 283
duke@0 284
duke@0 285 JVM_LEAF(jint, JVM_GetInterfaceVersion())
duke@0 286 return JVM_INTERFACE_VERSION;
duke@0 287 JVM_END
duke@0 288
duke@0 289
duke@0 290 // java.lang.System //////////////////////////////////////////////////////////////////////
duke@0 291
duke@0 292
duke@0 293 JVM_LEAF(jlong, JVM_CurrentTimeMillis(JNIEnv *env, jclass ignored))
duke@0 294 JVMWrapper("JVM_CurrentTimeMillis");
duke@0 295 return os::javaTimeMillis();
duke@0 296 JVM_END
duke@0 297
duke@0 298 JVM_LEAF(jlong, JVM_NanoTime(JNIEnv *env, jclass ignored))
duke@0 299 JVMWrapper("JVM_NanoTime");
duke@0 300 return os::javaTimeNanos();
duke@0 301 JVM_END
duke@0 302
duke@0 303
duke@0 304 JVM_ENTRY(void, JVM_ArrayCopy(JNIEnv *env, jclass ignored, jobject src, jint src_pos,
duke@0 305 jobject dst, jint dst_pos, jint length))
duke@0 306 JVMWrapper("JVM_ArrayCopy");
duke@0 307 // Check if we have null pointers
duke@0 308 if (src == NULL || dst == NULL) {
duke@0 309 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 310 }
duke@0 311 arrayOop s = arrayOop(JNIHandles::resolve_non_null(src));
duke@0 312 arrayOop d = arrayOop(JNIHandles::resolve_non_null(dst));
duke@0 313 assert(s->is_oop(), "JVM_ArrayCopy: src not an oop");
duke@0 314 assert(d->is_oop(), "JVM_ArrayCopy: dst not an oop");
duke@0 315 // Do copy
hseigel@3843 316 s->klass()->copy_array(s, src_pos, d, dst_pos, length, thread);
duke@0 317 JVM_END
duke@0 318
duke@0 319
duke@0 320 static void set_property(Handle props, const char* key, const char* value, TRAPS) {
duke@0 321 JavaValue r(T_OBJECT);
duke@0 322 // public synchronized Object put(Object key, Object value);
duke@0 323 HandleMark hm(THREAD);
duke@0 324 Handle key_str = java_lang_String::create_from_platform_dependent_str(key, CHECK);
duke@0 325 Handle value_str = java_lang_String::create_from_platform_dependent_str((value != NULL ? value : ""), CHECK);
duke@0 326 JavaCalls::call_virtual(&r,
duke@0 327 props,
never@1142 328 KlassHandle(THREAD, SystemDictionary::Properties_klass()),
coleenp@2062 329 vmSymbols::put_name(),
coleenp@2062 330 vmSymbols::object_object_object_signature(),
duke@0 331 key_str,
duke@0 332 value_str,
duke@0 333 THREAD);
duke@0 334 }
duke@0 335
duke@0 336
duke@0 337 #define PUTPROP(props, name, value) set_property((props), (name), (value), CHECK_(properties));
duke@0 338
duke@0 339
duke@0 340 JVM_ENTRY(jobject, JVM_InitProperties(JNIEnv *env, jobject properties))
duke@0 341 JVMWrapper("JVM_InitProperties");
duke@0 342 ResourceMark rm;
duke@0 343
duke@0 344 Handle props(THREAD, JNIHandles::resolve_non_null(properties));
duke@0 345
duke@0 346 // System property list includes both user set via -D option and
duke@0 347 // jvm system specific properties.
duke@0 348 for (SystemProperty* p = Arguments::system_properties(); p != NULL; p = p->next()) {
duke@0 349 PUTPROP(props, p->key(), p->value());
duke@0 350 }
duke@0 351
duke@0 352 // Convert the -XX:MaxDirectMemorySize= command line flag
duke@0 353 // to the sun.nio.MaxDirectMemorySize property.
duke@0 354 // Do this after setting user properties to prevent people
duke@0 355 // from setting the value with a -D option, as requested.
duke@0 356 {
dholmes@3467 357 if (FLAG_IS_DEFAULT(MaxDirectMemorySize)) {
dholmes@3467 358 PUTPROP(props, "sun.nio.MaxDirectMemorySize", "-1");
dholmes@3467 359 } else {
dholmes@3467 360 char as_chars[256];
dholmes@3467 361 jio_snprintf(as_chars, sizeof(as_chars), UINTX_FORMAT, MaxDirectMemorySize);
dholmes@3467 362 PUTPROP(props, "sun.nio.MaxDirectMemorySize", as_chars);
dholmes@3467 363 }
duke@0 364 }
duke@0 365
duke@0 366 // JVM monitoring and management support
duke@0 367 // Add the sun.management.compiler property for the compiler's name
duke@0 368 {
duke@0 369 #undef CSIZE
duke@0 370 #if defined(_LP64) || defined(_WIN64)
duke@0 371 #define CSIZE "64-Bit "
duke@0 372 #else
duke@0 373 #define CSIZE
duke@0 374 #endif // 64bit
duke@0 375
duke@0 376 #ifdef TIERED
duke@0 377 const char* compiler_name = "HotSpot " CSIZE "Tiered Compilers";
duke@0 378 #else
duke@0 379 #if defined(COMPILER1)
duke@0 380 const char* compiler_name = "HotSpot " CSIZE "Client Compiler";
duke@0 381 #elif defined(COMPILER2)
duke@0 382 const char* compiler_name = "HotSpot " CSIZE "Server Compiler";
duke@0 383 #else
duke@0 384 const char* compiler_name = "";
duke@0 385 #endif // compilers
duke@0 386 #endif // TIERED
duke@0 387
duke@0 388 if (*compiler_name != '\0' &&
duke@0 389 (Arguments::mode() != Arguments::_int)) {
duke@0 390 PUTPROP(props, "sun.management.compiler", compiler_name);
duke@0 391 }
duke@0 392 }
duke@0 393
duke@0 394 return properties;
duke@0 395 JVM_END
duke@0 396
duke@0 397
sla@6353 398 /*
sla@6353 399 * Return the temporary directory that the VM uses for the attach
sla@6353 400 * and perf data files.
sla@6353 401 *
sla@6353 402 * It is important that this directory is well-known and the
sla@6353 403 * same for all VM instances. It cannot be affected by configuration
sla@6353 404 * variables such as java.io.tmpdir.
sla@6353 405 */
sla@6353 406 JVM_ENTRY(jstring, JVM_GetTemporaryDirectory(JNIEnv *env))
sla@6353 407 JVMWrapper("JVM_GetTemporaryDirectory");
sla@6353 408 HandleMark hm(THREAD);
sla@6353 409 const char* temp_dir = os::get_temp_directory();
sla@6353 410 Handle h = java_lang_String::create_from_platform_dependent_str(temp_dir, CHECK_NULL);
sla@6353 411 return (jstring) JNIHandles::make_local(env, h());
sla@6353 412 JVM_END
sla@6353 413
sla@6353 414
duke@0 415 // java.lang.Runtime /////////////////////////////////////////////////////////////////////////
duke@0 416
duke@0 417 extern volatile jint vm_created;
duke@0 418
duke@0 419 JVM_ENTRY_NO_ENV(void, JVM_Halt(jint code))
duke@0 420 before_exit(thread);
duke@0 421 vm_exit(code);
duke@0 422 JVM_END
duke@0 423
duke@0 424
duke@0 425 JVM_ENTRY_NO_ENV(void, JVM_GC(void))
duke@0 426 JVMWrapper("JVM_GC");
duke@0 427 if (!DisableExplicitGC) {
duke@0 428 Universe::heap()->collect(GCCause::_java_lang_system_gc);
duke@0 429 }
duke@0 430 JVM_END
duke@0 431
duke@0 432
duke@0 433 JVM_LEAF(jlong, JVM_MaxObjectInspectionAge(void))
duke@0 434 JVMWrapper("JVM_MaxObjectInspectionAge");
duke@0 435 return Universe::heap()->millis_since_last_gc();
duke@0 436 JVM_END
duke@0 437
duke@0 438
duke@0 439 static inline jlong convert_size_t_to_jlong(size_t val) {
duke@0 440 // In the 64-bit vm, a size_t can overflow a jlong (which is signed).
duke@0 441 NOT_LP64 (return (jlong)val;)
duke@0 442 LP64_ONLY(return (jlong)MIN2(val, (size_t)max_jlong);)
duke@0 443 }
duke@0 444
duke@0 445 JVM_ENTRY_NO_ENV(jlong, JVM_TotalMemory(void))
duke@0 446 JVMWrapper("JVM_TotalMemory");
duke@0 447 size_t n = Universe::heap()->capacity();
duke@0 448 return convert_size_t_to_jlong(n);
duke@0 449 JVM_END
duke@0 450
duke@0 451
duke@0 452 JVM_ENTRY_NO_ENV(jlong, JVM_FreeMemory(void))
duke@0 453 JVMWrapper("JVM_FreeMemory");
duke@0 454 CollectedHeap* ch = Universe::heap();
ysr@342 455 size_t n;
ysr@342 456 {
ysr@342 457 MutexLocker x(Heap_lock);
ysr@342 458 n = ch->capacity() - ch->used();
ysr@342 459 }
duke@0 460 return convert_size_t_to_jlong(n);
duke@0 461 JVM_END
duke@0 462
duke@0 463
duke@0 464 JVM_ENTRY_NO_ENV(jlong, JVM_MaxMemory(void))
duke@0 465 JVMWrapper("JVM_MaxMemory");
duke@0 466 size_t n = Universe::heap()->max_capacity();
duke@0 467 return convert_size_t_to_jlong(n);
duke@0 468 JVM_END
duke@0 469
duke@0 470
duke@0 471 JVM_ENTRY_NO_ENV(jint, JVM_ActiveProcessorCount(void))
duke@0 472 JVMWrapper("JVM_ActiveProcessorCount");
duke@0 473 return os::active_processor_count();
duke@0 474 JVM_END
duke@0 475
duke@0 476
duke@0 477
duke@0 478 // java.lang.Throwable //////////////////////////////////////////////////////
duke@0 479
duke@0 480
duke@0 481 JVM_ENTRY(void, JVM_FillInStackTrace(JNIEnv *env, jobject receiver))
duke@0 482 JVMWrapper("JVM_FillInStackTrace");
duke@0 483 Handle exception(thread, JNIHandles::resolve_non_null(receiver));
duke@0 484 java_lang_Throwable::fill_in_stack_trace(exception);
duke@0 485 JVM_END
duke@0 486
duke@0 487
duke@0 488 JVM_ENTRY(jint, JVM_GetStackTraceDepth(JNIEnv *env, jobject throwable))
duke@0 489 JVMWrapper("JVM_GetStackTraceDepth");
duke@0 490 oop exception = JNIHandles::resolve(throwable);
duke@0 491 return java_lang_Throwable::get_stack_trace_depth(exception, THREAD);
duke@0 492 JVM_END
duke@0 493
duke@0 494
duke@0 495 JVM_ENTRY(jobject, JVM_GetStackTraceElement(JNIEnv *env, jobject throwable, jint index))
duke@0 496 JVMWrapper("JVM_GetStackTraceElement");
duke@0 497 JvmtiVMObjectAllocEventCollector oam; // This ctor (throughout this module) may trigger a safepoint/GC
duke@0 498 oop exception = JNIHandles::resolve(throwable);
duke@0 499 oop element = java_lang_Throwable::get_stack_trace_element(exception, index, CHECK_NULL);
duke@0 500 return JNIHandles::make_local(env, element);
duke@0 501 JVM_END
duke@0 502
duke@0 503
duke@0 504 // java.lang.Object ///////////////////////////////////////////////
duke@0 505
duke@0 506
duke@0 507 JVM_ENTRY(jint, JVM_IHashCode(JNIEnv* env, jobject handle))
duke@0 508 JVMWrapper("JVM_IHashCode");
duke@0 509 // as implemented in the classic virtual machine; return 0 if object is NULL
duke@0 510 return handle == NULL ? 0 : ObjectSynchronizer::FastHashCode (THREAD, JNIHandles::resolve_non_null(handle)) ;
duke@0 511 JVM_END
duke@0 512
duke@0 513
duke@0 514 JVM_ENTRY(void, JVM_MonitorWait(JNIEnv* env, jobject handle, jlong ms))
duke@0 515 JVMWrapper("JVM_MonitorWait");
duke@0 516 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
duke@0 517 JavaThreadInObjectWaitState jtiows(thread, ms != 0);
duke@0 518 if (JvmtiExport::should_post_monitor_wait()) {
duke@0 519 JvmtiExport::post_monitor_wait((JavaThread *)THREAD, (oop)obj(), ms);
dcubed@5891 520
dcubed@5891 521 // The current thread already owns the monitor and it has not yet
dcubed@5891 522 // been added to the wait queue so the current thread cannot be
dcubed@5891 523 // made the successor. This means that the JVMTI_EVENT_MONITOR_WAIT
dcubed@5891 524 // event handler cannot accidentally consume an unpark() meant for
dcubed@5891 525 // the ParkEvent associated with this ObjectMonitor.
duke@0 526 }
duke@0 527 ObjectSynchronizer::wait(obj, ms, CHECK);
duke@0 528 JVM_END
duke@0 529
duke@0 530
duke@0 531 JVM_ENTRY(void, JVM_MonitorNotify(JNIEnv* env, jobject handle))
duke@0 532 JVMWrapper("JVM_MonitorNotify");
duke@0 533 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
duke@0 534 ObjectSynchronizer::notify(obj, CHECK);
duke@0 535 JVM_END
duke@0 536
duke@0 537
duke@0 538 JVM_ENTRY(void, JVM_MonitorNotifyAll(JNIEnv* env, jobject handle))
duke@0 539 JVMWrapper("JVM_MonitorNotifyAll");
duke@0 540 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
duke@0 541 ObjectSynchronizer::notifyall(obj, CHECK);
duke@0 542 JVM_END
duke@0 543
duke@0 544
duke@0 545 JVM_ENTRY(jobject, JVM_Clone(JNIEnv* env, jobject handle))
duke@0 546 JVMWrapper("JVM_Clone");
duke@0 547 Handle obj(THREAD, JNIHandles::resolve_non_null(handle));
duke@0 548 const KlassHandle klass (THREAD, obj->klass());
duke@0 549 JvmtiVMObjectAllocEventCollector oam;
duke@0 550
duke@0 551 #ifdef ASSERT
duke@0 552 // Just checking that the cloneable flag is set correct
coleenp@3602 553 if (obj->is_array()) {
duke@0 554 guarantee(klass->is_cloneable(), "all arrays are cloneable");
duke@0 555 } else {
duke@0 556 guarantee(obj->is_instance(), "should be instanceOop");
never@1142 557 bool cloneable = klass->is_subtype_of(SystemDictionary::Cloneable_klass());
duke@0 558 guarantee(cloneable == klass->is_cloneable(), "incorrect cloneable flag");
duke@0 559 }
duke@0 560 #endif
duke@0 561
duke@0 562 // Check if class of obj supports the Cloneable interface.
duke@0 563 // All arrays are considered to be cloneable (See JLS 20.1.5)
duke@0 564 if (!klass->is_cloneable()) {
duke@0 565 ResourceMark rm(THREAD);
duke@0 566 THROW_MSG_0(vmSymbols::java_lang_CloneNotSupportedException(), klass->external_name());
duke@0 567 }
duke@0 568
duke@0 569 // Make shallow object copy
duke@0 570 const int size = obj->size();
coleenp@7415 571 oop new_obj_oop = NULL;
coleenp@3602 572 if (obj->is_array()) {
duke@0 573 const int length = ((arrayOop)obj())->length();
coleenp@7415 574 new_obj_oop = CollectedHeap::array_allocate(klass, size, length, CHECK_NULL);
duke@0 575 } else {
coleenp@7415 576 new_obj_oop = CollectedHeap::obj_allocate(klass, size, CHECK_NULL);
duke@0 577 }
coleenp@7415 578
duke@0 579 // 4839641 (4840070): We must do an oop-atomic copy, because if another thread
duke@0 580 // is modifying a reference field in the clonee, a non-oop-atomic copy might
duke@0 581 // be suspended in the middle of copying the pointer and end up with parts
duke@0 582 // of two different pointers in the field. Subsequent dereferences will crash.
duke@0 583 // 4846409: an oop-copy of objects with long or double fields or arrays of same
duke@0 584 // won't copy the longs/doubles atomically in 32-bit vm's, so we copy jlongs instead
duke@0 585 // of oops. We know objects are aligned on a minimum of an jlong boundary.
duke@0 586 // The same is true of StubRoutines::object_copy and the various oop_copy
duke@0 587 // variants, and of the code generated by the inline_native_clone intrinsic.
duke@0 588 assert(MinObjAlignmentInBytes >= BytesPerLong, "objects misaligned");
coleenp@7415 589 Copy::conjoint_jlongs_atomic((jlong*)obj(), (jlong*)new_obj_oop,
duke@0 590 (size_t)align_object_size(size) / HeapWordsPerLong);
duke@0 591 // Clear the header
coleenp@7415 592 new_obj_oop->init_mark();
duke@0 593
duke@0 594 // Store check (mark entire object and let gc sort it out)
duke@0 595 BarrierSet* bs = Universe::heap()->barrier_set();
duke@0 596 assert(bs->has_write_region_opt(), "Barrier set does not have write_region");
coleenp@7415 597 bs->write_region(MemRegion((HeapWord*)new_obj_oop, size));
coleenp@7415 598
coleenp@7415 599 Handle new_obj(THREAD, new_obj_oop);
coleenp@7415 600 // Special handling for MemberNames. Since they contain Method* metadata, they
coleenp@7415 601 // must be registered so that RedefineClasses can fix metadata contained in them.
coleenp@7415 602 if (java_lang_invoke_MemberName::is_instance(new_obj()) &&
coleenp@7415 603 java_lang_invoke_MemberName::is_method(new_obj())) {
coleenp@7415 604 Method* method = (Method*)java_lang_invoke_MemberName::vmtarget(new_obj());
coleenp@7415 605 // MemberName may be unresolved, so doesn't need registration until resolved.
coleenp@7415 606 if (method != NULL) {
coleenp@7415 607 methodHandle m(THREAD, method);
coleenp@7415 608 // This can safepoint and redefine method, so need both new_obj and method
coleenp@7415 609 // in a handle, for two different reasons. new_obj can move, method can be
coleenp@7415 610 // deleted if nothing is using it on the stack.
coleenp@7415 611 m->method_holder()->add_member_name(new_obj());
coleenp@7415 612 }
coleenp@7415 613 }
duke@0 614
duke@0 615 // Caution: this involves a java upcall, so the clone should be
duke@0 616 // "gc-robust" by this stage.
duke@0 617 if (klass->has_finalizer()) {
duke@0 618 assert(obj->is_instance(), "should be instanceOop");
coleenp@7415 619 new_obj_oop = InstanceKlass::register_finalizer(instanceOop(new_obj()), CHECK_NULL);
coleenp@7415 620 new_obj = Handle(THREAD, new_obj_oop);
duke@0 621 }
duke@0 622
coleenp@7415 623 return JNIHandles::make_local(env, new_obj());
duke@0 624 JVM_END
duke@0 625
duke@0 626 // java.io.File ///////////////////////////////////////////////////////////////
duke@0 627
duke@0 628 JVM_LEAF(char*, JVM_NativePath(char* path))
duke@0 629 JVMWrapper2("JVM_NativePath (%s)", path);
ikrylov@1887 630 return os::native_path(path);
duke@0 631 JVM_END
duke@0 632
duke@0 633
duke@0 634 // Misc. class handling ///////////////////////////////////////////////////////////
duke@0 635
duke@0 636
duke@0 637 JVM_ENTRY(jclass, JVM_GetCallerClass(JNIEnv* env, int depth))
duke@0 638 JVMWrapper("JVM_GetCallerClass");
twisti@4431 639
dholmes@5414 640 // Pre-JDK 8 and early builds of JDK 8 don't have a CallerSensitive annotation; or
dholmes@5414 641 // sun.reflect.Reflection.getCallerClass with a depth parameter is provided
dholmes@5414 642 // temporarily for existing code to use until a replacement API is defined.
dholmes@5414 643 if (SystemDictionary::reflect_CallerSensitive_klass() == NULL || depth != JVM_CALLER_DEPTH) {
twisti@4431 644 Klass* k = thread->security_get_caller_class(depth);
twisti@4431 645 return (k == NULL) ? NULL : (jclass) JNIHandles::make_local(env, k->java_mirror());
twisti@4431 646 }
twisti@4431 647
twisti@4431 648 // Getting the class of the caller frame.
twisti@4431 649 //
twisti@4431 650 // The call stack at this point looks something like this:
twisti@4431 651 //
twisti@4431 652 // [0] [ @CallerSensitive public sun.reflect.Reflection.getCallerClass ]
twisti@4431 653 // [1] [ @CallerSensitive API.method ]
twisti@4431 654 // [.] [ (skipped intermediate frames) ]
twisti@4431 655 // [n] [ caller ]
twisti@4431 656 vframeStream vfst(thread);
twisti@4431 657 // Cf. LibraryCallKit::inline_native_Reflection_getCallerClass
twisti@4431 658 for (int n = 0; !vfst.at_end(); vfst.security_next(), n++) {
twisti@4431 659 Method* m = vfst.method();
twisti@4431 660 assert(m != NULL, "sanity");
twisti@4431 661 switch (n) {
twisti@4431 662 case 0:
twisti@4431 663 // This must only be called from Reflection.getCallerClass
twisti@4431 664 if (m->intrinsic_id() != vmIntrinsics::_getCallerClass) {
twisti@4431 665 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetCallerClass must only be called from Reflection.getCallerClass");
twisti@4431 666 }
twisti@4431 667 // fall-through
twisti@4431 668 case 1:
twisti@4431 669 // Frame 0 and 1 must be caller sensitive.
twisti@4431 670 if (!m->caller_sensitive()) {
twisti@4431 671 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), err_msg("CallerSensitive annotation expected at frame %d", n));
twisti@4431 672 }
twisti@4431 673 break;
twisti@4431 674 default:
twisti@4431 675 if (!m->is_ignored_by_security_stack_walk()) {
twisti@4431 676 // We have reached the desired frame; return the holder class.
twisti@4431 677 return (jclass) JNIHandles::make_local(env, m->method_holder()->java_mirror());
twisti@4431 678 }
twisti@4431 679 break;
twisti@4431 680 }
twisti@4431 681 }
twisti@4431 682 return NULL;
duke@0 683 JVM_END
duke@0 684
duke@0 685
duke@0 686 JVM_ENTRY(jclass, JVM_FindPrimitiveClass(JNIEnv* env, const char* utf))
duke@0 687 JVMWrapper("JVM_FindPrimitiveClass");
duke@0 688 oop mirror = NULL;
duke@0 689 BasicType t = name2type(utf);
duke@0 690 if (t != T_ILLEGAL && t != T_OBJECT && t != T_ARRAY) {
duke@0 691 mirror = Universe::java_mirror(t);
duke@0 692 }
duke@0 693 if (mirror == NULL) {
duke@0 694 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), (char*) utf);
duke@0 695 } else {
duke@0 696 return (jclass) JNIHandles::make_local(env, mirror);
duke@0 697 }
duke@0 698 JVM_END
duke@0 699
duke@0 700
mchung@878 701 // Returns a class loaded by the bootstrap class loader; or null
mchung@878 702 // if not found. ClassNotFoundException is not thrown.
mchung@878 703 //
ksrini@226 704 // Rationale behind JVM_FindClassFromBootLoader
ksrini@226 705 // a> JVM_FindClassFromClassLoader was never exported in the export tables.
ksrini@226 706 // b> because of (a) java.dll has a direct dependecy on the unexported
ksrini@226 707 // private symbol "_JVM_FindClassFromClassLoader@20".
ksrini@226 708 // c> the launcher cannot use the private symbol as it dynamically opens
ksrini@226 709 // the entry point, so if something changes, the launcher will fail
ksrini@226 710 // unexpectedly at runtime, it is safest for the launcher to dlopen a
ksrini@226 711 // stable exported interface.
ksrini@226 712 // d> re-exporting JVM_FindClassFromClassLoader as public, will cause its
ksrini@226 713 // signature to change from _JVM_FindClassFromClassLoader@20 to
ksrini@226 714 // JVM_FindClassFromClassLoader and will not be backward compatible
ksrini@226 715 // with older JDKs.
ksrini@226 716 // Thus a public/stable exported entry point is the right solution,
ksrini@226 717 // public here means public in linker semantics, and is exported only
ksrini@226 718 // to the JDK, and is not intended to be a public API.
ksrini@226 719
ksrini@226 720 JVM_ENTRY(jclass, JVM_FindClassFromBootLoader(JNIEnv* env,
mchung@878 721 const char* name))
mchung@878 722 JVMWrapper2("JVM_FindClassFromBootLoader %s", name);
mchung@878 723
mchung@878 724 // Java libraries should ensure that name is never null...
coleenp@2062 725 if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
mchung@878 726 // It's impossible to create this class; the name cannot fit
mchung@878 727 // into the constant pool.
mchung@878 728 return NULL;
mchung@878 729 }
mchung@878 730
coleenp@2062 731 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL);
coleenp@3602 732 Klass* k = SystemDictionary::resolve_or_null(h_name, CHECK_NULL);
mchung@878 733 if (k == NULL) {
mchung@878 734 return NULL;
mchung@878 735 }
mchung@878 736
mchung@878 737 if (TraceClassResolution) {
mchung@878 738 trace_class_resolution(k);
mchung@878 739 }
hseigel@3843 740 return (jclass) JNIHandles::make_local(env, k->java_mirror());
ksrini@226 741 JVM_END
duke@0 742
coleenp@7206 743 // Not used; JVM_FindClassFromCaller replaces this.
duke@0 744 JVM_ENTRY(jclass, JVM_FindClassFromClassLoader(JNIEnv* env, const char* name,
duke@0 745 jboolean init, jobject loader,
duke@0 746 jboolean throwError))
duke@0 747 JVMWrapper3("JVM_FindClassFromClassLoader %s throw %s", name,
duke@0 748 throwError ? "error" : "exception");
mchung@878 749 // Java libraries should ensure that name is never null...
coleenp@2062 750 if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
mchung@878 751 // It's impossible to create this class; the name cannot fit
mchung@878 752 // into the constant pool.
mchung@878 753 if (throwError) {
mchung@878 754 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
mchung@878 755 } else {
mchung@878 756 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
mchung@878 757 }
mchung@878 758 }
coleenp@2062 759 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL);
mchung@878 760 Handle h_loader(THREAD, JNIHandles::resolve(loader));
mchung@878 761 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
mchung@878 762 Handle(), throwError, THREAD);
mchung@878 763
mchung@878 764 if (TraceClassResolution && result != NULL) {
coleenp@3602 765 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
mchung@878 766 }
mchung@878 767 return result;
duke@0 768 JVM_END
duke@0 769
coleenp@7206 770 // Find a class with this name in this loader, using the caller's protection domain.
coleenp@7206 771 JVM_ENTRY(jclass, JVM_FindClassFromCaller(JNIEnv* env, const char* name,
coleenp@7206 772 jboolean init, jobject loader,
coleenp@7206 773 jclass caller))
coleenp@7206 774 JVMWrapper2("JVM_FindClassFromCaller %s throws ClassNotFoundException", name);
coleenp@7206 775 // Java libraries should ensure that name is never null...
coleenp@7206 776 if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
coleenp@7206 777 // It's impossible to create this class; the name cannot fit
coleenp@7206 778 // into the constant pool.
coleenp@7206 779 THROW_MSG_0(vmSymbols::java_lang_ClassNotFoundException(), name);
coleenp@7206 780 }
coleenp@7206 781
coleenp@7206 782 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL);
coleenp@7206 783
coleenp@7206 784 oop loader_oop = JNIHandles::resolve(loader);
coleenp@7206 785 oop from_class = JNIHandles::resolve(caller);
coleenp@7206 786 oop protection_domain = NULL;
coleenp@7206 787 // If loader is null, shouldn't call ClassLoader.checkPackageAccess; otherwise get
coleenp@7206 788 // NPE. Put it in another way, the bootstrap class loader has all permission and
coleenp@7206 789 // thus no checkPackageAccess equivalence in the VM class loader.
coleenp@7206 790 // The caller is also passed as NULL by the java code if there is no security
coleenp@7206 791 // manager to avoid the performance cost of getting the calling class.
coleenp@7206 792 if (from_class != NULL && loader_oop != NULL) {
coleenp@7206 793 protection_domain = java_lang_Class::as_Klass(from_class)->protection_domain();
coleenp@7206 794 }
coleenp@7206 795
coleenp@7206 796 Handle h_loader(THREAD, loader_oop);
coleenp@7206 797 Handle h_prot(THREAD, protection_domain);
coleenp@7206 798 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
coleenp@7206 799 h_prot, false, THREAD);
coleenp@7206 800
coleenp@7206 801 if (TraceClassResolution && result != NULL) {
coleenp@7206 802 trace_class_resolution(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(result)));
coleenp@7206 803 }
coleenp@7206 804 return result;
coleenp@7206 805 JVM_END
duke@0 806
duke@0 807 JVM_ENTRY(jclass, JVM_FindClassFromClass(JNIEnv *env, const char *name,
duke@0 808 jboolean init, jclass from))
duke@0 809 JVMWrapper2("JVM_FindClassFromClass %s", name);
coleenp@2062 810 if (name == NULL || (int)strlen(name) > Symbol::max_length()) {
duke@0 811 // It's impossible to create this class; the name cannot fit
duke@0 812 // into the constant pool.
duke@0 813 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
duke@0 814 }
coleenp@2062 815 TempNewSymbol h_name = SymbolTable::new_symbol(name, CHECK_NULL);
duke@0 816 oop from_class_oop = JNIHandles::resolve(from);
coleenp@3602 817 Klass* from_class = (from_class_oop == NULL)
coleenp@3602 818 ? (Klass*)NULL
coleenp@3602 819 : java_lang_Class::as_Klass(from_class_oop);
duke@0 820 oop class_loader = NULL;
duke@0 821 oop protection_domain = NULL;
duke@0 822 if (from_class != NULL) {
hseigel@3843 823 class_loader = from_class->class_loader();
hseigel@3843 824 protection_domain = from_class->protection_domain();
duke@0 825 }
duke@0 826 Handle h_loader(THREAD, class_loader);
duke@0 827 Handle h_prot (THREAD, protection_domain);
duke@0 828 jclass result = find_class_from_class_loader(env, h_name, init, h_loader,
duke@0 829 h_prot, true, thread);
duke@0 830
duke@0 831 if (TraceClassResolution && result != NULL) {
duke@0 832 // this function is generally only used for class loading during verification.
duke@0 833 ResourceMark rm;
duke@0 834 oop from_mirror = JNIHandles::resolve_non_null(from);
coleenp@3602 835 Klass* from_class = java_lang_Class::as_Klass(from_mirror);
hseigel@3843 836 const char * from_name = from_class->external_name();
duke@0 837
duke@0 838 oop mirror = JNIHandles::resolve_non_null(result);
coleenp@3602 839 Klass* to_class = java_lang_Class::as_Klass(mirror);
hseigel@3843 840 const char * to = to_class->external_name();
duke@0 841 tty->print("RESOLVE %s %s (verification)\n", from_name, to);
duke@0 842 }
duke@0 843
duke@0 844 return result;
duke@0 845 JVM_END
duke@0 846
duke@0 847 static void is_lock_held_by_thread(Handle loader, PerfCounter* counter, TRAPS) {
duke@0 848 if (loader.is_null()) {
duke@0 849 return;
duke@0 850 }
duke@0 851
duke@0 852 // check whether the current caller thread holds the lock or not.
duke@0 853 // If not, increment the corresponding counter
duke@0 854 if (ObjectSynchronizer::query_lock_ownership((JavaThread*)THREAD, loader) !=
duke@0 855 ObjectSynchronizer::owner_self) {
duke@0 856 counter->inc();
duke@0 857 }
duke@0 858 }
duke@0 859
duke@0 860 // common code for JVM_DefineClass() and JVM_DefineClassWithSource()
acorn@973 861 // and JVM_DefineClassWithSourceCond()
acorn@973 862 static jclass jvm_define_class_common(JNIEnv *env, const char *name,
acorn@973 863 jobject loader, const jbyte *buf,
acorn@973 864 jsize len, jobject pd, const char *source,
acorn@973 865 jboolean verify, TRAPS) {
jrose@431 866 if (source == NULL) source = "__JVM_DefineClass__";
duke@0 867
mchung@875 868 assert(THREAD->is_Java_thread(), "must be a JavaThread");
mchung@875 869 JavaThread* jt = (JavaThread*) THREAD;
mchung@875 870
mchung@875 871 PerfClassTraceTime vmtimer(ClassLoader::perf_define_appclass_time(),
mchung@875 872 ClassLoader::perf_define_appclass_selftime(),
mchung@875 873 ClassLoader::perf_define_appclasses(),
mchung@875 874 jt->get_thread_stat()->perf_recursion_counts_addr(),
mchung@875 875 jt->get_thread_stat()->perf_timers_addr(),
mchung@875 876 PerfClassTraceTime::DEFINE_CLASS);
mchung@875 877
mchung@875 878 if (UsePerfData) {
mchung@875 879 ClassLoader::perf_app_classfile_bytes_read()->inc(len);
mchung@875 880 }
mchung@875 881
duke@0 882 // Since exceptions can be thrown, class initialization can take place
duke@0 883 // if name is NULL no check for class name in .class stream has to be made.
coleenp@2062 884 TempNewSymbol class_name = NULL;
duke@0 885 if (name != NULL) {
duke@0 886 const int str_len = (int)strlen(name);
coleenp@2062 887 if (str_len > Symbol::max_length()) {
duke@0 888 // It's impossible to create this class; the name cannot fit
duke@0 889 // into the constant pool.
duke@0 890 THROW_MSG_0(vmSymbols::java_lang_NoClassDefFoundError(), name);
duke@0 891 }
coleenp@2062 892 class_name = SymbolTable::new_symbol(name, str_len, CHECK_NULL);
duke@0 893 }
duke@0 894
duke@0 895 ResourceMark rm(THREAD);
duke@0 896 ClassFileStream st((u1*) buf, len, (char *)source);
duke@0 897 Handle class_loader (THREAD, JNIHandles::resolve(loader));
duke@0 898 if (UsePerfData) {
duke@0 899 is_lock_held_by_thread(class_loader,
duke@0 900 ClassLoader::sync_JVMDefineClassLockFreeCounter(),
duke@0 901 THREAD);
duke@0 902 }
duke@0 903 Handle protection_domain (THREAD, JNIHandles::resolve(pd));
coleenp@3602 904 Klass* k = SystemDictionary::resolve_from_stream(class_name, class_loader,
duke@0 905 protection_domain, &st,
acorn@973 906 verify != 0,
duke@0 907 CHECK_NULL);
duke@0 908
duke@0 909 if (TraceClassResolution && k != NULL) {
duke@0 910 trace_class_resolution(k);
duke@0 911 }
duke@0 912
hseigel@3843 913 return (jclass) JNIHandles::make_local(env, k->java_mirror());
duke@0 914 }
duke@0 915
duke@0 916
duke@0 917 JVM_ENTRY(jclass, JVM_DefineClass(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd))
duke@0 918 JVMWrapper2("JVM_DefineClass %s", name);
duke@0 919
acorn@973 920 return jvm_define_class_common(env, name, loader, buf, len, pd, NULL, true, THREAD);
duke@0 921 JVM_END
duke@0 922
duke@0 923
duke@0 924 JVM_ENTRY(jclass, JVM_DefineClassWithSource(JNIEnv *env, const char *name, jobject loader, const jbyte *buf, jsize len, jobject pd, const char *source))
duke@0 925 JVMWrapper2("JVM_DefineClassWithSource %s", name);
duke@0 926
acorn@973 927 return jvm_define_class_common(env, name, loader, buf, len, pd, source, true, THREAD);
acorn@973 928 JVM_END
acorn@973 929
acorn@973 930 JVM_ENTRY(jclass, JVM_DefineClassWithSourceCond(JNIEnv *env, const char *name,
acorn@973 931 jobject loader, const jbyte *buf,
acorn@973 932 jsize len, jobject pd,
acorn@973 933 const char *source, jboolean verify))
acorn@973 934 JVMWrapper2("JVM_DefineClassWithSourceCond %s", name);
acorn@973 935
acorn@973 936 return jvm_define_class_common(env, name, loader, buf, len, pd, source, verify, THREAD);
acorn@973 937 JVM_END
duke@0 938
duke@0 939 JVM_ENTRY(jclass, JVM_FindLoadedClass(JNIEnv *env, jobject loader, jstring name))
duke@0 940 JVMWrapper("JVM_FindLoadedClass");
duke@0 941 ResourceMark rm(THREAD);
duke@0 942
duke@0 943 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
duke@0 944 Handle string = java_lang_String::internalize_classname(h_name, CHECK_NULL);
duke@0 945
duke@0 946 const char* str = java_lang_String::as_utf8_string(string());
duke@0 947 // Sanity check, don't expect null
duke@0 948 if (str == NULL) return NULL;
duke@0 949
duke@0 950 const int str_len = (int)strlen(str);
coleenp@2062 951 if (str_len > Symbol::max_length()) {
duke@0 952 // It's impossible to create this class; the name cannot fit
duke@0 953 // into the constant pool.
duke@0 954 return NULL;
duke@0 955 }
coleenp@2062 956 TempNewSymbol klass_name = SymbolTable::new_symbol(str, str_len, CHECK_NULL);
duke@0 957
duke@0 958 // Security Note:
duke@0 959 // The Java level wrapper will perform the necessary security check allowing
duke@0 960 // us to pass the NULL as the initiating class loader.
duke@0 961 Handle h_loader(THREAD, JNIHandles::resolve(loader));
duke@0 962 if (UsePerfData) {
duke@0 963 is_lock_held_by_thread(h_loader,
duke@0 964 ClassLoader::sync_JVMFindLoadedClassLockFreeCounter(),
duke@0 965 THREAD);
duke@0 966 }
duke@0 967
coleenp@3602 968 Klass* k = SystemDictionary::find_instance_or_array_klass(klass_name,
duke@0 969 h_loader,
duke@0 970 Handle(),
duke@0 971 CHECK_NULL);
iklam@6872 972 #if INCLUDE_CDS
iklam@6872 973 if (k == NULL) {
iklam@6872 974 // If the class is not already loaded, try to see if it's in the shared
iklam@6872 975 // archive for the current classloader (h_loader).
iklam@6872 976 instanceKlassHandle ik = SystemDictionaryShared::find_or_load_shared_class(
iklam@6872 977 klass_name, h_loader, CHECK_NULL);
iklam@6872 978 k = ik();
iklam@6872 979 }
iklam@6872 980 #endif
duke@0 981 return (k == NULL) ? NULL :
hseigel@3843 982 (jclass) JNIHandles::make_local(env, k->java_mirror());
duke@0 983 JVM_END
duke@0 984
duke@0 985
duke@0 986 // Reflection support //////////////////////////////////////////////////////////////////////////////
duke@0 987
duke@0 988 JVM_ENTRY(jstring, JVM_GetClassName(JNIEnv *env, jclass cls))
duke@0 989 assert (cls != NULL, "illegal class");
duke@0 990 JVMWrapper("JVM_GetClassName");
duke@0 991 JvmtiVMObjectAllocEventCollector oam;
duke@0 992 ResourceMark rm(THREAD);
duke@0 993 const char* name;
duke@0 994 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
duke@0 995 name = type2name(java_lang_Class::primitive_type(JNIHandles::resolve(cls)));
duke@0 996 } else {
duke@0 997 // Consider caching interned string in Klass
coleenp@3602 998 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
duke@0 999 assert(k->is_klass(), "just checking");
hseigel@3843 1000 name = k->external_name();
duke@0 1001 }
duke@0 1002 oop result = StringTable::intern((char*) name, CHECK_NULL);
duke@0 1003 return (jstring) JNIHandles::make_local(env, result);
duke@0 1004 JVM_END
duke@0 1005
duke@0 1006
duke@0 1007 JVM_ENTRY(jobjectArray, JVM_GetClassInterfaces(JNIEnv *env, jclass cls))
duke@0 1008 JVMWrapper("JVM_GetClassInterfaces");
duke@0 1009 JvmtiVMObjectAllocEventCollector oam;
duke@0 1010 oop mirror = JNIHandles::resolve_non_null(cls);
duke@0 1011
duke@0 1012 // Special handling for primitive objects
duke@0 1013 if (java_lang_Class::is_primitive(mirror)) {
duke@0 1014 // Primitive objects does not have any interfaces
never@1142 1015 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
duke@0 1016 return (jobjectArray) JNIHandles::make_local(env, r);
duke@0 1017 }
duke@0 1018
coleenp@3602 1019 KlassHandle klass(thread, java_lang_Class::as_Klass(mirror));
duke@0 1020 // Figure size of result array
duke@0 1021 int size;
duke@0 1022 if (klass->oop_is_instance()) {
coleenp@3602 1023 size = InstanceKlass::cast(klass())->local_interfaces()->length();
duke@0 1024 } else {
duke@0 1025 assert(klass->oop_is_objArray() || klass->oop_is_typeArray(), "Illegal mirror klass");
duke@0 1026 size = 2;
duke@0 1027 }
duke@0 1028
duke@0 1029 // Allocate result array
never@1142 1030 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), size, CHECK_NULL);
duke@0 1031 objArrayHandle result (THREAD, r);
duke@0 1032 // Fill in result
duke@0 1033 if (klass->oop_is_instance()) {
duke@0 1034 // Regular instance klass, fill in all local interfaces
duke@0 1035 for (int index = 0; index < size; index++) {
coleenp@3602 1036 Klass* k = InstanceKlass::cast(klass())->local_interfaces()->at(index);
hseigel@3843 1037 result->obj_at_put(index, k->java_mirror());
duke@0 1038 }
duke@0 1039 } else {
duke@0 1040 // All arrays implement java.lang.Cloneable and java.io.Serializable
hseigel@3843 1041 result->obj_at_put(0, SystemDictionary::Cloneable_klass()->java_mirror());
hseigel@3843 1042 result->obj_at_put(1, SystemDictionary::Serializable_klass()->java_mirror());
duke@0 1043 }
duke@0 1044 return (jobjectArray) JNIHandles::make_local(env, result());
duke@0 1045 JVM_END
duke@0 1046
duke@0 1047
duke@0 1048 JVM_QUICK_ENTRY(jboolean, JVM_IsInterface(JNIEnv *env, jclass cls))
duke@0 1049 JVMWrapper("JVM_IsInterface");
duke@0 1050 oop mirror = JNIHandles::resolve_non_null(cls);
duke@0 1051 if (java_lang_Class::is_primitive(mirror)) {
duke@0 1052 return JNI_FALSE;
duke@0 1053 }
coleenp@3602 1054 Klass* k = java_lang_Class::as_Klass(mirror);
hseigel@3843 1055 jboolean result = k->is_interface();
hseigel@3843 1056 assert(!result || k->oop_is_instance(),
duke@0 1057 "all interfaces are instance types");
duke@0 1058 // The compiler intrinsic for isInterface tests the
duke@0 1059 // Klass::_access_flags bits in the same way.
duke@0 1060 return result;
duke@0 1061 JVM_END
duke@0 1062
duke@0 1063
duke@0 1064 JVM_ENTRY(jobjectArray, JVM_GetClassSigners(JNIEnv *env, jclass cls))
duke@0 1065 JVMWrapper("JVM_GetClassSigners");
duke@0 1066 JvmtiVMObjectAllocEventCollector oam;
duke@0 1067 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
duke@0 1068 // There are no signers for primitive types
duke@0 1069 return NULL;
duke@0 1070 }
duke@0 1071
coleenp@4741 1072 objArrayOop signers = java_lang_Class::signers(JNIHandles::resolve_non_null(cls));
duke@0 1073
duke@0 1074 // If there are no signers set in the class, or if the class
duke@0 1075 // is an array, return NULL.
duke@0 1076 if (signers == NULL) return NULL;
duke@0 1077
duke@0 1078 // copy of the signers array
coleenp@3707 1079 Klass* element = ObjArrayKlass::cast(signers->klass())->element_klass();
duke@0 1080 objArrayOop signers_copy = oopFactory::new_objArray(element, signers->length(), CHECK_NULL);
duke@0 1081 for (int index = 0; index < signers->length(); index++) {
duke@0 1082 signers_copy->obj_at_put(index, signers->obj_at(index));
duke@0 1083 }
duke@0 1084
duke@0 1085 // return the copy
duke@0 1086 return (jobjectArray) JNIHandles::make_local(env, signers_copy);
duke@0 1087 JVM_END
duke@0 1088
duke@0 1089
duke@0 1090 JVM_ENTRY(void, JVM_SetClassSigners(JNIEnv *env, jclass cls, jobjectArray signers))
duke@0 1091 JVMWrapper("JVM_SetClassSigners");
duke@0 1092 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
duke@0 1093 // This call is ignored for primitive types and arrays.
duke@0 1094 // Signers are only set once, ClassLoader.java, and thus shouldn't
duke@0 1095 // be called with an array. Only the bootstrap loader creates arrays.
coleenp@3602 1096 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
hseigel@3843 1097 if (k->oop_is_instance()) {
coleenp@4741 1098 java_lang_Class::set_signers(k->java_mirror(), objArrayOop(JNIHandles::resolve(signers)));
duke@0 1099 }
duke@0 1100 }
duke@0 1101 JVM_END
duke@0 1102
duke@0 1103
duke@0 1104 JVM_ENTRY(jobject, JVM_GetProtectionDomain(JNIEnv *env, jclass cls))
duke@0 1105 JVMWrapper("JVM_GetProtectionDomain");
duke@0 1106 if (JNIHandles::resolve(cls) == NULL) {
duke@0 1107 THROW_(vmSymbols::java_lang_NullPointerException(), NULL);
duke@0 1108 }
duke@0 1109
duke@0 1110 if (java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
duke@0 1111 // Primitive types does not have a protection domain.
duke@0 1112 return NULL;
duke@0 1113 }
duke@0 1114
coleenp@4741 1115 oop pd = java_lang_Class::protection_domain(JNIHandles::resolve(cls));
coleenp@4741 1116 return (jobject) JNIHandles::make_local(env, pd);
duke@0 1117 JVM_END
duke@0 1118
duke@0 1119
mullan@4807 1120 static bool is_authorized(Handle context, instanceKlassHandle klass, TRAPS) {
mullan@4807 1121 // If there is a security manager and protection domain, check the access
mullan@4807 1122 // in the protection domain, otherwise it is authorized.
mullan@4807 1123 if (java_lang_System::has_security_manager()) {
mullan@4807 1124
mullan@4807 1125 // For bootstrapping, if pd implies method isn't in the JDK, allow
mullan@4807 1126 // this context to revert to older behavior.
mullan@4807 1127 // In this case the isAuthorized field in AccessControlContext is also not
mullan@4807 1128 // present.
mullan@4807 1129 if (Universe::protection_domain_implies_method() == NULL) {
mullan@4807 1130 return true;
mullan@4807 1131 }
mullan@4807 1132
mullan@4807 1133 // Whitelist certain access control contexts
mullan@4807 1134 if (java_security_AccessControlContext::is_authorized(context)) {
mullan@4807 1135 return true;
mullan@4807 1136 }
mullan@4807 1137
mullan@4807 1138 oop prot = klass->protection_domain();
mullan@4807 1139 if (prot != NULL) {
mullan@4807 1140 // Call pd.implies(new SecurityPermission("createAccessControlContext"))
mullan@4807 1141 // in the new wrapper.
mullan@4807 1142 methodHandle m(THREAD, Universe::protection_domain_implies_method());
mullan@4807 1143 Handle h_prot(THREAD, prot);
mullan@4807 1144 JavaValue result(T_BOOLEAN);
mullan@4807 1145 JavaCallArguments args(h_prot);
mullan@4807 1146 JavaCalls::call(&result, m, &args, CHECK_false);
mullan@4807 1147 return (result.get_jboolean() != 0);
mullan@4807 1148 }
mullan@4807 1149 }
mullan@4807 1150 return true;
mullan@4807 1151 }
mullan@4807 1152
mullan@4807 1153 // Create an AccessControlContext with a protection domain with null codesource
mullan@4807 1154 // and null permissions - which gives no permissions.
mullan@4807 1155 oop create_dummy_access_control_context(TRAPS) {
mullan@4807 1156 InstanceKlass* pd_klass = InstanceKlass::cast(SystemDictionary::ProtectionDomain_klass());
coleenp@6139 1157 Handle obj = pd_klass->allocate_instance_handle(CHECK_NULL);
coleenp@6139 1158 // Call constructor ProtectionDomain(null, null);
coleenp@6139 1159 JavaValue result(T_VOID);
coleenp@6139 1160 JavaCalls::call_special(&result, obj, KlassHandle(THREAD, pd_klass),
coleenp@6139 1161 vmSymbols::object_initializer_name(),
coleenp@6139 1162 vmSymbols::codesource_permissioncollection_signature(),
coleenp@6139 1163 Handle(), Handle(), CHECK_NULL);
mullan@4807 1164
mullan@4807 1165 // new ProtectionDomain[] {pd};
mullan@4807 1166 objArrayOop context = oopFactory::new_objArray(pd_klass, 1, CHECK_NULL);
coleenp@6139 1167 context->obj_at_put(0, obj());
mullan@4807 1168
mullan@4807 1169 // new AccessControlContext(new ProtectionDomain[] {pd})
mullan@4807 1170 objArrayHandle h_context(THREAD, context);
coleenp@6139 1171 oop acc = java_security_AccessControlContext::create(h_context, false, Handle(), CHECK_NULL);
coleenp@6139 1172 return acc;
mullan@4807 1173 }
duke@0 1174
duke@0 1175 JVM_ENTRY(jobject, JVM_DoPrivileged(JNIEnv *env, jclass cls, jobject action, jobject context, jboolean wrapException))
duke@0 1176 JVMWrapper("JVM_DoPrivileged");
duke@0 1177
duke@0 1178 if (action == NULL) {
duke@0 1179 THROW_MSG_0(vmSymbols::java_lang_NullPointerException(), "Null action");
duke@0 1180 }
duke@0 1181
mullan@4807 1182 // Compute the frame initiating the do privileged operation and setup the privileged stack
mullan@4807 1183 vframeStream vfst(thread);
mullan@4807 1184 vfst.security_get_caller_frame(1);
mullan@4807 1185
mullan@4807 1186 if (vfst.at_end()) {
mullan@4807 1187 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "no caller?");
mullan@4807 1188 }
mullan@4807 1189
mullan@4807 1190 Method* method = vfst.method();
mullan@4807 1191 instanceKlassHandle klass (THREAD, method->method_holder());
mullan@4807 1192
mullan@4807 1193 // Check that action object understands "Object run()"
mullan@4807 1194 Handle h_context;
mullan@4807 1195 if (context != NULL) {
mullan@4807 1196 h_context = Handle(THREAD, JNIHandles::resolve(context));
mullan@4807 1197 bool authorized = is_authorized(h_context, klass, CHECK_NULL);
mullan@4807 1198 if (!authorized) {
mullan@4807 1199 // Create an unprivileged access control object and call it's run function
mullan@4807 1200 // instead.
mullan@4807 1201 oop noprivs = create_dummy_access_control_context(CHECK_NULL);
mullan@4807 1202 h_context = Handle(THREAD, noprivs);
mullan@4807 1203 }
mullan@4807 1204 }
duke@0 1205
duke@0 1206 // Check that action object understands "Object run()"
duke@0 1207 Handle object (THREAD, JNIHandles::resolve(action));
duke@0 1208
duke@0 1209 // get run() method
hseigel@3843 1210 Method* m_oop = object->klass()->uncached_lookup_method(
duke@0 1211 vmSymbols::run_method_name(),
lfoltan@6288 1212 vmSymbols::void_object_signature(),
lfoltan@6288 1213 Klass::normal);
duke@0 1214 methodHandle m (THREAD, m_oop);
coleenp@3602 1215 if (m.is_null() || !m->is_method() || !m()->is_public() || m()->is_static()) {
duke@0 1216 THROW_MSG_0(vmSymbols::java_lang_InternalError(), "No run method");
duke@0 1217 }
duke@0 1218
mullan@4807 1219 // Stack allocated list of privileged stack elements
mullan@4807 1220 PrivilegedElement pi;
duke@0 1221 if (!vfst.at_end()) {
mullan@4807 1222 pi.initialize(&vfst, h_context(), thread->privileged_stack_top(), CHECK_NULL);
duke@0 1223 thread->set_privileged_stack_top(&pi);
duke@0 1224 }
duke@0 1225
duke@0 1226
duke@0 1227 // invoke the Object run() in the action object. We cannot use call_interface here, since the static type
duke@0 1228 // is not really known - it is either java.security.PrivilegedAction or java.security.PrivilegedExceptionAction
duke@0 1229 Handle pending_exception;
duke@0 1230 JavaValue result(T_OBJECT);
duke@0 1231 JavaCallArguments args(object);
duke@0 1232 JavaCalls::call(&result, m, &args, THREAD);
duke@0 1233
duke@0 1234 // done with action, remove ourselves from the list
duke@0 1235 if (!vfst.at_end()) {
duke@0 1236 assert(thread->privileged_stack_top() != NULL && thread->privileged_stack_top() == &pi, "wrong top element");
duke@0 1237 thread->set_privileged_stack_top(thread->privileged_stack_top()->next());
duke@0 1238 }
duke@0 1239
duke@0 1240 if (HAS_PENDING_EXCEPTION) {
duke@0 1241 pending_exception = Handle(THREAD, PENDING_EXCEPTION);
duke@0 1242 CLEAR_PENDING_EXCEPTION;
jbachorik@6035 1243 // JVMTI has already reported the pending exception
jbachorik@6035 1244 // JVMTI internal flag reset is needed in order to report PrivilegedActionException
jbachorik@6035 1245 if (THREAD->is_Java_thread()) {
jbachorik@6035 1246 JvmtiExport::clear_detected_exception((JavaThread*) THREAD);
jbachorik@6035 1247 }
never@1142 1248 if ( pending_exception->is_a(SystemDictionary::Exception_klass()) &&
never@1142 1249 !pending_exception->is_a(SystemDictionary::RuntimeException_klass())) {
duke@0 1250 // Throw a java.security.PrivilegedActionException(Exception e) exception
duke@0 1251 JavaCallArguments args(pending_exception);
coleenp@2062 1252 THROW_ARG_0(vmSymbols::java_security_PrivilegedActionException(),
coleenp@2062 1253 vmSymbols::exception_void_signature(),
duke@0 1254 &args);
duke@0 1255 }
duke@0 1256 }
duke@0 1257
duke@0 1258 if (pending_exception.not_null()) THROW_OOP_0(pending_exception());
duke@0 1259 return JNIHandles::make_local(env, (oop) result.get_jobject());
duke@0 1260 JVM_END
duke@0 1261
duke@0 1262
duke@0 1263 // Returns the inherited_access_control_context field of the running thread.
duke@0 1264 JVM_ENTRY(jobject, JVM_GetInheritedAccessControlContext(JNIEnv *env, jclass cls))
duke@0 1265 JVMWrapper("JVM_GetInheritedAccessControlContext");
duke@0 1266 oop result = java_lang_Thread::inherited_access_control_context(thread->threadObj());
duke@0 1267 return JNIHandles::make_local(env, result);
duke@0 1268 JVM_END
duke@0 1269
duke@0 1270 class RegisterArrayForGC {
duke@0 1271 private:
duke@0 1272 JavaThread *_thread;
duke@0 1273 public:
duke@0 1274 RegisterArrayForGC(JavaThread *thread, GrowableArray<oop>* array) {
duke@0 1275 _thread = thread;
duke@0 1276 _thread->register_array_for_gc(array);
duke@0 1277 }
duke@0 1278
duke@0 1279 ~RegisterArrayForGC() {
duke@0 1280 _thread->register_array_for_gc(NULL);
duke@0 1281 }
duke@0 1282 };
duke@0 1283
duke@0 1284
duke@0 1285 JVM_ENTRY(jobject, JVM_GetStackAccessControlContext(JNIEnv *env, jclass cls))
duke@0 1286 JVMWrapper("JVM_GetStackAccessControlContext");
duke@0 1287 if (!UsePrivilegedStack) return NULL;
duke@0 1288
duke@0 1289 ResourceMark rm(THREAD);
duke@0 1290 GrowableArray<oop>* local_array = new GrowableArray<oop>(12);
duke@0 1291 JvmtiVMObjectAllocEventCollector oam;
duke@0 1292
duke@0 1293 // count the protection domains on the execution stack. We collapse
duke@0 1294 // duplicate consecutive protection domains into a single one, as
duke@0 1295 // well as stopping when we hit a privileged frame.
duke@0 1296
duke@0 1297 // Use vframeStream to iterate through Java frames
duke@0 1298 vframeStream vfst(thread);
duke@0 1299
duke@0 1300 oop previous_protection_domain = NULL;
duke@0 1301 Handle privileged_context(thread, NULL);
duke@0 1302 bool is_privileged = false;
duke@0 1303 oop protection_domain = NULL;
duke@0 1304
duke@0 1305 for(; !vfst.at_end(); vfst.next()) {
duke@0 1306 // get method of frame
coleenp@3602 1307 Method* method = vfst.method();
duke@0 1308 intptr_t* frame_id = vfst.frame_id();
duke@0 1309
duke@0 1310 // check the privileged frames to see if we have a match
duke@0 1311 if (thread->privileged_stack_top() && thread->privileged_stack_top()->frame_id() == frame_id) {
duke@0 1312 // this frame is privileged
duke@0 1313 is_privileged = true;
duke@0 1314 privileged_context = Handle(thread, thread->privileged_stack_top()->privileged_context());
duke@0 1315 protection_domain = thread->privileged_stack_top()->protection_domain();
duke@0 1316 } else {
coleenp@3816 1317 protection_domain = method->method_holder()->protection_domain();
duke@0 1318 }
duke@0 1319
duke@0 1320 if ((previous_protection_domain != protection_domain) && (protection_domain != NULL)) {
duke@0 1321 local_array->push(protection_domain);
duke@0 1322 previous_protection_domain = protection_domain;
duke@0 1323 }
duke@0 1324
duke@0 1325 if (is_privileged) break;
duke@0 1326 }
duke@0 1327
duke@0 1328
duke@0 1329 // either all the domains on the stack were system domains, or
duke@0 1330 // we had a privileged system domain
duke@0 1331 if (local_array->is_empty()) {
duke@0 1332 if (is_privileged && privileged_context.is_null()) return NULL;
duke@0 1333
duke@0 1334 oop result = java_security_AccessControlContext::create(objArrayHandle(), is_privileged, privileged_context, CHECK_NULL);
duke@0 1335 return JNIHandles::make_local(env, result);
duke@0 1336 }
duke@0 1337
duke@0 1338 // the resource area must be registered in case of a gc
duke@0 1339 RegisterArrayForGC ragc(thread, local_array);
never@1142 1340 objArrayOop context = oopFactory::new_objArray(SystemDictionary::ProtectionDomain_klass(),
duke@0 1341 local_array->length(), CHECK_NULL);
duke@0 1342 objArrayHandle h_context(thread, context);
duke@0 1343 for (int index = 0; index < local_array->length(); index++) {
duke@0 1344 h_context->obj_at_put(index, local_array->at(index));
duke@0 1345 }
duke@0 1346
duke@0 1347 oop result = java_security_AccessControlContext::create(h_context, is_privileged, privileged_context, CHECK_NULL);
duke@0 1348
duke@0 1349 return JNIHandles::make_local(env, result);
duke@0 1350 JVM_END
duke@0 1351
duke@0 1352
duke@0 1353 JVM_QUICK_ENTRY(jboolean, JVM_IsArrayClass(JNIEnv *env, jclass cls))
duke@0 1354 JVMWrapper("JVM_IsArrayClass");
coleenp@3602 1355 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
hseigel@3843 1356 return (k != NULL) && k->oop_is_array() ? true : false;
duke@0 1357 JVM_END
duke@0 1358
duke@0 1359
duke@0 1360 JVM_QUICK_ENTRY(jboolean, JVM_IsPrimitiveClass(JNIEnv *env, jclass cls))
duke@0 1361 JVMWrapper("JVM_IsPrimitiveClass");
duke@0 1362 oop mirror = JNIHandles::resolve_non_null(cls);
duke@0 1363 return (jboolean) java_lang_Class::is_primitive(mirror);
duke@0 1364 JVM_END
duke@0 1365
duke@0 1366
duke@0 1367 JVM_ENTRY(jint, JVM_GetClassModifiers(JNIEnv *env, jclass cls))
duke@0 1368 JVMWrapper("JVM_GetClassModifiers");
duke@0 1369 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
duke@0 1370 // Primitive type
duke@0 1371 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
duke@0 1372 }
duke@0 1373
hseigel@3843 1374 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 1375 debug_only(int computed_modifiers = k->compute_modifier_flags(CHECK_0));
duke@0 1376 assert(k->modifier_flags() == computed_modifiers, "modifiers cache is OK");
duke@0 1377 return k->modifier_flags();
duke@0 1378 JVM_END
duke@0 1379
duke@0 1380
duke@0 1381 // Inner class reflection ///////////////////////////////////////////////////////////////////////////////
duke@0 1382
duke@0 1383 JVM_ENTRY(jobjectArray, JVM_GetDeclaredClasses(JNIEnv *env, jclass ofClass))
duke@0 1384 JvmtiVMObjectAllocEventCollector oam;
duke@0 1385 // ofClass is a reference to a java_lang_Class object. The mirror object
coleenp@3602 1386 // of an InstanceKlass
duke@0 1387
duke@0 1388 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
hseigel@3843 1389 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) {
never@1142 1390 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
duke@0 1391 return (jobjectArray)JNIHandles::make_local(env, result);
duke@0 1392 }
duke@0 1393
coleenp@3602 1394 instanceKlassHandle k(thread, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
jiangli@3235 1395 InnerClassesIterator iter(k);
jiangli@3235 1396
jiangli@3235 1397 if (iter.length() == 0) {
duke@0 1398 // Neither an inner nor outer class
never@1142 1399 oop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), 0, CHECK_NULL);
duke@0 1400 return (jobjectArray)JNIHandles::make_local(env, result);
duke@0 1401 }
duke@0 1402
duke@0 1403 // find inner class info
duke@0 1404 constantPoolHandle cp(thread, k->constants());
jiangli@3235 1405 int length = iter.length();
duke@0 1406
duke@0 1407 // Allocate temp. result array
never@1142 1408 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Class_klass(), length/4, CHECK_NULL);
duke@0 1409 objArrayHandle result (THREAD, r);
duke@0 1410 int members = 0;
duke@0 1411
jiangli@3235 1412 for (; !iter.done(); iter.next()) {
jiangli@3235 1413 int ioff = iter.inner_class_info_index();
jiangli@3235 1414 int ooff = iter.outer_class_info_index();
duke@0 1415
duke@0 1416 if (ioff != 0 && ooff != 0) {
duke@0 1417 // Check to see if the name matches the class we're looking for
duke@0 1418 // before attempting to find the class.
duke@0 1419 if (cp->klass_name_at_matches(k, ooff)) {
coleenp@3602 1420 Klass* outer_klass = cp->klass_at(ooff, CHECK_NULL);
duke@0 1421 if (outer_klass == k()) {
coleenp@3602 1422 Klass* ik = cp->klass_at(ioff, CHECK_NULL);
duke@0 1423 instanceKlassHandle inner_klass (THREAD, ik);
duke@0 1424
duke@0 1425 // Throws an exception if outer klass has not declared k as
duke@0 1426 // an inner klass
jrose@665 1427 Reflection::check_for_inner_class(k, inner_klass, true, CHECK_NULL);
duke@0 1428
duke@0 1429 result->obj_at_put(members, inner_klass->java_mirror());
duke@0 1430 members++;
duke@0 1431 }
duke@0 1432 }
duke@0 1433 }
duke@0 1434 }
duke@0 1435
duke@0 1436 if (members != length) {
duke@0 1437 // Return array of right length
never@1142 1438 objArrayOop res = oopFactory::new_objArray(SystemDictionary::Class_klass(), members, CHECK_NULL);
duke@0 1439 for(int i = 0; i < members; i++) {
duke@0 1440 res->obj_at_put(i, result->obj_at(i));
duke@0 1441 }
duke@0 1442 return (jobjectArray)JNIHandles::make_local(env, res);
duke@0 1443 }
duke@0 1444
duke@0 1445 return (jobjectArray)JNIHandles::make_local(env, result());
duke@0 1446 JVM_END
duke@0 1447
duke@0 1448
duke@0 1449 JVM_ENTRY(jclass, JVM_GetDeclaringClass(JNIEnv *env, jclass ofClass))
jrose@665 1450 {
duke@0 1451 // ofClass is a reference to a java_lang_Class object.
duke@0 1452 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
hseigel@3843 1453 ! java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_instance()) {
duke@0 1454 return NULL;
duke@0 1455 }
duke@0 1456
xlu@1126 1457 bool inner_is_member = false;
coleenp@3602 1458 Klass* outer_klass
coleenp@3602 1459 = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))
xlu@1126 1460 )->compute_enclosing_class(&inner_is_member, CHECK_NULL);
jrose@665 1461 if (outer_klass == NULL) return NULL; // already a top-level class
xlu@1126 1462 if (!inner_is_member) return NULL; // an anonymous class (inside a method)
hseigel@3843 1463 return (jclass) JNIHandles::make_local(env, outer_klass->java_mirror());
jrose@665 1464 }
jrose@665 1465 JVM_END
jrose@665 1466
coleenp@3602 1467 // should be in InstanceKlass.cpp, but is here for historical reasons
coleenp@3602 1468 Klass* InstanceKlass::compute_enclosing_class_impl(instanceKlassHandle k,
xlu@1126 1469 bool* inner_is_member,
xlu@1126 1470 TRAPS) {
jrose@665 1471 Thread* thread = THREAD;
jiangli@3235 1472 InnerClassesIterator iter(k);
jiangli@3235 1473 if (iter.length() == 0) {
duke@0 1474 // No inner class info => no declaring class
duke@0 1475 return NULL;
duke@0 1476 }
duke@0 1477
duke@0 1478 constantPoolHandle i_cp(thread, k->constants());
duke@0 1479
duke@0 1480 bool found = false;
coleenp@3602 1481 Klass* ok;
duke@0 1482 instanceKlassHandle outer_klass;
xlu@1126 1483 *inner_is_member = false;
duke@0 1484
duke@0 1485 // Find inner_klass attribute
jiangli@3235 1486 for (; !iter.done() && !found; iter.next()) {
jiangli@3235 1487 int ioff = iter.inner_class_info_index();
jiangli@3235 1488 int ooff = iter.outer_class_info_index();
jiangli@3235 1489 int noff = iter.inner_name_index();
jrose@665 1490 if (ioff != 0) {
duke@0 1491 // Check to see if the name matches the class we're looking for
duke@0 1492 // before attempting to find the class.
duke@0 1493 if (i_cp->klass_name_at_matches(k, ioff)) {
coleenp@3602 1494 Klass* inner_klass = i_cp->klass_at(ioff, CHECK_NULL);
jrose@665 1495 found = (k() == inner_klass);
jrose@665 1496 if (found && ooff != 0) {
duke@0 1497 ok = i_cp->klass_at(ooff, CHECK_NULL);
duke@0 1498 outer_klass = instanceKlassHandle(thread, ok);
xlu@1126 1499 *inner_is_member = true;
duke@0 1500 }
duke@0 1501 }
duke@0 1502 }
duke@0 1503 }
duke@0 1504
jrose@665 1505 if (found && outer_klass.is_null()) {
jrose@665 1506 // It may be anonymous; try for that.
jrose@665 1507 int encl_method_class_idx = k->enclosing_method_class_index();
jrose@665 1508 if (encl_method_class_idx != 0) {
jrose@665 1509 ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
jrose@665 1510 outer_klass = instanceKlassHandle(thread, ok);
xlu@1126 1511 *inner_is_member = false;
jrose@665 1512 }
jrose@665 1513 }
jrose@665 1514
duke@0 1515 // If no inner class attribute found for this class.
jrose@665 1516 if (outer_klass.is_null()) return NULL;
duke@0 1517
duke@0 1518 // Throws an exception if outer klass has not declared k as an inner klass
jrose@665 1519 // We need evidence that each klass knows about the other, or else
jrose@665 1520 // the system could allow a spoof of an inner class to gain access rights.
xlu@1126 1521 Reflection::check_for_inner_class(outer_klass, k, *inner_is_member, CHECK_NULL);
jrose@665 1522 return outer_klass();
jrose@665 1523 }
duke@0 1524
duke@0 1525 JVM_ENTRY(jstring, JVM_GetClassSignature(JNIEnv *env, jclass cls))
duke@0 1526 assert (cls != NULL, "illegal class");
duke@0 1527 JVMWrapper("JVM_GetClassSignature");
duke@0 1528 JvmtiVMObjectAllocEventCollector oam;
duke@0 1529 ResourceMark rm(THREAD);
duke@0 1530 // Return null for arrays and primatives
duke@0 1531 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
coleenp@3602 1532 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
hseigel@3843 1533 if (k->oop_is_instance()) {
coleenp@3602 1534 Symbol* sym = InstanceKlass::cast(k)->generic_signature();
coleenp@2062 1535 if (sym == NULL) return NULL;
duke@0 1536 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
duke@0 1537 return (jstring) JNIHandles::make_local(env, str());
duke@0 1538 }
duke@0 1539 }
duke@0 1540 return NULL;
duke@0 1541 JVM_END
duke@0 1542
duke@0 1543
duke@0 1544 JVM_ENTRY(jbyteArray, JVM_GetClassAnnotations(JNIEnv *env, jclass cls))
duke@0 1545 assert (cls != NULL, "illegal class");
duke@0 1546 JVMWrapper("JVM_GetClassAnnotations");
rbackman@4383 1547
duke@0 1548 // Return null for arrays and primitives
duke@0 1549 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
coleenp@3602 1550 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
hseigel@3843 1551 if (k->oop_is_instance()) {
coleenp@3602 1552 typeArrayOop a = Annotations::make_java_array(InstanceKlass::cast(k)->class_annotations(), CHECK_NULL);
coleenp@3602 1553 return (jbyteArray) JNIHandles::make_local(env, a);
duke@0 1554 }
duke@0 1555 }
duke@0 1556 return NULL;
duke@0 1557 JVM_END
duke@0 1558
duke@0 1559
rbackman@4383 1560 static bool jvm_get_field_common(jobject field, fieldDescriptor& fd, TRAPS) {
duke@0 1561 // some of this code was adapted from from jni_FromReflectedField
duke@0 1562
duke@0 1563 oop reflected = JNIHandles::resolve_non_null(field);
duke@0 1564 oop mirror = java_lang_reflect_Field::clazz(reflected);
coleenp@3602 1565 Klass* k = java_lang_Class::as_Klass(mirror);
duke@0 1566 int slot = java_lang_reflect_Field::slot(reflected);
duke@0 1567 int modifiers = java_lang_reflect_Field::modifiers(reflected);
duke@0 1568
duke@0 1569 KlassHandle kh(THREAD, k);
coleenp@3602 1570 intptr_t offset = InstanceKlass::cast(kh())->field_offset(slot);
duke@0 1571
duke@0 1572 if (modifiers & JVM_ACC_STATIC) {
duke@0 1573 // for static fields we only look in the current class
coleenp@3602 1574 if (!InstanceKlass::cast(kh())->find_local_field_from_offset(offset, true, &fd)) {
duke@0 1575 assert(false, "cannot find static field");
rbackman@4383 1576 return false;
duke@0 1577 }
duke@0 1578 } else {
duke@0 1579 // for instance fields we start with the current class and work
duke@0 1580 // our way up through the superclass chain
coleenp@3602 1581 if (!InstanceKlass::cast(kh())->find_field_from_offset(offset, false, &fd)) {
duke@0 1582 assert(false, "cannot find instance field");
rbackman@4383 1583 return false;
duke@0 1584 }
duke@0 1585 }
rbackman@4383 1586 return true;
rbackman@4383 1587 }
rbackman@4383 1588
coleenp@3963 1589 static Method* jvm_get_method_common(jobject method) {
duke@0 1590 // some of this code was adapted from from jni_FromReflectedMethod
duke@0 1591
duke@0 1592 oop reflected = JNIHandles::resolve_non_null(method);
duke@0 1593 oop mirror = NULL;
duke@0 1594 int slot = 0;
duke@0 1595
never@1142 1596 if (reflected->klass() == SystemDictionary::reflect_Constructor_klass()) {
duke@0 1597 mirror = java_lang_reflect_Constructor::clazz(reflected);
duke@0 1598 slot = java_lang_reflect_Constructor::slot(reflected);
duke@0 1599 } else {
never@1142 1600 assert(reflected->klass() == SystemDictionary::reflect_Method_klass(),
duke@0 1601 "wrong type");
duke@0 1602 mirror = java_lang_reflect_Method::clazz(reflected);
duke@0 1603 slot = java_lang_reflect_Method::slot(reflected);
duke@0 1604 }
coleenp@3602 1605 Klass* k = java_lang_Class::as_Klass(mirror);
duke@0 1606
coleenp@3963 1607 Method* m = InstanceKlass::cast(k)->method_with_idnum(slot);
rbackman@4383 1608 assert(m != NULL, "cannot find method");
rbackman@4383 1609 return m; // caller has to deal with NULL in product mode
duke@0 1610 }
duke@0 1611
stefank@3958 1612 /* Type use annotations support (JDK 1.8) */
stefank@3958 1613
stefank@3958 1614 JVM_ENTRY(jbyteArray, JVM_GetClassTypeAnnotations(JNIEnv *env, jclass cls))
stefank@3958 1615 assert (cls != NULL, "illegal class");
stefank@3958 1616 JVMWrapper("JVM_GetClassTypeAnnotations");
stefank@3958 1617 ResourceMark rm(THREAD);
stefank@3958 1618 // Return null for arrays and primitives
stefank@3958 1619 if (!java_lang_Class::is_primitive(JNIHandles::resolve(cls))) {
stefank@3958 1620 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve(cls));
stefank@3958 1621 if (k->oop_is_instance()) {
coleenp@4137 1622 AnnotationArray* type_annotations = InstanceKlass::cast(k)->class_type_annotations();
stefank@4019 1623 if (type_annotations != NULL) {
coleenp@4137 1624 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
stefank@4019 1625 return (jbyteArray) JNIHandles::make_local(env, a);
stefank@4019 1626 }
stefank@3958 1627 }
stefank@3958 1628 }
stefank@3958 1629 return NULL;
stefank@3958 1630 JVM_END
stefank@3958 1631
rbackman@4383 1632 JVM_ENTRY(jbyteArray, JVM_GetMethodTypeAnnotations(JNIEnv *env, jobject method))
rbackman@4383 1633 assert (method != NULL, "illegal method");
rbackman@4383 1634 JVMWrapper("JVM_GetMethodTypeAnnotations");
rbackman@4383 1635
rbackman@4383 1636 // method is a handle to a java.lang.reflect.Method object
rbackman@4383 1637 Method* m = jvm_get_method_common(method);
rbackman@4383 1638 if (m == NULL) {
rbackman@4383 1639 return NULL;
rbackman@4383 1640 }
rbackman@4383 1641
rbackman@4383 1642 AnnotationArray* type_annotations = m->type_annotations();
rbackman@4383 1643 if (type_annotations != NULL) {
rbackman@4383 1644 typeArrayOop a = Annotations::make_java_array(type_annotations, CHECK_NULL);
rbackman@4383 1645 return (jbyteArray) JNIHandles::make_local(env, a);
rbackman@4383 1646 }
rbackman@4383 1647
rbackman@4383 1648 return NULL;
rbackman@4383 1649 JVM_END
rbackman@4383 1650
rbackman@4383 1651 JVM_ENTRY(jbyteArray, JVM_GetFieldTypeAnnotations(JNIEnv *env, jobject field))
rbackman@4383 1652 assert (field != NULL, "illegal field");
rbackman@4383 1653 JVMWrapper("JVM_GetFieldTypeAnnotations");
rbackman@4383 1654
rbackman@4383 1655 fieldDescriptor fd;
rbackman@4383 1656 bool gotFd = jvm_get_field_common(field, fd, CHECK_NULL);
rbackman@4383 1657 if (!gotFd) {
rbackman@4383 1658 return NULL;
rbackman@4383 1659 }
rbackman@4383 1660
rbackman@4383 1661 return (jbyteArray) JNIHandles::make_local(env, Annotations::make_java_array(fd.type_annotations(), THREAD));
rbackman@4383 1662 JVM_END
rbackman@4383 1663
coleenp@3996 1664 static void bounds_check(constantPoolHandle cp, jint index, TRAPS) {
coleenp@3996 1665 if (!cp->is_within_bounds(index)) {
coleenp@3996 1666 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "Constant pool index out of bounds");
coleenp@3996 1667 }
coleenp@3996 1668 }
coleenp@3996 1669
coleenp@3963 1670 JVM_ENTRY(jobjectArray, JVM_GetMethodParameters(JNIEnv *env, jobject method))
coleenp@3963 1671 {
coleenp@3963 1672 JVMWrapper("JVM_GetMethodParameters");
coleenp@3963 1673 // method is a handle to a java.lang.reflect.Method object
coleenp@3963 1674 Method* method_ptr = jvm_get_method_common(method);
coleenp@3963 1675 methodHandle mh (THREAD, method_ptr);
coleenp@3963 1676 Handle reflected_method (THREAD, JNIHandles::resolve_non_null(method));
coleenp@3963 1677 const int num_params = mh->method_parameters_length();
coleenp@3963 1678
emc@7353 1679 if (num_params < 0) {
emc@7353 1680 // A -1 return value from method_parameters_length means there is no
emc@7353 1681 // parameter data. Return null to indicate this to the reflection
emc@7353 1682 // API.
emc@7353 1683 assert(num_params == -1, "num_params should be -1 if it is less than zero");
emc@7353 1684 return (jobjectArray)NULL;
emc@7353 1685 } else {
emc@7353 1686 // Otherwise, we return something up to reflection, even if it is
emc@7353 1687 // a zero-length array. Why? Because in some cases this can
emc@7353 1688 // trigger a MalformedParametersException.
emc@7353 1689
coleenp@3996 1690 // make sure all the symbols are properly formatted
coleenp@3996 1691 for (int i = 0; i < num_params; i++) {
coleenp@3996 1692 MethodParametersElement* params = mh->method_parameters_start();
coleenp@3996 1693 int index = params[i].name_cp_index;
coleenp@3996 1694 bounds_check(mh->constants(), index, CHECK_NULL);
coleenp@3996 1695
coleenp@3996 1696 if (0 != index && !mh->constants()->tag_at(index).is_utf8()) {
coleenp@3996 1697 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(),
coleenp@3996 1698 "Wrong type at constant pool index");
coleenp@3996 1699 }
coleenp@3996 1700
coleenp@3996 1701 }
coleenp@3996 1702
coleenp@3963 1703 objArrayOop result_oop = oopFactory::new_objArray(SystemDictionary::reflect_Parameter_klass(), num_params, CHECK_NULL);
coleenp@3963 1704 objArrayHandle result (THREAD, result_oop);
coleenp@3963 1705
coleenp@3996 1706 for (int i = 0; i < num_params; i++) {
coleenp@3963 1707 MethodParametersElement* params = mh->method_parameters_start();
coleenp@3996 1708 // For a 0 index, give a NULL symbol
minqi@4662 1709 Symbol* sym = 0 != params[i].name_cp_index ?
coleenp@3996 1710 mh->constants()->symbol_at(params[i].name_cp_index) : NULL;
emc@4089 1711 int flags = params[i].flags;
coleenp@3963 1712 oop param = Reflection::new_parameter(reflected_method, i, sym,
coleenp@3996 1713 flags, CHECK_NULL);
coleenp@3963 1714 result->obj_at_put(i, param);
coleenp@3963 1715 }
coleenp@3963 1716 return (jobjectArray)JNIHandles::make_local(env, result());
coleenp@3963 1717 }
coleenp@3963 1718 }
coleenp@3963 1719 JVM_END
duke@0 1720
duke@0 1721 // New (JDK 1.4) reflection implementation /////////////////////////////////////
duke@0 1722
duke@0 1723 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass, jboolean publicOnly))
duke@0 1724 {
duke@0 1725 JVMWrapper("JVM_GetClassDeclaredFields");
duke@0 1726 JvmtiVMObjectAllocEventCollector oam;
duke@0 1727
duke@0 1728 // Exclude primitive types and array types
duke@0 1729 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass)) ||
hseigel@3843 1730 java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) {
duke@0 1731 // Return empty array
never@1142 1732 oop res = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), 0, CHECK_NULL);
duke@0 1733 return (jobjectArray) JNIHandles::make_local(env, res);
duke@0 1734 }
duke@0 1735
coleenp@3602 1736 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
duke@0 1737 constantPoolHandle cp(THREAD, k->constants());
duke@0 1738
duke@0 1739 // Ensure class is linked
duke@0 1740 k->link_class(CHECK_NULL);
duke@0 1741
duke@0 1742 // 4496456 We need to filter out java.lang.Throwable.backtrace
duke@0 1743 bool skip_backtrace = false;
duke@0 1744
duke@0 1745 // Allocate result
duke@0 1746 int num_fields;
duke@0 1747
duke@0 1748 if (publicOnly) {
duke@0 1749 num_fields = 0;
never@2702 1750 for (JavaFieldStream fs(k()); !fs.done(); fs.next()) {
never@2702 1751 if (fs.access_flags().is_public()) ++num_fields;
duke@0 1752 }
duke@0 1753 } else {
never@2702 1754 num_fields = k->java_fields_count();
duke@0 1755
never@1142 1756 if (k() == SystemDictionary::Throwable_klass()) {
duke@0 1757 num_fields--;
duke@0 1758 skip_backtrace = true;
duke@0 1759 }
duke@0 1760 }
duke@0 1761
never@1142 1762 objArrayOop r = oopFactory::new_objArray(SystemDictionary::reflect_Field_klass(), num_fields, CHECK_NULL);
duke@0 1763 objArrayHandle result (THREAD, r);
duke@0 1764
duke@0 1765 int out_idx = 0;
duke@0 1766 fieldDescriptor fd;
never@2702 1767 for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
duke@0 1768 if (skip_backtrace) {
duke@0 1769 // 4496456 skip java.lang.Throwable.backtrace
never@2702 1770 int offset = fs.offset();
duke@0 1771 if (offset == java_lang_Throwable::get_backtrace_offset()) continue;
duke@0 1772 }
duke@0 1773
never@2702 1774 if (!publicOnly || fs.access_flags().is_public()) {
drchase@5297 1775 fd.reinitialize(k(), fs.index());
coleenp@6444 1776 oop field = Reflection::new_field(&fd, CHECK_NULL);
duke@0 1777 result->obj_at_put(out_idx, field);
duke@0 1778 ++out_idx;
duke@0 1779 }
duke@0 1780 }
duke@0 1781 assert(out_idx == num_fields, "just checking");
duke@0 1782 return (jobjectArray) JNIHandles::make_local(env, result());
duke@0 1783 }
duke@0 1784 JVM_END
duke@0 1785
coleenp@5314 1786 static bool select_method(methodHandle method, bool want_constructor) {
coleenp@5314 1787 if (want_constructor) {
coleenp@5314 1788 return (method->is_initializer() && !method->is_static());
coleenp@5314 1789 } else {
coleenp@5314 1790 return (!method->is_initializer() && !method->is_overpass());
coleenp@5314 1791 }
coleenp@5314 1792 }
coleenp@5314 1793
coleenp@5314 1794 static jobjectArray get_class_declared_methods_helper(
coleenp@5314 1795 JNIEnv *env,
coleenp@5314 1796 jclass ofClass, jboolean publicOnly,
coleenp@5314 1797 bool want_constructor,
coleenp@5314 1798 Klass* klass, TRAPS) {
coleenp@5314 1799
duke@0 1800 JvmtiVMObjectAllocEventCollector oam;
duke@0 1801
duke@0 1802 // Exclude primitive types and array types
duke@0 1803 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(ofClass))
hseigel@3843 1804 || java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass))->oop_is_array()) {
duke@0 1805 // Return empty array
coleenp@5314 1806 oop res = oopFactory::new_objArray(klass, 0, CHECK_NULL);
duke@0 1807 return (jobjectArray) JNIHandles::make_local(env, res);
duke@0 1808 }
duke@0 1809
coleenp@3602 1810 instanceKlassHandle k(THREAD, java_lang_Class::as_Klass(JNIHandles::resolve_non_null(ofClass)));
duke@0 1811
duke@0 1812 // Ensure class is linked
duke@0 1813 k->link_class(CHECK_NULL);
duke@0 1814
coleenp@3602 1815 Array<Method*>* methods = k->methods();
duke@0 1816 int methods_length = methods->length();
coleenp@5314 1817
coleenp@5314 1818 // Save original method_idnum in case of redefinition, which can change
coleenp@5314 1819 // the idnum of obsolete methods. The new method will have the same idnum
coleenp@5314 1820 // but if we refresh the methods array, the counts will be wrong.
coleenp@5314 1821 ResourceMark rm(THREAD);
coleenp@5314 1822 GrowableArray<int>* idnums = new GrowableArray<int>(methods_length);
duke@0 1823 int num_methods = 0;
duke@0 1824
coleenp@5314 1825 for (int i = 0; i < methods_length; i++) {
coleenp@3602 1826 methodHandle method(THREAD, methods->at(i));
coleenp@5314 1827 if (select_method(method, want_constructor)) {
duke@0 1828 if (!publicOnly || method->is_public()) {
coleenp@5314 1829 idnums->push(method->method_idnum());
duke@0 1830 ++num_methods;
duke@0 1831 }
duke@0 1832 }
duke@0 1833 }
duke@0 1834
duke@0 1835 // Allocate result
coleenp@5314 1836 objArrayOop r = oopFactory::new_objArray(klass, num_methods, CHECK_NULL);
duke@0 1837 objArrayHandle result (THREAD, r);
duke@0 1838
coleenp@5314 1839 // Now just put the methods that we selected above, but go by their idnum
coleenp@5314 1840 // in case of redefinition. The methods can be redefined at any safepoint,
coleenp@5314 1841 // so above when allocating the oop array and below when creating reflect
coleenp@5314 1842 // objects.
coleenp@5314 1843 for (int i = 0; i < num_methods; i++) {
coleenp@5314 1844 methodHandle method(THREAD, k->method_with_idnum(idnums->at(i)));
coleenp@5314 1845 if (method.is_null()) {
coleenp@5314 1846 // Method may have been deleted and seems this API can handle null
coleenp@5314 1847 // Otherwise should probably put a method that throws NSME
coleenp@5314 1848 result->obj_at_put(i, NULL);
coleenp@5314 1849 } else {
coleenp@5314 1850 oop m;
coleenp@5314 1851 if (want_constructor) {
coleenp@5314 1852 m = Reflection::new_constructor(method, CHECK_NULL);
coleenp@5314 1853 } else {
coleenp@6444 1854 m = Reflection::new_method(method, false, CHECK_NULL);
duke@0 1855 }
coleenp@5314 1856 result->obj_at_put(i, m);
duke@0 1857 }
duke@0 1858 }
coleenp@5314 1859
duke@0 1860 return (jobjectArray) JNIHandles::make_local(env, result());
duke@0 1861 }
coleenp@5314 1862
coleenp@5314 1863 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredMethods(JNIEnv *env, jclass ofClass, jboolean publicOnly))
coleenp@5314 1864 {
coleenp@5314 1865 JVMWrapper("JVM_GetClassDeclaredMethods");
coleenp@5314 1866 return get_class_declared_methods_helper(env, ofClass, publicOnly,
coleenp@5314 1867 /*want_constructor*/ false,
coleenp@5314 1868 SystemDictionary::reflect_Method_klass(), THREAD);
coleenp@5314 1869 }
duke@0 1870 JVM_END
duke@0 1871
duke@0 1872 JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredConstructors(JNIEnv *env, jclass ofClass, jboolean publicOnly))
duke@0 1873 {
duke@0 1874 JVMWrapper("JVM_GetClassDeclaredConstructors");
coleenp@5314 1875 return get_class_declared_methods_helper(env, ofClass, publicOnly,
coleenp@5314 1876 /*want_constructor*/ true,
coleenp@5314 1877 SystemDictionary::reflect_Constructor_klass(), THREAD);
duke@0 1878 }
duke@0 1879 JVM_END
duke@0 1880
duke@0 1881 JVM_ENTRY(jint, JVM_GetClassAccessFlags(JNIEnv *env, jclass cls))
duke@0 1882 {
duke@0 1883 JVMWrapper("JVM_GetClassAccessFlags");
duke@0 1884 if (java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
duke@0 1885 // Primitive type
duke@0 1886 return JVM_ACC_ABSTRACT | JVM_ACC_FINAL | JVM_ACC_PUBLIC;
duke@0 1887 }
duke@0 1888
hseigel@3843 1889 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 1890 return k->access_flags().as_int() & JVM_ACC_WRITTEN_FLAGS;
duke@0 1891 }
duke@0 1892 JVM_END
duke@0 1893
duke@0 1894
duke@0 1895 // Constant pool access //////////////////////////////////////////////////////////
duke@0 1896
duke@0 1897 JVM_ENTRY(jobject, JVM_GetClassConstantPool(JNIEnv *env, jclass cls))
duke@0 1898 {
duke@0 1899 JVMWrapper("JVM_GetClassConstantPool");
duke@0 1900 JvmtiVMObjectAllocEventCollector oam;
duke@0 1901
duke@0 1902 // Return null for primitives and arrays
duke@0 1903 if (!java_lang_Class::is_primitive(JNIHandles::resolve_non_null(cls))) {
coleenp@3602 1904 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
hseigel@3843 1905 if (k->oop_is_instance()) {
duke@0 1906 instanceKlassHandle k_h(THREAD, k);
duke@0 1907 Handle jcp = sun_reflect_ConstantPool::create(CHECK_NULL);
coleenp@3602 1908 sun_reflect_ConstantPool::set_cp(jcp(), k_h->constants());
duke@0 1909 return JNIHandles::make_local(jcp());
duke@0 1910 }
duke@0 1911 }
duke@0 1912 return NULL;
duke@0 1913 }
duke@0 1914 JVM_END
duke@0 1915
duke@0 1916
coleenp@3602 1917 JVM_ENTRY(jint, JVM_ConstantPoolGetSize(JNIEnv *env, jobject obj, jobject unused))
duke@0 1918 {
duke@0 1919 JVMWrapper("JVM_ConstantPoolGetSize");
coleenp@3602 1920 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 1921 return cp->length();
duke@0 1922 }
duke@0 1923 JVM_END
duke@0 1924
duke@0 1925
coleenp@3602 1926 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 1927 {
duke@0 1928 JVMWrapper("JVM_ConstantPoolGetClassAt");
coleenp@3602 1929 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 1930 bounds_check(cp, index, CHECK_NULL);
duke@0 1931 constantTag tag = cp->tag_at(index);
duke@0 1932 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
duke@0 1933 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 1934 }
coleenp@3602 1935 Klass* k = cp->klass_at(index, CHECK_NULL);
never@2223 1936 return (jclass) JNIHandles::make_local(k->java_mirror());
duke@0 1937 }
duke@0 1938 JVM_END
duke@0 1939
coleenp@3602 1940 JVM_ENTRY(jclass, JVM_ConstantPoolGetClassAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 1941 {
duke@0 1942 JVMWrapper("JVM_ConstantPoolGetClassAtIfLoaded");
coleenp@3602 1943 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 1944 bounds_check(cp, index, CHECK_NULL);
duke@0 1945 constantTag tag = cp->tag_at(index);
duke@0 1946 if (!tag.is_klass() && !tag.is_unresolved_klass()) {
duke@0 1947 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 1948 }
coleenp@3602 1949 Klass* k = ConstantPool::klass_at_if_loaded(cp, index);
duke@0 1950 if (k == NULL) return NULL;
never@2223 1951 return (jclass) JNIHandles::make_local(k->java_mirror());
duke@0 1952 }
duke@0 1953 JVM_END
duke@0 1954
duke@0 1955 static jobject get_method_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
duke@0 1956 constantTag tag = cp->tag_at(index);
duke@0 1957 if (!tag.is_method() && !tag.is_interface_method()) {
duke@0 1958 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 1959 }
duke@0 1960 int klass_ref = cp->uncached_klass_ref_index_at(index);
coleenp@3602 1961 Klass* k_o;
duke@0 1962 if (force_resolution) {
duke@0 1963 k_o = cp->klass_at(klass_ref, CHECK_NULL);
duke@0 1964 } else {
coleenp@3602 1965 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
duke@0 1966 if (k_o == NULL) return NULL;
duke@0 1967 }
duke@0 1968 instanceKlassHandle k(THREAD, k_o);
coleenp@2062 1969 Symbol* name = cp->uncached_name_ref_at(index);
coleenp@2062 1970 Symbol* sig = cp->uncached_signature_ref_at(index);
duke@0 1971 methodHandle m (THREAD, k->find_method(name, sig));
duke@0 1972 if (m.is_null()) {
duke@0 1973 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up method in target class");
duke@0 1974 }
duke@0 1975 oop method;
duke@0 1976 if (!m->is_initializer() || m->is_static()) {
coleenp@6444 1977 method = Reflection::new_method(m, true, CHECK_NULL);
duke@0 1978 } else {
duke@0 1979 method = Reflection::new_constructor(m, CHECK_NULL);
duke@0 1980 }
duke@0 1981 return JNIHandles::make_local(method);
duke@0 1982 }
duke@0 1983
coleenp@3602 1984 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 1985 {
duke@0 1986 JVMWrapper("JVM_ConstantPoolGetMethodAt");
duke@0 1987 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 1988 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 1989 bounds_check(cp, index, CHECK_NULL);
duke@0 1990 jobject res = get_method_at_helper(cp, index, true, CHECK_NULL);
duke@0 1991 return res;
duke@0 1992 }
duke@0 1993 JVM_END
duke@0 1994
coleenp@3602 1995 JVM_ENTRY(jobject, JVM_ConstantPoolGetMethodAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 1996 {
duke@0 1997 JVMWrapper("JVM_ConstantPoolGetMethodAtIfLoaded");
duke@0 1998 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 1999 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2000 bounds_check(cp, index, CHECK_NULL);
duke@0 2001 jobject res = get_method_at_helper(cp, index, false, CHECK_NULL);
duke@0 2002 return res;
duke@0 2003 }
duke@0 2004 JVM_END
duke@0 2005
duke@0 2006 static jobject get_field_at_helper(constantPoolHandle cp, jint index, bool force_resolution, TRAPS) {
duke@0 2007 constantTag tag = cp->tag_at(index);
duke@0 2008 if (!tag.is_field()) {
duke@0 2009 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2010 }
duke@0 2011 int klass_ref = cp->uncached_klass_ref_index_at(index);
coleenp@3602 2012 Klass* k_o;
duke@0 2013 if (force_resolution) {
duke@0 2014 k_o = cp->klass_at(klass_ref, CHECK_NULL);
duke@0 2015 } else {
coleenp@3602 2016 k_o = ConstantPool::klass_at_if_loaded(cp, klass_ref);
duke@0 2017 if (k_o == NULL) return NULL;
duke@0 2018 }
duke@0 2019 instanceKlassHandle k(THREAD, k_o);
coleenp@2062 2020 Symbol* name = cp->uncached_name_ref_at(index);
coleenp@2062 2021 Symbol* sig = cp->uncached_signature_ref_at(index);
duke@0 2022 fieldDescriptor fd;
coleenp@3602 2023 Klass* target_klass = k->find_field(name, sig, &fd);
duke@0 2024 if (target_klass == NULL) {
duke@0 2025 THROW_MSG_0(vmSymbols::java_lang_RuntimeException(), "Unable to look up field in target class");
duke@0 2026 }
coleenp@6444 2027 oop field = Reflection::new_field(&fd, CHECK_NULL);
duke@0 2028 return JNIHandles::make_local(field);
duke@0 2029 }
duke@0 2030
coleenp@3602 2031 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAt(JNIEnv *env, jobject obj, jobject unusedl, jint index))
duke@0 2032 {
duke@0 2033 JVMWrapper("JVM_ConstantPoolGetFieldAt");
duke@0 2034 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 2035 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2036 bounds_check(cp, index, CHECK_NULL);
duke@0 2037 jobject res = get_field_at_helper(cp, index, true, CHECK_NULL);
duke@0 2038 return res;
duke@0 2039 }
duke@0 2040 JVM_END
duke@0 2041
coleenp@3602 2042 JVM_ENTRY(jobject, JVM_ConstantPoolGetFieldAtIfLoaded(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2043 {
duke@0 2044 JVMWrapper("JVM_ConstantPoolGetFieldAtIfLoaded");
duke@0 2045 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 2046 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2047 bounds_check(cp, index, CHECK_NULL);
duke@0 2048 jobject res = get_field_at_helper(cp, index, false, CHECK_NULL);
duke@0 2049 return res;
duke@0 2050 }
duke@0 2051 JVM_END
duke@0 2052
coleenp@3602 2053 JVM_ENTRY(jobjectArray, JVM_ConstantPoolGetMemberRefInfoAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2054 {
duke@0 2055 JVMWrapper("JVM_ConstantPoolGetMemberRefInfoAt");
duke@0 2056 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 2057 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2058 bounds_check(cp, index, CHECK_NULL);
duke@0 2059 constantTag tag = cp->tag_at(index);
duke@0 2060 if (!tag.is_field_or_method()) {
duke@0 2061 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2062 }
duke@0 2063 int klass_ref = cp->uncached_klass_ref_index_at(index);
coleenp@2062 2064 Symbol* klass_name = cp->klass_name_at(klass_ref);
coleenp@2062 2065 Symbol* member_name = cp->uncached_name_ref_at(index);
coleenp@2062 2066 Symbol* member_sig = cp->uncached_signature_ref_at(index);
never@1142 2067 objArrayOop dest_o = oopFactory::new_objArray(SystemDictionary::String_klass(), 3, CHECK_NULL);
duke@0 2068 objArrayHandle dest(THREAD, dest_o);
duke@0 2069 Handle str = java_lang_String::create_from_symbol(klass_name, CHECK_NULL);
duke@0 2070 dest->obj_at_put(0, str());
duke@0 2071 str = java_lang_String::create_from_symbol(member_name, CHECK_NULL);
duke@0 2072 dest->obj_at_put(1, str());
duke@0 2073 str = java_lang_String::create_from_symbol(member_sig, CHECK_NULL);
duke@0 2074 dest->obj_at_put(2, str());
duke@0 2075 return (jobjectArray) JNIHandles::make_local(dest());
duke@0 2076 }
duke@0 2077 JVM_END
duke@0 2078
coleenp@3602 2079 JVM_ENTRY(jint, JVM_ConstantPoolGetIntAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2080 {
duke@0 2081 JVMWrapper("JVM_ConstantPoolGetIntAt");
coleenp@3602 2082 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2083 bounds_check(cp, index, CHECK_0);
duke@0 2084 constantTag tag = cp->tag_at(index);
duke@0 2085 if (!tag.is_int()) {
duke@0 2086 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2087 }
duke@0 2088 return cp->int_at(index);
duke@0 2089 }
duke@0 2090 JVM_END
duke@0 2091
coleenp@3602 2092 JVM_ENTRY(jlong, JVM_ConstantPoolGetLongAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2093 {
duke@0 2094 JVMWrapper("JVM_ConstantPoolGetLongAt");
coleenp@3602 2095 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2096 bounds_check(cp, index, CHECK_(0L));
duke@0 2097 constantTag tag = cp->tag_at(index);
duke@0 2098 if (!tag.is_long()) {
duke@0 2099 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2100 }
duke@0 2101 return cp->long_at(index);
duke@0 2102 }
duke@0 2103 JVM_END
duke@0 2104
coleenp@3602 2105 JVM_ENTRY(jfloat, JVM_ConstantPoolGetFloatAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2106 {
duke@0 2107 JVMWrapper("JVM_ConstantPoolGetFloatAt");
coleenp@3602 2108 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2109 bounds_check(cp, index, CHECK_(0.0f));
duke@0 2110 constantTag tag = cp->tag_at(index);
duke@0 2111 if (!tag.is_float()) {
duke@0 2112 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2113 }
duke@0 2114 return cp->float_at(index);
duke@0 2115 }
duke@0 2116 JVM_END
duke@0 2117
coleenp@3602 2118 JVM_ENTRY(jdouble, JVM_ConstantPoolGetDoubleAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2119 {
duke@0 2120 JVMWrapper("JVM_ConstantPoolGetDoubleAt");
coleenp@3602 2121 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2122 bounds_check(cp, index, CHECK_(0.0));
duke@0 2123 constantTag tag = cp->tag_at(index);
duke@0 2124 if (!tag.is_double()) {
duke@0 2125 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2126 }
duke@0 2127 return cp->double_at(index);
duke@0 2128 }
duke@0 2129 JVM_END
duke@0 2130
coleenp@3602 2131 JVM_ENTRY(jstring, JVM_ConstantPoolGetStringAt(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2132 {
duke@0 2133 JVMWrapper("JVM_ConstantPoolGetStringAt");
coleenp@3602 2134 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2135 bounds_check(cp, index, CHECK_NULL);
duke@0 2136 constantTag tag = cp->tag_at(index);
coleenp@3602 2137 if (!tag.is_string()) {
duke@0 2138 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2139 }
duke@0 2140 oop str = cp->string_at(index, CHECK_NULL);
duke@0 2141 return (jstring) JNIHandles::make_local(str);
duke@0 2142 }
duke@0 2143 JVM_END
duke@0 2144
coleenp@3602 2145 JVM_ENTRY(jstring, JVM_ConstantPoolGetUTF8At(JNIEnv *env, jobject obj, jobject unused, jint index))
duke@0 2146 {
duke@0 2147 JVMWrapper("JVM_ConstantPoolGetUTF8At");
duke@0 2148 JvmtiVMObjectAllocEventCollector oam;
coleenp@3602 2149 constantPoolHandle cp = constantPoolHandle(THREAD, sun_reflect_ConstantPool::get_cp(JNIHandles::resolve_non_null(obj)));
duke@0 2150 bounds_check(cp, index, CHECK_NULL);
duke@0 2151 constantTag tag = cp->tag_at(index);
duke@0 2152 if (!tag.is_symbol()) {
duke@0 2153 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Wrong type at constant pool index");
duke@0 2154 }
coleenp@2062 2155 Symbol* sym = cp->symbol_at(index);
duke@0 2156 Handle str = java_lang_String::create_from_symbol(sym, CHECK_NULL);
duke@0 2157 return (jstring) JNIHandles::make_local(str());
duke@0 2158 }
duke@0 2159 JVM_END
duke@0 2160
duke@0 2161
duke@0 2162 // Assertion support. //////////////////////////////////////////////////////////
duke@0 2163
duke@0 2164 JVM_ENTRY(jboolean, JVM_DesiredAssertionStatus(JNIEnv *env, jclass unused, jclass cls))
duke@0 2165 JVMWrapper("JVM_DesiredAssertionStatus");
duke@0 2166 assert(cls != NULL, "bad class");
duke@0 2167
duke@0 2168 oop r = JNIHandles::resolve(cls);
duke@0 2169 assert(! java_lang_Class::is_primitive(r), "primitive classes not allowed");
duke@0 2170 if (java_lang_Class::is_primitive(r)) return false;
duke@0 2171
coleenp@3602 2172 Klass* k = java_lang_Class::as_Klass(r);
hseigel@3843 2173 assert(k->oop_is_instance(), "must be an instance klass");
hseigel@3843 2174 if (! k->oop_is_instance()) return false;
duke@0 2175
duke@0 2176 ResourceMark rm(THREAD);
hseigel@3843 2177 const char* name = k->name()->as_C_string();
hseigel@3843 2178 bool system_class = k->class_loader() == NULL;
duke@0 2179 return JavaAssertions::enabled(name, system_class);
duke@0 2180
duke@0 2181 JVM_END
duke@0 2182
duke@0 2183
duke@0 2184 // Return a new AssertionStatusDirectives object with the fields filled in with
duke@0 2185 // command-line assertion arguments (i.e., -ea, -da).
duke@0 2186 JVM_ENTRY(jobject, JVM_AssertionStatusDirectives(JNIEnv *env, jclass unused))
duke@0 2187 JVMWrapper("JVM_AssertionStatusDirectives");
duke@0 2188 JvmtiVMObjectAllocEventCollector oam;
duke@0 2189 oop asd = JavaAssertions::createAssertionStatusDirectives(CHECK_NULL);
duke@0 2190 return JNIHandles::make_local(env, asd);
duke@0 2191 JVM_END
duke@0 2192
duke@0 2193 // Verification ////////////////////////////////////////////////////////////////////////////////
duke@0 2194
duke@0 2195 // Reflection for the verifier /////////////////////////////////////////////////////////////////
duke@0 2196
duke@0 2197 // RedefineClasses support: bug 6214132 caused verification to fail.
duke@0 2198 // All functions from this section should call the jvmtiThreadSate function:
coleenp@3602 2199 // Klass* class_to_verify_considering_redefinition(Klass* klass).
coleenp@3602 2200 // The function returns a Klass* of the _scratch_class if the verifier
duke@0 2201 // was invoked in the middle of the class redefinition.
coleenp@3602 2202 // Otherwise it returns its argument value which is the _the_class Klass*.
duke@0 2203 // Please, refer to the description in the jvmtiThreadSate.hpp.
duke@0 2204
duke@0 2205 JVM_ENTRY(const char*, JVM_GetClassNameUTF(JNIEnv *env, jclass cls))
duke@0 2206 JVMWrapper("JVM_GetClassNameUTF");
coleenp@3602 2207 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2208 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
hseigel@3843 2209 return k->name()->as_utf8();
duke@0 2210 JVM_END
duke@0 2211
duke@0 2212
duke@0 2213 JVM_QUICK_ENTRY(void, JVM_GetClassCPTypes(JNIEnv *env, jclass cls, unsigned char *types))
duke@0 2214 JVMWrapper("JVM_GetClassCPTypes");
coleenp@3602 2215 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2216 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2217 // types will have length zero if this is not an InstanceKlass
duke@0 2218 // (length is determined by call to JVM_GetClassCPEntriesCount)
hseigel@3843 2219 if (k->oop_is_instance()) {
coleenp@3602 2220 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2221 for (int index = cp->length() - 1; index >= 0; index--) {
duke@0 2222 constantTag tag = cp->tag_at(index);
coleenp@3602 2223 types[index] = (tag.is_unresolved_klass()) ? JVM_CONSTANT_Class : tag.value();
duke@0 2224 }
duke@0 2225 }
duke@0 2226 JVM_END
duke@0 2227
duke@0 2228
duke@0 2229 JVM_QUICK_ENTRY(jint, JVM_GetClassCPEntriesCount(JNIEnv *env, jclass cls))
duke@0 2230 JVMWrapper("JVM_GetClassCPEntriesCount");
coleenp@3602 2231 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2232 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
hseigel@3843 2233 if (!k->oop_is_instance())
duke@0 2234 return 0;
coleenp@3602 2235 return InstanceKlass::cast(k)->constants()->length();
duke@0 2236 JVM_END
duke@0 2237
duke@0 2238
duke@0 2239 JVM_QUICK_ENTRY(jint, JVM_GetClassFieldsCount(JNIEnv *env, jclass cls))
duke@0 2240 JVMWrapper("JVM_GetClassFieldsCount");
coleenp@3602 2241 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2242 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
hseigel@3843 2243 if (!k->oop_is_instance())
duke@0 2244 return 0;
coleenp@3602 2245 return InstanceKlass::cast(k)->java_fields_count();
duke@0 2246 JVM_END
duke@0 2247
duke@0 2248
duke@0 2249 JVM_QUICK_ENTRY(jint, JVM_GetClassMethodsCount(JNIEnv *env, jclass cls))
duke@0 2250 JVMWrapper("JVM_GetClassMethodsCount");
coleenp@3602 2251 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2252 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
hseigel@3843 2253 if (!k->oop_is_instance())
duke@0 2254 return 0;
coleenp@3602 2255 return InstanceKlass::cast(k)->methods()->length();
duke@0 2256 JVM_END
duke@0 2257
duke@0 2258
duke@0 2259 // The following methods, used for the verifier, are never called with
coleenp@3602 2260 // array klasses, so a direct cast to InstanceKlass is safe.
duke@0 2261 // Typically, these methods are called in a loop with bounds determined
duke@0 2262 // by the results of JVM_GetClass{Fields,Methods}Count, which return
duke@0 2263 // zero for arrays.
duke@0 2264 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionIndexes(JNIEnv *env, jclass cls, jint method_index, unsigned short *exceptions))
duke@0 2265 JVMWrapper("JVM_GetMethodIxExceptionIndexes");
coleenp@3602 2266 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2267 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2268 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2269 int length = method->checked_exceptions_length();
duke@0 2270 if (length > 0) {
coleenp@3602 2271 CheckedExceptionElement* table= method->checked_exceptions_start();
duke@0 2272 for (int i = 0; i < length; i++) {
duke@0 2273 exceptions[i] = table[i].class_cp_index;
duke@0 2274 }
duke@0 2275 }
duke@0 2276 JVM_END
duke@0 2277
duke@0 2278
duke@0 2279 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionsCount(JNIEnv *env, jclass cls, jint method_index))
duke@0 2280 JVMWrapper("JVM_GetMethodIxExceptionsCount");
coleenp@3602 2281 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2282 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2283 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2284 return method->checked_exceptions_length();
duke@0 2285 JVM_END
duke@0 2286
duke@0 2287
duke@0 2288 JVM_QUICK_ENTRY(void, JVM_GetMethodIxByteCode(JNIEnv *env, jclass cls, jint method_index, unsigned char *code))
duke@0 2289 JVMWrapper("JVM_GetMethodIxByteCode");
coleenp@3602 2290 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2291 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2292 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2293 memcpy(code, method->code_base(), method->code_size());
duke@0 2294 JVM_END
duke@0 2295
duke@0 2296
duke@0 2297 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxByteCodeLength(JNIEnv *env, jclass cls, jint method_index))
duke@0 2298 JVMWrapper("JVM_GetMethodIxByteCodeLength");
coleenp@3602 2299 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2300 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2301 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2302 return method->code_size();
duke@0 2303 JVM_END
duke@0 2304
duke@0 2305
duke@0 2306 JVM_QUICK_ENTRY(void, JVM_GetMethodIxExceptionTableEntry(JNIEnv *env, jclass cls, jint method_index, jint entry_index, JVM_ExceptionTableEntryType *entry))
duke@0 2307 JVMWrapper("JVM_GetMethodIxExceptionTableEntry");
coleenp@3602 2308 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2309 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2310 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2311 ExceptionTable extable(method);
jiangli@3482 2312 entry->start_pc = extable.start_pc(entry_index);
jiangli@3482 2313 entry->end_pc = extable.end_pc(entry_index);
jiangli@3482 2314 entry->handler_pc = extable.handler_pc(entry_index);
jiangli@3482 2315 entry->catchType = extable.catch_type_index(entry_index);
duke@0 2316 JVM_END
duke@0 2317
duke@0 2318
duke@0 2319 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxExceptionTableLength(JNIEnv *env, jclass cls, int method_index))
duke@0 2320 JVMWrapper("JVM_GetMethodIxExceptionTableLength");
coleenp@3602 2321 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2322 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2323 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2324 return method->exception_table_length();
duke@0 2325 JVM_END
duke@0 2326
duke@0 2327
duke@0 2328 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxModifiers(JNIEnv *env, jclass cls, int method_index))
duke@0 2329 JVMWrapper("JVM_GetMethodIxModifiers");
coleenp@3602 2330 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2331 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2332 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2333 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
duke@0 2334 JVM_END
duke@0 2335
duke@0 2336
duke@0 2337 JVM_QUICK_ENTRY(jint, JVM_GetFieldIxModifiers(JNIEnv *env, jclass cls, int field_index))
duke@0 2338 JVMWrapper("JVM_GetFieldIxModifiers");
coleenp@3602 2339 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2340 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2341 return InstanceKlass::cast(k)->field_access_flags(field_index) & JVM_RECOGNIZED_FIELD_MODIFIERS;
duke@0 2342 JVM_END
duke@0 2343
duke@0 2344
duke@0 2345 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxLocalsCount(JNIEnv *env, jclass cls, int method_index))
duke@0 2346 JVMWrapper("JVM_GetMethodIxLocalsCount");
coleenp@3602 2347 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2348 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2349 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2350 return method->max_locals();
duke@0 2351 JVM_END
duke@0 2352
duke@0 2353
duke@0 2354 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxArgsSize(JNIEnv *env, jclass cls, int method_index))
duke@0 2355 JVMWrapper("JVM_GetMethodIxArgsSize");
coleenp@3602 2356 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2357 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2358 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2359 return method->size_of_parameters();
duke@0 2360 JVM_END
duke@0 2361
duke@0 2362
duke@0 2363 JVM_QUICK_ENTRY(jint, JVM_GetMethodIxMaxStack(JNIEnv *env, jclass cls, int method_index))
duke@0 2364 JVMWrapper("JVM_GetMethodIxMaxStack");
coleenp@3602 2365 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2366 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2367 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2368 return method->verifier_max_stack();
duke@0 2369 JVM_END
duke@0 2370
duke@0 2371
duke@0 2372 JVM_QUICK_ENTRY(jboolean, JVM_IsConstructorIx(JNIEnv *env, jclass cls, int method_index))
duke@0 2373 JVMWrapper("JVM_IsConstructorIx");
duke@0 2374 ResourceMark rm(THREAD);
coleenp@3602 2375 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2376 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2377 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2378 return method->name() == vmSymbols::object_initializer_name();
duke@0 2379 JVM_END
duke@0 2380
duke@0 2381
acorn@4064 2382 JVM_QUICK_ENTRY(jboolean, JVM_IsVMGeneratedMethodIx(JNIEnv *env, jclass cls, int method_index))
acorn@4064 2383 JVMWrapper("JVM_IsVMGeneratedMethodIx");
acorn@4064 2384 ResourceMark rm(THREAD);
acorn@4064 2385 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
acorn@4064 2386 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
acorn@4064 2387 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
acorn@4064 2388 return method->is_overpass();
acorn@4064 2389 JVM_END
acorn@4064 2390
duke@0 2391 JVM_ENTRY(const char*, JVM_GetMethodIxNameUTF(JNIEnv *env, jclass cls, jint method_index))
duke@0 2392 JVMWrapper("JVM_GetMethodIxIxUTF");
coleenp@3602 2393 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2394 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2395 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2396 return method->name()->as_utf8();
duke@0 2397 JVM_END
duke@0 2398
duke@0 2399
duke@0 2400 JVM_ENTRY(const char*, JVM_GetMethodIxSignatureUTF(JNIEnv *env, jclass cls, jint method_index))
duke@0 2401 JVMWrapper("JVM_GetMethodIxSignatureUTF");
coleenp@3602 2402 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2403 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2404 Method* method = InstanceKlass::cast(k)->methods()->at(method_index);
coleenp@3602 2405 return method->signature()->as_utf8();
duke@0 2406 JVM_END
duke@0 2407
duke@0 2408 /**
duke@0 2409 * All of these JVM_GetCP-xxx methods are used by the old verifier to
duke@0 2410 * read entries in the constant pool. Since the old verifier always
duke@0 2411 * works on a copy of the code, it will not see any rewriting that
duke@0 2412 * may possibly occur in the middle of verification. So it is important
duke@0 2413 * that nothing it calls tries to use the cpCache instead of the raw
duke@0 2414 * constant pool, so we must use cp->uncached_x methods when appropriate.
duke@0 2415 */
duke@0 2416 JVM_ENTRY(const char*, JVM_GetCPFieldNameUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2417 JVMWrapper("JVM_GetCPFieldNameUTF");
coleenp@3602 2418 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2419 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2420 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2421 switch (cp->tag_at(cp_index).value()) {
duke@0 2422 case JVM_CONSTANT_Fieldref:
duke@0 2423 return cp->uncached_name_ref_at(cp_index)->as_utf8();
duke@0 2424 default:
duke@0 2425 fatal("JVM_GetCPFieldNameUTF: illegal constant");
duke@0 2426 }
duke@0 2427 ShouldNotReachHere();
duke@0 2428 return NULL;
duke@0 2429 JVM_END
duke@0 2430
duke@0 2431
duke@0 2432 JVM_ENTRY(const char*, JVM_GetCPMethodNameUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2433 JVMWrapper("JVM_GetCPMethodNameUTF");
coleenp@3602 2434 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2435 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2436 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2437 switch (cp->tag_at(cp_index).value()) {
duke@0 2438 case JVM_CONSTANT_InterfaceMethodref:
duke@0 2439 case JVM_CONSTANT_Methodref:
jrose@1059 2440 case JVM_CONSTANT_NameAndType: // for invokedynamic
duke@0 2441 return cp->uncached_name_ref_at(cp_index)->as_utf8();
duke@0 2442 default:
duke@0 2443 fatal("JVM_GetCPMethodNameUTF: illegal constant");
duke@0 2444 }
duke@0 2445 ShouldNotReachHere();
duke@0 2446 return NULL;
duke@0 2447 JVM_END
duke@0 2448
duke@0 2449
duke@0 2450 JVM_ENTRY(const char*, JVM_GetCPMethodSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2451 JVMWrapper("JVM_GetCPMethodSignatureUTF");
coleenp@3602 2452 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2453 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2454 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2455 switch (cp->tag_at(cp_index).value()) {
duke@0 2456 case JVM_CONSTANT_InterfaceMethodref:
duke@0 2457 case JVM_CONSTANT_Methodref:
jrose@1059 2458 case JVM_CONSTANT_NameAndType: // for invokedynamic
duke@0 2459 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
duke@0 2460 default:
duke@0 2461 fatal("JVM_GetCPMethodSignatureUTF: illegal constant");
duke@0 2462 }
duke@0 2463 ShouldNotReachHere();
duke@0 2464 return NULL;
duke@0 2465 JVM_END
duke@0 2466
duke@0 2467
duke@0 2468 JVM_ENTRY(const char*, JVM_GetCPFieldSignatureUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2469 JVMWrapper("JVM_GetCPFieldSignatureUTF");
coleenp@3602 2470 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2471 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2472 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2473 switch (cp->tag_at(cp_index).value()) {
duke@0 2474 case JVM_CONSTANT_Fieldref:
duke@0 2475 return cp->uncached_signature_ref_at(cp_index)->as_utf8();
duke@0 2476 default:
duke@0 2477 fatal("JVM_GetCPFieldSignatureUTF: illegal constant");
duke@0 2478 }
duke@0 2479 ShouldNotReachHere();
duke@0 2480 return NULL;
duke@0 2481 JVM_END
duke@0 2482
duke@0 2483
duke@0 2484 JVM_ENTRY(const char*, JVM_GetCPClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2485 JVMWrapper("JVM_GetCPClassNameUTF");
coleenp@3602 2486 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2487 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2488 ConstantPool* cp = InstanceKlass::cast(k)->constants();
coleenp@2062 2489 Symbol* classname = cp->klass_name_at(cp_index);
duke@0 2490 return classname->as_utf8();
duke@0 2491 JVM_END
duke@0 2492
duke@0 2493
duke@0 2494 JVM_ENTRY(const char*, JVM_GetCPFieldClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2495 JVMWrapper("JVM_GetCPFieldClassNameUTF");
coleenp@3602 2496 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2497 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2498 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2499 switch (cp->tag_at(cp_index).value()) {
duke@0 2500 case JVM_CONSTANT_Fieldref: {
duke@0 2501 int class_index = cp->uncached_klass_ref_index_at(cp_index);
coleenp@2062 2502 Symbol* classname = cp->klass_name_at(class_index);
duke@0 2503 return classname->as_utf8();
duke@0 2504 }
duke@0 2505 default:
duke@0 2506 fatal("JVM_GetCPFieldClassNameUTF: illegal constant");
duke@0 2507 }
duke@0 2508 ShouldNotReachHere();
duke@0 2509 return NULL;
duke@0 2510 JVM_END
duke@0 2511
duke@0 2512
duke@0 2513 JVM_ENTRY(const char*, JVM_GetCPMethodClassNameUTF(JNIEnv *env, jclass cls, jint cp_index))
duke@0 2514 JVMWrapper("JVM_GetCPMethodClassNameUTF");
coleenp@3602 2515 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
duke@0 2516 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
coleenp@3602 2517 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2518 switch (cp->tag_at(cp_index).value()) {
duke@0 2519 case JVM_CONSTANT_Methodref:
duke@0 2520 case JVM_CONSTANT_InterfaceMethodref: {
duke@0 2521 int class_index = cp->uncached_klass_ref_index_at(cp_index);
coleenp@2062 2522 Symbol* classname = cp->klass_name_at(class_index);
duke@0 2523 return classname->as_utf8();
duke@0 2524 }
duke@0 2525 default:
duke@0 2526 fatal("JVM_GetCPMethodClassNameUTF: illegal constant");
duke@0 2527 }
duke@0 2528 ShouldNotReachHere();
duke@0 2529 return NULL;
duke@0 2530 JVM_END
duke@0 2531
duke@0 2532
never@2702 2533 JVM_ENTRY(jint, JVM_GetCPFieldModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
duke@0 2534 JVMWrapper("JVM_GetCPFieldModifiers");
coleenp@3602 2535 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
coleenp@3602 2536 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
duke@0 2537 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
duke@0 2538 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
coleenp@3602 2539 ConstantPool* cp = InstanceKlass::cast(k)->constants();
coleenp@3602 2540 ConstantPool* cp_called = InstanceKlass::cast(k_called)->constants();
duke@0 2541 switch (cp->tag_at(cp_index).value()) {
duke@0 2542 case JVM_CONSTANT_Fieldref: {
coleenp@2062 2543 Symbol* name = cp->uncached_name_ref_at(cp_index);
coleenp@2062 2544 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
never@2702 2545 for (JavaFieldStream fs(k_called); !fs.done(); fs.next()) {
never@2702 2546 if (fs.name() == name && fs.signature() == signature) {
never@2702 2547 return fs.access_flags().as_short() & JVM_RECOGNIZED_FIELD_MODIFIERS;
duke@0 2548 }
duke@0 2549 }
duke@0 2550 return -1;
duke@0 2551 }
duke@0 2552 default:
duke@0 2553 fatal("JVM_GetCPFieldModifiers: illegal constant");
duke@0 2554 }
duke@0 2555 ShouldNotReachHere();
duke@0 2556 return 0;
duke@0 2557 JVM_END
duke@0 2558
duke@0 2559
duke@0 2560 JVM_QUICK_ENTRY(jint, JVM_GetCPMethodModifiers(JNIEnv *env, jclass cls, int cp_index, jclass called_cls))
duke@0 2561 JVMWrapper("JVM_GetCPMethodModifiers");
coleenp@3602 2562 Klass* k = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(cls));
coleenp@3602 2563 Klass* k_called = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(called_cls));
duke@0 2564 k = JvmtiThreadState::class_to_verify_considering_redefinition(k, thread);
duke@0 2565 k_called = JvmtiThreadState::class_to_verify_considering_redefinition(k_called, thread);
coleenp@3602 2566 ConstantPool* cp = InstanceKlass::cast(k)->constants();
duke@0 2567 switch (cp->tag_at(cp_index).value()) {
duke@0 2568 case JVM_CONSTANT_Methodref:
duke@0 2569 case JVM_CONSTANT_InterfaceMethodref: {
coleenp@2062 2570 Symbol* name = cp->uncached_name_ref_at(cp_index);
coleenp@2062 2571 Symbol* signature = cp->uncached_signature_ref_at(cp_index);
coleenp@3602 2572 Array<Method*>* methods = InstanceKlass::cast(k_called)->methods();
duke@0 2573 int methods_count = methods->length();
duke@0 2574 for (int i = 0; i < methods_count; i++) {
coleenp@3602 2575 Method* method = methods->at(i);
duke@0 2576 if (method->name() == name && method->signature() == signature) {
duke@0 2577 return method->access_flags().as_int() & JVM_RECOGNIZED_METHOD_MODIFIERS;
duke@0 2578 }
duke@0 2579 }
duke@0 2580 return -1;
duke@0 2581 }
duke@0 2582 default:
duke@0 2583 fatal("JVM_GetCPMethodModifiers: illegal constant");
duke@0 2584 }
duke@0 2585 ShouldNotReachHere();
duke@0 2586 return 0;
duke@0 2587 JVM_END
duke@0 2588
duke@0 2589
duke@0 2590 // Misc //////////////////////////////////////////////////////////////////////////////////////////////
duke@0 2591
duke@0 2592 JVM_LEAF(void, JVM_ReleaseUTF(const char *utf))
duke@0 2593 // So long as UTF8::convert_to_utf8 returns resource strings, we don't have to do anything
duke@0 2594 JVM_END
duke@0 2595
duke@0 2596
duke@0 2597 JVM_ENTRY(jboolean, JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2))
duke@0 2598 JVMWrapper("JVM_IsSameClassPackage");
duke@0 2599 oop class1_mirror = JNIHandles::resolve_non_null(class1);
duke@0 2600 oop class2_mirror = JNIHandles::resolve_non_null(class2);
coleenp@3602 2601 Klass* klass1 = java_lang_Class::as_Klass(class1_mirror);
coleenp@3602 2602 Klass* klass2 = java_lang_Class::as_Klass(class2_mirror);
duke@0 2603 return (jboolean) Reflection::is_same_class_package(klass1, klass2);
duke@0 2604 JVM_END
duke@0 2605
duke@0 2606 // Printing support //////////////////////////////////////////////////
duke@0 2607 extern "C" {
duke@0 2608
drchase@6412 2609 ATTRIBUTE_PRINTF(3, 0)
duke@0 2610 int jio_vsnprintf(char *str, size_t count, const char *fmt, va_list args) {
duke@0 2611 // see bug 4399518, 4417214
duke@0 2612 if ((intptr_t)count <= 0) return -1;
goetz@7331 2613
goetz@7331 2614 int result = vsnprintf(str, count, fmt, args);
coleenp@7408 2615 // Note: on truncation vsnprintf(3) on Unix returns numbers of
coleenp@7408 2616 // characters which would have been written had the buffer been large
coleenp@7408 2617 // enough; on Windows, it returns -1. We handle both cases here and
coleenp@7408 2618 // always return -1, and perform null termination.
goetz@7331 2619 if ((result > 0 && (size_t)result >= count) || result == -1) {
goetz@7331 2620 str[count - 1] = '\0';
goetz@7331 2621 result = -1;
goetz@7331 2622 }
goetz@7331 2623
goetz@7331 2624 return result;
duke@0 2625 }
duke@0 2626
drchase@6412 2627 ATTRIBUTE_PRINTF(3, 0)
duke@0 2628 int jio_snprintf(char *str, size_t count, const char *fmt, ...) {
duke@0 2629 va_list args;
duke@0 2630 int len;
duke@0 2631 va_start(args, fmt);
duke@0 2632 len = jio_vsnprintf(str, count, fmt, args);
duke@0 2633 va_end(args);
duke@0 2634 return len;
duke@0 2635 }
duke@0 2636
drchase@6412 2637 ATTRIBUTE_PRINTF(2,3)
duke@0 2638 int jio_fprintf(FILE* f, const char *fmt, ...) {
duke@0 2639 int len;
duke@0 2640 va_list args;
duke@0 2641 va_start(args, fmt);
duke@0 2642 len = jio_vfprintf(f, fmt, args);
duke@0 2643 va_end(args);
duke@0 2644 return len;
duke@0 2645 }
duke@0 2646
drchase@6412 2647 ATTRIBUTE_PRINTF(2, 0)
duke@0 2648 int jio_vfprintf(FILE* f, const char *fmt, va_list args) {
duke@0 2649 if (Arguments::vfprintf_hook() != NULL) {
duke@0 2650 return Arguments::vfprintf_hook()(f, fmt, args);
duke@0 2651 } else {
duke@0 2652 return vfprintf(f, fmt, args);
duke@0 2653 }
duke@0 2654 }
duke@0 2655
drchase@6412 2656 ATTRIBUTE_PRINTF(1, 2)
coleenp@2072 2657 JNIEXPORT int jio_printf(const char *fmt, ...) {
duke@0 2658 int len;
duke@0 2659 va_list args;
duke@0 2660 va_start(args, fmt);
duke@0 2661 len = jio_vfprintf(defaultStream::output_stream(), fmt, args);
duke@0 2662 va_end(args);
duke@0 2663 return len;
duke@0 2664 }
duke@0 2665
duke@0 2666
duke@0 2667 // HotSpot specific jio method
duke@0 2668 void jio_print(const char* s) {
duke@0 2669 // Try to make this function as atomic as possible.
duke@0 2670 if (Arguments::vfprintf_hook() != NULL) {
duke@0 2671 jio_fprintf(defaultStream::output_stream(), "%s", s);
duke@0 2672 } else {
xlu@513 2673 // Make an unused local variable to avoid warning from gcc 4.x compiler.
xlu@513 2674 size_t count = ::write(defaultStream::output_fd(), s, (int)strlen(s));
duke@0 2675 }
duke@0 2676 }
duke@0 2677
duke@0 2678 } // Extern C
duke@0 2679
duke@0 2680 // java.lang.Thread //////////////////////////////////////////////////////////////////////////////
duke@0 2681
duke@0 2682 // In most of the JVM Thread support functions we need to be sure to lock the Threads_lock
duke@0 2683 // to prevent the target thread from exiting after we have a pointer to the C++ Thread or
duke@0 2684 // OSThread objects. The exception to this rule is when the target object is the thread
duke@0 2685 // doing the operation, in which case we know that the thread won't exit until the
duke@0 2686 // operation is done (all exits being voluntary). There are a few cases where it is
duke@0 2687 // rather silly to do operations on yourself, like resuming yourself or asking whether
duke@0 2688 // you are alive. While these can still happen, they are not subject to deadlocks if
duke@0 2689 // the lock is held while the operation occurs (this is not the case for suspend, for
duke@0 2690 // instance), and are very unlikely. Because IsAlive needs to be fast and its
duke@0 2691 // implementation is local to this file, we always lock Threads_lock for that one.
duke@0 2692
duke@0 2693 static void thread_entry(JavaThread* thread, TRAPS) {
duke@0 2694 HandleMark hm(THREAD);
duke@0 2695 Handle obj(THREAD, thread->threadObj());
duke@0 2696 JavaValue result(T_VOID);
duke@0 2697 JavaCalls::call_virtual(&result,
duke@0 2698 obj,
never@1142 2699 KlassHandle(THREAD, SystemDictionary::Thread_klass()),
coleenp@2062 2700 vmSymbols::run_method_name(),
coleenp@2062 2701 vmSymbols::void_method_signature(),
duke@0 2702 THREAD);
duke@0 2703 }
duke@0 2704
duke@0 2705
duke@0 2706 JVM_ENTRY(void, JVM_StartThread(JNIEnv* env, jobject jthread))
duke@0 2707 JVMWrapper("JVM_StartThread");
duke@0 2708 JavaThread *native_thread = NULL;
duke@0 2709
duke@0 2710 // We cannot hold the Threads_lock when we throw an exception,
duke@0 2711 // due to rank ordering issues. Example: we might need to grab the
duke@0 2712 // Heap_lock while we construct the exception.
duke@0 2713 bool throw_illegal_thread_state = false;
duke@0 2714
duke@0 2715 // We must release the Threads_lock before we can post a jvmti event
duke@0 2716 // in Thread::start.
duke@0 2717 {
duke@0 2718 // Ensure that the C++ Thread and OSThread structures aren't freed before
duke@0 2719 // we operate.
duke@0 2720 MutexLocker mu(Threads_lock);
duke@0 2721
dholmes@2047 2722 // Since JDK 5 the java.lang.Thread threadStatus is used to prevent
dholmes@2047 2723 // re-starting an already started thread, so we should usually find
dholmes@2047 2724 // that the JavaThread is null. However for a JNI attached thread
dholmes@2047 2725 // there is a small window between the Thread object being created
dholmes@2047 2726 // (with its JavaThread set) and the update to its threadStatus, so we
dholmes@2047 2727 // have to check for this
dholmes@2047 2728 if (java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread)) != NULL) {
dholmes@2047 2729 throw_illegal_thread_state = true;
duke@0 2730 } else {
dholmes@2047 2731 // We could also check the stillborn flag to see if this thread was already stopped, but
dholmes@2047 2732 // for historical reasons we let the thread detect that itself when it starts running
dholmes@2047 2733
duke@0 2734 jlong size =
duke@0 2735 java_lang_Thread::stackSize(JNIHandles::resolve_non_null(jthread));
duke@0 2736 // Allocate the C++ Thread structure and create the native thread. The
duke@0 2737 // stack size retrieved from java is signed, but the constructor takes
duke@0 2738 // size_t (an unsigned type), so avoid passing negative values which would
duke@0 2739 // result in really large stacks.
duke@0 2740 size_t sz = size > 0 ? (size_t) size : 0;
duke@0 2741 native_thread = new JavaThread(&thread_entry, sz);
duke@0 2742
duke@0 2743 // At this point it may be possible that no osthread was created for the
duke@0 2744 // JavaThread due to lack of memory. Check for this situation and throw
duke@0 2745 // an exception if necessary. Eventually we may want to change this so
duke@0 2746 // that we only grab the lock if the thread was created successfully -
duke@0 2747 // then we can also do this check and throw the exception in the
duke@0 2748 // JavaThread constructor.
duke@0 2749 if (native_thread->osthread() != NULL) {
duke@0 2750 // Note: the current thread is not being used within "prepare".
duke@0 2751 native_thread->prepare(jthread);
duke@0 2752 }
duke@0 2753 }
duke@0 2754 }
duke@0 2755
duke@0 2756 if (throw_illegal_thread_state) {
duke@0 2757 THROW(vmSymbols::java_lang_IllegalThreadStateException());
duke@0 2758 }
duke@0 2759
duke@0 2760 assert(native_thread != NULL, "Starting null thread?");
duke@0 2761
duke@0 2762 if (native_thread->osthread() == NULL) {
duke@0 2763 // No one should hold a reference to the 'native_thread'.
duke@0 2764 delete native_thread;
duke@0 2765 if (JvmtiExport::should_post_resource_exhausted()) {
duke@0 2766 JvmtiExport::post_resource_exhausted(
duke@0 2767 JVMTI_RESOURCE_EXHAUSTED_OOM_ERROR | JVMTI_RESOURCE_EXHAUSTED_THREADS,
iklam@5896 2768 os::native_thread_creation_failed_msg());
duke@0 2769 }
duke@0 2770 THROW_MSG(vmSymbols::java_lang_OutOfMemoryError(),
iklam@5896 2771 os::native_thread_creation_failed_msg());
duke@0 2772 }
duke@0 2773
duke@0 2774 Thread::start(native_thread);
duke@0 2775
duke@0 2776 JVM_END
duke@0 2777
duke@0 2778 // JVM_Stop is implemented using a VM_Operation, so threads are forced to safepoints
duke@0 2779 // before the quasi-asynchronous exception is delivered. This is a little obtrusive,
duke@0 2780 // but is thought to be reliable and simple. In the case, where the receiver is the
dholmes@2047 2781 // same thread as the sender, no safepoint is needed.
duke@0 2782 JVM_ENTRY(void, JVM_StopThread(JNIEnv* env, jobject jthread, jobject throwable))
duke@0 2783 JVMWrapper("JVM_StopThread");
duke@0 2784
duke@0 2785 oop java_throwable = JNIHandles::resolve(throwable);
duke@0 2786 if (java_throwable == NULL) {
duke@0 2787 THROW(vmSymbols::java_lang_NullPointerException());
duke@0 2788 }
duke@0 2789 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 2790 JavaThread* receiver = java_lang_Thread::thread(java_thread);
never@3064 2791 Events::log_exception(JavaThread::current(),
never@3064 2792 "JVM_StopThread thread JavaThread " INTPTR_FORMAT " as oop " INTPTR_FORMAT " [exception " INTPTR_FORMAT "]",
drchase@6412 2793 p2i(receiver), p2i((address)java_thread), p2i(throwable));
dholmes@2047 2794 // First check if thread is alive
duke@0 2795 if (receiver != NULL) {
duke@0 2796 // Check if exception is getting thrown at self (use oop equality, since the
duke@0 2797 // target object might exit)
duke@0 2798 if (java_thread == thread->threadObj()) {
duke@0 2799 THROW_OOP(java_throwable);
duke@0 2800 } else {
duke@0 2801 // Enques a VM_Operation to stop all threads and then deliver the exception...
duke@0 2802 Thread::send_async_exception(java_thread, JNIHandles::resolve(throwable));
duke@0 2803 }
duke@0 2804 }
dholmes@2047 2805 else {
dholmes@2047 2806 // Either:
dholmes@2047 2807 // - target thread has not been started before being stopped, or
dholmes@2047 2808 // - target thread already terminated
dholmes@2047 2809 // We could read the threadStatus to determine which case it is
dholmes@2047 2810 // but that is overkill as it doesn't matter. We must set the
dholmes@2047 2811 // stillborn flag for the first case, and if the thread has already
dholmes@2047 2812 // exited setting this flag has no affect
dholmes@2047 2813 java_lang_Thread::set_stillborn(java_thread);
dholmes@2047 2814 }
duke@0 2815 JVM_END
duke@0 2816
duke@0 2817
duke@0 2818 JVM_ENTRY(jboolean, JVM_IsThreadAlive(JNIEnv* env, jobject jthread))
duke@0 2819 JVMWrapper("JVM_IsThreadAlive");
duke@0 2820
duke@0 2821 oop thread_oop = JNIHandles::resolve_non_null(jthread);
duke@0 2822 return java_lang_Thread::is_alive(thread_oop);
duke@0 2823 JVM_END
duke@0 2824
duke@0 2825
duke@0 2826 JVM_ENTRY(void, JVM_SuspendThread(JNIEnv* env, jobject jthread))
duke@0 2827 JVMWrapper("JVM_SuspendThread");
duke@0 2828 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 2829 JavaThread* receiver = java_lang_Thread::thread(java_thread);
duke@0 2830
duke@0 2831 if (receiver != NULL) {
duke@0 2832 // thread has run and has not exited (still on threads list)
duke@0 2833
duke@0 2834 {
duke@0 2835 MutexLockerEx ml(receiver->SR_lock(), Mutex::_no_safepoint_check_flag);
duke@0 2836 if (receiver->is_external_suspend()) {
duke@0 2837 // Don't allow nested external suspend requests. We can't return
duke@0 2838 // an error from this interface so just ignore the problem.
duke@0 2839 return;
duke@0 2840 }
duke@0 2841 if (receiver->is_exiting()) { // thread is in the process of exiting
duke@0 2842 return;
duke@0 2843 }
duke@0 2844 receiver->set_external_suspend();
duke@0 2845 }
duke@0 2846
duke@0 2847 // java_suspend() will catch threads in the process of exiting
duke@0 2848 // and will ignore them.
duke@0 2849 receiver->java_suspend();
duke@0 2850
duke@0 2851 // It would be nice to have the following assertion in all the
duke@0 2852 // time, but it is possible for a racing resume request to have
duke@0 2853 // resumed this thread right after we suspended it. Temporarily
duke@0 2854 // enable this assertion if you are chasing a different kind of
duke@0 2855 // bug.
duke@0 2856 //
duke@0 2857 // assert(java_lang_Thread::thread(receiver->threadObj()) == NULL ||
duke@0 2858 // receiver->is_being_ext_suspended(), "thread is not suspended");
duke@0 2859 }
duke@0 2860 JVM_END
duke@0 2861
duke@0 2862
duke@0 2863 JVM_ENTRY(void, JVM_ResumeThread(JNIEnv* env, jobject jthread))
duke@0 2864 JVMWrapper("JVM_ResumeThread");
duke@0 2865 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate.
duke@0 2866 // We need to *always* get the threads lock here, since this operation cannot be allowed during
duke@0 2867 // a safepoint. The safepoint code relies on suspending a thread to examine its state. If other
duke@0 2868 // threads randomly resumes threads, then a thread might not be suspended when the safepoint code
duke@0 2869 // looks at it.
duke@0 2870 MutexLocker ml(Threads_lock);
duke@0 2871 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
duke@0 2872 if (thr != NULL) {
duke@0 2873 // the thread has run and is not in the process of exiting
duke@0 2874 thr->java_resume();
duke@0 2875 }
duke@0 2876 JVM_END
duke@0 2877
duke@0 2878
duke@0 2879 JVM_ENTRY(void, JVM_SetThreadPriority(JNIEnv* env, jobject jthread, jint prio))
duke@0 2880 JVMWrapper("JVM_SetThreadPriority");
duke@0 2881 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
duke@0 2882 MutexLocker ml(Threads_lock);
duke@0 2883 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 2884 java_lang_Thread::set_priority(java_thread, (ThreadPriority)prio);
duke@0 2885 JavaThread* thr = java_lang_Thread::thread(java_thread);
duke@0 2886 if (thr != NULL) { // Thread not yet started; priority pushed down when it is
duke@0 2887 Thread::set_priority(thr, (ThreadPriority)prio);
duke@0 2888 }
duke@0 2889 JVM_END
duke@0 2890
duke@0 2891
duke@0 2892 JVM_ENTRY(void, JVM_Yield(JNIEnv *env, jclass threadClass))
duke@0 2893 JVMWrapper("JVM_Yield");
duke@0 2894 if (os::dont_yield()) return;
dcubed@2767 2895 HOTSPOT_THREAD_YIELD();
sla@5934 2896
duke@0 2897 // When ConvertYieldToSleep is off (default), this matches the classic VM use of yield.
duke@0 2898 // Critical for similar threading behaviour
duke@0 2899 if (ConvertYieldToSleep) {
duke@0 2900 os::sleep(thread, MinSleepInterval, false);
duke@0 2901 } else {
fparain@6692 2902 os::naked_yield();
duke@0 2903 }
duke@0 2904 JVM_END
duke@0 2905
duke@0 2906
duke@0 2907 JVM_ENTRY(void, JVM_Sleep(JNIEnv* env, jclass threadClass, jlong millis))
duke@0 2908 JVMWrapper("JVM_Sleep");
duke@0 2909
duke@0 2910 if (millis < 0) {
duke@0 2911 THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(), "timeout value is negative");
duke@0 2912 }
duke@0 2913
duke@0 2914 if (Thread::is_interrupted (THREAD, true) && !HAS_PENDING_EXCEPTION) {
duke@0 2915 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
duke@0 2916 }
duke@0 2917
duke@0 2918 // Save current thread state and restore it at the end of this block.
duke@0 2919 // And set new thread state to SLEEPING.
duke@0 2920 JavaThreadSleepState jtss(thread);
duke@0 2921
sla@5934 2922 HOTSPOT_THREAD_SLEEP_BEGIN(millis);
fparain@1324 2923
sla@4802 2924 EventThreadSleep event;
sla@4802 2925
duke@0 2926 if (millis == 0) {
duke@0 2927 // When ConvertSleepToYield is on, this matches the classic VM implementation of
duke@0 2928 // JVM_Sleep. Critical for similar threading behaviour (Win32)
duke@0 2929 // It appears that in certain GUI contexts, it may be beneficial to do a short sleep
duke@0 2930 // for SOLARIS
duke@0 2931 if (ConvertSleepToYield) {
fparain@6692 2932 os::naked_yield();
duke@0 2933 } else {
duke@0 2934 ThreadState old_state = thread->osthread()->get_state();
duke@0 2935 thread->osthread()->set_state(SLEEPING);
duke@0 2936 os::sleep(thread, MinSleepInterval, false);
duke@0 2937 thread->osthread()->set_state(old_state);
duke@0 2938 }
duke@0 2939 } else {
duke@0 2940 ThreadState old_state = thread->osthread()->get_state();
duke@0 2941 thread->osthread()->set_state(SLEEPING);
duke@0 2942 if (os::sleep(thread, millis, true) == OS_INTRPT) {
duke@0 2943 // An asynchronous exception (e.g., ThreadDeathException) could have been thrown on
duke@0 2944 // us while we were sleeping. We do not overwrite those.
duke@0 2945 if (!HAS_PENDING_EXCEPTION) {
sla@4802 2946 if (event.should_commit()) {
sla@4802 2947 event.set_time(millis);
sla@4802 2948 event.commit();
sla@4802 2949 }
sla@5934 2950 HOTSPOT_THREAD_SLEEP_END(1);
sla@5934 2951
duke@0 2952 // TODO-FIXME: THROW_MSG returns which means we will not call set_state()
duke@0 2953 // to properly restore the thread state. That's likely wrong.
duke@0 2954 THROW_MSG(vmSymbols::java_lang_InterruptedException(), "sleep interrupted");
duke@0 2955 }
duke@0 2956 }
duke@0 2957 thread->osthread()->set_state(old_state);
duke@0 2958 }
sla@4802 2959 if (event.should_commit()) {
sla@4802 2960 event.set_time(millis);
sla@4802 2961 event.commit();
sla@4802 2962 }
sla@5934 2963 HOTSPOT_THREAD_SLEEP_END(0);
duke@0 2964 JVM_END
duke@0 2965
duke@0 2966 JVM_ENTRY(jobject, JVM_CurrentThread(JNIEnv* env, jclass threadClass))
duke@0 2967 JVMWrapper("JVM_CurrentThread");
duke@0 2968 oop jthread = thread->threadObj();
duke@0 2969 assert (thread != NULL, "no current thread!");
duke@0 2970 return JNIHandles::make_local(env, jthread);
duke@0 2971 JVM_END
duke@0 2972
duke@0 2973
duke@0 2974 JVM_ENTRY(jint, JVM_CountStackFrames(JNIEnv* env, jobject jthread))
duke@0 2975 JVMWrapper("JVM_CountStackFrames");
duke@0 2976
duke@0 2977 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
duke@0 2978 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 2979 bool throw_illegal_thread_state = false;
duke@0 2980 int count = 0;
duke@0 2981
duke@0 2982 {
duke@0 2983 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
duke@0 2984 // We need to re-resolve the java_thread, since a GC might have happened during the
duke@0 2985 // acquire of the lock
duke@0 2986 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
duke@0 2987
duke@0 2988 if (thr == NULL) {
duke@0 2989 // do nothing
duke@0 2990 } else if(! thr->is_external_suspend() || ! thr->frame_anchor()->walkable()) {
duke@0 2991 // Check whether this java thread has been suspended already. If not, throws
duke@0 2992 // IllegalThreadStateException. We defer to throw that exception until
duke@0 2993 // Threads_lock is released since loading exception class has to leave VM.
duke@0 2994 // The correct way to test a thread is actually suspended is
duke@0 2995 // wait_for_ext_suspend_completion(), but we can't call that while holding
duke@0 2996 // the Threads_lock. The above tests are sufficient for our purposes
duke@0 2997 // provided the walkability of the stack is stable - which it isn't
duke@0 2998 // 100% but close enough for most practical purposes.
duke@0 2999 throw_illegal_thread_state = true;
duke@0 3000 } else {
duke@0 3001 // Count all java activation, i.e., number of vframes
duke@0 3002 for(vframeStream vfst(thr); !vfst.at_end(); vfst.next()) {
duke@0 3003 // Native frames are not counted
duke@0 3004 if (!vfst.method()->is_native()) count++;
duke@0 3005 }
duke@0 3006 }
duke@0 3007 }
duke@0 3008
duke@0 3009 if (throw_illegal_thread_state) {
duke@0 3010 THROW_MSG_0(vmSymbols::java_lang_IllegalThreadStateException(),
duke@0 3011 "this thread is not suspended");
duke@0 3012 }
duke@0 3013 return count;
duke@0 3014 JVM_END
duke@0 3015
duke@0 3016 // Consider: A better way to implement JVM_Interrupt() is to acquire
duke@0 3017 // Threads_lock to resolve the jthread into a Thread pointer, fetch
duke@0 3018 // Thread->platformevent, Thread->native_thr, Thread->parker, etc.,
duke@0 3019 // drop Threads_lock, and the perform the unpark() and thr_kill() operations
duke@0 3020 // outside the critical section. Threads_lock is hot so we want to minimize
duke@0 3021 // the hold-time. A cleaner interface would be to decompose interrupt into
duke@0 3022 // two steps. The 1st phase, performed under Threads_lock, would return
duke@0 3023 // a closure that'd be invoked after Threads_lock was dropped.
duke@0 3024 // This tactic is safe as PlatformEvent and Parkers are type-stable (TSM) and
duke@0 3025 // admit spurious wakeups.
duke@0 3026
duke@0 3027 JVM_ENTRY(void, JVM_Interrupt(JNIEnv* env, jobject jthread))
duke@0 3028 JVMWrapper("JVM_Interrupt");
duke@0 3029
duke@0 3030 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
duke@0 3031 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 3032 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
duke@0 3033 // We need to re-resolve the java_thread, since a GC might have happened during the
duke@0 3034 // acquire of the lock
duke@0 3035 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
duke@0 3036 if (thr != NULL) {
duke@0 3037 Thread::interrupt(thr);
duke@0 3038 }
duke@0 3039 JVM_END
duke@0 3040
duke@0 3041
duke@0 3042 JVM_QUICK_ENTRY(jboolean, JVM_IsInterrupted(JNIEnv* env, jobject jthread, jboolean clear_interrupted))
duke@0 3043 JVMWrapper("JVM_IsInterrupted");
duke@0 3044
duke@0 3045 // Ensure that the C++ Thread and OSThread structures aren't freed before we operate
duke@0 3046 oop java_thread = JNIHandles::resolve_non_null(jthread);
duke@0 3047 MutexLockerEx ml(thread->threadObj() == java_thread ? NULL : Threads_lock);
duke@0 3048 // We need to re-resolve the java_thread, since a GC might have happened during the
duke@0 3049 // acquire of the lock
duke@0 3050 JavaThread* thr = java_lang_Thread::thread(JNIHandles::resolve_non_null(jthread));
duke@0 3051 if (thr == NULL) {
duke@0 3052 return JNI_FALSE;
duke@0 3053 } else {
duke@0 3054 return (jboolean) Thread::is_interrupted(thr, clear_interrupted != 0);
duke@0 3055 }
duke@0 3056 JVM_END
duke@0 3057
duke@0 3058
duke@0 3059 // Return true iff the current thread has locked the object passed in
duke@0 3060
duke@0 3061 JVM_ENTRY(jboolean, JVM_HoldsLock(JNIEnv* env, jclass threadClass, jobject obj))
duke@0 3062 JVMWrapper("JVM_HoldsLock");
duke@0 3063 assert(THREAD->is_Java_thread(), "sanity check");
duke@0 3064 if (obj == NULL) {
duke@0 3065 THROW_(vmSymbols::java_lang_NullPointerException(), JNI_FALSE);
duke@0 3066 }
duke@0 3067 Handle h_obj(THREAD, JNIHandles::resolve(obj));
duke@0 3068 return ObjectSynchronizer::current_thread_holds_lock((JavaThread*)THREAD, h_obj);
duke@0 3069 JVM_END
duke@0 3070
duke@0 3071
duke@0 3072 JVM_ENTRY(void, JVM_DumpAllStacks(JNIEnv* env, jclass))
duke@0 3073 JVMWrapper("JVM_DumpAllStacks");
duke@0 3074 VM_PrintThreads op;
duke@0 3075 VMThread::execute(&op);
duke@0 3076 if (JvmtiExport::should_post_data_dump()) {
duke@0 3077 JvmtiExport::post_data_dump();
duke@0 3078 }
duke@0 3079 JVM_END
duke@0 3080
dcubed@2767 3081 JVM_ENTRY(void, JVM_SetNativeThreadName(JNIEnv* env, jobject jthread, jstring name))
dcubed@2767 3082 JVMWrapper("JVM_SetNativeThreadName");
dcubed@2767 3083 ResourceMark rm(THREAD);
dcubed@2767 3084 oop java_thread = JNIHandles::resolve_non_null(jthread);
dcubed@2767 3085 JavaThread* thr = java_lang_Thread::thread(java_thread);
dcubed@2767 3086 // Thread naming only supported for the current thread, doesn't work for
dcubed@2767 3087 // target threads.
dcubed@2767 3088 if (Thread::current() == thr && !thr->has_attached_via_jni()) {
dcubed@2767 3089 // we don't set the name of an attached thread to avoid stepping
dcubed@2767 3090 // on other programs
dcubed@2767 3091 const char *thread_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
dcubed@2767 3092 os::set_native_thread_name(thread_name);
dcubed@2767 3093 }
dcubed@2767 3094 JVM_END
duke@0 3095
duke@0 3096 // java.lang.SecurityManager ///////////////////////////////////////////////////////////////////////
duke@0 3097
duke@0 3098 static bool is_trusted_frame(JavaThread* jthread, vframeStream* vfst) {
duke@0 3099 assert(jthread->is_Java_thread(), "must be a Java thread");
duke@0 3100 if (jthread->privileged_stack_top() == NULL) return false;
duke@0 3101 if (jthread->privileged_stack_top()->frame_id() == vfst->frame_id()) {
duke@0 3102 oop loader = jthread->privileged_stack_top()->class_loader();
duke@0 3103 if (loader == NULL) return true;
duke@0 3104 bool trusted = java_lang_ClassLoader::is_trusted_loader(loader);
duke@0 3105 if (trusted) return true;
duke@0 3106 }
duke@0 3107 return false;
duke@0 3108 }
duke@0 3109
duke@0 3110 JVM_ENTRY(jclass, JVM_CurrentLoadedClass(JNIEnv *env))
duke@0 3111 JVMWrapper("JVM_CurrentLoadedClass");
duke@0 3112 ResourceMark rm(THREAD);
duke@0 3113
duke@0 3114 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
duke@0 3115 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
duke@0 3116 bool trusted = is_trusted_frame(thread, &vfst);
duke@0 3117 if (trusted) return NULL;
duke@0 3118
coleenp@3602 3119 Method* m = vfst.method();
duke@0 3120 if (!m->is_native()) {
coleenp@3816 3121 InstanceKlass* holder = m->method_holder();
coleenp@3816 3122 oop loader = holder->class_loader();
duke@0 3123 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
coleenp@3816 3124 return (jclass) JNIHandles::make_local(env, holder->java_mirror());
duke@0 3125 }
duke@0 3126 }
duke@0 3127 }
duke@0 3128 return NULL;
duke@0 3129 JVM_END
duke@0 3130
duke@0 3131
duke@0 3132 JVM_ENTRY(jobject, JVM_CurrentClassLoader(JNIEnv *env))
duke@0 3133 JVMWrapper("JVM_CurrentClassLoader");
duke@0 3134 ResourceMark rm(THREAD);
duke@0 3135
duke@0 3136 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
duke@0 3137
duke@0 3138 // if a method in a class in a trusted loader is in a doPrivileged, return NULL
duke@0 3139 bool trusted = is_trusted_frame(thread, &vfst);
duke@0 3140 if (trusted) return NULL;
duke@0 3141
coleenp@3602 3142 Method* m = vfst.method();
duke@0 3143 if (!m->is_native()) {
coleenp@3816 3144 InstanceKlass* holder = m->method_holder();
duke@0 3145 assert(holder->is_klass(), "just checking");
coleenp@3816 3146 oop loader = holder->class_loader();
duke@0 3147 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
duke@0 3148 return JNIHandles::make_local(env, loader);
duke@0 3149 }
duke@0 3150 }
duke@0 3151 }
duke@0 3152 return NULL;
duke@0 3153 JVM_END
duke@0 3154
duke@0 3155
duke@0 3156 JVM_ENTRY(jobjectArray, JVM_GetClassContext(JNIEnv *env))
duke@0 3157 JVMWrapper("JVM_GetClassContext");
duke@0 3158 ResourceMark rm(THREAD);
duke@0 3159 JvmtiVMObjectAllocEventCollector oam;
twisti@4431 3160 vframeStream vfst(thread);
twisti@4431 3161
twisti@4431 3162 if (SystemDictionary::reflect_CallerSensitive_klass() != NULL) {
twisti@4431 3163 // This must only be called from SecurityManager.getClassContext
twisti@4431 3164 Method* m = vfst.method();
twisti@4431 3165 if (!(m->method_holder() == SystemDictionary::SecurityManager_klass() &&
twisti@4431 3166 m->name() == vmSymbols::getClassContext_name() &&
twisti@4431 3167 m->signature() == vmSymbols::void_class_array_signature())) {
twisti@4431 3168 THROW_MSG_NULL(vmSymbols::java_lang_InternalError(), "JVM_GetClassContext must only be called from SecurityManager.getClassContext");
twisti@4431 3169 }
twisti@4431 3170 }
twisti@4431 3171
twisti@4431 3172 // Collect method holders
drchase@4852 3173 GrowableArray<KlassHandle>* klass_array = new GrowableArray<KlassHandle>();
twisti@4431 3174 for (; !vfst.at_end(); vfst.security_next()) {
twisti@4431 3175 Method* m = vfst.method();
duke@0 3176 // Native frames are not returned
twisti@4431 3177 if (!m->is_ignored_by_security_stack_walk() && !m->is_native()) {
twisti@4431 3178 Klass* holder = m->method_holder();
duke@0 3179 assert(holder->is_klass(), "just checking");
drchase@4852 3180 klass_array->append(holder);
duke@0 3181 }
duke@0 3182 }
duke@0 3183
duke@0 3184 // Create result array of type [Ljava/lang/Class;
drchase@4852 3185 objArrayOop result = oopFactory::new_objArray(SystemDictionary::Class_klass(), klass_array->length(), CHECK_NULL);
duke@0 3186 // Fill in mirrors corresponding to method holders
drchase@4852 3187 for (int i = 0; i < klass_array->length(); i++) {
drchase@4852 3188 result->obj_at_put(i, klass_array->at(i)->java_mirror());
duke@0 3189 }
duke@0 3190
duke@0 3191 return (jobjectArray) JNIHandles::make_local(env, result);
duke@0 3192 JVM_END
duke@0 3193
duke@0 3194
duke@0 3195 JVM_ENTRY(jint, JVM_ClassDepth(JNIEnv *env, jstring name))
duke@0 3196 JVMWrapper("JVM_ClassDepth");
duke@0 3197 ResourceMark rm(THREAD);
duke@0 3198 Handle h_name (THREAD, JNIHandles::resolve_non_null(name));
duke@0 3199 Handle class_name_str = java_lang_String::internalize_classname(h_name, CHECK_0);
duke@0 3200
duke@0 3201 const char* str = java_lang_String::as_utf8_string(class_name_str());
coleenp@2062 3202 TempNewSymbol class_name_sym = SymbolTable::probe(str, (int)strlen(str));
coleenp@2062 3203 if (class_name_sym == NULL) {
duke@0 3204 return -1;
duke@0 3205 }
duke@0 3206
duke@0 3207 int depth = 0;
duke@0 3208
duke@0 3209 for(vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
duke@0 3210 if (!vfst.method()->is_native()) {
coleenp@3816 3211 InstanceKlass* holder = vfst.method()->method_holder();
duke@0 3212 assert(holder->is_klass(), "just checking");
coleenp@3816 3213 if (holder->name() == class_name_sym) {
duke@0 3214 return depth;
duke@0 3215 }
duke@0 3216 depth++;
duke@0 3217 }
duke@0 3218 }
duke@0 3219 return -1;
duke@0 3220 JVM_END
duke@0 3221
duke@0 3222
duke@0 3223 JVM_ENTRY(jint, JVM_ClassLoaderDepth(JNIEnv *env))
duke@0 3224 JVMWrapper("JVM_ClassLoaderDepth");
duke@0 3225 ResourceMark rm(THREAD);
duke@0 3226 int depth = 0;
duke@0 3227 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
duke@0 3228 // if a method in a class in a trusted loader is in a doPrivileged, return -1
duke@0 3229 bool trusted = is_trusted_frame(thread, &vfst);
duke@0 3230 if (trusted) return -1;
duke@0 3231
coleenp@3602 3232 Method* m = vfst.method();
duke@0 3233 if (!m->is_native()) {
coleenp@3816 3234 InstanceKlass* holder = m->method_holder();
duke@0 3235 assert(holder->is_klass(), "just checking");
coleenp@3816 3236 oop loader = holder->class_loader();
duke@0 3237 if (loader != NULL && !java_lang_ClassLoader::is_trusted_loader(loader)) {
duke@0 3238 return depth;
duke@0 3239 }
duke@0 3240 depth++;
duke@0 3241 }
duke@0 3242 }
duke@0 3243 return -1;
duke@0 3244 JVM_END
duke@0 3245
duke@0 3246
duke@0 3247 // java.lang.Package ////////////////////////////////////////////////////////////////
duke@0 3248
duke@0 3249
duke@0 3250 JVM_ENTRY(jstring, JVM_GetSystemPackage(JNIEnv *env, jstring name))
duke@0 3251 JVMWrapper("JVM_GetSystemPackage");
duke@0 3252 ResourceMark rm(THREAD);
duke@0 3253 JvmtiVMObjectAllocEventCollector oam;
duke@0 3254 char* str = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name));
duke@0 3255 oop result = ClassLoader::get_system_package(str, CHECK_NULL);
duke@0 3256 return (jstring) JNIHandles::make_local(result);
duke@0 3257 JVM_END
duke@0 3258
duke@0 3259
duke@0 3260 JVM_ENTRY(jobjectArray, JVM_GetSystemPackages(JNIEnv *env))
duke@0 3261 JVMWrapper("JVM_GetSystemPackages");
duke@0 3262 JvmtiVMObjectAllocEventCollector oam;
duke@0 3263 objArrayOop result = ClassLoader::get_system_packages(CHECK_NULL);
duke@0 3264 return (jobjectArray) JNIHandles::make_local(result);
duke@0 3265 JVM_END
duke@0 3266
duke@0 3267
duke@0 3268 // ObjectInputStream ///////////////////////////////////////////////////////////////
duke@0 3269
coleenp@3602 3270 bool force_verify_field_access(Klass* current_class, Klass* field_class, AccessFlags access, bool classloader_only) {
duke@0 3271 if (current_class == NULL) {
duke@0 3272 return true;
duke@0 3273 }
duke@0 3274 if ((current_class == field_class) || access.is_public()) {
duke@0 3275 return true;
duke@0 3276 }
duke@0 3277
duke@0 3278 if (access.is_protected()) {
duke@0 3279 // See if current_class is a subclass of field_class
hseigel@3843 3280 if (current_class->is_subclass_of(field_class)) {
duke@0 3281 return true;
duke@0 3282 }
duke@0 3283 }
duke@0 3284
coleenp@3602 3285 return (!access.is_private() && InstanceKlass::cast(current_class)->is_same_class_package(field_class));
duke@0 3286 }
duke@0 3287
duke@0 3288 // Return the first non-null class loader up the execution stack, or null
duke@0 3289 // if only code from the null class loader is on the stack.
duke@0 3290
duke@0 3291 JVM_ENTRY(jobject, JVM_LatestUserDefinedLoader(JNIEnv *env))
duke@0 3292 for (vframeStream vfst(thread); !vfst.at_end(); vfst.next()) {
duke@0 3293 vfst.skip_reflection_related_frames(); // Only needed for 1.4 reflection
coleenp@3816 3294 oop loader = vfst.method()->method_holder()->class_loader();
duke@0 3295 if (loader != NULL) {
duke@0 3296 return JNIHandles::make_local(env, loader);
duke@0 3297 }
duke@0 3298 }
duke@0 3299 return NULL;
duke@0 3300 JVM_END
duke@0 3301
duke@0 3302
duke@0 3303 // Array ///////////////////////////////////////////////////////////////////////////////////////////
duke@0 3304
duke@0 3305
duke@0 3306 // resolve array handle and check arguments
duke@0 3307 static inline arrayOop check_array(JNIEnv *env, jobject arr, bool type_array_only, TRAPS) {
duke@0 3308 if (arr == NULL) {
duke@0 3309 THROW_0(vmSymbols::java_lang_NullPointerException());
duke@0 3310 }
duke@0 3311 oop a = JNIHandles::resolve_non_null(arr);
cjplummer@7265 3312 if (!a->is_array()) {
duke@0 3313 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array");
cjplummer@7265 3314 } else if (type_array_only && !a->is_typeArray()) {
cjplummer@7265 3315 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Argument is not an array of primitive type");
duke@0 3316 }
duke@0 3317 return arrayOop(a);
duke@0 3318 }
duke@0 3319
duke@0 3320
duke@0 3321 JVM_ENTRY(jint, JVM_GetArrayLength(JNIEnv *env, jobject arr))
duke@0 3322 JVMWrapper("JVM_GetArrayLength");
duke@0 3323 arrayOop a = check_array(env, arr, false, CHECK_0);
duke@0 3324 return a->length();
duke@0 3325 JVM_END
duke@0 3326
duke@0 3327
duke@0 3328 JVM_ENTRY(jobject, JVM_GetArrayElement(JNIEnv *env, jobject arr, jint index))
duke@0 3329 JVMWrapper("JVM_Array_Get");
duke@0 3330 JvmtiVMObjectAllocEventCollector oam;
duke@0 3331 arrayOop a = check_array(env, arr, false, CHECK_NULL);
duke@0 3332 jvalue value;
duke@0 3333 BasicType type = Reflection::array_get(&value, a, index, CHECK_NULL);
duke@0 3334 oop box = Reflection::box(&value, type, CHECK_NULL);
duke@0 3335 return JNIHandles::make_local(env, box);
duke@0 3336 JVM_END
duke@0 3337
duke@0 3338
duke@0 3339 JVM_ENTRY(jvalue, JVM_GetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jint wCode))
duke@0 3340 JVMWrapper("JVM_GetPrimitiveArrayElement");
duke@0 3341 jvalue value;
duke@0 3342 value.i = 0; // to initialize value before getting used in CHECK
duke@0 3343 arrayOop a = check_array(env, arr, true, CHECK_(value));
duke@0 3344 assert(a->is_typeArray(), "just checking");
duke@0 3345 BasicType type = Reflection::array_get(&value, a, index, CHECK_(value));
duke@0 3346 BasicType wide_type = (BasicType) wCode;
duke@0 3347 if (type != wide_type) {
duke@0 3348 Reflection::widen(&value, type, wide_type, CHECK_(value));
duke@0 3349 }
duke@0 3350 return value;
duke@0 3351 JVM_END
duke@0 3352
duke@0 3353
duke@0 3354 JVM_ENTRY(void, JVM_SetArrayElement(JNIEnv *env, jobject arr, jint index, jobject val))
duke@0 3355 JVMWrapper("JVM_SetArrayElement");
duke@0 3356 arrayOop a = check_array(env, arr, false, CHECK);
duke@0 3357 oop box = JNIHandles::resolve(val);
duke@0 3358 jvalue value;
duke@0 3359 value.i = 0; // to initialize value before getting used in CHECK
duke@0 3360 BasicType value_type;
duke@0 3361 if (a->is_objArray()) {
duke@0 3362 // Make sure we do no unbox e.g. java/lang/Integer instances when storing into an object array
duke@0 3363 value_type = Reflection::unbox_for_regular_object(box, &value);
duke@0 3364 } else {
duke@0 3365 value_type = Reflection::unbox_for_primitive(box, &value, CHECK);
duke@0 3366 }
duke@0 3367 Reflection::array_set(&value, a, index, value_type, CHECK);
duke@0 3368 JVM_END
duke@0 3369
duke@0 3370
duke@0 3371 JVM_ENTRY(void, JVM_SetPrimitiveArrayElement(JNIEnv *env, jobject arr, jint index, jvalue v, unsigned char vCode))
duke@0 3372 JVMWrapper("JVM_SetPrimitiveArrayElement");
duke@0 3373 arrayOop a = check_array(env, arr, true, CHECK);
duke@0 3374 assert(a->is_typeArray(), "just checking");
duke@0 3375 BasicType value_type = (BasicType) vCode;
duke@0 3376 Reflection::array_set(&v, a, index, value_type, CHECK);
duke@0 3377 JVM_END
duke@0 3378
duke@0 3379
duke@0 3380 JVM_ENTRY(jobject, JVM_NewArray(JNIEnv *env, jclass eltClass, jint length))
duke@0 3381 JVMWrapper("JVM_NewArray");
duke@0 3382 JvmtiVMObjectAllocEventCollector oam;
duke@0 3383 oop element_mirror = JNIHandles::resolve(eltClass);
duke@0 3384 oop result = Reflection::reflect_new_array(element_mirror, length, CHECK_NULL);
duke@0 3385 return JNIHandles::make_local(env, result);
duke@0 3386 JVM_END
duke@0 3387
duke@0 3388
duke@0 3389 JVM_ENTRY(jobject, JVM_NewMultiArray(JNIEnv *env, jclass eltClass, jintArray dim))
duke@0 3390 JVMWrapper("JVM_NewMultiArray");
duke@0 3391 JvmtiVMObjectAllocEventCollector oam;
duke@0 3392 arrayOop dim_array = check_array(env, dim, true, CHECK_NULL);
duke@0 3393 oop element_mirror = JNIHandles::resolve(eltClass);
duke@0 3394 assert(dim_array->is_typeArray(), "just checking");
duke@0 3395 oop result = Reflection::reflect_new_multi_array(element_mirror, typeArrayOop(dim_array), CHECK_NULL);
duke@0 3396 return JNIHandles::make_local(env, result);
duke@0 3397 JVM_END
duke@0 3398
duke@0 3399
duke@0 3400 // Library support ///////////////////////////////////////////////////////////////////////////
duke@0 3401
duke@0 3402 JVM_ENTRY_NO_ENV(void*, JVM_LoadLibrary(const char* name))
duke@0 3403 //%note jvm_ct
duke@0 3404 JVMWrapper2("JVM_LoadLibrary (%s)", name);
duke@0 3405 char ebuf[1024];
duke@0 3406 void *load_result;
duke@0 3407 {
duke@0 3408 ThreadToNativeFromVM ttnfvm(thread);
ikrylov@1887 3409 load_result = os::dll_load(name, ebuf, sizeof ebuf);
duke@0 3410 }
duke@0 3411 if (load_result == NULL) {
duke@0 3412 char msg[1024];
duke@0 3413 jio_snprintf(msg, sizeof msg, "%s: %s", name, ebuf);
duke@0 3414 // Since 'ebuf' may contain a string encoded using
duke@0 3415 // platform encoding scheme, we need to pass
duke@0 3416 // Exceptions::unsafe_to_utf8 to the new_exception method
duke@0 3417 // as the last argument. See bug 6367357.
duke@0 3418 Handle h_exception =
duke@0 3419 Exceptions::new_exception(thread,
duke@0 3420 vmSymbols::java_lang_UnsatisfiedLinkError(),
duke@0 3421 msg, Exceptions::unsafe_to_utf8);
duke@0 3422
duke@0 3423 THROW_HANDLE_0(h_exception);
duke@0 3424 }
duke@0 3425 return load_result;
duke@0 3426 JVM_END
duke@0 3427
duke@0 3428
duke@0 3429 JVM_LEAF(void, JVM_UnloadLibrary(void* handle))
duke@0 3430 JVMWrapper("JVM_UnloadLibrary");
ikrylov@1887 3431 os::dll_unload(handle);
duke@0 3432 JVM_END
duke@0 3433
duke@0 3434
duke@0 3435 JVM_LEAF(void*, JVM_FindLibraryEntry(void* handle, const char* name))
duke@0 3436 JVMWrapper2("JVM_FindLibraryEntry (%s)", name);
ikrylov@1887 3437 return os::dll_lookup(handle, name);
duke@0 3438 JVM_END
duke@0 3439
phh@2909 3440
duke@0 3441 // JNI version ///////////////////////////////////////////////////////////////////////////////
duke@0 3442
duke@0 3443 JVM_LEAF(jboolean, JVM_IsSupportedJNIVersion(jint version))
duke@0 3444 JVMWrapper2("JVM_IsSupportedJNIVersion (%d)", version);
duke@0 3445 return Threads::is_supported_jni_version_including_1_1(version);
duke@0 3446 JVM_END
duke@0 3447
duke@0 3448
duke@0 3449 // String support ///////////////////////////////////////////////////////////////////////////
duke@0 3450
duke@0 3451 JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
duke@0 3452 JVMWrapper("JVM_InternString");
duke@0 3453 JvmtiVMObjectAllocEventCollector oam;
duke@0 3454 if (str == NULL) return NULL;
duke@0 3455 oop string = JNIHandles::resolve_non_null(str);
duke@0 3456 oop result = StringTable::intern(string, CHECK_NULL);
duke@0 3457 return (jstring) JNIHandles::make_local(env, result);
duke@0 3458 JVM_END
duke@0 3459
duke@0 3460
duke@0 3461 // Raw monitor support //////////////////////////////////////////////////////////////////////
duke@0 3462
duke@0 3463 // The lock routine below calls lock_without_safepoint_check in order to get a raw lock
duke@0 3464 // without interfering with the safepoint mechanism. The routines are not JVM_LEAF because
duke@0 3465 // they might be called by non-java threads. The JVM_LEAF installs a NoHandleMark check
duke@0 3466 // that only works with java threads.
duke@0 3467
duke@0 3468
duke@0 3469 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void) {
duke@0 3470 VM_Exit::block_if_vm_exited();
duke@0 3471 JVMWrapper("JVM_RawMonitorCreate");
duke@0 3472 return new Mutex(Mutex::native, "JVM_RawMonitorCreate");
duke@0 3473 }
duke@0 3474
duke@0 3475
duke@0 3476 JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon) {
duke@0 3477 VM_Exit::block_if_vm_exited();
duke@0 3478 JVMWrapper("JVM_RawMonitorDestroy");
duke@0 3479 delete ((Mutex*) mon);
duke@0 3480 }
duke@0 3481
duke@0 3482
duke@0 3483 JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon) {
duke@0 3484 VM_Exit::block_if_vm_exited();
duke@0 3485 JVMWrapper("JVM_RawMonitorEnter");
duke@0 3486 ((Mutex*) mon)->jvm_raw_lock();
duke@0 3487 return 0;
duke@0 3488 }
duke@0 3489
duke@0 3490
duke@0 3491 JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon) {
duke@0 3492 VM_Exit::block_if_vm_exited();
duke@0 3493 JVMWrapper("JVM_RawMonitorExit");
duke@0 3494 ((Mutex*) mon)->jvm_raw_unlock();
duke@0 3495 }
duke@0 3496
duke@0 3497
duke@0 3498 // Shared JNI/JVM entry points //////////////////////////////////////////////////////////////
duke@0 3499
coleenp@7206 3500 jclass find_class_from_class_loader(JNIEnv* env, Symbol* name, jboolean init,
coleenp@7206 3501 Handle loader, Handle protection_domain,
coleenp@7206 3502 jboolean throwError, TRAPS) {
duke@0 3503 // Security Note:
duke@0 3504 // The Java level wrapper will perform the necessary security check allowing
coleenp@7206 3505 // us to pass the NULL as the initiating class loader. The VM is responsible for
coleenp@7206 3506 // the checkPackageAccess relative to the initiating class loader via the
coleenp@7206 3507 // protection_domain. The protection_domain is passed as NULL by the java code
coleenp@7206 3508 // if there is no security manager in 3-arg Class.forName().
coleenp@3602 3509 Klass* klass = SystemDictionary::resolve_or_fail(name, loader, protection_domain, throwError != 0, CHECK_NULL);
mchung@878 3510
duke@0 3511 KlassHandle klass_handle(THREAD, klass);
duke@0 3512 // Check if we should initialize the class
duke@0 3513 if (init && klass_handle->oop_is_instance()) {
duke@0 3514 klass_handle->initialize(CHECK_NULL);
duke@0 3515 }
duke@0 3516 return (jclass) JNIHandles::make_local(env, klass_handle->java_mirror());
duke@0 3517 }
duke@0 3518
duke@0 3519
duke@0 3520 // Method ///////////////////////////////////////////////////////////////////////////////////////////
duke@0 3521
duke@0 3522 JVM_ENTRY(jobject, JVM_InvokeMethod(JNIEnv *env, jobject method, jobject obj, jobjectArray args0))
duke@0 3523 JVMWrapper("JVM_InvokeMethod");
duke@0 3524 Handle method_handle;
duke@0 3525 if (thread->stack_available((address) &method_handle) >= JVMInvokeMethodSlack) {
duke@0 3526 method_handle = Handle(THREAD, JNIHandles::resolve(method));
duke@0 3527 Handle receiver(THREAD, JNIHandles::resolve(obj));
duke@0 3528 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
duke@0 3529 oop result = Reflection::invoke_method(method_handle(), receiver, args, CHECK_NULL);
duke@0 3530 jobject res = JNIHandles::make_local(env, result);
duke@0 3531 if (JvmtiExport::should_post_vm_object_alloc()) {
duke@0 3532 oop ret_type = java_lang_reflect_Method::return_type(method_handle());
duke@0 3533 assert(ret_type != NULL, "sanity check: ret_type oop must not be NULL!");
duke@0 3534 if (java_lang_Class::is_primitive(ret_type)) {
duke@0 3535 // Only for primitive type vm allocates memory for java object.
duke@0 3536 // See box() method.
duke@0 3537 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
duke@0 3538 }
duke@0 3539 }
duke@0 3540 return res;
duke@0 3541 } else {
duke@0 3542 THROW_0(vmSymbols::java_lang_StackOverflowError());
duke@0 3543 }
duke@0 3544 JVM_END
duke@0 3545
duke@0 3546
duke@0 3547 JVM_ENTRY(jobject, JVM_NewInstanceFromConstructor(JNIEnv *env, jobject c, jobjectArray args0))
duke@0 3548 JVMWrapper("JVM_NewInstanceFromConstructor");
duke@0 3549 oop constructor_mirror = JNIHandles::resolve(c);
duke@0 3550 objArrayHandle args(THREAD, objArrayOop(JNIHandles::resolve(args0)));
duke@0 3551 oop result = Reflection::invoke_constructor(constructor_mirror, args, CHECK_NULL);
duke@0 3552 jobject res = JNIHandles::make_local(env, result);
duke@0 3553 if (JvmtiExport::should_post_vm_object_alloc()) {
duke@0 3554 JvmtiExport::post_vm_object_alloc(JavaThread::current(), result);
duke@0 3555 }
duke@0 3556 return res;
duke@0 3557 JVM_END
duke@0 3558
duke@0 3559 // Atomic ///////////////////////////////////////////////////////////////////////////////////////////
duke@0 3560
duke@0 3561 JVM_LEAF(jboolean, JVM_SupportsCX8())
duke@0 3562 JVMWrapper("JVM_SupportsCX8");
duke@0 3563 return VM_Version::supports_cx8();
duke@0 3564 JVM_END
duke@0 3565
kamg@116 3566 // DTrace ///////////////////////////////////////////////////////////////////
kamg@116 3567
kamg@116 3568 JVM_ENTRY(jint, JVM_DTraceGetVersion(JNIEnv* env))
kamg@116 3569 JVMWrapper("JVM_DTraceGetVersion");
kamg@116 3570 return (jint)JVM_TRACING_DTRACE_VERSION;
kamg@116 3571 JVM_END
kamg@116 3572
kamg@116 3573 JVM_ENTRY(jlong,JVM_DTraceActivate(
kamg@116 3574 JNIEnv* env, jint version, jstring module_name, jint providers_count,
kamg@116 3575 JVM_DTraceProvider* providers))
kamg@116 3576 JVMWrapper("JVM_DTraceActivate");
kamg@116 3577 return DTraceJSDT::activate(
stefank@7418 3578 version, module_name, providers_count, providers, THREAD);
kamg@116 3579 JVM_END
kamg@116 3580
kamg@116 3581 JVM_ENTRY(jboolean,JVM_DTraceIsProbeEnabled(JNIEnv* env, jmethodID method))
kamg@116 3582 JVMWrapper("JVM_DTraceIsProbeEnabled");
kamg@116 3583 return DTraceJSDT::is_probe_enabled(method);
kamg@116 3584 JVM_END
kamg@116 3585
kamg@116 3586 JVM_ENTRY(void,JVM_DTraceDispose(JNIEnv* env, jlong handle))
kamg@116 3587 JVMWrapper("JVM_DTraceDispose");
kamg@116 3588 DTraceJSDT::dispose(handle);
kamg@116 3589 JVM_END
kamg@116 3590
kamg@116 3591 JVM_ENTRY(jboolean,JVM_DTraceIsSupported(JNIEnv* env))
kamg@116 3592 JVMWrapper("JVM_DTraceIsSupported");
kamg@116 3593 return DTraceJSDT::is_supported();
kamg@116 3594 JVM_END
kamg@116 3595
duke@0 3596 // Returns an array of all live Thread objects (VM internal JavaThreads,
duke@0 3597 // jvmti agent threads, and JNI attaching threads are skipped)
duke@0 3598 // See CR 6404306 regarding JNI attaching threads
duke@0 3599 JVM_ENTRY(jobjectArray, JVM_GetAllThreads(JNIEnv *env, jclass dummy))
duke@0 3600 ResourceMark rm(THREAD);
duke@0 3601 ThreadsListEnumerator tle(THREAD, false, false);
duke@0 3602 JvmtiVMObjectAllocEventCollector oam;
duke@0 3603
duke@0 3604 int num_threads = tle.num_threads();
never@1142 3605 objArrayOop r = oopFactory::new_objArray(SystemDictionary::Thread_klass(), num_threads, CHECK_NULL);
duke@0 3606<