blob: 793260dc5c2c709a732939b862666b6846f3ff12 [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 Gampe2969bcd2015-03-09 12:57:41 -070032#include "base/logging.h"
33#include "base/macros.h"
34#include "class_linker.h"
35#include "common_throws.h"
36#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080037#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070038#include "handle_scope-inl.h"
39#include "interpreter/interpreter_common.h"
40#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070042#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "mirror/object-inl.h"
44#include "mirror/object_array-inl.h"
45#include "mirror/string-inl.h"
46#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070047#include "reflection.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020049#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070050#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070051#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070052
53namespace art {
54namespace interpreter {
55
Andreas Gampe068b0c02015-03-11 12:44:47 -070056static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020057 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070058 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020059
60static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070061 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070062 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020063 va_start(args, fmt);
64 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 va_end(args);
66 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020067 va_start(args, fmt);
68 std::string msg;
69 StringAppendV(&msg, fmt, args);
70 va_end(args);
71 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 UNREACHABLE();
73 }
74}
75
Andreas Gampe8ce9c302016-04-15 21:24:28 -070076// Restricted support for character upper case / lower case. Only support ASCII, where
77// it's easy. Abort the transaction otherwise.
78static void CharacterLowerUpper(Thread* self,
79 ShadowFrame* shadow_frame,
80 JValue* result,
81 size_t arg_offset,
82 bool to_lower_case) SHARED_REQUIRES(Locks::mutator_lock_) {
83 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
84
85 // Only ASCII (7-bit).
86 if (!isascii(int_value)) {
87 AbortTransactionOrFail(self,
88 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
89 int_value);
90 return;
91 }
92
93 std::locale c_locale("C");
94 char char_value = static_cast<char>(int_value);
95
96 if (to_lower_case) {
97 result->SetI(std::tolower(char_value, c_locale));
98 } else {
99 result->SetI(std::toupper(char_value, c_locale));
100 }
101}
102
103void UnstartedRuntime::UnstartedCharacterToLowerCase(
104 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
105 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
106}
107
108void UnstartedRuntime::UnstartedCharacterToUpperCase(
109 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
110 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
111}
112
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700113// Helper function to deal with class loading in an unstarted runtime.
114static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
115 Handle<mirror::ClassLoader> class_loader, JValue* result,
116 const std::string& method_name, bool initialize_class,
117 bool abort_if_not_found)
Mathieu Chartier90443472015-07-16 20:32:27 -0700118 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700119 CHECK(className.Get() != nullptr);
120 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
122
123 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
124 if (found == nullptr && abort_if_not_found) {
125 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700126 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
127 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700128 }
129 return;
130 }
131 if (found != nullptr && initialize_class) {
132 StackHandleScope<1> hs(self);
133 Handle<mirror::Class> h_class(hs.NewHandle(found));
134 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
135 CHECK(self->IsExceptionPending());
136 return;
137 }
138 }
139 result->SetL(found);
140}
141
142// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
143// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
144// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200145// actually the transaction abort exception. This must not be wrapped, as it signals an
146// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700147static void CheckExceptionGenerateClassNotFound(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700148 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200150 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700151 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200152 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700153 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
154 "ClassNotFoundException");
155 }
156 }
157}
158
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700159static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700160 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700161 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
162 if (param == nullptr) {
163 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
164 return nullptr;
165 }
166 return param->AsString();
167}
168
Andreas Gampe799681b2015-05-15 19:24:12 -0700169void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700170 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700171 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
172 if (class_name == nullptr) {
173 return;
174 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700175 StackHandleScope<1> hs(self);
176 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800177 UnstartedRuntimeFindClass(self,
178 h_class_name,
179 ScopedNullHandle<mirror::ClassLoader>(),
180 result,
181 "Class.forName",
182 true,
183 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700184 CheckExceptionGenerateClassNotFound(self);
185}
186
Andreas Gampe799681b2015-05-15 19:24:12 -0700187void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700188 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700189 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
190 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700191 return;
192 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700193 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
194 mirror::ClassLoader* class_loader =
195 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
196 StackHandleScope<2> hs(self);
197 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
198 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
199 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
200 initialize_class, false);
201 CheckExceptionGenerateClassNotFound(self);
202}
203
Andreas Gampe799681b2015-05-15 19:24:12 -0700204void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700205 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700206 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
207 if (class_name == nullptr) {
208 return;
209 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700210 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
211 mirror::ClassLoader* class_loader =
212 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
213 StackHandleScope<2> hs(self);
214 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
215 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
216 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
217 initialize_class, false);
218 CheckExceptionGenerateClassNotFound(self);
219}
220
Andreas Gampe799681b2015-05-15 19:24:12 -0700221void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700222 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
223 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700224 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
225 if (param == nullptr) {
226 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
227 return;
228 }
229 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700230 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700231
232 // Check that it's not null.
233 if (h_klass.Get() == nullptr) {
234 AbortTransactionOrFail(self, "Class reference is null for newInstance");
235 return;
236 }
237
238 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
239 if (Runtime::Current()->IsActiveTransaction()) {
240 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200241 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
242 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700243 return;
244 }
245 }
246
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700247 // There are two situations in which we'll abort this run.
248 // 1) If the class isn't yet initialized and initialization fails.
249 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
250 // Note that 2) could likely be handled here, but for safety abort the transaction.
251 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700252 auto* cl = Runtime::Current()->GetClassLinker();
253 if (cl->EnsureInitialized(self, h_klass, true, true)) {
254 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
255 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700256 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
257 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700259 if (!self->IsExceptionPending()) {
260 result->SetL(h_obj.Get());
261 ok = true;
262 }
263 } else {
264 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
265 "Could not find default constructor for '%s'",
266 PrettyClass(h_klass.Get()).c_str());
267 }
268 }
269 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700270 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
271 PrettyClass(h_klass.Get()).c_str(),
272 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700273 }
274}
275
Andreas Gampe799681b2015-05-15 19:24:12 -0700276void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700277 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700278 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
279 // going the reflective Dex way.
280 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
281 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700282 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700283 for (ArtField& field : klass->GetIFields()) {
284 if (name2->Equals(field.GetName())) {
285 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700286 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700287 }
288 }
289 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700290 for (ArtField& field : klass->GetSFields()) {
291 if (name2->Equals(field.GetName())) {
292 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700293 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700294 }
295 }
296 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700297 if (found == nullptr) {
298 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
299 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
300 PrettyDescriptor(klass).c_str());
301 return;
302 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700303 Runtime* runtime = Runtime::Current();
304 size_t pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
305 mirror::Field* field;
306 if (runtime->IsActiveTransaction()) {
307 if (pointer_size == 8) {
308 field = mirror::Field::CreateFromArtField<8U, true>(self, found, true);
309 } else {
310 DCHECK_EQ(pointer_size, 4U);
311 field = mirror::Field::CreateFromArtField<4U, true>(self, found, true);
312 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700313 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -0700314 if (pointer_size == 8) {
315 field = mirror::Field::CreateFromArtField<8U, false>(self, found, true);
316 } else {
317 DCHECK_EQ(pointer_size, 4U);
318 field = mirror::Field::CreateFromArtField<4U, false>(self, found, true);
319 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700320 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700321 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700322}
323
Andreas Gampebc4d2182016-02-22 10:03:12 -0800324// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
325void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
326 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
327 // Special managed code cut-out to allow method lookup in a un-started runtime.
328 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
329 if (klass == nullptr) {
330 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
331 return;
332 }
333 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
334 mirror::ObjectArray<mirror::Class>* args =
335 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700336 Runtime* runtime = Runtime::Current();
337 bool transaction = runtime->IsActiveTransaction();
338 size_t pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
339 mirror::Method* method;
340 if (transaction) {
341 if (pointer_size == 8U) {
342 method = mirror::Class::GetDeclaredMethodInternal<8U, true>(self, klass, name, args);
343 } else {
344 DCHECK_EQ(pointer_size, 4U);
345 method = mirror::Class::GetDeclaredMethodInternal<4U, true>(self, klass, name, args);
346 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800347 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -0700348 if (pointer_size == 8U) {
349 method = mirror::Class::GetDeclaredMethodInternal<8U, false>(self, klass, name, args);
350 } else {
351 DCHECK_EQ(pointer_size, 4U);
352 method = mirror::Class::GetDeclaredMethodInternal<4U, false>(self, klass, name, args);
353 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800354 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700355 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800356}
357
Andreas Gampe6039e562016-04-05 18:18:43 -0700358// Special managed code cut-out to allow constructor lookup in a un-started runtime.
359void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
360 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
361 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
362 if (klass == nullptr) {
363 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
364 return;
365 }
366 mirror::ObjectArray<mirror::Class>* args =
367 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700368 Runtime* runtime = Runtime::Current();
369 bool transaction = runtime->IsActiveTransaction();
370 size_t pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
371 mirror::Constructor* constructor;
372 if (transaction) {
373 if (pointer_size == 8U) {
374 constructor = mirror::Class::GetDeclaredConstructorInternal<8U, true>(self, klass, args);
375 } else {
376 DCHECK_EQ(pointer_size, 4U);
377 constructor = mirror::Class::GetDeclaredConstructorInternal<4U, true>(self, klass, args);
378 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700379 } else {
Andreas Gampee01e3642016-07-25 13:06:04 -0700380 if (pointer_size == 8U) {
381 constructor = mirror::Class::GetDeclaredConstructorInternal<8U, false>(self, klass, args);
382 } else {
383 DCHECK_EQ(pointer_size, 4U);
384 constructor = mirror::Class::GetDeclaredConstructorInternal<4U, false>(self, klass, args);
385 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700386 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700387 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700388}
389
Andreas Gampe633750c2016-02-19 10:49:50 -0800390void UnstartedRuntime::UnstartedClassGetEnclosingClass(
391 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
392 StackHandleScope<1> hs(self);
393 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
394 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
395 result->SetL(nullptr);
396 }
397 result->SetL(klass->GetDexFile().GetEnclosingClass(klass));
398}
399
Andreas Gampe715fdc22016-04-18 17:07:30 -0700400void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
401 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
402 StackHandleScope<1> hs(self);
403 Handle<mirror::Class> klass(hs.NewHandle(
404 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
405 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
406 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
407}
408
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700409static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
410 const char* entry_name,
411 size_t* size,
412 std::string* error_msg) {
413 CHECK(size != nullptr);
414
415 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
416 if (zip_archive == nullptr) {
417 return nullptr;;
418 }
419 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
420 if (zip_entry == nullptr) {
421 return nullptr;
422 }
423 std::unique_ptr<MemMap> tmp_map(
424 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
425 if (tmp_map == nullptr) {
426 return nullptr;
427 }
428
429 // OK, from here everything seems fine.
430 *size = zip_entry->GetUncompressedLength();
431 return tmp_map;
432}
433
434static void GetResourceAsStream(Thread* self,
435 ShadowFrame* shadow_frame,
436 JValue* result,
437 size_t arg_offset) SHARED_REQUIRES(Locks::mutator_lock_) {
438 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
439 if (resource_obj == nullptr) {
440 AbortTransactionOrFail(self, "null name for getResourceAsStream");
441 return;
442 }
443 CHECK(resource_obj->IsString());
444 mirror::String* resource_name = resource_obj->AsString();
445
446 std::string resource_name_str = resource_name->ToModifiedUtf8();
447 if (resource_name_str.empty() || resource_name_str == "/") {
448 AbortTransactionOrFail(self,
449 "Unsupported name %s for getResourceAsStream",
450 resource_name_str.c_str());
451 return;
452 }
453 const char* resource_cstr = resource_name_str.c_str();
454 if (resource_cstr[0] == '/') {
455 resource_cstr++;
456 }
457
458 Runtime* runtime = Runtime::Current();
459
460 std::vector<std::string> split;
461 Split(runtime->GetBootClassPathString(), ':', &split);
462 if (split.empty()) {
463 AbortTransactionOrFail(self,
464 "Boot classpath not set or split error:: %s",
465 runtime->GetBootClassPathString().c_str());
466 return;
467 }
468
469 std::unique_ptr<MemMap> mem_map;
470 size_t map_size;
471 std::string last_error_msg; // Only store the last message (we could concatenate).
472
473 for (const std::string& jar_file : split) {
474 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
475 if (mem_map != nullptr) {
476 break;
477 }
478 }
479
480 if (mem_map == nullptr) {
481 // Didn't find it. There's a good chance this will be the same at runtime, but still
482 // conservatively abort the transaction here.
483 AbortTransactionOrFail(self,
484 "Could not find resource %s. Last error was %s.",
485 resource_name_str.c_str(),
486 last_error_msg.c_str());
487 return;
488 }
489
490 StackHandleScope<3> hs(self);
491
492 // Create byte array for content.
493 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
494 if (h_array.Get() == nullptr) {
495 AbortTransactionOrFail(self, "Could not find/create byte array class");
496 return;
497 }
498 // Copy in content.
499 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
500 // Be proactive releasing memory.
501 mem_map.release();
502
503 // Create a ByteArrayInputStream.
504 Handle<mirror::Class> h_class(hs.NewHandle(
505 runtime->GetClassLinker()->FindClass(self,
506 "Ljava/io/ByteArrayInputStream;",
507 ScopedNullHandle<mirror::ClassLoader>())));
508 if (h_class.Get() == nullptr) {
509 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
510 return;
511 }
512 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
513 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
514 return;
515 }
516
517 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
518 if (h_obj.Get() == nullptr) {
519 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
520 return;
521 }
522
523 auto* cl = Runtime::Current()->GetClassLinker();
524 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
525 "<init>", "([B)V", cl->GetImagePointerSize());
526 if (constructor == nullptr) {
527 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
528 return;
529 }
530
531 uint32_t args[1];
532 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
533 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
534
535 if (self->IsExceptionPending()) {
536 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
537 return;
538 }
539
540 result->SetL(h_obj.Get());
541}
542
543void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
544 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
545 {
546 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
547 CHECK(this_obj != nullptr);
548 CHECK(this_obj->IsClassLoader());
549
550 StackHandleScope<1> hs(self);
551 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
552
553 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
554 this_classloader_class.Get()) {
555 AbortTransactionOrFail(self,
556 "Unsupported classloader type %s for getResourceAsStream",
557 PrettyClass(this_classloader_class.Get()).c_str());
558 return;
559 }
560 }
561
562 GetResourceAsStream(self, shadow_frame, result, arg_offset);
563}
564
Andreas Gampe799681b2015-05-15 19:24:12 -0700565void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700566 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700567 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
568 mirror::ClassLoader* class_loader =
569 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
570 StackHandleScope<2> hs(self);
571 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
572 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
573 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
574 "VMClassLoader.findLoadedClass", false, false);
575 // This might have an error pending. But semantics are to just return null.
576 if (self->IsExceptionPending()) {
577 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
578 std::string type(PrettyTypeOf(self->GetException()));
579 if (type != "java.lang.InternalError") {
580 self->ClearException();
581 }
582 }
583}
584
Mathieu Chartiere401d142015-04-22 13:56:20 -0700585void UnstartedRuntime::UnstartedVoidLookupType(
586 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
587 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700588 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
589}
590
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700591// Arraycopy emulation.
592// Note: we can't use any fast copy functions, as they are not available under transaction.
593
594template <typename T>
595static void PrimitiveArrayCopy(Thread* self,
596 mirror::Array* src_array, int32_t src_pos,
597 mirror::Array* dst_array, int32_t dst_pos,
598 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700599 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700600 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
601 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
602 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
603 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
604 return;
605 }
606 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
607 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
608 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
609 if (copy_forward) {
610 for (int32_t i = 0; i < length; ++i) {
611 dst->Set(dst_pos + i, src->Get(src_pos + i));
612 }
613 } else {
614 for (int32_t i = 1; i <= length; ++i) {
615 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
616 }
617 }
618}
619
Andreas Gampe799681b2015-05-15 19:24:12 -0700620void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700621 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700622 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700623 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
624 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700625 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700626
Andreas Gampe85a098a2016-03-31 13:30:53 -0700627 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
628 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
629 // Null checking. For simplicity, abort transaction.
630 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700631 AbortTransactionOrFail(self, "src is null in arraycopy.");
632 return;
633 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700634 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700635 AbortTransactionOrFail(self, "dst is null in arraycopy.");
636 return;
637 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700638 // Test for arrayness. Throw ArrayStoreException.
639 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
640 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
641 return;
642 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700643
Andreas Gampe85a098a2016-03-31 13:30:53 -0700644 mirror::Array* src_array = src_obj->AsArray();
645 mirror::Array* dst_array = dst_obj->AsArray();
646
647 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700648 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
649 UNLIKELY(src_pos > src_array->GetLength() - length) ||
650 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700651 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700652 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
653 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
654 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700655 return;
656 }
657
658 // Type checking.
659 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
660 GetComponentType();
661
662 if (!src_type->IsPrimitive()) {
663 // Check that the second type is not primitive.
664 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
665 GetComponentType();
666 if (trg_type->IsPrimitiveInt()) {
667 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
668 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
669 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
670 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700671 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700672
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700673 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
674 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
675 if (src == dst) {
676 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700677 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700678 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
679 if (copy_forward) {
680 for (int32_t i = 0; i < length; ++i) {
681 dst->Set(dst_pos + i, src->Get(src_pos + i));
682 }
683 } else {
684 for (int32_t i = 1; i <= length; ++i) {
685 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
686 }
687 }
688 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700689 // We're being lazy here. Optimally this could be a memcpy (if component types are
690 // assignable), but the ObjectArray implementation doesn't support transactions. The
691 // checking version, however, does.
692 if (Runtime::Current()->IsActiveTransaction()) {
693 dst->AssignableCheckingMemcpy<true>(
694 dst_pos, src, src_pos, length, true /* throw_exception */);
695 } else {
696 dst->AssignableCheckingMemcpy<false>(
697 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700698 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700699 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700700 } else if (src_type->IsPrimitiveByte()) {
701 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700702 } else if (src_type->IsPrimitiveChar()) {
703 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
704 } else if (src_type->IsPrimitiveInt()) {
705 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700706 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700707 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700708 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700709 }
710}
711
Andreas Gampe5c9af612016-04-05 14:16:10 -0700712void UnstartedRuntime::UnstartedSystemArraycopyByte(
713 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
714 // Just forward.
715 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
716}
717
Andreas Gampe799681b2015-05-15 19:24:12 -0700718void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700719 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700720 // Just forward.
721 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
722}
723
724void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700725 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700726 // Just forward.
727 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
728}
729
Narayan Kamath34a316f2016-03-30 13:11:18 +0100730void UnstartedRuntime::UnstartedSystemGetSecurityManager(
731 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
732 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
733 result->SetL(nullptr);
734}
735
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700736static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
737
738static void GetSystemProperty(Thread* self,
739 ShadowFrame* shadow_frame,
740 JValue* result,
741 size_t arg_offset,
742 bool is_default_version)
743 SHARED_REQUIRES(Locks::mutator_lock_) {
744 StackHandleScope<4> hs(self);
745 Handle<mirror::String> h_key(
746 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
747 if (h_key.Get() == nullptr) {
748 AbortTransactionOrFail(self, "getProperty key was null");
749 return;
750 }
751
752 // This is overall inefficient, but reflecting the values here is not great, either. So
753 // for simplicity, and with the assumption that the number of getProperty calls is not
754 // too great, just iterate each time.
755
756 // Get the storage class.
757 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
758 Handle<mirror::Class> h_props_class(hs.NewHandle(
759 class_linker->FindClass(self,
760 "Ljava/lang/AndroidHardcodedSystemProperties;",
761 ScopedNullHandle<mirror::ClassLoader>())));
762 if (h_props_class.Get() == nullptr) {
763 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
764 return;
765 }
766 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
767 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
768 return;
769 }
770
771 // Get the storage array.
772 ArtField* static_properties =
773 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
774 "[[Ljava/lang/String;");
775 if (static_properties == nullptr) {
776 AbortTransactionOrFail(self,
777 "Could not find %s field",
778 kAndroidHardcodedSystemPropertiesFieldName);
779 return;
780 }
781 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(
782 hs.NewHandle(reinterpret_cast<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>*>(
783 static_properties->GetObject(h_props_class.Get()))));
784 if (h_2string_array.Get() == nullptr) {
785 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
786 return;
787 }
788
789 // Iterate over it.
790 const int32_t prop_count = h_2string_array->GetLength();
791 // Use the third handle as mutable.
792 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
793 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
794 for (int32_t i = 0; i < prop_count; ++i) {
795 h_string_array.Assign(h_2string_array->Get(i));
796 if (h_string_array.Get() == nullptr ||
797 h_string_array->GetLength() != 2 ||
798 h_string_array->Get(0) == nullptr) {
799 AbortTransactionOrFail(self,
800 "Unexpected content of %s",
801 kAndroidHardcodedSystemPropertiesFieldName);
802 return;
803 }
804 if (h_key->Equals(h_string_array->Get(0))) {
805 // Found a value.
806 if (h_string_array->Get(1) == nullptr && is_default_version) {
807 // Null is being delegated to the default map, and then resolved to the given default value.
808 // As there's no default map, return the given value.
809 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
810 } else {
811 result->SetL(h_string_array->Get(1));
812 }
813 return;
814 }
815 }
816
817 // Key is not supported.
818 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
819}
820
821void UnstartedRuntime::UnstartedSystemGetProperty(
822 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
823 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
824}
825
826void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
827 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
828 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
829}
830
Andreas Gampe799681b2015-05-15 19:24:12 -0700831void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700833 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
834 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100835 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
836 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700837 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100838 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700839 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100840 } else if (caller ==
841 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
842 // Allocate new object.
843 StackHandleScope<2> hs(self);
844 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
845 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
846 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
847 h_real_to_string_class->AllocObject(self)));
848 if (h_real_to_string_obj.Get() != nullptr) {
849 auto* cl = Runtime::Current()->GetClassLinker();
850 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
851 "<init>", "()V", cl->GetImagePointerSize());
852 if (init_method == nullptr) {
853 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
854 } else {
855 JValue invoke_result;
856 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
857 nullptr);
858 if (!self->IsExceptionPending()) {
859 result->SetL(h_real_to_string_obj.Get());
860 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700861 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700862 }
863 }
864 }
865
866 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700867 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700868 }
869}
870
Sergio Giro83261202016-04-11 20:49:20 +0100871void UnstartedRuntime::UnstartedMathCeil(
872 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700873 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +0100874}
875
876void UnstartedRuntime::UnstartedMathFloor(
877 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -0700878 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700879}
880
Andreas Gampeb8a00f92016-04-18 20:51:13 -0700881void UnstartedRuntime::UnstartedMathSin(
882 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
883 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
884}
885
886void UnstartedRuntime::UnstartedMathCos(
887 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
888 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
889}
890
891void UnstartedRuntime::UnstartedMathPow(
892 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
893 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
894 shadow_frame->GetVRegDouble(arg_offset + 2)));
895}
896
Andreas Gampe799681b2015-05-15 19:24:12 -0700897void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700899 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
900 result->SetI(obj->IdentityHashCode());
901}
902
Andreas Gampe799681b2015-05-15 19:24:12 -0700903void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700904 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700905 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000906 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700907}
908
Andreas Gampedd9d0552015-03-09 12:57:41 -0700909static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700910 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700911 const DexFile* dex_file = dex_cache->GetDexFile();
912 if (dex_file == nullptr) {
913 return nullptr;
914 }
915
916 // Create the direct byte buffer.
917 JNIEnv* env = self->GetJniEnv();
918 DCHECK(env != nullptr);
919 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700920 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
921 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700922 DCHECK(self->IsExceptionPending());
923 return nullptr;
924 }
925
926 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700927 args[0].l = byte_buffer.get();
928
929 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
930 WellKnownClasses::com_android_dex_Dex,
931 WellKnownClasses::com_android_dex_Dex_create,
932 args));
933
934 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700935}
936
Andreas Gampe799681b2015-05-15 19:24:12 -0700937void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700938 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700939 // We will create the Dex object, but the image writer will release it before creating the
940 // art file.
941 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
942 bool have_dex = false;
943 if (src != nullptr) {
944 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
945 if (dex != nullptr) {
946 have_dex = true;
947 result->SetL(dex);
948 }
949 }
950 if (!have_dex) {
951 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200952 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700953 }
954}
955
956static void UnstartedMemoryPeek(
957 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
958 int64_t address = shadow_frame->GetVRegLong(arg_offset);
959 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
960 // aborting the transaction.
961
962 switch (type) {
963 case Primitive::kPrimByte: {
964 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
965 return;
966 }
967
968 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700969 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
970 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700971 return;
972 }
973
974 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700975 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
976 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700977 return;
978 }
979
980 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700981 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
982 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700983 return;
984 }
985
986 case Primitive::kPrimBoolean:
987 case Primitive::kPrimChar:
988 case Primitive::kPrimFloat:
989 case Primitive::kPrimDouble:
990 case Primitive::kPrimVoid:
991 case Primitive::kPrimNot:
992 LOG(FATAL) << "Not in the Memory API: " << type;
993 UNREACHABLE();
994 }
995 LOG(FATAL) << "Should not reach here";
996 UNREACHABLE();
997}
998
Andreas Gampe799681b2015-05-15 19:24:12 -0700999void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001000 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001001 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1002}
1003
1004void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001005 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001006 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1007}
1008
1009void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001010 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001011 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1012}
1013
1014void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001015 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001016 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001017}
1018
1019static void UnstartedMemoryPeekArray(
1020 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001021 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001022 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1023 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1024 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001025 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001026 return;
1027 }
1028 mirror::Array* array = obj->AsArray();
1029
1030 int offset = shadow_frame->GetVReg(arg_offset + 3);
1031 int count = shadow_frame->GetVReg(arg_offset + 4);
1032 if (offset < 0 || offset + count > array->GetLength()) {
1033 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1034 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001035 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001036 return;
1037 }
1038
1039 switch (type) {
1040 case Primitive::kPrimByte: {
1041 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1042 mirror::ByteArray* byte_array = array->AsByteArray();
1043 for (int32_t i = 0; i < count; ++i, ++address) {
1044 byte_array->SetWithoutChecks<true>(i + offset, *address);
1045 }
1046 return;
1047 }
1048
1049 case Primitive::kPrimShort:
1050 case Primitive::kPrimInt:
1051 case Primitive::kPrimLong:
1052 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1053 UNREACHABLE();
1054
1055 case Primitive::kPrimBoolean:
1056 case Primitive::kPrimChar:
1057 case Primitive::kPrimFloat:
1058 case Primitive::kPrimDouble:
1059 case Primitive::kPrimVoid:
1060 case Primitive::kPrimNot:
1061 LOG(FATAL) << "Not in the Memory API: " << type;
1062 UNREACHABLE();
1063 }
1064 LOG(FATAL) << "Should not reach here";
1065 UNREACHABLE();
1066}
1067
Andreas Gampe799681b2015-05-15 19:24:12 -07001068void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001069 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001070 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001071}
1072
Kenny Root1c9e61c2015-05-14 15:58:17 -07001073// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001074void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001075 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001076 jint start = shadow_frame->GetVReg(arg_offset + 1);
1077 jint end = shadow_frame->GetVReg(arg_offset + 2);
1078 jint index = shadow_frame->GetVReg(arg_offset + 4);
1079 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1080 if (string == nullptr) {
1081 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1082 return;
1083 }
Kenny Root57f91e82015-05-14 15:58:17 -07001084 DCHECK_GE(start, 0);
1085 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001086 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001087 Handle<mirror::CharArray> h_char_array(
1088 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -07001089 DCHECK_LE(index, h_char_array->GetLength());
1090 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001091 string->GetChars(start, end, h_char_array, index);
1092}
1093
1094// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001095void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001096 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001097 jint index = shadow_frame->GetVReg(arg_offset + 1);
1098 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1099 if (string == nullptr) {
1100 AbortTransactionOrFail(self, "String.charAt with null object");
1101 return;
1102 }
1103 result->SetC(string->CharAt(index));
1104}
1105
Kenny Root57f91e82015-05-14 15:58:17 -07001106// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001107void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001108 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001109 jint index = shadow_frame->GetVReg(arg_offset + 1);
1110 jchar c = shadow_frame->GetVReg(arg_offset + 2);
1111 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1112 if (string == nullptr) {
1113 AbortTransactionOrFail(self, "String.setCharAt with null object");
1114 return;
1115 }
1116 string->SetCharAt(index, c);
1117}
1118
Kenny Root1c9e61c2015-05-14 15:58:17 -07001119// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001120void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001121 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001122 jint offset = shadow_frame->GetVReg(arg_offset);
1123 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1124 DCHECK_GE(char_count, 0);
1125 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001126 Handle<mirror::CharArray> h_char_array(
1127 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001128 Runtime* runtime = Runtime::Current();
1129 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1130 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1131}
1132
1133// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001134void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001135 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001136 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1137 if (to_copy == nullptr) {
1138 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1139 return;
1140 }
1141 StackHandleScope<1> hs(self);
1142 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1143 Runtime* runtime = Runtime::Current();
1144 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1145 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1146 allocator));
1147}
1148
Andreas Gampe799681b2015-05-15 19:24:12 -07001149void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001150 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001151 jint start = shadow_frame->GetVReg(arg_offset + 1);
1152 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001153 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001154 DCHECK_GE(length, 0);
1155 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156 Handle<mirror::String> h_string(
1157 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001158 DCHECK_LE(start, h_string->GetLength());
1159 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001160 Runtime* runtime = Runtime::Current();
1161 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1162 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1163}
1164
Kenny Root57f91e82015-05-14 15:58:17 -07001165// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001166void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001167 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -07001168 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001169 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1170 if (string == nullptr) {
1171 AbortTransactionOrFail(self, "String.charAt with null object");
1172 return;
1173 }
1174 result->SetL(string->ToCharArray(self));
1175}
1176
Andreas Gampebc4d2182016-02-22 10:03:12 -08001177// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1178void UnstartedRuntime::UnstartedReferenceGetReferent(
1179 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1180 mirror::Reference* const ref = down_cast<mirror::Reference*>(
1181 shadow_frame->GetVRegReference(arg_offset));
1182 if (ref == nullptr) {
1183 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1184 return;
1185 }
1186 mirror::Object* const referent =
1187 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1188 result->SetL(referent);
1189}
1190
1191// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1192// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1193// where we can predict the behavior (somewhat).
1194// Note: this is required (instead of lazy initialization) as these classes are used in the static
1195// initialization of other classes, so will *use* the value.
1196void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1197 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1198 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
1199 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
1200 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1201 // 8 as a conservative upper approximation.
1202 result->SetI(8);
1203 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
1204 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1205 // a good upper bound.
1206 // TODO: Consider resetting in the zygote?
1207 result->SetI(8);
1208 } else {
1209 // Not supported.
1210 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1211 }
1212}
1213
1214// This allows accessing ConcurrentHashMap/SynchronousQueue.
1215
1216void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1217 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1218 // Argument 0 is the Unsafe instance, skip.
1219 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1220 if (obj == nullptr) {
1221 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1222 return;
1223 }
1224 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1225 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1226 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
1227
1228 // Must use non transactional mode.
1229 if (kUseReadBarrier) {
1230 // Need to make sure the reference stored in the field is a to-space one before attempting the
1231 // CAS or the CAS could fail incorrectly.
1232 mirror::HeapReference<mirror::Object>* field_addr =
1233 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1234 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1235 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1236 obj,
1237 MemberOffset(offset),
1238 field_addr);
1239 }
1240 bool success;
1241 // Check whether we're in a transaction, call accordingly.
1242 if (Runtime::Current()->IsActiveTransaction()) {
1243 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1244 expectedValue,
1245 newValue);
1246 } else {
1247 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1248 expectedValue,
1249 newValue);
1250 }
1251 result->SetZ(success ? 1 : 0);
1252}
1253
1254void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1255 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1256 // Argument 0 is the Unsafe instance, skip.
1257 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1258 if (obj == nullptr) {
1259 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1260 return;
1261 }
1262 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1263 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1264 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1265
1266 // Must use non transactional mode.
1267 if (kUseReadBarrier) {
1268 // Need to make sure the reference stored in the field is a to-space one before attempting the
1269 // CAS or the CAS could fail incorrectly.
1270 mirror::HeapReference<mirror::Object>* field_addr =
1271 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1272 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1273 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1274 obj,
1275 MemberOffset(offset),
1276 field_addr);
1277 }
1278 bool success;
1279 // Check whether we're in a transaction, call accordingly.
1280 if (Runtime::Current()->IsActiveTransaction()) {
1281 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1282 expected_value,
1283 newValue);
1284 } else {
1285 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1286 expected_value,
1287 newValue);
1288 }
1289 result->SetZ(success ? 1 : 0);
1290}
1291
1292void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1293 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1294 SHARED_REQUIRES(Locks::mutator_lock_) {
1295 // Argument 0 is the Unsafe instance, skip.
1296 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1297 if (obj == nullptr) {
1298 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1299 return;
1300 }
1301 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1302 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1303 result->SetL(value);
1304}
1305
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001306void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1307 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1308 SHARED_REQUIRES(Locks::mutator_lock_) {
1309 // Argument 0 is the Unsafe instance, skip.
1310 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1311 if (obj == nullptr) {
1312 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1313 return;
1314 }
1315 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1316 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1317 if (Runtime::Current()->IsActiveTransaction()) {
1318 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1319 } else {
1320 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1321 }
1322}
1323
Andreas Gampebc4d2182016-02-22 10:03:12 -08001324void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1325 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1326 SHARED_REQUIRES(Locks::mutator_lock_) {
1327 // Argument 0 is the Unsafe instance, skip.
1328 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1329 if (obj == nullptr) {
1330 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1331 return;
1332 }
1333 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1334 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1335 QuasiAtomic::ThreadFenceRelease();
1336 if (Runtime::Current()->IsActiveTransaction()) {
1337 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1338 } else {
1339 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1340 }
1341}
1342
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001343// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1344// of correctly handling the corner cases.
1345void UnstartedRuntime::UnstartedIntegerParseInt(
1346 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1347 SHARED_REQUIRES(Locks::mutator_lock_) {
1348 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1349 if (obj == nullptr) {
1350 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1351 return;
1352 }
1353
1354 std::string string_value = obj->AsString()->ToModifiedUtf8();
1355 if (string_value.empty()) {
1356 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1357 return;
1358 }
1359
1360 const char* c_str = string_value.c_str();
1361 char *end;
1362 // Can we set errno to 0? Is this always a variable, and not a macro?
1363 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1364 int64_t l = strtol(c_str, &end, 10);
1365
1366 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1367 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1368 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1369 return;
1370 }
1371 if (l == 0) {
1372 // Check whether the string wasn't exactly zero.
1373 if (string_value != "0") {
1374 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1375 return;
1376 }
1377 } else if (*end != '\0') {
1378 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1379 return;
1380 }
1381
1382 result->SetI(static_cast<int32_t>(l));
1383}
1384
1385// A cutout for Long.parseLong.
1386//
1387// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1388// well.
1389void UnstartedRuntime::UnstartedLongParseLong(
1390 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1391 SHARED_REQUIRES(Locks::mutator_lock_) {
1392 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1393 if (obj == nullptr) {
1394 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1395 return;
1396 }
1397
1398 std::string string_value = obj->AsString()->ToModifiedUtf8();
1399 if (string_value.empty()) {
1400 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1401 return;
1402 }
1403
1404 const char* c_str = string_value.c_str();
1405 char *end;
1406 // Can we set errno to 0? Is this always a variable, and not a macro?
1407 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1408 int64_t l = strtol(c_str, &end, 10);
1409
1410 // Note: comparing against int32_t min/max is intentional here.
1411 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1412 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1413 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1414 return;
1415 }
1416 if (l == 0) {
1417 // Check whether the string wasn't exactly zero.
1418 if (string_value != "0") {
1419 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1420 return;
1421 }
1422 } else if (*end != '\0') {
1423 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1424 return;
1425 }
1426
1427 result->SetJ(l);
1428}
1429
Andreas Gampe715fdc22016-04-18 17:07:30 -07001430void UnstartedRuntime::UnstartedMethodInvoke(
1431 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1432 SHARED_REQUIRES(Locks::mutator_lock_) {
1433 JNIEnvExt* env = self->GetJniEnv();
1434 ScopedObjectAccessUnchecked soa(self);
1435
1436 mirror::Object* java_method_obj = shadow_frame->GetVRegReference(arg_offset);
1437 ScopedLocalRef<jobject> java_method(env,
1438 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj));
1439
1440 mirror::Object* java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
1441 ScopedLocalRef<jobject> java_receiver(env,
1442 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1443
1444 mirror::Object* java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
1445 ScopedLocalRef<jobject> java_args(env,
1446 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1447
1448 ScopedLocalRef<jobject> result_jobj(env,
1449 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1450
1451 result->SetL(self->DecodeJObject(result_jobj.get()));
1452
1453 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1454 // InvocationTargetExceptions.
1455 if (self->IsExceptionPending()) {
1456 AbortTransactionOrFail(self, "Failed Method.invoke");
1457 }
1458}
1459
Andreas Gampebc4d2182016-02-22 10:03:12 -08001460
Mathieu Chartiere401d142015-04-22 13:56:20 -07001461void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1462 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1463 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001464 int32_t length = args[1];
1465 DCHECK_GE(length, 0);
1466 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1467 Runtime* runtime = Runtime::Current();
1468 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1469 DCHECK(array_class != nullptr);
1470 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1471 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1472 array_class->GetComponentSizeShift(), allocator));
1473}
1474
Mathieu Chartiere401d142015-04-22 13:56:20 -07001475void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1476 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1477 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001478 result->SetL(nullptr);
1479}
1480
Mathieu Chartiere401d142015-04-22 13:56:20 -07001481void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1482 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1483 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001484 NthCallerVisitor visitor(self, 3);
1485 visitor.WalkStack();
1486 if (visitor.caller != nullptr) {
1487 result->SetL(visitor.caller->GetDeclaringClass());
1488 }
1489}
1490
Mathieu Chartiere401d142015-04-22 13:56:20 -07001491void UnstartedRuntime::UnstartedJNIMathLog(
1492 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1493 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001494 JValue value;
1495 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1496 result->SetD(log(value.GetD()));
1497}
1498
Mathieu Chartiere401d142015-04-22 13:56:20 -07001499void UnstartedRuntime::UnstartedJNIMathExp(
1500 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1501 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001502 JValue value;
1503 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1504 result->SetD(exp(value.GetD()));
1505}
1506
Andreas Gampebc4d2182016-02-22 10:03:12 -08001507void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1508 Thread* self ATTRIBUTE_UNUSED,
1509 ArtMethod* method ATTRIBUTE_UNUSED,
1510 mirror::Object* receiver ATTRIBUTE_UNUSED,
1511 uint32_t* args ATTRIBUTE_UNUSED,
1512 JValue* result) {
1513 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1514 ? 0
1515 : 1);
1516}
1517
Mathieu Chartiere401d142015-04-22 13:56:20 -07001518void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1519 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1520 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001521 StackHandleScope<1> hs(self);
1522 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1523}
1524
Andreas Gampebc4d2182016-02-22 10:03:12 -08001525void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1526 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1527 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1528 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1529 result->SetD(bit_cast<double>(long_input));
1530}
1531
Mathieu Chartiere401d142015-04-22 13:56:20 -07001532void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1533 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1534 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001535 result->SetI(args[0]);
1536}
1537
Mathieu Chartiere401d142015-04-22 13:56:20 -07001538void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1539 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1540 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001541 result->SetI(args[0]);
1542}
1543
Mathieu Chartiere401d142015-04-22 13:56:20 -07001544void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1545 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1546 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001547 result->SetL(receiver->Clone(self));
1548}
1549
Mathieu Chartiere401d142015-04-22 13:56:20 -07001550void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1551 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1552 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001553 receiver->NotifyAll(self);
1554}
1555
Mathieu Chartiere401d142015-04-22 13:56:20 -07001556void UnstartedRuntime::UnstartedJNIStringCompareTo(
1557 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1558 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001559 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1560 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001561 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001562 }
1563 result->SetI(receiver->AsString()->CompareTo(rhs));
1564}
1565
Mathieu Chartiere401d142015-04-22 13:56:20 -07001566void UnstartedRuntime::UnstartedJNIStringIntern(
1567 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1568 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001569 result->SetL(receiver->AsString()->Intern());
1570}
1571
Mathieu Chartiere401d142015-04-22 13:56:20 -07001572void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1573 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1574 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001575 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1576}
1577
Mathieu Chartiere401d142015-04-22 13:56:20 -07001578void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1579 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1580 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001581 StackHandleScope<2> hs(self);
1582 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1583 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1584 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1585}
1586
Mathieu Chartiere401d142015-04-22 13:56:20 -07001587void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1588 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1589 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001590 int32_t length = static_cast<int32_t>(args[1]);
1591 if (length < 0) {
1592 ThrowNegativeArraySizeException(length);
1593 return;
1594 }
1595 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1596 Runtime* runtime = Runtime::Current();
1597 ClassLinker* class_linker = runtime->GetClassLinker();
1598 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001599 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001600 CHECK(self->IsExceptionPending());
1601 return;
1602 }
1603 DCHECK(array_class->IsObjectArrayClass());
1604 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1605 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1606 result->SetL(new_array);
1607}
1608
Mathieu Chartiere401d142015-04-22 13:56:20 -07001609void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1610 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1611 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001612 ScopedObjectAccessUnchecked soa(self);
1613 if (Runtime::Current()->IsActiveTransaction()) {
1614 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1615 } else {
1616 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1617 }
1618}
1619
Mathieu Chartiere401d142015-04-22 13:56:20 -07001620void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1621 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1622 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001623 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1624 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1625}
1626
Mathieu Chartiere401d142015-04-22 13:56:20 -07001627void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1628 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1629 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001630 result->SetZ(JNI_TRUE);
1631}
1632
Mathieu Chartiere401d142015-04-22 13:56:20 -07001633void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
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 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1638 jint expectedValue = args[3];
1639 jint newValue = args[4];
1640 bool success;
1641 if (Runtime::Current()->IsActiveTransaction()) {
1642 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1643 expectedValue, newValue);
1644 } else {
1645 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1646 expectedValue, newValue);
1647 }
1648 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1649}
1650
Narayan Kamath34a316f2016-03-30 13:11:18 +01001651void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1652 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1653 uint32_t* args, JValue* result) {
1654 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1655 if (obj == nullptr) {
1656 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1657 return;
1658 }
1659
1660 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1661 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1662}
1663
Mathieu Chartiere401d142015-04-22 13:56:20 -07001664void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1665 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1666 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001667 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1668 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1669 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1670 if (Runtime::Current()->IsActiveTransaction()) {
1671 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1672 } else {
1673 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1674 }
1675}
1676
Andreas Gampe799681b2015-05-15 19:24:12 -07001677void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001678 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1679 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001680 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1681 Primitive::Type primitive_type = component->GetPrimitiveType();
1682 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1683}
1684
Andreas Gampe799681b2015-05-15 19:24:12 -07001685void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001686 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1687 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001688 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1689 Primitive::Type primitive_type = component->GetPrimitiveType();
1690 result->SetI(Primitive::ComponentSize(primitive_type));
1691}
1692
Andreas Gampedd9d0552015-03-09 12:57:41 -07001693typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001694 size_t arg_size);
1695
Mathieu Chartiere401d142015-04-22 13:56:20 -07001696typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001697 uint32_t* args, JValue* result);
1698
1699static bool tables_initialized_ = false;
1700static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1701static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1702
Andreas Gampe799681b2015-05-15 19:24:12 -07001703void UnstartedRuntime::InitializeInvokeHandlers() {
1704#define UNSTARTED_DIRECT(ShortName, Sig) \
1705 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1706#include "unstarted_runtime_list.h"
1707 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1708#undef UNSTARTED_RUNTIME_DIRECT_LIST
1709#undef UNSTARTED_RUNTIME_JNI_LIST
1710#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001711}
1712
Andreas Gampe799681b2015-05-15 19:24:12 -07001713void UnstartedRuntime::InitializeJNIHandlers() {
1714#define UNSTARTED_JNI(ShortName, Sig) \
1715 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1716#include "unstarted_runtime_list.h"
1717 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1718#undef UNSTARTED_RUNTIME_DIRECT_LIST
1719#undef UNSTARTED_RUNTIME_JNI_LIST
1720#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001721}
1722
Andreas Gampe799681b2015-05-15 19:24:12 -07001723void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001724 CHECK(!tables_initialized_);
1725
Andreas Gampe799681b2015-05-15 19:24:12 -07001726 InitializeInvokeHandlers();
1727 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001728
1729 tables_initialized_ = true;
1730}
1731
Andreas Gampe799681b2015-05-15 19:24:12 -07001732void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1733 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001734 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1735 // problems in core libraries.
1736 CHECK(tables_initialized_);
1737
1738 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1739 const auto& iter = invoke_handlers_.find(name);
1740 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001741 // Clear out the result in case it's not zeroed out.
1742 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001743
1744 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1745 self->PushShadowFrame(shadow_frame);
1746
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001747 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001748
1749 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001750 } else {
1751 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001752 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001753 }
1754}
1755
1756// 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 -07001757void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001758 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001759 std::string name(PrettyMethod(method));
1760 const auto& iter = jni_handlers_.find(name);
1761 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001762 // Clear out the result in case it's not zeroed out.
1763 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001764 (*iter->second)(self, method, receiver, args, result);
1765 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001766 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1767 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001768 } else {
1769 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1770 "non-transactional runtime";
1771 }
1772}
1773
1774} // namespace interpreter
1775} // namespace art