Revert "Clean up `StackOverflowError` construction."

This reverts commit 74d6f336810100563bc3b9e555dfd2aea1838b54.

Bug: 253570082

Reason for revert: make those tests fail:
test-art-host-run-test-debug-prebuild-optimizing-no-relocate-ntrace-cms-checkjni-picimage-debuggable-redefine-stress-cdex-fast-1953-pop-frame64
test-art-host-run-test-debug-prebuild-optimizing-no-relocate-ntrace-cms-checkjni-picimage-debuggable-redefine-stress-cdex-fast-1954-pop-frame-jit64
test-art-host-run-test-debug-prebuild-optimizing-no-relocate-ntrace-cms-checkjni-picimage-debuggable-redefine-stress-cdex-fast-1955-pop-frame-jit-called64
test-art-host-run-test-debug-prebuild-optimizing-no-relocate-ntrace-cms-checkjni-picimage-debuggable-redefine-stress-cdex-fast-1956-pop-frame-jit-calling64
test-art-host-run-test-debug-prebuild-optimizing-no-relocate-ntrace-cms-checkjni-picimage-debuggable-redefine-stress-cdex-fast-1969-force-early-return-void64

Change-Id: Ie8d4d727119da531b9f802c6b29d4d101557f271
diff --git a/compiler/optimizing/critical_native_abi_fixup_arm.cc b/compiler/optimizing/critical_native_abi_fixup_arm.cc
index 2373053..1bf3542 100644
--- a/compiler/optimizing/critical_native_abi_fixup_arm.cc
+++ b/compiler/optimizing/critical_native_abi_fixup_arm.cc
@@ -45,9 +45,8 @@
     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 44a72c5..ecea692 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -1123,14 +1123,6 @@
   // 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,
@@ -1145,14 +1137,6 @@
   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 86dd19e..031cedd 100644
--- a/runtime/common_throws.cc
+++ b/runtime/common_throws.cc
@@ -714,10 +714,29 @@
     msg += PrettySize(self->GetStackSize());
 
     ScopedObjectAccessUnchecked soa(self);
-    StackHandleScope<2u> hs(self);
+    StackHandleScope<4u> hs(self);
     Handle<mirror::Class> j_l_soe = hs.NewHandle(
         soa.Decode<mirror::Class>(WellKnownClasses::java_lang_StackOverflowError));
-    DCHECK(j_l_soe->IsInitialized());
+    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;
+    }
 
     // Allocate an uninitialized object.
     Handle<mirror::Object> exc = hs.NewHandle(j_l_soe->AllocObject(self));
@@ -755,11 +774,8 @@
 
     // 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);
+          WellKnownClasses::java_util_Collections_EMPTY_LIST->GetObject(j_u_c.Get());
       CHECK(empty_list != nullptr);
       WellKnownClasses::java_lang_Throwable_suppressedExceptions
           ->SetObject</*kTransactionActive=*/ false>(exc.Get(), empty_list);
@@ -774,11 +790,8 @@
           ->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);
+          WellKnownClasses::libcore_util_EmptyArray_STACK_TRACE_ELEMENT->GetObject(l_u_ea.Get());
       CHECK(empty_ste != nullptr);
       WellKnownClasses::java_lang_Throwable_stackTrace
           ->SetObject</*kTransactionActive=*/ false>(exc.Get(), empty_ste);