Various minor fixes
Missed null checks, simplify toString, assorted javadoc fixes. All
unimportant, just things I noticed while closely reading the code.
Bug: 239554726
Test: atest MicrodroidTests
Change-Id: Ife683000c76eaacc05ccf6c010dd4030e910f22e
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachine.java b/javalib/src/android/system/virtualmachine/VirtualMachine.java
index 0ce7991..955b350 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachine.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachine.java
@@ -179,7 +179,7 @@
/**
* Creates a virtual machine with the given name and config. Once a virtual machine is created
* it is persisted until it is deleted by calling {@link #delete()}. The created virtual machine
- * is in {@link #STOPPED} state. To run the VM, call {@link #run()}.
+ * is in {@link Status#STOPPED} state. To run the VM, call {@link #run()}.
*/
/* package */ static @NonNull VirtualMachine create(
@NonNull Context context, @NonNull String name, @NonNull VirtualMachineConfig config)
@@ -270,7 +270,7 @@
* machines sharing the same config. Even in that case, the virtual machines are completely
* isolated from each other; one cannot share its secret to another virtual machine even if they
* share the same config. It is also possible that a virtual machine can switch its config,
- * which can be done by calling {@link #setConfig(VirtualMachineCOnfig)}.
+ * which can be done by calling {@link #setConfig(VirtualMachineConfig)}.
*/
public @NonNull VirtualMachineConfig getConfig() {
return mConfig;
@@ -563,12 +563,9 @@
/**
* Connects to a VM's RPC server via vsock, and returns a root IBinder object. Guest VMs are
- * expected to set up vsock servers in their payload. After the host app receives onPayloadReady
- * callback, the host app can use this method to establish an RPC session to the guest VMs.
- *
- * <p>If the connection succeeds, the root IBinder object will be returned via {@link
- * VirtualMachineCallback.onVsockServerReady()}. If the connection fails, {@link
- * VirtualMachineCallback.onVsockServerConnectionFailed()} will be called.
+ * expected to set up vsock servers in their payload. After the host app receives the {@link
+ * VirtualMachineCallback#onPayloadReady(VirtualMachine)}, it can use this method to
+ * establish an RPC session to the guest VMs.
*/
public Future<IBinder> connectToVsockServer(int port) throws VirtualMachineException {
if (getStatus() != Status.RUNNING) {
@@ -580,13 +577,11 @@
@Override
public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("VirtualMachine(");
- sb.append("name:" + getName() + ", ");
- sb.append("config:" + getConfig().getPayloadConfigPath() + ", ");
- sb.append("package: " + mPackageName);
- sb.append(")");
- return sb.toString();
+ return "VirtualMachine("
+ + "name:" + getName() + ", "
+ + "config:" + getConfig().getPayloadConfigPath() + ", "
+ + "package: " + mPackageName
+ + ")";
}
private static List<String> parseExtraApkListFromPayloadConfig(JsonReader reader)
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachineCallback.java b/javalib/src/android/system/virtualmachine/VirtualMachineCallback.java
index a37c15b..c802678 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachineCallback.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachineCallback.java
@@ -128,10 +128,16 @@
/** The VM killed due to hangup */
int DEATH_REASON_HANGUP = 16;
- /** Called when the payload starts in the VM. */
+ /**
+ * Called when the payload starts in the VM. The stream, if non-null, provides access
+ * to the stdin/stdout of the VM payload.
+ */
void onPayloadStarted(@NonNull VirtualMachine vm, @Nullable ParcelFileDescriptor stream);
- /** Called when the payload in the VM is ready to serve. */
+ /**
+ * Called when the payload in the VM is ready to serve. See
+ * {@link VirtualMachine#connectToVsockServer(int)} ()}.
+ */
void onPayloadReady(@NonNull VirtualMachine vm);
/** Called when the payload has finished in the VM. */
@@ -140,7 +146,7 @@
/** Called when an error occurs in the VM. */
void onError(@NonNull VirtualMachine vm, @ErrorCode int errorCode, @NonNull String message);
- /** Called when the VM died. */
+ /** Called when the VM has ended. */
void onDied(@NonNull VirtualMachine vm, @DeathReason int reason);
/** Called when kernel panic occurs and as a result ramdump is generated from the VM. */
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java b/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
index 7b5d5ab..e0f74ec 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachineConfig.java
@@ -35,6 +35,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+import java.util.Objects;
import java.util.regex.Pattern;
/**
@@ -111,8 +112,6 @@
*/
private final @NonNull String mPayloadConfigPath;
- // TODO(jiyong): add more items like # of cpu, size of ram, debuggability, etc.
-
private VirtualMachineConfig(
@NonNull String apkPath,
@NonNull Signature[] certs,
@@ -256,8 +255,8 @@
/** Creates a builder for the given context (APK), and the payload config file in APK. */
public Builder(@NonNull Context context, @NonNull String payloadConfigPath) {
- mContext = context;
- mPayloadConfigPath = payloadConfigPath;
+ mContext = Objects.requireNonNull(context);
+ mPayloadConfigPath = Objects.requireNonNull(payloadConfigPath);
mDebugLevel = DebugLevel.NONE;
mProtectedVm = false;
mNumCpus = 1;
diff --git a/javalib/src/android/system/virtualmachine/VirtualMachineManager.java b/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
index 51fa51f..eaa383e 100644
--- a/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
+++ b/javalib/src/android/system/virtualmachine/VirtualMachineManager.java
@@ -22,6 +22,7 @@
import java.lang.ref.WeakReference;
import java.util.Map;
+import java.util.Objects;
import java.util.WeakHashMap;
/**
@@ -40,6 +41,7 @@
/** Returns the per-context instance. */
public static @NonNull VirtualMachineManager getInstance(@NonNull Context context) {
+ Objects.requireNonNull(context);
synchronized (sInstances) {
VirtualMachineManager vmm =
sInstances.containsKey(context) ? sInstances.get(context).get() : null;