blob: 4347c37f623292d61f97e0e14ca39a1fa5d1ac46 [file] [log] [blame]
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "unstarted_runtime.h"
18
Andreas Gampe8ce9c302016-04-15 21:24:28 -070019#include <ctype.h>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070020#include <errno.h>
21#include <stdlib.h>
22
Andreas Gampe2969bcd2015-03-09 12:57:41 -070023#include <cmath>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070024#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070025#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include <unordered_map>
27
Andreas Gampeaacc25d2015-04-01 14:49:06 -070028#include "ScopedLocalRef.h"
29
Mathieu Chartiere401d142015-04-22 13:56:20 -070030#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080031#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070032#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070033#include "base/logging.h"
34#include "base/macros.h"
35#include "class_linker.h"
36#include "common_throws.h"
37#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080038#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070039#include "handle_scope-inl.h"
40#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070041#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070042#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070044#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010045#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070046#include "mirror/object-inl.h"
47#include "mirror/object_array-inl.h"
48#include "mirror/string-inl.h"
49#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070050#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070051#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020052#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070053#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070054#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070055
56namespace art {
57namespace interpreter {
58
Andreas Gampe068b0c02015-03-11 12:44:47 -070059static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020060 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070061 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020062
63static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070064 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020066 va_start(args, fmt);
67 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070068 va_end(args);
69 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020070 va_start(args, fmt);
71 std::string msg;
72 StringAppendV(&msg, fmt, args);
73 va_end(args);
74 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070075 UNREACHABLE();
76 }
77}
78
Andreas Gampe8ce9c302016-04-15 21:24:28 -070079// Restricted support for character upper case / lower case. Only support ASCII, where
80// it's easy. Abort the transaction otherwise.
81static void CharacterLowerUpper(Thread* self,
82 ShadowFrame* shadow_frame,
83 JValue* result,
84 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070085 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070086 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
87
88 // Only ASCII (7-bit).
89 if (!isascii(int_value)) {
90 AbortTransactionOrFail(self,
91 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
92 int_value);
93 return;
94 }
95
96 std::locale c_locale("C");
97 char char_value = static_cast<char>(int_value);
98
99 if (to_lower_case) {
100 result->SetI(std::tolower(char_value, c_locale));
101 } else {
102 result->SetI(std::toupper(char_value, c_locale));
103 }
104}
105
106void UnstartedRuntime::UnstartedCharacterToLowerCase(
107 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
108 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
109}
110
111void UnstartedRuntime::UnstartedCharacterToUpperCase(
112 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
113 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
114}
115
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700116// Helper function to deal with class loading in an unstarted runtime.
117static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
118 Handle<mirror::ClassLoader> class_loader, JValue* result,
119 const std::string& method_name, bool initialize_class,
120 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700121 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700122 CHECK(className.Get() != nullptr);
123 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
124 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
125
126 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
127 if (found == nullptr && abort_if_not_found) {
128 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700129 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
130 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700131 }
132 return;
133 }
134 if (found != nullptr && initialize_class) {
135 StackHandleScope<1> hs(self);
136 Handle<mirror::Class> h_class(hs.NewHandle(found));
137 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
138 CHECK(self->IsExceptionPending());
139 return;
140 }
141 }
142 result->SetL(found);
143}
144
145// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
146// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
147// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200148// actually the transaction abort exception. This must not be wrapped, as it signals an
149// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700150static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700151 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700152 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200153 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700154 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200155 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700156 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
157 "ClassNotFoundException");
158 }
159 }
160}
161
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700162static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700164 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
165 if (param == nullptr) {
166 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
167 return nullptr;
168 }
169 return param->AsString();
170}
171
Andreas Gampe799681b2015-05-15 19:24:12 -0700172void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700173 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700174 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
175 if (class_name == nullptr) {
176 return;
177 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700178 StackHandleScope<1> hs(self);
179 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800180 UnstartedRuntimeFindClass(self,
181 h_class_name,
182 ScopedNullHandle<mirror::ClassLoader>(),
183 result,
184 "Class.forName",
185 true,
186 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700187 CheckExceptionGenerateClassNotFound(self);
188}
189
Andreas Gampe799681b2015-05-15 19:24:12 -0700190void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700191 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700192 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
193 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700194 return;
195 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700196 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
197 mirror::ClassLoader* class_loader =
198 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
199 StackHandleScope<2> hs(self);
200 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
201 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
202 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
203 initialize_class, false);
204 CheckExceptionGenerateClassNotFound(self);
205}
206
Andreas Gampe799681b2015-05-15 19:24:12 -0700207void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700209 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
210 if (class_name == nullptr) {
211 return;
212 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700213 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
214 mirror::ClassLoader* class_loader =
215 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
216 StackHandleScope<2> hs(self);
217 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
218 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
219 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
220 initialize_class, false);
221 CheckExceptionGenerateClassNotFound(self);
222}
223
Andreas Gampe799681b2015-05-15 19:24:12 -0700224void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700225 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
226 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700227 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
228 if (param == nullptr) {
229 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
230 return;
231 }
232 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700233 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700234
235 // Check that it's not null.
236 if (h_klass.Get() == nullptr) {
237 AbortTransactionOrFail(self, "Class reference is null for newInstance");
238 return;
239 }
240
241 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
242 if (Runtime::Current()->IsActiveTransaction()) {
243 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200244 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
245 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700246 return;
247 }
248 }
249
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700250 // There are two situations in which we'll abort this run.
251 // 1) If the class isn't yet initialized and initialization fails.
252 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
253 // Note that 2) could likely be handled here, but for safety abort the transaction.
254 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700255 auto* cl = Runtime::Current()->GetClassLinker();
256 if (cl->EnsureInitialized(self, h_klass, true, true)) {
257 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
258 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700259 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
260 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700261 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700262 if (!self->IsExceptionPending()) {
263 result->SetL(h_obj.Get());
264 ok = true;
265 }
266 } else {
267 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
268 "Could not find default constructor for '%s'",
269 PrettyClass(h_klass.Get()).c_str());
270 }
271 }
272 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700273 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
274 PrettyClass(h_klass.Get()).c_str(),
275 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700276 }
277}
278
Andreas Gampe799681b2015-05-15 19:24:12 -0700279void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700280 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700281 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
282 // going the reflective Dex way.
283 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
284 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700285 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700286 for (ArtField& field : klass->GetIFields()) {
287 if (name2->Equals(field.GetName())) {
288 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700289 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700290 }
291 }
292 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700293 for (ArtField& field : klass->GetSFields()) {
294 if (name2->Equals(field.GetName())) {
295 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700296 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700297 }
298 }
299 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700300 if (found == nullptr) {
301 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
302 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
303 PrettyDescriptor(klass).c_str());
304 return;
305 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700306 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700307 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700308 mirror::Field* field;
309 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700310 if (pointer_size == PointerSize::k64) {
311 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
312 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700313 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700314 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
315 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700316 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700317 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700318 if (pointer_size == PointerSize::k64) {
319 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
320 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700321 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700322 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
323 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700324 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700325 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700326 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700327}
328
Andreas Gampebc4d2182016-02-22 10:03:12 -0800329// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
330void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
332 // Special managed code cut-out to allow method lookup in a un-started runtime.
333 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
334 if (klass == nullptr) {
335 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
336 return;
337 }
338 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
339 mirror::ObjectArray<mirror::Class>* args =
340 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700341 Runtime* runtime = Runtime::Current();
342 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700343 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700344 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700345 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700346 if (pointer_size == PointerSize::k64) {
347 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
348 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700349 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700350 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
351 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700352 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800353 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700354 if (pointer_size == PointerSize::k64) {
355 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
356 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700357 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700358 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
359 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700360 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800361 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700362 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800363}
364
Andreas Gampe6039e562016-04-05 18:18:43 -0700365// Special managed code cut-out to allow constructor lookup in a un-started runtime.
366void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
367 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
368 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
369 if (klass == nullptr) {
370 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
371 return;
372 }
373 mirror::ObjectArray<mirror::Class>* args =
374 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700375 Runtime* runtime = Runtime::Current();
376 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700377 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700378 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700379 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700380 if (pointer_size == PointerSize::k64) {
381 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
382 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700383 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700384 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
385 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700386 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700387 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700388 if (pointer_size == PointerSize::k64) {
389 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
390 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700391 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700392 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
393 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700394 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700395 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700396 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700397}
398
Andreas Gampe633750c2016-02-19 10:49:50 -0800399void UnstartedRuntime::UnstartedClassGetEnclosingClass(
400 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
401 StackHandleScope<1> hs(self);
402 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
403 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
404 result->SetL(nullptr);
405 }
David Sehr9323e6e2016-09-13 08:58:35 -0700406 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800407}
408
Andreas Gampe715fdc22016-04-18 17:07:30 -0700409void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
410 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
411 StackHandleScope<1> hs(self);
412 Handle<mirror::Class> klass(hs.NewHandle(
413 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
414 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
415 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
416}
417
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700418static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
419 const char* entry_name,
420 size_t* size,
421 std::string* error_msg) {
422 CHECK(size != nullptr);
423
424 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
425 if (zip_archive == nullptr) {
426 return nullptr;;
427 }
428 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
429 if (zip_entry == nullptr) {
430 return nullptr;
431 }
432 std::unique_ptr<MemMap> tmp_map(
433 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
434 if (tmp_map == nullptr) {
435 return nullptr;
436 }
437
438 // OK, from here everything seems fine.
439 *size = zip_entry->GetUncompressedLength();
440 return tmp_map;
441}
442
443static void GetResourceAsStream(Thread* self,
444 ShadowFrame* shadow_frame,
445 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700446 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700447 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
448 if (resource_obj == nullptr) {
449 AbortTransactionOrFail(self, "null name for getResourceAsStream");
450 return;
451 }
452 CHECK(resource_obj->IsString());
453 mirror::String* resource_name = resource_obj->AsString();
454
455 std::string resource_name_str = resource_name->ToModifiedUtf8();
456 if (resource_name_str.empty() || resource_name_str == "/") {
457 AbortTransactionOrFail(self,
458 "Unsupported name %s for getResourceAsStream",
459 resource_name_str.c_str());
460 return;
461 }
462 const char* resource_cstr = resource_name_str.c_str();
463 if (resource_cstr[0] == '/') {
464 resource_cstr++;
465 }
466
467 Runtime* runtime = Runtime::Current();
468
469 std::vector<std::string> split;
470 Split(runtime->GetBootClassPathString(), ':', &split);
471 if (split.empty()) {
472 AbortTransactionOrFail(self,
473 "Boot classpath not set or split error:: %s",
474 runtime->GetBootClassPathString().c_str());
475 return;
476 }
477
478 std::unique_ptr<MemMap> mem_map;
479 size_t map_size;
480 std::string last_error_msg; // Only store the last message (we could concatenate).
481
482 for (const std::string& jar_file : split) {
483 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
484 if (mem_map != nullptr) {
485 break;
486 }
487 }
488
489 if (mem_map == nullptr) {
490 // Didn't find it. There's a good chance this will be the same at runtime, but still
491 // conservatively abort the transaction here.
492 AbortTransactionOrFail(self,
493 "Could not find resource %s. Last error was %s.",
494 resource_name_str.c_str(),
495 last_error_msg.c_str());
496 return;
497 }
498
499 StackHandleScope<3> hs(self);
500
501 // Create byte array for content.
502 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
503 if (h_array.Get() == nullptr) {
504 AbortTransactionOrFail(self, "Could not find/create byte array class");
505 return;
506 }
507 // Copy in content.
508 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
509 // Be proactive releasing memory.
510 mem_map.release();
511
512 // Create a ByteArrayInputStream.
513 Handle<mirror::Class> h_class(hs.NewHandle(
514 runtime->GetClassLinker()->FindClass(self,
515 "Ljava/io/ByteArrayInputStream;",
516 ScopedNullHandle<mirror::ClassLoader>())));
517 if (h_class.Get() == nullptr) {
518 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
519 return;
520 }
521 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
522 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
523 return;
524 }
525
526 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
527 if (h_obj.Get() == nullptr) {
528 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
529 return;
530 }
531
532 auto* cl = Runtime::Current()->GetClassLinker();
533 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
534 "<init>", "([B)V", cl->GetImagePointerSize());
535 if (constructor == nullptr) {
536 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
537 return;
538 }
539
540 uint32_t args[1];
541 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
542 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
543
544 if (self->IsExceptionPending()) {
545 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
546 return;
547 }
548
549 result->SetL(h_obj.Get());
550}
551
552void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
553 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
554 {
555 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
556 CHECK(this_obj != nullptr);
557 CHECK(this_obj->IsClassLoader());
558
559 StackHandleScope<1> hs(self);
560 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
561
562 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
563 this_classloader_class.Get()) {
564 AbortTransactionOrFail(self,
565 "Unsupported classloader type %s for getResourceAsStream",
566 PrettyClass(this_classloader_class.Get()).c_str());
567 return;
568 }
569 }
570
571 GetResourceAsStream(self, shadow_frame, result, arg_offset);
572}
573
Andreas Gampe799681b2015-05-15 19:24:12 -0700574void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700575 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700576 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
577 mirror::ClassLoader* class_loader =
578 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
579 StackHandleScope<2> hs(self);
580 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
581 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
582 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
583 "VMClassLoader.findLoadedClass", false, false);
584 // This might have an error pending. But semantics are to just return null.
585 if (self->IsExceptionPending()) {
586 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
587 std::string type(PrettyTypeOf(self->GetException()));
588 if (type != "java.lang.InternalError") {
589 self->ClearException();
590 }
591 }
592}
593
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594void UnstartedRuntime::UnstartedVoidLookupType(
595 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
596 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700597 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
598}
599
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700600// Arraycopy emulation.
601// Note: we can't use any fast copy functions, as they are not available under transaction.
602
603template <typename T>
604static void PrimitiveArrayCopy(Thread* self,
605 mirror::Array* src_array, int32_t src_pos,
606 mirror::Array* dst_array, int32_t dst_pos,
607 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700608 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700609 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
610 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
611 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
612 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
613 return;
614 }
615 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
616 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
617 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
618 if (copy_forward) {
619 for (int32_t i = 0; i < length; ++i) {
620 dst->Set(dst_pos + i, src->Get(src_pos + i));
621 }
622 } else {
623 for (int32_t i = 1; i <= length; ++i) {
624 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
625 }
626 }
627}
628
Andreas Gampe799681b2015-05-15 19:24:12 -0700629void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700630 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700631 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700632 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
633 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700634 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700635
Andreas Gampe85a098a2016-03-31 13:30:53 -0700636 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
637 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
638 // Null checking. For simplicity, abort transaction.
639 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700640 AbortTransactionOrFail(self, "src is null in arraycopy.");
641 return;
642 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700643 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700644 AbortTransactionOrFail(self, "dst is null in arraycopy.");
645 return;
646 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700647 // Test for arrayness. Throw ArrayStoreException.
648 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
649 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
650 return;
651 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700652
Andreas Gampe85a098a2016-03-31 13:30:53 -0700653 mirror::Array* src_array = src_obj->AsArray();
654 mirror::Array* dst_array = dst_obj->AsArray();
655
656 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700657 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
658 UNLIKELY(src_pos > src_array->GetLength() - length) ||
659 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700660 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700661 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
662 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
663 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700664 return;
665 }
666
667 // Type checking.
668 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
669 GetComponentType();
670
671 if (!src_type->IsPrimitive()) {
672 // Check that the second type is not primitive.
673 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
674 GetComponentType();
675 if (trg_type->IsPrimitiveInt()) {
676 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
677 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
678 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
679 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700680 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700681
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700682 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
683 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
684 if (src == dst) {
685 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700686 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700687 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
688 if (copy_forward) {
689 for (int32_t i = 0; i < length; ++i) {
690 dst->Set(dst_pos + i, src->Get(src_pos + i));
691 }
692 } else {
693 for (int32_t i = 1; i <= length; ++i) {
694 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
695 }
696 }
697 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700698 // We're being lazy here. Optimally this could be a memcpy (if component types are
699 // assignable), but the ObjectArray implementation doesn't support transactions. The
700 // checking version, however, does.
701 if (Runtime::Current()->IsActiveTransaction()) {
702 dst->AssignableCheckingMemcpy<true>(
703 dst_pos, src, src_pos, length, true /* throw_exception */);
704 } else {
705 dst->AssignableCheckingMemcpy<false>(
706 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700707 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700708 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700709 } else if (src_type->IsPrimitiveByte()) {
710 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700711 } else if (src_type->IsPrimitiveChar()) {
712 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
713 } else if (src_type->IsPrimitiveInt()) {
714 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700715 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700716 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700717 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700718 }
719}
720
Andreas Gampe5c9af612016-04-05 14:16:10 -0700721void UnstartedRuntime::UnstartedSystemArraycopyByte(
722 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
723 // Just forward.
724 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
725}
726
Andreas Gampe799681b2015-05-15 19:24:12 -0700727void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700728 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700729 // Just forward.
730 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
731}
732
733void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700734 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700735 // Just forward.
736 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
737}
738
Narayan Kamath34a316f2016-03-30 13:11:18 +0100739void UnstartedRuntime::UnstartedSystemGetSecurityManager(
740 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
741 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
742 result->SetL(nullptr);
743}
744
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700745static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
746
747static void GetSystemProperty(Thread* self,
748 ShadowFrame* shadow_frame,
749 JValue* result,
750 size_t arg_offset,
751 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700752 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700753 StackHandleScope<4> hs(self);
754 Handle<mirror::String> h_key(
755 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
756 if (h_key.Get() == nullptr) {
757 AbortTransactionOrFail(self, "getProperty key was null");
758 return;
759 }
760
761 // This is overall inefficient, but reflecting the values here is not great, either. So
762 // for simplicity, and with the assumption that the number of getProperty calls is not
763 // too great, just iterate each time.
764
765 // Get the storage class.
766 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
767 Handle<mirror::Class> h_props_class(hs.NewHandle(
768 class_linker->FindClass(self,
769 "Ljava/lang/AndroidHardcodedSystemProperties;",
770 ScopedNullHandle<mirror::ClassLoader>())));
771 if (h_props_class.Get() == nullptr) {
772 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
773 return;
774 }
775 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
776 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
777 return;
778 }
779
780 // Get the storage array.
781 ArtField* static_properties =
782 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
783 "[[Ljava/lang/String;");
784 if (static_properties == nullptr) {
785 AbortTransactionOrFail(self,
786 "Could not find %s field",
787 kAndroidHardcodedSystemPropertiesFieldName);
788 return;
789 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700790 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
791 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
792 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700793 if (h_2string_array.Get() == nullptr) {
794 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
795 return;
796 }
797
798 // Iterate over it.
799 const int32_t prop_count = h_2string_array->GetLength();
800 // Use the third handle as mutable.
801 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
802 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
803 for (int32_t i = 0; i < prop_count; ++i) {
804 h_string_array.Assign(h_2string_array->Get(i));
805 if (h_string_array.Get() == nullptr ||
806 h_string_array->GetLength() != 2 ||
807 h_string_array->Get(0) == nullptr) {
808 AbortTransactionOrFail(self,
809 "Unexpected content of %s",
810 kAndroidHardcodedSystemPropertiesFieldName);
811 return;
812 }
813 if (h_key->Equals(h_string_array->Get(0))) {
814 // Found a value.
815 if (h_string_array->Get(1) == nullptr && is_default_version) {
816 // Null is being delegated to the default map, and then resolved to the given default value.
817 // As there's no default map, return the given value.
818 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
819 } else {
820 result->SetL(h_string_array->Get(1));
821 }
822 return;
823 }
824 }
825
826 // Key is not supported.
827 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
828}
829
830void UnstartedRuntime::UnstartedSystemGetProperty(
831 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
832 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
833}
834
835void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
836 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
837 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
838}
839
Andreas Gampe799681b2015-05-15 19:24:12 -0700840void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700841 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700842 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
843 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100844 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
845 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700846 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100847 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700848 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100849 } else if (caller ==
850 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
851 // Allocate new object.
852 StackHandleScope<2> hs(self);
853 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
854 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
855 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
856 h_real_to_string_class->AllocObject(self)));
857 if (h_real_to_string_obj.Get() != nullptr) {
858 auto* cl = Runtime::Current()->GetClassLinker();
859 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
860 "<init>", "()V", cl->GetImagePointerSize());
861 if (init_method == nullptr) {
Andreas Gampe3fec9ac2016-09-13 10:47:28 -0700862 h_real_to_string_class->DumpClass(LOG_STREAM(FATAL), mirror::Class::kDumpClassFullDetail);
Narayan Kamatha1e93122016-03-30 15:41:54 +0100863 } else {
864 JValue invoke_result;
865 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
866 nullptr);
867 if (!self->IsExceptionPending()) {
868 result->SetL(h_real_to_string_obj.Get());
869 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700870 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700871 }
872 }
873 }
874
875 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700876 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700877 }
878}
879
Sergio Giro83261202016-04-11 20:49:20 +0100880void UnstartedRuntime::UnstartedMathCeil(
881 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700882 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100883}
884
885void UnstartedRuntime::UnstartedMathFloor(
886 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700887 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700888}
889
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700890void UnstartedRuntime::UnstartedMathSin(
891 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
892 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
893}
894
895void UnstartedRuntime::UnstartedMathCos(
896 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
897 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
898}
899
900void UnstartedRuntime::UnstartedMathPow(
901 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
902 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
903 shadow_frame->GetVRegDouble(arg_offset + 2)));
904}
905
Andreas Gampe799681b2015-05-15 19:24:12 -0700906void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700907 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700908 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
909 result->SetI(obj->IdentityHashCode());
910}
911
Andreas Gampe799681b2015-05-15 19:24:12 -0700912void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700913 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700914 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000915 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700916}
917
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700918static ObjPtr<mirror::Object> GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700919 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700920 const DexFile* dex_file = dex_cache->GetDexFile();
921 if (dex_file == nullptr) {
922 return nullptr;
923 }
924
925 // Create the direct byte buffer.
926 JNIEnv* env = self->GetJniEnv();
927 DCHECK(env != nullptr);
928 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700929 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
930 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700931 DCHECK(self->IsExceptionPending());
932 return nullptr;
933 }
934
935 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700936 args[0].l = byte_buffer.get();
937
938 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
939 WellKnownClasses::com_android_dex_Dex,
940 WellKnownClasses::com_android_dex_Dex_create,
941 args));
942
943 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700944}
945
Andreas Gampe799681b2015-05-15 19:24:12 -0700946void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700947 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700948 // We will create the Dex object, but the image writer will release it before creating the
949 // art file.
950 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
951 bool have_dex = false;
952 if (src != nullptr) {
Mathieu Chartierc4f39252016-10-05 18:32:08 -0700953 ObjPtr<mirror::Object> dex = GetDexFromDexCache(self, src->AsDexCache());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700954 if (dex != nullptr) {
955 have_dex = true;
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700956 result->SetL(dex);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700957 }
958 }
959 if (!have_dex) {
960 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200961 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700962 }
963}
964
965static void UnstartedMemoryPeek(
966 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
967 int64_t address = shadow_frame->GetVRegLong(arg_offset);
968 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
969 // aborting the transaction.
970
971 switch (type) {
972 case Primitive::kPrimByte: {
973 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
974 return;
975 }
976
977 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700978 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
979 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700980 return;
981 }
982
983 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700984 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
985 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700986 return;
987 }
988
989 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700990 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
991 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700992 return;
993 }
994
995 case Primitive::kPrimBoolean:
996 case Primitive::kPrimChar:
997 case Primitive::kPrimFloat:
998 case Primitive::kPrimDouble:
999 case Primitive::kPrimVoid:
1000 case Primitive::kPrimNot:
1001 LOG(FATAL) << "Not in the Memory API: " << type;
1002 UNREACHABLE();
1003 }
1004 LOG(FATAL) << "Should not reach here";
1005 UNREACHABLE();
1006}
1007
Andreas Gampe799681b2015-05-15 19:24:12 -07001008void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001009 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001010 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1011}
1012
1013void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001014 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001015 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1016}
1017
1018void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001019 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001020 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1021}
1022
1023void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001024 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001025 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001026}
1027
1028static void UnstartedMemoryPeekArray(
1029 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001030 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001031 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1032 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1033 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001034 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001035 return;
1036 }
1037 mirror::Array* array = obj->AsArray();
1038
1039 int offset = shadow_frame->GetVReg(arg_offset + 3);
1040 int count = shadow_frame->GetVReg(arg_offset + 4);
1041 if (offset < 0 || offset + count > array->GetLength()) {
1042 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1043 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001044 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001045 return;
1046 }
1047
1048 switch (type) {
1049 case Primitive::kPrimByte: {
1050 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1051 mirror::ByteArray* byte_array = array->AsByteArray();
1052 for (int32_t i = 0; i < count; ++i, ++address) {
1053 byte_array->SetWithoutChecks<true>(i + offset, *address);
1054 }
1055 return;
1056 }
1057
1058 case Primitive::kPrimShort:
1059 case Primitive::kPrimInt:
1060 case Primitive::kPrimLong:
1061 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1062 UNREACHABLE();
1063
1064 case Primitive::kPrimBoolean:
1065 case Primitive::kPrimChar:
1066 case Primitive::kPrimFloat:
1067 case Primitive::kPrimDouble:
1068 case Primitive::kPrimVoid:
1069 case Primitive::kPrimNot:
1070 LOG(FATAL) << "Not in the Memory API: " << type;
1071 UNREACHABLE();
1072 }
1073 LOG(FATAL) << "Should not reach here";
1074 UNREACHABLE();
1075}
1076
Andreas Gampe799681b2015-05-15 19:24:12 -07001077void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001078 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001079 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001080}
1081
Kenny Root1c9e61c2015-05-14 15:58:17 -07001082// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001083void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001084 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001085 jint start = shadow_frame->GetVReg(arg_offset + 1);
1086 jint end = shadow_frame->GetVReg(arg_offset + 2);
1087 jint index = shadow_frame->GetVReg(arg_offset + 4);
1088 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1089 if (string == nullptr) {
1090 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1091 return;
1092 }
Kenny Root57f91e82015-05-14 15:58:17 -07001093 DCHECK_GE(start, 0);
1094 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001095 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001096 Handle<mirror::CharArray> h_char_array(
1097 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001098 DCHECK_LE(index, h_char_array->GetLength());
1099 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001100 string->GetChars(start, end, h_char_array, index);
1101}
1102
1103// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001104void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001105 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001106 jint index = shadow_frame->GetVReg(arg_offset + 1);
1107 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1108 if (string == nullptr) {
1109 AbortTransactionOrFail(self, "String.charAt with null object");
1110 return;
1111 }
1112 result->SetC(string->CharAt(index));
1113}
1114
Kenny Root57f91e82015-05-14 15:58:17 -07001115// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001116void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001117 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001118 jint index = shadow_frame->GetVReg(arg_offset + 1);
1119 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1120 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1121 if (string == nullptr) {
1122 AbortTransactionOrFail(self, "String.setCharAt with null object");
1123 return;
1124 }
1125 string->SetCharAt(index, c);
1126}
1127
Kenny Root1c9e61c2015-05-14 15:58:17 -07001128// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001129void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001131 jint offset = shadow_frame->GetVReg(arg_offset);
1132 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1133 DCHECK_GE(char_count, 0);
1134 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001135 Handle<mirror::CharArray> h_char_array(
1136 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001137 Runtime* runtime = Runtime::Current();
1138 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1139 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1140}
1141
1142// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001143void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001144 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001145 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1146 if (to_copy == nullptr) {
1147 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1148 return;
1149 }
1150 StackHandleScope<1> hs(self);
1151 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1152 Runtime* runtime = Runtime::Current();
1153 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1154 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1155 allocator));
1156}
1157
Andreas Gampe799681b2015-05-15 19:24:12 -07001158void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001159 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001160 jint start = shadow_frame->GetVReg(arg_offset + 1);
1161 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001162 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001163 DCHECK_GE(length, 0);
1164 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165 Handle<mirror::String> h_string(
1166 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001167 DCHECK_LE(start, h_string->GetLength());
1168 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001169 Runtime* runtime = Runtime::Current();
1170 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1171 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1172}
1173
Kenny Root57f91e82015-05-14 15:58:17 -07001174// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001175void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001176 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001177 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001178 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1179 if (string == nullptr) {
1180 AbortTransactionOrFail(self, "String.charAt with null object");
1181 return;
1182 }
1183 result->SetL(string->ToCharArray(self));
1184}
1185
Andreas Gampebc4d2182016-02-22 10:03:12 -08001186// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1187void UnstartedRuntime::UnstartedReferenceGetReferent(
1188 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001189 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001190 shadow_frame->GetVRegReference(arg_offset));
1191 if (ref == nullptr) {
1192 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1193 return;
1194 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001195 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001196 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1197 result->SetL(referent);
1198}
1199
1200// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1201// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1202// where we can predict the behavior (somewhat).
1203// Note: this is required (instead of lazy initialization) as these classes are used in the static
1204// initialization of other classes, so will *use* the value.
1205void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1206 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1207 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
1208 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1209 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1210 // 8 as a conservative upper approximation.
1211 result->SetI(8);
1212 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1213 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1214 // a good upper bound.
1215 // TODO: Consider resetting in the zygote?
1216 result->SetI(8);
1217 } else {
1218 // Not supported.
1219 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1220 }
1221}
1222
1223// This allows accessing ConcurrentHashMap/SynchronousQueue.
1224
1225void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1226 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1227 // Argument 0 is the Unsafe instance, skip.
1228 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1229 if (obj == nullptr) {
1230 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1231 return;
1232 }
1233 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1234 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1235 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
1236
1237 // Must use non transactional mode.
1238 if (kUseReadBarrier) {
1239 // Need to make sure the reference stored in the field is a to-space one before attempting the
1240 // CAS or the CAS could fail incorrectly.
1241 mirror::HeapReference<mirror::Object>* field_addr =
1242 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1243 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1244 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1245 obj,
1246 MemberOffset(offset),
1247 field_addr);
1248 }
1249 bool success;
1250 // Check whether we're in a transaction, call accordingly.
1251 if (Runtime::Current()->IsActiveTransaction()) {
1252 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1253 expectedValue,
1254 newValue);
1255 } else {
1256 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1257 expectedValue,
1258 newValue);
1259 }
1260 result->SetZ(success ? 1 : 0);
1261}
1262
1263void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1264 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1265 // Argument 0 is the Unsafe instance, skip.
1266 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1267 if (obj == nullptr) {
1268 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1269 return;
1270 }
1271 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1272 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1273 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1274
1275 // Must use non transactional mode.
1276 if (kUseReadBarrier) {
1277 // Need to make sure the reference stored in the field is a to-space one before attempting the
1278 // CAS or the CAS could fail incorrectly.
1279 mirror::HeapReference<mirror::Object>* field_addr =
1280 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1281 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1282 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1283 obj,
1284 MemberOffset(offset),
1285 field_addr);
1286 }
1287 bool success;
1288 // Check whether we're in a transaction, call accordingly.
1289 if (Runtime::Current()->IsActiveTransaction()) {
1290 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1291 expected_value,
1292 newValue);
1293 } else {
1294 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1295 expected_value,
1296 newValue);
1297 }
1298 result->SetZ(success ? 1 : 0);
1299}
1300
1301void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1302 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001303 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001304 // Argument 0 is the Unsafe instance, skip.
1305 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1306 if (obj == nullptr) {
1307 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1308 return;
1309 }
1310 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1311 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1312 result->SetL(value);
1313}
1314
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001315void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1316 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001317 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001318 // Argument 0 is the Unsafe instance, skip.
1319 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1320 if (obj == nullptr) {
1321 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1322 return;
1323 }
1324 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1325 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1326 if (Runtime::Current()->IsActiveTransaction()) {
1327 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1328 } else {
1329 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1330 }
1331}
1332
Andreas Gampebc4d2182016-02-22 10:03:12 -08001333void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1334 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001335 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001336 // Argument 0 is the Unsafe instance, skip.
1337 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1338 if (obj == nullptr) {
1339 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1340 return;
1341 }
1342 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1343 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1344 QuasiAtomic::ThreadFenceRelease();
1345 if (Runtime::Current()->IsActiveTransaction()) {
1346 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1347 } else {
1348 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1349 }
1350}
1351
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001352// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1353// of correctly handling the corner cases.
1354void UnstartedRuntime::UnstartedIntegerParseInt(
1355 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001356 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001357 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1358 if (obj == nullptr) {
1359 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1360 return;
1361 }
1362
1363 std::string string_value = obj->AsString()->ToModifiedUtf8();
1364 if (string_value.empty()) {
1365 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1366 return;
1367 }
1368
1369 const char* c_str = string_value.c_str();
1370 char *end;
1371 // Can we set errno to 0? Is this always a variable, and not a macro?
1372 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1373 int64_t l = strtol(c_str, &end, 10);
1374
1375 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1376 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1377 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1378 return;
1379 }
1380 if (l == 0) {
1381 // Check whether the string wasn't exactly zero.
1382 if (string_value != "0") {
1383 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1384 return;
1385 }
1386 } else if (*end != '\0') {
1387 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1388 return;
1389 }
1390
1391 result->SetI(static_cast<int32_t>(l));
1392}
1393
1394// A cutout for Long.parseLong.
1395//
1396// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1397// well.
1398void UnstartedRuntime::UnstartedLongParseLong(
1399 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001400 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001401 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1402 if (obj == nullptr) {
1403 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1404 return;
1405 }
1406
1407 std::string string_value = obj->AsString()->ToModifiedUtf8();
1408 if (string_value.empty()) {
1409 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1410 return;
1411 }
1412
1413 const char* c_str = string_value.c_str();
1414 char *end;
1415 // Can we set errno to 0? Is this always a variable, and not a macro?
1416 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1417 int64_t l = strtol(c_str, &end, 10);
1418
1419 // Note: comparing against int32_t min/max is intentional here.
1420 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1421 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1422 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1423 return;
1424 }
1425 if (l == 0) {
1426 // Check whether the string wasn't exactly zero.
1427 if (string_value != "0") {
1428 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1429 return;
1430 }
1431 } else if (*end != '\0') {
1432 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1433 return;
1434 }
1435
1436 result->SetJ(l);
1437}
1438
Andreas Gampe715fdc22016-04-18 17:07:30 -07001439void UnstartedRuntime::UnstartedMethodInvoke(
1440 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001441 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001442 JNIEnvExt* env = self->GetJniEnv();
1443 ScopedObjectAccessUnchecked soa(self);
1444
Mathieu Chartier8778c522016-10-04 19:06:30 -07001445 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001446 ScopedLocalRef<jobject> java_method(env,
1447 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1448
Mathieu Chartier8778c522016-10-04 19:06:30 -07001449 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001450 ScopedLocalRef<jobject> java_receiver(env,
1451 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1452
Mathieu Chartier8778c522016-10-04 19:06:30 -07001453 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001454 ScopedLocalRef<jobject> java_args(env,
1455 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1456
1457 ScopedLocalRef<jobject> result_jobj(env,
1458 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1459
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001460 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001461
1462 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1463 // InvocationTargetExceptions.
1464 if (self->IsExceptionPending()) {
1465 AbortTransactionOrFail(self, "Failed Method.invoke");
1466 }
1467}
1468
Andreas Gampebc4d2182016-02-22 10:03:12 -08001469
Mathieu Chartiere401d142015-04-22 13:56:20 -07001470void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1471 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1472 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001473 int32_t length = args[1];
1474 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001475 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001476 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001477 ObjPtr<mirror::Class> array_class =
1478 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001479 DCHECK(array_class != nullptr);
1480 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001481 result->SetL(mirror::Array::Alloc<true, true>(self,
1482 array_class,
1483 length,
1484 array_class->GetComponentSizeShift(),
1485 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001486}
1487
Mathieu Chartiere401d142015-04-22 13:56:20 -07001488void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1489 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1490 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001491 result->SetL(nullptr);
1492}
1493
Mathieu Chartiere401d142015-04-22 13:56:20 -07001494void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1495 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1496 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001497 NthCallerVisitor visitor(self, 3);
1498 visitor.WalkStack();
1499 if (visitor.caller != nullptr) {
1500 result->SetL(visitor.caller->GetDeclaringClass());
1501 }
1502}
1503
Mathieu Chartiere401d142015-04-22 13:56:20 -07001504void UnstartedRuntime::UnstartedJNIMathLog(
1505 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1506 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001507 JValue value;
1508 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1509 result->SetD(log(value.GetD()));
1510}
1511
Mathieu Chartiere401d142015-04-22 13:56:20 -07001512void UnstartedRuntime::UnstartedJNIMathExp(
1513 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1514 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001515 JValue value;
1516 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1517 result->SetD(exp(value.GetD()));
1518}
1519
Andreas Gampebc4d2182016-02-22 10:03:12 -08001520void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1521 Thread* self ATTRIBUTE_UNUSED,
1522 ArtMethod* method ATTRIBUTE_UNUSED,
1523 mirror::Object* receiver ATTRIBUTE_UNUSED,
1524 uint32_t* args ATTRIBUTE_UNUSED,
1525 JValue* result) {
1526 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1527 ? 0
1528 : 1);
1529}
1530
Mathieu Chartiere401d142015-04-22 13:56:20 -07001531void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1532 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1533 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001534 StackHandleScope<1> hs(self);
1535 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1536}
1537
Andreas Gampebc4d2182016-02-22 10:03:12 -08001538void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1539 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1540 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1541 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1542 result->SetD(bit_cast<double>(long_input));
1543}
1544
Mathieu Chartiere401d142015-04-22 13:56:20 -07001545void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1546 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1547 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001548 result->SetI(args[0]);
1549}
1550
Mathieu Chartiere401d142015-04-22 13:56:20 -07001551void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1552 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1553 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001554 result->SetI(args[0]);
1555}
1556
Mathieu Chartiere401d142015-04-22 13:56:20 -07001557void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1558 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1559 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001560 result->SetL(receiver->Clone(self));
1561}
1562
Mathieu Chartiere401d142015-04-22 13:56:20 -07001563void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1564 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1565 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001566 receiver->NotifyAll(self);
1567}
1568
Mathieu Chartiere401d142015-04-22 13:56:20 -07001569void UnstartedRuntime::UnstartedJNIStringCompareTo(
1570 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1571 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001572 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1573 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001574 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001575 }
1576 result->SetI(receiver->AsString()->CompareTo(rhs));
1577}
1578
Mathieu Chartiere401d142015-04-22 13:56:20 -07001579void UnstartedRuntime::UnstartedJNIStringIntern(
1580 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1581 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001582 result->SetL(receiver->AsString()->Intern());
1583}
1584
Mathieu Chartiere401d142015-04-22 13:56:20 -07001585void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1586 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1587 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001588 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1589}
1590
Mathieu Chartiere401d142015-04-22 13:56:20 -07001591void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1592 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1593 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001594 StackHandleScope<2> hs(self);
1595 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1596 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1597 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1598}
1599
Mathieu Chartiere401d142015-04-22 13:56:20 -07001600void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1601 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1602 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001603 int32_t length = static_cast<int32_t>(args[1]);
1604 if (length < 0) {
1605 ThrowNegativeArraySizeException(length);
1606 return;
1607 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001608 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001609 Runtime* runtime = Runtime::Current();
1610 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001611 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001612 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001613 CHECK(self->IsExceptionPending());
1614 return;
1615 }
1616 DCHECK(array_class->IsObjectArrayClass());
1617 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1618 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1619 result->SetL(new_array);
1620}
1621
Mathieu Chartiere401d142015-04-22 13:56:20 -07001622void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1623 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1624 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001625 ScopedObjectAccessUnchecked soa(self);
1626 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001627 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001628 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001629 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001630 }
1631}
1632
Mathieu Chartiere401d142015-04-22 13:56:20 -07001633void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1634 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1635 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001636 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1637 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1638}
1639
Mathieu Chartiere401d142015-04-22 13:56:20 -07001640void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1641 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1642 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001643 result->SetZ(JNI_TRUE);
1644}
1645
Mathieu Chartiere401d142015-04-22 13:56:20 -07001646void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1647 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1648 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001649 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1650 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1651 jint expectedValue = args[3];
1652 jint newValue = args[4];
1653 bool success;
1654 if (Runtime::Current()->IsActiveTransaction()) {
1655 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1656 expectedValue, newValue);
1657 } else {
1658 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1659 expectedValue, newValue);
1660 }
1661 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1662}
1663
Narayan Kamath34a316f2016-03-30 13:11:18 +01001664void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1665 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1666 uint32_t* args, JValue* result) {
1667 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1668 if (obj == nullptr) {
1669 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1670 return;
1671 }
1672
1673 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1674 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1675}
1676
Mathieu Chartiere401d142015-04-22 13:56:20 -07001677void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1678 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1679 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001680 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1681 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1682 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1683 if (Runtime::Current()->IsActiveTransaction()) {
1684 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1685 } else {
1686 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1687 }
1688}
1689
Andreas Gampe799681b2015-05-15 19:24:12 -07001690void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001691 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1692 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001693 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1694 Primitive::Type primitive_type = component->GetPrimitiveType();
1695 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1696}
1697
Andreas Gampe799681b2015-05-15 19:24:12 -07001698void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001699 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1700 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001701 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1702 Primitive::Type primitive_type = component->GetPrimitiveType();
1703 result->SetI(Primitive::ComponentSize(primitive_type));
1704}
1705
Andreas Gampedd9d0552015-03-09 12:57:41 -07001706typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001707 size_t arg_size);
1708
Mathieu Chartiere401d142015-04-22 13:56:20 -07001709typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001710 uint32_t* args, JValue* result);
1711
1712static bool tables_initialized_ = false;
1713static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1714static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1715
Andreas Gampe799681b2015-05-15 19:24:12 -07001716void UnstartedRuntime::InitializeInvokeHandlers() {
1717#define UNSTARTED_DIRECT(ShortName, Sig) \
1718 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1719#include "unstarted_runtime_list.h"
1720 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1721#undef UNSTARTED_RUNTIME_DIRECT_LIST
1722#undef UNSTARTED_RUNTIME_JNI_LIST
1723#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001724}
1725
Andreas Gampe799681b2015-05-15 19:24:12 -07001726void UnstartedRuntime::InitializeJNIHandlers() {
1727#define UNSTARTED_JNI(ShortName, Sig) \
1728 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1729#include "unstarted_runtime_list.h"
1730 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1731#undef UNSTARTED_RUNTIME_DIRECT_LIST
1732#undef UNSTARTED_RUNTIME_JNI_LIST
1733#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001734}
1735
Andreas Gampe799681b2015-05-15 19:24:12 -07001736void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001737 CHECK(!tables_initialized_);
1738
Andreas Gampe799681b2015-05-15 19:24:12 -07001739 InitializeInvokeHandlers();
1740 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001741
1742 tables_initialized_ = true;
1743}
1744
Andreas Gampe799681b2015-05-15 19:24:12 -07001745void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1746 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001747 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1748 // problems in core libraries.
1749 CHECK(tables_initialized_);
1750
1751 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1752 const auto& iter = invoke_handlers_.find(name);
1753 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001754 // Clear out the result in case it's not zeroed out.
1755 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001756
1757 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1758 self->PushShadowFrame(shadow_frame);
1759
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001760 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001761
1762 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001763 } else {
1764 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001765 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001766 }
1767}
1768
1769// 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 -07001770void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001771 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001772 std::string name(PrettyMethod(method));
1773 const auto& iter = jni_handlers_.find(name);
1774 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001775 // Clear out the result in case it's not zeroed out.
1776 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001777 (*iter->second)(self, method, receiver, args, result);
1778 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001779 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1780 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001781 } else {
1782 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1783 "non-transactional runtime";
1784 }
1785}
1786
1787} // namespace interpreter
1788} // namespace art