blob: 7a1b7eba2214d7c26f929ff917cd249126f6383d [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"
David Sehr79e26072018-04-06 17:58:50 -070037#include "base/zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070038#include "class_linker.h"
39#include "common_throws.h"
David Sehrb2ec9f52018-02-21 13:20:31 -080040#include "dex/descriptors_names.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080042#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "handle_scope-inl.h"
David Brazdil5a61bb72018-01-19 16:59:46 +000044#include "hidden_api.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045#include "interpreter/interpreter_common.h"
Mathieu Chartier28bd2e42016-10-04 13:54:57 -070046#include "jvalue-inl.h"
Andreas Gampe8e0f0432018-10-24 13:38:03 -070047#include "mirror/array-alloc-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "mirror/array-inl.h"
Andreas Gampe70f5fd02018-10-24 19:58:37 -070049#include "mirror/class-alloc-inl.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070050#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010051#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070052#include "mirror/object-inl.h"
Andreas Gampe52ecb652018-10-24 15:18:21 -070053#include "mirror/object_array-alloc-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070054#include "mirror/object_array-inl.h"
Andreas Gampefd63bbf2018-10-29 12:55:35 -070055#include "mirror/string-alloc-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070056#include "mirror/string-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070057#include "nativehelper/scoped_local_ref.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070058#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070059#include "reflection.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070060#include "thread-inl.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020061#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070062#include "well_known_classes.h"
63
64namespace art {
65namespace interpreter {
66
Andreas Gampe46ee31b2016-12-14 10:11:49 -080067using android::base::StringAppendV;
68using android::base::StringPrintf;
69
Andreas Gampe068b0c02015-03-11 12:44:47 -070070static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020071 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070072 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020073
74static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070075 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070076 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020077 va_start(args, fmt);
78 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070079 va_end(args);
80 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020081 va_start(args, fmt);
82 std::string msg;
83 StringAppendV(&msg, fmt, args);
84 va_end(args);
85 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070086 UNREACHABLE();
87 }
88}
89
Andreas Gampe8ce9c302016-04-15 21:24:28 -070090// Restricted support for character upper case / lower case. Only support ASCII, where
91// it's easy. Abort the transaction otherwise.
92static void CharacterLowerUpper(Thread* self,
93 ShadowFrame* shadow_frame,
94 JValue* result,
95 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070096 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070097 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
98
99 // Only ASCII (7-bit).
100 if (!isascii(int_value)) {
101 AbortTransactionOrFail(self,
102 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
103 int_value);
104 return;
105 }
106
107 std::locale c_locale("C");
108 char char_value = static_cast<char>(int_value);
109
110 if (to_lower_case) {
111 result->SetI(std::tolower(char_value, c_locale));
112 } else {
113 result->SetI(std::toupper(char_value, c_locale));
114 }
115}
116
117void UnstartedRuntime::UnstartedCharacterToLowerCase(
118 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
119 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
120}
121
122void UnstartedRuntime::UnstartedCharacterToUpperCase(
123 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
124 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
125}
126
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700127// Helper function to deal with class loading in an unstarted runtime.
128static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
129 Handle<mirror::ClassLoader> class_loader, JValue* result,
130 const std::string& method_name, bool initialize_class,
131 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700132 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800133 CHECK(className != nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700134 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
135 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
136
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100137 ObjPtr<mirror::Class> found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700138 if (found == nullptr && abort_if_not_found) {
139 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700140 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700141 method_name.c_str(),
142 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700143 }
144 return;
145 }
146 if (found != nullptr && initialize_class) {
147 StackHandleScope<1> hs(self);
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100148 HandleWrapperObjPtr<mirror::Class> h_class = hs.NewHandleWrapper(&found);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
150 CHECK(self->IsExceptionPending());
151 return;
152 }
153 }
154 result->SetL(found);
155}
156
157// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
158// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
159// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200160// actually the transaction abort exception. This must not be wrapped, as it signals an
161// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700162static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700163 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700164 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200165 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700166 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200167 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700168 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
169 "ClassNotFoundException");
170 }
171 }
172}
173
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700174static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700175 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700176 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
177 if (param == nullptr) {
178 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
179 return nullptr;
180 }
181 return param->AsString();
182}
183
David Brazdil4bcd6572019-02-02 20:08:44 +0000184static std::function<hiddenapi::AccessContext()> GetHiddenapiAccessContextFunction(
185 ShadowFrame* frame) {
186 return [=]() REQUIRES_SHARED(Locks::mutator_lock_) {
187 return hiddenapi::AccessContext(frame->GetMethod()->GetDeclaringClass());
188 };
189}
190
David Brazdila02cb112018-01-31 11:36:39 +0000191template<typename T>
David Brazdilf50ac102018-10-17 18:00:06 +0100192static ALWAYS_INLINE bool ShouldDenyAccessToMember(T* member, ShadowFrame* frame)
David Brazdila02cb112018-01-31 11:36:39 +0000193 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100194 // All uses in this file are from reflection
David Brazdile7681822018-12-14 16:25:33 +0000195 constexpr hiddenapi::AccessMethod kAccessMethod = hiddenapi::AccessMethod::kReflection;
David Brazdil4bcd6572019-02-02 20:08:44 +0000196 return hiddenapi::ShouldDenyAccessToMember(member,
197 GetHiddenapiAccessContextFunction(frame),
198 kAccessMethod);
David Brazdila02cb112018-01-31 11:36:39 +0000199}
200
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800201void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
202 ShadowFrame* shadow_frame,
203 JValue* result,
204 size_t arg_offset,
205 bool long_form,
206 const char* caller) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700207 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
208 if (class_name == nullptr) {
209 return;
210 }
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800211 bool initialize_class;
212 mirror::ClassLoader* class_loader;
213 if (long_form) {
214 initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
215 class_loader = down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
216 } else {
217 initialize_class = true;
218 // TODO: This is really only correct for the boot classpath, and for robustness we should
219 // check the caller.
220 class_loader = nullptr;
221 }
222
223 ScopedObjectAccessUnchecked soa(self);
224 if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
225 AbortTransactionOrFail(self,
226 "Only the boot classloader is supported: %s",
227 mirror::Object::PrettyTypeOf(class_loader).c_str());
228 return;
229 }
230
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700231 StackHandleScope<1> hs(self);
232 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800233 UnstartedRuntimeFindClass(self,
234 h_class_name,
235 ScopedNullHandle<mirror::ClassLoader>(),
236 result,
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800237 caller,
238 initialize_class,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800239 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700240 CheckExceptionGenerateClassNotFound(self);
241}
242
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800243void UnstartedRuntime::UnstartedClassForName(
244 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
245 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
246}
247
Andreas Gampe799681b2015-05-15 19:24:12 -0700248void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700249 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800250 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700251}
252
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000253void UnstartedRuntime::UnstartedClassGetPrimitiveClass(
254 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
255 ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
256 ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(class_name);
257 if (UNLIKELY(klass == nullptr)) {
258 DCHECK(self->IsExceptionPending());
259 AbortTransactionOrFail(self,
260 "Class.getPrimitiveClass() failed: %s",
261 self->GetException()->GetDetailMessage()->ToModifiedUtf8().c_str());
262 return;
263 }
264 result->SetL(klass);
265}
266
Andreas Gampe799681b2015-05-15 19:24:12 -0700267void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700268 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800269 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700270}
271
Andreas Gampe799681b2015-05-15 19:24:12 -0700272void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700273 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
274 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700275 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
276 if (param == nullptr) {
277 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
278 return;
279 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100280 Handle<mirror::Class> h_klass(hs.NewHandle(param->AsClass()));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700281
282 // Check that it's not null.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800283 if (h_klass == nullptr) {
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700284 AbortTransactionOrFail(self, "Class reference is null for newInstance");
285 return;
286 }
287
288 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
289 if (Runtime::Current()->IsActiveTransaction()) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100290 if (h_klass->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200291 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700292 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700293 return;
294 }
295 }
296
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700297 // There are two situations in which we'll abort this run.
298 // 1) If the class isn't yet initialized and initialization fails.
299 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
300 // Note that 2) could likely be handled here, but for safety abort the transaction.
301 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700302 auto* cl = Runtime::Current()->GetClassLinker();
303 if (cl->EnsureInitialized(self, h_klass, true, true)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000304 ArtMethod* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
David Brazdilf50ac102018-10-17 18:00:06 +0100305 if (cons != nullptr && ShouldDenyAccessToMember(cons, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000306 cons = nullptr;
307 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700308 if (cons != nullptr) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100309 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800310 CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700311 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700312 if (!self->IsExceptionPending()) {
313 result->SetL(h_obj.Get());
314 ok = true;
315 }
316 } else {
317 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
318 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700319 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700320 }
321 }
322 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700323 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700324 h_klass->PrettyClass().c_str(),
325 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700326 }
327}
328
Andreas Gampe799681b2015-05-15 19:24:12 -0700329void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700330 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700331 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
332 // going the reflective Dex way.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100333 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
334 ObjPtr<mirror::String> name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700335 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700336 for (ArtField& field : klass->GetIFields()) {
337 if (name2->Equals(field.GetName())) {
338 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700339 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700340 }
341 }
342 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700343 for (ArtField& field : klass->GetSFields()) {
344 if (name2->Equals(field.GetName())) {
345 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700346 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700347 }
348 }
349 }
David Brazdilf50ac102018-10-17 18:00:06 +0100350 if (found != nullptr && ShouldDenyAccessToMember(found, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000351 found = nullptr;
352 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700353 if (found == nullptr) {
354 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
355 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700356 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700357 return;
358 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700359 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700360 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700361 mirror::Field* field;
362 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700363 if (pointer_size == PointerSize::k64) {
364 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
365 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700366 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700367 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
368 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700369 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700370 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700371 if (pointer_size == PointerSize::k64) {
372 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
373 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700374 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700375 field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
376 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700377 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700378 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700379 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700380}
381
Andreas Gampebc4d2182016-02-22 10:03:12 -0800382// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
383void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
384 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
385 // Special managed code cut-out to allow method lookup in a un-started runtime.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100386 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampebc4d2182016-02-22 10:03:12 -0800387 if (klass == nullptr) {
388 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
389 return;
390 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100391 ObjPtr<mirror::String> name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
392 ObjPtr<mirror::ObjectArray<mirror::Class>> args =
Andreas Gampebc4d2182016-02-22 10:03:12 -0800393 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700394 Runtime* runtime = Runtime::Current();
395 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700396 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
David Brazdil4bcd6572019-02-02 20:08:44 +0000397 auto fn_hiddenapi_access_context = GetHiddenapiAccessContextFunction(shadow_frame);
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700398 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700399 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700400 if (pointer_size == PointerSize::k64) {
401 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
David Brazdil4bcd6572019-02-02 20:08:44 +0000402 self, klass, name, args, fn_hiddenapi_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -0700403 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700404 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
David Brazdil4bcd6572019-02-02 20:08:44 +0000405 self, klass, name, args, fn_hiddenapi_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -0700406 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800407 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700408 if (pointer_size == PointerSize::k64) {
409 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
David Brazdil4bcd6572019-02-02 20:08:44 +0000410 self, klass, name, args, fn_hiddenapi_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -0700411 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700412 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
David Brazdil4bcd6572019-02-02 20:08:44 +0000413 self, klass, name, args, fn_hiddenapi_access_context);
Andreas Gampee01e3642016-07-25 13:06:04 -0700414 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800415 }
David Brazdilf50ac102018-10-17 18:00:06 +0100416 if (method != nullptr && ShouldDenyAccessToMember(method->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000417 method = nullptr;
418 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700419 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800420}
421
Andreas Gampe6039e562016-04-05 18:18:43 -0700422// Special managed code cut-out to allow constructor lookup in a un-started runtime.
423void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
424 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100425 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampe6039e562016-04-05 18:18:43 -0700426 if (klass == nullptr) {
427 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
428 return;
429 }
430 mirror::ObjectArray<mirror::Class>* args =
431 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700432 Runtime* runtime = Runtime::Current();
433 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700434 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700435 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700436 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700437 if (pointer_size == PointerSize::k64) {
438 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
439 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700440 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700441 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
442 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700443 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700444 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700445 if (pointer_size == PointerSize::k64) {
446 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
447 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700448 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700449 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
450 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700451 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700452 }
David Brazdil5a61bb72018-01-19 16:59:46 +0000453 if (constructor != nullptr &&
David Brazdilf50ac102018-10-17 18:00:06 +0100454 ShouldDenyAccessToMember(constructor->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000455 constructor = nullptr;
456 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700457 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700458}
459
Andreas Gampeae78c262017-02-01 20:40:44 -0800460void UnstartedRuntime::UnstartedClassGetDeclaringClass(
461 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
462 StackHandleScope<1> hs(self);
463 Handle<mirror::Class> klass(hs.NewHandle(
464 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
465 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
466 result->SetL(nullptr);
467 return;
468 }
469 // Return null for anonymous classes.
470 JValue is_anon_result;
471 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
472 if (is_anon_result.GetZ() != 0) {
473 result->SetL(nullptr);
474 return;
475 }
476 result->SetL(annotations::GetDeclaringClass(klass));
477}
478
Andreas Gampe633750c2016-02-19 10:49:50 -0800479void UnstartedRuntime::UnstartedClassGetEnclosingClass(
480 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
481 StackHandleScope<1> hs(self);
482 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
483 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
484 result->SetL(nullptr);
485 }
David Sehr9323e6e2016-09-13 08:58:35 -0700486 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800487}
488
Andreas Gampe715fdc22016-04-18 17:07:30 -0700489void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
490 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
491 StackHandleScope<1> hs(self);
492 Handle<mirror::Class> klass(hs.NewHandle(
493 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
494 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
495 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
496}
497
Andreas Gampe9486a162017-02-16 15:17:47 -0800498void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
499 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
500 StackHandleScope<1> hs(self);
501 Handle<mirror::Class> klass(hs.NewHandle(
502 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
503
504 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
505 result->SetL(nullptr);
506 return;
507 }
508
509 result->SetL(annotations::GetSignatureAnnotationForClass(klass));
510}
511
Andreas Gampeae78c262017-02-01 20:40:44 -0800512void UnstartedRuntime::UnstartedClassIsAnonymousClass(
513 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
514 StackHandleScope<1> hs(self);
515 Handle<mirror::Class> klass(hs.NewHandle(
516 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
517 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
518 result->SetZ(false);
519 return;
520 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100521 ObjPtr<mirror::String> class_name = nullptr;
Andreas Gampeae78c262017-02-01 20:40:44 -0800522 if (!annotations::GetInnerClass(klass, &class_name)) {
523 result->SetZ(false);
524 return;
525 }
526 result->SetZ(class_name == nullptr);
527}
528
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100529static MemMap FindAndExtractEntry(const std::string& jar_file,
530 const char* entry_name,
531 size_t* size,
532 std::string* error_msg) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700533 CHECK(size != nullptr);
534
535 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
536 if (zip_archive == nullptr) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100537 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700538 }
539 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
540 if (zip_entry == nullptr) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100541 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700542 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100543 MemMap tmp_map = zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg);
544 if (!tmp_map.IsValid()) {
545 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700546 }
547
548 // OK, from here everything seems fine.
549 *size = zip_entry->GetUncompressedLength();
550 return tmp_map;
551}
552
553static void GetResourceAsStream(Thread* self,
554 ShadowFrame* shadow_frame,
555 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700556 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700557 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
558 if (resource_obj == nullptr) {
559 AbortTransactionOrFail(self, "null name for getResourceAsStream");
560 return;
561 }
562 CHECK(resource_obj->IsString());
563 mirror::String* resource_name = resource_obj->AsString();
564
565 std::string resource_name_str = resource_name->ToModifiedUtf8();
566 if (resource_name_str.empty() || resource_name_str == "/") {
567 AbortTransactionOrFail(self,
568 "Unsupported name %s for getResourceAsStream",
569 resource_name_str.c_str());
570 return;
571 }
572 const char* resource_cstr = resource_name_str.c_str();
573 if (resource_cstr[0] == '/') {
574 resource_cstr++;
575 }
576
577 Runtime* runtime = Runtime::Current();
578
Vladimir Marko91f10322018-12-07 18:04:10 +0000579 const std::vector<std::string>& boot_class_path = Runtime::Current()->GetBootClassPath();
580 if (boot_class_path.empty()) {
581 AbortTransactionOrFail(self, "Boot classpath not set");
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700582 return;
583 }
584
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100585 MemMap mem_map;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700586 size_t map_size;
587 std::string last_error_msg; // Only store the last message (we could concatenate).
588
Vladimir Marko91f10322018-12-07 18:04:10 +0000589 for (const std::string& jar_file : boot_class_path) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700590 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100591 if (mem_map.IsValid()) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700592 break;
593 }
594 }
595
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100596 if (!mem_map.IsValid()) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700597 // Didn't find it. There's a good chance this will be the same at runtime, but still
598 // conservatively abort the transaction here.
599 AbortTransactionOrFail(self,
600 "Could not find resource %s. Last error was %s.",
601 resource_name_str.c_str(),
602 last_error_msg.c_str());
603 return;
604 }
605
606 StackHandleScope<3> hs(self);
607
608 // Create byte array for content.
609 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800610 if (h_array == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700611 AbortTransactionOrFail(self, "Could not find/create byte array class");
612 return;
613 }
614 // Copy in content.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100615 memcpy(h_array->GetData(), mem_map.Begin(), map_size);
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700616 // Be proactive releasing memory.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100617 mem_map.Reset();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700618
619 // Create a ByteArrayInputStream.
620 Handle<mirror::Class> h_class(hs.NewHandle(
621 runtime->GetClassLinker()->FindClass(self,
622 "Ljava/io/ByteArrayInputStream;",
623 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800624 if (h_class == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700625 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
626 return;
627 }
628 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
629 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
630 return;
631 }
632
633 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800634 if (h_obj == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700635 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
636 return;
637 }
638
639 auto* cl = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100640 ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700641 if (constructor == nullptr) {
642 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
643 return;
644 }
645
646 uint32_t args[1];
Vladimir Marko78baed52018-10-11 10:44:58 +0100647 args[0] = reinterpret_cast32<uint32_t>(h_array.Get());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700648 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
649
650 if (self->IsExceptionPending()) {
651 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
652 return;
653 }
654
655 result->SetL(h_obj.Get());
656}
657
658void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
659 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
660 {
661 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
662 CHECK(this_obj != nullptr);
663 CHECK(this_obj->IsClassLoader());
664
665 StackHandleScope<1> hs(self);
666 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
667
668 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
669 this_classloader_class.Get()) {
670 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700671 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700672 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700673 return;
674 }
675 }
676
677 GetResourceAsStream(self, shadow_frame, result, arg_offset);
678}
679
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800680void UnstartedRuntime::UnstartedConstructorNewInstance0(
681 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
682 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
683 StackHandleScope<4> hs(self);
684 Handle<mirror::Constructor> m = hs.NewHandle(
685 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
686 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
687 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
688 shadow_frame->GetVRegReference(arg_offset + 1)));
689 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
690 if (UNLIKELY(c->IsAbstract())) {
691 AbortTransactionOrFail(self, "Cannot handle abstract classes");
692 return;
693 }
694 // Verify that we can access the class.
695 if (!m->IsAccessible() && !c->IsPublic()) {
696 // Go 2 frames back, this method is always called from newInstance0, which is called from
697 // Constructor.newInstance(Object... args).
698 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
699 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
700 // access checks anyways. TODO: Investigate if this the correct behavior.
701 if (caller != nullptr && !caller->CanAccess(c.Get())) {
702 AbortTransactionOrFail(self, "Cannot access class");
703 return;
704 }
705 }
706 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
707 DCHECK(self->IsExceptionPending());
708 return;
709 }
710 if (c->IsClassClass()) {
711 AbortTransactionOrFail(self, "new Class() is not supported");
712 return;
713 }
714
715 // String constructor is replaced by a StringFactory method in InvokeMethod.
716 if (c->IsStringClass()) {
717 // We don't support strings.
718 AbortTransactionOrFail(self, "String construction is not supported");
719 return;
720 }
721
722 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
723 if (receiver == nullptr) {
724 AbortTransactionOrFail(self, "Could not allocate");
725 return;
726 }
727
728 // It's easier to use reflection to make the call, than create the uint32_t array.
729 {
730 ScopedObjectAccessUnchecked soa(self);
731 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
732 soa.AddLocalReference<jobject>(m.Get()));
733 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
734 soa.AddLocalReference<jobject>(receiver.Get()));
735 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
736 soa.AddLocalReference<jobject>(args.Get()));
737 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
738 }
739 if (self->IsExceptionPending()) {
740 AbortTransactionOrFail(self, "Failed running constructor");
741 } else {
742 result->SetL(receiver.Get());
743 }
744}
745
Andreas Gampe799681b2015-05-15 19:24:12 -0700746void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700747 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700748 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
749 mirror::ClassLoader* class_loader =
750 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
751 StackHandleScope<2> hs(self);
752 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
753 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
754 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
755 "VMClassLoader.findLoadedClass", false, false);
756 // This might have an error pending. But semantics are to just return null.
757 if (self->IsExceptionPending()) {
758 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700759 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700760 if (type != "java.lang.InternalError") {
761 self->ClearException();
762 }
763 }
764}
765
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700766// Arraycopy emulation.
767// Note: we can't use any fast copy functions, as they are not available under transaction.
768
769template <typename T>
770static void PrimitiveArrayCopy(Thread* self,
771 mirror::Array* src_array, int32_t src_pos,
772 mirror::Array* dst_array, int32_t dst_pos,
773 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700774 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700775 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700776 AbortTransactionOrFail(self,
777 "Types mismatched in arraycopy: %s vs %s.",
778 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700779 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700780 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700781 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700782 return;
783 }
784 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
785 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
786 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
787 if (copy_forward) {
788 for (int32_t i = 0; i < length; ++i) {
789 dst->Set(dst_pos + i, src->Get(src_pos + i));
790 }
791 } else {
792 for (int32_t i = 1; i <= length; ++i) {
793 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
794 }
795 }
796}
797
Andreas Gampe799681b2015-05-15 19:24:12 -0700798void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700799 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700800 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700801 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
802 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700803 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700804
Andreas Gampe85a098a2016-03-31 13:30:53 -0700805 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
806 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
807 // Null checking. For simplicity, abort transaction.
808 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700809 AbortTransactionOrFail(self, "src is null in arraycopy.");
810 return;
811 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700812 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700813 AbortTransactionOrFail(self, "dst is null in arraycopy.");
814 return;
815 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700816 // Test for arrayness. Throw ArrayStoreException.
817 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
818 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
819 return;
820 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700821
Andreas Gampe85a098a2016-03-31 13:30:53 -0700822 mirror::Array* src_array = src_obj->AsArray();
823 mirror::Array* dst_array = dst_obj->AsArray();
824
825 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700826 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
827 UNLIKELY(src_pos > src_array->GetLength() - length) ||
828 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700829 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700830 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
831 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
832 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700833 return;
834 }
835
836 // Type checking.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100837 ObjPtr<mirror::Class> src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700838 GetComponentType();
839
840 if (!src_type->IsPrimitive()) {
841 // Check that the second type is not primitive.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100842 ObjPtr<mirror::Class> trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700843 GetComponentType();
844 if (trg_type->IsPrimitiveInt()) {
845 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700846 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700847 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700848 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700849 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700850 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700851 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700852
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700853 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
854 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
855 if (src == dst) {
856 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700857 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700858 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
859 if (copy_forward) {
860 for (int32_t i = 0; i < length; ++i) {
861 dst->Set(dst_pos + i, src->Get(src_pos + i));
862 }
863 } else {
864 for (int32_t i = 1; i <= length; ++i) {
865 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
866 }
867 }
868 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700869 // We're being lazy here. Optimally this could be a memcpy (if component types are
870 // assignable), but the ObjectArray implementation doesn't support transactions. The
871 // checking version, however, does.
872 if (Runtime::Current()->IsActiveTransaction()) {
873 dst->AssignableCheckingMemcpy<true>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700874 dst_pos, src, src_pos, length, /* throw_exception= */ true);
Andreas Gampe85a098a2016-03-31 13:30:53 -0700875 } else {
876 dst->AssignableCheckingMemcpy<false>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700877 dst_pos, src, src_pos, length, /* throw_exception= */ true);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700878 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700879 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700880 } else if (src_type->IsPrimitiveByte()) {
881 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700882 } else if (src_type->IsPrimitiveChar()) {
883 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
884 } else if (src_type->IsPrimitiveInt()) {
885 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700886 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700887 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700888 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700889 }
890}
891
Andreas Gampe5c9af612016-04-05 14:16:10 -0700892void UnstartedRuntime::UnstartedSystemArraycopyByte(
893 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
894 // Just forward.
895 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
896}
897
Andreas Gampe799681b2015-05-15 19:24:12 -0700898void UnstartedRuntime::UnstartedSystemArraycopyChar(
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
904void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700905 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700906 // Just forward.
907 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
908}
909
Narayan Kamath34a316f2016-03-30 13:11:18 +0100910void UnstartedRuntime::UnstartedSystemGetSecurityManager(
911 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
912 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
913 result->SetL(nullptr);
914}
915
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700916static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
917
918static void GetSystemProperty(Thread* self,
919 ShadowFrame* shadow_frame,
920 JValue* result,
921 size_t arg_offset,
922 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700923 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700924 StackHandleScope<4> hs(self);
925 Handle<mirror::String> h_key(
926 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800927 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700928 AbortTransactionOrFail(self, "getProperty key was null");
929 return;
930 }
931
932 // This is overall inefficient, but reflecting the values here is not great, either. So
933 // for simplicity, and with the assumption that the number of getProperty calls is not
934 // too great, just iterate each time.
935
936 // Get the storage class.
937 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
938 Handle<mirror::Class> h_props_class(hs.NewHandle(
939 class_linker->FindClass(self,
940 "Ljava/lang/AndroidHardcodedSystemProperties;",
941 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800942 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700943 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
944 return;
945 }
946 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
947 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
948 return;
949 }
950
951 // Get the storage array.
952 ArtField* static_properties =
953 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
954 "[[Ljava/lang/String;");
955 if (static_properties == nullptr) {
956 AbortTransactionOrFail(self,
957 "Could not find %s field",
958 kAndroidHardcodedSystemPropertiesFieldName);
959 return;
960 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700961 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
962 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
963 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800964 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700965 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
966 return;
967 }
968
969 // Iterate over it.
970 const int32_t prop_count = h_2string_array->GetLength();
971 // Use the third handle as mutable.
972 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
973 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
974 for (int32_t i = 0; i < prop_count; ++i) {
975 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800976 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700977 h_string_array->GetLength() != 2 ||
978 h_string_array->Get(0) == nullptr) {
979 AbortTransactionOrFail(self,
980 "Unexpected content of %s",
981 kAndroidHardcodedSystemPropertiesFieldName);
982 return;
983 }
984 if (h_key->Equals(h_string_array->Get(0))) {
985 // Found a value.
986 if (h_string_array->Get(1) == nullptr && is_default_version) {
987 // Null is being delegated to the default map, and then resolved to the given default value.
988 // As there's no default map, return the given value.
989 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
990 } else {
991 result->SetL(h_string_array->Get(1));
992 }
993 return;
994 }
995 }
996
997 // Key is not supported.
998 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
999}
1000
1001void UnstartedRuntime::UnstartedSystemGetProperty(
1002 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1003 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
1004}
1005
1006void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
1007 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1008 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
1009}
1010
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001011static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
1012 REQUIRES_SHARED(Locks::mutator_lock_) {
1013 if (shadow_frame->GetLink() == nullptr) {
1014 return "<no caller>";
1015 }
1016 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1017}
1018
1019static bool CheckCallers(ShadowFrame* shadow_frame,
1020 std::initializer_list<std::string> allowed_call_stack)
1021 REQUIRES_SHARED(Locks::mutator_lock_) {
1022 for (const std::string& allowed_caller : allowed_call_stack) {
1023 if (shadow_frame->GetLink() == nullptr) {
1024 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001025 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001026
1027 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1028 if (allowed_caller != found_caller) {
1029 return false;
1030 }
1031
1032 shadow_frame = shadow_frame->GetLink();
1033 }
1034 return true;
1035}
1036
1037static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
1038 REQUIRES_SHARED(Locks::mutator_lock_) {
1039 // Find the requested class.
1040 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1041 ObjPtr<mirror::Class> klass =
1042 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1043 if (klass == nullptr) {
1044 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1045 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001046 }
1047
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001048 StackHandleScope<2> hs(self);
1049 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1050 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001051 if (h_obj != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01001052 ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001053 if (init_method == nullptr) {
1054 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1055 return nullptr;
1056 } else {
1057 JValue invoke_result;
1058 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1059 if (!self->IsExceptionPending()) {
1060 return h_obj.Get();
1061 }
1062 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1063 }
1064 }
1065 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1066 return nullptr;
1067}
1068
1069void UnstartedRuntime::UnstartedThreadLocalGet(
1070 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1071 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1072 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1073 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1074 } else {
1075 AbortTransactionOrFail(self,
1076 "ThreadLocal.get() does not support %s",
1077 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001078 }
1079}
1080
Andreas Gampebad529d2017-02-13 18:52:10 -08001081void UnstartedRuntime::UnstartedThreadCurrentThread(
1082 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1083 if (CheckCallers(shadow_frame,
1084 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Paul Duffin7ec95c52018-08-01 15:09:37 +01001085 "java.lang.String, long, java.security.AccessControlContext)",
1086 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Andreas Gampebad529d2017-02-13 18:52:10 -08001087 "java.lang.String, long)",
1088 "void java.lang.Thread.<init>()",
1089 "void java.util.logging.LogManager$Cleaner.<init>("
1090 "java.util.logging.LogManager)" })) {
1091 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1092 // Thread constructor only asks for the current thread to set up defaults and add the
1093 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1094 // these purposes.
1095 Runtime::Current()->InitThreadGroups(self);
1096 jobject main_peer =
1097 self->CreateCompileTimePeer(self->GetJniEnv(),
1098 "main",
1099 false,
1100 Runtime::Current()->GetMainThreadGroup());
1101 if (main_peer == nullptr) {
1102 AbortTransactionOrFail(self, "Failed allocating peer");
1103 return;
1104 }
1105
1106 result->SetL(self->DecodeJObject(main_peer));
1107 self->GetJniEnv()->DeleteLocalRef(main_peer);
1108 } else {
1109 AbortTransactionOrFail(self,
1110 "Thread.currentThread() does not support %s",
1111 GetImmediateCaller(shadow_frame).c_str());
1112 }
1113}
1114
1115void UnstartedRuntime::UnstartedThreadGetNativeState(
1116 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1117 if (CheckCallers(shadow_frame,
1118 { "java.lang.Thread$State java.lang.Thread.getState()",
1119 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1120 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Paul Duffin7ec95c52018-08-01 15:09:37 +01001121 "java.lang.String, long, java.security.AccessControlContext)",
1122 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Andreas Gampebad529d2017-02-13 18:52:10 -08001123 "java.lang.String, long)",
1124 "void java.lang.Thread.<init>()",
1125 "void java.util.logging.LogManager$Cleaner.<init>("
1126 "java.util.logging.LogManager)" })) {
1127 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1128 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1129 constexpr int32_t kJavaRunnable = 1;
1130 result->SetI(kJavaRunnable);
1131 } else {
1132 AbortTransactionOrFail(self,
1133 "Thread.getNativeState() does not support %s",
1134 GetImmediateCaller(shadow_frame).c_str());
1135 }
1136}
1137
Sergio Giro83261202016-04-11 20:49:20 +01001138void UnstartedRuntime::UnstartedMathCeil(
1139 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001140 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001141}
1142
1143void UnstartedRuntime::UnstartedMathFloor(
1144 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001145 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001146}
1147
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001148void UnstartedRuntime::UnstartedMathSin(
1149 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1150 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1151}
1152
1153void UnstartedRuntime::UnstartedMathCos(
1154 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1155 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1156}
1157
1158void UnstartedRuntime::UnstartedMathPow(
1159 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1160 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1161 shadow_frame->GetVRegDouble(arg_offset + 2)));
1162}
1163
Andreas Gampe799681b2015-05-15 19:24:12 -07001164void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001165 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001166 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1167 result->SetI(obj->IdentityHashCode());
1168}
1169
Andreas Gampe799681b2015-05-15 19:24:12 -07001170void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001171 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001172 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001173 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001174}
1175
Andreas Gampedd9d0552015-03-09 12:57:41 -07001176static void UnstartedMemoryPeek(
1177 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1178 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1179 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1180 // aborting the transaction.
1181
1182 switch (type) {
1183 case Primitive::kPrimByte: {
1184 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1185 return;
1186 }
1187
1188 case Primitive::kPrimShort: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001189 using unaligned_short __attribute__((__aligned__(1))) = int16_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001190 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001191 return;
1192 }
1193
1194 case Primitive::kPrimInt: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001195 using unaligned_int __attribute__((__aligned__(1))) = int32_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001196 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001197 return;
1198 }
1199
1200 case Primitive::kPrimLong: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001201 using unaligned_long __attribute__((__aligned__(1))) = int64_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001202 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001203 return;
1204 }
1205
1206 case Primitive::kPrimBoolean:
1207 case Primitive::kPrimChar:
1208 case Primitive::kPrimFloat:
1209 case Primitive::kPrimDouble:
1210 case Primitive::kPrimVoid:
1211 case Primitive::kPrimNot:
1212 LOG(FATAL) << "Not in the Memory API: " << type;
1213 UNREACHABLE();
1214 }
1215 LOG(FATAL) << "Should not reach here";
1216 UNREACHABLE();
1217}
1218
Andreas Gampe799681b2015-05-15 19:24:12 -07001219void UnstartedRuntime::UnstartedMemoryPeekByte(
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::kPrimByte, shadow_frame, result, arg_offset);
1222}
1223
1224void UnstartedRuntime::UnstartedMemoryPeekShort(
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::kPrimShort, shadow_frame, result, arg_offset);
1227}
1228
1229void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001230 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001231 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1232}
1233
1234void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001236 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001237}
1238
1239static void UnstartedMemoryPeekArray(
1240 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001241 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001242 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1243 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1244 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001245 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001246 return;
1247 }
1248 mirror::Array* array = obj->AsArray();
1249
1250 int offset = shadow_frame->GetVReg(arg_offset + 3);
1251 int count = shadow_frame->GetVReg(arg_offset + 4);
1252 if (offset < 0 || offset + count > array->GetLength()) {
1253 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1254 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001255 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001256 return;
1257 }
1258
1259 switch (type) {
1260 case Primitive::kPrimByte: {
1261 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1262 mirror::ByteArray* byte_array = array->AsByteArray();
1263 for (int32_t i = 0; i < count; ++i, ++address) {
1264 byte_array->SetWithoutChecks<true>(i + offset, *address);
1265 }
1266 return;
1267 }
1268
1269 case Primitive::kPrimShort:
1270 case Primitive::kPrimInt:
1271 case Primitive::kPrimLong:
1272 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1273 UNREACHABLE();
1274
1275 case Primitive::kPrimBoolean:
1276 case Primitive::kPrimChar:
1277 case Primitive::kPrimFloat:
1278 case Primitive::kPrimDouble:
1279 case Primitive::kPrimVoid:
1280 case Primitive::kPrimNot:
1281 LOG(FATAL) << "Not in the Memory API: " << type;
1282 UNREACHABLE();
1283 }
1284 LOG(FATAL) << "Should not reach here";
1285 UNREACHABLE();
1286}
1287
Andreas Gampe799681b2015-05-15 19:24:12 -07001288void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001289 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001290 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001291}
1292
Kenny Root1c9e61c2015-05-14 15:58:17 -07001293// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001294void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001295 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001296 jint start = shadow_frame->GetVReg(arg_offset + 1);
1297 jint end = shadow_frame->GetVReg(arg_offset + 2);
1298 jint index = shadow_frame->GetVReg(arg_offset + 4);
1299 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1300 if (string == nullptr) {
1301 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1302 return;
1303 }
Kenny Root57f91e82015-05-14 15:58:17 -07001304 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001305 DCHECK_LE(start, end);
1306 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001307 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308 Handle<mirror::CharArray> h_char_array(
1309 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001310 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001311 DCHECK_LE(index, h_char_array->GetLength());
1312 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001313 string->GetChars(start, end, h_char_array, index);
1314}
1315
1316// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001317void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001318 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001319 jint index = shadow_frame->GetVReg(arg_offset + 1);
1320 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1321 if (string == nullptr) {
1322 AbortTransactionOrFail(self, "String.charAt with null object");
1323 return;
1324 }
1325 result->SetC(string->CharAt(index));
1326}
1327
Vladimir Marko92907f32017-02-20 14:08:30 +00001328// This allows creating String objects with replaced characters during compilation.
1329// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1330void UnstartedRuntime::UnstartedStringDoReplace(
1331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1332 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1333 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001334 StackHandleScope<1> hs(self);
1335 Handle<mirror::String> string =
1336 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
Kenny Root57f91e82015-05-14 15:58:17 -07001337 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001338 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001339 return;
1340 }
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001341 result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001342}
1343
Kenny Root1c9e61c2015-05-14 15:58:17 -07001344// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001345void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001346 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001347 jint offset = shadow_frame->GetVReg(arg_offset);
1348 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1349 DCHECK_GE(char_count, 0);
1350 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001351 Handle<mirror::CharArray> h_char_array(
1352 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001353 Runtime* runtime = Runtime::Current();
1354 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1355 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1356}
1357
1358// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001359void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001360 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001361 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1362 if (to_copy == nullptr) {
1363 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1364 return;
1365 }
1366 StackHandleScope<1> hs(self);
1367 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1368 Runtime* runtime = Runtime::Current();
1369 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1370 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1371 allocator));
1372}
1373
Andreas Gampe799681b2015-05-15 19:24:12 -07001374void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001375 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001376 jint start = shadow_frame->GetVReg(arg_offset + 1);
1377 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001378 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001379 DCHECK_GE(length, 0);
1380 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001381 Handle<mirror::String> h_string(
1382 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001383 DCHECK_LE(start, h_string->GetLength());
1384 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001385 Runtime* runtime = Runtime::Current();
1386 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1387 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1388}
1389
Kenny Root57f91e82015-05-14 15:58:17 -07001390// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001391void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001392 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001393 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001394 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1395 if (string == nullptr) {
1396 AbortTransactionOrFail(self, "String.charAt with null object");
1397 return;
1398 }
1399 result->SetL(string->ToCharArray(self));
1400}
1401
Andreas Gampebc4d2182016-02-22 10:03:12 -08001402// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1403void UnstartedRuntime::UnstartedReferenceGetReferent(
1404 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001405 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001406 shadow_frame->GetVRegReference(arg_offset));
1407 if (ref == nullptr) {
1408 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1409 return;
1410 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001411 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001412 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1413 result->SetL(referent);
1414}
1415
1416// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1417// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1418// where we can predict the behavior (somewhat).
1419// Note: this is required (instead of lazy initialization) as these classes are used in the static
1420// initialization of other classes, so will *use* the value.
1421void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1422 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001423 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001424 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1425 // 8 as a conservative upper approximation.
1426 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001427 } else if (CheckCallers(shadow_frame,
1428 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001429 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1430 // a good upper bound.
1431 // TODO: Consider resetting in the zygote?
1432 result->SetI(8);
1433 } else {
1434 // Not supported.
1435 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1436 }
1437}
1438
1439// This allows accessing ConcurrentHashMap/SynchronousQueue.
1440
1441void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1442 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1443 // Argument 0 is the Unsafe instance, skip.
1444 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1445 if (obj == nullptr) {
1446 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1447 return;
1448 }
1449 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1450 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1451 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001452 bool success;
1453 // Check whether we're in a transaction, call accordingly.
1454 if (Runtime::Current()->IsActiveTransaction()) {
1455 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1456 expectedValue,
1457 newValue);
1458 } else {
1459 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1460 expectedValue,
1461 newValue);
1462 }
1463 result->SetZ(success ? 1 : 0);
1464}
1465
1466void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1467 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1468 // Argument 0 is the Unsafe instance, skip.
1469 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1470 if (obj == nullptr) {
1471 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1472 return;
1473 }
1474 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1475 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1476 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1477
1478 // Must use non transactional mode.
1479 if (kUseReadBarrier) {
1480 // Need to make sure the reference stored in the field is a to-space one before attempting the
1481 // CAS or the CAS could fail incorrectly.
1482 mirror::HeapReference<mirror::Object>* field_addr =
1483 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1484 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001485 ReadBarrier::Barrier<
1486 mirror::Object,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001487 /* kIsVolatile= */ false,
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001488 kWithReadBarrier,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001489 /* kAlwaysUpdateField= */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001490 obj,
1491 MemberOffset(offset),
1492 field_addr);
1493 }
1494 bool success;
1495 // Check whether we're in a transaction, call accordingly.
1496 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -07001497 success = obj->CasFieldObject<true>(MemberOffset(offset),
1498 expected_value,
1499 newValue,
1500 CASMode::kStrong,
1501 std::memory_order_seq_cst);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001502 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -07001503 success = obj->CasFieldObject<false>(MemberOffset(offset),
1504 expected_value,
1505 newValue,
1506 CASMode::kStrong,
1507 std::memory_order_seq_cst);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001508 }
1509 result->SetZ(success ? 1 : 0);
1510}
1511
1512void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1513 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001514 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001515 // 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 = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1523 result->SetL(value);
1524}
1525
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001526void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1527 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001528 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001529 // Argument 0 is the Unsafe instance, skip.
1530 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1531 if (obj == nullptr) {
1532 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1533 return;
1534 }
1535 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1536 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1537 if (Runtime::Current()->IsActiveTransaction()) {
1538 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1539 } else {
1540 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1541 }
1542}
1543
Andreas Gampebc4d2182016-02-22 10:03:12 -08001544void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1545 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001546 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001547 // Argument 0 is the Unsafe instance, skip.
1548 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1549 if (obj == nullptr) {
1550 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1551 return;
1552 }
1553 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1554 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
Orion Hodson27b96762018-03-13 16:06:57 +00001555 std::atomic_thread_fence(std::memory_order_release);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001556 if (Runtime::Current()->IsActiveTransaction()) {
1557 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1558 } else {
1559 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1560 }
1561}
1562
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001563// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1564// of correctly handling the corner cases.
1565void UnstartedRuntime::UnstartedIntegerParseInt(
1566 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001567 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001568 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1569 if (obj == nullptr) {
1570 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1571 return;
1572 }
1573
1574 std::string string_value = obj->AsString()->ToModifiedUtf8();
1575 if (string_value.empty()) {
1576 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1577 return;
1578 }
1579
1580 const char* c_str = string_value.c_str();
1581 char *end;
1582 // Can we set errno to 0? Is this always a variable, and not a macro?
1583 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1584 int64_t l = strtol(c_str, &end, 10);
1585
1586 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1587 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1588 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1589 return;
1590 }
1591 if (l == 0) {
1592 // Check whether the string wasn't exactly zero.
1593 if (string_value != "0") {
1594 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1595 return;
1596 }
1597 } else if (*end != '\0') {
1598 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1599 return;
1600 }
1601
1602 result->SetI(static_cast<int32_t>(l));
1603}
1604
1605// A cutout for Long.parseLong.
1606//
1607// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1608// well.
1609void UnstartedRuntime::UnstartedLongParseLong(
1610 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001611 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001612 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1613 if (obj == nullptr) {
1614 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1615 return;
1616 }
1617
1618 std::string string_value = obj->AsString()->ToModifiedUtf8();
1619 if (string_value.empty()) {
1620 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1621 return;
1622 }
1623
1624 const char* c_str = string_value.c_str();
1625 char *end;
1626 // Can we set errno to 0? Is this always a variable, and not a macro?
1627 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1628 int64_t l = strtol(c_str, &end, 10);
1629
1630 // Note: comparing against int32_t min/max is intentional here.
1631 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1632 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1633 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1634 return;
1635 }
1636 if (l == 0) {
1637 // Check whether the string wasn't exactly zero.
1638 if (string_value != "0") {
1639 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1640 return;
1641 }
1642 } else if (*end != '\0') {
1643 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1644 return;
1645 }
1646
1647 result->SetJ(l);
1648}
1649
Andreas Gampe715fdc22016-04-18 17:07:30 -07001650void UnstartedRuntime::UnstartedMethodInvoke(
1651 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001652 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001653 JNIEnvExt* env = self->GetJniEnv();
1654 ScopedObjectAccessUnchecked soa(self);
1655
Mathieu Chartier8778c522016-10-04 19:06:30 -07001656 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001657 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001658 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001659
Mathieu Chartier8778c522016-10-04 19:06:30 -07001660 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001661 ScopedLocalRef<jobject> java_receiver(env,
1662 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1663
Mathieu Chartier8778c522016-10-04 19:06:30 -07001664 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001665 ScopedLocalRef<jobject> java_args(env,
1666 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1667
1668 ScopedLocalRef<jobject> result_jobj(env,
1669 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1670
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001671 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001672
1673 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1674 // InvocationTargetExceptions.
1675 if (self->IsExceptionPending()) {
1676 AbortTransactionOrFail(self, "Failed Method.invoke");
1677 }
1678}
1679
Nicolas Geoffrayece2f7c2017-03-08 16:11:23 +00001680void UnstartedRuntime::UnstartedSystemIdentityHashCode(
1681 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1682 REQUIRES_SHARED(Locks::mutator_lock_) {
1683 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1684 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1685}
Andreas Gampebc4d2182016-02-22 10:03:12 -08001686
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001687// Checks whether the runtime is s64-bit. This is needed for the clinit of
1688// java.lang.invoke.VarHandle clinit. The clinit determines sets of
1689// available VarHandle accessors and these differ based on machine
1690// word size.
1691void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
1692 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1693 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
1694 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1695 jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
1696 result->SetZ(is64bit);
1697}
1698
Mathieu Chartiere401d142015-04-22 13:56:20 -07001699void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
Vladimir Marko78baed52018-10-11 10:44:58 +01001700 Thread* self,
1701 ArtMethod* method ATTRIBUTE_UNUSED,
1702 mirror::Object* receiver ATTRIBUTE_UNUSED,
1703 uint32_t* args,
1704 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001705 int32_t length = args[1];
1706 DCHECK_GE(length, 0);
Vladimir Marko78baed52018-10-11 10:44:58 +01001707 ObjPtr<mirror::Object> element_class = reinterpret_cast32<mirror::Object*>(args[0])->AsClass();
1708 if (element_class == nullptr) {
1709 AbortTransactionOrFail(self, "VMRuntime.newUnpaddedArray with null element_class.");
1710 return;
1711 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001712 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001713 ObjPtr<mirror::Class> array_class =
Vladimir Marko78baed52018-10-11 10:44:58 +01001714 runtime->GetClassLinker()->FindArrayClass(self, element_class->AsClass());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001715 DCHECK(array_class != nullptr);
1716 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001717 result->SetL(mirror::Array::Alloc<true, true>(self,
1718 array_class,
1719 length,
1720 array_class->GetComponentSizeShift(),
1721 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001722}
1723
Mathieu Chartiere401d142015-04-22 13:56:20 -07001724void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1725 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1726 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001727 result->SetL(nullptr);
1728}
1729
Mathieu Chartiere401d142015-04-22 13:56:20 -07001730void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1731 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1732 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001733 NthCallerVisitor visitor(self, 3);
1734 visitor.WalkStack();
1735 if (visitor.caller != nullptr) {
1736 result->SetL(visitor.caller->GetDeclaringClass());
1737 }
1738}
1739
Mathieu Chartiere401d142015-04-22 13:56:20 -07001740void UnstartedRuntime::UnstartedJNIMathLog(
1741 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1742 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001743 JValue value;
1744 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1745 result->SetD(log(value.GetD()));
1746}
1747
Mathieu Chartiere401d142015-04-22 13:56:20 -07001748void UnstartedRuntime::UnstartedJNIMathExp(
1749 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1750 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001751 JValue value;
1752 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1753 result->SetD(exp(value.GetD()));
1754}
1755
Andreas Gampebc4d2182016-02-22 10:03:12 -08001756void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1757 Thread* self ATTRIBUTE_UNUSED,
1758 ArtMethod* method ATTRIBUTE_UNUSED,
1759 mirror::Object* receiver ATTRIBUTE_UNUSED,
1760 uint32_t* args ATTRIBUTE_UNUSED,
1761 JValue* result) {
1762 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1763 ? 0
1764 : 1);
1765}
1766
Mathieu Chartiere401d142015-04-22 13:56:20 -07001767void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1768 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1769 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001770 StackHandleScope<1> hs(self);
1771 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1772}
1773
Andreas Gampebc4d2182016-02-22 10:03:12 -08001774void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1775 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1776 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1777 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1778 result->SetD(bit_cast<double>(long_input));
1779}
1780
Mathieu Chartiere401d142015-04-22 13:56:20 -07001781void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1782 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1783 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001784 result->SetI(args[0]);
1785}
1786
Mathieu Chartiere401d142015-04-22 13:56:20 -07001787void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1788 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1789 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001790 result->SetI(args[0]);
1791}
1792
Mathieu Chartiere401d142015-04-22 13:56:20 -07001793void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1794 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1795 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001796 result->SetL(receiver->Clone(self));
1797}
1798
Mathieu Chartiere401d142015-04-22 13:56:20 -07001799void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1800 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1801 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001802 receiver->NotifyAll(self);
1803}
1804
Vladimir Marko78baed52018-10-11 10:44:58 +01001805void UnstartedRuntime::UnstartedJNIStringCompareTo(Thread* self,
1806 ArtMethod* method ATTRIBUTE_UNUSED,
1807 mirror::Object* receiver,
1808 uint32_t* args,
1809 JValue* result) {
1810 ObjPtr<mirror::Object> rhs = reinterpret_cast32<mirror::Object*>(args[0]);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001811 if (rhs == nullptr) {
Vladimir Marko78baed52018-10-11 10:44:58 +01001812 AbortTransactionOrFail(self, "String.compareTo with null object.");
1813 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001814 }
Vladimir Marko78baed52018-10-11 10:44:58 +01001815 result->SetI(receiver->AsString()->CompareTo(rhs->AsString()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001816}
1817
Mathieu Chartiere401d142015-04-22 13:56:20 -07001818void UnstartedRuntime::UnstartedJNIStringIntern(
1819 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1820 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001821 result->SetL(receiver->AsString()->Intern());
1822}
1823
Mathieu Chartiere401d142015-04-22 13:56:20 -07001824void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1825 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1826 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001827 StackHandleScope<2> hs(self);
1828 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1829 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1830 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1831}
1832
Mathieu Chartiere401d142015-04-22 13:56:20 -07001833void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1834 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1835 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001836 int32_t length = static_cast<int32_t>(args[1]);
1837 if (length < 0) {
1838 ThrowNegativeArraySizeException(length);
1839 return;
1840 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001841 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001842 Runtime* runtime = Runtime::Current();
1843 ClassLinker* class_linker = runtime->GetClassLinker();
Vladimir Markobcf17522018-06-01 13:14:32 +01001844 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001845 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001846 CHECK(self->IsExceptionPending());
1847 return;
1848 }
1849 DCHECK(array_class->IsObjectArrayClass());
Vladimir Markobcf17522018-06-01 13:14:32 +01001850 ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc(
Andreas Gampee598e042015-04-10 14:57:10 -07001851 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1852 result->SetL(new_array);
1853}
1854
Mathieu Chartiere401d142015-04-22 13:56:20 -07001855void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1856 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1857 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001858 ScopedObjectAccessUnchecked soa(self);
1859 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001860 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001861 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001862 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001863 }
1864}
1865
Mathieu Chartiere401d142015-04-22 13:56:20 -07001866void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1867 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1868 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001869 result->SetZ(JNI_TRUE);
1870}
1871
Mathieu Chartiere401d142015-04-22 13:56:20 -07001872void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
Vladimir Marko78baed52018-10-11 10:44:58 +01001873 Thread* self,
1874 ArtMethod* method ATTRIBUTE_UNUSED,
1875 mirror::Object* receiver ATTRIBUTE_UNUSED,
1876 uint32_t* args,
1877 JValue* result) {
1878 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
1879 if (obj == nullptr) {
1880 AbortTransactionOrFail(self, "Unsafe.compareAndSwapInt with null object.");
1881 return;
1882 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001883 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1884 jint expectedValue = args[3];
1885 jint newValue = args[4];
1886 bool success;
1887 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001888 success = obj->CasField32<true>(MemberOffset(offset),
1889 expectedValue,
1890 newValue,
1891 CASMode::kStrong,
1892 std::memory_order_seq_cst);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001893 } else {
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001894 success = obj->CasField32<false>(MemberOffset(offset),
1895 expectedValue,
1896 newValue,
1897 CASMode::kStrong,
1898 std::memory_order_seq_cst);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001899 }
1900 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1901}
1902
Vladimir Marko78baed52018-10-11 10:44:58 +01001903void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(Thread* self,
1904 ArtMethod* method ATTRIBUTE_UNUSED,
1905 mirror::Object* receiver ATTRIBUTE_UNUSED,
1906 uint32_t* args,
1907 JValue* result) {
1908 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
Narayan Kamath34a316f2016-03-30 13:11:18 +01001909 if (obj == nullptr) {
Vladimir Marko78baed52018-10-11 10:44:58 +01001910 AbortTransactionOrFail(self, "Unsafe.compareAndSwapIntVolatile with null object.");
Narayan Kamath34a316f2016-03-30 13:11:18 +01001911 return;
1912 }
1913
1914 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1915 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1916}
1917
Vladimir Marko78baed52018-10-11 10:44:58 +01001918void UnstartedRuntime::UnstartedJNIUnsafePutObject(Thread* self,
1919 ArtMethod* method ATTRIBUTE_UNUSED,
1920 mirror::Object* receiver ATTRIBUTE_UNUSED,
1921 uint32_t* args,
1922 JValue* result ATTRIBUTE_UNUSED) {
1923 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
1924 if (obj == nullptr) {
1925 AbortTransactionOrFail(self, "Unsafe.putObject with null object.");
1926 return;
1927 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001928 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Vladimir Marko78baed52018-10-11 10:44:58 +01001929 ObjPtr<mirror::Object> newValue = reinterpret_cast32<mirror::Object*>(args[3]);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001930 if (Runtime::Current()->IsActiveTransaction()) {
1931 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1932 } else {
1933 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1934 }
1935}
1936
Andreas Gampe799681b2015-05-15 19:24:12 -07001937void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Vladimir Marko78baed52018-10-11 10:44:58 +01001938 Thread* self,
1939 ArtMethod* method ATTRIBUTE_UNUSED,
1940 mirror::Object* receiver ATTRIBUTE_UNUSED,
1941 uint32_t* args,
1942 JValue* result) {
1943 ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
1944 if (component == nullptr) {
1945 AbortTransactionOrFail(self, "Unsafe.getArrayBaseOffsetForComponentType with null component.");
1946 return;
1947 }
1948 Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001949 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1950}
1951
Andreas Gampe799681b2015-05-15 19:24:12 -07001952void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Vladimir Marko78baed52018-10-11 10:44:58 +01001953 Thread* self,
1954 ArtMethod* method ATTRIBUTE_UNUSED,
1955 mirror::Object* receiver ATTRIBUTE_UNUSED,
1956 uint32_t* args,
1957 JValue* result) {
1958 ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
1959 if (component == nullptr) {
1960 AbortTransactionOrFail(self, "Unsafe.getArrayIndexScaleForComponentType with null component.");
1961 return;
1962 }
1963 Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001964 result->SetI(Primitive::ComponentSize(primitive_type));
1965}
1966
Andreas Gampec55bb392018-09-21 00:02:02 +00001967using InvokeHandler = void(*)(Thread* self,
1968 ShadowFrame* shadow_frame,
1969 JValue* result,
1970 size_t arg_size);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001971
Andreas Gampec55bb392018-09-21 00:02:02 +00001972using JNIHandler = void(*)(Thread* self,
1973 ArtMethod* method,
1974 mirror::Object* receiver,
1975 uint32_t* args,
1976 JValue* result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001977
1978static bool tables_initialized_ = false;
1979static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1980static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1981
Andreas Gampe799681b2015-05-15 19:24:12 -07001982void UnstartedRuntime::InitializeInvokeHandlers() {
1983#define UNSTARTED_DIRECT(ShortName, Sig) \
1984 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1985#include "unstarted_runtime_list.h"
1986 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1987#undef UNSTARTED_RUNTIME_DIRECT_LIST
1988#undef UNSTARTED_RUNTIME_JNI_LIST
1989#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001990}
1991
Andreas Gampe799681b2015-05-15 19:24:12 -07001992void UnstartedRuntime::InitializeJNIHandlers() {
1993#define UNSTARTED_JNI(ShortName, Sig) \
1994 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1995#include "unstarted_runtime_list.h"
1996 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1997#undef UNSTARTED_RUNTIME_DIRECT_LIST
1998#undef UNSTARTED_RUNTIME_JNI_LIST
1999#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002000}
2001
Andreas Gampe799681b2015-05-15 19:24:12 -07002002void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002003 CHECK(!tables_initialized_);
2004
Andreas Gampe799681b2015-05-15 19:24:12 -07002005 InitializeInvokeHandlers();
2006 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002007
2008 tables_initialized_ = true;
2009}
2010
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002011void UnstartedRuntime::Invoke(Thread* self, const CodeItemDataAccessor& accessor,
Andreas Gampe799681b2015-05-15 19:24:12 -07002012 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002013 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
2014 // problems in core libraries.
2015 CHECK(tables_initialized_);
2016
David Sehr709b0702016-10-13 09:12:37 -07002017 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002018 const auto& iter = invoke_handlers_.find(name);
2019 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07002020 // Clear out the result in case it's not zeroed out.
Yi Kong4b22b342018-08-02 14:43:21 -07002021 result->SetL(nullptr);
Andreas Gampe715fdc22016-04-18 17:07:30 -07002022
2023 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
2024 self->PushShadowFrame(shadow_frame);
2025
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002026 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07002027
2028 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002029 } else {
2030 // Not special, continue with regular interpreter execution.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002031 ArtInterpreterToInterpreterBridge(self, accessor, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002032 }
2033}
2034
2035// 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 -07002036void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07002037 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07002038 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002039 const auto& iter = jni_handlers_.find(name);
2040 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07002041 // Clear out the result in case it's not zeroed out.
Yi Kong4b22b342018-08-02 14:43:21 -07002042 result->SetL(nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002043 (*iter->second)(self, method, receiver, args, result);
2044 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02002045 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
2046 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002047 } else {
David Sehr709b0702016-10-13 09:12:37 -07002048 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002049 "non-transactional runtime";
2050 }
2051}
2052
2053} // namespace interpreter
2054} // namespace art