blob: b0d1eec6dc64869835d895c2abcbade7992ef6f6 [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
Elliott Hughesbf86d042011-08-31 17:53:14 -070020/*
21 * We make guarantees about the atomicity of accesses to primitive
22 * variables. These guarantees also apply to elements of arrays.
23 * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and
24 * must not cause "word tearing". Accesses to 64-bit array elements must
25 * either be atomic or treated as two 32-bit operations. References are
26 * always read and written atomically, regardless of the number of bits
27 * used to represent them.
28 *
29 * We can't rely on standard libc functions like memcpy(3) and memmove(3)
30 * in our implementation of System.arraycopy, because they may copy
31 * byte-by-byte (either for the full run or for "unaligned" parts at the
32 * start or end). We need to use functions that guarantee 16-bit or 32-bit
33 * atomicity as appropriate.
34 *
35 * System.arraycopy() is heavily used, so having an efficient implementation
36 * is important. The bionic libc provides a platform-optimized memory move
37 * function that should be used when possible. If it's not available,
38 * the trivial "reference implementation" versions below can be used until
39 * a proper version can be written.
40 *
41 * For these functions, The caller must guarantee that dst/src are aligned
42 * appropriately for the element type, and that n is a multiple of the
43 * element size.
44 */
45#ifdef __BIONIC__
46#define HAVE_MEMMOVE_WORDS
47#endif
48
49#ifdef HAVE_MEMMOVE_WORDS
50extern "C" void _memmove_words(void* dst, const void* src, size_t n);
51#define move16 _memmove_words
52#define move32 _memmove_words
53#else
54static void move16(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070055 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070056
57 uint16_t* d = reinterpret_cast<uint16_t*>(dst);
58 const uint16_t* s = reinterpret_cast<const uint16_t*>(src);
59
60 n /= sizeof(uint16_t);
61
62 if (d < s) {
63 // Copy forwards.
64 while (n--) {
65 *d++ = *s++;
66 }
67 } else {
68 // Copy backwards.
69 d += n;
70 s += n;
71 while (n--) {
72 *--d = *--s;
73 }
74 }
75}
76
77static void move32(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070078 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070079
80 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
81 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
82
83 n /= sizeof(uint32_t);
84
85 if (d < s) {
86 // Copy forwards.
87 while (n--) {
88 *d++ = *s++;
89 }
90 } else {
91 // Copy backwards.
92 d += n;
93 s += n;
94 while (n--) {
95 *--d = *--s;
96 }
97 }
98}
99#endif // HAVE_MEMMOVE_WORDS
100
101namespace art {
102
Elliott Hughes0512f022012-03-15 22:10:52 -0700103static void ThrowArrayStoreException_NotAnArray(const char* identifier, Object* array) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700104 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700105 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800106 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700107}
108
Elliott Hughes0512f022012-03-15 22:10:52 -0700109static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Elliott Hughes34e06962012-04-09 13:55:55 -0700110 ScopedThreadStateChange tsc(Thread::Current(), kRunnable);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700111 Thread* self = Thread::Current();
112
113 // Null pointer checks.
114 if (javaSrc == NULL) {
115 self->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
116 return;
117 }
118 if (javaDst == NULL) {
119 self->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
120 return;
121 }
122
123 // Make sure source and destination are both arrays.
124 Object* srcObject = Decode<Object*>(env, javaSrc);
125 Object* dstObject = Decode<Object*>(env, javaDst);
126 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800127 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700128 return;
129 }
130 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800131 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132 return;
133 }
134 Array* srcArray = srcObject->AsArray();
135 Array* dstArray = dstObject->AsArray();
136 Class* srcComponentType = srcArray->GetClass()->GetComponentType();
137 Class* dstComponentType = dstArray->GetClass()->GetComponentType();
138
139 // Bounds checking.
140 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700141 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
143 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
144 return;
145 }
146
Elliott Hughesbf86d042011-08-31 17:53:14 -0700147 // Handle primitive arrays.
148 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
149 // If one of the arrays holds a primitive type the other array must hold the exact same type.
150 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700151 std::string srcType(PrettyTypeOf(srcArray));
152 std::string dstType(PrettyTypeOf(dstArray));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700153 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700154 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
155 return;
156 }
157
Ian Rogersa15e67d2012-02-28 13:51:55 -0800158 size_t width = srcArray->GetClass()->GetComponentSize();
159 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
160 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
161
162 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700163 case 1:
164 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
165 break;
166 case 2:
167 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
168 break;
169 case 4:
170 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
171 break;
172 case 8:
173 // We don't need to guarantee atomicity of the entire 64-bit word.
174 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
175 break;
176 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700177 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700178 }
179
180 return;
181 }
182
183 // Neither class is primitive. Are the types trivially compatible?
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700184 const size_t width = sizeof(Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800185 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
186 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800187 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700188 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700189 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700190 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800191 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700192 return;
193 }
194
Elliott Hughesab3530d2012-01-09 16:04:56 -0800195 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
196 // the elements if the source objects are compatible (for example, copying an Object[] to
197 // String[], the Objects being copied might actually be Strings).
198 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
199 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700200
Elliott Hughesab3530d2012-01-09 16:04:56 -0800201 // We already dealt with overlapping copies, so we don't need to cope with that case below.
202 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700203
Elliott Hughesab3530d2012-01-09 16:04:56 -0800204 Object* const * srcObjects = reinterpret_cast<Object* const *>(srcBytes + srcPos * width);
205 Object** dstObjects = reinterpret_cast<Object**>(dstBytes + dstPos * width);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700206 Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700207
Elliott Hughesab3530d2012-01-09 16:04:56 -0800208 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
209 // we know is assignable to the destination array's component type.
210 Class* lastAssignableElementClass = dstClass;
211
Elliott Hughes025c5de2012-01-10 11:05:48 -0800212 Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800213 int i = 0;
214 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800215 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800216 if (o != NULL) {
217 Class* oClass = o->GetClass();
218 if (lastAssignableElementClass == oClass) {
219 dstObjects[i] = o;
220 } else if (dstClass->IsAssignableFrom(oClass)) {
221 lastAssignableElementClass = oClass;
222 dstObjects[i] = o;
223 } else {
224 // Can't put this element into the array.
225 break;
226 }
227 } else {
228 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700229 }
230 }
231
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800232 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800233 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800234 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700235 std::string dstType(PrettyTypeOf(dstArray));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700236 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700237 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800238 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700239 return;
240 }
241}
242
Elliott Hughes0512f022012-03-15 22:10:52 -0700243static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700244 Object* o = Decode<Object*>(env, javaObject);
245 return static_cast<jint>(reinterpret_cast<uintptr_t>(o));
246}
247
Elliott Hughes0512f022012-03-15 22:10:52 -0700248static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700249 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
250 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
251};
252
Elliott Hughesbf86d042011-08-31 17:53:14 -0700253void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700254 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700255}
256
257} // namespace art