1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "sun_misc_Unsafe.h"
18 
19 #include <unistd.h>
20 
21 #include <cstdlib>
22 #include <cstring>
23 #include <atomic>
24 
25 #include "nativehelper/jni_macros.h"
26 
27 #include "base/quasi_atomic.h"
28 #include "common_throws.h"
29 #include "gc/accounting/card_table-inl.h"
30 #include "jni/jni_internal.h"
31 #include "mirror/array.h"
32 #include "mirror/class-inl.h"
33 #include "mirror/object-inl.h"
34 #include "art_field-inl.h"
35 #include "native_util.h"
36 #include "scoped_fast_native_object_access-inl.h"
37 #include "well_known_classes-inl.h"
38 
39 namespace art HIDDEN {
40 
Unsafe_compareAndSwapInt(JNIEnv * env,jobject,jobject javaObj,jlong offset,jint expectedValue,jint newValue)41 static jboolean Unsafe_compareAndSwapInt(JNIEnv* env, jobject, jobject javaObj, jlong offset,
42                                          jint expectedValue, jint newValue) {
43   ScopedFastNativeObjectAccess soa(env);
44   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
45   // JNI must use non transactional mode.
46   bool success = obj->CasField32<false>(MemberOffset(offset),
47                                         expectedValue,
48                                         newValue,
49                                         CASMode::kStrong,
50                                         std::memory_order_seq_cst);
51   return success ? JNI_TRUE : JNI_FALSE;
52 }
53 
Unsafe_compareAndSwapLong(JNIEnv * env,jobject,jobject javaObj,jlong offset,jlong expectedValue,jlong newValue)54 static jboolean Unsafe_compareAndSwapLong(JNIEnv* env, jobject, jobject javaObj, jlong offset,
55                                           jlong expectedValue, jlong newValue) {
56   ScopedFastNativeObjectAccess soa(env);
57   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
58   // JNI must use non transactional mode.
59   bool success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
60                                                                     expectedValue,
61                                                                     newValue);
62   return success ? JNI_TRUE : JNI_FALSE;
63 }
64 
Unsafe_compareAndSwapObject(JNIEnv * env,jobject,jobject javaObj,jlong offset,jobject javaExpectedValue,jobject javaNewValue)65 static jboolean Unsafe_compareAndSwapObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
66                                             jobject javaExpectedValue, jobject javaNewValue) {
67   ScopedFastNativeObjectAccess soa(env);
68   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
69   ObjPtr<mirror::Object> expectedValue = soa.Decode<mirror::Object>(javaExpectedValue);
70   ObjPtr<mirror::Object> newValue = soa.Decode<mirror::Object>(javaNewValue);
71   // JNI must use non transactional mode.
72   if (gUseReadBarrier) {
73     // Need to make sure the reference stored in the field is a to-space one before attempting the
74     // CAS or the CAS could fail incorrectly.
75     // Note that the read barrier load does NOT need to be volatile.
76     mirror::HeapReference<mirror::Object>* field_addr =
77         reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
78             reinterpret_cast<uint8_t*>(obj.Ptr()) + static_cast<size_t>(offset));
79     ReadBarrier::Barrier<mirror::Object, /* kIsVolatile= */ false, kWithReadBarrier,
80         /* kAlwaysUpdateField= */ true>(
81         obj.Ptr(),
82         MemberOffset(offset),
83         field_addr);
84   }
85   bool success = obj->CasFieldObject<false>(MemberOffset(offset),
86                                             expectedValue,
87                                             newValue,
88                                             CASMode::kStrong,
89                                             std::memory_order_seq_cst);
90   return success ? JNI_TRUE : JNI_FALSE;
91 }
92 
Unsafe_getInt(JNIEnv * env,jobject,jobject javaObj,jlong offset)93 static jint Unsafe_getInt(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
94   ScopedFastNativeObjectAccess soa(env);
95   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
96   return obj->GetField32(MemberOffset(offset));
97 }
98 
Unsafe_getIntVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset)99 static jint Unsafe_getIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
100   ScopedFastNativeObjectAccess soa(env);
101   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
102   return obj->GetField32Volatile(MemberOffset(offset));
103 }
104 
Unsafe_putInt(JNIEnv * env,jobject,jobject javaObj,jlong offset,jint newValue)105 static void Unsafe_putInt(JNIEnv* env, jobject, jobject javaObj, jlong offset, jint newValue) {
106   ScopedFastNativeObjectAccess soa(env);
107   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
108   // JNI must use non transactional mode.
109   obj->SetField32<false>(MemberOffset(offset), newValue);
110 }
111 
Unsafe_putIntVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset,jint newValue)112 static void Unsafe_putIntVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
113                                   jint newValue) {
114   ScopedFastNativeObjectAccess soa(env);
115   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
116   // JNI must use non transactional mode.
117   obj->SetField32Volatile<false>(MemberOffset(offset), newValue);
118 }
119 
Unsafe_putOrderedInt(JNIEnv * env,jobject,jobject javaObj,jlong offset,jint newValue)120 static void Unsafe_putOrderedInt(JNIEnv* env, jobject, jobject javaObj, jlong offset,
121                                  jint newValue) {
122   ScopedFastNativeObjectAccess soa(env);
123   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
124   // TODO: A release store is likely to be faster on future processors.
125   std::atomic_thread_fence(std::memory_order_release);
126   // JNI must use non transactional mode.
127   obj->SetField32<false>(MemberOffset(offset), newValue);
128 }
129 
Unsafe_getLong(JNIEnv * env,jobject,jobject javaObj,jlong offset)130 static jlong Unsafe_getLong(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
131   ScopedFastNativeObjectAccess soa(env);
132   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
133   return obj->GetField64(MemberOffset(offset));
134 }
135 
Unsafe_getLongVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset)136 static jlong Unsafe_getLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
137   ScopedFastNativeObjectAccess soa(env);
138   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
139   return obj->GetField64Volatile(MemberOffset(offset));
140 }
141 
Unsafe_putLong(JNIEnv * env,jobject,jobject javaObj,jlong offset,jlong newValue)142 static void Unsafe_putLong(JNIEnv* env, jobject, jobject javaObj, jlong offset, jlong newValue) {
143   ScopedFastNativeObjectAccess soa(env);
144   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
145   // JNI must use non transactional mode.
146   obj->SetField64<false>(MemberOffset(offset), newValue);
147 }
148 
Unsafe_putLongVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset,jlong newValue)149 static void Unsafe_putLongVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
150                                    jlong newValue) {
151   ScopedFastNativeObjectAccess soa(env);
152   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
153   // JNI must use non transactional mode.
154   obj->SetField64Volatile<false>(MemberOffset(offset), newValue);
155 }
156 
Unsafe_putOrderedLong(JNIEnv * env,jobject,jobject javaObj,jlong offset,jlong newValue)157 static void Unsafe_putOrderedLong(JNIEnv* env, jobject, jobject javaObj, jlong offset,
158                                   jlong newValue) {
159   ScopedFastNativeObjectAccess soa(env);
160   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
161   std::atomic_thread_fence(std::memory_order_release);
162   // JNI must use non transactional mode.
163   obj->SetField64<false>(MemberOffset(offset), newValue);
164 }
165 
Unsafe_getObjectVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset)166 static jobject Unsafe_getObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
167   ScopedFastNativeObjectAccess soa(env);
168   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
169   ObjPtr<mirror::Object> value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
170   return soa.AddLocalReference<jobject>(value);
171 }
172 
Unsafe_getObject(JNIEnv * env,jobject,jobject javaObj,jlong offset)173 static jobject Unsafe_getObject(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
174   ScopedFastNativeObjectAccess soa(env);
175   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
176   ObjPtr<mirror::Object> value = obj->GetFieldObject<mirror::Object>(MemberOffset(offset));
177   return soa.AddLocalReference<jobject>(value);
178 }
179 
Unsafe_putObject(JNIEnv * env,jobject,jobject javaObj,jlong offset,jobject javaNewValue)180 static void Unsafe_putObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
181                              jobject javaNewValue) {
182   ScopedFastNativeObjectAccess soa(env);
183   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
184   ObjPtr<mirror::Object> newValue = soa.Decode<mirror::Object>(javaNewValue);
185   // JNI must use non transactional mode.
186   obj->SetFieldObject<false>(MemberOffset(offset), newValue);
187 }
188 
Unsafe_putObjectVolatile(JNIEnv * env,jobject,jobject javaObj,jlong offset,jobject javaNewValue)189 static void Unsafe_putObjectVolatile(JNIEnv* env, jobject, jobject javaObj, jlong offset,
190                                      jobject javaNewValue) {
191   ScopedFastNativeObjectAccess soa(env);
192   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
193   ObjPtr<mirror::Object> newValue = soa.Decode<mirror::Object>(javaNewValue);
194   // JNI must use non transactional mode.
195   obj->SetFieldObjectVolatile<false>(MemberOffset(offset), newValue);
196 }
197 
Unsafe_putOrderedObject(JNIEnv * env,jobject,jobject javaObj,jlong offset,jobject javaNewValue)198 static void Unsafe_putOrderedObject(JNIEnv* env, jobject, jobject javaObj, jlong offset,
199                                     jobject javaNewValue) {
200   ScopedFastNativeObjectAccess soa(env);
201   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
202   ObjPtr<mirror::Object> newValue = soa.Decode<mirror::Object>(javaNewValue);
203   std::atomic_thread_fence(std::memory_order_release);
204   // JNI must use non transactional mode.
205   obj->SetFieldObject<false>(MemberOffset(offset), newValue);
206 }
207 
Unsafe_getArrayBaseOffsetForComponentType(JNIEnv * env,jclass,jclass component_class)208 static jint Unsafe_getArrayBaseOffsetForComponentType(JNIEnv* env, jclass, jclass component_class) {
209   ScopedFastNativeObjectAccess soa(env);
210   ObjPtr<mirror::Class> component = soa.Decode<mirror::Class>(component_class);
211   Primitive::Type primitive_type = component->GetPrimitiveType();
212   return mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value();
213 }
214 
Unsafe_getArrayIndexScaleForComponentType(JNIEnv * env,jclass,jclass component_class)215 static jint Unsafe_getArrayIndexScaleForComponentType(JNIEnv* env, jclass, jclass component_class) {
216   ScopedFastNativeObjectAccess soa(env);
217   ObjPtr<mirror::Class> component = soa.Decode<mirror::Class>(component_class);
218   Primitive::Type primitive_type = component->GetPrimitiveType();
219   return Primitive::ComponentSize(primitive_type);
220 }
221 
Unsafe_addressSize(JNIEnv * env,jobject ob)222 static jint Unsafe_addressSize([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jobject ob) {
223   return sizeof(void*);
224 }
225 
Unsafe_pageSize(JNIEnv * env,jobject ob)226 static jint Unsafe_pageSize([[maybe_unused]] JNIEnv* env, [[maybe_unused]] jobject ob) {
227   return sysconf(_SC_PAGESIZE);
228 }
229 
Unsafe_allocateMemory(JNIEnv * env,jobject,jlong bytes)230 static jlong Unsafe_allocateMemory(JNIEnv* env, jobject, jlong bytes) {
231   ScopedFastNativeObjectAccess soa(env);
232   // bytes is nonnegative and fits into size_t
233   if (bytes < 0 || bytes != static_cast<jlong>(static_cast<size_t>(bytes))) {
234     ThrowIllegalAccessException("wrong number of bytes");
235     return 0;
236   }
237   void* mem = malloc(bytes);
238   if (mem == nullptr) {
239     soa.Self()->ThrowOutOfMemoryError("native alloc");
240     return 0;
241   }
242   return (uintptr_t) mem;
243 }
244 
Unsafe_freeMemory(JNIEnv * env,jobject,jlong address)245 static void Unsafe_freeMemory([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
246   free(reinterpret_cast<void*>(static_cast<uintptr_t>(address)));
247 }
248 
Unsafe_setMemory(JNIEnv * env,jobject,jlong address,jlong bytes,jbyte value)249 static void Unsafe_setMemory(
250     [[maybe_unused]] JNIEnv* env, jobject, jlong address, jlong bytes, jbyte value) {
251   memset(reinterpret_cast<void*>(static_cast<uintptr_t>(address)), value, bytes);
252 }
253 
Unsafe_getByteJ(JNIEnv * env,jobject,jlong address)254 static jbyte Unsafe_getByteJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
255   return *reinterpret_cast<jbyte*>(address);
256 }
257 
Unsafe_putByteJB(JNIEnv * env,jobject,jlong address,jbyte value)258 static void Unsafe_putByteJB([[maybe_unused]] JNIEnv* env, jobject, jlong address, jbyte value) {
259   *reinterpret_cast<jbyte*>(address) = value;
260 }
261 
Unsafe_getShortJ(JNIEnv * env,jobject,jlong address)262 static jshort Unsafe_getShortJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
263   return *reinterpret_cast<jshort*>(address);
264 }
265 
Unsafe_putShortJS(JNIEnv * env,jobject,jlong address,jshort value)266 static void Unsafe_putShortJS([[maybe_unused]] JNIEnv* env, jobject, jlong address, jshort value) {
267   *reinterpret_cast<jshort*>(address) = value;
268 }
269 
Unsafe_getCharJ(JNIEnv * env,jobject,jlong address)270 static jchar Unsafe_getCharJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
271   return *reinterpret_cast<jchar*>(address);
272 }
273 
Unsafe_putCharJC(JNIEnv * env,jobject,jlong address,jchar value)274 static void Unsafe_putCharJC([[maybe_unused]] JNIEnv* env, jobject, jlong address, jchar value) {
275   *reinterpret_cast<jchar*>(address) = value;
276 }
277 
Unsafe_getIntJ(JNIEnv * env,jobject,jlong address)278 static jint Unsafe_getIntJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
279   return *reinterpret_cast<jint*>(address);
280 }
281 
Unsafe_putIntJI(JNIEnv * env,jobject,jlong address,jint value)282 static void Unsafe_putIntJI([[maybe_unused]] JNIEnv* env, jobject, jlong address, jint value) {
283   *reinterpret_cast<jint*>(address) = value;
284 }
285 
Unsafe_getLongJ(JNIEnv * env,jobject,jlong address)286 static jlong Unsafe_getLongJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
287   return *reinterpret_cast<jlong*>(address);
288 }
289 
Unsafe_putLongJJ(JNIEnv * env,jobject,jlong address,jlong value)290 static void Unsafe_putLongJJ([[maybe_unused]] JNIEnv* env, jobject, jlong address, jlong value) {
291   *reinterpret_cast<jlong*>(address) = value;
292 }
293 
Unsafe_getFloatJ(JNIEnv * env,jobject,jlong address)294 static jfloat Unsafe_getFloatJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
295   return *reinterpret_cast<jfloat*>(address);
296 }
297 
Unsafe_putFloatJF(JNIEnv * env,jobject,jlong address,jfloat value)298 static void Unsafe_putFloatJF([[maybe_unused]] JNIEnv* env, jobject, jlong address, jfloat value) {
299   *reinterpret_cast<jfloat*>(address) = value;
300 }
Unsafe_getDoubleJ(JNIEnv * env,jobject,jlong address)301 static jdouble Unsafe_getDoubleJ([[maybe_unused]] JNIEnv* env, jobject, jlong address) {
302   return *reinterpret_cast<jdouble*>(address);
303 }
304 
Unsafe_putDoubleJD(JNIEnv * env,jobject,jlong address,jdouble value)305 static void Unsafe_putDoubleJD([[maybe_unused]] JNIEnv* env,
306                                jobject,
307                                jlong address,
308                                jdouble value) {
309   *reinterpret_cast<jdouble*>(address) = value;
310 }
311 
Unsafe_copyMemory(JNIEnv * env,jobject unsafe,jlong src,jlong dst,jlong size)312 static void Unsafe_copyMemory(
313     JNIEnv* env, [[maybe_unused]] jobject unsafe, jlong src, jlong dst, jlong size) {
314   if (size == 0) {
315     return;
316   }
317   // size is nonnegative and fits into size_t
318   if (size < 0 || size != static_cast<jlong>(static_cast<size_t>(size))) {
319     ScopedFastNativeObjectAccess soa(env);
320     ThrowIllegalAccessException("wrong number of bytes");
321   }
322   size_t sz = static_cast<size_t>(size);
323   memcpy(reinterpret_cast<void *>(dst), reinterpret_cast<void *>(src), sz);
324 }
325 
326 template<typename T>
copyToArray(jlong srcAddr,ObjPtr<mirror::PrimitiveArray<T>> array,size_t array_offset,size_t size)327 static void copyToArray(jlong srcAddr,
328                         ObjPtr<mirror::PrimitiveArray<T>> array,
329                         size_t array_offset,
330                         size_t size)
331         REQUIRES_SHARED(Locks::mutator_lock_) {
332   const T* src = reinterpret_cast<T*>(srcAddr);
333   size_t sz = size / sizeof(T);
334   size_t of = array_offset / sizeof(T);
335   for (size_t i = 0; i < sz; ++i) {
336     array->Set(i + of, *(src + i));
337   }
338 }
339 
340 template<typename T>
copyFromArray(jlong dstAddr,ObjPtr<mirror::PrimitiveArray<T>> array,size_t array_offset,size_t size)341 static void copyFromArray(jlong dstAddr,
342                           ObjPtr<mirror::PrimitiveArray<T>> array,
343                           size_t array_offset,
344                           size_t size)
345         REQUIRES_SHARED(Locks::mutator_lock_) {
346   T* dst = reinterpret_cast<T*>(dstAddr);
347   size_t sz = size / sizeof(T);
348   size_t of = array_offset / sizeof(T);
349   for (size_t i = 0; i < sz; ++i) {
350     *(dst + i) = array->Get(i + of);
351   }
352 }
353 
Unsafe_copyMemoryToPrimitiveArray(JNIEnv * env,jobject unsafe,jlong srcAddr,jobject dstObj,jlong dstOffset,jlong size)354 static void Unsafe_copyMemoryToPrimitiveArray(JNIEnv* env,
355                                               [[maybe_unused]] jobject unsafe,
356                                               jlong srcAddr,
357                                               jobject dstObj,
358                                               jlong dstOffset,
359                                               jlong size) {
360   ScopedObjectAccess soa(env);
361   if (size == 0) {
362     return;
363   }
364   // size is nonnegative and fits into size_t
365   if (size < 0 || size != static_cast<jlong>(static_cast<size_t>(size))) {
366     ThrowIllegalAccessException("wrong number of bytes");
367   }
368   size_t sz = static_cast<size_t>(size);
369   size_t dst_offset = static_cast<size_t>(dstOffset);
370   ObjPtr<mirror::Object> dst = soa.Decode<mirror::Object>(dstObj);
371   ObjPtr<mirror::Class> component_type = dst->GetClass()->GetComponentType();
372   if (component_type->IsPrimitiveByte() || component_type->IsPrimitiveBoolean()) {
373     // Note: Treating BooleanArray as ByteArray.
374     copyToArray(srcAddr, ObjPtr<mirror::ByteArray>::DownCast(dst), dst_offset, sz);
375   } else if (component_type->IsPrimitiveShort() || component_type->IsPrimitiveChar()) {
376     // Note: Treating CharArray as ShortArray.
377     copyToArray(srcAddr, ObjPtr<mirror::ShortArray>::DownCast(dst), dst_offset, sz);
378   } else if (component_type->IsPrimitiveInt() || component_type->IsPrimitiveFloat()) {
379     // Note: Treating FloatArray as IntArray.
380     copyToArray(srcAddr, ObjPtr<mirror::IntArray>::DownCast(dst), dst_offset, sz);
381   } else if (component_type->IsPrimitiveLong() || component_type->IsPrimitiveDouble()) {
382     // Note: Treating DoubleArray as LongArray.
383     copyToArray(srcAddr, ObjPtr<mirror::LongArray>::DownCast(dst), dst_offset, sz);
384   } else {
385     ThrowIllegalAccessException("not a primitive array");
386   }
387 }
388 
Unsafe_copyMemoryFromPrimitiveArray(JNIEnv * env,jobject unsafe,jobject srcObj,jlong srcOffset,jlong dstAddr,jlong size)389 static void Unsafe_copyMemoryFromPrimitiveArray(JNIEnv* env,
390                                                 [[maybe_unused]] jobject unsafe,
391                                                 jobject srcObj,
392                                                 jlong srcOffset,
393                                                 jlong dstAddr,
394                                                 jlong size) {
395   ScopedObjectAccess soa(env);
396   if (size == 0) {
397     return;
398   }
399   // size is nonnegative and fits into size_t
400   if (size < 0 || size != static_cast<jlong>(static_cast<size_t>(size))) {
401     ThrowIllegalAccessException("wrong number of bytes");
402   }
403   size_t sz = static_cast<size_t>(size);
404   size_t src_offset = static_cast<size_t>(srcOffset);
405   ObjPtr<mirror::Object> src = soa.Decode<mirror::Object>(srcObj);
406   ObjPtr<mirror::Class> component_type = src->GetClass()->GetComponentType();
407   if (component_type->IsPrimitiveByte() || component_type->IsPrimitiveBoolean()) {
408     // Note: Treating BooleanArray as ByteArray.
409     copyFromArray(dstAddr, ObjPtr<mirror::ByteArray>::DownCast(src), src_offset, sz);
410   } else if (component_type->IsPrimitiveShort() || component_type->IsPrimitiveChar()) {
411     // Note: Treating CharArray as ShortArray.
412     copyFromArray(dstAddr, ObjPtr<mirror::ShortArray>::DownCast(src), src_offset, sz);
413   } else if (component_type->IsPrimitiveInt() || component_type->IsPrimitiveFloat()) {
414     // Note: Treating FloatArray as IntArray.
415     copyFromArray(dstAddr, ObjPtr<mirror::IntArray>::DownCast(src), src_offset, sz);
416   } else if (component_type->IsPrimitiveLong() || component_type->IsPrimitiveDouble()) {
417     // Note: Treating DoubleArray as LongArray.
418     copyFromArray(dstAddr, ObjPtr<mirror::LongArray>::DownCast(src), src_offset, sz);
419   } else {
420     ThrowIllegalAccessException("not a primitive array");
421   }
422 }
Unsafe_getBoolean(JNIEnv * env,jobject,jobject javaObj,jlong offset)423 static jboolean Unsafe_getBoolean(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
424   ScopedFastNativeObjectAccess soa(env);
425   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
426   return obj->GetFieldBoolean(MemberOffset(offset));
427 }
428 
Unsafe_putBoolean(JNIEnv * env,jobject,jobject javaObj,jlong offset,jboolean newValue)429 static void Unsafe_putBoolean(JNIEnv* env, jobject, jobject javaObj, jlong offset, jboolean newValue) {
430   ScopedFastNativeObjectAccess soa(env);
431   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
432   // JNI must use non transactional mode (SetField8 is non-transactional).
433   obj->SetFieldBoolean<false>(MemberOffset(offset), newValue);
434 }
435 
Unsafe_getByte(JNIEnv * env,jobject,jobject javaObj,jlong offset)436 static jbyte Unsafe_getByte(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
437   ScopedFastNativeObjectAccess soa(env);
438   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
439   return obj->GetFieldByte(MemberOffset(offset));
440 }
441 
Unsafe_putByte(JNIEnv * env,jobject,jobject javaObj,jlong offset,jbyte newValue)442 static void Unsafe_putByte(JNIEnv* env, jobject, jobject javaObj, jlong offset, jbyte newValue) {
443   ScopedFastNativeObjectAccess soa(env);
444   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
445   // JNI must use non transactional mode.
446   obj->SetFieldByte<false>(MemberOffset(offset), newValue);
447 }
448 
Unsafe_getChar(JNIEnv * env,jobject,jobject javaObj,jlong offset)449 static jchar Unsafe_getChar(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
450   ScopedFastNativeObjectAccess soa(env);
451   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
452   return obj->GetFieldChar(MemberOffset(offset));
453 }
454 
Unsafe_putChar(JNIEnv * env,jobject,jobject javaObj,jlong offset,jchar newValue)455 static void Unsafe_putChar(JNIEnv* env, jobject, jobject javaObj, jlong offset, jchar newValue) {
456   ScopedFastNativeObjectAccess soa(env);
457   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
458   // JNI must use non transactional mode.
459   obj->SetFieldChar<false>(MemberOffset(offset), newValue);
460 }
461 
Unsafe_getShort(JNIEnv * env,jobject,jobject javaObj,jlong offset)462 static jshort Unsafe_getShort(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
463   ScopedFastNativeObjectAccess soa(env);
464   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
465   return obj->GetFieldShort(MemberOffset(offset));
466 }
467 
Unsafe_putShort(JNIEnv * env,jobject,jobject javaObj,jlong offset,jshort newValue)468 static void Unsafe_putShort(JNIEnv* env, jobject, jobject javaObj, jlong offset, jshort newValue) {
469   ScopedFastNativeObjectAccess soa(env);
470   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
471   // JNI must use non transactional mode.
472   obj->SetFieldShort<false>(MemberOffset(offset), newValue);
473 }
474 
Unsafe_getFloat(JNIEnv * env,jobject,jobject javaObj,jlong offset)475 static jfloat Unsafe_getFloat(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
476   ScopedFastNativeObjectAccess soa(env);
477   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
478   union {int32_t val; jfloat converted;} conv;
479   conv.val = obj->GetField32(MemberOffset(offset));
480   return conv.converted;
481 }
482 
Unsafe_putFloat(JNIEnv * env,jobject,jobject javaObj,jlong offset,jfloat newValue)483 static void Unsafe_putFloat(JNIEnv* env, jobject, jobject javaObj, jlong offset, jfloat newValue) {
484   ScopedFastNativeObjectAccess soa(env);
485   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
486   union {int32_t converted; jfloat val;} conv;
487   conv.val = newValue;
488   // JNI must use non transactional mode.
489   obj->SetField32<false>(MemberOffset(offset), conv.converted);
490 }
491 
Unsafe_getDouble(JNIEnv * env,jobject,jobject javaObj,jlong offset)492 static jdouble Unsafe_getDouble(JNIEnv* env, jobject, jobject javaObj, jlong offset) {
493   ScopedFastNativeObjectAccess soa(env);
494   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
495   union {int64_t val; jdouble converted;} conv;
496   conv.val = obj->GetField64(MemberOffset(offset));
497   return conv.converted;
498 }
499 
Unsafe_putDouble(JNIEnv * env,jobject,jobject javaObj,jlong offset,jdouble newValue)500 static void Unsafe_putDouble(JNIEnv* env, jobject, jobject javaObj, jlong offset, jdouble newValue) {
501   ScopedFastNativeObjectAccess soa(env);
502   ObjPtr<mirror::Object> obj = soa.Decode<mirror::Object>(javaObj);
503   union {int64_t converted; jdouble val;} conv;
504   conv.val = newValue;
505   // JNI must use non transactional mode.
506   obj->SetField64<false>(MemberOffset(offset), conv.converted);
507 }
508 
Unsafe_loadFence(JNIEnv *,jobject)509 static void Unsafe_loadFence(JNIEnv*, jobject) {
510   std::atomic_thread_fence(std::memory_order_acquire);
511 }
512 
Unsafe_storeFence(JNIEnv *,jobject)513 static void Unsafe_storeFence(JNIEnv*, jobject) {
514   std::atomic_thread_fence(std::memory_order_release);
515 }
516 
Unsafe_fullFence(JNIEnv *,jobject)517 static void Unsafe_fullFence(JNIEnv*, jobject) {
518   std::atomic_thread_fence(std::memory_order_seq_cst);
519 }
520 
Unsafe_park(JNIEnv * env,jobject,jboolean isAbsolute,jlong time)521 static void Unsafe_park(JNIEnv* env, jobject, jboolean isAbsolute, jlong time) {
522   ScopedObjectAccess soa(env);
523   Thread::Current()->Park(isAbsolute, time);
524 }
525 
Unsafe_unpark(JNIEnv * env,jobject,jobject jthread)526 static void Unsafe_unpark(JNIEnv* env, jobject, jobject jthread) {
527   art::ScopedFastNativeObjectAccess soa(env);
528   ObjPtr<mirror::Object> mirror_thread = soa.Decode<mirror::Object>(jthread);
529   if (mirror_thread == nullptr ||
530       !mirror_thread->InstanceOf(WellKnownClasses::java_lang_Thread.Get())) {
531     ThrowIllegalArgumentException("Argument to unpark() was not a Thread");
532     return;
533   }
534   Thread* self = soa.Self();
535   art::MutexLock mu(self, *art::Locks::thread_list_lock_);
536   art::Thread* thread = art::Thread::FromManagedThread(self, mirror_thread);
537   if (thread != nullptr) {
538     thread->Unpark();
539   } else {
540     // If thread is null, that means that either the thread is not started yet,
541     // or the thread has already terminated. Setting the field to true will be
542     // respected when the thread does start, and is harmless if the thread has
543     // already terminated.
544     ArtField* unparked = WellKnownClasses::java_lang_Thread_unparkedBeforeStart;
545     // JNI must use non transactional mode.
546     unparked->SetBoolean<false>(mirror_thread, JNI_TRUE);
547   }
548 }
549 
550 static JNINativeMethod gMethods[] = {
551   FAST_NATIVE_METHOD(Unsafe, compareAndSwapInt, "(Ljava/lang/Object;JII)Z"),
552   FAST_NATIVE_METHOD(Unsafe, compareAndSwapLong, "(Ljava/lang/Object;JJJ)Z"),
553   FAST_NATIVE_METHOD(Unsafe, compareAndSwapObject, "(Ljava/lang/Object;JLjava/lang/Object;Ljava/lang/Object;)Z"),
554   FAST_NATIVE_METHOD(Unsafe, getIntVolatile, "(Ljava/lang/Object;J)I"),
555   FAST_NATIVE_METHOD(Unsafe, putIntVolatile, "(Ljava/lang/Object;JI)V"),
556   FAST_NATIVE_METHOD(Unsafe, getLongVolatile, "(Ljava/lang/Object;J)J"),
557   FAST_NATIVE_METHOD(Unsafe, putLongVolatile, "(Ljava/lang/Object;JJ)V"),
558   FAST_NATIVE_METHOD(Unsafe, getObjectVolatile, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
559   FAST_NATIVE_METHOD(Unsafe, putObjectVolatile, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
560   FAST_NATIVE_METHOD(Unsafe, getInt, "(Ljava/lang/Object;J)I"),
561   FAST_NATIVE_METHOD(Unsafe, putInt, "(Ljava/lang/Object;JI)V"),
562   FAST_NATIVE_METHOD(Unsafe, putOrderedInt, "(Ljava/lang/Object;JI)V"),
563   FAST_NATIVE_METHOD(Unsafe, getLong, "(Ljava/lang/Object;J)J"),
564   FAST_NATIVE_METHOD(Unsafe, putLong, "(Ljava/lang/Object;JJ)V"),
565   FAST_NATIVE_METHOD(Unsafe, putOrderedLong, "(Ljava/lang/Object;JJ)V"),
566   FAST_NATIVE_METHOD(Unsafe, getObject, "(Ljava/lang/Object;J)Ljava/lang/Object;"),
567   FAST_NATIVE_METHOD(Unsafe, putObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
568   FAST_NATIVE_METHOD(Unsafe, putOrderedObject, "(Ljava/lang/Object;JLjava/lang/Object;)V"),
569   FAST_NATIVE_METHOD(Unsafe, getArrayBaseOffsetForComponentType, "(Ljava/lang/Class;)I"),
570   FAST_NATIVE_METHOD(Unsafe, getArrayIndexScaleForComponentType, "(Ljava/lang/Class;)I"),
571   FAST_NATIVE_METHOD(Unsafe, addressSize, "()I"),
572   FAST_NATIVE_METHOD(Unsafe, pageSize, "()I"),
573   FAST_NATIVE_METHOD(Unsafe, allocateMemory, "(J)J"),
574   FAST_NATIVE_METHOD(Unsafe, freeMemory, "(J)V"),
575   FAST_NATIVE_METHOD(Unsafe, setMemory, "(JJB)V"),
576   FAST_NATIVE_METHOD(Unsafe, copyMemory, "(JJJ)V"),
577   FAST_NATIVE_METHOD(Unsafe, copyMemoryToPrimitiveArray, "(JLjava/lang/Object;JJ)V"),
578   FAST_NATIVE_METHOD(Unsafe, copyMemoryFromPrimitiveArray, "(Ljava/lang/Object;JJJ)V"),
579   FAST_NATIVE_METHOD(Unsafe, getBoolean, "(Ljava/lang/Object;J)Z"),
580 
581   FAST_NATIVE_METHOD(Unsafe, getByte, "(Ljava/lang/Object;J)B"),
582   FAST_NATIVE_METHOD(Unsafe, getChar, "(Ljava/lang/Object;J)C"),
583   FAST_NATIVE_METHOD(Unsafe, getShort, "(Ljava/lang/Object;J)S"),
584   FAST_NATIVE_METHOD(Unsafe, getFloat, "(Ljava/lang/Object;J)F"),
585   FAST_NATIVE_METHOD(Unsafe, getDouble, "(Ljava/lang/Object;J)D"),
586   FAST_NATIVE_METHOD(Unsafe, putBoolean, "(Ljava/lang/Object;JZ)V"),
587   FAST_NATIVE_METHOD(Unsafe, putByte, "(Ljava/lang/Object;JB)V"),
588   FAST_NATIVE_METHOD(Unsafe, putChar, "(Ljava/lang/Object;JC)V"),
589   FAST_NATIVE_METHOD(Unsafe, putShort, "(Ljava/lang/Object;JS)V"),
590   FAST_NATIVE_METHOD(Unsafe, putFloat, "(Ljava/lang/Object;JF)V"),
591   FAST_NATIVE_METHOD(Unsafe, putDouble, "(Ljava/lang/Object;JD)V"),
592   FAST_NATIVE_METHOD(Unsafe, unpark, "(Ljava/lang/Object;)V"),
593   NATIVE_METHOD(Unsafe, park, "(ZJ)V"),
594 
595   // Each of the getFoo variants are overloaded with a call that operates
596   // directively on a native pointer.
597   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getByte, "(J)B", getByteJ),
598   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getChar, "(J)C", getCharJ),
599   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getShort, "(J)S", getShortJ),
600   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getInt, "(J)I", getIntJ),
601   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getLong, "(J)J", getLongJ),
602   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getFloat, "(J)F", getFloatJ),
603   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, getDouble, "(J)D", getDoubleJ),
604   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putByte, "(JB)V", putByteJB),
605   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putChar, "(JC)V", putCharJC),
606   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putShort, "(JS)V", putShortJS),
607   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putInt, "(JI)V", putIntJI),
608   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putLong, "(JJ)V", putLongJJ),
609   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putFloat, "(JF)V", putFloatJF),
610   OVERLOADED_FAST_NATIVE_METHOD(Unsafe, putDouble, "(JD)V", putDoubleJD),
611 
612   // CAS
613   FAST_NATIVE_METHOD(Unsafe, loadFence, "()V"),
614   FAST_NATIVE_METHOD(Unsafe, storeFence, "()V"),
615   FAST_NATIVE_METHOD(Unsafe, fullFence, "()V"),
616 };
617 
register_sun_misc_Unsafe(JNIEnv * env)618 void register_sun_misc_Unsafe(JNIEnv* env) {
619   REGISTER_NATIVE_METHODS("sun/misc/Unsafe");
620 }
621 
622 }  // namespace art
623