blob: 4a321e6d273c90ea90d23558ebd56fd56c758ee1 [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 Gampe799681b2015-05-15 19:24:12 -0700635void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700637 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
638 mirror::ClassLoader* class_loader =
639 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
640 StackHandleScope<2> hs(self);
641 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
642 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
643 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
644 "VMClassLoader.findLoadedClass", false, false);
645 // This might have an error pending. But semantics are to just return null.
646 if (self->IsExceptionPending()) {
647 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700648 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700649 if (type != "java.lang.InternalError") {
650 self->ClearException();
651 }
652 }
653}
654
Mathieu Chartiere401d142015-04-22 13:56:20 -0700655void UnstartedRuntime::UnstartedVoidLookupType(
656 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
657 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700658 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
659}
660
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700661// Arraycopy emulation.
662// Note: we can't use any fast copy functions, as they are not available under transaction.
663
664template <typename T>
665static void PrimitiveArrayCopy(Thread* self,
666 mirror::Array* src_array, int32_t src_pos,
667 mirror::Array* dst_array, int32_t dst_pos,
668 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700669 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700670 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700671 AbortTransactionOrFail(self,
672 "Types mismatched in arraycopy: %s vs %s.",
673 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700674 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700675 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700676 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700677 return;
678 }
679 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
680 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
681 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
682 if (copy_forward) {
683 for (int32_t i = 0; i < length; ++i) {
684 dst->Set(dst_pos + i, src->Get(src_pos + i));
685 }
686 } else {
687 for (int32_t i = 1; i <= length; ++i) {
688 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
689 }
690 }
691}
692
Andreas Gampe799681b2015-05-15 19:24:12 -0700693void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700694 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700695 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700696 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
697 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700698 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700699
Andreas Gampe85a098a2016-03-31 13:30:53 -0700700 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
701 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
702 // Null checking. For simplicity, abort transaction.
703 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700704 AbortTransactionOrFail(self, "src is null in arraycopy.");
705 return;
706 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700707 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700708 AbortTransactionOrFail(self, "dst is null in arraycopy.");
709 return;
710 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700711 // Test for arrayness. Throw ArrayStoreException.
712 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
713 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
714 return;
715 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700716
Andreas Gampe85a098a2016-03-31 13:30:53 -0700717 mirror::Array* src_array = src_obj->AsArray();
718 mirror::Array* dst_array = dst_obj->AsArray();
719
720 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700721 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
722 UNLIKELY(src_pos > src_array->GetLength() - length) ||
723 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700724 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700725 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
726 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
727 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700728 return;
729 }
730
731 // Type checking.
732 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
733 GetComponentType();
734
735 if (!src_type->IsPrimitive()) {
736 // Check that the second type is not primitive.
737 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
738 GetComponentType();
739 if (trg_type->IsPrimitiveInt()) {
740 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700741 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700742 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700743 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700744 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700745 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700746 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700747
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700748 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
749 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
750 if (src == dst) {
751 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700752 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700753 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
754 if (copy_forward) {
755 for (int32_t i = 0; i < length; ++i) {
756 dst->Set(dst_pos + i, src->Get(src_pos + i));
757 }
758 } else {
759 for (int32_t i = 1; i <= length; ++i) {
760 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
761 }
762 }
763 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700764 // We're being lazy here. Optimally this could be a memcpy (if component types are
765 // assignable), but the ObjectArray implementation doesn't support transactions. The
766 // checking version, however, does.
767 if (Runtime::Current()->IsActiveTransaction()) {
768 dst->AssignableCheckingMemcpy<true>(
769 dst_pos, src, src_pos, length, true /* throw_exception */);
770 } else {
771 dst->AssignableCheckingMemcpy<false>(
772 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700773 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700774 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700775 } else if (src_type->IsPrimitiveByte()) {
776 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700777 } else if (src_type->IsPrimitiveChar()) {
778 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
779 } else if (src_type->IsPrimitiveInt()) {
780 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700781 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700782 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700783 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700784 }
785}
786
Andreas Gampe5c9af612016-04-05 14:16:10 -0700787void UnstartedRuntime::UnstartedSystemArraycopyByte(
788 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
789 // Just forward.
790 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
791}
792
Andreas Gampe799681b2015-05-15 19:24:12 -0700793void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700794 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700795 // Just forward.
796 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
797}
798
799void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700801 // Just forward.
802 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
803}
804
Narayan Kamath34a316f2016-03-30 13:11:18 +0100805void UnstartedRuntime::UnstartedSystemGetSecurityManager(
806 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
807 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
808 result->SetL(nullptr);
809}
810
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700811static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
812
813static void GetSystemProperty(Thread* self,
814 ShadowFrame* shadow_frame,
815 JValue* result,
816 size_t arg_offset,
817 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700818 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700819 StackHandleScope<4> hs(self);
820 Handle<mirror::String> h_key(
821 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800822 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700823 AbortTransactionOrFail(self, "getProperty key was null");
824 return;
825 }
826
827 // This is overall inefficient, but reflecting the values here is not great, either. So
828 // for simplicity, and with the assumption that the number of getProperty calls is not
829 // too great, just iterate each time.
830
831 // Get the storage class.
832 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
833 Handle<mirror::Class> h_props_class(hs.NewHandle(
834 class_linker->FindClass(self,
835 "Ljava/lang/AndroidHardcodedSystemProperties;",
836 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800837 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700838 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
839 return;
840 }
841 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
842 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
843 return;
844 }
845
846 // Get the storage array.
847 ArtField* static_properties =
848 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
849 "[[Ljava/lang/String;");
850 if (static_properties == nullptr) {
851 AbortTransactionOrFail(self,
852 "Could not find %s field",
853 kAndroidHardcodedSystemPropertiesFieldName);
854 return;
855 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700856 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
857 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
858 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800859 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700860 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
861 return;
862 }
863
864 // Iterate over it.
865 const int32_t prop_count = h_2string_array->GetLength();
866 // Use the third handle as mutable.
867 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
868 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
869 for (int32_t i = 0; i < prop_count; ++i) {
870 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800871 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700872 h_string_array->GetLength() != 2 ||
873 h_string_array->Get(0) == nullptr) {
874 AbortTransactionOrFail(self,
875 "Unexpected content of %s",
876 kAndroidHardcodedSystemPropertiesFieldName);
877 return;
878 }
879 if (h_key->Equals(h_string_array->Get(0))) {
880 // Found a value.
881 if (h_string_array->Get(1) == nullptr && is_default_version) {
882 // Null is being delegated to the default map, and then resolved to the given default value.
883 // As there's no default map, return the given value.
884 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
885 } else {
886 result->SetL(h_string_array->Get(1));
887 }
888 return;
889 }
890 }
891
892 // Key is not supported.
893 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
894}
895
896void UnstartedRuntime::UnstartedSystemGetProperty(
897 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
898 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
899}
900
901void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
902 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
903 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
904}
905
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800906static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
907 REQUIRES_SHARED(Locks::mutator_lock_) {
908 if (shadow_frame->GetLink() == nullptr) {
909 return "<no caller>";
910 }
911 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
912}
913
914static bool CheckCallers(ShadowFrame* shadow_frame,
915 std::initializer_list<std::string> allowed_call_stack)
916 REQUIRES_SHARED(Locks::mutator_lock_) {
917 for (const std::string& allowed_caller : allowed_call_stack) {
918 if (shadow_frame->GetLink() == nullptr) {
919 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700920 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800921
922 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
923 if (allowed_caller != found_caller) {
924 return false;
925 }
926
927 shadow_frame = shadow_frame->GetLink();
928 }
929 return true;
930}
931
932static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
933 REQUIRES_SHARED(Locks::mutator_lock_) {
934 // Find the requested class.
935 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
936 ObjPtr<mirror::Class> klass =
937 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
938 if (klass == nullptr) {
939 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
940 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700941 }
942
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800943 StackHandleScope<2> hs(self);
944 Handle<mirror::Class> h_class(hs.NewHandle(klass));
945 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800946 if (h_obj != nullptr) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800947 ArtMethod* init_method = h_class->FindDirectMethod(
948 "<init>", "()V", class_linker->GetImagePointerSize());
949 if (init_method == nullptr) {
950 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
951 return nullptr;
952 } else {
953 JValue invoke_result;
954 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
955 if (!self->IsExceptionPending()) {
956 return h_obj.Get();
957 }
958 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
959 }
960 }
961 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
962 return nullptr;
963}
964
965void UnstartedRuntime::UnstartedThreadLocalGet(
966 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
967 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
968 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
969 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
970 } else {
971 AbortTransactionOrFail(self,
972 "ThreadLocal.get() does not support %s",
973 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700974 }
975}
976
Andreas Gampebad529d2017-02-13 18:52:10 -0800977void UnstartedRuntime::UnstartedThreadCurrentThread(
978 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
979 if (CheckCallers(shadow_frame,
980 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
981 "java.lang.String, long)",
982 "void java.lang.Thread.<init>()",
983 "void java.util.logging.LogManager$Cleaner.<init>("
984 "java.util.logging.LogManager)" })) {
985 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
986 // Thread constructor only asks for the current thread to set up defaults and add the
987 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
988 // these purposes.
989 Runtime::Current()->InitThreadGroups(self);
990 jobject main_peer =
991 self->CreateCompileTimePeer(self->GetJniEnv(),
992 "main",
993 false,
994 Runtime::Current()->GetMainThreadGroup());
995 if (main_peer == nullptr) {
996 AbortTransactionOrFail(self, "Failed allocating peer");
997 return;
998 }
999
1000 result->SetL(self->DecodeJObject(main_peer));
1001 self->GetJniEnv()->DeleteLocalRef(main_peer);
1002 } else {
1003 AbortTransactionOrFail(self,
1004 "Thread.currentThread() does not support %s",
1005 GetImmediateCaller(shadow_frame).c_str());
1006 }
1007}
1008
1009void UnstartedRuntime::UnstartedThreadGetNativeState(
1010 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1011 if (CheckCallers(shadow_frame,
1012 { "java.lang.Thread$State java.lang.Thread.getState()",
1013 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1014 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1015 "java.lang.String, long)",
1016 "void java.lang.Thread.<init>()",
1017 "void java.util.logging.LogManager$Cleaner.<init>("
1018 "java.util.logging.LogManager)" })) {
1019 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1020 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1021 constexpr int32_t kJavaRunnable = 1;
1022 result->SetI(kJavaRunnable);
1023 } else {
1024 AbortTransactionOrFail(self,
1025 "Thread.getNativeState() does not support %s",
1026 GetImmediateCaller(shadow_frame).c_str());
1027 }
1028}
1029
Sergio Giro83261202016-04-11 20:49:20 +01001030void UnstartedRuntime::UnstartedMathCeil(
1031 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001032 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001033}
1034
1035void UnstartedRuntime::UnstartedMathFloor(
1036 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001037 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001038}
1039
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001040void UnstartedRuntime::UnstartedMathSin(
1041 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1042 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1043}
1044
1045void UnstartedRuntime::UnstartedMathCos(
1046 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1047 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1048}
1049
1050void UnstartedRuntime::UnstartedMathPow(
1051 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1052 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1053 shadow_frame->GetVRegDouble(arg_offset + 2)));
1054}
1055
Andreas Gampe799681b2015-05-15 19:24:12 -07001056void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001058 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1059 result->SetI(obj->IdentityHashCode());
1060}
1061
Andreas Gampe799681b2015-05-15 19:24:12 -07001062void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001063 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001064 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001065 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001066}
1067
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001068static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001069 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001070 const DexFile* dex_file = dex_cache->GetDexFile();
1071 if (dex_file == nullptr) {
1072 return nullptr;
1073 }
1074
1075 // Create the direct byte buffer.
1076 JNIEnv* env = self->GetJniEnv();
1077 DCHECK(env != nullptr);
1078 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001079 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
1080 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001081 DCHECK(self->IsExceptionPending());
1082 return nullptr;
1083 }
1084
1085 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001086 args[0].l = byte_buffer.get();
1087
1088 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
1089 WellKnownClasses::com_android_dex_Dex,
1090 WellKnownClasses::com_android_dex_Dex_create,
1091 args));
1092
1093 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001094}
1095
Andreas Gampe799681b2015-05-15 19:24:12 -07001096void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001097 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001098 // We will create the Dex object, but the image writer will release it before creating the
1099 // art file.
1100 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
1101 bool have_dex = false;
1102 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001103 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001104 if (dex != nullptr) {
1105 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001106 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001107 }
1108 }
1109 if (!have_dex) {
1110 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001111 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001112 }
1113}
1114
1115static void UnstartedMemoryPeek(
1116 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1117 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1118 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1119 // aborting the transaction.
1120
1121 switch (type) {
1122 case Primitive::kPrimByte: {
1123 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1124 return;
1125 }
1126
1127 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001128 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1129 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001130 return;
1131 }
1132
1133 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001134 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1135 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001136 return;
1137 }
1138
1139 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001140 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1141 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001142 return;
1143 }
1144
1145 case Primitive::kPrimBoolean:
1146 case Primitive::kPrimChar:
1147 case Primitive::kPrimFloat:
1148 case Primitive::kPrimDouble:
1149 case Primitive::kPrimVoid:
1150 case Primitive::kPrimNot:
1151 LOG(FATAL) << "Not in the Memory API: " << type;
1152 UNREACHABLE();
1153 }
1154 LOG(FATAL) << "Should not reach here";
1155 UNREACHABLE();
1156}
1157
Andreas Gampe799681b2015-05-15 19:24:12 -07001158void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001159 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001160 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1161}
1162
1163void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001164 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001165 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1166}
1167
1168void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001169 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001170 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1171}
1172
1173void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001174 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001175 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001176}
1177
1178static void UnstartedMemoryPeekArray(
1179 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001180 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001181 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1182 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1183 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001184 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001185 return;
1186 }
1187 mirror::Array* array = obj->AsArray();
1188
1189 int offset = shadow_frame->GetVReg(arg_offset + 3);
1190 int count = shadow_frame->GetVReg(arg_offset + 4);
1191 if (offset < 0 || offset + count > array->GetLength()) {
1192 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1193 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001194 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001195 return;
1196 }
1197
1198 switch (type) {
1199 case Primitive::kPrimByte: {
1200 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1201 mirror::ByteArray* byte_array = array->AsByteArray();
1202 for (int32_t i = 0; i < count; ++i, ++address) {
1203 byte_array->SetWithoutChecks<true>(i + offset, *address);
1204 }
1205 return;
1206 }
1207
1208 case Primitive::kPrimShort:
1209 case Primitive::kPrimInt:
1210 case Primitive::kPrimLong:
1211 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1212 UNREACHABLE();
1213
1214 case Primitive::kPrimBoolean:
1215 case Primitive::kPrimChar:
1216 case Primitive::kPrimFloat:
1217 case Primitive::kPrimDouble:
1218 case Primitive::kPrimVoid:
1219 case Primitive::kPrimNot:
1220 LOG(FATAL) << "Not in the Memory API: " << type;
1221 UNREACHABLE();
1222 }
1223 LOG(FATAL) << "Should not reach here";
1224 UNREACHABLE();
1225}
1226
Andreas Gampe799681b2015-05-15 19:24:12 -07001227void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001228 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001229 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001230}
1231
Kenny Root1c9e61c2015-05-14 15:58:17 -07001232// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001233void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001234 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001235 jint start = shadow_frame->GetVReg(arg_offset + 1);
1236 jint end = shadow_frame->GetVReg(arg_offset + 2);
1237 jint index = shadow_frame->GetVReg(arg_offset + 4);
1238 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1239 if (string == nullptr) {
1240 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1241 return;
1242 }
Kenny Root57f91e82015-05-14 15:58:17 -07001243 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001244 DCHECK_LE(start, end);
1245 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001246 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001247 Handle<mirror::CharArray> h_char_array(
1248 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001249 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001250 DCHECK_LE(index, h_char_array->GetLength());
1251 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001252 string->GetChars(start, end, h_char_array, index);
1253}
1254
1255// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001256void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001257 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001258 jint index = shadow_frame->GetVReg(arg_offset + 1);
1259 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1260 if (string == nullptr) {
1261 AbortTransactionOrFail(self, "String.charAt with null object");
1262 return;
1263 }
1264 result->SetC(string->CharAt(index));
1265}
1266
Kenny Root57f91e82015-05-14 15:58:17 -07001267// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001268void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001269 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001270 jint index = shadow_frame->GetVReg(arg_offset + 1);
1271 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1272 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1273 if (string == nullptr) {
1274 AbortTransactionOrFail(self, "String.setCharAt with null object");
1275 return;
1276 }
1277 string->SetCharAt(index, c);
1278}
1279
Kenny Root1c9e61c2015-05-14 15:58:17 -07001280// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001281void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001282 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001283 jint offset = shadow_frame->GetVReg(arg_offset);
1284 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1285 DCHECK_GE(char_count, 0);
1286 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001287 Handle<mirror::CharArray> h_char_array(
1288 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001289 Runtime* runtime = Runtime::Current();
1290 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1291 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1292}
1293
1294// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001295void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001296 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001297 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1298 if (to_copy == nullptr) {
1299 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1300 return;
1301 }
1302 StackHandleScope<1> hs(self);
1303 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1304 Runtime* runtime = Runtime::Current();
1305 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1306 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1307 allocator));
1308}
1309
Andreas Gampe799681b2015-05-15 19:24:12 -07001310void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001312 jint start = shadow_frame->GetVReg(arg_offset + 1);
1313 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001314 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001315 DCHECK_GE(length, 0);
1316 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001317 Handle<mirror::String> h_string(
1318 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001319 DCHECK_LE(start, h_string->GetLength());
1320 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001321 Runtime* runtime = Runtime::Current();
1322 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1323 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1324}
1325
Kenny Root57f91e82015-05-14 15:58:17 -07001326// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001327void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001328 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001329 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001330 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1331 if (string == nullptr) {
1332 AbortTransactionOrFail(self, "String.charAt with null object");
1333 return;
1334 }
1335 result->SetL(string->ToCharArray(self));
1336}
1337
Andreas Gampebc4d2182016-02-22 10:03:12 -08001338// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1339void UnstartedRuntime::UnstartedReferenceGetReferent(
1340 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001341 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001342 shadow_frame->GetVRegReference(arg_offset));
1343 if (ref == nullptr) {
1344 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1345 return;
1346 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001347 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001348 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1349 result->SetL(referent);
1350}
1351
1352// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1353// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1354// where we can predict the behavior (somewhat).
1355// Note: this is required (instead of lazy initialization) as these classes are used in the static
1356// initialization of other classes, so will *use* the value.
1357void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1358 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001359 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001360 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1361 // 8 as a conservative upper approximation.
1362 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001363 } else if (CheckCallers(shadow_frame,
1364 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001365 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1366 // a good upper bound.
1367 // TODO: Consider resetting in the zygote?
1368 result->SetI(8);
1369 } else {
1370 // Not supported.
1371 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1372 }
1373}
1374
1375// This allows accessing ConcurrentHashMap/SynchronousQueue.
1376
1377void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1378 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1379 // Argument 0 is the Unsafe instance, skip.
1380 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1381 if (obj == nullptr) {
1382 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1383 return;
1384 }
1385 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1386 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1387 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001388 bool success;
1389 // Check whether we're in a transaction, call accordingly.
1390 if (Runtime::Current()->IsActiveTransaction()) {
1391 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1392 expectedValue,
1393 newValue);
1394 } else {
1395 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1396 expectedValue,
1397 newValue);
1398 }
1399 result->SetZ(success ? 1 : 0);
1400}
1401
1402void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1403 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1404 // Argument 0 is the Unsafe instance, skip.
1405 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1406 if (obj == nullptr) {
1407 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1408 return;
1409 }
1410 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1411 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1412 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1413
1414 // Must use non transactional mode.
1415 if (kUseReadBarrier) {
1416 // Need to make sure the reference stored in the field is a to-space one before attempting the
1417 // CAS or the CAS could fail incorrectly.
1418 mirror::HeapReference<mirror::Object>* field_addr =
1419 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1420 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001421 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001422 obj,
1423 MemberOffset(offset),
1424 field_addr);
1425 }
1426 bool success;
1427 // Check whether we're in a transaction, call accordingly.
1428 if (Runtime::Current()->IsActiveTransaction()) {
1429 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1430 expected_value,
1431 newValue);
1432 } else {
1433 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1434 expected_value,
1435 newValue);
1436 }
1437 result->SetZ(success ? 1 : 0);
1438}
1439
1440void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1441 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001442 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001443 // Argument 0 is the Unsafe instance, skip.
1444 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1445 if (obj == nullptr) {
1446 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1447 return;
1448 }
1449 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1450 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1451 result->SetL(value);
1452}
1453
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001454void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1455 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001456 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001457 // Argument 0 is the Unsafe instance, skip.
1458 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1459 if (obj == nullptr) {
1460 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1461 return;
1462 }
1463 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1464 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1465 if (Runtime::Current()->IsActiveTransaction()) {
1466 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1467 } else {
1468 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1469 }
1470}
1471
Andreas Gampebc4d2182016-02-22 10:03:12 -08001472void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1473 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001474 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001475 // Argument 0 is the Unsafe instance, skip.
1476 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1477 if (obj == nullptr) {
1478 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1479 return;
1480 }
1481 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1482 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1483 QuasiAtomic::ThreadFenceRelease();
1484 if (Runtime::Current()->IsActiveTransaction()) {
1485 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1486 } else {
1487 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1488 }
1489}
1490
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001491// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1492// of correctly handling the corner cases.
1493void UnstartedRuntime::UnstartedIntegerParseInt(
1494 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001495 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001496 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1497 if (obj == nullptr) {
1498 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1499 return;
1500 }
1501
1502 std::string string_value = obj->AsString()->ToModifiedUtf8();
1503 if (string_value.empty()) {
1504 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1505 return;
1506 }
1507
1508 const char* c_str = string_value.c_str();
1509 char *end;
1510 // Can we set errno to 0? Is this always a variable, and not a macro?
1511 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1512 int64_t l = strtol(c_str, &end, 10);
1513
1514 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1515 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1516 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1517 return;
1518 }
1519 if (l == 0) {
1520 // Check whether the string wasn't exactly zero.
1521 if (string_value != "0") {
1522 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1523 return;
1524 }
1525 } else if (*end != '\0') {
1526 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1527 return;
1528 }
1529
1530 result->SetI(static_cast<int32_t>(l));
1531}
1532
1533// A cutout for Long.parseLong.
1534//
1535// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1536// well.
1537void UnstartedRuntime::UnstartedLongParseLong(
1538 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001539 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001540 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1541 if (obj == nullptr) {
1542 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1543 return;
1544 }
1545
1546 std::string string_value = obj->AsString()->ToModifiedUtf8();
1547 if (string_value.empty()) {
1548 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1549 return;
1550 }
1551
1552 const char* c_str = string_value.c_str();
1553 char *end;
1554 // Can we set errno to 0? Is this always a variable, and not a macro?
1555 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1556 int64_t l = strtol(c_str, &end, 10);
1557
1558 // Note: comparing against int32_t min/max is intentional here.
1559 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1560 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1561 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1562 return;
1563 }
1564 if (l == 0) {
1565 // Check whether the string wasn't exactly zero.
1566 if (string_value != "0") {
1567 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1568 return;
1569 }
1570 } else if (*end != '\0') {
1571 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1572 return;
1573 }
1574
1575 result->SetJ(l);
1576}
1577
Andreas Gampe715fdc22016-04-18 17:07:30 -07001578void UnstartedRuntime::UnstartedMethodInvoke(
1579 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001580 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001581 JNIEnvExt* env = self->GetJniEnv();
1582 ScopedObjectAccessUnchecked soa(self);
1583
Mathieu Chartier8778c522016-10-04 19:06:30 -07001584 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001585 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001586 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001587
Mathieu Chartier8778c522016-10-04 19:06:30 -07001588 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001589 ScopedLocalRef<jobject> java_receiver(env,
1590 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1591
Mathieu Chartier8778c522016-10-04 19:06:30 -07001592 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001593 ScopedLocalRef<jobject> java_args(env,
1594 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1595
1596 ScopedLocalRef<jobject> result_jobj(env,
1597 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1598
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001599 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001600
1601 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1602 // InvocationTargetExceptions.
1603 if (self->IsExceptionPending()) {
1604 AbortTransactionOrFail(self, "Failed Method.invoke");
1605 }
1606}
1607
Andreas Gampebc4d2182016-02-22 10:03:12 -08001608
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1610 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1611 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001612 int32_t length = args[1];
1613 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001614 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001615 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001616 ObjPtr<mirror::Class> array_class =
1617 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001618 DCHECK(array_class != nullptr);
1619 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001620 result->SetL(mirror::Array::Alloc<true, true>(self,
1621 array_class,
1622 length,
1623 array_class->GetComponentSizeShift(),
1624 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001625}
1626
Mathieu Chartiere401d142015-04-22 13:56:20 -07001627void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1628 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1629 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001630 result->SetL(nullptr);
1631}
1632
Mathieu Chartiere401d142015-04-22 13:56:20 -07001633void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1634 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1635 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001636 NthCallerVisitor visitor(self, 3);
1637 visitor.WalkStack();
1638 if (visitor.caller != nullptr) {
1639 result->SetL(visitor.caller->GetDeclaringClass());
1640 }
1641}
1642
Mathieu Chartiere401d142015-04-22 13:56:20 -07001643void UnstartedRuntime::UnstartedJNIMathLog(
1644 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1645 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001646 JValue value;
1647 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1648 result->SetD(log(value.GetD()));
1649}
1650
Mathieu Chartiere401d142015-04-22 13:56:20 -07001651void UnstartedRuntime::UnstartedJNIMathExp(
1652 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1653 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001654 JValue value;
1655 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1656 result->SetD(exp(value.GetD()));
1657}
1658
Andreas Gampebc4d2182016-02-22 10:03:12 -08001659void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1660 Thread* self ATTRIBUTE_UNUSED,
1661 ArtMethod* method ATTRIBUTE_UNUSED,
1662 mirror::Object* receiver ATTRIBUTE_UNUSED,
1663 uint32_t* args ATTRIBUTE_UNUSED,
1664 JValue* result) {
1665 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1666 ? 0
1667 : 1);
1668}
1669
Mathieu Chartiere401d142015-04-22 13:56:20 -07001670void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1671 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1672 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001673 StackHandleScope<1> hs(self);
1674 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1675}
1676
Andreas Gampebc4d2182016-02-22 10:03:12 -08001677void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1678 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1679 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1680 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1681 result->SetD(bit_cast<double>(long_input));
1682}
1683
Mathieu Chartiere401d142015-04-22 13:56:20 -07001684void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1685 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1686 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001687 result->SetI(args[0]);
1688}
1689
Mathieu Chartiere401d142015-04-22 13:56:20 -07001690void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1691 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1692 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001693 result->SetI(args[0]);
1694}
1695
Mathieu Chartiere401d142015-04-22 13:56:20 -07001696void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1697 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1698 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001699 result->SetL(receiver->Clone(self));
1700}
1701
Mathieu Chartiere401d142015-04-22 13:56:20 -07001702void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1703 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1704 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001705 receiver->NotifyAll(self);
1706}
1707
Mathieu Chartiere401d142015-04-22 13:56:20 -07001708void UnstartedRuntime::UnstartedJNIStringCompareTo(
1709 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1710 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001711 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1712 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001713 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001714 }
1715 result->SetI(receiver->AsString()->CompareTo(rhs));
1716}
1717
Mathieu Chartiere401d142015-04-22 13:56:20 -07001718void UnstartedRuntime::UnstartedJNIStringIntern(
1719 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1720 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001721 result->SetL(receiver->AsString()->Intern());
1722}
1723
Mathieu Chartiere401d142015-04-22 13:56:20 -07001724void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1725 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1726 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001727 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1728}
1729
Mathieu Chartiere401d142015-04-22 13:56:20 -07001730void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1731 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1732 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001733 StackHandleScope<2> hs(self);
1734 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1735 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1736 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1737}
1738
Mathieu Chartiere401d142015-04-22 13:56:20 -07001739void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1740 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1741 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001742 int32_t length = static_cast<int32_t>(args[1]);
1743 if (length < 0) {
1744 ThrowNegativeArraySizeException(length);
1745 return;
1746 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001747 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001748 Runtime* runtime = Runtime::Current();
1749 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001750 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001751 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001752 CHECK(self->IsExceptionPending());
1753 return;
1754 }
1755 DCHECK(array_class->IsObjectArrayClass());
1756 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1757 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1758 result->SetL(new_array);
1759}
1760
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1762 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1763 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001764 ScopedObjectAccessUnchecked soa(self);
1765 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001766 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001767 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001768 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001769 }
1770}
1771
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1773 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1774 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001775 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1776 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1777}
1778
Mathieu Chartiere401d142015-04-22 13:56:20 -07001779void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1780 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1781 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001782 result->SetZ(JNI_TRUE);
1783}
1784
Mathieu Chartiere401d142015-04-22 13:56:20 -07001785void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1786 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1787 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001788 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1789 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1790 jint expectedValue = args[3];
1791 jint newValue = args[4];
1792 bool success;
1793 if (Runtime::Current()->IsActiveTransaction()) {
1794 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1795 expectedValue, newValue);
1796 } else {
1797 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1798 expectedValue, newValue);
1799 }
1800 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1801}
1802
Narayan Kamath34a316f2016-03-30 13:11:18 +01001803void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1804 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1805 uint32_t* args, JValue* result) {
1806 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1807 if (obj == nullptr) {
1808 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1809 return;
1810 }
1811
1812 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1813 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1814}
1815
Mathieu Chartiere401d142015-04-22 13:56:20 -07001816void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1817 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1818 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001819 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1820 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1821 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1822 if (Runtime::Current()->IsActiveTransaction()) {
1823 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1824 } else {
1825 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1826 }
1827}
1828
Andreas Gampe799681b2015-05-15 19:24:12 -07001829void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001830 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1831 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001832 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1833 Primitive::Type primitive_type = component->GetPrimitiveType();
1834 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1835}
1836
Andreas Gampe799681b2015-05-15 19:24:12 -07001837void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001838 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1839 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001840 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1841 Primitive::Type primitive_type = component->GetPrimitiveType();
1842 result->SetI(Primitive::ComponentSize(primitive_type));
1843}
1844
Andreas Gampedd9d0552015-03-09 12:57:41 -07001845typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001846 size_t arg_size);
1847
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001849 uint32_t* args, JValue* result);
1850
1851static bool tables_initialized_ = false;
1852static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1853static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1854
Andreas Gampe799681b2015-05-15 19:24:12 -07001855void UnstartedRuntime::InitializeInvokeHandlers() {
1856#define UNSTARTED_DIRECT(ShortName, Sig) \
1857 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1858#include "unstarted_runtime_list.h"
1859 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1860#undef UNSTARTED_RUNTIME_DIRECT_LIST
1861#undef UNSTARTED_RUNTIME_JNI_LIST
1862#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001863}
1864
Andreas Gampe799681b2015-05-15 19:24:12 -07001865void UnstartedRuntime::InitializeJNIHandlers() {
1866#define UNSTARTED_JNI(ShortName, Sig) \
1867 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1868#include "unstarted_runtime_list.h"
1869 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1870#undef UNSTARTED_RUNTIME_DIRECT_LIST
1871#undef UNSTARTED_RUNTIME_JNI_LIST
1872#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001873}
1874
Andreas Gampe799681b2015-05-15 19:24:12 -07001875void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001876 CHECK(!tables_initialized_);
1877
Andreas Gampe799681b2015-05-15 19:24:12 -07001878 InitializeInvokeHandlers();
1879 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001880
1881 tables_initialized_ = true;
1882}
1883
Andreas Gampe799681b2015-05-15 19:24:12 -07001884void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1885 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001886 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1887 // problems in core libraries.
1888 CHECK(tables_initialized_);
1889
David Sehr709b0702016-10-13 09:12:37 -07001890 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001891 const auto& iter = invoke_handlers_.find(name);
1892 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001893 // Clear out the result in case it's not zeroed out.
1894 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001895
1896 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1897 self->PushShadowFrame(shadow_frame);
1898
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001899 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001900
1901 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001902 } else {
1903 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001904 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001905 }
1906}
1907
1908// 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 -07001909void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001910 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001911 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001912 const auto& iter = jni_handlers_.find(name);
1913 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001914 // Clear out the result in case it's not zeroed out.
1915 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001916 (*iter->second)(self, method, receiver, args, result);
1917 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001918 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1919 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001920 } else {
David Sehr709b0702016-10-13 09:12:37 -07001921 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001922 "non-transactional runtime";
1923 }
1924}
1925
1926} // namespace interpreter
1927} // namespace art