Refactor hidden API runtime flag from negative to positive

There are only two situations in which we want to enable hidden API
access checks: (a) when forking a non-platform app from zygote, and
(b) when compiling such app with dex2oat. It is easier to cover these
two situations than all the other situations when we want to disable
the checks. Make the flag in Runtime class false by default and only
enable if checks are explicitly requested by the runtime.

Bug: 64382372
Test: boot device, install and run apps, check log messages
Change-Id: I7faf440ed714a5ddd08f6172d370202f0131db50
diff --git a/runtime/native/dalvik_system_ZygoteHooks.cc b/runtime/native/dalvik_system_ZygoteHooks.cc
index 648a464..12400e2 100644
--- a/runtime/native/dalvik_system_ZygoteHooks.cc
+++ b/runtime/native/dalvik_system_ZygoteHooks.cc
@@ -173,7 +173,7 @@
   DEBUG_JAVA_DEBUGGABLE           = 1 << 8,
   DISABLE_VERIFIER                = 1 << 9,
   ONLY_USE_SYSTEM_OAT_FILES       = 1 << 10,
-  DISABLE_HIDDEN_API_CHECKS       = 1 << 11,
+  ENABLE_HIDDEN_API_CHECKS        = 1 << 11,
   DEBUG_GENERATE_MINI_DEBUG_INFO  = 1 << 12,
 };
 
@@ -282,7 +282,7 @@
   // Our system thread ID, etc, has changed so reset Thread state.
   thread->InitAfterFork();
   runtime_flags = EnableDebugFeatures(runtime_flags);
-  bool do_hidden_api_checks = true;
+  bool do_hidden_api_checks = false;
 
   if ((runtime_flags & DISABLE_VERIFIER) != 0) {
     Runtime::Current()->DisableVerifier();
@@ -294,9 +294,9 @@
     runtime_flags &= ~ONLY_USE_SYSTEM_OAT_FILES;
   }
 
-  if ((runtime_flags & DISABLE_HIDDEN_API_CHECKS) != 0) {
-    do_hidden_api_checks = false;
-    runtime_flags &= ~DISABLE_HIDDEN_API_CHECKS;
+  if ((runtime_flags & ENABLE_HIDDEN_API_CHECKS) != 0) {
+    do_hidden_api_checks = true;
+    runtime_flags &= ~ENABLE_HIDDEN_API_CHECKS;
   }
 
   if (runtime_flags != 0) {
diff --git a/runtime/parsed_options.cc b/runtime/parsed_options.cc
index 72346cc..c7f73d1 100644
--- a/runtime/parsed_options.cc
+++ b/runtime/parsed_options.cc
@@ -330,8 +330,8 @@
       .Define("-Xtarget-sdk-version:_")
           .WithType<int>()
           .IntoKey(M::TargetSdkVersion)
-      .Define("-Xno-hidden-api-checks")
-          .IntoKey(M::NoHiddenApiChecks)
+      .Define("-Xhidden-api-checks")
+          .IntoKey(M::HiddenApiChecks)
       .Define("-Xuse-stderr-logger")
           .IntoKey(M::UseStderrLogger)
       .Ignore({
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 33298ad..7aca12e 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -266,7 +266,7 @@
       oat_file_manager_(nullptr),
       is_low_memory_mode_(false),
       safe_mode_(false),
-      do_hidden_api_checks_(true),
+      do_hidden_api_checks_(false),
       pending_hidden_api_warning_(false),
       dedupe_hidden_api_warnings_(true),
       always_set_hidden_api_warning_flag_(false),
@@ -1182,14 +1182,7 @@
   // by default and we only enable them if:
   // (a) runtime was started with a flag that enables the checks, or
   // (b) Zygote forked a new process that is not exempt (see ZygoteHooks).
-  // TODO(dbrazdil): Turn the NoHiddenApiChecks negative flag into a positive one
-  // to clean up this logic.
-  if (kIsTargetBuild && IsAotCompiler() && !runtime_options.Exists(Opt::NoHiddenApiChecks)) {
-    // dex2oat on target without -Xno-hidden-api-checks.
-    do_hidden_api_checks_ = !IsCompilingBootImage();
-  } else {
-    do_hidden_api_checks_ = false;
-  }
+  do_hidden_api_checks_ = runtime_options.Exists(Opt::HiddenApiChecks);
   DCHECK(!is_zygote_ || !do_hidden_api_checks_)
       << "Zygote should not be started with hidden API checks";
 
diff --git a/runtime/runtime_options.def b/runtime/runtime_options.def
index 4831d7d..dba648e 100644
--- a/runtime/runtime_options.def
+++ b/runtime/runtime_options.def
@@ -119,7 +119,7 @@
 RUNTIME_OPTIONS_KEY (verifier::VerifyMode, \
                                           Verify,                         verifier::VerifyMode::kEnable)
 RUNTIME_OPTIONS_KEY (int,                 TargetSdkVersion,               Runtime::kUnsetSdkVersion)
-RUNTIME_OPTIONS_KEY (Unit,                NoHiddenApiChecks)
+RUNTIME_OPTIONS_KEY (Unit,                HiddenApiChecks)
 RUNTIME_OPTIONS_KEY (std::string,         NativeBridge)
 RUNTIME_OPTIONS_KEY (unsigned int,        ZygoteMaxFailedBoots,           10)
 RUNTIME_OPTIONS_KEY (Unit,                NoDexFileFallback)