blob: 371e2f1e65523a421dfe5f1e93ff26909e584f15 [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 Gampeae78c262017-02-01 20:40:44 -0800404void UnstartedRuntime::UnstartedClassGetDeclaringClass(
405 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
406 StackHandleScope<1> hs(self);
407 Handle<mirror::Class> klass(hs.NewHandle(
408 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
409 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
410 result->SetL(nullptr);
411 return;
412 }
413 // Return null for anonymous classes.
414 JValue is_anon_result;
415 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
416 if (is_anon_result.GetZ() != 0) {
417 result->SetL(nullptr);
418 return;
419 }
420 result->SetL(annotations::GetDeclaringClass(klass));
421}
422
Andreas Gampe633750c2016-02-19 10:49:50 -0800423void UnstartedRuntime::UnstartedClassGetEnclosingClass(
424 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
425 StackHandleScope<1> hs(self);
426 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
427 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
428 result->SetL(nullptr);
429 }
David Sehr9323e6e2016-09-13 08:58:35 -0700430 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800431}
432
Andreas Gampe715fdc22016-04-18 17:07:30 -0700433void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
434 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
435 StackHandleScope<1> hs(self);
436 Handle<mirror::Class> klass(hs.NewHandle(
437 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
438 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
439 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
440}
441
Andreas Gampeae78c262017-02-01 20:40:44 -0800442void UnstartedRuntime::UnstartedClassIsAnonymousClass(
443 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
444 StackHandleScope<1> hs(self);
445 Handle<mirror::Class> klass(hs.NewHandle(
446 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
447 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
448 result->SetZ(false);
449 return;
450 }
451 mirror::String* class_name = nullptr;
452 if (!annotations::GetInnerClass(klass, &class_name)) {
453 result->SetZ(false);
454 return;
455 }
456 result->SetZ(class_name == nullptr);
457}
458
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700459static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
460 const char* entry_name,
461 size_t* size,
462 std::string* error_msg) {
463 CHECK(size != nullptr);
464
465 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
466 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800467 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700468 }
469 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
470 if (zip_entry == nullptr) {
471 return nullptr;
472 }
473 std::unique_ptr<MemMap> tmp_map(
474 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
475 if (tmp_map == nullptr) {
476 return nullptr;
477 }
478
479 // OK, from here everything seems fine.
480 *size = zip_entry->GetUncompressedLength();
481 return tmp_map;
482}
483
484static void GetResourceAsStream(Thread* self,
485 ShadowFrame* shadow_frame,
486 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700487 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700488 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
489 if (resource_obj == nullptr) {
490 AbortTransactionOrFail(self, "null name for getResourceAsStream");
491 return;
492 }
493 CHECK(resource_obj->IsString());
494 mirror::String* resource_name = resource_obj->AsString();
495
496 std::string resource_name_str = resource_name->ToModifiedUtf8();
497 if (resource_name_str.empty() || resource_name_str == "/") {
498 AbortTransactionOrFail(self,
499 "Unsupported name %s for getResourceAsStream",
500 resource_name_str.c_str());
501 return;
502 }
503 const char* resource_cstr = resource_name_str.c_str();
504 if (resource_cstr[0] == '/') {
505 resource_cstr++;
506 }
507
508 Runtime* runtime = Runtime::Current();
509
510 std::vector<std::string> split;
511 Split(runtime->GetBootClassPathString(), ':', &split);
512 if (split.empty()) {
513 AbortTransactionOrFail(self,
514 "Boot classpath not set or split error:: %s",
515 runtime->GetBootClassPathString().c_str());
516 return;
517 }
518
519 std::unique_ptr<MemMap> mem_map;
520 size_t map_size;
521 std::string last_error_msg; // Only store the last message (we could concatenate).
522
523 for (const std::string& jar_file : split) {
524 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
525 if (mem_map != nullptr) {
526 break;
527 }
528 }
529
530 if (mem_map == nullptr) {
531 // Didn't find it. There's a good chance this will be the same at runtime, but still
532 // conservatively abort the transaction here.
533 AbortTransactionOrFail(self,
534 "Could not find resource %s. Last error was %s.",
535 resource_name_str.c_str(),
536 last_error_msg.c_str());
537 return;
538 }
539
540 StackHandleScope<3> hs(self);
541
542 // Create byte array for content.
543 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
544 if (h_array.Get() == nullptr) {
545 AbortTransactionOrFail(self, "Could not find/create byte array class");
546 return;
547 }
548 // Copy in content.
549 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
550 // Be proactive releasing memory.
551 mem_map.release();
552
553 // Create a ByteArrayInputStream.
554 Handle<mirror::Class> h_class(hs.NewHandle(
555 runtime->GetClassLinker()->FindClass(self,
556 "Ljava/io/ByteArrayInputStream;",
557 ScopedNullHandle<mirror::ClassLoader>())));
558 if (h_class.Get() == nullptr) {
559 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
560 return;
561 }
562 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
563 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
564 return;
565 }
566
567 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
568 if (h_obj.Get() == nullptr) {
569 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
570 return;
571 }
572
573 auto* cl = Runtime::Current()->GetClassLinker();
574 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
575 "<init>", "([B)V", cl->GetImagePointerSize());
576 if (constructor == nullptr) {
577 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
578 return;
579 }
580
581 uint32_t args[1];
582 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
583 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
584
585 if (self->IsExceptionPending()) {
586 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
587 return;
588 }
589
590 result->SetL(h_obj.Get());
591}
592
593void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
594 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
595 {
596 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
597 CHECK(this_obj != nullptr);
598 CHECK(this_obj->IsClassLoader());
599
600 StackHandleScope<1> hs(self);
601 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
602
603 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
604 this_classloader_class.Get()) {
605 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700606 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700607 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700608 return;
609 }
610 }
611
612 GetResourceAsStream(self, shadow_frame, result, arg_offset);
613}
614
Andreas Gampe799681b2015-05-15 19:24:12 -0700615void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700616 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700617 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
618 mirror::ClassLoader* class_loader =
619 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
620 StackHandleScope<2> hs(self);
621 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
622 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
623 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
624 "VMClassLoader.findLoadedClass", false, false);
625 // This might have an error pending. But semantics are to just return null.
626 if (self->IsExceptionPending()) {
627 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700628 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700629 if (type != "java.lang.InternalError") {
630 self->ClearException();
631 }
632 }
633}
634
Mathieu Chartiere401d142015-04-22 13:56:20 -0700635void UnstartedRuntime::UnstartedVoidLookupType(
636 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
637 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700638 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
639}
640
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700641// Arraycopy emulation.
642// Note: we can't use any fast copy functions, as they are not available under transaction.
643
644template <typename T>
645static void PrimitiveArrayCopy(Thread* self,
646 mirror::Array* src_array, int32_t src_pos,
647 mirror::Array* dst_array, int32_t dst_pos,
648 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700649 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700650 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700651 AbortTransactionOrFail(self,
652 "Types mismatched in arraycopy: %s vs %s.",
653 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700654 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700655 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700656 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700657 return;
658 }
659 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
660 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
661 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
662 if (copy_forward) {
663 for (int32_t i = 0; i < length; ++i) {
664 dst->Set(dst_pos + i, src->Get(src_pos + i));
665 }
666 } else {
667 for (int32_t i = 1; i <= length; ++i) {
668 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
669 }
670 }
671}
672
Andreas Gampe799681b2015-05-15 19:24:12 -0700673void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700674 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700675 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700676 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
677 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700678 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700679
Andreas Gampe85a098a2016-03-31 13:30:53 -0700680 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
681 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
682 // Null checking. For simplicity, abort transaction.
683 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700684 AbortTransactionOrFail(self, "src is null in arraycopy.");
685 return;
686 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700687 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700688 AbortTransactionOrFail(self, "dst is null in arraycopy.");
689 return;
690 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700691 // Test for arrayness. Throw ArrayStoreException.
692 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
693 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
694 return;
695 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700696
Andreas Gampe85a098a2016-03-31 13:30:53 -0700697 mirror::Array* src_array = src_obj->AsArray();
698 mirror::Array* dst_array = dst_obj->AsArray();
699
700 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700701 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
702 UNLIKELY(src_pos > src_array->GetLength() - length) ||
703 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700704 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700705 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
706 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
707 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700708 return;
709 }
710
711 // Type checking.
712 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
713 GetComponentType();
714
715 if (!src_type->IsPrimitive()) {
716 // Check that the second type is not primitive.
717 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
718 GetComponentType();
719 if (trg_type->IsPrimitiveInt()) {
720 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700721 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700722 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700723 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700724 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700725 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700726 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700727
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700728 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
729 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
730 if (src == dst) {
731 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700732 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700733 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
734 if (copy_forward) {
735 for (int32_t i = 0; i < length; ++i) {
736 dst->Set(dst_pos + i, src->Get(src_pos + i));
737 }
738 } else {
739 for (int32_t i = 1; i <= length; ++i) {
740 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
741 }
742 }
743 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700744 // We're being lazy here. Optimally this could be a memcpy (if component types are
745 // assignable), but the ObjectArray implementation doesn't support transactions. The
746 // checking version, however, does.
747 if (Runtime::Current()->IsActiveTransaction()) {
748 dst->AssignableCheckingMemcpy<true>(
749 dst_pos, src, src_pos, length, true /* throw_exception */);
750 } else {
751 dst->AssignableCheckingMemcpy<false>(
752 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700753 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700754 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700755 } else if (src_type->IsPrimitiveByte()) {
756 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700757 } else if (src_type->IsPrimitiveChar()) {
758 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
759 } else if (src_type->IsPrimitiveInt()) {
760 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700761 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700762 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700763 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700764 }
765}
766
Andreas Gampe5c9af612016-04-05 14:16:10 -0700767void UnstartedRuntime::UnstartedSystemArraycopyByte(
768 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
769 // Just forward.
770 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
771}
772
Andreas Gampe799681b2015-05-15 19:24:12 -0700773void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700774 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700775 // Just forward.
776 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
777}
778
779void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700780 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700781 // Just forward.
782 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
783}
784
Narayan Kamath34a316f2016-03-30 13:11:18 +0100785void UnstartedRuntime::UnstartedSystemGetSecurityManager(
786 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
787 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
788 result->SetL(nullptr);
789}
790
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700791static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
792
793static void GetSystemProperty(Thread* self,
794 ShadowFrame* shadow_frame,
795 JValue* result,
796 size_t arg_offset,
797 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700798 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700799 StackHandleScope<4> hs(self);
800 Handle<mirror::String> h_key(
801 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
802 if (h_key.Get() == nullptr) {
803 AbortTransactionOrFail(self, "getProperty key was null");
804 return;
805 }
806
807 // This is overall inefficient, but reflecting the values here is not great, either. So
808 // for simplicity, and with the assumption that the number of getProperty calls is not
809 // too great, just iterate each time.
810
811 // Get the storage class.
812 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
813 Handle<mirror::Class> h_props_class(hs.NewHandle(
814 class_linker->FindClass(self,
815 "Ljava/lang/AndroidHardcodedSystemProperties;",
816 ScopedNullHandle<mirror::ClassLoader>())));
817 if (h_props_class.Get() == nullptr) {
818 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
819 return;
820 }
821 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
822 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
823 return;
824 }
825
826 // Get the storage array.
827 ArtField* static_properties =
828 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
829 "[[Ljava/lang/String;");
830 if (static_properties == nullptr) {
831 AbortTransactionOrFail(self,
832 "Could not find %s field",
833 kAndroidHardcodedSystemPropertiesFieldName);
834 return;
835 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700836 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
837 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
838 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700839 if (h_2string_array.Get() == nullptr) {
840 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
841 return;
842 }
843
844 // Iterate over it.
845 const int32_t prop_count = h_2string_array->GetLength();
846 // Use the third handle as mutable.
847 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
848 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
849 for (int32_t i = 0; i < prop_count; ++i) {
850 h_string_array.Assign(h_2string_array->Get(i));
851 if (h_string_array.Get() == nullptr ||
852 h_string_array->GetLength() != 2 ||
853 h_string_array->Get(0) == nullptr) {
854 AbortTransactionOrFail(self,
855 "Unexpected content of %s",
856 kAndroidHardcodedSystemPropertiesFieldName);
857 return;
858 }
859 if (h_key->Equals(h_string_array->Get(0))) {
860 // Found a value.
861 if (h_string_array->Get(1) == nullptr && is_default_version) {
862 // Null is being delegated to the default map, and then resolved to the given default value.
863 // As there's no default map, return the given value.
864 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
865 } else {
866 result->SetL(h_string_array->Get(1));
867 }
868 return;
869 }
870 }
871
872 // Key is not supported.
873 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
874}
875
876void UnstartedRuntime::UnstartedSystemGetProperty(
877 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
878 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
879}
880
881void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
882 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
883 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
884}
885
Andreas Gampe799681b2015-05-15 19:24:12 -0700886void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700887 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -0700888 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700889 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100890 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
891 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700892 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100893 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700894 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100895 } else if (caller ==
896 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
897 // Allocate new object.
898 StackHandleScope<2> hs(self);
899 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
900 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
901 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
902 h_real_to_string_class->AllocObject(self)));
903 if (h_real_to_string_obj.Get() != nullptr) {
904 auto* cl = Runtime::Current()->GetClassLinker();
905 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
906 "<init>", "()V", cl->GetImagePointerSize());
907 if (init_method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700908 h_real_to_string_class->DumpClass(LOG_STREAM(FATAL), mirror::Class::kDumpClassFullDetail);
Narayan Kamatha1e93122016-03-30 15:41:54 +0100909 } else {
910 JValue invoke_result;
911 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
912 nullptr);
913 if (!self->IsExceptionPending()) {
914 result->SetL(h_real_to_string_obj.Get());
915 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700916 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700917 }
918 }
919 }
920
921 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700922 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700923 }
924}
925
Sergio Giro83261202016-04-11 20:49:20 +0100926void UnstartedRuntime::UnstartedMathCeil(
927 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700928 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100929}
930
931void UnstartedRuntime::UnstartedMathFloor(
932 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700933 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700934}
935
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700936void UnstartedRuntime::UnstartedMathSin(
937 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
938 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
939}
940
941void UnstartedRuntime::UnstartedMathCos(
942 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
943 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
944}
945
946void UnstartedRuntime::UnstartedMathPow(
947 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
948 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
949 shadow_frame->GetVRegDouble(arg_offset + 2)));
950}
951
Andreas Gampe799681b2015-05-15 19:24:12 -0700952void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700954 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
955 result->SetI(obj->IdentityHashCode());
956}
957
Andreas Gampe799681b2015-05-15 19:24:12 -0700958void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700959 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700960 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000961 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700962}
963
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700964static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700965 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700966 const DexFile* dex_file = dex_cache->GetDexFile();
967 if (dex_file == nullptr) {
968 return nullptr;
969 }
970
971 // Create the direct byte buffer.
972 JNIEnv* env = self->GetJniEnv();
973 DCHECK(env != nullptr);
974 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700975 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
976 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700977 DCHECK(self->IsExceptionPending());
978 return nullptr;
979 }
980
981 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700982 args[0].l = byte_buffer.get();
983
984 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
985 WellKnownClasses::com_android_dex_Dex,
986 WellKnownClasses::com_android_dex_Dex_create,
987 args));
988
989 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700990}
991
Andreas Gampe799681b2015-05-15 19:24:12 -0700992void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700993 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700994 // We will create the Dex object, but the image writer will release it before creating the
995 // art file.
996 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
997 bool have_dex = false;
998 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700999 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001000 if (dex != nullptr) {
1001 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001002 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001003 }
1004 }
1005 if (!have_dex) {
1006 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001007 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001008 }
1009}
1010
1011static void UnstartedMemoryPeek(
1012 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1013 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1014 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1015 // aborting the transaction.
1016
1017 switch (type) {
1018 case Primitive::kPrimByte: {
1019 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1020 return;
1021 }
1022
1023 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001024 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1025 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001026 return;
1027 }
1028
1029 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001030 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1031 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001032 return;
1033 }
1034
1035 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001036 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1037 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001038 return;
1039 }
1040
1041 case Primitive::kPrimBoolean:
1042 case Primitive::kPrimChar:
1043 case Primitive::kPrimFloat:
1044 case Primitive::kPrimDouble:
1045 case Primitive::kPrimVoid:
1046 case Primitive::kPrimNot:
1047 LOG(FATAL) << "Not in the Memory API: " << type;
1048 UNREACHABLE();
1049 }
1050 LOG(FATAL) << "Should not reach here";
1051 UNREACHABLE();
1052}
1053
Andreas Gampe799681b2015-05-15 19:24:12 -07001054void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001055 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001056 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1057}
1058
1059void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001060 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001061 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1062}
1063
1064void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001065 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001066 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1067}
1068
1069void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001070 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001071 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001072}
1073
1074static void UnstartedMemoryPeekArray(
1075 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001076 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001077 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1078 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1079 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001080 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001081 return;
1082 }
1083 mirror::Array* array = obj->AsArray();
1084
1085 int offset = shadow_frame->GetVReg(arg_offset + 3);
1086 int count = shadow_frame->GetVReg(arg_offset + 4);
1087 if (offset < 0 || offset + count > array->GetLength()) {
1088 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1089 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001090 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001091 return;
1092 }
1093
1094 switch (type) {
1095 case Primitive::kPrimByte: {
1096 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1097 mirror::ByteArray* byte_array = array->AsByteArray();
1098 for (int32_t i = 0; i < count; ++i, ++address) {
1099 byte_array->SetWithoutChecks<true>(i + offset, *address);
1100 }
1101 return;
1102 }
1103
1104 case Primitive::kPrimShort:
1105 case Primitive::kPrimInt:
1106 case Primitive::kPrimLong:
1107 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1108 UNREACHABLE();
1109
1110 case Primitive::kPrimBoolean:
1111 case Primitive::kPrimChar:
1112 case Primitive::kPrimFloat:
1113 case Primitive::kPrimDouble:
1114 case Primitive::kPrimVoid:
1115 case Primitive::kPrimNot:
1116 LOG(FATAL) << "Not in the Memory API: " << type;
1117 UNREACHABLE();
1118 }
1119 LOG(FATAL) << "Should not reach here";
1120 UNREACHABLE();
1121}
1122
Andreas Gampe799681b2015-05-15 19:24:12 -07001123void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001124 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001125 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001126}
1127
Kenny Root1c9e61c2015-05-14 15:58:17 -07001128// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001129void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001131 jint start = shadow_frame->GetVReg(arg_offset + 1);
1132 jint end = shadow_frame->GetVReg(arg_offset + 2);
1133 jint index = shadow_frame->GetVReg(arg_offset + 4);
1134 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1135 if (string == nullptr) {
1136 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1137 return;
1138 }
Kenny Root57f91e82015-05-14 15:58:17 -07001139 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001140 DCHECK_LE(start, end);
1141 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001142 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143 Handle<mirror::CharArray> h_char_array(
1144 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001145 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001146 DCHECK_LE(index, h_char_array->GetLength());
1147 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001148 string->GetChars(start, end, h_char_array, index);
1149}
1150
1151// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001152void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001153 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001154 jint index = shadow_frame->GetVReg(arg_offset + 1);
1155 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1156 if (string == nullptr) {
1157 AbortTransactionOrFail(self, "String.charAt with null object");
1158 return;
1159 }
1160 result->SetC(string->CharAt(index));
1161}
1162
Kenny Root57f91e82015-05-14 15:58:17 -07001163// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001164void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001166 jint index = shadow_frame->GetVReg(arg_offset + 1);
1167 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1168 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1169 if (string == nullptr) {
1170 AbortTransactionOrFail(self, "String.setCharAt with null object");
1171 return;
1172 }
1173 string->SetCharAt(index, c);
1174}
1175
Kenny Root1c9e61c2015-05-14 15:58:17 -07001176// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001177void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001178 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001179 jint offset = shadow_frame->GetVReg(arg_offset);
1180 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1181 DCHECK_GE(char_count, 0);
1182 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001183 Handle<mirror::CharArray> h_char_array(
1184 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001185 Runtime* runtime = Runtime::Current();
1186 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1187 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1188}
1189
1190// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001191void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001192 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001193 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1194 if (to_copy == nullptr) {
1195 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1196 return;
1197 }
1198 StackHandleScope<1> hs(self);
1199 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1200 Runtime* runtime = Runtime::Current();
1201 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1202 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1203 allocator));
1204}
1205
Andreas Gampe799681b2015-05-15 19:24:12 -07001206void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001207 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001208 jint start = shadow_frame->GetVReg(arg_offset + 1);
1209 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001210 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001211 DCHECK_GE(length, 0);
1212 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001213 Handle<mirror::String> h_string(
1214 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001215 DCHECK_LE(start, h_string->GetLength());
1216 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001217 Runtime* runtime = Runtime::Current();
1218 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1219 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1220}
1221
Kenny Root57f91e82015-05-14 15:58:17 -07001222// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001223void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001224 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001225 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001226 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1227 if (string == nullptr) {
1228 AbortTransactionOrFail(self, "String.charAt with null object");
1229 return;
1230 }
1231 result->SetL(string->ToCharArray(self));
1232}
1233
Andreas Gampebc4d2182016-02-22 10:03:12 -08001234// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1235void UnstartedRuntime::UnstartedReferenceGetReferent(
1236 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001237 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001238 shadow_frame->GetVRegReference(arg_offset));
1239 if (ref == nullptr) {
1240 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1241 return;
1242 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001243 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001244 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1245 result->SetL(referent);
1246}
1247
1248// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1249// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1250// where we can predict the behavior (somewhat).
1251// Note: this is required (instead of lazy initialization) as these classes are used in the static
1252// initialization of other classes, so will *use* the value.
1253void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1254 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -07001255 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampebc4d2182016-02-22 10:03:12 -08001256 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1257 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1258 // 8 as a conservative upper approximation.
1259 result->SetI(8);
1260 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1261 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1262 // a good upper bound.
1263 // TODO: Consider resetting in the zygote?
1264 result->SetI(8);
1265 } else {
1266 // Not supported.
1267 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1268 }
1269}
1270
1271// This allows accessing ConcurrentHashMap/SynchronousQueue.
1272
1273void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1274 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1275 // Argument 0 is the Unsafe instance, skip.
1276 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1277 if (obj == nullptr) {
1278 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1279 return;
1280 }
1281 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1282 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1283 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001284 bool success;
1285 // Check whether we're in a transaction, call accordingly.
1286 if (Runtime::Current()->IsActiveTransaction()) {
1287 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1288 expectedValue,
1289 newValue);
1290 } else {
1291 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1292 expectedValue,
1293 newValue);
1294 }
1295 result->SetZ(success ? 1 : 0);
1296}
1297
1298void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1299 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1300 // Argument 0 is the Unsafe instance, skip.
1301 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1302 if (obj == nullptr) {
1303 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1304 return;
1305 }
1306 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1307 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1308 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1309
1310 // Must use non transactional mode.
1311 if (kUseReadBarrier) {
1312 // Need to make sure the reference stored in the field is a to-space one before attempting the
1313 // CAS or the CAS could fail incorrectly.
1314 mirror::HeapReference<mirror::Object>* field_addr =
1315 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1316 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001317 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001318 obj,
1319 MemberOffset(offset),
1320 field_addr);
1321 }
1322 bool success;
1323 // Check whether we're in a transaction, call accordingly.
1324 if (Runtime::Current()->IsActiveTransaction()) {
1325 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1326 expected_value,
1327 newValue);
1328 } else {
1329 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1330 expected_value,
1331 newValue);
1332 }
1333 result->SetZ(success ? 1 : 0);
1334}
1335
1336void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1337 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001338 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001339 // Argument 0 is the Unsafe instance, skip.
1340 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1341 if (obj == nullptr) {
1342 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1343 return;
1344 }
1345 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1346 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1347 result->SetL(value);
1348}
1349
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001350void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1351 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001352 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001353 // Argument 0 is the Unsafe instance, skip.
1354 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1355 if (obj == nullptr) {
1356 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1357 return;
1358 }
1359 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1360 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1361 if (Runtime::Current()->IsActiveTransaction()) {
1362 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1363 } else {
1364 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1365 }
1366}
1367
Andreas Gampebc4d2182016-02-22 10:03:12 -08001368void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1369 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001370 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001371 // Argument 0 is the Unsafe instance, skip.
1372 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1373 if (obj == nullptr) {
1374 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1375 return;
1376 }
1377 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1378 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1379 QuasiAtomic::ThreadFenceRelease();
1380 if (Runtime::Current()->IsActiveTransaction()) {
1381 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1382 } else {
1383 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1384 }
1385}
1386
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001387// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1388// of correctly handling the corner cases.
1389void UnstartedRuntime::UnstartedIntegerParseInt(
1390 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001391 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001392 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1393 if (obj == nullptr) {
1394 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1395 return;
1396 }
1397
1398 std::string string_value = obj->AsString()->ToModifiedUtf8();
1399 if (string_value.empty()) {
1400 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1401 return;
1402 }
1403
1404 const char* c_str = string_value.c_str();
1405 char *end;
1406 // Can we set errno to 0? Is this always a variable, and not a macro?
1407 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1408 int64_t l = strtol(c_str, &end, 10);
1409
1410 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1411 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1412 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1413 return;
1414 }
1415 if (l == 0) {
1416 // Check whether the string wasn't exactly zero.
1417 if (string_value != "0") {
1418 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1419 return;
1420 }
1421 } else if (*end != '\0') {
1422 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1423 return;
1424 }
1425
1426 result->SetI(static_cast<int32_t>(l));
1427}
1428
1429// A cutout for Long.parseLong.
1430//
1431// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1432// well.
1433void UnstartedRuntime::UnstartedLongParseLong(
1434 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001435 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001436 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1437 if (obj == nullptr) {
1438 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1439 return;
1440 }
1441
1442 std::string string_value = obj->AsString()->ToModifiedUtf8();
1443 if (string_value.empty()) {
1444 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1445 return;
1446 }
1447
1448 const char* c_str = string_value.c_str();
1449 char *end;
1450 // Can we set errno to 0? Is this always a variable, and not a macro?
1451 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1452 int64_t l = strtol(c_str, &end, 10);
1453
1454 // Note: comparing against int32_t min/max is intentional here.
1455 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1456 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1457 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1458 return;
1459 }
1460 if (l == 0) {
1461 // Check whether the string wasn't exactly zero.
1462 if (string_value != "0") {
1463 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1464 return;
1465 }
1466 } else if (*end != '\0') {
1467 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1468 return;
1469 }
1470
1471 result->SetJ(l);
1472}
1473
Andreas Gampe715fdc22016-04-18 17:07:30 -07001474void UnstartedRuntime::UnstartedMethodInvoke(
1475 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001476 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001477 JNIEnvExt* env = self->GetJniEnv();
1478 ScopedObjectAccessUnchecked soa(self);
1479
Mathieu Chartier8778c522016-10-04 19:06:30 -07001480 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001481 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001482 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001483
Mathieu Chartier8778c522016-10-04 19:06:30 -07001484 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001485 ScopedLocalRef<jobject> java_receiver(env,
1486 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1487
Mathieu Chartier8778c522016-10-04 19:06:30 -07001488 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001489 ScopedLocalRef<jobject> java_args(env,
1490 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1491
1492 ScopedLocalRef<jobject> result_jobj(env,
1493 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1494
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001495 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001496
1497 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1498 // InvocationTargetExceptions.
1499 if (self->IsExceptionPending()) {
1500 AbortTransactionOrFail(self, "Failed Method.invoke");
1501 }
1502}
1503
Andreas Gampebc4d2182016-02-22 10:03:12 -08001504
Mathieu Chartiere401d142015-04-22 13:56:20 -07001505void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1506 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1507 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001508 int32_t length = args[1];
1509 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001510 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001511 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001512 ObjPtr<mirror::Class> array_class =
1513 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001514 DCHECK(array_class != nullptr);
1515 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001516 result->SetL(mirror::Array::Alloc<true, true>(self,
1517 array_class,
1518 length,
1519 array_class->GetComponentSizeShift(),
1520 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001521}
1522
Mathieu Chartiere401d142015-04-22 13:56:20 -07001523void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1524 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1525 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001526 result->SetL(nullptr);
1527}
1528
Mathieu Chartiere401d142015-04-22 13:56:20 -07001529void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1530 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1531 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001532 NthCallerVisitor visitor(self, 3);
1533 visitor.WalkStack();
1534 if (visitor.caller != nullptr) {
1535 result->SetL(visitor.caller->GetDeclaringClass());
1536 }
1537}
1538
Mathieu Chartiere401d142015-04-22 13:56:20 -07001539void UnstartedRuntime::UnstartedJNIMathLog(
1540 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1541 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001542 JValue value;
1543 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1544 result->SetD(log(value.GetD()));
1545}
1546
Mathieu Chartiere401d142015-04-22 13:56:20 -07001547void UnstartedRuntime::UnstartedJNIMathExp(
1548 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1549 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001550 JValue value;
1551 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1552 result->SetD(exp(value.GetD()));
1553}
1554
Andreas Gampebc4d2182016-02-22 10:03:12 -08001555void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1556 Thread* self ATTRIBUTE_UNUSED,
1557 ArtMethod* method ATTRIBUTE_UNUSED,
1558 mirror::Object* receiver ATTRIBUTE_UNUSED,
1559 uint32_t* args ATTRIBUTE_UNUSED,
1560 JValue* result) {
1561 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1562 ? 0
1563 : 1);
1564}
1565
Mathieu Chartiere401d142015-04-22 13:56:20 -07001566void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1567 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1568 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001569 StackHandleScope<1> hs(self);
1570 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1571}
1572
Andreas Gampebc4d2182016-02-22 10:03:12 -08001573void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1574 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1575 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1576 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1577 result->SetD(bit_cast<double>(long_input));
1578}
1579
Mathieu Chartiere401d142015-04-22 13:56:20 -07001580void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1581 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1582 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001583 result->SetI(args[0]);
1584}
1585
Mathieu Chartiere401d142015-04-22 13:56:20 -07001586void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1587 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1588 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001589 result->SetI(args[0]);
1590}
1591
Mathieu Chartiere401d142015-04-22 13:56:20 -07001592void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1593 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1594 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001595 result->SetL(receiver->Clone(self));
1596}
1597
Mathieu Chartiere401d142015-04-22 13:56:20 -07001598void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1599 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1600 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001601 receiver->NotifyAll(self);
1602}
1603
Mathieu Chartiere401d142015-04-22 13:56:20 -07001604void UnstartedRuntime::UnstartedJNIStringCompareTo(
1605 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1606 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001607 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1608 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001609 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001610 }
1611 result->SetI(receiver->AsString()->CompareTo(rhs));
1612}
1613
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614void UnstartedRuntime::UnstartedJNIStringIntern(
1615 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1616 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001617 result->SetL(receiver->AsString()->Intern());
1618}
1619
Mathieu Chartiere401d142015-04-22 13:56:20 -07001620void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1621 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1622 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001623 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1624}
1625
Mathieu Chartiere401d142015-04-22 13:56:20 -07001626void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1627 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1628 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001629 StackHandleScope<2> hs(self);
1630 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1631 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1632 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1633}
1634
Mathieu Chartiere401d142015-04-22 13:56:20 -07001635void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1636 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1637 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001638 int32_t length = static_cast<int32_t>(args[1]);
1639 if (length < 0) {
1640 ThrowNegativeArraySizeException(length);
1641 return;
1642 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001643 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001644 Runtime* runtime = Runtime::Current();
1645 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001646 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001647 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001648 CHECK(self->IsExceptionPending());
1649 return;
1650 }
1651 DCHECK(array_class->IsObjectArrayClass());
1652 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1653 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1654 result->SetL(new_array);
1655}
1656
Mathieu Chartiere401d142015-04-22 13:56:20 -07001657void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1658 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1659 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001660 ScopedObjectAccessUnchecked soa(self);
1661 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001662 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001663 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001664 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001665 }
1666}
1667
Mathieu Chartiere401d142015-04-22 13:56:20 -07001668void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1669 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1670 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001671 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1672 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1673}
1674
Mathieu Chartiere401d142015-04-22 13:56:20 -07001675void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1676 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1677 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001678 result->SetZ(JNI_TRUE);
1679}
1680
Mathieu Chartiere401d142015-04-22 13:56:20 -07001681void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1682 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1683 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001684 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1685 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1686 jint expectedValue = args[3];
1687 jint newValue = args[4];
1688 bool success;
1689 if (Runtime::Current()->IsActiveTransaction()) {
1690 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1691 expectedValue, newValue);
1692 } else {
1693 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1694 expectedValue, newValue);
1695 }
1696 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1697}
1698
Narayan Kamath34a316f2016-03-30 13:11:18 +01001699void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1700 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1701 uint32_t* args, JValue* result) {
1702 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1703 if (obj == nullptr) {
1704 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1705 return;
1706 }
1707
1708 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1709 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1710}
1711
Mathieu Chartiere401d142015-04-22 13:56:20 -07001712void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1713 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1714 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001715 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1716 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1717 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1718 if (Runtime::Current()->IsActiveTransaction()) {
1719 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1720 } else {
1721 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1722 }
1723}
1724
Andreas Gampe799681b2015-05-15 19:24:12 -07001725void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001726 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1727 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001728 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1729 Primitive::Type primitive_type = component->GetPrimitiveType();
1730 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1731}
1732
Andreas Gampe799681b2015-05-15 19:24:12 -07001733void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001734 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1735 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001736 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1737 Primitive::Type primitive_type = component->GetPrimitiveType();
1738 result->SetI(Primitive::ComponentSize(primitive_type));
1739}
1740
Andreas Gampedd9d0552015-03-09 12:57:41 -07001741typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001742 size_t arg_size);
1743
Mathieu Chartiere401d142015-04-22 13:56:20 -07001744typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001745 uint32_t* args, JValue* result);
1746
1747static bool tables_initialized_ = false;
1748static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1749static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1750
Andreas Gampe799681b2015-05-15 19:24:12 -07001751void UnstartedRuntime::InitializeInvokeHandlers() {
1752#define UNSTARTED_DIRECT(ShortName, Sig) \
1753 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1754#include "unstarted_runtime_list.h"
1755 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1756#undef UNSTARTED_RUNTIME_DIRECT_LIST
1757#undef UNSTARTED_RUNTIME_JNI_LIST
1758#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001759}
1760
Andreas Gampe799681b2015-05-15 19:24:12 -07001761void UnstartedRuntime::InitializeJNIHandlers() {
1762#define UNSTARTED_JNI(ShortName, Sig) \
1763 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1764#include "unstarted_runtime_list.h"
1765 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1766#undef UNSTARTED_RUNTIME_DIRECT_LIST
1767#undef UNSTARTED_RUNTIME_JNI_LIST
1768#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001769}
1770
Andreas Gampe799681b2015-05-15 19:24:12 -07001771void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001772 CHECK(!tables_initialized_);
1773
Andreas Gampe799681b2015-05-15 19:24:12 -07001774 InitializeInvokeHandlers();
1775 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001776
1777 tables_initialized_ = true;
1778}
1779
Andreas Gampe799681b2015-05-15 19:24:12 -07001780void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1781 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001782 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1783 // problems in core libraries.
1784 CHECK(tables_initialized_);
1785
David Sehr709b0702016-10-13 09:12:37 -07001786 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001787 const auto& iter = invoke_handlers_.find(name);
1788 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001789 // Clear out the result in case it's not zeroed out.
1790 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001791
1792 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1793 self->PushShadowFrame(shadow_frame);
1794
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001795 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001796
1797 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001798 } else {
1799 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001800 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001801 }
1802}
1803
1804// 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 -07001805void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001806 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001807 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001808 const auto& iter = jni_handlers_.find(name);
1809 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001810 // Clear out the result in case it's not zeroed out.
1811 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001812 (*iter->second)(self, method, receiver, args, result);
1813 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001814 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1815 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001816 } else {
David Sehr709b0702016-10-13 09:12:37 -07001817 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001818 "non-transactional runtime";
1819 }
1820}
1821
1822} // namespace interpreter
1823} // namespace art