blob: 49db49b6be538f39223f3e39d15186608145b4bb [file] [log] [blame]
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001/*
2 * Copyright (C) 2015 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
17#include "unstarted_runtime.h"
18
19#include <cmath>
20#include <unordered_map>
21
Andreas Gampeaacc25d2015-04-01 14:49:06 -070022#include "ScopedLocalRef.h"
23
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080025#include "base/casts.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include "base/logging.h"
27#include "base/macros.h"
28#include "class_linker.h"
29#include "common_throws.h"
30#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080031#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070032#include "handle_scope-inl.h"
33#include "interpreter/interpreter_common.h"
34#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070036#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/string-inl.h"
40#include "nth_caller_visitor.h"
41#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020042#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070044#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045
46namespace art {
47namespace interpreter {
48
Andreas Gampe068b0c02015-03-11 12:44:47 -070049static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020050 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020052
53static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070054 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070055 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020056 va_start(args, fmt);
57 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070058 va_end(args);
59 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020060 va_start(args, fmt);
61 std::string msg;
62 StringAppendV(&msg, fmt, args);
63 va_end(args);
64 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 UNREACHABLE();
66 }
67}
68
Andreas Gampe2969bcd2015-03-09 12:57:41 -070069// Helper function to deal with class loading in an unstarted runtime.
70static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
71 Handle<mirror::ClassLoader> class_loader, JValue* result,
72 const std::string& method_name, bool initialize_class,
73 bool abort_if_not_found)
Mathieu Chartier90443472015-07-16 20:32:27 -070074 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -070075 CHECK(className.Get() != nullptr);
76 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
77 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
78
79 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
80 if (found == nullptr && abort_if_not_found) {
81 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070082 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
83 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -070084 }
85 return;
86 }
87 if (found != nullptr && initialize_class) {
88 StackHandleScope<1> hs(self);
89 Handle<mirror::Class> h_class(hs.NewHandle(found));
90 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
91 CHECK(self->IsExceptionPending());
92 return;
93 }
94 }
95 result->SetL(found);
96}
97
98// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
99// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
100// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200101// actually the transaction abort exception. This must not be wrapped, as it signals an
102// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700103static void CheckExceptionGenerateClassNotFound(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700105 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200106 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700107 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200108 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700109 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
110 "ClassNotFoundException");
111 }
112 }
113}
114
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700115static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700117 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
118 if (param == nullptr) {
119 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
120 return nullptr;
121 }
122 return param->AsString();
123}
124
Andreas Gampe799681b2015-05-15 19:24:12 -0700125void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700127 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
128 if (class_name == nullptr) {
129 return;
130 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700131 StackHandleScope<1> hs(self);
132 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800133 UnstartedRuntimeFindClass(self,
134 h_class_name,
135 ScopedNullHandle<mirror::ClassLoader>(),
136 result,
137 "Class.forName",
138 true,
139 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700140 CheckExceptionGenerateClassNotFound(self);
141}
142
Andreas Gampe799681b2015-05-15 19:24:12 -0700143void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700145 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
146 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700147 return;
148 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
150 mirror::ClassLoader* class_loader =
151 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
152 StackHandleScope<2> hs(self);
153 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
154 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
155 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
156 initialize_class, false);
157 CheckExceptionGenerateClassNotFound(self);
158}
159
Andreas Gampe799681b2015-05-15 19:24:12 -0700160void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700162 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
163 if (class_name == nullptr) {
164 return;
165 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700166 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
167 mirror::ClassLoader* class_loader =
168 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
169 StackHandleScope<2> hs(self);
170 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
171 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
172 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
173 initialize_class, false);
174 CheckExceptionGenerateClassNotFound(self);
175}
176
Andreas Gampe799681b2015-05-15 19:24:12 -0700177void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
179 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700180 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
181 if (param == nullptr) {
182 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
183 return;
184 }
185 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700186 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700187
188 // Check that it's not null.
189 if (h_klass.Get() == nullptr) {
190 AbortTransactionOrFail(self, "Class reference is null for newInstance");
191 return;
192 }
193
194 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
195 if (Runtime::Current()->IsActiveTransaction()) {
196 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200197 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
198 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700199 return;
200 }
201 }
202
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700203 // There are two situations in which we'll abort this run.
204 // 1) If the class isn't yet initialized and initialization fails.
205 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
206 // Note that 2) could likely be handled here, but for safety abort the transaction.
207 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 auto* cl = Runtime::Current()->GetClassLinker();
209 if (cl->EnsureInitialized(self, h_klass, true, true)) {
210 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
211 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700212 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
213 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700215 if (!self->IsExceptionPending()) {
216 result->SetL(h_obj.Get());
217 ok = true;
218 }
219 } else {
220 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
221 "Could not find default constructor for '%s'",
222 PrettyClass(h_klass.Get()).c_str());
223 }
224 }
225 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700226 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
227 PrettyClass(h_klass.Get()).c_str(),
228 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700229 }
230}
231
Andreas Gampe799681b2015-05-15 19:24:12 -0700232void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700234 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
235 // going the reflective Dex way.
236 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
237 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700238 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700239 for (ArtField& field : klass->GetIFields()) {
240 if (name2->Equals(field.GetName())) {
241 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700243 }
244 }
245 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700246 for (ArtField& field : klass->GetSFields()) {
247 if (name2->Equals(field.GetName())) {
248 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700249 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700250 }
251 }
252 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700253 if (found == nullptr) {
254 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
255 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
256 PrettyDescriptor(klass).c_str());
257 return;
258 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700259 if (Runtime::Current()->IsActiveTransaction()) {
260 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
261 } else {
262 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
263 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700264}
265
Andreas Gampebc4d2182016-02-22 10:03:12 -0800266// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
267void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
268 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
269 // Special managed code cut-out to allow method lookup in a un-started runtime.
270 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
271 if (klass == nullptr) {
272 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
273 return;
274 }
275 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
276 mirror::ObjectArray<mirror::Class>* args =
277 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
278 if (Runtime::Current()->IsActiveTransaction()) {
279 result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args));
280 } else {
281 result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args));
282 }
283}
284
Andreas Gampe633750c2016-02-19 10:49:50 -0800285void UnstartedRuntime::UnstartedClassGetEnclosingClass(
286 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
287 StackHandleScope<1> hs(self);
288 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
289 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
290 result->SetL(nullptr);
291 }
292 result->SetL(klass->GetDexFile().GetEnclosingClass(klass));
293}
294
Andreas Gampe799681b2015-05-15 19:24:12 -0700295void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700296 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700297 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
298 mirror::ClassLoader* class_loader =
299 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
300 StackHandleScope<2> hs(self);
301 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
302 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
303 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
304 "VMClassLoader.findLoadedClass", false, false);
305 // This might have an error pending. But semantics are to just return null.
306 if (self->IsExceptionPending()) {
307 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
308 std::string type(PrettyTypeOf(self->GetException()));
309 if (type != "java.lang.InternalError") {
310 self->ClearException();
311 }
312 }
313}
314
Mathieu Chartiere401d142015-04-22 13:56:20 -0700315void UnstartedRuntime::UnstartedVoidLookupType(
316 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
317 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700318 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
319}
320
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700321// Arraycopy emulation.
322// Note: we can't use any fast copy functions, as they are not available under transaction.
323
324template <typename T>
325static void PrimitiveArrayCopy(Thread* self,
326 mirror::Array* src_array, int32_t src_pos,
327 mirror::Array* dst_array, int32_t dst_pos,
328 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700329 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700330 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
331 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
332 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
333 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
334 return;
335 }
336 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
337 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
338 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
339 if (copy_forward) {
340 for (int32_t i = 0; i < length; ++i) {
341 dst->Set(dst_pos + i, src->Get(src_pos + i));
342 }
343 } else {
344 for (int32_t i = 1; i <= length; ++i) {
345 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
346 }
347 }
348}
349
Andreas Gampe799681b2015-05-15 19:24:12 -0700350void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700351 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700352 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700353 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
354 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700355 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700356
Andreas Gampe85a098a2016-03-31 13:30:53 -0700357 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
358 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
359 // Null checking. For simplicity, abort transaction.
360 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700361 AbortTransactionOrFail(self, "src is null in arraycopy.");
362 return;
363 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700364 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700365 AbortTransactionOrFail(self, "dst is null in arraycopy.");
366 return;
367 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700368 // Test for arrayness. Throw ArrayStoreException.
369 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
370 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
371 return;
372 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700373
Andreas Gampe85a098a2016-03-31 13:30:53 -0700374 mirror::Array* src_array = src_obj->AsArray();
375 mirror::Array* dst_array = dst_obj->AsArray();
376
377 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700378 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
379 UNLIKELY(src_pos > src_array->GetLength() - length) ||
380 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700381 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700382 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
383 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
384 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700385 return;
386 }
387
388 // Type checking.
389 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
390 GetComponentType();
391
392 if (!src_type->IsPrimitive()) {
393 // Check that the second type is not primitive.
394 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
395 GetComponentType();
396 if (trg_type->IsPrimitiveInt()) {
397 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
398 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
399 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
400 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700401 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700402
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700403 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
404 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
405 if (src == dst) {
406 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700407 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700408 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
409 if (copy_forward) {
410 for (int32_t i = 0; i < length; ++i) {
411 dst->Set(dst_pos + i, src->Get(src_pos + i));
412 }
413 } else {
414 for (int32_t i = 1; i <= length; ++i) {
415 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
416 }
417 }
418 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700419 // We're being lazy here. Optimally this could be a memcpy (if component types are
420 // assignable), but the ObjectArray implementation doesn't support transactions. The
421 // checking version, however, does.
422 if (Runtime::Current()->IsActiveTransaction()) {
423 dst->AssignableCheckingMemcpy<true>(
424 dst_pos, src, src_pos, length, true /* throw_exception */);
425 } else {
426 dst->AssignableCheckingMemcpy<false>(
427 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700428 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700429 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700430 } else if (src_type->IsPrimitiveChar()) {
431 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
432 } else if (src_type->IsPrimitiveInt()) {
433 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700434 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700435 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700436 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700437 }
438}
439
Andreas Gampe799681b2015-05-15 19:24:12 -0700440void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700441 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700442 // Just forward.
443 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
444}
445
446void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700447 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700448 // Just forward.
449 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
450}
451
452void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700453 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700454 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
455 bool ok = false;
456 if (caller == "java.lang.String java.lang.IntegralToString.convertInt"
457 "(java.lang.AbstractStringBuilder, int)") {
458 // Allocate non-threadlocal buffer.
459 result->SetL(mirror::CharArray::Alloc(self, 11));
460 ok = true;
461 } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") {
462 // Note: RealToString is implemented and used in a different fashion than IntegralToString.
463 // Conversion is done over an actual object of RealToString (the conversion method is an
464 // instance method). This means it is not as clear whether it is correct to return a new
465 // object each time. The caller needs to be inspected by hand to see whether it (incorrectly)
466 // stores the object for later use.
467 // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString.
468 if (shadow_frame->GetLink()->GetLink() != nullptr) {
469 std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod()));
470 if (caller2 == "java.lang.String java.lang.Double.toString(double)") {
471 // Allocate new object.
472 StackHandleScope<2> hs(self);
473 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
474 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
475 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
476 h_real_to_string_class->AllocObject(self)));
477 if (h_real_to_string_obj.Get() != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700478 auto* cl = Runtime::Current()->GetClassLinker();
479 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
480 "<init>", "()V", cl->GetImagePointerSize());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700481 if (init_method == nullptr) {
482 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
483 } else {
484 JValue invoke_result;
485 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
486 nullptr);
487 if (!self->IsExceptionPending()) {
488 result->SetL(h_real_to_string_obj.Get());
489 ok = true;
490 }
491 }
492 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700493 }
494 }
495 }
496
497 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700498 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700499 }
500}
501
Andreas Gampe799681b2015-05-15 19:24:12 -0700502void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700503 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700504 double in = shadow_frame->GetVRegDouble(arg_offset);
505 double out;
506 // Special cases:
507 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
508 // -1 < in < 0 -> out := -0.
509 if (-1.0 < in && in < 0) {
510 out = -0.0;
511 } else {
512 out = ceil(in);
513 }
514 result->SetD(out);
515}
516
Andreas Gampe799681b2015-05-15 19:24:12 -0700517void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700518 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700519 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
520 result->SetI(obj->IdentityHashCode());
521}
522
Andreas Gampe799681b2015-05-15 19:24:12 -0700523void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700524 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700525 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000526 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700527}
528
Andreas Gampedd9d0552015-03-09 12:57:41 -0700529static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700530 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700531 const DexFile* dex_file = dex_cache->GetDexFile();
532 if (dex_file == nullptr) {
533 return nullptr;
534 }
535
536 // Create the direct byte buffer.
537 JNIEnv* env = self->GetJniEnv();
538 DCHECK(env != nullptr);
539 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700540 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
541 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700542 DCHECK(self->IsExceptionPending());
543 return nullptr;
544 }
545
546 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700547 args[0].l = byte_buffer.get();
548
549 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
550 WellKnownClasses::com_android_dex_Dex,
551 WellKnownClasses::com_android_dex_Dex_create,
552 args));
553
554 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700555}
556
Andreas Gampe799681b2015-05-15 19:24:12 -0700557void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700558 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700559 // We will create the Dex object, but the image writer will release it before creating the
560 // art file.
561 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
562 bool have_dex = false;
563 if (src != nullptr) {
564 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
565 if (dex != nullptr) {
566 have_dex = true;
567 result->SetL(dex);
568 }
569 }
570 if (!have_dex) {
571 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200572 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700573 }
574}
575
576static void UnstartedMemoryPeek(
577 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
578 int64_t address = shadow_frame->GetVRegLong(arg_offset);
579 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
580 // aborting the transaction.
581
582 switch (type) {
583 case Primitive::kPrimByte: {
584 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
585 return;
586 }
587
588 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700589 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
590 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700591 return;
592 }
593
594 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700595 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
596 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700597 return;
598 }
599
600 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700601 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
602 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700603 return;
604 }
605
606 case Primitive::kPrimBoolean:
607 case Primitive::kPrimChar:
608 case Primitive::kPrimFloat:
609 case Primitive::kPrimDouble:
610 case Primitive::kPrimVoid:
611 case Primitive::kPrimNot:
612 LOG(FATAL) << "Not in the Memory API: " << type;
613 UNREACHABLE();
614 }
615 LOG(FATAL) << "Should not reach here";
616 UNREACHABLE();
617}
618
Andreas Gampe799681b2015-05-15 19:24:12 -0700619void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700620 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700621 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
622}
623
624void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700625 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700626 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
627}
628
629void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700631 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
632}
633
634void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700636 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700637}
638
639static void UnstartedMemoryPeekArray(
640 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700641 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700642 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
643 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
644 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200645 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700646 return;
647 }
648 mirror::Array* array = obj->AsArray();
649
650 int offset = shadow_frame->GetVReg(arg_offset + 3);
651 int count = shadow_frame->GetVReg(arg_offset + 4);
652 if (offset < 0 || offset + count > array->GetLength()) {
653 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
654 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200655 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700656 return;
657 }
658
659 switch (type) {
660 case Primitive::kPrimByte: {
661 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
662 mirror::ByteArray* byte_array = array->AsByteArray();
663 for (int32_t i = 0; i < count; ++i, ++address) {
664 byte_array->SetWithoutChecks<true>(i + offset, *address);
665 }
666 return;
667 }
668
669 case Primitive::kPrimShort:
670 case Primitive::kPrimInt:
671 case Primitive::kPrimLong:
672 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
673 UNREACHABLE();
674
675 case Primitive::kPrimBoolean:
676 case Primitive::kPrimChar:
677 case Primitive::kPrimFloat:
678 case Primitive::kPrimDouble:
679 case Primitive::kPrimVoid:
680 case Primitive::kPrimNot:
681 LOG(FATAL) << "Not in the Memory API: " << type;
682 UNREACHABLE();
683 }
684 LOG(FATAL) << "Should not reach here";
685 UNREACHABLE();
686}
687
Andreas Gampe799681b2015-05-15 19:24:12 -0700688void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700689 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700690 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700691}
692
Andreas Gampef778eb22015-04-13 14:17:09 -0700693// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700694void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700695 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
696 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700697 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700698
699 std::vector<std::string> split;
700 Split(runtime->GetBootClassPathString(), ':', &split);
701 if (split.empty()) {
702 AbortTransactionOrFail(self,
703 "Boot classpath not set or split error:: %s",
704 runtime->GetBootClassPathString().c_str());
705 return;
706 }
707 const std::string& source = split[0];
708
Andreas Gampef778eb22015-04-13 14:17:09 -0700709 mirror::String* string_data;
710
711 // Use a block to enclose the I/O and MemMap code so buffers are released early.
712 {
713 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700714 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700715 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700716 AbortTransactionOrFail(self,
717 "Could not open zip file %s: %s",
718 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700719 error_msg.c_str());
720 return;
721 }
722 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
723 &error_msg));
724 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700725 AbortTransactionOrFail(self,
726 "Could not find security.properties file in %s: %s",
727 source.c_str(),
728 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700729 return;
730 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700731 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700732 "java/security/security.properties",
733 &error_msg));
734 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700735 AbortTransactionOrFail(self,
736 "Could not unzip security.properties file in %s: %s",
737 source.c_str(),
738 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700739 return;
740 }
741
742 uint32_t length = zip_entry->GetUncompressedLength();
743 std::unique_ptr<char[]> tmp(new char[length + 1]);
744 memcpy(tmp.get(), map->Begin(), length);
745 tmp.get()[length] = 0; // null terminator
746
747 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
748 }
749
750 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700751 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700752 return;
753 }
754
755 // Create a StringReader.
756 StackHandleScope<3> hs(self);
757 Handle<mirror::String> h_string(hs.NewHandle(string_data));
758
759 Handle<mirror::Class> h_class(hs.NewHandle(
760 runtime->GetClassLinker()->FindClass(self,
761 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800762 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700763 if (h_class.Get() == nullptr) {
764 AbortTransactionOrFail(self, "Could not find StringReader class");
765 return;
766 }
767
768 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
769 AbortTransactionOrFail(self, "Could not initialize StringReader class");
770 return;
771 }
772
773 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
774 if (h_obj.Get() == nullptr) {
775 AbortTransactionOrFail(self, "Could not allocate StringReader object");
776 return;
777 }
778
Mathieu Chartiere401d142015-04-22 13:56:20 -0700779 auto* cl = Runtime::Current()->GetClassLinker();
780 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
781 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700782 if (constructor == nullptr) {
783 AbortTransactionOrFail(self, "Could not find StringReader constructor");
784 return;
785 }
786
787 uint32_t args[1];
788 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
789 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
790
791 if (self->IsExceptionPending()) {
792 AbortTransactionOrFail(self, "Could not run StringReader constructor");
793 return;
794 }
795
796 result->SetL(h_obj.Get());
797}
798
Kenny Root1c9e61c2015-05-14 15:58:17 -0700799// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700800void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700801 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700802 jint start = shadow_frame->GetVReg(arg_offset + 1);
803 jint end = shadow_frame->GetVReg(arg_offset + 2);
804 jint index = shadow_frame->GetVReg(arg_offset + 4);
805 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
806 if (string == nullptr) {
807 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
808 return;
809 }
Kenny Root57f91e82015-05-14 15:58:17 -0700810 DCHECK_GE(start, 0);
811 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700812 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700813 Handle<mirror::CharArray> h_char_array(
814 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700815 DCHECK_LE(index, h_char_array->GetLength());
816 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700817 string->GetChars(start, end, h_char_array, index);
818}
819
820// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700821void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700822 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700823 jint index = shadow_frame->GetVReg(arg_offset + 1);
824 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
825 if (string == nullptr) {
826 AbortTransactionOrFail(self, "String.charAt with null object");
827 return;
828 }
829 result->SetC(string->CharAt(index));
830}
831
Kenny Root57f91e82015-05-14 15:58:17 -0700832// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700833void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700834 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700835 jint index = shadow_frame->GetVReg(arg_offset + 1);
836 jchar c = shadow_frame->GetVReg(arg_offset + 2);
837 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
838 if (string == nullptr) {
839 AbortTransactionOrFail(self, "String.setCharAt with null object");
840 return;
841 }
842 string->SetCharAt(index, c);
843}
844
Kenny Root1c9e61c2015-05-14 15:58:17 -0700845// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700846void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700848 jint offset = shadow_frame->GetVReg(arg_offset);
849 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
850 DCHECK_GE(char_count, 0);
851 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700852 Handle<mirror::CharArray> h_char_array(
853 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700854 Runtime* runtime = Runtime::Current();
855 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
856 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
857}
858
859// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700860void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700861 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700862 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
863 if (to_copy == nullptr) {
864 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
865 return;
866 }
867 StackHandleScope<1> hs(self);
868 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
869 Runtime* runtime = Runtime::Current();
870 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
871 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
872 allocator));
873}
874
Andreas Gampe799681b2015-05-15 19:24:12 -0700875void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700876 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700877 jint start = shadow_frame->GetVReg(arg_offset + 1);
878 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700879 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700880 DCHECK_GE(length, 0);
881 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700882 Handle<mirror::String> h_string(
883 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700884 DCHECK_LE(start, h_string->GetLength());
885 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700886 Runtime* runtime = Runtime::Current();
887 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
888 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
889}
890
Kenny Root57f91e82015-05-14 15:58:17 -0700891// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700892void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700893 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700894 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700895 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
896 if (string == nullptr) {
897 AbortTransactionOrFail(self, "String.charAt with null object");
898 return;
899 }
900 result->SetL(string->ToCharArray(self));
901}
902
Andreas Gampebc4d2182016-02-22 10:03:12 -0800903// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
904void UnstartedRuntime::UnstartedReferenceGetReferent(
905 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
906 mirror::Reference* const ref = down_cast<mirror::Reference*>(
907 shadow_frame->GetVRegReference(arg_offset));
908 if (ref == nullptr) {
909 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
910 return;
911 }
912 mirror::Object* const referent =
913 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
914 result->SetL(referent);
915}
916
917// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
918// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
919// where we can predict the behavior (somewhat).
920// Note: this is required (instead of lazy initialization) as these classes are used in the static
921// initialization of other classes, so will *use* the value.
922void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
923 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
924 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
925 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
926 // SynchronousQueue really only separates between single- and multiprocessor case. Return
927 // 8 as a conservative upper approximation.
928 result->SetI(8);
929 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
930 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
931 // a good upper bound.
932 // TODO: Consider resetting in the zygote?
933 result->SetI(8);
934 } else {
935 // Not supported.
936 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
937 }
938}
939
940// This allows accessing ConcurrentHashMap/SynchronousQueue.
941
942void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
943 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
944 // Argument 0 is the Unsafe instance, skip.
945 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
946 if (obj == nullptr) {
947 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
948 return;
949 }
950 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
951 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
952 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
953
954 // Must use non transactional mode.
955 if (kUseReadBarrier) {
956 // Need to make sure the reference stored in the field is a to-space one before attempting the
957 // CAS or the CAS could fail incorrectly.
958 mirror::HeapReference<mirror::Object>* field_addr =
959 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
960 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
961 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
962 obj,
963 MemberOffset(offset),
964 field_addr);
965 }
966 bool success;
967 // Check whether we're in a transaction, call accordingly.
968 if (Runtime::Current()->IsActiveTransaction()) {
969 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
970 expectedValue,
971 newValue);
972 } else {
973 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
974 expectedValue,
975 newValue);
976 }
977 result->SetZ(success ? 1 : 0);
978}
979
980void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
981 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
982 // Argument 0 is the Unsafe instance, skip.
983 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
984 if (obj == nullptr) {
985 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
986 return;
987 }
988 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
989 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
990 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
991
992 // Must use non transactional mode.
993 if (kUseReadBarrier) {
994 // Need to make sure the reference stored in the field is a to-space one before attempting the
995 // CAS or the CAS could fail incorrectly.
996 mirror::HeapReference<mirror::Object>* field_addr =
997 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
998 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
999 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1000 obj,
1001 MemberOffset(offset),
1002 field_addr);
1003 }
1004 bool success;
1005 // Check whether we're in a transaction, call accordingly.
1006 if (Runtime::Current()->IsActiveTransaction()) {
1007 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1008 expected_value,
1009 newValue);
1010 } else {
1011 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1012 expected_value,
1013 newValue);
1014 }
1015 result->SetZ(success ? 1 : 0);
1016}
1017
1018void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1019 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1020 SHARED_REQUIRES(Locks::mutator_lock_) {
1021 // Argument 0 is the Unsafe instance, skip.
1022 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1023 if (obj == nullptr) {
1024 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1025 return;
1026 }
1027 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1028 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1029 result->SetL(value);
1030}
1031
1032void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1033 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1034 SHARED_REQUIRES(Locks::mutator_lock_) {
1035 // Argument 0 is the Unsafe instance, skip.
1036 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1037 if (obj == nullptr) {
1038 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1039 return;
1040 }
1041 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1042 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1043 QuasiAtomic::ThreadFenceRelease();
1044 if (Runtime::Current()->IsActiveTransaction()) {
1045 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1046 } else {
1047 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1048 }
1049}
1050
1051
Mathieu Chartiere401d142015-04-22 13:56:20 -07001052void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1053 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1054 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001055 int32_t length = args[1];
1056 DCHECK_GE(length, 0);
1057 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1058 Runtime* runtime = Runtime::Current();
1059 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1060 DCHECK(array_class != nullptr);
1061 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1062 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1063 array_class->GetComponentSizeShift(), allocator));
1064}
1065
Mathieu Chartiere401d142015-04-22 13:56:20 -07001066void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1067 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1068 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001069 result->SetL(nullptr);
1070}
1071
Mathieu Chartiere401d142015-04-22 13:56:20 -07001072void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1073 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1074 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001075 NthCallerVisitor visitor(self, 3);
1076 visitor.WalkStack();
1077 if (visitor.caller != nullptr) {
1078 result->SetL(visitor.caller->GetDeclaringClass());
1079 }
1080}
1081
Mathieu Chartiere401d142015-04-22 13:56:20 -07001082void UnstartedRuntime::UnstartedJNIMathLog(
1083 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1084 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001085 JValue value;
1086 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1087 result->SetD(log(value.GetD()));
1088}
1089
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090void UnstartedRuntime::UnstartedJNIMathExp(
1091 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1092 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001093 JValue value;
1094 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1095 result->SetD(exp(value.GetD()));
1096}
1097
Andreas Gampebc4d2182016-02-22 10:03:12 -08001098void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1099 Thread* self ATTRIBUTE_UNUSED,
1100 ArtMethod* method ATTRIBUTE_UNUSED,
1101 mirror::Object* receiver ATTRIBUTE_UNUSED,
1102 uint32_t* args ATTRIBUTE_UNUSED,
1103 JValue* result) {
1104 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1105 ? 0
1106 : 1);
1107}
1108
Mathieu Chartiere401d142015-04-22 13:56:20 -07001109void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1110 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1111 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001112 StackHandleScope<1> hs(self);
1113 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1114}
1115
Andreas Gampebc4d2182016-02-22 10:03:12 -08001116void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1117 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1118 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1119 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1120 result->SetD(bit_cast<double>(long_input));
1121}
1122
Mathieu Chartiere401d142015-04-22 13:56:20 -07001123void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1124 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1125 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001126 result->SetI(args[0]);
1127}
1128
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1130 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1131 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001132 result->SetI(args[0]);
1133}
1134
Mathieu Chartiere401d142015-04-22 13:56:20 -07001135void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1136 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1137 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001138 result->SetL(receiver->Clone(self));
1139}
1140
Mathieu Chartiere401d142015-04-22 13:56:20 -07001141void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1142 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1143 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001144 receiver->NotifyAll(self);
1145}
1146
Mathieu Chartiere401d142015-04-22 13:56:20 -07001147void UnstartedRuntime::UnstartedJNIStringCompareTo(
1148 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1149 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001150 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1151 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001152 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001153 }
1154 result->SetI(receiver->AsString()->CompareTo(rhs));
1155}
1156
Mathieu Chartiere401d142015-04-22 13:56:20 -07001157void UnstartedRuntime::UnstartedJNIStringIntern(
1158 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1159 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001160 result->SetL(receiver->AsString()->Intern());
1161}
1162
Mathieu Chartiere401d142015-04-22 13:56:20 -07001163void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1164 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1165 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001166 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1167}
1168
Mathieu Chartiere401d142015-04-22 13:56:20 -07001169void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1170 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1171 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001172 StackHandleScope<2> hs(self);
1173 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1174 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1175 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1176}
1177
Mathieu Chartiere401d142015-04-22 13:56:20 -07001178void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1179 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1180 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001181 int32_t length = static_cast<int32_t>(args[1]);
1182 if (length < 0) {
1183 ThrowNegativeArraySizeException(length);
1184 return;
1185 }
1186 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1187 Runtime* runtime = Runtime::Current();
1188 ClassLinker* class_linker = runtime->GetClassLinker();
1189 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001190 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001191 CHECK(self->IsExceptionPending());
1192 return;
1193 }
1194 DCHECK(array_class->IsObjectArrayClass());
1195 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1196 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1197 result->SetL(new_array);
1198}
1199
Mathieu Chartiere401d142015-04-22 13:56:20 -07001200void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1201 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1202 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001203 ScopedObjectAccessUnchecked soa(self);
1204 if (Runtime::Current()->IsActiveTransaction()) {
1205 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1206 } else {
1207 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1208 }
1209}
1210
Mathieu Chartiere401d142015-04-22 13:56:20 -07001211void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1212 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1213 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001214 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1215 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1216}
1217
Mathieu Chartiere401d142015-04-22 13:56:20 -07001218void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1219 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1220 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001221 result->SetZ(JNI_TRUE);
1222}
1223
Mathieu Chartiere401d142015-04-22 13:56:20 -07001224void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1225 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1226 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001227 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1228 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1229 jint expectedValue = args[3];
1230 jint newValue = args[4];
1231 bool success;
1232 if (Runtime::Current()->IsActiveTransaction()) {
1233 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1234 expectedValue, newValue);
1235 } else {
1236 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1237 expectedValue, newValue);
1238 }
1239 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1240}
1241
Mathieu Chartiere401d142015-04-22 13:56:20 -07001242void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1243 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1244 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001245 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1246 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1247 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1248 if (Runtime::Current()->IsActiveTransaction()) {
1249 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1250 } else {
1251 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1252 }
1253}
1254
Andreas Gampe799681b2015-05-15 19:24:12 -07001255void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001256 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1257 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001258 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1259 Primitive::Type primitive_type = component->GetPrimitiveType();
1260 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1261}
1262
Andreas Gampe799681b2015-05-15 19:24:12 -07001263void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001264 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1265 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001266 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1267 Primitive::Type primitive_type = component->GetPrimitiveType();
1268 result->SetI(Primitive::ComponentSize(primitive_type));
1269}
1270
Andreas Gampedd9d0552015-03-09 12:57:41 -07001271typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001272 size_t arg_size);
1273
Mathieu Chartiere401d142015-04-22 13:56:20 -07001274typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001275 uint32_t* args, JValue* result);
1276
1277static bool tables_initialized_ = false;
1278static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1279static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1280
Andreas Gampe799681b2015-05-15 19:24:12 -07001281void UnstartedRuntime::InitializeInvokeHandlers() {
1282#define UNSTARTED_DIRECT(ShortName, Sig) \
1283 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1284#include "unstarted_runtime_list.h"
1285 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1286#undef UNSTARTED_RUNTIME_DIRECT_LIST
1287#undef UNSTARTED_RUNTIME_JNI_LIST
1288#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001289}
1290
Andreas Gampe799681b2015-05-15 19:24:12 -07001291void UnstartedRuntime::InitializeJNIHandlers() {
1292#define UNSTARTED_JNI(ShortName, Sig) \
1293 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1294#include "unstarted_runtime_list.h"
1295 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1296#undef UNSTARTED_RUNTIME_DIRECT_LIST
1297#undef UNSTARTED_RUNTIME_JNI_LIST
1298#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001299}
1300
Andreas Gampe799681b2015-05-15 19:24:12 -07001301void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001302 CHECK(!tables_initialized_);
1303
Andreas Gampe799681b2015-05-15 19:24:12 -07001304 InitializeInvokeHandlers();
1305 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001306
1307 tables_initialized_ = true;
1308}
1309
Andreas Gampe799681b2015-05-15 19:24:12 -07001310void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1311 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001312 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1313 // problems in core libraries.
1314 CHECK(tables_initialized_);
1315
1316 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1317 const auto& iter = invoke_handlers_.find(name);
1318 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001319 // Clear out the result in case it's not zeroed out.
1320 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001321 (*iter->second)(self, shadow_frame, result, arg_offset);
1322 } else {
1323 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001324 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001325 }
1326}
1327
1328// Hand select a number of methods to be run in a not yet started runtime without using JNI.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001329void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001330 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001331 std::string name(PrettyMethod(method));
1332 const auto& iter = jni_handlers_.find(name);
1333 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001334 // Clear out the result in case it's not zeroed out.
1335 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001336 (*iter->second)(self, method, receiver, args, result);
1337 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001338 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1339 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001340 } else {
1341 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1342 "non-transactional runtime";
1343 }
1344}
1345
1346} // namespace interpreter
1347} // namespace art