blob: d505aea10aa0a9bf6bd44e8944297c63c38fc13d [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"
41#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070042#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070043#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010044#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045#include "mirror/object-inl.h"
46#include "mirror/object_array-inl.h"
47#include "mirror/string-inl.h"
48#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070049#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070050#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020051#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070052#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070053#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070054
55namespace art {
56namespace interpreter {
57
Andreas Gampe068b0c02015-03-11 12:44:47 -070058static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020059 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070060 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020061
62static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070063 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070064 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020065 va_start(args, fmt);
66 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070067 va_end(args);
68 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020069 va_start(args, fmt);
70 std::string msg;
71 StringAppendV(&msg, fmt, args);
72 va_end(args);
73 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070074 UNREACHABLE();
75 }
76}
77
Andreas Gampe8ce9c302016-04-15 21:24:28 -070078// Restricted support for character upper case / lower case. Only support ASCII, where
79// it's easy. Abort the transaction otherwise.
80static void CharacterLowerUpper(Thread* self,
81 ShadowFrame* shadow_frame,
82 JValue* result,
83 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070084 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070085 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
86
87 // Only ASCII (7-bit).
88 if (!isascii(int_value)) {
89 AbortTransactionOrFail(self,
90 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
91 int_value);
92 return;
93 }
94
95 std::locale c_locale("C");
96 char char_value = static_cast<char>(int_value);
97
98 if (to_lower_case) {
99 result->SetI(std::tolower(char_value, c_locale));
100 } else {
101 result->SetI(std::toupper(char_value, c_locale));
102 }
103}
104
105void UnstartedRuntime::UnstartedCharacterToLowerCase(
106 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
107 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
108}
109
110void UnstartedRuntime::UnstartedCharacterToUpperCase(
111 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
112 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
113}
114
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700115// Helper function to deal with class loading in an unstarted runtime.
116static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
117 Handle<mirror::ClassLoader> class_loader, JValue* result,
118 const std::string& method_name, bool initialize_class,
119 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700120 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700121 CHECK(className.Get() != nullptr);
122 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
123 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
124
125 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
126 if (found == nullptr && abort_if_not_found) {
127 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700128 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
129 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700130 }
131 return;
132 }
133 if (found != nullptr && initialize_class) {
134 StackHandleScope<1> hs(self);
135 Handle<mirror::Class> h_class(hs.NewHandle(found));
136 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
137 CHECK(self->IsExceptionPending());
138 return;
139 }
140 }
141 result->SetL(found);
142}
143
144// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
145// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
146// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200147// actually the transaction abort exception. This must not be wrapped, as it signals an
148// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700150 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700151 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200152 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700153 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200154 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700155 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
156 "ClassNotFoundException");
157 }
158 }
159}
160
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700161static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700162 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700163 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
164 if (param == nullptr) {
165 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
166 return nullptr;
167 }
168 return param->AsString();
169}
170
Andreas Gampe799681b2015-05-15 19:24:12 -0700171void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700172 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700173 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
174 if (class_name == nullptr) {
175 return;
176 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700177 StackHandleScope<1> hs(self);
178 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800179 UnstartedRuntimeFindClass(self,
180 h_class_name,
181 ScopedNullHandle<mirror::ClassLoader>(),
182 result,
183 "Class.forName",
184 true,
185 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700186 CheckExceptionGenerateClassNotFound(self);
187}
188
Andreas Gampe799681b2015-05-15 19:24:12 -0700189void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700190 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700191 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
192 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700193 return;
194 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700195 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
196 mirror::ClassLoader* class_loader =
197 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
198 StackHandleScope<2> hs(self);
199 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
200 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
201 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
202 initialize_class, false);
203 CheckExceptionGenerateClassNotFound(self);
204}
205
Andreas Gampe799681b2015-05-15 19:24:12 -0700206void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700207 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700208 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
209 if (class_name == nullptr) {
210 return;
211 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700212 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
213 mirror::ClassLoader* class_loader =
214 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
215 StackHandleScope<2> hs(self);
216 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
217 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
218 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
219 initialize_class, false);
220 CheckExceptionGenerateClassNotFound(self);
221}
222
Andreas Gampe799681b2015-05-15 19:24:12 -0700223void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700224 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
225 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700226 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
227 if (param == nullptr) {
228 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
229 return;
230 }
231 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700232 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700233
234 // Check that it's not null.
235 if (h_klass.Get() == nullptr) {
236 AbortTransactionOrFail(self, "Class reference is null for newInstance");
237 return;
238 }
239
240 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
241 if (Runtime::Current()->IsActiveTransaction()) {
242 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200243 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
244 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700245 return;
246 }
247 }
248
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700249 // There are two situations in which we'll abort this run.
250 // 1) If the class isn't yet initialized and initialization fails.
251 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
252 // Note that 2) could likely be handled here, but for safety abort the transaction.
253 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700254 auto* cl = Runtime::Current()->GetClassLinker();
255 if (cl->EnsureInitialized(self, h_klass, true, true)) {
256 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
257 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700258 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
259 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700261 if (!self->IsExceptionPending()) {
262 result->SetL(h_obj.Get());
263 ok = true;
264 }
265 } else {
266 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
267 "Could not find default constructor for '%s'",
268 PrettyClass(h_klass.Get()).c_str());
269 }
270 }
271 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700272 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
273 PrettyClass(h_klass.Get()).c_str(),
274 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700275 }
276}
277
Andreas Gampe799681b2015-05-15 19:24:12 -0700278void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700279 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700280 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
281 // going the reflective Dex way.
282 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
283 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700284 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700285 for (ArtField& field : klass->GetIFields()) {
286 if (name2->Equals(field.GetName())) {
287 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700288 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700289 }
290 }
291 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700292 for (ArtField& field : klass->GetSFields()) {
293 if (name2->Equals(field.GetName())) {
294 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700295 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700296 }
297 }
298 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700299 if (found == nullptr) {
300 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
301 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
302 PrettyDescriptor(klass).c_str());
303 return;
304 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700305 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700306 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700307 mirror::Field* field;
308 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700309 if (pointer_size == PointerSize::k64) {
310 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
311 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700312 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700313 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
314 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700315 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700316 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700317 if (pointer_size == PointerSize::k64) {
318 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
319 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700320 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700321 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
322 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700323 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700324 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700325 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700326}
327
Andreas Gampebc4d2182016-02-22 10:03:12 -0800328// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
329void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
330 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
331 // Special managed code cut-out to allow method lookup in a un-started runtime.
332 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
333 if (klass == nullptr) {
334 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
335 return;
336 }
337 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
338 mirror::ObjectArray<mirror::Class>* args =
339 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700340 Runtime* runtime = Runtime::Current();
341 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700342 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700343 mirror::Method* method;
344 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700345 if (pointer_size == PointerSize::k64) {
346 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
347 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700348 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700349 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
350 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700351 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800352 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700353 if (pointer_size == PointerSize::k64) {
354 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
355 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700356 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700357 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
358 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700359 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800360 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700361 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800362}
363
Andreas Gampe6039e562016-04-05 18:18:43 -0700364// Special managed code cut-out to allow constructor lookup in a un-started runtime.
365void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
366 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
367 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
368 if (klass == nullptr) {
369 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
370 return;
371 }
372 mirror::ObjectArray<mirror::Class>* args =
373 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700374 Runtime* runtime = Runtime::Current();
375 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700376 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700377 mirror::Constructor* constructor;
378 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700379 if (pointer_size == PointerSize::k64) {
380 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
381 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700382 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700383 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
384 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700385 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700386 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 if (pointer_size == PointerSize::k64) {
388 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
389 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700390 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700391 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
392 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700393 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700394 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700395 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700396}
397
Andreas Gampe633750c2016-02-19 10:49:50 -0800398void UnstartedRuntime::UnstartedClassGetEnclosingClass(
399 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
400 StackHandleScope<1> hs(self);
401 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
402 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
403 result->SetL(nullptr);
404 }
David Sehr9323e6e2016-09-13 08:58:35 -0700405 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800406}
407
Andreas Gampe715fdc22016-04-18 17:07:30 -0700408void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
409 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
410 StackHandleScope<1> hs(self);
411 Handle<mirror::Class> klass(hs.NewHandle(
412 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
413 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
414 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
415}
416
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700417static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
418 const char* entry_name,
419 size_t* size,
420 std::string* error_msg) {
421 CHECK(size != nullptr);
422
423 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
424 if (zip_archive == nullptr) {
425 return nullptr;;
426 }
427 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
428 if (zip_entry == nullptr) {
429 return nullptr;
430 }
431 std::unique_ptr<MemMap> tmp_map(
432 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
433 if (tmp_map == nullptr) {
434 return nullptr;
435 }
436
437 // OK, from here everything seems fine.
438 *size = zip_entry->GetUncompressedLength();
439 return tmp_map;
440}
441
442static void GetResourceAsStream(Thread* self,
443 ShadowFrame* shadow_frame,
444 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700445 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700446 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
447 if (resource_obj == nullptr) {
448 AbortTransactionOrFail(self, "null name for getResourceAsStream");
449 return;
450 }
451 CHECK(resource_obj->IsString());
452 mirror::String* resource_name = resource_obj->AsString();
453
454 std::string resource_name_str = resource_name->ToModifiedUtf8();
455 if (resource_name_str.empty() || resource_name_str == "/") {
456 AbortTransactionOrFail(self,
457 "Unsupported name %s for getResourceAsStream",
458 resource_name_str.c_str());
459 return;
460 }
461 const char* resource_cstr = resource_name_str.c_str();
462 if (resource_cstr[0] == '/') {
463 resource_cstr++;
464 }
465
466 Runtime* runtime = Runtime::Current();
467
468 std::vector<std::string> split;
469 Split(runtime->GetBootClassPathString(), ':', &split);
470 if (split.empty()) {
471 AbortTransactionOrFail(self,
472 "Boot classpath not set or split error:: %s",
473 runtime->GetBootClassPathString().c_str());
474 return;
475 }
476
477 std::unique_ptr<MemMap> mem_map;
478 size_t map_size;
479 std::string last_error_msg; // Only store the last message (we could concatenate).
480
481 for (const std::string& jar_file : split) {
482 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
483 if (mem_map != nullptr) {
484 break;
485 }
486 }
487
488 if (mem_map == nullptr) {
489 // Didn't find it. There's a good chance this will be the same at runtime, but still
490 // conservatively abort the transaction here.
491 AbortTransactionOrFail(self,
492 "Could not find resource %s. Last error was %s.",
493 resource_name_str.c_str(),
494 last_error_msg.c_str());
495 return;
496 }
497
498 StackHandleScope<3> hs(self);
499
500 // Create byte array for content.
501 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
502 if (h_array.Get() == nullptr) {
503 AbortTransactionOrFail(self, "Could not find/create byte array class");
504 return;
505 }
506 // Copy in content.
507 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
508 // Be proactive releasing memory.
509 mem_map.release();
510
511 // Create a ByteArrayInputStream.
512 Handle<mirror::Class> h_class(hs.NewHandle(
513 runtime->GetClassLinker()->FindClass(self,
514 "Ljava/io/ByteArrayInputStream;",
515 ScopedNullHandle<mirror::ClassLoader>())));
516 if (h_class.Get() == nullptr) {
517 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
518 return;
519 }
520 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
521 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
522 return;
523 }
524
525 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
526 if (h_obj.Get() == nullptr) {
527 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
528 return;
529 }
530
531 auto* cl = Runtime::Current()->GetClassLinker();
532 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
533 "<init>", "([B)V", cl->GetImagePointerSize());
534 if (constructor == nullptr) {
535 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
536 return;
537 }
538
539 uint32_t args[1];
540 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
541 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
542
543 if (self->IsExceptionPending()) {
544 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
545 return;
546 }
547
548 result->SetL(h_obj.Get());
549}
550
551void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
552 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
553 {
554 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
555 CHECK(this_obj != nullptr);
556 CHECK(this_obj->IsClassLoader());
557
558 StackHandleScope<1> hs(self);
559 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
560
561 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
562 this_classloader_class.Get()) {
563 AbortTransactionOrFail(self,
564 "Unsupported classloader type %s for getResourceAsStream",
565 PrettyClass(this_classloader_class.Get()).c_str());
566 return;
567 }
568 }
569
570 GetResourceAsStream(self, shadow_frame, result, arg_offset);
571}
572
Andreas Gampe799681b2015-05-15 19:24:12 -0700573void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700574 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700575 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
576 mirror::ClassLoader* class_loader =
577 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
578 StackHandleScope<2> hs(self);
579 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
580 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
581 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
582 "VMClassLoader.findLoadedClass", false, false);
583 // This might have an error pending. But semantics are to just return null.
584 if (self->IsExceptionPending()) {
585 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
586 std::string type(PrettyTypeOf(self->GetException()));
587 if (type != "java.lang.InternalError") {
588 self->ClearException();
589 }
590 }
591}
592
Mathieu Chartiere401d142015-04-22 13:56:20 -0700593void UnstartedRuntime::UnstartedVoidLookupType(
594 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
595 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700596 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
597}
598
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700599// Arraycopy emulation.
600// Note: we can't use any fast copy functions, as they are not available under transaction.
601
602template <typename T>
603static void PrimitiveArrayCopy(Thread* self,
604 mirror::Array* src_array, int32_t src_pos,
605 mirror::Array* dst_array, int32_t dst_pos,
606 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700607 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700608 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
609 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
610 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
611 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
612 return;
613 }
614 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
615 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
616 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
617 if (copy_forward) {
618 for (int32_t i = 0; i < length; ++i) {
619 dst->Set(dst_pos + i, src->Get(src_pos + i));
620 }
621 } else {
622 for (int32_t i = 1; i <= length; ++i) {
623 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
624 }
625 }
626}
627
Andreas Gampe799681b2015-05-15 19:24:12 -0700628void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700629 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700630 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700631 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
632 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700633 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700634
Andreas Gampe85a098a2016-03-31 13:30:53 -0700635 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
636 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
637 // Null checking. For simplicity, abort transaction.
638 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700639 AbortTransactionOrFail(self, "src is null in arraycopy.");
640 return;
641 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700642 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700643 AbortTransactionOrFail(self, "dst is null in arraycopy.");
644 return;
645 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700646 // Test for arrayness. Throw ArrayStoreException.
647 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
648 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
649 return;
650 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700651
Andreas Gampe85a098a2016-03-31 13:30:53 -0700652 mirror::Array* src_array = src_obj->AsArray();
653 mirror::Array* dst_array = dst_obj->AsArray();
654
655 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700656 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
657 UNLIKELY(src_pos > src_array->GetLength() - length) ||
658 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700659 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700660 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
661 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
662 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700663 return;
664 }
665
666 // Type checking.
667 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
668 GetComponentType();
669
670 if (!src_type->IsPrimitive()) {
671 // Check that the second type is not primitive.
672 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
673 GetComponentType();
674 if (trg_type->IsPrimitiveInt()) {
675 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
676 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
677 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
678 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700679 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700680
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700681 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
682 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
683 if (src == dst) {
684 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700685 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700686 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
687 if (copy_forward) {
688 for (int32_t i = 0; i < length; ++i) {
689 dst->Set(dst_pos + i, src->Get(src_pos + i));
690 }
691 } else {
692 for (int32_t i = 1; i <= length; ++i) {
693 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
694 }
695 }
696 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700697 // We're being lazy here. Optimally this could be a memcpy (if component types are
698 // assignable), but the ObjectArray implementation doesn't support transactions. The
699 // checking version, however, does.
700 if (Runtime::Current()->IsActiveTransaction()) {
701 dst->AssignableCheckingMemcpy<true>(
702 dst_pos, src, src_pos, length, true /* throw_exception */);
703 } else {
704 dst->AssignableCheckingMemcpy<false>(
705 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700706 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700707 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700708 } else if (src_type->IsPrimitiveByte()) {
709 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700710 } else if (src_type->IsPrimitiveChar()) {
711 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
712 } else if (src_type->IsPrimitiveInt()) {
713 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700714 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700715 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700716 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700717 }
718}
719
Andreas Gampe5c9af612016-04-05 14:16:10 -0700720void UnstartedRuntime::UnstartedSystemArraycopyByte(
721 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
722 // Just forward.
723 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
724}
725
Andreas Gampe799681b2015-05-15 19:24:12 -0700726void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700727 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700728 // Just forward.
729 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
730}
731
732void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700733 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700734 // Just forward.
735 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
736}
737
Narayan Kamath34a316f2016-03-30 13:11:18 +0100738void UnstartedRuntime::UnstartedSystemGetSecurityManager(
739 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
740 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
741 result->SetL(nullptr);
742}
743
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700744static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
745
746static void GetSystemProperty(Thread* self,
747 ShadowFrame* shadow_frame,
748 JValue* result,
749 size_t arg_offset,
750 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700751 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700752 StackHandleScope<4> hs(self);
753 Handle<mirror::String> h_key(
754 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
755 if (h_key.Get() == nullptr) {
756 AbortTransactionOrFail(self, "getProperty key was null");
757 return;
758 }
759
760 // This is overall inefficient, but reflecting the values here is not great, either. So
761 // for simplicity, and with the assumption that the number of getProperty calls is not
762 // too great, just iterate each time.
763
764 // Get the storage class.
765 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
766 Handle<mirror::Class> h_props_class(hs.NewHandle(
767 class_linker->FindClass(self,
768 "Ljava/lang/AndroidHardcodedSystemProperties;",
769 ScopedNullHandle<mirror::ClassLoader>())));
770 if (h_props_class.Get() == nullptr) {
771 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
772 return;
773 }
774 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
775 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
776 return;
777 }
778
779 // Get the storage array.
780 ArtField* static_properties =
781 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
782 "[[Ljava/lang/String;");
783 if (static_properties == nullptr) {
784 AbortTransactionOrFail(self,
785 "Could not find %s field",
786 kAndroidHardcodedSystemPropertiesFieldName);
787 return;
788 }
789 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(
790 hs.NewHandle(reinterpret_cast<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>*>(
791 static_properties->GetObject(h_props_class.Get()))));
792 if (h_2string_array.Get() == nullptr) {
793 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
794 return;
795 }
796
797 // Iterate over it.
798 const int32_t prop_count = h_2string_array->GetLength();
799 // Use the third handle as mutable.
800 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
801 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
802 for (int32_t i = 0; i < prop_count; ++i) {
803 h_string_array.Assign(h_2string_array->Get(i));
804 if (h_string_array.Get() == nullptr ||
805 h_string_array->GetLength() != 2 ||
806 h_string_array->Get(0) == nullptr) {
807 AbortTransactionOrFail(self,
808 "Unexpected content of %s",
809 kAndroidHardcodedSystemPropertiesFieldName);
810 return;
811 }
812 if (h_key->Equals(h_string_array->Get(0))) {
813 // Found a value.
814 if (h_string_array->Get(1) == nullptr && is_default_version) {
815 // Null is being delegated to the default map, and then resolved to the given default value.
816 // As there's no default map, return the given value.
817 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
818 } else {
819 result->SetL(h_string_array->Get(1));
820 }
821 return;
822 }
823 }
824
825 // Key is not supported.
826 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
827}
828
829void UnstartedRuntime::UnstartedSystemGetProperty(
830 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
831 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
832}
833
834void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
835 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
836 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
837}
838
Andreas Gampe799681b2015-05-15 19:24:12 -0700839void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700840 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700841 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
842 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100843 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
844 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700845 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100846 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700847 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100848 } else if (caller ==
849 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
850 // Allocate new object.
851 StackHandleScope<2> hs(self);
852 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
853 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
854 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
855 h_real_to_string_class->AllocObject(self)));
856 if (h_real_to_string_obj.Get() != nullptr) {
857 auto* cl = Runtime::Current()->GetClassLinker();
858 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
859 "<init>", "()V", cl->GetImagePointerSize());
860 if (init_method == nullptr) {
861 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
862 } else {
863 JValue invoke_result;
864 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
865 nullptr);
866 if (!self->IsExceptionPending()) {
867 result->SetL(h_real_to_string_obj.Get());
868 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700869 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700870 }
871 }
872 }
873
874 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700875 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700876 }
877}
878
Sergio Giro83261202016-04-11 20:49:20 +0100879void UnstartedRuntime::UnstartedMathCeil(
880 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700881 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100882}
883
884void UnstartedRuntime::UnstartedMathFloor(
885 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700886 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700887}
888
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700889void UnstartedRuntime::UnstartedMathSin(
890 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
891 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
892}
893
894void UnstartedRuntime::UnstartedMathCos(
895 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
896 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
897}
898
899void UnstartedRuntime::UnstartedMathPow(
900 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
901 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
902 shadow_frame->GetVRegDouble(arg_offset + 2)));
903}
904
Andreas Gampe799681b2015-05-15 19:24:12 -0700905void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700907 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
908 result->SetI(obj->IdentityHashCode());
909}
910
Andreas Gampe799681b2015-05-15 19:24:12 -0700911void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700912 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700913 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000914 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700915}
916
Andreas Gampedd9d0552015-03-09 12:57:41 -0700917static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700918 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700919 const DexFile* dex_file = dex_cache->GetDexFile();
920 if (dex_file == nullptr) {
921 return nullptr;
922 }
923
924 // Create the direct byte buffer.
925 JNIEnv* env = self->GetJniEnv();
926 DCHECK(env != nullptr);
927 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700928 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
929 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700930 DCHECK(self->IsExceptionPending());
931 return nullptr;
932 }
933
934 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700935 args[0].l = byte_buffer.get();
936
937 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
938 WellKnownClasses::com_android_dex_Dex,
939 WellKnownClasses::com_android_dex_Dex_create,
940 args));
941
942 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700943}
944
Andreas Gampe799681b2015-05-15 19:24:12 -0700945void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700946 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700947 // We will create the Dex object, but the image writer will release it before creating the
948 // art file.
949 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
950 bool have_dex = false;
951 if (src != nullptr) {
952 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
953 if (dex != nullptr) {
954 have_dex = true;
955 result->SetL(dex);
956 }
957 }
958 if (!have_dex) {
959 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200960 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700961 }
962}
963
964static void UnstartedMemoryPeek(
965 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
966 int64_t address = shadow_frame->GetVRegLong(arg_offset);
967 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
968 // aborting the transaction.
969
970 switch (type) {
971 case Primitive::kPrimByte: {
972 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
973 return;
974 }
975
976 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700977 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
978 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700979 return;
980 }
981
982 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700983 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
984 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700985 return;
986 }
987
988 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700989 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
990 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700991 return;
992 }
993
994 case Primitive::kPrimBoolean:
995 case Primitive::kPrimChar:
996 case Primitive::kPrimFloat:
997 case Primitive::kPrimDouble:
998 case Primitive::kPrimVoid:
999 case Primitive::kPrimNot:
1000 LOG(FATAL) << "Not in the Memory API: " << type;
1001 UNREACHABLE();
1002 }
1003 LOG(FATAL) << "Should not reach here";
1004 UNREACHABLE();
1005}
1006
Andreas Gampe799681b2015-05-15 19:24:12 -07001007void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001008 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001009 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1010}
1011
1012void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001013 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001014 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1015}
1016
1017void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001018 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001019 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1020}
1021
1022void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001023 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001024 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001025}
1026
1027static void UnstartedMemoryPeekArray(
1028 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001029 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001030 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1031 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1032 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001033 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001034 return;
1035 }
1036 mirror::Array* array = obj->AsArray();
1037
1038 int offset = shadow_frame->GetVReg(arg_offset + 3);
1039 int count = shadow_frame->GetVReg(arg_offset + 4);
1040 if (offset < 0 || offset + count > array->GetLength()) {
1041 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1042 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001043 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001044 return;
1045 }
1046
1047 switch (type) {
1048 case Primitive::kPrimByte: {
1049 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1050 mirror::ByteArray* byte_array = array->AsByteArray();
1051 for (int32_t i = 0; i < count; ++i, ++address) {
1052 byte_array->SetWithoutChecks<true>(i + offset, *address);
1053 }
1054 return;
1055 }
1056
1057 case Primitive::kPrimShort:
1058 case Primitive::kPrimInt:
1059 case Primitive::kPrimLong:
1060 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1061 UNREACHABLE();
1062
1063 case Primitive::kPrimBoolean:
1064 case Primitive::kPrimChar:
1065 case Primitive::kPrimFloat:
1066 case Primitive::kPrimDouble:
1067 case Primitive::kPrimVoid:
1068 case Primitive::kPrimNot:
1069 LOG(FATAL) << "Not in the Memory API: " << type;
1070 UNREACHABLE();
1071 }
1072 LOG(FATAL) << "Should not reach here";
1073 UNREACHABLE();
1074}
1075
Andreas Gampe799681b2015-05-15 19:24:12 -07001076void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001077 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001078 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001079}
1080
Kenny Root1c9e61c2015-05-14 15:58:17 -07001081// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001082void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001083 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001084 jint start = shadow_frame->GetVReg(arg_offset + 1);
1085 jint end = shadow_frame->GetVReg(arg_offset + 2);
1086 jint index = shadow_frame->GetVReg(arg_offset + 4);
1087 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1088 if (string == nullptr) {
1089 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1090 return;
1091 }
Kenny Root57f91e82015-05-14 15:58:17 -07001092 DCHECK_GE(start, 0);
1093 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001094 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001095 Handle<mirror::CharArray> h_char_array(
1096 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001097 DCHECK_LE(index, h_char_array->GetLength());
1098 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001099 string->GetChars(start, end, h_char_array, index);
1100}
1101
1102// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001103void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001104 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001105 jint index = shadow_frame->GetVReg(arg_offset + 1);
1106 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1107 if (string == nullptr) {
1108 AbortTransactionOrFail(self, "String.charAt with null object");
1109 return;
1110 }
1111 result->SetC(string->CharAt(index));
1112}
1113
Kenny Root57f91e82015-05-14 15:58:17 -07001114// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001115void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001116 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001117 jint index = shadow_frame->GetVReg(arg_offset + 1);
1118 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1119 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1120 if (string == nullptr) {
1121 AbortTransactionOrFail(self, "String.setCharAt with null object");
1122 return;
1123 }
1124 string->SetCharAt(index, c);
1125}
1126
Kenny Root1c9e61c2015-05-14 15:58:17 -07001127// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001128void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001129 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001130 jint offset = shadow_frame->GetVReg(arg_offset);
1131 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1132 DCHECK_GE(char_count, 0);
1133 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001134 Handle<mirror::CharArray> h_char_array(
1135 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001136 Runtime* runtime = Runtime::Current();
1137 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1138 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1139}
1140
1141// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001142void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001143 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001144 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1145 if (to_copy == nullptr) {
1146 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1147 return;
1148 }
1149 StackHandleScope<1> hs(self);
1150 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1151 Runtime* runtime = Runtime::Current();
1152 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1153 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1154 allocator));
1155}
1156
Andreas Gampe799681b2015-05-15 19:24:12 -07001157void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001158 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001159 jint start = shadow_frame->GetVReg(arg_offset + 1);
1160 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001161 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001162 DCHECK_GE(length, 0);
1163 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001164 Handle<mirror::String> h_string(
1165 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001166 DCHECK_LE(start, h_string->GetLength());
1167 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001168 Runtime* runtime = Runtime::Current();
1169 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1170 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1171}
1172
Kenny Root57f91e82015-05-14 15:58:17 -07001173// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001174void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001175 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001176 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001177 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1178 if (string == nullptr) {
1179 AbortTransactionOrFail(self, "String.charAt with null object");
1180 return;
1181 }
1182 result->SetL(string->ToCharArray(self));
1183}
1184
Andreas Gampebc4d2182016-02-22 10:03:12 -08001185// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1186void UnstartedRuntime::UnstartedReferenceGetReferent(
1187 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1188 mirror::Reference* const ref = down_cast<mirror::Reference*>(
1189 shadow_frame->GetVRegReference(arg_offset));
1190 if (ref == nullptr) {
1191 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1192 return;
1193 }
1194 mirror::Object* const referent =
1195 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1196 result->SetL(referent);
1197}
1198
1199// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1200// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1201// where we can predict the behavior (somewhat).
1202// Note: this is required (instead of lazy initialization) as these classes are used in the static
1203// initialization of other classes, so will *use* the value.
1204void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1205 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1206 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
1207 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1208 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1209 // 8 as a conservative upper approximation.
1210 result->SetI(8);
1211 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1212 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1213 // a good upper bound.
1214 // TODO: Consider resetting in the zygote?
1215 result->SetI(8);
1216 } else {
1217 // Not supported.
1218 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1219 }
1220}
1221
1222// This allows accessing ConcurrentHashMap/SynchronousQueue.
1223
1224void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1225 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1226 // Argument 0 is the Unsafe instance, skip.
1227 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1228 if (obj == nullptr) {
1229 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1230 return;
1231 }
1232 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1233 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1234 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
1235
1236 // Must use non transactional mode.
1237 if (kUseReadBarrier) {
1238 // Need to make sure the reference stored in the field is a to-space one before attempting the
1239 // CAS or the CAS could fail incorrectly.
1240 mirror::HeapReference<mirror::Object>* field_addr =
1241 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1242 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1243 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1244 obj,
1245 MemberOffset(offset),
1246 field_addr);
1247 }
1248 bool success;
1249 // Check whether we're in a transaction, call accordingly.
1250 if (Runtime::Current()->IsActiveTransaction()) {
1251 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1252 expectedValue,
1253 newValue);
1254 } else {
1255 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1256 expectedValue,
1257 newValue);
1258 }
1259 result->SetZ(success ? 1 : 0);
1260}
1261
1262void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1263 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1264 // Argument 0 is the Unsafe instance, skip.
1265 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1266 if (obj == nullptr) {
1267 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1268 return;
1269 }
1270 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1271 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1272 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1273
1274 // Must use non transactional mode.
1275 if (kUseReadBarrier) {
1276 // Need to make sure the reference stored in the field is a to-space one before attempting the
1277 // CAS or the CAS could fail incorrectly.
1278 mirror::HeapReference<mirror::Object>* field_addr =
1279 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1280 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1281 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1282 obj,
1283 MemberOffset(offset),
1284 field_addr);
1285 }
1286 bool success;
1287 // Check whether we're in a transaction, call accordingly.
1288 if (Runtime::Current()->IsActiveTransaction()) {
1289 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1290 expected_value,
1291 newValue);
1292 } else {
1293 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1294 expected_value,
1295 newValue);
1296 }
1297 result->SetZ(success ? 1 : 0);
1298}
1299
1300void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1301 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001302 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001303 // Argument 0 is the Unsafe instance, skip.
1304 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1305 if (obj == nullptr) {
1306 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1307 return;
1308 }
1309 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1310 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1311 result->SetL(value);
1312}
1313
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001314void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1315 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001316 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001317 // Argument 0 is the Unsafe instance, skip.
1318 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1319 if (obj == nullptr) {
1320 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1321 return;
1322 }
1323 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1324 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1325 if (Runtime::Current()->IsActiveTransaction()) {
1326 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1327 } else {
1328 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1329 }
1330}
1331
Andreas Gampebc4d2182016-02-22 10:03:12 -08001332void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1333 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001334 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001335 // Argument 0 is the Unsafe instance, skip.
1336 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1337 if (obj == nullptr) {
1338 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1339 return;
1340 }
1341 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1342 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1343 QuasiAtomic::ThreadFenceRelease();
1344 if (Runtime::Current()->IsActiveTransaction()) {
1345 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1346 } else {
1347 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1348 }
1349}
1350
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001351// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1352// of correctly handling the corner cases.
1353void UnstartedRuntime::UnstartedIntegerParseInt(
1354 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001355 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001356 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1357 if (obj == nullptr) {
1358 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1359 return;
1360 }
1361
1362 std::string string_value = obj->AsString()->ToModifiedUtf8();
1363 if (string_value.empty()) {
1364 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1365 return;
1366 }
1367
1368 const char* c_str = string_value.c_str();
1369 char *end;
1370 // Can we set errno to 0? Is this always a variable, and not a macro?
1371 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1372 int64_t l = strtol(c_str, &end, 10);
1373
1374 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1375 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1376 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1377 return;
1378 }
1379 if (l == 0) {
1380 // Check whether the string wasn't exactly zero.
1381 if (string_value != "0") {
1382 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1383 return;
1384 }
1385 } else if (*end != '\0') {
1386 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1387 return;
1388 }
1389
1390 result->SetI(static_cast<int32_t>(l));
1391}
1392
1393// A cutout for Long.parseLong.
1394//
1395// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1396// well.
1397void UnstartedRuntime::UnstartedLongParseLong(
1398 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001399 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001400 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1401 if (obj == nullptr) {
1402 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1403 return;
1404 }
1405
1406 std::string string_value = obj->AsString()->ToModifiedUtf8();
1407 if (string_value.empty()) {
1408 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1409 return;
1410 }
1411
1412 const char* c_str = string_value.c_str();
1413 char *end;
1414 // Can we set errno to 0? Is this always a variable, and not a macro?
1415 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1416 int64_t l = strtol(c_str, &end, 10);
1417
1418 // Note: comparing against int32_t min/max is intentional here.
1419 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1420 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1421 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1422 return;
1423 }
1424 if (l == 0) {
1425 // Check whether the string wasn't exactly zero.
1426 if (string_value != "0") {
1427 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1428 return;
1429 }
1430 } else if (*end != '\0') {
1431 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1432 return;
1433 }
1434
1435 result->SetJ(l);
1436}
1437
Andreas Gampe715fdc22016-04-18 17:07:30 -07001438void UnstartedRuntime::UnstartedMethodInvoke(
1439 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001440 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001441 JNIEnvExt* env = self->GetJniEnv();
1442 ScopedObjectAccessUnchecked soa(self);
1443
1444 mirror::Object* java_method_obj = shadow_frame->GetVRegReference(arg_offset);
1445 ScopedLocalRef<jobject> java_method(env,
1446 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1447
1448 mirror::Object* java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
1449 ScopedLocalRef<jobject> java_receiver(env,
1450 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1451
1452 mirror::Object* java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
1453 ScopedLocalRef<jobject> java_args(env,
1454 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1455
1456 ScopedLocalRef<jobject> result_jobj(env,
1457 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1458
1459 result->SetL(self->DecodeJObject(result_jobj.get()));
1460
1461 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1462 // InvocationTargetExceptions.
1463 if (self->IsExceptionPending()) {
1464 AbortTransactionOrFail(self, "Failed Method.invoke");
1465 }
1466}
1467
Andreas Gampebc4d2182016-02-22 10:03:12 -08001468
Mathieu Chartiere401d142015-04-22 13:56:20 -07001469void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1470 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1471 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001472 int32_t length = args[1];
1473 DCHECK_GE(length, 0);
1474 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1475 Runtime* runtime = Runtime::Current();
1476 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1477 DCHECK(array_class != nullptr);
1478 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1479 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1480 array_class->GetComponentSizeShift(), allocator));
1481}
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 }
1603 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1604 Runtime* runtime = Runtime::Current();
1605 ClassLinker* class_linker = runtime->GetClassLinker();
1606 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()) {
1622 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1623 } else {
1624 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1625 }
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
1746 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1747 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) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001767 std::string name(PrettyMethod(method));
1768 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 {
1777 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1778 "non-transactional runtime";
1779 }
1780}
1781
1782} // namespace interpreter
1783} // namespace art