Fix tests on aosp_cf_x86_64_only_phone

Our nested virtualization check failed on this target. Move to a
prefix check to support this device and any other ones that appear.

Also extract the code to do the check to a library, rather than
changing in two places.

Bug: 236922543
Test: Presubmit
Change-Id: Id1d424f5f107e2098d89ca2ea8089ba956f212ca
diff --git a/compos/common/Android.bp b/compos/common/Android.bp
index 51f97f8..d1b45c4 100644
--- a/compos/common/Android.bp
+++ b/compos/common/Android.bp
@@ -14,6 +14,7 @@
         "libbinder_common",
         "libbinder_rpc_unstable_bindgen",
         "liblog_rust",
+        "libnested_virt",
         "libnum_traits",
         "librustutils",
         "libvmclient",
diff --git a/compos/common/timeouts.rs b/compos/common/timeouts.rs
index bdabb1e..d0d107f 100644
--- a/compos/common/timeouts.rs
+++ b/compos/common/timeouts.rs
@@ -18,7 +18,6 @@
 //! virtualization.
 
 use anyhow::Result;
-use rustutils::system_properties;
 use std::time::Duration;
 
 /// Holder for the various timeouts we use.
@@ -30,19 +29,10 @@
     pub vm_max_time_to_ready: Duration,
 }
 
-/// Whether the current platform requires extra time for operations inside a VM.
-pub fn need_extra_time() -> Result<bool> {
-    // Nested virtualization is slow. Check if we are running on vsoc as a proxy for this.
-    if let Some(value) = system_properties::read("ro.build.product")? {
-        Ok(value == "vsoc_x86_64" || value == "vsoc_x86")
-    } else {
-        Ok(false)
-    }
-}
-
 /// Return the timeouts that are appropriate on the current platform.
 pub fn timeouts() -> Result<&'static Timeouts> {
-    if need_extra_time()? {
+    // Nested virtualization is slow.
+    if nested_virt::is_nested_virtualization()? {
         Ok(&EXTENDED_TIMEOUTS)
     } else {
         Ok(&NORMAL_TIMEOUTS)
@@ -50,14 +40,14 @@
 }
 
 /// The timeouts that we use normally.
-pub const NORMAL_TIMEOUTS: Timeouts = Timeouts {
+const NORMAL_TIMEOUTS: Timeouts = Timeouts {
     // Note: the source of truth for these odrefresh timeouts is art/odrefresh/odr_config.h.
     odrefresh_max_execution_time: Duration::from_secs(300),
     vm_max_time_to_ready: Duration::from_secs(15),
 };
 
 /// The timeouts that we use when need_extra_time() returns true.
-pub const EXTENDED_TIMEOUTS: Timeouts = Timeouts {
+const EXTENDED_TIMEOUTS: Timeouts = Timeouts {
     odrefresh_max_execution_time: Duration::from_secs(480),
     vm_max_time_to_ready: Duration::from_secs(120),
 };
diff --git a/libs/nested_virt/Android.bp b/libs/nested_virt/Android.bp
new file mode 100644
index 0000000..e364a2d
--- /dev/null
+++ b/libs/nested_virt/Android.bp
@@ -0,0 +1,18 @@
+package {
+    default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+rust_library {
+    name: "libnested_virt",
+    crate_name: "nested_virt",
+    srcs: ["src/lib.rs"],
+    edition: "2018",
+    rustlibs: [
+        "libanyhow",
+        "librustutils",
+    ],
+    apex_available: [
+        "com.android.compos",
+        "com.android.virt",
+    ],
+}
diff --git a/libs/nested_virt/src/lib.rs b/libs/nested_virt/src/lib.rs
new file mode 100644
index 0000000..ab1f06a
--- /dev/null
+++ b/libs/nested_virt/src/lib.rs
@@ -0,0 +1,32 @@
+/*
+ * Copyright 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.
+ */
+
+//! Detection for nested virtualization.
+
+use anyhow::Result;
+use rustutils::system_properties;
+
+/// Return whether we will be running our VM in a VM, which causes the nested VM to run very slowly.
+pub fn is_nested_virtualization() -> Result<bool> {
+    // Currently nested virtualization only occurs when we run KVM inside the cuttlefish VM.
+    // So we just need to check for vsoc.
+    if let Some(value) = system_properties::read("ro.build.product")? {
+        // Fuzzy matching to allow for vsoc_x86, vsoc_x86_64, vsoc_x86_64_only, ...
+        Ok(value.starts_with("vsoc_x86"))
+    } else {
+        Ok(false)
+    }
+}
diff --git a/virtualizationservice/Android.bp b/virtualizationservice/Android.bp
index 791da24..0c9496a 100644
--- a/virtualizationservice/Android.bp
+++ b/virtualizationservice/Android.bp
@@ -35,6 +35,7 @@
         "liblog_rust",
         "libmicrodroid_metadata",
         "libmicrodroid_payload_config",
+        "libnested_virt",
         "libnix",
         "libonce_cell",
         "libregex",
diff --git a/virtualizationservice/src/crosvm.rs b/virtualizationservice/src/crosvm.rs
index 7ca802f..23719a7 100644
--- a/virtualizationservice/src/crosvm.rs
+++ b/virtualizationservice/src/crosvm.rs
@@ -22,7 +22,6 @@
 use log::{debug, error, info};
 use semver::{Version, VersionReq};
 use nix::{fcntl::OFlag, unistd::pipe2};
-use rustutils::system_properties;
 use shared_child::SharedChild;
 use std::borrow::Cow;
 use std::fs::{remove_dir_all, File};
@@ -55,18 +54,10 @@
 /// The exit status which crosvm returns when it crashes due to an error.
 const CROSVM_CRASH_STATUS: i32 = 33;
 
-fn is_nested_virtualization() -> bool {
-    //  Check if we are running on vsoc as a proxy for this.
-    matches!(
-        system_properties::read("ro.build.product").unwrap().as_deref(),
-        Some("vsoc_x86_64") | Some("vsoc_x86")
-    )
-}
-
 lazy_static! {
     /// If the VM doesn't move to the Started state within this amount time, a hang-up error is
     /// triggered.
-    static ref BOOT_HANGUP_TIMEOUT: Duration = if is_nested_virtualization() {
+    static ref BOOT_HANGUP_TIMEOUT: Duration = if nested_virt::is_nested_virtualization().unwrap() {
         // Nested virtualization is slow, so we need a longer timeout.
         Duration::from_secs(100)
     } else {