blob: 30733b57b98df3ee73ab15db6aac00ddf34c522e [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 mirror::Array* src_array = shadow_frame->GetVRegReference(arg_offset)->AsArray();
357 mirror::Array* dst_array = shadow_frame->GetVRegReference(arg_offset + 2)->AsArray();
358
359 // Null checking.
360 if (src_array == nullptr) {
361 AbortTransactionOrFail(self, "src is null in arraycopy.");
362 return;
363 }
364 if (dst_array == nullptr) {
365 AbortTransactionOrFail(self, "dst is null in arraycopy.");
366 return;
367 }
368
369 // Bounds checking.
370 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
371 UNLIKELY(src_pos > src_array->GetLength() - length) ||
372 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
373 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
374 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
375 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
376 length);
377 AbortTransactionOrFail(self, "Index out of bounds.");
378 return;
379 }
380
381 // Type checking.
382 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
383 GetComponentType();
384
385 if (!src_type->IsPrimitive()) {
386 // Check that the second type is not primitive.
387 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
388 GetComponentType();
389 if (trg_type->IsPrimitiveInt()) {
390 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
391 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
392 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
393 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700394 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700395
396 // For simplicity only do this if the component types are the same. Otherwise we have to copy
397 // even more code from the object-array functions.
398 if (src_type != trg_type) {
399 AbortTransactionOrFail(self, "Types not the same in arraycopy: %s vs %s",
400 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
401 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
402 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700403 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700404
405 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
406 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
407 if (src == dst) {
408 // Can overlap, but not have type mismatches.
409 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
410 if (copy_forward) {
411 for (int32_t i = 0; i < length; ++i) {
412 dst->Set(dst_pos + i, src->Get(src_pos + i));
413 }
414 } else {
415 for (int32_t i = 1; i <= length; ++i) {
416 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
417 }
418 }
419 } else {
420 // Can't overlap. Would need type checks, but we abort above.
421 for (int32_t i = 0; i < length; ++i) {
422 dst->Set(dst_pos + i, src->Get(src_pos + i));
423 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700424 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700425 } else if (src_type->IsPrimitiveChar()) {
426 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
427 } else if (src_type->IsPrimitiveInt()) {
428 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700429 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700430 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700431 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700432 }
433}
434
Andreas Gampe799681b2015-05-15 19:24:12 -0700435void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700436 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700437 // Just forward.
438 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
439}
440
441void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700443 // Just forward.
444 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
445}
446
Narayan Kamath34a316f2016-03-30 13:11:18 +0100447void UnstartedRuntime::UnstartedSystemGetSecurityManager(
448 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
449 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
450 result->SetL(nullptr);
451}
452
Andreas Gampe799681b2015-05-15 19:24:12 -0700453void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700454 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700455 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
456 bool ok = false;
457 if (caller == "java.lang.String java.lang.IntegralToString.convertInt"
458 "(java.lang.AbstractStringBuilder, int)") {
459 // Allocate non-threadlocal buffer.
460 result->SetL(mirror::CharArray::Alloc(self, 11));
461 ok = true;
462 } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") {
463 // Note: RealToString is implemented and used in a different fashion than IntegralToString.
464 // Conversion is done over an actual object of RealToString (the conversion method is an
465 // instance method). This means it is not as clear whether it is correct to return a new
466 // object each time. The caller needs to be inspected by hand to see whether it (incorrectly)
467 // stores the object for later use.
468 // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString.
469 if (shadow_frame->GetLink()->GetLink() != nullptr) {
470 std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod()));
471 if (caller2 == "java.lang.String java.lang.Double.toString(double)") {
472 // Allocate new object.
473 StackHandleScope<2> hs(self);
474 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
475 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
476 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
477 h_real_to_string_class->AllocObject(self)));
478 if (h_real_to_string_obj.Get() != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700479 auto* cl = Runtime::Current()->GetClassLinker();
480 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
481 "<init>", "()V", cl->GetImagePointerSize());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700482 if (init_method == nullptr) {
483 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
484 } else {
485 JValue invoke_result;
486 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
487 nullptr);
488 if (!self->IsExceptionPending()) {
489 result->SetL(h_real_to_string_obj.Get());
490 ok = true;
491 }
492 }
493 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700494 }
495 }
496 }
497
498 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700499 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700500 }
501}
502
Andreas Gampe799681b2015-05-15 19:24:12 -0700503void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700504 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700505 double in = shadow_frame->GetVRegDouble(arg_offset);
506 double out;
507 // Special cases:
508 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
509 // -1 < in < 0 -> out := -0.
510 if (-1.0 < in && in < 0) {
511 out = -0.0;
512 } else {
513 out = ceil(in);
514 }
515 result->SetD(out);
516}
517
Andreas Gampe799681b2015-05-15 19:24:12 -0700518void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700519 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700520 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
521 result->SetI(obj->IdentityHashCode());
522}
523
Andreas Gampe799681b2015-05-15 19:24:12 -0700524void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700525 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700526 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000527 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700528}
529
Andreas Gampedd9d0552015-03-09 12:57:41 -0700530static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700531 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700532 const DexFile* dex_file = dex_cache->GetDexFile();
533 if (dex_file == nullptr) {
534 return nullptr;
535 }
536
537 // Create the direct byte buffer.
538 JNIEnv* env = self->GetJniEnv();
539 DCHECK(env != nullptr);
540 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700541 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
542 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700543 DCHECK(self->IsExceptionPending());
544 return nullptr;
545 }
546
547 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700548 args[0].l = byte_buffer.get();
549
550 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
551 WellKnownClasses::com_android_dex_Dex,
552 WellKnownClasses::com_android_dex_Dex_create,
553 args));
554
555 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700556}
557
Andreas Gampe799681b2015-05-15 19:24:12 -0700558void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700559 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700560 // We will create the Dex object, but the image writer will release it before creating the
561 // art file.
562 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
563 bool have_dex = false;
564 if (src != nullptr) {
565 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
566 if (dex != nullptr) {
567 have_dex = true;
568 result->SetL(dex);
569 }
570 }
571 if (!have_dex) {
572 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200573 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700574 }
575}
576
577static void UnstartedMemoryPeek(
578 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
579 int64_t address = shadow_frame->GetVRegLong(arg_offset);
580 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
581 // aborting the transaction.
582
583 switch (type) {
584 case Primitive::kPrimByte: {
585 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
586 return;
587 }
588
589 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700590 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
591 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700592 return;
593 }
594
595 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700596 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
597 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700598 return;
599 }
600
601 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700602 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
603 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700604 return;
605 }
606
607 case Primitive::kPrimBoolean:
608 case Primitive::kPrimChar:
609 case Primitive::kPrimFloat:
610 case Primitive::kPrimDouble:
611 case Primitive::kPrimVoid:
612 case Primitive::kPrimNot:
613 LOG(FATAL) << "Not in the Memory API: " << type;
614 UNREACHABLE();
615 }
616 LOG(FATAL) << "Should not reach here";
617 UNREACHABLE();
618}
619
Andreas Gampe799681b2015-05-15 19:24:12 -0700620void UnstartedRuntime::UnstartedMemoryPeekByte(
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::kPrimByte, shadow_frame, result, arg_offset);
623}
624
625void UnstartedRuntime::UnstartedMemoryPeekShort(
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::kPrimShort, shadow_frame, result, arg_offset);
628}
629
630void UnstartedRuntime::UnstartedMemoryPeekInt(
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::kPrimInt, shadow_frame, result, arg_offset);
633}
634
635void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700637 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700638}
639
640static void UnstartedMemoryPeekArray(
641 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700642 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700643 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
644 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
645 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200646 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700647 return;
648 }
649 mirror::Array* array = obj->AsArray();
650
651 int offset = shadow_frame->GetVReg(arg_offset + 3);
652 int count = shadow_frame->GetVReg(arg_offset + 4);
653 if (offset < 0 || offset + count > array->GetLength()) {
654 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
655 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200656 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700657 return;
658 }
659
660 switch (type) {
661 case Primitive::kPrimByte: {
662 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
663 mirror::ByteArray* byte_array = array->AsByteArray();
664 for (int32_t i = 0; i < count; ++i, ++address) {
665 byte_array->SetWithoutChecks<true>(i + offset, *address);
666 }
667 return;
668 }
669
670 case Primitive::kPrimShort:
671 case Primitive::kPrimInt:
672 case Primitive::kPrimLong:
673 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
674 UNREACHABLE();
675
676 case Primitive::kPrimBoolean:
677 case Primitive::kPrimChar:
678 case Primitive::kPrimFloat:
679 case Primitive::kPrimDouble:
680 case Primitive::kPrimVoid:
681 case Primitive::kPrimNot:
682 LOG(FATAL) << "Not in the Memory API: " << type;
683 UNREACHABLE();
684 }
685 LOG(FATAL) << "Should not reach here";
686 UNREACHABLE();
687}
688
Andreas Gampe799681b2015-05-15 19:24:12 -0700689void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700690 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700691 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700692}
693
Andreas Gampef778eb22015-04-13 14:17:09 -0700694// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700695void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700696 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
697 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700698 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700699
700 std::vector<std::string> split;
701 Split(runtime->GetBootClassPathString(), ':', &split);
702 if (split.empty()) {
703 AbortTransactionOrFail(self,
704 "Boot classpath not set or split error:: %s",
705 runtime->GetBootClassPathString().c_str());
706 return;
707 }
708 const std::string& source = split[0];
709
Andreas Gampef778eb22015-04-13 14:17:09 -0700710 mirror::String* string_data;
711
712 // Use a block to enclose the I/O and MemMap code so buffers are released early.
713 {
714 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700715 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700716 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700717 AbortTransactionOrFail(self,
718 "Could not open zip file %s: %s",
719 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700720 error_msg.c_str());
721 return;
722 }
723 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
724 &error_msg));
725 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700726 AbortTransactionOrFail(self,
727 "Could not find security.properties file in %s: %s",
728 source.c_str(),
729 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700730 return;
731 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700732 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700733 "java/security/security.properties",
734 &error_msg));
735 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700736 AbortTransactionOrFail(self,
737 "Could not unzip security.properties file in %s: %s",
738 source.c_str(),
739 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700740 return;
741 }
742
743 uint32_t length = zip_entry->GetUncompressedLength();
744 std::unique_ptr<char[]> tmp(new char[length + 1]);
745 memcpy(tmp.get(), map->Begin(), length);
746 tmp.get()[length] = 0; // null terminator
747
748 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
749 }
750
751 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700752 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700753 return;
754 }
755
756 // Create a StringReader.
757 StackHandleScope<3> hs(self);
758 Handle<mirror::String> h_string(hs.NewHandle(string_data));
759
760 Handle<mirror::Class> h_class(hs.NewHandle(
761 runtime->GetClassLinker()->FindClass(self,
762 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800763 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700764 if (h_class.Get() == nullptr) {
765 AbortTransactionOrFail(self, "Could not find StringReader class");
766 return;
767 }
768
769 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
770 AbortTransactionOrFail(self, "Could not initialize StringReader class");
771 return;
772 }
773
774 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
775 if (h_obj.Get() == nullptr) {
776 AbortTransactionOrFail(self, "Could not allocate StringReader object");
777 return;
778 }
779
Mathieu Chartiere401d142015-04-22 13:56:20 -0700780 auto* cl = Runtime::Current()->GetClassLinker();
781 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
782 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700783 if (constructor == nullptr) {
784 AbortTransactionOrFail(self, "Could not find StringReader constructor");
785 return;
786 }
787
788 uint32_t args[1];
789 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
790 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
791
792 if (self->IsExceptionPending()) {
793 AbortTransactionOrFail(self, "Could not run StringReader constructor");
794 return;
795 }
796
797 result->SetL(h_obj.Get());
798}
799
Kenny Root1c9e61c2015-05-14 15:58:17 -0700800// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700801void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700802 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700803 jint start = shadow_frame->GetVReg(arg_offset + 1);
804 jint end = shadow_frame->GetVReg(arg_offset + 2);
805 jint index = shadow_frame->GetVReg(arg_offset + 4);
806 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
807 if (string == nullptr) {
808 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
809 return;
810 }
Kenny Root57f91e82015-05-14 15:58:17 -0700811 DCHECK_GE(start, 0);
812 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700813 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700814 Handle<mirror::CharArray> h_char_array(
815 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700816 DCHECK_LE(index, h_char_array->GetLength());
817 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700818 string->GetChars(start, end, h_char_array, index);
819}
820
821// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700822void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700823 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700824 jint index = shadow_frame->GetVReg(arg_offset + 1);
825 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
826 if (string == nullptr) {
827 AbortTransactionOrFail(self, "String.charAt with null object");
828 return;
829 }
830 result->SetC(string->CharAt(index));
831}
832
Kenny Root57f91e82015-05-14 15:58:17 -0700833// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700834void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700835 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700836 jint index = shadow_frame->GetVReg(arg_offset + 1);
837 jchar c = shadow_frame->GetVReg(arg_offset + 2);
838 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
839 if (string == nullptr) {
840 AbortTransactionOrFail(self, "String.setCharAt with null object");
841 return;
842 }
843 string->SetCharAt(index, c);
844}
845
Kenny Root1c9e61c2015-05-14 15:58:17 -0700846// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700847void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700848 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700849 jint offset = shadow_frame->GetVReg(arg_offset);
850 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
851 DCHECK_GE(char_count, 0);
852 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853 Handle<mirror::CharArray> h_char_array(
854 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700855 Runtime* runtime = Runtime::Current();
856 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
857 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
858}
859
860// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700861void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700862 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700863 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
864 if (to_copy == nullptr) {
865 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
866 return;
867 }
868 StackHandleScope<1> hs(self);
869 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
870 Runtime* runtime = Runtime::Current();
871 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
872 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
873 allocator));
874}
875
Andreas Gampe799681b2015-05-15 19:24:12 -0700876void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700877 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700878 jint start = shadow_frame->GetVReg(arg_offset + 1);
879 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700880 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700881 DCHECK_GE(length, 0);
882 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883 Handle<mirror::String> h_string(
884 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700885 DCHECK_LE(start, h_string->GetLength());
886 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700887 Runtime* runtime = Runtime::Current();
888 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
889 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
890}
891
Kenny Root57f91e82015-05-14 15:58:17 -0700892// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700893void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700894 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700895 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700896 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
897 if (string == nullptr) {
898 AbortTransactionOrFail(self, "String.charAt with null object");
899 return;
900 }
901 result->SetL(string->ToCharArray(self));
902}
903
Andreas Gampebc4d2182016-02-22 10:03:12 -0800904// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
905void UnstartedRuntime::UnstartedReferenceGetReferent(
906 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
907 mirror::Reference* const ref = down_cast<mirror::Reference*>(
908 shadow_frame->GetVRegReference(arg_offset));
909 if (ref == nullptr) {
910 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
911 return;
912 }
913 mirror::Object* const referent =
914 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
915 result->SetL(referent);
916}
917
918// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
919// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
920// where we can predict the behavior (somewhat).
921// Note: this is required (instead of lazy initialization) as these classes are used in the static
922// initialization of other classes, so will *use* the value.
923void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
924 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
925 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
926 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
927 // SynchronousQueue really only separates between single- and multiprocessor case. Return
928 // 8 as a conservative upper approximation.
929 result->SetI(8);
930 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
931 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
932 // a good upper bound.
933 // TODO: Consider resetting in the zygote?
934 result->SetI(8);
935 } else {
936 // Not supported.
937 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
938 }
939}
940
941// This allows accessing ConcurrentHashMap/SynchronousQueue.
942
943void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
944 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
945 // Argument 0 is the Unsafe instance, skip.
946 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
947 if (obj == nullptr) {
948 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
949 return;
950 }
951 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
952 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
953 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
954
955 // Must use non transactional mode.
956 if (kUseReadBarrier) {
957 // Need to make sure the reference stored in the field is a to-space one before attempting the
958 // CAS or the CAS could fail incorrectly.
959 mirror::HeapReference<mirror::Object>* field_addr =
960 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
961 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
962 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
963 obj,
964 MemberOffset(offset),
965 field_addr);
966 }
967 bool success;
968 // Check whether we're in a transaction, call accordingly.
969 if (Runtime::Current()->IsActiveTransaction()) {
970 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
971 expectedValue,
972 newValue);
973 } else {
974 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
975 expectedValue,
976 newValue);
977 }
978 result->SetZ(success ? 1 : 0);
979}
980
981void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
982 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
983 // Argument 0 is the Unsafe instance, skip.
984 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
985 if (obj == nullptr) {
986 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
987 return;
988 }
989 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
990 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
991 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
992
993 // Must use non transactional mode.
994 if (kUseReadBarrier) {
995 // Need to make sure the reference stored in the field is a to-space one before attempting the
996 // CAS or the CAS could fail incorrectly.
997 mirror::HeapReference<mirror::Object>* field_addr =
998 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
999 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1000 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1001 obj,
1002 MemberOffset(offset),
1003 field_addr);
1004 }
1005 bool success;
1006 // Check whether we're in a transaction, call accordingly.
1007 if (Runtime::Current()->IsActiveTransaction()) {
1008 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1009 expected_value,
1010 newValue);
1011 } else {
1012 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1013 expected_value,
1014 newValue);
1015 }
1016 result->SetZ(success ? 1 : 0);
1017}
1018
1019void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1020 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1021 SHARED_REQUIRES(Locks::mutator_lock_) {
1022 // Argument 0 is the Unsafe instance, skip.
1023 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1024 if (obj == nullptr) {
1025 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1026 return;
1027 }
1028 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1029 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1030 result->SetL(value);
1031}
1032
1033void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1034 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1035 SHARED_REQUIRES(Locks::mutator_lock_) {
1036 // Argument 0 is the Unsafe instance, skip.
1037 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1038 if (obj == nullptr) {
1039 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1040 return;
1041 }
1042 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1043 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1044 QuasiAtomic::ThreadFenceRelease();
1045 if (Runtime::Current()->IsActiveTransaction()) {
1046 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1047 } else {
1048 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1049 }
1050}
1051
1052
Mathieu Chartiere401d142015-04-22 13:56:20 -07001053void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1054 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1055 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001056 int32_t length = args[1];
1057 DCHECK_GE(length, 0);
1058 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1059 Runtime* runtime = Runtime::Current();
1060 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1061 DCHECK(array_class != nullptr);
1062 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1063 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1064 array_class->GetComponentSizeShift(), allocator));
1065}
1066
Mathieu Chartiere401d142015-04-22 13:56:20 -07001067void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1068 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1069 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001070 result->SetL(nullptr);
1071}
1072
Mathieu Chartiere401d142015-04-22 13:56:20 -07001073void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1074 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1075 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001076 NthCallerVisitor visitor(self, 3);
1077 visitor.WalkStack();
1078 if (visitor.caller != nullptr) {
1079 result->SetL(visitor.caller->GetDeclaringClass());
1080 }
1081}
1082
Mathieu Chartiere401d142015-04-22 13:56:20 -07001083void UnstartedRuntime::UnstartedJNIMathLog(
1084 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1085 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001086 JValue value;
1087 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1088 result->SetD(log(value.GetD()));
1089}
1090
Mathieu Chartiere401d142015-04-22 13:56:20 -07001091void UnstartedRuntime::UnstartedJNIMathExp(
1092 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1093 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001094 JValue value;
1095 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1096 result->SetD(exp(value.GetD()));
1097}
1098
Andreas Gampebc4d2182016-02-22 10:03:12 -08001099void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1100 Thread* self ATTRIBUTE_UNUSED,
1101 ArtMethod* method ATTRIBUTE_UNUSED,
1102 mirror::Object* receiver ATTRIBUTE_UNUSED,
1103 uint32_t* args ATTRIBUTE_UNUSED,
1104 JValue* result) {
1105 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1106 ? 0
1107 : 1);
1108}
1109
Mathieu Chartiere401d142015-04-22 13:56:20 -07001110void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1111 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1112 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001113 StackHandleScope<1> hs(self);
1114 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1115}
1116
Andreas Gampebc4d2182016-02-22 10:03:12 -08001117void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1118 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1119 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1120 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1121 result->SetD(bit_cast<double>(long_input));
1122}
1123
Mathieu Chartiere401d142015-04-22 13:56:20 -07001124void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1125 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1126 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001127 result->SetI(args[0]);
1128}
1129
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1131 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1132 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001133 result->SetI(args[0]);
1134}
1135
Mathieu Chartiere401d142015-04-22 13:56:20 -07001136void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1137 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1138 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001139 result->SetL(receiver->Clone(self));
1140}
1141
Mathieu Chartiere401d142015-04-22 13:56:20 -07001142void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1143 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1144 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001145 receiver->NotifyAll(self);
1146}
1147
Mathieu Chartiere401d142015-04-22 13:56:20 -07001148void UnstartedRuntime::UnstartedJNIStringCompareTo(
1149 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1150 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001151 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1152 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001153 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001154 }
1155 result->SetI(receiver->AsString()->CompareTo(rhs));
1156}
1157
Mathieu Chartiere401d142015-04-22 13:56:20 -07001158void UnstartedRuntime::UnstartedJNIStringIntern(
1159 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1160 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001161 result->SetL(receiver->AsString()->Intern());
1162}
1163
Mathieu Chartiere401d142015-04-22 13:56:20 -07001164void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1165 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1166 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001167 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1168}
1169
Mathieu Chartiere401d142015-04-22 13:56:20 -07001170void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1171 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1172 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001173 StackHandleScope<2> hs(self);
1174 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1175 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1176 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1177}
1178
Mathieu Chartiere401d142015-04-22 13:56:20 -07001179void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1180 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1181 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001182 int32_t length = static_cast<int32_t>(args[1]);
1183 if (length < 0) {
1184 ThrowNegativeArraySizeException(length);
1185 return;
1186 }
1187 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1188 Runtime* runtime = Runtime::Current();
1189 ClassLinker* class_linker = runtime->GetClassLinker();
1190 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001191 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001192 CHECK(self->IsExceptionPending());
1193 return;
1194 }
1195 DCHECK(array_class->IsObjectArrayClass());
1196 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1197 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1198 result->SetL(new_array);
1199}
1200
Mathieu Chartiere401d142015-04-22 13:56:20 -07001201void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1202 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1203 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001204 ScopedObjectAccessUnchecked soa(self);
1205 if (Runtime::Current()->IsActiveTransaction()) {
1206 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1207 } else {
1208 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1209 }
1210}
1211
Mathieu Chartiere401d142015-04-22 13:56:20 -07001212void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1213 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1214 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001215 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1216 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1217}
1218
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1220 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1221 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001222 result->SetZ(JNI_TRUE);
1223}
1224
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1226 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1227 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001228 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1229 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1230 jint expectedValue = args[3];
1231 jint newValue = args[4];
1232 bool success;
1233 if (Runtime::Current()->IsActiveTransaction()) {
1234 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1235 expectedValue, newValue);
1236 } else {
1237 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1238 expectedValue, newValue);
1239 }
1240 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1241}
1242
Narayan Kamath34a316f2016-03-30 13:11:18 +01001243void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1244 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1245 uint32_t* args, JValue* result) {
1246 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1247 if (obj == nullptr) {
1248 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1249 return;
1250 }
1251
1252 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1253 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1254}
1255
Mathieu Chartiere401d142015-04-22 13:56:20 -07001256void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1257 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1258 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001259 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1260 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1261 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1262 if (Runtime::Current()->IsActiveTransaction()) {
1263 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1264 } else {
1265 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1266 }
1267}
1268
Andreas Gampe799681b2015-05-15 19:24:12 -07001269void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001270 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1271 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001272 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1273 Primitive::Type primitive_type = component->GetPrimitiveType();
1274 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1275}
1276
Andreas Gampe799681b2015-05-15 19:24:12 -07001277void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001278 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1279 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001280 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1281 Primitive::Type primitive_type = component->GetPrimitiveType();
1282 result->SetI(Primitive::ComponentSize(primitive_type));
1283}
1284
Andreas Gampedd9d0552015-03-09 12:57:41 -07001285typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001286 size_t arg_size);
1287
Mathieu Chartiere401d142015-04-22 13:56:20 -07001288typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001289 uint32_t* args, JValue* result);
1290
1291static bool tables_initialized_ = false;
1292static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1293static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1294
Andreas Gampe799681b2015-05-15 19:24:12 -07001295void UnstartedRuntime::InitializeInvokeHandlers() {
1296#define UNSTARTED_DIRECT(ShortName, Sig) \
1297 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1298#include "unstarted_runtime_list.h"
1299 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1300#undef UNSTARTED_RUNTIME_DIRECT_LIST
1301#undef UNSTARTED_RUNTIME_JNI_LIST
1302#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001303}
1304
Andreas Gampe799681b2015-05-15 19:24:12 -07001305void UnstartedRuntime::InitializeJNIHandlers() {
1306#define UNSTARTED_JNI(ShortName, Sig) \
1307 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1308#include "unstarted_runtime_list.h"
1309 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1310#undef UNSTARTED_RUNTIME_DIRECT_LIST
1311#undef UNSTARTED_RUNTIME_JNI_LIST
1312#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001313}
1314
Andreas Gampe799681b2015-05-15 19:24:12 -07001315void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001316 CHECK(!tables_initialized_);
1317
Andreas Gampe799681b2015-05-15 19:24:12 -07001318 InitializeInvokeHandlers();
1319 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001320
1321 tables_initialized_ = true;
1322}
1323
Andreas Gampe799681b2015-05-15 19:24:12 -07001324void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1325 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001326 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1327 // problems in core libraries.
1328 CHECK(tables_initialized_);
1329
1330 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1331 const auto& iter = invoke_handlers_.find(name);
1332 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001333 // Clear out the result in case it's not zeroed out.
1334 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001335 (*iter->second)(self, shadow_frame, result, arg_offset);
1336 } else {
1337 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001338 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001339 }
1340}
1341
1342// 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 -07001343void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001344 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001345 std::string name(PrettyMethod(method));
1346 const auto& iter = jni_handlers_.find(name);
1347 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001348 // Clear out the result in case it's not zeroed out.
1349 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001350 (*iter->second)(self, method, receiver, args, result);
1351 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001352 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1353 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001354 } else {
1355 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1356 "non-transactional runtime";
1357 }
1358}
1359
1360} // namespace interpreter
1361} // namespace art