blob: b48cee99413ad208dd6b2393151d009766907ca7 [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
17#include "jni_internal.h"
18#include "object.h"
19
20#include "JniConstants.h" // Last to avoid problems with LOG redefinition.
21
22/*
23 * We make guarantees about the atomicity of accesses to primitive
24 * variables. These guarantees also apply to elements of arrays.
25 * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and
26 * must not cause "word tearing". Accesses to 64-bit array elements must
27 * either be atomic or treated as two 32-bit operations. References are
28 * always read and written atomically, regardless of the number of bits
29 * used to represent them.
30 *
31 * We can't rely on standard libc functions like memcpy(3) and memmove(3)
32 * in our implementation of System.arraycopy, because they may copy
33 * byte-by-byte (either for the full run or for "unaligned" parts at the
34 * start or end). We need to use functions that guarantee 16-bit or 32-bit
35 * atomicity as appropriate.
36 *
37 * System.arraycopy() is heavily used, so having an efficient implementation
38 * is important. The bionic libc provides a platform-optimized memory move
39 * function that should be used when possible. If it's not available,
40 * the trivial "reference implementation" versions below can be used until
41 * a proper version can be written.
42 *
43 * For these functions, The caller must guarantee that dst/src are aligned
44 * appropriately for the element type, and that n is a multiple of the
45 * element size.
46 */
47#ifdef __BIONIC__
48#define HAVE_MEMMOVE_WORDS
49#endif
50
51#ifdef HAVE_MEMMOVE_WORDS
52extern "C" void _memmove_words(void* dst, const void* src, size_t n);
53#define move16 _memmove_words
54#define move32 _memmove_words
55#else
56static void move16(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070057 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070058
59 uint16_t* d = reinterpret_cast<uint16_t*>(dst);
60 const uint16_t* s = reinterpret_cast<const uint16_t*>(src);
61
62 n /= sizeof(uint16_t);
63
64 if (d < s) {
65 // Copy forwards.
66 while (n--) {
67 *d++ = *s++;
68 }
69 } else {
70 // Copy backwards.
71 d += n;
72 s += n;
73 while (n--) {
74 *--d = *--s;
75 }
76 }
77}
78
79static void move32(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070080 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070081
82 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
83 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
84
85 n /= sizeof(uint32_t);
86
87 if (d < s) {
88 // Copy forwards.
89 while (n--) {
90 *d++ = *s++;
91 }
92 } else {
93 // Copy backwards.
94 d += n;
95 s += n;
96 while (n--) {
97 *--d = *--s;
98 }
99 }
100}
101#endif // HAVE_MEMMOVE_WORDS
102
103namespace art {
104
105namespace {
106
107void ThrowArrayStoreException_NotAnArray(const char* identifier, Object* array) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700108 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700109 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800110 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111}
112
113void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Brian Carlstromb82b6872011-10-26 17:18:07 -0700114 ScopedThreadStateChange tsc(Thread::Current(), Thread::kRunnable);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700115 Thread* self = Thread::Current();
116
117 // Null pointer checks.
118 if (javaSrc == NULL) {
119 self->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
120 return;
121 }
122 if (javaDst == NULL) {
123 self->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
124 return;
125 }
126
127 // Make sure source and destination are both arrays.
128 Object* srcObject = Decode<Object*>(env, javaSrc);
129 Object* dstObject = Decode<Object*>(env, javaDst);
130 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800131 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132 return;
133 }
134 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800135 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700136 return;
137 }
138 Array* srcArray = srcObject->AsArray();
139 Array* dstArray = dstObject->AsArray();
140 Class* srcComponentType = srcArray->GetClass()->GetComponentType();
141 Class* dstComponentType = dstArray->GetClass()->GetComponentType();
142
143 // Bounds checking.
144 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700145 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700146 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
147 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
148 return;
149 }
150
Elliott Hughesbf86d042011-08-31 17:53:14 -0700151 // Handle primitive arrays.
152 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
153 // If one of the arrays holds a primitive type the other array must hold the exact same type.
154 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700155 std::string srcType(PrettyTypeOf(srcArray));
156 std::string dstType(PrettyTypeOf(dstArray));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700157 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700158 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
159 return;
160 }
161
Ian Rogersa15e67d2012-02-28 13:51:55 -0800162 size_t width = srcArray->GetClass()->GetComponentSize();
163 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
164 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
165
166 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700167 case 1:
168 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
169 break;
170 case 2:
171 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
172 break;
173 case 4:
174 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
175 break;
176 case 8:
177 // We don't need to guarantee atomicity of the entire 64-bit word.
178 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
179 break;
180 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700181 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700182 }
183
184 return;
185 }
186
187 // Neither class is primitive. Are the types trivially compatible?
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700188 const size_t width = sizeof(Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800189 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
190 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800191 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700192 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700193 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700194 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Ian Rogers5d76c432011-10-31 21:42:49 -0700195 Heap::WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700196 return;
197 }
198
Elliott Hughesab3530d2012-01-09 16:04:56 -0800199 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
200 // the elements if the source objects are compatible (for example, copying an Object[] to
201 // String[], the Objects being copied might actually be Strings).
202 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
203 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700204
Elliott Hughesab3530d2012-01-09 16:04:56 -0800205 // We already dealt with overlapping copies, so we don't need to cope with that case below.
206 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700207
Elliott Hughesab3530d2012-01-09 16:04:56 -0800208 Object* const * srcObjects = reinterpret_cast<Object* const *>(srcBytes + srcPos * width);
209 Object** dstObjects = reinterpret_cast<Object**>(dstBytes + dstPos * width);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700210 Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700211
Elliott Hughesab3530d2012-01-09 16:04:56 -0800212 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
213 // we know is assignable to the destination array's component type.
214 Class* lastAssignableElementClass = dstClass;
215
Elliott Hughes025c5de2012-01-10 11:05:48 -0800216 Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800217 int i = 0;
218 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800219 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800220 if (o != NULL) {
221 Class* oClass = o->GetClass();
222 if (lastAssignableElementClass == oClass) {
223 dstObjects[i] = o;
224 } else if (dstClass->IsAssignableFrom(oClass)) {
225 lastAssignableElementClass = oClass;
226 dstObjects[i] = o;
227 } else {
228 // Can't put this element into the array.
229 break;
230 }
231 } else {
232 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700233 }
234 }
235
Ian Rogers5d76c432011-10-31 21:42:49 -0700236 Heap::WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800237 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800238 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700239 std::string dstType(PrettyTypeOf(dstArray));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700240 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700241 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800242 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700243 return;
244 }
245}
246
247jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
248 Object* o = Decode<Object*>(env, javaObject);
249 return static_cast<jint>(reinterpret_cast<uintptr_t>(o));
250}
251
252JNINativeMethod gMethods[] = {
253 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
254 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
255};
256
257} // namespace
258
259void register_java_lang_System(JNIEnv* env) {
260 jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods));
261}
262
263} // namespace art