blob: 60ad0cbb10eda87cbd845e3030d671e1c8e8fe9e [file] [log] [blame]
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "unstarted_runtime.h"
18
19#include <cmath>
20#include <unordered_map>
21
Andreas Gampeaacc25d2015-04-01 14:49:06 -070022#include "ScopedLocalRef.h"
23
Mathieu Chartiere401d142015-04-22 13:56:20 -070024#include "art_method-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070025#include "base/logging.h"
26#include "base/macros.h"
27#include "class_linker.h"
28#include "common_throws.h"
29#include "entrypoints/entrypoint_utils-inl.h"
30#include "handle_scope-inl.h"
31#include "interpreter/interpreter_common.h"
32#include "mirror/array-inl.h"
Andreas Gampe2969bcd2015-03-09 12:57:41 -070033#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)))
Mathieu Chartier90443472015-07-16 20:32:27 -070049 SHARED_REQUIRES(Locks::mutator_lock_);
Sebastien Hertz45b15972015-04-03 16:07:05 +020050
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)
Mathieu Chartier90443472015-07-16 20:32:27 -070072 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -070073 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700102 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700103 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)
Mathieu Chartier90443472015-07-16 20:32:27 -0700114 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700115 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(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700124 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700125 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
126 if (class_name == nullptr) {
127 return;
128 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700129 StackHandleScope<1> hs(self);
130 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800131 UnstartedRuntimeFindClass(self,
132 h_class_name,
133 ScopedNullHandle<mirror::ClassLoader>(),
134 result,
135 "Class.forName",
136 true,
137 false);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700138 CheckExceptionGenerateClassNotFound(self);
139}
140
Andreas Gampe799681b2015-05-15 19:24:12 -0700141void UnstartedRuntime::UnstartedClassForNameLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700142 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700143 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
144 if (class_name == nullptr) {
Andreas Gampebf4d3af2015-04-14 10:10:33 -0700145 return;
146 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700147 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
148 mirror::ClassLoader* class_loader =
149 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
150 StackHandleScope<2> hs(self);
151 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
152 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
153 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
154 initialize_class, false);
155 CheckExceptionGenerateClassNotFound(self);
156}
157
Andreas Gampe799681b2015-05-15 19:24:12 -0700158void UnstartedRuntime::UnstartedClassClassForName(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700159 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700160 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset);
161 if (class_name == nullptr) {
162 return;
163 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700164 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
165 mirror::ClassLoader* class_loader =
166 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
167 StackHandleScope<2> hs(self);
168 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
169 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
170 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
171 initialize_class, false);
172 CheckExceptionGenerateClassNotFound(self);
173}
174
Andreas Gampe799681b2015-05-15 19:24:12 -0700175void UnstartedRuntime::UnstartedClassNewInstance(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700176 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
177 StackHandleScope<2> hs(self); // Class, constructor, object.
Andreas Gampe5d4bb1d2015-04-14 22:16:14 -0700178 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
179 if (param == nullptr) {
180 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
181 return;
182 }
183 mirror::Class* klass = param->AsClass();
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700184 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700185
186 // Check that it's not null.
187 if (h_klass.Get() == nullptr) {
188 AbortTransactionOrFail(self, "Class reference is null for newInstance");
189 return;
190 }
191
192 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
193 if (Runtime::Current()->IsActiveTransaction()) {
194 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200195 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
196 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700197 return;
198 }
199 }
200
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700201 // There are two situations in which we'll abort this run.
202 // 1) If the class isn't yet initialized and initialization fails.
203 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
204 // Note that 2) could likely be handled here, but for safety abort the transaction.
205 bool ok = false;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700206 auto* cl = Runtime::Current()->GetClassLinker();
207 if (cl->EnsureInitialized(self, h_klass, true, true)) {
208 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize());
209 if (cons != nullptr) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700210 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
211 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700212 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700213 if (!self->IsExceptionPending()) {
214 result->SetL(h_obj.Get());
215 ok = true;
216 }
217 } else {
218 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
219 "Could not find default constructor for '%s'",
220 PrettyClass(h_klass.Get()).c_str());
221 }
222 }
223 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700224 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
225 PrettyClass(h_klass.Get()).c_str(),
226 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700227 }
228}
229
Andreas Gampe799681b2015-05-15 19:24:12 -0700230void UnstartedRuntime::UnstartedClassGetDeclaredField(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700231 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700232 // 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;
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700237 for (ArtField& field : klass->GetIFields()) {
238 if (name2->Equals(field.GetName())) {
239 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700240 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700241 }
242 }
243 if (found == nullptr) {
Mathieu Chartier54d220e2015-07-30 16:20:06 -0700244 for (ArtField& field : klass->GetSFields()) {
245 if (name2->Equals(field.GetName())) {
246 found = &field;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700247 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700248 }
249 }
250 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700251 if (found == nullptr) {
252 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
253 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
254 PrettyDescriptor(klass).c_str());
255 return;
256 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700257 if (Runtime::Current()->IsActiveTransaction()) {
258 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
259 } else {
260 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
261 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700262}
263
Andreas Gampe799681b2015-05-15 19:24:12 -0700264void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700265 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700266 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
267 mirror::ClassLoader* class_loader =
268 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
269 StackHandleScope<2> hs(self);
270 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
271 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
272 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
273 "VMClassLoader.findLoadedClass", false, false);
274 // This might have an error pending. But semantics are to just return null.
275 if (self->IsExceptionPending()) {
276 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
277 std::string type(PrettyTypeOf(self->GetException()));
278 if (type != "java.lang.InternalError") {
279 self->ClearException();
280 }
281 }
282}
283
Mathieu Chartiere401d142015-04-22 13:56:20 -0700284void UnstartedRuntime::UnstartedVoidLookupType(
285 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
286 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700287 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
288}
289
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700290// Arraycopy emulation.
291// Note: we can't use any fast copy functions, as they are not available under transaction.
292
293template <typename T>
294static void PrimitiveArrayCopy(Thread* self,
295 mirror::Array* src_array, int32_t src_pos,
296 mirror::Array* dst_array, int32_t dst_pos,
297 int32_t length)
Mathieu Chartier90443472015-07-16 20:32:27 -0700298 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700299 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
300 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
301 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
302 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
303 return;
304 }
305 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
306 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
307 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
308 if (copy_forward) {
309 for (int32_t i = 0; i < length; ++i) {
310 dst->Set(dst_pos + i, src->Get(src_pos + i));
311 }
312 } else {
313 for (int32_t i = 1; i <= length; ++i) {
314 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
315 }
316 }
317}
318
Andreas Gampe799681b2015-05-15 19:24:12 -0700319void UnstartedRuntime::UnstartedSystemArraycopy(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700321 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700322 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
323 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700324 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700325 mirror::Array* src_array = shadow_frame->GetVRegReference(arg_offset)->AsArray();
326 mirror::Array* dst_array = shadow_frame->GetVRegReference(arg_offset + 2)->AsArray();
327
328 // Null checking.
329 if (src_array == nullptr) {
330 AbortTransactionOrFail(self, "src is null in arraycopy.");
331 return;
332 }
333 if (dst_array == nullptr) {
334 AbortTransactionOrFail(self, "dst is null in arraycopy.");
335 return;
336 }
337
338 // Bounds checking.
339 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
340 UNLIKELY(src_pos > src_array->GetLength() - length) ||
341 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
342 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
343 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
344 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
345 length);
346 AbortTransactionOrFail(self, "Index out of bounds.");
347 return;
348 }
349
350 // Type checking.
351 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
352 GetComponentType();
353
354 if (!src_type->IsPrimitive()) {
355 // Check that the second type is not primitive.
356 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
357 GetComponentType();
358 if (trg_type->IsPrimitiveInt()) {
359 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
360 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
361 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
362 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700363 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700364
365 // For simplicity only do this if the component types are the same. Otherwise we have to copy
366 // even more code from the object-array functions.
367 if (src_type != trg_type) {
368 AbortTransactionOrFail(self, "Types not the same in arraycopy: %s vs %s",
369 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
370 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
371 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700372 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700373
374 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
375 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
376 if (src == dst) {
377 // Can overlap, but not have type mismatches.
378 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
379 if (copy_forward) {
380 for (int32_t i = 0; i < length; ++i) {
381 dst->Set(dst_pos + i, src->Get(src_pos + i));
382 }
383 } else {
384 for (int32_t i = 1; i <= length; ++i) {
385 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
386 }
387 }
388 } else {
389 // Can't overlap. Would need type checks, but we abort above.
390 for (int32_t i = 0; i < length; ++i) {
391 dst->Set(dst_pos + i, src->Get(src_pos + i));
392 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700393 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700394 } else if (src_type->IsPrimitiveChar()) {
395 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
396 } else if (src_type->IsPrimitiveInt()) {
397 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700398 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700399 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700400 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700401 }
402}
403
Andreas Gampe799681b2015-05-15 19:24:12 -0700404void UnstartedRuntime::UnstartedSystemArraycopyChar(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700405 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700406 // Just forward.
407 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
408}
409
410void UnstartedRuntime::UnstartedSystemArraycopyInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700411 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700412 // Just forward.
413 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
414}
415
416void UnstartedRuntime::UnstartedThreadLocalGet(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700417 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700418 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
419 bool ok = false;
420 if (caller == "java.lang.String java.lang.IntegralToString.convertInt"
421 "(java.lang.AbstractStringBuilder, int)") {
422 // Allocate non-threadlocal buffer.
423 result->SetL(mirror::CharArray::Alloc(self, 11));
424 ok = true;
425 } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") {
426 // Note: RealToString is implemented and used in a different fashion than IntegralToString.
427 // Conversion is done over an actual object of RealToString (the conversion method is an
428 // instance method). This means it is not as clear whether it is correct to return a new
429 // object each time. The caller needs to be inspected by hand to see whether it (incorrectly)
430 // stores the object for later use.
431 // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString.
432 if (shadow_frame->GetLink()->GetLink() != nullptr) {
433 std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod()));
434 if (caller2 == "java.lang.String java.lang.Double.toString(double)") {
435 // Allocate new object.
436 StackHandleScope<2> hs(self);
437 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
438 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
439 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
440 h_real_to_string_class->AllocObject(self)));
441 if (h_real_to_string_obj.Get() != nullptr) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700442 auto* cl = Runtime::Current()->GetClassLinker();
443 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod(
444 "<init>", "()V", cl->GetImagePointerSize());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700445 if (init_method == nullptr) {
446 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
447 } else {
448 JValue invoke_result;
449 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
450 nullptr);
451 if (!self->IsExceptionPending()) {
452 result->SetL(h_real_to_string_obj.Get());
453 ok = true;
454 }
455 }
456 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700457 }
458 }
459 }
460
461 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700462 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700463 }
464}
465
Andreas Gampe799681b2015-05-15 19:24:12 -0700466void UnstartedRuntime::UnstartedMathCeil(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700467 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700468 double in = shadow_frame->GetVRegDouble(arg_offset);
469 double out;
470 // Special cases:
471 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
472 // -1 < in < 0 -> out := -0.
473 if (-1.0 < in && in < 0) {
474 out = -0.0;
475 } else {
476 out = ceil(in);
477 }
478 result->SetD(out);
479}
480
Andreas Gampe799681b2015-05-15 19:24:12 -0700481void UnstartedRuntime::UnstartedObjectHashCode(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700482 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700483 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
484 result->SetI(obj->IdentityHashCode());
485}
486
Andreas Gampe799681b2015-05-15 19:24:12 -0700487void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Andreas Gampedd9d0552015-03-09 12:57:41 -0700488 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700489 double in = shadow_frame->GetVRegDouble(arg_offset);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000490 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700491}
492
Andreas Gampedd9d0552015-03-09 12:57:41 -0700493static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
Mathieu Chartier90443472015-07-16 20:32:27 -0700494 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700495 const DexFile* dex_file = dex_cache->GetDexFile();
496 if (dex_file == nullptr) {
497 return nullptr;
498 }
499
500 // Create the direct byte buffer.
501 JNIEnv* env = self->GetJniEnv();
502 DCHECK(env != nullptr);
503 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700504 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
505 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700506 DCHECK(self->IsExceptionPending());
507 return nullptr;
508 }
509
510 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700511 args[0].l = byte_buffer.get();
512
513 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
514 WellKnownClasses::com_android_dex_Dex,
515 WellKnownClasses::com_android_dex_Dex_create,
516 args));
517
518 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700519}
520
Andreas Gampe799681b2015-05-15 19:24:12 -0700521void UnstartedRuntime::UnstartedDexCacheGetDexNative(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700522 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700523 // We will create the Dex object, but the image writer will release it before creating the
524 // art file.
525 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
526 bool have_dex = false;
527 if (src != nullptr) {
528 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
529 if (dex != nullptr) {
530 have_dex = true;
531 result->SetL(dex);
532 }
533 }
534 if (!have_dex) {
535 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200536 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700537 }
538}
539
540static void UnstartedMemoryPeek(
541 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
542 int64_t address = shadow_frame->GetVRegLong(arg_offset);
543 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
544 // aborting the transaction.
545
546 switch (type) {
547 case Primitive::kPrimByte: {
548 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
549 return;
550 }
551
552 case Primitive::kPrimShort: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700553 typedef int16_t unaligned_short __attribute__ ((aligned (1)));
554 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700555 return;
556 }
557
558 case Primitive::kPrimInt: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700559 typedef int32_t unaligned_int __attribute__ ((aligned (1)));
560 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700561 return;
562 }
563
564 case Primitive::kPrimLong: {
Andreas Gampe799681b2015-05-15 19:24:12 -0700565 typedef int64_t unaligned_long __attribute__ ((aligned (1)));
566 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
Andreas Gampedd9d0552015-03-09 12:57:41 -0700567 return;
568 }
569
570 case Primitive::kPrimBoolean:
571 case Primitive::kPrimChar:
572 case Primitive::kPrimFloat:
573 case Primitive::kPrimDouble:
574 case Primitive::kPrimVoid:
575 case Primitive::kPrimNot:
576 LOG(FATAL) << "Not in the Memory API: " << type;
577 UNREACHABLE();
578 }
579 LOG(FATAL) << "Should not reach here";
580 UNREACHABLE();
581}
582
Andreas Gampe799681b2015-05-15 19:24:12 -0700583void UnstartedRuntime::UnstartedMemoryPeekByte(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700584 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700585 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
586}
587
588void UnstartedRuntime::UnstartedMemoryPeekShort(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700589 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700590 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
591}
592
593void UnstartedRuntime::UnstartedMemoryPeekInt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700594 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700595 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
596}
597
598void UnstartedRuntime::UnstartedMemoryPeekLong(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700599 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700600 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700601}
602
603static void UnstartedMemoryPeekArray(
604 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700605 SHARED_REQUIRES(Locks::mutator_lock_) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700606 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
607 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
608 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200609 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700610 return;
611 }
612 mirror::Array* array = obj->AsArray();
613
614 int offset = shadow_frame->GetVReg(arg_offset + 3);
615 int count = shadow_frame->GetVReg(arg_offset + 4);
616 if (offset < 0 || offset + count > array->GetLength()) {
617 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
618 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200619 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700620 return;
621 }
622
623 switch (type) {
624 case Primitive::kPrimByte: {
625 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
626 mirror::ByteArray* byte_array = array->AsByteArray();
627 for (int32_t i = 0; i < count; ++i, ++address) {
628 byte_array->SetWithoutChecks<true>(i + offset, *address);
629 }
630 return;
631 }
632
633 case Primitive::kPrimShort:
634 case Primitive::kPrimInt:
635 case Primitive::kPrimLong:
636 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
637 UNREACHABLE();
638
639 case Primitive::kPrimBoolean:
640 case Primitive::kPrimChar:
641 case Primitive::kPrimFloat:
642 case Primitive::kPrimDouble:
643 case Primitive::kPrimVoid:
644 case Primitive::kPrimNot:
645 LOG(FATAL) << "Not in the Memory API: " << type;
646 UNREACHABLE();
647 }
648 LOG(FATAL) << "Should not reach here";
649 UNREACHABLE();
650}
651
Andreas Gampe799681b2015-05-15 19:24:12 -0700652void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700653 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Andreas Gampe799681b2015-05-15 19:24:12 -0700654 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
Andreas Gampedd9d0552015-03-09 12:57:41 -0700655}
656
Andreas Gampef778eb22015-04-13 14:17:09 -0700657// This allows reading security.properties in an unstarted runtime and initialize Security.
Andreas Gampe799681b2015-05-15 19:24:12 -0700658void UnstartedRuntime::UnstartedSecurityGetSecurityPropertiesReader(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700659 Thread* self, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result,
660 size_t arg_offset ATTRIBUTE_UNUSED) {
Andreas Gampef778eb22015-04-13 14:17:09 -0700661 Runtime* runtime = Runtime::Current();
662 const std::vector<const DexFile*>& path = runtime->GetClassLinker()->GetBootClassPath();
663 std::string canonical(DexFile::GetDexCanonicalLocation(path[0]->GetLocation().c_str()));
664 mirror::String* string_data;
665
666 // Use a block to enclose the I/O and MemMap code so buffers are released early.
667 {
668 std::string error_msg;
669 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(canonical.c_str(), &error_msg));
670 if (zip_archive.get() == nullptr) {
671 AbortTransactionOrFail(self, "Could not open zip file %s: %s", canonical.c_str(),
672 error_msg.c_str());
673 return;
674 }
675 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find("java/security/security.properties",
676 &error_msg));
677 if (zip_entry.get() == nullptr) {
678 AbortTransactionOrFail(self, "Could not find security.properties file in %s: %s",
679 canonical.c_str(), error_msg.c_str());
680 return;
681 }
682 std::unique_ptr<MemMap> map(zip_entry->ExtractToMemMap(canonical.c_str(),
683 "java/security/security.properties",
684 &error_msg));
685 if (map.get() == nullptr) {
686 AbortTransactionOrFail(self, "Could not unzip security.properties file in %s: %s",
687 canonical.c_str(), error_msg.c_str());
688 return;
689 }
690
691 uint32_t length = zip_entry->GetUncompressedLength();
692 std::unique_ptr<char[]> tmp(new char[length + 1]);
693 memcpy(tmp.get(), map->Begin(), length);
694 tmp.get()[length] = 0; // null terminator
695
696 string_data = mirror::String::AllocFromModifiedUtf8(self, tmp.get());
697 }
698
699 if (string_data == nullptr) {
700 AbortTransactionOrFail(self, "Could not create string from file content of %s",
701 canonical.c_str());
702 return;
703 }
704
705 // Create a StringReader.
706 StackHandleScope<3> hs(self);
707 Handle<mirror::String> h_string(hs.NewHandle(string_data));
708
709 Handle<mirror::Class> h_class(hs.NewHandle(
710 runtime->GetClassLinker()->FindClass(self,
711 "Ljava/io/StringReader;",
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800712 ScopedNullHandle<mirror::ClassLoader>())));
Andreas Gampef778eb22015-04-13 14:17:09 -0700713 if (h_class.Get() == nullptr) {
714 AbortTransactionOrFail(self, "Could not find StringReader class");
715 return;
716 }
717
718 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
719 AbortTransactionOrFail(self, "Could not initialize StringReader class");
720 return;
721 }
722
723 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
724 if (h_obj.Get() == nullptr) {
725 AbortTransactionOrFail(self, "Could not allocate StringReader object");
726 return;
727 }
728
Mathieu Chartiere401d142015-04-22 13:56:20 -0700729 auto* cl = Runtime::Current()->GetClassLinker();
730 ArtMethod* constructor = h_class->FindDeclaredDirectMethod(
731 "<init>", "(Ljava/lang/String;)V", cl->GetImagePointerSize());
Andreas Gampef778eb22015-04-13 14:17:09 -0700732 if (constructor == nullptr) {
733 AbortTransactionOrFail(self, "Could not find StringReader constructor");
734 return;
735 }
736
737 uint32_t args[1];
738 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_string.Get()));
739 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
740
741 if (self->IsExceptionPending()) {
742 AbortTransactionOrFail(self, "Could not run StringReader constructor");
743 return;
744 }
745
746 result->SetL(h_obj.Get());
747}
748
Kenny Root1c9e61c2015-05-14 15:58:17 -0700749// This allows reading the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700750void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700751 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700752 jint start = shadow_frame->GetVReg(arg_offset + 1);
753 jint end = shadow_frame->GetVReg(arg_offset + 2);
754 jint index = shadow_frame->GetVReg(arg_offset + 4);
755 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
756 if (string == nullptr) {
757 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
758 return;
759 }
Kenny Root57f91e82015-05-14 15:58:17 -0700760 DCHECK_GE(start, 0);
761 DCHECK_GE(end, string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700762 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700763 Handle<mirror::CharArray> h_char_array(
764 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
Kenny Root57f91e82015-05-14 15:58:17 -0700765 DCHECK_LE(index, h_char_array->GetLength());
766 DCHECK_LE(end - start, h_char_array->GetLength() - index);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700767 string->GetChars(start, end, h_char_array, index);
768}
769
770// This allows reading chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700771void UnstartedRuntime::UnstartedStringCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700772 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700773 jint index = shadow_frame->GetVReg(arg_offset + 1);
774 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
775 if (string == nullptr) {
776 AbortTransactionOrFail(self, "String.charAt with null object");
777 return;
778 }
779 result->SetC(string->CharAt(index));
780}
781
Kenny Root57f91e82015-05-14 15:58:17 -0700782// This allows setting chars from the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700783void UnstartedRuntime::UnstartedStringSetCharAt(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700784 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700785 jint index = shadow_frame->GetVReg(arg_offset + 1);
786 jchar c = shadow_frame->GetVReg(arg_offset + 2);
787 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
788 if (string == nullptr) {
789 AbortTransactionOrFail(self, "String.setCharAt with null object");
790 return;
791 }
792 string->SetCharAt(index, c);
793}
794
Kenny Root1c9e61c2015-05-14 15:58:17 -0700795// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700796void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700797 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700798 jint offset = shadow_frame->GetVReg(arg_offset);
799 jint char_count = shadow_frame->GetVReg(arg_offset + 1);
800 DCHECK_GE(char_count, 0);
801 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700802 Handle<mirror::CharArray> h_char_array(
803 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Kenny Root1c9e61c2015-05-14 15:58:17 -0700804 Runtime* runtime = Runtime::Current();
805 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
806 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator));
807}
808
809// This allows creating the new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700810void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700811 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root57f91e82015-05-14 15:58:17 -0700812 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
813 if (to_copy == nullptr) {
814 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
815 return;
816 }
817 StackHandleScope<1> hs(self);
818 Handle<mirror::String> h_string(hs.NewHandle(to_copy));
819 Runtime* runtime = Runtime::Current();
820 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
821 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0,
822 allocator));
823}
824
Andreas Gampe799681b2015-05-15 19:24:12 -0700825void UnstartedRuntime::UnstartedStringFastSubstring(
Mathieu Chartiere401d142015-04-22 13:56:20 -0700826 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Kenny Root1c9e61c2015-05-14 15:58:17 -0700827 jint start = shadow_frame->GetVReg(arg_offset + 1);
828 jint length = shadow_frame->GetVReg(arg_offset + 2);
Kenny Root57f91e82015-05-14 15:58:17 -0700829 DCHECK_GE(start, 0);
Kenny Root1c9e61c2015-05-14 15:58:17 -0700830 DCHECK_GE(length, 0);
831 StackHandleScope<1> hs(self);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700832 Handle<mirror::String> h_string(
833 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
Kenny Root57f91e82015-05-14 15:58:17 -0700834 DCHECK_LE(start, h_string->GetLength());
835 DCHECK_LE(start + length, h_string->GetLength());
Kenny Root1c9e61c2015-05-14 15:58:17 -0700836 Runtime* runtime = Runtime::Current();
837 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
838 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator));
839}
840
Kenny Root57f91e82015-05-14 15:58:17 -0700841// This allows getting the char array for new style of String objects during compilation.
Andreas Gampe799681b2015-05-15 19:24:12 -0700842void UnstartedRuntime::UnstartedStringToCharArray(
Kenny Root57f91e82015-05-14 15:58:17 -0700843 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Mathieu Chartier90443472015-07-16 20:32:27 -0700844 SHARED_REQUIRES(Locks::mutator_lock_) {
Kenny Root57f91e82015-05-14 15:58:17 -0700845 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString();
846 if (string == nullptr) {
847 AbortTransactionOrFail(self, "String.charAt with null object");
848 return;
849 }
850 result->SetL(string->ToCharArray(self));
851}
852
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
854 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
855 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700856 int32_t length = args[1];
857 DCHECK_GE(length, 0);
858 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
859 Runtime* runtime = Runtime::Current();
860 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
861 DCHECK(array_class != nullptr);
862 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
863 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
864 array_class->GetComponentSizeShift(), allocator));
865}
866
Mathieu Chartiere401d142015-04-22 13:56:20 -0700867void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
868 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
869 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700870 result->SetL(nullptr);
871}
872
Mathieu Chartiere401d142015-04-22 13:56:20 -0700873void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
874 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
875 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700876 NthCallerVisitor visitor(self, 3);
877 visitor.WalkStack();
878 if (visitor.caller != nullptr) {
879 result->SetL(visitor.caller->GetDeclaringClass());
880 }
881}
882
Mathieu Chartiere401d142015-04-22 13:56:20 -0700883void UnstartedRuntime::UnstartedJNIMathLog(
884 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
885 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700886 JValue value;
887 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
888 result->SetD(log(value.GetD()));
889}
890
Mathieu Chartiere401d142015-04-22 13:56:20 -0700891void UnstartedRuntime::UnstartedJNIMathExp(
892 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
893 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700894 JValue value;
895 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
896 result->SetD(exp(value.GetD()));
897}
898
Mathieu Chartiere401d142015-04-22 13:56:20 -0700899void UnstartedRuntime::UnstartedJNIClassGetNameNative(
900 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
901 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700902 StackHandleScope<1> hs(self);
903 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
904}
905
Mathieu Chartiere401d142015-04-22 13:56:20 -0700906void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
907 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
908 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700909 result->SetI(args[0]);
910}
911
Mathieu Chartiere401d142015-04-22 13:56:20 -0700912void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
913 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
914 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700915 result->SetI(args[0]);
916}
917
Mathieu Chartiere401d142015-04-22 13:56:20 -0700918void UnstartedRuntime::UnstartedJNIObjectInternalClone(
919 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
920 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700921 result->SetL(receiver->Clone(self));
922}
923
Mathieu Chartiere401d142015-04-22 13:56:20 -0700924void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
925 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
926 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700927 receiver->NotifyAll(self);
928}
929
Mathieu Chartiere401d142015-04-22 13:56:20 -0700930void UnstartedRuntime::UnstartedJNIStringCompareTo(
931 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args,
932 JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700933 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
934 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700935 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700936 }
937 result->SetI(receiver->AsString()->CompareTo(rhs));
938}
939
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940void UnstartedRuntime::UnstartedJNIStringIntern(
941 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
942 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700943 result->SetL(receiver->AsString()->Intern());
944}
945
Mathieu Chartiere401d142015-04-22 13:56:20 -0700946void UnstartedRuntime::UnstartedJNIStringFastIndexOf(
947 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
948 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700949 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
950}
951
Mathieu Chartiere401d142015-04-22 13:56:20 -0700952void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
953 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
954 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700955 StackHandleScope<2> hs(self);
956 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
957 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
958 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
959}
960
Mathieu Chartiere401d142015-04-22 13:56:20 -0700961void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
962 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
963 uint32_t* args, JValue* result) {
Andreas Gampee598e042015-04-10 14:57:10 -0700964 int32_t length = static_cast<int32_t>(args[1]);
965 if (length < 0) {
966 ThrowNegativeArraySizeException(length);
967 return;
968 }
969 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
970 Runtime* runtime = Runtime::Current();
971 ClassLinker* class_linker = runtime->GetClassLinker();
972 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
Mathieu Chartier2cebb242015-04-21 16:50:40 -0700973 if (UNLIKELY(array_class == nullptr)) {
Andreas Gampee598e042015-04-10 14:57:10 -0700974 CHECK(self->IsExceptionPending());
975 return;
976 }
977 DCHECK(array_class->IsObjectArrayClass());
978 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
979 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
980 result->SetL(new_array);
981}
982
Mathieu Chartiere401d142015-04-22 13:56:20 -0700983void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
984 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
985 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700986 ScopedObjectAccessUnchecked soa(self);
987 if (Runtime::Current()->IsActiveTransaction()) {
988 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
989 } else {
990 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
991 }
992}
993
Mathieu Chartiere401d142015-04-22 13:56:20 -0700994void UnstartedRuntime::UnstartedJNISystemIdentityHashCode(
995 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
996 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700997 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
998 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
999}
1000
Mathieu Chartiere401d142015-04-22 13:56:20 -07001001void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
1002 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1003 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001004 result->SetZ(JNI_TRUE);
1005}
1006
Mathieu Chartiere401d142015-04-22 13:56:20 -07001007void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
1008 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1009 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001010 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1011 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1012 jint expectedValue = args[3];
1013 jint newValue = args[4];
1014 bool success;
1015 if (Runtime::Current()->IsActiveTransaction()) {
1016 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
1017 expectedValue, newValue);
1018 } else {
1019 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
1020 expectedValue, newValue);
1021 }
1022 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
1023}
1024
Mathieu Chartiere401d142015-04-22 13:56:20 -07001025void UnstartedRuntime::UnstartedJNIUnsafePutObject(
1026 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1027 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001028 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
1029 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
1030 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
1031 if (Runtime::Current()->IsActiveTransaction()) {
1032 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
1033 } else {
1034 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
1035 }
1036}
1037
Andreas Gampe799681b2015-05-15 19:24:12 -07001038void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001039 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1040 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001041 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1042 Primitive::Type primitive_type = component->GetPrimitiveType();
1043 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
1044}
1045
Andreas Gampe799681b2015-05-15 19:24:12 -07001046void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Mathieu Chartiere401d142015-04-22 13:56:20 -07001047 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
1048 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001049 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
1050 Primitive::Type primitive_type = component->GetPrimitiveType();
1051 result->SetI(Primitive::ComponentSize(primitive_type));
1052}
1053
Andreas Gampedd9d0552015-03-09 12:57:41 -07001054typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001055 size_t arg_size);
1056
Mathieu Chartiere401d142015-04-22 13:56:20 -07001057typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001058 uint32_t* args, JValue* result);
1059
1060static bool tables_initialized_ = false;
1061static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
1062static std::unordered_map<std::string, JNIHandler> jni_handlers_;
1063
Andreas Gampe799681b2015-05-15 19:24:12 -07001064void UnstartedRuntime::InitializeInvokeHandlers() {
1065#define UNSTARTED_DIRECT(ShortName, Sig) \
1066 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
1067#include "unstarted_runtime_list.h"
1068 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
1069#undef UNSTARTED_RUNTIME_DIRECT_LIST
1070#undef UNSTARTED_RUNTIME_JNI_LIST
1071#undef UNSTARTED_DIRECT
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001072}
1073
Andreas Gampe799681b2015-05-15 19:24:12 -07001074void UnstartedRuntime::InitializeJNIHandlers() {
1075#define UNSTARTED_JNI(ShortName, Sig) \
1076 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
1077#include "unstarted_runtime_list.h"
1078 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
1079#undef UNSTARTED_RUNTIME_DIRECT_LIST
1080#undef UNSTARTED_RUNTIME_JNI_LIST
1081#undef UNSTARTED_JNI
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001082}
1083
Andreas Gampe799681b2015-05-15 19:24:12 -07001084void UnstartedRuntime::Initialize() {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001085 CHECK(!tables_initialized_);
1086
Andreas Gampe799681b2015-05-15 19:24:12 -07001087 InitializeInvokeHandlers();
1088 InitializeJNIHandlers();
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001089
1090 tables_initialized_ = true;
1091}
1092
Andreas Gampe799681b2015-05-15 19:24:12 -07001093void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item,
1094 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001095 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1096 // problems in core libraries.
1097 CHECK(tables_initialized_);
1098
1099 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1100 const auto& iter = invoke_handlers_.find(name);
1101 if (iter != invoke_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001102 // Clear out the result in case it's not zeroed out.
1103 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001104 (*iter->second)(self, shadow_frame, result, arg_offset);
1105 } else {
1106 // Not special, continue with regular interpreter execution.
Andreas Gampe3cfa4d02015-10-06 17:04:01 -07001107 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001108 }
1109}
1110
1111// 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 -07001112void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
Andreas Gampe799681b2015-05-15 19:24:12 -07001113 uint32_t* args, JValue* result) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001114 std::string name(PrettyMethod(method));
1115 const auto& iter = jni_handlers_.find(name);
1116 if (iter != jni_handlers_.end()) {
Kenny Root57f91e82015-05-14 15:58:17 -07001117 // Clear out the result in case it's not zeroed out.
1118 result->SetL(0);
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001119 (*iter->second)(self, method, receiver, args, result);
1120 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001121 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1122 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001123 } else {
1124 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1125 "non-transactional runtime";
1126 }
1127}
1128
1129} // namespace interpreter
1130} // namespace art