Merge "Add libminijail to the compos APEX" into main
diff --git a/Android.bp b/Android.bp
index 7cedfb7..dcf67dd 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,6 +30,7 @@
"release_avf_enable_remote_attestation",
"release_avf_enable_vendor_modules",
"release_avf_enable_virt_cpufreq",
+ "release_avf_support_custom_vm_with_paravirtualized_devices",
],
properties: [
"cfgs",
@@ -60,6 +61,9 @@
release_avf_enable_virt_cpufreq: {
cfgs: ["virt_cpufreq"],
},
+ release_avf_support_custom_vm_with_paravirtualized_devices: {
+ cfgs: ["paravirtualized_devices"],
+ },
},
}
@@ -69,6 +73,7 @@
config_namespace: "ANDROID",
bool_variables: [
"release_avf_enable_dice_changes",
+ "release_avf_enable_vendor_modules",
"release_avf_enable_virt_cpufreq",
],
properties: [
@@ -82,6 +87,9 @@
release_avf_enable_dice_changes: {
cflags: ["-DAVF_OPEN_DICE_CHANGES=1"],
},
+ release_avf_enable_vendor_modules: {
+ cflags: ["-DAVF_ENABLE_VENDOR_MODULES=1"],
+ },
release_avf_enable_virt_cpufreq: {
cflags: ["-DAVF_ENABLE_VIRT_CPUFREQ=1"],
},
diff --git a/TEST_MAPPING b/TEST_MAPPING
index 58dcc06..1c4f5ca 100644
--- a/TEST_MAPPING
+++ b/TEST_MAPPING
@@ -36,15 +36,9 @@
"name": "initrd_bootconfig.test"
},
{
- "name": "libdice_policy.test"
- },
- {
"name": "libapkzip.test"
},
{
- "name": "libsecretkeeper_comm.test"
- },
- {
"name": "libdice_driver_test"
}
],
diff --git a/apex/canned_fs_config_sys_nice b/apex/canned_fs_config_sys_nice
index 5b12eb5..90c9747 100644
--- a/apex/canned_fs_config_sys_nice
+++ b/apex/canned_fs_config_sys_nice
@@ -1,2 +1,3 @@
/bin/virtualizationservice 0 2000 0755 capabilities=0x1000001 # CAP_CHOWN, CAP_SYS_RESOURCE
-/bin/crosvm 0 3013 0755 capabilities=0x800000 # SYS_NICE
+/bin/crosvm 0 3013 0755 capabilities=0x800000 # CAP_SYS_NICE
+/bin/virtmgr 0 3013 0755 capabilities=0x800000 # CAP_SYS_NICE
diff --git a/authfs/fd_server/src/aidl.rs b/authfs/fd_server/src/aidl.rs
index 8edd899..5f91987 100644
--- a/authfs/fd_server/src/aidl.rs
+++ b/authfs/fd_server/src/aidl.rs
@@ -289,7 +289,7 @@
FdConfig::OutputDir(dir) => {
let mode = validate_file_mode(mode)?;
let new_fd = openat(
- dir.as_raw_fd(),
+ Some(dir.as_raw_fd()),
basename,
// This function is supposed to be only called when FUSE/authfs thinks the file
// does not exist. However, if the file does exist from the view of fd_server
@@ -319,10 +319,14 @@
FdConfig::InputDir(_) => Err(new_errno_error(Errno::EACCES)),
FdConfig::OutputDir(_) => {
let mode = validate_file_mode(mode)?;
- mkdirat(dir_fd, basename, mode).map_err(new_errno_error)?;
- let new_dir_fd =
- openat(dir_fd, basename, OFlag::O_DIRECTORY | OFlag::O_RDONLY, Mode::empty())
- .map_err(new_errno_error)?;
+ mkdirat(Some(dir_fd), basename, mode).map_err(new_errno_error)?;
+ let new_dir_fd = openat(
+ Some(dir_fd),
+ basename,
+ OFlag::O_DIRECTORY | OFlag::O_RDONLY,
+ Mode::empty(),
+ )
+ .map_err(new_errno_error)?;
// SAFETY: new_dir_fd is just created and not an error.
let fd_owner = unsafe { OwnedFd::from_raw_fd(new_dir_fd) };
Ok((new_dir_fd, FdConfig::OutputDir(fd_owner)))
@@ -403,7 +407,7 @@
}
fn open_readonly_at(dir_fd: BorrowedFd, path: &Path) -> nix::Result<File> {
- let new_fd = openat(dir_fd.as_raw_fd(), path, OFlag::O_RDONLY, Mode::empty())?;
+ let new_fd = openat(Some(dir_fd.as_raw_fd()), path, OFlag::O_RDONLY, Mode::empty())?;
// SAFETY: new_fd is just created successfully and not owned.
let new_file = unsafe { File::from_raw_fd(new_fd) };
Ok(new_file)
diff --git a/compos/composd/src/fd_server_helper.rs b/compos/composd/src/fd_server_helper.rs
index 24371b5..91860ab 100644
--- a/compos/composd/src/fd_server_helper.rs
+++ b/compos/composd/src/fd_server_helper.rs
@@ -23,7 +23,7 @@
use nix::unistd::pipe2;
use std::fs::File;
use std::io::Read;
-use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd};
+use std::os::unix::io::{AsRawFd, OwnedFd};
use std::path::Path;
const FD_SERVER_BIN: &str = "/apex/com.android.virt/bin/fd_server";
@@ -106,12 +106,8 @@
}
fn create_pipe() -> Result<(File, File)> {
- let (raw_read, raw_write) = pipe2(OFlag::O_CLOEXEC)?;
- // SAFETY: We are the sole owner of raw_read and it is valid as it was just created.
- let read_fd = unsafe { File::from_raw_fd(raw_read) };
- // SAFETY: We are the sole owner of raw_write and it is valid as it was just created.
- let write_fd = unsafe { File::from_raw_fd(raw_write) };
- Ok((read_fd, write_fd))
+ let (read_fd, write_fd) = pipe2(OFlag::O_CLOEXEC)?;
+ Ok((read_fd.into(), write_fd.into()))
}
fn wait_for_fd_server_ready(mut ready_fd: File) -> Result<()> {
diff --git a/flags/cpp/include/android/avf_cc_flags.h b/flags/cpp/include/android/avf_cc_flags.h
index 536ea9f..c922266 100644
--- a/flags/cpp/include/android/avf_cc_flags.h
+++ b/flags/cpp/include/android/avf_cc_flags.h
@@ -27,5 +27,13 @@
#endif
}
+inline bool IsVendorModulesFlagEnabled() {
+#ifdef AVF_ENABLE_VENDOR_MODULES
+ return AVF_ENABLE_VENDOR_MODULES;
+#else
+ return false;
+#endif
+}
+
} // namespace virtualization
} // namespace android
diff --git a/java/framework/src/android/system/virtualmachine/VirtualMachine.java b/java/framework/src/android/system/virtualmachine/VirtualMachine.java
index cc126eb..1076219 100644
--- a/java/framework/src/android/system/virtualmachine/VirtualMachine.java
+++ b/java/framework/src/android/system/virtualmachine/VirtualMachine.java
@@ -63,11 +63,14 @@
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.ServiceSpecificException;
+import android.view.KeyEvent;
+import android.view.MotionEvent;
import android.system.virtualizationcommon.DeathReason;
import android.system.virtualizationcommon.ErrorCode;
import android.system.virtualizationservice.IVirtualMachine;
import android.system.virtualizationservice.IVirtualMachineCallback;
import android.system.virtualizationservice.IVirtualizationService;
+import android.system.virtualizationservice.InputDevice;
import android.system.virtualizationservice.MemoryTrimLevel;
import android.system.virtualizationservice.PartitionType;
import android.system.virtualizationservice.VirtualMachineAppConfig;
@@ -79,6 +82,8 @@
import com.android.internal.annotations.GuardedBy;
import com.android.system.virtualmachine.flags.Flags;
+import libcore.io.IoBridge;
+
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -89,6 +94,8 @@
import java.io.OutputStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileVisitResult;
@@ -96,6 +103,7 @@
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -154,6 +162,9 @@
@SuppressLint("MinMaxConstant") // Won't change: see man 7 vsock.
public static final long MAX_VSOCK_PORT = (1L << 32) - 1;
+ private ParcelFileDescriptor mTouchSock;
+ private ParcelFileDescriptor mKeySock;
+
/**
* Status of a virtual machine
*
@@ -849,9 +860,102 @@
createVirtualMachineConfigForRawFrom(VirtualMachineConfig vmConfig)
throws IllegalStateException, IOException {
VirtualMachineRawConfig rawConfig = vmConfig.toVsRawConfig();
+
+ // Handle input devices here
+ List<InputDevice> inputDevices = new ArrayList<>();
+ if (vmConfig.getCustomImageConfig() != null
+ && rawConfig.displayConfig != null) {
+ if (vmConfig.getCustomImageConfig().useTouch()) {
+ ParcelFileDescriptor[] pfds = ParcelFileDescriptor.createSocketPair();
+ mTouchSock = pfds[0];
+ InputDevice.SingleTouch t = new InputDevice.SingleTouch();
+ t.width = rawConfig.displayConfig.width;
+ t.height = rawConfig.displayConfig.height;
+ t.pfd = pfds[1];
+ inputDevices.add(InputDevice.singleTouch(t));
+ }
+ if (vmConfig.getCustomImageConfig().useKeyboard()) {
+ ParcelFileDescriptor[] pfds = ParcelFileDescriptor.createSocketPair();
+ mKeySock = pfds[0];
+ InputDevice.Keyboard k = new InputDevice.Keyboard();
+ k.pfd = pfds[1];
+ inputDevices.add(InputDevice.keyboard(k));
+ }
+ }
+ rawConfig.inputDevices = inputDevices.toArray(new InputDevice[0]);
+
return android.system.virtualizationservice.VirtualMachineConfig.rawConfig(rawConfig);
}
+ private static record InputEvent(short type, short code, int value) {}
+
+ /** @hide */
+ public boolean sendKeyEvent(KeyEvent event) {
+ if (mKeySock == null) {
+ Log.d(TAG, "mKeySock == null");
+ return false;
+ }
+ // from include/uapi/linux/input-event-codes.h in the kernel.
+ short EV_SYN = 0x00;
+ short EV_KEY = 0x01;
+ short SYN_REPORT = 0x00;
+ boolean down = event.getAction() != MotionEvent.ACTION_UP;
+
+ return writeEventsToSock(
+ mKeySock,
+ Arrays.asList(
+ new InputEvent(EV_KEY, (short) event.getScanCode(), down ? 1 : 0),
+ new InputEvent(EV_SYN, SYN_REPORT, 0)));
+ }
+
+ /** @hide */
+ public boolean sendSingleTouchEvent(MotionEvent event) {
+ if (mTouchSock == null) {
+ Log.d(TAG, "mTouchSock == null");
+ return false;
+ }
+ // from include/uapi/linux/input-event-codes.h in the kernel.
+ short EV_SYN = 0x00;
+ short EV_ABS = 0x03;
+ short EV_KEY = 0x01;
+ short BTN_TOUCH = 0x14a;
+ short ABS_X = 0x00;
+ short ABS_Y = 0x01;
+ short SYN_REPORT = 0x00;
+
+ int x = (int) event.getX();
+ int y = (int) event.getY();
+ boolean down = event.getAction() != MotionEvent.ACTION_UP;
+
+ return writeEventsToSock(
+ mTouchSock,
+ Arrays.asList(
+ new InputEvent(EV_ABS, ABS_X, x),
+ new InputEvent(EV_ABS, ABS_Y, y),
+ new InputEvent(EV_KEY, BTN_TOUCH, down ? 1 : 0),
+ new InputEvent(EV_SYN, SYN_REPORT, 0)));
+ }
+
+ private boolean writeEventsToSock(ParcelFileDescriptor sock, List<InputEvent> evtList) {
+ ByteBuffer byteBuffer =
+ ByteBuffer.allocate(8 /* (type: u16 + code: u16 + value: i32) */ * evtList.size());
+ byteBuffer.clear();
+ byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
+ for (InputEvent e : evtList) {
+ byteBuffer.putShort(e.type);
+ byteBuffer.putShort(e.code);
+ byteBuffer.putInt(e.value);
+ }
+ try {
+ IoBridge.write(
+ sock.getFileDescriptor(), byteBuffer.array(), 0, byteBuffer.array().length);
+ } catch (IOException e) {
+ Log.d(TAG, "cannot send event", e);
+ return false;
+ }
+ return true;
+ }
+
private android.system.virtualizationservice.VirtualMachineConfig
createVirtualMachineConfigForAppFrom(
VirtualMachineConfig vmConfig, IVirtualizationService service)
diff --git a/java/framework/src/android/system/virtualmachine/VirtualMachineCustomImageConfig.java b/java/framework/src/android/system/virtualmachine/VirtualMachineCustomImageConfig.java
index 89df1f2..8d294fd 100644
--- a/java/framework/src/android/system/virtualmachine/VirtualMachineCustomImageConfig.java
+++ b/java/framework/src/android/system/virtualmachine/VirtualMachineCustomImageConfig.java
@@ -34,6 +34,8 @@
private static final String KEY_DISK_WRITABLES = "disk_writables";
private static final String KEY_DISK_IMAGES = "disk_images";
private static final String KEY_DISPLAY_CONFIG = "display_config";
+ private static final String KEY_TOUCH = "touch";
+ private static final String KEY_KEYBOARD = "keyboard";
@Nullable private final String name;
@NonNull private final String kernelPath;
@@ -42,6 +44,8 @@
@Nullable private final String[] params;
@Nullable private final Disk[] disks;
@Nullable private final DisplayConfig displayConfig;
+ private final boolean touch;
+ private final boolean keyboard;
@Nullable
public Disk[] getDisks() {
@@ -73,6 +77,14 @@
return params;
}
+ public boolean useTouch() {
+ return touch;
+ }
+
+ public boolean useKeyboard() {
+ return keyboard;
+ }
+
/** @hide */
public VirtualMachineCustomImageConfig(
String name,
@@ -81,7 +93,9 @@
String bootloaderPath,
String[] params,
Disk[] disks,
- DisplayConfig displayConfig) {
+ DisplayConfig displayConfig,
+ boolean touch,
+ boolean keyboard) {
this.name = name;
this.kernelPath = kernelPath;
this.initrdPath = initrdPath;
@@ -89,6 +103,8 @@
this.params = params;
this.disks = disks;
this.displayConfig = displayConfig;
+ this.touch = touch;
+ this.keyboard = keyboard;
}
static VirtualMachineCustomImageConfig from(PersistableBundle customImageConfigBundle) {
@@ -116,7 +132,8 @@
PersistableBundle displayConfigPb =
customImageConfigBundle.getPersistableBundle(KEY_DISPLAY_CONFIG);
builder.setDisplayConfig(DisplayConfig.from(displayConfigPb));
-
+ builder.useTouch(customImageConfigBundle.getBoolean(KEY_TOUCH));
+ builder.useKeyboard(customImageConfigBundle.getBoolean(KEY_KEYBOARD));
return builder.build();
}
@@ -145,6 +162,8 @@
Optional.ofNullable(displayConfig)
.map(dc -> dc.toPersistableBundle())
.orElse(null));
+ pb.putBoolean(KEY_TOUCH, touch);
+ pb.putBoolean(KEY_KEYBOARD, keyboard);
return pb;
}
@@ -193,6 +212,8 @@
private List<String> params = new ArrayList<>();
private List<Disk> disks = new ArrayList<>();
private DisplayConfig displayConfig;
+ private boolean touch;
+ private boolean keyboard;
/** @hide */
public Builder() {}
@@ -240,6 +261,18 @@
}
/** @hide */
+ public Builder useTouch(boolean touch) {
+ this.touch = touch;
+ return this;
+ }
+
+ /** @hide */
+ public Builder useKeyboard(boolean keyboard) {
+ this.keyboard = keyboard;
+ return this;
+ }
+
+ /** @hide */
public VirtualMachineCustomImageConfig build() {
return new VirtualMachineCustomImageConfig(
this.name,
@@ -248,7 +281,9 @@
this.bootloaderPath,
this.params.toArray(new String[0]),
this.disks.toArray(new Disk[0]),
- displayConfig);
+ displayConfig,
+ touch,
+ keyboard);
}
}
diff --git a/java/framework/src/android/system/virtualmachine/VirtualMachineManager.java b/java/framework/src/android/system/virtualmachine/VirtualMachineManager.java
index 8c0c20e..4a9e943 100644
--- a/java/framework/src/android/system/virtualmachine/VirtualMachineManager.java
+++ b/java/framework/src/android/system/virtualmachine/VirtualMachineManager.java
@@ -38,9 +38,11 @@
import com.android.internal.annotations.GuardedBy;
import com.android.system.virtualmachine.flags.Flags;
+import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.ref.WeakReference;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@@ -357,6 +359,30 @@
return null;
}
+ private static final String JSON_SUFFIX = ".json";
+ private static final List<String> SUPPORTED_OS_LIST_FROM_CFG =
+ extractSupportedOSListFromConfig();
+
+ private boolean isVendorModuleEnabled() {
+ return VirtualizationService.nativeIsVendorModulesFlagEnabled();
+ }
+
+ private static List<String> extractSupportedOSListFromConfig() {
+ List<String> supportedOsList = new ArrayList<>();
+ File directory = new File("/apex/com.android.virt/etc");
+ File[] files = directory.listFiles();
+ if (files != null) {
+ for (File file : files) {
+ String fileName = file.getName();
+ if (fileName.endsWith(JSON_SUFFIX)) {
+ supportedOsList.add(
+ fileName.substring(0, fileName.length() - JSON_SUFFIX.length()));
+ }
+ }
+ }
+ return supportedOsList;
+ }
+
/**
* Returns a list of supported OS names.
*
@@ -366,13 +392,10 @@
@FlaggedApi(Flags.FLAG_AVF_V_TEST_APIS)
@NonNull
public List<String> getSupportedOSList() throws VirtualMachineException {
- synchronized (sCreateLock) {
- VirtualizationService service = VirtualizationService.getInstance();
- try {
- return Arrays.asList(service.getBinder().getSupportedOSList());
- } catch (RemoteException e) {
- throw e.rethrowAsRuntimeException();
- }
+ if (isVendorModuleEnabled()) {
+ return SUPPORTED_OS_LIST_FROM_CFG;
+ } else {
+ return Arrays.asList("microdroid");
}
}
diff --git a/java/framework/src/android/system/virtualmachine/VirtualizationService.java b/java/framework/src/android/system/virtualmachine/VirtualizationService.java
index 57990a9..83b64ee 100644
--- a/java/framework/src/android/system/virtualmachine/VirtualizationService.java
+++ b/java/framework/src/android/system/virtualmachine/VirtualizationService.java
@@ -51,6 +51,12 @@
private native boolean nativeIsOk(int clientFd);
/*
+ * Retrieve boolean value whether RELEASE_AVF_ENABLE_VENDOR_MODULES build flag is enabled or
+ * not.
+ */
+ static native boolean nativeIsVendorModulesFlagEnabled();
+
+ /*
* Spawns a new virtmgr subprocess that will host a VirtualizationService
* AIDL service.
*/
diff --git a/java/jni/Android.bp b/java/jni/Android.bp
index 74a1766..4a569d4 100644
--- a/java/jni/Android.bp
+++ b/java/jni/Android.bp
@@ -16,6 +16,7 @@
"liblog",
"libnativehelper",
],
+ static_libs: ["libavf_cc_flags"],
}
cc_library_shared {
diff --git a/java/jni/android_system_virtualmachine_VirtualizationService.cpp b/java/jni/android_system_virtualmachine_VirtualizationService.cpp
index fbd1fd5..ced2079 100644
--- a/java/jni/android_system_virtualmachine_VirtualizationService.cpp
+++ b/java/jni/android_system_virtualmachine_VirtualizationService.cpp
@@ -17,6 +17,7 @@
#define LOG_TAG "VirtualizationService"
#include <android-base/unique_fd.h>
+#include <android/avf_cc_flags.h>
#include <android/binder_ibinder_jni.h>
#include <jni.h>
#include <log/log.h>
@@ -66,10 +67,16 @@
// Wait for the server to signal its readiness by closing its end of the pipe.
char buf;
- if (read(waitFd.get(), &buf, sizeof(buf)) < 0) {
+ int ret = read(waitFd.get(), &buf, sizeof(buf));
+ if (ret < 0) {
env->ThrowNew(env->FindClass("android/system/virtualmachine/VirtualMachineException"),
"Failed to wait for VirtualizationService to be ready");
return -1;
+ } else if (ret < 1) {
+ env->ThrowNew(env->FindClass("java/lang/SecurityException"),
+ "Virtmgr didn't send any data through pipe. Please consider checking if "
+ "android.permission.MANAGE_VIRTUAL_MACHINE permission is granted");
+ return -1;
}
return clientFd.release();
@@ -101,3 +108,9 @@
}
return pfds[0].revents == 0;
}
+
+extern "C" JNIEXPORT jboolean JNICALL
+Java_android_system_virtualmachine_VirtualizationService_nativeIsVendorModulesFlagEnabled(
+ [[maybe_unused]] JNIEnv* env, [[maybe_unused]] jobject obj) {
+ return android::virtualization::IsVendorModulesFlagEnabled();
+}
diff --git a/libs/android_display_backend/Android.bp b/libs/android_display_backend/Android.bp
index f792a04..32587dd 100644
--- a/libs/android_display_backend/Android.bp
+++ b/libs/android_display_backend/Android.bp
@@ -45,7 +45,6 @@
"android.system.virtualizationservice_internal-ndk",
"android.system.virtualizationcommon-ndk",
"android.system.virtualizationservice-ndk",
- "libyuv",
],
shared_libs: [
"libbinder_ndk",
diff --git a/libs/android_display_backend/crosvm_android_display_client.cpp b/libs/android_display_backend/crosvm_android_display_client.cpp
index a16b7f2..66320f3 100644
--- a/libs/android_display_backend/crosvm_android_display_client.cpp
+++ b/libs/android_display_backend/crosvm_android_display_client.cpp
@@ -18,38 +18,27 @@
#include <aidl/android/system/virtualizationservice_internal/IVirtualizationServiceInternal.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
-#include <android/native_window.h>
-#include <android/native_window_aidl.h>
-#include <libyuv.h>
-#include <stdint.h>
-#include <utils/Errors.h>
+#include <system/graphics.h> // for HAL_PIXEL_FORMAT_*
#include <condition_variable>
#include <memory>
#include <mutex>
-#include <vector>
+using aidl::android::crosvm::BnCrosvmAndroidDisplayService;
using aidl::android::system::virtualizationservice_internal::IVirtualizationServiceInternal;
-
-#define LIBEXPORT __attribute__((visibility("default"))) extern "C"
-
-typedef void (*android_display_log_callback_type)(const char* message);
-
-static void android_display_log_callback_stub(const char* message) {
- (void)message;
-}
+using aidl::android::view::Surface;
namespace {
-class DisplayService : public aidl::android::crosvm::BnCrosvmAndroidDisplayService {
+class DisplayService : public BnCrosvmAndroidDisplayService {
public:
DisplayService() = default;
virtual ~DisplayService() = default;
- ndk::ScopedAStatus setSurface(aidl::android::view::Surface* surface) override {
+ ndk::ScopedAStatus setSurface(Surface* surface) override {
{
std::lock_guard lk(mSurfaceReadyMutex);
- mSurface = std::make_unique<aidl::android::view::Surface>(surface->release());
+ mSurface = std::make_unique<Surface>(surface->release());
}
mSurfaceReady.notify_one();
return ::ndk::ScopedAStatus::ok();
@@ -64,7 +53,7 @@
return ::ndk::ScopedAStatus::ok();
}
- aidl::android::view::Surface* getSurface() {
+ Surface* getSurface() {
std::unique_lock lk(mSurfaceReadyMutex);
mSurfaceReady.wait(lk, [this] { return mSurface != nullptr; });
return mSurface.get();
@@ -73,214 +62,116 @@
private:
std::condition_variable mSurfaceReady;
std::mutex mSurfaceReadyMutex;
- std::unique_ptr<aidl::android::view::Surface> mSurface;
+ std::unique_ptr<Surface> mSurface;
};
-void ErrorF(android_display_log_callback_type error_callback, const char* format, ...) {
- char buffer[1024];
-
- va_list vararg;
- va_start(vararg, format);
- vsnprintf(buffer, sizeof(buffer), format, vararg);
- va_end(vararg);
-
- error_callback(buffer);
-}
-
} // namespace
-struct android_display_context {
- uint32_t width;
- uint32_t height;
- std::shared_ptr<DisplayService> displayService;
+typedef void (*ErrorCallback)(const char* message);
+
+struct AndroidDisplayContext {
+ std::shared_ptr<IVirtualizationServiceInternal> virt_service;
+ std::shared_ptr<DisplayService> disp_service;
+ ErrorCallback error_callback;
+
+ AndroidDisplayContext(ErrorCallback cb) : error_callback(cb) {
+ auto disp_service = ::ndk::SharedRefBase::make<DisplayService>();
+
+ // Creates DisplayService and register it to the virtualizationservice. This is needed
+ // because this code is executed inside of crosvm which runs as an app. Apps are not allowed
+ // to register a service to the service manager.
+ auto virt_service = IVirtualizationServiceInternal::fromBinder(ndk::SpAIBinder(
+ AServiceManager_waitForService("android.system.virtualizationservice")));
+ if (virt_service == nullptr) {
+ errorf("Failed to find virtualization service");
+ return;
+ }
+ auto status = virt_service->setDisplayService(disp_service->asBinder());
+ if (!status.isOk()) {
+ errorf("Failed to register display service");
+ return;
+ }
+
+ this->virt_service = virt_service;
+ this->disp_service = disp_service;
+ ABinderProcess_startThreadPool();
+ }
+
+ ~AndroidDisplayContext() {
+ if (virt_service == nullptr) {
+ errorf("Not connected to virtualization service");
+ return;
+ }
+ auto status = this->virt_service->clearDisplayService();
+ if (!status.isOk()) {
+ errorf("Failed to clear display service");
+ }
+ }
+
+ void errorf(const char* format, ...) {
+ char buffer[1024];
+
+ va_list vararg;
+ va_start(vararg, format);
+ vsnprintf(buffer, sizeof(buffer), format, vararg);
+ va_end(vararg);
+
+ error_callback(buffer);
+ }
};
-LIBEXPORT
-struct android_display_context* create_android_display_context(
- const char* name, size_t name_len, android_display_log_callback_type error_callback) {
- auto ctx = new android_display_context();
-
- auto service = ::ndk::SharedRefBase::make<DisplayService>();
-
- if (strlen(name) != name_len) {
- ErrorF(error_callback, "Invalid service name length. Expected %u, actual %u", name_len,
- strlen(name));
- return nullptr;
- }
- ::ndk::SpAIBinder binder(
- AServiceManager_waitForService("android.system.virtualizationservice"));
-
- auto virt_service = IVirtualizationServiceInternal::fromBinder(binder);
- if (virt_service == nullptr) {
- ErrorF(error_callback, "Failed to find android.system.virtualizationservice");
- return nullptr;
- }
- auto status = virt_service->setDisplayService(service->asBinder());
- if (!status.isOk()) {
- ErrorF(error_callback, "Failed to register %s",
- aidl::android::crosvm::ICrosvmAndroidDisplayService::descriptor);
- return nullptr;
- }
-
- ABinderProcess_startThreadPool();
-
- auto surface = service->getSurface();
- ctx->width = static_cast<uint32_t>(ANativeWindow_getWidth(surface->get()));
- ctx->height = static_cast<uint32_t>(ANativeWindow_getHeight(surface->get()));
- ctx->displayService = service;
- return ctx;
+extern "C" struct AndroidDisplayContext* create_android_display_context(
+ const char*, ErrorCallback error_callback) {
+ return new AndroidDisplayContext(error_callback);
}
-LIBEXPORT
-void destroy_android_display_context(android_display_log_callback_type error_callback,
- struct android_display_context* ctx) {
- auto service = ::ndk::SharedRefBase::make<DisplayService>();
- ::ndk::SpAIBinder binder(
- AServiceManager_waitForService("android.system.virtualizationservice"));
- auto virt_service = IVirtualizationServiceInternal::fromBinder(binder);
- if (virt_service != nullptr) {
- auto status = virt_service->clearDisplayService();
- } else {
- ErrorF(error_callback, "Failed to find android.system.virtualizationservice");
- }
-
- if (!ctx) {
- ErrorF(error_callback, "Invalid context.");
- return;
- }
-
+extern "C" void destroy_android_display_context(struct AndroidDisplayContext* ctx) {
delete ctx;
}
-LIBEXPORT
-uint32_t get_android_display_width(android_display_log_callback_type error_callback,
- struct android_display_context* ctx) {
- if (!ctx) {
- ErrorF(error_callback, "Invalid context.");
- return -1;
+extern "C" ANativeWindow* create_android_surface(struct AndroidDisplayContext* ctx, uint32_t width,
+ uint32_t height) {
+ if (ctx->disp_service == nullptr) {
+ ctx->errorf("Display service was not created");
+ return nullptr;
}
- if (!ctx->displayService->getSurface()) {
- ErrorF(error_callback, "Invalid context surface for ctx:%p.", ctx);
- return -1;
+ // Note: crosvm always uses BGRA8888 or BGRX8888. See devices/src/virtio/gpu/mod.rs in crosvm
+ // where the SetScanoutBlob command is handled. Let's use BGRA not BGRX with a hope that we will
+ // need alpha blending for the cursor surface.
+ int format = HAL_PIXEL_FORMAT_BGRA_8888;
+ ANativeWindow* surface = ctx->disp_service->getSurface()->get(); // this can block
+ if (ANativeWindow_setBuffersGeometry(surface, width, height, format) != 0) {
+ ctx->errorf("Failed to set buffer gemoetry");
+ return nullptr;
}
- return ctx->width;
+ // TODO(b/332785161): if we know that surface can get destroyed dynamically while VM is running,
+ // consider calling ANativeWindow_acquire here and _release in destroy_android_surface, so that
+ // crosvm doesn't hold a dangling pointer.
+ return surface;
}
-LIBEXPORT
-uint32_t get_android_display_height(android_display_log_callback_type error_callback,
- struct android_display_context* ctx) {
- if (!ctx) {
- ErrorF(error_callback, "Invalid context.");
- return -1;
- }
- if (!ctx->displayService->getSurface()) {
- ErrorF(error_callback, "Invalid context surface for ctx:%p.", ctx);
- return -1;
- }
- return ctx->height;
+extern "C" void destroy_android_surface(struct AndroidDisplayContext*, ANativeWindow*) {
+ // NOT IMPLEMENTED
}
-uint16_t RGBA8888ToRGB565(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
- (void)a;
- return (static_cast<uint16_t>(r >> 3) << 11) | (static_cast<uint16_t>(g >> 2) << 5) |
- (static_cast<uint16_t>(b >> 3) << 0);
+extern "C" bool get_android_surface_buffer(struct AndroidDisplayContext* ctx,
+ ANativeWindow* surface,
+ ANativeWindow_Buffer* out_buffer) {
+ if (out_buffer == nullptr) {
+ ctx->errorf("out_buffer is null");
+ return false;
+ }
+ if (ANativeWindow_lock(surface, out_buffer, nullptr) != 0) {
+ ctx->errorf("Failed to lock buffer");
+ return false;
+ }
+ return true;
}
-LIBEXPORT
-void blit_android_display(android_display_log_callback_type error_callback,
- struct android_display_context* ctx, uint32_t width, uint32_t height,
- uint8_t* pixels, size_t pixels_num_bytes) {
- if (!ctx) {
- ErrorF(error_callback, "Invalid context.");
- return;
- }
- if (!ctx->displayService->getSurface()) {
- ErrorF(error_callback, "Invalid context surface.");
- return;
- }
- if (pixels_num_bytes != width * height * 4) {
- ErrorF(error_callback, "Invalid buffer size.");
- return;
- }
- ANativeWindow* anw = ctx->displayService->getSurface()->get();
- if (!anw) {
- ErrorF(error_callback, "Invalid context surface.");
- return;
- }
-
- ANativeWindow_Buffer anwBuffer = {};
- if (ANativeWindow_lock(anw, &anwBuffer, nullptr) != android::OK) {
- ErrorF(error_callback, "Failed to lock ANativeWindow.");
- return;
- }
-
- // Source is always BGRA8888.
- auto* src = reinterpret_cast<uint32_t*>(pixels);
- auto srcWidth = static_cast<uint32_t>(width);
- auto srcHeight = static_cast<uint32_t>(height);
- auto srcStrideBytes = srcWidth * 4;
- auto srcStridePixels = srcWidth;
-
- auto dstWidth = static_cast<uint32_t>(anwBuffer.width);
- auto dstHeight = static_cast<uint32_t>(anwBuffer.height);
-
- // Scale to fit if needed.
- std::vector<uint32_t> scaledSrc;
- if (srcWidth != dstWidth || srcHeight != dstHeight) {
- const float ratioWidth = static_cast<float>(dstWidth) / static_cast<float>(srcWidth);
- const float ratioHeight = static_cast<float>(dstHeight) / static_cast<float>(srcHeight);
- const float ratioUsed = std::min(ratioWidth, ratioHeight);
-
- uint32_t scaledSrcWidth = static_cast<uint32_t>(static_cast<float>(srcWidth) * ratioUsed);
- uint32_t scaledSrcHeight = static_cast<uint32_t>(static_cast<float>(srcHeight) * ratioUsed);
- uint32_t scaledSrcStrideBytes = scaledSrcWidth * 4;
- uint32_t scaledSrcStridePixels = scaledSrcWidth;
-
- scaledSrc.resize(scaledSrcHeight * scaledSrcStridePixels);
-
- libyuv::ARGBScale(reinterpret_cast<uint8_t*>(src), srcStrideBytes, srcWidth, srcHeight,
- reinterpret_cast<uint8_t*>(scaledSrc.data()), scaledSrcStrideBytes,
- scaledSrcWidth, scaledSrcHeight, libyuv::kFilterBilinear);
-
- src = scaledSrc.data();
- srcWidth = scaledSrcWidth;
- srcHeight = scaledSrcHeight;
- srcStrideBytes = scaledSrcStrideBytes;
- srcStridePixels = scaledSrcStridePixels;
- }
-
- if (anwBuffer.format == AHARDWAREBUFFER_FORMAT_R8G8B8A8_UNORM) {
- auto* dst = reinterpret_cast<uint32_t*>(anwBuffer.bits);
- auto dstStridePixels = static_cast<uint32_t>(anwBuffer.stride);
-
- for (uint32_t h = 0; h < std::min(srcHeight, dstHeight); h++) {
- for (uint32_t w = 0; w < std::min(srcWidth, dstWidth); w++) {
- dst[(h * dstStridePixels) + w] = src[(h * srcStridePixels) + w];
- }
- }
- } else if (anwBuffer.format == AHARDWAREBUFFER_FORMAT_R5G6B5_UNORM) {
- auto* dst = reinterpret_cast<uint16_t*>(anwBuffer.bits);
- auto dstWidth = static_cast<uint32_t>(anwBuffer.width);
- auto dstHeight = static_cast<uint32_t>(anwBuffer.height);
- auto dstStridePixels = static_cast<uint32_t>(anwBuffer.stride);
-
- for (uint32_t h = 0; h < std::min(srcHeight, dstHeight); h++) {
- for (uint32_t w = 0; w < std::min(srcWidth, dstWidth); w++) {
- uint32_t srcPixel = src[(h * srcStridePixels) + w];
- uint8_t* srcPixelBytes = reinterpret_cast<uint8_t*>(&srcPixel);
- uint8_t r = srcPixelBytes[2];
- uint8_t g = srcPixelBytes[1];
- uint8_t b = srcPixelBytes[0];
- uint8_t a = srcPixelBytes[3];
- dst[(h * dstStridePixels) + w] = RGBA8888ToRGB565(r, g, b, a);
- }
- }
- } else {
- ErrorF(error_callback, "Unhandled format: %d", anwBuffer.format);
- }
-
- if (ANativeWindow_unlockAndPost(anw) != android::OK) {
- ErrorF(error_callback, "Failed to unlock and post ANativeWindow.");
+extern "C" void post_android_surface_buffer(struct AndroidDisplayContext* ctx,
+ ANativeWindow* surface) {
+ if (ANativeWindow_unlockAndPost(surface) != 0) {
+ ctx->errorf("Failed to unlock and post surface.");
return;
}
}
diff --git a/service_vm/manager/src/lib.rs b/service_vm/manager/src/lib.rs
index 273b54d..987325d 100644
--- a/service_vm/manager/src/lib.rs
+++ b/service_vm/manager/src/lib.rs
@@ -30,7 +30,6 @@
use service_vm_comm::{Request, Response, ServiceVmRequest, VmType};
use std::fs::{self, File, OpenOptions};
use std::io::{self, BufRead, BufReader, BufWriter, Read, Write};
-use std::os::unix::io::FromRawFd;
use std::path::{Path, PathBuf};
use std::sync::{Condvar, Mutex};
use std::thread;
@@ -294,10 +293,8 @@
pub fn android_log_fd() -> io::Result<File> {
let (reader_fd, writer_fd) = nix::unistd::pipe()?;
- // SAFETY: These are new FDs with no previous owner.
- let reader = unsafe { File::from_raw_fd(reader_fd) };
- // SAFETY: These are new FDs with no previous owner.
- let writer = unsafe { File::from_raw_fd(writer_fd) };
+ let reader = File::from(reader_fd);
+ let writer = File::from(writer_fd);
thread::spawn(|| {
for line in BufReader::new(reader).lines() {
diff --git a/service_vm/test_apk/Android.bp b/service_vm/test_apk/Android.bp
index 450d475..72e411e 100644
--- a/service_vm/test_apk/Android.bp
+++ b/service_vm/test_apk/Android.bp
@@ -58,14 +58,8 @@
manifest: "AndroidManifest.rkpd.xml",
test_config: "AndroidTest.rkpd.xml",
static_libs: [
- "RkpdAppTestUtil",
"VmAttestationTestUtil",
- "androidx.work_work-testing",
],
- instrumentation_for: "rkpdapp",
- // This app is a variation of rkpdapp, with additional permissions to run
- // a VM. It is defined in packages/modules/RemoteKeyProvisioning.
- data: [":avf-rkpdapp"],
}
java_library {
diff --git a/service_vm/test_apk/AndroidManifest.rkpd.xml b/service_vm/test_apk/AndroidManifest.rkpd.xml
index 6ecc5a9..369a456 100644
--- a/service_vm/test_apk/AndroidManifest.rkpd.xml
+++ b/service_vm/test_apk/AndroidManifest.rkpd.xml
@@ -17,8 +17,15 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.virt.rkpd.vm_attestation.testapp">
+ <uses-permission android:name="android.permission.MANAGE_VIRTUAL_MACHINE" />
+ <uses-permission android:name="android.permission.USE_CUSTOM_VIRTUAL_MACHINE" />
+
+ <!-- Required to check the network access -->
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
+ <uses-permission android:name="android.permission.INTERNET" />
+
<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
- android:targetPackage="com.android.rkpdapp"
+ android:targetPackage="com.android.virt.rkpd.vm_attestation.testapp"
android:label="AVF rkpd app integration tests" />
</manifest>
diff --git a/service_vm/test_apk/AndroidTest.rkpd.xml b/service_vm/test_apk/AndroidTest.rkpd.xml
index 39eca32..8ff7bb4 100644
--- a/service_vm/test_apk/AndroidTest.rkpd.xml
+++ b/service_vm/test_apk/AndroidTest.rkpd.xml
@@ -17,22 +17,18 @@
<option name="test-suite-tag" value="apct" />
<option name="test-suite-tag" value="apct-instrumentation" />
- <!-- Need to disable SELinux policy to allow com.android.rkpdapp to run a VM. -->
- <target_preparer class="com.android.tradefed.targetprep.DisableSELinuxTargetPreparer"/>
+ <target_preparer class="com.android.tradefed.targetprep.DeviceSetup">
+ <!-- The host name is needed for RKPD key provisioning -->
+ <option name="set-property" key="remote_provisioning.hostname"
+ value="remoteprovisioning.googleapis.com" />
+ <option name="restore-properties" value="true"/>
+ </target_preparer>
<target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
<option name="test-file-name" value="AvfRkpdVmAttestationTestApp.apk" />
- <option name="test-file-name" value="avf-rkpdapp.apk" />
</target_preparer>
<test class="com.android.tradefed.testtype.AndroidJUnitTest" >
<option name="package" value="com.android.virt.rkpd.vm_attestation.testapp" />
</test>
-
- <!-- Only run if RKPD mainline module is installed -->
- <object type="module_controller"
- class="com.android.tradefed.testtype.suite.module.MainlineTestModuleController">
- <option name="enable" value="true" />
- <option name="mainline-module-package-name" value="com.android.rkpd" />
- </object>
</configuration>
diff --git a/service_vm/test_apk/src/java/com/android/virt/rkpd/vm_attestation/testapp/RkpdVmAttestationTest.java b/service_vm/test_apk/src/java/com/android/virt/rkpd/vm_attestation/testapp/RkpdVmAttestationTest.java
index ce7fc45..f456cb4 100644
--- a/service_vm/test_apk/src/java/com/android/virt/rkpd/vm_attestation/testapp/RkpdVmAttestationTest.java
+++ b/service_vm/test_apk/src/java/com/android/virt/rkpd/vm_attestation/testapp/RkpdVmAttestationTest.java
@@ -18,31 +18,20 @@
import static android.system.virtualmachine.VirtualMachineConfig.DEBUG_LEVEL_FULL;
-import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
+import android.net.ConnectivityManager;
+import android.net.NetworkCapabilities;
+import android.net.Network;
import android.content.Context;
-import android.hardware.security.keymint.IRemotelyProvisionedComponent;
-import android.os.SystemProperties;
import android.system.virtualmachine.VirtualMachine;
import android.system.virtualmachine.VirtualMachineConfig;
-import androidx.work.ListenableWorker;
-import androidx.work.testing.TestWorkerBuilder;
-
import com.android.microdroid.test.device.MicrodroidDeviceTestBase;
-import com.android.rkpdapp.database.ProvisionedKeyDao;
-import com.android.rkpdapp.database.RkpdDatabase;
-import com.android.rkpdapp.interfaces.ServerInterface;
-import com.android.rkpdapp.interfaces.ServiceManagerInterface;
-import com.android.rkpdapp.interfaces.SystemInterface;
-import com.android.rkpdapp.provisioner.PeriodicProvisioner;
-import com.android.rkpdapp.testutil.SystemInterfaceSelector;
-import com.android.rkpdapp.utils.Settings;
import com.android.virt.vm_attestation.testservice.IAttestationService.SigningResult;
import com.android.virt.vm_attestation.util.X509Utils;
+import android.system.virtualmachine.VirtualMachineManager;
-import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -53,7 +42,6 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
-import java.util.concurrent.Executors;
/**
* End-to-end test for the pVM remote attestation.
@@ -71,23 +59,16 @@
* <p>- Have an arm64 device supporting protected VMs.
*
* <p>- Have a stable network connection on the device.
- *
- * <p>- Have the RKP server hostname configured in the device. If not, you can set it using: $ adb
- * shell setprop remote_provisioning.hostname remoteprovisioning.googleapis.com
*/
@RunWith(Parameterized.class)
public class RkpdVmAttestationTest extends MicrodroidDeviceTestBase {
private static final String TAG = "RkpdVmAttestationTest";
- private static final String SERVICE_NAME = IRemotelyProvisionedComponent.DESCRIPTOR + "/avf";
private static final String VM_PAYLOAD_PATH = "libvm_attestation_test_payload.so";
private static final String MESSAGE = "Hello RKP from AVF!";
private static final String TEST_APP_PACKAGE_NAME =
"com.android.virt.rkpd.vm_attestation.testapp";
- private ProvisionedKeyDao mKeyDao;
- private PeriodicProvisioner mProvisioner;
-
@Parameterized.Parameter(0)
public String mGki;
@@ -103,57 +84,33 @@
@Before
public void setUp() throws Exception {
- assume().withMessage("The RKP server hostname is not configured -- assume RKP disabled.")
- .that(SystemProperties.get("remote_provisioning.hostname"))
- .isNotEmpty();
assume().withMessage("RKP Integration tests rely on network availability.")
- .that(ServerInterface.isNetworkConnected(getContext()))
+ .that(isNetworkConnected(getContext()))
.isTrue();
- // TODO(b/329652894): Assume that pVM remote attestation feature is supported.
+ assumeFeatureEnabled(VirtualMachineManager.FEATURE_REMOTE_ATTESTATION);
+ assume().withMessage("Test needs Remote Attestation support")
+ .that(getVirtualMachineManager().isRemoteAttestationSupported())
+ .isTrue();
- prepareTestSetup(true /* protectedVm */, mGki);
-
- Settings.clearPreferences(getContext());
- mKeyDao = RkpdDatabase.getDatabase(getContext()).provisionedKeyDao();
- mKeyDao.deleteAllKeys();
-
- mProvisioner =
- TestWorkerBuilder.from(
- getContext(),
- PeriodicProvisioner.class,
- Executors.newSingleThreadExecutor())
- .build();
-
- SystemInterface systemInterface =
- SystemInterfaceSelector.getSystemInterfaceForServiceName(SERVICE_NAME);
- ServiceManagerInterface.setInstances(new SystemInterface[] {systemInterface});
-
- setMaxPerformanceTaskProfile();
- }
-
- @After
- public void tearDown() throws Exception {
- ServiceManagerInterface.setInstances(null);
- if (mKeyDao != null) {
- mKeyDao.deleteAllKeys();
+ if (mGki == null) {
+ // We don't need this permission to use the microdroid kernel.
+ revokePermission(VirtualMachine.USE_CUSTOM_VIRTUAL_MACHINE_PERMISSION);
+ } else {
+ // The permission is needed to use the GKI kernel.
+ // Granting the permission is needed as the microdroid kernel test setup
+ // can revoke the permission before the GKI kernel test.
+ grantPermission(VirtualMachine.USE_CUSTOM_VIRTUAL_MACHINE_PERMISSION);
}
- Settings.clearPreferences(getContext());
+ prepareTestSetup(true /* protectedVm */, mGki);
+ setMaxPerformanceTaskProfile();
}
@Test
public void usingProvisionedKeyForVmAttestationSucceeds() throws Exception {
- // Provision keys.
- assertThat(mProvisioner.doWork()).isEqualTo(ListenableWorker.Result.success());
- assertThat(mKeyDao.getTotalUnassignedKeysForIrpc(SERVICE_NAME)).isGreaterThan(0);
-
// Arrange.
- Context ctx = getContext();
- Context otherAppCtx = ctx.createPackageContext(TEST_APP_PACKAGE_NAME, 0);
VirtualMachineConfig config =
- new VirtualMachineConfig.Builder(otherAppCtx)
- .setProtectedVm(true)
+ newVmConfigBuilderWithPayloadBinary(VM_PAYLOAD_PATH)
.setDebugLevel(DEBUG_LEVEL_FULL)
- .setPayloadBinaryName(VM_PAYLOAD_PATH)
.setVmOutputCaptured(true)
.build();
VirtualMachine vm = forceCreateNewVirtualMachine("attestation_with_rkpd_client", config);
@@ -170,4 +127,13 @@
X509Utils.verifyAvfRelatedCerts(certs, challenge, TEST_APP_PACKAGE_NAME);
X509Utils.verifySignature(certs[0], MESSAGE.getBytes(), signingResult.signature);
}
+
+ private static boolean isNetworkConnected(Context context) {
+ ConnectivityManager cm = context.getSystemService(ConnectivityManager.class);
+ Network network = cm.getActiveNetwork();
+ NetworkCapabilities capabilities = cm.getNetworkCapabilities(network);
+ return capabilities != null
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
+ && capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED);
+ }
}
diff --git a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
index 2b53571..2b5c564 100644
--- a/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
+++ b/tests/hostside/java/com/android/microdroid/test/MicrodroidHostTests.java
@@ -828,6 +828,11 @@
AtomsProto.VmExited atomVmExited = data.get(2).getAtom().getVmExited();
assertThat(atomVmExited.getVmIdentifier()).isEqualTo("VmRunApp");
assertThat(atomVmExited.getDeathReason()).isEqualTo(AtomsProto.VmExited.DeathReason.KILLED);
+ assertThat(atomVmExited.getExitSignal()).isEqualTo(9);
+ // In CPU & memory related fields, check whether positive values are collected or not.
+ assertThat(atomVmExited.getGuestTimeMillis()).isGreaterThan(0);
+ assertThat(atomVmExited.getRssVmKb()).isGreaterThan(0);
+ assertThat(atomVmExited.getRssCrosvmKb()).isGreaterThan(0);
// Check UID and elapsed_time by comparing each other.
assertThat(atomVmBooted.getUid()).isEqualTo(atomVmCreationRequested.getUid());
diff --git a/virtualizationmanager/src/aidl.rs b/virtualizationmanager/src/aidl.rs
index 279b4ec..99a0078 100644
--- a/virtualizationmanager/src/aidl.rs
+++ b/virtualizationmanager/src/aidl.rs
@@ -17,7 +17,7 @@
use crate::{get_calling_pid, get_calling_uid};
use crate::atom::{write_vm_booted_stats, write_vm_creation_stats};
use crate::composite::make_composite_image;
-use crate::crosvm::{CrosvmConfig, DiskFile, DisplayConfig, PayloadState, VmContext, VmInstance, VmState};
+use crate::crosvm::{CrosvmConfig, DiskFile, DisplayConfig, InputDeviceOption, PayloadState, VmContext, VmInstance, VmState};
use crate::debug_config::DebugConfig;
use crate::dt_overlay::{create_device_tree_overlay, VM_DT_OVERLAY_MAX_SIZE, VM_DT_OVERLAY_PATH};
use crate::payload::{add_microdroid_payload_images, add_microdroid_system_images, add_microdroid_vendor_image};
@@ -32,6 +32,7 @@
AssignableDevice::AssignableDevice,
CpuTopology::CpuTopology,
DiskImage::DiskImage,
+ InputDevice::InputDevice,
IVirtualMachine::{BnVirtualMachine, IVirtualMachine},
IVirtualMachineCallback::IVirtualMachineCallback,
IVirtualizationService::IVirtualizationService,
@@ -581,13 +582,27 @@
} else {
(vec![], None)
};
+ let display_config = if cfg!(paravirtualized_devices) {
+ config
+ .displayConfig
+ .as_ref()
+ .map(DisplayConfig::new)
+ .transpose()
+ .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
+ } else {
+ None
+ };
- let display_config = config
- .displayConfig
- .as_ref()
- .map(DisplayConfig::new)
- .transpose()
- .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?;
+ let input_device_options = if cfg!(paravirtualized_devices) {
+ config
+ .inputDevices
+ .iter()
+ .map(to_input_device_option_from)
+ .collect::<Result<Vec<InputDeviceOption>, _>>()
+ .or_binder_exception(ExceptionCode::ILLEGAL_ARGUMENT)?
+ } else {
+ vec![]
+ };
// Actually start the VM.
let crosvm_config = CrosvmConfig {
@@ -615,6 +630,7 @@
dtbo,
device_tree_overlay,
display_config,
+ input_device_options,
};
let instance = Arc::new(
VmInstance::new(
@@ -720,6 +736,26 @@
(result / granularity) * granularity
}
+fn to_input_device_option_from(input_device: &InputDevice) -> Result<InputDeviceOption> {
+ Ok(match input_device {
+ InputDevice::SingleTouch(single_touch) => InputDeviceOption::SingleTouch {
+ file: clone_file(single_touch.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?)?,
+ height: u32::try_from(single_touch.height)?,
+ width: u32::try_from(single_touch.width)?,
+ name: if !single_touch.name.is_empty() {
+ Some(single_touch.name.clone())
+ } else {
+ None
+ },
+ },
+ InputDevice::EvDev(evdev) => InputDeviceOption::EvDev(clone_file(
+ evdev.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
+ )?),
+ InputDevice::Keyboard(keyboard) => InputDeviceOption::Keyboard(clone_file(
+ keyboard.pfd.as_ref().ok_or(anyhow!("pfd should have value"))?,
+ )?),
+ })
+}
/// Given the configuration for a disk image, assembles the `DiskFile` to pass to crosvm.
///
/// This may involve assembling a composite disk from a set of partition images.
@@ -1410,13 +1446,11 @@
return Ok(None);
};
- let (raw_read_fd, raw_write_fd) =
+ let (read_fd, write_fd) =
pipe().context("Failed to create pipe").or_service_specific_exception(-1)?;
- // SAFETY: We are the sole owner of this FD as we just created it, and it is valid and open.
- let mut reader = BufReader::new(unsafe { File::from_raw_fd(raw_read_fd) });
- // SAFETY: We are the sole owner of this FD as we just created it, and it is valid and open.
- let write_fd = unsafe { File::from_raw_fd(raw_write_fd) };
+ let mut reader = BufReader::new(File::from(read_fd));
+ let write_fd = File::from(write_fd);
std::thread::spawn(move || loop {
let mut buf = vec![];
diff --git a/virtualizationmanager/src/crosvm.rs b/virtualizationmanager/src/crosvm.rs
index 86c9af3..040e552 100644
--- a/virtualizationmanager/src/crosvm.rs
+++ b/virtualizationmanager/src/crosvm.rs
@@ -34,7 +34,7 @@
use std::io::{self, Read};
use std::mem;
use std::num::{NonZeroU16, NonZeroU32};
-use std::os::unix::io::{AsRawFd, RawFd, FromRawFd};
+use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::process::ExitStatusExt;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus};
@@ -120,6 +120,7 @@
pub dtbo: Option<File>,
pub device_tree_overlay: Option<File>,
pub display_config: Option<DisplayConfig>,
+ pub input_device_options: Vec<InputDeviceOption>,
}
#[derive(Debug)]
@@ -154,6 +155,15 @@
pub writable: bool,
}
+/// virtio-input device configuration from `external/crosvm/src/crosvm/config.rs`
+#[derive(Debug)]
+#[allow(dead_code)]
+pub enum InputDeviceOption {
+ EvDev(File),
+ SingleTouch { file: File, width: u32, height: u32, name: Option<String> },
+ Keyboard(File),
+}
+
type VfioDevice = Strong<dyn IBoundDevice>;
/// The lifecycle state which the payload in the VM has reported itself to be in.
@@ -955,14 +965,46 @@
if let Some(dt_overlay) = &config.device_tree_overlay {
command.arg("--device-tree-overlay").arg(add_preserved_fd(&mut preserved_fds, dt_overlay));
}
- if let Some(display_config) = &config.display_config {
- command.arg("--gpu")
- // TODO(b/331708504): support backend config as well
- .arg("backend=virglrenderer,context-types=virgl2,egl=true,surfaceless=true,glx=false,gles=true")
- .arg(format!("--gpu-display=mode=windowed[{},{}],dpi=[{},{}],refresh-rate={}", display_config.width, display_config.height, display_config.horizontal_dpi, display_config.vertical_dpi, display_config.refresh_rate))
- .arg(format!("--android-display-service={}", config.name));
+
+ if cfg!(paravirtualized_devices) {
+ if let Some(display_config) = &config.display_config {
+ command.arg("--gpu")
+ // TODO(b/331708504): support backend config as well
+ .arg("backend=virglrenderer,context-types=virgl2,egl=true,surfaceless=true,glx=false,gles=true")
+ .arg(format!("--gpu-display=mode=windowed[{},{}],dpi=[{},{}],refresh-rate={}", display_config.width, display_config.height, display_config.horizontal_dpi, display_config.vertical_dpi, display_config.refresh_rate))
+ .arg(format!("--android-display-service={}", config.name));
+ }
}
+ if cfg!(paravirtualized_devices) {
+ // TODO(b/325929096): Need to set up network from the config
+ if rustutils::system_properties::read_bool("ro.crosvm.network.setup.done", false)
+ .unwrap_or(false)
+ {
+ command.arg("--net").arg("tap-name=crosvm_tap");
+ }
+ }
+
+ if cfg!(paravirtualized_devices) {
+ for input_device_option in config.input_device_options.iter() {
+ command.arg("--input");
+ command.arg(match input_device_option {
+ InputDeviceOption::EvDev(file) => {
+ format!("evdev[path={}]", add_preserved_fd(&mut preserved_fds, file))
+ }
+ InputDeviceOption::Keyboard(file) => {
+ format!("keyboard[path={}]", add_preserved_fd(&mut preserved_fds, file))
+ }
+ InputDeviceOption::SingleTouch { file, width, height, name } => format!(
+ "single-touch[path={},width={},height={}{}]",
+ add_preserved_fd(&mut preserved_fds, file),
+ width,
+ height,
+ name.as_ref().map_or("".into(), |n| format!(",name={}", n))
+ ),
+ });
+ }
+ }
append_platform_devices(&mut command, &mut preserved_fds, &config)?;
debug!("Preserving FDs {:?}", preserved_fds);
@@ -1041,10 +1083,6 @@
/// Creates a new pipe with the `O_CLOEXEC` flag set, and returns the read side and write side.
fn create_pipe() -> Result<(File, File), Error> {
- let (raw_read, raw_write) = pipe2(OFlag::O_CLOEXEC)?;
- // SAFETY: We are the sole owner of this FD as we just created it, and it is valid and open.
- let read_fd = unsafe { File::from_raw_fd(raw_read) };
- // SAFETY: We are the sole owner of this FD as we just created it, and it is valid and open.
- let write_fd = unsafe { File::from_raw_fd(raw_write) };
- Ok((read_fd, write_fd))
+ let (read_fd, write_fd) = pipe2(OFlag::O_CLOEXEC)?;
+ Ok((read_fd.into(), write_fd.into()))
}
diff --git a/virtualizationmanager/src/main.rs b/virtualizationmanager/src/main.rs
index b2a734a..a31fd0a 100644
--- a/virtualizationmanager/src/main.rs
+++ b/virtualizationmanager/src/main.rs
@@ -30,10 +30,10 @@
use lazy_static::lazy_static;
use log::{info, LevelFilter};
use rpcbinder::{FileDescriptorTransportMode, RpcServer};
-use std::os::unix::io::{FromRawFd, OwnedFd, RawFd};
+use std::os::unix::io::{AsFd, FromRawFd, OwnedFd, RawFd};
use clap::Parser;
use nix::fcntl::{fcntl, F_GETFD, F_SETFD, FdFlag};
-use nix::unistd::{Pid, Uid};
+use nix::unistd::{write, Pid, Uid};
use std::os::unix::raw::{pid_t, uid_t};
const LOG_TAG: &str = "virtmgr";
@@ -138,6 +138,8 @@
info!("Started VirtualizationService RpcServer. Ready to accept connections");
// Signal readiness to the caller by closing our end of the pipe.
+ write(ready_fd.as_fd(), "o".as_bytes())
+ .expect("Failed to write a single character through ready_fd");
drop(ready_fd);
server.join();
diff --git a/virtualizationservice/aidl/android/system/virtualizationservice/InputDevice.aidl b/virtualizationservice/aidl/android/system/virtualizationservice/InputDevice.aidl
new file mode 100644
index 0000000..712d6a9
--- /dev/null
+++ b/virtualizationservice/aidl/android/system/virtualizationservice/InputDevice.aidl
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2024 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.
+ */
+package android.system.virtualizationservice;
+
+// Refer to https://crosvm.dev/book/devices/input.html
+union InputDevice {
+ // Add a single-touch touchscreen virtio-input device.
+ parcelable SingleTouch {
+ ParcelFileDescriptor pfd;
+ // Default values come from https://crosvm.dev/book/devices/input.html#single-touch
+ int width = 1280;
+ int height = 1080;
+ @utf8InCpp String name = "";
+ }
+ // Passes an event device node into the VM. The device will be grabbed (unusable from the host)
+ // and made available to the guest with the same configuration it shows on the host.
+ parcelable EvDev {
+ ParcelFileDescriptor pfd;
+ }
+ // Keyboard input
+ parcelable Keyboard {
+ ParcelFileDescriptor pfd;
+ }
+ SingleTouch singleTouch;
+ EvDev evDev;
+ Keyboard keyboard;
+}
diff --git a/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl b/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
index 1a18bf8..86e26da 100644
--- a/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
+++ b/virtualizationservice/aidl/android/system/virtualizationservice/VirtualMachineRawConfig.aidl
@@ -18,6 +18,7 @@
import android.system.virtualizationservice.CpuTopology;
import android.system.virtualizationservice.DiskImage;
import android.system.virtualizationservice.DisplayConfig;
+import android.system.virtualizationservice.InputDevice;
/** Raw configuration for running a VM. */
parcelable VirtualMachineRawConfig {
@@ -73,4 +74,7 @@
String[] devices;
@nullable DisplayConfig displayConfig;
+
+ /** List of input devices to the VM */
+ InputDevice[] inputDevices;
}
diff --git a/vmbase/example/src/main.rs b/vmbase/example/src/main.rs
index 48b24be..da82b17 100644
--- a/vmbase/example/src/main.rs
+++ b/vmbase/example/src/main.rs
@@ -28,6 +28,7 @@
use aarch64_paging::paging::MemoryRegion;
use aarch64_paging::MapError;
use alloc::{vec, vec::Vec};
+use core::ptr::addr_of_mut;
use cstr::cstr;
use fdtpci::PciInfo;
use libfdt::Fdt;
@@ -138,14 +139,15 @@
// SAFETY: Nowhere else in the program accesses this static mutable variable, so there is no
// chance of concurrent access.
- let zeroed_data = unsafe { &mut ZEROED_DATA };
+ let zeroed_data = unsafe { &mut *addr_of_mut!(ZEROED_DATA) };
// SAFETY: Nowhere else in the program accesses this static mutable variable, so there is no
// chance of concurrent access.
- let mutable_data = unsafe { &mut MUTABLE_DATA };
+ let mutable_data = unsafe { &mut *addr_of_mut!(MUTABLE_DATA) };
for element in zeroed_data.iter() {
assert_eq!(*element, 0);
}
+
zeroed_data[0] = 13;
assert_eq!(zeroed_data[0], 13);
zeroed_data[0] = 0;
diff --git a/vmbase/example/tests/test.rs b/vmbase/example/tests/test.rs
index 2df5a80..4dc6aec 100644
--- a/vmbase/example/tests/test.rs
+++ b/vmbase/example/tests/test.rs
@@ -27,7 +27,6 @@
collections::{HashSet, VecDeque},
fs::File,
io::{self, BufRead, BufReader, Read, Write},
- os::unix::io::FromRawFd,
panic, thread,
};
use vmclient::{DeathReason, VmInstance};
@@ -142,13 +141,7 @@
fn pipe() -> io::Result<(File, File)> {
let (reader_fd, writer_fd) = nix::unistd::pipe()?;
-
- // SAFETY: These are new FDs with no previous owner.
- let reader = unsafe { File::from_raw_fd(reader_fd) };
- // SAFETY: These are new FDs with no previous owner.
- let writer = unsafe { File::from_raw_fd(writer_fd) };
-
- Ok((reader, writer))
+ Ok((reader_fd.into(), writer_fd.into()))
}
struct VmLogProcessor {
diff --git a/vmclient/src/lib.rs b/vmclient/src/lib.rs
index a2a88d8..88072a7 100644
--- a/vmclient/src/lib.rs
+++ b/vmclient/src/lib.rs
@@ -48,7 +48,7 @@
use std::{
fmt::{self, Debug, Formatter},
fs::File,
- os::unix::io::{AsFd, AsRawFd, FromRawFd, IntoRawFd, OwnedFd},
+ os::unix::io::{AsFd, AsRawFd, IntoRawFd, OwnedFd},
sync::Arc,
time::Duration,
};
@@ -62,10 +62,7 @@
// Create new POSIX pipe. Make it O_CLOEXEC to align with how Rust creates
// file descriptors (expected by SharedChild).
- let (raw1, raw2) = pipe2(OFlag::O_CLOEXEC)?;
-
- // SAFETY: Taking ownership of brand new FDs.
- unsafe { Ok((OwnedFd::from_raw_fd(raw1), OwnedFd::from_raw_fd(raw2))) }
+ Ok(pipe2(OFlag::O_CLOEXEC)?)
}
fn posix_socketpair() -> Result<(OwnedFd, OwnedFd), io::Error> {
diff --git a/vmlauncher_app/AndroidManifest.xml b/vmlauncher_app/AndroidManifest.xml
index 607a895..d800ec7 100644
--- a/vmlauncher_app/AndroidManifest.xml
+++ b/vmlauncher_app/AndroidManifest.xml
@@ -4,9 +4,11 @@
<uses-permission android:name="android.permission.MANAGE_VIRTUAL_MACHINE" />
<uses-permission android:name="android.permission.USE_CUSTOM_VIRTUAL_MACHINE" />
+ <uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.software.virtualization_framework" android:required="true" />
<application
- android:label="VmLauncherApp">
+ android:label="VmLauncherApp"
+ android:networkSecurityConfig="@xml/network_security_config">
<activity android:name=".MainActivity"
android:enabled="false"
android:screenOrientation="landscape"
diff --git a/vmlauncher_app/README.md b/vmlauncher_app/README.md
index 9175e57..0109f37 100644
--- a/vmlauncher_app/README.md
+++ b/vmlauncher_app/README.md
@@ -1,13 +1,16 @@
# VM launcher app
-## Building & Installing
+## Building
-Add `VmLauncherApp` into `PRODUCT_PACKAGES` and then `m`
+This app is now part of the virt APEX.
-You can also explicitly grant or revoke the permission, e.g.
+## Enabling
+
+This app is disabled by default. To re-enable it, execute the following command.
+
```
-adb shell pm grant com.android.virtualization.vmlauncher android.permission.USE_CUSTOM_VIRTUAL_MACHINE
-adb shell pm grant com.android.virtualization.vmlauncher android.permission.MANAGE_VIRTUAL_MACHINE
+adb root
+adb shell pm enable com.android.virtualization.vmlauncher/.MainActivity
```
## Running
diff --git a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
index b5995b8..4c42bb4 100644
--- a/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
+++ b/vmlauncher_app/java/com/android/virtualization/vmlauncher/MainActivity.java
@@ -37,6 +37,7 @@
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
+import android.view.KeyEvent;
import android.view.WindowManager;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
@@ -126,6 +127,9 @@
}
customImageConfigBuilder.setDisplayConfig(displayConfigBuilder.build());
+ customImageConfigBuilder.useTouch(true);
+ customImageConfigBuilder.useKeyboard(true);
+
configBuilder.setCustomImageConfig(customImageConfigBuilder.build());
} catch (JSONException | IOException e) {
@@ -135,6 +139,22 @@
}
@Override
+ public boolean onKeyDown(int keyCode, KeyEvent event) {
+ if (mVirtualMachine == null) {
+ return false;
+ }
+ return mVirtualMachine.sendKeyEvent(event);
+ }
+
+ @Override
+ public boolean onKeyUp(int keyCode, KeyEvent event) {
+ if (mVirtualMachine == null) {
+ return false;
+ }
+ return mVirtualMachine.sendKeyEvent(event);
+ }
+
+ @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
@@ -224,6 +244,13 @@
}
SurfaceView surfaceView = findViewById(R.id.surface_view);
+ surfaceView.setOnTouchListener(
+ (v, event) -> {
+ if (mVirtualMachine == null) {
+ return false;
+ }
+ return mVirtualMachine.sendSingleTouchEvent(event);
+ });
surfaceView
.getHolder()
.addCallback(
diff --git a/vmlauncher_app/res/values/themes.xml b/vmlauncher_app/res/values/themes.xml
index 395f089..c10b6d9 100644
--- a/vmlauncher_app/res/values/themes.xml
+++ b/vmlauncher_app/res/values/themes.xml
@@ -8,7 +8,7 @@
@android:color/transparent
</item>
<item name="android:windowLayoutInDisplayCutoutMode">
- shortEdges
+ always
</item>
</style>
</resources>
diff --git a/vmlauncher_app/res/xml/network_security_config.xml b/vmlauncher_app/res/xml/network_security_config.xml
new file mode 100644
index 0000000..f27fa56
--- /dev/null
+++ b/vmlauncher_app/res/xml/network_security_config.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2024 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.
+ -->
+
+<network-security-config>
+ <domain-config cleartextTrafficPermitted="true">
+ <domain includeSubdomains="true">localhost</domain>
+ </domain-config>
+</network-security-config>