blob: 54ee2e914e05ecc2580ccbfb5551f9f44c12383f [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 Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070024
Elliott Hughesbf86d042011-08-31 17:53:14 -070025/*
26 * We make guarantees about the atomicity of accesses to primitive
27 * variables. These guarantees also apply to elements of arrays.
28 * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and
29 * must not cause "word tearing". Accesses to 64-bit array elements must
30 * either be atomic or treated as two 32-bit operations. References are
31 * always read and written atomically, regardless of the number of bits
32 * used to represent them.
33 *
34 * We can't rely on standard libc functions like memcpy(3) and memmove(3)
35 * in our implementation of System.arraycopy, because they may copy
36 * byte-by-byte (either for the full run or for "unaligned" parts at the
37 * start or end). We need to use functions that guarantee 16-bit or 32-bit
38 * atomicity as appropriate.
39 *
40 * System.arraycopy() is heavily used, so having an efficient implementation
41 * is important. The bionic libc provides a platform-optimized memory move
42 * function that should be used when possible. If it's not available,
43 * the trivial "reference implementation" versions below can be used until
44 * a proper version can be written.
45 *
46 * For these functions, The caller must guarantee that dst/src are aligned
47 * appropriately for the element type, and that n is a multiple of the
48 * element size.
49 */
50#ifdef __BIONIC__
51#define HAVE_MEMMOVE_WORDS
52#endif
53
54#ifdef HAVE_MEMMOVE_WORDS
55extern "C" void _memmove_words(void* dst, const void* src, size_t n);
56#define move16 _memmove_words
57#define move32 _memmove_words
58#else
59static void move16(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070060 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070061
62 uint16_t* d = reinterpret_cast<uint16_t*>(dst);
63 const uint16_t* s = reinterpret_cast<const uint16_t*>(src);
64
65 n /= sizeof(uint16_t);
66
67 if (d < s) {
68 // Copy forwards.
69 while (n--) {
70 *d++ = *s++;
71 }
72 } else {
73 // Copy backwards.
74 d += n;
75 s += n;
76 while (n--) {
77 *--d = *--s;
78 }
79 }
80}
81
82static void move32(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070083 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070084
85 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
86 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
87
88 n /= sizeof(uint32_t);
89
90 if (d < s) {
91 // Copy forwards.
92 while (n--) {
93 *d++ = *s++;
94 }
95 } else {
96 // Copy backwards.
97 d += n;
98 s += n;
99 while (n--) {
100 *--d = *--s;
101 }
102 }
103}
104#endif // HAVE_MEMMOVE_WORDS
105
106namespace art {
107
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800108static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -0700109 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700110 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700111 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800112 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113}
114
Elliott Hughes0512f022012-03-15 22:10:52 -0700115static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700117
118 // Null pointer checks.
119 if (javaSrc == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700121 return;
122 }
123 if (javaDst == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700124 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700125 return;
126 }
127
128 // Make sure source and destination are both arrays.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800129 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
130 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700131 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800132 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133 return;
134 }
135 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800136 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137 return;
138 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800139 mirror::Array* srcArray = srcObject->AsArray();
140 mirror::Array* dstArray = dstObject->AsArray();
141 mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType();
142 mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700143
144 // Bounds checking.
145 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700146 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700147 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
148 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
149 return;
150 }
151
Elliott Hughesbf86d042011-08-31 17:53:14 -0700152 // Handle primitive arrays.
153 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
154 // If one of the arrays holds a primitive type the other array must hold the exact same type.
155 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700156 std::string srcType(PrettyTypeOf(srcArray));
157 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700158 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700159 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
160 return;
161 }
162
Ian Rogersa15e67d2012-02-28 13:51:55 -0800163 size_t width = srcArray->GetClass()->GetComponentSize();
164 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
165 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
166
167 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700168 case 1:
169 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
170 break;
171 case 2:
172 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
173 break;
174 case 4:
175 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
176 break;
177 case 8:
178 // We don't need to guarantee atomicity of the entire 64-bit word.
179 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
180 break;
181 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700182 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700183 }
184
185 return;
186 }
187
188 // Neither class is primitive. Are the types trivially compatible?
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800189 const size_t width = sizeof(mirror::Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800190 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
191 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800192 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700193 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700194 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700195 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800196 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700197 return;
198 }
199
Elliott Hughesab3530d2012-01-09 16:04:56 -0800200 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
201 // the elements if the source objects are compatible (for example, copying an Object[] to
202 // String[], the Objects being copied might actually be Strings).
203 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
204 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700205
Elliott Hughesab3530d2012-01-09 16:04:56 -0800206 // We already dealt with overlapping copies, so we don't need to cope with that case below.
207 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700208
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800209 mirror::Object* const * srcObjects =
210 reinterpret_cast<mirror::Object* const *>(srcBytes + srcPos * width);
211 mirror::Object** dstObjects = reinterpret_cast<mirror::Object**>(dstBytes + dstPos * width);
212 mirror::Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700213
Elliott Hughesab3530d2012-01-09 16:04:56 -0800214 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
215 // we know is assignable to the destination array's component type.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800216 mirror::Class* lastAssignableElementClass = dstClass;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800217
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800218 mirror::Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800219 int i = 0;
220 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800221 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800222 if (o != NULL) {
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800223 mirror::Class* oClass = o->GetClass();
Elliott Hughesab3530d2012-01-09 16:04:56 -0800224 if (lastAssignableElementClass == oClass) {
225 dstObjects[i] = o;
226 } else if (dstClass->IsAssignableFrom(oClass)) {
227 lastAssignableElementClass = oClass;
228 dstObjects[i] = o;
229 } else {
230 // Can't put this element into the array.
231 break;
232 }
233 } else {
234 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700235 }
236 }
237
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800238 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800239 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800240 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700241 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700242 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700243 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800244 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700245 return;
246 }
247}
248
Elliott Hughes0512f022012-03-15 22:10:52 -0700249static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700250 ScopedObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800251 mirror::Object* o = soa.Decode<mirror::Object*>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700252 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700253}
254
Elliott Hughes0512f022012-03-15 22:10:52 -0700255static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700256 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
257 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
258};
259
Elliott Hughesbf86d042011-08-31 17:53:14 -0700260void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700261 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700262}
263
264} // namespace art