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