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 | |
| 17 | #include "jni_internal.h" |
| 18 | #include "object.h" |
| 19 | |
| 20 | #include "JniConstants.h" // Last to avoid problems with LOG redefinition. |
| 21 | |
| 22 | /* |
| 23 | * We make guarantees about the atomicity of accesses to primitive |
| 24 | * variables. These guarantees also apply to elements of arrays. |
| 25 | * In particular, 8-bit, 16-bit, and 32-bit accesses must be atomic and |
| 26 | * must not cause "word tearing". Accesses to 64-bit array elements must |
| 27 | * either be atomic or treated as two 32-bit operations. References are |
| 28 | * always read and written atomically, regardless of the number of bits |
| 29 | * used to represent them. |
| 30 | * |
| 31 | * We can't rely on standard libc functions like memcpy(3) and memmove(3) |
| 32 | * in our implementation of System.arraycopy, because they may copy |
| 33 | * byte-by-byte (either for the full run or for "unaligned" parts at the |
| 34 | * start or end). We need to use functions that guarantee 16-bit or 32-bit |
| 35 | * atomicity as appropriate. |
| 36 | * |
| 37 | * System.arraycopy() is heavily used, so having an efficient implementation |
| 38 | * is important. The bionic libc provides a platform-optimized memory move |
| 39 | * function that should be used when possible. If it's not available, |
| 40 | * the trivial "reference implementation" versions below can be used until |
| 41 | * a proper version can be written. |
| 42 | * |
| 43 | * For these functions, The caller must guarantee that dst/src are aligned |
| 44 | * appropriately for the element type, and that n is a multiple of the |
| 45 | * element size. |
| 46 | */ |
| 47 | #ifdef __BIONIC__ |
| 48 | #define HAVE_MEMMOVE_WORDS |
| 49 | #endif |
| 50 | |
| 51 | #ifdef HAVE_MEMMOVE_WORDS |
| 52 | extern "C" void _memmove_words(void* dst, const void* src, size_t n); |
| 53 | #define move16 _memmove_words |
| 54 | #define move32 _memmove_words |
| 55 | #else |
| 56 | static void move16(void* dst, const void* src, size_t n) { |
| 57 | DCHECK((((uintptr_t) dst | (uintptr_t) src | n) & 0x01) == 0); |
| 58 | |
| 59 | uint16_t* d = reinterpret_cast<uint16_t*>(dst); |
| 60 | const uint16_t* s = reinterpret_cast<const uint16_t*>(src); |
| 61 | |
| 62 | n /= sizeof(uint16_t); |
| 63 | |
| 64 | if (d < s) { |
| 65 | // Copy forwards. |
| 66 | while (n--) { |
| 67 | *d++ = *s++; |
| 68 | } |
| 69 | } else { |
| 70 | // Copy backwards. |
| 71 | d += n; |
| 72 | s += n; |
| 73 | while (n--) { |
| 74 | *--d = *--s; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | static void move32(void* dst, const void* src, size_t n) { |
| 80 | DCHECK((((uintptr_t) dst | (uintptr_t) src | n) & 0x03) == 0); |
| 81 | |
| 82 | uint32_t* d = reinterpret_cast<uint32_t*>(dst); |
| 83 | const uint32_t* s = reinterpret_cast<const uint32_t*>(src); |
| 84 | |
| 85 | n /= sizeof(uint32_t); |
| 86 | |
| 87 | if (d < s) { |
| 88 | // Copy forwards. |
| 89 | while (n--) { |
| 90 | *d++ = *s++; |
| 91 | } |
| 92 | } else { |
| 93 | // Copy backwards. |
| 94 | d += n; |
| 95 | s += n; |
| 96 | while (n--) { |
| 97 | *--d = *--s; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | #endif // HAVE_MEMMOVE_WORDS |
| 102 | |
| 103 | namespace art { |
| 104 | |
| 105 | namespace { |
| 106 | |
| 107 | void ThrowArrayStoreException_NotAnArray(const char* identifier, Object* array) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 108 | std::string actualType(PrettyTypeOf(array)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 109 | Thread::Current()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
| 110 | "%s is not an array: %s", identifier, actualType.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, jint dstPos, jint length) { |
| 114 | Thread* self = Thread::Current(); |
| 115 | |
| 116 | // Null pointer checks. |
| 117 | if (javaSrc == NULL) { |
| 118 | self->ThrowNewException("Ljava/lang/NullPointerException;", "src == null"); |
| 119 | return; |
| 120 | } |
| 121 | if (javaDst == NULL) { |
| 122 | self->ThrowNewException("Ljava/lang/NullPointerException;", "dst == null"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | // Make sure source and destination are both arrays. |
| 127 | Object* srcObject = Decode<Object*>(env, javaSrc); |
| 128 | Object* dstObject = Decode<Object*>(env, javaDst); |
| 129 | if (!srcObject->IsArrayInstance()) { |
| 130 | ThrowArrayStoreException_NotAnArray("src", srcObject); |
| 131 | return; |
| 132 | } |
| 133 | if (!dstObject->IsArrayInstance()) { |
| 134 | ThrowArrayStoreException_NotAnArray("dst", dstObject); |
| 135 | return; |
| 136 | } |
| 137 | Array* srcArray = srcObject->AsArray(); |
| 138 | Array* dstArray = dstObject->AsArray(); |
| 139 | Class* srcComponentType = srcArray->GetClass()->GetComponentType(); |
| 140 | Class* dstComponentType = dstArray->GetClass()->GetComponentType(); |
| 141 | |
| 142 | // Bounds checking. |
| 143 | if (srcPos < 0 || dstPos < 0 || length < 0 || srcPos > srcArray->GetLength() - length || dstPos > dstArray->GetLength() - length) { |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 144 | self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 145 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
| 146 | srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, length); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | uint8_t* dstBytes = reinterpret_cast<uint8_t*>(dstArray->GetRawData()); |
| 151 | const uint8_t* srcBytes = reinterpret_cast<const uint8_t*>(srcArray->GetRawData()); |
| 152 | |
| 153 | // 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 Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 157 | std::string srcType(PrettyTypeOf(srcArray)); |
| 158 | std::string dstType(PrettyTypeOf(dstArray)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 159 | self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 160 | "Incompatible types: src=%s, dst=%s", srcType.c_str(), dstType.c_str()); |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | switch (srcArray->GetClass()->GetComponentSize()) { |
| 165 | case 1: |
| 166 | memmove(dstBytes + dstPos, srcBytes + srcPos, length); |
| 167 | break; |
| 168 | case 2: |
| 169 | move16(dstBytes + dstPos * 2, srcBytes + srcPos * 2, length * 2); |
| 170 | break; |
| 171 | case 4: |
| 172 | move32(dstBytes + dstPos * 4, srcBytes + srcPos * 4, length * 4); |
| 173 | break; |
| 174 | case 8: |
| 175 | // We don't need to guarantee atomicity of the entire 64-bit word. |
| 176 | move32(dstBytes + dstPos * 8, srcBytes + srcPos * 8, length * 8); |
| 177 | break; |
| 178 | default: |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 179 | LOG(FATAL) << "Unknown primitive array type: " << PrettyTypeOf(srcArray); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | // Neither class is primitive. Are the types trivially compatible? |
Brian Carlstrom | 5b8e4c8 | 2011-09-18 01:38:59 -0700 | [diff] [blame] | 186 | const size_t width = sizeof(Object*); |
| 187 | if (dstComponentType->IsAssignableFrom(srcComponentType)) { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 188 | // Yes. Bulk copy. |
Brian Carlstrom | b6db9d2 | 2011-09-18 11:39:12 -0700 | [diff] [blame] | 189 | 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] | 190 | move32(dstBytes + dstPos * width, srcBytes + srcPos * width, length * width); |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 191 | Heap::WriteBarrier(dstArray); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | // The arrays are not trivially compatible. However, we |
| 196 | // may still be able to do this if the destination object is |
| 197 | // compatible (e.g. copy Object[] to String[], but the Object |
| 198 | // being copied is actually a String). We need to copy elements |
| 199 | // one by one until something goes wrong. |
| 200 | // |
| 201 | // Because of overlapping moves, what we really want to do |
| 202 | // is compare the types and count up how many we can move, |
| 203 | // then call move32() to shift the actual data. If we just |
| 204 | // start from the front we could do a smear rather than a move. |
| 205 | |
| 206 | // TODO: this idea is flawed. a malicious caller could exploit the check-use |
| 207 | // race by modifying the source array after we check but before we copy, |
| 208 | // and cause us to copy incompatible elements. |
| 209 | |
Ian Rogers | 0cfe1fb | 2011-08-26 03:29:44 -0700 | [diff] [blame] | 210 | Object* const * srcObj = reinterpret_cast<Object* const *>(srcBytes + srcPos * width); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 211 | Class* dstClass = dstArray->GetClass()->GetComponentType(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 212 | |
| 213 | Class* initialElementClass = NULL; |
| 214 | if (length > 0 && srcObj[0] != NULL) { |
| 215 | initialElementClass = srcObj[0]->GetClass(); |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 216 | if (!dstClass->IsAssignableFrom(initialElementClass)) { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 217 | initialElementClass = NULL; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | int copyCount; |
| 222 | for (copyCount = 0; copyCount < length; copyCount++) { |
Brian Carlstrom | 25c3325 | 2011-09-18 15:58:35 -0700 | [diff] [blame] | 223 | if (srcObj[copyCount] != NULL && srcObj[copyCount]->GetClass() != initialElementClass && !dstClass->IsAssignableFrom(srcObj[copyCount]->GetClass())) { |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 224 | // Can't put this element into the array. |
| 225 | // We'll copy up to this point, then throw. |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | move32(dstBytes + dstPos * width, srcBytes + srcPos * width, copyCount * width); |
Elliott Hughes | 3a4f8df | 2011-09-13 15:22:36 -0700 | [diff] [blame] | 231 | Heap::WriteBarrier(dstArray); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 232 | if (copyCount != length) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 233 | std::string actualSrcType(PrettyTypeOf(srcObj[copyCount])); |
| 234 | std::string dstType(PrettyTypeOf(dstArray)); |
Elliott Hughes | 5cb5ad2 | 2011-10-02 12:13:39 -0700 | [diff] [blame] | 235 | self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;", |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 236 | "source[%d] of type %s cannot be stored in destination array of type %s", |
| 237 | srcPos + copyCount, actualSrcType.c_str(), dstType.c_str()); |
| 238 | return; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) { |
| 243 | Object* o = Decode<Object*>(env, javaObject); |
| 244 | return static_cast<jint>(reinterpret_cast<uintptr_t>(o)); |
| 245 | } |
| 246 | |
| 247 | JNINativeMethod gMethods[] = { |
| 248 | NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"), |
| 249 | NATIVE_METHOD(System, identityHashCode, "(Ljava/lang/Object;)I"), |
| 250 | }; |
| 251 | |
| 252 | } // namespace |
| 253 | |
| 254 | void register_java_lang_System(JNIEnv* env) { |
| 255 | jniRegisterNativeMethods(env, "java/lang/System", gMethods, NELEM(gMethods)); |
| 256 | } |
| 257 | |
| 258 | } // namespace art |