Merge "Add notifyError/onError notification for VM errors"
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
index 00f34b9..a6b1f95 100644
--- a/PREUPLOAD.cfg
+++ b/PREUPLOAD.cfg
@@ -4,7 +4,6 @@
 bpfmt = true
 clang_format = true
 jsonlint = true
-google_java_format = true
 pylint3 = true
 rustfmt = true
 xmllint = true
diff --git a/apex/product_packages.mk b/apex/product_packages.mk
index e78d3dc..02d7989 100644
--- a/apex/product_packages.mk
+++ b/apex/product_packages.mk
@@ -24,5 +24,11 @@
     com.android.virt \
 
 # TODO(b/207336449): Figure out how to get these off /system
-PRODUCT_ARTIFACT_PATH_REQUIREMENT_ALLOWED_LIST += \
-     system/lib64/libgfxstream_backend.so \
+PRODUCT_ARTIFACT_PATH_REQUIREMENT_ALLOWED_LIST := \
+    system/lib64/libgfxstream_backend.so \
+    system/framework/oat/%@service-compos.jar@classes.odex \
+    system/framework/oat/%@service-compos.jar@classes.vdex \
+
+# PRODUCT_APEX_SYSTEM_SERVER_JARS := com.android.compos:service-compos
+
+# PRODUCT_SYSTEM_EXT_PROPERTIES := ro.config.isolated_compilation_enabled=true
diff --git a/compos/aidl/Android.bp b/compos/aidl/Android.bp
index 4d36d3d..7036511 100644
--- a/compos/aidl/Android.bp
+++ b/compos/aidl/Android.bp
@@ -9,6 +9,11 @@
         "com/android/compos/*.aidl",
     ],
     backend: {
+        java: {
+            apex_available: [
+                "com.android.compos",
+            ],
+        },
         rust: {
             enabled: true,
             apex_available: [
diff --git a/compos/apex/Android.bp b/compos/apex/Android.bp
index c68899a..43e75e4 100644
--- a/compos/apex/Android.bp
+++ b/compos/apex/Android.bp
@@ -51,6 +51,8 @@
         "compsvc",
     ],
 
+    systemserverclasspath_fragments: ["com.android.compos-systemserverclasspath-fragment"],
+
     apps: [
         "CompOSPayloadApp",
     ],
@@ -61,6 +63,12 @@
     ],
 }
 
