blob: 81be959cd5d92f13700fc6f87423dae61bfe1e66 [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
Narayan Kamath34a316f2016-03-30 13:11:18 +0100452void UnstartedRuntime::UnstartedSystemGetSecurityManager(
453 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
454 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
455 result->SetL(nullptr);
456}
457
Andreas Gampe799681b2015-05-15 19:24:12 -0700458void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700459 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700460 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
461 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100462 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
463 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700464 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100465 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700466 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100467 } else if (caller ==
468 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
469 // Allocate new object.
470 StackHandleScope<2> hs(self);
471 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
472 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
473 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
474 h_real_to_string_class->AllocObject(self)));
475 if (h_real_to_string_obj.Get() != nullptr) {
476 auto* cl = Runtime::Current()->GetClassLinker();
477 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
478 "<init>", "()V", cl->GetImagePointerSize());
479 if (init_method == nullptr) {
480 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
481 } else {
482 JValue invoke_result;
483 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
484 nullptr);
485 if (!self->IsExceptionPending()) {
486 result->SetL(h_real_to_string_obj.Get());
487 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700488 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700489 }
490 }
491 }
492
493 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700494 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700495 }
496}
497
Andreas Gampe799681b2015-05-15 19:24:12 -0700498void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700499 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700500 double in = shadow_frame->GetVRegDouble(arg_offset);
501 double out;
502 // Special cases:
503 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
504 // -1 < in < 0 -> out := -0.
505 if (-1.0 < in && in < 0) {
506 out = -0.0;
507 } else {
508 out = ceil(in);
509 }
510 result->SetD(out);
511}
512
Andreas Gampe799681b2015-05-15 19:24:12 -0700513void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700514 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700515 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
516 result->SetI(obj->IdentityHashCode());
517}
518
Andreas Gampe799681b2015-05-15 19:24:12 -0700519void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700520 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700521 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000522 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700523}
524
Andreas Gampedd9d0552015-03-09 12:57:41 -0700525static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700526 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700527 const DexFile* dex_file = dex_cache->GetDexFile();
528 if (dex_file == nullptr) {
529 return nullptr;
530 }
531
532 // Create the direct byte buffer.
533 JNIEnv* env = self->GetJniEnv();
534 DCHECK(env != nullptr);
535 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700536 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
537 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700538 DCHECK(self->IsExceptionPending());
539 return nullptr;
540 }
541
542 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700543 args[0].l = byte_buffer.get();
544
545 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
546 WellKnownClasses::com_android_dex_Dex,
547 WellKnownClasses::com_android_dex_Dex_create,
548 args));
549
550 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700551}
552
Andreas Gampe799681b2015-05-15 19:24:12 -0700553void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700554 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700555 // We will create the Dex object, but the image writer will release it before creating the
556 // art file.
557 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
558 bool have_dex = false;
559 if (src != nullptr) {
560 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
561 if (dex != nullptr) {
562 have_dex = true;
563 result->SetL(dex);
564 }
565 }
566 if (!have_dex) {
567 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200568 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700569 }
570}
571
572static void UnstartedMemoryPeek(
573 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
574 int64_t address = shadow_frame->GetVRegLong(arg_offset);
575 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
576 // aborting the transaction.
577
578 switch (type) {
579 case Primitive::kPrimByte: {
580 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
581 return;
582 }
583
584 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700585 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
586 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700587 return;
588 }
589
590 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700591 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
592 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700593 return;
594 }
595
596 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700597 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
598 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700599 return;
600 }
601
602 case Primitive::kPrimBoolean:
603 case Primitive::kPrimChar:
604 case Primitive::kPrimFloat:
605 case Primitive::kPrimDouble:
606 case Primitive::kPrimVoid:
607 case Primitive::kPrimNot:
608 LOG(FATAL) << "Not in the Memory API: " << type;
609 UNREACHABLE();
610 }
611 LOG(FATAL) << "Should not reach here";
612 UNREACHABLE();
613}
614
Andreas Gampe799681b2015-05-15 19:24:12 -0700615void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700617 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
618}
619
620void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700622 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
623}
624
625void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700626 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700627 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
628}
629
630void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700631 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700632 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700633}
634
635static void UnstartedMemoryPeekArray(
636 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700637 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700638 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
639 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
640 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200641 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700642 return;
643 }
644 mirror::Array* array = obj->AsArray();
645
646 int offset = shadow_frame->GetVReg(arg_offset + 3);
647 int count = shadow_frame->GetVReg(arg_offset + 4);
648 if (offset < 0 || offset + count > array->GetLength()) {
649 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
650 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200651 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700652 return;
653 }
654
655 switch (type) {
656 case Primitive::kPrimByte: {
657 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
658 mirror::ByteArray* byte_array = array->AsByteArray();
659 for (int32_t i = 0; i < count; ++i, ++address) {
660 byte_array->SetWithoutChecks<true>(i + offset, *address);
661 }
662 return;
663 }
664
665 case Primitive::kPrimShort:
666 case Primitive::kPrimInt:
667 case Primitive::kPrimLong:
668 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
669 UNREACHABLE();
670
671 case Primitive::kPrimBoolean:
672 case Primitive::kPrimChar:
673 case Primitive::kPrimFloat:
674 case Primitive::kPrimDouble:
675 case Primitive::kPrimVoid:
676 case Primitive::kPrimNot:
677 LOG(FATAL) << "Not in the Memory API: " << type;
678 UNREACHABLE();
679 }
680 LOG(FATAL) << "Should not reach here";
681 UNREACHABLE();
682}
683
Andreas Gampe799681b2015-05-15 19:24:12 -0700684void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700685 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700686 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700687}
688
Andreas Gampef778eb22015-04-13 14:17:09 -0700689// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700690void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700691 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
692 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700693 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700694
695 std::vector<std::string> split;
696 Split(runtime->GetBootClassPathString(), ':', &split);
697 if (split.empty()) {
698 AbortTransactionOrFail(self,
699 "Boot classpath not set or split error:: %s",
700 runtime->GetBootClassPathString().c_str());
701 return;
702 }
703 const std::string& source = split[0];
704
Andreas Gampef778eb22015-04-13 14:17:09 -0700705 mirror::String* string_data;
706
707 // Use a block to enclose the I/O and MemMap code so buffers are released early.
708 {
709 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700710 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700711 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700712 AbortTransactionOrFail(self,
713 "Could not open zip file %s: %s",
714 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700715 error_msg.c_str());
716 return;
717 }
718 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
719 &error_msg));
720 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700721 AbortTransactionOrFail(self,
722 "Could not find security.properties file in %s: %s",
723 source.c_str(),
724 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700725 return;
726 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700727 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700728 "java/security/security.properties",
729 &error_msg));
730 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700731 AbortTransactionOrFail(self,
732 "Could not unzip security.properties file in %s: %s",
733 source.c_str(),
734 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700735 return;
736 }
737
738 uint32_t length = zip_entry->GetUncompressedLength();
739 std::unique_ptr<char[]> tmp(new char[length + 1]);
740 memcpy(tmp.get(), map->Begin(), length);
741 tmp.get()[length] = 0; // null terminator
742
743 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
744 }
745
746 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700747 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700748 return;
749 }
750
751 // Create a StringReader.
752 StackHandleScope<3> hs(self);
753 Handle<mirror::String> h_string(hs.NewHandle(string_data));
754
755 Handle<mirror::Class> h_class(hs.NewHandle(
756 runtime->GetClassLinker()->FindClass(self,
757 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800758 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700759 if (h_class.Get() == nullptr) {
760 AbortTransactionOrFail(self, "Could not find StringReader class");
761 return;
762 }
763
764 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
765 AbortTransactionOrFail(self, "Could not initialize StringReader class");
766 return;
767 }
768
769 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
770 if (h_obj.Get() == nullptr) {
771 AbortTransactionOrFail(self, "Could not allocate StringReader object");
772 return;
773 }
774
Mathieu Chartiere401d142015-04-22 13:56:20 -0700775 auto* cl = Runtime::Current()->GetClassLinker();
776 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
777 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700778 if (constructor == nullptr) {
779 AbortTransactionOrFail(self, "Could not find StringReader constructor");
780 return;
781 }
782
783 uint32_t args[1];
784 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
785 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
786
787 if (self->IsExceptionPending()) {
788 AbortTransactionOrFail(self, "Could not run StringReader constructor");
789 return;
790 }
791
792 result->SetL(h_obj.Get());
793}
794
Kenny Root1c9e61c2015-05-14 15:58:17 -0700795// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700796void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700797 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700798 jint start = shadow_frame->GetVReg(arg_offset + 1);
799 jint end = shadow_frame->GetVReg(arg_offset + 2);
800 jint index = shadow_frame->GetVReg(arg_offset + 4);
801 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
802 if (string == nullptr) {
803 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
804 return;
805 }
Kenny Root57f91e82015-05-14 15:58:17 -0700806 DCHECK_GE(start, 0);
807 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700808 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700809 Handle<mirror::CharArray> h_char_array(
810 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700811 DCHECK_LE(index, h_char_array->GetLength());
812 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700813 string->GetChars(start, end, h_char_array, index);
814}
815
816// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700817void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700818 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700819 jint index = shadow_frame->GetVReg(arg_offset + 1);
820 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
821 if (string == nullptr) {
822 AbortTransactionOrFail(self, "String.charAt with null object");
823 return;
824 }
825 result->SetC(string->CharAt(index));
826}
827
Kenny Root57f91e82015-05-14 15:58:17 -0700828// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700829void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700830 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700831 jint index = shadow_frame->GetVReg(arg_offset + 1);
832 jchar c = shadow_frame->GetVReg(arg_offset + 2);
833 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
834 if (string == nullptr) {
835 AbortTransactionOrFail(self, "String.setCharAt with null object");
836 return;
837 }
838 string->SetCharAt(index, c);
839}
840
Kenny Root1c9e61c2015-05-14 15:58:17 -0700841// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700842void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700844 jint offset = shadow_frame->GetVReg(arg_offset);
845 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
846 DCHECK_GE(char_count, 0);
847 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 Handle<mirror::CharArray> h_char_array(
849 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700850 Runtime* runtime = Runtime::Current();
851 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
852 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
853}
854
855// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700856void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700857 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700858 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
859 if (to_copy == nullptr) {
860 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
861 return;
862 }
863 StackHandleScope<1> hs(self);
864 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
865 Runtime* runtime = Runtime::Current();
866 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
867 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
868 allocator));
869}
870
Andreas Gampe799681b2015-05-15 19:24:12 -0700871void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700872 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700873 jint start = shadow_frame->GetVReg(arg_offset + 1);
874 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700875 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700876 DCHECK_GE(length, 0);
877 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700878 Handle<mirror::String> h_string(
879 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700880 DCHECK_LE(start, h_string->GetLength());
881 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700882 Runtime* runtime = Runtime::Current();
883 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
884 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
885}
886
Kenny Root57f91e82015-05-14 15:58:17 -0700887// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700888void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700889 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700890 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700891 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
892 if (string == nullptr) {
893 AbortTransactionOrFail(self, "String.charAt with null object");
894 return;
895 }
896 result->SetL(string->ToCharArray(self));
897}
898
Andreas Gampebc4d2182016-02-22 10:03:12 -0800899// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
900void UnstartedRuntime::UnstartedReferenceGetReferent(
901 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
902 mirror::Reference* const ref = down_cast<mirror::Reference*>(
903 shadow_frame->GetVRegReference(arg_offset));
904 if (ref == nullptr) {
905 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
906 return;
907 }
908 mirror::Object* const referent =
909 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
910 result->SetL(referent);
911}
912
913// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
914// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
915// where we can predict the behavior (somewhat).
916// Note: this is required (instead of lazy initialization) as these classes are used in the static
917// initialization of other classes, so will *use* the value.
918void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
919 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
920 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
921 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
922 // SynchronousQueue really only separates between single- and multiprocessor case. Return
923 // 8 as a conservative upper approximation.
924 result->SetI(8);
925 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
926 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
927 // a good upper bound.
928 // TODO: Consider resetting in the zygote?
929 result->SetI(8);
930 } else {
931 // Not supported.
932 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
933 }
934}
935
936// This allows accessing ConcurrentHashMap/SynchronousQueue.
937
938void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
939 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
940 // Argument 0 is the Unsafe instance, skip.
941 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
942 if (obj == nullptr) {
943 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
944 return;
945 }
946 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
947 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
948 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
949
950 // Must use non transactional mode.
951 if (kUseReadBarrier) {
952 // Need to make sure the reference stored in the field is a to-space one before attempting the
953 // CAS or the CAS could fail incorrectly.
954 mirror::HeapReference<mirror::Object>* field_addr =
955 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
956 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
957 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
958 obj,
959 MemberOffset(offset),
960 field_addr);
961 }
962 bool success;
963 // Check whether we're in a transaction, call accordingly.
964 if (Runtime::Current()->IsActiveTransaction()) {
965 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
966 expectedValue,
967 newValue);
968 } else {
969 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
970 expectedValue,
971 newValue);
972 }
973 result->SetZ(success ? 1 : 0);
974}
975
976void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
977 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
978 // Argument 0 is the Unsafe instance, skip.
979 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
980 if (obj == nullptr) {
981 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
982 return;
983 }
984 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
985 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
986 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
987
988 // Must use non transactional mode.
989 if (kUseReadBarrier) {
990 // Need to make sure the reference stored in the field is a to-space one before attempting the
991 // CAS or the CAS could fail incorrectly.
992 mirror::HeapReference<mirror::Object>* field_addr =
993 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
994 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
995 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
996 obj,
997 MemberOffset(offset),
998 field_addr);
999 }
1000 bool success;
1001 // Check whether we're in a transaction, call accordingly.
1002 if (Runtime::Current()->IsActiveTransaction()) {
1003 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1004 expected_value,
1005 newValue);
1006 } else {
1007 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1008 expected_value,
1009 newValue);
1010 }
1011 result->SetZ(success ? 1 : 0);
1012}
1013
1014void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1015 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1016 SHARED_REQUIRES(Locks::mutator_lock_) {
1017 // Argument 0 is the Unsafe instance, skip.
1018 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1019 if (obj == nullptr) {
1020 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1021 return;
1022 }
1023 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1024 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1025 result->SetL(value);
1026}
1027
1028void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1029 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1030 SHARED_REQUIRES(Locks::mutator_lock_) {
1031 // Argument 0 is the Unsafe instance, skip.
1032 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1033 if (obj == nullptr) {
1034 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1035 return;
1036 }
1037 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1038 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1039 QuasiAtomic::ThreadFenceRelease();
1040 if (Runtime::Current()->IsActiveTransaction()) {
1041 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1042 } else {
1043 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1044 }
1045}
1046
1047
Mathieu Chartiere401d142015-04-22 13:56:20 -07001048void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1049 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1050 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001051 int32_t length = args[1];
1052 DCHECK_GE(length, 0);
1053 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1054 Runtime* runtime = Runtime::Current();
1055 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1056 DCHECK(array_class != nullptr);
1057 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1058 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1059 array_class->GetComponentSizeShift(), allocator));
1060}
1061
Mathieu Chartiere401d142015-04-22 13:56:20 -07001062void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1063 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1064 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001065 result->SetL(nullptr);
1066}
1067
Mathieu Chartiere401d142015-04-22 13:56:20 -07001068void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1069 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1070 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001071 NthCallerVisitor visitor(self, 3);
1072 visitor.WalkStack();
1073 if (visitor.caller != nullptr) {
1074 result->SetL(visitor.caller->GetDeclaringClass());
1075 }
1076}
1077
Mathieu Chartiere401d142015-04-22 13:56:20 -07001078void UnstartedRuntime::UnstartedJNIMathLog(
1079 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1080 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001081 JValue value;
1082 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1083 result->SetD(log(value.GetD()));
1084}
1085
Mathieu Chartiere401d142015-04-22 13:56:20 -07001086void UnstartedRuntime::UnstartedJNIMathExp(
1087 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1088 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001089 JValue value;
1090 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1091 result->SetD(exp(value.GetD()));
1092}
1093
Andreas Gampebc4d2182016-02-22 10:03:12 -08001094void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1095 Thread* self ATTRIBUTE_UNUSED,
1096 ArtMethod* method ATTRIBUTE_UNUSED,
1097 mirror::Object* receiver ATTRIBUTE_UNUSED,
1098 uint32_t* args ATTRIBUTE_UNUSED,
1099 JValue* result) {
1100 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1101 ? 0
1102 : 1);
1103}
1104
Mathieu Chartiere401d142015-04-22 13:56:20 -07001105void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1106 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1107 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001108 StackHandleScope<1> hs(self);
1109 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1110}
1111
Andreas Gampebc4d2182016-02-22 10:03:12 -08001112void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1113 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1114 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1115 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1116 result->SetD(bit_cast<double>(long_input));
1117}
1118
Mathieu Chartiere401d142015-04-22 13:56:20 -07001119void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1120 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1121 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001122 result->SetI(args[0]);
1123}
1124
Mathieu Chartiere401d142015-04-22 13:56:20 -07001125void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1126 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1127 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001128 result->SetI(args[0]);
1129}
1130
Mathieu Chartiere401d142015-04-22 13:56:20 -07001131void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1132 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1133 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001134 result->SetL(receiver->Clone(self));
1135}
1136
Mathieu Chartiere401d142015-04-22 13:56:20 -07001137void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1138 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1139 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001140 receiver->NotifyAll(self);
1141}
1142
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143void UnstartedRuntime::UnstartedJNIStringCompareTo(
1144 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1145 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001146 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1147 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001148 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001149 }
1150 result->SetI(receiver->AsString()->CompareTo(rhs));
1151}
1152
Mathieu Chartiere401d142015-04-22 13:56:20 -07001153void UnstartedRuntime::UnstartedJNIStringIntern(
1154 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1155 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001156 result->SetL(receiver->AsString()->Intern());
1157}
1158
Mathieu Chartiere401d142015-04-22 13:56:20 -07001159void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1160 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1161 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001162 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1163}
1164
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1166 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1167 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001168 StackHandleScope<2> hs(self);
1169 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1170 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1171 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1172}
1173
Mathieu Chartiere401d142015-04-22 13:56:20 -07001174void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1175 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1176 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001177 int32_t length = static_cast<int32_t>(args[1]);
1178 if (length < 0) {
1179 ThrowNegativeArraySizeException(length);
1180 return;
1181 }
1182 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1183 Runtime* runtime = Runtime::Current();
1184 ClassLinker* class_linker = runtime->GetClassLinker();
1185 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001186 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001187 CHECK(self->IsExceptionPending());
1188 return;
1189 }
1190 DCHECK(array_class->IsObjectArrayClass());
1191 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1192 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1193 result->SetL(new_array);
1194}
1195
Mathieu Chartiere401d142015-04-22 13:56:20 -07001196void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1197 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1198 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001199 ScopedObjectAccessUnchecked soa(self);
1200 if (Runtime::Current()->IsActiveTransaction()) {
1201 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1202 } else {
1203 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1204 }
1205}
1206
Mathieu Chartiere401d142015-04-22 13:56:20 -07001207void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1208 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1209 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001210 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1211 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1212}
1213
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1215 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1216 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001217 result->SetZ(JNI_TRUE);
1218}
1219
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1221 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1222 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001223 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1224 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1225 jint expectedValue = args[3];
1226 jint newValue = args[4];
1227 bool success;
1228 if (Runtime::Current()->IsActiveTransaction()) {
1229 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1230 expectedValue, newValue);
1231 } else {
1232 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1233 expectedValue, newValue);
1234 }
1235 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1236}
1237
Narayan Kamath34a316f2016-03-30 13:11:18 +01001238void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1239 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1240 uint32_t* args, JValue* result) {
1241 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1242 if (obj == nullptr) {
1243 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1244 return;
1245 }
1246
1247 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1248 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1249}
1250
Mathieu Chartiere401d142015-04-22 13:56:20 -07001251void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1252 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1253 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001254 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1255 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1256 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1257 if (Runtime::Current()->IsActiveTransaction()) {
1258 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1259 } else {
1260 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1261 }
1262}
1263
Andreas Gampe799681b2015-05-15 19:24:12 -07001264void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001265 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1266 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001267 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1268 Primitive::Type primitive_type = component->GetPrimitiveType();
1269 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1270}
1271
Andreas Gampe799681b2015-05-15 19:24:12 -07001272void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001273 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1274 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001275 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1276 Primitive::Type primitive_type = component->GetPrimitiveType();
1277 result->SetI(Primitive::ComponentSize(primitive_type));
1278}
1279
Andreas Gampedd9d0552015-03-09 12:57:41 -07001280typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001281 size_t arg_size);
1282
Mathieu Chartiere401d142015-04-22 13:56:20 -07001283typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001284 uint32_t* args, JValue* result);
1285
1286static bool tables_initialized_ = false;
1287static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1288static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1289
Andreas Gampe799681b2015-05-15 19:24:12 -07001290void UnstartedRuntime::InitializeInvokeHandlers() {
1291#define UNSTARTED_DIRECT(ShortName, Sig) \
1292 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1293#include "unstarted_runtime_list.h"
1294 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1295#undef UNSTARTED_RUNTIME_DIRECT_LIST
1296#undef UNSTARTED_RUNTIME_JNI_LIST
1297#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001298}
1299
Andreas Gampe799681b2015-05-15 19:24:12 -07001300void UnstartedRuntime::InitializeJNIHandlers() {
1301#define UNSTARTED_JNI(ShortName, Sig) \
1302 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1303#include "unstarted_runtime_list.h"
1304 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1305#undef UNSTARTED_RUNTIME_DIRECT_LIST
1306#undef UNSTARTED_RUNTIME_JNI_LIST
1307#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001308}
1309
Andreas Gampe799681b2015-05-15 19:24:12 -07001310void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001311 CHECK(!tables_initialized_);
1312
Andreas Gampe799681b2015-05-15 19:24:12 -07001313 InitializeInvokeHandlers();
1314 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001315
1316 tables_initialized_ = true;
1317}
1318
Andreas Gampe799681b2015-05-15 19:24:12 -07001319void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1320 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001321 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1322 // problems in core libraries.
1323 CHECK(tables_initialized_);
1324
1325 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1326 const auto& iter = invoke_handlers_.find(name);
1327 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001328 // Clear out the result in case it's not zeroed out.
1329 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001330 (*iter->second)(self, shadow_frame, result, arg_offset);
1331 } else {
1332 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001333 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001334 }
1335}
1336
1337// 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 -07001338void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001339 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001340 std::string name(PrettyMethod(method));
1341 const auto& iter = jni_handlers_.find(name);
1342 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001343 // Clear out the result in case it's not zeroed out.
1344 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001345 (*iter->second)(self, method, receiver, args, result);
1346 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001347 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1348 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001349 } else {
1350 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1351 "non-transactional runtime";
1352 }
1353}
1354
1355} // namespace interpreter
1356} // namespace art