blob: 79614aeab4b43438608bfafabe160af59a3ab6d5 [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 */
51#ifdef __BIONIC__
52#define HAVE_MEMMOVE_WORDS
53#endif
54
55#ifdef HAVE_MEMMOVE_WORDS
56extern "C" void _memmove_words(void* dst, const void* src, size_t n);
57#define move16 _memmove_words
58#define move32 _memmove_words
59#else
60static void move16(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070061 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070062
63 uint16_t* d = reinterpret_cast<uint16_t*>(dst);
64 const uint16_t* s = reinterpret_cast<const uint16_t*>(src);
65
66 n /= sizeof(uint16_t);
67
68 if (d < s) {
69 // Copy forwards.
70 while (n--) {
71 *d++ = *s++;
72 }
73 } else {
74 // Copy backwards.
75 d += n;
76 s += n;
77 while (n--) {
78 *--d = *--s;
79 }
80 }
81}
82
83static void move32(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070084 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070085
86 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
87 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
88
89 n /= sizeof(uint32_t);
90
91 if (d < s) {
92 // Copy forwards.
93 while (n--) {
94 *d++ = *s++;
95 }
96 } else {
97 // Copy backwards.
98 d += n;
99 s += n;
100 while (n--) {
101 *--d = *--s;
102 }
103 }
104}
105#endif // HAVE_MEMMOVE_WORDS
106
107namespace art {
108
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800109static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700111 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700112 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800113 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700114}
115
Elliott Hughes0512f022012-03-15 22:10:52 -0700116static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700117 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700118
119 // Null pointer checks.
120 if (javaSrc == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700122 return;
123 }
124 if (javaDst == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126 return;
127 }
128
129 // Make sure source and destination are both arrays.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800130 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
131 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800133 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700134 return;
135 }
136 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800137 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700138 return;
139 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800140 mirror::Array* srcArray = srcObject->AsArray();
141 mirror::Array* dstArray = dstObject->AsArray();
142 mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType();
143 mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700144
145 // Bounds checking.
146 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700147 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
149 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
150 return;
151 }
152
Elliott Hughesbf86d042011-08-31 17:53:14 -0700153 // Handle primitive arrays.
154 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
155 // If one of the arrays holds a primitive type the other array must hold the exact same type.
156 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700157 std::string srcType(PrettyTypeOf(srcArray));
158 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700159 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700160 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
161 return;
162 }
163
Ian Rogersa15e67d2012-02-28 13:51:55 -0800164 size_t width = srcArray->GetClass()->GetComponentSize();
165 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
166 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
167
168 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700169 case 1:
170 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
171 break;
172 case 2:
173 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
174 break;
175 case 4:
176 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
177 break;
178 case 8:
179 // We don't need to guarantee atomicity of the entire 64-bit word.
180 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
181 break;
182 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700183 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700184 }
185
186 return;
187 }
188
189 // Neither class is primitive. Are the types trivially compatible?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800190 const size_t width = sizeof(mirror::Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800191 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
192 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800193 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700194 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700195 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700196 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800197 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700198 return;
199 }
200
Elliott Hughesab3530d2012-01-09 16:04:56 -0800201 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
202 // the elements if the source objects are compatible (for example, copying an Object[] to
203 // String[], the Objects being copied might actually be Strings).
204 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
205 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700206
Elliott Hughesab3530d2012-01-09 16:04:56 -0800207 // We already dealt with overlapping copies, so we don't need to cope with that case below.
208 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700209
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800210 mirror::Object* const * srcObjects =
211 reinterpret_cast<mirror::Object* const *>(srcBytes + srcPos * width);
212 mirror::Object** dstObjects = reinterpret_cast<mirror::Object**>(dstBytes + dstPos * width);
213 mirror::Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700214
Elliott Hughesab3530d2012-01-09 16:04:56 -0800215 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
216 // we know is assignable to the destination array's component type.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800217 mirror::Class* lastAssignableElementClass = dstClass;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800218
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800219 mirror::Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800220 int i = 0;
221 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800222 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800223 if (o != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 mirror::Class* oClass = o->GetClass();
Elliott Hughesab3530d2012-01-09 16:04:56 -0800225 if (lastAssignableElementClass == oClass) {
226 dstObjects[i] = o;
227 } else if (dstClass->IsAssignableFrom(oClass)) {
228 lastAssignableElementClass = oClass;
229 dstObjects[i] = o;
230 } else {
231 // Can't put this element into the array.
232 break;
233 }
234 } else {
235 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700236 }
237 }
238
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800239 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800240 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800241 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700242 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700243 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700244 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800245 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700246 return;
247 }
248}
249
Elliott Hughes0512f022012-03-15 22:10:52 -0700250static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700251 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800252 mirror::Object* o = soa.Decode<mirror::Object*>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700253 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700254}
255
Elliott Hughes0512f022012-03-15 22:10:52 -0700256static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700257 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
258 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
259};
260
Elliott Hughesbf86d042011-08-31 17:53:14 -0700261void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700262 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700263}
264
265} // namespace art