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