blob: 5572623a0c3bc2ab103d9dd1f68481baaf40d12d [file] [log] [blame]
Elliott Hughesbf86d042011-08-31 17:53:14 -07001/*
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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#include "gc/card_table-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070018#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/array.h"
20#include "mirror/class.h"
21#include "mirror/class-inl.h"
22#include "mirror/object-inl.h"
Ian Rogers693ff612013-02-01 10:56:12 -080023#include "mirror/object_array-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070025
Elliott Hughesbf86d042011-08-31 17:53:14 -070026/*
27 * We make guarantees about the atomicity of accesses to primitive
28 * variables. These guarantees also apply to elements of arrays.
29 * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and
30 * must not cause "word tearing". Accesses to 64-bit array elements must
31 * either be atomic or treated as two 32-bit operations. References are
32 * always read and written atomically, regardless of the number of bits
33 * used to represent them.
34 *
35 * We can't rely on standard libc functions like memcpy(3) and memmove(3)
36 * in our implementation of System.arraycopy, because they may copy
37 * byte-by-byte (either for the full run or for "unaligned" parts at the
38 * start or end). We need to use functions that guarantee 16-bit or 32-bit
39 * atomicity as appropriate.
40 *
41 * System.arraycopy() is heavily used, so having an efficient implementation
42 * is important. The bionic libc provides a platform-optimized memory move
43 * function that should be used when possible. If it's not available,
44 * the trivial "reference implementation" versions below can be used until
45 * a proper version can be written.
46 *
47 * For these functions, The caller must guarantee that dst/src are aligned
48 * appropriately for the element type, and that n is a multiple of the
49 * element size.
50 */
Elliott Hughesbf86d042011-08-31 17:53:14 -070051
Elliott Hughes6324e212013-02-15 17:55:35 -080052/*
53 * Works like memmove(), except:
54 * - if all arguments are at least 32-bit aligned, we guarantee that we
55 * will use operations that preserve atomicity of 32-bit values
56 * - if not, we guarantee atomicity of 16-bit values
57 *
58 * If all three arguments are not at least 16-bit aligned, the behavior
59 * of this function is undefined. (We could remove this restriction by
60 * testing for unaligned values and punting to memmove(), but that's
61 * not currently useful.)
62 *
63 * TODO: add loop for 64-bit alignment
64 * TODO: use __builtin_prefetch
65 * TODO: write ARM/MIPS/x86 optimized versions
66 */
67void MemmoveWords(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070068 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070069
Elliott Hughes6324e212013-02-15 17:55:35 -080070 char* d = reinterpret_cast<char*>(dst);
71 const char* s = reinterpret_cast<const char*>(src);
72 size_t copyCount;
Elliott Hughesbf86d042011-08-31 17:53:14 -070073
Elliott Hughes6324e212013-02-15 17:55:35 -080074 // If the source and destination pointers are the same, this is
75 // an expensive no-op. Testing for an empty move now allows us
76 // to skip a check later.
77 if (n == 0 || d == s) {
78 return;
79 }
Elliott Hughesbf86d042011-08-31 17:53:14 -070080
Elliott Hughes6324e212013-02-15 17:55:35 -080081 // Determine if the source and destination buffers will overlap if
82 // we copy data forward (i.e. *dst++ = *src++).
83 //
84 // It's okay if the destination buffer starts before the source and
85 // there is some overlap, because the reader is always ahead of the
86 // writer.
87 if (LIKELY((d < s) || ((size_t)(d - s) >= n))) {
88 // Copy forward. We prefer 32-bit loads and stores even for 16-bit
89 // data, so sort that out.
90 if (((reinterpret_cast<uintptr_t>(d) | reinterpret_cast<uintptr_t>(s)) & 0x03) != 0) {
91 // Not 32-bit aligned. Two possibilities:
92 // (1) Congruent, we can align to 32-bit by copying one 16-bit val
93 // (2) Non-congruent, we can do one of:
94 // a. copy whole buffer as a series of 16-bit values
95 // b. load/store 32 bits, using shifts to ensure alignment
96 // c. just copy the as 32-bit values and assume the CPU
97 // will do a reasonable job
98 //
99 // We're currently using (a), which is suboptimal.
100 if (((reinterpret_cast<uintptr_t>(d) ^ reinterpret_cast<uintptr_t>(s)) & 0x03) != 0) {
101 copyCount = n;
102 } else {
103 copyCount = 2;
104 }
105 n -= copyCount;
106 copyCount /= sizeof(uint16_t);
107
108 while (copyCount--) {
109 *reinterpret_cast<uint16_t*>(d) = *reinterpret_cast<const uint16_t*>(s);
110 d += sizeof(uint16_t);
111 s += sizeof(uint16_t);
112 }
113 }
114
115 // Copy 32-bit aligned words.
116 copyCount = n / sizeof(uint32_t);
117 while (copyCount--) {
118 *reinterpret_cast<uint32_t*>(d) = *reinterpret_cast<const uint32_t*>(s);
119 d += sizeof(uint32_t);
120 s += sizeof(uint32_t);
121 }
122
123 // Check for leftovers. Either we finished exactly, or we have one remaining 16-bit chunk.
124 if ((n & 0x02) != 0) {
125 *(uint16_t*)d = *(uint16_t*)s;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126 }
127 } else {
Elliott Hughes6324e212013-02-15 17:55:35 -0800128 // Copy backward, starting at the end.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129 d += n;
130 s += n;
Elliott Hughes6324e212013-02-15 17:55:35 -0800131
132 if (((reinterpret_cast<uintptr_t>(d) | reinterpret_cast<uintptr_t>(s)) & 0x03) != 0) {
133 // try for 32-bit alignment.
134 if (((reinterpret_cast<uintptr_t>(d) ^ reinterpret_cast<uintptr_t>(s)) & 0x03) != 0) {
135 copyCount = n;
136 } else {
137 copyCount = 2;
138 }
139 n -= copyCount;
140 copyCount /= sizeof(uint16_t);
141
142 while (copyCount--) {
143 d -= sizeof(uint16_t);
144 s -= sizeof(uint16_t);
145 *reinterpret_cast<uint16_t*>(d) = *reinterpret_cast<const uint16_t*>(s);
146 }
147 }
148
149 // Copy 32-bit aligned words.
150 copyCount = n / sizeof(uint32_t);
151 while (copyCount--) {
152 d -= sizeof(uint32_t);
153 s -= sizeof(uint32_t);
154 *reinterpret_cast<uint32_t*>(d) = *reinterpret_cast<const uint32_t*>(s);
155 }
156
157 // Copy leftovers.
158 if ((n & 0x02) != 0) {
159 d -= sizeof(uint16_t);
160 s -= sizeof(uint16_t);
161 *reinterpret_cast<uint16_t*>(d) = *reinterpret_cast<const uint16_t*>(s);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700162 }
163 }
164}
165
Elliott Hughes6324e212013-02-15 17:55:35 -0800166#define move16 MemmoveWords
167#define move32 MemmoveWords
Elliott Hughesbf86d042011-08-31 17:53:14 -0700168
169namespace art {
170
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800171static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700172 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700173 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700174 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800175 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700176}
177
Elliott Hughes0512f022012-03-15 22:10:52 -0700178static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700179 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700180
181 // Null pointer checks.
182 if (javaSrc == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700183 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700184 return;
185 }
186 if (javaDst == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700187 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700188 return;
189 }
190
191 // Make sure source and destination are both arrays.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800192 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
193 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700194 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800195 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700196 return;
197 }
198 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800199 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700200 return;
201 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 mirror::Array* srcArray = srcObject->AsArray();
203 mirror::Array* dstArray = dstObject->AsArray();
204 mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType();
205 mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700206
207 // Bounds checking.
208 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700209 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700210 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
211 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
212 return;
213 }
214
Elliott Hughesbf86d042011-08-31 17:53:14 -0700215 // Handle primitive arrays.
216 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
217 // If one of the arrays holds a primitive type the other array must hold the exact same type.
218 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700219 std::string srcType(PrettyTypeOf(srcArray));
220 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700221 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700222 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
223 return;
224 }
225
Ian Rogersa15e67d2012-02-28 13:51:55 -0800226 size_t width = srcArray->GetClass()->GetComponentSize();
227 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
228 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
229
230 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700231 case 1:
232 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
233 break;
234 case 2:
235 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
236 break;
237 case 4:
238 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
239 break;
240 case 8:
241 // We don't need to guarantee atomicity of the entire 64-bit word.
242 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
243 break;
244 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700245 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700246 }
247
248 return;
249 }
250
251 // Neither class is primitive. Are the types trivially compatible?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 const size_t width = sizeof(mirror::Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800253 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
254 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800255 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700256 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700257 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700258 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800259 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700260 return;
261 }
262
Elliott Hughesab3530d2012-01-09 16:04:56 -0800263 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
264 // the elements if the source objects are compatible (for example, copying an Object[] to
265 // String[], the Objects being copied might actually be Strings).
266 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
267 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700268
Elliott Hughesab3530d2012-01-09 16:04:56 -0800269 // We already dealt with overlapping copies, so we don't need to cope with that case below.
270 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700271
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800272 mirror::Object* const * srcObjects =
273 reinterpret_cast<mirror::Object* const *>(srcBytes + srcPos * width);
274 mirror::Object** dstObjects = reinterpret_cast<mirror::Object**>(dstBytes + dstPos * width);
275 mirror::Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700276
Elliott Hughesab3530d2012-01-09 16:04:56 -0800277 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
278 // we know is assignable to the destination array's component type.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800279 mirror::Class* lastAssignableElementClass = dstClass;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800280
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800281 mirror::Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800282 int i = 0;
283 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800284 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800285 if (o != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800286 mirror::Class* oClass = o->GetClass();
Elliott Hughesab3530d2012-01-09 16:04:56 -0800287 if (lastAssignableElementClass == oClass) {
288 dstObjects[i] = o;
289 } else if (dstClass->IsAssignableFrom(oClass)) {
290 lastAssignableElementClass = oClass;
291 dstObjects[i] = o;
292 } else {
293 // Can't put this element into the array.
294 break;
295 }
296 } else {
297 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700298 }
299 }
300
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800301 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800302 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800303 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700304 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700305 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700306 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800307 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700308 return;
309 }
310}
311
Elliott Hughes0512f022012-03-15 22:10:52 -0700312static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700313 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800314 mirror::Object* o = soa.Decode<mirror::Object*>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700315 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700316}
317
Elliott Hughes0512f022012-03-15 22:10:52 -0700318static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700319 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
320 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
321};
322
Elliott Hughesbf86d042011-08-31 17:53:14 -0700323void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700324 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700325}
326
327} // namespace art