+systemserverclasspath_fragment {
+    name: "com.android.compos-systemserverclasspath-fragment",
+    contents: ["service-compos"],
+    apex_available: ["com.android.compos"],
+}
+
 prebuilt_etc {
     name: "com.android.compos.init.rc",
     src: "composd.rc",
diff --git a/compos/composd/aidl/Android.bp b/compos/composd/aidl/Android.bp
index 62c1b40..376313b 100644
--- a/compos/composd/aidl/Android.bp
+++ b/compos/composd/aidl/Android.bp
@@ -9,7 +9,7 @@
     unstable: true,
     backend: {
         java: {
-            apex_available: ["//apex_available:platform"],
+            apex_available: ["com.android.compos"],
         },
         rust: {
             enabled: true,
diff --git a/compos/composd/aidl/android/system/composd/ICompilationTask.aidl b/compos/composd/aidl/android/system/composd/ICompilationTask.aidl
index ae03fcc..c1da0a5 100644
--- a/compos/composd/aidl/android/system/composd/ICompilationTask.aidl
+++ b/compos/composd/aidl/android/system/composd/ICompilationTask.aidl
@@ -23,5 +23,5 @@
      * Attempt to cancel compilation. If successful compilation will end and no further success or
      * failed callbacks will be received (although any in flight may still be delivered).
      */
-    void cancel();
+    oneway void cancel();
 }
diff --git a/compos/composd/aidl/android/system/composd/ICompilationTaskCallback.aidl b/compos/composd/aidl/android/system/composd/ICompilationTaskCallback.aidl
index a9d41b8..b334d8b 100644
--- a/compos/composd/aidl/android/system/composd/ICompilationTaskCallback.aidl
+++ b/compos/composd/aidl/android/system/composd/ICompilationTaskCallback.aidl
@@ -19,7 +19,7 @@
  * Interface to be implemented by clients of IIsolatedCompilationService to be notified when a
  * requested compilation task completes.
  */
-interface ICompilationTaskCallback {
+oneway interface ICompilationTaskCallback {
     /**
      * Called if a compilation task has ended successfully, generating all the required artifacts.
      */
diff --git a/compos/service/Android.bp b/compos/service/Android.bp
new file mode 100644
index 0000000..6270c9a
--- /dev/null
+++ b/compos/service/Android.bp
@@ -0,0 +1,37 @@
+// 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.
+
+java_library {
+    name: "service-compos",
+    srcs: [
+        "java/**/*.java",
+    ],
+    defaults: ["framework-system-server-module-defaults"],
+    permitted_packages: [
+        "com.android.server.compos",
+        "com.android.compos",
+        "android.system.composd",
+    ],
+    static_libs: [
+        "android.system.composd-java",
+    ],
+    apex_available: [
+        "com.android.compos",
+    ],
+    // Access to SystemService, ServiceManager#waitForService etc
+    libs: ["services"],
+    sdk_version: "",
+    platform_apis: true,
+    installable: true,
+}
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
new file mode 100644
index 0000000..2aacc2d
--- /dev/null
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationJobService.java
@@ -0,0 +1,195 @@
+/*
+ * Copyright 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.
+ */
+
+package com.android.server.compos;
+
+import static java.util.Objects.requireNonNull;
+
+import android.app.job.JobParameters;
+import android.app.job.JobService;
+import android.os.IBinder;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.system.composd.ICompilationTask;
+import android.system.composd.ICompilationTaskCallback;
+import android.system.composd.IIsolatedCompilationService;
+import android.util.Log;
+
+import java.util.concurrent.atomic.AtomicReference;
+
+/**
+ * A job scheduler service responsible for performing Isolated Compilation when scheduled.
+ *
+ * @hide
+ */
+public class IsolatedCompilationJobService extends JobService {
+    private static final String TAG = IsolatedCompilationJobService.class.getName();
+
+    private final AtomicReference<CompilationJob> mCurrentJob = new AtomicReference<>();
+
+    @Override
+    public boolean onStartJob(JobParameters params) {
+        Log.i(TAG, "starting job");
+
+        CompilationJob oldJob = mCurrentJob.getAndSet(null);
+        if (oldJob != null) {
+            // This should probably never happen, but just in case
+            oldJob.stop();
+        }
+
+        // This function (and onStopJob) are only ever called on the main thread, so we don't have
+        // to worry about two starts at once, or start and stop happening at once. But onCompletion
+        // can be called on any thread, so we need to be careful with that.
+
+        CompilationCallback callback = new CompilationCallback() {
+            @Override
+            public void onSuccess() {
+                onCompletion(params, true);
+            }
+
+            @Override
+            public void onFailure() {
+                onCompletion(params, false);
+            }
+        };
+        CompilationJob newJob = new CompilationJob(callback);
+        mCurrentJob.set(newJob);
+
+        try {
+            // This can take some time - we need to start up a VM - so we do it on a separate
+            // thread. This thread exits as soon as the compilation Ttsk has been started (or
+            // there's a failure), and then compilation continues in composd and the VM.
+            new Thread("IsolatedCompilationJob_starter") {
+                @Override
+                public void run() {
+                    newJob.start();
+                }
+            }.start();
+        } catch (RuntimeException e) {
+            Log.e(TAG, "Starting CompilationJob failed", e);
+            return false; // We're finished
+        }
+        return true; // Job is running in the background
+    }
+
+    @Override
+    public boolean onStopJob(JobParameters params) {
+        CompilationJob job = mCurrentJob.getAndSet(null);
+        if (job == null) {
+            return false; // No need to reschedule, we'd finished
+        } else {
+            job.stop();
+            return true; // We didn't get to finish, please re-schedule
+        }
+    }
+
+    void onCompletion(JobParameters params, boolean succeeded) {
+        Log.i(TAG, "onCompletion, succeeded=" + succeeded);
+
+        CompilationJob job = mCurrentJob.getAndSet(null);
+        if (job == null) {
+            // No need to call jobFinished if we've been told to stop.
+            return;
+        }
+        // On success we don't need to reschedule.
+        // On failure we could reschedule, but that could just use a lot of resources and still
+        // fail; instead we just let odsign do compilation on reboot if necessary.
+        jobFinished(params, /*wantReschedule=*/ false);
+    }
+
+    interface CompilationCallback {
+        void onSuccess();
+
+        void onFailure();
+    }
+
+    static class CompilationJob extends ICompilationTaskCallback.Stub
+            implements IBinder.DeathRecipient {
+        private final AtomicReference<ICompilationTask> mTask = new AtomicReference<>();
+        private final CompilationCallback mCallback;
+        private volatile boolean mStopRequested = false;
+        private volatile boolean mCanceled = false;
+
+        CompilationJob(CompilationCallback callback) {
+            mCallback = requireNonNull(callback);
+        }
+
+        void start() {
+            IBinder binder = ServiceManager.waitForService("android.system.composd");
+            IIsolatedCompilationService composd =
+                    IIsolatedCompilationService.Stub.asInterface(binder);
+
+            if (composd == null) {
+                throw new IllegalStateException("Unable to find composd service");
+            }
+
+            try {
+                ICompilationTask composTask = composd.startTestCompile(this);
+                mTask.set(composTask);
+                composTask.asBinder().linkToDeath(this, 0);
+            } catch (RemoteException e) {
+                throw e.rethrowAsRuntimeException();
+            }
+
+            if (mStopRequested) {
+                // We were asked to stop while we were starting the task. We need to
+                // cancel it now, since we couldn't before.
+                cancelTask();
+            }
+        }
+
+        void stop() {
+            mStopRequested = true;
+            cancelTask();
+        }
+
+        private void cancelTask() {
+            ICompilationTask task = mTask.getAndSet(null);
+            if (task != null) {
+                mCanceled = true;
+                Log.i(TAG, "Cancelling task");
+                try {
+                    task.cancel();
+                } catch (RuntimeException | RemoteException e) {
+                    // If canceling failed we'll assume it means that the task has already failed;
+                    // there's nothing else we can do anyway.
+                    Log.w(TAG, "Failed to cancel CompilationTask", e);
+                }
+            }
+        }
+
+        @Override
+        public void binderDied() {
+            onFailure();
+        }
+
+        @Override
+        public void onSuccess() {
+            mTask.set(null);
+            if (!mCanceled) {
+                mCallback.onSuccess();
+            }
+        }
+
+        @Override
+        public void onFailure() {
+            mTask.set(null);
+            if (!mCanceled) {
+                mCallback.onFailure();
+            }
+        }
+    }
+}
diff --git a/compos/service/java/com/android/server/compos/IsolatedCompilationService.java b/compos/service/java/com/android/server/compos/IsolatedCompilationService.java
new file mode 100644
index 0000000..cbc3371
--- /dev/null
+++ b/compos/service/java/com/android/server/compos/IsolatedCompilationService.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 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.
+ */
+
+package com.android.server.compos;
+
+import android.annotation.NonNull;
+import android.app.job.JobInfo;
+import android.app.job.JobScheduler;
+import android.content.ComponentName;
+import android.content.Context;
+import android.provider.DeviceConfig;
+import android.util.Log;
+
+import com.android.server.SystemService;
+
+import java.io.File;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * A system service responsible for performing Isolated Compilation (compiling boot & system server
+ * classpath JARs in a protected VM) when appropriate.
+ *
+ * @hide
+ */
+public class IsolatedCompilationService extends SystemService {
+    private static final String TAG = IsolatedCompilationService.class.getName();
+    private static final int JOB_ID = 5132250;
+    private static final long JOB_PERIOD_MILLIS = TimeUnit.DAYS.toMillis(1);
+
+    public IsolatedCompilationService(@NonNull Context context) {
+        super(context);
+    }
+
+    @Override
+    public void onStart() {
+        // Note that our binder service is exposed directly from native code in composd, so
+        // we don't need to do anything here.
+    }
+
+    @Override
+    public void onBootPhase(/* @BootPhase */ int phase) {
+        if (phase != PHASE_BOOT_COMPLETED) return;
+
+        if (!isIsolatedCompilationSupported()) {
+            Log.i(TAG, "Isolated compilation not supported, not scheduling job");
+            return;
+        }
+
+        ComponentName serviceName =
+                new ComponentName("android", IsolatedCompilationJobService.class.getName());
+
+        JobScheduler scheduler = getContext().getSystemService(JobScheduler.class);
+        if (scheduler == null) {
+            Log.e(TAG, "No scheduler");
+            return;
+        }
+        int result =
+                scheduler.schedule(
+                        new JobInfo.Builder(JOB_ID, serviceName)
+                                .setRequiresDeviceIdle(true)
+                                .setRequiresCharging(true)
+                                .setPeriodic(JOB_PERIOD_MILLIS)
+                                .build());
+        if (result != JobScheduler.RESULT_SUCCESS) {
+            Log.e(TAG, "Failed to schedule job");
+        }
+    }
+
+    private static boolean isIsolatedCompilationSupported() {
+        // Check that the relevant experiment is enabled on this device
+        // TODO - Remove this once we are ready for wider use.
+        if (!DeviceConfig.getBoolean(
+                "virtualization_framework_native", "isolated_compilation_enabled", false)) {
+            return false;
+        }
+
+        // Check that KVM is enabled on the device
+        if (!new File("/dev/kvm").exists()) {
+            return false;
+        }
+
+        return true;
+    }
+}