blob: 63cbd2c81587003f7b5607f5fdc7ff88873361fb [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
Andreas Gampe277ccbd2014-11-03 21:36:10 -080017#include "java_lang_System.h"
18
Andreas Gampea14100c2017-04-24 15:09:56 -070019#include "nativehelper/jni_macros.h"
20
Ian Rogers62d6c772013-02-27 08:32:07 -080021#include "common_throws.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "gc/accounting/card_table-inl.h"
Vladimir Markoa3ad0cd2018-05-04 10:06:38 +010023#include "jni/jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080024#include "mirror/array.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080025#include "mirror/class-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070026#include "mirror/class.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080027#include "mirror/object-inl.h"
Ian Rogers693ff612013-02-01 10:56:12 -080028#include "mirror/object_array-inl.h"
Andreas Gampe87583b32017-05-25 11:22:18 -070029#include "native_util.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070030#include "scoped_fast_native_object_access-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070031
Elliott Hughesbf86d042011-08-31 17:53:14 -070032namespace art {
33
Ian Rogersef7d42f2014-01-06 12:55:46 -080034/*
35 * We make guarantees about the atomicity of accesses to primitive variables. These guarantees
36 * also apply to elements of arrays. In particular, 8-bit, 16-bit, and 32-bit accesses must not
37 * cause "word tearing". Accesses to 64-bit array elements may be two 32-bit operations.
38 * References are never torn regardless of the number of bits used to represent them.
39 */
40
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070041static void ThrowArrayStoreException_NotAnArray(const char* identifier,
42 ObjPtr<mirror::Object> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070043 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -070044 std::string actualType(mirror::Object::PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -080045 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000046 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080047 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070048}
49
Ian Rogersef7d42f2014-01-06 12:55:46 -080050static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst,
51 jint dstPos, jint length) {
52 // The API is defined in terms of length, but length is somewhat overloaded so we use count.
53 const jint count = length;
Ian Rogers1eb512d2013-10-18 15:42:20 -070054 ScopedFastNativeObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070055
56 // Null pointer checks.
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 if (UNLIKELY(javaSrc == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000058 ThrowNullPointerException("src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070059 return;
60 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080061 if (UNLIKELY(javaDst == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000062 ThrowNullPointerException("dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070063 return;
64 }
65
66 // Make sure source and destination are both arrays.
Mathieu Chartier0795f232016-09-27 18:43:30 -070067 ObjPtr<mirror::Object> srcObject = soa.Decode<mirror::Object>(javaSrc);
Ian Rogers62d6c772013-02-27 08:32:07 -080068 if (UNLIKELY(!srcObject->IsArrayInstance())) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070069 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070070 return;
71 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070072 ObjPtr<mirror::Object> dstObject = soa.Decode<mirror::Object>(javaDst);
Ian Rogers62d6c772013-02-27 08:32:07 -080073 if (UNLIKELY(!dstObject->IsArrayInstance())) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070074 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070075 return;
76 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070077 ObjPtr<mirror::Array> srcArray = srcObject->AsArray();
78 ObjPtr<mirror::Array> dstArray = dstObject->AsArray();
Elliott Hughesbf86d042011-08-31 17:53:14 -070079
80 // Bounds checking.
Ian Rogersef7d42f2014-01-06 12:55:46 -080081 if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) ||
82 UNLIKELY(srcPos > srcArray->GetLength() - count) ||
83 UNLIKELY(dstPos > dstArray->GetLength() - count)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000084 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080085 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
Ian Rogersef7d42f2014-01-06 12:55:46 -080086 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos,
87 count);
Elliott Hughesbf86d042011-08-31 17:53:14 -070088 return;
89 }
90
Mathieu Chartierbc5a7952016-10-17 15:46:31 -070091 ObjPtr<mirror::Class> dstComponentType = dstArray->GetClass()->GetComponentType();
92 ObjPtr<mirror::Class> srcComponentType = srcArray->GetClass()->GetComponentType();
Ian Rogersef7d42f2014-01-06 12:55:46 -080093 Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType();
Elliott Hughesbf86d042011-08-31 17:53:14 -070094
Ian Rogersef7d42f2014-01-06 12:55:46 -080095 if (LIKELY(srcComponentType == dstComponentType)) {
96 // Trivial assignability.
97 switch (dstComponentPrimitiveType) {
98 case Primitive::kPrimVoid:
99 LOG(FATAL) << "Unreachable, cannot have arrays of type void";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700100 UNREACHABLE();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800101 case Primitive::kPrimBoolean:
102 case Primitive::kPrimByte:
103 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 1U);
Vladimir Marko104883b2018-11-09 17:12:23 +0000104 // Note: Treating BooleanArray as ByteArray.
105 ObjPtr<mirror::ByteArray>::DownCast(dstArray)->Memmove(
106 dstPos, ObjPtr<mirror::ByteArray>::DownCast(srcArray), srcPos, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800107 return;
108 case Primitive::kPrimChar:
109 case Primitive::kPrimShort:
110 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 2U);
Vladimir Marko104883b2018-11-09 17:12:23 +0000111 // Note: Treating CharArray as ShortArray.
112 ObjPtr<mirror::ShortArray>::DownCast(dstArray)->Memmove(
113 dstPos, ObjPtr<mirror::ShortArray>::DownCast(srcArray), srcPos, count);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800114 return;
115 case Primitive::kPrimInt:
Andreas Gampec952ac92015-07-16 17:41:25 -0700116 case Primitive::kPrimFloat:
117 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
Vladimir Marko104883b2018-11-09 17:12:23 +0000118 // Note: Treating FloatArray as IntArray.
119 ObjPtr<mirror::IntArray>::DownCast(dstArray)->Memmove(
120 dstPos, ObjPtr<mirror::IntArray>::DownCast(srcArray), srcPos, count);
Andreas Gampec952ac92015-07-16 17:41:25 -0700121 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800122 case Primitive::kPrimLong:
Andreas Gampec952ac92015-07-16 17:41:25 -0700123 case Primitive::kPrimDouble:
124 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
Vladimir Marko104883b2018-11-09 17:12:23 +0000125 // Note: Treating DoubleArray as LongArray.
126 ObjPtr<mirror::LongArray>::DownCast(dstArray)->Memmove(
127 dstPos, ObjPtr<mirror::LongArray>::DownCast(srcArray), srcPos, count);
Andreas Gampec952ac92015-07-16 17:41:25 -0700128 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 case Primitive::kPrimNot: {
Vladimir Marko4617d582019-03-28 13:48:31 +0000130 ObjPtr<mirror::ObjectArray<mirror::Object>> dstObjArray =
Vladimir Marko104883b2018-11-09 17:12:23 +0000131 dstArray->AsObjectArray<mirror::Object>();
Vladimir Marko4617d582019-03-28 13:48:31 +0000132 ObjPtr<mirror::ObjectArray<mirror::Object>> srcObjArray =
Vladimir Marko104883b2018-11-09 17:12:23 +0000133 srcArray->AsObjectArray<mirror::Object>();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count);
135 return;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800136 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800137 default:
David Sehr709b0702016-10-13 09:12:37 -0700138 LOG(FATAL) << "Unknown array type: " << srcArray->PrettyTypeOf();
Ian Rogers2c4257b2014-10-24 14:20:06 -0700139 UNREACHABLE();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700140 }
141 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800142 // If one of the arrays holds a primitive type the other array must hold the exact same type.
143 if (UNLIKELY((dstComponentPrimitiveType != Primitive::kPrimNot) ||
144 srcComponentType->IsPrimitive())) {
David Sehr709b0702016-10-13 09:12:37 -0700145 std::string srcType(srcArray->PrettyTypeOf());
146 std::string dstType(dstArray->PrettyTypeOf());
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000147 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800148 "Incompatible types: src=%s, dst=%s",
149 srcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700150 return;
151 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800152 // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove.
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700153 ObjPtr<mirror::ObjectArray<mirror::Object>> dstObjArray =
154 dstArray->AsObjectArray<mirror::Object>();
155 ObjPtr<mirror::ObjectArray<mirror::Object>> srcObjArray =
156 srcArray->AsObjectArray<mirror::Object>();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800157 // If we're assigning into say Object[] then we don't need per element checks.
158 if (dstComponentType->IsAssignableFrom(srcComponentType)) {
159 dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count);
160 return;
161 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700162 // This code is never run under a transaction.
163 DCHECK(!Runtime::Current()->IsActiveTransaction());
164 dstObjArray->AssignableCheckingMemcpy<false>(dstPos, srcObjArray, srcPos, count, true);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700165}
166
Emma Meersmand735fe42014-06-18 11:50:59 -0700167// Template to convert general array to that of its specific primitive type.
168template <typename T>
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700169inline ObjPtr<T> AsPrimitiveArray(ObjPtr<mirror::Array> array)
170 REQUIRES_SHARED(Locks::mutator_lock_) {
171 return ObjPtr<T>::DownCast(array);
Emma Meersmand735fe42014-06-18 11:50:59 -0700172}
173
174template <typename T, Primitive::Type kPrimType>
175inline void System_arraycopyTUnchecked(JNIEnv* env, jobject javaSrc, jint srcPos,
176 jobject javaDst, jint dstPos, jint count) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700177 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700178 ObjPtr<mirror::Object> srcObject = soa.Decode<mirror::Object>(javaSrc);
179 ObjPtr<mirror::Object> dstObject = soa.Decode<mirror::Object>(javaDst);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800180 DCHECK(dstObject != nullptr);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -0700181 ObjPtr<mirror::Array> srcArray = srcObject->AsArray();
182 ObjPtr<mirror::Array> dstArray = dstObject->AsArray();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800183 DCHECK_GE(count, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 DCHECK_EQ(srcArray->GetClass(), dstArray->GetClass());
Emma Meersmand735fe42014-06-18 11:50:59 -0700185 DCHECK_EQ(srcArray->GetClass()->GetComponentType()->GetPrimitiveType(), kPrimType);
186 AsPrimitiveArray<T>(dstArray)->Memmove(dstPos, AsPrimitiveArray<T>(srcArray), srcPos, count);
187}
188
Igor Murashkin06537f72018-02-22 15:03:05 -0800189static void System_arraycopyCharUnchecked(JNIEnv* env, jclass, jcharArray javaSrc, jint srcPos,
190 jcharArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700191 System_arraycopyTUnchecked<mirror::CharArray, Primitive::kPrimChar>(env, javaSrc, srcPos,
192 javaDst, dstPos, count);
193}
194
Igor Murashkin06537f72018-02-22 15:03:05 -0800195static void System_arraycopyByteUnchecked(JNIEnv* env, jclass, jbyteArray javaSrc, jint srcPos,
196 jbyteArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700197 System_arraycopyTUnchecked<mirror::ByteArray, Primitive::kPrimByte>(env, javaSrc, srcPos,
198 javaDst, dstPos, count);
199}
200
Igor Murashkin06537f72018-02-22 15:03:05 -0800201static void System_arraycopyShortUnchecked(JNIEnv* env, jclass, jshortArray javaSrc, jint srcPos,
202 jshortArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700203 System_arraycopyTUnchecked<mirror::ShortArray, Primitive::kPrimShort>(env, javaSrc, srcPos,
204 javaDst, dstPos, count);
205}
206
Igor Murashkin06537f72018-02-22 15:03:05 -0800207static void System_arraycopyIntUnchecked(JNIEnv* env, jclass, jintArray javaSrc, jint srcPos,
208 jintArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700209 System_arraycopyTUnchecked<mirror::IntArray, Primitive::kPrimInt>(env, javaSrc, srcPos,
210 javaDst, dstPos, count);
211}
212
Igor Murashkin06537f72018-02-22 15:03:05 -0800213static void System_arraycopyLongUnchecked(JNIEnv* env, jclass, jlongArray javaSrc, jint srcPos,
214 jlongArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700215 System_arraycopyTUnchecked<mirror::LongArray, Primitive::kPrimLong>(env, javaSrc, srcPos,
216 javaDst, dstPos, count);
217}
218
Igor Murashkin06537f72018-02-22 15:03:05 -0800219static void System_arraycopyFloatUnchecked(JNIEnv* env, jclass, jfloatArray javaSrc, jint srcPos,
220 jfloatArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700221 System_arraycopyTUnchecked<mirror::FloatArray, Primitive::kPrimFloat>(env, javaSrc, srcPos,
222 javaDst, dstPos, count);
223}
224
Igor Murashkin06537f72018-02-22 15:03:05 -0800225static void System_arraycopyDoubleUnchecked(JNIEnv* env, jclass, jdoubleArray javaSrc, jint srcPos,
226 jdoubleArray javaDst, jint dstPos, jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700227 System_arraycopyTUnchecked<mirror::DoubleArray, Primitive::kPrimDouble>(env, javaSrc, srcPos,
228 javaDst, dstPos, count);
229}
230
Igor Murashkin06537f72018-02-22 15:03:05 -0800231static void System_arraycopyBooleanUnchecked(JNIEnv* env,
232 jclass,
233 jbooleanArray javaSrc,
234 jint srcPos,
235 jbooleanArray javaDst,
236 jint dstPos,
237 jint count) {
Emma Meersmand735fe42014-06-18 11:50:59 -0700238 System_arraycopyTUnchecked<mirror::BooleanArray, Primitive::kPrimBoolean>(env, javaSrc, srcPos,
239 javaDst, dstPos, count);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700240}
241
Elliott Hughes0512f022012-03-15 22:10:52 -0700242static JNINativeMethod gMethods[] = {
Igor Murashkin3b6f4402017-02-16 16:13:17 -0800243 FAST_NATIVE_METHOD(System, arraycopy, "(Ljava/lang/Object;ILjava/lang/Object;II)V"),
244 FAST_NATIVE_METHOD(System, arraycopyCharUnchecked, "([CI[CII)V"),
245 FAST_NATIVE_METHOD(System, arraycopyByteUnchecked, "([BI[BII)V"),
246 FAST_NATIVE_METHOD(System, arraycopyShortUnchecked, "([SI[SII)V"),
247 FAST_NATIVE_METHOD(System, arraycopyIntUnchecked, "([II[III)V"),
248 FAST_NATIVE_METHOD(System, arraycopyLongUnchecked, "([JI[JII)V"),
249 FAST_NATIVE_METHOD(System, arraycopyFloatUnchecked, "([FI[FII)V"),
250 FAST_NATIVE_METHOD(System, arraycopyDoubleUnchecked, "([DI[DII)V"),
251 FAST_NATIVE_METHOD(System, arraycopyBooleanUnchecked, "([ZI[ZII)V"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700252};
253
Elliott Hughesbf86d042011-08-31 17:53:14 -0700254void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700255 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700256}
257
258} // namespace art