blob: 239b8259deb97c612f51c15748873f9b03cbeb1a [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
19#include <cmath>
20#include <unordered_map>
21
Andreas Gampeaacc25d2015-04-01 14:49:06 -070022#include "ScopedLocalRef.h"
23
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080025#include "base/casts.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070026#include "base/logging.h"
27#include "base/macros.h"
28#include "class_linker.h"
29#include "common_throws.h"
30#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080031#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070032#include "handle_scope-inl.h"
33#include "interpreter/interpreter_common.h"
34#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070036#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070037#include "mirror/object-inl.h"
38#include "mirror/object_array-inl.h"
39#include "mirror/string-inl.h"
40#include "nth_caller_visitor.h"
41#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020042#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070044#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070045
46namespace art {
47namespace interpreter {
48
Andreas Gampe068b0c02015-03-11 12:44:47 -070049static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020050 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070051 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020052
53static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070054 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070055 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020056 va_start(args, fmt);
57 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070058 va_end(args);
59 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020060 va_start(args, fmt);
61 std::string msg;
62 StringAppendV(&msg, fmt, args);
63 va_end(args);
64 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070065 UNREACHABLE();
66 }
67}
68
Andreas Gampe2969bcd2015-03-09 12:57:41 -070069// Helper function to deal with class loading in an unstarted runtime.
70static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
71 Handle<mirror::ClassLoader> class_loader, JValue* result,
72 const std::string& method_name, bool initialize_class,
73 bool abort_if_not_found)
Mathieu Chartier90443472015-07-16 20:32:27 -070074 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -070075 CHECK(className.Get() != nullptr);
76 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
77 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
78
79 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
80 if (found == nullptr && abort_if_not_found) {
81 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070082 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
83 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -070084 }
85 return;
86 }
87 if (found != nullptr && initialize_class) {
88 StackHandleScope<1> hs(self);
89 Handle<mirror::Class> h_class(hs.NewHandle(found));
90 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
91 CHECK(self->IsExceptionPending());
92 return;
93 }
94 }
95 result->SetL(found);
96}
97
98// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
99// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
100// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200101// actually the transaction abort exception. This must not be wrapped, as it signals an
102// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700103static void CheckExceptionGenerateClassNotFound(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700104 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700105 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200106 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700107 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200108 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700109 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
110 "ClassNotFoundException");
111 }
112 }
113}
114
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700115static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700116 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700117 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
118 if (param == nullptr) {
119 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
120 return nullptr;
121 }
122 return param->AsString();
123}
124
Andreas Gampe799681b2015-05-15 19:24:12 -0700125void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700126 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700127 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
128 if (class_name == nullptr) {
129 return;
130 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700131 StackHandleScope<1> hs(self);
132 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800133 UnstartedRuntimeFindClass(self,
134 h_class_name,
135 ScopedNullHandle<mirror::ClassLoader>(),
136 result,
137 "Class.forName",
138 true,
139 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700140 CheckExceptionGenerateClassNotFound(self);
141}
142
Andreas Gampe799681b2015-05-15 19:24:12 -0700143void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700144 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700145 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
146 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700147 return;
148 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700149 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
150 mirror::ClassLoader* class_loader =
151 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
152 StackHandleScope<2> hs(self);
153 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
154 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
155 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
156 initialize_class, false);
157 CheckExceptionGenerateClassNotFound(self);
158}
159
Andreas Gampe799681b2015-05-15 19:24:12 -0700160void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700161 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700162 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
163 if (class_name == nullptr) {
164 return;
165 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700166 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
167 mirror::ClassLoader* class_loader =
168 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
169 StackHandleScope<2> hs(self);
170 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
171 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
172 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
173 initialize_class, false);
174 CheckExceptionGenerateClassNotFound(self);
175}
176
Andreas Gampe799681b2015-05-15 19:24:12 -0700177void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700178 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
179 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700180 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
181 if (param == nullptr) {
182 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
183 return;
184 }
185 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700186 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700187
188 // Check that it's not null.
189 if (h_klass.Get() == nullptr) {
190 AbortTransactionOrFail(self, "Class reference is null for newInstance");
191 return;
192 }
193
194 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
195 if (Runtime::Current()->IsActiveTransaction()) {
196 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200197 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
198 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700199 return;
200 }
201 }
202
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700203 // There are two situations in which we'll abort this run.
204 // 1) If the class isn't yet initialized and initialization fails.
205 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
206 // Note that 2) could likely be handled here, but for safety abort the transaction.
207 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700208 auto* cl = Runtime::Current()->GetClassLinker();
209 if (cl->EnsureInitialized(self, h_klass, true, true)) {
210 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
211 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700212 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
213 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700214 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700215 if (!self->IsExceptionPending()) {
216 result->SetL(h_obj.Get());
217 ok = true;
218 }
219 } else {
220 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
221 "Could not find default constructor for '%s'",
222 PrettyClass(h_klass.Get()).c_str());
223 }
224 }
225 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700226 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
227 PrettyClass(h_klass.Get()).c_str(),
228 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700229 }
230}
231
Andreas Gampe799681b2015-05-15 19:24:12 -0700232void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700233 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700234 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
235 // going the reflective Dex way.
236 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
237 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700238 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700239 for (ArtField& field : klass->GetIFields()) {
240 if (name2->Equals(field.GetName())) {
241 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700243 }
244 }
245 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700246 for (ArtField& field : klass->GetSFields()) {
247 if (name2->Equals(field.GetName())) {
248 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700249 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700250 }
251 }
252 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700253 if (found == nullptr) {
254 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
255 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
256 PrettyDescriptor(klass).c_str());
257 return;
258 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700259 if (Runtime::Current()->IsActiveTransaction()) {
260 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
261 } else {
262 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
263 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700264}
265
Andreas Gampebc4d2182016-02-22 10:03:12 -0800266// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
267void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
268 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
269 // Special managed code cut-out to allow method lookup in a un-started runtime.
270 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
271 if (klass == nullptr) {
272 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
273 return;
274 }
275 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
276 mirror::ObjectArray<mirror::Class>* args =
277 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
278 if (Runtime::Current()->IsActiveTransaction()) {
279 result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args));
280 } else {
281 result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args));
282 }
283}
284
Andreas Gampe6039e562016-04-05 18:18:43 -0700285// Special managed code cut-out to allow constructor lookup in a un-started runtime.
286void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
287 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
288 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
289 if (klass == nullptr) {
290 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
291 return;
292 }
293 mirror::ObjectArray<mirror::Class>* args =
294 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
295 if (Runtime::Current()->IsActiveTransaction()) {
296 result->SetL(mirror::Class::GetDeclaredConstructorInternal<true>(self, klass, args));
297 } else {
298 result->SetL(mirror::Class::GetDeclaredConstructorInternal<false>(self, klass, args));
299 }
300}
301
Andreas Gampe633750c2016-02-19 10:49:50 -0800302void UnstartedRuntime::UnstartedClassGetEnclosingClass(
303 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
304 StackHandleScope<1> hs(self);
305 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
306 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
307 result->SetL(nullptr);
308 }
309 result->SetL(klass->GetDexFile().GetEnclosingClass(klass));
310}
311
Andreas Gampe799681b2015-05-15 19:24:12 -0700312void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700313 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700314 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
315 mirror::ClassLoader* class_loader =
316 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
317 StackHandleScope<2> hs(self);
318 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
319 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
320 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
321 "VMClassLoader.findLoadedClass", false, false);
322 // This might have an error pending. But semantics are to just return null.
323 if (self->IsExceptionPending()) {
324 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
325 std::string type(PrettyTypeOf(self->GetException()));
326 if (type != "java.lang.InternalError") {
327 self->ClearException();
328 }
329 }
330}
331
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332void UnstartedRuntime::UnstartedVoidLookupType(
333 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
334 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700335 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
336}
337
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700338// Arraycopy emulation.
339// Note: we can't use any fast copy functions, as they are not available under transaction.
340
341template <typename T>
342static void PrimitiveArrayCopy(Thread* self,
343 mirror::Array* src_array, int32_t src_pos,
344 mirror::Array* dst_array, int32_t dst_pos,
345 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700346 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700347 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
348 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
349 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
350 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
351 return;
352 }
353 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
354 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
355 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
356 if (copy_forward) {
357 for (int32_t i = 0; i < length; ++i) {
358 dst->Set(dst_pos + i, src->Get(src_pos + i));
359 }
360 } else {
361 for (int32_t i = 1; i <= length; ++i) {
362 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
363 }
364 }
365}
366
Andreas Gampe799681b2015-05-15 19:24:12 -0700367void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700368 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700369 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700370 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
371 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700372 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700373
Andreas Gampe85a098a2016-03-31 13:30:53 -0700374 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
375 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
376 // Null checking. For simplicity, abort transaction.
377 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700378 AbortTransactionOrFail(self, "src is null in arraycopy.");
379 return;
380 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700381 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700382 AbortTransactionOrFail(self, "dst is null in arraycopy.");
383 return;
384 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700385 // Test for arrayness. Throw ArrayStoreException.
386 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
387 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
388 return;
389 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700390
Andreas Gampe85a098a2016-03-31 13:30:53 -0700391 mirror::Array* src_array = src_obj->AsArray();
392 mirror::Array* dst_array = dst_obj->AsArray();
393
394 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700395 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
396 UNLIKELY(src_pos > src_array->GetLength() - length) ||
397 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700398 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700399 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
400 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
401 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700402 return;
403 }
404
405 // Type checking.
406 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
407 GetComponentType();
408
409 if (!src_type->IsPrimitive()) {
410 // Check that the second type is not primitive.
411 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
412 GetComponentType();
413 if (trg_type->IsPrimitiveInt()) {
414 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
415 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
416 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
417 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700418 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700419
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700420 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
421 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
422 if (src == dst) {
423 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700424 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700425 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
426 if (copy_forward) {
427 for (int32_t i = 0; i < length; ++i) {
428 dst->Set(dst_pos + i, src->Get(src_pos + i));
429 }
430 } else {
431 for (int32_t i = 1; i <= length; ++i) {
432 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
433 }
434 }
435 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700436 // We're being lazy here. Optimally this could be a memcpy (if component types are
437 // assignable), but the ObjectArray implementation doesn't support transactions. The
438 // checking version, however, does.
439 if (Runtime::Current()->IsActiveTransaction()) {
440 dst->AssignableCheckingMemcpy<true>(
441 dst_pos, src, src_pos, length, true /* throw_exception */);
442 } else {
443 dst->AssignableCheckingMemcpy<false>(
444 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700445 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700446 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700447 } else if (src_type->IsPrimitiveByte()) {
448 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700449 } else if (src_type->IsPrimitiveChar()) {
450 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
451 } else if (src_type->IsPrimitiveInt()) {
452 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700453 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700454 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700455 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700456 }
457}
458
Andreas Gampe5c9af612016-04-05 14:16:10 -0700459void UnstartedRuntime::UnstartedSystemArraycopyByte(
460 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
461 // Just forward.
462 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
463}
464
Andreas Gampe799681b2015-05-15 19:24:12 -0700465void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700466 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700467 // Just forward.
468 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
469}
470
471void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700472 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700473 // Just forward.
474 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
475}
476
Narayan Kamath34a316f2016-03-30 13:11:18 +0100477void UnstartedRuntime::UnstartedSystemGetSecurityManager(
478 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
479 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
480 result->SetL(nullptr);
481}
482
Andreas Gampe799681b2015-05-15 19:24:12 -0700483void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700484 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700485 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
486 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100487 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
488 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700489 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100490 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700491 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100492 } else if (caller ==
493 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
494 // Allocate new object.
495 StackHandleScope<2> hs(self);
496 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
497 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
498 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
499 h_real_to_string_class->AllocObject(self)));
500 if (h_real_to_string_obj.Get() != nullptr) {
501 auto* cl = Runtime::Current()->GetClassLinker();
502 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
503 "<init>", "()V", cl->GetImagePointerSize());
504 if (init_method == nullptr) {
505 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
506 } else {
507 JValue invoke_result;
508 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
509 nullptr);
510 if (!self->IsExceptionPending()) {
511 result->SetL(h_real_to_string_obj.Get());
512 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700513 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700514 }
515 }
516 }
517
518 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700519 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700520 }
521}
522
Andreas Gampe799681b2015-05-15 19:24:12 -0700523void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700524 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700525 double in = shadow_frame->GetVRegDouble(arg_offset);
526 double out;
527 // Special cases:
528 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
529 // -1 < in < 0 -> out := -0.
530 if (-1.0 < in && in < 0) {
531 out = -0.0;
532 } else {
533 out = ceil(in);
534 }
535 result->SetD(out);
536}
537
Andreas Gampe799681b2015-05-15 19:24:12 -0700538void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700539 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700540 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
541 result->SetI(obj->IdentityHashCode());
542}
543
Andreas Gampe799681b2015-05-15 19:24:12 -0700544void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700545 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700546 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000547 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700548}
549
Andreas Gampedd9d0552015-03-09 12:57:41 -0700550static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700551 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700552 const DexFile* dex_file = dex_cache->GetDexFile();
553 if (dex_file == nullptr) {
554 return nullptr;
555 }
556
557 // Create the direct byte buffer.
558 JNIEnv* env = self->GetJniEnv();
559 DCHECK(env != nullptr);
560 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700561 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
562 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700563 DCHECK(self->IsExceptionPending());
564 return nullptr;
565 }
566
567 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700568 args[0].l = byte_buffer.get();
569
570 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
571 WellKnownClasses::com_android_dex_Dex,
572 WellKnownClasses::com_android_dex_Dex_create,
573 args));
574
575 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700576}
577
Andreas Gampe799681b2015-05-15 19:24:12 -0700578void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700579 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700580 // We will create the Dex object, but the image writer will release it before creating the
581 // art file.
582 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
583 bool have_dex = false;
584 if (src != nullptr) {
585 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
586 if (dex != nullptr) {
587 have_dex = true;
588 result->SetL(dex);
589 }
590 }
591 if (!have_dex) {
592 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200593 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700594 }
595}
596
597static void UnstartedMemoryPeek(
598 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
599 int64_t address = shadow_frame->GetVRegLong(arg_offset);
600 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
601 // aborting the transaction.
602
603 switch (type) {
604 case Primitive::kPrimByte: {
605 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
606 return;
607 }
608
609 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700610 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
611 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700612 return;
613 }
614
615 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700616 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
617 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700618 return;
619 }
620
621 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700622 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
623 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700624 return;
625 }
626
627 case Primitive::kPrimBoolean:
628 case Primitive::kPrimChar:
629 case Primitive::kPrimFloat:
630 case Primitive::kPrimDouble:
631 case Primitive::kPrimVoid:
632 case Primitive::kPrimNot:
633 LOG(FATAL) << "Not in the Memory API: " << type;
634 UNREACHABLE();
635 }
636 LOG(FATAL) << "Should not reach here";
637 UNREACHABLE();
638}
639
Andreas Gampe799681b2015-05-15 19:24:12 -0700640void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700641 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700642 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
643}
644
645void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700647 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
648}
649
650void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700651 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700652 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
653}
654
655void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700656 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700657 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700658}
659
660static void UnstartedMemoryPeekArray(
661 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700662 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700663 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
664 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
665 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200666 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700667 return;
668 }
669 mirror::Array* array = obj->AsArray();
670
671 int offset = shadow_frame->GetVReg(arg_offset + 3);
672 int count = shadow_frame->GetVReg(arg_offset + 4);
673 if (offset < 0 || offset + count > array->GetLength()) {
674 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
675 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200676 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700677 return;
678 }
679
680 switch (type) {
681 case Primitive::kPrimByte: {
682 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
683 mirror::ByteArray* byte_array = array->AsByteArray();
684 for (int32_t i = 0; i < count; ++i, ++address) {
685 byte_array->SetWithoutChecks<true>(i + offset, *address);
686 }
687 return;
688 }
689
690 case Primitive::kPrimShort:
691 case Primitive::kPrimInt:
692 case Primitive::kPrimLong:
693 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
694 UNREACHABLE();
695
696 case Primitive::kPrimBoolean:
697 case Primitive::kPrimChar:
698 case Primitive::kPrimFloat:
699 case Primitive::kPrimDouble:
700 case Primitive::kPrimVoid:
701 case Primitive::kPrimNot:
702 LOG(FATAL) << "Not in the Memory API: " << type;
703 UNREACHABLE();
704 }
705 LOG(FATAL) << "Should not reach here";
706 UNREACHABLE();
707}
708
Andreas Gampe799681b2015-05-15 19:24:12 -0700709void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700710 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700711 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700712}
713
Andreas Gampef778eb22015-04-13 14:17:09 -0700714// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700715void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700716 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
717 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700718 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700719
720 std::vector<std::string> split;
721 Split(runtime->GetBootClassPathString(), ':', &split);
722 if (split.empty()) {
723 AbortTransactionOrFail(self,
724 "Boot classpath not set or split error:: %s",
725 runtime->GetBootClassPathString().c_str());
726 return;
727 }
728 const std::string& source = split[0];
729
Andreas Gampef778eb22015-04-13 14:17:09 -0700730 mirror::String* string_data;
731
732 // Use a block to enclose the I/O and MemMap code so buffers are released early.
733 {
734 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700735 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700736 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700737 AbortTransactionOrFail(self,
738 "Could not open zip file %s: %s",
739 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700740 error_msg.c_str());
741 return;
742 }
743 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
744 &error_msg));
745 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700746 AbortTransactionOrFail(self,
747 "Could not find security.properties file in %s: %s",
748 source.c_str(),
749 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700750 return;
751 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700752 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700753 "java/security/security.properties",
754 &error_msg));
755 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700756 AbortTransactionOrFail(self,
757 "Could not unzip security.properties file in %s: %s",
758 source.c_str(),
759 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700760 return;
761 }
762
763 uint32_t length = zip_entry->GetUncompressedLength();
764 std::unique_ptr<char[]> tmp(new char[length + 1]);
765 memcpy(tmp.get(), map->Begin(), length);
766 tmp.get()[length] = 0; // null terminator
767
768 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
769 }
770
771 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700772 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700773 return;
774 }
775
776 // Create a StringReader.
777 StackHandleScope<3> hs(self);
778 Handle<mirror::String> h_string(hs.NewHandle(string_data));
779
780 Handle<mirror::Class> h_class(hs.NewHandle(
781 runtime->GetClassLinker()->FindClass(self,
782 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800783 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700784 if (h_class.Get() == nullptr) {
785 AbortTransactionOrFail(self, "Could not find StringReader class");
786 return;
787 }
788
789 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
790 AbortTransactionOrFail(self, "Could not initialize StringReader class");
791 return;
792 }
793
794 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
795 if (h_obj.Get() == nullptr) {
796 AbortTransactionOrFail(self, "Could not allocate StringReader object");
797 return;
798 }
799
Mathieu Chartiere401d142015-04-22 13:56:20 -0700800 auto* cl = Runtime::Current()->GetClassLinker();
801 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
802 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700803 if (constructor == nullptr) {
804 AbortTransactionOrFail(self, "Could not find StringReader constructor");
805 return;
806 }
807
808 uint32_t args[1];
809 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
810 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
811
812 if (self->IsExceptionPending()) {
813 AbortTransactionOrFail(self, "Could not run StringReader constructor");
814 return;
815 }
816
817 result->SetL(h_obj.Get());
818}
819
Kenny Root1c9e61c2015-05-14 15:58:17 -0700820// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700821void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700822 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700823 jint start = shadow_frame->GetVReg(arg_offset + 1);
824 jint end = shadow_frame->GetVReg(arg_offset + 2);
825 jint index = shadow_frame->GetVReg(arg_offset + 4);
826 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
827 if (string == nullptr) {
828 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
829 return;
830 }
Kenny Root57f91e82015-05-14 15:58:17 -0700831 DCHECK_GE(start, 0);
832 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700833 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700834 Handle<mirror::CharArray> h_char_array(
835 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700836 DCHECK_LE(index, h_char_array->GetLength());
837 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700838 string->GetChars(start, end, h_char_array, index);
839}
840
841// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700842void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700843 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700844 jint index = shadow_frame->GetVReg(arg_offset + 1);
845 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
846 if (string == nullptr) {
847 AbortTransactionOrFail(self, "String.charAt with null object");
848 return;
849 }
850 result->SetC(string->CharAt(index));
851}
852
Kenny Root57f91e82015-05-14 15:58:17 -0700853// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700854void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700855 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700856 jint index = shadow_frame->GetVReg(arg_offset + 1);
857 jchar c = shadow_frame->GetVReg(arg_offset + 2);
858 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
859 if (string == nullptr) {
860 AbortTransactionOrFail(self, "String.setCharAt with null object");
861 return;
862 }
863 string->SetCharAt(index, c);
864}
865
Kenny Root1c9e61c2015-05-14 15:58:17 -0700866// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700867void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700868 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700869 jint offset = shadow_frame->GetVReg(arg_offset);
870 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
871 DCHECK_GE(char_count, 0);
872 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700873 Handle<mirror::CharArray> h_char_array(
874 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700875 Runtime* runtime = Runtime::Current();
876 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
877 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
878}
879
880// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700881void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700882 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700883 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
884 if (to_copy == nullptr) {
885 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
886 return;
887 }
888 StackHandleScope<1> hs(self);
889 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
890 Runtime* runtime = Runtime::Current();
891 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
892 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
893 allocator));
894}
895
Andreas Gampe799681b2015-05-15 19:24:12 -0700896void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700897 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700898 jint start = shadow_frame->GetVReg(arg_offset + 1);
899 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700900 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700901 DCHECK_GE(length, 0);
902 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903 Handle<mirror::String> h_string(
904 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700905 DCHECK_LE(start, h_string->GetLength());
906 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700907 Runtime* runtime = Runtime::Current();
908 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
909 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
910}
911
Kenny Root57f91e82015-05-14 15:58:17 -0700912// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700913void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700914 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700915 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700916 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
917 if (string == nullptr) {
918 AbortTransactionOrFail(self, "String.charAt with null object");
919 return;
920 }
921 result->SetL(string->ToCharArray(self));
922}
923
Andreas Gampebc4d2182016-02-22 10:03:12 -0800924// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
925void UnstartedRuntime::UnstartedReferenceGetReferent(
926 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
927 mirror::Reference* const ref = down_cast<mirror::Reference*>(
928 shadow_frame->GetVRegReference(arg_offset));
929 if (ref == nullptr) {
930 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
931 return;
932 }
933 mirror::Object* const referent =
934 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
935 result->SetL(referent);
936}
937
938// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
939// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
940// where we can predict the behavior (somewhat).
941// Note: this is required (instead of lazy initialization) as these classes are used in the static
942// initialization of other classes, so will *use* the value.
943void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
944 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
945 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
946 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
947 // SynchronousQueue really only separates between single- and multiprocessor case. Return
948 // 8 as a conservative upper approximation.
949 result->SetI(8);
950 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
951 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
952 // a good upper bound.
953 // TODO: Consider resetting in the zygote?
954 result->SetI(8);
955 } else {
956 // Not supported.
957 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
958 }
959}
960
961// This allows accessing ConcurrentHashMap/SynchronousQueue.
962
963void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
964 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
965 // Argument 0 is the Unsafe instance, skip.
966 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
967 if (obj == nullptr) {
968 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
969 return;
970 }
971 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
972 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
973 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
974
975 // Must use non transactional mode.
976 if (kUseReadBarrier) {
977 // Need to make sure the reference stored in the field is a to-space one before attempting the
978 // CAS or the CAS could fail incorrectly.
979 mirror::HeapReference<mirror::Object>* field_addr =
980 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
981 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
982 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
983 obj,
984 MemberOffset(offset),
985 field_addr);
986 }
987 bool success;
988 // Check whether we're in a transaction, call accordingly.
989 if (Runtime::Current()->IsActiveTransaction()) {
990 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
991 expectedValue,
992 newValue);
993 } else {
994 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
995 expectedValue,
996 newValue);
997 }
998 result->SetZ(success ? 1 : 0);
999}
1000
1001void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1002 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1003 // Argument 0 is the Unsafe instance, skip.
1004 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1005 if (obj == nullptr) {
1006 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1007 return;
1008 }
1009 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1010 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1011 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1012
1013 // Must use non transactional mode.
1014 if (kUseReadBarrier) {
1015 // Need to make sure the reference stored in the field is a to-space one before attempting the
1016 // CAS or the CAS could fail incorrectly.
1017 mirror::HeapReference<mirror::Object>* field_addr =
1018 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1019 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1020 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1021 obj,
1022 MemberOffset(offset),
1023 field_addr);
1024 }
1025 bool success;
1026 // Check whether we're in a transaction, call accordingly.
1027 if (Runtime::Current()->IsActiveTransaction()) {
1028 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1029 expected_value,
1030 newValue);
1031 } else {
1032 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1033 expected_value,
1034 newValue);
1035 }
1036 result->SetZ(success ? 1 : 0);
1037}
1038
1039void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1040 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1041 SHARED_REQUIRES(Locks::mutator_lock_) {
1042 // Argument 0 is the Unsafe instance, skip.
1043 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1044 if (obj == nullptr) {
1045 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1046 return;
1047 }
1048 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1049 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1050 result->SetL(value);
1051}
1052
1053void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1054 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1055 SHARED_REQUIRES(Locks::mutator_lock_) {
1056 // Argument 0 is the Unsafe instance, skip.
1057 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1058 if (obj == nullptr) {
1059 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1060 return;
1061 }
1062 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1063 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1064 QuasiAtomic::ThreadFenceRelease();
1065 if (Runtime::Current()->IsActiveTransaction()) {
1066 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1067 } else {
1068 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1069 }
1070}
1071
1072
Mathieu Chartiere401d142015-04-22 13:56:20 -07001073void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1074 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1075 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001076 int32_t length = args[1];
1077 DCHECK_GE(length, 0);
1078 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1079 Runtime* runtime = Runtime::Current();
1080 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1081 DCHECK(array_class != nullptr);
1082 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1083 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1084 array_class->GetComponentSizeShift(), allocator));
1085}
1086
Mathieu Chartiere401d142015-04-22 13:56:20 -07001087void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1088 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1089 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001090 result->SetL(nullptr);
1091}
1092
Mathieu Chartiere401d142015-04-22 13:56:20 -07001093void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1094 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1095 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001096 NthCallerVisitor visitor(self, 3);
1097 visitor.WalkStack();
1098 if (visitor.caller != nullptr) {
1099 result->SetL(visitor.caller->GetDeclaringClass());
1100 }
1101}
1102
Mathieu Chartiere401d142015-04-22 13:56:20 -07001103void UnstartedRuntime::UnstartedJNIMathLog(
1104 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1105 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001106 JValue value;
1107 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1108 result->SetD(log(value.GetD()));
1109}
1110
Mathieu Chartiere401d142015-04-22 13:56:20 -07001111void UnstartedRuntime::UnstartedJNIMathExp(
1112 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1113 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001114 JValue value;
1115 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1116 result->SetD(exp(value.GetD()));
1117}
1118
Andreas Gampebc4d2182016-02-22 10:03:12 -08001119void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1120 Thread* self ATTRIBUTE_UNUSED,
1121 ArtMethod* method ATTRIBUTE_UNUSED,
1122 mirror::Object* receiver ATTRIBUTE_UNUSED,
1123 uint32_t* args ATTRIBUTE_UNUSED,
1124 JValue* result) {
1125 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1126 ? 0
1127 : 1);
1128}
1129
Mathieu Chartiere401d142015-04-22 13:56:20 -07001130void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1131 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1132 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001133 StackHandleScope<1> hs(self);
1134 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1135}
1136
Andreas Gampebc4d2182016-02-22 10:03:12 -08001137void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1138 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1139 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1140 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1141 result->SetD(bit_cast<double>(long_input));
1142}
1143
Mathieu Chartiere401d142015-04-22 13:56:20 -07001144void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1145 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1146 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001147 result->SetI(args[0]);
1148}
1149
Mathieu Chartiere401d142015-04-22 13:56:20 -07001150void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1151 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1152 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001153 result->SetI(args[0]);
1154}
1155
Mathieu Chartiere401d142015-04-22 13:56:20 -07001156void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1157 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1158 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001159 result->SetL(receiver->Clone(self));
1160}
1161
Mathieu Chartiere401d142015-04-22 13:56:20 -07001162void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1163 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1164 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001165 receiver->NotifyAll(self);
1166}
1167
Mathieu Chartiere401d142015-04-22 13:56:20 -07001168void UnstartedRuntime::UnstartedJNIStringCompareTo(
1169 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1170 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001171 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1172 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001173 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001174 }
1175 result->SetI(receiver->AsString()->CompareTo(rhs));
1176}
1177
Mathieu Chartiere401d142015-04-22 13:56:20 -07001178void UnstartedRuntime::UnstartedJNIStringIntern(
1179 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1180 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001181 result->SetL(receiver->AsString()->Intern());
1182}
1183
Mathieu Chartiere401d142015-04-22 13:56:20 -07001184void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1185 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1186 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001187 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1188}
1189
Mathieu Chartiere401d142015-04-22 13:56:20 -07001190void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1191 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1192 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001193 StackHandleScope<2> hs(self);
1194 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1195 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1196 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1197}
1198
Mathieu Chartiere401d142015-04-22 13:56:20 -07001199void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1200 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1201 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001202 int32_t length = static_cast<int32_t>(args[1]);
1203 if (length < 0) {
1204 ThrowNegativeArraySizeException(length);
1205 return;
1206 }
1207 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1208 Runtime* runtime = Runtime::Current();
1209 ClassLinker* class_linker = runtime->GetClassLinker();
1210 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001211 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001212 CHECK(self->IsExceptionPending());
1213 return;
1214 }
1215 DCHECK(array_class->IsObjectArrayClass());
1216 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1217 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1218 result->SetL(new_array);
1219}
1220
Mathieu Chartiere401d142015-04-22 13:56:20 -07001221void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1222 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1223 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001224 ScopedObjectAccessUnchecked soa(self);
1225 if (Runtime::Current()->IsActiveTransaction()) {
1226 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1227 } else {
1228 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1229 }
1230}
1231
Mathieu Chartiere401d142015-04-22 13:56:20 -07001232void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1233 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1234 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001235 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1236 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1237}
1238
Mathieu Chartiere401d142015-04-22 13:56:20 -07001239void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1240 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1241 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001242 result->SetZ(JNI_TRUE);
1243}
1244
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1246 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1247 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001248 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1249 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1250 jint expectedValue = args[3];
1251 jint newValue = args[4];
1252 bool success;
1253 if (Runtime::Current()->IsActiveTransaction()) {
1254 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1255 expectedValue, newValue);
1256 } else {
1257 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1258 expectedValue, newValue);
1259 }
1260 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1261}
1262
Narayan Kamath34a316f2016-03-30 13:11:18 +01001263void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1264 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1265 uint32_t* args, JValue* result) {
1266 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1267 if (obj == nullptr) {
1268 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1269 return;
1270 }
1271
1272 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1273 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1274}
1275
Mathieu Chartiere401d142015-04-22 13:56:20 -07001276void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1277 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1278 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001279 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1280 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1281 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1282 if (Runtime::Current()->IsActiveTransaction()) {
1283 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1284 } else {
1285 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1286 }
1287}
1288
Andreas Gampe799681b2015-05-15 19:24:12 -07001289void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001290 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1291 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001292 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1293 Primitive::Type primitive_type = component->GetPrimitiveType();
1294 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1295}
1296
Andreas Gampe799681b2015-05-15 19:24:12 -07001297void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001298 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1299 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001300 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1301 Primitive::Type primitive_type = component->GetPrimitiveType();
1302 result->SetI(Primitive::ComponentSize(primitive_type));
1303}
1304
Andreas Gampedd9d0552015-03-09 12:57:41 -07001305typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001306 size_t arg_size);
1307
Mathieu Chartiere401d142015-04-22 13:56:20 -07001308typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001309 uint32_t* args, JValue* result);
1310
1311static bool tables_initialized_ = false;
1312static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1313static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1314
Andreas Gampe799681b2015-05-15 19:24:12 -07001315void UnstartedRuntime::InitializeInvokeHandlers() {
1316#define UNSTARTED_DIRECT(ShortName, Sig) \
1317 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1318#include "unstarted_runtime_list.h"
1319 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1320#undef UNSTARTED_RUNTIME_DIRECT_LIST
1321#undef UNSTARTED_RUNTIME_JNI_LIST
1322#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001323}
1324
Andreas Gampe799681b2015-05-15 19:24:12 -07001325void UnstartedRuntime::InitializeJNIHandlers() {
1326#define UNSTARTED_JNI(ShortName, Sig) \
1327 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1328#include "unstarted_runtime_list.h"
1329 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1330#undef UNSTARTED_RUNTIME_DIRECT_LIST
1331#undef UNSTARTED_RUNTIME_JNI_LIST
1332#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001333}
1334
Andreas Gampe799681b2015-05-15 19:24:12 -07001335void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001336 CHECK(!tables_initialized_);
1337
Andreas Gampe799681b2015-05-15 19:24:12 -07001338 InitializeInvokeHandlers();
1339 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001340
1341 tables_initialized_ = true;
1342}
1343
Andreas Gampe799681b2015-05-15 19:24:12 -07001344void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1345 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001346 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1347 // problems in core libraries.
1348 CHECK(tables_initialized_);
1349
1350 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1351 const auto& iter = invoke_handlers_.find(name);
1352 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001353 // Clear out the result in case it's not zeroed out.
1354 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001355 (*iter->second)(self, shadow_frame, result, arg_offset);
1356 } else {
1357 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001358 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001359 }
1360}
1361
1362// 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 -07001363void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001364 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001365 std::string name(PrettyMethod(method));
1366 const auto& iter = jni_handlers_.find(name);
1367 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001368 // Clear out the result in case it's not zeroed out.
1369 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001370 (*iter->second)(self, method, receiver, args, result);
1371 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001372 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1373 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001374 } else {
1375 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1376 "non-transactional runtime";
1377 }
1378}
1379
1380} // namespace interpreter
1381} // namespace art