blob: d9863c579e2117508a0f74018a5b7c9cdb0c16c3 [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"
Ian Rogers1eb512d2013-10-18 15:42:20 -070027#include "scoped_fast_native_object_access.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
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080038static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array)
Mathieu Chartier90443472015-07-16 20:32:27 -070039 SHARED_REQUIRES(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -070040 std::string actualType(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -080041 Thread* self = Thread::Current();
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000042 self->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080043 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070044}
45
Ian Rogersef7d42f2014-01-06 12:55:46 -080046static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst,
47 jint dstPos, jint length) {
48 // The API is defined in terms of length, but length is somewhat overloaded so we use count.
49 const jint count = length;
Ian Rogers1eb512d2013-10-18 15:42:20 -070050 ScopedFastNativeObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070051
52 // Null pointer checks.
Ian Rogersef7d42f2014-01-06 12:55:46 -080053 if (UNLIKELY(javaSrc == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000054 ThrowNullPointerException("src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070055 return;
56 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080057 if (UNLIKELY(javaDst == nullptr)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000058 ThrowNullPointerException("dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070059 return;
60 }
61
62 // Make sure source and destination are both arrays.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080063 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
Ian Rogers62d6c772013-02-27 08:32:07 -080064 if (UNLIKELY(!srcObject->IsArrayInstance())) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -080065 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070066 return;
67 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080068 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Ian Rogers62d6c772013-02-27 08:32:07 -080069 if (UNLIKELY(!dstObject->IsArrayInstance())) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -080070 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070071 return;
72 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080073 mirror::Array* srcArray = srcObject->AsArray();
74 mirror::Array* dstArray = dstObject->AsArray();
Elliott Hughesbf86d042011-08-31 17:53:14 -070075
76 // Bounds checking.
Ian Rogersef7d42f2014-01-06 12:55:46 -080077 if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) ||
78 UNLIKELY(srcPos > srcArray->GetLength() - count) ||
79 UNLIKELY(dstPos > dstArray->GetLength() - count)) {
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +000080 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
Ian Rogers62d6c772013-02-27 08:32:07 -080081 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
Ian Rogersef7d42f2014-01-06 12:55:46 -080082 srcArray->GetLength(), srcPos, dstArray->GetLength(), dstPos,
83 count);
Elliott Hughesbf86d042011-08-31 17:53:14 -070084 return;
85 }
86
Ian Rogersef7d42f2014-01-06 12:55:46 -080087 mirror::Class* dstComponentType = dstArray->GetClass()->GetComponentType();
88 mirror::Class* srcComponentType = srcArray->GetClass()->GetComponentType();
89 Primitive::Type dstComponentPrimitiveType = dstComponentType->GetPrimitiveType();
Elliott Hughesbf86d042011-08-31 17:53:14 -070090
Ian Rogersef7d42f2014-01-06 12:55:46 -080091 if (LIKELY(srcComponentType == dstComponentType)) {
92 // Trivial assignability.
93 switch (dstComponentPrimitiveType) {
94 case Primitive::kPrimVoid:
95 LOG(FATAL) << "Unreachable, cannot have arrays of type void";
Ian Rogers2c4257b2014-10-24 14:20:06 -070096 UNREACHABLE();
Ian Rogersef7d42f2014-01-06 12:55:46 -080097 case Primitive::kPrimBoolean:
98 case Primitive::kPrimByte:
99 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 1U);
100 dstArray->AsByteSizedArray()->Memmove(dstPos, srcArray->AsByteSizedArray(), srcPos, count);
101 return;
102 case Primitive::kPrimChar:
103 case Primitive::kPrimShort:
104 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 2U);
105 dstArray->AsShortSizedArray()->Memmove(dstPos, srcArray->AsShortSizedArray(), srcPos, count);
106 return;
107 case Primitive::kPrimInt:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800108 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
109 dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count);
110 return;
Andreas Gampec952ac92015-07-16 17:41:25 -0700111 case Primitive::kPrimFloat:
112 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
113 dstArray->AsFloatArray()->Memmove(dstPos, srcArray->AsFloatArray(), srcPos, count);
114 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800115 case Primitive::kPrimLong:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800116 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
117 dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count);
118 return;
Andreas Gampec952ac92015-07-16 17:41:25 -0700119 case Primitive::kPrimDouble:
120 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
121 dstArray->AsDoubleArray()->Memmove(dstPos, srcArray->AsDoubleArray(), srcPos, count);
122 return;
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 case Primitive::kPrimNot: {
124 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
125 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
126 dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count);
127 return;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800128 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800129 default:
130 LOG(FATAL) << "Unknown array type: " << PrettyTypeOf(srcArray);
Ian Rogers2c4257b2014-10-24 14:20:06 -0700131 UNREACHABLE();
Elliott Hughesbf86d042011-08-31 17:53:14 -0700132 }
133 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800134 // If one of the arrays holds a primitive type the other array must hold the exact same type.
135 if (UNLIKELY((dstComponentPrimitiveType != Primitive::kPrimNot) ||
136 srcComponentType->IsPrimitive())) {
137 std::string srcType(PrettyTypeOf(srcArray));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700138 std::string dstType(PrettyTypeOf(dstArray));
Nicolas Geoffray0aa50ce2015-03-10 11:03:29 +0000139 soa.Self()->ThrowNewExceptionF("Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800140 "Incompatible types: src=%s, dst=%s",
141 srcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700142 return;
143 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800144 // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove.
145 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
146 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
147 // If we're assigning into say Object[] then we don't need per element checks.
148 if (dstComponentType->IsAssignableFrom(srcComponentType)) {
149 dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count);
150 return;
151 }
152 dstObjArray->AssignableCheckingMemcpy(dstPos, srcObjArray, srcPos, count, true);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700153}
154
Emma Meersmand735fe42014-06-18 11:50:59 -0700155// Template to convert general array to that of its specific primitive type.
156template <typename T>
157inline T* AsPrimitiveArray(mirror::Array* array) {
158 return down_cast<T*>(array);
159}
160
161template <typename T, Primitive::Type kPrimType>
162inline void System_arraycopyTUnchecked(JNIEnv* env, jobject javaSrc, jint srcPos,
163 jobject javaDst, jint dstPos, jint count) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700164 ScopedFastNativeObjectAccess soa(env);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700165 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
166 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800167 DCHECK(dstObject != nullptr);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700168 mirror::Array* srcArray = srcObject->AsArray();
169 mirror::Array* dstArray = dstObject->AsArray();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800170 DCHECK_GE(count, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800171 DCHECK_EQ(srcArray->GetClass(), dstArray->GetClass());
Emma Meersmand735fe42014-06-18 11:50:59 -0700172 DCHECK_EQ(srcArray->GetClass()->GetComponentType()->GetPrimitiveType(), kPrimType);
173 AsPrimitiveArray<T>(dstArray)->Memmove(dstPos, AsPrimitiveArray<T>(srcArray), srcPos, count);
174}
175
176static void System_arraycopyCharUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
177 jobject javaDst, jint dstPos, jint count) {
178 System_arraycopyTUnchecked<mirror::CharArray, Primitive::kPrimChar>(env, javaSrc, srcPos,
179 javaDst, dstPos, count);
180}
181
182static void System_arraycopyByteUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
183 jobject javaDst, jint dstPos, jint count) {
184 System_arraycopyTUnchecked<mirror::ByteArray, Primitive::kPrimByte>(env, javaSrc, srcPos,
185 javaDst, dstPos, count);
186}
187
188static void System_arraycopyShortUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
189 jobject javaDst, jint dstPos, jint count) {
190 System_arraycopyTUnchecked<mirror::ShortArray, Primitive::kPrimShort>(env, javaSrc, srcPos,
191 javaDst, dstPos, count);
192}
193
194static void System_arraycopyIntUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
195 jobject javaDst, jint dstPos, jint count) {
196 System_arraycopyTUnchecked<mirror::IntArray, Primitive::kPrimInt>(env, javaSrc, srcPos,
197 javaDst, dstPos, count);
198}
199
200static void System_arraycopyLongUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
201 jobject javaDst, jint dstPos, jint count) {
202 System_arraycopyTUnchecked<mirror::LongArray, Primitive::kPrimLong>(env, javaSrc, srcPos,
203 javaDst, dstPos, count);
204}
205
206static void System_arraycopyFloatUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
207 jobject javaDst, jint dstPos, jint count) {
208 System_arraycopyTUnchecked<mirror::FloatArray, Primitive::kPrimFloat>(env, javaSrc, srcPos,
209 javaDst, dstPos, count);
210}
211
212static void System_arraycopyDoubleUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
213 jobject javaDst, jint dstPos, jint count) {
214 System_arraycopyTUnchecked<mirror::DoubleArray, Primitive::kPrimDouble>(env, javaSrc, srcPos,
215 javaDst, dstPos, count);
216}
217
218static void System_arraycopyBooleanUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
219 jobject javaDst, jint dstPos, jint count) {
220 System_arraycopyTUnchecked<mirror::BooleanArray, Primitive::kPrimBoolean>(env, javaSrc, srcPos,
221 javaDst, dstPos, count);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700222}
223
Elliott Hughes0512f022012-03-15 22:10:52 -0700224static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800225 if (UNLIKELY(javaObject == nullptr)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700226 return 0;
227 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700228 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800229 mirror::Object* o = soa.Decode<mirror::Object*>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700230 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700231}
232
Elliott Hughes0512f022012-03-15 22:10:52 -0700233static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700234 NATIVE_METHOD(System, arraycopy, "!(Ljava/lang/Object;ILjava/lang/Object;II)V"),
235 NATIVE_METHOD(System, arraycopyCharUnchecked, "!([CI[CII)V"),
Emma Meersmand735fe42014-06-18 11:50:59 -0700236 NATIVE_METHOD(System, arraycopyByteUnchecked, "!([BI[BII)V"),
237 NATIVE_METHOD(System, arraycopyShortUnchecked, "!([SI[SII)V"),
238 NATIVE_METHOD(System, arraycopyIntUnchecked, "!([II[III)V"),
239 NATIVE_METHOD(System, arraycopyLongUnchecked, "!([JI[JII)V"),
240 NATIVE_METHOD(System, arraycopyFloatUnchecked, "!([FI[FII)V"),
241 NATIVE_METHOD(System, arraycopyDoubleUnchecked, "!([DI[DII)V"),
242 NATIVE_METHOD(System, arraycopyBooleanUnchecked, "!([ZI[ZII)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700243 NATIVE_METHOD(System, identityHashCode, "!(Ljava/lang/Object;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700244};
245
Elliott Hughesbf86d042011-08-31 17:53:14 -0700246void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700247 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700248}
249
250} // namespace art