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 <ctype.h> 20 #include <errno.h> 21 #include <stdlib.h> 22 23 #include <cmath> 24 #include <limits> 25 #include <locale> 26 #include <unordered_map> 27 28 #include "ScopedLocalRef.h" 29 30 #include "art_method-inl.h" 31 #include "base/casts.h" 32 #include "base/logging.h" 33 #include "base/macros.h" 34 #include "class_linker.h" 35 #include "common_throws.h" 36 #include "entrypoints/entrypoint_utils-inl.h" 37 #include "gc/reference_processor.h" 38 #include "handle_scope-inl.h" 39 #include "interpreter/interpreter_common.h" 40 #include "mirror/array-inl.h" 41 #include "mirror/class.h" 42 #include "mirror/field-inl.h" 43 #include "mirror/object-inl.h" 44 #include "mirror/object_array-inl.h" 45 #include "mirror/string-inl.h" 46 #include "nth_caller_visitor.h" 47 #include "reflection.h" 48 #include "thread.h" 49 #include "transaction.h" 50 #include "well_known_classes.h" 51 #include "zip_archive.h" 52 53 namespace art { 54 namespace interpreter { 55 56 static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) 57 __attribute__((__format__(__printf__, 2, 3))) 58 SHARED_REQUIRES(Locks::mutator_lock_); 59 60 static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) { 61 va_list args; 62 if (Runtime::Current()->IsActiveTransaction()) { 63 va_start(args, fmt); 64 AbortTransactionV(self, fmt, args); 65 va_end(args); 66 } else { 67 va_start(args, fmt); 68 std::string msg; 69 StringAppendV(&msg, fmt, args); 70 va_end(args); 71 LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg; 72 UNREACHABLE(); 73 } 74 } 75 76 // Restricted support for character upper case / lower case. Only support ASCII, where 77 // it's easy. Abort the transaction otherwise. 78 static void CharacterLowerUpper(Thread* self, 79 ShadowFrame* shadow_frame, 80 JValue* result, 81 size_t arg_offset, 82 bool to_lower_case) SHARED_REQUIRES(Locks::mutator_lock_) { 83 uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset)); 84 85 // Only ASCII (7-bit). 86 if (!isascii(int_value)) { 87 AbortTransactionOrFail(self, 88 "Only support ASCII characters for toLowerCase/toUpperCase: %u", 89 int_value); 90 return; 91 } 92 93 std::locale c_locale("C"); 94 char char_value = static_cast<char>(int_value); 95 96 if (to_lower_case) { 97 result->SetI(std::tolower(char_value, c_locale)); 98 } else { 99 result->SetI(std::toupper(char_value, c_locale)); 100 } 101 } 102 103 void UnstartedRuntime::UnstartedCharacterToLowerCase( 104 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 105 CharacterLowerUpper(self, shadow_frame, result, arg_offset, true); 106 } 107 108 void UnstartedRuntime::UnstartedCharacterToUpperCase( 109 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 110 CharacterLowerUpper(self, shadow_frame, result, arg_offset, false); 111 } 112 113 // Helper function to deal with class loading in an unstarted runtime. 114 static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className, 115 Handle<mirror::ClassLoader> class_loader, JValue* result, 116 const std::string& method_name, bool initialize_class, 117 bool abort_if_not_found) 118 SHARED_REQUIRES(Locks::mutator_lock_) { 119 CHECK(className.Get() != nullptr); 120 std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str())); 121 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 122 123 mirror::Class* found = class_linker->FindClass(self, descriptor.c_str(), class_loader); 124 if (found == nullptr && abort_if_not_found) { 125 if (!self->IsExceptionPending()) { 126 AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s", 127 method_name.c_str(), PrettyDescriptor(descriptor.c_str()).c_str()); 128 } 129 return; 130 } 131 if (found != nullptr && initialize_class) { 132 StackHandleScope<1> hs(self); 133 Handle<mirror::Class> h_class(hs.NewHandle(found)); 134 if (!class_linker->EnsureInitialized(self, h_class, true, true)) { 135 CHECK(self->IsExceptionPending()); 136 return; 137 } 138 } 139 result->SetL(found); 140 } 141 142 // Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that 143 // rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into 144 // ClassNotFoundException), so need to do the same. The only exception is if the exception is 145 // actually the transaction abort exception. This must not be wrapped, as it signals an 146 // initialization abort. 147 static void CheckExceptionGenerateClassNotFound(Thread* self) 148 SHARED_REQUIRES(Locks::mutator_lock_) { 149 if (self->IsExceptionPending()) { 150 // If it is not the transaction abort exception, wrap it. 151 std::string type(PrettyTypeOf(self->GetException())); 152 if (type != Transaction::kAbortExceptionDescriptor) { 153 self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;", 154 "ClassNotFoundException"); 155 } 156 } 157 } 158 159 static mirror::String* GetClassName(Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) 160 SHARED_REQUIRES(Locks::mutator_lock_) { 161 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); 162 if (param == nullptr) { 163 AbortTransactionOrFail(self, "Null-pointer in Class.forName."); 164 return nullptr; 165 } 166 return param->AsString(); 167 } 168 169 void UnstartedRuntime::UnstartedClassForName( 170 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 171 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); 172 if (class_name == nullptr) { 173 return; 174 } 175 StackHandleScope<1> hs(self); 176 Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); 177 UnstartedRuntimeFindClass(self, 178 h_class_name, 179 ScopedNullHandle<mirror::ClassLoader>(), 180 result, 181 "Class.forName", 182 true, 183 false); 184 CheckExceptionGenerateClassNotFound(self); 185 } 186 187 void UnstartedRuntime::UnstartedClassForNameLong( 188 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 189 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); 190 if (class_name == nullptr) { 191 return; 192 } 193 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; 194 mirror::ClassLoader* class_loader = 195 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); 196 StackHandleScope<2> hs(self); 197 Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); 198 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); 199 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.forName", 200 initialize_class, false); 201 CheckExceptionGenerateClassNotFound(self); 202 } 203 204 void UnstartedRuntime::UnstartedClassClassForName( 205 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 206 mirror::String* class_name = GetClassName(self, shadow_frame, arg_offset); 207 if (class_name == nullptr) { 208 return; 209 } 210 bool initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0; 211 mirror::ClassLoader* class_loader = 212 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset + 2)); 213 StackHandleScope<2> hs(self); 214 Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); 215 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); 216 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, "Class.classForName", 217 initialize_class, false); 218 CheckExceptionGenerateClassNotFound(self); 219 } 220 221 void UnstartedRuntime::UnstartedClassNewInstance( 222 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 223 StackHandleScope<2> hs(self); // Class, constructor, object. 224 mirror::Object* param = shadow_frame->GetVRegReference(arg_offset); 225 if (param == nullptr) { 226 AbortTransactionOrFail(self, "Null-pointer in Class.newInstance."); 227 return; 228 } 229 mirror::Class* klass = param->AsClass(); 230 Handle<mirror::Class> h_klass(hs.NewHandle(klass)); 231 232 // Check that it's not null. 233 if (h_klass.Get() == nullptr) { 234 AbortTransactionOrFail(self, "Class reference is null for newInstance"); 235 return; 236 } 237 238 // If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer). 239 if (Runtime::Current()->IsActiveTransaction()) { 240 if (h_klass.Get()->IsFinalizable()) { 241 AbortTransactionF(self, "Class for newInstance is finalizable: '%s'", 242 PrettyClass(h_klass.Get()).c_str()); 243 return; 244 } 245 } 246 247 // There are two situations in which we'll abort this run. 248 // 1) If the class isn't yet initialized and initialization fails. 249 // 2) If we can't find the default constructor. We'll postpone the exception to runtime. 250 // Note that 2) could likely be handled here, but for safety abort the transaction. 251 bool ok = false; 252 auto* cl = Runtime::Current()->GetClassLinker(); 253 if (cl->EnsureInitialized(self, h_klass, true, true)) { 254 auto* cons = h_klass->FindDeclaredDirectMethod("<init>", "()V", cl->GetImagePointerSize()); 255 if (cons != nullptr) { 256 Handle<mirror::Object> h_obj(hs.NewHandle(klass->AllocObject(self))); 257 CHECK(h_obj.Get() != nullptr); // We don't expect OOM at compile-time. 258 EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr); 259 if (!self->IsExceptionPending()) { 260 result->SetL(h_obj.Get()); 261 ok = true; 262 } 263 } else { 264 self->ThrowNewExceptionF("Ljava/lang/InternalError;", 265 "Could not find default constructor for '%s'", 266 PrettyClass(h_klass.Get()).c_str()); 267 } 268 } 269 if (!ok) { 270 AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s", 271 PrettyClass(h_klass.Get()).c_str(), 272 PrettyTypeOf(self->GetException()).c_str()); 273 } 274 } 275 276 void UnstartedRuntime::UnstartedClassGetDeclaredField( 277 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 278 // Special managed code cut-out to allow field lookup in a un-started runtime that'd fail 279 // going the reflective Dex way. 280 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); 281 mirror::String* name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); 282 ArtField* found = nullptr; 283 for (ArtField& field : klass->GetIFields()) { 284 if (name2->Equals(field.GetName())) { 285 found = &field; 286 break; 287 } 288 } 289 if (found == nullptr) { 290 for (ArtField& field : klass->GetSFields()) { 291 if (name2->Equals(field.GetName())) { 292 found = &field; 293 break; 294 } 295 } 296 } 297 if (found == nullptr) { 298 AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started " 299 " runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(), 300 PrettyDescriptor(klass).c_str()); 301 return; 302 } 303 if (Runtime::Current()->IsActiveTransaction()) { 304 result->SetL(mirror::Field::CreateFromArtField<true>(self, found, true)); 305 } else { 306 result->SetL(mirror::Field::CreateFromArtField<false>(self, found, true)); 307 } 308 } 309 310 // This is required for Enum(Set) code, as that uses reflection to inspect enum classes. 311 void UnstartedRuntime::UnstartedClassGetDeclaredMethod( 312 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 313 // Special managed code cut-out to allow method lookup in a un-started runtime. 314 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); 315 if (klass == nullptr) { 316 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual); 317 return; 318 } 319 mirror::String* name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); 320 mirror::ObjectArray<mirror::Class>* args = 321 shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>(); 322 if (Runtime::Current()->IsActiveTransaction()) { 323 result->SetL(mirror::Class::GetDeclaredMethodInternal<true>(self, klass, name, args)); 324 } else { 325 result->SetL(mirror::Class::GetDeclaredMethodInternal<false>(self, klass, name, args)); 326 } 327 } 328 329 // Special managed code cut-out to allow constructor lookup in a un-started runtime. 330 void UnstartedRuntime::UnstartedClassGetDeclaredConstructor( 331 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 332 mirror::Class* klass = shadow_frame->GetVRegReference(arg_offset)->AsClass(); 333 if (klass == nullptr) { 334 ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual); 335 return; 336 } 337 mirror::ObjectArray<mirror::Class>* args = 338 shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>(); 339 if (Runtime::Current()->IsActiveTransaction()) { 340 result->SetL(mirror::Class::GetDeclaredConstructorInternal<true>(self, klass, args)); 341 } else { 342 result->SetL(mirror::Class::GetDeclaredConstructorInternal<false>(self, klass, args)); 343 } 344 } 345 346 void UnstartedRuntime::UnstartedClassGetEnclosingClass( 347 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 348 StackHandleScope<1> hs(self); 349 Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass())); 350 if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) { 351 result->SetL(nullptr); 352 } 353 result->SetL(klass->GetDexFile().GetEnclosingClass(klass)); 354 } 355 356 void UnstartedRuntime::UnstartedClassGetInnerClassFlags( 357 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 358 StackHandleScope<1> hs(self); 359 Handle<mirror::Class> klass(hs.NewHandle( 360 reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset)))); 361 const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1); 362 result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value)); 363 } 364 365 static std::unique_ptr<MemMap> FindAndExtractEntry(const std::string& jar_file, 366 const char* entry_name, 367 size_t* size, 368 std::string* error_msg) { 369 CHECK(size != nullptr); 370 371 std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg)); 372 if (zip_archive == nullptr) { 373 return nullptr;; 374 } 375 std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg)); 376 if (zip_entry == nullptr) { 377 return nullptr; 378 } 379 std::unique_ptr<MemMap> tmp_map( 380 zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg)); 381 if (tmp_map == nullptr) { 382 return nullptr; 383 } 384 385 // OK, from here everything seems fine. 386 *size = zip_entry->GetUncompressedLength(); 387 return tmp_map; 388 } 389 390 static void GetResourceAsStream(Thread* self, 391 ShadowFrame* shadow_frame, 392 JValue* result, 393 size_t arg_offset) SHARED_REQUIRES(Locks::mutator_lock_) { 394 mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1); 395 if (resource_obj == nullptr) { 396 AbortTransactionOrFail(self, "null name for getResourceAsStream"); 397 return; 398 } 399 CHECK(resource_obj->IsString()); 400 mirror::String* resource_name = resource_obj->AsString(); 401 402 std::string resource_name_str = resource_name->ToModifiedUtf8(); 403 if (resource_name_str.empty() || resource_name_str == "/") { 404 AbortTransactionOrFail(self, 405 "Unsupported name %s for getResourceAsStream", 406 resource_name_str.c_str()); 407 return; 408 } 409 const char* resource_cstr = resource_name_str.c_str(); 410 if (resource_cstr[0] == '/') { 411 resource_cstr++; 412 } 413 414 Runtime* runtime = Runtime::Current(); 415 416 std::vector<std::string> split; 417 Split(runtime->GetBootClassPathString(), ':', &split); 418 if (split.empty()) { 419 AbortTransactionOrFail(self, 420 "Boot classpath not set or split error:: %s", 421 runtime->GetBootClassPathString().c_str()); 422 return; 423 } 424 425 std::unique_ptr<MemMap> mem_map; 426 size_t map_size; 427 std::string last_error_msg; // Only store the last message (we could concatenate). 428 429 for (const std::string& jar_file : split) { 430 mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg); 431 if (mem_map != nullptr) { 432 break; 433 } 434 } 435 436 if (mem_map == nullptr) { 437 // Didn't find it. There's a good chance this will be the same at runtime, but still 438 // conservatively abort the transaction here. 439 AbortTransactionOrFail(self, 440 "Could not find resource %s. Last error was %s.", 441 resource_name_str.c_str(), 442 last_error_msg.c_str()); 443 return; 444 } 445 446 StackHandleScope<3> hs(self); 447 448 // Create byte array for content. 449 Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size))); 450 if (h_array.Get() == nullptr) { 451 AbortTransactionOrFail(self, "Could not find/create byte array class"); 452 return; 453 } 454 // Copy in content. 455 memcpy(h_array->GetData(), mem_map->Begin(), map_size); 456 // Be proactive releasing memory. 457 mem_map.release(); 458 459 // Create a ByteArrayInputStream. 460 Handle<mirror::Class> h_class(hs.NewHandle( 461 runtime->GetClassLinker()->FindClass(self, 462 "Ljava/io/ByteArrayInputStream;", 463 ScopedNullHandle<mirror::ClassLoader>()))); 464 if (h_class.Get() == nullptr) { 465 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class"); 466 return; 467 } 468 if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) { 469 AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class"); 470 return; 471 } 472 473 Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self))); 474 if (h_obj.Get() == nullptr) { 475 AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object"); 476 return; 477 } 478 479 auto* cl = Runtime::Current()->GetClassLinker(); 480 ArtMethod* constructor = h_class->FindDeclaredDirectMethod( 481 "<init>", "([B)V", cl->GetImagePointerSize()); 482 if (constructor == nullptr) { 483 AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor"); 484 return; 485 } 486 487 uint32_t args[1]; 488 args[0] = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(h_array.Get())); 489 EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr); 490 491 if (self->IsExceptionPending()) { 492 AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor"); 493 return; 494 } 495 496 result->SetL(h_obj.Get()); 497 } 498 499 void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream( 500 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 501 { 502 mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset); 503 CHECK(this_obj != nullptr); 504 CHECK(this_obj->IsClassLoader()); 505 506 StackHandleScope<1> hs(self); 507 Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass())); 508 509 if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) != 510 this_classloader_class.Get()) { 511 AbortTransactionOrFail(self, 512 "Unsupported classloader type %s for getResourceAsStream", 513 PrettyClass(this_classloader_class.Get()).c_str()); 514 return; 515 } 516 } 517 518 GetResourceAsStream(self, shadow_frame, result, arg_offset); 519 } 520 521 void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass( 522 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 523 mirror::String* class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString(); 524 mirror::ClassLoader* class_loader = 525 down_cast<mirror::ClassLoader*>(shadow_frame->GetVRegReference(arg_offset)); 526 StackHandleScope<2> hs(self); 527 Handle<mirror::String> h_class_name(hs.NewHandle(class_name)); 528 Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader)); 529 UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result, 530 "VMClassLoader.findLoadedClass", false, false); 531 // This might have an error pending. But semantics are to just return null. 532 if (self->IsExceptionPending()) { 533 // If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound. 534 std::string type(PrettyTypeOf(self->GetException())); 535 if (type != "java.lang.InternalError") { 536 self->ClearException(); 537 } 538 } 539 } 540 541 void UnstartedRuntime::UnstartedVoidLookupType( 542 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, JValue* result, 543 size_t arg_offset ATTRIBUTE_UNUSED) { 544 result->SetL(Runtime::Current()->GetClassLinker()->FindPrimitiveClass('V')); 545 } 546 547 // Arraycopy emulation. 548 // Note: we can't use any fast copy functions, as they are not available under transaction. 549 550 template <typename T> 551 static void PrimitiveArrayCopy(Thread* self, 552 mirror::Array* src_array, int32_t src_pos, 553 mirror::Array* dst_array, int32_t dst_pos, 554 int32_t length) 555 SHARED_REQUIRES(Locks::mutator_lock_) { 556 if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) { 557 AbortTransactionOrFail(self, "Types mismatched in arraycopy: %s vs %s.", 558 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), 559 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); 560 return; 561 } 562 mirror::PrimitiveArray<T>* src = down_cast<mirror::PrimitiveArray<T>*>(src_array); 563 mirror::PrimitiveArray<T>* dst = down_cast<mirror::PrimitiveArray<T>*>(dst_array); 564 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); 565 if (copy_forward) { 566 for (int32_t i = 0; i < length; ++i) { 567 dst->Set(dst_pos + i, src->Get(src_pos + i)); 568 } 569 } else { 570 for (int32_t i = 1; i <= length; ++i) { 571 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); 572 } 573 } 574 } 575 576 void UnstartedRuntime::UnstartedSystemArraycopy( 577 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { 578 // Special case array copying without initializing System. 579 jint src_pos = shadow_frame->GetVReg(arg_offset + 1); 580 jint dst_pos = shadow_frame->GetVReg(arg_offset + 3); 581 jint length = shadow_frame->GetVReg(arg_offset + 4); 582 583 mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset); 584 mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2); 585 // Null checking. For simplicity, abort transaction. 586 if (src_obj == nullptr) { 587 AbortTransactionOrFail(self, "src is null in arraycopy."); 588 return; 589 } 590 if (dst_obj == nullptr) { 591 AbortTransactionOrFail(self, "dst is null in arraycopy."); 592 return; 593 } 594 // Test for arrayness. Throw ArrayStoreException. 595 if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) { 596 self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array"); 597 return; 598 } 599 600 mirror::Array* src_array = src_obj->AsArray(); 601 mirror::Array* dst_array = dst_obj->AsArray(); 602 603 // Bounds checking. Throw IndexOutOfBoundsException. 604 if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) || 605 UNLIKELY(src_pos > src_array->GetLength() - length) || 606 UNLIKELY(dst_pos > dst_array->GetLength() - length)) { 607 self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;", 608 "src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d", 609 src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos, 610 length); 611 return; 612 } 613 614 // Type checking. 615 mirror::Class* src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()-> 616 GetComponentType(); 617 618 if (!src_type->IsPrimitive()) { 619 // Check that the second type is not primitive. 620 mirror::Class* trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()-> 621 GetComponentType(); 622 if (trg_type->IsPrimitiveInt()) { 623 AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s", 624 PrettyDescriptor(src_array->GetClass()->GetComponentType()).c_str(), 625 PrettyDescriptor(dst_array->GetClass()->GetComponentType()).c_str()); 626 return; 627 } 628 629 mirror::ObjectArray<mirror::Object>* src = src_array->AsObjectArray<mirror::Object>(); 630 mirror::ObjectArray<mirror::Object>* dst = dst_array->AsObjectArray<mirror::Object>(); 631 if (src == dst) { 632 // Can overlap, but not have type mismatches. 633 // We cannot use ObjectArray::MemMove here, as it doesn't support transactions. 634 const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length); 635 if (copy_forward) { 636 for (int32_t i = 0; i < length; ++i) { 637 dst->Set(dst_pos + i, src->Get(src_pos + i)); 638 } 639 } else { 640 for (int32_t i = 1; i <= length; ++i) { 641 dst->Set(dst_pos + length - i, src->Get(src_pos + length - i)); 642 } 643 } 644 } else { 645 // We're being lazy here. Optimally this could be a memcpy (if component types are 646 // assignable), but the ObjectArray implementation doesn't support transactions. The 647 // checking version, however, does. 648 if (Runtime::Current()->IsActiveTransaction()) { 649 dst->AssignableCheckingMemcpy<true>( 650 dst_pos, src, src_pos, length, true /* throw_exception */); 651 } else { 652 dst->AssignableCheckingMemcpy<false>( 653 dst_pos, src, src_pos, length, true /* throw_exception */); 654 } 655 } 656 } else if (src_type->IsPrimitiveByte()) { 657 PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length); 658 } else if (src_type->IsPrimitiveChar()) { 659 PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length); 660 } else if (src_type->IsPrimitiveInt()) { 661 PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length); 662 } else { 663 AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'", 664 PrettyDescriptor(src_type).c_str()); 665 } 666 } 667 668 void UnstartedRuntime::UnstartedSystemArraycopyByte( 669 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 670 // Just forward. 671 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); 672 } 673 674 void UnstartedRuntime::UnstartedSystemArraycopyChar( 675 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 676 // Just forward. 677 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); 678 } 679 680 void UnstartedRuntime::UnstartedSystemArraycopyInt( 681 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 682 // Just forward. 683 UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset); 684 } 685 686 void UnstartedRuntime::UnstartedSystemGetSecurityManager( 687 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED, 688 JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { 689 result->SetL(nullptr); 690 } 691 692 static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES"; 693 694 static void GetSystemProperty(Thread* self, 695 ShadowFrame* shadow_frame, 696 JValue* result, 697 size_t arg_offset, 698 bool is_default_version) 699 SHARED_REQUIRES(Locks::mutator_lock_) { 700 StackHandleScope<4> hs(self); 701 Handle<mirror::String> h_key( 702 hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset)))); 703 if (h_key.Get() == nullptr) { 704 AbortTransactionOrFail(self, "getProperty key was null"); 705 return; 706 } 707 708 // This is overall inefficient, but reflecting the values here is not great, either. So 709 // for simplicity, and with the assumption that the number of getProperty calls is not 710 // too great, just iterate each time. 711 712 // Get the storage class. 713 ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); 714 Handle<mirror::Class> h_props_class(hs.NewHandle( 715 class_linker->FindClass(self, 716 "Ljava/lang/AndroidHardcodedSystemProperties;", 717 ScopedNullHandle<mirror::ClassLoader>()))); 718 if (h_props_class.Get() == nullptr) { 719 AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties"); 720 return; 721 } 722 if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) { 723 AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties"); 724 return; 725 } 726 727 // Get the storage array. 728 ArtField* static_properties = 729 h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName, 730 "[[Ljava/lang/String;"); 731 if (static_properties == nullptr) { 732 AbortTransactionOrFail(self, 733 "Could not find %s field", 734 kAndroidHardcodedSystemPropertiesFieldName); 735 return; 736 } 737 Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array( 738 hs.NewHandle(reinterpret_cast<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>*>( 739 static_properties->GetObject(h_props_class.Get())))); 740 if (h_2string_array.Get() == nullptr) { 741 AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName); 742 return; 743 } 744 745 // Iterate over it. 746 const int32_t prop_count = h_2string_array->GetLength(); 747 // Use the third handle as mutable. 748 MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array( 749 hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr)); 750 for (int32_t i = 0; i < prop_count; ++i) { 751 h_string_array.Assign(h_2string_array->Get(i)); 752 if (h_string_array.Get() == nullptr || 753 h_string_array->GetLength() != 2 || 754 h_string_array->Get(0) == nullptr) { 755 AbortTransactionOrFail(self, 756 "Unexpected content of %s", 757 kAndroidHardcodedSystemPropertiesFieldName); 758 return; 759 } 760 if (h_key->Equals(h_string_array->Get(0))) { 761 // Found a value. 762 if (h_string_array->Get(1) == nullptr && is_default_version) { 763 // Null is being delegated to the default map, and then resolved to the given default value. 764 // As there's no default map, return the given value. 765 result->SetL(shadow_frame->GetVRegReference(arg_offset + 1)); 766 } else { 767 result->SetL(h_string_array->Get(1)); 768 } 769 return; 770 } 771 } 772 773 // Key is not supported. 774 AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str()); 775 } 776 777 void UnstartedRuntime::UnstartedSystemGetProperty( 778 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 779 GetSystemProperty(self, shadow_frame, result, arg_offset, false); 780 } 781 782 void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault( 783 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 784 GetSystemProperty(self, shadow_frame, result, arg_offset, true); 785 } 786 787 void UnstartedRuntime::UnstartedThreadLocalGet( 788 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { 789 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); 790 bool ok = false; 791 if (caller == "void java.lang.FloatingDecimal.developLongDigits(int, long, long)" || 792 caller == "java.lang.String java.lang.FloatingDecimal.toJavaFormatString()") { 793 // Allocate non-threadlocal buffer. 794 result->SetL(mirror::CharArray::Alloc(self, 26)); 795 ok = true; 796 } else if (caller == 797 "java.lang.FloatingDecimal java.lang.FloatingDecimal.getThreadLocalInstance()") { 798 // Allocate new object. 799 StackHandleScope<2> hs(self); 800 Handle<mirror::Class> h_real_to_string_class(hs.NewHandle( 801 shadow_frame->GetLink()->GetMethod()->GetDeclaringClass())); 802 Handle<mirror::Object> h_real_to_string_obj(hs.NewHandle( 803 h_real_to_string_class->AllocObject(self))); 804 if (h_real_to_string_obj.Get() != nullptr) { 805 auto* cl = Runtime::Current()->GetClassLinker(); 806 ArtMethod* init_method = h_real_to_string_class->FindDirectMethod( 807 "<init>", "()V", cl->GetImagePointerSize()); 808 if (init_method == nullptr) { 809 h_real_to_string_class->DumpClass(LOG(FATAL), mirror::Class::kDumpClassFullDetail); 810 } else { 811 JValue invoke_result; 812 EnterInterpreterFromInvoke(self, init_method, h_real_to_string_obj.Get(), nullptr, 813 nullptr); 814 if (!self->IsExceptionPending()) { 815 result->SetL(h_real_to_string_obj.Get()); 816 ok = true; 817 } 818 } 819 } 820 } 821 822 if (!ok) { 823 AbortTransactionOrFail(self, "Could not create RealToString object"); 824 } 825 } 826 827 void UnstartedRuntime::UnstartedMathCeil( 828 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 829 result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset))); 830 } 831 832 void UnstartedRuntime::UnstartedMathFloor( 833 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 834 result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset))); 835 } 836 837 void UnstartedRuntime::UnstartedMathSin( 838 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 839 result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset))); 840 } 841 842 void UnstartedRuntime::UnstartedMathCos( 843 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 844 result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset))); 845 } 846 847 void UnstartedRuntime::UnstartedMathPow( 848 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 849 result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset), 850 shadow_frame->GetVRegDouble(arg_offset + 2))); 851 } 852 853 void UnstartedRuntime::UnstartedObjectHashCode( 854 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 855 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); 856 result->SetI(obj->IdentityHashCode()); 857 } 858 859 void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits( 860 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 861 double in = shadow_frame->GetVRegDouble(arg_offset); 862 result->SetJ(bit_cast<int64_t, double>(in)); 863 } 864 865 static mirror::Object* GetDexFromDexCache(Thread* self, mirror::DexCache* dex_cache) 866 SHARED_REQUIRES(Locks::mutator_lock_) { 867 const DexFile* dex_file = dex_cache->GetDexFile(); 868 if (dex_file == nullptr) { 869 return nullptr; 870 } 871 872 // Create the direct byte buffer. 873 JNIEnv* env = self->GetJniEnv(); 874 DCHECK(env != nullptr); 875 void* address = const_cast<void*>(reinterpret_cast<const void*>(dex_file->Begin())); 876 ScopedLocalRef<jobject> byte_buffer(env, env->NewDirectByteBuffer(address, dex_file->Size())); 877 if (byte_buffer.get() == nullptr) { 878 DCHECK(self->IsExceptionPending()); 879 return nullptr; 880 } 881 882 jvalue args[1]; 883 args[0].l = byte_buffer.get(); 884 885 ScopedLocalRef<jobject> dex(env, env->CallStaticObjectMethodA( 886 WellKnownClasses::com_android_dex_Dex, 887 WellKnownClasses::com_android_dex_Dex_create, 888 args)); 889 890 return self->DecodeJObject(dex.get()); 891 } 892 893 void UnstartedRuntime::UnstartedDexCacheGetDexNative( 894 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 895 // We will create the Dex object, but the image writer will release it before creating the 896 // art file. 897 mirror::Object* src = shadow_frame->GetVRegReference(arg_offset); 898 bool have_dex = false; 899 if (src != nullptr) { 900 mirror::Object* dex = GetDexFromDexCache(self, reinterpret_cast<mirror::DexCache*>(src)); 901 if (dex != nullptr) { 902 have_dex = true; 903 result->SetL(dex); 904 } 905 } 906 if (!have_dex) { 907 self->ClearException(); 908 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Could not create Dex object"); 909 } 910 } 911 912 static void UnstartedMemoryPeek( 913 Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 914 int64_t address = shadow_frame->GetVRegLong(arg_offset); 915 // TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of 916 // aborting the transaction. 917 918 switch (type) { 919 case Primitive::kPrimByte: { 920 result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address))); 921 return; 922 } 923 924 case Primitive::kPrimShort: { 925 typedef int16_t unaligned_short __attribute__ ((aligned (1))); 926 result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address))); 927 return; 928 } 929 930 case Primitive::kPrimInt: { 931 typedef int32_t unaligned_int __attribute__ ((aligned (1))); 932 result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address))); 933 return; 934 } 935 936 case Primitive::kPrimLong: { 937 typedef int64_t unaligned_long __attribute__ ((aligned (1))); 938 result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address))); 939 return; 940 } 941 942 case Primitive::kPrimBoolean: 943 case Primitive::kPrimChar: 944 case Primitive::kPrimFloat: 945 case Primitive::kPrimDouble: 946 case Primitive::kPrimVoid: 947 case Primitive::kPrimNot: 948 LOG(FATAL) << "Not in the Memory API: " << type; 949 UNREACHABLE(); 950 } 951 LOG(FATAL) << "Should not reach here"; 952 UNREACHABLE(); 953 } 954 955 void UnstartedRuntime::UnstartedMemoryPeekByte( 956 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 957 UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset); 958 } 959 960 void UnstartedRuntime::UnstartedMemoryPeekShort( 961 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 962 UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset); 963 } 964 965 void UnstartedRuntime::UnstartedMemoryPeekInt( 966 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 967 UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset); 968 } 969 970 void UnstartedRuntime::UnstartedMemoryPeekLong( 971 Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 972 UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset); 973 } 974 975 static void UnstartedMemoryPeekArray( 976 Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset) 977 SHARED_REQUIRES(Locks::mutator_lock_) { 978 int64_t address_long = shadow_frame->GetVRegLong(arg_offset); 979 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2); 980 if (obj == nullptr) { 981 Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray"); 982 return; 983 } 984 mirror::Array* array = obj->AsArray(); 985 986 int offset = shadow_frame->GetVReg(arg_offset + 3); 987 int count = shadow_frame->GetVReg(arg_offset + 4); 988 if (offset < 0 || offset + count > array->GetLength()) { 989 std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d", 990 offset, count, array->GetLength())); 991 Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str()); 992 return; 993 } 994 995 switch (type) { 996 case Primitive::kPrimByte: { 997 int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long)); 998 mirror::ByteArray* byte_array = array->AsByteArray(); 999 for (int32_t i = 0; i < count; ++i, ++address) { 1000 byte_array->SetWithoutChecks<true>(i + offset, *address); 1001 } 1002 return; 1003 } 1004 1005 case Primitive::kPrimShort: 1006 case Primitive::kPrimInt: 1007 case Primitive::kPrimLong: 1008 LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type; 1009 UNREACHABLE(); 1010 1011 case Primitive::kPrimBoolean: 1012 case Primitive::kPrimChar: 1013 case Primitive::kPrimFloat: 1014 case Primitive::kPrimDouble: 1015 case Primitive::kPrimVoid: 1016 case Primitive::kPrimNot: 1017 LOG(FATAL) << "Not in the Memory API: " << type; 1018 UNREACHABLE(); 1019 } 1020 LOG(FATAL) << "Should not reach here"; 1021 UNREACHABLE(); 1022 } 1023 1024 void UnstartedRuntime::UnstartedMemoryPeekByteArray( 1025 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { 1026 UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset); 1027 } 1028 1029 // This allows reading the new style of String objects during compilation. 1030 void UnstartedRuntime::UnstartedStringGetCharsNoCheck( 1031 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { 1032 jint start = shadow_frame->GetVReg(arg_offset + 1); 1033 jint end = shadow_frame->GetVReg(arg_offset + 2); 1034 jint index = shadow_frame->GetVReg(arg_offset + 4); 1035 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); 1036 if (string == nullptr) { 1037 AbortTransactionOrFail(self, "String.getCharsNoCheck with null object"); 1038 return; 1039 } 1040 DCHECK_GE(start, 0); 1041 DCHECK_GE(end, string->GetLength()); 1042 StackHandleScope<1> hs(self); 1043 Handle<mirror::CharArray> h_char_array( 1044 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray())); 1045 DCHECK_LE(index, h_char_array->GetLength()); 1046 DCHECK_LE(end - start, h_char_array->GetLength() - index); 1047 string->GetChars(start, end, h_char_array, index); 1048 } 1049 1050 // This allows reading chars from the new style of String objects during compilation. 1051 void UnstartedRuntime::UnstartedStringCharAt( 1052 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1053 jint index = shadow_frame->GetVReg(arg_offset + 1); 1054 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); 1055 if (string == nullptr) { 1056 AbortTransactionOrFail(self, "String.charAt with null object"); 1057 return; 1058 } 1059 result->SetC(string->CharAt(index)); 1060 } 1061 1062 // This allows setting chars from the new style of String objects during compilation. 1063 void UnstartedRuntime::UnstartedStringSetCharAt( 1064 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) { 1065 jint index = shadow_frame->GetVReg(arg_offset + 1); 1066 jchar c = shadow_frame->GetVReg(arg_offset + 2); 1067 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); 1068 if (string == nullptr) { 1069 AbortTransactionOrFail(self, "String.setCharAt with null object"); 1070 return; 1071 } 1072 string->SetCharAt(index, c); 1073 } 1074 1075 // This allows creating the new style of String objects during compilation. 1076 void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars( 1077 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1078 jint offset = shadow_frame->GetVReg(arg_offset); 1079 jint char_count = shadow_frame->GetVReg(arg_offset + 1); 1080 DCHECK_GE(char_count, 0); 1081 StackHandleScope<1> hs(self); 1082 Handle<mirror::CharArray> h_char_array( 1083 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray())); 1084 Runtime* runtime = Runtime::Current(); 1085 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); 1086 result->SetL(mirror::String::AllocFromCharArray<true>(self, char_count, h_char_array, offset, allocator)); 1087 } 1088 1089 // This allows creating the new style of String objects during compilation. 1090 void UnstartedRuntime::UnstartedStringFactoryNewStringFromString( 1091 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1092 mirror::String* to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString(); 1093 if (to_copy == nullptr) { 1094 AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object"); 1095 return; 1096 } 1097 StackHandleScope<1> hs(self); 1098 Handle<mirror::String> h_string(hs.NewHandle(to_copy)); 1099 Runtime* runtime = Runtime::Current(); 1100 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); 1101 result->SetL(mirror::String::AllocFromString<true>(self, h_string->GetLength(), h_string, 0, 1102 allocator)); 1103 } 1104 1105 void UnstartedRuntime::UnstartedStringFastSubstring( 1106 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1107 jint start = shadow_frame->GetVReg(arg_offset + 1); 1108 jint length = shadow_frame->GetVReg(arg_offset + 2); 1109 DCHECK_GE(start, 0); 1110 DCHECK_GE(length, 0); 1111 StackHandleScope<1> hs(self); 1112 Handle<mirror::String> h_string( 1113 hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString())); 1114 DCHECK_LE(start, h_string->GetLength()); 1115 DCHECK_LE(start + length, h_string->GetLength()); 1116 Runtime* runtime = Runtime::Current(); 1117 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); 1118 result->SetL(mirror::String::AllocFromString<true>(self, length, h_string, start, allocator)); 1119 } 1120 1121 // This allows getting the char array for new style of String objects during compilation. 1122 void UnstartedRuntime::UnstartedStringToCharArray( 1123 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) 1124 SHARED_REQUIRES(Locks::mutator_lock_) { 1125 mirror::String* string = shadow_frame->GetVRegReference(arg_offset)->AsString(); 1126 if (string == nullptr) { 1127 AbortTransactionOrFail(self, "String.charAt with null object"); 1128 return; 1129 } 1130 result->SetL(string->ToCharArray(self)); 1131 } 1132 1133 // This allows statically initializing ConcurrentHashMap and SynchronousQueue. 1134 void UnstartedRuntime::UnstartedReferenceGetReferent( 1135 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1136 mirror::Reference* const ref = down_cast<mirror::Reference*>( 1137 shadow_frame->GetVRegReference(arg_offset)); 1138 if (ref == nullptr) { 1139 AbortTransactionOrFail(self, "Reference.getReferent() with null object"); 1140 return; 1141 } 1142 mirror::Object* const referent = 1143 Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref); 1144 result->SetL(referent); 1145 } 1146 1147 // This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat 1148 // conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap, 1149 // where we can predict the behavior (somewhat). 1150 // Note: this is required (instead of lazy initialization) as these classes are used in the static 1151 // initialization of other classes, so will *use* the value. 1152 void UnstartedRuntime::UnstartedRuntimeAvailableProcessors( 1153 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) { 1154 std::string caller(PrettyMethod(shadow_frame->GetLink()->GetMethod())); 1155 if (caller == "void java.util.concurrent.SynchronousQueue.<clinit>()") { 1156 // SynchronousQueue really only separates between single- and multiprocessor case. Return 1157 // 8 as a conservative upper approximation. 1158 result->SetI(8); 1159 } else if (caller == "void java.util.concurrent.ConcurrentHashMap.<clinit>()") { 1160 // ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely 1161 // a good upper bound. 1162 // TODO: Consider resetting in the zygote? 1163 result->SetI(8); 1164 } else { 1165 // Not supported. 1166 AbortTransactionOrFail(self, "Accessing availableProcessors not allowed"); 1167 } 1168 } 1169 1170 // This allows accessing ConcurrentHashMap/SynchronousQueue. 1171 1172 void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong( 1173 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1174 // Argument 0 is the Unsafe instance, skip. 1175 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); 1176 if (obj == nullptr) { 1177 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1178 return; 1179 } 1180 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); 1181 int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4); 1182 int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6); 1183 1184 // Must use non transactional mode. 1185 if (kUseReadBarrier) { 1186 // Need to make sure the reference stored in the field is a to-space one before attempting the 1187 // CAS or the CAS could fail incorrectly. 1188 mirror::HeapReference<mirror::Object>* field_addr = 1189 reinterpret_cast<mirror::HeapReference<mirror::Object>*>( 1190 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset)); 1191 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>( 1192 obj, 1193 MemberOffset(offset), 1194 field_addr); 1195 } 1196 bool success; 1197 // Check whether we're in a transaction, call accordingly. 1198 if (Runtime::Current()->IsActiveTransaction()) { 1199 success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset), 1200 expectedValue, 1201 newValue); 1202 } else { 1203 success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset), 1204 expectedValue, 1205 newValue); 1206 } 1207 result->SetZ(success ? 1 : 0); 1208 } 1209 1210 void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject( 1211 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1212 // Argument 0 is the Unsafe instance, skip. 1213 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); 1214 if (obj == nullptr) { 1215 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1216 return; 1217 } 1218 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); 1219 mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4); 1220 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 5); 1221 1222 // Must use non transactional mode. 1223 if (kUseReadBarrier) { 1224 // Need to make sure the reference stored in the field is a to-space one before attempting the 1225 // CAS or the CAS could fail incorrectly. 1226 mirror::HeapReference<mirror::Object>* field_addr = 1227 reinterpret_cast<mirror::HeapReference<mirror::Object>*>( 1228 reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset)); 1229 ReadBarrier::Barrier<mirror::Object, kWithReadBarrier, /*kAlwaysUpdateField*/true>( 1230 obj, 1231 MemberOffset(offset), 1232 field_addr); 1233 } 1234 bool success; 1235 // Check whether we're in a transaction, call accordingly. 1236 if (Runtime::Current()->IsActiveTransaction()) { 1237 success = obj->CasFieldStrongSequentiallyConsistentObject<true>(MemberOffset(offset), 1238 expected_value, 1239 newValue); 1240 } else { 1241 success = obj->CasFieldStrongSequentiallyConsistentObject<false>(MemberOffset(offset), 1242 expected_value, 1243 newValue); 1244 } 1245 result->SetZ(success ? 1 : 0); 1246 } 1247 1248 void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile( 1249 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) 1250 SHARED_REQUIRES(Locks::mutator_lock_) { 1251 // Argument 0 is the Unsafe instance, skip. 1252 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); 1253 if (obj == nullptr) { 1254 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1255 return; 1256 } 1257 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); 1258 mirror::Object* value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset)); 1259 result->SetL(value); 1260 } 1261 1262 void UnstartedRuntime::UnstartedUnsafePutObjectVolatile( 1263 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) 1264 SHARED_REQUIRES(Locks::mutator_lock_) { 1265 // Argument 0 is the Unsafe instance, skip. 1266 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); 1267 if (obj == nullptr) { 1268 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1269 return; 1270 } 1271 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); 1272 mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4); 1273 if (Runtime::Current()->IsActiveTransaction()) { 1274 obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value); 1275 } else { 1276 obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value); 1277 } 1278 } 1279 1280 void UnstartedRuntime::UnstartedUnsafePutOrderedObject( 1281 Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) 1282 SHARED_REQUIRES(Locks::mutator_lock_) { 1283 // Argument 0 is the Unsafe instance, skip. 1284 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1); 1285 if (obj == nullptr) { 1286 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1287 return; 1288 } 1289 int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2); 1290 mirror::Object* newValue = shadow_frame->GetVRegReference(arg_offset + 4); 1291 QuasiAtomic::ThreadFenceRelease(); 1292 if (Runtime::Current()->IsActiveTransaction()) { 1293 obj->SetFieldObject<true>(MemberOffset(offset), newValue); 1294 } else { 1295 obj->SetFieldObject<false>(MemberOffset(offset), newValue); 1296 } 1297 } 1298 1299 // A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead 1300 // of correctly handling the corner cases. 1301 void UnstartedRuntime::UnstartedIntegerParseInt( 1302 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) 1303 SHARED_REQUIRES(Locks::mutator_lock_) { 1304 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); 1305 if (obj == nullptr) { 1306 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime."); 1307 return; 1308 } 1309 1310 std::string string_value = obj->AsString()->ToModifiedUtf8(); 1311 if (string_value.empty()) { 1312 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime."); 1313 return; 1314 } 1315 1316 const char* c_str = string_value.c_str(); 1317 char *end; 1318 // Can we set errno to 0? Is this always a variable, and not a macro? 1319 // Worst case, we'll incorrectly fail a transaction. Seems OK. 1320 int64_t l = strtol(c_str, &end, 10); 1321 1322 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() || 1323 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) { 1324 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1325 return; 1326 } 1327 if (l == 0) { 1328 // Check whether the string wasn't exactly zero. 1329 if (string_value != "0") { 1330 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1331 return; 1332 } 1333 } else if (*end != '\0') { 1334 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1335 return; 1336 } 1337 1338 result->SetI(static_cast<int32_t>(l)); 1339 } 1340 1341 // A cutout for Long.parseLong. 1342 // 1343 // Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported 1344 // well. 1345 void UnstartedRuntime::UnstartedLongParseLong( 1346 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) 1347 SHARED_REQUIRES(Locks::mutator_lock_) { 1348 mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset); 1349 if (obj == nullptr) { 1350 AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime."); 1351 return; 1352 } 1353 1354 std::string string_value = obj->AsString()->ToModifiedUtf8(); 1355 if (string_value.empty()) { 1356 AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime."); 1357 return; 1358 } 1359 1360 const char* c_str = string_value.c_str(); 1361 char *end; 1362 // Can we set errno to 0? Is this always a variable, and not a macro? 1363 // Worst case, we'll incorrectly fail a transaction. Seems OK. 1364 int64_t l = strtol(c_str, &end, 10); 1365 1366 // Note: comparing against int32_t min/max is intentional here. 1367 if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() || 1368 (errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) { 1369 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1370 return; 1371 } 1372 if (l == 0) { 1373 // Check whether the string wasn't exactly zero. 1374 if (string_value != "0") { 1375 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1376 return; 1377 } 1378 } else if (*end != '\0') { 1379 AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str); 1380 return; 1381 } 1382 1383 result->SetJ(l); 1384 } 1385 1386 void UnstartedRuntime::UnstartedMethodInvoke( 1387 Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) 1388 SHARED_REQUIRES(Locks::mutator_lock_) { 1389 JNIEnvExt* env = self->GetJniEnv(); 1390 ScopedObjectAccessUnchecked soa(self); 1391 1392 mirror::Object* java_method_obj = shadow_frame->GetVRegReference(arg_offset); 1393 ScopedLocalRef<jobject> java_method(env, 1394 java_method_obj == nullptr ? nullptr :env->AddLocalReference<jobject>(java_method_obj)); 1395 1396 mirror::Object* java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1); 1397 ScopedLocalRef<jobject> java_receiver(env, 1398 java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj)); 1399 1400 mirror::Object* java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2); 1401 ScopedLocalRef<jobject> java_args(env, 1402 java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj)); 1403 1404 ScopedLocalRef<jobject> result_jobj(env, 1405 InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get())); 1406 1407 result->SetL(self->DecodeJObject(result_jobj.get())); 1408 1409 // Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap 1410 // InvocationTargetExceptions. 1411 if (self->IsExceptionPending()) { 1412 AbortTransactionOrFail(self, "Failed Method.invoke"); 1413 } 1414 } 1415 1416 1417 void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray( 1418 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1419 uint32_t* args, JValue* result) { 1420 int32_t length = args[1]; 1421 DCHECK_GE(length, 0); 1422 mirror::Class* element_class = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); 1423 Runtime* runtime = Runtime::Current(); 1424 mirror::Class* array_class = runtime->GetClassLinker()->FindArrayClass(self, &element_class); 1425 DCHECK(array_class != nullptr); 1426 gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator(); 1427 result->SetL(mirror::Array::Alloc<true, true>(self, array_class, length, 1428 array_class->GetComponentSizeShift(), allocator)); 1429 } 1430 1431 void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader( 1432 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1433 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1434 result->SetL(nullptr); 1435 } 1436 1437 void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2( 1438 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1439 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1440 NthCallerVisitor visitor(self, 3); 1441 visitor.WalkStack(); 1442 if (visitor.caller != nullptr) { 1443 result->SetL(visitor.caller->GetDeclaringClass()); 1444 } 1445 } 1446 1447 void UnstartedRuntime::UnstartedJNIMathLog( 1448 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1449 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1450 JValue value; 1451 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); 1452 result->SetD(log(value.GetD())); 1453 } 1454 1455 void UnstartedRuntime::UnstartedJNIMathExp( 1456 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1457 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1458 JValue value; 1459 value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]); 1460 result->SetD(exp(value.GetD())); 1461 } 1462 1463 void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8( 1464 Thread* self ATTRIBUTE_UNUSED, 1465 ArtMethod* method ATTRIBUTE_UNUSED, 1466 mirror::Object* receiver ATTRIBUTE_UNUSED, 1467 uint32_t* args ATTRIBUTE_UNUSED, 1468 JValue* result) { 1469 result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet()) 1470 ? 0 1471 : 1); 1472 } 1473 1474 void UnstartedRuntime::UnstartedJNIClassGetNameNative( 1475 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, 1476 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1477 StackHandleScope<1> hs(self); 1478 result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass()))); 1479 } 1480 1481 void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble( 1482 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1483 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1484 uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32); 1485 result->SetD(bit_cast<double>(long_input)); 1486 } 1487 1488 void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits( 1489 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1490 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1491 result->SetI(args[0]); 1492 } 1493 1494 void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat( 1495 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1496 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1497 result->SetI(args[0]); 1498 } 1499 1500 void UnstartedRuntime::UnstartedJNIObjectInternalClone( 1501 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, 1502 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1503 result->SetL(receiver->Clone(self)); 1504 } 1505 1506 void UnstartedRuntime::UnstartedJNIObjectNotifyAll( 1507 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, 1508 uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) { 1509 receiver->NotifyAll(self); 1510 } 1511 1512 void UnstartedRuntime::UnstartedJNIStringCompareTo( 1513 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, uint32_t* args, 1514 JValue* result) { 1515 mirror::String* rhs = reinterpret_cast<mirror::Object*>(args[0])->AsString(); 1516 if (rhs == nullptr) { 1517 AbortTransactionOrFail(self, "String.compareTo with null object"); 1518 } 1519 result->SetI(receiver->AsString()->CompareTo(rhs)); 1520 } 1521 1522 void UnstartedRuntime::UnstartedJNIStringIntern( 1523 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, 1524 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1525 result->SetL(receiver->AsString()->Intern()); 1526 } 1527 1528 void UnstartedRuntime::UnstartedJNIStringFastIndexOf( 1529 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver, 1530 uint32_t* args, JValue* result) { 1531 result->SetI(receiver->AsString()->FastIndexOf(args[0], args[1])); 1532 } 1533 1534 void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray( 1535 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1536 uint32_t* args, JValue* result) { 1537 StackHandleScope<2> hs(self); 1538 auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass())); 1539 auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray())); 1540 result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions)); 1541 } 1542 1543 void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray( 1544 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1545 uint32_t* args, JValue* result) { 1546 int32_t length = static_cast<int32_t>(args[1]); 1547 if (length < 0) { 1548 ThrowNegativeArraySizeException(length); 1549 return; 1550 } 1551 mirror::Class* element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass(); 1552 Runtime* runtime = Runtime::Current(); 1553 ClassLinker* class_linker = runtime->GetClassLinker(); 1554 mirror::Class* array_class = class_linker->FindArrayClass(self, &element_class); 1555 if (UNLIKELY(array_class == nullptr)) { 1556 CHECK(self->IsExceptionPending()); 1557 return; 1558 } 1559 DCHECK(array_class->IsObjectArrayClass()); 1560 mirror::Array* new_array = mirror::ObjectArray<mirror::Object*>::Alloc( 1561 self, array_class, length, runtime->GetHeap()->GetCurrentAllocator()); 1562 result->SetL(new_array); 1563 } 1564 1565 void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace( 1566 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1567 uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1568 ScopedObjectAccessUnchecked soa(self); 1569 if (Runtime::Current()->IsActiveTransaction()) { 1570 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<true>(soa))); 1571 } else { 1572 result->SetL(soa.Decode<mirror::Object*>(self->CreateInternalStackTrace<false>(soa))); 1573 } 1574 } 1575 1576 void UnstartedRuntime::UnstartedJNISystemIdentityHashCode( 1577 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1578 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1579 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); 1580 result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0); 1581 } 1582 1583 void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian( 1584 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1585 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) { 1586 result->SetZ(JNI_TRUE); 1587 } 1588 1589 void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt( 1590 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1591 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1592 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); 1593 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; 1594 jint expectedValue = args[3]; 1595 jint newValue = args[4]; 1596 bool success; 1597 if (Runtime::Current()->IsActiveTransaction()) { 1598 success = obj->CasFieldStrongSequentiallyConsistent32<true>(MemberOffset(offset), 1599 expectedValue, newValue); 1600 } else { 1601 success = obj->CasFieldStrongSequentiallyConsistent32<false>(MemberOffset(offset), 1602 expectedValue, newValue); 1603 } 1604 result->SetZ(success ? JNI_TRUE : JNI_FALSE); 1605 } 1606 1607 void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile( 1608 Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED, 1609 uint32_t* args, JValue* result) { 1610 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); 1611 if (obj == nullptr) { 1612 AbortTransactionOrFail(self, "Cannot access null object, retry at runtime."); 1613 return; 1614 } 1615 1616 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; 1617 result->SetI(obj->GetField32Volatile(MemberOffset(offset))); 1618 } 1619 1620 void UnstartedRuntime::UnstartedJNIUnsafePutObject( 1621 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1622 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result ATTRIBUTE_UNUSED) { 1623 mirror::Object* obj = reinterpret_cast<mirror::Object*>(args[0]); 1624 jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1]; 1625 mirror::Object* newValue = reinterpret_cast<mirror::Object*>(args[3]); 1626 if (Runtime::Current()->IsActiveTransaction()) { 1627 obj->SetFieldObject<true>(MemberOffset(offset), newValue); 1628 } else { 1629 obj->SetFieldObject<false>(MemberOffset(offset), newValue); 1630 } 1631 } 1632 1633 void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType( 1634 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1635 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1636 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); 1637 Primitive::Type primitive_type = component->GetPrimitiveType(); 1638 result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value()); 1639 } 1640 1641 void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType( 1642 Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, 1643 mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) { 1644 mirror::Class* component = reinterpret_cast<mirror::Object*>(args[0])->AsClass(); 1645 Primitive::Type primitive_type = component->GetPrimitiveType(); 1646 result->SetI(Primitive::ComponentSize(primitive_type)); 1647 } 1648 1649 typedef void (*InvokeHandler)(Thread* self, ShadowFrame* shadow_frame, JValue* result, 1650 size_t arg_size); 1651 1652 typedef void (*JNIHandler)(Thread* self, ArtMethod* method, mirror::Object* receiver, 1653 uint32_t* args, JValue* result); 1654 1655 static bool tables_initialized_ = false; 1656 static std::unordered_map<std::string, InvokeHandler> invoke_handlers_; 1657 static std::unordered_map<std::string, JNIHandler> jni_handlers_; 1658 1659 void UnstartedRuntime::InitializeInvokeHandlers() { 1660 #define UNSTARTED_DIRECT(ShortName, Sig) \ 1661 invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName)); 1662 #include "unstarted_runtime_list.h" 1663 UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT) 1664 #undef UNSTARTED_RUNTIME_DIRECT_LIST 1665 #undef UNSTARTED_RUNTIME_JNI_LIST 1666 #undef UNSTARTED_DIRECT 1667 } 1668 1669 void UnstartedRuntime::InitializeJNIHandlers() { 1670 #define UNSTARTED_JNI(ShortName, Sig) \ 1671 jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName)); 1672 #include "unstarted_runtime_list.h" 1673 UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI) 1674 #undef UNSTARTED_RUNTIME_DIRECT_LIST 1675 #undef UNSTARTED_RUNTIME_JNI_LIST 1676 #undef UNSTARTED_JNI 1677 } 1678 1679 void UnstartedRuntime::Initialize() { 1680 CHECK(!tables_initialized_); 1681 1682 InitializeInvokeHandlers(); 1683 InitializeJNIHandlers(); 1684 1685 tables_initialized_ = true; 1686 } 1687 1688 void UnstartedRuntime::Invoke(Thread* self, const DexFile::CodeItem* code_item, 1689 ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) { 1690 // In a runtime that's not started we intercept certain methods to avoid complicated dependency 1691 // problems in core libraries. 1692 CHECK(tables_initialized_); 1693 1694 std::string name(PrettyMethod(shadow_frame->GetMethod())); 1695 const auto& iter = invoke_handlers_.find(name); 1696 if (iter != invoke_handlers_.end()) { 1697 // Clear out the result in case it's not zeroed out. 1698 result->SetL(0); 1699 1700 // Push the shadow frame. This is so the failing method can be seen in abort dumps. 1701 self->PushShadowFrame(shadow_frame); 1702 1703 (*iter->second)(self, shadow_frame, result, arg_offset); 1704 1705 self->PopShadowFrame(); 1706 } else { 1707 // Not special, continue with regular interpreter execution. 1708 ArtInterpreterToInterpreterBridge(self, code_item, shadow_frame, result); 1709 } 1710 } 1711 1712 // Hand select a number of methods to be run in a not yet started runtime without using JNI. 1713 void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver, 1714 uint32_t* args, JValue* result) { 1715 std::string name(PrettyMethod(method)); 1716 const auto& iter = jni_handlers_.find(name); 1717 if (iter != jni_handlers_.end()) { 1718 // Clear out the result in case it's not zeroed out. 1719 result->SetL(0); 1720 (*iter->second)(self, method, receiver, args, result); 1721 } else if (Runtime::Current()->IsActiveTransaction()) { 1722 AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s", 1723 name.c_str()); 1724 } else { 1725 LOG(FATAL) << "Calling native method " << PrettyMethod(method) << " in an unstarted " 1726 "non-transactional runtime"; 1727 } 1728 } 1729 1730 } // namespace interpreter 1731 } // namespace art 1732