blob: 4c7a97dfa8390af6a28c853b5b3a15850b712d67 [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 Gampe3d2fcaa2017-02-09 12:50:52 -080024#include <initializer_list>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070025#include <limits>
Andreas Gampe8ce9c302016-04-15 21:24:28 -070026#include <locale>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070027#include <unordered_map>
28
Andreas Gampe57943812017-12-06 21:39:13 -080029#include <android-base/logging.h>
30#include <android-base/stringprintf.h>
Andreas Gampeaacc25d2015-04-01 14:49:06 -070031
Mathieu Chartiere401d142015-04-22 13:56:20 -070032#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080033#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070034#include "base/enums.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "base/macros.h"
David Sehrc431b9d2018-03-02 12:01:51 -080036#include "base/quasi_atomic.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070037#include "class_linker.h"
38#include "common_throws.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080039#include "dex/descriptors_names.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070040#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080041#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070042#include "handle_scope-inl.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000043#include "hidden_api.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070044#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070045#include "jvalue-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070046#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070047#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070048#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010049#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070050#include "mirror/object-inl.h"
51#include "mirror/object_array-inl.h"
52#include "mirror/string-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070053#include "nativehelper/scoped_local_ref.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070054#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070055#include "reflection.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070056#include "thread-inl.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020057#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070058#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070059#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070060
61namespace art {
62namespace interpreter {
63
Andreas Gampe46ee31b2016-12-14 10:11:49 -080064using android::base::StringAppendV;
65using android::base::StringPrintf;
66
Andreas Gampe068b0c02015-03-11 12:44:47 -070067static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020068 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020070
71static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070073 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020074 va_start(args, fmt);
75 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070076 va_end(args);
77 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020078 va_start(args, fmt);
79 std::string msg;
80 StringAppendV(&msg, fmt, args);
81 va_end(args);
82 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070083 UNREACHABLE();
84 }
85}
86
Andreas Gampe8ce9c302016-04-15 21:24:28 -070087// Restricted support for character upper case / lower case. Only support ASCII, where
88// it's easy. Abort the transaction otherwise.
89static void CharacterLowerUpper(Thread* self,
90 ShadowFrame* shadow_frame,
91 JValue* result,
92 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070094 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
95
96 // Only ASCII (7-bit).
97 if (!isascii(int_value)) {
98 AbortTransactionOrFail(self,
99 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
100 int_value);
101 return;
102 }
103
104 std::locale c_locale("C");
105 char char_value = static_cast<char>(int_value);
106
107 if (to_lower_case) {
108 result->SetI(std::tolower(char_value, c_locale));
109 } else {
110 result->SetI(std::toupper(char_value, c_locale));
111 }
112}
113
114void UnstartedRuntime::UnstartedCharacterToLowerCase(
115 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
116 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
117}
118
119void UnstartedRuntime::UnstartedCharacterToUpperCase(
120 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
121 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
122}
123
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700124// Helper function to deal with class loading in an unstarted runtime.
125static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
126 Handle<mirror::ClassLoader> class_loader, JValue* result,
127 const std::string& method_name, bool initialize_class,
128 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800130 CHECK(className != nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700131 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
133
134 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
135 if (found == nullptr && abort_if_not_found) {
136 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700137 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700138 method_name.c_str(),
139 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700140 }
141 return;
142 }
143 if (found != nullptr && initialize_class) {
144 StackHandleScope<1> hs(self);
145 Handle<mirror::Class> h_class(hs.NewHandle(found));
146 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
147 CHECK(self->IsExceptionPending());
148 return;
149 }
150 }
151 result->SetL(found);
152}
153
154// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
155// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
156// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200157// actually the transaction abort exception. This must not be wrapped, as it signals an
158// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700159static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700161 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200162 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700163 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200164 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700165 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
166 "ClassNotFoundException");
167 }
168 }
169}
170
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700171static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700173 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
174 if (param == nullptr) {
175 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
176 return nullptr;
177 }
178 return param->AsString();
179}
180
David Brazdila02cb112018-01-31 11:36:39 +0000181template<typename T>
182static ALWAYS_INLINE bool ShouldBlockAccessToMember(T* member, ShadowFrame* frame)
183 REQUIRES_SHARED(Locks::mutator_lock_) {
184 return hiddenapi::ShouldBlockAccessToMember(
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000185 member,
186 frame->GetMethod()->GetDeclaringClass()->GetClassLoader(),
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000187 frame->GetMethod()->GetDeclaringClass()->GetDexCache(),
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000188 hiddenapi::kReflection); // all uses in this file are from reflection
David Brazdila02cb112018-01-31 11:36:39 +0000189}
190
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800191void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
192 ShadowFrame* shadow_frame,
193 JValue* result,
194 size_t arg_offset,
195 bool long_form,
196 const char* caller) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700197 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
198 if (class_name == nullptr) {
199 return;
200 }
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800201 bool initialize_class;
202 mirror::ClassLoader* class_loader;
203 if (long_form) {
204 initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
205 class_loader = down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
206 } else {
207 initialize_class = true;
208 // TODO: This is really only correct for the boot classpath, and for robustness we should
209 // check the caller.
210 class_loader = nullptr;
211 }
212
213 ScopedObjectAccessUnchecked soa(self);
214 if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
215 AbortTransactionOrFail(self,
216 "Only the boot classloader is supported: %s",
217 mirror::Object::PrettyTypeOf(class_loader).c_str());
218 return;
219 }
220
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700221 StackHandleScope<1> hs(self);
222 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800223 UnstartedRuntimeFindClass(self,
224 h_class_name,
225 ScopedNullHandle<mirror::ClassLoader>(),
226 result,
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800227 caller,
228 initialize_class,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800229 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700230 CheckExceptionGenerateClassNotFound(self);
231}
232
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800233void UnstartedRuntime::UnstartedClassForName(
234 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
235 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
236}
237
Andreas Gampe799681b2015-05-15 19:24:12 -0700238void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700239 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800240 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700241}
242
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000243void UnstartedRuntime::UnstartedClassGetPrimitiveClass(
244 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
245 ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
246 ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(class_name);
247 if (UNLIKELY(klass == nullptr)) {
248 DCHECK(self->IsExceptionPending());
249 AbortTransactionOrFail(self,
250 "Class.getPrimitiveClass() failed: %s",
251 self->GetException()->GetDetailMessage()->ToModifiedUtf8().c_str());
252 return;
253 }
254 result->SetL(klass);
255}
256
Andreas Gampe799681b2015-05-15 19:24:12 -0700257void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700258 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800259 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700260}
261
Andreas Gampe799681b2015-05-15 19:24:12 -0700262void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700263 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
264 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700265 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
266 if (param == nullptr) {
267 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
268 return;
269 }
270 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700271 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700272
273 // Check that it's not null.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800274 if (h_klass == nullptr) {
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700275 AbortTransactionOrFail(self, "Class reference is null for newInstance");
276 return;
277 }
278
279 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
280 if (Runtime::Current()->IsActiveTransaction()) {
281 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200282 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700283 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700284 return;
285 }
286 }
287
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700288 // There are two situations in which we'll abort this run.
289 // 1) If the class isn't yet initialized and initialization fails.
290 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
291 // Note that 2) could likely be handled here, but for safety abort the transaction.
292 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700293 auto* cl = Runtime::Current()->GetClassLinker();
294 if (cl->EnsureInitialized(self, h_klass, true, true)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000295 ArtMethod* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
David Brazdila02cb112018-01-31 11:36:39 +0000296 if (cons != nullptr && ShouldBlockAccessToMember(cons, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000297 cons = nullptr;
298 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700299 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700300 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800301 CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700303 if (!self->IsExceptionPending()) {
304 result->SetL(h_obj.Get());
305 ok = true;
306 }
307 } else {
308 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
309 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700310 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700311 }
312 }
313 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700314 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700315 h_klass->PrettyClass().c_str(),
316 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700317 }
318}
319
Andreas Gampe799681b2015-05-15 19:24:12 -0700320void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700322 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
323 // going the reflective Dex way.
324 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
325 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700326 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700327 for (ArtField& field : klass->GetIFields()) {
328 if (name2->Equals(field.GetName())) {
329 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700330 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700331 }
332 }
333 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700334 for (ArtField& field : klass->GetSFields()) {
335 if (name2->Equals(field.GetName())) {
336 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700337 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700338 }
339 }
340 }
David Brazdila02cb112018-01-31 11:36:39 +0000341 if (found != nullptr && ShouldBlockAccessToMember(found, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000342 found = nullptr;
343 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700344 if (found == nullptr) {
345 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
346 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700347 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700348 return;
349 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700350 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700351 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700352 mirror::Field* field;
353 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700354 if (pointer_size == PointerSize::k64) {
355 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
356 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700357 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700358 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
359 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700360 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700361 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700362 if (pointer_size == PointerSize::k64) {
363 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
364 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700365 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700366 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
367 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700368 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700369 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700370 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700371}
372
Andreas Gampebc4d2182016-02-22 10:03:12 -0800373// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
374void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
375 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
376 // Special managed code cut-out to allow method lookup in a un-started runtime.
377 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
378 if (klass == nullptr) {
379 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
380 return;
381 }
382 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
383 mirror::ObjectArray<mirror::Class>* args =
384 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700385 Runtime* runtime = Runtime::Current();
386 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700387 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700388 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700389 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700390 if (pointer_size == PointerSize::k64) {
391 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
392 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700393 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700394 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
395 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700396 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800397 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700398 if (pointer_size == PointerSize::k64) {
399 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
400 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700401 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700402 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
403 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700404 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800405 }
David Brazdila02cb112018-01-31 11:36:39 +0000406 if (method != nullptr && ShouldBlockAccessToMember(method->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000407 method = nullptr;
408 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700409 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800410}
411
Andreas Gampe6039e562016-04-05 18:18:43 -0700412// Special managed code cut-out to allow constructor lookup in a un-started runtime.
413void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
414 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
415 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
416 if (klass == nullptr) {
417 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
418 return;
419 }
420 mirror::ObjectArray<mirror::Class>* args =
421 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700422 Runtime* runtime = Runtime::Current();
423 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700424 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700425 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700426 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700427 if (pointer_size == PointerSize::k64) {
428 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
429 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700430 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700431 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
432 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700433 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700434 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700435 if (pointer_size == PointerSize::k64) {
436 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
437 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700438 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700439 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
440 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700441 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700442 }
David Brazdil5a61bb72018-01-19 16:59:46 +0000443 if (constructor != nullptr &&
David Brazdila02cb112018-01-31 11:36:39 +0000444 ShouldBlockAccessToMember(constructor->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000445 constructor = nullptr;
446 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700447 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700448}
449
Andreas Gampeae78c262017-02-01 20:40:44 -0800450void UnstartedRuntime::UnstartedClassGetDeclaringClass(
451 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
452 StackHandleScope<1> hs(self);
453 Handle<mirror::Class> klass(hs.NewHandle(
454 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
455 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
456 result->SetL(nullptr);
457 return;
458 }
459 // Return null for anonymous classes.
460 JValue is_anon_result;
461 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
462 if (is_anon_result.GetZ() != 0) {
463 result->SetL(nullptr);
464 return;
465 }
466 result->SetL(annotations::GetDeclaringClass(klass));
467}
468
Andreas Gampe633750c2016-02-19 10:49:50 -0800469void UnstartedRuntime::UnstartedClassGetEnclosingClass(
470 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
471 StackHandleScope<1> hs(self);
472 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
473 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
474 result->SetL(nullptr);
475 }
David Sehr9323e6e2016-09-13 08:58:35 -0700476 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800477}
478
Andreas Gampe715fdc22016-04-18 17:07:30 -0700479void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
480 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
481 StackHandleScope<1> hs(self);
482 Handle<mirror::Class> klass(hs.NewHandle(
483 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
484 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
485 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
486}
487
Andreas Gampe9486a162017-02-16 15:17:47 -0800488void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
489 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
490 StackHandleScope<1> hs(self);
491 Handle<mirror::Class> klass(hs.NewHandle(
492 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
493
494 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
495 result->SetL(nullptr);
496 return;
497 }
498
499 result->SetL(annotations::GetSignatureAnnotationForClass(klass));
500}
501
Andreas Gampeae78c262017-02-01 20:40:44 -0800502void UnstartedRuntime::UnstartedClassIsAnonymousClass(
503 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
504 StackHandleScope<1> hs(self);
505 Handle<mirror::Class> klass(hs.NewHandle(
506 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
507 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
508 result->SetZ(false);
509 return;
510 }
511 mirror::String* class_name = nullptr;
512 if (!annotations::GetInnerClass(klass, &class_name)) {
513 result->SetZ(false);
514 return;
515 }
516 result->SetZ(class_name == nullptr);
517}
518
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700519static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file,
520 const char* entry_name,
521 size_t* size,
522 std::string* error_msg) {
523 CHECK(size != nullptr);
524
525 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
526 if (zip_archive == nullptr) {
Mathieu Chartier6beced42016-11-15 15:51:31 -0800527 return nullptr;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700528 }
529 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
530 if (zip_entry == nullptr) {
531 return nullptr;
532 }
533 std::unique_ptr<MemMap> tmp_map(
534 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg));
535 if (tmp_map == nullptr) {
536 return nullptr;
537 }
538
539 // OK, from here everything seems fine.
540 *size = zip_entry->GetUncompressedLength();
541 return tmp_map;
542}
543
544static void GetResourceAsStream(Thread* self,
545 ShadowFrame* shadow_frame,
546 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700547 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700548 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
549 if (resource_obj == nullptr) {
550 AbortTransactionOrFail(self, "null name for getResourceAsStream");
551 return;
552 }
553 CHECK(resource_obj->IsString());
554 mirror::String* resource_name = resource_obj->AsString();
555
556 std::string resource_name_str = resource_name->ToModifiedUtf8();
557 if (resource_name_str.empty() || resource_name_str == "/") {
558 AbortTransactionOrFail(self,
559 "Unsupported name %s for getResourceAsStream",
560 resource_name_str.c_str());
561 return;
562 }
563 const char* resource_cstr = resource_name_str.c_str();
564 if (resource_cstr[0] == '/') {
565 resource_cstr++;
566 }
567
568 Runtime* runtime = Runtime::Current();
569
570 std::vector<std::string> split;
571 Split(runtime->GetBootClassPathString(), ':', &split);
572 if (split.empty()) {
573 AbortTransactionOrFail(self,
574 "Boot classpath not set or split error:: %s",
575 runtime->GetBootClassPathString().c_str());
576 return;
577 }
578
579 std::unique_ptr<MemMap> mem_map;
580 size_t map_size;
581 std::string last_error_msg; // Only store the last message (we could concatenate).
582
583 for (const std::string& jar_file : split) {
584 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
585 if (mem_map != nullptr) {
586 break;
587 }
588 }
589
590 if (mem_map == nullptr) {
591 // Didn't find it. There's a good chance this will be the same at runtime, but still
592 // conservatively abort the transaction here.
593 AbortTransactionOrFail(self,
594 "Could not find resource %s. Last error was %s.",
595 resource_name_str.c_str(),
596 last_error_msg.c_str());
597 return;
598 }
599
600 StackHandleScope<3> hs(self);
601
602 // Create byte array for content.
603 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800604 if (h_array == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700605 AbortTransactionOrFail(self, "Could not find/create byte array class");
606 return;
607 }
608 // Copy in content.
609 memcpy(h_array->GetData(), mem_map->Begin(), map_size);
610 // Be proactive releasing memory.
Andreas Gampeeac4f282017-04-26 21:07:04 -0700611 mem_map.reset();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700612
613 // Create a ByteArrayInputStream.
614 Handle<mirror::Class> h_class(hs.NewHandle(
615 runtime->GetClassLinker()->FindClass(self,
616 "Ljava/io/ByteArrayInputStream;",
617 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800618 if (h_class == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700619 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
620 return;
621 }
622 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
623 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
624 return;
625 }
626
627 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800628 if (h_obj == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700629 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
630 return;
631 }
632
633 auto* cl = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100634 ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700635 if (constructor == nullptr) {
636 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
637 return;
638 }
639
640 uint32_t args[1];
641 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get()));
642 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
643
644 if (self->IsExceptionPending()) {
645 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
646 return;
647 }
648
649 result->SetL(h_obj.Get());
650}
651
652void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
653 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
654 {
655 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
656 CHECK(this_obj != nullptr);
657 CHECK(this_obj->IsClassLoader());
658
659 StackHandleScope<1> hs(self);
660 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
661
662 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
663 this_classloader_class.Get()) {
664 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700665 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700666 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700667 return;
668 }
669 }
670
671 GetResourceAsStream(self, shadow_frame, result, arg_offset);
672}
673
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800674void UnstartedRuntime::UnstartedConstructorNewInstance0(
675 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
676 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
677 StackHandleScope<4> hs(self);
678 Handle<mirror::Constructor> m = hs.NewHandle(
679 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
680 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
681 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
682 shadow_frame->GetVRegReference(arg_offset + 1)));
683 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
684 if (UNLIKELY(c->IsAbstract())) {
685 AbortTransactionOrFail(self, "Cannot handle abstract classes");
686 return;
687 }
688 // Verify that we can access the class.
689 if (!m->IsAccessible() && !c->IsPublic()) {
690 // Go 2 frames back, this method is always called from newInstance0, which is called from
691 // Constructor.newInstance(Object... args).
692 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
693 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
694 // access checks anyways. TODO: Investigate if this the correct behavior.
695 if (caller != nullptr && !caller->CanAccess(c.Get())) {
696 AbortTransactionOrFail(self, "Cannot access class");
697 return;
698 }
699 }
700 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
701 DCHECK(self->IsExceptionPending());
702 return;
703 }
704 if (c->IsClassClass()) {
705 AbortTransactionOrFail(self, "new Class() is not supported");
706 return;
707 }
708
709 // String constructor is replaced by a StringFactory method in InvokeMethod.
710 if (c->IsStringClass()) {
711 // We don't support strings.
712 AbortTransactionOrFail(self, "String construction is not supported");
713 return;
714 }
715
716 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
717 if (receiver == nullptr) {
718 AbortTransactionOrFail(self, "Could not allocate");
719 return;
720 }
721
722 // It's easier to use reflection to make the call, than create the uint32_t array.
723 {
724 ScopedObjectAccessUnchecked soa(self);
725 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
726 soa.AddLocalReference<jobject>(m.Get()));
727 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
728 soa.AddLocalReference<jobject>(receiver.Get()));
729 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
730 soa.AddLocalReference<jobject>(args.Get()));
731 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
732 }
733 if (self->IsExceptionPending()) {
734 AbortTransactionOrFail(self, "Failed running constructor");
735 } else {
736 result->SetL(receiver.Get());
737 }
738}
739
Andreas Gampe799681b2015-05-15 19:24:12 -0700740void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700741 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700742 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
743 mirror::ClassLoader* class_loader =
744 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
745 StackHandleScope<2> hs(self);
746 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
747 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
748 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
749 "VMClassLoader.findLoadedClass", false, false);
750 // This might have an error pending. But semantics are to just return null.
751 if (self->IsExceptionPending()) {
752 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700753 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700754 if (type != "java.lang.InternalError") {
755 self->ClearException();
756 }
757 }
758}
759
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700760// Arraycopy emulation.
761// Note: we can't use any fast copy functions, as they are not available under transaction.
762
763template <typename T>
764static void PrimitiveArrayCopy(Thread* self,
765 mirror::Array* src_array, int32_t src_pos,
766 mirror::Array* dst_array, int32_t dst_pos,
767 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700768 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700769 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700770 AbortTransactionOrFail(self,
771 "Types mismatched in arraycopy: %s vs %s.",
772 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700773 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700774 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700775 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700776 return;
777 }
778 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
779 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
780 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
781 if (copy_forward) {
782 for (int32_t i = 0; i < length; ++i) {
783 dst->Set(dst_pos + i, src->Get(src_pos + i));
784 }
785 } else {
786 for (int32_t i = 1; i <= length; ++i) {
787 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
788 }
789 }
790}
791
Andreas Gampe799681b2015-05-15 19:24:12 -0700792void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700794 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700795 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
796 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700797 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700798
Andreas Gampe85a098a2016-03-31 13:30:53 -0700799 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
800 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
801 // Null checking. For simplicity, abort transaction.
802 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700803 AbortTransactionOrFail(self, "src is null in arraycopy.");
804 return;
805 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700806 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700807 AbortTransactionOrFail(self, "dst is null in arraycopy.");
808 return;
809 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700810 // Test for arrayness. Throw ArrayStoreException.
811 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
812 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
813 return;
814 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700815
Andreas Gampe85a098a2016-03-31 13:30:53 -0700816 mirror::Array* src_array = src_obj->AsArray();
817 mirror::Array* dst_array = dst_obj->AsArray();
818
819 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700820 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
821 UNLIKELY(src_pos > src_array->GetLength() - length) ||
822 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700823 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700824 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
825 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
826 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700827 return;
828 }
829
830 // Type checking.
831 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
832 GetComponentType();
833
834 if (!src_type->IsPrimitive()) {
835 // Check that the second type is not primitive.
836 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
837 GetComponentType();
838 if (trg_type->IsPrimitiveInt()) {
839 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700840 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700841 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700842 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700843 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700844 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700845 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700846
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700847 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
848 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
849 if (src == dst) {
850 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700851 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700852 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
853 if (copy_forward) {
854 for (int32_t i = 0; i < length; ++i) {
855 dst->Set(dst_pos + i, src->Get(src_pos + i));
856 }
857 } else {
858 for (int32_t i = 1; i <= length; ++i) {
859 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
860 }
861 }
862 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700863 // We're being lazy here. Optimally this could be a memcpy (if component types are
864 // assignable), but the ObjectArray implementation doesn't support transactions. The
865 // checking version, however, does.
866 if (Runtime::Current()->IsActiveTransaction()) {
867 dst->AssignableCheckingMemcpy<true>(
868 dst_pos, src, src_pos, length, true /* throw_exception */);
869 } else {
870 dst->AssignableCheckingMemcpy<false>(
871 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700872 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700873 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700874 } else if (src_type->IsPrimitiveByte()) {
875 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700876 } else if (src_type->IsPrimitiveChar()) {
877 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
878 } else if (src_type->IsPrimitiveInt()) {
879 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700880 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700881 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700882 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700883 }
884}
885
Andreas Gampe5c9af612016-04-05 14:16:10 -0700886void UnstartedRuntime::UnstartedSystemArraycopyByte(
887 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
888 // Just forward.
889 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
890}
891
Andreas Gampe799681b2015-05-15 19:24:12 -0700892void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700894 // Just forward.
895 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
896}
897
898void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700900 // Just forward.
901 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
902}
903
Narayan Kamath34a316f2016-03-30 13:11:18 +0100904void UnstartedRuntime::UnstartedSystemGetSecurityManager(
905 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
906 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
907 result->SetL(nullptr);
908}
909
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700910static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
911
912static void GetSystemProperty(Thread* self,
913 ShadowFrame* shadow_frame,
914 JValue* result,
915 size_t arg_offset,
916 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700917 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700918 StackHandleScope<4> hs(self);
919 Handle<mirror::String> h_key(
920 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800921 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700922 AbortTransactionOrFail(self, "getProperty key was null");
923 return;
924 }
925
926 // This is overall inefficient, but reflecting the values here is not great, either. So
927 // for simplicity, and with the assumption that the number of getProperty calls is not
928 // too great, just iterate each time.
929
930 // Get the storage class.
931 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
932 Handle<mirror::Class> h_props_class(hs.NewHandle(
933 class_linker->FindClass(self,
934 "Ljava/lang/AndroidHardcodedSystemProperties;",
935 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800936 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700937 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
938 return;
939 }
940 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
941 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
942 return;
943 }
944
945 // Get the storage array.
946 ArtField* static_properties =
947 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
948 "[[Ljava/lang/String;");
949 if (static_properties == nullptr) {
950 AbortTransactionOrFail(self,
951 "Could not find %s field",
952 kAndroidHardcodedSystemPropertiesFieldName);
953 return;
954 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700955 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
956 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
957 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800958 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700959 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
960 return;
961 }
962
963 // Iterate over it.
964 const int32_t prop_count = h_2string_array->GetLength();
965 // Use the third handle as mutable.
966 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
967 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
968 for (int32_t i = 0; i < prop_count; ++i) {
969 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800970 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700971 h_string_array->GetLength() != 2 ||
972 h_string_array->Get(0) == nullptr) {
973 AbortTransactionOrFail(self,
974 "Unexpected content of %s",
975 kAndroidHardcodedSystemPropertiesFieldName);
976 return;
977 }
978 if (h_key->Equals(h_string_array->Get(0))) {
979 // Found a value.
980 if (h_string_array->Get(1) == nullptr && is_default_version) {
981 // Null is being delegated to the default map, and then resolved to the given default value.
982 // As there's no default map, return the given value.
983 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
984 } else {
985 result->SetL(h_string_array->Get(1));
986 }
987 return;
988 }
989 }
990
991 // Key is not supported.
992 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
993}
994
995void UnstartedRuntime::UnstartedSystemGetProperty(
996 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
997 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
998}
999
1000void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
1001 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1002 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
1003}
1004
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001005static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
1006 REQUIRES_SHARED(Locks::mutator_lock_) {
1007 if (shadow_frame->GetLink() == nullptr) {
1008 return "<no caller>";
1009 }
1010 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1011}
1012
1013static bool CheckCallers(ShadowFrame* shadow_frame,
1014 std::initializer_list<std::string> allowed_call_stack)
1015 REQUIRES_SHARED(Locks::mutator_lock_) {
1016 for (const std::string& allowed_caller : allowed_call_stack) {
1017 if (shadow_frame->GetLink() == nullptr) {
1018 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001019 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001020
1021 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1022 if (allowed_caller != found_caller) {
1023 return false;
1024 }
1025
1026 shadow_frame = shadow_frame->GetLink();
1027 }
1028 return true;
1029}
1030
1031static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
1032 REQUIRES_SHARED(Locks::mutator_lock_) {
1033 // Find the requested class.
1034 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1035 ObjPtr<mirror::Class> klass =
1036 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1037 if (klass == nullptr) {
1038 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1039 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001040 }
1041
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001042 StackHandleScope<2> hs(self);
1043 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1044 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001045 if (h_obj != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01001046 ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001047 if (init_method == nullptr) {
1048 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1049 return nullptr;
1050 } else {
1051 JValue invoke_result;
1052 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1053 if (!self->IsExceptionPending()) {
1054 return h_obj.Get();
1055 }
1056 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1057 }
1058 }
1059 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1060 return nullptr;
1061}
1062
1063void UnstartedRuntime::UnstartedThreadLocalGet(
1064 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1065 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1066 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1067 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1068 } else {
1069 AbortTransactionOrFail(self,
1070 "ThreadLocal.get() does not support %s",
1071 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001072 }
1073}
1074
Andreas Gampebad529d2017-02-13 18:52:10 -08001075void UnstartedRuntime::UnstartedThreadCurrentThread(
1076 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1077 if (CheckCallers(shadow_frame,
1078 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1079 "java.lang.String, long)",
1080 "void java.lang.Thread.<init>()",
1081 "void java.util.logging.LogManager$Cleaner.<init>("
1082 "java.util.logging.LogManager)" })) {
1083 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1084 // Thread constructor only asks for the current thread to set up defaults and add the
1085 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1086 // these purposes.
1087 Runtime::Current()->InitThreadGroups(self);
1088 jobject main_peer =
1089 self->CreateCompileTimePeer(self->GetJniEnv(),
1090 "main",
1091 false,
1092 Runtime::Current()->GetMainThreadGroup());
1093 if (main_peer == nullptr) {
1094 AbortTransactionOrFail(self, "Failed allocating peer");
1095 return;
1096 }
1097
1098 result->SetL(self->DecodeJObject(main_peer));
1099 self->GetJniEnv()->DeleteLocalRef(main_peer);
1100 } else {
1101 AbortTransactionOrFail(self,
1102 "Thread.currentThread() does not support %s",
1103 GetImmediateCaller(shadow_frame).c_str());
1104 }
1105}
1106
1107void UnstartedRuntime::UnstartedThreadGetNativeState(
1108 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1109 if (CheckCallers(shadow_frame,
1110 { "java.lang.Thread$State java.lang.Thread.getState()",
1111 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1112 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
1113 "java.lang.String, long)",
1114 "void java.lang.Thread.<init>()",
1115 "void java.util.logging.LogManager$Cleaner.<init>("
1116 "java.util.logging.LogManager)" })) {
1117 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1118 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1119 constexpr int32_t kJavaRunnable = 1;
1120 result->SetI(kJavaRunnable);
1121 } else {
1122 AbortTransactionOrFail(self,
1123 "Thread.getNativeState() does not support %s",
1124 GetImmediateCaller(shadow_frame).c_str());
1125 }
1126}
1127
Sergio Giro83261202016-04-11 20:49:20 +01001128void UnstartedRuntime::UnstartedMathCeil(
1129 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001130 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001131}
1132
1133void UnstartedRuntime::UnstartedMathFloor(
1134 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001135 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001136}
1137
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001138void UnstartedRuntime::UnstartedMathSin(
1139 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1140 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1141}
1142
1143void UnstartedRuntime::UnstartedMathCos(
1144 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1145 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1146}
1147
1148void UnstartedRuntime::UnstartedMathPow(
1149 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1150 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1151 shadow_frame->GetVRegDouble(arg_offset + 2)));
1152}
1153
Andreas Gampe799681b2015-05-15 19:24:12 -07001154void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001155 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001156 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1157 result->SetI(obj->IdentityHashCode());
1158}
1159
Andreas Gampe799681b2015-05-15 19:24:12 -07001160void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001161 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001162 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001163 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001164}
1165
Andreas Gampedd9d0552015-03-09 12:57:41 -07001166static void UnstartedMemoryPeek(
1167 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1168 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1169 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1170 // aborting the transaction.
1171
1172 switch (type) {
1173 case Primitive::kPrimByte: {
1174 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1175 return;
1176 }
1177
1178 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001179 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
1180 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001181 return;
1182 }
1183
1184 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001185 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
1186 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001187 return;
1188 }
1189
1190 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -07001191 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
1192 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001193 return;
1194 }
1195
1196 case Primitive::kPrimBoolean:
1197 case Primitive::kPrimChar:
1198 case Primitive::kPrimFloat:
1199 case Primitive::kPrimDouble:
1200 case Primitive::kPrimVoid:
1201 case Primitive::kPrimNot:
1202 LOG(FATAL) << "Not in the Memory API: " << type;
1203 UNREACHABLE();
1204 }
1205 LOG(FATAL) << "Should not reach here";
1206 UNREACHABLE();
1207}
1208
Andreas Gampe799681b2015-05-15 19:24:12 -07001209void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001210 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001211 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1212}
1213
1214void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001215 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001216 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1217}
1218
1219void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001220 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001221 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1222}
1223
1224void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001225 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001226 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001227}
1228
1229static void UnstartedMemoryPeekArray(
1230 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001231 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001232 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1233 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1234 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001235 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001236 return;
1237 }
1238 mirror::Array* array = obj->AsArray();
1239
1240 int offset = shadow_frame->GetVReg(arg_offset + 3);
1241 int count = shadow_frame->GetVReg(arg_offset + 4);
1242 if (offset < 0 || offset + count > array->GetLength()) {
1243 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1244 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001245 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001246 return;
1247 }
1248
1249 switch (type) {
1250 case Primitive::kPrimByte: {
1251 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1252 mirror::ByteArray* byte_array = array->AsByteArray();
1253 for (int32_t i = 0; i < count; ++i, ++address) {
1254 byte_array->SetWithoutChecks<true>(i + offset, *address);
1255 }
1256 return;
1257 }
1258
1259 case Primitive::kPrimShort:
1260 case Primitive::kPrimInt:
1261 case Primitive::kPrimLong:
1262 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1263 UNREACHABLE();
1264
1265 case Primitive::kPrimBoolean:
1266 case Primitive::kPrimChar:
1267 case Primitive::kPrimFloat:
1268 case Primitive::kPrimDouble:
1269 case Primitive::kPrimVoid:
1270 case Primitive::kPrimNot:
1271 LOG(FATAL) << "Not in the Memory API: " << type;
1272 UNREACHABLE();
1273 }
1274 LOG(FATAL) << "Should not reach here";
1275 UNREACHABLE();
1276}
1277
Andreas Gampe799681b2015-05-15 19:24:12 -07001278void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001279 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001280 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001281}
1282
Kenny Root1c9e61c2015-05-14 15:58:17 -07001283// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001284void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001285 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001286 jint start = shadow_frame->GetVReg(arg_offset + 1);
1287 jint end = shadow_frame->GetVReg(arg_offset + 2);
1288 jint index = shadow_frame->GetVReg(arg_offset + 4);
1289 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1290 if (string == nullptr) {
1291 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1292 return;
1293 }
Kenny Root57f91e82015-05-14 15:58:17 -07001294 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001295 DCHECK_LE(start, end);
1296 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001297 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001298 Handle<mirror::CharArray> h_char_array(
1299 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001300 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001301 DCHECK_LE(index, h_char_array->GetLength());
1302 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001303 string->GetChars(start, end, h_char_array, index);
1304}
1305
1306// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001307void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001309 jint index = shadow_frame->GetVReg(arg_offset + 1);
1310 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1311 if (string == nullptr) {
1312 AbortTransactionOrFail(self, "String.charAt with null object");
1313 return;
1314 }
1315 result->SetC(string->CharAt(index));
1316}
1317
Vladimir Marko92907f32017-02-20 14:08:30 +00001318// This allows creating String objects with replaced characters during compilation.
1319// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1320void UnstartedRuntime::UnstartedStringDoReplace(
1321 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1322 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1323 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001324 StackHandleScope<1> hs(self);
1325 Handle<mirror::String> string =
1326 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
Kenny Root57f91e82015-05-14 15:58:17 -07001327 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001328 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001329 return;
1330 }
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001331 result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001332}
1333
Kenny Root1c9e61c2015-05-14 15:58:17 -07001334// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001335void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001336 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001337 jint offset = shadow_frame->GetVReg(arg_offset);
1338 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1339 DCHECK_GE(char_count, 0);
1340 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001341 Handle<mirror::CharArray> h_char_array(
1342 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001343 Runtime* runtime = Runtime::Current();
1344 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1345 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1346}
1347
1348// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001349void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001350 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001351 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1352 if (to_copy == nullptr) {
1353 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1354 return;
1355 }
1356 StackHandleScope<1> hs(self);
1357 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1358 Runtime* runtime = Runtime::Current();
1359 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1360 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1361 allocator));
1362}
1363
Andreas Gampe799681b2015-05-15 19:24:12 -07001364void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001365 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001366 jint start = shadow_frame->GetVReg(arg_offset + 1);
1367 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001368 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001369 DCHECK_GE(length, 0);
1370 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001371 Handle<mirror::String> h_string(
1372 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001373 DCHECK_LE(start, h_string->GetLength());
1374 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001375 Runtime* runtime = Runtime::Current();
1376 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1377 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1378}
1379
Kenny Root57f91e82015-05-14 15:58:17 -07001380// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001381void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001382 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001383 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001384 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1385 if (string == nullptr) {
1386 AbortTransactionOrFail(self, "String.charAt with null object");
1387 return;
1388 }
1389 result->SetL(string->ToCharArray(self));
1390}
1391
Andreas Gampebc4d2182016-02-22 10:03:12 -08001392// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1393void UnstartedRuntime::UnstartedReferenceGetReferent(
1394 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001395 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001396 shadow_frame->GetVRegReference(arg_offset));
1397 if (ref == nullptr) {
1398 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1399 return;
1400 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001401 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001402 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1403 result->SetL(referent);
1404}
1405
1406// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1407// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1408// where we can predict the behavior (somewhat).
1409// Note: this is required (instead of lazy initialization) as these classes are used in the static
1410// initialization of other classes, so will *use* the value.
1411void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1412 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001413 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001414 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1415 // 8 as a conservative upper approximation.
1416 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001417 } else if (CheckCallers(shadow_frame,
1418 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001419 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1420 // a good upper bound.
1421 // TODO: Consider resetting in the zygote?
1422 result->SetI(8);
1423 } else {
1424 // Not supported.
1425 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1426 }
1427}
1428
1429// This allows accessing ConcurrentHashMap/SynchronousQueue.
1430
1431void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1432 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1433 // Argument 0 is the Unsafe instance, skip.
1434 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1435 if (obj == nullptr) {
1436 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1437 return;
1438 }
1439 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1440 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1441 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001442 bool success;
1443 // Check whether we're in a transaction, call accordingly.
1444 if (Runtime::Current()->IsActiveTransaction()) {
1445 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1446 expectedValue,
1447 newValue);
1448 } else {
1449 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1450 expectedValue,
1451 newValue);
1452 }
1453 result->SetZ(success ? 1 : 0);
1454}
1455
1456void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1457 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1458 // Argument 0 is the Unsafe instance, skip.
1459 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1460 if (obj == nullptr) {
1461 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1462 return;
1463 }
1464 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1465 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1466 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1467
1468 // Must use non transactional mode.
1469 if (kUseReadBarrier) {
1470 // Need to make sure the reference stored in the field is a to-space one before attempting the
1471 // CAS or the CAS could fail incorrectly.
1472 mirror::HeapReference<mirror::Object>* field_addr =
1473 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1474 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001475 ReadBarrier::Barrier<
1476 mirror::Object,
1477 /* kIsVolatile */ false,
1478 kWithReadBarrier,
1479 /* kAlwaysUpdateField */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001480 obj,
1481 MemberOffset(offset),
1482 field_addr);
1483 }
1484 bool success;
1485 // Check whether we're in a transaction, call accordingly.
1486 if (Runtime::Current()->IsActiveTransaction()) {
1487 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1488 expected_value,
1489 newValue);
1490 } else {
1491 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1492 expected_value,
1493 newValue);
1494 }
1495 result->SetZ(success ? 1 : 0);
1496}
1497
1498void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1499 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001500 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001501 // Argument 0 is the Unsafe instance, skip.
1502 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1503 if (obj == nullptr) {
1504 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1505 return;
1506 }
1507 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1508 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1509 result->SetL(value);
1510}
1511
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001512void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1513 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001514 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001515 // Argument 0 is the Unsafe instance, skip.
1516 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1517 if (obj == nullptr) {
1518 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1519 return;
1520 }
1521 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1522 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1523 if (Runtime::Current()->IsActiveTransaction()) {
1524 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1525 } else {
1526 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1527 }
1528}
1529
Andreas Gampebc4d2182016-02-22 10:03:12 -08001530void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1531 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001532 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001533 // Argument 0 is the Unsafe instance, skip.
1534 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1535 if (obj == nullptr) {
1536 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1537 return;
1538 }
1539 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1540 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
Orion Hodson27b96762018-03-13 16:06:57 +00001541 std::atomic_thread_fence(std::memory_order_release);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001542 if (Runtime::Current()->IsActiveTransaction()) {
1543 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1544 } else {
1545 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1546 }
1547}
1548
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001549// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1550// of correctly handling the corner cases.
1551void UnstartedRuntime::UnstartedIntegerParseInt(
1552 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001553 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001554 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1555 if (obj == nullptr) {
1556 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1557 return;
1558 }
1559
1560 std::string string_value = obj->AsString()->ToModifiedUtf8();
1561 if (string_value.empty()) {
1562 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1563 return;
1564 }
1565
1566 const char* c_str = string_value.c_str();
1567 char *end;
1568 // Can we set errno to 0? Is this always a variable, and not a macro?
1569 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1570 int64_t l = strtol(c_str, &end, 10);
1571
1572 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1573 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1574 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1575 return;
1576 }
1577 if (l == 0) {
1578 // Check whether the string wasn't exactly zero.
1579 if (string_value != "0") {
1580 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1581 return;
1582 }
1583 } else if (*end != '\0') {
1584 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1585 return;
1586 }
1587
1588 result->SetI(static_cast<int32_t>(l));
1589}
1590
1591// A cutout for Long.parseLong.
1592//
1593// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1594// well.
1595void UnstartedRuntime::UnstartedLongParseLong(
1596 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001597 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001598 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1599 if (obj == nullptr) {
1600 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1601 return;
1602 }
1603
1604 std::string string_value = obj->AsString()->ToModifiedUtf8();
1605 if (string_value.empty()) {
1606 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1607 return;
1608 }
1609
1610 const char* c_str = string_value.c_str();
1611 char *end;
1612 // Can we set errno to 0? Is this always a variable, and not a macro?
1613 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1614 int64_t l = strtol(c_str, &end, 10);
1615
1616 // Note: comparing against int32_t min/max is intentional here.
1617 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1618 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1619 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1620 return;
1621 }
1622 if (l == 0) {
1623 // Check whether the string wasn't exactly zero.
1624 if (string_value != "0") {
1625 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1626 return;
1627 }
1628 } else if (*end != '\0') {
1629 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1630 return;
1631 }
1632
1633 result->SetJ(l);
1634}
1635
Andreas Gampe715fdc22016-04-18 17:07:30 -07001636void UnstartedRuntime::UnstartedMethodInvoke(
1637 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001638 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001639 JNIEnvExt* env = self->GetJniEnv();
1640 ScopedObjectAccessUnchecked soa(self);
1641
Mathieu Chartier8778c522016-10-04 19:06:30 -07001642 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001643 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001644 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001645
Mathieu Chartier8778c522016-10-04 19:06:30 -07001646 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001647 ScopedLocalRef<jobject> java_receiver(env,
1648 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1649
Mathieu Chartier8778c522016-10-04 19:06:30 -07001650 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001651 ScopedLocalRef<jobject> java_args(env,
1652 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1653
1654 ScopedLocalRef<jobject> result_jobj(env,
1655 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1656
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001657 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001658
1659 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1660 // InvocationTargetExceptions.
1661 if (self->IsExceptionPending()) {
1662 AbortTransactionOrFail(self, "Failed Method.invoke");
1663 }
1664}
1665
Nicolas Geoffrayece2f7c2017-03-08 16:11:23 +00001666void UnstartedRuntime::UnstartedSystemIdentityHashCode(
1667 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1668 REQUIRES_SHARED(Locks::mutator_lock_) {
1669 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1670 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1671}
Andreas Gampebc4d2182016-02-22 10:03:12 -08001672
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001673// Checks whether the runtime is s64-bit. This is needed for the clinit of
1674// java.lang.invoke.VarHandle clinit. The clinit determines sets of
1675// available VarHandle accessors and these differ based on machine
1676// word size.
1677void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
1678 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1679 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
1680 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1681 jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
1682 result->SetZ(is64bit);
1683}
1684
Mathieu Chartiere401d142015-04-22 13:56:20 -07001685void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1686 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1687 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001688 int32_t length = args[1];
1689 DCHECK_GE(length, 0);
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001690 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001691 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001692 ObjPtr<mirror::Class> array_class =
1693 runtime->GetClassLinker()->FindArrayClass(self, &element_class);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001694 DCHECK(array_class != nullptr);
1695 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001696 result->SetL(mirror::Array::Alloc<true, true>(self,
1697 array_class,
1698 length,
1699 array_class->GetComponentSizeShift(),
1700 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001701}
1702
Mathieu Chartiere401d142015-04-22 13:56:20 -07001703void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1704 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1705 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001706 result->SetL(nullptr);
1707}
1708
Mathieu Chartiere401d142015-04-22 13:56:20 -07001709void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1710 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1711 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001712 NthCallerVisitor visitor(self, 3);
1713 visitor.WalkStack();
1714 if (visitor.caller != nullptr) {
1715 result->SetL(visitor.caller->GetDeclaringClass());
1716 }
1717}
1718
Mathieu Chartiere401d142015-04-22 13:56:20 -07001719void UnstartedRuntime::UnstartedJNIMathLog(
1720 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1721 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001722 JValue value;
1723 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1724 result->SetD(log(value.GetD()));
1725}
1726
Mathieu Chartiere401d142015-04-22 13:56:20 -07001727void UnstartedRuntime::UnstartedJNIMathExp(
1728 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1729 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001730 JValue value;
1731 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1732 result->SetD(exp(value.GetD()));
1733}
1734
Andreas Gampebc4d2182016-02-22 10:03:12 -08001735void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1736 Thread* self ATTRIBUTE_UNUSED,
1737 ArtMethod* method ATTRIBUTE_UNUSED,
1738 mirror::Object* receiver ATTRIBUTE_UNUSED,
1739 uint32_t* args ATTRIBUTE_UNUSED,
1740 JValue* result) {
1741 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1742 ? 0
1743 : 1);
1744}
1745
Mathieu Chartiere401d142015-04-22 13:56:20 -07001746void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1747 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1748 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001749 StackHandleScope<1> hs(self);
1750 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1751}
1752
Andreas Gampebc4d2182016-02-22 10:03:12 -08001753void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1754 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1755 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1756 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1757 result->SetD(bit_cast<double>(long_input));
1758}
1759
Mathieu Chartiere401d142015-04-22 13:56:20 -07001760void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1761 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1762 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001763 result->SetI(args[0]);
1764}
1765
Mathieu Chartiere401d142015-04-22 13:56:20 -07001766void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1767 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1768 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001769 result->SetI(args[0]);
1770}
1771
Mathieu Chartiere401d142015-04-22 13:56:20 -07001772void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1773 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1774 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001775 result->SetL(receiver->Clone(self));
1776}
1777
Mathieu Chartiere401d142015-04-22 13:56:20 -07001778void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1779 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1780 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001781 receiver->NotifyAll(self);
1782}
1783
Mathieu Chartiere401d142015-04-22 13:56:20 -07001784void UnstartedRuntime::UnstartedJNIStringCompareTo(
1785 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1786 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001787 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1788 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001789 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001790 }
1791 result->SetI(receiver->AsString()->CompareTo(rhs));
1792}
1793
Mathieu Chartiere401d142015-04-22 13:56:20 -07001794void UnstartedRuntime::UnstartedJNIStringIntern(
1795 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1796 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001797 result->SetL(receiver->AsString()->Intern());
1798}
1799
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1801 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1802 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001803 StackHandleScope<2> hs(self);
1804 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1805 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1806 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1807}
1808
Mathieu Chartiere401d142015-04-22 13:56:20 -07001809void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1810 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1811 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001812 int32_t length = static_cast<int32_t>(args[1]);
1813 if (length < 0) {
1814 ThrowNegativeArraySizeException(length);
1815 return;
1816 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001817 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001818 Runtime* runtime = Runtime::Current();
1819 ClassLinker* class_linker = runtime->GetClassLinker();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001820 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001821 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001822 CHECK(self->IsExceptionPending());
1823 return;
1824 }
1825 DCHECK(array_class->IsObjectArrayClass());
1826 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1827 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1828 result->SetL(new_array);
1829}
1830
Mathieu Chartiere401d142015-04-22 13:56:20 -07001831void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1832 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1833 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001834 ScopedObjectAccessUnchecked soa(self);
1835 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001836 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001837 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001838 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001839 }
1840}
1841
Mathieu Chartiere401d142015-04-22 13:56:20 -07001842void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1843 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1844 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001845 result->SetZ(JNI_TRUE);
1846}
1847
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1849 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1850 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001851 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1852 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1853 jint expectedValue = args[3];
1854 jint newValue = args[4];
1855 bool success;
1856 if (Runtime::Current()->IsActiveTransaction()) {
1857 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1858 expectedValue, newValue);
1859 } else {
1860 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1861 expectedValue, newValue);
1862 }
1863 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1864}
1865
Narayan Kamath34a316f2016-03-30 13:11:18 +01001866void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1867 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1868 uint32_t* args, JValue* result) {
1869 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1870 if (obj == nullptr) {
1871 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1872 return;
1873 }
1874
1875 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1876 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1877}
1878
Mathieu Chartiere401d142015-04-22 13:56:20 -07001879void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1880 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1881 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001882 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1883 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1884 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1885 if (Runtime::Current()->IsActiveTransaction()) {
1886 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1887 } else {
1888 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1889 }
1890}
1891
Andreas Gampe799681b2015-05-15 19:24:12 -07001892void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001893 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1894 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001895 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1896 Primitive::Type primitive_type = component->GetPrimitiveType();
1897 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1898}
1899
Andreas Gampe799681b2015-05-15 19:24:12 -07001900void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001901 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1902 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001903 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1904 Primitive::Type primitive_type = component->GetPrimitiveType();
1905 result->SetI(Primitive::ComponentSize(primitive_type));
1906}
1907
Andreas Gampedd9d0552015-03-09 12:57:41 -07001908typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001909 size_t arg_size);
1910
Mathieu Chartiere401d142015-04-22 13:56:20 -07001911typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001912 uint32_t* args, JValue* result);
1913
1914static bool tables_initialized_ = false;
1915static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1916static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1917
Andreas Gampe799681b2015-05-15 19:24:12 -07001918void UnstartedRuntime::InitializeInvokeHandlers() {
1919#define UNSTARTED_DIRECT(ShortName, Sig) \
1920 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1921#include "unstarted_runtime_list.h"
1922 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1923#undef UNSTARTED_RUNTIME_DIRECT_LIST
1924#undef UNSTARTED_RUNTIME_JNI_LIST
1925#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001926}
1927
Andreas Gampe799681b2015-05-15 19:24:12 -07001928void UnstartedRuntime::InitializeJNIHandlers() {
1929#define UNSTARTED_JNI(ShortName, Sig) \
1930 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1931#include "unstarted_runtime_list.h"
1932 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1933#undef UNSTARTED_RUNTIME_DIRECT_LIST
1934#undef UNSTARTED_RUNTIME_JNI_LIST
1935#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001936}
1937
Andreas Gampe799681b2015-05-15 19:24:12 -07001938void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001939 CHECK(!tables_initialized_);
1940
Andreas Gampe799681b2015-05-15 19:24:12 -07001941 InitializeInvokeHandlers();
1942 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001943
1944 tables_initialized_ = true;
1945}
1946
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001947void UnstartedRuntime::Invoke(Thread* self, const CodeItemDataAccessor& accessor,
Andreas Gampe799681b2015-05-15 19:24:12 -07001948 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001949 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1950 // problems in core libraries.
1951 CHECK(tables_initialized_);
1952
David Sehr709b0702016-10-13 09:12:37 -07001953 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001954 const auto& iter = invoke_handlers_.find(name);
1955 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001956 // Clear out the result in case it's not zeroed out.
1957 result->SetL(0);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001958
1959 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
1960 self->PushShadowFrame(shadow_frame);
1961
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001962 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001963
1964 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001965 } else {
1966 // Not special, continue with regular interpreter execution.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08001967 ArtInterpreterToInterpreterBridge(self, accessor, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001968 }
1969}
1970
1971// 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 -07001972void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001973 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07001974 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001975 const auto& iter = jni_handlers_.find(name);
1976 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001977 // Clear out the result in case it's not zeroed out.
1978 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001979 (*iter->second)(self, method, receiver, args, result);
1980 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001981 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1982 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001983 } else {
David Sehr709b0702016-10-13 09:12:37 -07001984 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001985 "non-transactional runtime";
1986 }
1987}
1988
1989} // namespace interpreter
1990} // namespace art