blob: a0b58ef29ef0939826910136babf2416e8e9c362 [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 Gampe57943812017-12-06 21:39:13 -080029#include <android-base/logging.h>
30#include <android-base/stringprintf.h>
Andreas Gampeaacc25d2015-04-01 14:49:06 -070031
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080033#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "base/macros.h"
36#include "class_linker.h"
37#include "common_throws.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080038#include "dex/descriptors_names.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070039#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080040#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "handle_scope-inl.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000042#include "hidden_api.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070044#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070046#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070047#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010048#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070049#include "mirror/object-inl.h"
50#include "mirror/object_array-inl.h"
51#include "mirror/string-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070052#include "nativehelper/scoped_local_ref.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070053#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070054#include "reflection.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070055#include "thread-inl.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020056#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070057#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070058#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070059
60namespace art {
61namespace interpreter {
62
Andreas Gampe46ee31b2016-12-14 10:11:49 -080063using android::base::StringAppendV;
64using android::base::StringPrintf;
65
Andreas Gampe068b0c02015-03-11 12:44:47 -070066static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020067 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070068 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020069
70static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070071 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020073 va_start(args, fmt);
74 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070075 va_end(args);
76 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020077 va_start(args, fmt);
78 std::string msg;
79 StringAppendV(&msg, fmt, args);
80 va_end(args);
81 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070082 UNREACHABLE();
83 }
84}
85
Andreas Gampe8ce9c302016-04-15 21:24:28 -070086// Restricted support for character upper case / lower case. Only support ASCII, where
87// it's easy. Abort the transaction otherwise.
88static void CharacterLowerUpper(Thread* self,
89 ShadowFrame* shadow_frame,
90 JValue* result,
91 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070092 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070093 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
94
95 // Only ASCII (7-bit).
96 if (!isascii(int_value)) {
97 AbortTransactionOrFail(self,
98 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
99 int_value);
100 return;
101 }
102
103 std::locale c_locale("C");
104 char char_value = static_cast<char>(int_value);
105
106 if (to_lower_case) {
107 result->SetI(std::tolower(char_value, c_locale));
108 } else {
109 result->SetI(std::toupper(char_value, c_locale));
110 }
111}
112
113void UnstartedRuntime::UnstartedCharacterToLowerCase(
114 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
115 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
116}
117
118void UnstartedRuntime::UnstartedCharacterToUpperCase(
119 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
120 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
121}
122
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700123// Helper function to deal with class loading in an unstarted runtime.
124static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
125 Handle<mirror::ClassLoader> class_loader, JValue* result,
126 const std::string& method_name, bool initialize_class,
127 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700128 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800129 CHECK(className != nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700130 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
131 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
132
133 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
134 if (found == nullptr && abort_if_not_found) {
135 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700136 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700137 method_name.c_str(),
138 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700139 }
140 return;
141 }
142 if (found != nullptr && initialize_class) {
143 StackHandleScope<1> hs(self);
144 Handle<mirror::Class> h_class(hs.NewHandle(found));
145 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
146 CHECK(self->IsExceptionPending());
147 return;
148 }
149 }
150 result->SetL(found);
151}
152
153// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
154// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
155// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200156// actually the transaction abort exception. This must not be wrapped, as it signals an
157// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700158static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700159 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700160 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200161 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700162 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200163 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700164 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
165 "ClassNotFoundException");
166 }
167 }
168}
169
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700170static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700171 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700172 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
173 if (param == nullptr) {
174 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
175 return nullptr;
176 }
177 return param->AsString();
178}
179
David Brazdila02cb112018-01-31 11:36:39 +0000180template<typename T>
181static ALWAYS_INLINE bool ShouldBlockAccessToMember(T* member, ShadowFrame* frame)
182 REQUIRES_SHARED(Locks::mutator_lock_) {
183 return hiddenapi::ShouldBlockAccessToMember(
184 member->GetAccessFlags(), frame->GetMethod()->GetDeclaringClass());
185}
186
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800187void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
188 ShadowFrame* shadow_frame,
189 JValue* result,
190 size_t arg_offset,
191 bool long_form,
192 const char* caller) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700193 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
194 if (class_name == nullptr) {
195 return;
196 }
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800197 bool initialize_class;
198 mirror::ClassLoader* class_loader;
199 if (long_form) {
200 initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
201 class_loader = down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
202 } else {
203 initialize_class = true;
204 // TODO: This is really only correct for the boot classpath, and for robustness we should
205 // check the caller.
206 class_loader = nullptr;
207 }
208
209 ScopedObjectAccessUnchecked soa(self);
210 if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
211 AbortTransactionOrFail(self,
212 "Only the boot classloader is supported: %s",
213 mirror::Object::PrettyTypeOf(class_loader).c_str());
214 return;
215 }
216
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700217 StackHandleScope<1> hs(self);
218 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800219 UnstartedRuntimeFindClass(self,
220 h_class_name,
221 ScopedNullHandle<mirror::ClassLoader>(),
222 result,
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800223 caller,
224 initialize_class,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800225 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700226 CheckExceptionGenerateClassNotFound(self);
227}
228
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800229void UnstartedRuntime::UnstartedClassForName(
230 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
231 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
232}
233
Andreas Gampe799681b2015-05-15 19:24:12 -0700234void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700235 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800236 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700237}
238
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000239void UnstartedRuntime::UnstartedClassGetPrimitiveClass(
240 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
241 ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
242 ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(class_name);
243 if (UNLIKELY(klass == nullptr)) {
244 DCHECK(self->IsExceptionPending());
245 AbortTransactionOrFail(self,
246 "Class.getPrimitiveClass() failed: %s",
247 self->GetException()->GetDetailMessage()->ToModifiedUtf8().c_str());
248 return;
249 }
250 result->SetL(klass);
251}
252
Andreas Gampe799681b2015-05-15 19:24:12 -0700253void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800255 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700256}
257
Andreas Gampe799681b2015-05-15 19:24:12 -0700258void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700259 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
260 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700261 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
262 if (param == nullptr) {
263 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
264 return;
265 }
266 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700267 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700268
269 // Check that it's not null.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800270 if (h_klass == nullptr) {
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700271 AbortTransactionOrFail(self, "Class reference is null for newInstance");
272 return;
273 }
274
275 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
276 if (Runtime::Current()->IsActiveTransaction()) {
277 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200278 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700279 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700280 return;
281 }
282 }
283
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700284 // There are two situations in which we'll abort this run.
285 // 1) If the class isn't yet initialized and initialization fails.
286 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
287 // Note that 2) could likely be handled here, but for safety abort the transaction.
288 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700289 auto* cl = Runtime::Current()->GetClassLinker();
290 if (cl->EnsureInitialized(self, h_klass, true, true)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000291 ArtMethod* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
David Brazdila02cb112018-01-31 11:36:39 +0000292 if (cons != nullptr && ShouldBlockAccessToMember(cons, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000293 cons = nullptr;
294 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700295 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700296 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800297 CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700298 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700299 if (!self->IsExceptionPending()) {
300 result->SetL(h_obj.Get());
301 ok = true;
302 }
303 } else {
304 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
305 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700306 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700307 }
308 }
309 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700310 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700311 h_klass->PrettyClass().c_str(),
312 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700313 }
314}
315
Andreas Gampe799681b2015-05-15 19:24:12 -0700316void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700318 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
319 // going the reflective Dex way.
320 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
321 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700322 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700323 for (ArtField& field : klass->GetIFields()) {
324 if (name2->Equals(field.GetName())) {
325 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700326 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700327 }
328 }
329 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700330 for (ArtField& field : klass->GetSFields()) {
331 if (name2->Equals(field.GetName())) {
332 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700333 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700334 }
335 }
336 }
David Brazdila02cb112018-01-31 11:36:39 +0000337 if (found != nullptr && ShouldBlockAccessToMember(found, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000338 found = nullptr;
339 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700340 if (found == nullptr) {
341 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
342 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700343 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700344 return;
345 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700346 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700347 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700348 mirror::Field* field;
349 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700350 if (pointer_size == PointerSize::k64) {
351 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
352 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700353 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700354 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
355 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700356 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700357 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700358 if (pointer_size == PointerSize::k64) {
359 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
360 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700361 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700362 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
363 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700364 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700365 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700366 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700367}
368
Andreas Gampebc4d2182016-02-22 10:03:12 -0800369// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
370void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
371 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
372 // Special managed code cut-out to allow method lookup in a un-started runtime.
373 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
374 if (klass == nullptr) {
375 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
376 return;
377 }
378 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
379 mirror::ObjectArray<mirror::Class>* args =
380 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700381 Runtime* runtime = Runtime::Current();
382 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700383 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700384 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700385 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700386 if (pointer_size == PointerSize::k64) {
387 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
388 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700389 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700390 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
391 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700392 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800393 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700394 if (pointer_size == PointerSize::k64) {
395 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
396 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700397 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700398 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
399 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700400 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800401 }
David Brazdila02cb112018-01-31 11:36:39 +0000402 if (method != nullptr && ShouldBlockAccessToMember(method->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000403 method = nullptr;
404 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700405 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800406}
407
Andreas Gampe6039e562016-04-05 18:18:43 -0700408// Special managed code cut-out to allow constructor lookup in a un-started runtime.
409void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
410 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
411 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
412 if (klass == nullptr) {
413 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
414 return;
415 }
416 mirror::ObjectArray<mirror::Class>* args =
417 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700418 Runtime* runtime = Runtime::Current();
419 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700420 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700421 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700422 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700423 if (pointer_size == PointerSize::k64) {
424 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
425 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700426 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700427 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
428 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700429 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700430 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700431 if (pointer_size == PointerSize::k64) {
432 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
433 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700434 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700435 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
436 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700437 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700438 }
David Brazdil5a61bb72018-01-19 16:59:46 +0000439 if (constructor != nullptr &&
David Brazdila02cb112018-01-31 11:36:39 +0000440 ShouldBlockAccessToMember(constructor->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000441 constructor = nullptr;
442 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700443 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700444}
445
Andreas Gampeae78c262017-02-01 20:40:44 -0800446void UnstartedRuntime::UnstartedClassGetDeclaringClass(
447 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
448 StackHandleScope<1> hs(self);
449 Handle<mirror::Class> klass(hs.NewHandle(
450 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
451 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
452 result->SetL(nullptr);
453 return;
454 }
455 // Return null for anonymous classes.
456 JValue is_anon_result;
457 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
458 if (is_anon_result.GetZ() != 0) {
459 result->SetL(nullptr);
460 return;
461 }
462 result->SetL(annotations::GetDeclaringClass(klass));
463}
464
Andreas Gampe633750c2016-02-19 10:49:50 -0800465void UnstartedRuntime::UnstartedClassGetEnclosingClass(
466 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
467 StackHandleScope<1> hs(self);
468 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
469 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
470 result->SetL(nullptr);
471 }
David Sehr9323e6e2016-09-13 08:58:35 -0700472 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800473}
474
Andreas Gampe715fdc22016-04-18 17:07:30 -0700475void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
476 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
477 StackHandleScope<1> hs(self);
478 Handle<mirror::Class> klass(hs.NewHandle(
479 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
480 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
481 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
482}
483
Andreas Gampe9486a162017-02-16 15:17:47 -0800484void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
485 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
486 StackHandleScope<1> hs(self);
487 Handle<mirror::Class> klass(hs.NewHandle(
488 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
489
490 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
491 result->SetL(nullptr);
492 return;
493 }
494
495 result->SetL(annotations::GetSignatureAnnotationForClass(klass));
496}
497
Andreas Gampeae78c262017-02-01 20:40:44 -0800498void UnstartedRuntime::UnstartedClassIsAnonymousClass(
499 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
500 StackHandleScope<1> hs(self);
501 Handle<mirror::Class> klass(hs.NewHandle(
502 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
503 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
504 result->SetZ(false);
505 return;
506 }
507 mirror::String* class_name = nullptr;
508 if (!annotations::GetInnerClass(klass, &class_name)) {
509 result->SetZ(false);
510 return;
511 }
512 result->SetZ(class_name == nullptr);
513}
514
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700515static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
516 const char* entry_name,
517 size_t* size,
518 std::string* error_msg) {
519 CHECK(size != nullptr);
520
521 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
522 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800523 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700524 }
525 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
526 if (zip_entry == nullptr) {
527 return nullptr;
528 }
529 std::unique_ptr<MemMap> tmp_map(
530 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
531 if (tmp_map == nullptr) {
532 return nullptr;
533 }
534
535 // OK, from here everything seems fine.
536 *size = zip_entry->GetUncompressedLength();
537 return tmp_map;
538}
539
540static void GetResourceAsStream(Thread* self,
541 ShadowFrame* shadow_frame,
542 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700543 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700544 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
545 if (resource_obj == nullptr) {
546 AbortTransactionOrFail(self, "null name for getResourceAsStream");
547 return;
548 }
549 CHECK(resource_obj->IsString());
550 mirror::String* resource_name = resource_obj->AsString();
551
552 std::string resource_name_str = resource_name->ToModifiedUtf8();
553 if (resource_name_str.empty() || resource_name_str == "/") {
554 AbortTransactionOrFail(self,
555 "Unsupported name %s for getResourceAsStream",
556 resource_name_str.c_str());
557 return;
558 }
559 const char* resource_cstr = resource_name_str.c_str();
560 if (resource_cstr[0] == '/') {
561 resource_cstr++;
562 }
563
564 Runtime* runtime = Runtime::Current();
565
566 std::vector<std::string> split;
567 Split(runtime->GetBootClassPathString(), ':', &split);
568 if (split.empty()) {
569 AbortTransactionOrFail(self,
570 "Boot classpath not set or split error:: %s",
571 runtime->GetBootClassPathString().c_str());
572 return;
573 }
574
575 std::unique_ptr<MemMap> mem_map;
576 size_t map_size;
577 std::string last_error_msg; // Only store the last message (we could concatenate).
578
579 for (const std::string& jar_file : split) {
580 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
581 if (mem_map != nullptr) {
582 break;
583 }
584 }
585
586 if (mem_map == nullptr) {
587 // Didn't find it. There's a good chance this will be the same at runtime, but still
588 // conservatively abort the transaction here.
589 AbortTransactionOrFail(self,
590 "Could not find resource %s. Last error was %s.",
591 resource_name_str.c_str(),
592 last_error_msg.c_str());
593 return;
594 }
595
596 StackHandleScope<3> hs(self);
597
598 // Create byte array for content.
599 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800600 if (h_array == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700601 AbortTransactionOrFail(self, "Could not find/create byte array class");
602 return;
603 }
604 // Copy in content.
605 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
606 // Be proactive releasing memory.
Andreas Gampeeac4f282017-04-26 21:07:04 -0700607 mem_map.reset();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700608
609 // Create a ByteArrayInputStream.
610 Handle<mirror::Class> h_class(hs.NewHandle(
611 runtime->GetClassLinker()->FindClass(self,
612 "Ljava/io/ByteArrayInputStream;",
613 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800614 if (h_class == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700615 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
616 return;
617 }
618 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
619 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
620 return;
621 }
622
623 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800624 if (h_obj == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700625 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
626 return;
627 }
628
629 auto* cl = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100630 ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700631 if (constructor == nullptr) {
632 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
633 return;
634 }
635
636 uint32_t args[1];
637 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
638 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
639
640 if (self->IsExceptionPending()) {
641 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
642 return;
643 }
644
645 result->SetL(h_obj.Get());
646}
647
648void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
649 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
650 {
651 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
652 CHECK(this_obj != nullptr);
653 CHECK(this_obj->IsClassLoader());
654
655 StackHandleScope<1> hs(self);
656 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
657
658 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
659 this_classloader_class.Get()) {
660 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700661 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700662 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700663 return;
664 }
665 }
666
667 GetResourceAsStream(self, shadow_frame, result, arg_offset);
668}
669
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800670void UnstartedRuntime::UnstartedConstructorNewInstance0(
671 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
672 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
673 StackHandleScope<4> hs(self);
674 Handle<mirror::Constructor> m = hs.NewHandle(
675 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
676 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
677 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
678 shadow_frame->GetVRegReference(arg_offset + 1)));
679 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
680 if (UNLIKELY(c->IsAbstract())) {
681 AbortTransactionOrFail(self, "Cannot handle abstract classes");
682 return;
683 }
684 // Verify that we can access the class.
685 if (!m->IsAccessible() && !c->IsPublic()) {
686 // Go 2 frames back, this method is always called from newInstance0, which is called from
687 // Constructor.newInstance(Object... args).
688 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
689 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
690 // access checks anyways. TODO: Investigate if this the correct behavior.
691 if (caller != nullptr && !caller->CanAccess(c.Get())) {
692 AbortTransactionOrFail(self, "Cannot access class");
693 return;
694 }
695 }
696 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
697 DCHECK(self->IsExceptionPending());
698 return;
699 }
700 if (c->IsClassClass()) {
701 AbortTransactionOrFail(self, "new Class() is not supported");
702 return;
703 }
704
705 // String constructor is replaced by a StringFactory method in InvokeMethod.
706 if (c->IsStringClass()) {
707 // We don't support strings.
708 AbortTransactionOrFail(self, "String construction is not supported");
709 return;
710 }
711
712 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
713 if (receiver == nullptr) {
714 AbortTransactionOrFail(self, "Could not allocate");
715 return;
716 }
717
718 // It's easier to use reflection to make the call, than create the uint32_t array.
719 {
720 ScopedObjectAccessUnchecked soa(self);
721 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
722 soa.AddLocalReference<jobject>(m.Get()));
723 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
724 soa.AddLocalReference<jobject>(receiver.Get()));
725 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
726 soa.AddLocalReference<jobject>(args.Get()));
727 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
728 }
729 if (self->IsExceptionPending()) {
730 AbortTransactionOrFail(self, "Failed running constructor");
731 } else {
732 result->SetL(receiver.Get());
733 }
734}
735
Andreas Gampe799681b2015-05-15 19:24:12 -0700736void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700737 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700738 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
739 mirror::ClassLoader* class_loader =
740 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
741 StackHandleScope<2> hs(self);
742 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
743 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
744 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
745 "VMClassLoader.findLoadedClass", false, false);
746 // This might have an error pending. But semantics are to just return null.
747 if (self->IsExceptionPending()) {
748 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700749 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700750 if (type != "java.lang.InternalError") {
751 self->ClearException();
752 }
753 }
754}
755
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700756// Arraycopy emulation.
757// Note: we can't use any fast copy functions, as they are not available under transaction.
758
759template <typename T>
760static void PrimitiveArrayCopy(Thread* self,
761 mirror::Array* src_array, int32_t src_pos,
762 mirror::Array* dst_array, int32_t dst_pos,
763 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700764 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700765 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700766 AbortTransactionOrFail(self,
767 "Types mismatched in arraycopy: %s vs %s.",
768 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700769 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700770 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700771 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700772 return;
773 }
774 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
775 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
776 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
777 if (copy_forward) {
778 for (int32_t i = 0; i < length; ++i) {
779 dst->Set(dst_pos + i, src->Get(src_pos + i));
780 }
781 } else {
782 for (int32_t i = 1; i <= length; ++i) {
783 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
784 }
785 }
786}
787
Andreas Gampe799681b2015-05-15 19:24:12 -0700788void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700789 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700790 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700791 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
792 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700793 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700794
Andreas Gampe85a098a2016-03-31 13:30:53 -0700795 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
796 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
797 // Null checking. For simplicity, abort transaction.
798 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700799 AbortTransactionOrFail(self, "src is null in arraycopy.");
800 return;
801 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700802 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700803 AbortTransactionOrFail(self, "dst is null in arraycopy.");
804 return;
805 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700806 // Test for arrayness. Throw ArrayStoreException.
807 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
808 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
809 return;
810 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700811
Andreas Gampe85a098a2016-03-31 13:30:53 -0700812 mirror::Array* src_array = src_obj->AsArray();
813 mirror::Array* dst_array = dst_obj->AsArray();
814
815 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700816 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
817 UNLIKELY(src_pos > src_array->GetLength() - length) ||
818 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700819 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700820 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
821 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
822 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700823 return;
824 }
825
826 // Type checking.
827 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
828 GetComponentType();
829
830 if (!src_type->IsPrimitive()) {
831 // Check that the second type is not primitive.
832 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
833 GetComponentType();
834 if (trg_type->IsPrimitiveInt()) {
835 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700836 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700837 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700838 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700839 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700840 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700841 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700842
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700843 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
844 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
845 if (src == dst) {
846 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700847 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700848 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
849 if (copy_forward) {
850 for (int32_t i = 0; i < length; ++i) {
851 dst->Set(dst_pos + i, src->Get(src_pos + i));
852 }
853 } else {
854 for (int32_t i = 1; i <= length; ++i) {
855 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
856 }
857 }
858 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700859 // We're being lazy here. Optimally this could be a memcpy (if component types are
860 // assignable), but the ObjectArray implementation doesn't support transactions. The
861 // checking version, however, does.
862 if (Runtime::Current()->IsActiveTransaction()) {
863 dst->AssignableCheckingMemcpy<true>(
864 dst_pos, src, src_pos, length, true /* throw_exception */);
865 } else {
866 dst->AssignableCheckingMemcpy<false>(
867 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700868 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700869 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700870 } else if (src_type->IsPrimitiveByte()) {
871 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700872 } else if (src_type->IsPrimitiveChar()) {
873 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
874 } else if (src_type->IsPrimitiveInt()) {
875 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700876 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700877 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700878 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700879 }
880}
881
Andreas Gampe5c9af612016-04-05 14:16:10 -0700882void UnstartedRuntime::UnstartedSystemArraycopyByte(
883 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
884 // Just forward.
885 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
886}
887
Andreas Gampe799681b2015-05-15 19:24:12 -0700888void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700889 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700890 // Just forward.
891 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
892}
893
894void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700895 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700896 // Just forward.
897 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
898}
899
Narayan Kamath34a316f2016-03-30 13:11:18 +0100900void UnstartedRuntime::UnstartedSystemGetSecurityManager(
901 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
902 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
903 result->SetL(nullptr);
904}
905
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700906static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
907
908static void GetSystemProperty(Thread* self,
909 ShadowFrame* shadow_frame,
910 JValue* result,
911 size_t arg_offset,
912 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700913 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700914 StackHandleScope<4> hs(self);
915 Handle<mirror::String> h_key(
916 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800917 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700918 AbortTransactionOrFail(self, "getProperty key was null");
919 return;
920 }
921
922 // This is overall inefficient, but reflecting the values here is not great, either. So
923 // for simplicity, and with the assumption that the number of getProperty calls is not
924 // too great, just iterate each time.
925
926 // Get the storage class.
927 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
928 Handle<mirror::Class> h_props_class(hs.NewHandle(
929 class_linker->FindClass(self,
930 "Ljava/lang/AndroidHardcodedSystemProperties;",
931 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800932 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700933 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
934 return;
935 }
936 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
937 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
938 return;
939 }
940
941 // Get the storage array.
942 ArtField* static_properties =
943 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
944 "[[Ljava/lang/String;");
945 if (static_properties == nullptr) {
946 AbortTransactionOrFail(self,
947 "Could not find %s field",
948 kAndroidHardcodedSystemPropertiesFieldName);
949 return;
950 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700951 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
952 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
953 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800954 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700955 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
956 return;
957 }
958
959 // Iterate over it.
960 const int32_t prop_count = h_2string_array->GetLength();
961 // Use the third handle as mutable.
962 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
963 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
964 for (int32_t i = 0; i < prop_count; ++i) {
965 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800966 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700967 h_string_array->GetLength() != 2 ||
968 h_string_array->Get(0) == nullptr) {
969 AbortTransactionOrFail(self,
970 "Unexpected content of %s",
971 kAndroidHardcodedSystemPropertiesFieldName);
972 return;
973 }
974 if (h_key->Equals(h_string_array->Get(0))) {
975 // Found a value.
976 if (h_string_array->Get(1) == nullptr && is_default_version) {
977 // Null is being delegated to the default map, and then resolved to the given default value.
978 // As there's no default map, return the given value.
979 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
980 } else {
981 result->SetL(h_string_array->Get(1));
982 }
983 return;
984 }
985 }
986
987 // Key is not supported.
988 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
989}
990
991void UnstartedRuntime::UnstartedSystemGetProperty(
992 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
993 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
994}
995
996void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
997 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
998 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
999}
1000
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001001static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
1002 REQUIRES_SHARED(Locks::mutator_lock_) {
1003 if (shadow_frame->GetLink() == nullptr) {
1004 return "<no caller>";
1005 }
1006 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1007}
1008
1009static bool CheckCallers(ShadowFrame* shadow_frame,
1010 std::initializer_list<std::string> allowed_call_stack)
1011 REQUIRES_SHARED(Locks::mutator_lock_) {
1012 for (const std::string& allowed_caller : allowed_call_stack) {
1013 if (shadow_frame->GetLink() == nullptr) {
1014 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001015 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001016
1017 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1018 if (allowed_caller != found_caller) {
1019 return false;
1020 }
1021
1022 shadow_frame = shadow_frame->GetLink();
1023 }
1024 return true;
1025}
1026
1027static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
1028 REQUIRES_SHARED(Locks::mutator_lock_) {
1029 // Find the requested class.
1030 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1031 ObjPtr<mirror::Class> klass =
1032 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1033 if (klass == nullptr) {
1034 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1035 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001036 }
1037
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001038 StackHandleScope<2> hs(self);
1039 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1040 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001041 if (h_obj != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01001042 ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001043 if (init_method == nullptr) {
1044 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1045 return nullptr;
1046 } else {
1047 JValue invoke_result;
1048 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1049 if (!self->IsExceptionPending()) {
1050 return h_obj.Get();
1051 }
1052 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1053 }
1054 }
1055 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1056 return nullptr;
1057}
1058
1059void UnstartedRuntime::UnstartedThreadLocalGet(
1060 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1061 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1062 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1063 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1064 } else {
1065 AbortTransactionOrFail(self,
1066 "ThreadLocal.get() does not support %s",
1067 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001068 }
1069}
1070
Andreas Gampebad529d2017-02-13 18:52:10 -08001071void UnstartedRuntime::UnstartedThreadCurrentThread(
1072 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1073 if (CheckCallers(shadow_frame,
1074 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1075 "java.lang.String, long)",
1076 "void java.lang.Thread.<init>()",
1077 "void java.util.logging.LogManager$Cleaner.<init>("
1078 "java.util.logging.LogManager)" })) {
1079 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1080 // Thread constructor only asks for the current thread to set up defaults and add the
1081 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1082 // these purposes.
1083 Runtime::Current()->InitThreadGroups(self);
1084 jobject main_peer =
1085 self->CreateCompileTimePeer(self->GetJniEnv(),
1086 "main",
1087 false,
1088 Runtime::Current()->GetMainThreadGroup());
1089 if (main_peer == nullptr) {
1090 AbortTransactionOrFail(self, "Failed allocating peer");
1091 return;
1092 }
1093
1094 result->SetL(self->DecodeJObject(main_peer));
1095 self->GetJniEnv()->DeleteLocalRef(main_peer);
1096 } else {
1097 AbortTransactionOrFail(self,
1098 "Thread.currentThread() does not support %s",
1099 GetImmediateCaller(shadow_frame).c_str());
1100 }
1101}
1102
1103void UnstartedRuntime::UnstartedThreadGetNativeState(
1104 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1105 if (CheckCallers(shadow_frame,
1106 { "java.lang.Thread$State java.lang.Thread.getState()",
1107 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1108 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1109 "java.lang.String, long)",
1110 "void java.lang.Thread.<init>()",
1111 "void java.util.logging.LogManager$Cleaner.<init>("
1112 "java.util.logging.LogManager)" })) {
1113 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1114 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1115 constexpr int32_t kJavaRunnable = 1;
1116 result->SetI(kJavaRunnable);
1117 } else {
1118 AbortTransactionOrFail(self,
1119 "Thread.getNativeState() does not support %s",
1120 GetImmediateCaller(shadow_frame).c_str());
1121 }
1122}
1123
Sergio Giro83261202016-04-11 20:49:20 +01001124void UnstartedRuntime::UnstartedMathCeil(
1125 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001126 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001127}
1128
1129void UnstartedRuntime::UnstartedMathFloor(
1130 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001131 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001132}
1133
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001134void UnstartedRuntime::UnstartedMathSin(
1135 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1136 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1137}
1138
1139void UnstartedRuntime::UnstartedMathCos(
1140 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1141 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1142}
1143
1144void UnstartedRuntime::UnstartedMathPow(
1145 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1146 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1147 shadow_frame->GetVRegDouble(arg_offset + 2)));
1148}
1149
Andreas Gampe799681b2015-05-15 19:24:12 -07001150void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001151 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001152 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1153 result->SetI(obj->IdentityHashCode());
1154}
1155
Andreas Gampe799681b2015-05-15 19:24:12 -07001156void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001157 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001158 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001159 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001160}
1161
Andreas Gampedd9d0552015-03-09 12:57:41 -07001162static void UnstartedMemoryPeek(
1163 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1164 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1165 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1166 // aborting the transaction.
1167
1168 switch (type) {
1169 case Primitive::kPrimByte: {
1170 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1171 return;
1172 }
1173
1174 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001175 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1176 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001177 return;
1178 }
1179
1180 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001181 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1182 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001183 return;
1184 }
1185
1186 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001187 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1188 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001189 return;
1190 }
1191
1192 case Primitive::kPrimBoolean:
1193 case Primitive::kPrimChar:
1194 case Primitive::kPrimFloat:
1195 case Primitive::kPrimDouble:
1196 case Primitive::kPrimVoid:
1197 case Primitive::kPrimNot:
1198 LOG(FATAL) << "Not in the Memory API: " << type;
1199 UNREACHABLE();
1200 }
1201 LOG(FATAL) << "Should not reach here";
1202 UNREACHABLE();
1203}
1204
Andreas Gampe799681b2015-05-15 19:24:12 -07001205void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001206 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001207 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1208}
1209
1210void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001211 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001212 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1213}
1214
1215void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001216 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001217 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1218}
1219
1220void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001221 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001222 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001223}
1224
1225static void UnstartedMemoryPeekArray(
1226 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001227 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001228 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1229 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1230 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001231 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001232 return;
1233 }
1234 mirror::Array* array = obj->AsArray();
1235
1236 int offset = shadow_frame->GetVReg(arg_offset + 3);
1237 int count = shadow_frame->GetVReg(arg_offset + 4);
1238 if (offset < 0 || offset + count > array->GetLength()) {
1239 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1240 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001241 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001242 return;
1243 }
1244
1245 switch (type) {
1246 case Primitive::kPrimByte: {
1247 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1248 mirror::ByteArray* byte_array = array->AsByteArray();
1249 for (int32_t i = 0; i < count; ++i, ++address) {
1250 byte_array->SetWithoutChecks<true>(i + offset, *address);
1251 }
1252 return;
1253 }
1254
1255 case Primitive::kPrimShort:
1256 case Primitive::kPrimInt:
1257 case Primitive::kPrimLong:
1258 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1259 UNREACHABLE();
1260
1261 case Primitive::kPrimBoolean:
1262 case Primitive::kPrimChar:
1263 case Primitive::kPrimFloat:
1264 case Primitive::kPrimDouble:
1265 case Primitive::kPrimVoid:
1266 case Primitive::kPrimNot:
1267 LOG(FATAL) << "Not in the Memory API: " << type;
1268 UNREACHABLE();
1269 }
1270 LOG(FATAL) << "Should not reach here";
1271 UNREACHABLE();
1272}
1273
Andreas Gampe799681b2015-05-15 19:24:12 -07001274void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001275 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001276 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001277}
1278
Kenny Root1c9e61c2015-05-14 15:58:17 -07001279// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001280void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001281 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001282 jint start = shadow_frame->GetVReg(arg_offset + 1);
1283 jint end = shadow_frame->GetVReg(arg_offset + 2);
1284 jint index = shadow_frame->GetVReg(arg_offset + 4);
1285 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1286 if (string == nullptr) {
1287 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1288 return;
1289 }
Kenny Root57f91e82015-05-14 15:58:17 -07001290 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001291 DCHECK_LE(start, end);
1292 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001293 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001294 Handle<mirror::CharArray> h_char_array(
1295 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001296 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001297 DCHECK_LE(index, h_char_array->GetLength());
1298 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001299 string->GetChars(start, end, h_char_array, index);
1300}
1301
1302// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001303void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001304 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001305 jint index = shadow_frame->GetVReg(arg_offset + 1);
1306 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1307 if (string == nullptr) {
1308 AbortTransactionOrFail(self, "String.charAt with null object");
1309 return;
1310 }
1311 result->SetC(string->CharAt(index));
1312}
1313
Vladimir Marko92907f32017-02-20 14:08:30 +00001314// This allows creating String objects with replaced characters during compilation.
1315// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1316void UnstartedRuntime::UnstartedStringDoReplace(
1317 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1318 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1319 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001320 StackHandleScope<1> hs(self);
1321 Handle<mirror::String> string =
1322 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
Kenny Root57f91e82015-05-14 15:58:17 -07001323 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001324 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001325 return;
1326 }
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001327 result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001328}
1329
Kenny Root1c9e61c2015-05-14 15:58:17 -07001330// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001331void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001332 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001333 jint offset = shadow_frame->GetVReg(arg_offset);
1334 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1335 DCHECK_GE(char_count, 0);
1336 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001337 Handle<mirror::CharArray> h_char_array(
1338 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001339 Runtime* runtime = Runtime::Current();
1340 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1341 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1342}
1343
1344// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001345void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001346 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001347 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1348 if (to_copy == nullptr) {
1349 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1350 return;
1351 }
1352 StackHandleScope<1> hs(self);
1353 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1354 Runtime* runtime = Runtime::Current();
1355 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1356 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1357 allocator));
1358}
1359
Andreas Gampe799681b2015-05-15 19:24:12 -07001360void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001361 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001362 jint start = shadow_frame->GetVReg(arg_offset + 1);
1363 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001364 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001365 DCHECK_GE(length, 0);
1366 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001367 Handle<mirror::String> h_string(
1368 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001369 DCHECK_LE(start, h_string->GetLength());
1370 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001371 Runtime* runtime = Runtime::Current();
1372 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1373 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1374}
1375
Kenny Root57f91e82015-05-14 15:58:17 -07001376// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001377void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001378 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001379 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001380 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1381 if (string == nullptr) {
1382 AbortTransactionOrFail(self, "String.charAt with null object");
1383 return;
1384 }
1385 result->SetL(string->ToCharArray(self));
1386}
1387
Andreas Gampebc4d2182016-02-22 10:03:12 -08001388// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1389void UnstartedRuntime::UnstartedReferenceGetReferent(
1390 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001391 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001392 shadow_frame->GetVRegReference(arg_offset));
1393 if (ref == nullptr) {
1394 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1395 return;
1396 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001397 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001398 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1399 result->SetL(referent);
1400}
1401
1402// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1403// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1404// where we can predict the behavior (somewhat).
1405// Note: this is required (instead of lazy initialization) as these classes are used in the static
1406// initialization of other classes, so will *use* the value.
1407void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1408 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001409 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001410 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1411 // 8 as a conservative upper approximation.
1412 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001413 } else if (CheckCallers(shadow_frame,
1414 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001415 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1416 // a good upper bound.
1417 // TODO: Consider resetting in the zygote?
1418 result->SetI(8);
1419 } else {
1420 // Not supported.
1421 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1422 }
1423}
1424
1425// This allows accessing ConcurrentHashMap/SynchronousQueue.
1426
1427void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1428 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1429 // Argument 0 is the Unsafe instance, skip.
1430 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1431 if (obj == nullptr) {
1432 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1433 return;
1434 }
1435 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1436 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1437 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001438 bool success;
1439 // Check whether we're in a transaction, call accordingly.
1440 if (Runtime::Current()->IsActiveTransaction()) {
1441 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1442 expectedValue,
1443 newValue);
1444 } else {
1445 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1446 expectedValue,
1447 newValue);
1448 }
1449 result->SetZ(success ? 1 : 0);
1450}
1451
1452void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1453 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1454 // Argument 0 is the Unsafe instance, skip.
1455 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1456 if (obj == nullptr) {
1457 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1458 return;
1459 }
1460 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1461 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1462 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1463
1464 // Must use non transactional mode.
1465 if (kUseReadBarrier) {
1466 // Need to make sure the reference stored in the field is a to-space one before attempting the
1467 // CAS or the CAS could fail incorrectly.
1468 mirror::HeapReference<mirror::Object>* field_addr =
1469 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1470 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001471 ReadBarrier::Barrier<
1472 mirror::Object,
1473 /* kIsVolatile */ false,
1474 kWithReadBarrier,
1475 /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001476 obj,
1477 MemberOffset(offset),
1478 field_addr);
1479 }
1480 bool success;
1481 // Check whether we're in a transaction, call accordingly.
1482 if (Runtime::Current()->IsActiveTransaction()) {
1483 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1484 expected_value,
1485 newValue);
1486 } else {
1487 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1488 expected_value,
1489 newValue);
1490 }
1491 result->SetZ(success ? 1 : 0);
1492}
1493
1494void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1495 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001496 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001497 // Argument 0 is the Unsafe instance, skip.
1498 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1499 if (obj == nullptr) {
1500 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1501 return;
1502 }
1503 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1504 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1505 result->SetL(value);
1506}
1507
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001508void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1509 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001510 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001511 // Argument 0 is the Unsafe instance, skip.
1512 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1513 if (obj == nullptr) {
1514 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1515 return;
1516 }
1517 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1518 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1519 if (Runtime::Current()->IsActiveTransaction()) {
1520 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1521 } else {
1522 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1523 }
1524}
1525
Andreas Gampebc4d2182016-02-22 10:03:12 -08001526void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1527 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001528 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001529 // Argument 0 is the Unsafe instance, skip.
1530 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1531 if (obj == nullptr) {
1532 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1533 return;
1534 }
1535 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1536 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1537 QuasiAtomic::ThreadFenceRelease();
1538 if (Runtime::Current()->IsActiveTransaction()) {
1539 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1540 } else {
1541 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1542 }
1543}
1544
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001545// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1546// of correctly handling the corner cases.
1547void UnstartedRuntime::UnstartedIntegerParseInt(
1548 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001549 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001550 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1551 if (obj == nullptr) {
1552 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1553 return;
1554 }
1555
1556 std::string string_value = obj->AsString()->ToModifiedUtf8();
1557 if (string_value.empty()) {
1558 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1559 return;
1560 }
1561
1562 const char* c_str = string_value.c_str();
1563 char *end;
1564 // Can we set errno to 0? Is this always a variable, and not a macro?
1565 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1566 int64_t l = strtol(c_str, &end, 10);
1567
1568 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1569 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1570 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1571 return;
1572 }
1573 if (l == 0) {
1574 // Check whether the string wasn't exactly zero.
1575 if (string_value != "0") {
1576 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1577 return;
1578 }
1579 } else if (*end != '\0') {
1580 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1581 return;
1582 }
1583
1584 result->SetI(static_cast<int32_t>(l));
1585}
1586
1587// A cutout for Long.parseLong.
1588//
1589// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1590// well.
1591void UnstartedRuntime::UnstartedLongParseLong(
1592 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001593 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001594 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1595 if (obj == nullptr) {
1596 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1597 return;
1598 }
1599
1600 std::string string_value = obj->AsString()->ToModifiedUtf8();
1601 if (string_value.empty()) {
1602 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1603 return;
1604 }
1605
1606 const char* c_str = string_value.c_str();
1607 char *end;
1608 // Can we set errno to 0? Is this always a variable, and not a macro?
1609 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1610 int64_t l = strtol(c_str, &end, 10);
1611
1612 // Note: comparing against int32_t min/max is intentional here.
1613 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1614 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1615 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1616 return;
1617 }
1618 if (l == 0) {
1619 // Check whether the string wasn't exactly zero.
1620 if (string_value != "0") {
1621 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1622 return;
1623 }
1624 } else if (*end != '\0') {
1625 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1626 return;
1627 }
1628
1629 result->SetJ(l);
1630}
1631
Andreas Gampe715fdc22016-04-18 17:07:30 -07001632void UnstartedRuntime::UnstartedMethodInvoke(
1633 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001634 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001635 JNIEnvExt* env = self->GetJniEnv();
1636 ScopedObjectAccessUnchecked soa(self);
1637
Mathieu Chartier8778c522016-10-04 19:06:30 -07001638 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001639 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001640 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001641
Mathieu Chartier8778c522016-10-04 19:06:30 -07001642 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001643 ScopedLocalRef<jobject> java_receiver(env,
1644 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1645
Mathieu Chartier8778c522016-10-04 19:06:30 -07001646 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001647 ScopedLocalRef<jobject> java_args(env,
1648 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1649
1650 ScopedLocalRef<jobject> result_jobj(env,
1651 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1652
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001653 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001654
1655 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1656 // InvocationTargetExceptions.
1657 if (self->IsExceptionPending()) {
1658 AbortTransactionOrFail(self, "Failed Method.invoke");
1659 }
1660}
1661
Nicolas Geoffrayece2f7c2017-03-08 16:11:23 +00001662void UnstartedRuntime::UnstartedSystemIdentityHashCode(
1663 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1664 REQUIRES_SHARED(Locks::mutator_lock_) {
1665 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1666 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1667}
Andreas Gampebc4d2182016-02-22 10:03:12 -08001668
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001669// Checks whether the runtime is s64-bit. This is needed for the clinit of
1670// java.lang.invoke.VarHandle clinit. The clinit determines sets of
1671// available VarHandle accessors and these differ based on machine
1672// word size.
1673void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
1674 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1675 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
1676 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1677 jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
1678 result->SetZ(is64bit);
1679}
1680
Mathieu Chartiere401d142015-04-22 13:56:20 -07001681void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1682 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1683 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001684 int32_t length = args[1];
1685 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001686 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001687 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001688 ObjPtr<mirror::Class> array_class =
1689 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001690 DCHECK(array_class != nullptr);
1691 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001692 result->SetL(mirror::Array::Alloc<true, true>(self,
1693 array_class,
1694 length,
1695 array_class->GetComponentSizeShift(),
1696 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001697}
1698
Mathieu Chartiere401d142015-04-22 13:56:20 -07001699void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1700 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1701 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001702 result->SetL(nullptr);
1703}
1704
Mathieu Chartiere401d142015-04-22 13:56:20 -07001705void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1706 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1707 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001708 NthCallerVisitor visitor(self, 3);
1709 visitor.WalkStack();
1710 if (visitor.caller != nullptr) {
1711 result->SetL(visitor.caller->GetDeclaringClass());
1712 }
1713}
1714
Mathieu Chartiere401d142015-04-22 13:56:20 -07001715void UnstartedRuntime::UnstartedJNIMathLog(
1716 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1717 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001718 JValue value;
1719 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1720 result->SetD(log(value.GetD()));
1721}
1722
Mathieu Chartiere401d142015-04-22 13:56:20 -07001723void UnstartedRuntime::UnstartedJNIMathExp(
1724 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1725 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001726 JValue value;
1727 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1728 result->SetD(exp(value.GetD()));
1729}
1730
Andreas Gampebc4d2182016-02-22 10:03:12 -08001731void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1732 Thread* self ATTRIBUTE_UNUSED,
1733 ArtMethod* method ATTRIBUTE_UNUSED,
1734 mirror::Object* receiver ATTRIBUTE_UNUSED,
1735 uint32_t* args ATTRIBUTE_UNUSED,
1736 JValue* result) {
1737 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1738 ? 0
1739 : 1);
1740}
1741
Mathieu Chartiere401d142015-04-22 13:56:20 -07001742void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1743 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1744 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001745 StackHandleScope<1> hs(self);
1746 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1747}
1748
Andreas Gampebc4d2182016-02-22 10:03:12 -08001749void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1750 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1751 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1752 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1753 result->SetD(bit_cast<double>(long_input));
1754}
1755
Mathieu Chartiere401d142015-04-22 13:56:20 -07001756void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1757 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1758 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001759 result->SetI(args[0]);
1760}
1761
Mathieu Chartiere401d142015-04-22 13:56:20 -07001762void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1763 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1764 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001765 result->SetI(args[0]);
1766}
1767
Mathieu Chartiere401d142015-04-22 13:56:20 -07001768void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1769 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1770 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001771 result->SetL(receiver->Clone(self));
1772}
1773
Mathieu Chartiere401d142015-04-22 13:56:20 -07001774void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1775 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1776 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001777 receiver->NotifyAll(self);
1778}
1779
Mathieu Chartiere401d142015-04-22 13:56:20 -07001780void UnstartedRuntime::UnstartedJNIStringCompareTo(
1781 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1782 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001783 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1784 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001785 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001786 }
1787 result->SetI(receiver->AsString()->CompareTo(rhs));
1788}
1789
Mathieu Chartiere401d142015-04-22 13:56:20 -07001790void UnstartedRuntime::UnstartedJNIStringIntern(
1791 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1792 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001793 result->SetL(receiver->AsString()->Intern());
1794}
1795
Mathieu Chartiere401d142015-04-22 13:56:20 -07001796void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1797 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1798 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001799 StackHandleScope<2> hs(self);
1800 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1801 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1802 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1803}
1804
Mathieu Chartiere401d142015-04-22 13:56:20 -07001805void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1806 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1807 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001808 int32_t length = static_cast<int32_t>(args[1]);
1809 if (length < 0) {
1810 ThrowNegativeArraySizeException(length);
1811 return;
1812 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001813 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001814 Runtime* runtime = Runtime::Current();
1815 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001816 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001817 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001818 CHECK(self->IsExceptionPending());
1819 return;
1820 }
1821 DCHECK(array_class->IsObjectArrayClass());
1822 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1823 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1824 result->SetL(new_array);
1825}
1826
Mathieu Chartiere401d142015-04-22 13:56:20 -07001827void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1828 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1829 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001830 ScopedObjectAccessUnchecked soa(self);
1831 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001832 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001833 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001834 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001835 }
1836}
1837
Mathieu Chartiere401d142015-04-22 13:56:20 -07001838void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1839 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1840 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001841 result->SetZ(JNI_TRUE);
1842}
1843
Mathieu Chartiere401d142015-04-22 13:56:20 -07001844void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1845 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1846 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001847 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1848 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1849 jint expectedValue = args[3];
1850 jint newValue = args[4];
1851 bool success;
1852 if (Runtime::Current()->IsActiveTransaction()) {
1853 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1854 expectedValue, newValue);
1855 } else {
1856 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1857 expectedValue, newValue);
1858 }
1859 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1860}
1861
Narayan Kamath34a316f2016-03-30 13:11:18 +01001862void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1863 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1864 uint32_t* args, JValue* result) {
1865 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1866 if (obj == nullptr) {
1867 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1868 return;
1869 }
1870
1871 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1872 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1873}
1874
Mathieu Chartiere401d142015-04-22 13:56:20 -07001875void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1876 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1877 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001878 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1879 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1880 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1881 if (Runtime::Current()->IsActiveTransaction()) {
1882 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1883 } else {
1884 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1885 }
1886}
1887
Andreas Gampe799681b2015-05-15 19:24:12 -07001888void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001889 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1890 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001891 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1892 Primitive::Type primitive_type = component->GetPrimitiveType();
1893 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1894}
1895
Andreas Gampe799681b2015-05-15 19:24:12 -07001896void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001897 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1898 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001899 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1900 Primitive::Type primitive_type = component->GetPrimitiveType();
1901 result->SetI(Primitive::ComponentSize(primitive_type));
1902}
1903
Andreas Gampedd9d0552015-03-09 12:57:41 -07001904typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001905 size_t arg_size);
1906
Mathieu Chartiere401d142015-04-22 13:56:20 -07001907typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001908 uint32_t* args, JValue* result);
1909
1910static bool tables_initialized_ = false;
1911static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1912static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1913
Andreas Gampe799681b2015-05-15 19:24:12 -07001914void UnstartedRuntime::InitializeInvokeHandlers() {
1915#define UNSTARTED_DIRECT(ShortName, Sig) \
1916 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1917#include "unstarted_runtime_list.h"
1918 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1919#undef UNSTARTED_RUNTIME_DIRECT_LIST
1920#undef UNSTARTED_RUNTIME_JNI_LIST
1921#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001922}
1923
Andreas Gampe799681b2015-05-15 19:24:12 -07001924void UnstartedRuntime::InitializeJNIHandlers() {
1925#define UNSTARTED_JNI(ShortName, Sig) \
1926 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1927#include "unstarted_runtime_list.h"
1928 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1929#undef UNSTARTED_RUNTIME_DIRECT_LIST
1930#undef UNSTARTED_RUNTIME_JNI_LIST
1931#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001932}
1933
Andreas Gampe799681b2015-05-15 19:24:12 -07001934void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001935 CHECK(!tables_initialized_);
1936
Andreas Gampe799681b2015-05-15 19:24:12 -07001937 InitializeInvokeHandlers();
1938 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001939
1940 tables_initialized_ = true;
1941}
1942
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001943void UnstartedRuntime::Invoke(Thread* self, const CodeItemDataAccessor& accessor,
Andreas Gampe799681b2015-05-15 19:24:12 -07001944 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001945 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1946 // problems in core libraries.
1947 CHECK(tables_initialized_);
1948
David Sehr709b0702016-10-13 09:12:37 -07001949 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001950 const auto& iter = invoke_handlers_.find(name);
1951 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001952 // Clear out the result in case it's not zeroed out.
1953 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001954
1955 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1956 self->PushShadowFrame(shadow_frame);
1957
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001958 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001959
1960 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001961 } else {
1962 // Not special, continue with regular interpreter execution.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001963 ArtInterpreterToInterpreterBridge(self, accessor, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001964 }
1965}
1966
1967// 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 -07001968void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001969 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001970 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001971 const auto& iter = jni_handlers_.find(name);
1972 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001973 // Clear out the result in case it's not zeroed out.
1974 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001975 (*iter->second)(self, method, receiver, args, result);
1976 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001977 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1978 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001979 } else {
David Sehr709b0702016-10-13 09:12:37 -07001980 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001981 "non-transactional runtime";
1982 }
1983}
1984
1985} // namespace interpreter
1986} // namespace art