annotate src/share/vm/prims/jvm.cpp @ 7505:10ad4b9d79f9

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