Merge "Filter bootarg for non-debuggable VMs"
diff --git a/pvmfw/src/dice.rs b/pvmfw/src/dice.rs
index 4e303ac..0aa1273 100644
--- a/pvmfw/src/dice.rs
+++ b/pvmfw/src/dice.rs
@@ -17,7 +17,6 @@
 use crate::cstr;
 use crate::helpers::flushed_zeroize;
 use core::ffi::c_void;
-use core::ffi::CStr;
 use core::mem::size_of;
 use core::slice;
 
diff --git a/pvmfw/src/entry.rs b/pvmfw/src/entry.rs
index 8e3e47b..d172474 100644
--- a/pvmfw/src/entry.rs
+++ b/pvmfw/src/entry.rs
@@ -64,6 +64,9 @@
     // - can't access non-pvmfw memory (only statically-mapped memory)
     // - can't access MMIO (therefore, no logging)
 
+    // SAFETY - This function should and will only be called once, here.
+    unsafe { heap::init() };
+
     match main_wrapper(fdt_address as usize, payload_start as usize, payload_size as usize) {
         Ok((entry, bcc)) => jump_to_payload(fdt_address, entry.try_into().unwrap(), bcc),
         Err(_) => reboot(), // TODO(b/220071963) propagate the reason back to the host.
@@ -188,9 +191,6 @@
     // - only perform logging once the logger has been initialized
     // - only access non-pvmfw memory once (and while) it has been mapped
 
-    // SAFETY - This function should and will only be called once, here.
-    unsafe { heap::init() };
-
     logger::init(LevelFilter::Info).map_err(|_| RebootReason::InternalError)?;
 
     // Use debug!() to avoid printing to the UART if we failed to configure it as only local
diff --git a/pvmfw/src/exceptions.rs b/pvmfw/src/exceptions.rs
index 462a9cc..a6ac4fe 100644
--- a/pvmfw/src/exceptions.rs
+++ b/pvmfw/src/exceptions.rs
@@ -86,7 +86,7 @@
 }
 
 #[no_mangle]
