src/share/vm/prims/jvm.cpp

Tue, 11 Mar 2014 15:06:34 +0400

author
vlivanov
date
Tue, 11 Mar 2014 15:06:34 +0400
changeset 7179
7301840ea20e
parent 7089
6e0cb14ce59b
child 7241
8cb56c8cb30d
permissions
-rw-r--r--

8023461: Thread holding lock at safepoint that vm can block on: MethodCompileQueue_lock
Reviewed-by: kvn, iveresov

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

mercurial