Remove support for command-line args

We no longer support passing string arguments to a VM. Any
customization needed can be done by having the host communicate with
the VM after it starts.

Also start adding a header to provide the entry-point prototype for
better safety.

Bug: 249064104
Bug: 243513572
Test: atest MicrodroidTests MicrodroidHostTestCases
Change-Id: I0a1775f3b6a0b11c71c4831f6c380d96bb637c00
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java b/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
index 3061f65..08f1bb8 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
@@ -273,7 +273,6 @@
         if (mPayloadBinaryPath != null) {
             VirtualMachinePayloadConfig payloadConfig = new VirtualMachinePayloadConfig();
             payloadConfig.payloadPath = mPayloadBinaryPath;
-            payloadConfig.args = new String[]{};
             parcel.payload =
                     VirtualMachineAppConfig.Payload.payloadConfig(payloadConfig);
         } else {
diff --git a/launcher/Android.bp b/launcher/Android.bp
index 93cae96..123ec2e 100644
--- a/launcher/Android.bp
+++ b/launcher/Android.bp
@@ -9,4 +9,5 @@
         "libdl",
         "libdl_android",
     ],
+    header_libs: ["vm_payload_headers"],
 }
diff --git a/launcher/main.cpp b/launcher/main.cpp
index 4ecef3f..18a768d 100644
--- a/launcher/main.cpp
+++ b/launcher/main.cpp
@@ -14,13 +14,14 @@
  * limitations under the License.
  */
 
+#include <android/dlext.h>
 #include <dlfcn.h>
 
 #include <cstdlib>
 #include <iostream>
 #include <string>
 
-#include <android/dlext.h>
+#include "vm_main.h"
 
 extern "C" {
 enum {
@@ -37,10 +38,12 @@
 
 static void* load(const std::string& libname);
 
+constexpr char entrypoint_name[] = "AVmPayload_main";
+
 int main(int argc, char* argv[]) {
-    if (argc < 2) {
+    if (argc != 2) {
         std::cout << "Usage:\n";
-        std::cout << "    " << argv[0] << " LIBNAME [ARGS...]\n";
+        std::cout << "    " << argv[0] << " LIBNAME\n";
         return EXIT_FAILURE;
     }
 
@@ -51,14 +54,13 @@
         return EXIT_FAILURE;
     }
 
-    int (*entry)(int argc, char* argv[]) = nullptr;
-    entry = reinterpret_cast<decltype(entry)>(dlsym(handle, "android_native_main"));
+    AVmPayload_main_t* entry = reinterpret_cast<decltype(entry)>(dlsym(handle, entrypoint_name));
     if (entry == nullptr) {
-        std::cerr << "Failed to find entrypoint `android_native_main`: " << dlerror() << "\n";
+        std::cerr << "Failed to find entrypoint `" << entrypoint_name << "`: " << dlerror() << "\n";
         return EXIT_FAILURE;
     }
 
-    return entry(argc - 1, argv + 1);
+    return entry();
 }
 
 // Create a new linker namespace whose search path is set to the directory of the library. Then
diff --git a/microdroid/README.md b/microdroid/README.md
index 3523e9d..2519416 100644
--- a/microdroid/README.md
+++ b/microdroid/README.md
@@ -42,10 +42,10 @@
 ## Building an app
 
 An app in microdroid is a shared library file embedded in an APK. The shared
-library should have an entry point `android_native_main` as shown below:
+library should have an entry point `AVmPayload_main` as shown below:
 
 ```C++
-extern "C" int android_native_main(int argc, char* argv[]) {
+extern "C" int AVmPayload_main() {
   printf("Hello Microdroid!\n");
 }
 ```
diff --git a/microdroid/payload/config/src/lib.rs b/microdroid/payload/config/src/lib.rs
index 54b745e..08b8b42 100644
--- a/microdroid/payload/config/src/lib.rs
+++ b/microdroid/payload/config/src/lib.rs
@@ -85,10 +85,6 @@
     /// - For executable task, this is the path to the executable.
     /// - For microdroid_launcher task, this is the name of .so
     pub command: String,
-
-    /// Args to the command
-    #[serde(default)]
-    pub args: Vec<String>,
 }
 
 impl Default for TaskType {
diff --git a/microdroid/payload/metadata.proto b/microdroid/payload/metadata.proto
index 06cbbf4..1d8dd8c 100644
--- a/microdroid/payload/metadata.proto
+++ b/microdroid/payload/metadata.proto
@@ -67,9 +67,4 @@
   // Required.
   // Path to the payload binary file inside the APK.
   string payload_binary_path = 1;
-
-  // Optional.
-  // Arguments to be passed to the payload.
-  // TODO(b/249064104): Remove this
-  repeated string args = 2;
 }
diff --git a/microdroid/vm_payload/Android.bp b/microdroid/vm_payload/Android.bp
index a68595f..925928e 100644
--- a/microdroid/vm_payload/Android.bp
+++ b/microdroid/vm_payload/Android.bp
@@ -31,3 +31,8 @@
         "libvm_payload",
     ],
 }
