blob: 80ffedcaa023b262035ced76ededd1a345c36b47 [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 Gampe13fc1be2016-04-05 20:14:30 -070019#include <errno.h>
20#include <stdlib.h>
21
Andreas Gampe2969bcd2015-03-09 12:57:41 -070022#include <cmath>
Andreas Gampe13fc1be2016-04-05 20:14:30 -070023#include <limits>
Andreas Gampe2969bcd2015-03-09 12:57:41 -070024#include <unordered_map>
25
Andreas Gampeaacc25d2015-04-01 14:49:06 -070026#include "ScopedLocalRef.h"
27
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "art_method-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080029#include "base/casts.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070030#include "base/logging.h"
31#include "base/macros.h"
32#include "class_linker.h"
33#include "common_throws.h"
34#include "entrypoints/entrypoint_utils-inl.h"
Andreas Gampebc4d2182016-02-22 10:03:12 -080035#include "gc/reference_processor.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070036#include "handle_scope-inl.h"
37#include "interpreter/interpreter_common.h"
38#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070039#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070040#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
43#include "mirror/string-inl.h"
44#include "nth_caller_visitor.h"
45#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020046#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070047#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070048#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070049
50namespace art {
51namespace interpreter {
52
Andreas Gampe068b0c02015-03-11 12:44:47 -070053static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020054 __attribute__((__format__(__printf__, 2, 3)))
Mathieu Chartier90443472015-07-16 20:32:27 -070055 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020056
57static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070058 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070059 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020060 va_start(args, fmt);
61 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070062 va_end(args);
63 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020064 va_start(args, fmt);
65 std::string msg;
66 StringAppendV(&msg, fmt, args);
67 va_end(args);
68 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070069 UNREACHABLE();
70 }
71}
72
Andreas Gampe2969bcd2015-03-09 12:57:41 -070073// Helper function to deal with class loading in an unstarted runtime.
74static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
75 Handle<mirror::ClassLoader> class_loader, JValue* result,
76 const std::string& method_name, bool initialize_class,
77 bool abort_if_not_found)
Mathieu Chartier90443472015-07-16 20:32:27 -070078 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -070079 CHECK(className.Get() != nullptr);
80 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
81 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
82
83 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
84 if (found == nullptr && abort_if_not_found) {
85 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070086 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
87 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -070088 }
89 return;
90 }
91 if (found != nullptr && initialize_class) {
92 StackHandleScope<1> hs(self);
93 Handle<mirror::Class> h_class(hs.NewHandle(found));
94 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
95 CHECK(self->IsExceptionPending());
96 return;
97 }
98 }
99 result->SetL(found);
100}
101
102// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
103// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
104// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200105// actually the transaction abort exception. This must not be wrapped, as it signals an
106// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700107static void CheckExceptionGenerateClassNotFound(Thread* self)
Mathieu Chartier90443472015-07-16 20:32:27 -0700108 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700109 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200110 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700111 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200112 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700113 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
114 "ClassNotFoundException");
115 }
116 }
117}
118
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700119static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700120 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700121 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
122 if (param == nullptr) {
123 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
124 return nullptr;
125 }
126 return param->AsString();
127}
128
Andreas Gampe799681b2015-05-15 19:24:12 -0700129void UnstartedRuntime::UnstartedClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700130 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700131 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
132 if (class_name == nullptr) {
133 return;
134 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700135 StackHandleScope<1> hs(self);
136 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800137 UnstartedRuntimeFindClass(self,
138 h_class_name,
139 ScopedNullHandle<mirror::ClassLoader>(),
140 result,
141 "Class.forName",
142 true,
143 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700144 CheckExceptionGenerateClassNotFound(self);
145}
146
Andreas Gampe799681b2015-05-15 19:24:12 -0700147void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700148 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700149 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
150 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700151 return;
152 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700153 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
154 mirror::ClassLoader* class_loader =
155 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
156 StackHandleScope<2> hs(self);
157 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
158 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
159 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
160 initialize_class, false);
161 CheckExceptionGenerateClassNotFound(self);
162}
163
Andreas Gampe799681b2015-05-15 19:24:12 -0700164void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700165 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700166 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
167 if (class_name == nullptr) {
168 return;
169 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700170 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
171 mirror::ClassLoader* class_loader =
172 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
173 StackHandleScope<2> hs(self);
174 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
175 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
176 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
177 initialize_class, false);
178 CheckExceptionGenerateClassNotFound(self);
179}
180
Andreas Gampe799681b2015-05-15 19:24:12 -0700181void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700182 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
183 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700184 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
185 if (param == nullptr) {
186 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
187 return;
188 }
189 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700190 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700191
192 // Check that it's not null.
193 if (h_klass.Get() == nullptr) {
194 AbortTransactionOrFail(self, "Class reference is null for newInstance");
195 return;
196 }
197
198 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
199 if (Runtime::Current()->IsActiveTransaction()) {
200 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200201 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
202 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700203 return;
204 }
205 }
206
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700207 // There are two situations in which we'll abort this run.
208 // 1) If the class isn't yet initialized and initialization fails.
209 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
210 // Note that 2) could likely be handled here, but for safety abort the transaction.
211 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 auto* cl = Runtime::Current()->GetClassLinker();
213 if (cl->EnsureInitialized(self, h_klass, true, true)) {
214 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
215 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700216 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
217 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700218 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700219 if (!self->IsExceptionPending()) {
220 result->SetL(h_obj.Get());
221 ok = true;
222 }
223 } else {
224 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
225 "Could not find default constructor for '%s'",
226 PrettyClass(h_klass.Get()).c_str());
227 }
228 }
229 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700230 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
231 PrettyClass(h_klass.Get()).c_str(),
232 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700233 }
234}
235
Andreas Gampe799681b2015-05-15 19:24:12 -0700236void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700237 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700238 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
239 // going the reflective Dex way.
240 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
241 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 ArtField* found = nullptr;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700243 for (ArtField& field : klass->GetIFields()) {
244 if (name2->Equals(field.GetName())) {
245 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700246 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700247 }
248 }
249 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700250 for (ArtField& field : klass->GetSFields()) {
251 if (name2->Equals(field.GetName())) {
252 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700253 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700254 }
255 }
256 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700257 if (found == nullptr) {
258 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
259 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
260 PrettyDescriptor(klass).c_str());
261 return;
262 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700263 if (Runtime::Current()->IsActiveTransaction()) {
264 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
265 } else {
266 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
267 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700268}
269
Andreas Gampebc4d2182016-02-22 10:03:12 -0800270// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
271void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
272 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
273 // Special managed code cut-out to allow method lookup in a un-started runtime.
274 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
275 if (klass == nullptr) {
276 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
277 return;
278 }
279 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
280 mirror::ObjectArray<mirror::Class>* args =
281 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
282 if (Runtime::Current()->IsActiveTransaction()) {
283 result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args));
284 } else {
285 result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args));
286 }
287}
288
Andreas Gampe6039e562016-04-05 18:18:43 -0700289// Special managed code cut-out to allow constructor lookup in a un-started runtime.
290void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
291 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
292 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
293 if (klass == nullptr) {
294 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
295 return;
296 }
297 mirror::ObjectArray<mirror::Class>* args =
298 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
299 if (Runtime::Current()->IsActiveTransaction()) {
300 result->SetL(mirror::Class::GetDeclaredConstructorInternal<true>(self, klass, args));
301 } else {
302 result->SetL(mirror::Class::GetDeclaredConstructorInternal<false>(self, klass, args));
303 }
304}
305
Andreas Gampe633750c2016-02-19 10:49:50 -0800306void UnstartedRuntime::UnstartedClassGetEnclosingClass(
307 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
308 StackHandleScope<1> hs(self);
309 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
310 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
311 result->SetL(nullptr);
312 }
313 result->SetL(klass->GetDexFile().GetEnclosingClass(klass));
314}
315
Andreas Gampe799681b2015-05-15 19:24:12 -0700316void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700317 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700318 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
319 mirror::ClassLoader* class_loader =
320 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
321 StackHandleScope<2> hs(self);
322 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
323 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
324 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
325 "VMClassLoader.findLoadedClass", false, false);
326 // This might have an error pending. But semantics are to just return null.
327 if (self->IsExceptionPending()) {
328 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
329 std::string type(PrettyTypeOf(self->GetException()));
330 if (type != "java.lang.InternalError") {
331 self->ClearException();
332 }
333 }
334}
335
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336void UnstartedRuntime::UnstartedVoidLookupType(
337 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
338 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700339 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
340}
341
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700342// Arraycopy emulation.
343// Note: we can't use any fast copy functions, as they are not available under transaction.
344
345template <typename T>
346static void PrimitiveArrayCopy(Thread* self,
347 mirror::Array* src_array, int32_t src_pos,
348 mirror::Array* dst_array, int32_t dst_pos,
349 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700350 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700351 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
352 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
353 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
354 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
355 return;
356 }
357 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
358 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
359 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
360 if (copy_forward) {
361 for (int32_t i = 0; i < length; ++i) {
362 dst->Set(dst_pos + i, src->Get(src_pos + i));
363 }
364 } else {
365 for (int32_t i = 1; i <= length; ++i) {
366 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
367 }
368 }
369}
370
Andreas Gampe799681b2015-05-15 19:24:12 -0700371void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700372 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700373 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700374 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
375 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700376 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700377
Andreas Gampe85a098a2016-03-31 13:30:53 -0700378 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
379 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
380 // Null checking. For simplicity, abort transaction.
381 if (src_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700382 AbortTransactionOrFail(self, "src is null in arraycopy.");
383 return;
384 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700385 if (dst_obj == nullptr) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700386 AbortTransactionOrFail(self, "dst is null in arraycopy.");
387 return;
388 }
Andreas Gampe85a098a2016-03-31 13:30:53 -0700389 // Test for arrayness. Throw ArrayStoreException.
390 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
391 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
392 return;
393 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700394
Andreas Gampe85a098a2016-03-31 13:30:53 -0700395 mirror::Array* src_array = src_obj->AsArray();
396 mirror::Array* dst_array = dst_obj->AsArray();
397
398 // Bounds checking. Throw IndexOutOfBoundsException.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700399 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
400 UNLIKELY(src_pos > src_array->GetLength() - length) ||
401 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700402 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700403 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
404 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
405 length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700406 return;
407 }
408
409 // Type checking.
410 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
411 GetComponentType();
412
413 if (!src_type->IsPrimitive()) {
414 // Check that the second type is not primitive.
415 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
416 GetComponentType();
417 if (trg_type->IsPrimitiveInt()) {
418 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
419 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
420 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
421 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700422 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700423
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700424 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
425 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
426 if (src == dst) {
427 // Can overlap, but not have type mismatches.
Andreas Gampe85a098a2016-03-31 13:30:53 -0700428 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700429 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
430 if (copy_forward) {
431 for (int32_t i = 0; i < length; ++i) {
432 dst->Set(dst_pos + i, src->Get(src_pos + i));
433 }
434 } else {
435 for (int32_t i = 1; i <= length; ++i) {
436 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
437 }
438 }
439 } else {
Andreas Gampe85a098a2016-03-31 13:30:53 -0700440 // We're being lazy here. Optimally this could be a memcpy (if component types are
441 // assignable), but the ObjectArray implementation doesn't support transactions. The
442 // checking version, however, does.
443 if (Runtime::Current()->IsActiveTransaction()) {
444 dst->AssignableCheckingMemcpy<true>(
445 dst_pos, src, src_pos, length, true /* throw_exception */);
446 } else {
447 dst->AssignableCheckingMemcpy<false>(
448 dst_pos, src, src_pos, length, true /* throw_exception */);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700449 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700450 }
Andreas Gampe5c9af612016-04-05 14:16:10 -0700451 } else if (src_type->IsPrimitiveByte()) {
452 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700453 } else if (src_type->IsPrimitiveChar()) {
454 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
455 } else if (src_type->IsPrimitiveInt()) {
456 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700457 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700458 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700459 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700460 }
461}
462
Andreas Gampe5c9af612016-04-05 14:16:10 -0700463void UnstartedRuntime::UnstartedSystemArraycopyByte(
464 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
465 // Just forward.
466 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
467}
468
Andreas Gampe799681b2015-05-15 19:24:12 -0700469void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700470 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700471 // Just forward.
472 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
473}
474
475void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700476 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700477 // Just forward.
478 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
479}
480
Narayan Kamath34a316f2016-03-30 13:11:18 +0100481void UnstartedRuntime::UnstartedSystemGetSecurityManager(
482 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
483 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
484 result->SetL(nullptr);
485}
486
Andreas Gampe799681b2015-05-15 19:24:12 -0700487void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700488 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700489 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
490 bool ok = false;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100491 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" ||
492 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700493 // Allocate non-threadlocal buffer.
Narayan Kamatha1e93122016-03-30 15:41:54 +0100494 result->SetL(mirror::CharArray::Alloc(self, 26));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700495 ok = true;
Narayan Kamatha1e93122016-03-30 15:41:54 +0100496 } else if (caller ==
497 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") {
498 // Allocate new object.
499 StackHandleScope<2> hs(self);
500 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
501 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
502 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
503 h_real_to_string_class->AllocObject(self)));
504 if (h_real_to_string_obj.Get() != nullptr) {
505 auto* cl = Runtime::Current()->GetClassLinker();
506 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
507 "<init>", "()V", cl->GetImagePointerSize());
508 if (init_method == nullptr) {
509 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
510 } else {
511 JValue invoke_result;
512 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
513 nullptr);
514 if (!self->IsExceptionPending()) {
515 result->SetL(h_real_to_string_obj.Get());
516 ok = true;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700517 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700518 }
519 }
520 }
521
522 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700523 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700524 }
525}
526
Sergio Giro83261202016-04-11 20:49:20 +0100527static double ComputeCeil(double in) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700528 double out;
529 // Special cases:
530 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
531 // -1 < in < 0 -> out := -0.
532 if (-1.0 < in && in < 0) {
533 out = -0.0;
534 } else {
535 out = ceil(in);
536 }
Sergio Giro83261202016-04-11 20:49:20 +0100537 return out;
538}
539
540void UnstartedRuntime::UnstartedMathCeil(
541 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
542 double in = shadow_frame->GetVRegDouble(arg_offset);
543 result->SetD(ComputeCeil(in));
544}
545
546void UnstartedRuntime::UnstartedMathFloor(
547 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
548 double in = shadow_frame->GetVRegDouble(arg_offset);
549 // From the JavaDocs:
550 // "Note that the value of Math.ceil(x) is exactly the value of -Math.floor(-x)."
551 result->SetD(-ComputeCeil(-in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700552}
553
Andreas Gampe799681b2015-05-15 19:24:12 -0700554void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700555 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700556 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
557 result->SetI(obj->IdentityHashCode());
558}
559
Andreas Gampe799681b2015-05-15 19:24:12 -0700560void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700561 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700562 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000563 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700564}
565
Andreas Gampedd9d0552015-03-09 12:57:41 -0700566static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700567 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700568 const DexFile* dex_file = dex_cache->GetDexFile();
569 if (dex_file == nullptr) {
570 return nullptr;
571 }
572
573 // Create the direct byte buffer.
574 JNIEnv* env = self->GetJniEnv();
575 DCHECK(env != nullptr);
576 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700577 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
578 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700579 DCHECK(self->IsExceptionPending());
580 return nullptr;
581 }
582
583 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700584 args[0].l = byte_buffer.get();
585
586 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
587 WellKnownClasses::com_android_dex_Dex,
588 WellKnownClasses::com_android_dex_Dex_create,
589 args));
590
591 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700592}
593
Andreas Gampe799681b2015-05-15 19:24:12 -0700594void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700595 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700596 // We will create the Dex object, but the image writer will release it before creating the
597 // art file.
598 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
599 bool have_dex = false;
600 if (src != nullptr) {
601 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
602 if (dex != nullptr) {
603 have_dex = true;
604 result->SetL(dex);
605 }
606 }
607 if (!have_dex) {
608 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200609 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700610 }
611}
612
613static void UnstartedMemoryPeek(
614 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
615 int64_t address = shadow_frame->GetVRegLong(arg_offset);
616 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
617 // aborting the transaction.
618
619 switch (type) {
620 case Primitive::kPrimByte: {
621 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
622 return;
623 }
624
625 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700626 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
627 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700628 return;
629 }
630
631 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700632 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
633 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700634 return;
635 }
636
637 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700638 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
639 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700640 return;
641 }
642
643 case Primitive::kPrimBoolean:
644 case Primitive::kPrimChar:
645 case Primitive::kPrimFloat:
646 case Primitive::kPrimDouble:
647 case Primitive::kPrimVoid:
648 case Primitive::kPrimNot:
649 LOG(FATAL) << "Not in the Memory API: " << type;
650 UNREACHABLE();
651 }
652 LOG(FATAL) << "Should not reach here";
653 UNREACHABLE();
654}
655
Andreas Gampe799681b2015-05-15 19:24:12 -0700656void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700657 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700658 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
659}
660
661void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700662 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700663 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
664}
665
666void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700667 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700668 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
669}
670
671void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700672 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700673 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700674}
675
676static void UnstartedMemoryPeekArray(
677 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700678 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700679 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
680 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
681 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200682 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700683 return;
684 }
685 mirror::Array* array = obj->AsArray();
686
687 int offset = shadow_frame->GetVReg(arg_offset + 3);
688 int count = shadow_frame->GetVReg(arg_offset + 4);
689 if (offset < 0 || offset + count > array->GetLength()) {
690 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
691 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200692 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700693 return;
694 }
695
696 switch (type) {
697 case Primitive::kPrimByte: {
698 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
699 mirror::ByteArray* byte_array = array->AsByteArray();
700 for (int32_t i = 0; i < count; ++i, ++address) {
701 byte_array->SetWithoutChecks<true>(i + offset, *address);
702 }
703 return;
704 }
705
706 case Primitive::kPrimShort:
707 case Primitive::kPrimInt:
708 case Primitive::kPrimLong:
709 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
710 UNREACHABLE();
711
712 case Primitive::kPrimBoolean:
713 case Primitive::kPrimChar:
714 case Primitive::kPrimFloat:
715 case Primitive::kPrimDouble:
716 case Primitive::kPrimVoid:
717 case Primitive::kPrimNot:
718 LOG(FATAL) << "Not in the Memory API: " << type;
719 UNREACHABLE();
720 }
721 LOG(FATAL) << "Should not reach here";
722 UNREACHABLE();
723}
724
Andreas Gampe799681b2015-05-15 19:24:12 -0700725void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700726 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700727 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700728}
729
Andreas Gampef778eb22015-04-13 14:17:09 -0700730// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700731void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700732 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
733 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700734 Runtime* runtime = Runtime::Current();
Andreas Gampee0f633e2016-03-29 19:33:56 -0700735
736 std::vector<std::string> split;
737 Split(runtime->GetBootClassPathString(), ':', &split);
738 if (split.empty()) {
739 AbortTransactionOrFail(self,
740 "Boot classpath not set or split error:: %s",
741 runtime->GetBootClassPathString().c_str());
742 return;
743 }
744 const std::string& source = split[0];
745
Andreas Gampef778eb22015-04-13 14:17:09 -0700746 mirror::String* string_data;
747
748 // Use a block to enclose the I/O and MemMap code so buffers are released early.
749 {
750 std::string error_msg;
Andreas Gampee0f633e2016-03-29 19:33:56 -0700751 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(source.c_str(), &error_msg));
Andreas Gampef778eb22015-04-13 14:17:09 -0700752 if (zip_archive.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700753 AbortTransactionOrFail(self,
754 "Could not open zip file %s: %s",
755 source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700756 error_msg.c_str());
757 return;
758 }
759 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
760 &error_msg));
761 if (zip_entry.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700762 AbortTransactionOrFail(self,
763 "Could not find security.properties file in %s: %s",
764 source.c_str(),
765 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700766 return;
767 }
Andreas Gampee0f633e2016-03-29 19:33:56 -0700768 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(source.c_str(),
Andreas Gampef778eb22015-04-13 14:17:09 -0700769 "java/security/security.properties",
770 &error_msg));
771 if (map.get() == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700772 AbortTransactionOrFail(self,
773 "Could not unzip security.properties file in %s: %s",
774 source.c_str(),
775 error_msg.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700776 return;
777 }
778
779 uint32_t length = zip_entry->GetUncompressedLength();
780 std::unique_ptr<char[]> tmp(new char[length + 1]);
781 memcpy(tmp.get(), map->Begin(), length);
782 tmp.get()[length] = 0; // null terminator
783
784 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
785 }
786
787 if (string_data == nullptr) {
Andreas Gampee0f633e2016-03-29 19:33:56 -0700788 AbortTransactionOrFail(self, "Could not create string from file content of %s", source.c_str());
Andreas Gampef778eb22015-04-13 14:17:09 -0700789 return;
790 }
791
792 // Create a StringReader.
793 StackHandleScope<3> hs(self);
794 Handle<mirror::String> h_string(hs.NewHandle(string_data));
795
796 Handle<mirror::Class> h_class(hs.NewHandle(
797 runtime->GetClassLinker()->FindClass(self,
798 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800799 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700800 if (h_class.Get() == nullptr) {
801 AbortTransactionOrFail(self, "Could not find StringReader class");
802 return;
803 }
804
805 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
806 AbortTransactionOrFail(self, "Could not initialize StringReader class");
807 return;
808 }
809
810 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
811 if (h_obj.Get() == nullptr) {
812 AbortTransactionOrFail(self, "Could not allocate StringReader object");
813 return;
814 }
815
Mathieu Chartiere401d142015-04-22 13:56:20 -0700816 auto* cl = Runtime::Current()->GetClassLinker();
817 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
818 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700819 if (constructor == nullptr) {
820 AbortTransactionOrFail(self, "Could not find StringReader constructor");
821 return;
822 }
823
824 uint32_t args[1];
825 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
826 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
827
828 if (self->IsExceptionPending()) {
829 AbortTransactionOrFail(self, "Could not run StringReader constructor");
830 return;
831 }
832
833 result->SetL(h_obj.Get());
834}
835
Kenny Root1c9e61c2015-05-14 15:58:17 -0700836// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700837void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700838 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700839 jint start = shadow_frame->GetVReg(arg_offset + 1);
840 jint end = shadow_frame->GetVReg(arg_offset + 2);
841 jint index = shadow_frame->GetVReg(arg_offset + 4);
842 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
843 if (string == nullptr) {
844 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
845 return;
846 }
Kenny Root57f91e82015-05-14 15:58:17 -0700847 DCHECK_GE(start, 0);
848 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700849 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700850 Handle<mirror::CharArray> h_char_array(
851 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700852 DCHECK_LE(index, h_char_array->GetLength());
853 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700854 string->GetChars(start, end, h_char_array, index);
855}
856
857// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700858void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700859 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700860 jint index = shadow_frame->GetVReg(arg_offset + 1);
861 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
862 if (string == nullptr) {
863 AbortTransactionOrFail(self, "String.charAt with null object");
864 return;
865 }
866 result->SetC(string->CharAt(index));
867}
868
Kenny Root57f91e82015-05-14 15:58:17 -0700869// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700870void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700871 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700872 jint index = shadow_frame->GetVReg(arg_offset + 1);
873 jchar c = shadow_frame->GetVReg(arg_offset + 2);
874 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
875 if (string == nullptr) {
876 AbortTransactionOrFail(self, "String.setCharAt with null object");
877 return;
878 }
879 string->SetCharAt(index, c);
880}
881
Kenny Root1c9e61c2015-05-14 15:58:17 -0700882// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700883void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700884 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700885 jint offset = shadow_frame->GetVReg(arg_offset);
886 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
887 DCHECK_GE(char_count, 0);
888 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700889 Handle<mirror::CharArray> h_char_array(
890 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700891 Runtime* runtime = Runtime::Current();
892 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
893 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
894}
895
896// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700897void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700898 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700899 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
900 if (to_copy == nullptr) {
901 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
902 return;
903 }
904 StackHandleScope<1> hs(self);
905 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
906 Runtime* runtime = Runtime::Current();
907 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
908 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
909 allocator));
910}
911
Andreas Gampe799681b2015-05-15 19:24:12 -0700912void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700913 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700914 jint start = shadow_frame->GetVReg(arg_offset + 1);
915 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700916 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700917 DCHECK_GE(length, 0);
918 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700919 Handle<mirror::String> h_string(
920 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700921 DCHECK_LE(start, h_string->GetLength());
922 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700923 Runtime* runtime = Runtime::Current();
924 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
925 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
926}
927
Kenny Root57f91e82015-05-14 15:58:17 -0700928// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700929void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700930 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700931 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700932 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
933 if (string == nullptr) {
934 AbortTransactionOrFail(self, "String.charAt with null object");
935 return;
936 }
937 result->SetL(string->ToCharArray(self));
938}
939
Andreas Gampebc4d2182016-02-22 10:03:12 -0800940// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
941void UnstartedRuntime::UnstartedReferenceGetReferent(
942 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
943 mirror::Reference* const ref = down_cast<mirror::Reference*>(
944 shadow_frame->GetVRegReference(arg_offset));
945 if (ref == nullptr) {
946 AbortTransactionOrFail(self, "Reference.getReferent() with null object");
947 return;
948 }
949 mirror::Object* const referent =
950 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
951 result->SetL(referent);
952}
953
954// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
955// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
956// where we can predict the behavior (somewhat).
957// Note: this is required (instead of lazy initialization) as these classes are used in the static
958// initialization of other classes, so will *use* the value.
959void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
960 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
961 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
962 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") {
963 // SynchronousQueue really only separates between single- and multiprocessor case. Return
964 // 8 as a conservative upper approximation.
965 result->SetI(8);
966 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") {
967 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
968 // a good upper bound.
969 // TODO: Consider resetting in the zygote?
970 result->SetI(8);
971 } else {
972 // Not supported.
973 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
974 }
975}
976
977// This allows accessing ConcurrentHashMap/SynchronousQueue.
978
979void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
980 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
981 // Argument 0 is the Unsafe instance, skip.
982 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
983 if (obj == nullptr) {
984 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
985 return;
986 }
987 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
988 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
989 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
990
991 // Must use non transactional mode.
992 if (kUseReadBarrier) {
993 // Need to make sure the reference stored in the field is a to-space one before attempting the
994 // CAS or the CAS could fail incorrectly.
995 mirror::HeapReference<mirror::Object>* field_addr =
996 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
997 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
998 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
999 obj,
1000 MemberOffset(offset),
1001 field_addr);
1002 }
1003 bool success;
1004 // Check whether we're in a transaction, call accordingly.
1005 if (Runtime::Current()->IsActiveTransaction()) {
1006 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
1007 expectedValue,
1008 newValue);
1009 } else {
1010 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
1011 expectedValue,
1012 newValue);
1013 }
1014 result->SetZ(success ? 1 : 0);
1015}
1016
1017void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
1018 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1019 // Argument 0 is the Unsafe instance, skip.
1020 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1021 if (obj == nullptr) {
1022 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1023 return;
1024 }
1025 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1026 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
1027 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5);
1028
1029 // Must use non transactional mode.
1030 if (kUseReadBarrier) {
1031 // Need to make sure the reference stored in the field is a to-space one before attempting the
1032 // CAS or the CAS could fail incorrectly.
1033 mirror::HeapReference<mirror::Object>* field_addr =
1034 reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
1035 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
1036 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>(
1037 obj,
1038 MemberOffset(offset),
1039 field_addr);
1040 }
1041 bool success;
1042 // Check whether we're in a transaction, call accordingly.
1043 if (Runtime::Current()->IsActiveTransaction()) {
1044 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset),
1045 expected_value,
1046 newValue);
1047 } else {
1048 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset),
1049 expected_value,
1050 newValue);
1051 }
1052 result->SetZ(success ? 1 : 0);
1053}
1054
1055void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
1056 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1057 SHARED_REQUIRES(Locks::mutator_lock_) {
1058 // Argument 0 is the Unsafe instance, skip.
1059 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1060 if (obj == nullptr) {
1061 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1062 return;
1063 }
1064 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1065 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
1066 result->SetL(value);
1067}
1068
Andreas Gampe8a18fde2016-04-05 21:12:51 -07001069void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
1070 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1071 SHARED_REQUIRES(Locks::mutator_lock_) {
1072 // Argument 0 is the Unsafe instance, skip.
1073 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1074 if (obj == nullptr) {
1075 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1076 return;
1077 }
1078 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1079 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
1080 if (Runtime::Current()->IsActiveTransaction()) {
1081 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
1082 } else {
1083 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
1084 }
1085}
1086
Andreas Gampebc4d2182016-02-22 10:03:12 -08001087void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
1088 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
1089 SHARED_REQUIRES(Locks::mutator_lock_) {
1090 // Argument 0 is the Unsafe instance, skip.
1091 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
1092 if (obj == nullptr) {
1093 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1094 return;
1095 }
1096 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
1097 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4);
1098 QuasiAtomic::ThreadFenceRelease();
1099 if (Runtime::Current()->IsActiveTransaction()) {
1100 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1101 } else {
1102 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1103 }
1104}
1105
Andreas Gampe13fc1be2016-04-05 20:14:30 -07001106// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
1107// of correctly handling the corner cases.
1108void UnstartedRuntime::UnstartedIntegerParseInt(
1109 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1110 SHARED_REQUIRES(Locks::mutator_lock_) {
1111 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1112 if (obj == nullptr) {
1113 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1114 return;
1115 }
1116
1117 std::string string_value = obj->AsString()->ToModifiedUtf8();
1118 if (string_value.empty()) {
1119 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1120 return;
1121 }
1122
1123 const char* c_str = string_value.c_str();
1124 char *end;
1125 // Can we set errno to 0? Is this always a variable, and not a macro?
1126 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1127 int64_t l = strtol(c_str, &end, 10);
1128
1129 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1130 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1131 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1132 return;
1133 }
1134 if (l == 0) {
1135 // Check whether the string wasn't exactly zero.
1136 if (string_value != "0") {
1137 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1138 return;
1139 }
1140 } else if (*end != '\0') {
1141 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1142 return;
1143 }
1144
1145 result->SetI(static_cast<int32_t>(l));
1146}
1147
1148// A cutout for Long.parseLong.
1149//
1150// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
1151// well.
1152void UnstartedRuntime::UnstartedLongParseLong(
1153 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
1154 SHARED_REQUIRES(Locks::mutator_lock_) {
1155 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
1156 if (obj == nullptr) {
1157 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
1158 return;
1159 }
1160
1161 std::string string_value = obj->AsString()->ToModifiedUtf8();
1162 if (string_value.empty()) {
1163 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
1164 return;
1165 }
1166
1167 const char* c_str = string_value.c_str();
1168 char *end;
1169 // Can we set errno to 0? Is this always a variable, and not a macro?
1170 // Worst case, we'll incorrectly fail a transaction. Seems OK.
1171 int64_t l = strtol(c_str, &end, 10);
1172
1173 // Note: comparing against int32_t min/max is intentional here.
1174 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
1175 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
1176 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1177 return;
1178 }
1179 if (l == 0) {
1180 // Check whether the string wasn't exactly zero.
1181 if (string_value != "0") {
1182 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1183 return;
1184 }
1185 } else if (*end != '\0') {
1186 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
1187 return;
1188 }
1189
1190 result->SetJ(l);
1191}
1192
Andreas Gampebc4d2182016-02-22 10:03:12 -08001193
Mathieu Chartiere401d142015-04-22 13:56:20 -07001194void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
1195 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1196 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001197 int32_t length = args[1];
1198 DCHECK_GE(length, 0);
1199 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1200 Runtime* runtime = Runtime::Current();
1201 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
1202 DCHECK(array_class != nullptr);
1203 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
1204 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
1205 array_class->GetComponentSizeShift(), allocator));
1206}
1207
Mathieu Chartiere401d142015-04-22 13:56:20 -07001208void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
1209 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1210 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001211 result->SetL(nullptr);
1212}
1213
Mathieu Chartiere401d142015-04-22 13:56:20 -07001214void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
1215 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1216 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001217 NthCallerVisitor visitor(self, 3);
1218 visitor.WalkStack();
1219 if (visitor.caller != nullptr) {
1220 result->SetL(visitor.caller->GetDeclaringClass());
1221 }
1222}
1223
Mathieu Chartiere401d142015-04-22 13:56:20 -07001224void UnstartedRuntime::UnstartedJNIMathLog(
1225 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1226 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001227 JValue value;
1228 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1229 result->SetD(log(value.GetD()));
1230}
1231
Mathieu Chartiere401d142015-04-22 13:56:20 -07001232void UnstartedRuntime::UnstartedJNIMathExp(
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 JValue value;
1236 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
1237 result->SetD(exp(value.GetD()));
1238}
1239
Andreas Gampebc4d2182016-02-22 10:03:12 -08001240void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
1241 Thread* self ATTRIBUTE_UNUSED,
1242 ArtMethod* method ATTRIBUTE_UNUSED,
1243 mirror::Object* receiver ATTRIBUTE_UNUSED,
1244 uint32_t* args ATTRIBUTE_UNUSED,
1245 JValue* result) {
1246 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
1247 ? 0
1248 : 1);
1249}
1250
Mathieu Chartiere401d142015-04-22 13:56:20 -07001251void UnstartedRuntime::UnstartedJNIClassGetNameNative(
1252 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1253 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001254 StackHandleScope<1> hs(self);
1255 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
1256}
1257
Andreas Gampebc4d2182016-02-22 10:03:12 -08001258void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
1259 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1260 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
1261 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
1262 result->SetD(bit_cast<double>(long_input));
1263}
1264
Mathieu Chartiere401d142015-04-22 13:56:20 -07001265void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
1266 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1267 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001268 result->SetI(args[0]);
1269}
1270
Mathieu Chartiere401d142015-04-22 13:56:20 -07001271void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
1272 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1273 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001274 result->SetI(args[0]);
1275}
1276
Mathieu Chartiere401d142015-04-22 13:56:20 -07001277void UnstartedRuntime::UnstartedJNIObjectInternalClone(
1278 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1279 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001280 result->SetL(receiver->Clone(self));
1281}
1282
Mathieu Chartiere401d142015-04-22 13:56:20 -07001283void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
1284 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1285 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001286 receiver->NotifyAll(self);
1287}
1288
Mathieu Chartiere401d142015-04-22 13:56:20 -07001289void UnstartedRuntime::UnstartedJNIStringCompareTo(
1290 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
1291 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001292 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
1293 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -07001294 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001295 }
1296 result->SetI(receiver->AsString()->CompareTo(rhs));
1297}
1298
Mathieu Chartiere401d142015-04-22 13:56:20 -07001299void UnstartedRuntime::UnstartedJNIStringIntern(
1300 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1301 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001302 result->SetL(receiver->AsString()->Intern());
1303}
1304
Mathieu Chartiere401d142015-04-22 13:56:20 -07001305void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
1306 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
1307 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001308 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1309}
1310
Mathieu Chartiere401d142015-04-22 13:56:20 -07001311void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
1312 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1313 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001314 StackHandleScope<2> hs(self);
1315 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1316 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1317 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1318}
1319
Mathieu Chartiere401d142015-04-22 13:56:20 -07001320void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
1321 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1322 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -07001323 int32_t length = static_cast<int32_t>(args[1]);
1324 if (length < 0) {
1325 ThrowNegativeArraySizeException(length);
1326 return;
1327 }
1328 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1329 Runtime* runtime = Runtime::Current();
1330 ClassLinker* class_linker = runtime->GetClassLinker();
1331 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001332 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001333 CHECK(self->IsExceptionPending());
1334 return;
1335 }
1336 DCHECK(array_class->IsObjectArrayClass());
1337 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1338 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1339 result->SetL(new_array);
1340}
1341
Mathieu Chartiere401d142015-04-22 13:56:20 -07001342void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
1343 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1344 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001345 ScopedObjectAccessUnchecked soa(self);
1346 if (Runtime::Current()->IsActiveTransaction()) {
1347 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1348 } else {
1349 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1350 }
1351}
1352
Mathieu Chartiere401d142015-04-22 13:56:20 -07001353void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
1354 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1355 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001356 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1357 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1358}
1359
Mathieu Chartiere401d142015-04-22 13:56:20 -07001360void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1361 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1362 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001363 result->SetZ(JNI_TRUE);
1364}
1365
Mathieu Chartiere401d142015-04-22 13:56:20 -07001366void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1367 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1368 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001369 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1370 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1371 jint expectedValue = args[3];
1372 jint newValue = args[4];
1373 bool success;
1374 if (Runtime::Current()->IsActiveTransaction()) {
1375 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1376 expectedValue, newValue);
1377 } else {
1378 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1379 expectedValue, newValue);
1380 }
1381 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1382}
1383
Narayan Kamath34a316f2016-03-30 13:11:18 +01001384void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(
1385 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
1386 uint32_t* args, JValue* result) {
1387 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1388 if (obj == nullptr) {
1389 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
1390 return;
1391 }
1392
1393 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1394 result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
1395}
1396
Mathieu Chartiere401d142015-04-22 13:56:20 -07001397void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1398 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1399 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001400 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1401 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1402 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1403 if (Runtime::Current()->IsActiveTransaction()) {
1404 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1405 } else {
1406 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1407 }
1408}
1409
Andreas Gampe799681b2015-05-15 19:24:12 -07001410void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001411 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1412 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001413 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1414 Primitive::Type primitive_type = component->GetPrimitiveType();
1415 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1416}
1417
Andreas Gampe799681b2015-05-15 19:24:12 -07001418void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001419 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1420 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001421 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1422 Primitive::Type primitive_type = component->GetPrimitiveType();
1423 result->SetI(Primitive::ComponentSize(primitive_type));
1424}
1425
Andreas Gampedd9d0552015-03-09 12:57:41 -07001426typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001427 size_t arg_size);
1428
Mathieu Chartiere401d142015-04-22 13:56:20 -07001429typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001430 uint32_t* args, JValue* result);
1431
1432static bool tables_initialized_ = false;
1433static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1434static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1435
Andreas Gampe799681b2015-05-15 19:24:12 -07001436void UnstartedRuntime::InitializeInvokeHandlers() {
1437#define UNSTARTED_DIRECT(ShortName, Sig) \
1438 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1439#include "unstarted_runtime_list.h"
1440 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1441#undef UNSTARTED_RUNTIME_DIRECT_LIST
1442#undef UNSTARTED_RUNTIME_JNI_LIST
1443#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001444}
1445
Andreas Gampe799681b2015-05-15 19:24:12 -07001446void UnstartedRuntime::InitializeJNIHandlers() {
1447#define UNSTARTED_JNI(ShortName, Sig) \
1448 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1449#include "unstarted_runtime_list.h"
1450 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1451#undef UNSTARTED_RUNTIME_DIRECT_LIST
1452#undef UNSTARTED_RUNTIME_JNI_LIST
1453#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001454}
1455
Andreas Gampe799681b2015-05-15 19:24:12 -07001456void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001457 CHECK(!tables_initialized_);
1458
Andreas Gampe799681b2015-05-15 19:24:12 -07001459 InitializeInvokeHandlers();
1460 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001461
1462 tables_initialized_ = true;
1463}
1464
Andreas Gampe799681b2015-05-15 19:24:12 -07001465void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1466 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001467 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1468 // problems in core libraries.
1469 CHECK(tables_initialized_);
1470
1471 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1472 const auto& iter = invoke_handlers_.find(name);
1473 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001474 // Clear out the result in case it's not zeroed out.
1475 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001476 (*iter->second)(self, shadow_frame, result, arg_offset);
1477 } else {
1478 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001479 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001480 }
1481}
1482
1483// 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 -07001484void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001485 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001486 std::string name(PrettyMethod(method));
1487 const auto& iter = jni_handlers_.find(name);
1488 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001489 // Clear out the result in case it's not zeroed out.
1490 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001491 (*iter->second)(self, method, receiver, args, result);
1492 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001493 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1494 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001495 } else {
1496 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1497 "non-transactional runtime";
1498 }
1499}
1500
1501} // namespace interpreter
1502} // namespace art