blob: 4fb634b668bd9c6390d35359e501430677d7f012 [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"
42
43namespace art {
44namespace interpreter {
45
Andreas Gampe068b0c02015-03-11 12:44:47 -070046static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
Sebastien Hertz45b15972015-04-03 16:07:05 +020047 __attribute__((__format__(__printf__, 2, 3)))
48 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
49
50static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070051 va_list args;
Andreas Gampe068b0c02015-03-11 12:44:47 -070052 if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +020053 va_start(args, fmt);
54 AbortTransactionV(self, fmt, args);
Andreas Gampe068b0c02015-03-11 12:44:47 -070055 va_end(args);
56 } else {
Sebastien Hertz45b15972015-04-03 16:07:05 +020057 va_start(args, fmt);
58 std::string msg;
59 StringAppendV(&msg, fmt, args);
60 va_end(args);
61 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
Andreas Gampe068b0c02015-03-11 12:44:47 -070062 UNREACHABLE();
63 }
64}
65
Andreas Gampe2969bcd2015-03-09 12:57:41 -070066// Helper function to deal with class loading in an unstarted runtime.
67static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
68 Handle<mirror::ClassLoader> class_loader, JValue* result,
69 const std::string& method_name, bool initialize_class,
70 bool abort_if_not_found)
71 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
72 CHECK(className.Get() != nullptr);
73 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
74 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
75
76 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
77 if (found == nullptr && abort_if_not_found) {
78 if (!self->IsExceptionPending()) {
Andreas Gampe068b0c02015-03-11 12:44:47 -070079 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
80 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -070081 }
82 return;
83 }
84 if (found != nullptr && initialize_class) {
85 StackHandleScope<1> hs(self);
86 Handle<mirror::Class> h_class(hs.NewHandle(found));
87 if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
88 CHECK(self->IsExceptionPending());
89 return;
90 }
91 }
92 result->SetL(found);
93}
94
95// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
96// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
97// ClassNotFoundException), so need to do the same. The only exception is if the exception is
Sebastien Hertz2fd7e692015-04-02 11:11:19 +020098// actually the transaction abort exception. This must not be wrapped, as it signals an
99// initialization abort.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700100static void CheckExceptionGenerateClassNotFound(Thread* self)
101 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
102 if (self->IsExceptionPending()) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200103 // If it is not the transaction abort exception, wrap it.
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700104 std::string type(PrettyTypeOf(self->GetException()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200105 if (type != Transaction::kAbortExceptionDescriptor) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700106 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
107 "ClassNotFoundException");
108 }
109 }
110}
111
Andreas Gampedd9d0552015-03-09 12:57:41 -0700112static void UnstartedClassForName(
113 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700114 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
115 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
116 StackHandleScope<1> hs(self);
117 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
118 UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result,
119 "Class.forName", true, false);
120 CheckExceptionGenerateClassNotFound(self);
121}
122
Andreas Gampedd9d0552015-03-09 12:57:41 -0700123static void UnstartedClassForNameLong(
124 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_) {
126 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
127 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
128 mirror::ClassLoader* class_loader =
129 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
130 StackHandleScope<2> hs(self);
131 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
132 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
133 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName",
134 initialize_class, false);
135 CheckExceptionGenerateClassNotFound(self);
136}
137
Andreas Gampedd9d0552015-03-09 12:57:41 -0700138static void UnstartedClassClassForName(
139 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700140 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
141 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString();
142 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
143 mirror::ClassLoader* class_loader =
144 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2));
145 StackHandleScope<2> hs(self);
146 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
147 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
148 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName",
149 initialize_class, false);
150 CheckExceptionGenerateClassNotFound(self);
151}
152
Andreas Gampedd9d0552015-03-09 12:57:41 -0700153static void UnstartedClassNewInstance(
154 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700155 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
156 StackHandleScope<3> hs(self); // Class, constructor, object.
157 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
158 Handle<mirror::Class> h_klass(hs.NewHandle(klass));
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700159
160 // Check that it's not null.
161 if (h_klass.Get() == nullptr) {
162 AbortTransactionOrFail(self, "Class reference is null for newInstance");
163 return;
164 }
165
166 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
167 if (Runtime::Current()->IsActiveTransaction()) {
168 if (h_klass.Get()->IsFinalizable()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +0200169 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
170 PrettyClass(h_klass.Get()).c_str());
Andreas Gampe0f7e3d62015-03-11 13:24:35 -0700171 return;
172 }
173 }
174
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700175 // There are two situations in which we'll abort this run.
176 // 1) If the class isn't yet initialized and initialization fails.
177 // 2) If we can't find the default constructor. We'll postpone the exception to runtime.
178 // Note that 2) could likely be handled here, but for safety abort the transaction.
179 bool ok = false;
180 if (Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) {
181 Handle<mirror::ArtMethod> h_cons(hs.NewHandle(
182 h_klass->FindDeclaredDirectMethod("<init>", "()V")));
183 if (h_cons.Get() != nullptr) {
184 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self)));
185 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time.
186 EnterInterpreterFromInvoke(self, h_cons.Get(), h_obj.Get(), nullptr, nullptr);
187 if (!self->IsExceptionPending()) {
188 result->SetL(h_obj.Get());
189 ok = true;
190 }
191 } else {
192 self->ThrowNewExceptionF("Ljava/lang/InternalError;",
193 "Could not find default constructor for '%s'",
194 PrettyClass(h_klass.Get()).c_str());
195 }
196 }
197 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700198 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
199 PrettyClass(h_klass.Get()).c_str(),
200 PrettyTypeOf(self->GetException()).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700201 }
202}
203
Andreas Gampedd9d0552015-03-09 12:57:41 -0700204static void UnstartedClassGetDeclaredField(
205 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700206 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
207 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
208 // going the reflective Dex way.
209 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
210 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700211 ArtField* found = nullptr;
212 ArtField* fields = klass->GetIFields();
213 for (int32_t i = 0, count = klass->NumInstanceFields(); i < count; ++i) {
214 ArtField* f = &fields[i];
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700215 if (name2->Equals(f->GetName())) {
216 found = f;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700217 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700218 }
219 }
220 if (found == nullptr) {
221 fields = klass->GetSFields();
Mathieu Chartierc7853442015-03-27 14:35:38 -0700222 for (int32_t i = 0, count = klass->NumStaticFields(); i < count; ++i) {
223 ArtField* f = &fields[i];
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700224 if (name2->Equals(f->GetName())) {
225 found = f;
Mathieu Chartierc7853442015-03-27 14:35:38 -0700226 break;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700227 }
228 }
229 }
Andreas Gampe068b0c02015-03-11 12:44:47 -0700230 if (found == nullptr) {
231 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
232 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
233 PrettyDescriptor(klass).c_str());
234 return;
235 }
Mathieu Chartierdaaf3262015-03-24 13:30:28 -0700236 if (Runtime::Current()->IsActiveTransaction()) {
237 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true));
238 } else {
239 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true));
240 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700241}
242
Andreas Gampedd9d0552015-03-09 12:57:41 -0700243static void UnstartedVmClassLoaderFindLoadedClass(
244 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700245 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
246 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
247 mirror::ClassLoader* class_loader =
248 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset));
249 StackHandleScope<2> hs(self);
250 Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
251 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
252 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
253 "VMClassLoader.findLoadedClass", false, false);
254 // This might have an error pending. But semantics are to just return null.
255 if (self->IsExceptionPending()) {
256 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
257 std::string type(PrettyTypeOf(self->GetException()));
258 if (type != "java.lang.InternalError") {
259 self->ClearException();
260 }
261 }
262}
263
264static void UnstartedVoidLookupType(Thread* self ATTRIBUTE_UNUSED,
265 ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
266 JValue* result,
267 size_t arg_offset ATTRIBUTE_UNUSED)
268 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
269 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V'));
270}
271
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700272// Arraycopy emulation.
273// Note: we can't use any fast copy functions, as they are not available under transaction.
274
275template <typename T>
276static void PrimitiveArrayCopy(Thread* self,
277 mirror::Array* src_array, int32_t src_pos,
278 mirror::Array* dst_array, int32_t dst_pos,
279 int32_t length)
280 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
281 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
282 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.",
283 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
284 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
285 return;
286 }
287 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array);
288 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array);
289 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
290 if (copy_forward) {
291 for (int32_t i = 0; i < length; ++i) {
292 dst->Set(dst_pos + i, src->Get(src_pos + i));
293 }
294 } else {
295 for (int32_t i = 1; i <= length; ++i) {
296 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
297 }
298 }
299}
300
Andreas Gampedd9d0552015-03-09 12:57:41 -0700301static void UnstartedSystemArraycopy(
302 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700303 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
304 // Special case array copying without initializing System.
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700305 jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
306 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700307 jint length = shadow_frame->GetVReg(arg_offset + 4);
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700308 mirror::Array* src_array = shadow_frame->GetVRegReference(arg_offset)->AsArray();
309 mirror::Array* dst_array = shadow_frame->GetVRegReference(arg_offset + 2)->AsArray();
310
311 // Null checking.
312 if (src_array == nullptr) {
313 AbortTransactionOrFail(self, "src is null in arraycopy.");
314 return;
315 }
316 if (dst_array == nullptr) {
317 AbortTransactionOrFail(self, "dst is null in arraycopy.");
318 return;
319 }
320
321 // Bounds checking.
322 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
323 UNLIKELY(src_pos > src_array->GetLength() - length) ||
324 UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
325 self->ThrowNewExceptionF("Ljava/lang/ArrayIndexOutOfBoundsException;",
326 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
327 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
328 length);
329 AbortTransactionOrFail(self, "Index out of bounds.");
330 return;
331 }
332
333 // Type checking.
334 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
335 GetComponentType();
336
337 if (!src_type->IsPrimitive()) {
338 // Check that the second type is not primitive.
339 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
340 GetComponentType();
341 if (trg_type->IsPrimitiveInt()) {
342 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
343 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
344 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
345 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700346 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700347
348 // For simplicity only do this if the component types are the same. Otherwise we have to copy
349 // even more code from the object-array functions.
350 if (src_type != trg_type) {
351 AbortTransactionOrFail(self, "Types not the same in arraycopy: %s vs %s",
352 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(),
353 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str());
354 return;
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700355 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700356
357 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>();
358 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>();
359 if (src == dst) {
360 // Can overlap, but not have type mismatches.
361 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
362 if (copy_forward) {
363 for (int32_t i = 0; i < length; ++i) {
364 dst->Set(dst_pos + i, src->Get(src_pos + i));
365 }
366 } else {
367 for (int32_t i = 1; i <= length; ++i) {
368 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
369 }
370 }
371 } else {
372 // Can't overlap. Would need type checks, but we abort above.
373 for (int32_t i = 0; i < length; ++i) {
374 dst->Set(dst_pos + i, src->Get(src_pos + i));
375 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700376 }
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700377 } else if (src_type->IsPrimitiveChar()) {
378 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
379 } else if (src_type->IsPrimitiveInt()) {
380 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700381 } else {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700382 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
Andreas Gampe8e6c3fd2015-03-11 18:34:44 -0700383 PrettyDescriptor(src_type).c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700384 }
385}
386
Andreas Gampedd9d0552015-03-09 12:57:41 -0700387static void UnstartedThreadLocalGet(
388 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700389 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
390 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod()));
391 bool ok = false;
392 if (caller == "java.lang.String java.lang.IntegralToString.convertInt"
393 "(java.lang.AbstractStringBuilder, int)") {
394 // Allocate non-threadlocal buffer.
395 result->SetL(mirror::CharArray::Alloc(self, 11));
396 ok = true;
397 } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") {
398 // Note: RealToString is implemented and used in a different fashion than IntegralToString.
399 // Conversion is done over an actual object of RealToString (the conversion method is an
400 // instance method). This means it is not as clear whether it is correct to return a new
401 // object each time. The caller needs to be inspected by hand to see whether it (incorrectly)
402 // stores the object for later use.
403 // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString.
404 if (shadow_frame->GetLink()->GetLink() != nullptr) {
405 std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod()));
406 if (caller2 == "java.lang.String java.lang.Double.toString(double)") {
407 // Allocate new object.
408 StackHandleScope<2> hs(self);
409 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle(
410 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass()));
411 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle(
412 h_real_to_string_class->AllocObject(self)));
413 if (h_real_to_string_obj.Get() != nullptr) {
414 mirror::ArtMethod* init_method =
415 h_real_to_string_class->FindDirectMethod("<init>", "()V");
416 if (init_method == nullptr) {
417 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail);
418 } else {
419 JValue invoke_result;
420 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr,
421 nullptr);
422 if (!self->IsExceptionPending()) {
423 result->SetL(h_real_to_string_obj.Get());
424 ok = true;
425 }
426 }
427 }
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700428 }
429 }
430 }
431
432 if (!ok) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700433 AbortTransactionOrFail(self, "Could not create RealToString object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700434 }
435}
436
Andreas Gampedd9d0552015-03-09 12:57:41 -0700437static void UnstartedMathCeil(
438 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700439 double in = shadow_frame->GetVRegDouble(arg_offset);
440 double out;
441 // Special cases:
442 // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath.
443 // -1 < in < 0 -> out := -0.
444 if (-1.0 < in && in < 0) {
445 out = -0.0;
446 } else {
447 out = ceil(in);
448 }
449 result->SetD(out);
450}
451
Andreas Gampedd9d0552015-03-09 12:57:41 -0700452static void UnstartedArtMethodGetMethodName(
453 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700454 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
455 mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod();
456 result->SetL(method->GetNameAsString(self));
457}
458
Andreas Gampedd9d0552015-03-09 12:57:41 -0700459static void UnstartedObjectHashCode(
460 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700461 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
462 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
463 result->SetI(obj->IdentityHashCode());
464}
465
Andreas Gampedd9d0552015-03-09 12:57:41 -0700466static void UnstartedDoubleDoubleToRawLongBits(
467 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);
Roland Levillainda4d79b2015-03-24 14:36:11 +0000469 result->SetJ(bit_cast<int64_t, double>(in));
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700470}
471
Andreas Gampedd9d0552015-03-09 12:57:41 -0700472static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache)
473 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
474 const DexFile* dex_file = dex_cache->GetDexFile();
475 if (dex_file == nullptr) {
476 return nullptr;
477 }
478
479 // Create the direct byte buffer.
480 JNIEnv* env = self->GetJniEnv();
481 DCHECK(env != nullptr);
482 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin()));
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700483 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size()));
484 if (byte_buffer.get() == nullptr) {
Andreas Gampedd9d0552015-03-09 12:57:41 -0700485 DCHECK(self->IsExceptionPending());
486 return nullptr;
487 }
488
489 jvalue args[1];
Andreas Gampeaacc25d2015-04-01 14:49:06 -0700490 args[0].l = byte_buffer.get();
491
492 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA(
493 WellKnownClasses::com_android_dex_Dex,
494 WellKnownClasses::com_android_dex_Dex_create,
495 args));
496
497 return self->DecodeJObject(dex.get());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700498}
499
500static void UnstartedDexCacheGetDexNative(
501 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
502 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
503 // We will create the Dex object, but the image writer will release it before creating the
504 // art file.
505 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset);
506 bool have_dex = false;
507 if (src != nullptr) {
508 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src));
509 if (dex != nullptr) {
510 have_dex = true;
511 result->SetL(dex);
512 }
513 }
514 if (!have_dex) {
515 self->ClearException();
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200516 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700517 }
518}
519
520static void UnstartedMemoryPeek(
521 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
522 int64_t address = shadow_frame->GetVRegLong(arg_offset);
523 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
524 // aborting the transaction.
525
526 switch (type) {
527 case Primitive::kPrimByte: {
528 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
529 return;
530 }
531
532 case Primitive::kPrimShort: {
533 result->SetS(*reinterpret_cast<int16_t*>(static_cast<intptr_t>(address)));
534 return;
535 }
536
537 case Primitive::kPrimInt: {
538 result->SetI(*reinterpret_cast<int32_t*>(static_cast<intptr_t>(address)));
539 return;
540 }
541
542 case Primitive::kPrimLong: {
543 result->SetJ(*reinterpret_cast<int64_t*>(static_cast<intptr_t>(address)));
544 return;
545 }
546
547 case Primitive::kPrimBoolean:
548 case Primitive::kPrimChar:
549 case Primitive::kPrimFloat:
550 case Primitive::kPrimDouble:
551 case Primitive::kPrimVoid:
552 case Primitive::kPrimNot:
553 LOG(FATAL) << "Not in the Memory API: " << type;
554 UNREACHABLE();
555 }
556 LOG(FATAL) << "Should not reach here";
557 UNREACHABLE();
558}
559
560static void UnstartedMemoryPeekEntry(
561 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
562 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
563 std::string name(PrettyMethod(shadow_frame->GetMethod()));
564 if (name == "byte libcore.io.Memory.peekByte(long)") {
565 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
566 } else if (name == "short libcore.io.Memory.peekShortNative(long)") {
567 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
568 } else if (name == "int libcore.io.Memory.peekIntNative(long)") {
569 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
570 } else if (name == "long libcore.io.Memory.peekLongNative(long)") {
571 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
572 } else {
573 LOG(FATAL) << "Unsupported Memory.peek entry: " << name;
574 UNREACHABLE();
575 }
576}
577
578static void UnstartedMemoryPeekArray(
579 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
580 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
581 int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
582 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
583 if (obj == nullptr) {
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200584 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
Andreas Gampedd9d0552015-03-09 12:57:41 -0700585 return;
586 }
587 mirror::Array* array = obj->AsArray();
588
589 int offset = shadow_frame->GetVReg(arg_offset + 3);
590 int count = shadow_frame->GetVReg(arg_offset + 4);
591 if (offset < 0 || offset + count > array->GetLength()) {
592 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
593 offset, count, array->GetLength()));
Sebastien Hertz2fd7e692015-04-02 11:11:19 +0200594 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
Andreas Gampedd9d0552015-03-09 12:57:41 -0700595 return;
596 }
597
598 switch (type) {
599 case Primitive::kPrimByte: {
600 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
601 mirror::ByteArray* byte_array = array->AsByteArray();
602 for (int32_t i = 0; i < count; ++i, ++address) {
603 byte_array->SetWithoutChecks<true>(i + offset, *address);
604 }
605 return;
606 }
607
608 case Primitive::kPrimShort:
609 case Primitive::kPrimInt:
610 case Primitive::kPrimLong:
611 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
612 UNREACHABLE();
613
614 case Primitive::kPrimBoolean:
615 case Primitive::kPrimChar:
616 case Primitive::kPrimFloat:
617 case Primitive::kPrimDouble:
618 case Primitive::kPrimVoid:
619 case Primitive::kPrimNot:
620 LOG(FATAL) << "Not in the Memory API: " << type;
621 UNREACHABLE();
622 }
623 LOG(FATAL) << "Should not reach here";
624 UNREACHABLE();
625}
626
627static void UnstartedMemoryPeekArrayEntry(
628 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
629 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
630 std::string name(PrettyMethod(shadow_frame->GetMethod()));
631 if (name == "void libcore.io.Memory.peekByteArray(long, byte[], int, int)") {
632 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
633 } else {
634 LOG(FATAL) << "Unsupported Memory.peekArray entry: " << name;
635 UNREACHABLE();
636 }
637}
638
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700639static void UnstartedJNIVMRuntimeNewUnpaddedArray(Thread* self,
640 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
641 mirror::Object* receiver ATTRIBUTE_UNUSED,
642 uint32_t* args,
643 JValue* result)
644 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
645 int32_t length = args[1];
646 DCHECK_GE(length, 0);
647 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
648 Runtime* runtime = Runtime::Current();
649 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class);
650 DCHECK(array_class != nullptr);
651 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
652 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length,
653 array_class->GetComponentSizeShift(), allocator));
654}
655
656static void UnstartedJNIVMStackGetCallingClassLoader(Thread* self ATTRIBUTE_UNUSED,
657 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
658 mirror::Object* receiver ATTRIBUTE_UNUSED,
659 uint32_t* args ATTRIBUTE_UNUSED,
660 JValue* result) {
661 result->SetL(nullptr);
662}
663
664static void UnstartedJNIVMStackGetStackClass2(Thread* self,
665 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
666 mirror::Object* receiver ATTRIBUTE_UNUSED,
667 uint32_t* args ATTRIBUTE_UNUSED,
668 JValue* result)
669 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
670 NthCallerVisitor visitor(self, 3);
671 visitor.WalkStack();
672 if (visitor.caller != nullptr) {
673 result->SetL(visitor.caller->GetDeclaringClass());
674 }
675}
676
677static void UnstartedJNIMathLog(Thread* self ATTRIBUTE_UNUSED,
678 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
679 mirror::Object* receiver ATTRIBUTE_UNUSED,
680 uint32_t* args,
681 JValue* result) {
682 JValue value;
683 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
684 result->SetD(log(value.GetD()));
685}
686
687static void UnstartedJNIMathExp(Thread* self ATTRIBUTE_UNUSED,
688 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
689 mirror::Object* receiver ATTRIBUTE_UNUSED,
690 uint32_t* args,
691 JValue* result) {
692 JValue value;
693 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
694 result->SetD(exp(value.GetD()));
695}
696
697static void UnstartedJNIClassGetNameNative(Thread* self,
698 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
699 mirror::Object* receiver,
700 uint32_t* args ATTRIBUTE_UNUSED,
701 JValue* result)
702 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
703 StackHandleScope<1> hs(self);
704 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
705}
706
707static void UnstartedJNIFloatFloatToRawIntBits(Thread* self ATTRIBUTE_UNUSED,
708 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
709 mirror::Object* receiver ATTRIBUTE_UNUSED,
710 uint32_t* args,
711 JValue* result) {
712 result->SetI(args[0]);
713}
714
715static void UnstartedJNIFloatIntBitsToFloat(Thread* self ATTRIBUTE_UNUSED,
716 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
717 mirror::Object* receiver ATTRIBUTE_UNUSED,
718 uint32_t* args,
719 JValue* result) {
720 result->SetI(args[0]);
721}
722
Andreas Gampeca714582015-04-03 19:41:34 -0700723static void UnstartedJNIObjectInternalClone(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700724 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
725 mirror::Object* receiver,
726 uint32_t* args ATTRIBUTE_UNUSED,
727 JValue* result)
728 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
729 result->SetL(receiver->Clone(self));
730}
731
Andreas Gampeca714582015-04-03 19:41:34 -0700732static void UnstartedJNIObjectNotifyAll(Thread* self,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700733 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
734 mirror::Object* receiver,
735 uint32_t* args ATTRIBUTE_UNUSED,
736 JValue* result ATTRIBUTE_UNUSED)
737 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
738 receiver->NotifyAll(self);
739}
740
741static void UnstartedJNIStringCompareTo(Thread* self,
742 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
743 mirror::Object* receiver,
744 uint32_t* args,
745 JValue* result)
746 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
747 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString();
748 if (rhs == nullptr) {
Andreas Gampe068b0c02015-03-11 12:44:47 -0700749 AbortTransactionOrFail(self, "String.compareTo with null object");
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700750 }
751 result->SetI(receiver->AsString()->CompareTo(rhs));
752}
753
754static void UnstartedJNIStringIntern(Thread* self ATTRIBUTE_UNUSED,
755 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
756 mirror::Object* receiver,
757 uint32_t* args ATTRIBUTE_UNUSED,
758 JValue* result)
759 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
760 result->SetL(receiver->AsString()->Intern());
761}
762
763static void UnstartedJNIStringFastIndexOf(Thread* self ATTRIBUTE_UNUSED,
764 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
765 mirror::Object* receiver,
766 uint32_t* args,
767 JValue* result)
768 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
769 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1]));
770}
771
772static void UnstartedJNIArrayCreateMultiArray(Thread* self,
773 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
774 mirror::Object* receiver ATTRIBUTE_UNUSED,
775 uint32_t* args,
776 JValue* result)
777 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
778 StackHandleScope<2> hs(self);
779 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
780 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
781 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
782}
783
Andreas Gampee598e042015-04-10 14:57:10 -0700784static void UnstartedJNIArrayCreateObjectArray(Thread* self,
785 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
786 mirror::Object* receiver ATTRIBUTE_UNUSED,
787 uint32_t* args,
788 JValue* result)
789 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
790 int32_t length = static_cast<int32_t>(args[1]);
791 if (length < 0) {
792 ThrowNegativeArraySizeException(length);
793 return;
794 }
795 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
796 Runtime* runtime = Runtime::Current();
797 ClassLinker* class_linker = runtime->GetClassLinker();
798 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class);
799 if (UNLIKELY(array_class == NULL)) {
800 CHECK(self->IsExceptionPending());
801 return;
802 }
803 DCHECK(array_class->IsObjectArrayClass());
804 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc(
805 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
806 result->SetL(new_array);
807}
808
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700809static void UnstartedJNIThrowableNativeFillInStackTrace(Thread* self,
810 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
811 mirror::Object* receiver ATTRIBUTE_UNUSED,
812 uint32_t* args ATTRIBUTE_UNUSED,
813 JValue* result)
814 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
815 ScopedObjectAccessUnchecked soa(self);
816 if (Runtime::Current()->IsActiveTransaction()) {
817 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa)));
818 } else {
819 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa)));
820 }
821}
822
823static void UnstartedJNISystemIdentityHashCode(Thread* self ATTRIBUTE_UNUSED,
824 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
825 mirror::Object* receiver ATTRIBUTE_UNUSED,
826 uint32_t* args,
827 JValue* result)
828 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
829 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
830 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
831}
832
833static void UnstartedJNIByteOrderIsLittleEndian(Thread* self ATTRIBUTE_UNUSED,
834 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
835 mirror::Object* receiver ATTRIBUTE_UNUSED,
836 uint32_t* args ATTRIBUTE_UNUSED,
837 JValue* result) {
838 result->SetZ(JNI_TRUE);
839}
840
841static void UnstartedJNIUnsafeCompareAndSwapInt(Thread* self ATTRIBUTE_UNUSED,
842 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
843 mirror::Object* receiver ATTRIBUTE_UNUSED,
844 uint32_t* args,
845 JValue* result)
846 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
847 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
848 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
849 jint expectedValue = args[3];
850 jint newValue = args[4];
851 bool success;
852 if (Runtime::Current()->IsActiveTransaction()) {
853 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset),
854 expectedValue, newValue);
855 } else {
856 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset),
857 expectedValue, newValue);
858 }
859 result->SetZ(success ? JNI_TRUE : JNI_FALSE);
860}
861
862static void UnstartedJNIUnsafePutObject(Thread* self ATTRIBUTE_UNUSED,
863 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
864 mirror::Object* receiver ATTRIBUTE_UNUSED,
865 uint32_t* args,
866 JValue* result ATTRIBUTE_UNUSED)
867 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
868 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]);
869 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
870 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]);
871 if (Runtime::Current()->IsActiveTransaction()) {
872 obj->SetFieldObject<true>(MemberOffset(offset), newValue);
873 } else {
874 obj->SetFieldObject<false>(MemberOffset(offset), newValue);
875 }
876}
877
878static void UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
879 Thread* self ATTRIBUTE_UNUSED,
880 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
881 mirror::Object* receiver ATTRIBUTE_UNUSED,
882 uint32_t* args,
883 JValue* result)
884 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
885 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
886 Primitive::Type primitive_type = component->GetPrimitiveType();
887 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
888}
889
890static void UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
891 Thread* self ATTRIBUTE_UNUSED,
892 mirror::ArtMethod* method ATTRIBUTE_UNUSED,
893 mirror::Object* receiver ATTRIBUTE_UNUSED,
894 uint32_t* args,
895 JValue* result)
896 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
897 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass();
898 Primitive::Type primitive_type = component->GetPrimitiveType();
899 result->SetI(Primitive::ComponentSize(primitive_type));
900}
901
Andreas Gampedd9d0552015-03-09 12:57:41 -0700902typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700903 size_t arg_size);
904
Andreas Gampedd9d0552015-03-09 12:57:41 -0700905typedef void (*JNIHandler)(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver,
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700906 uint32_t* args, JValue* result);
907
908static bool tables_initialized_ = false;
909static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
910static std::unordered_map<std::string, JNIHandler> jni_handlers_;
911
912static void UnstartedRuntimeInitializeInvokeHandlers() {
913 struct InvokeHandlerDef {
914 std::string name;
915 InvokeHandler function;
916 };
917
918 InvokeHandlerDef defs[] {
919 { "java.lang.Class java.lang.Class.forName(java.lang.String)",
920 &UnstartedClassForName },
921 { "java.lang.Class java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader)",
922 &UnstartedClassForNameLong },
923 { "java.lang.Class java.lang.Class.classForName(java.lang.String, boolean, java.lang.ClassLoader)",
924 &UnstartedClassClassForName },
925 { "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)",
926 &UnstartedVmClassLoaderFindLoadedClass },
927 { "java.lang.Class java.lang.Void.lookupType()",
928 &UnstartedVoidLookupType },
929 { "java.lang.Object java.lang.Class.newInstance()",
930 &UnstartedClassNewInstance },
931 { "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)",
932 &UnstartedClassGetDeclaredField },
933 { "int java.lang.Object.hashCode()",
934 &UnstartedObjectHashCode },
935 { "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)",
936 &UnstartedArtMethodGetMethodName },
937 { "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)",
938 &UnstartedSystemArraycopy},
939 { "void java.lang.System.arraycopy(char[], int, char[], int, int)",
940 &UnstartedSystemArraycopy },
941 { "void java.lang.System.arraycopy(int[], int, int[], int, int)",
942 &UnstartedSystemArraycopy },
943 { "long java.lang.Double.doubleToRawLongBits(double)",
944 &UnstartedDoubleDoubleToRawLongBits },
945 { "double java.lang.Math.ceil(double)",
946 &UnstartedMathCeil },
947 { "java.lang.Object java.lang.ThreadLocal.get()",
948 &UnstartedThreadLocalGet },
Andreas Gampedd9d0552015-03-09 12:57:41 -0700949 { "com.android.dex.Dex java.lang.DexCache.getDexNative()",
950 &UnstartedDexCacheGetDexNative },
951 { "byte libcore.io.Memory.peekByte(long)",
952 &UnstartedMemoryPeekEntry },
953 { "short libcore.io.Memory.peekShortNative(long)",
954 &UnstartedMemoryPeekEntry },
955 { "int libcore.io.Memory.peekIntNative(long)",
956 &UnstartedMemoryPeekEntry },
957 { "long libcore.io.Memory.peekLongNative(long)",
958 &UnstartedMemoryPeekEntry },
959 { "void libcore.io.Memory.peekByteArray(long, byte[], int, int)",
960 &UnstartedMemoryPeekArrayEntry },
Andreas Gampe2969bcd2015-03-09 12:57:41 -0700961 };
962
963 for (auto& def : defs) {
964 invoke_handlers_.insert(std::make_pair(def.name, def.function));
965 }
966}
967
968static void UnstartedRuntimeInitializeJNIHandlers() {
969 struct JNIHandlerDef {
970 std::string name;
971 JNIHandler function;
972 };
973
974 JNIHandlerDef defs[] {
975 { "java.lang.Object dalvik.system.VMRuntime.newUnpaddedArray(java.lang.Class, int)",
976 &UnstartedJNIVMRuntimeNewUnpaddedArray },
977 { "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()",
978 &UnstartedJNIVMStackGetCallingClassLoader },
979 { "java.lang.Class dalvik.system.VMStack.getStackClass2()",
980 &UnstartedJNIVMStackGetStackClass2 },
981 { "double java.lang.Math.log(double)",
982 &UnstartedJNIMathLog },
983 { "java.lang.String java.lang.Class.getNameNative()",
984 &UnstartedJNIClassGetNameNative },
985 { "int java.lang.Float.floatToRawIntBits(float)",
986 &UnstartedJNIFloatFloatToRawIntBits },
987 { "float java.lang.Float.intBitsToFloat(int)",
988 &UnstartedJNIFloatIntBitsToFloat },
989 { "double java.lang.Math.exp(double)",
990 &UnstartedJNIMathExp },
991 { "java.lang.Object java.lang.Object.internalClone()",
992 &UnstartedJNIObjectInternalClone },
993 { "void java.lang.Object.notifyAll()",
994 &UnstartedJNIObjectNotifyAll},
995 { "int java.lang.String.compareTo(java.lang.String)",
996 &UnstartedJNIStringCompareTo },
997 { "java.lang.String java.lang.String.intern()",
998 &UnstartedJNIStringIntern },
999 { "int java.lang.String.fastIndexOf(int, int)",
1000 &UnstartedJNIStringFastIndexOf },
1001 { "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])",
1002 &UnstartedJNIArrayCreateMultiArray },
Andreas Gampee598e042015-04-10 14:57:10 -07001003 { "java.lang.Object java.lang.reflect.Array.createObjectArray(java.lang.Class, int)",
1004 &UnstartedJNIArrayCreateObjectArray },
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001005 { "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()",
1006 &UnstartedJNIThrowableNativeFillInStackTrace },
1007 { "int java.lang.System.identityHashCode(java.lang.Object)",
1008 &UnstartedJNISystemIdentityHashCode },
1009 { "boolean java.nio.ByteOrder.isLittleEndian()",
1010 &UnstartedJNIByteOrderIsLittleEndian },
1011 { "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)",
1012 &UnstartedJNIUnsafeCompareAndSwapInt },
1013 { "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)",
1014 &UnstartedJNIUnsafePutObject },
1015 { "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)",
1016 &UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType },
1017 { "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)",
1018 &UnstartedJNIUnsafeGetArrayIndexScaleForComponentType },
1019 };
1020
1021 for (auto& def : defs) {
1022 jni_handlers_.insert(std::make_pair(def.name, def.function));
1023 }
1024}
1025
1026void UnstartedRuntimeInitialize() {
1027 CHECK(!tables_initialized_);
1028
1029 UnstartedRuntimeInitializeInvokeHandlers();
1030 UnstartedRuntimeInitializeJNIHandlers();
1031
1032 tables_initialized_ = true;
1033}
1034
1035void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item,
1036 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
1037 // In a runtime that's not started we intercept certain methods to avoid complicated dependency
1038 // problems in core libraries.
1039 CHECK(tables_initialized_);
1040
1041 std::string name(PrettyMethod(shadow_frame->GetMethod()));
1042 const auto& iter = invoke_handlers_.find(name);
1043 if (iter != invoke_handlers_.end()) {
1044 (*iter->second)(self, shadow_frame, result, arg_offset);
1045 } else {
1046 // Not special, continue with regular interpreter execution.
1047 artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result);
1048 }
1049}
1050
1051// Hand select a number of methods to be run in a not yet started runtime without using JNI.
1052void UnstartedRuntimeJni(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver,
1053 uint32_t* args, JValue* result) {
1054 std::string name(PrettyMethod(method));
1055 const auto& iter = jni_handlers_.find(name);
1056 if (iter != jni_handlers_.end()) {
1057 (*iter->second)(self, method, receiver, args, result);
1058 } else if (Runtime::Current()->IsActiveTransaction()) {
Sebastien Hertz45b15972015-04-03 16:07:05 +02001059 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
1060 name.c_str());
Andreas Gampe2969bcd2015-03-09 12:57:41 -07001061 } else {
1062 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted "
1063 "non-transactional runtime";
1064 }
1065}
1066
1067} // namespace interpreter
1068} // namespace art