-extern "C" fn sync_exception_current(_elr: u64, _spsr: u64) {
+extern "C" fn sync_exception_current(elr: u64, _spsr: u64) {
     // Disable logging in exception handler to prevent unsafe writes to UART.
     let _guard = logger::suppress();
     let esr: Esr = read_sysreg!("esr_el1").into();
@@ -97,7 +97,7 @@
         if !handling_uart_exception(esr, far) {
             eprintln!("sync_exception_current");
             eprintln!("{e}");
-            eprintln!("{esr}, far={far:#08x}");
+            eprintln!("{esr}, far={far:#08x}, elr={elr:#08x}");
         }
         reboot()
     }
diff --git a/pvmfw/src/helpers.rs b/pvmfw/src/helpers.rs
index c230784..403c7e4 100644
--- a/pvmfw/src/helpers.rs
+++ b/pvmfw/src/helpers.rs
@@ -31,6 +31,7 @@
     ($sysreg:literal) => {{
         let mut r: usize;
         // Safe because it reads a system register and does not affect Rust.
+        #[allow(unused_unsafe)] // In case the macro is used within an unsafe block.
         unsafe {
             core::arch::asm!(
                 concat!("mrs {}, ", $sysreg),
@@ -182,6 +183,6 @@
 #[macro_export]
 macro_rules! cstr {
     ($str:literal) => {{
-        CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
+        core::ffi::CStr::from_bytes_with_nul(concat!($str, "\0").as_bytes()).unwrap()
     }};
 }
diff --git a/tests/hostside/helper/java/com/android/microdroid/test/host/MicrodroidHostTestCaseBase.java b/tests/hostside/helper/java/com/android/microdroid/test/host/MicrodroidHostTestCaseBase.java
index 4807c0f..81ccec7 100644
--- a/tests/hostside/helper/java/com/android/microdroid/test/host/MicrodroidHostTestCaseBase.java
+++ b/tests/hostside/helper/java/com/android/microdroid/test/host/MicrodroidHostTestCaseBase.java
@@ -16,11 +16,8 @@
 
 package com.android.microdroid.test.host;
 
-import static com.android.microdroid.test.host.CommandResultSubject.assertThat;
-import static com.android.microdroid.test.host.CommandResultSubject.command_results;
 import static com.android.tradefed.testtype.DeviceJUnit4ClassRunner.TestLogData;
 
-import static com.google.common.truth.Truth.assertThat;
 import static com.google.common.truth.Truth.assertWithMessage;
 
 import static org.junit.Assume.assumeTrue;
@@ -111,58 +108,13 @@
         LogArchiver.archiveLogThenDelete(logs, device, remotePath, localName);
     }
 
-    // Run an arbitrary command in the host side and returns the result
-    public static String runOnHost(String... cmd) {
-        return runOnHostWithTimeout(10000, cmd);
-    }
-
-    // Same as runOnHost, but failure is not an error
+    // Run an arbitrary command in the host side and returns the result.
+    // Note failure is not an error.
     public static String tryRunOnHost(String... cmd) {
         final long timeout = 10000;
         CommandResult result = RunUtil.getDefault().runTimedCmd(timeout, cmd);
         return result.getStdout().trim();
     }
-
-    // Same as runOnHost, but with custom timeout
-    private static String runOnHostWithTimeout(long timeoutMillis, String... cmd) {
-        assertThat(timeoutMillis).isAtLeast(0);
-        CommandResult result = RunUtil.getDefault().runTimedCmd(timeoutMillis, cmd);
-        assertWithMessage("Host command `" + join(cmd) + "` did not succeed")
-                .about(command_results())
-                .that(result)
-                .isSuccess();
-        return result.getStdout().trim();
-    }
-
-    // Run a shell command on Microdroid
-    public static String runOnMicrodroid(String... cmd) {
-        CommandResult result = runOnMicrodroidForResult(cmd);
-        assertWithMessage("Microdroid command `" + join(cmd) + "` did not succeed")
-                .about(command_results())
-                .that(result)
-                .isSuccess();
-        return result.getStdout().trim();
-    }
-
-    // Same as runOnHost, but keeps retrying on error for maximum attempts times
-    // Each attempt with timeoutMs
-    public static String runOnHostRetryingOnFailure(long timeoutMs, int attempts, String... cmd) {
-        CommandResult result = RunUtil.getDefault()
-                .runTimedCmdRetry(timeoutMs,
-                        MICRODROID_COMMAND_RETRY_INTERVAL_MILLIS, attempts, cmd);
-        assertWithMessage("Command `" + Arrays.toString(cmd) + "` has failed")
-                .about(command_results())
-                .that(result)
-                .isSuccess();
-        return result.getStdout().trim();
-    }
-
-    public static CommandResult runOnMicrodroidForResult(String... cmd) {
-        final long timeoutMs = 30000; // 30 sec. Microdroid is extremely slow on GCE-on-CF.
-        return RunUtil.getDefault()
-                .runTimedCmd(timeoutMs, "adb", "-s", MICRODROID_SERIAL, "shell", join(cmd));
-    }
-
     private static String join(String... strs) {
         return String.join(" ", Arrays.asList(strs));
     }
@@ -195,52 +147,4 @@
                 .that(pathLine).startsWith("package:");
         return pathLine.substring("package:".length());
     }
-
-    // Establish an adb connection to microdroid by letting Android forward the connection to
-    // microdroid. Wait until the connection is established and microdroid is booted.
-    public static void adbConnectToMicrodroid(ITestDevice androidDevice, String cid) {
-        long start = System.currentTimeMillis();
-        long timeoutMillis = MICRODROID_ADB_CONNECT_TIMEOUT_MINUTES * 60 * 1000;
-        long elapsed = 0;
-
-        // In case there is a stale connection...
-        tryRunOnHost("adb", "disconnect", MICRODROID_SERIAL);
-
-        final String serial = androidDevice.getSerialNumber();
-        final String from = "tcp:" + TEST_VM_ADB_PORT;
-        final String to = "vsock:" + cid + ":5555";
-        runOnHost("adb", "-s", serial, "forward", from, to);
-
-        boolean disconnected = true;
-        while (disconnected) {
-            elapsed = System.currentTimeMillis() - start;
-            timeoutMillis -= elapsed;
-            start = System.currentTimeMillis();
-            String ret = runOnHostWithTimeout(timeoutMillis, "adb", "connect", MICRODROID_SERIAL);
-            disconnected = ret.equals("failed to connect to " + MICRODROID_SERIAL);
-            if (disconnected) {
-                // adb demands us to disconnect if the prior connection was a failure.
-                // b/194375443: this somtimes fails, thus 'try*'.
-                tryRunOnHost("adb", "disconnect", MICRODROID_SERIAL);
-            }
-        }
-
-        elapsed = System.currentTimeMillis() - start;
-        timeoutMillis -= elapsed;
-        runOnHostWithTimeout(timeoutMillis, "adb", "-s", MICRODROID_SERIAL, "wait-for-device");
-
-        boolean dataAvailable = false;
-        while (!dataAvailable && timeoutMillis >= 0) {
-            elapsed = System.currentTimeMillis() - start;
-            timeoutMillis -= elapsed;
-            start = System.currentTimeMillis();
-            final String checkCmd = "if [ -d /data/local/tmp ]; then echo 1; fi";
-            dataAvailable = runOnMicrodroid(checkCmd).equals("1");
-        }
-
-        // Check if it actually booted by reading a sysprop.
-        assertThat(runOnMicrodroidForResult("getprop", "ro.hardware"))
-                .stdoutTrimmed()
-                .isEqualTo("microdroid");
-    }
 }
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
index dabb31d..42a3085 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
@@ -470,8 +470,11 @@
         VmInfo vmInfo =
                 runMicrodroidWithResignedImages(
                         key, keyOverrides, /*isProtected=*/ false, /*updateBootconfigs=*/ true);
-        // Device online means that boot must have succeeded.
-        adbConnectToMicrodroid(getDevice(), vmInfo.mCid);
+        assertThatEventually(
+                100000,
+                () -> getDevice().pullFileContents(CONSOLE_PATH),
+                containsString("boot completed, time to run payload"));
+
         vmInfo.mProcess.destroy();
     }