Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 17 | #include "gc/card_table-inl.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 18 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 19 | #include "mirror/array.h" |
| 20 | #include "mirror/class.h" |
| 21 | #include "mirror/class-inl.h" |
| 22 | #include "mirror/object-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 23 | #include "scoped_thread_state_change.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 24 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 25 | /* |
| 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 |
| 55 | extern "C" void _memmove_words(void* dst, const void* src, size_t n); |
| 56 | #define move16 _memmove_words |
| 57 | #define move32 _memmove_words |
| 58 | #else |
| 59 | static void move16(void* dst, const void* src, size_t n) { |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 60 | DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x01), 0U); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 61 | |
| 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 | |
| 82 | static void move32(void* dst, const void* src, size_t n) { |
Elliott Hughes | cc60747 | 2011-10-17 15:34:11 -0700 | [diff] [blame] | 83 | DCHECK_EQ((((uintptr_t) dst | (uintptr_t) src | n) & 0x03), 0U); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 84 | |
| 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 | |
| 106 | namespace art { |
| 107 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 108 | static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 109 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 110 | std::string actualType(PrettyTypeOf(array)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 111 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | 81aa5b3 | 2012-01-17 13:47:13 -0800 | [diff] [blame] | 112 | "%s of type %s is not an array", identifier, actualType.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 115 | static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 116 | ScopedObjectAccess soa(env); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 117 | |
| 118 | // Null pointer checks. |
| 119 | if (javaSrc == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 120 | soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "src == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 121 | return; |
| 122 | } |
| 123 | if (javaDst == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 124 | soa.Self()->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 125 | return; |
| 126 | } |
| 127 | |
| 128 | // Make sure source and destination are both arrays. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 129 | mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc); |
| 130 | mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 131 | if (!srcObject->IsArrayInstance()) { |
Elliott Hughes | 81aa5b3 | 2012-01-17 13:47:13 -0800 | [diff] [blame] | 132 | ThrowArrayStoreException_NotAnArray("source", srcObject); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | if (!dstObject->IsArrayInstance()) { |
Elliott Hughes | 81aa5b3 | 2012-01-17 13:47:13 -0800 | [diff] [blame] | 136 | ThrowArrayStoreException_NotAnArray("destination", dstObject); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 137 | return; |
| 138 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 139 | 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 143 | |
| 144 | // Bounds checking. |
| 145 | if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 146 | soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 147 | "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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 152 | // 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 Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 156 | std::string srcType(PrettyTypeOf(srcArray)); |
| 157 | std::string dstType(PrettyTypeOf(dstArray)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 158 | soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 159 | "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str()); |
| 160 | return; |
| 161 | } |
| 162 | |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 163 | 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 168 | 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 Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 182 | LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | // Neither class is primitive. Are the types trivially compatible? |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 189 | const size_t width = sizeof(mirror::Object*); |
Ian Rogers | a15e67d | 2012-02-28 13:51:55 -0800 | [diff] [blame] | 190 | uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData(width)); |
| 191 | const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData(width)); |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 192 | if (dstArray == srcArray || dstComponentType->IsAssignableFrom(srcComponentType)) { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 193 | // Yes. Bulk copy. |
Brian Carlstrom | b6db9d2 | 2011-09-18 11:39:12 -0700 | [diff] [blame] | 194 | COMPILE_ASSERT(sizeof(width) == sizeof(uint32_t), move32_assumes_Object_references_are_32_bit); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 195 | move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width); |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 196 | Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 200 | // 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 205 | |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 206 | // We already dealt with overlapping copies, so we don't need to cope with that case below. |
| 207 | CHECK_NE(dstArray, srcArray); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 208 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 209 | 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 213 | |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 214 | // 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 Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 216 | mirror::Class* lastAssignableElementClass = dstClass; |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 217 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 218 | mirror::Object* o = NULL; |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 219 | int i = 0; |
| 220 | for (; i < length; ++i) { |
Elliott Hughes | 025c5de | 2012-01-10 11:05:48 -0800 | [diff] [blame] | 221 | o = srcObjects[i]; |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 222 | if (o != NULL) { |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 223 | mirror::Class* oClass = o->GetClass(); |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 224 | 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 Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Elliott Hughes | b3bd5f0 | 2012-03-08 21:05:27 -0800 | [diff] [blame] | 238 | Runtime::Current()->GetHeap()->WriteBarrierArray(dstArray, dstPos, length); |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 239 | if (i != length) { |
Elliott Hughes | 025c5de | 2012-01-10 11:05:48 -0800 | [diff] [blame] | 240 | std::string actualSrcType(PrettyTypeOf(o)); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 241 | std::string dstType(PrettyTypeOf(dstArray)); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 242 | soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 243 | "source[%d] of type %s cannot be stored in destination array of type %s", |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 244 | srcPos + i, actualSrcType.c_str(), dstType.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 245 | return; |
| 246 | } |
| 247 | } |
| 248 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 249 | static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 250 | ScopedObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame^] | 251 | mirror::Object* o = soa.Decode<mirror::Object*>(javaObject); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 252 | return static_cast<jint>(o->IdentityHashCode()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 255 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 256 | NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"), |
| 257 | NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"), |
| 258 | }; |
| 259 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 260 | void register_java_lang_System(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 261 | REGISTER_NATIVE_METHODS("java/lang/System"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | } // namespace art |