blob: 31e798677070bc9ab6363b276133608c511b2d1d [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
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080032#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070033#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070034#include "base/logging.h"
35#include "base/macros.h"
36#include "class_linker.h"
37#include "common_throws.h"
38#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080039#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070040#include "handle_scope-inl.h"
41#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070042#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070044#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070045#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010046#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
49#include "mirror/string-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070050#include "nativehelper/scoped_local_ref.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070051#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070052#include "reflection.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070053#include "thread-inl.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)) {
Vladimir Markoba118822017-06-12 15:41:56 +0100268 auto* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
Mathieu Chartiere401d142015-04-22 13:56:20 -0700269 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.
Andreas Gampeeac4f282017-04-26 21:07:04 -0700571 mem_map.reset();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700572
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();
Vladimir Markoba118822017-06-12 15:41:56 +0100594 ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700595 if (constructor == nullptr) {
596 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
597 return;
598 }
599
600 uint32_t args[1];
601 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
602 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
603
604 if (self->IsExceptionPending()) {
605 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
606 return;
607 }
608
609 result->SetL(h_obj.Get());
610}
611
612void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
613 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
614 {
615 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
616 CHECK(this_obj != nullptr);
617 CHECK(this_obj->IsClassLoader());
618
619 StackHandleScope<1> hs(self);
620 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
621
622 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
623 this_classloader_class.Get()) {
624 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700625 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700626 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700627 return;
628 }
629 }
630
631 GetResourceAsStream(self, shadow_frame, result, arg_offset);
632}
633
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800634void UnstartedRuntime::UnstartedConstructorNewInstance0(
635 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
636 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
637 StackHandleScope<4> hs(self);
638 Handle<mirror::Constructor> m = hs.NewHandle(
639 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
640 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
641 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
642 shadow_frame->GetVRegReference(arg_offset + 1)));
643 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
644 if (UNLIKELY(c->IsAbstract())) {
645 AbortTransactionOrFail(self, "Cannot handle abstract classes");
646 return;
647 }
648 // Verify that we can access the class.
649 if (!m->IsAccessible() && !c->IsPublic()) {
650 // Go 2 frames back, this method is always called from newInstance0, which is called from
651 // Constructor.newInstance(Object... args).
652 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
653 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
654 // access checks anyways. TODO: Investigate if this the correct behavior.
655 if (caller != nullptr && !caller->CanAccess(c.Get())) {
656 AbortTransactionOrFail(self, "Cannot access class");
657 return;
658 }
659 }
660 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
661 DCHECK(self->IsExceptionPending());
662 return;
663 }
664 if (c->IsClassClass()) {
665 AbortTransactionOrFail(self, "new Class() is not supported");
666 return;
667 }
668
669 // String constructor is replaced by a StringFactory method in InvokeMethod.
670 if (c->IsStringClass()) {
671 // We don't support strings.
672 AbortTransactionOrFail(self, "String construction is not supported");
673 return;
674 }
675
676 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
677 if (receiver == nullptr) {
678 AbortTransactionOrFail(self, "Could not allocate");
679 return;
680 }
681
682 // It's easier to use reflection to make the call, than create the uint32_t array.
683 {
684 ScopedObjectAccessUnchecked soa(self);
685 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
686 soa.AddLocalReference<jobject>(m.Get()));
687 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
688 soa.AddLocalReference<jobject>(receiver.Get()));
689 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
690 soa.AddLocalReference<jobject>(args.Get()));
691 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
692 }
693 if (self->IsExceptionPending()) {
694 AbortTransactionOrFail(self, "Failed running constructor");
695 } else {
696 result->SetL(receiver.Get());
697 }
698}
699
Andreas Gampe799681b2015-05-15 19:24:12 -0700700void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700702 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
703 mirror::ClassLoader* class_loader =
704 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
705 StackHandleScope<2> hs(self);
706 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
707 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
708 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
709 "VMClassLoader.findLoadedClass", false, false);
710 // This might have an error pending. But semantics are to just return null.
711 if (self->IsExceptionPending()) {
712 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700713 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700714 if (type != "java.lang.InternalError") {
715 self->ClearException();
716 }
717 }
718}
719
Mathieu Chartiere401d142015-04-22 13:56:20 -0700720void UnstartedRuntime::UnstartedVoidLookupType(
721 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
722 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700723 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
724}
725
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700726// Arraycopy emulation.
727// Note: we can't use any fast copy functions, as they are not available under transaction.
728
729template <typename T>
730static void PrimitiveArrayCopy(Thread* self,
731 mirror::Array* src_array, int32_t src_pos,
732 mirror::Array* dst_array, int32_t dst_pos,
733 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700734 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700735 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700736 AbortTransactionOrFail(self,
737 "Types mismatched in arraycopy: %s vs %s.",
738 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700739 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700740 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700741 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700742 return;
743 }
744 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
745 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
746 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
747 if (copy_forward) {
748 for (int32_t i = 0; i < length; ++i) {
749 dst->Set(dst_pos + i, src->Get(src_pos + i));
750 }
751 } else {
752 for (int32_t i = 1; i <= length; ++i) {
753 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
754 }
755 }
756}
757
Andreas Gampe799681b2015-05-15 19:24:12 -0700758void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700759 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700760 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700761 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
762 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700763 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700764
Andreas Gampe85a098a2016-03-31 13:30:53 -0700765 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
766 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
767 // Null checking. For simplicity, abort transaction.
768 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700769 AbortTransactionOrFail(self, "src is null in arraycopy.");
770 return;
771 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700772 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700773 AbortTransactionOrFail(self, "dst is null in arraycopy.");
774 return;
775 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700776 // Test for arrayness. Throw ArrayStoreException.
777 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
778 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
779 return;
780 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700781
Andreas Gampe85a098a2016-03-31 13:30:53 -0700782 mirror::Array* src_array = src_obj->AsArray();
783 mirror::Array* dst_array = dst_obj->AsArray();
784
785 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700786 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
787 UNLIKELY(src_pos > src_array->GetLength() - length) ||
788 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700789 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700790 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
791 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
792 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700793 return;
794 }
795
796 // Type checking.
797 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
798 GetComponentType();
799
800 if (!src_type->IsPrimitive()) {
801 // Check that the second type is not primitive.
802 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
803 GetComponentType();
804 if (trg_type->IsPrimitiveInt()) {
805 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700806 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700807 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700808 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700809 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700810 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700811 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700812
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700813 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
814 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
815 if (src == dst) {
816 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700817 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700818 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
819 if (copy_forward) {
820 for (int32_t i = 0; i < length; ++i) {
821 dst->Set(dst_pos + i, src->Get(src_pos + i));
822 }
823 } else {
824 for (int32_t i = 1; i <= length; ++i) {
825 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
826 }
827 }
828 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700829 // We're being lazy here. Optimally this could be a memcpy (if component types are
830 // assignable), but the ObjectArray implementation doesn't support transactions. The
831 // checking version, however, does.
832 if (Runtime::Current()->IsActiveTransaction()) {
833 dst->AssignableCheckingMemcpy<true>(
834 dst_pos, src, src_pos, length, true /* throw_exception */);
835 } else {
836 dst->AssignableCheckingMemcpy<false>(
837 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700838 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700839 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700840 } else if (src_type->IsPrimitiveByte()) {
841 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700842 } else if (src_type->IsPrimitiveChar()) {
843 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
844 } else if (src_type->IsPrimitiveInt()) {
845 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700846 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700847 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700848 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700849 }
850}
851
Andreas Gampe5c9af612016-04-05 14:16:10 -0700852void UnstartedRuntime::UnstartedSystemArraycopyByte(
853 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
854 // Just forward.
855 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
856}
857
Andreas Gampe799681b2015-05-15 19:24:12 -0700858void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700860 // Just forward.
861 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
862}
863
864void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700865 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700866 // Just forward.
867 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
868}
869
Narayan Kamath34a316f2016-03-30 13:11:18 +0100870void UnstartedRuntime::UnstartedSystemGetSecurityManager(
871 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
872 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
873 result->SetL(nullptr);
874}
875
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700876static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
877
878static void GetSystemProperty(Thread* self,
879 ShadowFrame* shadow_frame,
880 JValue* result,
881 size_t arg_offset,
882 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700883 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700884 StackHandleScope<4> hs(self);
885 Handle<mirror::String> h_key(
886 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800887 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700888 AbortTransactionOrFail(self, "getProperty key was null");
889 return;
890 }
891
892 // This is overall inefficient, but reflecting the values here is not great, either. So
893 // for simplicity, and with the assumption that the number of getProperty calls is not
894 // too great, just iterate each time.
895
896 // Get the storage class.
897 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
898 Handle<mirror::Class> h_props_class(hs.NewHandle(
899 class_linker->FindClass(self,
900 "Ljava/lang/AndroidHardcodedSystemProperties;",
901 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800902 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700903 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
904 return;
905 }
906 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
907 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
908 return;
909 }
910
911 // Get the storage array.
912 ArtField* static_properties =
913 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
914 "[[Ljava/lang/String;");
915 if (static_properties == nullptr) {
916 AbortTransactionOrFail(self,
917 "Could not find %s field",
918 kAndroidHardcodedSystemPropertiesFieldName);
919 return;
920 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700921 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
922 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
923 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800924 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700925 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
926 return;
927 }
928
929 // Iterate over it.
930 const int32_t prop_count = h_2string_array->GetLength();
931 // Use the third handle as mutable.
932 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
933 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
934 for (int32_t i = 0; i < prop_count; ++i) {
935 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800936 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700937 h_string_array->GetLength() != 2 ||
938 h_string_array->Get(0) == nullptr) {
939 AbortTransactionOrFail(self,
940 "Unexpected content of %s",
941 kAndroidHardcodedSystemPropertiesFieldName);
942 return;
943 }
944 if (h_key->Equals(h_string_array->Get(0))) {
945 // Found a value.
946 if (h_string_array->Get(1) == nullptr && is_default_version) {
947 // Null is being delegated to the default map, and then resolved to the given default value.
948 // As there's no default map, return the given value.
949 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
950 } else {
951 result->SetL(h_string_array->Get(1));
952 }
953 return;
954 }
955 }
956
957 // Key is not supported.
958 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
959}
960
961void UnstartedRuntime::UnstartedSystemGetProperty(
962 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
963 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
964}
965
966void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
967 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
968 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
969}
970
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800971static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
972 REQUIRES_SHARED(Locks::mutator_lock_) {
973 if (shadow_frame->GetLink() == nullptr) {
974 return "<no caller>";
975 }
976 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
977}
978
979static bool CheckCallers(ShadowFrame* shadow_frame,
980 std::initializer_list<std::string> allowed_call_stack)
981 REQUIRES_SHARED(Locks::mutator_lock_) {
982 for (const std::string& allowed_caller : allowed_call_stack) {
983 if (shadow_frame->GetLink() == nullptr) {
984 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700985 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800986
987 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
988 if (allowed_caller != found_caller) {
989 return false;
990 }
991
992 shadow_frame = shadow_frame->GetLink();
993 }
994 return true;
995}
996
997static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
998 REQUIRES_SHARED(Locks::mutator_lock_) {
999 // Find the requested class.
1000 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1001 ObjPtr<mirror::Class> klass =
1002 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1003 if (klass == nullptr) {
1004 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1005 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001006 }
1007
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001008 StackHandleScope<2> hs(self);
1009 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1010 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001011 if (h_obj != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01001012 ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001013 if (init_method == nullptr) {
1014 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1015 return nullptr;
1016 } else {
1017 JValue invoke_result;
1018 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1019 if (!self->IsExceptionPending()) {
1020 return h_obj.Get();
1021 }
1022 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1023 }
1024 }
1025 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1026 return nullptr;
1027}
1028
1029void UnstartedRuntime::UnstartedThreadLocalGet(
1030 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1031 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1032 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1033 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1034 } else {
1035 AbortTransactionOrFail(self,
1036 "ThreadLocal.get() does not support %s",
1037 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001038 }
1039}
1040
Andreas Gampebad529d2017-02-13 18:52:10 -08001041void UnstartedRuntime::UnstartedThreadCurrentThread(
1042 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1043 if (CheckCallers(shadow_frame,
1044 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1045 "java.lang.String, long)",
1046 "void java.lang.Thread.<init>()",
1047 "void java.util.logging.LogManager$Cleaner.<init>("
1048 "java.util.logging.LogManager)" })) {
1049 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1050 // Thread constructor only asks for the current thread to set up defaults and add the
1051 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1052 // these purposes.
1053 Runtime::Current()->InitThreadGroups(self);
1054 jobject main_peer =
1055 self->CreateCompileTimePeer(self->GetJniEnv(),
1056 "main",
1057 false,
1058 Runtime::Current()->GetMainThreadGroup());
1059 if (main_peer == nullptr) {
1060 AbortTransactionOrFail(self, "Failed allocating peer");
1061 return;
1062 }
1063
1064 result->SetL(self->DecodeJObject(main_peer));
1065 self->GetJniEnv()->DeleteLocalRef(main_peer);
1066 } else {
1067 AbortTransactionOrFail(self,
1068 "Thread.currentThread() does not support %s",
1069 GetImmediateCaller(shadow_frame).c_str());
1070 }
1071}
1072
1073void UnstartedRuntime::UnstartedThreadGetNativeState(
1074 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1075 if (CheckCallers(shadow_frame,
1076 { "java.lang.Thread$State java.lang.Thread.getState()",
1077 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1078 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1079 "java.lang.String, long)",
1080 "void java.lang.Thread.<init>()",
1081 "void java.util.logging.LogManager$Cleaner.<init>("
1082 "java.util.logging.LogManager)" })) {
1083 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1084 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1085 constexpr int32_t kJavaRunnable = 1;
1086 result->SetI(kJavaRunnable);
1087 } else {
1088 AbortTransactionOrFail(self,
1089 "Thread.getNativeState() does not support %s",
1090 GetImmediateCaller(shadow_frame).c_str());
1091 }
1092}
1093
Sergio Giro83261202016-04-11 20:49:20 +01001094void UnstartedRuntime::UnstartedMathCeil(
1095 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001096 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001097}
1098
1099void UnstartedRuntime::UnstartedMathFloor(
1100 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001101 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001102}
1103
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001104void UnstartedRuntime::UnstartedMathSin(
1105 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1106 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1107}
1108
1109void UnstartedRuntime::UnstartedMathCos(
1110 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1111 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1112}
1113
1114void UnstartedRuntime::UnstartedMathPow(
1115 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1116 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1117 shadow_frame->GetVRegDouble(arg_offset + 2)));
1118}
1119
Andreas Gampe799681b2015-05-15 19:24:12 -07001120void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001122 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1123 result->SetI(obj->IdentityHashCode());
1124}
1125
Andreas Gampe799681b2015-05-15 19:24:12 -07001126void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001127 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001128 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001129 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001130}
1131
Andreas Gampedd9d0552015-03-09 12:57:41 -07001132static void UnstartedMemoryPeek(
1133 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1134 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1135 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1136 // aborting the transaction.
1137
1138 switch (type) {
1139 case Primitive::kPrimByte: {
1140 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1141 return;
1142 }
1143
1144 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001145 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1146 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001147 return;
1148 }
1149
1150 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001151 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1152 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001153 return;
1154 }
1155
1156 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001157 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1158 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001159 return;
1160 }
1161
1162 case Primitive::kPrimBoolean:
1163 case Primitive::kPrimChar:
1164 case Primitive::kPrimFloat:
1165 case Primitive::kPrimDouble:
1166 case Primitive::kPrimVoid:
1167 case Primitive::kPrimNot:
1168 LOG(FATAL) << "Not in the Memory API: " << type;
1169 UNREACHABLE();
1170 }
1171 LOG(FATAL) << "Should not reach here";
1172 UNREACHABLE();
1173}
1174
Andreas Gampe799681b2015-05-15 19:24:12 -07001175void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001176 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001177 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1178}
1179
1180void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001181 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001182 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1183}
1184
1185void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001186 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001187 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1188}
1189
1190void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001191 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001192 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001193}
1194
1195static void UnstartedMemoryPeekArray(
1196 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001197 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001198 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1199 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1200 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001201 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001202 return;
1203 }
1204 mirror::Array* array = obj->AsArray();
1205
1206 int offset = shadow_frame->GetVReg(arg_offset + 3);
1207 int count = shadow_frame->GetVReg(arg_offset + 4);
1208 if (offset < 0 || offset + count > array->GetLength()) {
1209 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1210 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001211 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001212 return;
1213 }
1214
1215 switch (type) {
1216 case Primitive::kPrimByte: {
1217 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1218 mirror::ByteArray* byte_array = array->AsByteArray();
1219 for (int32_t i = 0; i < count; ++i, ++address) {
1220 byte_array->SetWithoutChecks<true>(i + offset, *address);
1221 }
1222 return;
1223 }
1224
1225 case Primitive::kPrimShort:
1226 case Primitive::kPrimInt:
1227 case Primitive::kPrimLong:
1228 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1229 UNREACHABLE();
1230
1231 case Primitive::kPrimBoolean:
1232 case Primitive::kPrimChar:
1233 case Primitive::kPrimFloat:
1234 case Primitive::kPrimDouble:
1235 case Primitive::kPrimVoid:
1236 case Primitive::kPrimNot:
1237 LOG(FATAL) << "Not in the Memory API: " << type;
1238 UNREACHABLE();
1239 }
1240 LOG(FATAL) << "Should not reach here";
1241 UNREACHABLE();
1242}
1243
Andreas Gampe799681b2015-05-15 19:24:12 -07001244void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001246 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001247}
1248
Kenny Root1c9e61c2015-05-14 15:58:17 -07001249// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001250void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001251 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001252 jint start = shadow_frame->GetVReg(arg_offset + 1);
1253 jint end = shadow_frame->GetVReg(arg_offset + 2);
1254 jint index = shadow_frame->GetVReg(arg_offset + 4);
1255 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1256 if (string == nullptr) {
1257 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1258 return;
1259 }
Kenny Root57f91e82015-05-14 15:58:17 -07001260 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001261 DCHECK_LE(start, end);
1262 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001263 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001264 Handle<mirror::CharArray> h_char_array(
1265 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001266 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001267 DCHECK_LE(index, h_char_array->GetLength());
1268 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001269 string->GetChars(start, end, h_char_array, index);
1270}
1271
1272// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001273void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001274 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001275 jint index = shadow_frame->GetVReg(arg_offset + 1);
1276 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1277 if (string == nullptr) {
1278 AbortTransactionOrFail(self, "String.charAt with null object");
1279 return;
1280 }
1281 result->SetC(string->CharAt(index));
1282}
1283
Vladimir Marko92907f32017-02-20 14:08:30 +00001284// This allows creating String objects with replaced characters during compilation.
1285// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1286void UnstartedRuntime::UnstartedStringDoReplace(
1287 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1288 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1289 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001290 StackHandleScope<1> hs(self);
1291 Handle<mirror::String> string =
1292 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
Kenny Root57f91e82015-05-14 15:58:17 -07001293 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001294 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001295 return;
1296 }
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001297 result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001298}
1299
Kenny Root1c9e61c2015-05-14 15:58:17 -07001300// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001301void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001302 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001303 jint offset = shadow_frame->GetVReg(arg_offset);
1304 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1305 DCHECK_GE(char_count, 0);
1306 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001307 Handle<mirror::CharArray> h_char_array(
1308 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001309 Runtime* runtime = Runtime::Current();
1310 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1311 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1312}
1313
1314// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001315void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001316 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001317 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1318 if (to_copy == nullptr) {
1319 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1320 return;
1321 }
1322 StackHandleScope<1> hs(self);
1323 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1324 Runtime* runtime = Runtime::Current();
1325 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1326 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1327 allocator));
1328}
1329
Andreas Gampe799681b2015-05-15 19:24:12 -07001330void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001332 jint start = shadow_frame->GetVReg(arg_offset + 1);
1333 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001334 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001335 DCHECK_GE(length, 0);
1336 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001337 Handle<mirror::String> h_string(
1338 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001339 DCHECK_LE(start, h_string->GetLength());
1340 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001341 Runtime* runtime = Runtime::Current();
1342 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1343 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1344}
1345
Kenny Root57f91e82015-05-14 15:58:17 -07001346// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001347void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001348 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001349 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001350 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1351 if (string == nullptr) {
1352 AbortTransactionOrFail(self, "String.charAt with null object");
1353 return;
1354 }
1355 result->SetL(string->ToCharArray(self));
1356}
1357
Andreas Gampebc4d2182016-02-22 10:03:12 -08001358// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1359void UnstartedRuntime::UnstartedReferenceGetReferent(
1360 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001361 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001362 shadow_frame->GetVRegReference(arg_offset));
1363 if (ref == nullptr) {
1364 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1365 return;
1366 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001367 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001368 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1369 result->SetL(referent);
1370}
1371
1372// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1373// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1374// where we can predict the behavior (somewhat).
1375// Note: this is required (instead of lazy initialization) as these classes are used in the static
1376// initialization of other classes, so will *use* the value.
1377void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1378 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001379 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001380 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1381 // 8 as a conservative upper approximation.
1382 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001383 } else if (CheckCallers(shadow_frame,
1384 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001385 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1386 // a good upper bound.
1387 // TODO: Consider resetting in the zygote?
1388 result->SetI(8);
1389 } else {
1390 // Not supported.
1391 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1392 }
1393}
1394
1395// This allows accessing ConcurrentHashMap/SynchronousQueue.
1396
1397void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1398 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1399 // Argument 0 is the Unsafe instance, skip.
1400 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1401 if (obj == nullptr) {
1402 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1403 return;
1404 }
1405 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1406 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1407 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001408 bool success;
1409 // Check whether we're in a transaction, call accordingly.
1410 if (Runtime::Current()->IsActiveTransaction()) {
1411 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1412 expectedValue,
1413 newValue);
1414 } else {
1415 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1416 expectedValue,
1417 newValue);
1418 }
1419 result->SetZ(success ? 1 : 0);
1420}
1421
1422void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1423 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1424 // Argument 0 is the Unsafe instance, skip.
1425 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1426 if (obj == nullptr) {
1427 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1428 return;
1429 }
1430 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1431 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1432 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1433
1434 // Must use non transactional mode.
1435 if (kUseReadBarrier) {
1436 // Need to make sure the reference stored in the field is a to-space one before attempting the
1437 // CAS or the CAS could fail incorrectly.
1438 mirror::HeapReference<mirror::Object>* field_addr =
1439 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1440 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001441 ReadBarrier::Barrier<
1442 mirror::Object,
1443 /* kIsVolatile */ false,
1444 kWithReadBarrier,
1445 /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001446 obj,
1447 MemberOffset(offset),
1448 field_addr);
1449 }
1450 bool success;
1451 // Check whether we're in a transaction, call accordingly.
1452 if (Runtime::Current()->IsActiveTransaction()) {
1453 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1454 expected_value,
1455 newValue);
1456 } else {
1457 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1458 expected_value,
1459 newValue);
1460 }
1461 result->SetZ(success ? 1 : 0);
1462}
1463
1464void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1465 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001466 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001467 // Argument 0 is the Unsafe instance, skip.
1468 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1469 if (obj == nullptr) {
1470 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1471 return;
1472 }
1473 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1474 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1475 result->SetL(value);
1476}
1477
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001478void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1479 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001480 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001481 // Argument 0 is the Unsafe instance, skip.
1482 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1483 if (obj == nullptr) {
1484 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1485 return;
1486 }
1487 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1488 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1489 if (Runtime::Current()->IsActiveTransaction()) {
1490 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1491 } else {
1492 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1493 }
1494}
1495
Andreas Gampebc4d2182016-02-22 10:03:12 -08001496void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1497 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001498 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001499 // Argument 0 is the Unsafe instance, skip.
1500 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1501 if (obj == nullptr) {
1502 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1503 return;
1504 }
1505 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1506 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1507 QuasiAtomic::ThreadFenceRelease();
1508 if (Runtime::Current()->IsActiveTransaction()) {
1509 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1510 } else {
1511 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1512 }
1513}
1514
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001515// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1516// of correctly handling the corner cases.
1517void UnstartedRuntime::UnstartedIntegerParseInt(
1518 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001519 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001520 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1521 if (obj == nullptr) {
1522 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1523 return;
1524 }
1525
1526 std::string string_value = obj->AsString()->ToModifiedUtf8();
1527 if (string_value.empty()) {
1528 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1529 return;
1530 }
1531
1532 const char* c_str = string_value.c_str();
1533 char *end;
1534 // Can we set errno to 0? Is this always a variable, and not a macro?
1535 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1536 int64_t l = strtol(c_str, &end, 10);
1537
1538 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1539 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1540 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1541 return;
1542 }
1543 if (l == 0) {
1544 // Check whether the string wasn't exactly zero.
1545 if (string_value != "0") {
1546 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1547 return;
1548 }
1549 } else if (*end != '\0') {
1550 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1551 return;
1552 }
1553
1554 result->SetI(static_cast<int32_t>(l));
1555}
1556
1557// A cutout for Long.parseLong.
1558//
1559// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1560// well.
1561void UnstartedRuntime::UnstartedLongParseLong(
1562 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001563 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001564 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1565 if (obj == nullptr) {
1566 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1567 return;
1568 }
1569
1570 std::string string_value = obj->AsString()->ToModifiedUtf8();
1571 if (string_value.empty()) {
1572 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1573 return;
1574 }
1575
1576 const char* c_str = string_value.c_str();
1577 char *end;
1578 // Can we set errno to 0? Is this always a variable, and not a macro?
1579 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1580 int64_t l = strtol(c_str, &end, 10);
1581
1582 // Note: comparing against int32_t min/max is intentional here.
1583 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1584 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1585 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1586 return;
1587 }
1588 if (l == 0) {
1589 // Check whether the string wasn't exactly zero.
1590 if (string_value != "0") {
1591 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1592 return;
1593 }
1594 } else if (*end != '\0') {
1595 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1596 return;
1597 }
1598
1599 result->SetJ(l);
1600}
1601
Andreas Gampe715fdc22016-04-18 17:07:30 -07001602void UnstartedRuntime::UnstartedMethodInvoke(
1603 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001604 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001605 JNIEnvExt* env = self->GetJniEnv();
1606 ScopedObjectAccessUnchecked soa(self);
1607
Mathieu Chartier8778c522016-10-04 19:06:30 -07001608 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001609 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001610 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001611
Mathieu Chartier8778c522016-10-04 19:06:30 -07001612 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001613 ScopedLocalRef<jobject> java_receiver(env,
1614 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1615
Mathieu Chartier8778c522016-10-04 19:06:30 -07001616 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001617 ScopedLocalRef<jobject> java_args(env,
1618 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1619
1620 ScopedLocalRef<jobject> result_jobj(env,
1621 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1622
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001623 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001624
1625 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1626 // InvocationTargetExceptions.
1627 if (self->IsExceptionPending()) {
1628 AbortTransactionOrFail(self, "Failed Method.invoke");
1629 }
1630}
1631
Nicolas Geoffrayece2f7c2017-03-08 16:11:23 +00001632void UnstartedRuntime::UnstartedSystemIdentityHashCode(
1633 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1634 REQUIRES_SHARED(Locks::mutator_lock_) {
1635 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1636 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1637}
Andreas Gampebc4d2182016-02-22 10:03:12 -08001638
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001639// Checks whether the runtime is s64-bit. This is needed for the clinit of
1640// java.lang.invoke.VarHandle clinit. The clinit determines sets of
1641// available VarHandle accessors and these differ based on machine
1642// word size.
1643void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
1644 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1645 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
1646 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1647 jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
1648 result->SetZ(is64bit);
1649}
1650
Mathieu Chartiere401d142015-04-22 13:56:20 -07001651void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1652 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1653 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001654 int32_t length = args[1];
1655 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001656 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001657 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001658 ObjPtr<mirror::Class> array_class =
1659 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001660 DCHECK(array_class != nullptr);
1661 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001662 result->SetL(mirror::Array::Alloc<true, true>(self,
1663 array_class,
1664 length,
1665 array_class->GetComponentSizeShift(),
1666 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001667}
1668
Mathieu Chartiere401d142015-04-22 13:56:20 -07001669void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1670 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1671 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001672 result->SetL(nullptr);
1673}
1674
Mathieu Chartiere401d142015-04-22 13:56:20 -07001675void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1676 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1677 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001678 NthCallerVisitor visitor(self, 3);
1679 visitor.WalkStack();
1680 if (visitor.caller != nullptr) {
1681 result->SetL(visitor.caller->GetDeclaringClass());
1682 }
1683}
1684
Mathieu Chartiere401d142015-04-22 13:56:20 -07001685void UnstartedRuntime::UnstartedJNIMathLog(
1686 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1687 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001688 JValue value;
1689 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1690 result->SetD(log(value.GetD()));
1691}
1692
Mathieu Chartiere401d142015-04-22 13:56:20 -07001693void UnstartedRuntime::UnstartedJNIMathExp(
1694 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1695 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001696 JValue value;
1697 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1698 result->SetD(exp(value.GetD()));
1699}
1700
Andreas Gampebc4d2182016-02-22 10:03:12 -08001701void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1702 Thread* self ATTRIBUTE_UNUSED,
1703 ArtMethod* method ATTRIBUTE_UNUSED,
1704 mirror::Object* receiver ATTRIBUTE_UNUSED,
1705 uint32_t* args ATTRIBUTE_UNUSED,
1706 JValue* result) {
1707 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1708 ? 0
1709 : 1);
1710}
1711
Mathieu Chartiere401d142015-04-22 13:56:20 -07001712void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1713 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1714 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001715 StackHandleScope<1> hs(self);
1716 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1717}
1718
Andreas Gampebc4d2182016-02-22 10:03:12 -08001719void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1720 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1721 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1722 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1723 result->SetD(bit_cast<double>(long_input));
1724}
1725
Mathieu Chartiere401d142015-04-22 13:56:20 -07001726void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1727 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1728 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001729 result->SetI(args[0]);
1730}
1731
Mathieu Chartiere401d142015-04-22 13:56:20 -07001732void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1733 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1734 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001735 result->SetI(args[0]);
1736}
1737
Mathieu Chartiere401d142015-04-22 13:56:20 -07001738void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1739 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1740 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001741 result->SetL(receiver->Clone(self));
1742}
1743
Mathieu Chartiere401d142015-04-22 13:56:20 -07001744void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1745 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1746 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001747 receiver->NotifyAll(self);
1748}
1749
Mathieu Chartiere401d142015-04-22 13:56:20 -07001750void UnstartedRuntime::UnstartedJNIStringCompareTo(
1751 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1752 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001753 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1754 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001755 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001756 }
1757 result->SetI(receiver->AsString()->CompareTo(rhs));
1758}
1759
Mathieu Chartiere401d142015-04-22 13:56:20 -07001760void UnstartedRuntime::UnstartedJNIStringIntern(
1761 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1762 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001763 result->SetL(receiver->AsString()->Intern());
1764}
1765
Mathieu Chartiere401d142015-04-22 13:56:20 -07001766void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1767 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1768 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001769 StackHandleScope<2> hs(self);
1770 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1771 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1772 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1773}
1774
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1776 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1777 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001778 int32_t length = static_cast<int32_t>(args[1]);
1779 if (length < 0) {
1780 ThrowNegativeArraySizeException(length);
1781 return;
1782 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001783 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001784 Runtime* runtime = Runtime::Current();
1785 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001786 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001787 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001788 CHECK(self->IsExceptionPending());
1789 return;
1790 }
1791 DCHECK(array_class->IsObjectArrayClass());
1792 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1793 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1794 result->SetL(new_array);
1795}
1796
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1798 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1799 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001800 ScopedObjectAccessUnchecked soa(self);
1801 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001802 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001803 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001804 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001805 }
1806}
1807
Mathieu Chartiere401d142015-04-22 13:56:20 -07001808void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1809 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1810 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001811 result->SetZ(JNI_TRUE);
1812}
1813
Mathieu Chartiere401d142015-04-22 13:56:20 -07001814void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1815 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1816 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001817 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1818 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1819 jint expectedValue = args[3];
1820 jint newValue = args[4];
1821 bool success;
1822 if (Runtime::Current()->IsActiveTransaction()) {
1823 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1824 expectedValue, newValue);
1825 } else {
1826 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1827 expectedValue, newValue);
1828 }
1829 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1830}
1831
Narayan Kamath34a316f2016-03-30 13:11:18 +01001832void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1833 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1834 uint32_t* args, JValue* result) {
1835 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1836 if (obj == nullptr) {
1837 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1838 return;
1839 }
1840
1841 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1842 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1843}
1844
Mathieu Chartiere401d142015-04-22 13:56:20 -07001845void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1846 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1847 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001848 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1849 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1850 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1851 if (Runtime::Current()->IsActiveTransaction()) {
1852 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1853 } else {
1854 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1855 }
1856}
1857
Andreas Gampe799681b2015-05-15 19:24:12 -07001858void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001859 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1860 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001861 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1862 Primitive::Type primitive_type = component->GetPrimitiveType();
1863 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1864}
1865
Andreas Gampe799681b2015-05-15 19:24:12 -07001866void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001867 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1868 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001869 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1870 Primitive::Type primitive_type = component->GetPrimitiveType();
1871 result->SetI(Primitive::ComponentSize(primitive_type));
1872}
1873
Andreas Gampedd9d0552015-03-09 12:57:41 -07001874typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001875 size_t arg_size);
1876
Mathieu Chartiere401d142015-04-22 13:56:20 -07001877typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001878 uint32_t* args, JValue* result);
1879
1880static bool tables_initialized_ = false;
1881static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1882static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1883
Andreas Gampe799681b2015-05-15 19:24:12 -07001884void UnstartedRuntime::InitializeInvokeHandlers() {
1885#define UNSTARTED_DIRECT(ShortName, Sig) \
1886 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1887#include "unstarted_runtime_list.h"
1888 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1889#undef UNSTARTED_RUNTIME_DIRECT_LIST
1890#undef UNSTARTED_RUNTIME_JNI_LIST
1891#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001892}
1893
Andreas Gampe799681b2015-05-15 19:24:12 -07001894void UnstartedRuntime::InitializeJNIHandlers() {
1895#define UNSTARTED_JNI(ShortName, Sig) \
1896 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1897#include "unstarted_runtime_list.h"
1898 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1899#undef UNSTARTED_RUNTIME_DIRECT_LIST
1900#undef UNSTARTED_RUNTIME_JNI_LIST
1901#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001902}
1903
Andreas Gampe799681b2015-05-15 19:24:12 -07001904void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001905 CHECK(!tables_initialized_);
1906
Andreas Gampe799681b2015-05-15 19:24:12 -07001907 InitializeInvokeHandlers();
1908 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001909
1910 tables_initialized_ = true;
1911}
1912
Andreas Gampe799681b2015-05-15 19:24:12 -07001913void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1914 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001915 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1916 // problems in core libraries.
1917 CHECK(tables_initialized_);
1918
David Sehr709b0702016-10-13 09:12:37 -07001919 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001920 const auto& iter = invoke_handlers_.find(name);
1921 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001922 // Clear out the result in case it's not zeroed out.
1923 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001924
1925 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1926 self->PushShadowFrame(shadow_frame);
1927
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001928 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001929
1930 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001931 } else {
1932 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001933 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001934 }
1935}
1936
1937// 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 -07001938void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001939 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001940 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001941 const auto& iter = jni_handlers_.find(name);
1942 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001943 // Clear out the result in case it's not zeroed out.
1944 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001945 (*iter->second)(self, method, receiver, args, result);
1946 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001947 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1948 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001949 } else {
David Sehr709b0702016-10-13 09:12:37 -07001950 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001951 "non-transactional runtime";
1952 }
1953}
1954
1955} // namespace interpreter
1956} // namespace art