blob: 738e52beac9c105af607366c93dd34c9c427c693 [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
Andreas Gampe2969bcd2015-03-09 12:57:41 -070024#include "base/logging.h"
25#include "base/macros.h"
26#include "class_linker.h"
27#include "common_throws.h"
28#include "entrypoints/entrypoint_utils-inl.h"
29#include "handle_scope-inl.h"
30#include "interpreter/interpreter_common.h"
31#include "mirror/array-inl.h"
32#include "mirror/art_method-inl.h"
33#include "mirror/class.h"
Mathieu Chartierdaaf3262015-03-24 13:30:28 -070034#include "mirror/field-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070035#include "mirror/object-inl.h"
36#include "mirror/object_array-inl.h"
37#include "mirror/string-inl.h"
38#include "nth_caller_visitor.h"
39#include "thread.h"
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020040#include "transaction.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070041#include "well_known_classes.h"
Andreas Gampef778eb22015-04-13 14:17:09 -070042#include "zip_archive.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070043
44namespace art {
45namespace interpreter {
46
Andreas Gampe068b0c02015-03-11 12:44:47 -070047static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020048 __attribute__((__format__(__printf__, 2, 3)))
49 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
50
51static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070052 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070053 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020054 va_start(args, fmt);
55 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070056 va_end(args);
57 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020058 va_start(args, fmt);
59 std::string msg;
60 StringAppendV(&msg, fmt, args);
61 va_end(args);
62 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070063 UNREACHABLE();
64 }
65}
66
Andreas Gampe2969bcd2015-03-09 12:57:41 -070067// Helper function to deal with class loading in an unstarted runtime.
68static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
69 Handle<mirror::ClassLoader> class_loader, JValue* result,
70 const std::string& method_name, bool initialize_class,
71 bool abort_if_not_found)
72 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
73 CHECK(className.Get() != nullptr);
74 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
75 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
76
77 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
78 if (found == nullptr && abort_if_not_found) {
79 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070080 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
81 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -070082 }
83 return;
84 }
85 if (found != nullptr && initialize_class) {
86 StackHandleScope<1> hs(self);
87 Handle<mirror::Class> h_class(hs.NewHandle(found));
88 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
89 CHECK(self->IsExceptionPending());
90 return;
91 }
92 }
93 result->SetL(found);
94}
95
96// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
97// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
98// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020099// actually the transaction abort exception. This must not be wrapped, as it signals an
100// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700101static void CheckExceptionGenerateClassNotFound(Thread* self)
102 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
103 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200104 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700105 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200106 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700107 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
108 "ClassNotFoundException");
109 }
110 }
111}
112
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700113static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
116 if (param == nullptr) {
117 AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
118 return nullptr;
119 }
120 return param->AsString();
121}
122
Andreas Gampe799681b2015-05-15 19:24:12 -0700123void UnstartedRuntime::UnstartedClassForName(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700124 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700126 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
127 if (class_name == nullptr) {
128 return;
129 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700130 StackHandleScope<1> hs(self);
131 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
132 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result,
133 "Class.forName", true, false);
134 CheckExceptionGenerateClassNotFound(self);
135}
136
Andreas Gampe799681b2015-05-15 19:24:12 -0700137void UnstartedRuntime::UnstartedClassForNameLong(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700138 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700140 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
141 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700142 return;
143 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700144 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
145 mirror::ClassLoader* class_loader =
146 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
147 StackHandleScope<2> hs(self);
148 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
149 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
150 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
151 initialize_class, false);
152 CheckExceptionGenerateClassNotFound(self);
153}
154
Andreas Gampe799681b2015-05-15 19:24:12 -0700155void UnstartedRuntime::UnstartedClassClassForName(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700156 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700157 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700158 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
159 if (class_name == nullptr) {
160 return;
161 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700162 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
163 mirror::ClassLoader* class_loader =
164 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
165 StackHandleScope<2> hs(self);
166 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
167 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
168 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
169 initialize_class, false);
170 CheckExceptionGenerateClassNotFound(self);
171}
172
Andreas Gampe799681b2015-05-15 19:24:12 -0700173void UnstartedRuntime::UnstartedClassNewInstance(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700174 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700175 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
176 StackHandleScope<3> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700177 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
178 if (param == nullptr) {
179 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
180 return;
181 }
182 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700183 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700184
185 // Check that it's not null.
186 if (h_klass.Get() == nullptr) {
187 AbortTransactionOrFail(self, "Class reference is null for newInstance");
188 return;
189 }
190
191 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
192 if (Runtime::Current()->IsActiveTransaction()) {
193 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200194 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
195 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700196 return;
197 }
198 }
199
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700200 // There are two situations in which we'll abort this run.
201 // 1) If the class isn't yet initialized and initialization fails.
202 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
203 // Note that 2) could likely be handled here, but for safety abort the transaction.
204 bool ok = false;
205 if (Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
206 Handle<mirror::ArtMethod> h_cons(hs.NewHandle(
207 h_klass->FindDeclaredDirectMethod("<init>", "()V")));
208 if (h_cons.Get() != nullptr) {
209 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
210 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
211 EnterInterpreterFromInvoke(self, h_cons.Get(), h_obj.Get(), nullptr, nullptr);
212 if (!self->IsExceptionPending()) {
213 result->SetL(h_obj.Get());
214 ok = true;
215 }
216 } else {
217 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
218 "Could not find default constructor for '%s'",
219 PrettyClass(h_klass.Get()).c_str());
220 }
221 }
222 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700223 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
224 PrettyClass(h_klass.Get()).c_str(),
225 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700226 }
227}
228
Andreas Gampe799681b2015-05-15 19:24:12 -0700229void UnstartedRuntime::UnstartedClassGetDeclaredField(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700230 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700231 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
232 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
233 // going the reflective Dex way.
234 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
235 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700236 ArtField* found = nullptr;
237 ArtField* fields = klass->GetIFields();
238 for (int32_t i = 0, count = klass->NumInstanceFields(); i < count; ++i) {
239 ArtField* f = &fields[i];
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700240 if (name2->Equals(f->GetName())) {
241 found = f;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700242 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700243 }
244 }
245 if (found == nullptr) {
246 fields = klass->GetSFields();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 for (int32_t i = 0, count = klass->NumStaticFields(); i < count; ++i) {
248 ArtField* f = &fields[i];
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700249 if (name2->Equals(f->GetName())) {
250 found = f;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700251 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700252 }
253 }
254 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700255 if (found == nullptr) {
256 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
257 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
258 PrettyDescriptor(klass).c_str());
259 return;
260 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700261 if (Runtime::Current()->IsActiveTransaction()) {
262 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
263 } else {
264 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
265 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700266}
267
Andreas Gampe799681b2015-05-15 19:24:12 -0700268void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700269 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700270 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
271 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
272 mirror::ClassLoader* class_loader =
273 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
274 StackHandleScope<2> hs(self);
275 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
276 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
277 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
278 "VMClassLoader.findLoadedClass", false, false);
279 // This might have an error pending. But semantics are to just return null.
280 if (self->IsExceptionPending()) {
281 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
282 std::string type(PrettyTypeOf(self->GetException()));
283 if (type != "java.lang.InternalError") {
284 self->ClearException();
285 }
286 }
287}
288
Andreas Gampe799681b2015-05-15 19:24:12 -0700289void UnstartedRuntime::UnstartedVoidLookupType(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700290 ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
291 JValue* result,
292 size_t arg_offset ATTRIBUTE_UNUSED)
293 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
294 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
295}
296
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700297// Arraycopy emulation.
298// Note: we can't use any fast copy functions, as they are not available under transaction.
299
300template <typename T>
301static void PrimitiveArrayCopy(Thread* self,
302 mirror::Array* src_array, int32_t src_pos,
303 mirror::Array* dst_array, int32_t dst_pos,
304 int32_t length)
305 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
306 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
307 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
308 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
309 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
310 return;
311 }
312 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
313 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
314 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
315 if (copy_forward) {
316 for (int32_t i = 0; i < length; ++i) {
317 dst->Set(dst_pos + i, src->Get(src_pos + i));
318 }
319 } else {
320 for (int32_t i = 1; i <= length; ++i) {
321 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
322 }
323 }
324}
325
Andreas Gampe799681b2015-05-15 19:24:12 -0700326void UnstartedRuntime::UnstartedSystemArraycopy(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700327 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700328 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
329 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700330 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
331 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700332 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700333 mirror::Array* src_array = shadow_frame->GetVRegReference(arg_offset)->AsArray();
334 mirror::Array* dst_array = shadow_frame->GetVRegReference(arg_offset + 2)->AsArray();
335
336 // Null checking.
337 if (src_array == nullptr) {
338 AbortTransactionOrFail(self, "src is null in arraycopy.");
339 return;
340 }
341 if (dst_array == nullptr) {
342 AbortTransactionOrFail(self, "dst is null in arraycopy.");
343 return;
344 }
345
346 // Bounds checking.
347 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
348 UNLIKELY(src_pos > src_array->GetLength() - length) ||
349 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
350 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
351 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
352 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
353 length);
354 AbortTransactionOrFail(self, "Index out of bounds.");
355 return;
356 }
357
358 // Type checking.
359 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
360 GetComponentType();
361
362 if (!src_type->IsPrimitive()) {
363 // Check that the second type is not primitive.
364 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
365 GetComponentType();
366 if (trg_type->IsPrimitiveInt()) {
367 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
368 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
369 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
370 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700371 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700372
373 // For simplicity only do this if the component types are the same. Otherwise we have to copy
374 // even more code from the object-array functions.
375 if (src_type != trg_type) {
376 AbortTransactionOrFail(self, "Types not the same in arraycopy: %s vs %s",
377 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
378 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
379 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700380 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700381
382 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
383 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
384 if (src == dst) {
385 // Can overlap, but not have type mismatches.
386 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
387 if (copy_forward) {
388 for (int32_t i = 0; i < length; ++i) {
389 dst->Set(dst_pos + i, src->Get(src_pos + i));
390 }
391 } else {
392 for (int32_t i = 1; i <= length; ++i) {
393 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
394 }
395 }
396 } else {
397 // Can't overlap. Would need type checks, but we abort above.
398 for (int32_t i = 0; i < length; ++i) {
399 dst->Set(dst_pos + i, src->Get(src_pos + i));
400 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700401 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700402 } else if (src_type->IsPrimitiveChar()) {
403 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
404 } else if (src_type->IsPrimitiveInt()) {
405 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700406 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700407 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700408 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700409 }
410}
411
Andreas Gampe799681b2015-05-15 19:24:12 -0700412void UnstartedRuntime::UnstartedSystemArraycopyChar(
413 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
414 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
415 // Just forward.
416 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
417}
418
419void UnstartedRuntime::UnstartedSystemArraycopyInt(
420 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
421 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
422 // Just forward.
423 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
424}
425
426void UnstartedRuntime::UnstartedThreadLocalGet(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700427 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700428 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
429 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
430 bool ok = false;
431 if (caller == "java.lang.String java.lang.IntegralToString.convertInt"
432 "(java.lang.AbstractStringBuilder, int)") {
433 // Allocate non-threadlocal buffer.
434 result->SetL(mirror::CharArray::Alloc(self, 11));
435 ok = true;
436 } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") {
437 // Note: RealToString is implemented and used in a different fashion than IntegralToString.
438 // Conversion is done over an actual object of RealToString (the conversion method is an
439 // instance method). This means it is not as clear whether it is correct to return a new
440 // object each time. The caller needs to be inspected by hand to see whether it (incorrectly)
441 // stores the object for later use.
442 // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString.
443 if (shadow_frame->GetLink()->GetLink() != nullptr) {
444 std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod()));
445 if (caller2 == "java.lang.String java.lang.Double.toString(double)") {
446 // Allocate new object.
447 StackHandleScope<2> hs(self);
448 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
449 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
450 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
451 h_real_to_string_class->AllocObject(self)));
452 if (h_real_to_string_obj.Get() != nullptr) {
453 mirror::ArtMethod* init_method =
454 h_real_to_string_class->FindDirectMethod("<init>", "()V");
455 if (init_method == nullptr) {
456 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
457 } else {
458 JValue invoke_result;
459 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
460 nullptr);
461 if (!self->IsExceptionPending()) {
462 result->SetL(h_real_to_string_obj.Get());
463 ok = true;
464 }
465 }
466 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700467 }
468 }
469 }
470
471 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700472 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700473 }
474}
475
Andreas Gampe799681b2015-05-15 19:24:12 -0700476void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700477 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700478 double in = shadow_frame->GetVRegDouble(arg_offset);
479 double out;
480 // Special cases:
481 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
482 // -1 < in < 0 -> out := -0.
483 if (-1.0 < in && in < 0) {
484 out = -0.0;
485 } else {
486 out = ceil(in);
487 }
488 result->SetD(out);
489}
490
Andreas Gampe799681b2015-05-15 19:24:12 -0700491void UnstartedRuntime::UnstartedArtMethodGetMethodName(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700492 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700493 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
494 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
495 result->SetL(method->GetNameAsString(self));
496}
497
Andreas Gampe799681b2015-05-15 19:24:12 -0700498void UnstartedRuntime::UnstartedObjectHashCode(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700499 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700500 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
501 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
502 result->SetI(obj->IdentityHashCode());
503}
504
Andreas Gampe799681b2015-05-15 19:24:12 -0700505void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700506 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700507 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000508 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700509}
510
Andreas Gampedd9d0552015-03-09 12:57:41 -0700511static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
512 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
513 const DexFile* dex_file = dex_cache->GetDexFile();
514 if (dex_file == nullptr) {
515 return nullptr;
516 }
517
518 // Create the direct byte buffer.
519 JNIEnv* env = self->GetJniEnv();
520 DCHECK(env != nullptr);
521 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700522 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
523 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700524 DCHECK(self->IsExceptionPending());
525 return nullptr;
526 }
527
528 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700529 args[0].l = byte_buffer.get();
530
531 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
532 WellKnownClasses::com_android_dex_Dex,
533 WellKnownClasses::com_android_dex_Dex_create,
534 args));
535
536 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700537}
538
Andreas Gampe799681b2015-05-15 19:24:12 -0700539void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700540 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
541 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
542 // We will create the Dex object, but the image writer will release it before creating the
543 // art file.
544 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
545 bool have_dex = false;
546 if (src != nullptr) {
547 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
548 if (dex != nullptr) {
549 have_dex = true;
550 result->SetL(dex);
551 }
552 }
553 if (!have_dex) {
554 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200555 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700556 }
557}
558
559static void UnstartedMemoryPeek(
560 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
561 int64_t address = shadow_frame->GetVRegLong(arg_offset);
562 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
563 // aborting the transaction.
564
565 switch (type) {
566 case Primitive::kPrimByte: {
567 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
568 return;
569 }
570
571 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700572 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
573 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700574 return;
575 }
576
577 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700578 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
579 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700580 return;
581 }
582
583 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700584 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
585 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700586 return;
587 }
588
589 case Primitive::kPrimBoolean:
590 case Primitive::kPrimChar:
591 case Primitive::kPrimFloat:
592 case Primitive::kPrimDouble:
593 case Primitive::kPrimVoid:
594 case Primitive::kPrimNot:
595 LOG(FATAL) << "Not in the Memory API: " << type;
596 UNREACHABLE();
597 }
598 LOG(FATAL) << "Should not reach here";
599 UNREACHABLE();
600}
601
Andreas Gampe799681b2015-05-15 19:24:12 -0700602void UnstartedRuntime::UnstartedMemoryPeekByte(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700603 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
604 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700605 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
606}
607
608void UnstartedRuntime::UnstartedMemoryPeekShort(
609 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
610 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
611 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
612}
613
614void UnstartedRuntime::UnstartedMemoryPeekInt(
615 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
616 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
617 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
618}
619
620void UnstartedRuntime::UnstartedMemoryPeekLong(
621 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
622 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
623 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700624}
625
626static void UnstartedMemoryPeekArray(
627 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
628 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
629 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
630 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
631 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200632 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700633 return;
634 }
635 mirror::Array* array = obj->AsArray();
636
637 int offset = shadow_frame->GetVReg(arg_offset + 3);
638 int count = shadow_frame->GetVReg(arg_offset + 4);
639 if (offset < 0 || offset + count > array->GetLength()) {
640 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
641 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200642 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700643 return;
644 }
645
646 switch (type) {
647 case Primitive::kPrimByte: {
648 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
649 mirror::ByteArray* byte_array = array->AsByteArray();
650 for (int32_t i = 0; i < count; ++i, ++address) {
651 byte_array->SetWithoutChecks<true>(i + offset, *address);
652 }
653 return;
654 }
655
656 case Primitive::kPrimShort:
657 case Primitive::kPrimInt:
658 case Primitive::kPrimLong:
659 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
660 UNREACHABLE();
661
662 case Primitive::kPrimBoolean:
663 case Primitive::kPrimChar:
664 case Primitive::kPrimFloat:
665 case Primitive::kPrimDouble:
666 case Primitive::kPrimVoid:
667 case Primitive::kPrimNot:
668 LOG(FATAL) << "Not in the Memory API: " << type;
669 UNREACHABLE();
670 }
671 LOG(FATAL) << "Should not reach here";
672 UNREACHABLE();
673}
674
Andreas Gampe799681b2015-05-15 19:24:12 -0700675void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700676 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
677 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700678 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700679}
680
Andreas Gampef778eb22015-04-13 14:17:09 -0700681// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700682void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Andreas Gampef778eb22015-04-13 14:17:09 -0700683 Thread* self,
684 ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
685 JValue* result,
686 size_t arg_offset ATTRIBUTE_UNUSED)
687 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
688 Runtime* runtime = Runtime::Current();
689 const std::vector<const DexFile*>& path = runtime->GetClassLinker()->GetBootClassPath();
690 std::string canonical(DexFile::GetDexCanonicalLocation(path[0]->GetLocation().c_str()));
691 mirror::String* string_data;
692
693 // Use a block to enclose the I/O and MemMap code so buffers are released early.
694 {
695 std::string error_msg;
696 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(canonical.c_str(), &error_msg));
697 if (zip_archive.get() == nullptr) {
698 AbortTransactionOrFail(self, "Could not open zip file %s: %s", canonical.c_str(),
699 error_msg.c_str());
700 return;
701 }
702 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
703 &error_msg));
704 if (zip_entry.get() == nullptr) {
705 AbortTransactionOrFail(self, "Could not find security.properties file in %s: %s",
706 canonical.c_str(), error_msg.c_str());
707 return;
708 }
709 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(canonical.c_str(),
710 "java/security/security.properties",
711 &error_msg));
712 if (map.get() == nullptr) {
713 AbortTransactionOrFail(self, "Could not unzip security.properties file in %s: %s",
714 canonical.c_str(), error_msg.c_str());
715 return;
716 }
717
718 uint32_t length = zip_entry->GetUncompressedLength();
719 std::unique_ptr<char[]> tmp(new char[length + 1]);
720 memcpy(tmp.get(), map->Begin(), length);
721 tmp.get()[length] = 0; // null terminator
722
723 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
724 }
725
726 if (string_data == nullptr) {
727 AbortTransactionOrFail(self, "Could not create string from file content of %s",
728 canonical.c_str());
729 return;
730 }
731
732 // Create a StringReader.
733 StackHandleScope<3> hs(self);
734 Handle<mirror::String> h_string(hs.NewHandle(string_data));
735
736 Handle<mirror::Class> h_class(hs.NewHandle(
737 runtime->GetClassLinker()->FindClass(self,
738 "Ljava/io/StringReader;",
739 NullHandle<mirror::ClassLoader>())));
740 if (h_class.Get() == nullptr) {
741 AbortTransactionOrFail(self, "Could not find StringReader class");
742 return;
743 }
744
745 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
746 AbortTransactionOrFail(self, "Could not initialize StringReader class");
747 return;
748 }
749
750 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
751 if (h_obj.Get() == nullptr) {
752 AbortTransactionOrFail(self, "Could not allocate StringReader object");
753 return;
754 }
755
756 mirror::ArtMethod* constructor = h_class->FindDeclaredDirectMethod("<init>",
757 "(Ljava/lang/String;)V");
758 if (constructor == nullptr) {
759 AbortTransactionOrFail(self, "Could not find StringReader constructor");
760 return;
761 }
762
763 uint32_t args[1];
764 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
765 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
766
767 if (self->IsExceptionPending()) {
768 AbortTransactionOrFail(self, "Could not run StringReader constructor");
769 return;
770 }
771
772 result->SetL(h_obj.Get());
773}
774
Kenny Root1c9e61c2015-05-14 15:58:17 -0700775// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700776void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Kenny Root1c9e61c2015-05-14 15:58:17 -0700777 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
778 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
779 jint start = shadow_frame->GetVReg(arg_offset + 1);
780 jint end = shadow_frame->GetVReg(arg_offset + 2);
781 jint index = shadow_frame->GetVReg(arg_offset + 4);
782 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
783 if (string == nullptr) {
784 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
785 return;
786 }
Kenny Root57f91e82015-05-14 15:58:17 -0700787 DCHECK_GE(start, 0);
788 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700789 StackHandleScope<1> hs(self);
790 Handle<mirror::CharArray> h_char_array(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700791 DCHECK_LE(index, h_char_array->GetLength());
792 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700793 string->GetChars(start, end, h_char_array, index);
794}
795
796// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700797void UnstartedRuntime::UnstartedStringCharAt(
Kenny Root1c9e61c2015-05-14 15:58:17 -0700798 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
799 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
800 jint index = shadow_frame->GetVReg(arg_offset + 1);
801 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
802 if (string == nullptr) {
803 AbortTransactionOrFail(self, "String.charAt with null object");
804 return;
805 }
806 result->SetC(string->CharAt(index));
807}
808
Kenny Root57f91e82015-05-14 15:58:17 -0700809// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700810void UnstartedRuntime::UnstartedStringSetCharAt(
Kenny Root57f91e82015-05-14 15:58:17 -0700811 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
812 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
813 jint index = shadow_frame->GetVReg(arg_offset + 1);
814 jchar c = shadow_frame->GetVReg(arg_offset + 2);
815 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
816 if (string == nullptr) {
817 AbortTransactionOrFail(self, "String.setCharAt with null object");
818 return;
819 }
820 string->SetCharAt(index, c);
821}
822
Kenny Root1c9e61c2015-05-14 15:58:17 -0700823// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700824void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Kenny Root1c9e61c2015-05-14 15:58:17 -0700825 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
826 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
827 jint offset = shadow_frame->GetVReg(arg_offset);
828 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
829 DCHECK_GE(char_count, 0);
830 StackHandleScope<1> hs(self);
831 Handle<mirror::CharArray> h_char_array(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
832 Runtime* runtime = Runtime::Current();
833 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
834 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
835}
836
837// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700838void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Kenny Root57f91e82015-05-14 15:58:17 -0700839 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
840 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
841 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
842 if (to_copy == nullptr) {
843 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
844 return;
845 }
846 StackHandleScope<1> hs(self);
847 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
848 Runtime* runtime = Runtime::Current();
849 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
850 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
851 allocator));
852}
853
Andreas Gampe799681b2015-05-15 19:24:12 -0700854void UnstartedRuntime::UnstartedStringFastSubstring(
Kenny Root1c9e61c2015-05-14 15:58:17 -0700855 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
856 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
857 jint start = shadow_frame->GetVReg(arg_offset + 1);
858 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700859 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700860 DCHECK_GE(length, 0);
861 StackHandleScope<1> hs(self);
862 Handle<mirror::String> h_string(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700863 DCHECK_LE(start, h_string->GetLength());
864 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700865 Runtime* runtime = Runtime::Current();
866 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
867 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
868}
869
Kenny Root57f91e82015-05-14 15:58:17 -0700870// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700871void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700872 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
873 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
874 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
875 if (string == nullptr) {
876 AbortTransactionOrFail(self, "String.charAt with null object");
877 return;
878 }
879 result->SetL(string->ToCharArray(self));
880}
881
Andreas Gampe799681b2015-05-15 19:24:12 -0700882void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700883 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
884 mirror::Object* receiver ATTRIBUTE_UNUSED,
885 uint32_t* args,
886 JValue* result)
887 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
888 int32_t length = args[1];
889 DCHECK_GE(length, 0);
890 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
891 Runtime* runtime = Runtime::Current();
892 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
893 DCHECK(array_class != nullptr);
894 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
895 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
896 array_class->GetComponentSizeShift(), allocator));
897}
898
Andreas Gampe799681b2015-05-15 19:24:12 -0700899void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700900 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
901 mirror::Object* receiver ATTRIBUTE_UNUSED,
902 uint32_t* args ATTRIBUTE_UNUSED,
903 JValue* result) {
904 result->SetL(nullptr);
905}
906
Andreas Gampe799681b2015-05-15 19:24:12 -0700907void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700908 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
909 mirror::Object* receiver ATTRIBUTE_UNUSED,
910 uint32_t* args ATTRIBUTE_UNUSED,
911 JValue* result)
912 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
913 NthCallerVisitor visitor(self, 3);
914 visitor.WalkStack();
915 if (visitor.caller != nullptr) {
916 result->SetL(visitor.caller->GetDeclaringClass());
917 }
918}
919
Andreas Gampe799681b2015-05-15 19:24:12 -0700920void UnstartedRuntime::UnstartedJNIMathLog(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700921 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
922 mirror::Object* receiver ATTRIBUTE_UNUSED,
923 uint32_t* args,
924 JValue* result) {
925 JValue value;
926 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
927 result->SetD(log(value.GetD()));
928}
929
Andreas Gampe799681b2015-05-15 19:24:12 -0700930void UnstartedRuntime::UnstartedJNIMathExp(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700931 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
932 mirror::Object* receiver ATTRIBUTE_UNUSED,
933 uint32_t* args,
934 JValue* result) {
935 JValue value;
936 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
937 result->SetD(exp(value.GetD()));
938}
939
Andreas Gampe799681b2015-05-15 19:24:12 -0700940void UnstartedRuntime::UnstartedJNIClassGetNameNative(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700941 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
942 mirror::Object* receiver,
943 uint32_t* args ATTRIBUTE_UNUSED,
944 JValue* result)
945 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
946 StackHandleScope<1> hs(self);
947 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
948}
949
Andreas Gampe799681b2015-05-15 19:24:12 -0700950void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700951 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
952 mirror::Object* receiver ATTRIBUTE_UNUSED,
953 uint32_t* args,
954 JValue* result) {
955 result->SetI(args[0]);
956}
957
Andreas Gampe799681b2015-05-15 19:24:12 -0700958void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700959 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
960 mirror::Object* receiver ATTRIBUTE_UNUSED,
961 uint32_t* args,
962 JValue* result) {
963 result->SetI(args[0]);
964}
965
Andreas Gampe799681b2015-05-15 19:24:12 -0700966void UnstartedRuntime::UnstartedJNIObjectInternalClone(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700967 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
968 mirror::Object* receiver,
969 uint32_t* args ATTRIBUTE_UNUSED,
970 JValue* result)
971 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
972 result->SetL(receiver->Clone(self));
973}
974
Andreas Gampe799681b2015-05-15 19:24:12 -0700975void UnstartedRuntime::UnstartedJNIObjectNotifyAll(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700976 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
977 mirror::Object* receiver,
978 uint32_t* args ATTRIBUTE_UNUSED,
979 JValue* result ATTRIBUTE_UNUSED)
980 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
981 receiver->NotifyAll(self);
982}
983
Andreas Gampe799681b2015-05-15 19:24:12 -0700984void UnstartedRuntime::UnstartedJNIStringCompareTo(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700985 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
986 mirror::Object* receiver,
987 uint32_t* args,
988 JValue* result)
989 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
990 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
991 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700992 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700993 }
994 result->SetI(receiver->AsString()->CompareTo(rhs));
995}
996
Andreas Gampe799681b2015-05-15 19:24:12 -0700997void UnstartedRuntime::UnstartedJNIStringIntern(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700998 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
999 mirror::Object* receiver,
1000 uint32_t* args ATTRIBUTE_UNUSED,
1001 JValue* result)
1002 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1003 result->SetL(receiver->AsString()->Intern());
1004}
1005
Andreas Gampe799681b2015-05-15 19:24:12 -07001006void UnstartedRuntime::UnstartedJNIStringFastIndexOf(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001007 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1008 mirror::Object* receiver,
1009 uint32_t* args,
1010 JValue* result)
1011 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1012 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
1013}
1014
Andreas Gampe799681b2015-05-15 19:24:12 -07001015void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001016 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1017 mirror::Object* receiver ATTRIBUTE_UNUSED,
1018 uint32_t* args,
1019 JValue* result)
1020 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1021 StackHandleScope<2> hs(self);
1022 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
1023 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
1024 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
1025}
1026
Andreas Gampe799681b2015-05-15 19:24:12 -07001027void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(Thread* self,
Andreas Gampee598e042015-04-10 14:57:10 -07001028 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1029 mirror::Object* receiver ATTRIBUTE_UNUSED,
1030 uint32_t* args,
1031 JValue* result)
1032 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1033 int32_t length = static_cast<int32_t>(args[1]);
1034 if (length < 0) {
1035 ThrowNegativeArraySizeException(length);
1036 return;
1037 }
1038 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
1039 Runtime* runtime = Runtime::Current();
1040 ClassLinker* class_linker = runtime->GetClassLinker();
1041 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -07001042 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -07001043 CHECK(self->IsExceptionPending());
1044 return;
1045 }
1046 DCHECK(array_class->IsObjectArrayClass());
1047 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
1048 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
1049 result->SetL(new_array);
1050}
1051
Andreas Gampe799681b2015-05-15 19:24:12 -07001052void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001053 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1054 mirror::Object* receiver ATTRIBUTE_UNUSED,
1055 uint32_t* args ATTRIBUTE_UNUSED,
1056 JValue* result)
1057 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1058 ScopedObjectAccessUnchecked soa(self);
1059 if (Runtime::Current()->IsActiveTransaction()) {
1060 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
1061 } else {
1062 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
1063 }
1064}
1065
Andreas Gampe799681b2015-05-15 19:24:12 -07001066void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001067 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1068 mirror::Object* receiver ATTRIBUTE_UNUSED,
1069 uint32_t* args,
1070 JValue* result)
1071 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1072 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1073 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
1074}
1075
Andreas Gampe799681b2015-05-15 19:24:12 -07001076void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001077 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1078 mirror::Object* receiver ATTRIBUTE_UNUSED,
1079 uint32_t* args ATTRIBUTE_UNUSED,
1080 JValue* result) {
1081 result->SetZ(JNI_TRUE);
1082}
1083
Andreas Gampe799681b2015-05-15 19:24:12 -07001084void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001085 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1086 mirror::Object* receiver ATTRIBUTE_UNUSED,
1087 uint32_t* args,
1088 JValue* result)
1089 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1090 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1091 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1092 jint expectedValue = args[3];
1093 jint newValue = args[4];
1094 bool success;
1095 if (Runtime::Current()->IsActiveTransaction()) {
1096 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1097 expectedValue, newValue);
1098 } else {
1099 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1100 expectedValue, newValue);
1101 }
1102 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1103}
1104
Andreas Gampe799681b2015-05-15 19:24:12 -07001105void UnstartedRuntime::UnstartedJNIUnsafePutObject(Thread* self ATTRIBUTE_UNUSED,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001106 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1107 mirror::Object* receiver ATTRIBUTE_UNUSED,
1108 uint32_t* args,
1109 JValue* result ATTRIBUTE_UNUSED)
1110 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1111 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1112 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1113 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1114 if (Runtime::Current()->IsActiveTransaction()) {
1115 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1116 } else {
1117 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1118 }
1119}
1120
Andreas Gampe799681b2015-05-15 19:24:12 -07001121void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001122 Thread* self ATTRIBUTE_UNUSED,
1123 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1124 mirror::Object* receiver ATTRIBUTE_UNUSED,
1125 uint32_t* args,
1126 JValue* result)
1127 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1128 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1129 Primitive::Type primitive_type = component->GetPrimitiveType();
1130 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1131}
1132
Andreas Gampe799681b2015-05-15 19:24:12 -07001133void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001134 Thread* self ATTRIBUTE_UNUSED,
1135 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
1136 mirror::Object* receiver ATTRIBUTE_UNUSED,
1137 uint32_t* args,
1138 JValue* result)
1139 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
1140 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1141 Primitive::Type primitive_type = component->GetPrimitiveType();
1142 result->SetI(Primitive::ComponentSize(primitive_type));
1143}
1144
Andreas Gampedd9d0552015-03-09 12:57:41 -07001145typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001146 size_t arg_size);
1147
Andreas Gampedd9d0552015-03-09 12:57:41 -07001148typedef void (*JNIHandler)(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001149 uint32_t* args, JValue* result);
1150
1151static bool tables_initialized_ = false;
1152static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1153static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1154
Andreas Gampe799681b2015-05-15 19:24:12 -07001155void UnstartedRuntime::InitializeInvokeHandlers() {
1156#define UNSTARTED_DIRECT(ShortName, Sig) \
1157 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1158#include "unstarted_runtime_list.h"
1159 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1160#undef UNSTARTED_RUNTIME_DIRECT_LIST
1161#undef UNSTARTED_RUNTIME_JNI_LIST
1162#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001163}
1164
Andreas Gampe799681b2015-05-15 19:24:12 -07001165void UnstartedRuntime::InitializeJNIHandlers() {
1166#define UNSTARTED_JNI(ShortName, Sig) \
1167 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1168#include "unstarted_runtime_list.h"
1169 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1170#undef UNSTARTED_RUNTIME_DIRECT_LIST
1171#undef UNSTARTED_RUNTIME_JNI_LIST
1172#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001173}
1174
Andreas Gampe799681b2015-05-15 19:24:12 -07001175void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001176 CHECK(!tables_initialized_);
1177
Andreas Gampe799681b2015-05-15 19:24:12 -07001178 InitializeInvokeHandlers();
1179 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001180
1181 tables_initialized_ = true;
1182}
1183
Andreas Gampe799681b2015-05-15 19:24:12 -07001184void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1185 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001186 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1187 // problems in core libraries.
1188 CHECK(tables_initialized_);
1189
1190 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1191 const auto& iter = invoke_handlers_.find(name);
1192 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001193 // Clear out the result in case it's not zeroed out.
1194 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001195 (*iter->second)(self, shadow_frame, result, arg_offset);
1196 } else {
1197 // Not special, continue with regular interpreter execution.
1198 artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
1199 }
1200}
1201
1202// Hand select a number of methods to be run in a not yet started runtime without using JNI.
Andreas Gampe799681b2015-05-15 19:24:12 -07001203void UnstartedRuntime::Jni(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver,
1204 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001205 std::string name(PrettyMethod(method));
1206 const auto& iter = jni_handlers_.find(name);
1207 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001208 // Clear out the result in case it's not zeroed out.
1209 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001210 (*iter->second)(self, method, receiver, args, result);
1211 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001212 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1213 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001214 } else {
1215 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1216 "non-transactional runtime";
1217 }
1218}
1219
1220} // namespace interpreter
1221} // namespace art