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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "java_lang_System.h" |
| 18 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 19 | #include "common_throws.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 20 | #include "gc/accounting/card_table-inl.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 21 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 22 | #include "mirror/array.h" |
| 23 | #include "mirror/class.h" |
| 24 | #include "mirror/class-inl.h" |
| 25 | #include "mirror/object-inl.h" |
Ian Rogers | 693ff61 | 2013-02-01 10:56:12 -0800 | [diff] [blame] | 26 | #include "mirror/object_array-inl.h" |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 27 | #include "scoped_fast_native_object_access.h" |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 29 | namespace art { |
| 30 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 31 | /* |
| 32 | * We make guarantees about the atomicity of accesses to primitive variables. These guarantees |
| 33 | * also apply to elements of arrays. In particular, 8-bit, 16-bit, and 32-bit accesses must not |
| 34 | * cause "word tearing". Accesses to 64-bit array elements may be two 32-bit operations. |
| 35 | * References are never torn regardless of the number of bits used to represent them. |
| 36 | */ |
| 37 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 38 | static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array) |
Ian Rogers | b726dcb | 2012-09-05 08:57:23 -0700 | [diff] [blame] | 39 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 40 | std::string actualType(PrettyTypeOf(array)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 41 | Thread* self = Thread::Current(); |
| 42 | ThrowLocation throw_location = self->GetCurrentLocationForThrow(); |
| 43 | self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
| 44 | "%s of type %s is not an array", identifier, actualType.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 47 | static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst, |
| 48 | jint dstPos, jint length) { |
| 49 | // The API is defined in terms of length, but length is somewhat overloaded so we use count. |
| 50 | const jint count = length; |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 51 | ScopedFastNativeObjectAccess soa(env); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 52 | |
| 53 | // Null pointer checks. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 54 | if (UNLIKELY(javaSrc == nullptr)) { |
| 55 | ThrowNullPointerException(nullptr, "src == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 56 | return; |
| 57 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 58 | if (UNLIKELY(javaDst == nullptr)) { |
| 59 | ThrowNullPointerException(nullptr, "dst == null"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 60 | return; |
| 61 | } |
| 62 | |
| 63 | // Make sure source and destination are both arrays. |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 64 | mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 65 | if (UNLIKELY(!srcObject->IsArrayInstance())) { |
Elliott Hughes | 81aa5b3 | 2012-01-17 13:47:13 -0800 | [diff] [blame] | 66 | ThrowArrayStoreException_NotAnArray("source", srcObject); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 67 | return; |
| 68 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 69 | mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 70 | if (UNLIKELY(!dstObject->IsArrayInstance())) { |
Elliott Hughes | 81aa5b3 | 2012-01-17 13:47:13 -0800 | [diff] [blame] | 71 | ThrowArrayStoreException_NotAnArray("destination", dstObject); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 72 | return; |
| 73 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 74 | mirror::Array* srcArray = srcObject->AsArray(); |
| 75 | mirror::Array* dstArray = dstObject->AsArray(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 76 | |
| 77 | // Bounds checking. |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 78 | if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) || |
| 79 | UNLIKELY(srcPos > srcArray->GetLength() - count) || |
| 80 | UNLIKELY(dstPos > dstArray->GetLength() - count)) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 81 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 82 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;", |
| 83 | "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 84 | srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos, |
| 85 | count); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 89 | mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType(); |
| 90 | mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType(); |
| 91 | Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 92 | |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 93 | if (LIKELY(srcComponentType == dstComponentType)) { |
| 94 | // Trivial assignability. |
| 95 | switch (dstComponentPrimitiveType) { |
| 96 | case Primitive::kPrimVoid: |
| 97 | LOG(FATAL) << "Unreachable, cannot have arrays of type void"; |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 98 | UNREACHABLE(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 99 | case Primitive::kPrimBoolean: |
| 100 | case Primitive::kPrimByte: |
| 101 | DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 1U); |
| 102 | dstArray->AsByteSizedArray()->Memmove(dstPos, srcArray->AsByteSizedArray(), srcPos, count); |
| 103 | return; |
| 104 | case Primitive::kPrimChar: |
| 105 | case Primitive::kPrimShort: |
| 106 | DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 2U); |
| 107 | dstArray->AsShortSizedArray()->Memmove(dstPos, srcArray->AsShortSizedArray(), srcPos, count); |
| 108 | return; |
| 109 | case Primitive::kPrimInt: |
| 110 | case Primitive::kPrimFloat: |
| 111 | DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U); |
| 112 | dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count); |
| 113 | return; |
| 114 | case Primitive::kPrimLong: |
| 115 | case Primitive::kPrimDouble: |
| 116 | DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U); |
| 117 | dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count); |
| 118 | return; |
| 119 | case Primitive::kPrimNot: { |
| 120 | mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>(); |
| 121 | mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>(); |
| 122 | dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count); |
| 123 | return; |
Elliott Hughes | ab3530d | 2012-01-09 16:04:56 -0800 | [diff] [blame] | 124 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 125 | default: |
| 126 | LOG(FATAL) << "Unknown array type: " << PrettyTypeOf(srcArray); |
Ian Rogers | 2c4257b | 2014-10-24 14:20:06 -0700 | [diff] [blame] | 127 | UNREACHABLE(); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 128 | } |
| 129 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 130 | // If one of the arrays holds a primitive type the other array must hold the exact same type. |
| 131 | if (UNLIKELY((dstComponentPrimitiveType != Primitive::kPrimNot) || |
| 132 | srcComponentType->IsPrimitive())) { |
| 133 | std::string srcType(PrettyTypeOf(srcArray)); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 134 | std::string dstType(PrettyTypeOf(dstArray)); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 135 | ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow(); |
| 136 | soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;", |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 137 | "Incompatible types: src=%s, dst=%s", |
| 138 | srcType.c_str(), dstType.c_str()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 139 | return; |
| 140 | } |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 141 | // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove. |
| 142 | mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>(); |
| 143 | mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>(); |
| 144 | // If we're assigning into say Object[] then we don't need per element checks. |
| 145 | if (dstComponentType->IsAssignableFrom(srcComponentType)) { |
| 146 | dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count); |
| 147 | return; |
| 148 | } |
| 149 | dstObjArray->AssignableCheckingMemcpy(dstPos, srcObjArray, srcPos, count, true); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Emma Meersman | d735fe4 | 2014-06-18 11:50:59 -0700 | [diff] [blame] | 152 | // Template to convert general array to that of its specific primitive type. |
| 153 | template <typename T> |
| 154 | inline T* AsPrimitiveArray(mirror::Array* array) { |
| 155 | return down_cast<T*>(array); |
| 156 | } |
| 157 | |
| 158 | template <typename T, Primitive::Type kPrimType> |
| 159 | inline void System_arraycopyTUnchecked(JNIEnv* env, jobject javaSrc, jint srcPos, |
| 160 | jobject javaDst, jint dstPos, jint count) { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 161 | ScopedFastNativeObjectAccess soa(env); |
Hiroshi Yamauchi | f38ea80 | 2013-08-27 13:04:26 -0700 | [diff] [blame] | 162 | mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc); |
| 163 | mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 164 | DCHECK(dstObject != nullptr); |
Hiroshi Yamauchi | f38ea80 | 2013-08-27 13:04:26 -0700 | [diff] [blame] | 165 | mirror::Array* srcArray = srcObject->AsArray(); |
| 166 | mirror::Array* dstArray = dstObject->AsArray(); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 167 | DCHECK_GE(count, 0); |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 168 | DCHECK_EQ(srcArray->GetClass(), dstArray->GetClass()); |
Emma Meersman | d735fe4 | 2014-06-18 11:50:59 -0700 | [diff] [blame] | 169 | DCHECK_EQ(srcArray->GetClass()->GetComponentType()->GetPrimitiveType(), kPrimType); |
| 170 | AsPrimitiveArray<T>(dstArray)->Memmove(dstPos, AsPrimitiveArray<T>(srcArray), srcPos, count); |
| 171 | } |
| 172 | |
| 173 | static void System_arraycopyCharUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 174 | jobject javaDst, jint dstPos, jint count) { |
| 175 | System_arraycopyTUnchecked<mirror::CharArray, Primitive::kPrimChar>(env, javaSrc, srcPos, |
| 176 | javaDst, dstPos, count); |
| 177 | } |
| 178 | |
| 179 | static void System_arraycopyByteUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 180 | jobject javaDst, jint dstPos, jint count) { |
| 181 | System_arraycopyTUnchecked<mirror::ByteArray, Primitive::kPrimByte>(env, javaSrc, srcPos, |
| 182 | javaDst, dstPos, count); |
| 183 | } |
| 184 | |
| 185 | static void System_arraycopyShortUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 186 | jobject javaDst, jint dstPos, jint count) { |
| 187 | System_arraycopyTUnchecked<mirror::ShortArray, Primitive::kPrimShort>(env, javaSrc, srcPos, |
| 188 | javaDst, dstPos, count); |
| 189 | } |
| 190 | |
| 191 | static void System_arraycopyIntUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 192 | jobject javaDst, jint dstPos, jint count) { |
| 193 | System_arraycopyTUnchecked<mirror::IntArray, Primitive::kPrimInt>(env, javaSrc, srcPos, |
| 194 | javaDst, dstPos, count); |
| 195 | } |
| 196 | |
| 197 | static void System_arraycopyLongUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 198 | jobject javaDst, jint dstPos, jint count) { |
| 199 | System_arraycopyTUnchecked<mirror::LongArray, Primitive::kPrimLong>(env, javaSrc, srcPos, |
| 200 | javaDst, dstPos, count); |
| 201 | } |
| 202 | |
| 203 | static void System_arraycopyFloatUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 204 | jobject javaDst, jint dstPos, jint count) { |
| 205 | System_arraycopyTUnchecked<mirror::FloatArray, Primitive::kPrimFloat>(env, javaSrc, srcPos, |
| 206 | javaDst, dstPos, count); |
| 207 | } |
| 208 | |
| 209 | static void System_arraycopyDoubleUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 210 | jobject javaDst, jint dstPos, jint count) { |
| 211 | System_arraycopyTUnchecked<mirror::DoubleArray, Primitive::kPrimDouble>(env, javaSrc, srcPos, |
| 212 | javaDst, dstPos, count); |
| 213 | } |
| 214 | |
| 215 | static void System_arraycopyBooleanUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, |
| 216 | jobject javaDst, jint dstPos, jint count) { |
| 217 | System_arraycopyTUnchecked<mirror::BooleanArray, Primitive::kPrimBoolean>(env, javaSrc, srcPos, |
| 218 | javaDst, dstPos, count); |
Hiroshi Yamauchi | f38ea80 | 2013-08-27 13:04:26 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 221 | static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 222 | if (UNLIKELY(javaObject == nullptr)) { |
Mathieu Chartier | ad2541a | 2013-10-25 10:05:23 -0700 | [diff] [blame] | 223 | return 0; |
| 224 | } |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 225 | ScopedFastNativeObjectAccess soa(env); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 226 | mirror::Object* o = soa.Decode<mirror::Object*>(javaObject); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 227 | return static_cast<jint>(o->IdentityHashCode()); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 230 | static JNINativeMethod gMethods[] = { |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 231 | NATIVE_METHOD(System, arraycopy, "!(Ljava/lang/Object;ILjava/lang/Object;II)V"), |
| 232 | NATIVE_METHOD(System, arraycopyCharUnchecked, "!([CI[CII)V"), |
Emma Meersman | d735fe4 | 2014-06-18 11:50:59 -0700 | [diff] [blame] | 233 | NATIVE_METHOD(System, arraycopyByteUnchecked, "!([BI[BII)V"), |
| 234 | NATIVE_METHOD(System, arraycopyShortUnchecked, "!([SI[SII)V"), |
| 235 | NATIVE_METHOD(System, arraycopyIntUnchecked, "!([II[III)V"), |
| 236 | NATIVE_METHOD(System, arraycopyLongUnchecked, "!([JI[JII)V"), |
| 237 | NATIVE_METHOD(System, arraycopyFloatUnchecked, "!([FI[FII)V"), |
| 238 | NATIVE_METHOD(System, arraycopyDoubleUnchecked, "!([DI[DII)V"), |
| 239 | NATIVE_METHOD(System, arraycopyBooleanUnchecked, "!([ZI[ZII)V"), |
Ian Rogers | 1eb512d | 2013-10-18 15:42:20 -0700 | [diff] [blame] | 240 | NATIVE_METHOD(System, identityHashCode, "!(Ljava/lang/Object;)I"), |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 243 | void register_java_lang_System(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 244 | REGISTER_NATIVE_METHODS("java/lang/System"); |
Elliott Hughes | bf86d04 | 2011-08-31 17:53:14 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | } // namespace art |