blob: f4fe6ca9cd763c881bbcc713ae5116212dbc9cbc [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"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070019#include "scoped_thread_state_change.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070020
Elliott Hughesbf86d042011-08-31 17:53:14 -070021/*
22 * We make guarantees about the atomicity of accesses to primitive
23 * variables. These guarantees also apply to elements of arrays.
24 * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and
25 * must not cause "word tearing". Accesses to 64-bit array elements must
26 * either be atomic or treated as two 32-bit operations. References are
27 * always read and written atomically, regardless of the number of bits
28 * used to represent them.
29 *
30 * We can't rely on standard libc functions like memcpy(3) and memmove(3)
31 * in our implementation of System.arraycopy, because they may copy
32 * byte-by-byte (either for the full run or for "unaligned" parts at the
33 * start or end). We need to use functions that guarantee 16-bit or 32-bit
34 * atomicity as appropriate.
35 *
36 * System.arraycopy() is heavily used, so having an efficient implementation
37 * is important. The bionic libc provides a platform-optimized memory move
38 * function that should be used when possible. If it's not available,
39 * the trivial "reference implementation" versions below can be used until
40 * a proper version can be written.
41 *
42 * For these functions, The caller must guarantee that dst/src are aligned
43 * appropriately for the element type, and that n is a multiple of the
44 * element size.
45 */
46#ifdef __BIONIC__
47#define HAVE_MEMMOVE_WORDS
48#endif
49
50#ifdef HAVE_MEMMOVE_WORDS
51extern "C" void _memmove_words(void* dst, const void* src, size_t n);
52#define move16 _memmove_words
53#define move32 _memmove_words
54#else
55static void move16(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070056 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070057
58 uint16_t* d = reinterpret_cast<uint16_t*>(dst);
59 const uint16_t* s = reinterpret_cast<const uint16_t*>(src);
60
61 n /= sizeof(uint16_t);
62
63 if (d < s) {
64 // Copy forwards.
65 while (n--) {
66 *d++ = *s++;
67 }
68 } else {
69 // Copy backwards.
70 d += n;
71 s += n;
72 while (n--) {
73 *--d = *--s;
74 }
75 }
76}
77
78static void move32(void* dst, const void* src, size_t n) {
Elliott Hughescc607472011-10-17 15:34:11 -070079 DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U);
Elliott Hughesbf86d042011-08-31 17:53:14 -070080
81 uint32_t* d = reinterpret_cast<uint32_t*>(dst);
82 const uint32_t* s = reinterpret_cast<const uint32_t*>(src);
83
84 n /= sizeof(uint32_t);
85
86 if (d < s) {
87 // Copy forwards.
88 while (n--) {
89 *d++ = *s++;
90 }
91 } else {
92 // Copy backwards.
93 d += n;
94 s += n;
95 while (n--) {
96 *--d = *--s;
97 }
98 }
99}
100#endif // HAVE_MEMMOVE_WORDS
101
102namespace art {
103
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104static void ThrowArrayStoreException_NotAnArray(const char* identifier, Object* array)
105 SHARED_LOCKS_REQUIRED(GlobalSynchronization::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700106 std::string actualType(PrettyTypeOf(array));
Elliott Hughes5cb5ad22011-10-02 12:13:39 -0700107 Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800108 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700109}
110
Elliott Hughes0512f022012-03-15 22:10:52 -0700111static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 ScopedObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700113
114 // Null pointer checks.
115 if (javaSrc == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700116 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700117 return;
118 }
119 if (javaDst == NULL) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700120 soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700121 return;
122 }
123
124 // Make sure source and destination are both arrays.
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700125 Object* srcObject = soa.Decode<Object*>(javaSrc);
126 Object* dstObject = soa.Decode<Object*>(javaDst);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700127 if (!srcObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800128 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700129 return;
130 }
131 if (!dstObject->IsArrayInstance()) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -0800132 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133 return;
134 }
135 Array* srcArray = srcObject->AsArray();
136 Array* dstArray = dstObject->AsArray();
137 Class* srcComponentType = srcArray->GetClass()->GetComponentType();
138 Class* dstComponentType = dstArray->GetClass()->GetComponentType();
139
140 // Bounds checking.
141 if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700142 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700143 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
144 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length);
145 return;
146 }
147
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148 // Handle primitive arrays.
149 if (srcComponentType->IsPrimitive() || dstComponentType->IsPrimitive()) {
150 // If one of the arrays holds a primitive type the other array must hold the exact same type.
151 if (srcComponentType->IsPrimitive() != dstComponentType->IsPrimitive() || srcComponentType != dstComponentType) {
Elliott Hughes54e7df12011-09-16 11:47:04 -0700152 std::string srcType(PrettyTypeOf(srcArray));
153 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700154 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700155 "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str());
156 return;
157 }
158
Ian Rogersa15e67d2012-02-28 13:51:55 -0800159 size_t width = srcArray->GetClass()->GetComponentSize();
160 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
161 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
162
163 switch (width) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700164 case 1:
165 memmove(dstBytes + dstPos, srcBytes + srcPos, length);
166 break;
167 case 2:
168 move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2);
169 break;
170 case 4:
171 move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4);
172 break;
173 case 8:
174 // We don't need to guarantee atomicity of the entire 64-bit word.
175 move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8);
176 break;
177 default:
Elliott Hughes54e7df12011-09-16 11:47:04 -0700178 LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700179 }
180
181 return;
182 }
183
184 // Neither class is primitive. Are the types trivially compatible?
Brian Carlstrom5b8e4c82011-09-18 01:38:59 -0700185 const size_t width = sizeof(Object*);
Ian Rogersa15e67d2012-02-28 13:51:55 -0800186 uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width));
187 const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width));
Elliott Hughesab3530d2012-01-09 16:04:56 -0800188 if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700189 // Yes. Bulk copy.
Brian Carlstromb6db9d22011-09-18 11:39:12 -0700190 COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700191 move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width);
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800192 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700193 return;
194 }
195
Elliott Hughesab3530d2012-01-09 16:04:56 -0800196 // The arrays are not trivially compatible. However, we may still be able to copy some or all of
197 // the elements if the source objects are compatible (for example, copying an Object[] to
198 // String[], the Objects being copied might actually be Strings).
199 // We can't do a bulk move because that would introduce a check-use race condition, so we copy
200 // elements one by one.
Elliott Hughesbf86d042011-08-31 17:53:14 -0700201
Elliott Hughesab3530d2012-01-09 16:04:56 -0800202 // We already dealt with overlapping copies, so we don't need to cope with that case below.
203 CHECK_NE(dstArray, srcArray);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700204
Elliott Hughesab3530d2012-01-09 16:04:56 -0800205 Object* const * srcObjects = reinterpret_cast<Object* const *>(srcBytes + srcPos * width);
206 Object** dstObjects = reinterpret_cast<Object**>(dstBytes + dstPos * width);
Brian Carlstrom25c33252011-09-18 15:58:35 -0700207 Class* dstClass = dstArray->GetClass()->GetComponentType();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700208
Elliott Hughesab3530d2012-01-09 16:04:56 -0800209 // We want to avoid redundant IsAssignableFrom checks where possible, so we cache a class that
210 // we know is assignable to the destination array's component type.
211 Class* lastAssignableElementClass = dstClass;
212
Elliott Hughes025c5de2012-01-10 11:05:48 -0800213 Object* o = NULL;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800214 int i = 0;
215 for (; i < length; ++i) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800216 o = srcObjects[i];
Elliott Hughesab3530d2012-01-09 16:04:56 -0800217 if (o != NULL) {
218 Class* oClass = o->GetClass();
219 if (lastAssignableElementClass == oClass) {
220 dstObjects[i] = o;
221 } else if (dstClass->IsAssignableFrom(oClass)) {
222 lastAssignableElementClass = oClass;
223 dstObjects[i] = o;
224 } else {
225 // Can't put this element into the array.
226 break;
227 }
228 } else {
229 dstObjects[i] = NULL;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700230 }
231 }
232
Elliott Hughesb3bd5f02012-03-08 21:05:27 -0800233 Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length);
Elliott Hughesab3530d2012-01-09 16:04:56 -0800234 if (i != length) {
Elliott Hughes025c5de2012-01-10 11:05:48 -0800235 std::string actualSrcType(PrettyTypeOf(o));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700236 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700237 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Elliott Hughesbf86d042011-08-31 17:53:14 -0700238 "source[%d] of type %s cannot be stored in destination array of type %s",
Elliott Hughesab3530d2012-01-09 16:04:56 -0800239 srcPos + i, actualSrcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700240 return;
241 }
242}
243
Elliott Hughes0512f022012-03-15 22:10:52 -0700244static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700245 ScopedObjectAccess soa(env);
246 Object* o = soa.Decode<Object*>(javaObject);
247 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700248}
249
Elliott Hughes0512f022012-03-15 22:10:52 -0700250static JNINativeMethod gMethods[] = {
Elliott Hughesbf86d042011-08-31 17:53:14 -0700251 NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
252 NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"),
253};
254
Elliott Hughesbf86d042011-08-31 17:53:14 -0700255void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700256 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700257}
258
259} // namespace art