Reland "Clean up `StackOverflowError` construction."

This reverts commit 3e1b1f8ff913d2fb811e3fe5714552fc98000d15.

Bug: 253570082

Reason for revert: Not the culprit after all

Change-Id: I5846fb3695880ffbf373858afae688c82f7a5c92
diff --git a/compiler/optimizing/critical_native_abi_fixup_arm.cc b/compiler/optimizing/critical_native_abi_fixup_arm.cc
index 1bf3542..2373053 100644
--- a/compiler/optimizing/critical_native_abi_fixup_arm.cc
+++ b/compiler/optimizing/critical_native_abi_fixup_arm.cc
@@ -45,8 +45,9 @@
     if (DataType::IsFloatingPointType(input_type)) {
       bool is_double = (input_type == DataType::Type::kFloat64);
       DataType::Type converted_type = is_double ? DataType::Type::kInt64 : DataType::Type::kInt32;
-      ArtMethod* resolved_method = is_double ? WellKnownClasses::java_lang_Double_doubleToRawLongBits
-                                             : WellKnownClasses::java_lang_Float_floatToRawIntBits;
+      ArtMethod* resolved_method = is_double
+          ? WellKnownClasses::java_lang_Double_doubleToRawLongBits
+          : WellKnownClasses::java_lang_Float_floatToRawIntBits;
       DCHECK(resolved_method != nullptr);
       DCHECK(resolved_method->IsIntrinsic());
       MethodReference target_method(nullptr, 0);
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index ecea692..44a72c5 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1123,6 +1123,14 @@
   // classes are always in the boot image, so this code is primarily intended
   // for running without boot image but may be needed for boot image if the
   // AOT-initialization fails due to introduction of new code to `<clinit>`.
+  jclass classes_to_initialize[] = {
+      // Initialize `StackOverflowError`.
+      WellKnownClasses::java_lang_StackOverflowError,
+  };
+  auto* vm = down_cast<JNIEnvExt*>(self->GetJniEnv())->GetVm();
+  for (jclass c : classes_to_initialize) {
+    EnsureRootInitialized(this, self, ObjPtr<mirror::Class>::DownCast(vm->DecodeGlobal(c)));
+  }
   ArtMethod* static_methods_of_classes_to_initialize[] = {
       // Initialize primitive boxing classes (avoid check at runtime).
       WellKnownClasses::java_lang_Boolean_valueOf,
@@ -1137,6 +1145,14 @@
   for (ArtMethod* method : static_methods_of_classes_to_initialize) {
     EnsureRootInitialized(this, self, method->GetDeclaringClass());
   }
+  ArtField* static_fields_of_classes_to_initialize[] = {
+      // Initialize empty arrays needed by `StackOverflowError`.
+      WellKnownClasses::java_util_Collections_EMPTY_LIST,
+      WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT,
+  };
+  for (ArtField* field : static_fields_of_classes_to_initialize) {
+    EnsureRootInitialized(this, self, field->GetDeclaringClass());
+  }
 }
 
 ALWAYS_INLINE
diff --git a/runtime/common_throws.cc b/runtime/common_throws.cc
index 031cedd..86dd19e 100644
--- a/runtime/common_throws.cc
+++ b/runtime/common_throws.cc
@@ -714,29 +714,10 @@
     msg += PrettySize(self->GetStackSize());
 
     ScopedObjectAccessUnchecked soa(self);
-    StackHandleScope<4u> hs(self);
+    StackHandleScope<2u> hs(self);
     Handle<mirror::Class> j_l_soe = hs.NewHandle(
         soa.Decode<mirror::Class>(WellKnownClasses::java_lang_StackOverflowError));
-    Handle<mirror::Class> j_u_c = hs.NewHandle(
-        WellKnownClasses::java_util_Collections_EMPTY_LIST->GetDeclaringClass());
-    Handle<mirror::Class> l_u_ea = hs.NewHandle(
-        WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT->GetDeclaringClass());
-
-    // Initialize the required classes if needed.
-    // TODO: Initialize these classes during `ClassLinker` initialization to avoid doing it here.
-    auto ensure_initialized = [self](Handle<mirror::Class> klass)
-        REQUIRES_SHARED(Locks::mutator_lock_) {
-      if (UNLIKELY(!klass->IsVisiblyInitialized()) &&
-          UNLIKELY(!Runtime::Current()->GetClassLinker()->EnsureInitialized(
-                        self, klass, /*can_init_fields=*/ true, /*can_init_parents=*/ true))) {
-        LOG(WARNING) << "Failed to initialize class " << klass->PrettyDescriptor();
-        return false;
-      }
-      return true;
-    };
-    if (!ensure_initialized(j_l_soe) || !ensure_initialized(j_u_c) || !ensure_initialized(l_u_ea)) {
-      return;
-    }
+    DCHECK(j_l_soe->IsInitialized());
 
     // Allocate an uninitialized object.
     Handle<mirror::Object> exc = hs.NewHandle(j_l_soe->AllocObject(self));
@@ -774,8 +755,11 @@
 
     // suppressedExceptions.
     {
+      ObjPtr<mirror::Class> j_u_c =
+          WellKnownClasses::java_util_Collections_EMPTY_LIST->GetDeclaringClass();
+      DCHECK(j_u_c->IsInitialized());
       ObjPtr<mirror::Object> empty_list =
-          WellKnownClasses::java_util_Collections_EMPTY_LIST->GetObject(j_u_c.Get());
+          WellKnownClasses::java_util_Collections_EMPTY_LIST->GetObject(j_u_c);
       CHECK(empty_list != nullptr);
       WellKnownClasses::java_lang_Throwable_suppressedExceptions
           ->SetObject</*kTransactionActive=*/ false>(exc.Get(), empty_list);
@@ -790,8 +774,11 @@
           ->SetObject</*kTransactionActive=*/ false>(exc.Get(), stack_state_val);
 
       // stackTrace.
+      ObjPtr<mirror::Class> l_u_ea =
+          WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT->GetDeclaringClass();
+      DCHECK(l_u_ea->IsInitialized());
       ObjPtr<mirror::Object> empty_ste =
-          WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT->GetObject(l_u_ea.Get());
+          WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT->GetObject(l_u_ea);
       CHECK(empty_ste != nullptr);
       WellKnownClasses::java_lang_Throwable_stackTrace
           ->SetObject</*kTransactionActive=*/ false>(exc.Get(), empty_ste);