blob: a75f87895f12b89d94e2c2382c393be5b791c520 [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
Sergio Giro83261202016-04-11 20:49:20 +0100498static double ComputeCeil(double in) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700499 double out;
500 // Special cases:
501 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
502 // -1 < in < 0 -> out := -0.
503 if (-1.0 < in && in < 0) {
504 out = -0.0;
505 } else {
506 out = ceil(in);
507 }
Sergio Giro83261202016-04-11 20:49:20 +0100508 return out;
509}
510
511void UnstartedRuntime::UnstartedMathCeil(
512 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
513 double in = shadow_frame->GetVRegDouble(arg_offset);
514 result->SetD(ComputeCeil(in));
515}
516
517void UnstartedRuntime::UnstartedMathFloor(
518 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
519 double in = shadow_frame->GetVRegDouble(arg_offset);
520 // From the JavaDocs:
521 // "Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x)."
522 result->SetD(-ComputeCeil(-in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700523}
524
Andreas Gampe799681b2015-05-15 19:24:12 -0700525void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700526 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700527 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
528 result->SetI(obj->IdentityHashCode());
529}
530
Andreas Gampe799681b2015-05-15 19:24:12 -0700531void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700532 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700533 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000534 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700535}
536
Andreas Gampedd9d0552015-03-09 12:57:41 -0700537static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700538 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700539 const DexFile* dex_file = dex_cache->GetDexFile();
540 if (dex_file == nullptr) {
541 return nullptr;
542 }
543
544 // Create the direct byte buffer.
545 JNIEnv* env = self->GetJniEnv();
546 DCHECK(env != nullptr);
547 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700548 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
549 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700550 DCHECK(self->IsExceptionPending());
551 return nullptr;
552 }
553
554 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700555 args[0].l = byte_buffer.get();
556
557 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
558 WellKnownClasses::com_android_dex_Dex,
559 WellKnownClasses::com_android_dex_Dex_create,
560 args));
561
562 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700563}
564
Andreas Gampe799681b2015-05-15 19:24:12 -0700565void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700567 // We will create the Dex object, but the image writer will release it before creating the
568 // art file.
569 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
570 bool have_dex = false;
571 if (src != nullptr) {
572 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
573 if (dex != nullptr) {
574 have_dex = true;
575 result->SetL(dex);
576 }
577 }
578 if (!have_dex) {
579 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200580 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700581 }
582}
583
584static void UnstartedMemoryPeek(
585 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
586 int64_t address = shadow_frame->GetVRegLong(arg_offset);
587 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
588 // aborting the transaction.
589
590 switch (type) {
591 case Primitive::kPrimByte: {
592 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
593 return;
594 }
595
596 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700597 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
598 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700599 return;
600 }
601
602 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700603 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
604 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700605 return;
606 }
607
608 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700609 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
610 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700611 return;
612 }
613
614 case Primitive::kPrimBoolean:
615 case Primitive::kPrimChar:
616 case Primitive::kPrimFloat:
617 case Primitive::kPrimDouble:
618 case Primitive::kPrimVoid:
619 case Primitive::kPrimNot:
620 LOG(FATAL) << "Not in the Memory API: " << type;
621 UNREACHABLE();
622 }
623 LOG(FATAL) << "Should not reach here";
624 UNREACHABLE();
625}
626
Andreas Gampe799681b2015-05-15 19:24:12 -0700627void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700628 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700629 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
630}
631
632void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700634 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
635}
636
637void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700639 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
640}
641
642void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700643 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700644 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700645}
646
647static void UnstartedMemoryPeekArray(
648 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700649 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700650 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
651 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
652 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200653 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700654 return;
655 }
656 mirror::Array* array = obj->AsArray();
657
658 int offset = shadow_frame->GetVReg(arg_offset + 3);
659 int count = shadow_frame->GetVReg(arg_offset + 4);
660 if (offset < 0 || offset + count > array->GetLength()) {
661 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
662 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200663 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700664 return;
665 }
666
667 switch (type) {
668 case Primitive::kPrimByte: {
669 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
670 mirror::ByteArray* byte_array = array->AsByteArray();
671 for (int32_t i = 0; i < count; ++i, ++address) {
672 byte_array->SetWithoutChecks<true>(i + offset, *address);
673 }
674 return;
675 }
676
677 case Primitive::kPrimShort:
678 case Primitive::kPrimInt:
679 case Primitive::kPrimLong:
680 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
681 UNREACHABLE();
682
683 case Primitive::kPrimBoolean:
684 case Primitive::kPrimChar:
685 case Primitive::kPrimFloat:
686 case Primitive::kPrimDouble:
687 case Primitive::kPrimVoid:
688 case Primitive::kPrimNot:
689 LOG(FATAL) << "Not in the Memory API: " << type;
690 UNREACHABLE();
691 }
692 LOG(FATAL) << "Should not reach here";
693 UNREACHABLE();
694}
695
Andreas Gampe799681b2015-05-15 19:24:12 -0700696void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700697 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700698 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700699}
700
Andreas Gampef778eb22015-04-13 14:17:09 -0700701// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700702void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700703 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
704 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700705 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700706
707 std::vector<std::string> split;
708 Split(runtime->GetBootClassPathString(), ':', &split);
709 if (split.empty()) {
710 AbortTransactionOrFail(self,
711 "Boot classpath not set or split error:: %s",
712 runtime->GetBootClassPathString().c_str());
713 return;
714 }
715 const std::string& source = split[0];
716
Andreas Gampef778eb22015-04-13 14:17:09 -0700717 mirror::String* string_data;
718
719 // Use a block to enclose the I/O and MemMap code so buffers are released early.
720 {
721 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700722 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700723 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700724 AbortTransactionOrFail(self,
725 "Could not open zip file %s: %s",
726 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700727 error_msg.c_str());
728 return;
729 }
730 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
731 &error_msg));
732 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700733 AbortTransactionOrFail(self,
734 "Could not find security.properties file in %s: %s",
735 source.c_str(),
736 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700737 return;
738 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700739 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700740 "java/security/security.properties",
741 &error_msg));
742 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700743 AbortTransactionOrFail(self,
744 "Could not unzip security.properties file in %s: %s",
745 source.c_str(),
746 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700747 return;
748 }
749
750 uint32_t length = zip_entry->GetUncompressedLength();
751 std::unique_ptr<char[]> tmp(new char[length + 1]);
752 memcpy(tmp.get(), map->Begin(), length);
753 tmp.get()[length] = 0; // null terminator
754
755 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
756 }
757
758 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700759 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700760 return;
761 }
762
763 // Create a StringReader.
764 StackHandleScope<3> hs(self);
765 Handle<mirror::String> h_string(hs.NewHandle(string_data));
766
767 Handle<mirror::Class> h_class(hs.NewHandle(
768 runtime->GetClassLinker()->FindClass(self,
769 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800770 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700771 if (h_class.Get() == nullptr) {
772 AbortTransactionOrFail(self, "Could not find StringReader class");
773 return;
774 }
775
776 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
777 AbortTransactionOrFail(self, "Could not initialize StringReader class");
778 return;
779 }
780
781 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
782 if (h_obj.Get() == nullptr) {
783 AbortTransactionOrFail(self, "Could not allocate StringReader object");
784 return;
785 }
786
Mathieu Chartiere401d142015-04-22 13:56:20 -0700787 auto* cl = Runtime::Current()->GetClassLinker();
788 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
789 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700790 if (constructor == nullptr) {
791 AbortTransactionOrFail(self, "Could not find StringReader constructor");
792 return;
793 }
794
795 uint32_t args[1];
796 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
797 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
798
799 if (self->IsExceptionPending()) {
800 AbortTransactionOrFail(self, "Could not run StringReader constructor");
801 return;
802 }
803
804 result->SetL(h_obj.Get());
805}
806
Kenny Root1c9e61c2015-05-14 15:58:17 -0700807// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700808void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700809 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700810 jint start = shadow_frame->GetVReg(arg_offset + 1);
811 jint end = shadow_frame->GetVReg(arg_offset + 2);
812 jint index = shadow_frame->GetVReg(arg_offset + 4);
813 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
814 if (string == nullptr) {
815 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
816 return;
817 }
Kenny Root57f91e82015-05-14 15:58:17 -0700818 DCHECK_GE(start, 0);
819 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700820 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700821 Handle<mirror::CharArray> h_char_array(
822 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700823 DCHECK_LE(index, h_char_array->GetLength());
824 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700825 string->GetChars(start, end, h_char_array, index);
826}
827
828// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700829void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700830 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700831 jint index = shadow_frame->GetVReg(arg_offset + 1);
832 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
833 if (string == nullptr) {
834 AbortTransactionOrFail(self, "String.charAt with null object");
835 return;
836 }
837 result->SetC(string->CharAt(index));
838}
839
Kenny Root57f91e82015-05-14 15:58:17 -0700840// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700841void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700842 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700843 jint index = shadow_frame->GetVReg(arg_offset + 1);
844 jchar c = shadow_frame->GetVReg(arg_offset + 2);
845 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
846 if (string == nullptr) {
847 AbortTransactionOrFail(self, "String.setCharAt with null object");
848 return;
849 }
850 string->SetCharAt(index, c);
851}
852
Kenny Root1c9e61c2015-05-14 15:58:17 -0700853// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700854void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700856 jint offset = shadow_frame->GetVReg(arg_offset);
857 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
858 DCHECK_GE(char_count, 0);
859 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700860 Handle<mirror::CharArray> h_char_array(
861 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700862 Runtime* runtime = Runtime::Current();
863 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
864 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
865}
866
867// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700868void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700869 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700870 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
871 if (to_copy == nullptr) {
872 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
873 return;
874 }
875 StackHandleScope<1> hs(self);
876 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
877 Runtime* runtime = Runtime::Current();
878 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
879 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
880 allocator));
881}
882
Andreas Gampe799681b2015-05-15 19:24:12 -0700883void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700884 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700885 jint start = shadow_frame->GetVReg(arg_offset + 1);
886 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700887 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700888 DCHECK_GE(length, 0);
889 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700890 Handle<mirror::String> h_string(
891 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700892 DCHECK_LE(start, h_string->GetLength());
893 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700894 Runtime* runtime = Runtime::Current();
895 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
896 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
897}
898
Kenny Root57f91e82015-05-14 15:58:17 -0700899// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700900void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700901 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700902 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700903 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
904 if (string == nullptr) {
905 AbortTransactionOrFail(self, "String.charAt with null object");
906 return;
907 }
908 result->SetL(string->ToCharArray(self));
909}
910
Andreas Gampebc4d2182016-02-22 10:03:12 -0800911// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
912void UnstartedRuntime::UnstartedReferenceGetReferent(
913 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
914 mirror::Reference* const ref = down_cast<mirror::Reference*>(
915 shadow_frame->GetVRegReference(arg_offset));
916 if (ref == nullptr) {
917 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
918 return;
919 }
920 mirror::Object* const referent =
921 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
922 result->SetL(referent);
923}
924
925// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
926// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
927// where we can predict the behavior (somewhat).
928// Note: this is required (instead of lazy initialization) as these classes are used in the static
929// initialization of other classes, so will *use* the value.
930void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
931 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
932 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
933 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
934 // SynchronousQueue really only separates between single- and multiprocessor case. Return
935 // 8 as a conservative upper approximation.
936 result->SetI(8);
937 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
938 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
939 // a good upper bound.
940 // TODO: Consider resetting in the zygote?
941 result->SetI(8);
942 } else {
943 // Not supported.
944 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
945 }
946}
947
948// This allows accessing ConcurrentHashMap/SynchronousQueue.
949
950void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
951 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
952 // Argument 0 is the Unsafe instance, skip.
953 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
954 if (obj == nullptr) {
955 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
956 return;
957 }
958 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
959 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
960 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
961
962 // Must use non transactional mode.
963 if (kUseReadBarrier) {
964 // Need to make sure the reference stored in the field is a to-space one before attempting the
965 // CAS or the CAS could fail incorrectly.
966 mirror::HeapReference<mirror::Object>* field_addr =
967 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
968 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
969 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
970 obj,
971 MemberOffset(offset),
972 field_addr);
973 }
974 bool success;
975 // Check whether we're in a transaction, call accordingly.
976 if (Runtime::Current()->IsActiveTransaction()) {
977 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
978 expectedValue,
979 newValue);
980 } else {
981 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
982 expectedValue,
983 newValue);
984 }
985 result->SetZ(success ? 1 : 0);
986}
987
988void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
989 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
990 // Argument 0 is the Unsafe instance, skip.
991 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
992 if (obj == nullptr) {
993 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
994 return;
995 }
996 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
997 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
998 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
999
1000 // Must use non transactional mode.
1001 if (kUseReadBarrier) {
1002 // Need to make sure the reference stored in the field is a to-space one before attempting the
1003 // CAS or the CAS could fail incorrectly.
1004 mirror::HeapReference<mirror::Object>* field_addr =
1005 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1006 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1007 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1008 obj,
1009 MemberOffset(offset),
1010 field_addr);
1011 }
1012 bool success;
1013 // Check whether we're in a transaction, call accordingly.
1014 if (Runtime::Current()->IsActiveTransaction()) {
1015 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1016 expected_value,
1017 newValue);
1018 } else {
1019 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1020 expected_value,
1021 newValue);
1022 }
1023 result->SetZ(success ? 1 : 0);
1024}
1025
1026void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1027 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1028 SHARED_REQUIRES(Locks::mutator_lock_) {
1029 // Argument 0 is the Unsafe instance, skip.
1030 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1031 if (obj == nullptr) {
1032 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1033 return;
1034 }
1035 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1036 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1037 result->SetL(value);
1038}
1039
1040void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1041 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1042 SHARED_REQUIRES(Locks::mutator_lock_) {
1043 // Argument 0 is the Unsafe instance, skip.
1044 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1045 if (obj == nullptr) {
1046 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1047 return;
1048 }
1049 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1050 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1051 QuasiAtomic::ThreadFenceRelease();
1052 if (Runtime::Current()->IsActiveTransaction()) {
1053 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1054 } else {
1055 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1056 }
1057}
1058
1059
Mathieu Chartiere401d142015-04-22 13:56:20 -07001060void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1061 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1062 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001063 int32_t length = args[1];
1064 DCHECK_GE(length, 0);
1065 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1066 Runtime* runtime = Runtime::Current();
1067 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1068 DCHECK(array_class != nullptr);
1069 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1070 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1071 array_class->GetComponentSizeShift(), allocator));
1072}
1073
Mathieu Chartiere401d142015-04-22 13:56:20 -07001074void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1075 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1076 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001077 result->SetL(nullptr);
1078}
1079
Mathieu Chartiere401d142015-04-22 13:56:20 -07001080void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1081 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1082 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001083 NthCallerVisitor visitor(self, 3);
1084 visitor.WalkStack();
1085 if (visitor.caller != nullptr) {
1086 result->SetL(visitor.caller->GetDeclaringClass());
1087 }
1088}
1089
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090void UnstartedRuntime::UnstartedJNIMathLog(
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(log(value.GetD()));
1096}
1097
Mathieu Chartiere401d142015-04-22 13:56:20 -07001098void UnstartedRuntime::UnstartedJNIMathExp(
1099 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1100 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001101 JValue value;
1102 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1103 result->SetD(exp(value.GetD()));
1104}
1105
Andreas Gampebc4d2182016-02-22 10:03:12 -08001106void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1107 Thread* self ATTRIBUTE_UNUSED,
1108 ArtMethod* method ATTRIBUTE_UNUSED,
1109 mirror::Object* receiver ATTRIBUTE_UNUSED,
1110 uint32_t* args ATTRIBUTE_UNUSED,
1111 JValue* result) {
1112 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1113 ? 0
1114 : 1);
1115}
1116
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1118 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1119 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001120 StackHandleScope<1> hs(self);
1121 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1122}
1123
Andreas Gampebc4d2182016-02-22 10:03:12 -08001124void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1125 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1126 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1127 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1128 result->SetD(bit_cast<double>(long_input));
1129}
1130
Mathieu Chartiere401d142015-04-22 13:56:20 -07001131void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1132 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1133 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001134 result->SetI(args[0]);
1135}
1136
Mathieu Chartiere401d142015-04-22 13:56:20 -07001137void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1138 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1139 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001140 result->SetI(args[0]);
1141}
1142
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1144 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1145 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001146 result->SetL(receiver->Clone(self));
1147}
1148
Mathieu Chartiere401d142015-04-22 13:56:20 -07001149void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1150 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1151 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001152 receiver->NotifyAll(self);
1153}
1154
Mathieu Chartiere401d142015-04-22 13:56:20 -07001155void UnstartedRuntime::UnstartedJNIStringCompareTo(
1156 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1157 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001158 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1159 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001160 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001161 }
1162 result->SetI(receiver->AsString()->CompareTo(rhs));
1163}
1164
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165void UnstartedRuntime::UnstartedJNIStringIntern(
1166 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1167 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001168 result->SetL(receiver->AsString()->Intern());
1169}
1170
Mathieu Chartiere401d142015-04-22 13:56:20 -07001171void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1172 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1173 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001174 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1175}
1176
Mathieu Chartiere401d142015-04-22 13:56:20 -07001177void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1178 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1179 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001180 StackHandleScope<2> hs(self);
1181 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1182 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1183 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1184}
1185
Mathieu Chartiere401d142015-04-22 13:56:20 -07001186void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1187 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1188 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001189 int32_t length = static_cast<int32_t>(args[1]);
1190 if (length < 0) {
1191 ThrowNegativeArraySizeException(length);
1192 return;
1193 }
1194 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1195 Runtime* runtime = Runtime::Current();
1196 ClassLinker* class_linker = runtime->GetClassLinker();
1197 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001198 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001199 CHECK(self->IsExceptionPending());
1200 return;
1201 }
1202 DCHECK(array_class->IsObjectArrayClass());
1203 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1204 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1205 result->SetL(new_array);
1206}
1207
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1209 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1210 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001211 ScopedObjectAccessUnchecked soa(self);
1212 if (Runtime::Current()->IsActiveTransaction()) {
1213 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1214 } else {
1215 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1216 }
1217}
1218
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1220 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1221 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001222 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1223 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1224}
1225
Mathieu Chartiere401d142015-04-22 13:56:20 -07001226void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1227 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1228 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001229 result->SetZ(JNI_TRUE);
1230}
1231
Mathieu Chartiere401d142015-04-22 13:56:20 -07001232void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1233 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1234 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001235 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1236 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1237 jint expectedValue = args[3];
1238 jint newValue = args[4];
1239 bool success;
1240 if (Runtime::Current()->IsActiveTransaction()) {
1241 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1242 expectedValue, newValue);
1243 } else {
1244 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1245 expectedValue, newValue);
1246 }
1247 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1248}
1249
Narayan Kamath34a316f2016-03-30 13:11:18 +01001250void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1251 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1252 uint32_t* args, JValue* result) {
1253 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1254 if (obj == nullptr) {
1255 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1256 return;
1257 }
1258
1259 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1260 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1261}
1262
Mathieu Chartiere401d142015-04-22 13:56:20 -07001263void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1264 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1265 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001266 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1267 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1268 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1269 if (Runtime::Current()->IsActiveTransaction()) {
1270 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1271 } else {
1272 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1273 }
1274}
1275
Andreas Gampe799681b2015-05-15 19:24:12 -07001276void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001277 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1278 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001279 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1280 Primitive::Type primitive_type = component->GetPrimitiveType();
1281 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1282}
1283
Andreas Gampe799681b2015-05-15 19:24:12 -07001284void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001285 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1286 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001287 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1288 Primitive::Type primitive_type = component->GetPrimitiveType();
1289 result->SetI(Primitive::ComponentSize(primitive_type));
1290}
1291
Andreas Gampedd9d0552015-03-09 12:57:41 -07001292typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001293 size_t arg_size);
1294
Mathieu Chartiere401d142015-04-22 13:56:20 -07001295typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001296 uint32_t* args, JValue* result);
1297
1298static bool tables_initialized_ = false;
1299static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1300static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1301
Andreas Gampe799681b2015-05-15 19:24:12 -07001302void UnstartedRuntime::InitializeInvokeHandlers() {
1303#define UNSTARTED_DIRECT(ShortName, Sig) \
1304 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1305#include "unstarted_runtime_list.h"
1306 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1307#undef UNSTARTED_RUNTIME_DIRECT_LIST
1308#undef UNSTARTED_RUNTIME_JNI_LIST
1309#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001310}
1311
Andreas Gampe799681b2015-05-15 19:24:12 -07001312void UnstartedRuntime::InitializeJNIHandlers() {
1313#define UNSTARTED_JNI(ShortName, Sig) \
1314 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1315#include "unstarted_runtime_list.h"
1316 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1317#undef UNSTARTED_RUNTIME_DIRECT_LIST
1318#undef UNSTARTED_RUNTIME_JNI_LIST
1319#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001320}
1321
Andreas Gampe799681b2015-05-15 19:24:12 -07001322void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001323 CHECK(!tables_initialized_);
1324
Andreas Gampe799681b2015-05-15 19:24:12 -07001325 InitializeInvokeHandlers();
1326 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001327
1328 tables_initialized_ = true;
1329}
1330
Andreas Gampe799681b2015-05-15 19:24:12 -07001331void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1332 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001333 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1334 // problems in core libraries.
1335 CHECK(tables_initialized_);
1336
1337 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1338 const auto& iter = invoke_handlers_.find(name);
1339 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001340 // Clear out the result in case it's not zeroed out.
1341 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001342 (*iter->second)(self, shadow_frame, result, arg_offset);
1343 } else {
1344 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001345 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001346 }
1347}
1348
1349// 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 -07001350void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001351 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001352 std::string name(PrettyMethod(method));
1353 const auto& iter = jni_handlers_.find(name);
1354 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001355 // Clear out the result in case it's not zeroed out.
1356 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001357 (*iter->second)(self, method, receiver, args, result);
1358 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001359 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1360 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001361 } else {
1362 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1363 "non-transactional runtime";
1364 }
1365}
1366
1367} // namespace interpreter
1368} // namespace art