blob: 80554c240d8bf9e1d8b6859f3478dede26f32915 [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
Andreas Gampe8ce9c302016-04-15 21:24:28 -070019#include <ctype.h>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070020#include <errno.h>
21#include <stdlib.h>
22
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include <cmath>
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -080024#include <initializer_list>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070025#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070026#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include <unordered_map>
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampeaacc25d2015-04-01 14:49:06 -070030#include "ScopedLocalRef.h"
31
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080033#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "base/logging.h"
36#include "base/macros.h"
37#include "class_linker.h"
38#include "common_throws.h"
39#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080040#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "handle_scope-inl.h"
42#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070043#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070044#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070046#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010047#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
50#include "mirror/string-inl.h"
51#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070052#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070053#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020054#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070055#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070056#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070057
58namespace art {
59namespace interpreter {
60
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringAppendV;
62using android::base::StringPrintf;
63
Andreas Gampe068b0c02015-03-11 12:44:47 -070064static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020065 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020067
68static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070069 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070070 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020071 va_start(args, fmt);
72 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070073 va_end(args);
74 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020075 va_start(args, fmt);
76 std::string msg;
77 StringAppendV(&msg, fmt, args);
78 va_end(args);
79 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070080 UNREACHABLE();
81 }
82}
83
Andreas Gampe8ce9c302016-04-15 21:24:28 -070084// Restricted support for character upper case / lower case. Only support ASCII, where
85// it's easy. Abort the transaction otherwise.
86static void CharacterLowerUpper(Thread* self,
87 ShadowFrame* shadow_frame,
88 JValue* result,
89 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070091 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
92
93 // Only ASCII (7-bit).
94 if (!isascii(int_value)) {
95 AbortTransactionOrFail(self,
96 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
97 int_value);
98 return;
99 }
100
101 std::locale c_locale("C");
102 char char_value = static_cast<char>(int_value);
103
104 if (to_lower_case) {
105 result->SetI(std::tolower(char_value, c_locale));
106 } else {
107 result->SetI(std::toupper(char_value, c_locale));
108 }
109}
110
111void UnstartedRuntime::UnstartedCharacterToLowerCase(
112 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
113 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
114}
115
116void UnstartedRuntime::UnstartedCharacterToUpperCase(
117 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
118 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
119}
120
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700121// Helper function to deal with class loading in an unstarted runtime.
122static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
123 Handle<mirror::ClassLoader> class_loader, JValue* result,
124 const std::string& method_name, bool initialize_class,
125 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800127 CHECK(className != nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700128 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
129 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
130
131 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
132 if (found == nullptr && abort_if_not_found) {
133 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700134 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700135 method_name.c_str(),
136 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700137 }
138 return;
139 }
140 if (found != nullptr && initialize_class) {
141 StackHandleScope<1> hs(self);
142 Handle<mirror::Class> h_class(hs.NewHandle(found));
143 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
144 CHECK(self->IsExceptionPending());
145 return;
146 }
147 }
148 result->SetL(found);
149}
150
151// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
152// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
153// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200154// actually the transaction abort exception. This must not be wrapped, as it signals an
155// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700156static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700158 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200159 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700160 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200161 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700162 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
163 "ClassNotFoundException");
164 }
165 }
166}
167
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700168static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700170 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
171 if (param == nullptr) {
172 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
173 return nullptr;
174 }
175 return param->AsString();
176}
177
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800178void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
179 ShadowFrame* shadow_frame,
180 JValue* result,
181 size_t arg_offset,
182 bool long_form,
183 const char* caller) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700184 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
185 if (class_name == nullptr) {
186 return;
187 }
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800188 bool initialize_class;
189 mirror::ClassLoader* class_loader;
190 if (long_form) {
191 initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
192 class_loader = down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
193 } else {
194 initialize_class = true;
195 // TODO: This is really only correct for the boot classpath, and for robustness we should
196 // check the caller.
197 class_loader = nullptr;
198 }
199
200 ScopedObjectAccessUnchecked soa(self);
201 if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
202 AbortTransactionOrFail(self,
203 "Only the boot classloader is supported: %s",
204 mirror::Object::PrettyTypeOf(class_loader).c_str());
205 return;
206 }
207
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700208 StackHandleScope<1> hs(self);
209 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800210 UnstartedRuntimeFindClass(self,
211 h_class_name,
212 ScopedNullHandle<mirror::ClassLoader>(),
213 result,
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800214 caller,
215 initialize_class,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800216 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700217 CheckExceptionGenerateClassNotFound(self);
218}
219
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800220void UnstartedRuntime::UnstartedClassForName(
221 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
222 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
223}
224
Andreas Gampe799681b2015-05-15 19:24:12 -0700225void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800227 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700228}
229
Andreas Gampe799681b2015-05-15 19:24:12 -0700230void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800232 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700233}
234
Andreas Gampe799681b2015-05-15 19:24:12 -0700235void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700236 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
237 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700238 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
239 if (param == nullptr) {
240 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
241 return;
242 }
243 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700244 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700245
246 // Check that it's not null.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800247 if (h_klass == nullptr) {
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700248 AbortTransactionOrFail(self, "Class reference is null for newInstance");
249 return;
250 }
251
252 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
253 if (Runtime::Current()->IsActiveTransaction()) {
254 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200255 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700256 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700257 return;
258 }
259 }
260
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700261 // There are two situations in which we'll abort this run.
262 // 1) If the class isn't yet initialized and initialization fails.
263 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
264 // Note that 2) could likely be handled here, but for safety abort the transaction.
265 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 auto* cl = Runtime::Current()->GetClassLinker();
267 if (cl->EnsureInitialized(self, h_klass, true, true)) {
268 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
269 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700270 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800271 CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700272 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700273 if (!self->IsExceptionPending()) {
274 result->SetL(h_obj.Get());
275 ok = true;
276 }
277 } else {
278 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
279 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700280 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700281 }
282 }
283 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700284 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700285 h_klass->PrettyClass().c_str(),
286 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700287 }
288}
289
Andreas Gampe799681b2015-05-15 19:24:12 -0700290void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700291 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700292 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
293 // going the reflective Dex way.
294 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
295 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700296 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700297 for (ArtField& field : klass->GetIFields()) {
298 if (name2->Equals(field.GetName())) {
299 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700300 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700301 }
302 }
303 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700304 for (ArtField& field : klass->GetSFields()) {
305 if (name2->Equals(field.GetName())) {
306 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700307 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700308 }
309 }
310 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700311 if (found == nullptr) {
312 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
313 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700314 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700315 return;
316 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700317 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700318 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700319 mirror::Field* field;
320 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700321 if (pointer_size == PointerSize::k64) {
322 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
323 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700324 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700325 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
326 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700327 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700328 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700329 if (pointer_size == PointerSize::k64) {
330 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
331 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700332 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700333 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
334 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700335 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700336 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700337 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700338}
339
Andreas Gampebc4d2182016-02-22 10:03:12 -0800340// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
341void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
342 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
343 // Special managed code cut-out to allow method lookup in a un-started runtime.
344 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
345 if (klass == nullptr) {
346 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
347 return;
348 }
349 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
350 mirror::ObjectArray<mirror::Class>* args =
351 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700352 Runtime* runtime = Runtime::Current();
353 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700354 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700355 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700356 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700357 if (pointer_size == PointerSize::k64) {
358 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
359 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700360 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700361 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
362 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700363 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800364 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700365 if (pointer_size == PointerSize::k64) {
366 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
367 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700368 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700369 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
370 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700371 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800372 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700373 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800374}
375
Andreas Gampe6039e562016-04-05 18:18:43 -0700376// Special managed code cut-out to allow constructor lookup in a un-started runtime.
377void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
378 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
379 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
380 if (klass == nullptr) {
381 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
382 return;
383 }
384 mirror::ObjectArray<mirror::Class>* args =
385 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700386 Runtime* runtime = Runtime::Current();
387 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700388 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700389 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700390 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700391 if (pointer_size == PointerSize::k64) {
392 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
393 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700394 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700395 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
396 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700397 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700398 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700399 if (pointer_size == PointerSize::k64) {
400 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
401 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700402 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700403 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
404 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700405 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700406 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700407 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700408}
409
Andreas Gampeae78c262017-02-01 20:40:44 -0800410void UnstartedRuntime::UnstartedClassGetDeclaringClass(
411 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
412 StackHandleScope<1> hs(self);
413 Handle<mirror::Class> klass(hs.NewHandle(
414 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
415 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
416 result->SetL(nullptr);
417 return;
418 }
419 // Return null for anonymous classes.
420 JValue is_anon_result;
421 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
422 if (is_anon_result.GetZ() != 0) {
423 result->SetL(nullptr);
424 return;
425 }
426 result->SetL(annotations::GetDeclaringClass(klass));
427}
428
Andreas Gampe633750c2016-02-19 10:49:50 -0800429void UnstartedRuntime::UnstartedClassGetEnclosingClass(
430 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
431 StackHandleScope<1> hs(self);
432 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
433 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
434 result->SetL(nullptr);
435 }
David Sehr9323e6e2016-09-13 08:58:35 -0700436 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800437}
438
Andreas Gampe715fdc22016-04-18 17:07:30 -0700439void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
440 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
441 StackHandleScope<1> hs(self);
442 Handle<mirror::Class> klass(hs.NewHandle(
443 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
444 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
445 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
446}
447
Andreas Gampe9486a162017-02-16 15:17:47 -0800448void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
449 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
450 StackHandleScope<1> hs(self);
451 Handle<mirror::Class> klass(hs.NewHandle(
452 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
453
454 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
455 result->SetL(nullptr);
456 return;
457 }
458
459 result->SetL(annotations::GetSignatureAnnotationForClass(klass));
460}
461
Andreas Gampeae78c262017-02-01 20:40:44 -0800462void UnstartedRuntime::UnstartedClassIsAnonymousClass(
463 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
464 StackHandleScope<1> hs(self);
465 Handle<mirror::Class> klass(hs.NewHandle(
466 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
467 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
468 result->SetZ(false);
469 return;
470 }
471 mirror::String* class_name = nullptr;
472 if (!annotations::GetInnerClass(klass, &class_name)) {
473 result->SetZ(false);
474 return;
475 }
476 result->SetZ(class_name == nullptr);
477}
478
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700479static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
480 const char* entry_name,
481 size_t* size,
482 std::string* error_msg) {
483 CHECK(size != nullptr);
484
485 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
486 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800487 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700488 }
489 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
490 if (zip_entry == nullptr) {
491 return nullptr;
492 }
493 std::unique_ptr<MemMap> tmp_map(
494 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
495 if (tmp_map == nullptr) {
496 return nullptr;
497 }
498
499 // OK, from here everything seems fine.
500 *size = zip_entry->GetUncompressedLength();
501 return tmp_map;
502}
503
504static void GetResourceAsStream(Thread* self,
505 ShadowFrame* shadow_frame,
506 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700507 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700508 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
509 if (resource_obj == nullptr) {
510 AbortTransactionOrFail(self, "null name for getResourceAsStream");
511 return;
512 }
513 CHECK(resource_obj->IsString());
514 mirror::String* resource_name = resource_obj->AsString();
515
516 std::string resource_name_str = resource_name->ToModifiedUtf8();
517 if (resource_name_str.empty() || resource_name_str == "/") {
518 AbortTransactionOrFail(self,
519 "Unsupported name %s for getResourceAsStream",
520 resource_name_str.c_str());
521 return;
522 }
523 const char* resource_cstr = resource_name_str.c_str();
524 if (resource_cstr[0] == '/') {
525 resource_cstr++;
526 }
527
528 Runtime* runtime = Runtime::Current();
529
530 std::vector<std::string> split;
531 Split(runtime->GetBootClassPathString(), ':', &split);
532 if (split.empty()) {
533 AbortTransactionOrFail(self,
534 "Boot classpath not set or split error:: %s",
535 runtime->GetBootClassPathString().c_str());
536 return;
537 }
538
539 std::unique_ptr<MemMap> mem_map;
540 size_t map_size;
541 std::string last_error_msg; // Only store the last message (we could concatenate).
542
543 for (const std::string& jar_file : split) {
544 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
545 if (mem_map != nullptr) {
546 break;
547 }
548 }
549
550 if (mem_map == nullptr) {
551 // Didn't find it. There's a good chance this will be the same at runtime, but still
552 // conservatively abort the transaction here.
553 AbortTransactionOrFail(self,
554 "Could not find resource %s. Last error was %s.",
555 resource_name_str.c_str(),
556 last_error_msg.c_str());
557 return;
558 }
559
560 StackHandleScope<3> hs(self);
561
562 // Create byte array for content.
563 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800564 if (h_array == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700565 AbortTransactionOrFail(self, "Could not find/create byte array class");
566 return;
567 }
568 // Copy in content.
569 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
570 // Be proactive releasing memory.
571 mem_map.release();
572
573 // Create a ByteArrayInputStream.
574 Handle<mirror::Class> h_class(hs.NewHandle(
575 runtime->GetClassLinker()->FindClass(self,
576 "Ljava/io/ByteArrayInputStream;",
577 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800578 if (h_class == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700579 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
580 return;
581 }
582 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
583 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
584 return;
585 }
586
587 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800588 if (h_obj == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700589 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
590 return;
591 }
592
593 auto* cl = Runtime::Current()->GetClassLinker();
594 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
595 "<init>", "([B)V", cl->GetImagePointerSize());
596 if (constructor == nullptr) {
597 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
598 return;
599 }
600
601 uint32_t args[1];
602 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
603 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
604
605 if (self->IsExceptionPending()) {
606 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
607 return;
608 }
609
610 result->SetL(h_obj.Get());
611}
612
613void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
614 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
615 {
616 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
617 CHECK(this_obj != nullptr);
618 CHECK(this_obj->IsClassLoader());
619
620 StackHandleScope<1> hs(self);
621 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
622
623 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
624 this_classloader_class.Get()) {
625 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700626 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700627 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700628 return;
629 }
630 }
631
632 GetResourceAsStream(self, shadow_frame, result, arg_offset);
633}
634
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800635void UnstartedRuntime::UnstartedConstructorNewInstance0(
636 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
637 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
638 StackHandleScope<4> hs(self);
639 Handle<mirror::Constructor> m = hs.NewHandle(
640 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
641 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
642 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
643 shadow_frame->GetVRegReference(arg_offset + 1)));
644 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
645 if (UNLIKELY(c->IsAbstract())) {
646 AbortTransactionOrFail(self, "Cannot handle abstract classes");
647 return;
648 }
649 // Verify that we can access the class.
650 if (!m->IsAccessible() && !c->IsPublic()) {
651 // Go 2 frames back, this method is always called from newInstance0, which is called from
652 // Constructor.newInstance(Object... args).
653 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
654 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
655 // access checks anyways. TODO: Investigate if this the correct behavior.
656 if (caller != nullptr && !caller->CanAccess(c.Get())) {
657 AbortTransactionOrFail(self, "Cannot access class");
658 return;
659 }
660 }
661 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
662 DCHECK(self->IsExceptionPending());
663 return;
664 }
665 if (c->IsClassClass()) {
666 AbortTransactionOrFail(self, "new Class() is not supported");
667 return;
668 }
669
670 // String constructor is replaced by a StringFactory method in InvokeMethod.
671 if (c->IsStringClass()) {
672 // We don't support strings.
673 AbortTransactionOrFail(self, "String construction is not supported");
674 return;
675 }
676
677 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
678 if (receiver == nullptr) {
679 AbortTransactionOrFail(self, "Could not allocate");
680 return;
681 }
682
683 // It's easier to use reflection to make the call, than create the uint32_t array.
684 {
685 ScopedObjectAccessUnchecked soa(self);
686 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
687 soa.AddLocalReference<jobject>(m.Get()));
688 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
689 soa.AddLocalReference<jobject>(receiver.Get()));
690 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
691 soa.AddLocalReference<jobject>(args.Get()));
692 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
693 }
694 if (self->IsExceptionPending()) {
695 AbortTransactionOrFail(self, "Failed running constructor");
696 } else {
697 result->SetL(receiver.Get());
698 }
699}
700
Andreas Gampe799681b2015-05-15 19:24:12 -0700701void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700702 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700703 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
704 mirror::ClassLoader* class_loader =
705 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
706 StackHandleScope<2> hs(self);
707 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
708 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
709 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
710 "VMClassLoader.findLoadedClass", false, false);
711 // This might have an error pending. But semantics are to just return null.
712 if (self->IsExceptionPending()) {
713 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700714 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700715 if (type != "java.lang.InternalError") {
716 self->ClearException();
717 }
718 }
719}
720
Mathieu Chartiere401d142015-04-22 13:56:20 -0700721void UnstartedRuntime::UnstartedVoidLookupType(
722 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
723 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700724 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
725}
726
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700727// Arraycopy emulation.
728// Note: we can't use any fast copy functions, as they are not available under transaction.
729
730template <typename T>
731static void PrimitiveArrayCopy(Thread* self,
732 mirror::Array* src_array, int32_t src_pos,
733 mirror::Array* dst_array, int32_t dst_pos,
734 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700735 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700736 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700737 AbortTransactionOrFail(self,
738 "Types mismatched in arraycopy: %s vs %s.",
739 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700740 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700741 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700742 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700743 return;
744 }
745 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
746 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
747 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
748 if (copy_forward) {
749 for (int32_t i = 0; i < length; ++i) {
750 dst->Set(dst_pos + i, src->Get(src_pos + i));
751 }
752 } else {
753 for (int32_t i = 1; i <= length; ++i) {
754 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
755 }
756 }
757}
758
Andreas Gampe799681b2015-05-15 19:24:12 -0700759void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700760 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700761 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700762 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
763 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700764 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700765
Andreas Gampe85a098a2016-03-31 13:30:53 -0700766 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
767 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
768 // Null checking. For simplicity, abort transaction.
769 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700770 AbortTransactionOrFail(self, "src is null in arraycopy.");
771 return;
772 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700773 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700774 AbortTransactionOrFail(self, "dst is null in arraycopy.");
775 return;
776 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700777 // Test for arrayness. Throw ArrayStoreException.
778 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
779 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
780 return;
781 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700782
Andreas Gampe85a098a2016-03-31 13:30:53 -0700783 mirror::Array* src_array = src_obj->AsArray();
784 mirror::Array* dst_array = dst_obj->AsArray();
785
786 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700787 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
788 UNLIKELY(src_pos > src_array->GetLength() - length) ||
789 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700790 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700791 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
792 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
793 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700794 return;
795 }
796
797 // Type checking.
798 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
799 GetComponentType();
800
801 if (!src_type->IsPrimitive()) {
802 // Check that the second type is not primitive.
803 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
804 GetComponentType();
805 if (trg_type->IsPrimitiveInt()) {
806 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700807 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700808 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700809 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700810 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700811 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700812 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700813
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700814 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
815 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
816 if (src == dst) {
817 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700818 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700819 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
820 if (copy_forward) {
821 for (int32_t i = 0; i < length; ++i) {
822 dst->Set(dst_pos + i, src->Get(src_pos + i));
823 }
824 } else {
825 for (int32_t i = 1; i <= length; ++i) {
826 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
827 }
828 }
829 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700830 // We're being lazy here. Optimally this could be a memcpy (if component types are
831 // assignable), but the ObjectArray implementation doesn't support transactions. The
832 // checking version, however, does.
833 if (Runtime::Current()->IsActiveTransaction()) {
834 dst->AssignableCheckingMemcpy<true>(
835 dst_pos, src, src_pos, length, true /* throw_exception */);
836 } else {
837 dst->AssignableCheckingMemcpy<false>(
838 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700839 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700840 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700841 } else if (src_type->IsPrimitiveByte()) {
842 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700843 } else if (src_type->IsPrimitiveChar()) {
844 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
845 } else if (src_type->IsPrimitiveInt()) {
846 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700847 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700848 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700849 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700850 }
851}
852
Andreas Gampe5c9af612016-04-05 14:16:10 -0700853void UnstartedRuntime::UnstartedSystemArraycopyByte(
854 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
855 // Just forward.
856 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
857}
858
Andreas Gampe799681b2015-05-15 19:24:12 -0700859void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700860 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700861 // Just forward.
862 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
863}
864
865void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700866 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700867 // Just forward.
868 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
869}
870
Narayan Kamath34a316f2016-03-30 13:11:18 +0100871void UnstartedRuntime::UnstartedSystemGetSecurityManager(
872 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
873 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
874 result->SetL(nullptr);
875}
876
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700877static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
878
879static void GetSystemProperty(Thread* self,
880 ShadowFrame* shadow_frame,
881 JValue* result,
882 size_t arg_offset,
883 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700884 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700885 StackHandleScope<4> hs(self);
886 Handle<mirror::String> h_key(
887 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800888 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700889 AbortTransactionOrFail(self, "getProperty key was null");
890 return;
891 }
892
893 // This is overall inefficient, but reflecting the values here is not great, either. So
894 // for simplicity, and with the assumption that the number of getProperty calls is not
895 // too great, just iterate each time.
896
897 // Get the storage class.
898 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
899 Handle<mirror::Class> h_props_class(hs.NewHandle(
900 class_linker->FindClass(self,
901 "Ljava/lang/AndroidHardcodedSystemProperties;",
902 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800903 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700904 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
905 return;
906 }
907 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
908 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
909 return;
910 }
911
912 // Get the storage array.
913 ArtField* static_properties =
914 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
915 "[[Ljava/lang/String;");
916 if (static_properties == nullptr) {
917 AbortTransactionOrFail(self,
918 "Could not find %s field",
919 kAndroidHardcodedSystemPropertiesFieldName);
920 return;
921 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700922 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
923 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
924 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800925 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700926 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
927 return;
928 }
929
930 // Iterate over it.
931 const int32_t prop_count = h_2string_array->GetLength();
932 // Use the third handle as mutable.
933 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
934 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
935 for (int32_t i = 0; i < prop_count; ++i) {
936 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800937 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700938 h_string_array->GetLength() != 2 ||
939 h_string_array->Get(0) == nullptr) {
940 AbortTransactionOrFail(self,
941 "Unexpected content of %s",
942 kAndroidHardcodedSystemPropertiesFieldName);
943 return;
944 }
945 if (h_key->Equals(h_string_array->Get(0))) {
946 // Found a value.
947 if (h_string_array->Get(1) == nullptr && is_default_version) {
948 // Null is being delegated to the default map, and then resolved to the given default value.
949 // As there's no default map, return the given value.
950 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
951 } else {
952 result->SetL(h_string_array->Get(1));
953 }
954 return;
955 }
956 }
957
958 // Key is not supported.
959 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
960}
961
962void UnstartedRuntime::UnstartedSystemGetProperty(
963 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
964 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
965}
966
967void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
968 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
969 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
970}
971
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800972static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
973 REQUIRES_SHARED(Locks::mutator_lock_) {
974 if (shadow_frame->GetLink() == nullptr) {
975 return "<no caller>";
976 }
977 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
978}
979
980static bool CheckCallers(ShadowFrame* shadow_frame,
981 std::initializer_list<std::string> allowed_call_stack)
982 REQUIRES_SHARED(Locks::mutator_lock_) {
983 for (const std::string& allowed_caller : allowed_call_stack) {
984 if (shadow_frame->GetLink() == nullptr) {
985 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700986 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800987
988 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
989 if (allowed_caller != found_caller) {
990 return false;
991 }
992
993 shadow_frame = shadow_frame->GetLink();
994 }
995 return true;
996}
997
998static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
999 REQUIRES_SHARED(Locks::mutator_lock_) {
1000 // Find the requested class.
1001 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1002 ObjPtr<mirror::Class> klass =
1003 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1004 if (klass == nullptr) {
1005 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1006 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001007 }
1008
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001009 StackHandleScope<2> hs(self);
1010 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1011 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001012 if (h_obj != nullptr) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001013 ArtMethod* init_method = h_class->FindDirectMethod(
1014 "<init>", "()V", class_linker->GetImagePointerSize());
1015 if (init_method == nullptr) {
1016 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1017 return nullptr;
1018 } else {
1019 JValue invoke_result;
1020 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1021 if (!self->IsExceptionPending()) {
1022 return h_obj.Get();
1023 }
1024 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1025 }
1026 }
1027 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1028 return nullptr;
1029}
1030
1031void UnstartedRuntime::UnstartedThreadLocalGet(
1032 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1033 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1034 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1035 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1036 } else {
1037 AbortTransactionOrFail(self,
1038 "ThreadLocal.get() does not support %s",
1039 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001040 }
1041}
1042
Andreas Gampebad529d2017-02-13 18:52:10 -08001043void UnstartedRuntime::UnstartedThreadCurrentThread(
1044 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1045 if (CheckCallers(shadow_frame,
1046 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1047 "java.lang.String, long)",
1048 "void java.lang.Thread.<init>()",
1049 "void java.util.logging.LogManager$Cleaner.<init>("
1050 "java.util.logging.LogManager)" })) {
1051 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1052 // Thread constructor only asks for the current thread to set up defaults and add the
1053 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1054 // these purposes.
1055 Runtime::Current()->InitThreadGroups(self);
1056 jobject main_peer =
1057 self->CreateCompileTimePeer(self->GetJniEnv(),
1058 "main",
1059 false,
1060 Runtime::Current()->GetMainThreadGroup());
1061 if (main_peer == nullptr) {
1062 AbortTransactionOrFail(self, "Failed allocating peer");
1063 return;
1064 }
1065
1066 result->SetL(self->DecodeJObject(main_peer));
1067 self->GetJniEnv()->DeleteLocalRef(main_peer);
1068 } else {
1069 AbortTransactionOrFail(self,
1070 "Thread.currentThread() does not support %s",
1071 GetImmediateCaller(shadow_frame).c_str());
1072 }
1073}
1074
1075void UnstartedRuntime::UnstartedThreadGetNativeState(
1076 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1077 if (CheckCallers(shadow_frame,
1078 { "java.lang.Thread$State java.lang.Thread.getState()",
1079 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1080 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1081 "java.lang.String, long)",
1082 "void java.lang.Thread.<init>()",
1083 "void java.util.logging.LogManager$Cleaner.<init>("
1084 "java.util.logging.LogManager)" })) {
1085 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1086 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1087 constexpr int32_t kJavaRunnable = 1;
1088 result->SetI(kJavaRunnable);
1089 } else {
1090 AbortTransactionOrFail(self,
1091 "Thread.getNativeState() does not support %s",
1092 GetImmediateCaller(shadow_frame).c_str());
1093 }
1094}
1095
Sergio Giro83261202016-04-11 20:49:20 +01001096void UnstartedRuntime::UnstartedMathCeil(
1097 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001098 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001099}
1100
1101void UnstartedRuntime::UnstartedMathFloor(
1102 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001103 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001104}
1105
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001106void UnstartedRuntime::UnstartedMathSin(
1107 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1108 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1109}
1110
1111void UnstartedRuntime::UnstartedMathCos(
1112 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1113 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1114}
1115
1116void UnstartedRuntime::UnstartedMathPow(
1117 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1118 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1119 shadow_frame->GetVRegDouble(arg_offset + 2)));
1120}
1121
Andreas Gampe799681b2015-05-15 19:24:12 -07001122void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001123 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001124 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1125 result->SetI(obj->IdentityHashCode());
1126}
1127
Andreas Gampe799681b2015-05-15 19:24:12 -07001128void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001129 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001130 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001131 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001132}
1133
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001134static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001135 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001136 const DexFile* dex_file = dex_cache->GetDexFile();
1137 if (dex_file == nullptr) {
1138 return nullptr;
1139 }
1140
1141 // Create the direct byte buffer.
1142 JNIEnv* env = self->GetJniEnv();
1143 DCHECK(env != nullptr);
1144 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001145 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
1146 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001147 DCHECK(self->IsExceptionPending());
1148 return nullptr;
1149 }
1150
1151 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001152 args[0].l = byte_buffer.get();
1153
1154 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
1155 WellKnownClasses::com_android_dex_Dex,
1156 WellKnownClasses::com_android_dex_Dex_create,
1157 args));
1158
1159 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001160}
1161
Andreas Gampe799681b2015-05-15 19:24:12 -07001162void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001163 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001164 // We will create the Dex object, but the image writer will release it before creating the
1165 // art file.
1166 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
1167 bool have_dex = false;
1168 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001169 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001170 if (dex != nullptr) {
1171 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001172 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001173 }
1174 }
1175 if (!have_dex) {
1176 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001177 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001178 }
1179}
1180
1181static void UnstartedMemoryPeek(
1182 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1183 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1184 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1185 // aborting the transaction.
1186
1187 switch (type) {
1188 case Primitive::kPrimByte: {
1189 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1190 return;
1191 }
1192
1193 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001194 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1195 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001196 return;
1197 }
1198
1199 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001200 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1201 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001202 return;
1203 }
1204
1205 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001206 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1207 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001208 return;
1209 }
1210
1211 case Primitive::kPrimBoolean:
1212 case Primitive::kPrimChar:
1213 case Primitive::kPrimFloat:
1214 case Primitive::kPrimDouble:
1215 case Primitive::kPrimVoid:
1216 case Primitive::kPrimNot:
1217 LOG(FATAL) << "Not in the Memory API: " << type;
1218 UNREACHABLE();
1219 }
1220 LOG(FATAL) << "Should not reach here";
1221 UNREACHABLE();
1222}
1223
Andreas Gampe799681b2015-05-15 19:24:12 -07001224void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001226 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1227}
1228
1229void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001231 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1232}
1233
1234void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001236 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1237}
1238
1239void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001240 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001241 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001242}
1243
1244static void UnstartedMemoryPeekArray(
1245 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001246 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001247 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1248 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1249 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001250 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001251 return;
1252 }
1253 mirror::Array* array = obj->AsArray();
1254
1255 int offset = shadow_frame->GetVReg(arg_offset + 3);
1256 int count = shadow_frame->GetVReg(arg_offset + 4);
1257 if (offset < 0 || offset + count > array->GetLength()) {
1258 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1259 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001260 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001261 return;
1262 }
1263
1264 switch (type) {
1265 case Primitive::kPrimByte: {
1266 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1267 mirror::ByteArray* byte_array = array->AsByteArray();
1268 for (int32_t i = 0; i < count; ++i, ++address) {
1269 byte_array->SetWithoutChecks<true>(i + offset, *address);
1270 }
1271 return;
1272 }
1273
1274 case Primitive::kPrimShort:
1275 case Primitive::kPrimInt:
1276 case Primitive::kPrimLong:
1277 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1278 UNREACHABLE();
1279
1280 case Primitive::kPrimBoolean:
1281 case Primitive::kPrimChar:
1282 case Primitive::kPrimFloat:
1283 case Primitive::kPrimDouble:
1284 case Primitive::kPrimVoid:
1285 case Primitive::kPrimNot:
1286 LOG(FATAL) << "Not in the Memory API: " << type;
1287 UNREACHABLE();
1288 }
1289 LOG(FATAL) << "Should not reach here";
1290 UNREACHABLE();
1291}
1292
Andreas Gampe799681b2015-05-15 19:24:12 -07001293void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001294 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001295 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001296}
1297
Kenny Root1c9e61c2015-05-14 15:58:17 -07001298// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001299void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001300 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001301 jint start = shadow_frame->GetVReg(arg_offset + 1);
1302 jint end = shadow_frame->GetVReg(arg_offset + 2);
1303 jint index = shadow_frame->GetVReg(arg_offset + 4);
1304 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1305 if (string == nullptr) {
1306 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1307 return;
1308 }
Kenny Root57f91e82015-05-14 15:58:17 -07001309 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001310 DCHECK_LE(start, end);
1311 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001312 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001313 Handle<mirror::CharArray> h_char_array(
1314 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001315 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001316 DCHECK_LE(index, h_char_array->GetLength());
1317 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001318 string->GetChars(start, end, h_char_array, index);
1319}
1320
1321// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001322void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001323 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001324 jint index = shadow_frame->GetVReg(arg_offset + 1);
1325 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1326 if (string == nullptr) {
1327 AbortTransactionOrFail(self, "String.charAt with null object");
1328 return;
1329 }
1330 result->SetC(string->CharAt(index));
1331}
1332
Vladimir Marko92907f32017-02-20 14:08:30 +00001333// This allows creating String objects with replaced characters during compilation.
1334// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1335void UnstartedRuntime::UnstartedStringDoReplace(
1336 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1337 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1338 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
1339 ObjPtr<mirror::String> string = shadow_frame->GetVRegReference(arg_offset)->AsString();
Kenny Root57f91e82015-05-14 15:58:17 -07001340 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001341 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001342 return;
1343 }
Vladimir Marko92907f32017-02-20 14:08:30 +00001344 result->SetL(string->DoReplace(self, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001345}
1346
Kenny Root1c9e61c2015-05-14 15:58:17 -07001347// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001348void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001349 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001350 jint offset = shadow_frame->GetVReg(arg_offset);
1351 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1352 DCHECK_GE(char_count, 0);
1353 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001354 Handle<mirror::CharArray> h_char_array(
1355 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001356 Runtime* runtime = Runtime::Current();
1357 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1358 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1359}
1360
1361// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001362void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001363 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001364 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1365 if (to_copy == nullptr) {
1366 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1367 return;
1368 }
1369 StackHandleScope<1> hs(self);
1370 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1371 Runtime* runtime = Runtime::Current();
1372 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1373 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1374 allocator));
1375}
1376
Andreas Gampe799681b2015-05-15 19:24:12 -07001377void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001378 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001379 jint start = shadow_frame->GetVReg(arg_offset + 1);
1380 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001381 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001382 DCHECK_GE(length, 0);
1383 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001384 Handle<mirror::String> h_string(
1385 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001386 DCHECK_LE(start, h_string->GetLength());
1387 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001388 Runtime* runtime = Runtime::Current();
1389 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1390 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1391}
1392
Kenny Root57f91e82015-05-14 15:58:17 -07001393// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001394void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001395 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001396 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001397 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1398 if (string == nullptr) {
1399 AbortTransactionOrFail(self, "String.charAt with null object");
1400 return;
1401 }
1402 result->SetL(string->ToCharArray(self));
1403}
1404
Andreas Gampebc4d2182016-02-22 10:03:12 -08001405// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1406void UnstartedRuntime::UnstartedReferenceGetReferent(
1407 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001408 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001409 shadow_frame->GetVRegReference(arg_offset));
1410 if (ref == nullptr) {
1411 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1412 return;
1413 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001414 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001415 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1416 result->SetL(referent);
1417}
1418
1419// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1420// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1421// where we can predict the behavior (somewhat).
1422// Note: this is required (instead of lazy initialization) as these classes are used in the static
1423// initialization of other classes, so will *use* the value.
1424void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1425 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001426 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001427 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1428 // 8 as a conservative upper approximation.
1429 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001430 } else if (CheckCallers(shadow_frame,
1431 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001432 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1433 // a good upper bound.
1434 // TODO: Consider resetting in the zygote?
1435 result->SetI(8);
1436 } else {
1437 // Not supported.
1438 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1439 }
1440}
1441
1442// This allows accessing ConcurrentHashMap/SynchronousQueue.
1443
1444void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1445 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1446 // Argument 0 is the Unsafe instance, skip.
1447 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1448 if (obj == nullptr) {
1449 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1450 return;
1451 }
1452 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1453 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1454 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001455 bool success;
1456 // Check whether we're in a transaction, call accordingly.
1457 if (Runtime::Current()->IsActiveTransaction()) {
1458 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1459 expectedValue,
1460 newValue);
1461 } else {
1462 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1463 expectedValue,
1464 newValue);
1465 }
1466 result->SetZ(success ? 1 : 0);
1467}
1468
1469void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1470 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1471 // Argument 0 is the Unsafe instance, skip.
1472 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1473 if (obj == nullptr) {
1474 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1475 return;
1476 }
1477 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1478 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1479 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1480
1481 // Must use non transactional mode.
1482 if (kUseReadBarrier) {
1483 // Need to make sure the reference stored in the field is a to-space one before attempting the
1484 // CAS or the CAS could fail incorrectly.
1485 mirror::HeapReference<mirror::Object>* field_addr =
1486 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1487 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001488 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001489 obj,
1490 MemberOffset(offset),
1491 field_addr);
1492 }
1493 bool success;
1494 // Check whether we're in a transaction, call accordingly.
1495 if (Runtime::Current()->IsActiveTransaction()) {
1496 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1497 expected_value,
1498 newValue);
1499 } else {
1500 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1501 expected_value,
1502 newValue);
1503 }
1504 result->SetZ(success ? 1 : 0);
1505}
1506
1507void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1508 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001509 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001510 // Argument 0 is the Unsafe instance, skip.
1511 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1512 if (obj == nullptr) {
1513 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1514 return;
1515 }
1516 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1517 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1518 result->SetL(value);
1519}
1520
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001521void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1522 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001523 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001524 // Argument 0 is the Unsafe instance, skip.
1525 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1526 if (obj == nullptr) {
1527 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1528 return;
1529 }
1530 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1531 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1532 if (Runtime::Current()->IsActiveTransaction()) {
1533 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1534 } else {
1535 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1536 }
1537}
1538
Andreas Gampebc4d2182016-02-22 10:03:12 -08001539void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1540 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001541 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001542 // Argument 0 is the Unsafe instance, skip.
1543 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1544 if (obj == nullptr) {
1545 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1546 return;
1547 }
1548 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1549 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1550 QuasiAtomic::ThreadFenceRelease();
1551 if (Runtime::Current()->IsActiveTransaction()) {
1552 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1553 } else {
1554 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1555 }
1556}
1557
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001558// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1559// of correctly handling the corner cases.
1560void UnstartedRuntime::UnstartedIntegerParseInt(
1561 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001562 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001563 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1564 if (obj == nullptr) {
1565 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1566 return;
1567 }
1568
1569 std::string string_value = obj->AsString()->ToModifiedUtf8();
1570 if (string_value.empty()) {
1571 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1572 return;
1573 }
1574
1575 const char* c_str = string_value.c_str();
1576 char *end;
1577 // Can we set errno to 0? Is this always a variable, and not a macro?
1578 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1579 int64_t l = strtol(c_str, &end, 10);
1580
1581 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1582 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1583 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1584 return;
1585 }
1586 if (l == 0) {
1587 // Check whether the string wasn't exactly zero.
1588 if (string_value != "0") {
1589 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1590 return;
1591 }
1592 } else if (*end != '\0') {
1593 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1594 return;
1595 }
1596
1597 result->SetI(static_cast<int32_t>(l));
1598}
1599
1600// A cutout for Long.parseLong.
1601//
1602// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1603// well.
1604void UnstartedRuntime::UnstartedLongParseLong(
1605 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001606 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001607 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1608 if (obj == nullptr) {
1609 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1610 return;
1611 }
1612
1613 std::string string_value = obj->AsString()->ToModifiedUtf8();
1614 if (string_value.empty()) {
1615 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1616 return;
1617 }
1618
1619 const char* c_str = string_value.c_str();
1620 char *end;
1621 // Can we set errno to 0? Is this always a variable, and not a macro?
1622 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1623 int64_t l = strtol(c_str, &end, 10);
1624
1625 // Note: comparing against int32_t min/max is intentional here.
1626 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1627 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1628 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1629 return;
1630 }
1631 if (l == 0) {
1632 // Check whether the string wasn't exactly zero.
1633 if (string_value != "0") {
1634 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1635 return;
1636 }
1637 } else if (*end != '\0') {
1638 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1639 return;
1640 }
1641
1642 result->SetJ(l);
1643}
1644
Andreas Gampe715fdc22016-04-18 17:07:30 -07001645void UnstartedRuntime::UnstartedMethodInvoke(
1646 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001647 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001648 JNIEnvExt* env = self->GetJniEnv();
1649 ScopedObjectAccessUnchecked soa(self);
1650
Mathieu Chartier8778c522016-10-04 19:06:30 -07001651 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001652 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001653 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001654
Mathieu Chartier8778c522016-10-04 19:06:30 -07001655 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001656 ScopedLocalRef<jobject> java_receiver(env,
1657 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1658
Mathieu Chartier8778c522016-10-04 19:06:30 -07001659 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001660 ScopedLocalRef<jobject> java_args(env,
1661 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1662
1663 ScopedLocalRef<jobject> result_jobj(env,
1664 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1665
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001666 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001667
1668 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1669 // InvocationTargetExceptions.
1670 if (self->IsExceptionPending()) {
1671 AbortTransactionOrFail(self, "Failed Method.invoke");
1672 }
1673}
1674
Andreas Gampebc4d2182016-02-22 10:03:12 -08001675
Mathieu Chartiere401d142015-04-22 13:56:20 -07001676void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1677 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1678 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001679 int32_t length = args[1];
1680 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001681 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001682 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001683 ObjPtr<mirror::Class> array_class =
1684 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001685 DCHECK(array_class != nullptr);
1686 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001687 result->SetL(mirror::Array::Alloc<true, true>(self,
1688 array_class,
1689 length,
1690 array_class->GetComponentSizeShift(),
1691 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001692}
1693
Mathieu Chartiere401d142015-04-22 13:56:20 -07001694void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1695 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1696 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001697 result->SetL(nullptr);
1698}
1699
Mathieu Chartiere401d142015-04-22 13:56:20 -07001700void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1701 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1702 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001703 NthCallerVisitor visitor(self, 3);
1704 visitor.WalkStack();
1705 if (visitor.caller != nullptr) {
1706 result->SetL(visitor.caller->GetDeclaringClass());
1707 }
1708}
1709
Mathieu Chartiere401d142015-04-22 13:56:20 -07001710void UnstartedRuntime::UnstartedJNIMathLog(
1711 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1712 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001713 JValue value;
1714 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1715 result->SetD(log(value.GetD()));
1716}
1717
Mathieu Chartiere401d142015-04-22 13:56:20 -07001718void UnstartedRuntime::UnstartedJNIMathExp(
1719 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1720 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001721 JValue value;
1722 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1723 result->SetD(exp(value.GetD()));
1724}
1725
Andreas Gampebc4d2182016-02-22 10:03:12 -08001726void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1727 Thread* self ATTRIBUTE_UNUSED,
1728 ArtMethod* method ATTRIBUTE_UNUSED,
1729 mirror::Object* receiver ATTRIBUTE_UNUSED,
1730 uint32_t* args ATTRIBUTE_UNUSED,
1731 JValue* result) {
1732 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1733 ? 0
1734 : 1);
1735}
1736
Mathieu Chartiere401d142015-04-22 13:56:20 -07001737void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1738 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1739 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001740 StackHandleScope<1> hs(self);
1741 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1742}
1743
Andreas Gampebc4d2182016-02-22 10:03:12 -08001744void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1745 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1746 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1747 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1748 result->SetD(bit_cast<double>(long_input));
1749}
1750
Mathieu Chartiere401d142015-04-22 13:56:20 -07001751void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1752 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1753 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001754 result->SetI(args[0]);
1755}
1756
Mathieu Chartiere401d142015-04-22 13:56:20 -07001757void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1758 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1759 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001760 result->SetI(args[0]);
1761}
1762
Mathieu Chartiere401d142015-04-22 13:56:20 -07001763void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1764 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1765 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001766 result->SetL(receiver->Clone(self));
1767}
1768
Mathieu Chartiere401d142015-04-22 13:56:20 -07001769void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1770 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1771 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001772 receiver->NotifyAll(self);
1773}
1774
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775void UnstartedRuntime::UnstartedJNIStringCompareTo(
1776 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1777 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001778 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1779 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001780 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001781 }
1782 result->SetI(receiver->AsString()->CompareTo(rhs));
1783}
1784
Mathieu Chartiere401d142015-04-22 13:56:20 -07001785void UnstartedRuntime::UnstartedJNIStringIntern(
1786 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1787 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001788 result->SetL(receiver->AsString()->Intern());
1789}
1790
Mathieu Chartiere401d142015-04-22 13:56:20 -07001791void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1792 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1793 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001794 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1795}
1796
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1798 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1799 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001800 StackHandleScope<2> hs(self);
1801 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1802 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1803 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1804}
1805
Mathieu Chartiere401d142015-04-22 13:56:20 -07001806void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1807 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1808 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001809 int32_t length = static_cast<int32_t>(args[1]);
1810 if (length < 0) {
1811 ThrowNegativeArraySizeException(length);
1812 return;
1813 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001814 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001815 Runtime* runtime = Runtime::Current();
1816 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001817 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001818 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001819 CHECK(self->IsExceptionPending());
1820 return;
1821 }
1822 DCHECK(array_class->IsObjectArrayClass());
1823 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1824 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1825 result->SetL(new_array);
1826}
1827
Mathieu Chartiere401d142015-04-22 13:56:20 -07001828void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1829 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1830 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001831 ScopedObjectAccessUnchecked soa(self);
1832 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001833 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001834 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001835 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001836 }
1837}
1838
Mathieu Chartiere401d142015-04-22 13:56:20 -07001839void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1840 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1841 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001842 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1843 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1844}
1845
Mathieu Chartiere401d142015-04-22 13:56:20 -07001846void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1847 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1848 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001849 result->SetZ(JNI_TRUE);
1850}
1851
Mathieu Chartiere401d142015-04-22 13:56:20 -07001852void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1853 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1854 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001855 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1856 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1857 jint expectedValue = args[3];
1858 jint newValue = args[4];
1859 bool success;
1860 if (Runtime::Current()->IsActiveTransaction()) {
1861 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1862 expectedValue, newValue);
1863 } else {
1864 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1865 expectedValue, newValue);
1866 }
1867 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1868}
1869
Narayan Kamath34a316f2016-03-30 13:11:18 +01001870void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1871 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1872 uint32_t* args, JValue* result) {
1873 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1874 if (obj == nullptr) {
1875 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1876 return;
1877 }
1878
1879 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1880 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1881}
1882
Mathieu Chartiere401d142015-04-22 13:56:20 -07001883void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1884 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1885 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001886 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1887 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1888 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1889 if (Runtime::Current()->IsActiveTransaction()) {
1890 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1891 } else {
1892 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1893 }
1894}
1895
Andreas Gampe799681b2015-05-15 19:24:12 -07001896void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001897 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1898 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001899 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1900 Primitive::Type primitive_type = component->GetPrimitiveType();
1901 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1902}
1903
Andreas Gampe799681b2015-05-15 19:24:12 -07001904void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1906 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001907 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1908 Primitive::Type primitive_type = component->GetPrimitiveType();
1909 result->SetI(Primitive::ComponentSize(primitive_type));
1910}
1911
Andreas Gampedd9d0552015-03-09 12:57:41 -07001912typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001913 size_t arg_size);
1914
Mathieu Chartiere401d142015-04-22 13:56:20 -07001915typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001916 uint32_t* args, JValue* result);
1917
1918static bool tables_initialized_ = false;
1919static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1920static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1921
Andreas Gampe799681b2015-05-15 19:24:12 -07001922void UnstartedRuntime::InitializeInvokeHandlers() {
1923#define UNSTARTED_DIRECT(ShortName, Sig) \
1924 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1925#include "unstarted_runtime_list.h"
1926 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1927#undef UNSTARTED_RUNTIME_DIRECT_LIST
1928#undef UNSTARTED_RUNTIME_JNI_LIST
1929#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001930}
1931
Andreas Gampe799681b2015-05-15 19:24:12 -07001932void UnstartedRuntime::InitializeJNIHandlers() {
1933#define UNSTARTED_JNI(ShortName, Sig) \
1934 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1935#include "unstarted_runtime_list.h"
1936 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1937#undef UNSTARTED_RUNTIME_DIRECT_LIST
1938#undef UNSTARTED_RUNTIME_JNI_LIST
1939#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001940}
1941
Andreas Gampe799681b2015-05-15 19:24:12 -07001942void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001943 CHECK(!tables_initialized_);
1944
Andreas Gampe799681b2015-05-15 19:24:12 -07001945 InitializeInvokeHandlers();
1946 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001947
1948 tables_initialized_ = true;
1949}
1950
Andreas Gampe799681b2015-05-15 19:24:12 -07001951void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1952 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001953 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1954 // problems in core libraries.
1955 CHECK(tables_initialized_);
1956
David Sehr709b0702016-10-13 09:12:37 -07001957 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001958 const auto& iter = invoke_handlers_.find(name);
1959 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001960 // Clear out the result in case it's not zeroed out.
1961 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001962
1963 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1964 self->PushShadowFrame(shadow_frame);
1965
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001966 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001967
1968 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001969 } else {
1970 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001971 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001972 }
1973}
1974
1975// 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 -07001976void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001977 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001978 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001979 const auto& iter = jni_handlers_.find(name);
1980 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001981 // Clear out the result in case it's not zeroed out.
1982 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001983 (*iter->second)(self, method, receiver, args, result);
1984 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001985 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1986 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001987 } else {
David Sehr709b0702016-10-13 09:12:37 -07001988 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001989 "non-transactional runtime";
1990 }
1991}
1992
1993} // namespace interpreter
1994} // namespace art