Add microdroid_launcher
microdroid_launcher is a very thin executable that loads a shared
library and execute the entry function (android_native_main). This is
used to execute a shared library in an APK that is shared from the host
Android.
Bug: 188513012
Test: atest MicrodroidHostTestCases
Change-Id: I0f98eba4c72601adaa27733dae6a51d3852301db
diff --git a/launcher/Android.bp b/launcher/Android.bp
new file mode 100644
index 0000000..2c3f093
--- /dev/null
+++ b/launcher/Android.bp
@@ -0,0 +1,9 @@
+package {
+ default_applicable_licenses: ["Android-Apache-2.0"],
+}
+
+cc_binary {
+ name: "microdroid_launcher",
+ srcs: ["main.cpp"],
+ shared_libs: ["libdl"],
+}
diff --git a/launcher/main.cpp b/launcher/main.cpp
new file mode 100644
index 0000000..fc9477d
--- /dev/null
+++ b/launcher/main.cpp
@@ -0,0 +1,44 @@
+/*
+ * Copyright (C) 2021 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.
+ */
+
+#include <dlfcn.h>
+
+#include <cstdlib>
+#include <iostream>
+
+int main(int argc, char* argv[]) {
+ if (argc < 2) {
+ std::cout << "Usage:\n";
+ std::cout << " " << argv[0] << " LIBNAME [ARGS...]\n";
+ return EXIT_FAILURE;
+ }
+
+ const char* libname = argv[1];
+ void* handle = dlopen(libname, RTLD_NOW);
+ if (handle == nullptr) {
+ std::cerr << "Failed to load " << libname << ": " << dlerror() << "\n";
+ return EXIT_FAILURE;
+ }
+
+ int (*entry)(int argc, char* argv[]) = nullptr;
+ entry = reinterpret_cast<decltype(entry)>(dlsym(handle, "android_native_main"));
+ if (entry == nullptr) {
+ std::cerr << "Failed to find entrypoint `android_native_main`: " << dlerror() << "\n";
+ return EXIT_FAILURE;
+ }
+
+ return entry(argc - 1, argv + 1);
+}
diff --git a/microdroid/Android.bp b/microdroid/Android.bp
index 1fc1b7a..98b2f98 100644
--- a/microdroid/Android.bp
+++ b/microdroid/Android.bp
@@ -45,8 +45,9 @@
partition_name: "system",
deps: [
"init_second_stage",
- "microdroid_init_rc",
"microdroid_build_prop",
+ "microdroid_init_rc",
+ "microdroid_launcher",
"ueventd.rc",
"libbinder",
"libbinder_ndk",
diff --git a/microdroid/microdroid_file_contexts b/microdroid/microdroid_file_contexts
index 369c700..b033110 100644
--- a/microdroid/microdroid_file_contexts
+++ b/microdroid/microdroid_file_contexts
@@ -367,6 +367,7 @@
/system/bin/android\.frameworks\.automotive\.display@1\.0-service u:object_r:automotive_display_service_exec:s0
/system/bin/snapuserd u:object_r:snapuserd_exec:s0
/system/bin/zipfuse u:object_r:zipfuse_exec:s0
+/system/bin/microdroid_launcher u:object_r:microdroid_launcher_exec:s0
#############################
# Vendor files
diff --git a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
index 9419541..ea1cf7c 100644
--- a/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
+++ b/tests/hostside/java/android/virt/test/MicrodroidTestCase.java
@@ -178,9 +178,15 @@
executeCommandOnMicrodroid("shell mount"),
containsString("zipfuse on /mnt/apk type fuse.zipfuse"));
+ final String libPath = "/mnt/apk/lib/x86_64/MicrodroidTestNativeLib.so";
assertThat(
- executeCommandOnMicrodroid("shell ls /mnt/apk/classes.dex"),
- is("/mnt/apk/classes.dex"));
+ executeCommandOnMicrodroid("shell ls " + libPath),
+ is(libPath));
+
+ assertThat(
+ executeCommandOnMicrodroid("shell /system/bin/microdroid_launcher " + libPath
+ + " arg1 arg2"),
+ is("Hello Microdroid " + libPath + " arg1 arg2"));
// Shutdown microdroid
executeCommand("adb -s localhost:" + TEST_VM_ADB_PORT + " shell reboot");
diff --git a/tests/testapk/src/native/testbinary.cpp b/tests/testapk/src/native/testbinary.cpp
index c315bf1..c317cd2 100644
--- a/tests/testapk/src/native/testbinary.cpp
+++ b/tests/testapk/src/native/testbinary.cpp
@@ -15,6 +15,15 @@
*/
#include <stdio.h>
-void say_hello() {
- printf("Hello Microdroid\n");
+extern "C" int android_native_main(int argc, char* argv[]) {
+ printf("Hello Microdroid ");
+ for (int i = 0; i < argc; i++) {
+ printf("%s", argv[i]);
+ bool last = i == (argc - 1);
+ if (!last) {
+ printf(" ");
+ }
+ }
+ printf("\n");
+ return 0;
}