Revert "Harden hidden api checks."

Revert submission 1203343-hidden-api-check-hardening

Reason for revert: art-asan target failure
Reverted Changes:
I5af9f79f5: Add change id for hidden api check hardening
Ifc8d39ba2: Harden hidden api checks.

Change-Id: Ifb824ff00cb78f03cd388c56dd7411f5c51bb531
diff --git a/runtime/native/java_lang_Class.cc b/runtime/native/java_lang_Class.cc
index 11399c0..da87713 100644
--- a/runtime/native/java_lang_Class.cc
+++ b/runtime/native/java_lang_Class.cc
@@ -55,17 +55,12 @@
 
 namespace art {
 
-// Should be the same as dalvik.system.VMRuntime.HIDDEN_API_CHECK_HARDENING.
-// Corresponds to a bug id.
-static constexpr uint64_t kHiddenApiCheckHardeningId = 142365358;
-
 // Walks the stack, finds the caller of this reflective call and returns
 // a hiddenapi AccessContext formed from its declaring class.
 static hiddenapi::AccessContext GetReflectionCaller(Thread* self)
     REQUIRES_SHARED(Locks::mutator_lock_) {
-  // Walk the stack and find the first frame not from java.lang.Class,
-  // java.lang.invoke or java.lang.reflect. This is very expensive.
-  // Save this till the last.
+  // Walk the stack and find the first frame not from java.lang.Class and not
+  // from java.lang.invoke. This is very expensive. Save this till the last.
   struct FirstExternalCallerVisitor : public StackVisitor {
     explicit FirstExternalCallerVisitor(Thread* thread)
         : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames),
@@ -98,13 +93,6 @@
             && !m->IsClassInitializer()) {
           return true;
         }
-        // Check for classes in the java.lang.reflect package.
-        if (Runtime::Current()->isChangeEnabled(kHiddenApiCheckHardeningId)) {
-          ObjPtr<mirror::Class> method_class = GetClassRoot<mirror::Method>();
-          if (declaring_class->IsInSamePackage(method_class)) {
-            return true;
-          }
-        }
       }
 
       caller = m;
diff --git a/test/674-hiddenapi/hiddenapi.cc b/test/674-hiddenapi/hiddenapi.cc
index 5528a77..413c707 100644
--- a/test/674-hiddenapi/hiddenapi.cc
+++ b/test/674-hiddenapi/hiddenapi.cc
@@ -27,9 +27,6 @@
 namespace art {
 namespace Test674HiddenApi {
 
-// Should be the same as dalvik.system.VMRuntime.HIDDEN_API_CHECK_HARDENING
-static constexpr uint64_t kHiddenApiCheckHardeningId = 142365358;
-
 std::vector<std::vector<std::unique_ptr<const DexFile>>> opened_dex_files;
 
 extern "C" JNIEXPORT void JNICALL Java_Main_init(JNIEnv*, jclass) {
@@ -319,18 +316,5 @@
   return static_cast<jint>(kAccHiddenapiBits);
 }
 
-extern "C" JNIEXPORT void JNICALL Java_Reflection_setHiddenApiCheckHardening(JNIEnv*, jclass,
-    jboolean value) {
-  std::set<uint64_t> disabled_changes = Runtime::Current()->GetDisabledCompatChanges();
-  if (value == JNI_TRUE) {
-    // If hidden api check hardening is enabled, remove it from the set of disabled changes.
-    disabled_changes.erase(kHiddenApiCheckHardeningId);
-  } else {
-    // If hidden api check hardening is disabled, add it to the set of disabled changes.
-    disabled_changes.insert(kHiddenApiCheckHardeningId);
-  }
-  Runtime::Current()->SetDisabledCompatChanges(disabled_changes);
-}
-
 }  // namespace Test674HiddenApi
 }  // namespace art
diff --git a/test/674-hiddenapi/src-ex/ChildClass.java b/test/674-hiddenapi/src-ex/ChildClass.java
index 9295655..f120bda 100644
--- a/test/674-hiddenapi/src-ex/ChildClass.java
+++ b/test/674-hiddenapi/src-ex/ChildClass.java
@@ -105,13 +105,6 @@
     boolean isSameBoot = (isParentInBoot == isChildInBoot);
     boolean isDebuggable = VMRuntime.getRuntime().isJavaDebuggable();
 
