blob: cc88d7edde26c550ee5af30abf6ff98bfb1fdf2c [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 Gampeaacc25d2015-04-01 14:49:06 -070028#include "ScopedLocalRef.h"
29
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080031#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070032#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070033#include "base/logging.h"
34#include "base/macros.h"
35#include "class_linker.h"
36#include "common_throws.h"
37#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080038#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070039#include "handle_scope-inl.h"
40#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070041#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070042#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070044#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010045#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
48#include "mirror/string-inl.h"
49#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070050#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070051#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020052#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070053#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070054#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070055
56namespace art {
57namespace interpreter {
58
Andreas Gampe068b0c02015-03-11 12:44:47 -070059static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020060 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020062
63static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070064 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020066 va_start(args, fmt);
67 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070068 va_end(args);
69 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020070 va_start(args, fmt);
71 std::string msg;
72 StringAppendV(&msg, fmt, args);
73 va_end(args);
74 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070075 UNREACHABLE();
76 }
77}
78
Andreas Gampe8ce9c302016-04-15 21:24:28 -070079// Restricted support for character upper case / lower case. Only support ASCII, where
80// it's easy. Abort the transaction otherwise.
81static void CharacterLowerUpper(Thread* self,
82 ShadowFrame* shadow_frame,
83 JValue* result,
84 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070086 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
87
88 // Only ASCII (7-bit).
89 if (!isascii(int_value)) {
90 AbortTransactionOrFail(self,
91 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
92 int_value);
93 return;
94 }
95
96 std::locale c_locale("C");
97 char char_value = static_cast<char>(int_value);
98
99 if (to_lower_case) {
100 result->SetI(std::tolower(char_value, c_locale));
101 } else {
102 result->SetI(std::toupper(char_value, c_locale));
103 }
104}
105
106void UnstartedRuntime::UnstartedCharacterToLowerCase(
107 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
108 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
109}
110
111void UnstartedRuntime::UnstartedCharacterToUpperCase(
112 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
113 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
114}
115
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700116// Helper function to deal with class loading in an unstarted runtime.
117static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
118 Handle<mirror::ClassLoader> class_loader, JValue* result,
119 const std::string& method_name, bool initialize_class,
120 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700122 CHECK(className.Get() != nullptr);
123 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
125
126 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
127 if (found == nullptr && abort_if_not_found) {
128 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700129 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700130 method_name.c_str(),
131 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700132 }
133 return;
134 }
135 if (found != nullptr && initialize_class) {
136 StackHandleScope<1> hs(self);
137 Handle<mirror::Class> h_class(hs.NewHandle(found));
138 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
139 CHECK(self->IsExceptionPending());
140 return;
141 }
142 }
143 result->SetL(found);
144}
145
146// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
147// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
148// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200149// actually the transaction abort exception. This must not be wrapped, as it signals an
150// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700151static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700152 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700153 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200154 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700155 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200156 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700157 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
158 "ClassNotFoundException");
159 }
160 }
161}
162
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700163static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700164 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700165 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
166 if (param == nullptr) {
167 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
168 return nullptr;
169 }
170 return param->AsString();
171}
172
Andreas Gampe799681b2015-05-15 19:24:12 -0700173void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700174 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700175 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
176 if (class_name == nullptr) {
177 return;
178 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700179 StackHandleScope<1> hs(self);
180 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800181 UnstartedRuntimeFindClass(self,
182 h_class_name,
183 ScopedNullHandle<mirror::ClassLoader>(),
184 result,
185 "Class.forName",
186 true,
187 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700188 CheckExceptionGenerateClassNotFound(self);
189}
190
Andreas Gampe799681b2015-05-15 19:24:12 -0700191void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700192 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700193 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
194 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700195 return;
196 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700197 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
198 mirror::ClassLoader* class_loader =
199 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
200 StackHandleScope<2> hs(self);
201 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
202 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
203 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
204 initialize_class, false);
205 CheckExceptionGenerateClassNotFound(self);
206}
207
Andreas Gampe799681b2015-05-15 19:24:12 -0700208void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700209 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700210 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
211 if (class_name == nullptr) {
212 return;
213 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700214 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
215 mirror::ClassLoader* class_loader =
216 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
217 StackHandleScope<2> hs(self);
218 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
219 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
220 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
221 initialize_class, false);
222 CheckExceptionGenerateClassNotFound(self);
223}
224
Andreas Gampe799681b2015-05-15 19:24:12 -0700225void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700226 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
227 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700228 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
229 if (param == nullptr) {
230 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
231 return;
232 }
233 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700234 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700235
236 // Check that it's not null.
237 if (h_klass.Get() == nullptr) {
238 AbortTransactionOrFail(self, "Class reference is null for newInstance");
239 return;
240 }
241
242 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
243 if (Runtime::Current()->IsActiveTransaction()) {
244 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200245 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700246 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700247 return;
248 }
249 }
250
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700251 // There are two situations in which we'll abort this run.
252 // 1) If the class isn't yet initialized and initialization fails.
253 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
254 // Note that 2) could likely be handled here, but for safety abort the transaction.
255 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700256 auto* cl = Runtime::Current()->GetClassLinker();
257 if (cl->EnsureInitialized(self, h_klass, true, true)) {
258 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
259 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700260 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
261 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700262 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700263 if (!self->IsExceptionPending()) {
264 result->SetL(h_obj.Get());
265 ok = true;
266 }
267 } else {
268 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
269 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700270 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700271 }
272 }
273 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700274 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700275 h_klass->PrettyClass().c_str(),
276 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700277 }
278}
279
Andreas Gampe799681b2015-05-15 19:24:12 -0700280void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700281 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700282 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
283 // going the reflective Dex way.
284 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
285 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700286 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700287 for (ArtField& field : klass->GetIFields()) {
288 if (name2->Equals(field.GetName())) {
289 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700290 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700291 }
292 }
293 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700294 for (ArtField& field : klass->GetSFields()) {
295 if (name2->Equals(field.GetName())) {
296 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700297 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700298 }
299 }
300 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700301 if (found == nullptr) {
302 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
303 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700304 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700305 return;
306 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700307 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700308 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700309 mirror::Field* field;
310 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700311 if (pointer_size == PointerSize::k64) {
312 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
313 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700314 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700315 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
316 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700317 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700318 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700319 if (pointer_size == PointerSize::k64) {
320 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
321 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700322 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700323 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
324 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700325 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700326 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700327 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700328}
329
Andreas Gampebc4d2182016-02-22 10:03:12 -0800330// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
331void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
332 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
333 // Special managed code cut-out to allow method lookup in a un-started runtime.
334 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
335 if (klass == nullptr) {
336 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
337 return;
338 }
339 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
340 mirror::ObjectArray<mirror::Class>* args =
341 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700342 Runtime* runtime = Runtime::Current();
343 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700344 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700345 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700346 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700347 if (pointer_size == PointerSize::k64) {
348 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
349 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700350 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700351 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
352 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700353 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800354 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700355 if (pointer_size == PointerSize::k64) {
356 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
357 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700358 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700359 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
360 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700361 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800362 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700363 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800364}
365
Andreas Gampe6039e562016-04-05 18:18:43 -0700366// Special managed code cut-out to allow constructor lookup in a un-started runtime.
367void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
368 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
369 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
370 if (klass == nullptr) {
371 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
372 return;
373 }
374 mirror::ObjectArray<mirror::Class>* args =
375 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700376 Runtime* runtime = Runtime::Current();
377 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700378 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700379 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700380 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700381 if (pointer_size == PointerSize::k64) {
382 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
383 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700384 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700385 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
386 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700387 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700388 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700389 if (pointer_size == PointerSize::k64) {
390 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
391 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700392 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700393 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
394 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700395 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700396 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700397 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700398}
399
Andreas Gampe633750c2016-02-19 10:49:50 -0800400void UnstartedRuntime::UnstartedClassGetEnclosingClass(
401 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
402 StackHandleScope<1> hs(self);
403 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
404 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
405 result->SetL(nullptr);
406 }
David Sehr9323e6e2016-09-13 08:58:35 -0700407 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800408}
409
Andreas Gampe715fdc22016-04-18 17:07:30 -0700410void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
411 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
412 StackHandleScope<1> hs(self);
413 Handle<mirror::Class> klass(hs.NewHandle(
414 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
415 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
416 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
417}
418
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700419static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
420 const char* entry_name,
421 size_t* size,
422 std::string* error_msg) {
423 CHECK(size != nullptr);
424
425 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
426 if (zip_archive == nullptr) {
427 return nullptr;;
428 }
429 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
430 if (zip_entry == nullptr) {
431 return nullptr;
432 }
433 std::unique_ptr<MemMap> tmp_map(
434 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
435 if (tmp_map == nullptr) {
436 return nullptr;
437 }
438
439 // OK, from here everything seems fine.
440 *size = zip_entry->GetUncompressedLength();
441 return tmp_map;
442}
443
444static void GetResourceAsStream(Thread* self,
445 ShadowFrame* shadow_frame,
446 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700447 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700448 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
449 if (resource_obj == nullptr) {
450 AbortTransactionOrFail(self, "null name for getResourceAsStream");
451 return;
452 }
453 CHECK(resource_obj->IsString());
454 mirror::String* resource_name = resource_obj->AsString();
455
456 std::string resource_name_str = resource_name->ToModifiedUtf8();
457 if (resource_name_str.empty() || resource_name_str == "/") {
458 AbortTransactionOrFail(self,
459 "Unsupported name %s for getResourceAsStream",
460 resource_name_str.c_str());
461 return;
462 }
463 const char* resource_cstr = resource_name_str.c_str();
464 if (resource_cstr[0] == '/') {
465 resource_cstr++;
466 }
467
468 Runtime* runtime = Runtime::Current();
469
470 std::vector<std::string> split;
471 Split(runtime->GetBootClassPathString(), ':', &split);
472 if (split.empty()) {
473 AbortTransactionOrFail(self,
474 "Boot classpath not set or split error:: %s",
475 runtime->GetBootClassPathString().c_str());
476 return;
477 }
478
479 std::unique_ptr<MemMap> mem_map;
480 size_t map_size;
481 std::string last_error_msg; // Only store the last message (we could concatenate).
482
483 for (const std::string& jar_file : split) {
484 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
485 if (mem_map != nullptr) {
486 break;
487 }
488 }
489
490 if (mem_map == nullptr) {
491 // Didn't find it. There's a good chance this will be the same at runtime, but still
492 // conservatively abort the transaction here.
493 AbortTransactionOrFail(self,
494 "Could not find resource %s. Last error was %s.",
495 resource_name_str.c_str(),
496 last_error_msg.c_str());
497 return;
498 }
499
500 StackHandleScope<3> hs(self);
501
502 // Create byte array for content.
503 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
504 if (h_array.Get() == nullptr) {
505 AbortTransactionOrFail(self, "Could not find/create byte array class");
506 return;
507 }
508 // Copy in content.
509 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
510 // Be proactive releasing memory.
511 mem_map.release();
512
513 // Create a ByteArrayInputStream.
514 Handle<mirror::Class> h_class(hs.NewHandle(
515 runtime->GetClassLinker()->FindClass(self,
516 "Ljava/io/ByteArrayInputStream;",
517 ScopedNullHandle<mirror::ClassLoader>())));
518 if (h_class.Get() == nullptr) {
519 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
520 return;
521 }
522 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
523 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
524 return;
525 }
526
527 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
528 if (h_obj.Get() == nullptr) {
529 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
530 return;
531 }
532
533 auto* cl = Runtime::Current()->GetClassLinker();
534 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
535 "<init>", "([B)V", cl->GetImagePointerSize());
536 if (constructor == nullptr) {
537 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
538 return;
539 }
540
541 uint32_t args[1];
542 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
543 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
544
545 if (self->IsExceptionPending()) {
546 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
547 return;
548 }
549
550 result->SetL(h_obj.Get());
551}
552
553void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
554 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
555 {
556 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
557 CHECK(this_obj != nullptr);
558 CHECK(this_obj->IsClassLoader());
559
560 StackHandleScope<1> hs(self);
561 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
562
563 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
564 this_classloader_class.Get()) {
565 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700566 "Unsupported classloader type %s for getResourceAsStream",
567 Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700568 return;
569 }
570 }
571
572 GetResourceAsStream(self, shadow_frame, result, arg_offset);
573}
574
Andreas Gampe799681b2015-05-15 19:24:12 -0700575void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700576 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700577 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
578 mirror::ClassLoader* class_loader =
579 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
580 StackHandleScope<2> hs(self);
581 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
582 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
583 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
584 "VMClassLoader.findLoadedClass", false, false);
585 // This might have an error pending. But semantics are to just return null.
586 if (self->IsExceptionPending()) {
587 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700588 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700589 if (type != "java.lang.InternalError") {
590 self->ClearException();
591 }
592 }
593}
594
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595void UnstartedRuntime::UnstartedVoidLookupType(
596 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
597 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700598 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
599}
600
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700601// Arraycopy emulation.
602// Note: we can't use any fast copy functions, as they are not available under transaction.
603
604template <typename T>
605static void PrimitiveArrayCopy(Thread* self,
606 mirror::Array* src_array, int32_t src_pos,
607 mirror::Array* dst_array, int32_t dst_pos,
608 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700609 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700610 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
611 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
David Sehr709b0702016-10-13 09:12:37 -0700612 Class::PrettyDescriptor(
613 src_array->GetClass()->GetComponentType()).c_str(),
614 Class::PrettyDescriptor(
615 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700616 return;
617 }
618 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
619 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
620 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
621 if (copy_forward) {
622 for (int32_t i = 0; i < length; ++i) {
623 dst->Set(dst_pos + i, src->Get(src_pos + i));
624 }
625 } else {
626 for (int32_t i = 1; i <= length; ++i) {
627 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
628 }
629 }
630}
631
Andreas Gampe799681b2015-05-15 19:24:12 -0700632void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700633 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700634 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700635 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
636 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700637 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700638
Andreas Gampe85a098a2016-03-31 13:30:53 -0700639 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
640 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
641 // Null checking. For simplicity, abort transaction.
642 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700643 AbortTransactionOrFail(self, "src is null in arraycopy.");
644 return;
645 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700646 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700647 AbortTransactionOrFail(self, "dst is null in arraycopy.");
648 return;
649 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700650 // Test for arrayness. Throw ArrayStoreException.
651 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
652 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
653 return;
654 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700655
Andreas Gampe85a098a2016-03-31 13:30:53 -0700656 mirror::Array* src_array = src_obj->AsArray();
657 mirror::Array* dst_array = dst_obj->AsArray();
658
659 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700660 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
661 UNLIKELY(src_pos > src_array->GetLength() - length) ||
662 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700663 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700664 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
665 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
666 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700667 return;
668 }
669
670 // Type checking.
671 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
672 GetComponentType();
673
674 if (!src_type->IsPrimitive()) {
675 // Check that the second type is not primitive.
676 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
677 GetComponentType();
678 if (trg_type->IsPrimitiveInt()) {
679 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
David Sehr709b0702016-10-13 09:12:37 -0700680 Class::PrettyDescriptor(
681 src_array->GetClass()->GetComponentType()).c_str(),
682 Class::PrettyDescriptor(
683 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700684 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700685 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700686
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700687 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
688 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
689 if (src == dst) {
690 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700691 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700692 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
693 if (copy_forward) {
694 for (int32_t i = 0; i < length; ++i) {
695 dst->Set(dst_pos + i, src->Get(src_pos + i));
696 }
697 } else {
698 for (int32_t i = 1; i <= length; ++i) {
699 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
700 }
701 }
702 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700703 // We're being lazy here. Optimally this could be a memcpy (if component types are
704 // assignable), but the ObjectArray implementation doesn't support transactions. The
705 // checking version, however, does.
706 if (Runtime::Current()->IsActiveTransaction()) {
707 dst->AssignableCheckingMemcpy<true>(
708 dst_pos, src, src_pos, length, true /* throw_exception */);
709 } else {
710 dst->AssignableCheckingMemcpy<false>(
711 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700712 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700713 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700714 } else if (src_type->IsPrimitiveByte()) {
715 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700716 } else if (src_type->IsPrimitiveChar()) {
717 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
718 } else if (src_type->IsPrimitiveInt()) {
719 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700720 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700721 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700722 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700723 }
724}
725
Andreas Gampe5c9af612016-04-05 14:16:10 -0700726void UnstartedRuntime::UnstartedSystemArraycopyByte(
727 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
728 // Just forward.
729 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
730}
731
Andreas Gampe799681b2015-05-15 19:24:12 -0700732void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700734 // Just forward.
735 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
736}
737
738void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700739 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700740 // Just forward.
741 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
742}
743
Narayan Kamath34a316f2016-03-30 13:11:18 +0100744void UnstartedRuntime::UnstartedSystemGetSecurityManager(
745 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
746 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
747 result->SetL(nullptr);
748}
749
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700750static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
751
752static void GetSystemProperty(Thread* self,
753 ShadowFrame* shadow_frame,
754 JValue* result,
755 size_t arg_offset,
756 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700757 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700758 StackHandleScope<4> hs(self);
759 Handle<mirror::String> h_key(
760 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
761 if (h_key.Get() == nullptr) {
762 AbortTransactionOrFail(self, "getProperty key was null");
763 return;
764 }
765
766 // This is overall inefficient, but reflecting the values here is not great, either. So
767 // for simplicity, and with the assumption that the number of getProperty calls is not
768 // too great, just iterate each time.
769
770 // Get the storage class.
771 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
772 Handle<mirror::Class> h_props_class(hs.NewHandle(
773 class_linker->FindClass(self,
774 "Ljava/lang/AndroidHardcodedSystemProperties;",
775 ScopedNullHandle<mirror::ClassLoader>())));
776 if (h_props_class.Get() == nullptr) {
777 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
778 return;
779 }
780 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
781 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
782 return;
783 }
784
785 // Get the storage array.
786 ArtField* static_properties =
787 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
788 "[[Ljava/lang/String;");
789 if (static_properties == nullptr) {
790 AbortTransactionOrFail(self,
791 "Could not find %s field",
792 kAndroidHardcodedSystemPropertiesFieldName);
793 return;
794 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700795 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
796 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
797 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700798 if (h_2string_array.Get() == nullptr) {
799 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
800 return;
801 }
802
803 // Iterate over it.
804 const int32_t prop_count = h_2string_array->GetLength();
805 // Use the third handle as mutable.
806 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
807 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
808 for (int32_t i = 0; i < prop_count; ++i) {
809 h_string_array.Assign(h_2string_array->Get(i));
810 if (h_string_array.Get() == nullptr ||
811 h_string_array->GetLength() != 2 ||
812 h_string_array->Get(0) == nullptr) {
813 AbortTransactionOrFail(self,
814 "Unexpected content of %s",
815 kAndroidHardcodedSystemPropertiesFieldName);
816 return;
817 }
818 if (h_key->Equals(h_string_array->Get(0))) {
819 // Found a value.
820 if (h_string_array->Get(1) == nullptr && is_default_version) {
821 // Null is being delegated to the default map, and then resolved to the given default value.
822 // As there's no default map, return the given value.
823 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
824 } else {
825 result->SetL(h_string_array->Get(1));
826 }
827 return;
828 }
829 }
830
831 // Key is not supported.
832 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
833}
834
835void UnstartedRuntime::UnstartedSystemGetProperty(
836 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
837 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
838}
839
840void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
841 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
842 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
843}
844
Andreas Gampe799681b2015-05-15 19:24:12 -0700845void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700846 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -0700847 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700848 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100849 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
850 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700851 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100852 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700853 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100854 } else if (caller ==
855 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
856 // Allocate new object.
857 StackHandleScope<2> hs(self);
858 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
859 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
860 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
861 h_real_to_string_class->AllocObject(self)));
862 if (h_real_to_string_obj.Get() != nullptr) {
863 auto* cl = Runtime::Current()->GetClassLinker();
864 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
865 "<init>", "()V", cl->GetImagePointerSize());
866 if (init_method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700867 h_real_to_string_class->DumpClass(LOG_STREAM(FATAL), mirror::Class::kDumpClassFullDetail);
Narayan Kamatha1e93122016-03-30 15:41:54 +0100868 } else {
869 JValue invoke_result;
870 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
871 nullptr);
872 if (!self->IsExceptionPending()) {
873 result->SetL(h_real_to_string_obj.Get());
874 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700875 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700876 }
877 }
878 }
879
880 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700881 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700882 }
883}
884
Sergio Giro83261202016-04-11 20:49:20 +0100885void UnstartedRuntime::UnstartedMathCeil(
886 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700887 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100888}
889
890void UnstartedRuntime::UnstartedMathFloor(
891 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700892 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700893}
894
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700895void UnstartedRuntime::UnstartedMathSin(
896 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
897 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
898}
899
900void UnstartedRuntime::UnstartedMathCos(
901 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
902 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
903}
904
905void UnstartedRuntime::UnstartedMathPow(
906 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
907 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
908 shadow_frame->GetVRegDouble(arg_offset + 2)));
909}
910
Andreas Gampe799681b2015-05-15 19:24:12 -0700911void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700913 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
914 result->SetI(obj->IdentityHashCode());
915}
916
Andreas Gampe799681b2015-05-15 19:24:12 -0700917void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700918 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700919 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000920 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700921}
922
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700923static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700924 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700925 const DexFile* dex_file = dex_cache->GetDexFile();
926 if (dex_file == nullptr) {
927 return nullptr;
928 }
929
930 // Create the direct byte buffer.
931 JNIEnv* env = self->GetJniEnv();
932 DCHECK(env != nullptr);
933 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700934 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
935 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700936 DCHECK(self->IsExceptionPending());
937 return nullptr;
938 }
939
940 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700941 args[0].l = byte_buffer.get();
942
943 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
944 WellKnownClasses::com_android_dex_Dex,
945 WellKnownClasses::com_android_dex_Dex_create,
946 args));
947
948 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700949}
950
Andreas Gampe799681b2015-05-15 19:24:12 -0700951void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700952 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700953 // We will create the Dex object, but the image writer will release it before creating the
954 // art file.
955 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
956 bool have_dex = false;
957 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700958 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700959 if (dex != nullptr) {
960 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700961 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700962 }
963 }
964 if (!have_dex) {
965 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200966 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700967 }
968}
969
970static void UnstartedMemoryPeek(
971 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
972 int64_t address = shadow_frame->GetVRegLong(arg_offset);
973 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
974 // aborting the transaction.
975
976 switch (type) {
977 case Primitive::kPrimByte: {
978 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
979 return;
980 }
981
982 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700983 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
984 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700985 return;
986 }
987
988 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700989 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
990 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700991 return;
992 }
993
994 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700995 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
996 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700997 return;
998 }
999
1000 case Primitive::kPrimBoolean:
1001 case Primitive::kPrimChar:
1002 case Primitive::kPrimFloat:
1003 case Primitive::kPrimDouble:
1004 case Primitive::kPrimVoid:
1005 case Primitive::kPrimNot:
1006 LOG(FATAL) << "Not in the Memory API: " << type;
1007 UNREACHABLE();
1008 }
1009 LOG(FATAL) << "Should not reach here";
1010 UNREACHABLE();
1011}
1012
Andreas Gampe799681b2015-05-15 19:24:12 -07001013void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001014 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001015 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1016}
1017
1018void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001020 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1021}
1022
1023void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001024 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001025 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1026}
1027
1028void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001029 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001030 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001031}
1032
1033static void UnstartedMemoryPeekArray(
1034 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001035 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001036 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1037 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1038 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001039 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001040 return;
1041 }
1042 mirror::Array* array = obj->AsArray();
1043
1044 int offset = shadow_frame->GetVReg(arg_offset + 3);
1045 int count = shadow_frame->GetVReg(arg_offset + 4);
1046 if (offset < 0 || offset + count > array->GetLength()) {
1047 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1048 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001049 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001050 return;
1051 }
1052
1053 switch (type) {
1054 case Primitive::kPrimByte: {
1055 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1056 mirror::ByteArray* byte_array = array->AsByteArray();
1057 for (int32_t i = 0; i < count; ++i, ++address) {
1058 byte_array->SetWithoutChecks<true>(i + offset, *address);
1059 }
1060 return;
1061 }
1062
1063 case Primitive::kPrimShort:
1064 case Primitive::kPrimInt:
1065 case Primitive::kPrimLong:
1066 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1067 UNREACHABLE();
1068
1069 case Primitive::kPrimBoolean:
1070 case Primitive::kPrimChar:
1071 case Primitive::kPrimFloat:
1072 case Primitive::kPrimDouble:
1073 case Primitive::kPrimVoid:
1074 case Primitive::kPrimNot:
1075 LOG(FATAL) << "Not in the Memory API: " << type;
1076 UNREACHABLE();
1077 }
1078 LOG(FATAL) << "Should not reach here";
1079 UNREACHABLE();
1080}
1081
Andreas Gampe799681b2015-05-15 19:24:12 -07001082void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001083 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001084 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001085}
1086
Kenny Root1c9e61c2015-05-14 15:58:17 -07001087// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001088void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001089 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001090 jint start = shadow_frame->GetVReg(arg_offset + 1);
1091 jint end = shadow_frame->GetVReg(arg_offset + 2);
1092 jint index = shadow_frame->GetVReg(arg_offset + 4);
1093 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1094 if (string == nullptr) {
1095 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1096 return;
1097 }
Kenny Root57f91e82015-05-14 15:58:17 -07001098 DCHECK_GE(start, 0);
1099 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001100 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001101 Handle<mirror::CharArray> h_char_array(
1102 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001103 DCHECK_LE(index, h_char_array->GetLength());
1104 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001105 string->GetChars(start, end, h_char_array, index);
1106}
1107
1108// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001109void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001110 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001111 jint index = shadow_frame->GetVReg(arg_offset + 1);
1112 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1113 if (string == nullptr) {
1114 AbortTransactionOrFail(self, "String.charAt with null object");
1115 return;
1116 }
1117 result->SetC(string->CharAt(index));
1118}
1119
Kenny Root57f91e82015-05-14 15:58:17 -07001120// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001121void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001122 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001123 jint index = shadow_frame->GetVReg(arg_offset + 1);
1124 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1125 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1126 if (string == nullptr) {
1127 AbortTransactionOrFail(self, "String.setCharAt with null object");
1128 return;
1129 }
1130 string->SetCharAt(index, c);
1131}
1132
Kenny Root1c9e61c2015-05-14 15:58:17 -07001133// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001134void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001135 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001136 jint offset = shadow_frame->GetVReg(arg_offset);
1137 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1138 DCHECK_GE(char_count, 0);
1139 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001140 Handle<mirror::CharArray> h_char_array(
1141 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001142 Runtime* runtime = Runtime::Current();
1143 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1144 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1145}
1146
1147// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001148void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001149 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001150 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1151 if (to_copy == nullptr) {
1152 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1153 return;
1154 }
1155 StackHandleScope<1> hs(self);
1156 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1157 Runtime* runtime = Runtime::Current();
1158 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1159 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1160 allocator));
1161}
1162
Andreas Gampe799681b2015-05-15 19:24:12 -07001163void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001164 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001165 jint start = shadow_frame->GetVReg(arg_offset + 1);
1166 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001167 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001168 DCHECK_GE(length, 0);
1169 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001170 Handle<mirror::String> h_string(
1171 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001172 DCHECK_LE(start, h_string->GetLength());
1173 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001174 Runtime* runtime = Runtime::Current();
1175 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1176 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1177}
1178
Kenny Root57f91e82015-05-14 15:58:17 -07001179// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001180void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001181 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001182 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001183 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1184 if (string == nullptr) {
1185 AbortTransactionOrFail(self, "String.charAt with null object");
1186 return;
1187 }
1188 result->SetL(string->ToCharArray(self));
1189}
1190
Andreas Gampebc4d2182016-02-22 10:03:12 -08001191// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1192void UnstartedRuntime::UnstartedReferenceGetReferent(
1193 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001194 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001195 shadow_frame->GetVRegReference(arg_offset));
1196 if (ref == nullptr) {
1197 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1198 return;
1199 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001200 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001201 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1202 result->SetL(referent);
1203}
1204
1205// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1206// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1207// where we can predict the behavior (somewhat).
1208// Note: this is required (instead of lazy initialization) as these classes are used in the static
1209// initialization of other classes, so will *use* the value.
1210void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1211 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -07001212 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampebc4d2182016-02-22 10:03:12 -08001213 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1214 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1215 // 8 as a conservative upper approximation.
1216 result->SetI(8);
1217 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1218 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1219 // a good upper bound.
1220 // TODO: Consider resetting in the zygote?
1221 result->SetI(8);
1222 } else {
1223 // Not supported.
1224 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1225 }
1226}
1227
1228// This allows accessing ConcurrentHashMap/SynchronousQueue.
1229
1230void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1231 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1232 // Argument 0 is the Unsafe instance, skip.
1233 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1234 if (obj == nullptr) {
1235 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1236 return;
1237 }
1238 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1239 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1240 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001241 bool success;
1242 // Check whether we're in a transaction, call accordingly.
1243 if (Runtime::Current()->IsActiveTransaction()) {
1244 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1245 expectedValue,
1246 newValue);
1247 } else {
1248 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1249 expectedValue,
1250 newValue);
1251 }
1252 result->SetZ(success ? 1 : 0);
1253}
1254
1255void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1256 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1257 // Argument 0 is the Unsafe instance, skip.
1258 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1259 if (obj == nullptr) {
1260 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1261 return;
1262 }
1263 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1264 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1265 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1266
1267 // Must use non transactional mode.
1268 if (kUseReadBarrier) {
1269 // Need to make sure the reference stored in the field is a to-space one before attempting the
1270 // CAS or the CAS could fail incorrectly.
1271 mirror::HeapReference<mirror::Object>* field_addr =
1272 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1273 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001274 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001275 obj,
1276 MemberOffset(offset),
1277 field_addr);
1278 }
1279 bool success;
1280 // Check whether we're in a transaction, call accordingly.
1281 if (Runtime::Current()->IsActiveTransaction()) {
1282 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1283 expected_value,
1284 newValue);
1285 } else {
1286 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1287 expected_value,
1288 newValue);
1289 }
1290 result->SetZ(success ? 1 : 0);
1291}
1292
1293void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1294 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001295 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001296 // Argument 0 is the Unsafe instance, skip.
1297 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1298 if (obj == nullptr) {
1299 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1300 return;
1301 }
1302 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1303 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1304 result->SetL(value);
1305}
1306
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001307void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1308 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001309 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001310 // Argument 0 is the Unsafe instance, skip.
1311 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1312 if (obj == nullptr) {
1313 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1314 return;
1315 }
1316 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1317 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1318 if (Runtime::Current()->IsActiveTransaction()) {
1319 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1320 } else {
1321 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1322 }
1323}
1324
Andreas Gampebc4d2182016-02-22 10:03:12 -08001325void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1326 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001327 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001328 // Argument 0 is the Unsafe instance, skip.
1329 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1330 if (obj == nullptr) {
1331 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1332 return;
1333 }
1334 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1335 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1336 QuasiAtomic::ThreadFenceRelease();
1337 if (Runtime::Current()->IsActiveTransaction()) {
1338 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1339 } else {
1340 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1341 }
1342}
1343
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001344// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1345// of correctly handling the corner cases.
1346void UnstartedRuntime::UnstartedIntegerParseInt(
1347 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001348 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001349 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1350 if (obj == nullptr) {
1351 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1352 return;
1353 }
1354
1355 std::string string_value = obj->AsString()->ToModifiedUtf8();
1356 if (string_value.empty()) {
1357 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1358 return;
1359 }
1360
1361 const char* c_str = string_value.c_str();
1362 char *end;
1363 // Can we set errno to 0? Is this always a variable, and not a macro?
1364 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1365 int64_t l = strtol(c_str, &end, 10);
1366
1367 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1368 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1369 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1370 return;
1371 }
1372 if (l == 0) {
1373 // Check whether the string wasn't exactly zero.
1374 if (string_value != "0") {
1375 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1376 return;
1377 }
1378 } else if (*end != '\0') {
1379 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1380 return;
1381 }
1382
1383 result->SetI(static_cast<int32_t>(l));
1384}
1385
1386// A cutout for Long.parseLong.
1387//
1388// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1389// well.
1390void UnstartedRuntime::UnstartedLongParseLong(
1391 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001392 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001393 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1394 if (obj == nullptr) {
1395 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1396 return;
1397 }
1398
1399 std::string string_value = obj->AsString()->ToModifiedUtf8();
1400 if (string_value.empty()) {
1401 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1402 return;
1403 }
1404
1405 const char* c_str = string_value.c_str();
1406 char *end;
1407 // Can we set errno to 0? Is this always a variable, and not a macro?
1408 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1409 int64_t l = strtol(c_str, &end, 10);
1410
1411 // Note: comparing against int32_t min/max is intentional here.
1412 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1413 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1414 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1415 return;
1416 }
1417 if (l == 0) {
1418 // Check whether the string wasn't exactly zero.
1419 if (string_value != "0") {
1420 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1421 return;
1422 }
1423 } else if (*end != '\0') {
1424 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1425 return;
1426 }
1427
1428 result->SetJ(l);
1429}
1430
Andreas Gampe715fdc22016-04-18 17:07:30 -07001431void UnstartedRuntime::UnstartedMethodInvoke(
1432 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001433 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001434 JNIEnvExt* env = self->GetJniEnv();
1435 ScopedObjectAccessUnchecked soa(self);
1436
Mathieu Chartier8778c522016-10-04 19:06:30 -07001437 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001438 ScopedLocalRef<jobject> java_method(env,
1439 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1440
Mathieu Chartier8778c522016-10-04 19:06:30 -07001441 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001442 ScopedLocalRef<jobject> java_receiver(env,
1443 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1444
Mathieu Chartier8778c522016-10-04 19:06:30 -07001445 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001446 ScopedLocalRef<jobject> java_args(env,
1447 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1448
1449 ScopedLocalRef<jobject> result_jobj(env,
1450 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1451
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001452 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001453
1454 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1455 // InvocationTargetExceptions.
1456 if (self->IsExceptionPending()) {
1457 AbortTransactionOrFail(self, "Failed Method.invoke");
1458 }
1459}
1460
Andreas Gampebc4d2182016-02-22 10:03:12 -08001461
Mathieu Chartiere401d142015-04-22 13:56:20 -07001462void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1463 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1464 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001465 int32_t length = args[1];
1466 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001467 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001468 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001469 ObjPtr<mirror::Class> array_class =
1470 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001471 DCHECK(array_class != nullptr);
1472 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001473 result->SetL(mirror::Array::Alloc<true, true>(self,
1474 array_class,
1475 length,
1476 array_class->GetComponentSizeShift(),
1477 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001478}
1479
Mathieu Chartiere401d142015-04-22 13:56:20 -07001480void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1481 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1482 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001483 result->SetL(nullptr);
1484}
1485
Mathieu Chartiere401d142015-04-22 13:56:20 -07001486void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1487 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1488 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001489 NthCallerVisitor visitor(self, 3);
1490 visitor.WalkStack();
1491 if (visitor.caller != nullptr) {
1492 result->SetL(visitor.caller->GetDeclaringClass());
1493 }
1494}
1495
Mathieu Chartiere401d142015-04-22 13:56:20 -07001496void UnstartedRuntime::UnstartedJNIMathLog(
1497 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1498 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001499 JValue value;
1500 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1501 result->SetD(log(value.GetD()));
1502}
1503
Mathieu Chartiere401d142015-04-22 13:56:20 -07001504void UnstartedRuntime::UnstartedJNIMathExp(
1505 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1506 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001507 JValue value;
1508 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1509 result->SetD(exp(value.GetD()));
1510}
1511
Andreas Gampebc4d2182016-02-22 10:03:12 -08001512void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1513 Thread* self ATTRIBUTE_UNUSED,
1514 ArtMethod* method ATTRIBUTE_UNUSED,
1515 mirror::Object* receiver ATTRIBUTE_UNUSED,
1516 uint32_t* args ATTRIBUTE_UNUSED,
1517 JValue* result) {
1518 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1519 ? 0
1520 : 1);
1521}
1522
Mathieu Chartiere401d142015-04-22 13:56:20 -07001523void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1524 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1525 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001526 StackHandleScope<1> hs(self);
1527 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1528}
1529
Andreas Gampebc4d2182016-02-22 10:03:12 -08001530void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1531 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1532 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1533 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1534 result->SetD(bit_cast<double>(long_input));
1535}
1536
Mathieu Chartiere401d142015-04-22 13:56:20 -07001537void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1538 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1539 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001540 result->SetI(args[0]);
1541}
1542
Mathieu Chartiere401d142015-04-22 13:56:20 -07001543void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1544 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1545 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001546 result->SetI(args[0]);
1547}
1548
Mathieu Chartiere401d142015-04-22 13:56:20 -07001549void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1550 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1551 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001552 result->SetL(receiver->Clone(self));
1553}
1554
Mathieu Chartiere401d142015-04-22 13:56:20 -07001555void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1556 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1557 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001558 receiver->NotifyAll(self);
1559}
1560
Mathieu Chartiere401d142015-04-22 13:56:20 -07001561void UnstartedRuntime::UnstartedJNIStringCompareTo(
1562 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1563 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001564 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1565 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001566 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001567 }
1568 result->SetI(receiver->AsString()->CompareTo(rhs));
1569}
1570
Mathieu Chartiere401d142015-04-22 13:56:20 -07001571void UnstartedRuntime::UnstartedJNIStringIntern(
1572 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1573 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001574 result->SetL(receiver->AsString()->Intern());
1575}
1576
Mathieu Chartiere401d142015-04-22 13:56:20 -07001577void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1578 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1579 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001580 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1581}
1582
Mathieu Chartiere401d142015-04-22 13:56:20 -07001583void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1584 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1585 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001586 StackHandleScope<2> hs(self);
1587 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1588 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1589 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1590}
1591
Mathieu Chartiere401d142015-04-22 13:56:20 -07001592void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1593 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1594 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001595 int32_t length = static_cast<int32_t>(args[1]);
1596 if (length < 0) {
1597 ThrowNegativeArraySizeException(length);
1598 return;
1599 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001600 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001601 Runtime* runtime = Runtime::Current();
1602 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001603 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001604 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001605 CHECK(self->IsExceptionPending());
1606 return;
1607 }
1608 DCHECK(array_class->IsObjectArrayClass());
1609 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1610 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1611 result->SetL(new_array);
1612}
1613
Mathieu Chartiere401d142015-04-22 13:56:20 -07001614void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1615 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1616 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001617 ScopedObjectAccessUnchecked soa(self);
1618 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001619 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001620 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001621 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001622 }
1623}
1624
Mathieu Chartiere401d142015-04-22 13:56:20 -07001625void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1626 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1627 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001628 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1629 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1630}
1631
Mathieu Chartiere401d142015-04-22 13:56:20 -07001632void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1633 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1634 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001635 result->SetZ(JNI_TRUE);
1636}
1637
Mathieu Chartiere401d142015-04-22 13:56:20 -07001638void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1639 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1640 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001641 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1642 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1643 jint expectedValue = args[3];
1644 jint newValue = args[4];
1645 bool success;
1646 if (Runtime::Current()->IsActiveTransaction()) {
1647 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1648 expectedValue, newValue);
1649 } else {
1650 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1651 expectedValue, newValue);
1652 }
1653 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1654}
1655
Narayan Kamath34a316f2016-03-30 13:11:18 +01001656void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1657 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1658 uint32_t* args, JValue* result) {
1659 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1660 if (obj == nullptr) {
1661 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1662 return;
1663 }
1664
1665 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1666 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1667}
1668
Mathieu Chartiere401d142015-04-22 13:56:20 -07001669void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1670 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1671 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001672 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1673 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1674 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1675 if (Runtime::Current()->IsActiveTransaction()) {
1676 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1677 } else {
1678 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1679 }
1680}
1681
Andreas Gampe799681b2015-05-15 19:24:12 -07001682void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001683 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1684 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001685 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1686 Primitive::Type primitive_type = component->GetPrimitiveType();
1687 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1688}
1689
Andreas Gampe799681b2015-05-15 19:24:12 -07001690void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001691 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1692 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001693 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1694 Primitive::Type primitive_type = component->GetPrimitiveType();
1695 result->SetI(Primitive::ComponentSize(primitive_type));
1696}
1697
Andreas Gampedd9d0552015-03-09 12:57:41 -07001698typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001699 size_t arg_size);
1700
Mathieu Chartiere401d142015-04-22 13:56:20 -07001701typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001702 uint32_t* args, JValue* result);
1703
1704static bool tables_initialized_ = false;
1705static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1706static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1707
Andreas Gampe799681b2015-05-15 19:24:12 -07001708void UnstartedRuntime::InitializeInvokeHandlers() {
1709#define UNSTARTED_DIRECT(ShortName, Sig) \
1710 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1711#include "unstarted_runtime_list.h"
1712 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1713#undef UNSTARTED_RUNTIME_DIRECT_LIST
1714#undef UNSTARTED_RUNTIME_JNI_LIST
1715#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001716}
1717
Andreas Gampe799681b2015-05-15 19:24:12 -07001718void UnstartedRuntime::InitializeJNIHandlers() {
1719#define UNSTARTED_JNI(ShortName, Sig) \
1720 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1721#include "unstarted_runtime_list.h"
1722 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1723#undef UNSTARTED_RUNTIME_DIRECT_LIST
1724#undef UNSTARTED_RUNTIME_JNI_LIST
1725#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001726}
1727
Andreas Gampe799681b2015-05-15 19:24:12 -07001728void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001729 CHECK(!tables_initialized_);
1730
Andreas Gampe799681b2015-05-15 19:24:12 -07001731 InitializeInvokeHandlers();
1732 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001733
1734 tables_initialized_ = true;
1735}
1736
Andreas Gampe799681b2015-05-15 19:24:12 -07001737void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1738 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001739 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1740 // problems in core libraries.
1741 CHECK(tables_initialized_);
1742
David Sehr709b0702016-10-13 09:12:37 -07001743 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001744 const auto& iter = invoke_handlers_.find(name);
1745 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001746 // Clear out the result in case it's not zeroed out.
1747 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001748
1749 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1750 self->PushShadowFrame(shadow_frame);
1751
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001752 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001753
1754 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001755 } else {
1756 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001757 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001758 }
1759}
1760
1761// 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 -07001762void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001763 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001764 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001765 const auto& iter = jni_handlers_.find(name);
1766 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001767 // Clear out the result in case it's not zeroed out.
1768 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001769 (*iter->second)(self, method, receiver, args, result);
1770 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001771 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1772 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001773 } else {
David Sehr709b0702016-10-13 09:12:37 -07001774 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001775 "non-transactional runtime";
1776 }
1777}
1778
1779} // namespace interpreter
1780} // namespace art