blob: 07afba4ccec3b09da83b9232a292a1c901843471 [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 Gampe2969bcd2015-03-09 12:57:41 -070047#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070048#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070049#include "mirror/field-inl.h"
Narayan Kamath14832ef2016-08-05 11:44:32 +010050#include "mirror/method.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070051#include "mirror/object-inl.h"
52#include "mirror/object_array-inl.h"
53#include "mirror/string-inl.h"
Andreas Gampe373a9b52017-10-18 09:01:57 -070054#include "nativehelper/scoped_local_ref.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070055#include "nth_caller_visitor.h"
Andreas Gampe715fdc22016-04-18 17:07:30 -070056#include "reflection.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070057#include "thread-inl.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020058#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070059#include "well_known_classes.h"
60
61namespace art {
62namespace interpreter {
63
Andreas Gampe46ee31b2016-12-14 10:11:49 -080064using android::base::StringAppendV;
65using android::base::StringPrintf;
66
Andreas Gampe068b0c02015-03-11 12:44:47 -070067static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020068 __attribute__((__format__(__printf__, 2, 3)))
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070069 REQUIRES_SHARED(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020070
71static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070072 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070073 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020074 va_start(args, fmt);
75 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070076 va_end(args);
77 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020078 va_start(args, fmt);
79 std::string msg;
80 StringAppendV(&msg, fmt, args);
81 va_end(args);
82 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070083 UNREACHABLE();
84 }
85}
86
Andreas Gampe8ce9c302016-04-15 21:24:28 -070087// Restricted support for character upper case / lower case. Only support ASCII, where
88// it's easy. Abort the transaction otherwise.
89static void CharacterLowerUpper(Thread* self,
90 ShadowFrame* shadow_frame,
91 JValue* result,
92 size_t arg_offset,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -070093 bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8ce9c302016-04-15 21:24:28 -070094 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
95
96 // Only ASCII (7-bit).
97 if (!isascii(int_value)) {
98 AbortTransactionOrFail(self,
99 "Only support ASCII characters for toLowerCase/toUpperCase: %u",
100 int_value);
101 return;
102 }
103
104 std::locale c_locale("C");
105 char char_value = static_cast<char>(int_value);
106
107 if (to_lower_case) {
108 result->SetI(std::tolower(char_value, c_locale));
109 } else {
110 result->SetI(std::toupper(char_value, c_locale));
111 }
112}
113
114void UnstartedRuntime::UnstartedCharacterToLowerCase(
115 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
116 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
117}
118
119void UnstartedRuntime::UnstartedCharacterToUpperCase(
120 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
121 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
122}
123
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700124// Helper function to deal with class loading in an unstarted runtime.
125static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
126 Handle<mirror::ClassLoader> class_loader, JValue* result,
127 const std::string& method_name, bool initialize_class,
128 bool abort_if_not_found)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700129 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800130 CHECK(className != nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700131 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
132 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
133
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100134 ObjPtr<mirror::Class> found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700135 if (found == nullptr && abort_if_not_found) {
136 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700137 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
David Sehr709b0702016-10-13 09:12:37 -0700138 method_name.c_str(),
139 PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700140 }
141 return;
142 }
143 if (found != nullptr && initialize_class) {
144 StackHandleScope<1> hs(self);
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100145 HandleWrapperObjPtr<mirror::Class> h_class = hs.NewHandleWrapper(&found);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700146 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
147 CHECK(self->IsExceptionPending());
148 return;
149 }
150 }
151 result->SetL(found);
152}
153
154// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
155// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
156// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200157// actually the transaction abort exception. This must not be wrapped, as it signals an
158// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700159static void CheckExceptionGenerateClassNotFound(Thread* self)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700160 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700161 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200162 // If it is not the transaction abort exception, wrap it.
David Sehr709b0702016-10-13 09:12:37 -0700163 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200164 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700165 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
166 "ClassNotFoundException");
167 }
168 }
169}
170
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700171static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700172 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700173 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
174 if (param == nullptr) {
175 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
176 return nullptr;
177 }
178 return param->AsString();
179}
180
David Brazdila02cb112018-01-31 11:36:39 +0000181template<typename T>
182static ALWAYS_INLINE bool ShouldBlockAccessToMember(T* member, ShadowFrame* frame)
183 REQUIRES_SHARED(Locks::mutator_lock_) {
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100184 // All uses in this file are from reflection
185 constexpr hiddenapi::AccessMethod access_method = hiddenapi::kReflection;
186 return hiddenapi::GetMemberAction(
David Brazdil8ce3bfa2018-03-12 18:01:18 +0000187 member,
188 frame->GetMethod()->GetDeclaringClass()->GetClassLoader(),
David Brazdil8e1a7cb2018-03-27 08:14:25 +0000189 frame->GetMethod()->GetDeclaringClass()->GetDexCache(),
Narayan Kamathf5f1f802018-04-03 15:23:46 +0100190 access_method) == hiddenapi::kDeny;
David Brazdila02cb112018-01-31 11:36:39 +0000191}
192
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800193void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
194 ShadowFrame* shadow_frame,
195 JValue* result,
196 size_t arg_offset,
197 bool long_form,
198 const char* caller) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700199 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
200 if (class_name == nullptr) {
201 return;
202 }
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800203 bool initialize_class;
204 mirror::ClassLoader* class_loader;
205 if (long_form) {
206 initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
207 class_loader = down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
208 } else {
209 initialize_class = true;
210 // TODO: This is really only correct for the boot classpath, and for robustness we should
211 // check the caller.
212 class_loader = nullptr;
213 }
214
215 ScopedObjectAccessUnchecked soa(self);
216 if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
217 AbortTransactionOrFail(self,
218 "Only the boot classloader is supported: %s",
219 mirror::Object::PrettyTypeOf(class_loader).c_str());
220 return;
221 }
222
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700223 StackHandleScope<1> hs(self);
224 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800225 UnstartedRuntimeFindClass(self,
226 h_class_name,
227 ScopedNullHandle<mirror::ClassLoader>(),
228 result,
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800229 caller,
230 initialize_class,
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800231 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700232 CheckExceptionGenerateClassNotFound(self);
233}
234
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800235void UnstartedRuntime::UnstartedClassForName(
236 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
237 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
238}
239
Andreas Gampe799681b2015-05-15 19:24:12 -0700240void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700241 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800242 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700243}
244
Vladimir Marko7287c4d2018-02-15 10:41:07 +0000245void UnstartedRuntime::UnstartedClassGetPrimitiveClass(
246 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
247 ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
248 ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(class_name);
249 if (UNLIKELY(klass == nullptr)) {
250 DCHECK(self->IsExceptionPending());
251 AbortTransactionOrFail(self,
252 "Class.getPrimitiveClass() failed: %s",
253 self->GetException()->GetDetailMessage()->ToModifiedUtf8().c_str());
254 return;
255 }
256 result->SetL(klass);
257}
258
Andreas Gampe799681b2015-05-15 19:24:12 -0700259void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700260 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe47de0fa2017-02-15 19:29:36 -0800261 UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700262}
263
Andreas Gampe799681b2015-05-15 19:24:12 -0700264void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
266 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700267 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
268 if (param == nullptr) {
269 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
270 return;
271 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100272 Handle<mirror::Class> h_klass(hs.NewHandle(param->AsClass()));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700273
274 // Check that it's not null.
Andreas Gampefa4333d2017-02-14 11:10:34 -0800275 if (h_klass == nullptr) {
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700276 AbortTransactionOrFail(self, "Class reference is null for newInstance");
277 return;
278 }
279
280 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
281 if (Runtime::Current()->IsActiveTransaction()) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100282 if (h_klass->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200283 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700284 h_klass->PrettyClass().c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700285 return;
286 }
287 }
288
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700289 // There are two situations in which we'll abort this run.
290 // 1) If the class isn't yet initialized and initialization fails.
291 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
292 // Note that 2) could likely be handled here, but for safety abort the transaction.
293 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700294 auto* cl = Runtime::Current()->GetClassLinker();
295 if (cl->EnsureInitialized(self, h_klass, true, true)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000296 ArtMethod* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
David Brazdila02cb112018-01-31 11:36:39 +0000297 if (cons != nullptr && ShouldBlockAccessToMember(cons, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000298 cons = nullptr;
299 }
Mathieu Chartiere401d142015-04-22 13:56:20 -0700300 if (cons != nullptr) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100301 Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800302 CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700303 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700304 if (!self->IsExceptionPending()) {
305 result->SetL(h_obj.Get());
306 ok = true;
307 }
308 } else {
309 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
310 "Could not find default constructor for '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700311 h_klass->PrettyClass().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700312 }
313 }
314 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700315 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
David Sehr709b0702016-10-13 09:12:37 -0700316 h_klass->PrettyClass().c_str(),
317 mirror::Object::PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700318 }
319}
320
Andreas Gampe799681b2015-05-15 19:24:12 -0700321void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700322 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700323 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
324 // going the reflective Dex way.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100325 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
326 ObjPtr<mirror::String> name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700327 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700328 for (ArtField& field : klass->GetIFields()) {
329 if (name2->Equals(field.GetName())) {
330 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700331 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700332 }
333 }
334 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700335 for (ArtField& field : klass->GetSFields()) {
336 if (name2->Equals(field.GetName())) {
337 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700338 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700339 }
340 }
341 }
David Brazdila02cb112018-01-31 11:36:39 +0000342 if (found != nullptr && ShouldBlockAccessToMember(found, shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000343 found = nullptr;
344 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700345 if (found == nullptr) {
346 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
347 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
David Sehr709b0702016-10-13 09:12:37 -0700348 klass->PrettyDescriptor().c_str());
Andreas Gampe068b0c02015-03-11 12:44:47 -0700349 return;
350 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700351 Runtime* runtime = Runtime::Current();
Andreas Gampe542451c2016-07-26 09:02:02 -0700352 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Andreas Gampee01e3642016-07-25 13:06:04 -0700353 mirror::Field* field;
354 if (runtime->IsActiveTransaction()) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700355 if (pointer_size == PointerSize::k64) {
356 field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
357 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700358 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700359 field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
360 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700361 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700362 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700363 if (pointer_size == PointerSize::k64) {
364 field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
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, false>(
368 self, found, true);
Andreas Gampee01e3642016-07-25 13:06:04 -0700369 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700370 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700371 result->SetL(field);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700372}
373
Andreas Gampebc4d2182016-02-22 10:03:12 -0800374// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
375void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
376 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
377 // Special managed code cut-out to allow method lookup in a un-started runtime.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100378 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampebc4d2182016-02-22 10:03:12 -0800379 if (klass == nullptr) {
380 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
381 return;
382 }
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100383 ObjPtr<mirror::String> name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
384 ObjPtr<mirror::ObjectArray<mirror::Class>> args =
Andreas Gampebc4d2182016-02-22 10:03:12 -0800385 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700386 Runtime* runtime = Runtime::Current();
387 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700388 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700389 ObjPtr<mirror::Method> method;
Andreas Gampee01e3642016-07-25 13:06:04 -0700390 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700391 if (pointer_size == PointerSize::k64) {
392 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
393 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700394 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700395 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
396 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700397 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800398 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700399 if (pointer_size == PointerSize::k64) {
400 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
401 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700402 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700403 method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
404 self, klass, name, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700405 }
Andreas Gampebc4d2182016-02-22 10:03:12 -0800406 }
David Brazdila02cb112018-01-31 11:36:39 +0000407 if (method != nullptr && ShouldBlockAccessToMember(method->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000408 method = nullptr;
409 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700410 result->SetL(method);
Andreas Gampebc4d2182016-02-22 10:03:12 -0800411}
412
Andreas Gampe6039e562016-04-05 18:18:43 -0700413// Special managed code cut-out to allow constructor lookup in a un-started runtime.
414void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
415 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100416 ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
Andreas Gampe6039e562016-04-05 18:18:43 -0700417 if (klass == nullptr) {
418 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
419 return;
420 }
421 mirror::ObjectArray<mirror::Class>* args =
422 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Andreas Gampee01e3642016-07-25 13:06:04 -0700423 Runtime* runtime = Runtime::Current();
424 bool transaction = runtime->IsActiveTransaction();
Andreas Gampe542451c2016-07-26 09:02:02 -0700425 PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
Mathieu Chartier28bd2e42016-10-04 13:54:57 -0700426 ObjPtr<mirror::Constructor> constructor;
Andreas Gampee01e3642016-07-25 13:06:04 -0700427 if (transaction) {
Andreas Gampe542451c2016-07-26 09:02:02 -0700428 if (pointer_size == PointerSize::k64) {
429 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
430 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700431 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700432 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
433 true>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700434 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700435 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700436 if (pointer_size == PointerSize::k64) {
437 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
438 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700439 } else {
Andreas Gampe542451c2016-07-26 09:02:02 -0700440 constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
441 false>(self, klass, args);
Andreas Gampee01e3642016-07-25 13:06:04 -0700442 }
Andreas Gampe6039e562016-04-05 18:18:43 -0700443 }
David Brazdil5a61bb72018-01-19 16:59:46 +0000444 if (constructor != nullptr &&
David Brazdila02cb112018-01-31 11:36:39 +0000445 ShouldBlockAccessToMember(constructor->GetArtMethod(), shadow_frame)) {
David Brazdil5a61bb72018-01-19 16:59:46 +0000446 constructor = nullptr;
447 }
Andreas Gampee01e3642016-07-25 13:06:04 -0700448 result->SetL(constructor);
Andreas Gampe6039e562016-04-05 18:18:43 -0700449}
450
Andreas Gampeae78c262017-02-01 20:40:44 -0800451void UnstartedRuntime::UnstartedClassGetDeclaringClass(
452 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
453 StackHandleScope<1> hs(self);
454 Handle<mirror::Class> klass(hs.NewHandle(
455 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
456 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
457 result->SetL(nullptr);
458 return;
459 }
460 // Return null for anonymous classes.
461 JValue is_anon_result;
462 UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
463 if (is_anon_result.GetZ() != 0) {
464 result->SetL(nullptr);
465 return;
466 }
467 result->SetL(annotations::GetDeclaringClass(klass));
468}
469
Andreas Gampe633750c2016-02-19 10:49:50 -0800470void UnstartedRuntime::UnstartedClassGetEnclosingClass(
471 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
472 StackHandleScope<1> hs(self);
473 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
474 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
475 result->SetL(nullptr);
476 }
David Sehr9323e6e2016-09-13 08:58:35 -0700477 result->SetL(annotations::GetEnclosingClass(klass));
Andreas Gampe633750c2016-02-19 10:49:50 -0800478}
479
Andreas Gampe715fdc22016-04-18 17:07:30 -0700480void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
481 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
482 StackHandleScope<1> hs(self);
483 Handle<mirror::Class> klass(hs.NewHandle(
484 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
485 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
486 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
487}
488
Andreas Gampe9486a162017-02-16 15:17:47 -0800489void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
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
495 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
496 result->SetL(nullptr);
497 return;
498 }
499
500 result->SetL(annotations::GetSignatureAnnotationForClass(klass));
501}
502
Andreas Gampeae78c262017-02-01 20:40:44 -0800503void UnstartedRuntime::UnstartedClassIsAnonymousClass(
504 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
505 StackHandleScope<1> hs(self);
506 Handle<mirror::Class> klass(hs.NewHandle(
507 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
508 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
509 result->SetZ(false);
510 return;
511 }
Vladimir Marko2d3065e2018-05-22 13:56:09 +0100512 ObjPtr<mirror::String> class_name = nullptr;
Andreas Gampeae78c262017-02-01 20:40:44 -0800513 if (!annotations::GetInnerClass(klass, &class_name)) {
514 result->SetZ(false);
515 return;
516 }
517 result->SetZ(class_name == nullptr);
518}
519
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100520static MemMap FindAndExtractEntry(const std::string& jar_file,
521 const char* entry_name,
522 size_t* size,
523 std::string* error_msg) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700524 CHECK(size != nullptr);
525
526 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
527 if (zip_archive == nullptr) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100528 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700529 }
530 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
531 if (zip_entry == nullptr) {
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100532 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700533 }
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100534 MemMap tmp_map = zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg);
535 if (!tmp_map.IsValid()) {
536 return MemMap::Invalid();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700537 }
538
539 // OK, from here everything seems fine.
540 *size = zip_entry->GetUncompressedLength();
541 return tmp_map;
542}
543
544static void GetResourceAsStream(Thread* self,
545 ShadowFrame* shadow_frame,
546 JValue* result,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700547 size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700548 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
549 if (resource_obj == nullptr) {
550 AbortTransactionOrFail(self, "null name for getResourceAsStream");
551 return;
552 }
553 CHECK(resource_obj->IsString());
554 mirror::String* resource_name = resource_obj->AsString();
555
556 std::string resource_name_str = resource_name->ToModifiedUtf8();
557 if (resource_name_str.empty() || resource_name_str == "/") {
558 AbortTransactionOrFail(self,
559 "Unsupported name %s for getResourceAsStream",
560 resource_name_str.c_str());
561 return;
562 }
563 const char* resource_cstr = resource_name_str.c_str();
564 if (resource_cstr[0] == '/') {
565 resource_cstr++;
566 }
567
568 Runtime* runtime = Runtime::Current();
569
570 std::vector<std::string> split;
571 Split(runtime->GetBootClassPathString(), ':', &split);
572 if (split.empty()) {
573 AbortTransactionOrFail(self,
574 "Boot classpath not set or split error:: %s",
575 runtime->GetBootClassPathString().c_str());
576 return;
577 }
578
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100579 MemMap mem_map;
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700580 size_t map_size;
581 std::string last_error_msg; // Only store the last message (we could concatenate).
582
583 for (const std::string& jar_file : split) {
584 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100585 if (mem_map.IsValid()) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700586 break;
587 }
588 }
589
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100590 if (!mem_map.IsValid()) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700591 // Didn't find it. There's a good chance this will be the same at runtime, but still
592 // conservatively abort the transaction here.
593 AbortTransactionOrFail(self,
594 "Could not find resource %s. Last error was %s.",
595 resource_name_str.c_str(),
596 last_error_msg.c_str());
597 return;
598 }
599
600 StackHandleScope<3> hs(self);
601
602 // Create byte array for content.
603 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800604 if (h_array == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700605 AbortTransactionOrFail(self, "Could not find/create byte array class");
606 return;
607 }
608 // Copy in content.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100609 memcpy(h_array->GetData(), mem_map.Begin(), map_size);
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700610 // Be proactive releasing memory.
Vladimir Markoc34bebf2018-08-16 16:12:49 +0100611 mem_map.Reset();
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700612
613 // Create a ByteArrayInputStream.
614 Handle<mirror::Class> h_class(hs.NewHandle(
615 runtime->GetClassLinker()->FindClass(self,
616 "Ljava/io/ByteArrayInputStream;",
617 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800618 if (h_class == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700619 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
620 return;
621 }
622 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
623 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
624 return;
625 }
626
627 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800628 if (h_obj == nullptr) {
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700629 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
630 return;
631 }
632
633 auto* cl = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +0100634 ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700635 if (constructor == nullptr) {
636 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
637 return;
638 }
639
640 uint32_t args[1];
Vladimir Marko78baed52018-10-11 10:44:58 +0100641 args[0] = reinterpret_cast32<uint32_t>(h_array.Get());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700642 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
643
644 if (self->IsExceptionPending()) {
645 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
646 return;
647 }
648
649 result->SetL(h_obj.Get());
650}
651
652void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
653 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
654 {
655 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
656 CHECK(this_obj != nullptr);
657 CHECK(this_obj->IsClassLoader());
658
659 StackHandleScope<1> hs(self);
660 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
661
662 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
663 this_classloader_class.Get()) {
664 AbortTransactionOrFail(self,
David Sehr709b0702016-10-13 09:12:37 -0700665 "Unsupported classloader type %s for getResourceAsStream",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700666 mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
Andreas Gampeeb8b0ae2016-04-13 17:58:05 -0700667 return;
668 }
669 }
670
671 GetResourceAsStream(self, shadow_frame, result, arg_offset);
672}
673
Andreas Gampe85bef7e2017-02-16 18:13:26 -0800674void UnstartedRuntime::UnstartedConstructorNewInstance0(
675 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
676 // This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
677 StackHandleScope<4> hs(self);
678 Handle<mirror::Constructor> m = hs.NewHandle(
679 reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
680 Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
681 reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
682 shadow_frame->GetVRegReference(arg_offset + 1)));
683 Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
684 if (UNLIKELY(c->IsAbstract())) {
685 AbortTransactionOrFail(self, "Cannot handle abstract classes");
686 return;
687 }
688 // Verify that we can access the class.
689 if (!m->IsAccessible() && !c->IsPublic()) {
690 // Go 2 frames back, this method is always called from newInstance0, which is called from
691 // Constructor.newInstance(Object... args).
692 ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
693 // If caller is null, then we called from JNI, just avoid the check since JNI avoids most
694 // access checks anyways. TODO: Investigate if this the correct behavior.
695 if (caller != nullptr && !caller->CanAccess(c.Get())) {
696 AbortTransactionOrFail(self, "Cannot access class");
697 return;
698 }
699 }
700 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
701 DCHECK(self->IsExceptionPending());
702 return;
703 }
704 if (c->IsClassClass()) {
705 AbortTransactionOrFail(self, "new Class() is not supported");
706 return;
707 }
708
709 // String constructor is replaced by a StringFactory method in InvokeMethod.
710 if (c->IsStringClass()) {
711 // We don't support strings.
712 AbortTransactionOrFail(self, "String construction is not supported");
713 return;
714 }
715
716 Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
717 if (receiver == nullptr) {
718 AbortTransactionOrFail(self, "Could not allocate");
719 return;
720 }
721
722 // It's easier to use reflection to make the call, than create the uint32_t array.
723 {
724 ScopedObjectAccessUnchecked soa(self);
725 ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
726 soa.AddLocalReference<jobject>(m.Get()));
727 ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
728 soa.AddLocalReference<jobject>(receiver.Get()));
729 ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
730 soa.AddLocalReference<jobject>(args.Get()));
731 InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
732 }
733 if (self->IsExceptionPending()) {
734 AbortTransactionOrFail(self, "Failed running constructor");
735 } else {
736 result->SetL(receiver.Get());
737 }
738}
739
Andreas Gampe799681b2015-05-15 19:24:12 -0700740void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700741 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700742 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
743 mirror::ClassLoader* class_loader =
744 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
745 StackHandleScope<2> hs(self);
746 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
747 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
748 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
749 "VMClassLoader.findLoadedClass", false, false);
750 // This might have an error pending. But semantics are to just return null.
751 if (self->IsExceptionPending()) {
752 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
David Sehr709b0702016-10-13 09:12:37 -0700753 std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700754 if (type != "java.lang.InternalError") {
755 self->ClearException();
756 }
757 }
758}
759
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700760// Arraycopy emulation.
761// Note: we can't use any fast copy functions, as they are not available under transaction.
762
763template <typename T>
764static void PrimitiveArrayCopy(Thread* self,
765 mirror::Array* src_array, int32_t src_pos,
766 mirror::Array* dst_array, int32_t dst_pos,
767 int32_t length)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700768 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700769 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
Mathieu Chartieref41db72016-10-25 15:08:01 -0700770 AbortTransactionOrFail(self,
771 "Types mismatched in arraycopy: %s vs %s.",
772 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700773 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700774 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700775 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700776 return;
777 }
778 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
779 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
780 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
781 if (copy_forward) {
782 for (int32_t i = 0; i < length; ++i) {
783 dst->Set(dst_pos + i, src->Get(src_pos + i));
784 }
785 } else {
786 for (int32_t i = 1; i <= length; ++i) {
787 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
788 }
789 }
790}
791
Andreas Gampe799681b2015-05-15 19:24:12 -0700792void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700793 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700794 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700795 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
796 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700797 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700798
Andreas Gampe85a098a2016-03-31 13:30:53 -0700799 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
800 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
801 // Null checking. For simplicity, abort transaction.
802 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700803 AbortTransactionOrFail(self, "src is null in arraycopy.");
804 return;
805 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700806 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700807 AbortTransactionOrFail(self, "dst is null in arraycopy.");
808 return;
809 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700810 // Test for arrayness. Throw ArrayStoreException.
811 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
812 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
813 return;
814 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700815
Andreas Gampe85a098a2016-03-31 13:30:53 -0700816 mirror::Array* src_array = src_obj->AsArray();
817 mirror::Array* dst_array = dst_obj->AsArray();
818
819 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700820 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
821 UNLIKELY(src_pos > src_array->GetLength() - length) ||
822 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700823 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700824 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
825 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
826 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700827 return;
828 }
829
830 // Type checking.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100831 ObjPtr<mirror::Class> src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700832 GetComponentType();
833
834 if (!src_type->IsPrimitive()) {
835 // Check that the second type is not primitive.
Vladimir Marko6ec2a1b2018-05-22 15:33:48 +0100836 ObjPtr<mirror::Class> trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700837 GetComponentType();
838 if (trg_type->IsPrimitiveInt()) {
839 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
Mathieu Chartieref41db72016-10-25 15:08:01 -0700840 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700841 src_array->GetClass()->GetComponentType()).c_str(),
Mathieu Chartieref41db72016-10-25 15:08:01 -0700842 mirror::Class::PrettyDescriptor(
David Sehr709b0702016-10-13 09:12:37 -0700843 dst_array->GetClass()->GetComponentType()).c_str());
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700844 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700845 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700846
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700847 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
848 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
849 if (src == dst) {
850 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700851 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700852 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
853 if (copy_forward) {
854 for (int32_t i = 0; i < length; ++i) {
855 dst->Set(dst_pos + i, src->Get(src_pos + i));
856 }
857 } else {
858 for (int32_t i = 1; i <= length; ++i) {
859 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
860 }
861 }
862 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700863 // We're being lazy here. Optimally this could be a memcpy (if component types are
864 // assignable), but the ObjectArray implementation doesn't support transactions. The
865 // checking version, however, does.
866 if (Runtime::Current()->IsActiveTransaction()) {
867 dst->AssignableCheckingMemcpy<true>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700868 dst_pos, src, src_pos, length, /* throw_exception= */ true);
Andreas Gampe85a098a2016-03-31 13:30:53 -0700869 } else {
870 dst->AssignableCheckingMemcpy<false>(
Andreas Gampe98ea9d92018-10-19 14:06:15 -0700871 dst_pos, src, src_pos, length, /* throw_exception= */ true);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700872 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700873 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700874 } else if (src_type->IsPrimitiveByte()) {
875 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700876 } else if (src_type->IsPrimitiveChar()) {
877 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
878 } else if (src_type->IsPrimitiveInt()) {
879 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700880 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700881 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
David Sehr709b0702016-10-13 09:12:37 -0700882 src_type->PrettyDescriptor().c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700883 }
884}
885
Andreas Gampe5c9af612016-04-05 14:16:10 -0700886void UnstartedRuntime::UnstartedSystemArraycopyByte(
887 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
888 // Just forward.
889 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
890}
891
Andreas Gampe799681b2015-05-15 19:24:12 -0700892void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700893 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700894 // Just forward.
895 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
896}
897
898void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700900 // Just forward.
901 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
902}
903
Narayan Kamath34a316f2016-03-30 13:11:18 +0100904void UnstartedRuntime::UnstartedSystemGetSecurityManager(
905 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
906 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
907 result->SetL(nullptr);
908}
909
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700910static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
911
912static void GetSystemProperty(Thread* self,
913 ShadowFrame* shadow_frame,
914 JValue* result,
915 size_t arg_offset,
916 bool is_default_version)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700917 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700918 StackHandleScope<4> hs(self);
919 Handle<mirror::String> h_key(
920 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800921 if (h_key == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700922 AbortTransactionOrFail(self, "getProperty key was null");
923 return;
924 }
925
926 // This is overall inefficient, but reflecting the values here is not great, either. So
927 // for simplicity, and with the assumption that the number of getProperty calls is not
928 // too great, just iterate each time.
929
930 // Get the storage class.
931 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
932 Handle<mirror::Class> h_props_class(hs.NewHandle(
933 class_linker->FindClass(self,
934 "Ljava/lang/AndroidHardcodedSystemProperties;",
935 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800936 if (h_props_class == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700937 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
938 return;
939 }
940 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
941 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
942 return;
943 }
944
945 // Get the storage array.
946 ArtField* static_properties =
947 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
948 "[[Ljava/lang/String;");
949 if (static_properties == nullptr) {
950 AbortTransactionOrFail(self,
951 "Could not find %s field",
952 kAndroidHardcodedSystemPropertiesFieldName);
953 return;
954 }
Mathieu Chartier3398c782016-09-30 10:27:43 -0700955 ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
956 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
957 props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800958 if (h_2string_array == nullptr) {
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700959 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
960 return;
961 }
962
963 // Iterate over it.
964 const int32_t prop_count = h_2string_array->GetLength();
965 // Use the third handle as mutable.
966 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
967 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
968 for (int32_t i = 0; i < prop_count; ++i) {
969 h_string_array.Assign(h_2string_array->Get(i));
Andreas Gampefa4333d2017-02-14 11:10:34 -0800970 if (h_string_array == nullptr ||
Andreas Gamped4fa9f42016-04-13 14:53:23 -0700971 h_string_array->GetLength() != 2 ||
972 h_string_array->Get(0) == nullptr) {
973 AbortTransactionOrFail(self,
974 "Unexpected content of %s",
975 kAndroidHardcodedSystemPropertiesFieldName);
976 return;
977 }
978 if (h_key->Equals(h_string_array->Get(0))) {
979 // Found a value.
980 if (h_string_array->Get(1) == nullptr && is_default_version) {
981 // Null is being delegated to the default map, and then resolved to the given default value.
982 // As there's no default map, return the given value.
983 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
984 } else {
985 result->SetL(h_string_array->Get(1));
986 }
987 return;
988 }
989 }
990
991 // Key is not supported.
992 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
993}
994
995void UnstartedRuntime::UnstartedSystemGetProperty(
996 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
997 GetSystemProperty(self, shadow_frame, result, arg_offset, false);
998}
999
1000void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
1001 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1002 GetSystemProperty(self, shadow_frame, result, arg_offset, true);
1003}
1004
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001005static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
1006 REQUIRES_SHARED(Locks::mutator_lock_) {
1007 if (shadow_frame->GetLink() == nullptr) {
1008 return "<no caller>";
1009 }
1010 return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1011}
1012
1013static bool CheckCallers(ShadowFrame* shadow_frame,
1014 std::initializer_list<std::string> allowed_call_stack)
1015 REQUIRES_SHARED(Locks::mutator_lock_) {
1016 for (const std::string& allowed_caller : allowed_call_stack) {
1017 if (shadow_frame->GetLink() == nullptr) {
1018 return false;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001019 }
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001020
1021 std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
1022 if (allowed_caller != found_caller) {
1023 return false;
1024 }
1025
1026 shadow_frame = shadow_frame->GetLink();
1027 }
1028 return true;
1029}
1030
1031static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
1032 REQUIRES_SHARED(Locks::mutator_lock_) {
1033 // Find the requested class.
1034 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
1035 ObjPtr<mirror::Class> klass =
1036 class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
1037 if (klass == nullptr) {
1038 AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
1039 return nullptr;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001040 }
1041
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001042 StackHandleScope<2> hs(self);
1043 Handle<mirror::Class> h_class(hs.NewHandle(klass));
1044 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
Andreas Gampefa4333d2017-02-14 11:10:34 -08001045 if (h_obj != nullptr) {
Vladimir Markoba118822017-06-12 15:41:56 +01001046 ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001047 if (init_method == nullptr) {
1048 AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
1049 return nullptr;
1050 } else {
1051 JValue invoke_result;
1052 EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
1053 if (!self->IsExceptionPending()) {
1054 return h_obj.Get();
1055 }
1056 AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
1057 }
1058 }
1059 AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
1060 return nullptr;
1061}
1062
1063void UnstartedRuntime::UnstartedThreadLocalGet(
1064 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1065 if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
1066 "sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
1067 result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
1068 } else {
1069 AbortTransactionOrFail(self,
1070 "ThreadLocal.get() does not support %s",
1071 GetImmediateCaller(shadow_frame).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001072 }
1073}
1074
Andreas Gampebad529d2017-02-13 18:52:10 -08001075void UnstartedRuntime::UnstartedThreadCurrentThread(
1076 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1077 if (CheckCallers(shadow_frame,
1078 { "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Paul Duffin7ec95c52018-08-01 15:09:37 +01001079 "java.lang.String, long, java.security.AccessControlContext)",
1080 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Andreas Gampebad529d2017-02-13 18:52:10 -08001081 "java.lang.String, long)",
1082 "void java.lang.Thread.<init>()",
1083 "void java.util.logging.LogManager$Cleaner.<init>("
1084 "java.util.logging.LogManager)" })) {
1085 // Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
1086 // Thread constructor only asks for the current thread to set up defaults and add the
1087 // thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
1088 // these purposes.
1089 Runtime::Current()->InitThreadGroups(self);
1090 jobject main_peer =
1091 self->CreateCompileTimePeer(self->GetJniEnv(),
1092 "main",
1093 false,
1094 Runtime::Current()->GetMainThreadGroup());
1095 if (main_peer == nullptr) {
1096 AbortTransactionOrFail(self, "Failed allocating peer");
1097 return;
1098 }
1099
1100 result->SetL(self->DecodeJObject(main_peer));
1101 self->GetJniEnv()->DeleteLocalRef(main_peer);
1102 } else {
1103 AbortTransactionOrFail(self,
1104 "Thread.currentThread() does not support %s",
1105 GetImmediateCaller(shadow_frame).c_str());
1106 }
1107}
1108
1109void UnstartedRuntime::UnstartedThreadGetNativeState(
1110 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
1111 if (CheckCallers(shadow_frame,
1112 { "java.lang.Thread$State java.lang.Thread.getState()",
1113 "java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
1114 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Paul Duffin7ec95c52018-08-01 15:09:37 +01001115 "java.lang.String, long, java.security.AccessControlContext)",
1116 "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
Andreas Gampebad529d2017-02-13 18:52:10 -08001117 "java.lang.String, long)",
1118 "void java.lang.Thread.<init>()",
1119 "void java.util.logging.LogManager$Cleaner.<init>("
1120 "java.util.logging.LogManager)" })) {
1121 // Whitelist reading the state of the "main" thread when creating another (unstarted) thread
1122 // for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
1123 constexpr int32_t kJavaRunnable = 1;
1124 result->SetI(kJavaRunnable);
1125 } else {
1126 AbortTransactionOrFail(self,
1127 "Thread.getNativeState() does not support %s",
1128 GetImmediateCaller(shadow_frame).c_str());
1129 }
1130}
1131
Sergio Giro83261202016-04-11 20:49:20 +01001132void UnstartedRuntime::UnstartedMathCeil(
1133 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001134 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
Sergio Giro83261202016-04-11 20:49:20 +01001135}
1136
1137void UnstartedRuntime::UnstartedMathFloor(
1138 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe89e3b482016-04-12 18:07:36 -07001139 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001140}
1141
Andreas Gampeb8a00f92016-04-18 20:51:13 -07001142void UnstartedRuntime::UnstartedMathSin(
1143 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1144 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
1145}
1146
1147void UnstartedRuntime::UnstartedMathCos(
1148 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1149 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
1150}
1151
1152void UnstartedRuntime::UnstartedMathPow(
1153 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1154 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
1155 shadow_frame->GetVRegDouble(arg_offset + 2)));
1156}
1157
Andreas Gampe799681b2015-05-15 19:24:12 -07001158void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001159 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001160 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1161 result->SetI(obj->IdentityHashCode());
1162}
1163
Andreas Gampe799681b2015-05-15 19:24:12 -07001164void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -07001165 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001166 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +00001167 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001168}
1169
Andreas Gampedd9d0552015-03-09 12:57:41 -07001170static void UnstartedMemoryPeek(
1171 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1172 int64_t address = shadow_frame->GetVRegLong(arg_offset);
1173 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
1174 // aborting the transaction.
1175
1176 switch (type) {
1177 case Primitive::kPrimByte: {
1178 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
1179 return;
1180 }
1181
1182 case Primitive::kPrimShort: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001183 using unaligned_short __attribute__((__aligned__(1))) = int16_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001184 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001185 return;
1186 }
1187
1188 case Primitive::kPrimInt: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001189 using unaligned_int __attribute__((__aligned__(1))) = int32_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001190 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001191 return;
1192 }
1193
1194 case Primitive::kPrimLong: {
Andreas Gampec55bb392018-09-21 00:02:02 +00001195 using unaligned_long __attribute__((__aligned__(1))) = int64_t;
Andreas Gampe799681b2015-05-15 19:24:12 -07001196 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -07001197 return;
1198 }
1199
1200 case Primitive::kPrimBoolean:
1201 case Primitive::kPrimChar:
1202 case Primitive::kPrimFloat:
1203 case Primitive::kPrimDouble:
1204 case Primitive::kPrimVoid:
1205 case Primitive::kPrimNot:
1206 LOG(FATAL) << "Not in the Memory API: " << type;
1207 UNREACHABLE();
1208 }
1209 LOG(FATAL) << "Should not reach here";
1210 UNREACHABLE();
1211}
1212
Andreas Gampe799681b2015-05-15 19:24:12 -07001213void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001215 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
1216}
1217
1218void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001219 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001220 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
1221}
1222
1223void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001224 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001225 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
1226}
1227
1228void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001229 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001230 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001231}
1232
1233static void UnstartedMemoryPeekArray(
1234 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001235 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -07001236 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
1237 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
1238 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001239 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -07001240 return;
1241 }
1242 mirror::Array* array = obj->AsArray();
1243
1244 int offset = shadow_frame->GetVReg(arg_offset + 3);
1245 int count = shadow_frame->GetVReg(arg_offset + 4);
1246 if (offset < 0 || offset + count > array->GetLength()) {
1247 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
1248 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +02001249 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -07001250 return;
1251 }
1252
1253 switch (type) {
1254 case Primitive::kPrimByte: {
1255 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
1256 mirror::ByteArray* byte_array = array->AsByteArray();
1257 for (int32_t i = 0; i < count; ++i, ++address) {
1258 byte_array->SetWithoutChecks<true>(i + offset, *address);
1259 }
1260 return;
1261 }
1262
1263 case Primitive::kPrimShort:
1264 case Primitive::kPrimInt:
1265 case Primitive::kPrimLong:
1266 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
1267 UNREACHABLE();
1268
1269 case Primitive::kPrimBoolean:
1270 case Primitive::kPrimChar:
1271 case Primitive::kPrimFloat:
1272 case Primitive::kPrimDouble:
1273 case Primitive::kPrimVoid:
1274 case Primitive::kPrimNot:
1275 LOG(FATAL) << "Not in the Memory API: " << type;
1276 UNREACHABLE();
1277 }
1278 LOG(FATAL) << "Should not reach here";
1279 UNREACHABLE();
1280}
1281
Andreas Gampe799681b2015-05-15 19:24:12 -07001282void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001283 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -07001284 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -07001285}
1286
Kenny Root1c9e61c2015-05-14 15:58:17 -07001287// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001288void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001289 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001290 jint start = shadow_frame->GetVReg(arg_offset + 1);
1291 jint end = shadow_frame->GetVReg(arg_offset + 2);
1292 jint index = shadow_frame->GetVReg(arg_offset + 4);
1293 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1294 if (string == nullptr) {
1295 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
1296 return;
1297 }
Kenny Root57f91e82015-05-14 15:58:17 -07001298 DCHECK_GE(start, 0);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001299 DCHECK_LE(start, end);
1300 DCHECK_LE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001301 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001302 Handle<mirror::CharArray> h_char_array(
1303 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Vladimir Markofdaf0f42016-10-13 19:29:53 +01001304 DCHECK_GE(index, 0);
Kenny Root57f91e82015-05-14 15:58:17 -07001305 DCHECK_LE(index, h_char_array->GetLength());
1306 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001307 string->GetChars(start, end, h_char_array, index);
1308}
1309
1310// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001311void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001312 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001313 jint index = shadow_frame->GetVReg(arg_offset + 1);
1314 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1315 if (string == nullptr) {
1316 AbortTransactionOrFail(self, "String.charAt with null object");
1317 return;
1318 }
1319 result->SetC(string->CharAt(index));
1320}
1321
Vladimir Marko92907f32017-02-20 14:08:30 +00001322// This allows creating String objects with replaced characters during compilation.
1323// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
1324void UnstartedRuntime::UnstartedStringDoReplace(
1325 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1326 jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
1327 jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001328 StackHandleScope<1> hs(self);
1329 Handle<mirror::String> string =
1330 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
Kenny Root57f91e82015-05-14 15:58:17 -07001331 if (string == nullptr) {
Vladimir Marko92907f32017-02-20 14:08:30 +00001332 AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
Kenny Root57f91e82015-05-14 15:58:17 -07001333 return;
1334 }
Vladimir Marko9e57aba2017-03-16 10:45:40 +00001335 result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
Kenny Root57f91e82015-05-14 15:58:17 -07001336}
1337
Kenny Root1c9e61c2015-05-14 15:58:17 -07001338// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001339void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001340 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001341 jint offset = shadow_frame->GetVReg(arg_offset);
1342 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
1343 DCHECK_GE(char_count, 0);
1344 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001345 Handle<mirror::CharArray> h_char_array(
1346 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -07001347 Runtime* runtime = Runtime::Current();
1348 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1349 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
1350}
1351
1352// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001353void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001354 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -07001355 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
1356 if (to_copy == nullptr) {
1357 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
1358 return;
1359 }
1360 StackHandleScope<1> hs(self);
1361 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
1362 Runtime* runtime = Runtime::Current();
1363 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1364 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
1365 allocator));
1366}
1367
Andreas Gampe799681b2015-05-15 19:24:12 -07001368void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001369 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -07001370 jint start = shadow_frame->GetVReg(arg_offset + 1);
1371 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -07001372 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -07001373 DCHECK_GE(length, 0);
1374 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001375 Handle<mirror::String> h_string(
1376 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -07001377 DCHECK_LE(start, h_string->GetLength());
1378 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -07001379 Runtime* runtime = Runtime::Current();
1380 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1381 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
1382}
1383
Kenny Root57f91e82015-05-14 15:58:17 -07001384// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -07001385void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -07001386 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001387 REQUIRES_SHARED(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -07001388 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
1389 if (string == nullptr) {
1390 AbortTransactionOrFail(self, "String.charAt with null object");
1391 return;
1392 }
1393 result->SetL(string->ToCharArray(self));
1394}
1395
Andreas Gampebc4d2182016-02-22 10:03:12 -08001396// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
1397void UnstartedRuntime::UnstartedReferenceGetReferent(
1398 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001399 ObjPtr<mirror::Reference> const ref = down_cast<mirror::Reference*>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001400 shadow_frame->GetVRegReference(arg_offset));
1401 if (ref == nullptr) {
1402 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
1403 return;
1404 }
Mathieu Chartier5d3f73a2016-10-14 14:28:47 -07001405 ObjPtr<mirror::Object> const referent =
Andreas Gampebc4d2182016-02-22 10:03:12 -08001406 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
1407 result->SetL(referent);
1408}
1409
1410// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
1411// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
1412// where we can predict the behavior (somewhat).
1413// Note: this is required (instead of lazy initialization) as these classes are used in the static
1414// initialization of other classes, so will *use* the value.
1415void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
1416 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001417 if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001418 // SynchronousQueue really only separates between single- and multiprocessor case. Return
1419 // 8 as a conservative upper approximation.
1420 result->SetI(8);
Andreas Gampe3d2fcaa2017-02-09 12:50:52 -08001421 } else if (CheckCallers(shadow_frame,
1422 { "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001423 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
1424 // a good upper bound.
1425 // TODO: Consider resetting in the zygote?
1426 result->SetI(8);
1427 } else {
1428 // Not supported.
1429 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
1430 }
1431}
1432
1433// This allows accessing ConcurrentHashMap/SynchronousQueue.
1434
1435void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
1436 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1437 // Argument 0 is the Unsafe instance, skip.
1438 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1439 if (obj == nullptr) {
1440 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1441 return;
1442 }
1443 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1444 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
1445 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001446 bool success;
1447 // Check whether we're in a transaction, call accordingly.
1448 if (Runtime::Current()->IsActiveTransaction()) {
1449 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1450 expectedValue,
1451 newValue);
1452 } else {
1453 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1454 expectedValue,
1455 newValue);
1456 }
1457 result->SetZ(success ? 1 : 0);
1458}
1459
1460void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1461 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1462 // Argument 0 is the Unsafe instance, skip.
1463 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1464 if (obj == nullptr) {
1465 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1466 return;
1467 }
1468 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1469 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1470 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1471
1472 // Must use non transactional mode.
1473 if (kUseReadBarrier) {
1474 // Need to make sure the reference stored in the field is a to-space one before attempting the
1475 // CAS or the CAS could fail incorrectly.
1476 mirror::HeapReference<mirror::Object>* field_addr =
1477 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1478 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001479 ReadBarrier::Barrier<
1480 mirror::Object,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001481 /* kIsVolatile= */ false,
Hans Boehmcc55e1d2017-07-27 15:28:07 -07001482 kWithReadBarrier,
Andreas Gampe98ea9d92018-10-19 14:06:15 -07001483 /* kAlwaysUpdateField= */ true>(
Andreas Gampebc4d2182016-02-22 10:03:12 -08001484 obj,
1485 MemberOffset(offset),
1486 field_addr);
1487 }
1488 bool success;
1489 // Check whether we're in a transaction, call accordingly.
1490 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartiera9746b92018-06-22 10:25:40 -07001491 success = obj->CasFieldObject<true>(MemberOffset(offset),
1492 expected_value,
1493 newValue,
1494 CASMode::kStrong,
1495 std::memory_order_seq_cst);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001496 } else {
Mathieu Chartiera9746b92018-06-22 10:25:40 -07001497 success = obj->CasFieldObject<false>(MemberOffset(offset),
1498 expected_value,
1499 newValue,
1500 CASMode::kStrong,
1501 std::memory_order_seq_cst);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001502 }
1503 result->SetZ(success ? 1 : 0);
1504}
1505
1506void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1507 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001508 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001509 // Argument 0 is the Unsafe instance, skip.
1510 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1511 if (obj == nullptr) {
1512 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1513 return;
1514 }
1515 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1516 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1517 result->SetL(value);
1518}
1519
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001520void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1521 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001522 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001523 // Argument 0 is the Unsafe instance, skip.
1524 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1525 if (obj == nullptr) {
1526 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1527 return;
1528 }
1529 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1530 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1531 if (Runtime::Current()->IsActiveTransaction()) {
1532 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1533 } else {
1534 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1535 }
1536}
1537
Andreas Gampebc4d2182016-02-22 10:03:12 -08001538void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1539 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001540 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampebc4d2182016-02-22 10:03:12 -08001541 // Argument 0 is the Unsafe instance, skip.
1542 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1543 if (obj == nullptr) {
1544 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1545 return;
1546 }
1547 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1548 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
Orion Hodson27b96762018-03-13 16:06:57 +00001549 std::atomic_thread_fence(std::memory_order_release);
Andreas Gampebc4d2182016-02-22 10:03:12 -08001550 if (Runtime::Current()->IsActiveTransaction()) {
1551 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1552 } else {
1553 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1554 }
1555}
1556
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001557// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1558// of correctly handling the corner cases.
1559void UnstartedRuntime::UnstartedIntegerParseInt(
1560 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001561 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001562 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1563 if (obj == nullptr) {
1564 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1565 return;
1566 }
1567
1568 std::string string_value = obj->AsString()->ToModifiedUtf8();
1569 if (string_value.empty()) {
1570 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1571 return;
1572 }
1573
1574 const char* c_str = string_value.c_str();
1575 char *end;
1576 // Can we set errno to 0? Is this always a variable, and not a macro?
1577 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1578 int64_t l = strtol(c_str, &end, 10);
1579
1580 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1581 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1582 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1583 return;
1584 }
1585 if (l == 0) {
1586 // Check whether the string wasn't exactly zero.
1587 if (string_value != "0") {
1588 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1589 return;
1590 }
1591 } else if (*end != '\0') {
1592 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1593 return;
1594 }
1595
1596 result->SetI(static_cast<int32_t>(l));
1597}
1598
1599// A cutout for Long.parseLong.
1600//
1601// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1602// well.
1603void UnstartedRuntime::UnstartedLongParseLong(
1604 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001605 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001606 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1607 if (obj == nullptr) {
1608 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1609 return;
1610 }
1611
1612 std::string string_value = obj->AsString()->ToModifiedUtf8();
1613 if (string_value.empty()) {
1614 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1615 return;
1616 }
1617
1618 const char* c_str = string_value.c_str();
1619 char *end;
1620 // Can we set errno to 0? Is this always a variable, and not a macro?
1621 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1622 int64_t l = strtol(c_str, &end, 10);
1623
1624 // Note: comparing against int32_t min/max is intentional here.
1625 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1626 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1627 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1628 return;
1629 }
1630 if (l == 0) {
1631 // Check whether the string wasn't exactly zero.
1632 if (string_value != "0") {
1633 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1634 return;
1635 }
1636 } else if (*end != '\0') {
1637 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1638 return;
1639 }
1640
1641 result->SetJ(l);
1642}
1643
Andreas Gampe715fdc22016-04-18 17:07:30 -07001644void UnstartedRuntime::UnstartedMethodInvoke(
1645 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001646 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe715fdc22016-04-18 17:07:30 -07001647 JNIEnvExt* env = self->GetJniEnv();
1648 ScopedObjectAccessUnchecked soa(self);
1649
Mathieu Chartier8778c522016-10-04 19:06:30 -07001650 ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001651 ScopedLocalRef<jobject> java_method(env,
Roland Levillain5e8d5f02016-10-18 18:03:43 +01001652 java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001653
Mathieu Chartier8778c522016-10-04 19:06:30 -07001654 ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001655 ScopedLocalRef<jobject> java_receiver(env,
1656 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
1657
Mathieu Chartier8778c522016-10-04 19:06:30 -07001658 ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
Andreas Gampe715fdc22016-04-18 17:07:30 -07001659 ScopedLocalRef<jobject> java_args(env,
1660 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
1661
1662 ScopedLocalRef<jobject> result_jobj(env,
1663 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
1664
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001665 result->SetL(self->DecodeJObject(result_jobj.get()));
Andreas Gampe715fdc22016-04-18 17:07:30 -07001666
1667 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
1668 // InvocationTargetExceptions.
1669 if (self->IsExceptionPending()) {
1670 AbortTransactionOrFail(self, "Failed Method.invoke");
1671 }
1672}
1673
Nicolas Geoffrayece2f7c2017-03-08 16:11:23 +00001674void UnstartedRuntime::UnstartedSystemIdentityHashCode(
1675 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1676 REQUIRES_SHARED(Locks::mutator_lock_) {
1677 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1678 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1679}
Andreas Gampebc4d2182016-02-22 10:03:12 -08001680
Orion Hodson43f0cdb2017-10-10 14:47:32 +01001681// Checks whether the runtime is s64-bit. This is needed for the clinit of
1682// java.lang.invoke.VarHandle clinit. The clinit determines sets of
1683// available VarHandle accessors and these differ based on machine
1684// word size.
1685void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
1686 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1687 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
1688 PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
1689 jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
1690 result->SetZ(is64bit);
1691}
1692
Mathieu Chartiere401d142015-04-22 13:56:20 -07001693void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
Vladimir Marko78baed52018-10-11 10:44:58 +01001694 Thread* self,
1695 ArtMethod* method ATTRIBUTE_UNUSED,
1696 mirror::Object* receiver ATTRIBUTE_UNUSED,
1697 uint32_t* args,
1698 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001699 int32_t length = args[1];
1700 DCHECK_GE(length, 0);
Vladimir Marko78baed52018-10-11 10:44:58 +01001701 ObjPtr<mirror::Object> element_class = reinterpret_cast32<mirror::Object*>(args[0])->AsClass();
1702 if (element_class == nullptr) {
1703 AbortTransactionOrFail(self, "VMRuntime.newUnpaddedArray with null element_class.");
1704 return;
1705 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001706 Runtime* runtime = Runtime::Current();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001707 ObjPtr<mirror::Class> array_class =
Vladimir Marko78baed52018-10-11 10:44:58 +01001708 runtime->GetClassLinker()->FindArrayClass(self, element_class->AsClass());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001709 DCHECK(array_class != nullptr);
1710 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001711 result->SetL(mirror::Array::Alloc<true, true>(self,
1712 array_class,
1713 length,
1714 array_class->GetComponentSizeShift(),
1715 allocator));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001716}
1717
Mathieu Chartiere401d142015-04-22 13:56:20 -07001718void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1719 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1720 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001721 result->SetL(nullptr);
1722}
1723
Mathieu Chartiere401d142015-04-22 13:56:20 -07001724void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1725 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1726 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001727 NthCallerVisitor visitor(self, 3);
1728 visitor.WalkStack();
1729 if (visitor.caller != nullptr) {
1730 result->SetL(visitor.caller->GetDeclaringClass());
1731 }
1732}
1733
Mathieu Chartiere401d142015-04-22 13:56:20 -07001734void UnstartedRuntime::UnstartedJNIMathLog(
1735 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1736 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001737 JValue value;
1738 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1739 result->SetD(log(value.GetD()));
1740}
1741
Mathieu Chartiere401d142015-04-22 13:56:20 -07001742void UnstartedRuntime::UnstartedJNIMathExp(
1743 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1744 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001745 JValue value;
1746 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1747 result->SetD(exp(value.GetD()));
1748}
1749
Andreas Gampebc4d2182016-02-22 10:03:12 -08001750void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1751 Thread* self ATTRIBUTE_UNUSED,
1752 ArtMethod* method ATTRIBUTE_UNUSED,
1753 mirror::Object* receiver ATTRIBUTE_UNUSED,
1754 uint32_t* args ATTRIBUTE_UNUSED,
1755 JValue* result) {
1756 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1757 ? 0
1758 : 1);
1759}
1760
Mathieu Chartiere401d142015-04-22 13:56:20 -07001761void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1762 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1763 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001764 StackHandleScope<1> hs(self);
1765 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1766}
1767
Andreas Gampebc4d2182016-02-22 10:03:12 -08001768void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1769 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1770 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1771 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1772 result->SetD(bit_cast<double>(long_input));
1773}
1774
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1776 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1777 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001778 result->SetI(args[0]);
1779}
1780
Mathieu Chartiere401d142015-04-22 13:56:20 -07001781void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
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::UnstartedJNIObjectInternalClone(
1788 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1789 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001790 result->SetL(receiver->Clone(self));
1791}
1792
Mathieu Chartiere401d142015-04-22 13:56:20 -07001793void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1794 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1795 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001796 receiver->NotifyAll(self);
1797}
1798
Vladimir Marko78baed52018-10-11 10:44:58 +01001799void UnstartedRuntime::UnstartedJNIStringCompareTo(Thread* self,
1800 ArtMethod* method ATTRIBUTE_UNUSED,
1801 mirror::Object* receiver,
1802 uint32_t* args,
1803 JValue* result) {
1804 ObjPtr<mirror::Object> rhs = reinterpret_cast32<mirror::Object*>(args[0]);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001805 if (rhs == nullptr) {
Vladimir Marko78baed52018-10-11 10:44:58 +01001806 AbortTransactionOrFail(self, "String.compareTo with null object.");
1807 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001808 }
Vladimir Marko78baed52018-10-11 10:44:58 +01001809 result->SetI(receiver->AsString()->CompareTo(rhs->AsString()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001810}
1811
Mathieu Chartiere401d142015-04-22 13:56:20 -07001812void UnstartedRuntime::UnstartedJNIStringIntern(
1813 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1814 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001815 result->SetL(receiver->AsString()->Intern());
1816}
1817
Mathieu Chartiere401d142015-04-22 13:56:20 -07001818void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1819 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1820 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001821 StackHandleScope<2> hs(self);
1822 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1823 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1824 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1825}
1826
Mathieu Chartiere401d142015-04-22 13:56:20 -07001827void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1828 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1829 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001830 int32_t length = static_cast<int32_t>(args[1]);
1831 if (length < 0) {
1832 ThrowNegativeArraySizeException(length);
1833 return;
1834 }
Mathieu Chartierbc5a7952016-10-17 15:46:31 -07001835 ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Andreas Gampee598e042015-04-10 14:57:10 -07001836 Runtime* runtime = Runtime::Current();
1837 ClassLinker* class_linker = runtime->GetClassLinker();
Vladimir Markobcf17522018-06-01 13:14:32 +01001838 ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001839 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001840 CHECK(self->IsExceptionPending());
1841 return;
1842 }
1843 DCHECK(array_class->IsObjectArrayClass());
Vladimir Markobcf17522018-06-01 13:14:32 +01001844 ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc(
Andreas Gampee598e042015-04-10 14:57:10 -07001845 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1846 result->SetL(new_array);
1847}
1848
Mathieu Chartiere401d142015-04-22 13:56:20 -07001849void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1850 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1851 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001852 ScopedObjectAccessUnchecked soa(self);
1853 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001854 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001855 } else {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001856 result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001857 }
1858}
1859
Mathieu Chartiere401d142015-04-22 13:56:20 -07001860void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1861 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1862 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001863 result->SetZ(JNI_TRUE);
1864}
1865
Mathieu Chartiere401d142015-04-22 13:56:20 -07001866void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
Vladimir Marko78baed52018-10-11 10:44:58 +01001867 Thread* self,
1868 ArtMethod* method ATTRIBUTE_UNUSED,
1869 mirror::Object* receiver ATTRIBUTE_UNUSED,
1870 uint32_t* args,
1871 JValue* result) {
1872 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
1873 if (obj == nullptr) {
1874 AbortTransactionOrFail(self, "Unsafe.compareAndSwapInt with null object.");
1875 return;
1876 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001877 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1878 jint expectedValue = args[3];
1879 jint newValue = args[4];
1880 bool success;
1881 if (Runtime::Current()->IsActiveTransaction()) {
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001882 success = obj->CasField32<true>(MemberOffset(offset),
1883 expectedValue,
1884 newValue,
1885 CASMode::kStrong,
1886 std::memory_order_seq_cst);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001887 } else {
Mathieu Chartier42c2e502018-06-19 12:30:56 -07001888 success = obj->CasField32<false>(MemberOffset(offset),
1889 expectedValue,
1890 newValue,
1891 CASMode::kStrong,
1892 std::memory_order_seq_cst);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001893 }
1894 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1895}
1896
Vladimir Marko78baed52018-10-11 10:44:58 +01001897void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(Thread* self,
1898 ArtMethod* method ATTRIBUTE_UNUSED,
1899 mirror::Object* receiver ATTRIBUTE_UNUSED,
1900 uint32_t* args,
1901 JValue* result) {
1902 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
Narayan Kamath34a316f2016-03-30 13:11:18 +01001903 if (obj == nullptr) {
Vladimir Marko78baed52018-10-11 10:44:58 +01001904 AbortTransactionOrFail(self, "Unsafe.compareAndSwapIntVolatile with null object.");
Narayan Kamath34a316f2016-03-30 13:11:18 +01001905 return;
1906 }
1907
1908 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1909 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1910}
1911
Vladimir Marko78baed52018-10-11 10:44:58 +01001912void UnstartedRuntime::UnstartedJNIUnsafePutObject(Thread* self,
1913 ArtMethod* method ATTRIBUTE_UNUSED,
1914 mirror::Object* receiver ATTRIBUTE_UNUSED,
1915 uint32_t* args,
1916 JValue* result ATTRIBUTE_UNUSED) {
1917 ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
1918 if (obj == nullptr) {
1919 AbortTransactionOrFail(self, "Unsafe.putObject with null object.");
1920 return;
1921 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001922 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
Vladimir Marko78baed52018-10-11 10:44:58 +01001923 ObjPtr<mirror::Object> newValue = reinterpret_cast32<mirror::Object*>(args[3]);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001924 if (Runtime::Current()->IsActiveTransaction()) {
1925 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1926 } else {
1927 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1928 }
1929}
1930
Andreas Gampe799681b2015-05-15 19:24:12 -07001931void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Vladimir Marko78baed52018-10-11 10:44:58 +01001932 Thread* self,
1933 ArtMethod* method ATTRIBUTE_UNUSED,
1934 mirror::Object* receiver ATTRIBUTE_UNUSED,
1935 uint32_t* args,
1936 JValue* result) {
1937 ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
1938 if (component == nullptr) {
1939 AbortTransactionOrFail(self, "Unsafe.getArrayBaseOffsetForComponentType with null component.");
1940 return;
1941 }
1942 Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001943 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1944}
1945
Andreas Gampe799681b2015-05-15 19:24:12 -07001946void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Vladimir Marko78baed52018-10-11 10:44:58 +01001947 Thread* self,
1948 ArtMethod* method ATTRIBUTE_UNUSED,
1949 mirror::Object* receiver ATTRIBUTE_UNUSED,
1950 uint32_t* args,
1951 JValue* result) {
1952 ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
1953 if (component == nullptr) {
1954 AbortTransactionOrFail(self, "Unsafe.getArrayIndexScaleForComponentType with null component.");
1955 return;
1956 }
1957 Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001958 result->SetI(Primitive::ComponentSize(primitive_type));
1959}
1960
Andreas Gampec55bb392018-09-21 00:02:02 +00001961using InvokeHandler = void(*)(Thread* self,
1962 ShadowFrame* shadow_frame,
1963 JValue* result,
1964 size_t arg_size);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001965
Andreas Gampec55bb392018-09-21 00:02:02 +00001966using JNIHandler = void(*)(Thread* self,
1967 ArtMethod* method,
1968 mirror::Object* receiver,
1969 uint32_t* args,
1970 JValue* result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001971
1972static bool tables_initialized_ = false;
1973static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1974static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1975
Andreas Gampe799681b2015-05-15 19:24:12 -07001976void UnstartedRuntime::InitializeInvokeHandlers() {
1977#define UNSTARTED_DIRECT(ShortName, Sig) \
1978 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1979#include "unstarted_runtime_list.h"
1980 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1981#undef UNSTARTED_RUNTIME_DIRECT_LIST
1982#undef UNSTARTED_RUNTIME_JNI_LIST
1983#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001984}
1985
Andreas Gampe799681b2015-05-15 19:24:12 -07001986void UnstartedRuntime::InitializeJNIHandlers() {
1987#define UNSTARTED_JNI(ShortName, Sig) \
1988 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1989#include "unstarted_runtime_list.h"
1990 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1991#undef UNSTARTED_RUNTIME_DIRECT_LIST
1992#undef UNSTARTED_RUNTIME_JNI_LIST
1993#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001994}
1995
Andreas Gampe799681b2015-05-15 19:24:12 -07001996void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001997 CHECK(!tables_initialized_);
1998
Andreas Gampe799681b2015-05-15 19:24:12 -07001999 InitializeInvokeHandlers();
2000 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002001
2002 tables_initialized_ = true;
2003}
2004
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002005void UnstartedRuntime::Invoke(Thread* self, const CodeItemDataAccessor& accessor,
Andreas Gampe799681b2015-05-15 19:24:12 -07002006 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002007 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
2008 // problems in core libraries.
2009 CHECK(tables_initialized_);
2010
David Sehr709b0702016-10-13 09:12:37 -07002011 std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002012 const auto& iter = invoke_handlers_.find(name);
2013 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07002014 // Clear out the result in case it's not zeroed out.
Yi Kong4b22b342018-08-02 14:43:21 -07002015 result->SetL(nullptr);
Andreas Gampe715fdc22016-04-18 17:07:30 -07002016
2017 // Push the shadow frame. This is so the failing method can be seen in abort dumps.
2018 self->PushShadowFrame(shadow_frame);
2019
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002020 (*iter->second)(self, shadow_frame, result, arg_offset);
Andreas Gampe715fdc22016-04-18 17:07:30 -07002021
2022 self->PopShadowFrame();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002023 } else {
2024 // Not special, continue with regular interpreter execution.
Mathieu Chartier808c7a52017-12-15 11:19:33 -08002025 ArtInterpreterToInterpreterBridge(self, accessor, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002026 }
2027}
2028
2029// 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 -07002030void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07002031 uint32_t* args, JValue* result) {
David Sehr709b0702016-10-13 09:12:37 -07002032 std::string name(ArtMethod::PrettyMethod(method));
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002033 const auto& iter = jni_handlers_.find(name);
2034 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07002035 // Clear out the result in case it's not zeroed out.
Yi Kong4b22b342018-08-02 14:43:21 -07002036 result->SetL(nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002037 (*iter->second)(self, method, receiver, args, result);
2038 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02002039 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
2040 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002041 } else {
David Sehr709b0702016-10-13 09:12:37 -07002042 LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
Andreas Gampe2969bcd2015-03-09 12:57:41 -07002043 "non-transactional runtime";
2044 }
2045}
2046
2047} // namespace interpreter
2048} // namespace art