-    // For compat reasons, meta-reflection should still be usable by apps if hidden api check
-    // hardening is disabled (i.e. target SDK is Q or earlier). The only configuration where this
-    // workaround used to work is for ChildClass in the Application domain and ParentClass in the
-    // Platform domain, so only test that configuration with hidden api check hardening disabled.
-    boolean testHiddenApiCheckHardeningDisabled =
-        (childDomain == DexDomain.Application) && (parentDomain == DexDomain.Platform);
-
     // Run meaningful combinations of access flags.
     for (Hiddenness hiddenness : Hiddenness.values()) {
       final Behaviour expected;
@@ -145,19 +138,18 @@
           for (Class klass : new Class<?>[] { ParentClass.class, ParentInterface.class }) {
             String baseName = visibility.name() + suffix;
             checkField(klass, "field" + baseName, isStatic, visibility, expected,
-                invokesMemberCallback, testHiddenApiCheckHardeningDisabled);
+                invokesMemberCallback);
             checkMethod(klass, "method" + baseName, isStatic, visibility, expected,
-                invokesMemberCallback, testHiddenApiCheckHardeningDisabled);
+                invokesMemberCallback);
           }
 
           // Check whether one can use a class constructor.
-          checkConstructor(ParentClass.class, visibility, hiddenness, expected,
-                testHiddenApiCheckHardeningDisabled);
+          checkConstructor(ParentClass.class, visibility, hiddenness, expected);
 
           // Check whether one can use an interface default method.
           String name = "method" + visibility.name() + "Default" + hiddenness.name();
           checkMethod(ParentInterface.class, name, /*isStatic*/ false, visibility, expected,
-              invokesMemberCallback, testHiddenApiCheckHardeningDisabled);
+              invokesMemberCallback);
         }
 
         // Test whether static linking succeeds.
@@ -220,8 +212,7 @@
   }
 
   private static void checkField(Class<?> klass, String name, boolean isStatic,
-      Visibility visibility, Behaviour behaviour, boolean invokesMemberCallback,
-      boolean testHiddenApiCheckHardeningDisabled) throws Exception {
+      Visibility visibility, Behaviour behaviour, boolean invokesMemberCallback) throws Exception {
 
     boolean isPublic = (visibility == Visibility.Public);
     boolean canDiscover = (behaviour != Behaviour.Denied);
@@ -286,23 +277,6 @@
                               canDiscover);
     }
 
-    // Check for meta reflection.
-
-    // With hidden api check hardening enabled, only white and light greylisted fields should be
-    // discoverable.
-    if (Reflection.canDiscoverFieldWithMetaReflection(klass, name, true) != canDiscover) {
-      throwDiscoveryException(klass, name, false,
-          "Meta reflection with hidden api hardening enabled", canDiscover);
-    }
-
-    if (testHiddenApiCheckHardeningDisabled) {
-      // With hidden api check hardening disabled, all fields should be discoverable.
-      if (Reflection.canDiscoverFieldWithMetaReflection(klass, name, false) != true) {
-        throwDiscoveryException(klass, name, false,
-            "Meta reflection with hidden api hardening enabled", canDiscover);
-      }
-    }
-
     if (canDiscover) {
       // Test that modifiers are unaffected.
 
@@ -331,8 +305,7 @@
   }
 
   private static void checkMethod(Class<?> klass, String name, boolean isStatic,
-      Visibility visibility, Behaviour behaviour, boolean invokesMemberCallback,
-      boolean testHiddenApiCheckHardeningDisabled) throws Exception {
+      Visibility visibility, Behaviour behaviour, boolean invokesMemberCallback) throws Exception {
 
     boolean isPublic = (visibility == Visibility.Public);
     if (klass.isInterface() && !isPublic) {
@@ -380,23 +353,6 @@
                               canDiscover);
     }
 