+
+cc_library_headers {
+    name: "vm_payload_headers",
+    export_include_dirs: ["include"],
+}
diff --git a/microdroid/vm_payload/include/vm_main.h b/microdroid/vm_payload/include/vm_main.h
new file mode 100644
index 0000000..e351174
--- /dev/null
+++ b/microdroid/vm_payload/include/vm_main.h
@@ -0,0 +1,27 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+typedef int AVmPayload_main_t();
+AVmPayload_main_t AVmPayload_main;
+}
+#else
+typedef int AVmPayload_main_t(void);
+extern int AVmPayload_main(void);
+#endif
diff --git a/microdroid/vm_payload/include/vm_payload.h b/microdroid/vm_payload/include/vm_payload.h
index 05abdce..4ed07b8 100644
--- a/microdroid/vm_payload/include/vm_payload.h
+++ b/microdroid/vm_payload/include/vm_payload.h
@@ -19,6 +19,8 @@
 #include <stdbool.h>
 #include <stddef.h>
 
+#include "vm_main.h"
+
 #ifdef __cplusplus
 extern "C" {
 #endif
diff --git a/microdroid_manager/src/main.rs b/microdroid_manager/src/main.rs
index 4326f5b..748b497 100644
--- a/microdroid_manager/src/main.rs
+++ b/microdroid_manager/src/main.rs
@@ -278,7 +278,6 @@
     // }
     // PayloadConfig = {
     //   1: tstr // payload_binary_path
-    //   // TODO(b/249064104 Either include args, or deprecate them
     // }
 
     let mut config_desc = vec![
@@ -738,7 +737,6 @@
             let task = Task {
                 type_: TaskType::MicrodroidLauncher,
                 command: payload_config.payload_binary_path,
-                args: payload_config.args.into_vec(),
             };
             Ok(VmPayloadConfig {
                 os: OsConfig { name: "microdroid".to_owned() },
@@ -784,14 +782,10 @@
     const VMADDR_CID_HOST: u32 = 2;
 
     let mut command = match task.type_ {
-        TaskType::Executable => {
-            let mut command = Command::new(&task.command);
-            command.args(&task.args);
-            command
-        }
+        TaskType::Executable => Command::new(&task.command),
         TaskType::MicrodroidLauncher => {
             let mut command = Command::new("/system/bin/microdroid_launcher");
-            command.arg(find_library_path(&task.command)?).args(&task.args);
+            command.arg(find_library_path(&task.command)?);
             command
         }
     };
diff --git a/tests/benchmark/Android.bp b/tests/benchmark/Android.bp
index e3f4685..88e4d41 100644
--- a/tests/benchmark/Android.bp
+++ b/tests/benchmark/Android.bp
@@ -30,6 +30,7 @@
 cc_library_shared {
     name: "MicrodroidIdleNativeLib",
     srcs: ["src/native/idlebinary.cpp"],
+    header_libs: ["vm_payload_headers"],
     shared_libs: [
         "libbase",
     ],
diff --git a/tests/benchmark/src/native/benchmarkbinary.cpp b/tests/benchmark/src/native/benchmarkbinary.cpp
index 4e3c14d..842d713 100644
--- a/tests/benchmark/src/native/benchmarkbinary.cpp
+++ b/tests/benchmark/src/native/benchmarkbinary.cpp
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <time.h>
 #include <unistd.h>
+#include <vm_main.h>
 #include <vm_payload.h>
 
 #include <binder_rpc_unstable.hpp>
@@ -172,7 +173,7 @@
 }
 } // Anonymous namespace
 
-extern "C" int android_native_main(int /* argc */, char* /* argv */[]) {
+extern "C" int AVmPayload_main() {
     if (auto res = run_io_benchmark_tests(); !res.ok()) {
         LOG(ERROR) << "IO benchmark test failed: " << res.error() << "\n";
         return EXIT_FAILURE;
diff --git a/tests/benchmark/src/native/idlebinary.cpp b/tests/benchmark/src/native/idlebinary.cpp
index a74e7bf..9499d94 100644
--- a/tests/benchmark/src/native/idlebinary.cpp
+++ b/tests/benchmark/src/native/idlebinary.cpp
@@ -15,8 +15,9 @@
  */
 
 #include <unistd.h>
+#include <vm_main.h>
 
-extern "C" int android_native_main(int /* argc */, char* /* argv */[]) {
+extern "C" int AVmPayload_main() {
     // do nothing; just leave it alive. good night.
     for (;;) {
         sleep(1000);
diff --git a/tests/testapk/Android.bp b/tests/testapk/Android.bp
index e738930..bb17058 100644
--- a/tests/testapk/Android.bp
+++ b/tests/testapk/Android.bp
@@ -51,6 +51,7 @@
 
 cc_library_shared {
     name: "MicrodroidTestNativeCrashLib",
+    header_libs: ["vm_payload_headers"],
     srcs: ["src/native/crashbinary.cpp"],
 }
 
diff --git a/tests/testapk/src/native/crashbinary.cpp b/tests/testapk/src/native/crashbinary.cpp
index 9f80fd0..a0edc40 100644
--- a/tests/testapk/src/native/crashbinary.cpp
+++ b/tests/testapk/src/native/crashbinary.cpp
@@ -17,8 +17,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "vm_main.h"
+
 // A VM payload that crashes as soon as it starts, to allow us to exercise that error path.
-extern "C" int android_native_main(int /* argc */, char* /* argv */[]) {
+extern "C" int AVmPayload_main() {
     printf("test crash!!!!\n");
     abort();
 }
diff --git a/tests/testapk/src/native/testbinary.cpp b/tests/testapk/src/native/testbinary.cpp
index 003aca8..d810e3c 100644
--- a/tests/testapk/src/native/testbinary.cpp
+++ b/tests/testapk/src/native/testbinary.cpp
@@ -27,6 +27,7 @@
 #include <sys/ioctl.h>
 #include <sys/system_properties.h>
 #include <unistd.h>
+#include <vm_main.h>
 #include <vm_payload.h>
 
 #include <binder_rpc_unstable.hpp>
@@ -146,20 +147,13 @@
 
 } // Anonymous namespace
 
-extern "C" int android_native_main(int argc, char* argv[]) {
+extern "C" int AVmPayload_main() {
     // disable buffering to communicate seamlessly
     setvbuf(stdin, nullptr, _IONBF, 0);
     setvbuf(stdout, nullptr, _IONBF, 0);
     setvbuf(stderr, nullptr, _IONBF, 0);
 
-    printf("Hello Microdroid ");
-    for (int i = 0; i < argc; i++) {
-        printf("%s", argv[i]);
-        bool last = i == (argc - 1);
-        if (!last) {
-            printf(" ");
-        }
-    }
+    printf("Hello Microdroid");
     testlib_sub();
     printf("\n");
 
diff --git a/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachinePayloadConfig.aidl b/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachinePayloadConfig.aidl
index 4d37848..ed3c512 100644
--- a/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachinePayloadConfig.aidl
+++ b/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachinePayloadConfig.aidl
@@ -22,10 +22,4 @@
      * defined entry point; inside the VM this file is loaded and the entry function invoked.
      */
     @utf8InCpp String payloadPath;
-
-    /**
-     * Command-line style arguments to be passed to the payload when it is executed.
-     * TODO(b/249064104): Remove this
-     */
-    @utf8InCpp String[] args;
 }
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index eb2bac4..bff4f36 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -661,11 +661,8 @@
     // There isn't an actual config file. Construct a synthetic VmPayloadConfig from the explicit
     // parameters we've been given. Microdroid will do something equivalent inside the VM using the
     // payload config that we send it via the metadata file.
-    let task = Task {
-        type_: TaskType::MicrodroidLauncher,
-        command: payload_config.payloadPath.clone(),
-        args: payload_config.args.clone(),
-    };
+    let task =
+        Task { type_: TaskType::MicrodroidLauncher, command: payload_config.payloadPath.clone() };
     VmPayloadConfig {
         os: OsConfig { name: MICRODROID_OS_NAME.to_owned() },
         task: Some(task),
diff --git a/virtualizationservice/src/payload.rs b/virtualizationservice/src/payload.rs
index b6df500..4190fbb 100644
--- a/virtualizationservice/src/payload.rs
+++ b/virtualizationservice/src/payload.rs
@@ -165,7 +165,6 @@
     let payload_metadata = match &app_config.payload {
         Payload::PayloadConfig(payload_config) => PayloadMetadata::config(PayloadConfig {
             payload_binary_path: payload_config.payloadPath.clone(),
-            args: payload_config.args.clone().into(),
             ..Default::default()
         }),
         Payload::ConfigPath(config_path) => {