blob: 545cc1ad42587fc16cd02040871fc5634d1013a5 [file] [log] [blame]
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "unstarted_runtime.h"
18
Andreas Gampe8ce9c302016-04-15 21:24:28 -070019#include <ctype.h>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070020#include <errno.h>
21#include <stdlib.h>
22
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include <cmath>
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -080024#include <initializer_list>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070025#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070026#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include <unordered_map>
28
Andreas Gampe46ee31b2016-12-14 10:11:49 -080029#include "android-base/stringprintf.h"
Andreas Gampeaacc25d2015-04-01 14:49:06 -070030#include "ScopedLocalRef.h"
31
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080033#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "base/logging.h"
36#include "base/macros.h"
37#include "class_linker.h"
38#include "common_throws.h"
39#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080040#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "handle_scope-inl.h"
42#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070043#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070044#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070046#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010047#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "mirror/object-inl.h"
49#include "mirror/object_array-inl.h"
50#include "mirror/string-inl.h"
51#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070052#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070053#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020054#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070055#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070056#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070057
58namespace art {
59namespace interpreter {
60
Andreas Gampe46ee31b2016-12-14 10:11:49 -080061using android::base::StringAppendV;
62using android::base::StringPrintf;
63
Andreas Gampe068b0c02015-03-11 12:44:47 -070064static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020065 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070066 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020067
68static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070069 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070070 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020071 va_start(args, fmt);
72 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070073 va_end(args);
74 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020075 va_start(args, fmt);
76 std::string msg;
77 StringAppendV(&msg, fmt, args);
78 va_end(args);
79 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070080 UNREACHABLE();
81 }
82}
83
Andreas Gampe8ce9c302016-04-15 21:24:28 -070084// Restricted support for character upper case / lower case. Only support ASCII, where
85// it's easy. Abort the transaction otherwise.
86static void CharacterLowerUpper(Thread* self,
87 ShadowFrame* shadow_frame,
88 JValue* result,
89 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070090 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070091 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
92
93 // Only ASCII (7-bit).
94 if (!isascii(int_value)) {
95 AbortTransactionOrFail(self,
96 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
97 int_value);
98 return;
99 }
100
101 std::locale c_locale("C");
102 char char_value = static_cast<char>(int_value);
103
104 if (to_lower_case) {
105 result->SetI(std::tolower(char_value, c_locale));
106 } else {
107 result->SetI(std::toupper(char_value, c_locale));
108 }
109}
110
111void UnstartedRuntime::UnstartedCharacterToLowerCase(
112 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
113 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
114}
115
116void UnstartedRuntime::UnstartedCharacterToUpperCase(
117 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
118 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
119}
120
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700121// Helper function to deal with class loading in an unstarted runtime.
122static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
123 Handle<mirror::ClassLoader> class_loader, JValue* result,
124 const std::string& method_name, bool initialize_class,
125 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700126 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700127 CHECK(className.Get() != nullptr);
128 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
129 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
130
131 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
132 if (found == nullptr && abort_if_not_found) {
133 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700134 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700135 method_name.c_str(),
136 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700137 }
138 return;
139 }
140 if (found != nullptr && initialize_class) {
141 StackHandleScope<1> hs(self);
142 Handle<mirror::Class> h_class(hs.NewHandle(found));
143 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
144 CHECK(self->IsExceptionPending());
145 return;
146 }
147 }
148 result->SetL(found);
149}
150
151// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
152// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
153// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200154// actually the transaction abort exception. This must not be wrapped, as it signals an
155// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700156static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700157 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700158 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200159 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700160 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200161 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700162 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
163 "ClassNotFoundException");
164 }
165 }
166}
167
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700168static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700169 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700170 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
171 if (param == nullptr) {
172 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
173 return nullptr;
174 }
175 return param->AsString();
176}
177
Andreas Gampe799681b2015-05-15 19:24:12 -0700178void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700179 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700180 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
181 if (class_name == nullptr) {
182 return;
183 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700184 StackHandleScope<1> hs(self);
185 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800186 UnstartedRuntimeFindClass(self,
187 h_class_name,
188 ScopedNullHandle<mirror::ClassLoader>(),
189 result,
190 "Class.forName",
191 true,
192 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700193 CheckExceptionGenerateClassNotFound(self);
194}
195
Andreas Gampe799681b2015-05-15 19:24:12 -0700196void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700197 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700198 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
199 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700200 return;
201 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700202 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
203 mirror::ClassLoader* class_loader =
204 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
205 StackHandleScope<2> hs(self);
206 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
207 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
208 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
209 initialize_class, false);
210 CheckExceptionGenerateClassNotFound(self);
211}
212
Andreas Gampe799681b2015-05-15 19:24:12 -0700213void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700215 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
216 if (class_name == nullptr) {
217 return;
218 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700219 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
220 mirror::ClassLoader* class_loader =
221 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
222 StackHandleScope<2> hs(self);
223 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
224 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
225 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
226 initialize_class, false);
227 CheckExceptionGenerateClassNotFound(self);
228}
229
Andreas Gampe799681b2015-05-15 19:24:12 -0700230void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
232 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700233 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
234 if (param == nullptr) {
235 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
236 return;
237 }
238 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700239 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700240
241 // Check that it's not null.
242 if (h_klass.Get() == nullptr) {
243 AbortTransactionOrFail(self, "Class reference is null for newInstance");
244 return;
245 }
246
247 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
248 if (Runtime::Current()->IsActiveTransaction()) {
249 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200250 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700251 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700252 return;
253 }
254 }
255
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700256 // There are two situations in which we'll abort this run.
257 // 1) If the class isn't yet initialized and initialization fails.
258 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
259 // Note that 2) could likely be handled here, but for safety abort the transaction.
260 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 auto* cl = Runtime::Current()->GetClassLinker();
262 if (cl->EnsureInitialized(self, h_klass, true, true)) {
263 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
264 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700265 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
266 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700267 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700268 if (!self->IsExceptionPending()) {
269 result->SetL(h_obj.Get());
270 ok = true;
271 }
272 } else {
273 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
274 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700275 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700276 }
277 }
278 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700279 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700280 h_klass->PrettyClass().c_str(),
281 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700282 }
283}
284
Andreas Gampe799681b2015-05-15 19:24:12 -0700285void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700286 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700287 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
288 // going the reflective Dex way.
289 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
290 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700291 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700292 for (ArtField& field : klass->GetIFields()) {
293 if (name2->Equals(field.GetName())) {
294 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700295 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700296 }
297 }
298 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700299 for (ArtField& field : klass->GetSFields()) {
300 if (name2->Equals(field.GetName())) {
301 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700302 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700303 }
304 }
305 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700306 if (found == nullptr) {
307 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
308 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700309 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700310 return;
311 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700312 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700313 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700314 mirror::Field* field;
315 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700316 if (pointer_size == PointerSize::k64) {
317 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
318 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700319 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700320 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
321 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700322 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700323 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700324 if (pointer_size == PointerSize::k64) {
325 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
326 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700327 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700328 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
329 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700330 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700331 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700332 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700333}
334
Andreas Gampebc4d2182016-02-22 10:03:12 -0800335// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
336void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
337 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
338 // Special managed code cut-out to allow method lookup in a un-started runtime.
339 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
340 if (klass == nullptr) {
341 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
342 return;
343 }
344 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
345 mirror::ObjectArray<mirror::Class>* args =
346 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700347 Runtime* runtime = Runtime::Current();
348 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700349 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700350 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700351 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700352 if (pointer_size == PointerSize::k64) {
353 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
354 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700355 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700356 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
357 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700358 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800359 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700360 if (pointer_size == PointerSize::k64) {
361 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
362 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700363 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700364 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
365 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700366 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800367 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700368 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800369}
370
Andreas Gampe6039e562016-04-05 18:18:43 -0700371// Special managed code cut-out to allow constructor lookup in a un-started runtime.
372void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
373 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
374 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
375 if (klass == nullptr) {
376 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
377 return;
378 }
379 mirror::ObjectArray<mirror::Class>* args =
380 shadow_frame->GetVRegReference(arg_offset + 1)->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::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700385 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700386 if (pointer_size == PointerSize::k64) {
387 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
388 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700389 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700390 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
391 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700392 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700393 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700394 if (pointer_size == PointerSize::k64) {
395 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
396 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700397 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700398 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
399 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700400 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700401 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700402 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700403}
404
Andreas Gampeae78c262017-02-01 20:40:44 -0800405void UnstartedRuntime::UnstartedClassGetDeclaringClass(
406 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
407 StackHandleScope<1> hs(self);
408 Handle<mirror::Class> klass(hs.NewHandle(
409 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
410 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
411 result->SetL(nullptr);
412 return;
413 }
414 // Return null for anonymous classes.
415 JValue is_anon_result;
416 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
417 if (is_anon_result.GetZ() != 0) {
418 result->SetL(nullptr);
419 return;
420 }
421 result->SetL(annotations::GetDeclaringClass(klass));
422}
423
Andreas Gampe633750c2016-02-19 10:49:50 -0800424void UnstartedRuntime::UnstartedClassGetEnclosingClass(
425 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
426 StackHandleScope<1> hs(self);
427 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
428 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
429 result->SetL(nullptr);
430 }
David Sehr9323e6e2016-09-13 08:58:35 -0700431 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800432}
433
Andreas Gampe715fdc22016-04-18 17:07:30 -0700434void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
435 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
436 StackHandleScope<1> hs(self);
437 Handle<mirror::Class> klass(hs.NewHandle(
438 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
439 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
440 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
441}
442
Andreas Gampeae78c262017-02-01 20:40:44 -0800443void UnstartedRuntime::UnstartedClassIsAnonymousClass(
444 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
445 StackHandleScope<1> hs(self);
446 Handle<mirror::Class> klass(hs.NewHandle(
447 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
448 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
449 result->SetZ(false);
450 return;
451 }
452 mirror::String* class_name = nullptr;
453 if (!annotations::GetInnerClass(klass, &class_name)) {
454 result->SetZ(false);
455 return;
456 }
457 result->SetZ(class_name == nullptr);
458}
459
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700460static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
461 const char* entry_name,
462 size_t* size,
463 std::string* error_msg) {
464 CHECK(size != nullptr);
465
466 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
467 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800468 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700469 }
470 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
471 if (zip_entry == nullptr) {
472 return nullptr;
473 }
474 std::unique_ptr<MemMap> tmp_map(
475 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
476 if (tmp_map == nullptr) {
477 return nullptr;
478 }
479
480 // OK, from here everything seems fine.
481 *size = zip_entry->GetUncompressedLength();
482 return tmp_map;
483}
484
485static void GetResourceAsStream(Thread* self,
486 ShadowFrame* shadow_frame,
487 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700489 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
490 if (resource_obj == nullptr) {
491 AbortTransactionOrFail(self, "null name for getResourceAsStream");
492 return;
493 }
494 CHECK(resource_obj->IsString());
495 mirror::String* resource_name = resource_obj->AsString();
496
497 std::string resource_name_str = resource_name->ToModifiedUtf8();
498 if (resource_name_str.empty() || resource_name_str == "/") {
499 AbortTransactionOrFail(self,
500 "Unsupported name %s for getResourceAsStream",
501 resource_name_str.c_str());
502 return;
503 }
504 const char* resource_cstr = resource_name_str.c_str();
505 if (resource_cstr[0] == '/') {
506 resource_cstr++;
507 }
508
509 Runtime* runtime = Runtime::Current();
510
511 std::vector<std::string> split;
512 Split(runtime->GetBootClassPathString(), ':', &split);
513 if (split.empty()) {
514 AbortTransactionOrFail(self,
515 "Boot classpath not set or split error:: %s",
516 runtime->GetBootClassPathString().c_str());
517 return;
518 }
519
520 std::unique_ptr<MemMap> mem_map;
521 size_t map_size;
522 std::string last_error_msg; // Only store the last message (we could concatenate).
523
524 for (const std::string& jar_file : split) {
525 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
526 if (mem_map != nullptr) {
527 break;
528 }
529 }
530
531 if (mem_map == nullptr) {
532 // Didn't find it. There's a good chance this will be the same at runtime, but still
533 // conservatively abort the transaction here.
534 AbortTransactionOrFail(self,
535 "Could not find resource %s. Last error was %s.",
536 resource_name_str.c_str(),
537 last_error_msg.c_str());
538 return;
539 }
540
541 StackHandleScope<3> hs(self);
542
543 // Create byte array for content.
544 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
545 if (h_array.Get() == nullptr) {
546 AbortTransactionOrFail(self, "Could not find/create byte array class");
547 return;
548 }
549 // Copy in content.
550 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
551 // Be proactive releasing memory.
552 mem_map.release();
553
554 // Create a ByteArrayInputStream.
555 Handle<mirror::Class> h_class(hs.NewHandle(
556 runtime->GetClassLinker()->FindClass(self,
557 "Ljava/io/ByteArrayInputStream;",
558 ScopedNullHandle<mirror::ClassLoader>())));
559 if (h_class.Get() == nullptr) {
560 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
561 return;
562 }
563 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
564 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
565 return;
566 }
567
568 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
569 if (h_obj.Get() == nullptr) {
570 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
571 return;
572 }
573
574 auto* cl = Runtime::Current()->GetClassLinker();
575 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
576 "<init>", "([B)V", cl->GetImagePointerSize());
577 if (constructor == nullptr) {
578 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
579 return;
580 }
581
582 uint32_t args[1];
583 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
584 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
585
586 if (self->IsExceptionPending()) {
587 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
588 return;
589 }
590
591 result->SetL(h_obj.Get());
592}
593
594void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
595 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
596 {
597 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
598 CHECK(this_obj != nullptr);
599 CHECK(this_obj->IsClassLoader());
600
601 StackHandleScope<1> hs(self);
602 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
603
604 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
605 this_classloader_class.Get()) {
606 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700607 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700608 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700609 return;
610 }
611 }
612
613 GetResourceAsStream(self, shadow_frame, result, arg_offset);
614}
615
Andreas Gampe799681b2015-05-15 19:24:12 -0700616void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700617 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700618 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
619 mirror::ClassLoader* class_loader =
620 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
621 StackHandleScope<2> hs(self);
622 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
623 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
624 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
625 "VMClassLoader.findLoadedClass", false, false);
626 // This might have an error pending. But semantics are to just return null.
627 if (self->IsExceptionPending()) {
628 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700629 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700630 if (type != "java.lang.InternalError") {
631 self->ClearException();
632 }
633 }
634}
635
Mathieu Chartiere401d142015-04-22 13:56:20 -0700636void UnstartedRuntime::UnstartedVoidLookupType(
637 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
638 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700639 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
640}
641
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700642// Arraycopy emulation.
643// Note: we can't use any fast copy functions, as they are not available under transaction.
644
645template <typename T>
646static void PrimitiveArrayCopy(Thread* self,
647 mirror::Array* src_array, int32_t src_pos,
648 mirror::Array* dst_array, int32_t dst_pos,
649 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700650 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700651 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700652 AbortTransactionOrFail(self,
653 "Types mismatched in arraycopy: %s vs %s.",
654 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700655 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700656 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700657 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700658 return;
659 }
660 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
661 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
662 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
663 if (copy_forward) {
664 for (int32_t i = 0; i < length; ++i) {
665 dst->Set(dst_pos + i, src->Get(src_pos + i));
666 }
667 } else {
668 for (int32_t i = 1; i <= length; ++i) {
669 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
670 }
671 }
672}
673
Andreas Gampe799681b2015-05-15 19:24:12 -0700674void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700675 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700676 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700677 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
678 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700679 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700680
Andreas Gampe85a098a2016-03-31 13:30:53 -0700681 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
682 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
683 // Null checking. For simplicity, abort transaction.
684 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700685 AbortTransactionOrFail(self, "src is null in arraycopy.");
686 return;
687 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700688 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700689 AbortTransactionOrFail(self, "dst is null in arraycopy.");
690 return;
691 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700692 // Test for arrayness. Throw ArrayStoreException.
693 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
694 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
695 return;
696 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700697
Andreas Gampe85a098a2016-03-31 13:30:53 -0700698 mirror::Array* src_array = src_obj->AsArray();
699 mirror::Array* dst_array = dst_obj->AsArray();
700
701 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700702 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
703 UNLIKELY(src_pos > src_array->GetLength() - length) ||
704 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700705 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700706 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
707 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
708 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700709 return;
710 }
711
712 // Type checking.
713 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
714 GetComponentType();
715
716 if (!src_type->IsPrimitive()) {
717 // Check that the second type is not primitive.
718 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
719 GetComponentType();
720 if (trg_type->IsPrimitiveInt()) {
721 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700722 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700723 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700724 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700725 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700726 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700727 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700728
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700729 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
730 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
731 if (src == dst) {
732 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700733 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700734 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
735 if (copy_forward) {
736 for (int32_t i = 0; i < length; ++i) {
737 dst->Set(dst_pos + i, src->Get(src_pos + i));
738 }
739 } else {
740 for (int32_t i = 1; i <= length; ++i) {
741 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
742 }
743 }
744 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700745 // We're being lazy here. Optimally this could be a memcpy (if component types are
746 // assignable), but the ObjectArray implementation doesn't support transactions. The
747 // checking version, however, does.
748 if (Runtime::Current()->IsActiveTransaction()) {
749 dst->AssignableCheckingMemcpy<true>(
750 dst_pos, src, src_pos, length, true /* throw_exception */);
751 } else {
752 dst->AssignableCheckingMemcpy<false>(
753 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700754 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700755 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700756 } else if (src_type->IsPrimitiveByte()) {
757 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700758 } else if (src_type->IsPrimitiveChar()) {
759 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
760 } else if (src_type->IsPrimitiveInt()) {
761 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700762 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700763 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700764 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700765 }
766}
767
Andreas Gampe5c9af612016-04-05 14:16:10 -0700768void UnstartedRuntime::UnstartedSystemArraycopyByte(
769 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
770 // Just forward.
771 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
772}
773
Andreas Gampe799681b2015-05-15 19:24:12 -0700774void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700775 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700776 // Just forward.
777 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
778}
779
780void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700781 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700782 // Just forward.
783 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
784}
785
Narayan Kamath34a316f2016-03-30 13:11:18 +0100786void UnstartedRuntime::UnstartedSystemGetSecurityManager(
787 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
788 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
789 result->SetL(nullptr);
790}
791
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700792static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
793
794static void GetSystemProperty(Thread* self,
795 ShadowFrame* shadow_frame,
796 JValue* result,
797 size_t arg_offset,
798 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700799 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700800 StackHandleScope<4> hs(self);
801 Handle<mirror::String> h_key(
802 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
803 if (h_key.Get() == nullptr) {
804 AbortTransactionOrFail(self, "getProperty key was null");
805 return;
806 }
807
808 // This is overall inefficient, but reflecting the values here is not great, either. So
809 // for simplicity, and with the assumption that the number of getProperty calls is not
810 // too great, just iterate each time.
811
812 // Get the storage class.
813 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
814 Handle<mirror::Class> h_props_class(hs.NewHandle(
815 class_linker->FindClass(self,
816 "Ljava/lang/AndroidHardcodedSystemProperties;",
817 ScopedNullHandle<mirror::ClassLoader>())));
818 if (h_props_class.Get() == nullptr) {
819 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
820 return;
821 }
822 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
823 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
824 return;
825 }
826
827 // Get the storage array.
828 ArtField* static_properties =
829 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
830 "[[Ljava/lang/String;");
831 if (static_properties == nullptr) {
832 AbortTransactionOrFail(self,
833 "Could not find %s field",
834 kAndroidHardcodedSystemPropertiesFieldName);
835 return;
836 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700837 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
838 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
839 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700840 if (h_2string_array.Get() == nullptr) {
841 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
842 return;
843 }
844
845 // Iterate over it.
846 const int32_t prop_count = h_2string_array->GetLength();
847 // Use the third handle as mutable.
848 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
849 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
850 for (int32_t i = 0; i < prop_count; ++i) {
851 h_string_array.Assign(h_2string_array->Get(i));
852 if (h_string_array.Get() == nullptr ||
853 h_string_array->GetLength() != 2 ||
854 h_string_array->Get(0) == nullptr) {
855 AbortTransactionOrFail(self,
856 "Unexpected content of %s",
857 kAndroidHardcodedSystemPropertiesFieldName);
858 return;
859 }
860 if (h_key->Equals(h_string_array->Get(0))) {
861 // Found a value.
862 if (h_string_array->Get(1) == nullptr && is_default_version) {
863 // Null is being delegated to the default map, and then resolved to the given default value.
864 // As there's no default map, return the given value.
865 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
866 } else {
867 result->SetL(h_string_array->Get(1));
868 }
869 return;
870 }
871 }
872
873 // Key is not supported.
874 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
875}
876
877void UnstartedRuntime::UnstartedSystemGetProperty(
878 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
879 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
880}
881
882void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
883 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
884 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
885}
886
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800887static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
888 REQUIRES_SHARED(Locks::mutator_lock_) {
889 if (shadow_frame->GetLink() == nullptr) {
890 return "<no caller>";
891 }
892 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
893}
894
895static bool CheckCallers(ShadowFrame* shadow_frame,
896 std::initializer_list<std::string> allowed_call_stack)
897 REQUIRES_SHARED(Locks::mutator_lock_) {
898 for (const std::string& allowed_caller : allowed_call_stack) {
899 if (shadow_frame->GetLink() == nullptr) {
900 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700901 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800902
903 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
904 if (allowed_caller != found_caller) {
905 return false;
906 }
907
908 shadow_frame = shadow_frame->GetLink();
909 }
910 return true;
911}
912
913static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
914 REQUIRES_SHARED(Locks::mutator_lock_) {
915 // Find the requested class.
916 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
917 ObjPtr<mirror::Class> klass =
918 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
919 if (klass == nullptr) {
920 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
921 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700922 }
923
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -0800924 StackHandleScope<2> hs(self);
925 Handle<mirror::Class> h_class(hs.NewHandle(klass));
926 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
927 if (h_obj.Get() != nullptr) {
928 ArtMethod* init_method = h_class->FindDirectMethod(
929 "<init>", "()V", class_linker->GetImagePointerSize());
930 if (init_method == nullptr) {
931 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
932 return nullptr;
933 } else {
934 JValue invoke_result;
935 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
936 if (!self->IsExceptionPending()) {
937 return h_obj.Get();
938 }
939 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
940 }
941 }
942 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
943 return nullptr;
944}
945
946void UnstartedRuntime::UnstartedThreadLocalGet(
947 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
948 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
949 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
950 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
951 } else {
952 AbortTransactionOrFail(self,
953 "ThreadLocal.get() does not support %s",
954 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700955 }
956}
957
Sergio Giro83261202016-04-11 20:49:20 +0100958void UnstartedRuntime::UnstartedMathCeil(
959 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700960 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100961}
962
963void UnstartedRuntime::UnstartedMathFloor(
964 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700965 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700966}
967
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700968void UnstartedRuntime::UnstartedMathSin(
969 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
970 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
971}
972
973void UnstartedRuntime::UnstartedMathCos(
974 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
975 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
976}
977
978void UnstartedRuntime::UnstartedMathPow(
979 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
980 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
981 shadow_frame->GetVRegDouble(arg_offset + 2)));
982}
983
Andreas Gampe799681b2015-05-15 19:24:12 -0700984void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700985 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700986 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
987 result->SetI(obj->IdentityHashCode());
988}
989
Andreas Gampe799681b2015-05-15 19:24:12 -0700990void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700991 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700992 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000993 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700994}
995
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700996static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700997 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700998 const DexFile* dex_file = dex_cache->GetDexFile();
999 if (dex_file == nullptr) {
1000 return nullptr;
1001 }
1002
1003 // Create the direct byte buffer.
1004 JNIEnv* env = self->GetJniEnv();
1005 DCHECK(env != nullptr);
1006 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001007 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
1008 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001009 DCHECK(self->IsExceptionPending());
1010 return nullptr;
1011 }
1012
1013 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -07001014 args[0].l = byte_buffer.get();
1015
1016 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
1017 WellKnownClasses::com_android_dex_Dex,
1018 WellKnownClasses::com_android_dex_Dex_create,
1019 args));
1020
1021 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001022}
1023
Andreas Gampe799681b2015-05-15 19:24:12 -07001024void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001025 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001026 // We will create the Dex object, but the image writer will release it before creating the
1027 // art file.
1028 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
1029 bool have_dex = false;
1030 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -07001031 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001032 if (dex != nullptr) {
1033 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001034 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001035 }
1036 }
1037 if (!have_dex) {
1038 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001039 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001040 }
1041}
1042
1043static void UnstartedMemoryPeek(
1044 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1045 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1046 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1047 // aborting the transaction.
1048
1049 switch (type) {
1050 case Primitive::kPrimByte: {
1051 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1052 return;
1053 }
1054
1055 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001056 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1057 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001058 return;
1059 }
1060
1061 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001062 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1063 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001064 return;
1065 }
1066
1067 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001068 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1069 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001070 return;
1071 }
1072
1073 case Primitive::kPrimBoolean:
1074 case Primitive::kPrimChar:
1075 case Primitive::kPrimFloat:
1076 case Primitive::kPrimDouble:
1077 case Primitive::kPrimVoid:
1078 case Primitive::kPrimNot:
1079 LOG(FATAL) << "Not in the Memory API: " << type;
1080 UNREACHABLE();
1081 }
1082 LOG(FATAL) << "Should not reach here";
1083 UNREACHABLE();
1084}
1085
Andreas Gampe799681b2015-05-15 19:24:12 -07001086void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001087 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001088 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1089}
1090
1091void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001092 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001093 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1094}
1095
1096void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001097 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001098 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1099}
1100
1101void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001102 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001103 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001104}
1105
1106static void UnstartedMemoryPeekArray(
1107 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001108 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001109 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1110 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1111 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001112 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001113 return;
1114 }
1115 mirror::Array* array = obj->AsArray();
1116
1117 int offset = shadow_frame->GetVReg(arg_offset + 3);
1118 int count = shadow_frame->GetVReg(arg_offset + 4);
1119 if (offset < 0 || offset + count > array->GetLength()) {
1120 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1121 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001122 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001123 return;
1124 }
1125
1126 switch (type) {
1127 case Primitive::kPrimByte: {
1128 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1129 mirror::ByteArray* byte_array = array->AsByteArray();
1130 for (int32_t i = 0; i < count; ++i, ++address) {
1131 byte_array->SetWithoutChecks<true>(i + offset, *address);
1132 }
1133 return;
1134 }
1135
1136 case Primitive::kPrimShort:
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimLong:
1139 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1140 UNREACHABLE();
1141
1142 case Primitive::kPrimBoolean:
1143 case Primitive::kPrimChar:
1144 case Primitive::kPrimFloat:
1145 case Primitive::kPrimDouble:
1146 case Primitive::kPrimVoid:
1147 case Primitive::kPrimNot:
1148 LOG(FATAL) << "Not in the Memory API: " << type;
1149 UNREACHABLE();
1150 }
1151 LOG(FATAL) << "Should not reach here";
1152 UNREACHABLE();
1153}
1154
Andreas Gampe799681b2015-05-15 19:24:12 -07001155void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001157 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001158}
1159
Kenny Root1c9e61c2015-05-14 15:58:17 -07001160// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001161void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001162 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001163 jint start = shadow_frame->GetVReg(arg_offset + 1);
1164 jint end = shadow_frame->GetVReg(arg_offset + 2);
1165 jint index = shadow_frame->GetVReg(arg_offset + 4);
1166 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1167 if (string == nullptr) {
1168 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1169 return;
1170 }
Kenny Root57f91e82015-05-14 15:58:17 -07001171 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001172 DCHECK_LE(start, end);
1173 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001174 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001175 Handle<mirror::CharArray> h_char_array(
1176 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001177 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001178 DCHECK_LE(index, h_char_array->GetLength());
1179 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001180 string->GetChars(start, end, h_char_array, index);
1181}
1182
1183// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001184void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001185 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001186 jint index = shadow_frame->GetVReg(arg_offset + 1);
1187 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1188 if (string == nullptr) {
1189 AbortTransactionOrFail(self, "String.charAt with null object");
1190 return;
1191 }
1192 result->SetC(string->CharAt(index));
1193}
1194
Kenny Root57f91e82015-05-14 15:58:17 -07001195// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001196void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001197 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001198 jint index = shadow_frame->GetVReg(arg_offset + 1);
1199 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1200 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1201 if (string == nullptr) {
1202 AbortTransactionOrFail(self, "String.setCharAt with null object");
1203 return;
1204 }
1205 string->SetCharAt(index, c);
1206}
1207
Kenny Root1c9e61c2015-05-14 15:58:17 -07001208// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001209void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001210 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001211 jint offset = shadow_frame->GetVReg(arg_offset);
1212 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1213 DCHECK_GE(char_count, 0);
1214 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001215 Handle<mirror::CharArray> h_char_array(
1216 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001217 Runtime* runtime = Runtime::Current();
1218 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1219 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1220}
1221
1222// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001223void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001224 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001225 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1226 if (to_copy == nullptr) {
1227 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1228 return;
1229 }
1230 StackHandleScope<1> hs(self);
1231 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1232 Runtime* runtime = Runtime::Current();
1233 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1234 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1235 allocator));
1236}
1237
Andreas Gampe799681b2015-05-15 19:24:12 -07001238void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001239 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001240 jint start = shadow_frame->GetVReg(arg_offset + 1);
1241 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001242 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001243 DCHECK_GE(length, 0);
1244 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245 Handle<mirror::String> h_string(
1246 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001247 DCHECK_LE(start, h_string->GetLength());
1248 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001249 Runtime* runtime = Runtime::Current();
1250 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1251 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1252}
1253
Kenny Root57f91e82015-05-14 15:58:17 -07001254// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001255void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001256 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001257 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001258 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1259 if (string == nullptr) {
1260 AbortTransactionOrFail(self, "String.charAt with null object");
1261 return;
1262 }
1263 result->SetL(string->ToCharArray(self));
1264}
1265
Andreas Gampebc4d2182016-02-22 10:03:12 -08001266// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1267void UnstartedRuntime::UnstartedReferenceGetReferent(
1268 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001269 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001270 shadow_frame->GetVRegReference(arg_offset));
1271 if (ref == nullptr) {
1272 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1273 return;
1274 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001275 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001276 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1277 result->SetL(referent);
1278}
1279
1280// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1281// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1282// where we can predict the behavior (somewhat).
1283// Note: this is required (instead of lazy initialization) as these classes are used in the static
1284// initialization of other classes, so will *use* the value.
1285void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1286 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001287 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001288 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1289 // 8 as a conservative upper approximation.
1290 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001291 } else if (CheckCallers(shadow_frame,
1292 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001293 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1294 // a good upper bound.
1295 // TODO: Consider resetting in the zygote?
1296 result->SetI(8);
1297 } else {
1298 // Not supported.
1299 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1300 }
1301}
1302
1303// This allows accessing ConcurrentHashMap/SynchronousQueue.
1304
1305void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1306 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1307 // Argument 0 is the Unsafe instance, skip.
1308 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1309 if (obj == nullptr) {
1310 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1311 return;
1312 }
1313 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1314 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1315 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001316 bool success;
1317 // Check whether we're in a transaction, call accordingly.
1318 if (Runtime::Current()->IsActiveTransaction()) {
1319 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1320 expectedValue,
1321 newValue);
1322 } else {
1323 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1324 expectedValue,
1325 newValue);
1326 }
1327 result->SetZ(success ? 1 : 0);
1328}
1329
1330void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1332 // Argument 0 is the Unsafe instance, skip.
1333 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1334 if (obj == nullptr) {
1335 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1336 return;
1337 }
1338 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1339 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1340 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1341
1342 // Must use non transactional mode.
1343 if (kUseReadBarrier) {
1344 // Need to make sure the reference stored in the field is a to-space one before attempting the
1345 // CAS or the CAS could fail incorrectly.
1346 mirror::HeapReference<mirror::Object>* field_addr =
1347 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1348 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001349 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001350 obj,
1351 MemberOffset(offset),
1352 field_addr);
1353 }
1354 bool success;
1355 // Check whether we're in a transaction, call accordingly.
1356 if (Runtime::Current()->IsActiveTransaction()) {
1357 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1358 expected_value,
1359 newValue);
1360 } else {
1361 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1362 expected_value,
1363 newValue);
1364 }
1365 result->SetZ(success ? 1 : 0);
1366}
1367
1368void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1369 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001370 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001371 // Argument 0 is the Unsafe instance, skip.
1372 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1373 if (obj == nullptr) {
1374 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1375 return;
1376 }
1377 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1378 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1379 result->SetL(value);
1380}
1381
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001382void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1383 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001384 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001385 // Argument 0 is the Unsafe instance, skip.
1386 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1387 if (obj == nullptr) {
1388 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1389 return;
1390 }
1391 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1392 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1393 if (Runtime::Current()->IsActiveTransaction()) {
1394 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1395 } else {
1396 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1397 }
1398}
1399
Andreas Gampebc4d2182016-02-22 10:03:12 -08001400void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1401 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001402 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001403 // Argument 0 is the Unsafe instance, skip.
1404 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1405 if (obj == nullptr) {
1406 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1407 return;
1408 }
1409 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1410 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1411 QuasiAtomic::ThreadFenceRelease();
1412 if (Runtime::Current()->IsActiveTransaction()) {
1413 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1414 } else {
1415 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1416 }
1417}
1418
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001419// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1420// of correctly handling the corner cases.
1421void UnstartedRuntime::UnstartedIntegerParseInt(
1422 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001423 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001424 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1425 if (obj == nullptr) {
1426 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1427 return;
1428 }
1429
1430 std::string string_value = obj->AsString()->ToModifiedUtf8();
1431 if (string_value.empty()) {
1432 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1433 return;
1434 }
1435
1436 const char* c_str = string_value.c_str();
1437 char *end;
1438 // Can we set errno to 0? Is this always a variable, and not a macro?
1439 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1440 int64_t l = strtol(c_str, &end, 10);
1441
1442 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1443 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1444 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1445 return;
1446 }
1447 if (l == 0) {
1448 // Check whether the string wasn't exactly zero.
1449 if (string_value != "0") {
1450 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1451 return;
1452 }
1453 } else if (*end != '\0') {
1454 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1455 return;
1456 }
1457
1458 result->SetI(static_cast<int32_t>(l));
1459}
1460
1461// A cutout for Long.parseLong.
1462//
1463// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1464// well.
1465void UnstartedRuntime::UnstartedLongParseLong(
1466 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001467 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001468 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1469 if (obj == nullptr) {
1470 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1471 return;
1472 }
1473
1474 std::string string_value = obj->AsString()->ToModifiedUtf8();
1475 if (string_value.empty()) {
1476 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1477 return;
1478 }
1479
1480 const char* c_str = string_value.c_str();
1481 char *end;
1482 // Can we set errno to 0? Is this always a variable, and not a macro?
1483 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1484 int64_t l = strtol(c_str, &end, 10);
1485
1486 // Note: comparing against int32_t min/max is intentional here.
1487 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1488 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1489 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1490 return;
1491 }
1492 if (l == 0) {
1493 // Check whether the string wasn't exactly zero.
1494 if (string_value != "0") {
1495 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1496 return;
1497 }
1498 } else if (*end != '\0') {
1499 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1500 return;
1501 }
1502
1503 result->SetJ(l);
1504}
1505
Andreas Gampe715fdc22016-04-18 17:07:30 -07001506void UnstartedRuntime::UnstartedMethodInvoke(
1507 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001508 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001509 JNIEnvExt* env = self->GetJniEnv();
1510 ScopedObjectAccessUnchecked soa(self);
1511
Mathieu Chartier8778c522016-10-04 19:06:30 -07001512 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001513 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001514 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001515
Mathieu Chartier8778c522016-10-04 19:06:30 -07001516 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001517 ScopedLocalRef<jobject> java_receiver(env,
1518 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1519
Mathieu Chartier8778c522016-10-04 19:06:30 -07001520 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001521 ScopedLocalRef<jobject> java_args(env,
1522 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1523
1524 ScopedLocalRef<jobject> result_jobj(env,
1525 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1526
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001527 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001528
1529 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1530 // InvocationTargetExceptions.
1531 if (self->IsExceptionPending()) {
1532 AbortTransactionOrFail(self, "Failed Method.invoke");
1533 }
1534}
1535
Andreas Gampebc4d2182016-02-22 10:03:12 -08001536
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1538 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1539 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001540 int32_t length = args[1];
1541 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001542 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001543 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001544 ObjPtr<mirror::Class> array_class =
1545 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001546 DCHECK(array_class != nullptr);
1547 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001548 result->SetL(mirror::Array::Alloc<true, true>(self,
1549 array_class,
1550 length,
1551 array_class->GetComponentSizeShift(),
1552 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001553}
1554
Mathieu Chartiere401d142015-04-22 13:56:20 -07001555void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1556 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1557 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001558 result->SetL(nullptr);
1559}
1560
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1562 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1563 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001564 NthCallerVisitor visitor(self, 3);
1565 visitor.WalkStack();
1566 if (visitor.caller != nullptr) {
1567 result->SetL(visitor.caller->GetDeclaringClass());
1568 }
1569}
1570
Mathieu Chartiere401d142015-04-22 13:56:20 -07001571void UnstartedRuntime::UnstartedJNIMathLog(
1572 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1573 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001574 JValue value;
1575 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1576 result->SetD(log(value.GetD()));
1577}
1578
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579void UnstartedRuntime::UnstartedJNIMathExp(
1580 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1581 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001582 JValue value;
1583 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1584 result->SetD(exp(value.GetD()));
1585}
1586
Andreas Gampebc4d2182016-02-22 10:03:12 -08001587void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1588 Thread* self ATTRIBUTE_UNUSED,
1589 ArtMethod* method ATTRIBUTE_UNUSED,
1590 mirror::Object* receiver ATTRIBUTE_UNUSED,
1591 uint32_t* args ATTRIBUTE_UNUSED,
1592 JValue* result) {
1593 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1594 ? 0
1595 : 1);
1596}
1597
Mathieu Chartiere401d142015-04-22 13:56:20 -07001598void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1599 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1600 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001601 StackHandleScope<1> hs(self);
1602 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1603}
1604
Andreas Gampebc4d2182016-02-22 10:03:12 -08001605void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1606 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1607 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1608 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1609 result->SetD(bit_cast<double>(long_input));
1610}
1611
Mathieu Chartiere401d142015-04-22 13:56:20 -07001612void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1613 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1614 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001615 result->SetI(args[0]);
1616}
1617
Mathieu Chartiere401d142015-04-22 13:56:20 -07001618void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1619 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1620 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001621 result->SetI(args[0]);
1622}
1623
Mathieu Chartiere401d142015-04-22 13:56:20 -07001624void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1625 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1626 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001627 result->SetL(receiver->Clone(self));
1628}
1629
Mathieu Chartiere401d142015-04-22 13:56:20 -07001630void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1631 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1632 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001633 receiver->NotifyAll(self);
1634}
1635
Mathieu Chartiere401d142015-04-22 13:56:20 -07001636void UnstartedRuntime::UnstartedJNIStringCompareTo(
1637 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1638 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001639 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1640 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001641 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001642 }
1643 result->SetI(receiver->AsString()->CompareTo(rhs));
1644}
1645
Mathieu Chartiere401d142015-04-22 13:56:20 -07001646void UnstartedRuntime::UnstartedJNIStringIntern(
1647 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1648 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001649 result->SetL(receiver->AsString()->Intern());
1650}
1651
Mathieu Chartiere401d142015-04-22 13:56:20 -07001652void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1653 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1654 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001655 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1656}
1657
Mathieu Chartiere401d142015-04-22 13:56:20 -07001658void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1659 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1660 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001661 StackHandleScope<2> hs(self);
1662 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1663 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1664 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1665}
1666
Mathieu Chartiere401d142015-04-22 13:56:20 -07001667void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1668 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1669 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001670 int32_t length = static_cast<int32_t>(args[1]);
1671 if (length < 0) {
1672 ThrowNegativeArraySizeException(length);
1673 return;
1674 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001675 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001676 Runtime* runtime = Runtime::Current();
1677 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001678 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001679 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001680 CHECK(self->IsExceptionPending());
1681 return;
1682 }
1683 DCHECK(array_class->IsObjectArrayClass());
1684 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1685 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1686 result->SetL(new_array);
1687}
1688
Mathieu Chartiere401d142015-04-22 13:56:20 -07001689void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1690 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1691 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001692 ScopedObjectAccessUnchecked soa(self);
1693 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001694 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001695 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001696 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001697 }
1698}
1699
Mathieu Chartiere401d142015-04-22 13:56:20 -07001700void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1701 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1702 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001703 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1704 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1705}
1706
Mathieu Chartiere401d142015-04-22 13:56:20 -07001707void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1708 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1709 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001710 result->SetZ(JNI_TRUE);
1711}
1712
Mathieu Chartiere401d142015-04-22 13:56:20 -07001713void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1714 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1715 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001716 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1717 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1718 jint expectedValue = args[3];
1719 jint newValue = args[4];
1720 bool success;
1721 if (Runtime::Current()->IsActiveTransaction()) {
1722 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1723 expectedValue, newValue);
1724 } else {
1725 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1726 expectedValue, newValue);
1727 }
1728 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1729}
1730
Narayan Kamath34a316f2016-03-30 13:11:18 +01001731void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1732 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1733 uint32_t* args, JValue* result) {
1734 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1735 if (obj == nullptr) {
1736 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1737 return;
1738 }
1739
1740 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1741 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1742}
1743
Mathieu Chartiere401d142015-04-22 13:56:20 -07001744void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1745 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1746 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001747 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1748 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1749 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1750 if (Runtime::Current()->IsActiveTransaction()) {
1751 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1752 } else {
1753 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1754 }
1755}
1756
Andreas Gampe799681b2015-05-15 19:24:12 -07001757void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001758 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1759 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001760 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1761 Primitive::Type primitive_type = component->GetPrimitiveType();
1762 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1763}
1764
Andreas Gampe799681b2015-05-15 19:24:12 -07001765void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001766 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1767 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001768 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1769 Primitive::Type primitive_type = component->GetPrimitiveType();
1770 result->SetI(Primitive::ComponentSize(primitive_type));
1771}
1772
Andreas Gampedd9d0552015-03-09 12:57:41 -07001773typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001774 size_t arg_size);
1775
Mathieu Chartiere401d142015-04-22 13:56:20 -07001776typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001777 uint32_t* args, JValue* result);
1778
1779static bool tables_initialized_ = false;
1780static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1781static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1782
Andreas Gampe799681b2015-05-15 19:24:12 -07001783void UnstartedRuntime::InitializeInvokeHandlers() {
1784#define UNSTARTED_DIRECT(ShortName, Sig) \
1785 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1786#include "unstarted_runtime_list.h"
1787 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1788#undef UNSTARTED_RUNTIME_DIRECT_LIST
1789#undef UNSTARTED_RUNTIME_JNI_LIST
1790#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001791}
1792
Andreas Gampe799681b2015-05-15 19:24:12 -07001793void UnstartedRuntime::InitializeJNIHandlers() {
1794#define UNSTARTED_JNI(ShortName, Sig) \
1795 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1796#include "unstarted_runtime_list.h"
1797 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1798#undef UNSTARTED_RUNTIME_DIRECT_LIST
1799#undef UNSTARTED_RUNTIME_JNI_LIST
1800#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001801}
1802
Andreas Gampe799681b2015-05-15 19:24:12 -07001803void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001804 CHECK(!tables_initialized_);
1805
Andreas Gampe799681b2015-05-15 19:24:12 -07001806 InitializeInvokeHandlers();
1807 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001808
1809 tables_initialized_ = true;
1810}
1811
Andreas Gampe799681b2015-05-15 19:24:12 -07001812void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1813 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001814 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1815 // problems in core libraries.
1816 CHECK(tables_initialized_);
1817
David Sehr709b0702016-10-13 09:12:37 -07001818 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001819 const auto& iter = invoke_handlers_.find(name);
1820 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001821 // Clear out the result in case it's not zeroed out.
1822 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001823
1824 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1825 self->PushShadowFrame(shadow_frame);
1826
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001827 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001828
1829 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001830 } else {
1831 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001832 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001833 }
1834}
1835
1836// 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 -07001837void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001838 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001839 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001840 const auto& iter = jni_handlers_.find(name);
1841 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001842 // Clear out the result in case it's not zeroed out.
1843 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001844 (*iter->second)(self, method, receiver, args, result);
1845 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001846 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1847 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001848 } else {
David Sehr709b0702016-10-13 09:12:37 -07001849 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001850 "non-transactional runtime";
1851 }
1852}
1853
1854} // namespace interpreter
1855} // namespace art