|
1 /* |
|
2 * Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 #include "precompiled.hpp" |
|
26 #include "classfile/classFileStream.hpp" |
|
27 #include "classfile/vmSymbols.hpp" |
|
28 #include "memory/allocation.inline.hpp" |
|
29 #include "memory/resourceArea.hpp" |
|
30 #include "oops/fieldStreams.hpp" |
|
31 #include "oops/objArrayOop.inline.hpp" |
|
32 #include "oops/oop.inline.hpp" |
|
33 #include "prims/jni.h" |
|
34 #include "prims/jvm.h" |
|
35 #include "prims/unsafe.hpp" |
|
36 #include "runtime/atomic.hpp" |
|
37 #include "runtime/globals.hpp" |
|
38 #include "runtime/interfaceSupport.hpp" |
|
39 #include "runtime/orderAccess.inline.hpp" |
|
40 #include "runtime/reflection.hpp" |
|
41 #include "runtime/vm_version.hpp" |
|
42 #include "services/threadService.hpp" |
|
43 #include "trace/tracing.hpp" |
|
44 #include "utilities/align.hpp" |
|
45 #include "utilities/copy.hpp" |
|
46 #include "utilities/dtrace.hpp" |
|
47 #include "utilities/macros.hpp" |
|
48 #if INCLUDE_ALL_GCS |
|
49 #include "gc/g1/g1SATBCardTableModRefBS.hpp" |
|
50 #endif // INCLUDE_ALL_GCS |
|
51 |
|
52 /** |
|
53 * Implementation of the jdk.internal.misc.Unsafe class |
|
54 */ |
|
55 |
|
56 |
|
57 #define MAX_OBJECT_SIZE \ |
|
58 ( arrayOopDesc::header_size(T_DOUBLE) * HeapWordSize \ |
|
59 + ((julong)max_jint * sizeof(double)) ) |
|
60 |
|
61 |
|
62 #define UNSAFE_ENTRY(result_type, header) \ |
|
63 JVM_ENTRY(static result_type, header) |
|
64 |
|
65 #define UNSAFE_LEAF(result_type, header) \ |
|
66 JVM_LEAF(static result_type, header) |
|
67 |
|
68 #define UNSAFE_END JVM_END |
|
69 |
|
70 |
|
71 static inline void* addr_from_java(jlong addr) { |
|
72 // This assert fails in a variety of ways on 32-bit systems. |
|
73 // It is impossible to predict whether native code that converts |
|
74 // pointers to longs will sign-extend or zero-extend the addresses. |
|
75 //assert(addr == (uintptr_t)addr, "must not be odd high bits"); |
|
76 return (void*)(uintptr_t)addr; |
|
77 } |
|
78 |
|
79 static inline jlong addr_to_java(void* p) { |
|
80 assert(p == (void*)(uintptr_t)p, "must not be odd high bits"); |
|
81 return (uintptr_t)p; |
|
82 } |
|
83 |
|
84 |
|
85 // Note: The VM's obj_field and related accessors use byte-scaled |
|
86 // ("unscaled") offsets, just as the unsafe methods do. |
|
87 |
|
88 // However, the method Unsafe.fieldOffset explicitly declines to |
|
89 // guarantee this. The field offset values manipulated by the Java user |
|
90 // through the Unsafe API are opaque cookies that just happen to be byte |
|
91 // offsets. We represent this state of affairs by passing the cookies |
|
92 // through conversion functions when going between the VM and the Unsafe API. |
|
93 // The conversion functions just happen to be no-ops at present. |
|
94 |
|
95 static inline jlong field_offset_to_byte_offset(jlong field_offset) { |
|
96 return field_offset; |
|
97 } |
|
98 |
|
99 static inline jlong field_offset_from_byte_offset(jlong byte_offset) { |
|
100 return byte_offset; |
|
101 } |
|
102 |
|
103 static inline void* index_oop_from_field_offset_long(oop p, jlong field_offset) { |
|
104 jlong byte_offset = field_offset_to_byte_offset(field_offset); |
|
105 |
|
106 #ifdef ASSERT |
|
107 if (p != NULL) { |
|
108 assert(byte_offset >= 0 && byte_offset <= (jlong)MAX_OBJECT_SIZE, "sane offset"); |
|
109 if (byte_offset == (jint)byte_offset) { |
|
110 void* ptr_plus_disp = (address)p + byte_offset; |
|
111 assert((void*)p->obj_field_addr<oop>((jint)byte_offset) == ptr_plus_disp, |
|
112 "raw [ptr+disp] must be consistent with oop::field_base"); |
|
113 } |
|
114 jlong p_size = HeapWordSize * (jlong)(p->size()); |
|
115 assert(byte_offset < p_size, "Unsafe access: offset " INT64_FORMAT " > object's size " INT64_FORMAT, (int64_t)byte_offset, (int64_t)p_size); |
|
116 } |
|
117 #endif |
|
118 |
|
119 if (sizeof(char*) == sizeof(jint)) { // (this constant folds!) |
|
120 return (address)p + (jint) byte_offset; |
|
121 } else { |
|
122 return (address)p + byte_offset; |
|
123 } |
|
124 } |
|
125 |
|
126 // Externally callable versions: |
|
127 // (Use these in compiler intrinsics which emulate unsafe primitives.) |
|
128 jlong Unsafe_field_offset_to_byte_offset(jlong field_offset) { |
|
129 return field_offset; |
|
130 } |
|
131 jlong Unsafe_field_offset_from_byte_offset(jlong byte_offset) { |
|
132 return byte_offset; |
|
133 } |
|
134 |
|
135 |
|
136 ///// Data read/writes on the Java heap and in native (off-heap) memory |
|
137 |
|
138 /** |
|
139 * Helper class for accessing memory. |
|
140 * |
|
141 * Normalizes values and wraps accesses in |
|
142 * JavaThread::doing_unsafe_access() if needed. |
|
143 */ |
|
144 class MemoryAccess : StackObj { |
|
145 JavaThread* _thread; |
|
146 jobject _obj; |
|
147 jlong _offset; |
|
148 |
|
149 // Resolves and returns the address of the memory access |
|
150 void* addr() { |
|
151 return index_oop_from_field_offset_long(JNIHandles::resolve(_obj), _offset); |
|
152 } |
|
153 |
|
154 template <typename T> |
|
155 T normalize_for_write(T x) { |
|
156 return x; |
|
157 } |
|
158 |
|
159 jboolean normalize_for_write(jboolean x) { |
|
160 return x & 1; |
|
161 } |
|
162 |
|
163 template <typename T> |
|
164 T normalize_for_read(T x) { |
|
165 return x; |
|
166 } |
|
167 |
|
168 jboolean normalize_for_read(jboolean x) { |
|
169 return x != 0; |
|
170 } |
|
171 |
|
172 /** |
|
173 * Helper class to wrap memory accesses in JavaThread::doing_unsafe_access() |
|
174 */ |
|
175 class GuardUnsafeAccess { |
|
176 JavaThread* _thread; |
|
177 bool _active; |
|
178 |
|
179 public: |
|
180 GuardUnsafeAccess(JavaThread* thread, jobject _obj) : _thread(thread) { |
|
181 if (JNIHandles::resolve(_obj) == NULL) { |
|
182 // native/off-heap access which may raise SIGBUS if accessing |
|
183 // memory mapped file data in a region of the file which has |
|
184 // been truncated and is now invalid |
|
185 _thread->set_doing_unsafe_access(true); |
|
186 _active = true; |
|
187 } else { |
|
188 _active = false; |
|
189 } |
|
190 } |
|
191 |
|
192 ~GuardUnsafeAccess() { |
|
193 if (_active) { |
|
194 _thread->set_doing_unsafe_access(false); |
|
195 } |
|
196 } |
|
197 }; |
|
198 |
|
199 public: |
|
200 MemoryAccess(JavaThread* thread, jobject obj, jlong offset) |
|
201 : _thread(thread), _obj(obj), _offset(offset) { |
|
202 } |
|
203 |
|
204 template <typename T> |
|
205 T get() { |
|
206 GuardUnsafeAccess guard(_thread, _obj); |
|
207 |
|
208 T* p = (T*)addr(); |
|
209 |
|
210 T x = normalize_for_read(*p); |
|
211 |
|
212 return x; |
|
213 } |
|
214 |
|
215 template <typename T> |
|
216 void put(T x) { |
|
217 GuardUnsafeAccess guard(_thread, _obj); |
|
218 |
|
219 T* p = (T*)addr(); |
|
220 |
|
221 *p = normalize_for_write(x); |
|
222 } |
|
223 |
|
224 |
|
225 template <typename T> |
|
226 T get_volatile() { |
|
227 GuardUnsafeAccess guard(_thread, _obj); |
|
228 |
|
229 T* p = (T*)addr(); |
|
230 |
|
231 if (support_IRIW_for_not_multiple_copy_atomic_cpu) { |
|
232 OrderAccess::fence(); |
|
233 } |
|
234 |
|
235 T x = OrderAccess::load_acquire((volatile T*)p); |
|
236 |
|
237 return normalize_for_read(x); |
|
238 } |
|
239 |
|
240 template <typename T> |
|
241 void put_volatile(T x) { |
|
242 GuardUnsafeAccess guard(_thread, _obj); |
|
243 |
|
244 T* p = (T*)addr(); |
|
245 |
|
246 OrderAccess::release_store_fence((volatile T*)p, normalize_for_write(x)); |
|
247 } |
|
248 |
|
249 |
|
250 #ifndef SUPPORTS_NATIVE_CX8 |
|
251 jlong get_jlong_locked() { |
|
252 GuardUnsafeAccess guard(_thread, _obj); |
|
253 |
|
254 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); |
|
255 |
|
256 jlong* p = (jlong*)addr(); |
|
257 |
|
258 jlong x = Atomic::load(p); |
|
259 |
|
260 return x; |
|
261 } |
|
262 |
|
263 void put_jlong_locked(jlong x) { |
|
264 GuardUnsafeAccess guard(_thread, _obj); |
|
265 |
|
266 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); |
|
267 |
|
268 jlong* p = (jlong*)addr(); |
|
269 |
|
270 Atomic::store(normalize_for_write(x), p); |
|
271 } |
|
272 #endif |
|
273 }; |
|
274 |
|
275 // Get/PutObject must be special-cased, since it works with handles. |
|
276 |
|
277 // We could be accessing the referent field in a reference |
|
278 // object. If G1 is enabled then we need to register non-null |
|
279 // referent with the SATB barrier. |
|
280 |
|
281 #if INCLUDE_ALL_GCS |
|
282 static bool is_java_lang_ref_Reference_access(oop o, jlong offset) { |
|
283 if (offset == java_lang_ref_Reference::referent_offset && o != NULL) { |
|
284 Klass* k = o->klass(); |
|
285 if (InstanceKlass::cast(k)->reference_type() != REF_NONE) { |
|
286 assert(InstanceKlass::cast(k)->is_subclass_of(SystemDictionary::Reference_klass()), "sanity"); |
|
287 return true; |
|
288 } |
|
289 } |
|
290 return false; |
|
291 } |
|
292 #endif |
|
293 |
|
294 static void ensure_satb_referent_alive(oop o, jlong offset, oop v) { |
|
295 #if INCLUDE_ALL_GCS |
|
296 if (UseG1GC && v != NULL && is_java_lang_ref_Reference_access(o, offset)) { |
|
297 G1SATBCardTableModRefBS::enqueue(v); |
|
298 } |
|
299 #endif |
|
300 } |
|
301 |
|
302 // These functions allow a null base pointer with an arbitrary address. |
|
303 // But if the base pointer is non-null, the offset should make some sense. |
|
304 // That is, it should be in the range [0, MAX_OBJECT_SIZE]. |
|
305 UNSAFE_ENTRY(jobject, Unsafe_GetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { |
|
306 oop p = JNIHandles::resolve(obj); |
|
307 oop v; |
|
308 |
|
309 if (UseCompressedOops) { |
|
310 narrowOop n = *(narrowOop*)index_oop_from_field_offset_long(p, offset); |
|
311 v = oopDesc::decode_heap_oop(n); |
|
312 } else { |
|
313 v = *(oop*)index_oop_from_field_offset_long(p, offset); |
|
314 } |
|
315 |
|
316 ensure_satb_referent_alive(p, offset, v); |
|
317 |
|
318 return JNIHandles::make_local(env, v); |
|
319 } UNSAFE_END |
|
320 |
|
321 UNSAFE_ENTRY(void, Unsafe_PutObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) { |
|
322 oop x = JNIHandles::resolve(x_h); |
|
323 oop p = JNIHandles::resolve(obj); |
|
324 |
|
325 if (UseCompressedOops) { |
|
326 oop_store((narrowOop*)index_oop_from_field_offset_long(p, offset), x); |
|
327 } else { |
|
328 oop_store((oop*)index_oop_from_field_offset_long(p, offset), x); |
|
329 } |
|
330 } UNSAFE_END |
|
331 |
|
332 UNSAFE_ENTRY(jobject, Unsafe_GetObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { |
|
333 oop p = JNIHandles::resolve(obj); |
|
334 void* addr = index_oop_from_field_offset_long(p, offset); |
|
335 |
|
336 volatile oop v; |
|
337 |
|
338 if (support_IRIW_for_not_multiple_copy_atomic_cpu) { |
|
339 OrderAccess::fence(); |
|
340 } |
|
341 |
|
342 if (UseCompressedOops) { |
|
343 volatile narrowOop n = *(volatile narrowOop*) addr; |
|
344 (void)const_cast<oop&>(v = oopDesc::decode_heap_oop(n)); |
|
345 } else { |
|
346 (void)const_cast<oop&>(v = *(volatile oop*) addr); |
|
347 } |
|
348 |
|
349 ensure_satb_referent_alive(p, offset, v); |
|
350 |
|
351 OrderAccess::acquire(); |
|
352 return JNIHandles::make_local(env, v); |
|
353 } UNSAFE_END |
|
354 |
|
355 UNSAFE_ENTRY(void, Unsafe_PutObjectVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject x_h)) { |
|
356 oop x = JNIHandles::resolve(x_h); |
|
357 oop p = JNIHandles::resolve(obj); |
|
358 void* addr = index_oop_from_field_offset_long(p, offset); |
|
359 OrderAccess::release(); |
|
360 |
|
361 if (UseCompressedOops) { |
|
362 oop_store((narrowOop*)addr, x); |
|
363 } else { |
|
364 oop_store((oop*)addr, x); |
|
365 } |
|
366 |
|
367 OrderAccess::fence(); |
|
368 } UNSAFE_END |
|
369 |
|
370 UNSAFE_ENTRY(jobject, Unsafe_GetUncompressedObject(JNIEnv *env, jobject unsafe, jlong addr)) { |
|
371 oop v = *(oop*) (address) addr; |
|
372 |
|
373 return JNIHandles::make_local(env, v); |
|
374 } UNSAFE_END |
|
375 |
|
376 #ifndef SUPPORTS_NATIVE_CX8 |
|
377 |
|
378 // VM_Version::supports_cx8() is a surrogate for 'supports atomic long memory ops'. |
|
379 // |
|
380 // On platforms which do not support atomic compare-and-swap of jlong (8 byte) |
|
381 // values we have to use a lock-based scheme to enforce atomicity. This has to be |
|
382 // applied to all Unsafe operations that set the value of a jlong field. Even so |
|
383 // the compareAndSetLong operation will not be atomic with respect to direct stores |
|
384 // to the field from Java code. It is important therefore that any Java code that |
|
385 // utilizes these Unsafe jlong operations does not perform direct stores. To permit |
|
386 // direct loads of the field from Java code we must also use Atomic::store within the |
|
387 // locked regions. And for good measure, in case there are direct stores, we also |
|
388 // employ Atomic::load within those regions. Note that the field in question must be |
|
389 // volatile and so must have atomic load/store accesses applied at the Java level. |
|
390 // |
|
391 // The locking scheme could utilize a range of strategies for controlling the locking |
|
392 // granularity: from a lock per-field through to a single global lock. The latter is |
|
393 // the simplest and is used for the current implementation. Note that the Java object |
|
394 // that contains the field, can not, in general, be used for locking. To do so can lead |
|
395 // to deadlocks as we may introduce locking into what appears to the Java code to be a |
|
396 // lock-free path. |
|
397 // |
|
398 // As all the locked-regions are very short and themselves non-blocking we can treat |
|
399 // them as leaf routines and elide safepoint checks (ie we don't perform any thread |
|
400 // state transitions even when blocking for the lock). Note that if we do choose to |
|
401 // add safepoint checks and thread state transitions, we must ensure that we calculate |
|
402 // the address of the field _after_ we have acquired the lock, else the object may have |
|
403 // been moved by the GC |
|
404 |
|
405 UNSAFE_ENTRY(jlong, Unsafe_GetLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { |
|
406 if (VM_Version::supports_cx8()) { |
|
407 return MemoryAccess(thread, obj, offset).get_volatile<jlong>(); |
|
408 } else { |
|
409 return MemoryAccess(thread, obj, offset).get_jlong_locked(); |
|
410 } |
|
411 } UNSAFE_END |
|
412 |
|
413 UNSAFE_ENTRY(void, Unsafe_PutLongVolatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong x)) { |
|
414 if (VM_Version::supports_cx8()) { |
|
415 MemoryAccess(thread, obj, offset).put_volatile<jlong>(x); |
|
416 } else { |
|
417 MemoryAccess(thread, obj, offset).put_jlong_locked(x); |
|
418 } |
|
419 } UNSAFE_END |
|
420 |
|
421 #endif // not SUPPORTS_NATIVE_CX8 |
|
422 |
|
423 UNSAFE_LEAF(jboolean, Unsafe_isBigEndian0(JNIEnv *env, jobject unsafe)) { |
|
424 #ifdef VM_LITTLE_ENDIAN |
|
425 return false; |
|
426 #else |
|
427 return true; |
|
428 #endif |
|
429 } UNSAFE_END |
|
430 |
|
431 UNSAFE_LEAF(jint, Unsafe_unalignedAccess0(JNIEnv *env, jobject unsafe)) { |
|
432 return UseUnalignedAccesses; |
|
433 } UNSAFE_END |
|
434 |
|
435 #define DEFINE_GETSETOOP(java_type, Type) \ |
|
436 \ |
|
437 UNSAFE_ENTRY(java_type, Unsafe_Get##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \ |
|
438 return MemoryAccess(thread, obj, offset).get<java_type>(); \ |
|
439 } UNSAFE_END \ |
|
440 \ |
|
441 UNSAFE_ENTRY(void, Unsafe_Put##Type(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \ |
|
442 MemoryAccess(thread, obj, offset).put<java_type>(x); \ |
|
443 } UNSAFE_END \ |
|
444 \ |
|
445 // END DEFINE_GETSETOOP. |
|
446 |
|
447 DEFINE_GETSETOOP(jboolean, Boolean) |
|
448 DEFINE_GETSETOOP(jbyte, Byte) |
|
449 DEFINE_GETSETOOP(jshort, Short); |
|
450 DEFINE_GETSETOOP(jchar, Char); |
|
451 DEFINE_GETSETOOP(jint, Int); |
|
452 DEFINE_GETSETOOP(jlong, Long); |
|
453 DEFINE_GETSETOOP(jfloat, Float); |
|
454 DEFINE_GETSETOOP(jdouble, Double); |
|
455 |
|
456 #undef DEFINE_GETSETOOP |
|
457 |
|
458 #define DEFINE_GETSETOOP_VOLATILE(java_type, Type) \ |
|
459 \ |
|
460 UNSAFE_ENTRY(java_type, Unsafe_Get##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset)) { \ |
|
461 return MemoryAccess(thread, obj, offset).get_volatile<java_type>(); \ |
|
462 } UNSAFE_END \ |
|
463 \ |
|
464 UNSAFE_ENTRY(void, Unsafe_Put##Type##Volatile(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, java_type x)) { \ |
|
465 MemoryAccess(thread, obj, offset).put_volatile<java_type>(x); \ |
|
466 } UNSAFE_END \ |
|
467 \ |
|
468 // END DEFINE_GETSETOOP_VOLATILE. |
|
469 |
|
470 DEFINE_GETSETOOP_VOLATILE(jboolean, Boolean) |
|
471 DEFINE_GETSETOOP_VOLATILE(jbyte, Byte) |
|
472 DEFINE_GETSETOOP_VOLATILE(jshort, Short); |
|
473 DEFINE_GETSETOOP_VOLATILE(jchar, Char); |
|
474 DEFINE_GETSETOOP_VOLATILE(jint, Int); |
|
475 DEFINE_GETSETOOP_VOLATILE(jfloat, Float); |
|
476 DEFINE_GETSETOOP_VOLATILE(jdouble, Double); |
|
477 |
|
478 #ifdef SUPPORTS_NATIVE_CX8 |
|
479 DEFINE_GETSETOOP_VOLATILE(jlong, Long); |
|
480 #endif |
|
481 |
|
482 #undef DEFINE_GETSETOOP_VOLATILE |
|
483 |
|
484 UNSAFE_LEAF(void, Unsafe_LoadFence(JNIEnv *env, jobject unsafe)) { |
|
485 OrderAccess::acquire(); |
|
486 } UNSAFE_END |
|
487 |
|
488 UNSAFE_LEAF(void, Unsafe_StoreFence(JNIEnv *env, jobject unsafe)) { |
|
489 OrderAccess::release(); |
|
490 } UNSAFE_END |
|
491 |
|
492 UNSAFE_LEAF(void, Unsafe_FullFence(JNIEnv *env, jobject unsafe)) { |
|
493 OrderAccess::fence(); |
|
494 } UNSAFE_END |
|
495 |
|
496 ////// Allocation requests |
|
497 |
|
498 UNSAFE_ENTRY(jobject, Unsafe_AllocateInstance(JNIEnv *env, jobject unsafe, jclass cls)) { |
|
499 ThreadToNativeFromVM ttnfv(thread); |
|
500 return env->AllocObject(cls); |
|
501 } UNSAFE_END |
|
502 |
|
503 UNSAFE_ENTRY(jlong, Unsafe_AllocateMemory0(JNIEnv *env, jobject unsafe, jlong size)) { |
|
504 size_t sz = (size_t)size; |
|
505 |
|
506 sz = align_up(sz, HeapWordSize); |
|
507 void* x = os::malloc(sz, mtInternal); |
|
508 |
|
509 return addr_to_java(x); |
|
510 } UNSAFE_END |
|
511 |
|
512 UNSAFE_ENTRY(jlong, Unsafe_ReallocateMemory0(JNIEnv *env, jobject unsafe, jlong addr, jlong size)) { |
|
513 void* p = addr_from_java(addr); |
|
514 size_t sz = (size_t)size; |
|
515 sz = align_up(sz, HeapWordSize); |
|
516 |
|
517 void* x = os::realloc(p, sz, mtInternal); |
|
518 |
|
519 return addr_to_java(x); |
|
520 } UNSAFE_END |
|
521 |
|
522 UNSAFE_ENTRY(void, Unsafe_FreeMemory0(JNIEnv *env, jobject unsafe, jlong addr)) { |
|
523 void* p = addr_from_java(addr); |
|
524 |
|
525 os::free(p); |
|
526 } UNSAFE_END |
|
527 |
|
528 UNSAFE_ENTRY(void, Unsafe_SetMemory0(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong size, jbyte value)) { |
|
529 size_t sz = (size_t)size; |
|
530 |
|
531 oop base = JNIHandles::resolve(obj); |
|
532 void* p = index_oop_from_field_offset_long(base, offset); |
|
533 |
|
534 Copy::fill_to_memory_atomic(p, sz, value); |
|
535 } UNSAFE_END |
|
536 |
|
537 UNSAFE_ENTRY(void, Unsafe_CopyMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size)) { |
|
538 size_t sz = (size_t)size; |
|
539 |
|
540 oop srcp = JNIHandles::resolve(srcObj); |
|
541 oop dstp = JNIHandles::resolve(dstObj); |
|
542 |
|
543 void* src = index_oop_from_field_offset_long(srcp, srcOffset); |
|
544 void* dst = index_oop_from_field_offset_long(dstp, dstOffset); |
|
545 |
|
546 Copy::conjoint_memory_atomic(src, dst, sz); |
|
547 } UNSAFE_END |
|
548 |
|
549 // This function is a leaf since if the source and destination are both in native memory |
|
550 // the copy may potentially be very large, and we don't want to disable GC if we can avoid it. |
|
551 // If either source or destination (or both) are on the heap, the function will enter VM using |
|
552 // JVM_ENTRY_FROM_LEAF |
|
553 UNSAFE_LEAF(void, Unsafe_CopySwapMemory0(JNIEnv *env, jobject unsafe, jobject srcObj, jlong srcOffset, jobject dstObj, jlong dstOffset, jlong size, jlong elemSize)) { |
|
554 size_t sz = (size_t)size; |
|
555 size_t esz = (size_t)elemSize; |
|
556 |
|
557 if (srcObj == NULL && dstObj == NULL) { |
|
558 // Both src & dst are in native memory |
|
559 address src = (address)srcOffset; |
|
560 address dst = (address)dstOffset; |
|
561 |
|
562 Copy::conjoint_swap(src, dst, sz, esz); |
|
563 } else { |
|
564 // At least one of src/dst are on heap, transition to VM to access raw pointers |
|
565 |
|
566 JVM_ENTRY_FROM_LEAF(env, void, Unsafe_CopySwapMemory0) { |
|
567 oop srcp = JNIHandles::resolve(srcObj); |
|
568 oop dstp = JNIHandles::resolve(dstObj); |
|
569 |
|
570 address src = (address)index_oop_from_field_offset_long(srcp, srcOffset); |
|
571 address dst = (address)index_oop_from_field_offset_long(dstp, dstOffset); |
|
572 |
|
573 Copy::conjoint_swap(src, dst, sz, esz); |
|
574 } JVM_END |
|
575 } |
|
576 } UNSAFE_END |
|
577 |
|
578 ////// Random queries |
|
579 |
|
580 UNSAFE_LEAF(jint, Unsafe_AddressSize0(JNIEnv *env, jobject unsafe)) { |
|
581 return sizeof(void*); |
|
582 } UNSAFE_END |
|
583 |
|
584 UNSAFE_LEAF(jint, Unsafe_PageSize()) { |
|
585 return os::vm_page_size(); |
|
586 } UNSAFE_END |
|
587 |
|
588 static jlong find_field_offset(jclass clazz, jstring name, TRAPS) { |
|
589 assert(clazz != NULL, "clazz must not be NULL"); |
|
590 assert(name != NULL, "name must not be NULL"); |
|
591 |
|
592 ResourceMark rm(THREAD); |
|
593 char *utf_name = java_lang_String::as_utf8_string(JNIHandles::resolve_non_null(name)); |
|
594 |
|
595 InstanceKlass* k = InstanceKlass::cast(java_lang_Class::as_Klass(JNIHandles::resolve_non_null(clazz))); |
|
596 |
|
597 jint offset = -1; |
|
598 for (JavaFieldStream fs(k); !fs.done(); fs.next()) { |
|
599 Symbol *name = fs.name(); |
|
600 if (name->equals(utf_name)) { |
|
601 offset = fs.offset(); |
|
602 break; |
|
603 } |
|
604 } |
|
605 if (offset < 0) { |
|
606 THROW_0(vmSymbols::java_lang_InternalError()); |
|
607 } |
|
608 return field_offset_from_byte_offset(offset); |
|
609 } |
|
610 |
|
611 static jlong find_field_offset(jobject field, int must_be_static, TRAPS) { |
|
612 assert(field != NULL, "field must not be NULL"); |
|
613 |
|
614 oop reflected = JNIHandles::resolve_non_null(field); |
|
615 oop mirror = java_lang_reflect_Field::clazz(reflected); |
|
616 Klass* k = java_lang_Class::as_Klass(mirror); |
|
617 int slot = java_lang_reflect_Field::slot(reflected); |
|
618 int modifiers = java_lang_reflect_Field::modifiers(reflected); |
|
619 |
|
620 if (must_be_static >= 0) { |
|
621 int really_is_static = ((modifiers & JVM_ACC_STATIC) != 0); |
|
622 if (must_be_static != really_is_static) { |
|
623 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); |
|
624 } |
|
625 } |
|
626 |
|
627 int offset = InstanceKlass::cast(k)->field_offset(slot); |
|
628 return field_offset_from_byte_offset(offset); |
|
629 } |
|
630 |
|
631 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) { |
|
632 return find_field_offset(field, 0, THREAD); |
|
633 } UNSAFE_END |
|
634 |
|
635 UNSAFE_ENTRY(jlong, Unsafe_ObjectFieldOffset1(JNIEnv *env, jobject unsafe, jclass c, jstring name)) { |
|
636 return find_field_offset(c, name, THREAD); |
|
637 } UNSAFE_END |
|
638 |
|
639 UNSAFE_ENTRY(jlong, Unsafe_StaticFieldOffset0(JNIEnv *env, jobject unsafe, jobject field)) { |
|
640 return find_field_offset(field, 1, THREAD); |
|
641 } UNSAFE_END |
|
642 |
|
643 UNSAFE_ENTRY(jobject, Unsafe_StaticFieldBase0(JNIEnv *env, jobject unsafe, jobject field)) { |
|
644 assert(field != NULL, "field must not be NULL"); |
|
645 |
|
646 // Note: In this VM implementation, a field address is always a short |
|
647 // offset from the base of a a klass metaobject. Thus, the full dynamic |
|
648 // range of the return type is never used. However, some implementations |
|
649 // might put the static field inside an array shared by many classes, |
|
650 // or even at a fixed address, in which case the address could be quite |
|
651 // large. In that last case, this function would return NULL, since |
|
652 // the address would operate alone, without any base pointer. |
|
653 |
|
654 oop reflected = JNIHandles::resolve_non_null(field); |
|
655 oop mirror = java_lang_reflect_Field::clazz(reflected); |
|
656 int modifiers = java_lang_reflect_Field::modifiers(reflected); |
|
657 |
|
658 if ((modifiers & JVM_ACC_STATIC) == 0) { |
|
659 THROW_0(vmSymbols::java_lang_IllegalArgumentException()); |
|
660 } |
|
661 |
|
662 return JNIHandles::make_local(env, mirror); |
|
663 } UNSAFE_END |
|
664 |
|
665 UNSAFE_ENTRY(void, Unsafe_EnsureClassInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) { |
|
666 assert(clazz != NULL, "clazz must not be NULL"); |
|
667 |
|
668 oop mirror = JNIHandles::resolve_non_null(clazz); |
|
669 |
|
670 Klass* klass = java_lang_Class::as_Klass(mirror); |
|
671 if (klass != NULL && klass->should_be_initialized()) { |
|
672 InstanceKlass* k = InstanceKlass::cast(klass); |
|
673 k->initialize(CHECK); |
|
674 } |
|
675 } |
|
676 UNSAFE_END |
|
677 |
|
678 UNSAFE_ENTRY(jboolean, Unsafe_ShouldBeInitialized0(JNIEnv *env, jobject unsafe, jobject clazz)) { |
|
679 assert(clazz != NULL, "clazz must not be NULL"); |
|
680 |
|
681 oop mirror = JNIHandles::resolve_non_null(clazz); |
|
682 Klass* klass = java_lang_Class::as_Klass(mirror); |
|
683 |
|
684 if (klass != NULL && klass->should_be_initialized()) { |
|
685 return true; |
|
686 } |
|
687 |
|
688 return false; |
|
689 } |
|
690 UNSAFE_END |
|
691 |
|
692 static void getBaseAndScale(int& base, int& scale, jclass clazz, TRAPS) { |
|
693 assert(clazz != NULL, "clazz must not be NULL"); |
|
694 |
|
695 oop mirror = JNIHandles::resolve_non_null(clazz); |
|
696 Klass* k = java_lang_Class::as_Klass(mirror); |
|
697 |
|
698 if (k == NULL || !k->is_array_klass()) { |
|
699 THROW(vmSymbols::java_lang_InvalidClassException()); |
|
700 } else if (k->is_objArray_klass()) { |
|
701 base = arrayOopDesc::base_offset_in_bytes(T_OBJECT); |
|
702 scale = heapOopSize; |
|
703 } else if (k->is_typeArray_klass()) { |
|
704 TypeArrayKlass* tak = TypeArrayKlass::cast(k); |
|
705 base = tak->array_header_in_bytes(); |
|
706 assert(base == arrayOopDesc::base_offset_in_bytes(tak->element_type()), "array_header_size semantics ok"); |
|
707 scale = (1 << tak->log2_element_size()); |
|
708 } else { |
|
709 ShouldNotReachHere(); |
|
710 } |
|
711 } |
|
712 |
|
713 UNSAFE_ENTRY(jint, Unsafe_ArrayBaseOffset0(JNIEnv *env, jobject unsafe, jclass clazz)) { |
|
714 int base = 0, scale = 0; |
|
715 getBaseAndScale(base, scale, clazz, CHECK_0); |
|
716 |
|
717 return field_offset_from_byte_offset(base); |
|
718 } UNSAFE_END |
|
719 |
|
720 |
|
721 UNSAFE_ENTRY(jint, Unsafe_ArrayIndexScale0(JNIEnv *env, jobject unsafe, jclass clazz)) { |
|
722 int base = 0, scale = 0; |
|
723 getBaseAndScale(base, scale, clazz, CHECK_0); |
|
724 |
|
725 // This VM packs both fields and array elements down to the byte. |
|
726 // But watch out: If this changes, so that array references for |
|
727 // a given primitive type (say, T_BOOLEAN) use different memory units |
|
728 // than fields, this method MUST return zero for such arrays. |
|
729 // For example, the VM used to store sub-word sized fields in full |
|
730 // words in the object layout, so that accessors like getByte(Object,int) |
|
731 // did not really do what one might expect for arrays. Therefore, |
|
732 // this function used to report a zero scale factor, so that the user |
|
733 // would know not to attempt to access sub-word array elements. |
|
734 // // Code for unpacked fields: |
|
735 // if (scale < wordSize) return 0; |
|
736 |
|
737 // The following allows for a pretty general fieldOffset cookie scheme, |
|
738 // but requires it to be linear in byte offset. |
|
739 return field_offset_from_byte_offset(scale) - field_offset_from_byte_offset(0); |
|
740 } UNSAFE_END |
|
741 |
|
742 |
|
743 static inline void throw_new(JNIEnv *env, const char *ename) { |
|
744 jclass cls = env->FindClass(ename); |
|
745 if (env->ExceptionCheck()) { |
|
746 env->ExceptionClear(); |
|
747 tty->print_cr("Unsafe: cannot throw %s because FindClass has failed", ename); |
|
748 return; |
|
749 } |
|
750 |
|
751 env->ThrowNew(cls, NULL); |
|
752 } |
|
753 |
|
754 static jclass Unsafe_DefineClass_impl(JNIEnv *env, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd) { |
|
755 // Code lifted from JDK 1.3 ClassLoader.c |
|
756 |
|
757 jbyte *body; |
|
758 char *utfName = NULL; |
|
759 jclass result = 0; |
|
760 char buf[128]; |
|
761 |
|
762 assert(data != NULL, "Class bytes must not be NULL"); |
|
763 assert(length >= 0, "length must not be negative: %d", length); |
|
764 |
|
765 if (UsePerfData) { |
|
766 ClassLoader::unsafe_defineClassCallCounter()->inc(); |
|
767 } |
|
768 |
|
769 body = NEW_C_HEAP_ARRAY(jbyte, length, mtInternal); |
|
770 if (body == NULL) { |
|
771 throw_new(env, "java/lang/OutOfMemoryError"); |
|
772 return 0; |
|
773 } |
|
774 |
|
775 env->GetByteArrayRegion(data, offset, length, body); |
|
776 if (env->ExceptionOccurred()) { |
|
777 goto free_body; |
|
778 } |
|
779 |
|
780 if (name != NULL) { |
|
781 uint len = env->GetStringUTFLength(name); |
|
782 int unicode_len = env->GetStringLength(name); |
|
783 |
|
784 if (len >= sizeof(buf)) { |
|
785 utfName = NEW_C_HEAP_ARRAY(char, len + 1, mtInternal); |
|
786 if (utfName == NULL) { |
|
787 throw_new(env, "java/lang/OutOfMemoryError"); |
|
788 goto free_body; |
|
789 } |
|
790 } else { |
|
791 utfName = buf; |
|
792 } |
|
793 |
|
794 env->GetStringUTFRegion(name, 0, unicode_len, utfName); |
|
795 |
|
796 for (uint i = 0; i < len; i++) { |
|
797 if (utfName[i] == '.') utfName[i] = '/'; |
|
798 } |
|
799 } |
|
800 |
|
801 result = JVM_DefineClass(env, utfName, loader, body, length, pd); |
|
802 |
|
803 if (utfName && utfName != buf) { |
|
804 FREE_C_HEAP_ARRAY(char, utfName); |
|
805 } |
|
806 |
|
807 free_body: |
|
808 FREE_C_HEAP_ARRAY(jbyte, body); |
|
809 return result; |
|
810 } |
|
811 |
|
812 |
|
813 UNSAFE_ENTRY(jclass, Unsafe_DefineClass0(JNIEnv *env, jobject unsafe, jstring name, jbyteArray data, int offset, int length, jobject loader, jobject pd)) { |
|
814 ThreadToNativeFromVM ttnfv(thread); |
|
815 |
|
816 return Unsafe_DefineClass_impl(env, name, data, offset, length, loader, pd); |
|
817 } UNSAFE_END |
|
818 |
|
819 |
|
820 // define a class but do not make it known to the class loader or system dictionary |
|
821 // - host_class: supplies context for linkage, access control, protection domain, and class loader |
|
822 // if host_class is itself anonymous then it is replaced with its host class. |
|
823 // - data: bytes of a class file, a raw memory address (length gives the number of bytes) |
|
824 // - cp_patches: where non-null entries exist, they replace corresponding CP entries in data |
|
825 |
|
826 // When you load an anonymous class U, it works as if you changed its name just before loading, |
|
827 // to a name that you will never use again. Since the name is lost, no other class can directly |
|
828 // link to any member of U. Just after U is loaded, the only way to use it is reflectively, |
|
829 // through java.lang.Class methods like Class.newInstance. |
|
830 |
|
831 // The package of an anonymous class must either match its host's class's package or be in the |
|
832 // unnamed package. If it is in the unnamed package then it will be put in its host class's |
|
833 // package. |
|
834 // |
|
835 |
|
836 // Access checks for linkage sites within U continue to follow the same rules as for named classes. |
|
837 // An anonymous class also has special privileges to access any member of its host class. |
|
838 // This is the main reason why this loading operation is unsafe. The purpose of this is to |
|
839 // allow language implementations to simulate "open classes"; a host class in effect gets |
|
840 // new code when an anonymous class is loaded alongside it. A less convenient but more |
|
841 // standard way to do this is with reflection, which can also be set to ignore access |
|
842 // restrictions. |
|
843 |
|
844 // Access into an anonymous class is possible only through reflection. Therefore, there |
|
845 // are no special access rules for calling into an anonymous class. The relaxed access |
|
846 // rule for the host class is applied in the opposite direction: A host class reflectively |
|
847 // access one of its anonymous classes. |
|
848 |
|
849 // If you load the same bytecodes twice, you get two different classes. You can reload |
|
850 // the same bytecodes with or without varying CP patches. |
|
851 |
|
852 // By using the CP patching array, you can have a new anonymous class U2 refer to an older one U1. |
|
853 // The bytecodes for U2 should refer to U1 by a symbolic name (doesn't matter what the name is). |
|
854 // The CONSTANT_Class entry for that name can be patched to refer directly to U1. |
|
855 |
|
856 // This allows, for example, U2 to use U1 as a superclass or super-interface, or as |
|
857 // an outer class (so that U2 is an anonymous inner class of anonymous U1). |
|
858 // It is not possible for a named class, or an older anonymous class, to refer by |
|
859 // name (via its CP) to a newer anonymous class. |
|
860 |
|
861 // CP patching may also be used to modify (i.e., hack) the names of methods, classes, |
|
862 // or type descriptors used in the loaded anonymous class. |
|
863 |
|
864 // Finally, CP patching may be used to introduce "live" objects into the constant pool, |
|
865 // instead of "dead" strings. A compiled statement like println((Object)"hello") can |
|
866 // be changed to println(greeting), where greeting is an arbitrary object created before |
|
867 // the anonymous class is loaded. This is useful in dynamic languages, in which |
|
868 // various kinds of metaobjects must be introduced as constants into bytecode. |
|
869 // Note the cast (Object), which tells the verifier to expect an arbitrary object, |
|
870 // not just a literal string. For such ldc instructions, the verifier uses the |
|
871 // type Object instead of String, if the loaded constant is not in fact a String. |
|
872 |
|
873 static InstanceKlass* |
|
874 Unsafe_DefineAnonymousClass_impl(JNIEnv *env, |
|
875 jclass host_class, jbyteArray data, jobjectArray cp_patches_jh, |
|
876 u1** temp_alloc, |
|
877 TRAPS) { |
|
878 assert(host_class != NULL, "host_class must not be NULL"); |
|
879 assert(data != NULL, "data must not be NULL"); |
|
880 |
|
881 if (UsePerfData) { |
|
882 ClassLoader::unsafe_defineClassCallCounter()->inc(); |
|
883 } |
|
884 |
|
885 jint length = typeArrayOop(JNIHandles::resolve_non_null(data))->length(); |
|
886 assert(length >= 0, "class_bytes_length must not be negative: %d", length); |
|
887 |
|
888 int class_bytes_length = (int) length; |
|
889 |
|
890 u1* class_bytes = NEW_C_HEAP_ARRAY(u1, length, mtInternal); |
|
891 if (class_bytes == NULL) { |
|
892 THROW_0(vmSymbols::java_lang_OutOfMemoryError()); |
|
893 } |
|
894 |
|
895 // caller responsible to free it: |
|
896 *temp_alloc = class_bytes; |
|
897 |
|
898 jbyte* array_base = typeArrayOop(JNIHandles::resolve_non_null(data))->byte_at_addr(0); |
|
899 Copy::conjoint_jbytes(array_base, class_bytes, length); |
|
900 |
|
901 objArrayHandle cp_patches_h; |
|
902 if (cp_patches_jh != NULL) { |
|
903 oop p = JNIHandles::resolve_non_null(cp_patches_jh); |
|
904 assert(p->is_objArray(), "cp_patches must be an object[]"); |
|
905 cp_patches_h = objArrayHandle(THREAD, (objArrayOop)p); |
|
906 } |
|
907 |
|
908 const Klass* host_klass = java_lang_Class::as_Klass(JNIHandles::resolve_non_null(host_class)); |
|
909 |
|
910 // Make sure it's the real host class, not another anonymous class. |
|
911 while (host_klass != NULL && host_klass->is_instance_klass() && |
|
912 InstanceKlass::cast(host_klass)->is_anonymous()) { |
|
913 host_klass = InstanceKlass::cast(host_klass)->host_klass(); |
|
914 } |
|
915 |
|
916 // Primitive types have NULL Klass* fields in their java.lang.Class instances. |
|
917 if (host_klass == NULL) { |
|
918 THROW_MSG_0(vmSymbols::java_lang_IllegalArgumentException(), "Host class is null"); |
|
919 } |
|
920 |
|
921 assert(host_klass->is_instance_klass(), "Host class must be an instance class"); |
|
922 |
|
923 const char* host_source = host_klass->external_name(); |
|
924 Handle host_loader(THREAD, host_klass->class_loader()); |
|
925 Handle host_domain(THREAD, host_klass->protection_domain()); |
|
926 |
|
927 GrowableArray<Handle>* cp_patches = NULL; |
|
928 |
|
929 if (cp_patches_h.not_null()) { |
|
930 int alen = cp_patches_h->length(); |
|
931 |
|
932 for (int i = alen-1; i >= 0; i--) { |
|
933 oop p = cp_patches_h->obj_at(i); |
|
934 if (p != NULL) { |
|
935 Handle patch(THREAD, p); |
|
936 |
|
937 if (cp_patches == NULL) { |
|
938 cp_patches = new GrowableArray<Handle>(i+1, i+1, Handle()); |
|
939 } |
|
940 |
|
941 cp_patches->at_put(i, patch); |
|
942 } |
|
943 } |
|
944 } |
|
945 |
|
946 ClassFileStream st(class_bytes, class_bytes_length, host_source, ClassFileStream::verify); |
|
947 |
|
948 Symbol* no_class_name = NULL; |
|
949 Klass* anonk = SystemDictionary::parse_stream(no_class_name, |
|
950 host_loader, |
|
951 host_domain, |
|
952 &st, |
|
953 InstanceKlass::cast(host_klass), |
|
954 cp_patches, |
|
955 CHECK_NULL); |
|
956 if (anonk == NULL) { |
|
957 return NULL; |
|
958 } |
|
959 |
|
960 return InstanceKlass::cast(anonk); |
|
961 } |
|
962 |
|
963 UNSAFE_ENTRY(jclass, Unsafe_DefineAnonymousClass0(JNIEnv *env, jobject unsafe, jclass host_class, jbyteArray data, jobjectArray cp_patches_jh)) { |
|
964 ResourceMark rm(THREAD); |
|
965 |
|
966 jobject res_jh = NULL; |
|
967 u1* temp_alloc = NULL; |
|
968 |
|
969 InstanceKlass* anon_klass = Unsafe_DefineAnonymousClass_impl(env, host_class, data, cp_patches_jh, &temp_alloc, THREAD); |
|
970 if (anon_klass != NULL) { |
|
971 res_jh = JNIHandles::make_local(env, anon_klass->java_mirror()); |
|
972 } |
|
973 |
|
974 // try/finally clause: |
|
975 if (temp_alloc != NULL) { |
|
976 FREE_C_HEAP_ARRAY(u1, temp_alloc); |
|
977 } |
|
978 |
|
979 // The anonymous class loader data has been artificially been kept alive to |
|
980 // this point. The mirror and any instances of this class have to keep |
|
981 // it alive afterwards. |
|
982 if (anon_klass != NULL) { |
|
983 anon_klass->class_loader_data()->dec_keep_alive(); |
|
984 } |
|
985 |
|
986 // let caller initialize it as needed... |
|
987 |
|
988 return (jclass) res_jh; |
|
989 } UNSAFE_END |
|
990 |
|
991 |
|
992 |
|
993 UNSAFE_ENTRY(void, Unsafe_ThrowException(JNIEnv *env, jobject unsafe, jthrowable thr)) { |
|
994 ThreadToNativeFromVM ttnfv(thread); |
|
995 env->Throw(thr); |
|
996 } UNSAFE_END |
|
997 |
|
998 // JSR166 ------------------------------------------------------------------ |
|
999 |
|
1000 UNSAFE_ENTRY(jobject, Unsafe_CompareAndExchangeObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) { |
|
1001 oop x = JNIHandles::resolve(x_h); |
|
1002 oop e = JNIHandles::resolve(e_h); |
|
1003 oop p = JNIHandles::resolve(obj); |
|
1004 HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); |
|
1005 oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true); |
|
1006 if (res == e) { |
|
1007 update_barrier_set((void*)addr, x); |
|
1008 } |
|
1009 return JNIHandles::make_local(env, res); |
|
1010 } UNSAFE_END |
|
1011 |
|
1012 UNSAFE_ENTRY(jint, Unsafe_CompareAndExchangeInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) { |
|
1013 oop p = JNIHandles::resolve(obj); |
|
1014 jint* addr = (jint *) index_oop_from_field_offset_long(p, offset); |
|
1015 |
|
1016 return (jint)(Atomic::cmpxchg(x, addr, e)); |
|
1017 } UNSAFE_END |
|
1018 |
|
1019 UNSAFE_ENTRY(jlong, Unsafe_CompareAndExchangeLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) { |
|
1020 Handle p(THREAD, JNIHandles::resolve(obj)); |
|
1021 jlong* addr = (jlong*)index_oop_from_field_offset_long(p(), offset); |
|
1022 |
|
1023 #ifdef SUPPORTS_NATIVE_CX8 |
|
1024 return (jlong)(Atomic::cmpxchg(x, addr, e)); |
|
1025 #else |
|
1026 if (VM_Version::supports_cx8()) { |
|
1027 return (jlong)(Atomic::cmpxchg(x, addr, e)); |
|
1028 } else { |
|
1029 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); |
|
1030 |
|
1031 jlong val = Atomic::load(addr); |
|
1032 if (val == e) { |
|
1033 Atomic::store(x, addr); |
|
1034 } |
|
1035 return val; |
|
1036 } |
|
1037 #endif |
|
1038 } UNSAFE_END |
|
1039 |
|
1040 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetObject(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jobject e_h, jobject x_h)) { |
|
1041 oop x = JNIHandles::resolve(x_h); |
|
1042 oop e = JNIHandles::resolve(e_h); |
|
1043 oop p = JNIHandles::resolve(obj); |
|
1044 HeapWord* addr = (HeapWord *)index_oop_from_field_offset_long(p, offset); |
|
1045 oop res = oopDesc::atomic_compare_exchange_oop(x, addr, e, true); |
|
1046 if (res != e) { |
|
1047 return false; |
|
1048 } |
|
1049 |
|
1050 update_barrier_set((void*)addr, x); |
|
1051 |
|
1052 return true; |
|
1053 } UNSAFE_END |
|
1054 |
|
1055 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetInt(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jint e, jint x)) { |
|
1056 oop p = JNIHandles::resolve(obj); |
|
1057 jint* addr = (jint *)index_oop_from_field_offset_long(p, offset); |
|
1058 |
|
1059 return (jint)(Atomic::cmpxchg(x, addr, e)) == e; |
|
1060 } UNSAFE_END |
|
1061 |
|
1062 UNSAFE_ENTRY(jboolean, Unsafe_CompareAndSetLong(JNIEnv *env, jobject unsafe, jobject obj, jlong offset, jlong e, jlong x)) { |
|
1063 Handle p(THREAD, JNIHandles::resolve(obj)); |
|
1064 jlong* addr = (jlong*)index_oop_from_field_offset_long(p(), offset); |
|
1065 |
|
1066 #ifdef SUPPORTS_NATIVE_CX8 |
|
1067 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; |
|
1068 #else |
|
1069 if (VM_Version::supports_cx8()) { |
|
1070 return (jlong)(Atomic::cmpxchg(x, addr, e)) == e; |
|
1071 } else { |
|
1072 MutexLockerEx mu(UnsafeJlong_lock, Mutex::_no_safepoint_check_flag); |
|
1073 |
|
1074 jlong val = Atomic::load(addr); |
|
1075 if (val != e) { |
|
1076 return false; |
|
1077 } |
|
1078 |
|
1079 Atomic::store(x, addr); |
|
1080 return true; |
|
1081 } |
|
1082 #endif |
|
1083 } UNSAFE_END |
|
1084 |
|
1085 UNSAFE_ENTRY(void, Unsafe_Park(JNIEnv *env, jobject unsafe, jboolean isAbsolute, jlong time)) { |
|
1086 EventThreadPark event; |
|
1087 HOTSPOT_THREAD_PARK_BEGIN((uintptr_t) thread->parker(), (int) isAbsolute, time); |
|
1088 |
|
1089 JavaThreadParkedState jtps(thread, time != 0); |
|
1090 thread->parker()->park(isAbsolute != 0, time); |
|
1091 |
|
1092 HOTSPOT_THREAD_PARK_END((uintptr_t) thread->parker()); |
|
1093 |
|
1094 if (event.should_commit()) { |
|
1095 oop obj = thread->current_park_blocker(); |
|
1096 event.set_parkedClass((obj != NULL) ? obj->klass() : NULL); |
|
1097 event.set_timeout(time); |
|
1098 event.set_address((obj != NULL) ? (TYPE_ADDRESS) cast_from_oop<uintptr_t>(obj) : 0); |
|
1099 event.commit(); |
|
1100 } |
|
1101 } UNSAFE_END |
|
1102 |
|
1103 UNSAFE_ENTRY(void, Unsafe_Unpark(JNIEnv *env, jobject unsafe, jobject jthread)) { |
|
1104 Parker* p = NULL; |
|
1105 |
|
1106 if (jthread != NULL) { |
|
1107 oop java_thread = JNIHandles::resolve_non_null(jthread); |
|
1108 if (java_thread != NULL) { |
|
1109 jlong lp = java_lang_Thread::park_event(java_thread); |
|
1110 if (lp != 0) { |
|
1111 // This cast is OK even though the jlong might have been read |
|
1112 // non-atomically on 32bit systems, since there, one word will |
|
1113 // always be zero anyway and the value set is always the same |
|
1114 p = (Parker*)addr_from_java(lp); |
|
1115 } else { |
|
1116 // Grab lock if apparently null or using older version of library |
|
1117 MutexLocker mu(Threads_lock); |
|
1118 java_thread = JNIHandles::resolve_non_null(jthread); |
|
1119 |
|
1120 if (java_thread != NULL) { |
|
1121 JavaThread* thr = java_lang_Thread::thread(java_thread); |
|
1122 if (thr != NULL) { |
|
1123 p = thr->parker(); |
|
1124 if (p != NULL) { // Bind to Java thread for next time. |
|
1125 java_lang_Thread::set_park_event(java_thread, addr_to_java(p)); |
|
1126 } |
|
1127 } |
|
1128 } |
|
1129 } |
|
1130 } |
|
1131 } |
|
1132 |
|
1133 if (p != NULL) { |
|
1134 HOTSPOT_THREAD_UNPARK((uintptr_t) p); |
|
1135 p->unpark(); |
|
1136 } |
|
1137 } UNSAFE_END |
|
1138 |
|
1139 UNSAFE_ENTRY(jint, Unsafe_GetLoadAverage0(JNIEnv *env, jobject unsafe, jdoubleArray loadavg, jint nelem)) { |
|
1140 const int max_nelem = 3; |
|
1141 double la[max_nelem]; |
|
1142 jint ret; |
|
1143 |
|
1144 typeArrayOop a = typeArrayOop(JNIHandles::resolve_non_null(loadavg)); |
|
1145 assert(a->is_typeArray(), "must be type array"); |
|
1146 |
|
1147 ret = os::loadavg(la, nelem); |
|
1148 if (ret == -1) { |
|
1149 return -1; |
|
1150 } |
|
1151 |
|
1152 // if successful, ret is the number of samples actually retrieved. |
|
1153 assert(ret >= 0 && ret <= max_nelem, "Unexpected loadavg return value"); |
|
1154 switch(ret) { |
|
1155 case 3: a->double_at_put(2, (jdouble)la[2]); // fall through |
|
1156 case 2: a->double_at_put(1, (jdouble)la[1]); // fall through |
|
1157 case 1: a->double_at_put(0, (jdouble)la[0]); break; |
|
1158 } |
|
1159 |
|
1160 return ret; |
|
1161 } UNSAFE_END |
|
1162 |
|
1163 |
|
1164 /// JVM_RegisterUnsafeMethods |
|
1165 |
|
1166 #define ADR "J" |
|
1167 |
|
1168 #define LANG "Ljava/lang/" |
|
1169 |
|
1170 #define OBJ LANG "Object;" |
|
1171 #define CLS LANG "Class;" |
|
1172 #define FLD LANG "reflect/Field;" |
|
1173 #define THR LANG "Throwable;" |
|
1174 |
|
1175 #define DC_Args LANG "String;[BII" LANG "ClassLoader;" "Ljava/security/ProtectionDomain;" |
|
1176 #define DAC_Args CLS "[B[" OBJ |
|
1177 |
|
1178 #define CC (char*) /*cast a literal from (const char*)*/ |
|
1179 #define FN_PTR(f) CAST_FROM_FN_PTR(void*, &f) |
|
1180 |
|
1181 #define DECLARE_GETPUTOOP(Type, Desc) \ |
|
1182 {CC "get" #Type, CC "(" OBJ "J)" #Desc, FN_PTR(Unsafe_Get##Type)}, \ |
|
1183 {CC "put" #Type, CC "(" OBJ "J" #Desc ")V", FN_PTR(Unsafe_Put##Type)}, \ |
|
1184 {CC "get" #Type "Volatile", CC "(" OBJ "J)" #Desc, FN_PTR(Unsafe_Get##Type##Volatile)}, \ |
|
1185 {CC "put" #Type "Volatile", CC "(" OBJ "J" #Desc ")V", FN_PTR(Unsafe_Put##Type##Volatile)} |
|
1186 |
|
1187 |
|
1188 static JNINativeMethod jdk_internal_misc_Unsafe_methods[] = { |
|
1189 {CC "getObject", CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObject)}, |
|
1190 {CC "putObject", CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_PutObject)}, |
|
1191 {CC "getObjectVolatile",CC "(" OBJ "J)" OBJ "", FN_PTR(Unsafe_GetObjectVolatile)}, |
|
1192 {CC "putObjectVolatile",CC "(" OBJ "J" OBJ ")V", FN_PTR(Unsafe_PutObjectVolatile)}, |
|
1193 |
|
1194 {CC "getUncompressedObject", CC "(" ADR ")" OBJ, FN_PTR(Unsafe_GetUncompressedObject)}, |
|
1195 |
|
1196 DECLARE_GETPUTOOP(Boolean, Z), |
|
1197 DECLARE_GETPUTOOP(Byte, B), |
|
1198 DECLARE_GETPUTOOP(Short, S), |
|
1199 DECLARE_GETPUTOOP(Char, C), |
|
1200 DECLARE_GETPUTOOP(Int, I), |
|
1201 DECLARE_GETPUTOOP(Long, J), |
|
1202 DECLARE_GETPUTOOP(Float, F), |
|
1203 DECLARE_GETPUTOOP(Double, D), |
|
1204 |
|
1205 {CC "allocateMemory0", CC "(J)" ADR, FN_PTR(Unsafe_AllocateMemory0)}, |
|
1206 {CC "reallocateMemory0", CC "(" ADR "J)" ADR, FN_PTR(Unsafe_ReallocateMemory0)}, |
|
1207 {CC "freeMemory0", CC "(" ADR ")V", FN_PTR(Unsafe_FreeMemory0)}, |
|
1208 |
|
1209 {CC "objectFieldOffset0", CC "(" FLD ")J", FN_PTR(Unsafe_ObjectFieldOffset0)}, |
|
1210 {CC "objectFieldOffset1", CC "(" CLS LANG "String;)J", FN_PTR(Unsafe_ObjectFieldOffset1)}, |
|
1211 {CC "staticFieldOffset0", CC "(" FLD ")J", FN_PTR(Unsafe_StaticFieldOffset0)}, |
|
1212 {CC "staticFieldBase0", CC "(" FLD ")" OBJ, FN_PTR(Unsafe_StaticFieldBase0)}, |
|
1213 {CC "ensureClassInitialized0", CC "(" CLS ")V", FN_PTR(Unsafe_EnsureClassInitialized0)}, |
|
1214 {CC "arrayBaseOffset0", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayBaseOffset0)}, |
|
1215 {CC "arrayIndexScale0", CC "(" CLS ")I", FN_PTR(Unsafe_ArrayIndexScale0)}, |
|
1216 {CC "addressSize0", CC "()I", FN_PTR(Unsafe_AddressSize0)}, |
|
1217 {CC "pageSize", CC "()I", FN_PTR(Unsafe_PageSize)}, |
|
1218 |
|
1219 {CC "defineClass0", CC "(" DC_Args ")" CLS, FN_PTR(Unsafe_DefineClass0)}, |
|
1220 {CC "allocateInstance", CC "(" CLS ")" OBJ, FN_PTR(Unsafe_AllocateInstance)}, |
|
1221 {CC "throwException", CC "(" THR ")V", FN_PTR(Unsafe_ThrowException)}, |
|
1222 {CC "compareAndSetObject",CC "(" OBJ "J" OBJ "" OBJ ")Z", FN_PTR(Unsafe_CompareAndSetObject)}, |
|
1223 {CC "compareAndSetInt", CC "(" OBJ "J""I""I"")Z", FN_PTR(Unsafe_CompareAndSetInt)}, |
|
1224 {CC "compareAndSetLong", CC "(" OBJ "J""J""J"")Z", FN_PTR(Unsafe_CompareAndSetLong)}, |
|
1225 {CC "compareAndExchangeObject", CC "(" OBJ "J" OBJ "" OBJ ")" OBJ, FN_PTR(Unsafe_CompareAndExchangeObject)}, |
|
1226 {CC "compareAndExchangeInt", CC "(" OBJ "J""I""I"")I", FN_PTR(Unsafe_CompareAndExchangeInt)}, |
|
1227 {CC "compareAndExchangeLong", CC "(" OBJ "J""J""J"")J", FN_PTR(Unsafe_CompareAndExchangeLong)}, |
|
1228 |
|
1229 {CC "park", CC "(ZJ)V", FN_PTR(Unsafe_Park)}, |
|
1230 {CC "unpark", CC "(" OBJ ")V", FN_PTR(Unsafe_Unpark)}, |
|
1231 |
|
1232 {CC "getLoadAverage0", CC "([DI)I", FN_PTR(Unsafe_GetLoadAverage0)}, |
|
1233 |
|
1234 {CC "copyMemory0", CC "(" OBJ "J" OBJ "JJ)V", FN_PTR(Unsafe_CopyMemory0)}, |
|
1235 {CC "copySwapMemory0", CC "(" OBJ "J" OBJ "JJJ)V", FN_PTR(Unsafe_CopySwapMemory0)}, |
|
1236 {CC "setMemory0", CC "(" OBJ "JJB)V", FN_PTR(Unsafe_SetMemory0)}, |
|
1237 |
|
1238 {CC "defineAnonymousClass0", CC "(" DAC_Args ")" CLS, FN_PTR(Unsafe_DefineAnonymousClass0)}, |
|
1239 |
|
1240 {CC "shouldBeInitialized0", CC "(" CLS ")Z", FN_PTR(Unsafe_ShouldBeInitialized0)}, |
|
1241 |
|
1242 {CC "loadFence", CC "()V", FN_PTR(Unsafe_LoadFence)}, |
|
1243 {CC "storeFence", CC "()V", FN_PTR(Unsafe_StoreFence)}, |
|
1244 {CC "fullFence", CC "()V", FN_PTR(Unsafe_FullFence)}, |
|
1245 |
|
1246 {CC "isBigEndian0", CC "()Z", FN_PTR(Unsafe_isBigEndian0)}, |
|
1247 {CC "unalignedAccess0", CC "()Z", FN_PTR(Unsafe_unalignedAccess0)} |
|
1248 }; |
|
1249 |
|
1250 #undef CC |
|
1251 #undef FN_PTR |
|
1252 |
|
1253 #undef ADR |
|
1254 #undef LANG |
|
1255 #undef OBJ |
|
1256 #undef CLS |
|
1257 #undef FLD |
|
1258 #undef THR |
|
1259 #undef DC_Args |
|
1260 #undef DAC_Args |
|
1261 |
|
1262 #undef DECLARE_GETPUTOOP |
|
1263 |
|
1264 |
|
1265 // This function is exported, used by NativeLookup. |
|
1266 // The Unsafe_xxx functions above are called only from the interpreter. |
|
1267 // The optimizer looks at names and signatures to recognize |
|
1268 // individual functions. |
|
1269 |
|
1270 JVM_ENTRY(void, JVM_RegisterJDKInternalMiscUnsafeMethods(JNIEnv *env, jclass unsafeclass)) { |
|
1271 ThreadToNativeFromVM ttnfv(thread); |
|
1272 |
|
1273 int ok = env->RegisterNatives(unsafeclass, jdk_internal_misc_Unsafe_methods, sizeof(jdk_internal_misc_Unsafe_methods)/sizeof(JNINativeMethod)); |
|
1274 guarantee(ok == 0, "register jdk.internal.misc.Unsafe natives"); |
|
1275 } JVM_END |