Don't initialize at AOT time classes that need access checks.

The compiler driver currently cannot handle it.

Test: SuperWithAccessChecks
Bug: 242151549
Change-Id: I3f3f72ea4f21851eabad5d7f8a05764f3ee842da
diff --git a/build/apex/art_apex_test.py b/build/apex/art_apex_test.py
index 3ec163cb..0ee79bf 100755
--- a/build/apex/art_apex_test.py
+++ b/build/apex/art_apex_test.py
@@ -758,6 +758,7 @@
     self._checker.check_art_test_data('art-gtest-jars-MainEmptyUncompressed.jar')
     self._checker.check_art_test_data('art-gtest-jars-Dex2oatVdexTestDex.jar')
     self._checker.check_art_test_data('art-gtest-jars-Dex2oatVdexPublicSdkDex.dex')
+    self._checker.check_art_test_data('art-gtest-jars-SuperWithAccessChecks.dex')
 
 
 class NoSuperfluousBinariesChecker:
diff --git a/dex2oat/Android.bp b/dex2oat/Android.bp
index b111e63..cd04dc1 100644
--- a/dex2oat/Android.bp
+++ b/dex2oat/Android.bp
@@ -480,6 +480,7 @@
         ":art-gtest-jars-StaticLeafMethods",
         ":art-gtest-jars-Statics",
         ":art-gtest-jars-StringLiterals",
+        ":art-gtest-jars-SuperWithAccessChecks",
         ":art-gtest-jars-VerifierDeps",
         ":art-gtest-jars-VerifierDepsMulti",
         ":art-gtest-jars-VerifySoftFailDuringClinit",
diff --git a/dex2oat/art_standalone_dex2oat_tests.xml b/dex2oat/art_standalone_dex2oat_tests.xml
index 1b6ced8..8fb120c 100644
--- a/dex2oat/art_standalone_dex2oat_tests.xml
+++ b/dex2oat/art_standalone_dex2oat_tests.xml
@@ -24,6 +24,7 @@
         <option name="cleanup" value="true" />
         <option name="push" value="art-gtest-jars-AbstractMethod.jar->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-AbstractMethod.jar" />
         <option name="push" value="art-gtest-jars-ArrayClassWithUnresolvedComponent.dex->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-ArrayClassWithUnresolvedComponent.dex" />
+        <option name="push" value="art-gtest-jars-SuperWithAccessChecks.dex->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-SuperWithAccessChecks.dex" />
         <option name="push" value="art-gtest-jars-DefaultMethods.jar->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-DefaultMethods.jar" />
         <option name="push" value="art-gtest-jars-Dex2oatVdexPublicSdkDex.dex->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-Dex2oatVdexPublicSdkDex.dex" />
         <option name="push" value="art-gtest-jars-Dex2oatVdexTestDex.jar->/data/local/tmp/art_standalone_dex2oat_tests/art-gtest-jars-Dex2oatVdexTestDex.jar" />
diff --git a/dex2oat/linker/image_test.cc b/dex2oat/linker/image_test.cc
index 357d9c7..82c87f5 100644
--- a/dex2oat/linker/image_test.cc
+++ b/dex2oat/linker/image_test.cc
@@ -198,5 +198,15 @@
                               "[[LClassWithMissingSuper"});
 }
 
+TEST_F(ImageTest, TestSuperWithAccessChecks) {
+  CompilationHelper helper;
+  Compile(ImageHeader::kStorageModeUncompressed,
+          /*max_image_block_size=*/std::numeric_limits<uint32_t>::max(),
+          helper,
+          "SuperWithAccessChecks",
+          /*image_classes=*/ {"LSubClass;", "LImplementsClass;"},
+          /*image_classes_failing_aot_clinit=*/ {"LSubClass;", "LImplementsClass;"});
+}
+
 }  // namespace linker
 }  // namespace art
diff --git a/runtime/class_linker.cc b/runtime/class_linker.cc
index ccf4ff2..dc67dca 100644
--- a/runtime/class_linker.cc
+++ b/runtime/class_linker.cc
@@ -5088,11 +5088,19 @@
   CHECK_EQ(prototype, method->GetInterfaceMethodIfProxy(image_pointer_size_));
 }
 
-bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass, bool can_init_statics,
+bool ClassLinker::CanWeInitializeClass(ObjPtr<mirror::Class> klass,
+                                       bool can_init_statics,
                                        bool can_init_parents) {
   if (can_init_statics && can_init_parents) {
     return true;
   }
+  DCHECK(Runtime::Current()->IsAotCompiler());
+
+  // We currently don't support initializing at AOT time classes that need access
+  // checks.
+  if (klass->IsVerifiedNeedsAccessChecks()) {
+    return false;
+  }
   if (!can_init_statics) {
     // Check if there's a class initializer.
     ArtMethod* clinit = klass->FindClassInitializer(image_pointer_size_);
diff --git a/test/Android.bp b/test/Android.bp
index 715aefd..9bb7c4d 100644
--- a/test/Android.bp
+++ b/test/Android.bp
@@ -1443,6 +1443,7 @@
         ":art-gtest-jars-MainStripped",
         ":art-gtest-jars-MainUncompressedAligned",
         ":art-gtest-jars-MultiDexUncompressedAligned",
+        ":art-gtest-jars-SuperWithAccessChecks",
         ":art-gtest-jars-VerifierDeps",
         ":art-gtest-jars-VerifierDepsMulti",
         ":art-gtest-jars-VerifySoftFailDuringClinit",
@@ -1855,6 +1856,13 @@
 }
 
 genrule {
+    name: "art-gtest-jars-SuperWithAccessChecks",
+    defaults: ["art-gtest-jars-smali-defaults"],
+    srcs: ["SuperWithAccessChecks/*.smali"],
+    out: ["art-gtest-jars-SuperWithAccessChecks.dex"],
+}
+
+genrule {
     name: "art-gtest-jars-LinkageTest",
     defaults: ["art-gtest-jars-smali-defaults"],
     srcs: ["LinkageTest/*.smali"],
diff --git a/test/SuperWithAccessChecks/ImplementsClass.smali b/test/SuperWithAccessChecks/ImplementsClass.smali
new file mode 100644
index 0000000..e39877f
--- /dev/null
+++ b/test/SuperWithAccessChecks/ImplementsClass.smali
@@ -0,0 +1,18 @@
+# Copyright (C) 2022 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.
+
+.class public LImplementsClass;
+
+.super Ljava/lang/Object;
+.implements LItf;
diff --git a/test/SuperWithAccessChecks/Itf.smali b/test/SuperWithAccessChecks/Itf.smali
new file mode 100644
index 0000000..c91686e
--- /dev/null
+++ b/test/SuperWithAccessChecks/Itf.smali
@@ -0,0 +1,23 @@
+# Copyright (C) 2022 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.
+
+.class public abstract interface LItf;
+
+.super Ljava/lang/Object;
+
+.method public foo()V
+    .registers 1
+    invoke-static {}, LMissingClass;->forName()V
+    return-void
+.end method
diff --git a/test/SuperWithAccessChecks/SubClass.smali b/test/SuperWithAccessChecks/SubClass.smali
new file mode 100644
index 0000000..21c8f1c
--- /dev/null
+++ b/test/SuperWithAccessChecks/SubClass.smali
@@ -0,0 +1,17 @@
+# Copyright (C) 2022 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.
+
+.class public LSubClass;
+
+.super LSuperClass;
diff --git a/test/SuperWithAccessChecks/SuperClass.smali b/test/SuperWithAccessChecks/SuperClass.smali
new file mode 100644
index 0000000..8e12521
--- /dev/null
+++ b/test/SuperWithAccessChecks/SuperClass.smali
@@ -0,0 +1,24 @@
+# Copyright (C) 2022 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.
+
+.class public LSuperClass;
+
+.super Ljava/lang/Object;
+
+.method static foo()V
+    .registers 0
+    invoke-static {}, LMissingClass;->forName()V
+    return-void
+.end method
+