blob: feb6e0857a1541e8cb866de8f9bd99ff30af2de5 [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 Gampe13fc1be2016-04-05 20:14:30 -070024#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070025#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include <unordered_map>
27
Andreas Gampe46ee31b2016-12-14 10:11:49 -080028#include "android-base/stringprintf.h"
Andreas Gampeaacc25d2015-04-01 14:49:06 -070029#include "ScopedLocalRef.h"
30
Mathieu Chartiere401d142015-04-22 13:56:20 -070031#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080032#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070033#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070034#include "base/logging.h"
35#include "base/macros.h"
36#include "class_linker.h"
37#include "common_throws.h"
38#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080039#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070040#include "handle_scope-inl.h"
41#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070042#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070044#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070045#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010046#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070047#include "mirror/object-inl.h"
48#include "mirror/object_array-inl.h"
49#include "mirror/string-inl.h"
50#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070051#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070052#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020053#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070054#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070055#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070056
57namespace art {
58namespace interpreter {
59
Andreas Gampe46ee31b2016-12-14 10:11:49 -080060using android::base::StringAppendV;
61using android::base::StringPrintf;
62
Andreas Gampe068b0c02015-03-11 12:44:47 -070063static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020064 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070065 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020066
67static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070068 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070069 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020070 va_start(args, fmt);
71 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 va_end(args);
73 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020074 va_start(args, fmt);
75 std::string msg;
76 StringAppendV(&msg, fmt, args);
77 va_end(args);
78 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070079 UNREACHABLE();
80 }
81}
82
Andreas Gampe8ce9c302016-04-15 21:24:28 -070083// Restricted support for character upper case / lower case. Only support ASCII, where
84// it's easy. Abort the transaction otherwise.
85static void CharacterLowerUpper(Thread* self,
86 ShadowFrame* shadow_frame,
87 JValue* result,
88 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070089 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070090 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
91
92 // Only ASCII (7-bit).
93 if (!isascii(int_value)) {
94 AbortTransactionOrFail(self,
95 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
96 int_value);
97 return;
98 }
99
100 std::locale c_locale("C");
101 char char_value = static_cast<char>(int_value);
102
103 if (to_lower_case) {
104 result->SetI(std::tolower(char_value, c_locale));
105 } else {
106 result->SetI(std::toupper(char_value, c_locale));
107 }
108}
109
110void UnstartedRuntime::UnstartedCharacterToLowerCase(
111 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
112 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
113}
114
115void UnstartedRuntime::UnstartedCharacterToUpperCase(
116 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
117 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
118}
119
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700120// Helper function to deal with class loading in an unstarted runtime.
121static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
122 Handle<mirror::ClassLoader> class_loader, JValue* result,
123 const std::string& method_name, bool initialize_class,
124 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700125 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700126 CHECK(className.Get() != nullptr);
127 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
128 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
129
130 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
131 if (found == nullptr && abort_if_not_found) {
132 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700133 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700134 method_name.c_str(),
135 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700136 }
137 return;
138 }
139 if (found != nullptr && initialize_class) {
140 StackHandleScope<1> hs(self);
141 Handle<mirror::Class> h_class(hs.NewHandle(found));
142 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
143 CHECK(self->IsExceptionPending());
144 return;
145 }
146 }
147 result->SetL(found);
148}
149
150// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
151// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
152// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200153// actually the transaction abort exception. This must not be wrapped, as it signals an
154// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700155static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700156 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700157 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200158 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700159 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200160 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700161 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
162 "ClassNotFoundException");
163 }
164 }
165}
166
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700167static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700168 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700169 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
170 if (param == nullptr) {
171 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
172 return nullptr;
173 }
174 return param->AsString();
175}
176
Andreas Gampe799681b2015-05-15 19:24:12 -0700177void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700179 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
180 if (class_name == nullptr) {
181 return;
182 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700183 StackHandleScope<1> hs(self);
184 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800185 UnstartedRuntimeFindClass(self,
186 h_class_name,
187 ScopedNullHandle<mirror::ClassLoader>(),
188 result,
189 "Class.forName",
190 true,
191 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700192 CheckExceptionGenerateClassNotFound(self);
193}
194
Andreas Gampe799681b2015-05-15 19:24:12 -0700195void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700196 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700197 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
198 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700199 return;
200 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700201 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
202 mirror::ClassLoader* class_loader =
203 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
204 StackHandleScope<2> hs(self);
205 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
206 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
207 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
208 initialize_class, false);
209 CheckExceptionGenerateClassNotFound(self);
210}
211
Andreas Gampe799681b2015-05-15 19:24:12 -0700212void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700213 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700214 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
215 if (class_name == nullptr) {
216 return;
217 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700218 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
219 mirror::ClassLoader* class_loader =
220 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
221 StackHandleScope<2> hs(self);
222 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
223 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
224 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
225 initialize_class, false);
226 CheckExceptionGenerateClassNotFound(self);
227}
228
Andreas Gampe799681b2015-05-15 19:24:12 -0700229void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700230 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
231 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700232 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
233 if (param == nullptr) {
234 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
235 return;
236 }
237 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700238 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700239
240 // Check that it's not null.
241 if (h_klass.Get() == nullptr) {
242 AbortTransactionOrFail(self, "Class reference is null for newInstance");
243 return;
244 }
245
246 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
247 if (Runtime::Current()->IsActiveTransaction()) {
248 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200249 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700250 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700251 return;
252 }
253 }
254
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700255 // There are two situations in which we'll abort this run.
256 // 1) If the class isn't yet initialized and initialization fails.
257 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
258 // Note that 2) could likely be handled here, but for safety abort the transaction.
259 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 auto* cl = Runtime::Current()->GetClassLinker();
261 if (cl->EnsureInitialized(self, h_klass, true, true)) {
262 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
263 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700264 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
265 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700266 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700267 if (!self->IsExceptionPending()) {
268 result->SetL(h_obj.Get());
269 ok = true;
270 }
271 } else {
272 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
273 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700274 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700275 }
276 }
277 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700278 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700279 h_klass->PrettyClass().c_str(),
280 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700281 }
282}
283
Andreas Gampe799681b2015-05-15 19:24:12 -0700284void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700285 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700286 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
287 // going the reflective Dex way.
288 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
289 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700290 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700291 for (ArtField& field : klass->GetIFields()) {
292 if (name2->Equals(field.GetName())) {
293 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700294 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700295 }
296 }
297 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700298 for (ArtField& field : klass->GetSFields()) {
299 if (name2->Equals(field.GetName())) {
300 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700301 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700302 }
303 }
304 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700305 if (found == nullptr) {
306 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
307 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700308 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700309 return;
310 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700311 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700312 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700313 mirror::Field* field;
314 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700315 if (pointer_size == PointerSize::k64) {
316 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
317 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700318 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700319 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
320 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700321 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700322 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700323 if (pointer_size == PointerSize::k64) {
324 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
325 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700326 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700327 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
328 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700329 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700330 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700331 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700332}
333
Andreas Gampebc4d2182016-02-22 10:03:12 -0800334// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
335void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
336 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
337 // Special managed code cut-out to allow method lookup in a un-started runtime.
338 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
339 if (klass == nullptr) {
340 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
341 return;
342 }
343 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
344 mirror::ObjectArray<mirror::Class>* args =
345 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700346 Runtime* runtime = Runtime::Current();
347 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700348 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700349 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700350 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700351 if (pointer_size == PointerSize::k64) {
352 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
353 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700354 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700355 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
356 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700357 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800358 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700359 if (pointer_size == PointerSize::k64) {
360 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
361 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700362 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700363 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
364 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700365 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800366 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700367 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800368}
369
Andreas Gampe6039e562016-04-05 18:18:43 -0700370// Special managed code cut-out to allow constructor lookup in a un-started runtime.
371void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
372 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
373 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
374 if (klass == nullptr) {
375 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
376 return;
377 }
378 mirror::ObjectArray<mirror::Class>* args =
379 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700380 Runtime* runtime = Runtime::Current();
381 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700382 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700383 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700384 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700385 if (pointer_size == PointerSize::k64) {
386 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
387 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700388 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700389 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
390 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700391 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700392 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700393 if (pointer_size == PointerSize::k64) {
394 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
395 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700396 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700397 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
398 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700399 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700400 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700401 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700402}
403
Andreas Gampe633750c2016-02-19 10:49:50 -0800404void UnstartedRuntime::UnstartedClassGetEnclosingClass(
405 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
406 StackHandleScope<1> hs(self);
407 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
408 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
409 result->SetL(nullptr);
410 }
David Sehr9323e6e2016-09-13 08:58:35 -0700411 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800412}
413
Andreas Gampe715fdc22016-04-18 17:07:30 -0700414void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
415 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
416 StackHandleScope<1> hs(self);
417 Handle<mirror::Class> klass(hs.NewHandle(
418 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
419 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
420 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
421}
422
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700423static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
424 const char* entry_name,
425 size_t* size,
426 std::string* error_msg) {
427 CHECK(size != nullptr);
428
429 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
430 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800431 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700432 }
433 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
434 if (zip_entry == nullptr) {
435 return nullptr;
436 }
437 std::unique_ptr<MemMap> tmp_map(
438 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
439 if (tmp_map == nullptr) {
440 return nullptr;
441 }
442
443 // OK, from here everything seems fine.
444 *size = zip_entry->GetUncompressedLength();
445 return tmp_map;
446}
447
448static void GetResourceAsStream(Thread* self,
449 ShadowFrame* shadow_frame,
450 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700451 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700452 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
453 if (resource_obj == nullptr) {
454 AbortTransactionOrFail(self, "null name for getResourceAsStream");
455 return;
456 }
457 CHECK(resource_obj->IsString());
458 mirror::String* resource_name = resource_obj->AsString();
459
460 std::string resource_name_str = resource_name->ToModifiedUtf8();
461 if (resource_name_str.empty() || resource_name_str == "/") {
462 AbortTransactionOrFail(self,
463 "Unsupported name %s for getResourceAsStream",
464 resource_name_str.c_str());
465 return;
466 }
467 const char* resource_cstr = resource_name_str.c_str();
468 if (resource_cstr[0] == '/') {
469 resource_cstr++;
470 }
471
472 Runtime* runtime = Runtime::Current();
473
474 std::vector<std::string> split;
475 Split(runtime->GetBootClassPathString(), ':', &split);
476 if (split.empty()) {
477 AbortTransactionOrFail(self,
478 "Boot classpath not set or split error:: %s",
479 runtime->GetBootClassPathString().c_str());
480 return;
481 }
482
483 std::unique_ptr<MemMap> mem_map;
484 size_t map_size;
485 std::string last_error_msg; // Only store the last message (we could concatenate).
486
487 for (const std::string& jar_file : split) {
488 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
489 if (mem_map != nullptr) {
490 break;
491 }
492 }
493
494 if (mem_map == nullptr) {
495 // Didn't find it. There's a good chance this will be the same at runtime, but still
496 // conservatively abort the transaction here.
497 AbortTransactionOrFail(self,
498 "Could not find resource %s. Last error was %s.",
499 resource_name_str.c_str(),
500 last_error_msg.c_str());
501 return;
502 }
503
504 StackHandleScope<3> hs(self);
505
506 // Create byte array for content.
507 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
508 if (h_array.Get() == nullptr) {
509 AbortTransactionOrFail(self, "Could not find/create byte array class");
510 return;
511 }
512 // Copy in content.
513 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
514 // Be proactive releasing memory.
515 mem_map.release();
516
517 // Create a ByteArrayInputStream.
518 Handle<mirror::Class> h_class(hs.NewHandle(
519 runtime->GetClassLinker()->FindClass(self,
520 "Ljava/io/ByteArrayInputStream;",
521 ScopedNullHandle<mirror::ClassLoader>())));
522 if (h_class.Get() == nullptr) {
523 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
524 return;
525 }
526 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
527 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
528 return;
529 }
530
531 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
532 if (h_obj.Get() == nullptr) {
533 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
534 return;
535 }
536
537 auto* cl = Runtime::Current()->GetClassLinker();
538 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
539 "<init>", "([B)V", cl->GetImagePointerSize());
540 if (constructor == nullptr) {
541 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
542 return;
543 }
544
545 uint32_t args[1];
546 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
547 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
548
549 if (self->IsExceptionPending()) {
550 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
551 return;
552 }
553
554 result->SetL(h_obj.Get());
555}
556
557void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
558 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
559 {
560 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
561 CHECK(this_obj != nullptr);
562 CHECK(this_obj->IsClassLoader());
563
564 StackHandleScope<1> hs(self);
565 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
566
567 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
568 this_classloader_class.Get()) {
569 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700570 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700571 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700572 return;
573 }
574 }
575
576 GetResourceAsStream(self, shadow_frame, result, arg_offset);
577}
578
Andreas Gampe799681b2015-05-15 19:24:12 -0700579void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700580 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700581 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
582 mirror::ClassLoader* class_loader =
583 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
584 StackHandleScope<2> hs(self);
585 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
586 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
587 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
588 "VMClassLoader.findLoadedClass", false, false);
589 // This might have an error pending. But semantics are to just return null.
590 if (self->IsExceptionPending()) {
591 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700592 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700593 if (type != "java.lang.InternalError") {
594 self->ClearException();
595 }
596 }
597}
598
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599void UnstartedRuntime::UnstartedVoidLookupType(
600 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
601 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700602 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
603}
604
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700605// Arraycopy emulation.
606// Note: we can't use any fast copy functions, as they are not available under transaction.
607
608template <typename T>
609static void PrimitiveArrayCopy(Thread* self,
610 mirror::Array* src_array, int32_t src_pos,
611 mirror::Array* dst_array, int32_t dst_pos,
612 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700613 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700614 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700615 AbortTransactionOrFail(self,
616 "Types mismatched in arraycopy: %s vs %s.",
617 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700618 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700619 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700620 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700621 return;
622 }
623 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
624 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
625 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
626 if (copy_forward) {
627 for (int32_t i = 0; i < length; ++i) {
628 dst->Set(dst_pos + i, src->Get(src_pos + i));
629 }
630 } else {
631 for (int32_t i = 1; i <= length; ++i) {
632 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
633 }
634 }
635}
636
Andreas Gampe799681b2015-05-15 19:24:12 -0700637void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700638 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700639 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700640 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
641 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700642 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700643
Andreas Gampe85a098a2016-03-31 13:30:53 -0700644 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
645 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
646 // Null checking. For simplicity, abort transaction.
647 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700648 AbortTransactionOrFail(self, "src is null in arraycopy.");
649 return;
650 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700651 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700652 AbortTransactionOrFail(self, "dst is null in arraycopy.");
653 return;
654 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700655 // Test for arrayness. Throw ArrayStoreException.
656 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
657 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
658 return;
659 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700660
Andreas Gampe85a098a2016-03-31 13:30:53 -0700661 mirror::Array* src_array = src_obj->AsArray();
662 mirror::Array* dst_array = dst_obj->AsArray();
663
664 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700665 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
666 UNLIKELY(src_pos > src_array->GetLength() - length) ||
667 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700668 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700669 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
670 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
671 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700672 return;
673 }
674
675 // Type checking.
676 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
677 GetComponentType();
678
679 if (!src_type->IsPrimitive()) {
680 // Check that the second type is not primitive.
681 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
682 GetComponentType();
683 if (trg_type->IsPrimitiveInt()) {
684 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700685 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700686 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700687 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700688 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700689 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700690 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700691
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700692 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
693 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
694 if (src == dst) {
695 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700696 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700697 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
698 if (copy_forward) {
699 for (int32_t i = 0; i < length; ++i) {
700 dst->Set(dst_pos + i, src->Get(src_pos + i));
701 }
702 } else {
703 for (int32_t i = 1; i <= length; ++i) {
704 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
705 }
706 }
707 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700708 // We're being lazy here. Optimally this could be a memcpy (if component types are
709 // assignable), but the ObjectArray implementation doesn't support transactions. The
710 // checking version, however, does.
711 if (Runtime::Current()->IsActiveTransaction()) {
712 dst->AssignableCheckingMemcpy<true>(
713 dst_pos, src, src_pos, length, true /* throw_exception */);
714 } else {
715 dst->AssignableCheckingMemcpy<false>(
716 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700717 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700718 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700719 } else if (src_type->IsPrimitiveByte()) {
720 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700721 } else if (src_type->IsPrimitiveChar()) {
722 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
723 } else if (src_type->IsPrimitiveInt()) {
724 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700725 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700726 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700727 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700728 }
729}
730
Andreas Gampe5c9af612016-04-05 14:16:10 -0700731void UnstartedRuntime::UnstartedSystemArraycopyByte(
732 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
733 // Just forward.
734 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
735}
736
Andreas Gampe799681b2015-05-15 19:24:12 -0700737void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700738 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700739 // Just forward.
740 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
741}
742
743void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700744 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700745 // Just forward.
746 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
747}
748
Narayan Kamath34a316f2016-03-30 13:11:18 +0100749void UnstartedRuntime::UnstartedSystemGetSecurityManager(
750 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
751 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
752 result->SetL(nullptr);
753}
754
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700755static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
756
757static void GetSystemProperty(Thread* self,
758 ShadowFrame* shadow_frame,
759 JValue* result,
760 size_t arg_offset,
761 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700762 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700763 StackHandleScope<4> hs(self);
764 Handle<mirror::String> h_key(
765 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
766 if (h_key.Get() == nullptr) {
767 AbortTransactionOrFail(self, "getProperty key was null");
768 return;
769 }
770
771 // This is overall inefficient, but reflecting the values here is not great, either. So
772 // for simplicity, and with the assumption that the number of getProperty calls is not
773 // too great, just iterate each time.
774
775 // Get the storage class.
776 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
777 Handle<mirror::Class> h_props_class(hs.NewHandle(
778 class_linker->FindClass(self,
779 "Ljava/lang/AndroidHardcodedSystemProperties;",
780 ScopedNullHandle<mirror::ClassLoader>())));
781 if (h_props_class.Get() == nullptr) {
782 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
783 return;
784 }
785 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
786 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
787 return;
788 }
789
790 // Get the storage array.
791 ArtField* static_properties =
792 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
793 "[[Ljava/lang/String;");
794 if (static_properties == nullptr) {
795 AbortTransactionOrFail(self,
796 "Could not find %s field",
797 kAndroidHardcodedSystemPropertiesFieldName);
798 return;
799 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700800 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
801 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
802 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700803 if (h_2string_array.Get() == nullptr) {
804 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
805 return;
806 }
807
808 // Iterate over it.
809 const int32_t prop_count = h_2string_array->GetLength();
810 // Use the third handle as mutable.
811 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
812 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
813 for (int32_t i = 0; i < prop_count; ++i) {
814 h_string_array.Assign(h_2string_array->Get(i));
815 if (h_string_array.Get() == nullptr ||
816 h_string_array->GetLength() != 2 ||
817 h_string_array->Get(0) == nullptr) {
818 AbortTransactionOrFail(self,
819 "Unexpected content of %s",
820 kAndroidHardcodedSystemPropertiesFieldName);
821 return;
822 }
823 if (h_key->Equals(h_string_array->Get(0))) {
824 // Found a value.
825 if (h_string_array->Get(1) == nullptr && is_default_version) {
826 // Null is being delegated to the default map, and then resolved to the given default value.
827 // As there's no default map, return the given value.
828 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
829 } else {
830 result->SetL(h_string_array->Get(1));
831 }
832 return;
833 }
834 }
835
836 // Key is not supported.
837 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
838}
839
840void UnstartedRuntime::UnstartedSystemGetProperty(
841 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
842 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
843}
844
845void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
846 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
847 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
848}
849
Andreas Gampe799681b2015-05-15 19:24:12 -0700850void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700851 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -0700852 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700853 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100854 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
855 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700856 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100857 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700858 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100859 } else if (caller ==
860 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
861 // Allocate new object.
862 StackHandleScope<2> hs(self);
863 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
864 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
865 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
866 h_real_to_string_class->AllocObject(self)));
867 if (h_real_to_string_obj.Get() != nullptr) {
868 auto* cl = Runtime::Current()->GetClassLinker();
869 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
870 "<init>", "()V", cl->GetImagePointerSize());
871 if (init_method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700872 h_real_to_string_class->DumpClass(LOG_STREAM(FATAL), mirror::Class::kDumpClassFullDetail);
Narayan Kamatha1e93122016-03-30 15:41:54 +0100873 } else {
874 JValue invoke_result;
875 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
876 nullptr);
877 if (!self->IsExceptionPending()) {
878 result->SetL(h_real_to_string_obj.Get());
879 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700880 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700881 }
882 }
883 }
884
885 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700886 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700887 }
888}
889
Sergio Giro83261202016-04-11 20:49:20 +0100890void UnstartedRuntime::UnstartedMathCeil(
891 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700892 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100893}
894
895void UnstartedRuntime::UnstartedMathFloor(
896 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700897 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700898}
899
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700900void UnstartedRuntime::UnstartedMathSin(
901 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
902 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
903}
904
905void UnstartedRuntime::UnstartedMathCos(
906 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
907 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
908}
909
910void UnstartedRuntime::UnstartedMathPow(
911 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
912 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
913 shadow_frame->GetVRegDouble(arg_offset + 2)));
914}
915
Andreas Gampe799681b2015-05-15 19:24:12 -0700916void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700917 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700918 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
919 result->SetI(obj->IdentityHashCode());
920}
921
Andreas Gampe799681b2015-05-15 19:24:12 -0700922void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700923 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700924 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000925 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700926}
927
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700928static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700929 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700930 const DexFile* dex_file = dex_cache->GetDexFile();
931 if (dex_file == nullptr) {
932 return nullptr;
933 }
934
935 // Create the direct byte buffer.
936 JNIEnv* env = self->GetJniEnv();
937 DCHECK(env != nullptr);
938 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700939 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
940 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700941 DCHECK(self->IsExceptionPending());
942 return nullptr;
943 }
944
945 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700946 args[0].l = byte_buffer.get();
947
948 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
949 WellKnownClasses::com_android_dex_Dex,
950 WellKnownClasses::com_android_dex_Dex_create,
951 args));
952
953 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700954}
955
Andreas Gampe799681b2015-05-15 19:24:12 -0700956void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700958 // We will create the Dex object, but the image writer will release it before creating the
959 // art file.
960 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
961 bool have_dex = false;
962 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700963 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700964 if (dex != nullptr) {
965 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700966 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700967 }
968 }
969 if (!have_dex) {
970 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200971 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700972 }
973}
974
975static void UnstartedMemoryPeek(
976 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
977 int64_t address = shadow_frame->GetVRegLong(arg_offset);
978 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
979 // aborting the transaction.
980
981 switch (type) {
982 case Primitive::kPrimByte: {
983 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
984 return;
985 }
986
987 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700988 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
989 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700990 return;
991 }
992
993 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700994 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
995 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700996 return;
997 }
998
999 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001000 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1001 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001002 return;
1003 }
1004
1005 case Primitive::kPrimBoolean:
1006 case Primitive::kPrimChar:
1007 case Primitive::kPrimFloat:
1008 case Primitive::kPrimDouble:
1009 case Primitive::kPrimVoid:
1010 case Primitive::kPrimNot:
1011 LOG(FATAL) << "Not in the Memory API: " << type;
1012 UNREACHABLE();
1013 }
1014 LOG(FATAL) << "Should not reach here";
1015 UNREACHABLE();
1016}
1017
Andreas Gampe799681b2015-05-15 19:24:12 -07001018void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001020 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1021}
1022
1023void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001024 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001025 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1026}
1027
1028void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001029 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001030 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1031}
1032
1033void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001034 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001035 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001036}
1037
1038static void UnstartedMemoryPeekArray(
1039 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001040 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001041 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1042 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1043 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001044 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001045 return;
1046 }
1047 mirror::Array* array = obj->AsArray();
1048
1049 int offset = shadow_frame->GetVReg(arg_offset + 3);
1050 int count = shadow_frame->GetVReg(arg_offset + 4);
1051 if (offset < 0 || offset + count > array->GetLength()) {
1052 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1053 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001054 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001055 return;
1056 }
1057
1058 switch (type) {
1059 case Primitive::kPrimByte: {
1060 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1061 mirror::ByteArray* byte_array = array->AsByteArray();
1062 for (int32_t i = 0; i < count; ++i, ++address) {
1063 byte_array->SetWithoutChecks<true>(i + offset, *address);
1064 }
1065 return;
1066 }
1067
1068 case Primitive::kPrimShort:
1069 case Primitive::kPrimInt:
1070 case Primitive::kPrimLong:
1071 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1072 UNREACHABLE();
1073
1074 case Primitive::kPrimBoolean:
1075 case Primitive::kPrimChar:
1076 case Primitive::kPrimFloat:
1077 case Primitive::kPrimDouble:
1078 case Primitive::kPrimVoid:
1079 case Primitive::kPrimNot:
1080 LOG(FATAL) << "Not in the Memory API: " << type;
1081 UNREACHABLE();
1082 }
1083 LOG(FATAL) << "Should not reach here";
1084 UNREACHABLE();
1085}
1086
Andreas Gampe799681b2015-05-15 19:24:12 -07001087void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001088 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001089 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001090}
1091
Kenny Root1c9e61c2015-05-14 15:58:17 -07001092// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001093void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001094 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001095 jint start = shadow_frame->GetVReg(arg_offset + 1);
1096 jint end = shadow_frame->GetVReg(arg_offset + 2);
1097 jint index = shadow_frame->GetVReg(arg_offset + 4);
1098 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1099 if (string == nullptr) {
1100 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1101 return;
1102 }
Kenny Root57f91e82015-05-14 15:58:17 -07001103 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001104 DCHECK_LE(start, end);
1105 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001106 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001107 Handle<mirror::CharArray> h_char_array(
1108 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001109 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001110 DCHECK_LE(index, h_char_array->GetLength());
1111 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001112 string->GetChars(start, end, h_char_array, index);
1113}
1114
1115// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001116void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001118 jint index = shadow_frame->GetVReg(arg_offset + 1);
1119 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1120 if (string == nullptr) {
1121 AbortTransactionOrFail(self, "String.charAt with null object");
1122 return;
1123 }
1124 result->SetC(string->CharAt(index));
1125}
1126
Kenny Root57f91e82015-05-14 15:58:17 -07001127// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001128void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001130 jint index = shadow_frame->GetVReg(arg_offset + 1);
1131 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1132 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1133 if (string == nullptr) {
1134 AbortTransactionOrFail(self, "String.setCharAt with null object");
1135 return;
1136 }
1137 string->SetCharAt(index, c);
1138}
1139
Kenny Root1c9e61c2015-05-14 15:58:17 -07001140// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001141void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001142 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001143 jint offset = shadow_frame->GetVReg(arg_offset);
1144 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1145 DCHECK_GE(char_count, 0);
1146 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001147 Handle<mirror::CharArray> h_char_array(
1148 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001149 Runtime* runtime = Runtime::Current();
1150 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1151 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1152}
1153
1154// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001155void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001157 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1158 if (to_copy == nullptr) {
1159 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1160 return;
1161 }
1162 StackHandleScope<1> hs(self);
1163 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1164 Runtime* runtime = Runtime::Current();
1165 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1166 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1167 allocator));
1168}
1169
Andreas Gampe799681b2015-05-15 19:24:12 -07001170void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001171 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001172 jint start = shadow_frame->GetVReg(arg_offset + 1);
1173 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001174 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001175 DCHECK_GE(length, 0);
1176 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001177 Handle<mirror::String> h_string(
1178 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001179 DCHECK_LE(start, h_string->GetLength());
1180 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001181 Runtime* runtime = Runtime::Current();
1182 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1183 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1184}
1185
Kenny Root57f91e82015-05-14 15:58:17 -07001186// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001187void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001188 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001189 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001190 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1191 if (string == nullptr) {
1192 AbortTransactionOrFail(self, "String.charAt with null object");
1193 return;
1194 }
1195 result->SetL(string->ToCharArray(self));
1196}
1197
Andreas Gampebc4d2182016-02-22 10:03:12 -08001198// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1199void UnstartedRuntime::UnstartedReferenceGetReferent(
1200 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001201 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001202 shadow_frame->GetVRegReference(arg_offset));
1203 if (ref == nullptr) {
1204 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1205 return;
1206 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001207 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001208 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1209 result->SetL(referent);
1210}
1211
1212// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1213// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1214// where we can predict the behavior (somewhat).
1215// Note: this is required (instead of lazy initialization) as these classes are used in the static
1216// initialization of other classes, so will *use* the value.
1217void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1218 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -07001219 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampebc4d2182016-02-22 10:03:12 -08001220 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1221 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1222 // 8 as a conservative upper approximation.
1223 result->SetI(8);
1224 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1225 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1226 // a good upper bound.
1227 // TODO: Consider resetting in the zygote?
1228 result->SetI(8);
1229 } else {
1230 // Not supported.
1231 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1232 }
1233}
1234
1235// This allows accessing ConcurrentHashMap/SynchronousQueue.
1236
1237void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1238 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1239 // Argument 0 is the Unsafe instance, skip.
1240 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1241 if (obj == nullptr) {
1242 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1243 return;
1244 }
1245 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1246 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1247 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001248 bool success;
1249 // Check whether we're in a transaction, call accordingly.
1250 if (Runtime::Current()->IsActiveTransaction()) {
1251 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1252 expectedValue,
1253 newValue);
1254 } else {
1255 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1256 expectedValue,
1257 newValue);
1258 }
1259 result->SetZ(success ? 1 : 0);
1260}
1261
1262void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1263 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1264 // Argument 0 is the Unsafe instance, skip.
1265 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1266 if (obj == nullptr) {
1267 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1268 return;
1269 }
1270 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1271 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1272 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1273
1274 // Must use non transactional mode.
1275 if (kUseReadBarrier) {
1276 // Need to make sure the reference stored in the field is a to-space one before attempting the
1277 // CAS or the CAS could fail incorrectly.
1278 mirror::HeapReference<mirror::Object>* field_addr =
1279 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1280 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001281 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001282 obj,
1283 MemberOffset(offset),
1284 field_addr);
1285 }
1286 bool success;
1287 // Check whether we're in a transaction, call accordingly.
1288 if (Runtime::Current()->IsActiveTransaction()) {
1289 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1290 expected_value,
1291 newValue);
1292 } else {
1293 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1294 expected_value,
1295 newValue);
1296 }
1297 result->SetZ(success ? 1 : 0);
1298}
1299
1300void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1301 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001302 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001303 // Argument 0 is the Unsafe instance, skip.
1304 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1305 if (obj == nullptr) {
1306 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1307 return;
1308 }
1309 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1310 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1311 result->SetL(value);
1312}
1313
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001314void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1315 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001316 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001317 // Argument 0 is the Unsafe instance, skip.
1318 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1319 if (obj == nullptr) {
1320 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1321 return;
1322 }
1323 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1324 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1325 if (Runtime::Current()->IsActiveTransaction()) {
1326 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1327 } else {
1328 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1329 }
1330}
1331
Andreas Gampebc4d2182016-02-22 10:03:12 -08001332void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1333 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001334 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001335 // Argument 0 is the Unsafe instance, skip.
1336 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1337 if (obj == nullptr) {
1338 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1339 return;
1340 }
1341 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1342 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1343 QuasiAtomic::ThreadFenceRelease();
1344 if (Runtime::Current()->IsActiveTransaction()) {
1345 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1346 } else {
1347 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1348 }
1349}
1350
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001351// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1352// of correctly handling the corner cases.
1353void UnstartedRuntime::UnstartedIntegerParseInt(
1354 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001355 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001356 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1357 if (obj == nullptr) {
1358 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1359 return;
1360 }
1361
1362 std::string string_value = obj->AsString()->ToModifiedUtf8();
1363 if (string_value.empty()) {
1364 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1365 return;
1366 }
1367
1368 const char* c_str = string_value.c_str();
1369 char *end;
1370 // Can we set errno to 0? Is this always a variable, and not a macro?
1371 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1372 int64_t l = strtol(c_str, &end, 10);
1373
1374 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1375 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1376 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1377 return;
1378 }
1379 if (l == 0) {
1380 // Check whether the string wasn't exactly zero.
1381 if (string_value != "0") {
1382 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1383 return;
1384 }
1385 } else if (*end != '\0') {
1386 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1387 return;
1388 }
1389
1390 result->SetI(static_cast<int32_t>(l));
1391}
1392
1393// A cutout for Long.parseLong.
1394//
1395// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1396// well.
1397void UnstartedRuntime::UnstartedLongParseLong(
1398 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001399 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001400 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1401 if (obj == nullptr) {
1402 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1403 return;
1404 }
1405
1406 std::string string_value = obj->AsString()->ToModifiedUtf8();
1407 if (string_value.empty()) {
1408 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1409 return;
1410 }
1411
1412 const char* c_str = string_value.c_str();
1413 char *end;
1414 // Can we set errno to 0? Is this always a variable, and not a macro?
1415 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1416 int64_t l = strtol(c_str, &end, 10);
1417
1418 // Note: comparing against int32_t min/max is intentional here.
1419 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1420 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1421 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1422 return;
1423 }
1424 if (l == 0) {
1425 // Check whether the string wasn't exactly zero.
1426 if (string_value != "0") {
1427 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1428 return;
1429 }
1430 } else if (*end != '\0') {
1431 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1432 return;
1433 }
1434
1435 result->SetJ(l);
1436}
1437
Andreas Gampe715fdc22016-04-18 17:07:30 -07001438void UnstartedRuntime::UnstartedMethodInvoke(
1439 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001440 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001441 JNIEnvExt* env = self->GetJniEnv();
1442 ScopedObjectAccessUnchecked soa(self);
1443
Mathieu Chartier8778c522016-10-04 19:06:30 -07001444 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001445 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001446 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001447
Mathieu Chartier8778c522016-10-04 19:06:30 -07001448 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001449 ScopedLocalRef<jobject> java_receiver(env,
1450 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1451
Mathieu Chartier8778c522016-10-04 19:06:30 -07001452 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001453 ScopedLocalRef<jobject> java_args(env,
1454 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1455
1456 ScopedLocalRef<jobject> result_jobj(env,
1457 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1458
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001459 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001460
1461 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1462 // InvocationTargetExceptions.
1463 if (self->IsExceptionPending()) {
1464 AbortTransactionOrFail(self, "Failed Method.invoke");
1465 }
1466}
1467
Andreas Gampebc4d2182016-02-22 10:03:12 -08001468
Mathieu Chartiere401d142015-04-22 13:56:20 -07001469void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1470 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1471 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001472 int32_t length = args[1];
1473 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001474 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001475 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001476 ObjPtr<mirror::Class> array_class =
1477 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001478 DCHECK(array_class != nullptr);
1479 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001480 result->SetL(mirror::Array::Alloc<true, true>(self,
1481 array_class,
1482 length,
1483 array_class->GetComponentSizeShift(),
1484 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001485}
1486
Mathieu Chartiere401d142015-04-22 13:56:20 -07001487void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1488 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1489 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001490 result->SetL(nullptr);
1491}
1492
Mathieu Chartiere401d142015-04-22 13:56:20 -07001493void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1494 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1495 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001496 NthCallerVisitor visitor(self, 3);
1497 visitor.WalkStack();
1498 if (visitor.caller != nullptr) {
1499 result->SetL(visitor.caller->GetDeclaringClass());
1500 }
1501}
1502
Mathieu Chartiere401d142015-04-22 13:56:20 -07001503void UnstartedRuntime::UnstartedJNIMathLog(
1504 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1505 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001506 JValue value;
1507 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1508 result->SetD(log(value.GetD()));
1509}
1510
Mathieu Chartiere401d142015-04-22 13:56:20 -07001511void UnstartedRuntime::UnstartedJNIMathExp(
1512 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1513 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001514 JValue value;
1515 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1516 result->SetD(exp(value.GetD()));
1517}
1518
Andreas Gampebc4d2182016-02-22 10:03:12 -08001519void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1520 Thread* self ATTRIBUTE_UNUSED,
1521 ArtMethod* method ATTRIBUTE_UNUSED,
1522 mirror::Object* receiver ATTRIBUTE_UNUSED,
1523 uint32_t* args ATTRIBUTE_UNUSED,
1524 JValue* result) {
1525 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1526 ? 0
1527 : 1);
1528}
1529
Mathieu Chartiere401d142015-04-22 13:56:20 -07001530void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1531 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1532 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001533 StackHandleScope<1> hs(self);
1534 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1535}
1536
Andreas Gampebc4d2182016-02-22 10:03:12 -08001537void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1538 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1539 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1540 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1541 result->SetD(bit_cast<double>(long_input));
1542}
1543
Mathieu Chartiere401d142015-04-22 13:56:20 -07001544void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1545 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1546 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001547 result->SetI(args[0]);
1548}
1549
Mathieu Chartiere401d142015-04-22 13:56:20 -07001550void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1551 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1552 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001553 result->SetI(args[0]);
1554}
1555
Mathieu Chartiere401d142015-04-22 13:56:20 -07001556void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1557 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1558 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001559 result->SetL(receiver->Clone(self));
1560}
1561
Mathieu Chartiere401d142015-04-22 13:56:20 -07001562void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1563 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1564 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001565 receiver->NotifyAll(self);
1566}
1567
Mathieu Chartiere401d142015-04-22 13:56:20 -07001568void UnstartedRuntime::UnstartedJNIStringCompareTo(
1569 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1570 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001571 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1572 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001573 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001574 }
1575 result->SetI(receiver->AsString()->CompareTo(rhs));
1576}
1577
Mathieu Chartiere401d142015-04-22 13:56:20 -07001578void UnstartedRuntime::UnstartedJNIStringIntern(
1579 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1580 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001581 result->SetL(receiver->AsString()->Intern());
1582}
1583
Mathieu Chartiere401d142015-04-22 13:56:20 -07001584void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1585 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1586 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001587 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1588}
1589
Mathieu Chartiere401d142015-04-22 13:56:20 -07001590void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1591 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1592 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001593 StackHandleScope<2> hs(self);
1594 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1595 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1596 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1597}
1598
Mathieu Chartiere401d142015-04-22 13:56:20 -07001599void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1600 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1601 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001602 int32_t length = static_cast<int32_t>(args[1]);
1603 if (length < 0) {
1604 ThrowNegativeArraySizeException(length);
1605 return;
1606 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001607 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001608 Runtime* runtime = Runtime::Current();
1609 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001610 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001611 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001612 CHECK(self->IsExceptionPending());
1613 return;
1614 }
1615 DCHECK(array_class->IsObjectArrayClass());
1616 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1617 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1618 result->SetL(new_array);
1619}
1620
Mathieu Chartiere401d142015-04-22 13:56:20 -07001621void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1622 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1623 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001624 ScopedObjectAccessUnchecked soa(self);
1625 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001626 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001627 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001628 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001629 }
1630}
1631
Mathieu Chartiere401d142015-04-22 13:56:20 -07001632void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1633 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1634 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001635 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1636 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1637}
1638
Mathieu Chartiere401d142015-04-22 13:56:20 -07001639void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1640 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1641 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001642 result->SetZ(JNI_TRUE);
1643}
1644
Mathieu Chartiere401d142015-04-22 13:56:20 -07001645void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1646 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1647 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001648 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1649 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1650 jint expectedValue = args[3];
1651 jint newValue = args[4];
1652 bool success;
1653 if (Runtime::Current()->IsActiveTransaction()) {
1654 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1655 expectedValue, newValue);
1656 } else {
1657 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1658 expectedValue, newValue);
1659 }
1660 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1661}
1662
Narayan Kamath34a316f2016-03-30 13:11:18 +01001663void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1664 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1665 uint32_t* args, JValue* result) {
1666 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1667 if (obj == nullptr) {
1668 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1669 return;
1670 }
1671
1672 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1673 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1674}
1675
Mathieu Chartiere401d142015-04-22 13:56:20 -07001676void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1677 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1678 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001679 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1680 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1681 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1682 if (Runtime::Current()->IsActiveTransaction()) {
1683 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1684 } else {
1685 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1686 }
1687}
1688
Andreas Gampe799681b2015-05-15 19:24:12 -07001689void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001690 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1691 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001692 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1693 Primitive::Type primitive_type = component->GetPrimitiveType();
1694 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1695}
1696
Andreas Gampe799681b2015-05-15 19:24:12 -07001697void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001698 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1699 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001700 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1701 Primitive::Type primitive_type = component->GetPrimitiveType();
1702 result->SetI(Primitive::ComponentSize(primitive_type));
1703}
1704
Andreas Gampedd9d0552015-03-09 12:57:41 -07001705typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001706 size_t arg_size);
1707
Mathieu Chartiere401d142015-04-22 13:56:20 -07001708typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001709 uint32_t* args, JValue* result);
1710
1711static bool tables_initialized_ = false;
1712static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1713static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1714
Andreas Gampe799681b2015-05-15 19:24:12 -07001715void UnstartedRuntime::InitializeInvokeHandlers() {
1716#define UNSTARTED_DIRECT(ShortName, Sig) \
1717 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1718#include "unstarted_runtime_list.h"
1719 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1720#undef UNSTARTED_RUNTIME_DIRECT_LIST
1721#undef UNSTARTED_RUNTIME_JNI_LIST
1722#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001723}
1724
Andreas Gampe799681b2015-05-15 19:24:12 -07001725void UnstartedRuntime::InitializeJNIHandlers() {
1726#define UNSTARTED_JNI(ShortName, Sig) \
1727 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1728#include "unstarted_runtime_list.h"
1729 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1730#undef UNSTARTED_RUNTIME_DIRECT_LIST
1731#undef UNSTARTED_RUNTIME_JNI_LIST
1732#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001733}
1734
Andreas Gampe799681b2015-05-15 19:24:12 -07001735void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001736 CHECK(!tables_initialized_);
1737
Andreas Gampe799681b2015-05-15 19:24:12 -07001738 InitializeInvokeHandlers();
1739 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001740
1741 tables_initialized_ = true;
1742}
1743
Andreas Gampe799681b2015-05-15 19:24:12 -07001744void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1745 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001746 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1747 // problems in core libraries.
1748 CHECK(tables_initialized_);
1749
David Sehr709b0702016-10-13 09:12:37 -07001750 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001751 const auto& iter = invoke_handlers_.find(name);
1752 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001753 // Clear out the result in case it's not zeroed out.
1754 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001755
1756 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1757 self->PushShadowFrame(shadow_frame);
1758
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001759 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001760
1761 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001762 } else {
1763 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001764 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001765 }
1766}
1767
1768// 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 -07001769void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001770 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001771 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001772 const auto& iter = jni_handlers_.find(name);
1773 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001774 // Clear out the result in case it's not zeroed out.
1775 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001776 (*iter->second)(self, method, receiver, args, result);
1777 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001778 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1779 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001780 } else {
David Sehr709b0702016-10-13 09:12:37 -07001781 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001782 "non-transactional runtime";
1783 }
1784}
1785
1786} // namespace interpreter
1787} // namespace art