Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | #include "base/logging.h" |
| 23 | #include "base/macros.h" |
| 24 | #include "class_linker.h" |
| 25 | #include "common_throws.h" |
| 26 | #include "entrypoints/entrypoint_utils-inl.h" |
| 27 | #include "handle_scope-inl.h" |
| 28 | #include "interpreter/interpreter_common.h" |
| 29 | #include "mirror/array-inl.h" |
| 30 | #include "mirror/art_method-inl.h" |
| 31 | #include "mirror/class.h" |
| 32 | #include "mirror/object-inl.h" |
| 33 | #include "mirror/object_array-inl.h" |
| 34 | #include "mirror/string-inl.h" |
| 35 | #include "nth_caller_visitor.h" |
| 36 | #include "thread.h" |
| 37 | #include "well_known_classes.h" |
| 38 | |
| 39 | namespace art { |
| 40 | namespace interpreter { |
| 41 | |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 42 | static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) |
| 43 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 44 | va_list args; |
| 45 | va_start(args, fmt); |
| 46 | if (Runtime::Current()->IsActiveTransaction()) { |
| 47 | AbortTransaction(self, fmt, args); |
| 48 | va_end(args); |
| 49 | } else { |
| 50 | LOG(FATAL) << "Trying to abort, but not in transaction mode: " << StringPrintf(fmt, args); |
| 51 | UNREACHABLE(); |
| 52 | } |
| 53 | } |
| 54 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 55 | // Helper function to deal with class loading in an unstarted runtime. |
| 56 | static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, |
| 57 | Handle<mirror::ClassLoader> class_loader, JValue* result, |
| 58 | const std::string& method_name, bool initialize_class, |
| 59 | bool abort_if_not_found) |
| 60 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 61 | CHECK(className.Get() != nullptr); |
| 62 | std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); |
| 63 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 64 | |
| 65 | mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); |
| 66 | if (found == nullptr && abort_if_not_found) { |
| 67 | if (!self->IsExceptionPending()) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 68 | AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s", |
| 69 | method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 70 | } |
| 71 | return; |
| 72 | } |
| 73 | if (found != nullptr && initialize_class) { |
| 74 | StackHandleScope<1> hs(self); |
| 75 | Handle<mirror::Class> h_class(hs.NewHandle(found)); |
| 76 | if (!class_linker->EnsureInitialized(self, h_class, true, true)) { |
| 77 | CHECK(self->IsExceptionPending()); |
| 78 | return; |
| 79 | } |
| 80 | } |
| 81 | result->SetL(found); |
| 82 | } |
| 83 | |
| 84 | // Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that |
| 85 | // rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into |
| 86 | // ClassNotFoundException), so need to do the same. The only exception is if the exception is |
| 87 | // actually InternalError. This must not be wrapped, as it signals an initialization abort. |
| 88 | static void CheckExceptionGenerateClassNotFound(Thread* self) |
| 89 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 90 | if (self->IsExceptionPending()) { |
| 91 | // If it is not an InternalError, wrap it. |
| 92 | std::string type(PrettyTypeOf(self->GetException())); |
| 93 | if (type != "java.lang.InternalError") { |
| 94 | self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", |
| 95 | "ClassNotFoundException"); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 100 | static void UnstartedClassForName( |
| 101 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 102 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 103 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 104 | StackHandleScope<1> hs(self); |
| 105 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 106 | UnstartedRuntimeFindClass(self, h_class_name, NullHandle<mirror::ClassLoader>(), result, |
| 107 | "Class.forName", true, false); |
| 108 | CheckExceptionGenerateClassNotFound(self); |
| 109 | } |
| 110 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 111 | static void UnstartedClassForNameLong( |
| 112 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 113 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 114 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 115 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 116 | mirror::ClassLoader* class_loader = |
| 117 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 118 | StackHandleScope<2> hs(self); |
| 119 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 120 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 121 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName", |
| 122 | initialize_class, false); |
| 123 | CheckExceptionGenerateClassNotFound(self); |
| 124 | } |
| 125 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 126 | static void UnstartedClassClassForName( |
| 127 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 128 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 129 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset)->AsString(); |
| 130 | bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; |
| 131 | mirror::ClassLoader* class_loader = |
| 132 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); |
| 133 | StackHandleScope<2> hs(self); |
| 134 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 135 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 136 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName", |
| 137 | initialize_class, false); |
| 138 | CheckExceptionGenerateClassNotFound(self); |
| 139 | } |
| 140 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 141 | static void UnstartedClassNewInstance( |
| 142 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 143 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 144 | StackHandleScope<3> hs(self); // Class, constructor, object. |
| 145 | mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 146 | Handle<mirror::Class> h_klass(hs.NewHandle(klass)); |
Andreas Gampe | 0f7e3d6 | 2015-03-11 13:24:35 -0700 | [diff] [blame] | 147 | |
| 148 | // Check that it's not null. |
| 149 | if (h_klass.Get() == nullptr) { |
| 150 | AbortTransactionOrFail(self, "Class reference is null for newInstance"); |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer). |
| 155 | if (Runtime::Current()->IsActiveTransaction()) { |
| 156 | if (h_klass.Get()->IsFinalizable()) { |
| 157 | AbortTransaction(self, "Class for newInstance is finalizable: '%s'", |
| 158 | PrettyClass(h_klass.Get()).c_str()); |
| 159 | return; |
| 160 | } |
| 161 | } |
| 162 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 163 | // There are two situations in which we'll abort this run. |
| 164 | // 1) If the class isn't yet initialized and initialization fails. |
| 165 | // 2) If we can't find the default constructor. We'll postpone the exception to runtime. |
| 166 | // Note that 2) could likely be handled here, but for safety abort the transaction. |
| 167 | bool ok = false; |
| 168 | if (Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_klass, true, true)) { |
| 169 | Handle<mirror::ArtMethod> h_cons(hs.NewHandle( |
| 170 | h_klass->FindDeclaredDirectMethod("<init>", "()V"))); |
| 171 | if (h_cons.Get() != nullptr) { |
| 172 | Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self))); |
| 173 | CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time. |
| 174 | EnterInterpreterFromInvoke(self, h_cons.Get(), h_obj.Get(), nullptr, nullptr); |
| 175 | if (!self->IsExceptionPending()) { |
| 176 | result->SetL(h_obj.Get()); |
| 177 | ok = true; |
| 178 | } |
| 179 | } else { |
| 180 | self->ThrowNewExceptionF("Ljava/lang/InternalError;", |
| 181 | "Could not find default constructor for '%s'", |
| 182 | PrettyClass(h_klass.Get()).c_str()); |
| 183 | } |
| 184 | } |
| 185 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 186 | AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s", |
| 187 | PrettyClass(h_klass.Get()).c_str(), |
| 188 | PrettyTypeOf(self->GetException()).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 192 | static void UnstartedClassGetDeclaredField( |
| 193 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 194 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 195 | // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail |
| 196 | // going the reflective Dex way. |
| 197 | mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); |
| 198 | mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 199 | mirror::ArtField* found = nullptr; |
| 200 | mirror::ObjectArray<mirror::ArtField>* fields = klass->GetIFields(); |
| 201 | for (int32_t i = 0; i < fields->GetLength() && found == nullptr; ++i) { |
| 202 | mirror::ArtField* f = fields->Get(i); |
| 203 | if (name2->Equals(f->GetName())) { |
| 204 | found = f; |
| 205 | } |
| 206 | } |
| 207 | if (found == nullptr) { |
| 208 | fields = klass->GetSFields(); |
| 209 | for (int32_t i = 0; i < fields->GetLength() && found == nullptr; ++i) { |
| 210 | mirror::ArtField* f = fields->Get(i); |
| 211 | if (name2->Equals(f->GetName())) { |
| 212 | found = f; |
| 213 | } |
| 214 | } |
| 215 | } |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 216 | if (found == nullptr) { |
| 217 | AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started " |
| 218 | " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(), |
| 219 | PrettyDescriptor(klass).c_str()); |
| 220 | return; |
| 221 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 222 | // TODO: getDeclaredField calls GetType once the field is found to ensure a |
| 223 | // NoClassDefFoundError is thrown if the field's type cannot be resolved. |
| 224 | mirror::Class* jlr_Field = self->DecodeJObject( |
| 225 | WellKnownClasses::java_lang_reflect_Field)->AsClass(); |
| 226 | StackHandleScope<1> hs(self); |
| 227 | Handle<mirror::Object> field(hs.NewHandle(jlr_Field->AllocNonMovableObject(self))); |
| 228 | CHECK(field.Get() != nullptr); |
| 229 | mirror::ArtMethod* c = jlr_Field->FindDeclaredDirectMethod("<init>", |
| 230 | "(Ljava/lang/reflect/ArtField;)V"); |
| 231 | uint32_t args[1]; |
| 232 | args[0] = StackReference<mirror::Object>::FromMirrorPtr(found).AsVRegValue(); |
| 233 | EnterInterpreterFromInvoke(self, c, field.Get(), args, nullptr); |
| 234 | result->SetL(field.Get()); |
| 235 | } |
| 236 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 237 | static void UnstartedVmClassLoaderFindLoadedClass( |
| 238 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 239 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 240 | mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); |
| 241 | mirror::ClassLoader* class_loader = |
| 242 | down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); |
| 243 | StackHandleScope<2> hs(self); |
| 244 | Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); |
| 245 | Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); |
| 246 | UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, |
| 247 | "VMClassLoader.findLoadedClass", false, false); |
| 248 | // This might have an error pending. But semantics are to just return null. |
| 249 | if (self->IsExceptionPending()) { |
| 250 | // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound. |
| 251 | std::string type(PrettyTypeOf(self->GetException())); |
| 252 | if (type != "java.lang.InternalError") { |
| 253 | self->ClearException(); |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | static void UnstartedVoidLookupType(Thread* self ATTRIBUTE_UNUSED, |
| 259 | ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, |
| 260 | JValue* result, |
| 261 | size_t arg_offset ATTRIBUTE_UNUSED) |
| 262 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 263 | result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); |
| 264 | } |
| 265 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 266 | static void UnstartedSystemArraycopy( |
| 267 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 268 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 269 | // Special case array copying without initializing System. |
| 270 | mirror::Class* ctype = shadow_frame->GetVRegReference(arg_offset)->GetClass()->GetComponentType(); |
| 271 | jint srcPos = shadow_frame->GetVReg(arg_offset + 1); |
| 272 | jint dstPos = shadow_frame->GetVReg(arg_offset + 3); |
| 273 | jint length = shadow_frame->GetVReg(arg_offset + 4); |
| 274 | if (!ctype->IsPrimitive()) { |
| 275 | mirror::ObjectArray<mirror::Object>* src = shadow_frame->GetVRegReference(arg_offset)-> |
| 276 | AsObjectArray<mirror::Object>(); |
| 277 | mirror::ObjectArray<mirror::Object>* dst = shadow_frame->GetVRegReference(arg_offset + 2)-> |
| 278 | AsObjectArray<mirror::Object>(); |
| 279 | for (jint i = 0; i < length; ++i) { |
| 280 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 281 | } |
| 282 | } else if (ctype->IsPrimitiveChar()) { |
| 283 | mirror::CharArray* src = shadow_frame->GetVRegReference(arg_offset)->AsCharArray(); |
| 284 | mirror::CharArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray(); |
| 285 | for (jint i = 0; i < length; ++i) { |
| 286 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 287 | } |
| 288 | } else if (ctype->IsPrimitiveInt()) { |
| 289 | mirror::IntArray* src = shadow_frame->GetVRegReference(arg_offset)->AsIntArray(); |
| 290 | mirror::IntArray* dst = shadow_frame->GetVRegReference(arg_offset + 2)->AsIntArray(); |
| 291 | for (jint i = 0; i < length; ++i) { |
| 292 | dst->Set(dstPos + i, src->Get(srcPos + i)); |
| 293 | } |
| 294 | } else { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 295 | AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'", |
| 296 | PrettyDescriptor(ctype).c_str()); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 300 | static void UnstartedThreadLocalGet( |
| 301 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 302 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 303 | std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); |
| 304 | bool ok = false; |
| 305 | if (caller == "java.lang.String java.lang.IntegralToString.convertInt" |
| 306 | "(java.lang.AbstractStringBuilder, int)") { |
| 307 | // Allocate non-threadlocal buffer. |
| 308 | result->SetL(mirror::CharArray::Alloc(self, 11)); |
| 309 | ok = true; |
| 310 | } else if (caller == "java.lang.RealToString java.lang.RealToString.getInstance()") { |
| 311 | // Note: RealToString is implemented and used in a different fashion than IntegralToString. |
| 312 | // Conversion is done over an actual object of RealToString (the conversion method is an |
| 313 | // instance method). This means it is not as clear whether it is correct to return a new |
| 314 | // object each time. The caller needs to be inspected by hand to see whether it (incorrectly) |
| 315 | // stores the object for later use. |
| 316 | // See also b/19548084 for a possible rewrite and bringing it in line with IntegralToString. |
| 317 | if (shadow_frame->GetLink()->GetLink() != nullptr) { |
| 318 | std::string caller2(PrettyMethod(shadow_frame->GetLink()->GetLink()->GetMethod())); |
| 319 | if (caller2 == "java.lang.String java.lang.Double.toString(double)") { |
| 320 | // Allocate new object. |
| 321 | StackHandleScope<2> hs(self); |
| 322 | Handle<mirror::Class> h_real_to_string_class(hs.NewHandle( |
| 323 | shadow_frame->GetLink()->GetMethod()->GetDeclaringClass())); |
| 324 | Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle( |
| 325 | h_real_to_string_class->AllocObject(self))); |
| 326 | if (h_real_to_string_obj.Get() != nullptr) { |
| 327 | mirror::ArtMethod* init_method = |
| 328 | h_real_to_string_class->FindDirectMethod("<init>", "()V"); |
| 329 | if (init_method == nullptr) { |
| 330 | h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail); |
| 331 | } else { |
| 332 | JValue invoke_result; |
| 333 | EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr, |
| 334 | nullptr); |
| 335 | if (!self->IsExceptionPending()) { |
| 336 | result->SetL(h_real_to_string_obj.Get()); |
| 337 | ok = true; |
| 338 | } |
| 339 | } |
| 340 | } |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (!ok) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 346 | AbortTransactionOrFail(self, "Could not create RealToString object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 350 | static void UnstartedMathCeil( |
| 351 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 352 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 353 | double out; |
| 354 | // Special cases: |
| 355 | // 1) NaN, infinity, +0, -0 -> out := in. All are guaranteed by cmath. |
| 356 | // -1 < in < 0 -> out := -0. |
| 357 | if (-1.0 < in && in < 0) { |
| 358 | out = -0.0; |
| 359 | } else { |
| 360 | out = ceil(in); |
| 361 | } |
| 362 | result->SetD(out); |
| 363 | } |
| 364 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 365 | static void UnstartedArtMethodGetMethodName( |
| 366 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 367 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 368 | mirror::ArtMethod* method = shadow_frame->GetVRegReference(arg_offset)->AsArtMethod(); |
| 369 | result->SetL(method->GetNameAsString(self)); |
| 370 | } |
| 371 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 372 | static void UnstartedObjectHashCode( |
| 373 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 374 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 375 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); |
| 376 | result->SetI(obj->IdentityHashCode()); |
| 377 | } |
| 378 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 379 | static void UnstartedDoubleDoubleToRawLongBits( |
| 380 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 381 | double in = shadow_frame->GetVRegDouble(arg_offset); |
| 382 | result->SetJ(bit_cast<int64_t>(in)); |
| 383 | } |
| 384 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 385 | static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) |
| 386 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 387 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 388 | if (dex_file == nullptr) { |
| 389 | return nullptr; |
| 390 | } |
| 391 | |
| 392 | // Create the direct byte buffer. |
| 393 | JNIEnv* env = self->GetJniEnv(); |
| 394 | DCHECK(env != nullptr); |
| 395 | void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin())); |
| 396 | jobject byte_buffer = env->NewDirectByteBuffer(address, dex_file->Size()); |
| 397 | if (byte_buffer == nullptr) { |
| 398 | DCHECK(self->IsExceptionPending()); |
| 399 | return nullptr; |
| 400 | } |
| 401 | |
| 402 | jvalue args[1]; |
| 403 | args[0].l = byte_buffer; |
| 404 | return self->DecodeJObject( |
| 405 | env->CallStaticObjectMethodA(WellKnownClasses::com_android_dex_Dex, |
| 406 | WellKnownClasses::com_android_dex_Dex_create, |
| 407 | args)); |
| 408 | } |
| 409 | |
| 410 | static void UnstartedDexCacheGetDexNative( |
| 411 | Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
| 412 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 413 | // We will create the Dex object, but the image writer will release it before creating the |
| 414 | // art file. |
| 415 | mirror::Object* src = shadow_frame->GetVRegReference(arg_offset); |
| 416 | bool have_dex = false; |
| 417 | if (src != nullptr) { |
| 418 | mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src)); |
| 419 | if (dex != nullptr) { |
| 420 | have_dex = true; |
| 421 | result->SetL(dex); |
| 422 | } |
| 423 | } |
| 424 | if (!have_dex) { |
| 425 | self->ClearException(); |
| 426 | Runtime::Current()->AbortTransactionAndThrowInternalError(self, "Could not create Dex object"); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static void UnstartedMemoryPeek( |
| 431 | Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 432 | int64_t address = shadow_frame->GetVRegLong(arg_offset); |
| 433 | // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of |
| 434 | // aborting the transaction. |
| 435 | |
| 436 | switch (type) { |
| 437 | case Primitive::kPrimByte: { |
| 438 | result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address))); |
| 439 | return; |
| 440 | } |
| 441 | |
| 442 | case Primitive::kPrimShort: { |
| 443 | result->SetS(*reinterpret_cast<int16_t*>(static_cast<intptr_t>(address))); |
| 444 | return; |
| 445 | } |
| 446 | |
| 447 | case Primitive::kPrimInt: { |
| 448 | result->SetI(*reinterpret_cast<int32_t*>(static_cast<intptr_t>(address))); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | case Primitive::kPrimLong: { |
| 453 | result->SetJ(*reinterpret_cast<int64_t*>(static_cast<intptr_t>(address))); |
| 454 | return; |
| 455 | } |
| 456 | |
| 457 | case Primitive::kPrimBoolean: |
| 458 | case Primitive::kPrimChar: |
| 459 | case Primitive::kPrimFloat: |
| 460 | case Primitive::kPrimDouble: |
| 461 | case Primitive::kPrimVoid: |
| 462 | case Primitive::kPrimNot: |
| 463 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 464 | UNREACHABLE(); |
| 465 | } |
| 466 | LOG(FATAL) << "Should not reach here"; |
| 467 | UNREACHABLE(); |
| 468 | } |
| 469 | |
| 470 | static void UnstartedMemoryPeekEntry( |
| 471 | Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) |
| 472 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 473 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
| 474 | if (name == "byte libcore.io.Memory.peekByte(long)") { |
| 475 | UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset); |
| 476 | } else if (name == "short libcore.io.Memory.peekShortNative(long)") { |
| 477 | UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset); |
| 478 | } else if (name == "int libcore.io.Memory.peekIntNative(long)") { |
| 479 | UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset); |
| 480 | } else if (name == "long libcore.io.Memory.peekLongNative(long)") { |
| 481 | UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset); |
| 482 | } else { |
| 483 | LOG(FATAL) << "Unsupported Memory.peek entry: " << name; |
| 484 | UNREACHABLE(); |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static void UnstartedMemoryPeekArray( |
| 489 | Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) |
| 490 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 491 | int64_t address_long = shadow_frame->GetVRegLong(arg_offset); |
| 492 | mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2); |
| 493 | if (obj == nullptr) { |
| 494 | Runtime::Current()->AbortTransactionAndThrowInternalError(self, "Null pointer in peekArray"); |
| 495 | return; |
| 496 | } |
| 497 | mirror::Array* array = obj->AsArray(); |
| 498 | |
| 499 | int offset = shadow_frame->GetVReg(arg_offset + 3); |
| 500 | int count = shadow_frame->GetVReg(arg_offset + 4); |
| 501 | if (offset < 0 || offset + count > array->GetLength()) { |
| 502 | std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d", |
| 503 | offset, count, array->GetLength())); |
| 504 | Runtime::Current()->AbortTransactionAndThrowInternalError(self, error_msg.c_str()); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | switch (type) { |
| 509 | case Primitive::kPrimByte: { |
| 510 | int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long)); |
| 511 | mirror::ByteArray* byte_array = array->AsByteArray(); |
| 512 | for (int32_t i = 0; i < count; ++i, ++address) { |
| 513 | byte_array->SetWithoutChecks<true>(i + offset, *address); |
| 514 | } |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | case Primitive::kPrimShort: |
| 519 | case Primitive::kPrimInt: |
| 520 | case Primitive::kPrimLong: |
| 521 | LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type; |
| 522 | UNREACHABLE(); |
| 523 | |
| 524 | case Primitive::kPrimBoolean: |
| 525 | case Primitive::kPrimChar: |
| 526 | case Primitive::kPrimFloat: |
| 527 | case Primitive::kPrimDouble: |
| 528 | case Primitive::kPrimVoid: |
| 529 | case Primitive::kPrimNot: |
| 530 | LOG(FATAL) << "Not in the Memory API: " << type; |
| 531 | UNREACHABLE(); |
| 532 | } |
| 533 | LOG(FATAL) << "Should not reach here"; |
| 534 | UNREACHABLE(); |
| 535 | } |
| 536 | |
| 537 | static void UnstartedMemoryPeekArrayEntry( |
| 538 | Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) |
| 539 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 540 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
| 541 | if (name == "void libcore.io.Memory.peekByteArray(long, byte[], int, int)") { |
| 542 | UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset); |
| 543 | } else { |
| 544 | LOG(FATAL) << "Unsupported Memory.peekArray entry: " << name; |
| 545 | UNREACHABLE(); |
| 546 | } |
| 547 | } |
| 548 | |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 549 | static void UnstartedJNIVMRuntimeNewUnpaddedArray(Thread* self, |
| 550 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 551 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 552 | uint32_t* args, |
| 553 | JValue* result) |
| 554 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 555 | int32_t length = args[1]; |
| 556 | DCHECK_GE(length, 0); |
| 557 | mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 558 | Runtime* runtime = Runtime::Current(); |
| 559 | mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class); |
| 560 | DCHECK(array_class != nullptr); |
| 561 | gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); |
| 562 | result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length, |
| 563 | array_class->GetComponentSizeShift(), allocator)); |
| 564 | } |
| 565 | |
| 566 | static void UnstartedJNIVMStackGetCallingClassLoader(Thread* self ATTRIBUTE_UNUSED, |
| 567 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 568 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 569 | uint32_t* args ATTRIBUTE_UNUSED, |
| 570 | JValue* result) { |
| 571 | result->SetL(nullptr); |
| 572 | } |
| 573 | |
| 574 | static void UnstartedJNIVMStackGetStackClass2(Thread* self, |
| 575 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 576 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 577 | uint32_t* args ATTRIBUTE_UNUSED, |
| 578 | JValue* result) |
| 579 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 580 | NthCallerVisitor visitor(self, 3); |
| 581 | visitor.WalkStack(); |
| 582 | if (visitor.caller != nullptr) { |
| 583 | result->SetL(visitor.caller->GetDeclaringClass()); |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | static void UnstartedJNIMathLog(Thread* self ATTRIBUTE_UNUSED, |
| 588 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 589 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 590 | uint32_t* args, |
| 591 | JValue* result) { |
| 592 | JValue value; |
| 593 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 594 | result->SetD(log(value.GetD())); |
| 595 | } |
| 596 | |
| 597 | static void UnstartedJNIMathExp(Thread* self ATTRIBUTE_UNUSED, |
| 598 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 599 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 600 | uint32_t* args, |
| 601 | JValue* result) { |
| 602 | JValue value; |
| 603 | value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); |
| 604 | result->SetD(exp(value.GetD())); |
| 605 | } |
| 606 | |
| 607 | static void UnstartedJNIClassGetNameNative(Thread* self, |
| 608 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 609 | mirror::Object* receiver, |
| 610 | uint32_t* args ATTRIBUTE_UNUSED, |
| 611 | JValue* result) |
| 612 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 613 | StackHandleScope<1> hs(self); |
| 614 | result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass()))); |
| 615 | } |
| 616 | |
| 617 | static void UnstartedJNIFloatFloatToRawIntBits(Thread* self ATTRIBUTE_UNUSED, |
| 618 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 619 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 620 | uint32_t* args, |
| 621 | JValue* result) { |
| 622 | result->SetI(args[0]); |
| 623 | } |
| 624 | |
| 625 | static void UnstartedJNIFloatIntBitsToFloat(Thread* self ATTRIBUTE_UNUSED, |
| 626 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 627 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 628 | uint32_t* args, |
| 629 | JValue* result) { |
| 630 | result->SetI(args[0]); |
| 631 | } |
| 632 | |
| 633 | static void UnstartedJNIObjectInternalClone(Thread* self ATTRIBUTE_UNUSED, |
| 634 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 635 | mirror::Object* receiver, |
| 636 | uint32_t* args ATTRIBUTE_UNUSED, |
| 637 | JValue* result) |
| 638 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 639 | result->SetL(receiver->Clone(self)); |
| 640 | } |
| 641 | |
| 642 | static void UnstartedJNIObjectNotifyAll(Thread* self ATTRIBUTE_UNUSED, |
| 643 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 644 | mirror::Object* receiver, |
| 645 | uint32_t* args ATTRIBUTE_UNUSED, |
| 646 | JValue* result ATTRIBUTE_UNUSED) |
| 647 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 648 | receiver->NotifyAll(self); |
| 649 | } |
| 650 | |
| 651 | static void UnstartedJNIStringCompareTo(Thread* self, |
| 652 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 653 | mirror::Object* receiver, |
| 654 | uint32_t* args, |
| 655 | JValue* result) |
| 656 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 657 | mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString(); |
| 658 | if (rhs == nullptr) { |
Andreas Gampe | 068b0c0 | 2015-03-11 12:44:47 -0700 | [diff] [blame] | 659 | AbortTransactionOrFail(self, "String.compareTo with null object"); |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 660 | } |
| 661 | result->SetI(receiver->AsString()->CompareTo(rhs)); |
| 662 | } |
| 663 | |
| 664 | static void UnstartedJNIStringIntern(Thread* self ATTRIBUTE_UNUSED, |
| 665 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 666 | mirror::Object* receiver, |
| 667 | uint32_t* args ATTRIBUTE_UNUSED, |
| 668 | JValue* result) |
| 669 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 670 | result->SetL(receiver->AsString()->Intern()); |
| 671 | } |
| 672 | |
| 673 | static void UnstartedJNIStringFastIndexOf(Thread* self ATTRIBUTE_UNUSED, |
| 674 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 675 | mirror::Object* receiver, |
| 676 | uint32_t* args, |
| 677 | JValue* result) |
| 678 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 679 | result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1])); |
| 680 | } |
| 681 | |
| 682 | static void UnstartedJNIArrayCreateMultiArray(Thread* self, |
| 683 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 684 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 685 | uint32_t* args, |
| 686 | JValue* result) |
| 687 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 688 | StackHandleScope<2> hs(self); |
| 689 | auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass())); |
| 690 | auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray())); |
| 691 | result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions)); |
| 692 | } |
| 693 | |
| 694 | static void UnstartedJNIThrowableNativeFillInStackTrace(Thread* self, |
| 695 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 696 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 697 | uint32_t* args ATTRIBUTE_UNUSED, |
| 698 | JValue* result) |
| 699 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 700 | ScopedObjectAccessUnchecked soa(self); |
| 701 | if (Runtime::Current()->IsActiveTransaction()) { |
| 702 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa))); |
| 703 | } else { |
| 704 | result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa))); |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | static void UnstartedJNISystemIdentityHashCode(Thread* self ATTRIBUTE_UNUSED, |
| 709 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 710 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 711 | uint32_t* args, |
| 712 | JValue* result) |
| 713 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 714 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 715 | result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0); |
| 716 | } |
| 717 | |
| 718 | static void UnstartedJNIByteOrderIsLittleEndian(Thread* self ATTRIBUTE_UNUSED, |
| 719 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 720 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 721 | uint32_t* args ATTRIBUTE_UNUSED, |
| 722 | JValue* result) { |
| 723 | result->SetZ(JNI_TRUE); |
| 724 | } |
| 725 | |
| 726 | static void UnstartedJNIUnsafeCompareAndSwapInt(Thread* self ATTRIBUTE_UNUSED, |
| 727 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 728 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 729 | uint32_t* args, |
| 730 | JValue* result) |
| 731 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 732 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 733 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 734 | jint expectedValue = args[3]; |
| 735 | jint newValue = args[4]; |
| 736 | bool success; |
| 737 | if (Runtime::Current()->IsActiveTransaction()) { |
| 738 | success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset), |
| 739 | expectedValue, newValue); |
| 740 | } else { |
| 741 | success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset), |
| 742 | expectedValue, newValue); |
| 743 | } |
| 744 | result->SetZ(success ? JNI_TRUE : JNI_FALSE); |
| 745 | } |
| 746 | |
| 747 | static void UnstartedJNIUnsafePutObject(Thread* self ATTRIBUTE_UNUSED, |
| 748 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 749 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 750 | uint32_t* args, |
| 751 | JValue* result ATTRIBUTE_UNUSED) |
| 752 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 753 | mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); |
| 754 | jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; |
| 755 | mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]); |
| 756 | if (Runtime::Current()->IsActiveTransaction()) { |
| 757 | obj->SetFieldObject<true>(MemberOffset(offset), newValue); |
| 758 | } else { |
| 759 | obj->SetFieldObject<false>(MemberOffset(offset), newValue); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | static void UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType( |
| 764 | Thread* self ATTRIBUTE_UNUSED, |
| 765 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 766 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 767 | uint32_t* args, |
| 768 | JValue* result) |
| 769 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 770 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 771 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 772 | result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value()); |
| 773 | } |
| 774 | |
| 775 | static void UnstartedJNIUnsafeGetArrayIndexScaleForComponentType( |
| 776 | Thread* self ATTRIBUTE_UNUSED, |
| 777 | mirror::ArtMethod* method ATTRIBUTE_UNUSED, |
| 778 | mirror::Object* receiver ATTRIBUTE_UNUSED, |
| 779 | uint32_t* args, |
| 780 | JValue* result) |
| 781 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 782 | mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); |
| 783 | Primitive::Type primitive_type = component->GetPrimitiveType(); |
| 784 | result->SetI(Primitive::ComponentSize(primitive_type)); |
| 785 | } |
| 786 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 787 | typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 788 | size_t arg_size); |
| 789 | |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 790 | typedef void (*JNIHandler)(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 791 | uint32_t* args, JValue* result); |
| 792 | |
| 793 | static bool tables_initialized_ = false; |
| 794 | static std::unordered_map<std::string, InvokeHandler> invoke_handlers_; |
| 795 | static std::unordered_map<std::string, JNIHandler> jni_handlers_; |
| 796 | |
| 797 | static void UnstartedRuntimeInitializeInvokeHandlers() { |
| 798 | struct InvokeHandlerDef { |
| 799 | std::string name; |
| 800 | InvokeHandler function; |
| 801 | }; |
| 802 | |
| 803 | InvokeHandlerDef defs[] { |
| 804 | { "java.lang.Class java.lang.Class.forName(java.lang.String)", |
| 805 | &UnstartedClassForName }, |
| 806 | { "java.lang.Class java.lang.Class.forName(java.lang.String, boolean, java.lang.ClassLoader)", |
| 807 | &UnstartedClassForNameLong }, |
| 808 | { "java.lang.Class java.lang.Class.classForName(java.lang.String, boolean, java.lang.ClassLoader)", |
| 809 | &UnstartedClassClassForName }, |
| 810 | { "java.lang.Class java.lang.VMClassLoader.findLoadedClass(java.lang.ClassLoader, java.lang.String)", |
| 811 | &UnstartedVmClassLoaderFindLoadedClass }, |
| 812 | { "java.lang.Class java.lang.Void.lookupType()", |
| 813 | &UnstartedVoidLookupType }, |
| 814 | { "java.lang.Object java.lang.Class.newInstance()", |
| 815 | &UnstartedClassNewInstance }, |
| 816 | { "java.lang.reflect.Field java.lang.Class.getDeclaredField(java.lang.String)", |
| 817 | &UnstartedClassGetDeclaredField }, |
| 818 | { "int java.lang.Object.hashCode()", |
| 819 | &UnstartedObjectHashCode }, |
| 820 | { "java.lang.String java.lang.reflect.ArtMethod.getMethodName(java.lang.reflect.ArtMethod)", |
| 821 | &UnstartedArtMethodGetMethodName }, |
| 822 | { "void java.lang.System.arraycopy(java.lang.Object, int, java.lang.Object, int, int)", |
| 823 | &UnstartedSystemArraycopy}, |
| 824 | { "void java.lang.System.arraycopy(char[], int, char[], int, int)", |
| 825 | &UnstartedSystemArraycopy }, |
| 826 | { "void java.lang.System.arraycopy(int[], int, int[], int, int)", |
| 827 | &UnstartedSystemArraycopy }, |
| 828 | { "long java.lang.Double.doubleToRawLongBits(double)", |
| 829 | &UnstartedDoubleDoubleToRawLongBits }, |
| 830 | { "double java.lang.Math.ceil(double)", |
| 831 | &UnstartedMathCeil }, |
| 832 | { "java.lang.Object java.lang.ThreadLocal.get()", |
| 833 | &UnstartedThreadLocalGet }, |
Andreas Gampe | dd9d055 | 2015-03-09 12:57:41 -0700 | [diff] [blame^] | 834 | { "com.android.dex.Dex java.lang.DexCache.getDexNative()", |
| 835 | &UnstartedDexCacheGetDexNative }, |
| 836 | { "byte libcore.io.Memory.peekByte(long)", |
| 837 | &UnstartedMemoryPeekEntry }, |
| 838 | { "short libcore.io.Memory.peekShortNative(long)", |
| 839 | &UnstartedMemoryPeekEntry }, |
| 840 | { "int libcore.io.Memory.peekIntNative(long)", |
| 841 | &UnstartedMemoryPeekEntry }, |
| 842 | { "long libcore.io.Memory.peekLongNative(long)", |
| 843 | &UnstartedMemoryPeekEntry }, |
| 844 | { "void libcore.io.Memory.peekByteArray(long, byte[], int, int)", |
| 845 | &UnstartedMemoryPeekArrayEntry }, |
Andreas Gampe | 2969bcd | 2015-03-09 12:57:41 -0700 | [diff] [blame] | 846 | }; |
| 847 | |
| 848 | for (auto& def : defs) { |
| 849 | invoke_handlers_.insert(std::make_pair(def.name, def.function)); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | static void UnstartedRuntimeInitializeJNIHandlers() { |
| 854 | struct JNIHandlerDef { |
| 855 | std::string name; |
| 856 | JNIHandler function; |
| 857 | }; |
| 858 | |
| 859 | JNIHandlerDef defs[] { |
| 860 | { "java.lang.Object dalvik.system.VMRuntime.newUnpaddedArray(java.lang.Class, int)", |
| 861 | &UnstartedJNIVMRuntimeNewUnpaddedArray }, |
| 862 | { "java.lang.ClassLoader dalvik.system.VMStack.getCallingClassLoader()", |
| 863 | &UnstartedJNIVMStackGetCallingClassLoader }, |
| 864 | { "java.lang.Class dalvik.system.VMStack.getStackClass2()", |
| 865 | &UnstartedJNIVMStackGetStackClass2 }, |
| 866 | { "double java.lang.Math.log(double)", |
| 867 | &UnstartedJNIMathLog }, |
| 868 | { "java.lang.String java.lang.Class.getNameNative()", |
| 869 | &UnstartedJNIClassGetNameNative }, |
| 870 | { "int java.lang.Float.floatToRawIntBits(float)", |
| 871 | &UnstartedJNIFloatFloatToRawIntBits }, |
| 872 | { "float java.lang.Float.intBitsToFloat(int)", |
| 873 | &UnstartedJNIFloatIntBitsToFloat }, |
| 874 | { "double java.lang.Math.exp(double)", |
| 875 | &UnstartedJNIMathExp }, |
| 876 | { "java.lang.Object java.lang.Object.internalClone()", |
| 877 | &UnstartedJNIObjectInternalClone }, |
| 878 | { "void java.lang.Object.notifyAll()", |
| 879 | &UnstartedJNIObjectNotifyAll}, |
| 880 | { "int java.lang.String.compareTo(java.lang.String)", |
| 881 | &UnstartedJNIStringCompareTo }, |
| 882 | { "java.lang.String java.lang.String.intern()", |
| 883 | &UnstartedJNIStringIntern }, |
| 884 | { "int java.lang.String.fastIndexOf(int, int)", |
| 885 | &UnstartedJNIStringFastIndexOf }, |
| 886 | { "java.lang.Object java.lang.reflect.Array.createMultiArray(java.lang.Class, int[])", |
| 887 | &UnstartedJNIArrayCreateMultiArray }, |
| 888 | { "java.lang.Object java.lang.Throwable.nativeFillInStackTrace()", |
| 889 | &UnstartedJNIThrowableNativeFillInStackTrace }, |
| 890 | { "int java.lang.System.identityHashCode(java.lang.Object)", |
| 891 | &UnstartedJNISystemIdentityHashCode }, |
| 892 | { "boolean java.nio.ByteOrder.isLittleEndian()", |
| 893 | &UnstartedJNIByteOrderIsLittleEndian }, |
| 894 | { "boolean sun.misc.Unsafe.compareAndSwapInt(java.lang.Object, long, int, int)", |
| 895 | &UnstartedJNIUnsafeCompareAndSwapInt }, |
| 896 | { "void sun.misc.Unsafe.putObject(java.lang.Object, long, java.lang.Object)", |
| 897 | &UnstartedJNIUnsafePutObject }, |
| 898 | { "int sun.misc.Unsafe.getArrayBaseOffsetForComponentType(java.lang.Class)", |
| 899 | &UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType }, |
| 900 | { "int sun.misc.Unsafe.getArrayIndexScaleForComponentType(java.lang.Class)", |
| 901 | &UnstartedJNIUnsafeGetArrayIndexScaleForComponentType }, |
| 902 | }; |
| 903 | |
| 904 | for (auto& def : defs) { |
| 905 | jni_handlers_.insert(std::make_pair(def.name, def.function)); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | void UnstartedRuntimeInitialize() { |
| 910 | CHECK(!tables_initialized_); |
| 911 | |
| 912 | UnstartedRuntimeInitializeInvokeHandlers(); |
| 913 | UnstartedRuntimeInitializeJNIHandlers(); |
| 914 | |
| 915 | tables_initialized_ = true; |
| 916 | } |
| 917 | |
| 918 | void UnstartedRuntimeInvoke(Thread* self, const DexFile::CodeItem* code_item, |
| 919 | ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { |
| 920 | // In a runtime that's not started we intercept certain methods to avoid complicated dependency |
| 921 | // problems in core libraries. |
| 922 | CHECK(tables_initialized_); |
| 923 | |
| 924 | std::string name(PrettyMethod(shadow_frame->GetMethod())); |
| 925 | const auto& iter = invoke_handlers_.find(name); |
| 926 | if (iter != invoke_handlers_.end()) { |
| 927 | (*iter->second)(self, shadow_frame, result, arg_offset); |
| 928 | } else { |
| 929 | // Not special, continue with regular interpreter execution. |
| 930 | artInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | // Hand select a number of methods to be run in a not yet started runtime without using JNI. |
| 935 | void UnstartedRuntimeJni(Thread* self, mirror::ArtMethod* method, mirror::Object* receiver, |
| 936 | uint32_t* args, JValue* result) { |
| 937 | std::string name(PrettyMethod(method)); |
| 938 | const auto& iter = jni_handlers_.find(name); |
| 939 | if (iter != jni_handlers_.end()) { |
| 940 | (*iter->second)(self, method, receiver, args, result); |
| 941 | } else if (Runtime::Current()->IsActiveTransaction()) { |
| 942 | AbortTransaction(self, "Attempt to invoke native method in non-started runtime: %s", |
| 943 | name.c_str()); |
| 944 | } else { |
| 945 | LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted " |
| 946 | "non-transactional runtime"; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | } // namespace interpreter |
| 951 | } // namespace art |