blob: ee99e78067aae7a1c4b864d89de8e7632dc6cd47 [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
Ian Rogers62d6c772013-02-27 08:32:07 -080017#include "common_throws.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070018#include "gc/accounting/card_table-inl.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070019#include "jni_internal.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/array.h"
21#include "mirror/class.h"
22#include "mirror/class-inl.h"
23#include "mirror/object-inl.h"
Ian Rogers693ff612013-02-01 10:56:12 -080024#include "mirror/object_array-inl.h"
Ian Rogers1eb512d2013-10-18 15:42:20 -070025#include "scoped_fast_native_object_access.h"
Elliott Hughesbf86d042011-08-31 17:53:14 -070026
Elliott Hughesbf86d042011-08-31 17:53:14 -070027namespace art {
28
Ian Rogersef7d42f2014-01-06 12:55:46 -080029/*
30 * We make guarantees about the atomicity of accesses to primitive variables. These guarantees
31 * also apply to elements of arrays. In particular, 8-bit, 16-bit, and 32-bit accesses must not
32 * cause "word tearing". Accesses to 64-bit array elements may be two 32-bit operations.
33 * References are never torn regardless of the number of bits used to represent them.
34 */
35
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080036static void ThrowArrayStoreException_NotAnArray(const char* identifier, mirror::Object* array)
Ian Rogersb726dcb2012-09-05 08:57:23 -070037 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Elliott Hughes54e7df12011-09-16 11:47:04 -070038 std::string actualType(PrettyTypeOf(array));
Ian Rogers62d6c772013-02-27 08:32:07 -080039 Thread* self = Thread::Current();
40 ThrowLocation throw_location = self->GetCurrentLocationForThrow();
41 self->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
42 "%s of type %s is not an array", identifier, actualType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -070043}
44
Ian Rogersef7d42f2014-01-06 12:55:46 -080045static void System_arraycopy(JNIEnv* env, jclass, jobject javaSrc, jint srcPos, jobject javaDst,
46 jint dstPos, jint length) {
47 // The API is defined in terms of length, but length is somewhat overloaded so we use count.
48 const jint count = length;
Ian Rogers1eb512d2013-10-18 15:42:20 -070049 ScopedFastNativeObjectAccess soa(env);
Elliott Hughesbf86d042011-08-31 17:53:14 -070050
51 // Null pointer checks.
Ian Rogersef7d42f2014-01-06 12:55:46 -080052 if (UNLIKELY(javaSrc == nullptr)) {
53 ThrowNullPointerException(nullptr, "src == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070054 return;
55 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080056 if (UNLIKELY(javaDst == nullptr)) {
57 ThrowNullPointerException(nullptr, "dst == null");
Elliott Hughesbf86d042011-08-31 17:53:14 -070058 return;
59 }
60
61 // Make sure source and destination are both arrays.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080062 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
Ian Rogers62d6c772013-02-27 08:32:07 -080063 if (UNLIKELY(!srcObject->IsArrayInstance())) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -080064 ThrowArrayStoreException_NotAnArray("source", srcObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070065 return;
66 }
Ian Rogersef7d42f2014-01-06 12:55:46 -080067 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Ian Rogers62d6c772013-02-27 08:32:07 -080068 if (UNLIKELY(!dstObject->IsArrayInstance())) {
Elliott Hughes81aa5b32012-01-17 13:47:13 -080069 ThrowArrayStoreException_NotAnArray("destination", dstObject);
Elliott Hughesbf86d042011-08-31 17:53:14 -070070 return;
71 }
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080072 mirror::Array* srcArray = srcObject->AsArray();
73 mirror::Array* dstArray = dstObject->AsArray();
Elliott Hughesbf86d042011-08-31 17:53:14 -070074
75 // Bounds checking.
Ian Rogersef7d42f2014-01-06 12:55:46 -080076 if (UNLIKELY(srcPos < 0) || UNLIKELY(dstPos < 0) || UNLIKELY(count < 0) ||
77 UNLIKELY(srcPos > srcArray->GetLength() - count) ||
78 UNLIKELY(dstPos > dstArray->GetLength() - count)) {
Ian Rogers62d6c772013-02-27 08:32:07 -080079 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
80 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayIndexOutOfBoundsException;",
81 "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";
96 return;
97 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:
108 case Primitive::kPrimFloat:
109 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 4U);
110 dstArray->AsIntArray()->Memmove(dstPos, srcArray->AsIntArray(), srcPos, count);
111 return;
112 case Primitive::kPrimLong:
113 case Primitive::kPrimDouble:
114 DCHECK_EQ(Primitive::ComponentSize(dstComponentPrimitiveType), 8U);
115 dstArray->AsLongArray()->Memmove(dstPos, srcArray->AsLongArray(), srcPos, count);
116 return;
117 case Primitive::kPrimNot: {
118 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
119 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
120 dstObjArray->AssignableMemmove(dstPos, srcObjArray, srcPos, count);
121 return;
Elliott Hughesab3530d2012-01-09 16:04:56 -0800122 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800123 default:
124 LOG(FATAL) << "Unknown array type: " << PrettyTypeOf(srcArray);
125 return;
Elliott Hughesbf86d042011-08-31 17:53:14 -0700126 }
127 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800128 // If one of the arrays holds a primitive type the other array must hold the exact same type.
129 if (UNLIKELY((dstComponentPrimitiveType != Primitive::kPrimNot) ||
130 srcComponentType->IsPrimitive())) {
131 std::string srcType(PrettyTypeOf(srcArray));
Elliott Hughes54e7df12011-09-16 11:47:04 -0700132 std::string dstType(PrettyTypeOf(dstArray));
Ian Rogers62d6c772013-02-27 08:32:07 -0800133 ThrowLocation throw_location = soa.Self()->GetCurrentLocationForThrow();
134 soa.Self()->ThrowNewExceptionF(throw_location, "Ljava/lang/ArrayStoreException;",
Ian Rogersef7d42f2014-01-06 12:55:46 -0800135 "Incompatible types: src=%s, dst=%s",
136 srcType.c_str(), dstType.c_str());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700137 return;
138 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800139 // Arrays hold distinct types and so therefore can't alias - use memcpy instead of memmove.
140 mirror::ObjectArray<mirror::Object>* dstObjArray = dstArray->AsObjectArray<mirror::Object>();
141 mirror::ObjectArray<mirror::Object>* srcObjArray = srcArray->AsObjectArray<mirror::Object>();
142 // If we're assigning into say Object[] then we don't need per element checks.
143 if (dstComponentType->IsAssignableFrom(srcComponentType)) {
144 dstObjArray->AssignableMemcpy(dstPos, srcObjArray, srcPos, count);
145 return;
146 }
147 dstObjArray->AssignableCheckingMemcpy(dstPos, srcObjArray, srcPos, count, true);
Elliott Hughesbf86d042011-08-31 17:53:14 -0700148}
149
Emma Meersmand735fe42014-06-18 11:50:59 -0700150// Template to convert general array to that of its specific primitive type.
151template <typename T>
152inline T* AsPrimitiveArray(mirror::Array* array) {
153 return down_cast<T*>(array);
154}
155
156template <typename T, Primitive::Type kPrimType>
157inline void System_arraycopyTUnchecked(JNIEnv* env, jobject javaSrc, jint srcPos,
158 jobject javaDst, jint dstPos, jint count) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700159 ScopedFastNativeObjectAccess soa(env);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700160 mirror::Object* srcObject = soa.Decode<mirror::Object*>(javaSrc);
161 mirror::Object* dstObject = soa.Decode<mirror::Object*>(javaDst);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800162 DCHECK(dstObject != nullptr);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700163 mirror::Array* srcArray = srcObject->AsArray();
164 mirror::Array* dstArray = dstObject->AsArray();
Ian Rogersef7d42f2014-01-06 12:55:46 -0800165 DCHECK_GE(count, 0);
Ian Rogersef7d42f2014-01-06 12:55:46 -0800166 DCHECK_EQ(srcArray->GetClass(), dstArray->GetClass());
Emma Meersmand735fe42014-06-18 11:50:59 -0700167 DCHECK_EQ(srcArray->GetClass()->GetComponentType()->GetPrimitiveType(), kPrimType);
168 AsPrimitiveArray<T>(dstArray)->Memmove(dstPos, AsPrimitiveArray<T>(srcArray), srcPos, count);
169}
170
171static void System_arraycopyCharUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
172 jobject javaDst, jint dstPos, jint count) {
173 System_arraycopyTUnchecked<mirror::CharArray, Primitive::kPrimChar>(env, javaSrc, srcPos,
174 javaDst, dstPos, count);
175}
176
177static void System_arraycopyByteUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
178 jobject javaDst, jint dstPos, jint count) {
179 System_arraycopyTUnchecked<mirror::ByteArray, Primitive::kPrimByte>(env, javaSrc, srcPos,
180 javaDst, dstPos, count);
181}
182
183static void System_arraycopyShortUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
184 jobject javaDst, jint dstPos, jint count) {
185 System_arraycopyTUnchecked<mirror::ShortArray, Primitive::kPrimShort>(env, javaSrc, srcPos,
186 javaDst, dstPos, count);
187}
188
189static void System_arraycopyIntUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
190 jobject javaDst, jint dstPos, jint count) {
191 System_arraycopyTUnchecked<mirror::IntArray, Primitive::kPrimInt>(env, javaSrc, srcPos,
192 javaDst, dstPos, count);
193}
194
195static void System_arraycopyLongUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
196 jobject javaDst, jint dstPos, jint count) {
197 System_arraycopyTUnchecked<mirror::LongArray, Primitive::kPrimLong>(env, javaSrc, srcPos,
198 javaDst, dstPos, count);
199}
200
201static void System_arraycopyFloatUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
202 jobject javaDst, jint dstPos, jint count) {
203 System_arraycopyTUnchecked<mirror::FloatArray, Primitive::kPrimFloat>(env, javaSrc, srcPos,
204 javaDst, dstPos, count);
205}
206
207static void System_arraycopyDoubleUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
208 jobject javaDst, jint dstPos, jint count) {
209 System_arraycopyTUnchecked<mirror::DoubleArray, Primitive::kPrimDouble>(env, javaSrc, srcPos,
210 javaDst, dstPos, count);
211}
212
213static void System_arraycopyBooleanUnchecked(JNIEnv* env, jclass, jobject javaSrc, jint srcPos,
214 jobject javaDst, jint dstPos, jint count) {
215 System_arraycopyTUnchecked<mirror::BooleanArray, Primitive::kPrimBoolean>(env, javaSrc, srcPos,
216 javaDst, dstPos, count);
Hiroshi Yamauchif38ea802013-08-27 13:04:26 -0700217}
218
Elliott Hughes0512f022012-03-15 22:10:52 -0700219static jint System_identityHashCode(JNIEnv* env, jclass, jobject javaObject) {
Ian Rogersef7d42f2014-01-06 12:55:46 -0800220 if (UNLIKELY(javaObject == nullptr)) {
Mathieu Chartierad2541a2013-10-25 10:05:23 -0700221 return 0;
222 }
Ian Rogers1eb512d2013-10-18 15:42:20 -0700223 ScopedFastNativeObjectAccess soa(env);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800224 mirror::Object* o = soa.Decode<mirror::Object*>(javaObject);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700225 return static_cast<jint>(o->IdentityHashCode());
Elliott Hughesbf86d042011-08-31 17:53:14 -0700226}
227
Elliott Hughes0512f022012-03-15 22:10:52 -0700228static JNINativeMethod gMethods[] = {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700229 NATIVE_METHOD(System, arraycopy, "!(Ljava/lang/Object;ILjava/lang/Object;II)V"),
230 NATIVE_METHOD(System, arraycopyCharUnchecked, "!([CI[CII)V"),
Emma Meersmand735fe42014-06-18 11:50:59 -0700231 NATIVE_METHOD(System, arraycopyByteUnchecked, "!([BI[BII)V"),
232 NATIVE_METHOD(System, arraycopyShortUnchecked, "!([SI[SII)V"),
233 NATIVE_METHOD(System, arraycopyIntUnchecked, "!([II[III)V"),
234 NATIVE_METHOD(System, arraycopyLongUnchecked, "!([JI[JII)V"),
235 NATIVE_METHOD(System, arraycopyFloatUnchecked, "!([FI[FII)V"),
236 NATIVE_METHOD(System, arraycopyDoubleUnchecked, "!([DI[DII)V"),
237 NATIVE_METHOD(System, arraycopyBooleanUnchecked, "!([ZI[ZII)V"),
Ian Rogers1eb512d2013-10-18 15:42:20 -0700238 NATIVE_METHOD(System, identityHashCode, "!(Ljava/lang/Object;)I"),
Elliott Hughesbf86d042011-08-31 17:53:14 -0700239};
240
Elliott Hughesbf86d042011-08-31 17:53:14 -0700241void register_java_lang_System(JNIEnv* env) {
Elliott Hugheseac76672012-05-24 21:56:51 -0700242 REGISTER_NATIVE_METHODS("java/lang/System");
Elliott Hughesbf86d042011-08-31 17:53:14 -0700243}
244
245} // namespace art