Fix failure to initialize AnnotatedStackTraceElement.

Test: 171-init-aste
Test: testrunner.py --host --interpreter
Bug: 76208924
Change-Id: I2a0892c5cc8ab5cbc54a94c25a02add1031e68f5
diff --git a/runtime/thread.cc b/runtime/thread.cc
index b13d8ec..d17f409 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -2881,6 +2881,17 @@
 
   Handle<mirror::Class> h_aste_class(hs.NewHandle<mirror::Class>(
       h_aste_array_class->GetComponentType()));
+
+  // Make sure the AnnotatedStackTraceElement.class is initialized, b/76208924 .
+  class_linker->EnsureInitialized(soa.Self(),
+                                  h_aste_class,
+                                  /* can_init_fields */ true,
+                                  /* can_init_parents */ true);
+  if (soa.Self()->IsExceptionPending()) {
+    // This should not fail in a healthy runtime.
+    return nullptr;
+  }
+
   ArtField* stack_trace_element_field = h_aste_class->FindField(
       soa.Self(), h_aste_class.Get(), "stackTraceElement", "Ljava/lang/StackTraceElement;");
   DCHECK(stack_trace_element_field != nullptr);
diff --git a/test/171-init-aste/expected.txt b/test/171-init-aste/expected.txt
new file mode 100644
index 0000000..b0aad4d
--- /dev/null
+++ b/test/171-init-aste/expected.txt
@@ -0,0 +1 @@
+passed
diff --git a/test/171-init-aste/info.txt b/test/171-init-aste/info.txt
new file mode 100644
index 0000000..201e8ad
--- /dev/null
+++ b/test/171-init-aste/info.txt
@@ -0,0 +1 @@
+Regression test for failure to initialize dalvik.system.AnnotatedStackTraceElement.
diff --git a/test/171-init-aste/src-art/Main.java b/test/171-init-aste/src-art/Main.java
new file mode 100644
index 0000000..9d36610
--- /dev/null
+++ b/test/171-init-aste/src-art/Main.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.Method;
+import dalvik.system.AnnotatedStackTraceElement;
+
+public class Main {
+    public static void main(String args[]) throws Exception {
+        Class<?> vmStack = Class.forName("dalvik.system.VMStack");
+        Method getAnnotatedThreadStackTrace =
+                vmStack.getDeclaredMethod("getAnnotatedThreadStackTrace", Thread.class);
+        Object[] annotatedStackTrace =
+                (Object[]) getAnnotatedThreadStackTrace.invoke(null, Thread.currentThread());
+        AnnotatedStackTraceElement annotatedElement =
+            (AnnotatedStackTraceElement) annotatedStackTrace[0];
+        // This used to fail an assertion that the AnnotatedStackTraceElement.class
+        // is at least initializing (i.e. initializing, initialized or resolved-erroneous).
+        // Note: We cannot use reflection for this test because getDeclaredMethod() would
+        // initialize the class and hide the failure.
+        annotatedElement.getStackTraceElement();
+
+        System.out.println("passed");
+    }
+}
diff --git a/test/171-init-aste/src/Main.java b/test/171-init-aste/src/Main.java
new file mode 100644
index 0000000..4479cb4
--- /dev/null
+++ b/test/171-init-aste/src/Main.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+public class Main {
+    // Note: This file is used for the RI which does not support
+    // dalvik.system.AnnotatedStackTraceElement (see src-art/Main.java),
+    // so that we do not need an exclusion in known failures.
+    public static void main(String args[]) throws Exception {
+        System.out.println("passed");
+    }
+}