-    // Check for meta reflection.
-
-    // With hidden api check hardening enabled, only white and light greylisted methods should be
-    // discoverable.
-    if (Reflection.canDiscoverMethodWithMetaReflection(klass, name, true) != canDiscover) {
-      throwDiscoveryException(klass, name, false,
-          "Meta reflection with hidden api hardening enabled", canDiscover);
-    }
-
-    if (testHiddenApiCheckHardeningDisabled) {
-      // With hidden api check hardening disabled, all methods should be discoverable.
-      if (Reflection.canDiscoverMethodWithMetaReflection(klass, name, false) != true) {
-        throwDiscoveryException(klass, name, false,
-            "Meta reflection with hidden api hardening enabled", canDiscover);
-      }
-    }
-
     // Finish here if we could not discover the method.
 
     if (canDiscover) {
@@ -425,7 +381,7 @@
   }
 
   private static void checkConstructor(Class<?> klass, Visibility visibility, Hiddenness hiddenness,
-      Behaviour behaviour, boolean testHiddenApiCheckHardeningDisabled) throws Exception {
+      Behaviour behaviour) throws Exception {
 
     boolean isPublic = (visibility == Visibility.Public);
     String signature = "(" + visibility.mAssociatedType.mShorty +
@@ -480,23 +436,6 @@
                               canDiscover);
     }
 
-    // Check for meta reflection.
-
-    // With hidden api check hardening enabled, only white and light greylisted constructors should
-    // be discoverable.
-    if (Reflection.canDiscoverConstructorWithMetaReflection(klass, args, true) != canDiscover) {
-      throwDiscoveryException(klass, fullName, false,
-          "Meta reflection with hidden api hardening enabled", canDiscover);
-    }
-
-    if (testHiddenApiCheckHardeningDisabled) {
-      // With hidden api check hardening disabled, all constructors should be discoverable.
-      if (Reflection.canDiscoverConstructorWithMetaReflection(klass, args, false) != true) {
-        throwDiscoveryException(klass, fullName, false,
-            "Meta reflection with hidden api hardening enabled", canDiscover);
-      }
-    }
-
     if (canDiscover) {
       // Test whether we can invoke the constructor.
 
diff --git a/test/674-hiddenapi/src-ex/Reflection.java b/test/674-hiddenapi/src-ex/Reflection.java
index 173b9af..3667e91 100644
--- a/test/674-hiddenapi/src-ex/Reflection.java
+++ b/test/674-hiddenapi/src-ex/Reflection.java
@@ -16,7 +16,6 @@
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Field;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
@@ -187,45 +186,7 @@
     }
   }
 
-  public static boolean canDiscoverMethodWithMetaReflection(Class<?> klass, String name,
-      boolean hardeningEnabled) {
-    try {
-      setHiddenApiCheckHardening(hardeningEnabled);
-      Method metaGetDeclaredMethod =
-          Class.class.getDeclaredMethod("getDeclaredMethod", String.class, Class[].class);
-      // Assumes method without parameters.
-      return ((Method)metaGetDeclaredMethod.invoke(klass, name,  null)) != null;
-    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
-      return false;
-    }
-  }
-
-  public static boolean canDiscoverFieldWithMetaReflection(Class<?> klass, String name,
-      boolean hardeningEnabled) {
-    try {
-      setHiddenApiCheckHardening(hardeningEnabled);
-      Method metaGetDeclaredField =
-          Class.class.getDeclaredMethod("getDeclaredField", String.class);
-      return ((Field)metaGetDeclaredField.invoke(klass, name)) != null;
-    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
-      return false;
-    }
-  }
-
-  public static boolean canDiscoverConstructorWithMetaReflection(Class<?> klass, Class<?> args[],
-      boolean hardeningEnabled) {
-    try {
-      setHiddenApiCheckHardening(hardeningEnabled);
-      Method metaGetDeclaredConstructor =
-          Class.class.getDeclaredMethod("getDeclaredConstructor", Class[].class);
-      return ((Constructor<?>)metaGetDeclaredConstructor.invoke(klass, (Object)args)) != null;
-    } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
-      return false;
-    }
-  }
-
   private static native int getHiddenApiAccessFlags();
-  private static native void setHiddenApiCheckHardening(boolean value);
 
   public static boolean canObserveFieldHiddenAccessFlags(Class<?> klass, String name)
       throws Exception {