blob: 2257fd6395b189d79d5944ed586aefa07b47c7bb [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",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700567 mirror::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()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700611 AbortTransactionOrFail(self,
612 "Types mismatched in arraycopy: %s vs %s.",
613 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700614 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700615 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700616 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700617 return;
618 }
619 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
620 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
621 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
622 if (copy_forward) {
623 for (int32_t i = 0; i < length; ++i) {
624 dst->Set(dst_pos + i, src->Get(src_pos + i));
625 }
626 } else {
627 for (int32_t i = 1; i <= length; ++i) {
628 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
629 }
630 }
631}
632
Andreas Gampe799681b2015-05-15 19:24:12 -0700633void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700634 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700635 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700636 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
637 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700638 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700639
Andreas Gampe85a098a2016-03-31 13:30:53 -0700640 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
641 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
642 // Null checking. For simplicity, abort transaction.
643 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700644 AbortTransactionOrFail(self, "src is null in arraycopy.");
645 return;
646 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700647 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700648 AbortTransactionOrFail(self, "dst is null in arraycopy.");
649 return;
650 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700651 // Test for arrayness. Throw ArrayStoreException.
652 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
653 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
654 return;
655 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700656
Andreas Gampe85a098a2016-03-31 13:30:53 -0700657 mirror::Array* src_array = src_obj->AsArray();
658 mirror::Array* dst_array = dst_obj->AsArray();
659
660 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700661 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
662 UNLIKELY(src_pos > src_array->GetLength() - length) ||
663 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700664 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700665 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
666 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
667 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700668 return;
669 }
670
671 // Type checking.
672 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
673 GetComponentType();
674
675 if (!src_type->IsPrimitive()) {
676 // Check that the second type is not primitive.
677 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
678 GetComponentType();
679 if (trg_type->IsPrimitiveInt()) {
680 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700681 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700682 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700683 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700684 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700685 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700686 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700687
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700688 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
689 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
690 if (src == dst) {
691 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700692 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700693 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
694 if (copy_forward) {
695 for (int32_t i = 0; i < length; ++i) {
696 dst->Set(dst_pos + i, src->Get(src_pos + i));
697 }
698 } else {
699 for (int32_t i = 1; i <= length; ++i) {
700 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
701 }
702 }
703 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700704 // We're being lazy here. Optimally this could be a memcpy (if component types are
705 // assignable), but the ObjectArray implementation doesn't support transactions. The
706 // checking version, however, does.
707 if (Runtime::Current()->IsActiveTransaction()) {
708 dst->AssignableCheckingMemcpy<true>(
709 dst_pos, src, src_pos, length, true /* throw_exception */);
710 } else {
711 dst->AssignableCheckingMemcpy<false>(
712 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700713 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700714 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700715 } else if (src_type->IsPrimitiveByte()) {
716 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700717 } else if (src_type->IsPrimitiveChar()) {
718 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
719 } else if (src_type->IsPrimitiveInt()) {
720 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700721 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700722 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700723 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700724 }
725}
726
Andreas Gampe5c9af612016-04-05 14:16:10 -0700727void UnstartedRuntime::UnstartedSystemArraycopyByte(
728 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
729 // Just forward.
730 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
731}
732
Andreas Gampe799681b2015-05-15 19:24:12 -0700733void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700734 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700735 // Just forward.
736 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
737}
738
739void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700740 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700741 // Just forward.
742 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
743}
744
Narayan Kamath34a316f2016-03-30 13:11:18 +0100745void UnstartedRuntime::UnstartedSystemGetSecurityManager(
746 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
747 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
748 result->SetL(nullptr);
749}
750
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700751static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
752
753static void GetSystemProperty(Thread* self,
754 ShadowFrame* shadow_frame,
755 JValue* result,
756 size_t arg_offset,
757 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700758 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700759 StackHandleScope<4> hs(self);
760 Handle<mirror::String> h_key(
761 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
762 if (h_key.Get() == nullptr) {
763 AbortTransactionOrFail(self, "getProperty key was null");
764 return;
765 }
766
767 // This is overall inefficient, but reflecting the values here is not great, either. So
768 // for simplicity, and with the assumption that the number of getProperty calls is not
769 // too great, just iterate each time.
770
771 // Get the storage class.
772 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
773 Handle<mirror::Class> h_props_class(hs.NewHandle(
774 class_linker->FindClass(self,
775 "Ljava/lang/AndroidHardcodedSystemProperties;",
776 ScopedNullHandle<mirror::ClassLoader>())));
777 if (h_props_class.Get() == nullptr) {
778 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
779 return;
780 }
781 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
782 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
783 return;
784 }
785
786 // Get the storage array.
787 ArtField* static_properties =
788 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
789 "[[Ljava/lang/String;");
790 if (static_properties == nullptr) {
791 AbortTransactionOrFail(self,
792 "Could not find %s field",
793 kAndroidHardcodedSystemPropertiesFieldName);
794 return;
795 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700796 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
797 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
798 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700799 if (h_2string_array.Get() == nullptr) {
800 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
801 return;
802 }
803
804 // Iterate over it.
805 const int32_t prop_count = h_2string_array->GetLength();
806 // Use the third handle as mutable.
807 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
808 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
809 for (int32_t i = 0; i < prop_count; ++i) {
810 h_string_array.Assign(h_2string_array->Get(i));
811 if (h_string_array.Get() == nullptr ||
812 h_string_array->GetLength() != 2 ||
813 h_string_array->Get(0) == nullptr) {
814 AbortTransactionOrFail(self,
815 "Unexpected content of %s",
816 kAndroidHardcodedSystemPropertiesFieldName);
817 return;
818 }
819 if (h_key->Equals(h_string_array->Get(0))) {
820 // Found a value.
821 if (h_string_array->Get(1) == nullptr && is_default_version) {
822 // Null is being delegated to the default map, and then resolved to the given default value.
823 // As there's no default map, return the given value.
824 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
825 } else {
826 result->SetL(h_string_array->Get(1));
827 }
828 return;
829 }
830 }
831
832 // Key is not supported.
833 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
834}
835
836void UnstartedRuntime::UnstartedSystemGetProperty(
837 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
838 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
839}
840
841void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
842 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
843 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
844}
845
Andreas Gampe799681b2015-05-15 19:24:12 -0700846void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700847 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -0700848 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700849 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100850 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
851 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700852 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100853 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700854 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100855 } else if (caller ==
856 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
857 // Allocate new object.
858 StackHandleScope<2> hs(self);
859 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
860 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
861 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
862 h_real_to_string_class->AllocObject(self)));
863 if (h_real_to_string_obj.Get() != nullptr) {
864 auto* cl = Runtime::Current()->GetClassLinker();
865 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
866 "<init>", "()V", cl->GetImagePointerSize());
867 if (init_method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700868 h_real_to_string_class->DumpClass(LOG_STREAM(FATAL), mirror::Class::kDumpClassFullDetail);
Narayan Kamatha1e93122016-03-30 15:41:54 +0100869 } else {
870 JValue invoke_result;
871 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
872 nullptr);
873 if (!self->IsExceptionPending()) {
874 result->SetL(h_real_to_string_obj.Get());
875 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700876 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700877 }
878 }
879 }
880
881 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700882 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700883 }
884}
885
Sergio Giro83261202016-04-11 20:49:20 +0100886void UnstartedRuntime::UnstartedMathCeil(
887 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700888 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100889}
890
891void UnstartedRuntime::UnstartedMathFloor(
892 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700893 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700894}
895
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700896void UnstartedRuntime::UnstartedMathSin(
897 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
898 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
899}
900
901void UnstartedRuntime::UnstartedMathCos(
902 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
903 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
904}
905
906void UnstartedRuntime::UnstartedMathPow(
907 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
908 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
909 shadow_frame->GetVRegDouble(arg_offset + 2)));
910}
911
Andreas Gampe799681b2015-05-15 19:24:12 -0700912void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700914 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
915 result->SetI(obj->IdentityHashCode());
916}
917
Andreas Gampe799681b2015-05-15 19:24:12 -0700918void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700919 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700920 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000921 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700922}
923
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700924static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700925 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700926 const DexFile* dex_file = dex_cache->GetDexFile();
927 if (dex_file == nullptr) {
928 return nullptr;
929 }
930
931 // Create the direct byte buffer.
932 JNIEnv* env = self->GetJniEnv();
933 DCHECK(env != nullptr);
934 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700935 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
936 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700937 DCHECK(self->IsExceptionPending());
938 return nullptr;
939 }
940
941 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700942 args[0].l = byte_buffer.get();
943
944 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
945 WellKnownClasses::com_android_dex_Dex,
946 WellKnownClasses::com_android_dex_Dex_create,
947 args));
948
949 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700950}
951
Andreas Gampe799681b2015-05-15 19:24:12 -0700952void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700953 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700954 // We will create the Dex object, but the image writer will release it before creating the
955 // art file.
956 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
957 bool have_dex = false;
958 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700959 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700960 if (dex != nullptr) {
961 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700962 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700963 }
964 }
965 if (!have_dex) {
966 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200967 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700968 }
969}
970
971static void UnstartedMemoryPeek(
972 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
973 int64_t address = shadow_frame->GetVRegLong(arg_offset);
974 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
975 // aborting the transaction.
976
977 switch (type) {
978 case Primitive::kPrimByte: {
979 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
980 return;
981 }
982
983 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700984 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
985 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700986 return;
987 }
988
989 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700990 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
991 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700992 return;
993 }
994
995 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700996 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
997 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700998 return;
999 }
1000
1001 case Primitive::kPrimBoolean:
1002 case Primitive::kPrimChar:
1003 case Primitive::kPrimFloat:
1004 case Primitive::kPrimDouble:
1005 case Primitive::kPrimVoid:
1006 case Primitive::kPrimNot:
1007 LOG(FATAL) << "Not in the Memory API: " << type;
1008 UNREACHABLE();
1009 }
1010 LOG(FATAL) << "Should not reach here";
1011 UNREACHABLE();
1012}
1013
Andreas Gampe799681b2015-05-15 19:24:12 -07001014void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001015 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001016 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1017}
1018
1019void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001020 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001021 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1022}
1023
1024void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001025 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001026 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1027}
1028
1029void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001030 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001031 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001032}
1033
1034static void UnstartedMemoryPeekArray(
1035 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001036 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001037 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1038 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1039 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001040 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001041 return;
1042 }
1043 mirror::Array* array = obj->AsArray();
1044
1045 int offset = shadow_frame->GetVReg(arg_offset + 3);
1046 int count = shadow_frame->GetVReg(arg_offset + 4);
1047 if (offset < 0 || offset + count > array->GetLength()) {
1048 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1049 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001050 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001051 return;
1052 }
1053
1054 switch (type) {
1055 case Primitive::kPrimByte: {
1056 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1057 mirror::ByteArray* byte_array = array->AsByteArray();
1058 for (int32_t i = 0; i < count; ++i, ++address) {
1059 byte_array->SetWithoutChecks<true>(i + offset, *address);
1060 }
1061 return;
1062 }
1063
1064 case Primitive::kPrimShort:
1065 case Primitive::kPrimInt:
1066 case Primitive::kPrimLong:
1067 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1068 UNREACHABLE();
1069
1070 case Primitive::kPrimBoolean:
1071 case Primitive::kPrimChar:
1072 case Primitive::kPrimFloat:
1073 case Primitive::kPrimDouble:
1074 case Primitive::kPrimVoid:
1075 case Primitive::kPrimNot:
1076 LOG(FATAL) << "Not in the Memory API: " << type;
1077 UNREACHABLE();
1078 }
1079 LOG(FATAL) << "Should not reach here";
1080 UNREACHABLE();
1081}
1082
Andreas Gampe799681b2015-05-15 19:24:12 -07001083void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001084 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001085 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001086}
1087
Kenny Root1c9e61c2015-05-14 15:58:17 -07001088// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001089void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001090 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001091 jint start = shadow_frame->GetVReg(arg_offset + 1);
1092 jint end = shadow_frame->GetVReg(arg_offset + 2);
1093 jint index = shadow_frame->GetVReg(arg_offset + 4);
1094 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1095 if (string == nullptr) {
1096 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1097 return;
1098 }
Kenny Root57f91e82015-05-14 15:58:17 -07001099 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001100 DCHECK_LE(start, end);
1101 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001102 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001103 Handle<mirror::CharArray> h_char_array(
1104 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001105 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001106 DCHECK_LE(index, h_char_array->GetLength());
1107 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001108 string->GetChars(start, end, h_char_array, index);
1109}
1110
1111// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001112void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001113 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001114 jint index = shadow_frame->GetVReg(arg_offset + 1);
1115 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1116 if (string == nullptr) {
1117 AbortTransactionOrFail(self, "String.charAt with null object");
1118 return;
1119 }
1120 result->SetC(string->CharAt(index));
1121}
1122
Kenny Root57f91e82015-05-14 15:58:17 -07001123// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001124void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001125 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001126 jint index = shadow_frame->GetVReg(arg_offset + 1);
1127 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1128 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1129 if (string == nullptr) {
1130 AbortTransactionOrFail(self, "String.setCharAt with null object");
1131 return;
1132 }
1133 string->SetCharAt(index, c);
1134}
1135
Kenny Root1c9e61c2015-05-14 15:58:17 -07001136// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001137void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001138 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001139 jint offset = shadow_frame->GetVReg(arg_offset);
1140 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1141 DCHECK_GE(char_count, 0);
1142 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143 Handle<mirror::CharArray> h_char_array(
1144 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001145 Runtime* runtime = Runtime::Current();
1146 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1147 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1148}
1149
1150// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001151void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001152 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001153 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1154 if (to_copy == nullptr) {
1155 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1156 return;
1157 }
1158 StackHandleScope<1> hs(self);
1159 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1160 Runtime* runtime = Runtime::Current();
1161 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1162 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1163 allocator));
1164}
1165
Andreas Gampe799681b2015-05-15 19:24:12 -07001166void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001167 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001168 jint start = shadow_frame->GetVReg(arg_offset + 1);
1169 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001170 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001171 DCHECK_GE(length, 0);
1172 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001173 Handle<mirror::String> h_string(
1174 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001175 DCHECK_LE(start, h_string->GetLength());
1176 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001177 Runtime* runtime = Runtime::Current();
1178 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1179 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1180}
1181
Kenny Root57f91e82015-05-14 15:58:17 -07001182// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001183void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001184 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001185 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001186 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1187 if (string == nullptr) {
1188 AbortTransactionOrFail(self, "String.charAt with null object");
1189 return;
1190 }
1191 result->SetL(string->ToCharArray(self));
1192}
1193
Andreas Gampebc4d2182016-02-22 10:03:12 -08001194// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1195void UnstartedRuntime::UnstartedReferenceGetReferent(
1196 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001197 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001198 shadow_frame->GetVRegReference(arg_offset));
1199 if (ref == nullptr) {
1200 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1201 return;
1202 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001203 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001204 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1205 result->SetL(referent);
1206}
1207
1208// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1209// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1210// where we can predict the behavior (somewhat).
1211// Note: this is required (instead of lazy initialization) as these classes are used in the static
1212// initialization of other classes, so will *use* the value.
1213void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1214 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
David Sehr709b0702016-10-13 09:12:37 -07001215 std::string caller(ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod()));
Andreas Gampebc4d2182016-02-22 10:03:12 -08001216 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1217 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1218 // 8 as a conservative upper approximation.
1219 result->SetI(8);
1220 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1221 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1222 // a good upper bound.
1223 // TODO: Consider resetting in the zygote?
1224 result->SetI(8);
1225 } else {
1226 // Not supported.
1227 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1228 }
1229}
1230
1231// This allows accessing ConcurrentHashMap/SynchronousQueue.
1232
1233void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1234 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1235 // Argument 0 is the Unsafe instance, skip.
1236 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1237 if (obj == nullptr) {
1238 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1239 return;
1240 }
1241 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1242 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1243 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001244 bool success;
1245 // Check whether we're in a transaction, call accordingly.
1246 if (Runtime::Current()->IsActiveTransaction()) {
1247 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1248 expectedValue,
1249 newValue);
1250 } else {
1251 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1252 expectedValue,
1253 newValue);
1254 }
1255 result->SetZ(success ? 1 : 0);
1256}
1257
1258void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1259 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1260 // Argument 0 is the Unsafe instance, skip.
1261 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1262 if (obj == nullptr) {
1263 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1264 return;
1265 }
1266 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1267 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1268 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1269
1270 // Must use non transactional mode.
1271 if (kUseReadBarrier) {
1272 // Need to make sure the reference stored in the field is a to-space one before attempting the
1273 // CAS or the CAS could fail incorrectly.
1274 mirror::HeapReference<mirror::Object>* field_addr =
1275 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1276 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Roland Levillaina1aa3b12016-10-26 13:03:38 +01001277 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001278 obj,
1279 MemberOffset(offset),
1280 field_addr);
1281 }
1282 bool success;
1283 // Check whether we're in a transaction, call accordingly.
1284 if (Runtime::Current()->IsActiveTransaction()) {
1285 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1286 expected_value,
1287 newValue);
1288 } else {
1289 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1290 expected_value,
1291 newValue);
1292 }
1293 result->SetZ(success ? 1 : 0);
1294}
1295
1296void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1297 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001298 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001299 // Argument 0 is the Unsafe instance, skip.
1300 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1301 if (obj == nullptr) {
1302 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1303 return;
1304 }
1305 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1306 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1307 result->SetL(value);
1308}
1309
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001310void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1311 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001312 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001313 // Argument 0 is the Unsafe instance, skip.
1314 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1315 if (obj == nullptr) {
1316 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1317 return;
1318 }
1319 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1320 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1321 if (Runtime::Current()->IsActiveTransaction()) {
1322 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1323 } else {
1324 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1325 }
1326}
1327
Andreas Gampebc4d2182016-02-22 10:03:12 -08001328void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1329 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001330 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001331 // Argument 0 is the Unsafe instance, skip.
1332 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1333 if (obj == nullptr) {
1334 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1335 return;
1336 }
1337 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1338 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1339 QuasiAtomic::ThreadFenceRelease();
1340 if (Runtime::Current()->IsActiveTransaction()) {
1341 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1342 } else {
1343 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1344 }
1345}
1346
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001347// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1348// of correctly handling the corner cases.
1349void UnstartedRuntime::UnstartedIntegerParseInt(
1350 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001351 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001352 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1353 if (obj == nullptr) {
1354 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1355 return;
1356 }
1357
1358 std::string string_value = obj->AsString()->ToModifiedUtf8();
1359 if (string_value.empty()) {
1360 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1361 return;
1362 }
1363
1364 const char* c_str = string_value.c_str();
1365 char *end;
1366 // Can we set errno to 0? Is this always a variable, and not a macro?
1367 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1368 int64_t l = strtol(c_str, &end, 10);
1369
1370 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1371 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1372 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1373 return;
1374 }
1375 if (l == 0) {
1376 // Check whether the string wasn't exactly zero.
1377 if (string_value != "0") {
1378 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1379 return;
1380 }
1381 } else if (*end != '\0') {
1382 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1383 return;
1384 }
1385
1386 result->SetI(static_cast<int32_t>(l));
1387}
1388
1389// A cutout for Long.parseLong.
1390//
1391// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1392// well.
1393void UnstartedRuntime::UnstartedLongParseLong(
1394 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001395 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001396 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1397 if (obj == nullptr) {
1398 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1399 return;
1400 }
1401
1402 std::string string_value = obj->AsString()->ToModifiedUtf8();
1403 if (string_value.empty()) {
1404 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1405 return;
1406 }
1407
1408 const char* c_str = string_value.c_str();
1409 char *end;
1410 // Can we set errno to 0? Is this always a variable, and not a macro?
1411 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1412 int64_t l = strtol(c_str, &end, 10);
1413
1414 // Note: comparing against int32_t min/max is intentional here.
1415 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1416 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1417 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1418 return;
1419 }
1420 if (l == 0) {
1421 // Check whether the string wasn't exactly zero.
1422 if (string_value != "0") {
1423 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1424 return;
1425 }
1426 } else if (*end != '\0') {
1427 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1428 return;
1429 }
1430
1431 result->SetJ(l);
1432}
1433
Andreas Gampe715fdc22016-04-18 17:07:30 -07001434void UnstartedRuntime::UnstartedMethodInvoke(
1435 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001436 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001437 JNIEnvExt* env = self->GetJniEnv();
1438 ScopedObjectAccessUnchecked soa(self);
1439
Mathieu Chartier8778c522016-10-04 19:06:30 -07001440 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001441 ScopedLocalRef<jobject> java_method(env,
1442 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1443
Mathieu Chartier8778c522016-10-04 19:06:30 -07001444 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001445 ScopedLocalRef<jobject> java_receiver(env,
1446 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1447
Mathieu Chartier8778c522016-10-04 19:06:30 -07001448 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001449 ScopedLocalRef<jobject> java_args(env,
1450 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1451
1452 ScopedLocalRef<jobject> result_jobj(env,
1453 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1454
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001455 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001456
1457 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1458 // InvocationTargetExceptions.
1459 if (self->IsExceptionPending()) {
1460 AbortTransactionOrFail(self, "Failed Method.invoke");
1461 }
1462}
1463
Andreas Gampebc4d2182016-02-22 10:03:12 -08001464
Mathieu Chartiere401d142015-04-22 13:56:20 -07001465void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1466 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1467 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001468 int32_t length = args[1];
1469 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001470 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001471 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001472 ObjPtr<mirror::Class> array_class =
1473 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001474 DCHECK(array_class != nullptr);
1475 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001476 result->SetL(mirror::Array::Alloc<true, true>(self,
1477 array_class,
1478 length,
1479 array_class->GetComponentSizeShift(),
1480 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001481}
1482
Mathieu Chartiere401d142015-04-22 13:56:20 -07001483void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1484 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1485 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001486 result->SetL(nullptr);
1487}
1488
Mathieu Chartiere401d142015-04-22 13:56:20 -07001489void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1490 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1491 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001492 NthCallerVisitor visitor(self, 3);
1493 visitor.WalkStack();
1494 if (visitor.caller != nullptr) {
1495 result->SetL(visitor.caller->GetDeclaringClass());
1496 }
1497}
1498
Mathieu Chartiere401d142015-04-22 13:56:20 -07001499void UnstartedRuntime::UnstartedJNIMathLog(
1500 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1501 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001502 JValue value;
1503 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1504 result->SetD(log(value.GetD()));
1505}
1506
Mathieu Chartiere401d142015-04-22 13:56:20 -07001507void UnstartedRuntime::UnstartedJNIMathExp(
1508 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1509 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001510 JValue value;
1511 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1512 result->SetD(exp(value.GetD()));
1513}
1514
Andreas Gampebc4d2182016-02-22 10:03:12 -08001515void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1516 Thread* self ATTRIBUTE_UNUSED,
1517 ArtMethod* method ATTRIBUTE_UNUSED,
1518 mirror::Object* receiver ATTRIBUTE_UNUSED,
1519 uint32_t* args ATTRIBUTE_UNUSED,
1520 JValue* result) {
1521 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1522 ? 0
1523 : 1);
1524}
1525
Mathieu Chartiere401d142015-04-22 13:56:20 -07001526void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1527 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1528 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001529 StackHandleScope<1> hs(self);
1530 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1531}
1532
Andreas Gampebc4d2182016-02-22 10:03:12 -08001533void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1534 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1535 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1536 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1537 result->SetD(bit_cast<double>(long_input));
1538}
1539
Mathieu Chartiere401d142015-04-22 13:56:20 -07001540void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1541 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1542 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001543 result->SetI(args[0]);
1544}
1545
Mathieu Chartiere401d142015-04-22 13:56:20 -07001546void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1547 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1548 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001549 result->SetI(args[0]);
1550}
1551
Mathieu Chartiere401d142015-04-22 13:56:20 -07001552void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1553 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1554 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001555 result->SetL(receiver->Clone(self));
1556}
1557
Mathieu Chartiere401d142015-04-22 13:56:20 -07001558void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1559 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1560 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001561 receiver->NotifyAll(self);
1562}
1563
Mathieu Chartiere401d142015-04-22 13:56:20 -07001564void UnstartedRuntime::UnstartedJNIStringCompareTo(
1565 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1566 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001567 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1568 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001569 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001570 }
1571 result->SetI(receiver->AsString()->CompareTo(rhs));
1572}
1573
Mathieu Chartiere401d142015-04-22 13:56:20 -07001574void UnstartedRuntime::UnstartedJNIStringIntern(
1575 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1576 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001577 result->SetL(receiver->AsString()->Intern());
1578}
1579
Mathieu Chartiere401d142015-04-22 13:56:20 -07001580void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1581 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1582 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001583 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1584}
1585
Mathieu Chartiere401d142015-04-22 13:56:20 -07001586void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1587 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1588 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001589 StackHandleScope<2> hs(self);
1590 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1591 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1592 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1593}
1594
Mathieu Chartiere401d142015-04-22 13:56:20 -07001595void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1596 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1597 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001598 int32_t length = static_cast<int32_t>(args[1]);
1599 if (length < 0) {
1600 ThrowNegativeArraySizeException(length);
1601 return;
1602 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001603 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001604 Runtime* runtime = Runtime::Current();
1605 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001606 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001607 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001608 CHECK(self->IsExceptionPending());
1609 return;
1610 }
1611 DCHECK(array_class->IsObjectArrayClass());
1612 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1613 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1614 result->SetL(new_array);
1615}
1616
Mathieu Chartiere401d142015-04-22 13:56:20 -07001617void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1618 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1619 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001620 ScopedObjectAccessUnchecked soa(self);
1621 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001622 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001623 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001624 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001625 }
1626}
1627
Mathieu Chartiere401d142015-04-22 13:56:20 -07001628void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1629 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1630 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001631 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1632 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1633}
1634
Mathieu Chartiere401d142015-04-22 13:56:20 -07001635void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1636 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1637 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001638 result->SetZ(JNI_TRUE);
1639}
1640
Mathieu Chartiere401d142015-04-22 13:56:20 -07001641void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1642 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1643 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001644 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1645 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1646 jint expectedValue = args[3];
1647 jint newValue = args[4];
1648 bool success;
1649 if (Runtime::Current()->IsActiveTransaction()) {
1650 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1651 expectedValue, newValue);
1652 } else {
1653 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1654 expectedValue, newValue);
1655 }
1656 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1657}
1658
Narayan Kamath34a316f2016-03-30 13:11:18 +01001659void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1660 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1661 uint32_t* args, JValue* result) {
1662 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1663 if (obj == nullptr) {
1664 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1665 return;
1666 }
1667
1668 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1669 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1670}
1671
Mathieu Chartiere401d142015-04-22 13:56:20 -07001672void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1673 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1674 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001675 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1676 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1677 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1678 if (Runtime::Current()->IsActiveTransaction()) {
1679 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1680 } else {
1681 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1682 }
1683}
1684
Andreas Gampe799681b2015-05-15 19:24:12 -07001685void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001686 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1687 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001688 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1689 Primitive::Type primitive_type = component->GetPrimitiveType();
1690 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1691}
1692
Andreas Gampe799681b2015-05-15 19:24:12 -07001693void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001694 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1695 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001696 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1697 Primitive::Type primitive_type = component->GetPrimitiveType();
1698 result->SetI(Primitive::ComponentSize(primitive_type));
1699}
1700
Andreas Gampedd9d0552015-03-09 12:57:41 -07001701typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001702 size_t arg_size);
1703
Mathieu Chartiere401d142015-04-22 13:56:20 -07001704typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001705 uint32_t* args, JValue* result);
1706
1707static bool tables_initialized_ = false;
1708static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1709static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1710
Andreas Gampe799681b2015-05-15 19:24:12 -07001711void UnstartedRuntime::InitializeInvokeHandlers() {
1712#define UNSTARTED_DIRECT(ShortName, Sig) \
1713 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1714#include "unstarted_runtime_list.h"
1715 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1716#undef UNSTARTED_RUNTIME_DIRECT_LIST
1717#undef UNSTARTED_RUNTIME_JNI_LIST
1718#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001719}
1720
Andreas Gampe799681b2015-05-15 19:24:12 -07001721void UnstartedRuntime::InitializeJNIHandlers() {
1722#define UNSTARTED_JNI(ShortName, Sig) \
1723 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1724#include "unstarted_runtime_list.h"
1725 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1726#undef UNSTARTED_RUNTIME_DIRECT_LIST
1727#undef UNSTARTED_RUNTIME_JNI_LIST
1728#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001729}
1730
Andreas Gampe799681b2015-05-15 19:24:12 -07001731void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001732 CHECK(!tables_initialized_);
1733
Andreas Gampe799681b2015-05-15 19:24:12 -07001734 InitializeInvokeHandlers();
1735 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001736
1737 tables_initialized_ = true;
1738}
1739
Andreas Gampe799681b2015-05-15 19:24:12 -07001740void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1741 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001742 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1743 // problems in core libraries.
1744 CHECK(tables_initialized_);
1745
David Sehr709b0702016-10-13 09:12:37 -07001746 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001747 const auto& iter = invoke_handlers_.find(name);
1748 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001749 // Clear out the result in case it's not zeroed out.
1750 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001751
1752 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1753 self->PushShadowFrame(shadow_frame);
1754
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001755 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001756
1757 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001758 } else {
1759 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001760 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001761 }
1762}
1763
1764// 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 -07001765void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001766 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001767 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001768 const auto& iter = jni_handlers_.find(name);
1769 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001770 // Clear out the result in case it's not zeroed out.
1771 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001772 (*iter->second)(self, method, receiver, args, result);
1773 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001774 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1775 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001776 } else {
David Sehr709b0702016-10-13 09:12:37 -07001777 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001778 "non-transactional runtime";
1779 }
1780}
1781
1782} // namespace interpreter
1783} // namespace art