blob: 3f5fa73b459009f3feb471683bf81be3d55d147f [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
Ian Rogers62d6c772013-02-27 08:32:07 -080019#include "common_throws.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070020#include "gc/accounting/card_table-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070021#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080022#include "mirror/array.h"
23#include "mirror/class.h"
24#include "mirror/class-inl.h"
25#include "mirror/object-inl.h"
Ian Rogers693ff612013-02-01 10:56:12 -080026#include "mirror/object_array-inl.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070027#include "scoped_fast_native_object_access-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070028
Elliott Hughesbf86d042011-08-31 17:53:14 -070029namespace art {
30
Ian Rogersef7d42f2014-01-06 12:55:46 -080031/*
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
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070038static void ThrowArrayStoreException_NotAnArray(const char* identifier,
39 ObjPtr<mirror::Object> array)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070040 REQUIRES_SHARED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -070041 std::string actualType(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -080042 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000043 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080044 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070045}
46
Ian Rogersef7d42f2014-01-06 12:55:46 -080047static 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 Rogers1eb512d2013-10-18 15:42:20 -070051 ScopedFastNativeObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070052
53 // Null pointer checks.
Ian Rogersef7d42f2014-01-06 12:55:46 -080054 if (UNLIKELY(javaSrc == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000055 ThrowNullPointerException("src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070056 return;
57 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080058 if (UNLIKELY(javaDst == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000059 ThrowNullPointerException("dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070060 return;
61 }
62
63 // Make sure source and destination are both arrays.
Mathieu Chartier0795f232016-09-27 18:43:30 -070064 ObjPtr<mirror::Object> srcObject = soa.Decode<mirror::Object>(javaSrc);
Ian Rogers62d6c772013-02-27 08:32:07 -080065 if (UNLIKELY(!srcObject->IsArrayInstance())) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070066 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070067 return;
68 }
Mathieu Chartier0795f232016-09-27 18:43:30 -070069 ObjPtr<mirror::Object> dstObject = soa.Decode<mirror::Object>(javaDst);
Ian Rogers62d6c772013-02-27 08:32:07 -080070 if (UNLIKELY(!dstObject->IsArrayInstance())) {
Mathieu Chartier6b3d12b2016-10-13 13:59:58 -070071 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070072 return;
73 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080074 mirror::Array* srcArray = srcObject->AsArray();
75 mirror::Array* dstArray = dstObject->AsArray();
Elliott Hughesbf86d042011-08-31 17:53:14 -070076
77 // Bounds checking.
Ian Rogersef7d42f2014-01-06 12:55:46 -080078 if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) ||
79 UNLIKELY(srcPos > srcArray->GetLength() - count) ||
80 UNLIKELY(dstPos > dstArray->GetLength() - count)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000081 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080082 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
Ian Rogersef7d42f2014-01-06 12:55:46 -080083 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos,
84 count);
Elliott Hughesbf86d042011-08-31 17:53:14 -070085 return;
86 }
87
Ian Rogersef7d42f2014-01-06 12:55:46 -080088 mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType();
89 mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType();
90 Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType();
Elliott Hughesbf86d042011-08-31 17:53:14 -070091
Ian Rogersef7d42f2014-01-06 12:55:46 -080092 if (LIKELY(srcComponentType == dstComponentType)) {
93 // Trivial assignability.
94 switch (dstComponentPrimitiveType) {
95 case Primitive::kPrimVoid:
96 LOG(FATAL) << "Unreachable, cannot have arrays of type void";
Ian Rogers2c4257b2014-10-24 14:20:06 -070097 UNREACHABLE();
Ian Rogersef7d42f2014-01-06 12:55:46 -080098 case Primitive::kPrimBoolean:
99 case Primitive::kPrimByte:
100 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 1U);
101 dstArray->AsByteSizedArray()->Memmove(dstPos, srcArray->AsByteSizedArray(), srcPos, count);
102 return;
103 case Primitive::kPrimChar:
104 case Primitive::kPrimShort:
105 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 2U);
106 dstArray->AsShortSizedArray()->Memmove(dstPos, srcArray->AsShortSizedArray(), srcPos, count);
107 return;
108 case Primitive::kPrimInt:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800109 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
110 dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count);
111 return;
Andreas Gampec952ac92015-07-16 17:41:25 -0700112 case Primitive::kPrimFloat:
113 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
114 dstArray->AsFloatArray()->Memmove(dstPos, srcArray->AsFloatArray(), srcPos, count);
115 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800117 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
118 dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count);
119 return;
Andreas Gampec952ac92015-07-16 17:41:25 -0700120 case Primitive::kPrimDouble:
121 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
122 dstArray->AsDoubleArray()->Memmove(dstPos, srcArray->AsDoubleArray(), srcPos, count);
123 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800124 case Primitive::kPrimNot: {
125 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
126 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
127 dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count);
128 return;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800129 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800130 default:
131 LOG(FATAL) << "Unknown array type: " << PrettyTypeOf(srcArray);
Ian Rogers2c4257b2014-10-24 14:20:06 -0700132 UNREACHABLE();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700133 }
134 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 // If one of the arrays holds a primitive type the other array must hold the exact same type.
136 if (UNLIKELY((dstComponentPrimitiveType != Primitive::kPrimNot) ||
137 srcComponentType->IsPrimitive())) {
138 std::string srcType(PrettyTypeOf(srcArray));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700139 std::string dstType(PrettyTypeOf(dstArray));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000140 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800141 "Incompatible types: src=%s, dst=%s",
142 srcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700143 return;
144 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800145 // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove.
146 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
147 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
148 // If we're assigning into say Object[] then we don't need per element checks.
149 if (dstComponentType->IsAssignableFrom(srcComponentType)) {
150 dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count);
151 return;
152 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700153 // This code is never run under a transaction.
154 DCHECK(!Runtime::Current()->IsActiveTransaction());
155 dstObjArray->AssignableCheckingMemcpy<false>(dstPos, srcObjArray, srcPos, count, true);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700156}
157
Emma Meersmand735fe42014-06-18 11:50:59 -0700158// Template to convert general array to that of its specific primitive type.
159template <typename T>
160inline T* AsPrimitiveArray(mirror::Array* array) {
161 return down_cast<T*>(array);
162}
163
164template <typename T, Primitive::Type kPrimType>
165inline void System_arraycopyTUnchecked(JNIEnv* env, jobject javaSrc, jint srcPos,
166 jobject javaDst, jint dstPos, jint count) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700167 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700168 ObjPtr<mirror::Object> srcObject = soa.Decode<mirror::Object>(javaSrc);
169 ObjPtr<mirror::Object> dstObject = soa.Decode<mirror::Object>(javaDst);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 DCHECK(dstObject != nullptr);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700171 mirror::Array* srcArray = srcObject->AsArray();
172 mirror::Array* dstArray = dstObject->AsArray();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800173 DCHECK_GE(count, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800174 DCHECK_EQ(srcArray->GetClass(), dstArray->GetClass());
Emma Meersmand735fe42014-06-18 11:50:59 -0700175 DCHECK_EQ(srcArray->GetClass()->GetComponentType()->GetPrimitiveType(), kPrimType);
176 AsPrimitiveArray<T>(dstArray)->Memmove(dstPos, AsPrimitiveArray<T>(srcArray), srcPos, count);
177}
178
179static void System_arraycopyCharUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
180 jobject javaDst, jint dstPos, jint count) {
181 System_arraycopyTUnchecked<mirror::CharArray, Primitive::kPrimChar>(env, javaSrc, srcPos,
182 javaDst, dstPos, count);
183}
184
185static void System_arraycopyByteUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
186 jobject javaDst, jint dstPos, jint count) {
187 System_arraycopyTUnchecked<mirror::ByteArray, Primitive::kPrimByte>(env, javaSrc, srcPos,
188 javaDst, dstPos, count);
189}
190
191static void System_arraycopyShortUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
192 jobject javaDst, jint dstPos, jint count) {
193 System_arraycopyTUnchecked<mirror::ShortArray, Primitive::kPrimShort>(env, javaSrc, srcPos,
194 javaDst, dstPos, count);
195}
196
197static void System_arraycopyIntUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
198 jobject javaDst, jint dstPos, jint count) {
199 System_arraycopyTUnchecked<mirror::IntArray, Primitive::kPrimInt>(env, javaSrc, srcPos,
200 javaDst, dstPos, count);
201}
202
203static void System_arraycopyLongUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
204 jobject javaDst, jint dstPos, jint count) {
205 System_arraycopyTUnchecked<mirror::LongArray, Primitive::kPrimLong>(env, javaSrc, srcPos,
206 javaDst, dstPos, count);
207}
208
209static void System_arraycopyFloatUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
210 jobject javaDst, jint dstPos, jint count) {
211 System_arraycopyTUnchecked<mirror::FloatArray, Primitive::kPrimFloat>(env, javaSrc, srcPos,
212 javaDst, dstPos, count);
213}
214
215static void System_arraycopyDoubleUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
216 jobject javaDst, jint dstPos, jint count) {
217 System_arraycopyTUnchecked<mirror::DoubleArray, Primitive::kPrimDouble>(env, javaSrc, srcPos,
218 javaDst, dstPos, count);
219}
220
221static void System_arraycopyBooleanUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
222 jobject javaDst, jint dstPos, jint count) {
223 System_arraycopyTUnchecked<mirror::BooleanArray, Primitive::kPrimBoolean>(env, javaSrc, srcPos,
224 javaDst, dstPos, count);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700225}
226
Elliott Hughes0512f022012-03-15 22:10:52 -0700227static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800228 if (UNLIKELY(javaObject == nullptr)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700229 return 0;
230 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700231 ScopedFastNativeObjectAccess soa(env);
Mathieu Chartier0795f232016-09-27 18:43:30 -0700232 ObjPtr<mirror::Object> o = soa.Decode<mirror::Object>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700233 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700234}
235
Elliott Hughes0512f022012-03-15 22:10:52 -0700236static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700237 NATIVE_METHOD(System, arraycopy, "!(Ljava/lang/Object;ILjava/lang/Object;II)V"),
238 NATIVE_METHOD(System, arraycopyCharUnchecked, "!([CI[CII)V"),
Emma Meersmand735fe42014-06-18 11:50:59 -0700239 NATIVE_METHOD(System, arraycopyByteUnchecked, "!([BI[BII)V"),
240 NATIVE_METHOD(System, arraycopyShortUnchecked, "!([SI[SII)V"),
241 NATIVE_METHOD(System, arraycopyIntUnchecked, "!([II[III)V"),
242 NATIVE_METHOD(System, arraycopyLongUnchecked, "!([JI[JII)V"),
243 NATIVE_METHOD(System, arraycopyFloatUnchecked, "!([FI[FII)V"),
244 NATIVE_METHOD(System, arraycopyDoubleUnchecked, "!([DI[DII)V"),
245 NATIVE_METHOD(System, arraycopyBooleanUnchecked, "!([ZI[ZII)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700246 NATIVE_METHOD(System, identityHashCode, "!(Ljava/lang/Object;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700247};
248
Elliott Hughesbf86d042011-08-31 17:53:14 -0700249void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700250 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700251}
252
253} // namespace art