vulkan: Add functions to test unclean exit of vulkan guest processes

Bug: 168811024

Change-Id: If1584a166777919971d32daa3eedcfd1665e799c
diff --git a/system/OpenglSystemCommon/AddressSpaceStream.cpp b/system/OpenglSystemCommon/AddressSpaceStream.cpp
index ba9f289..b4586b2 100644
--- a/system/OpenglSystemCommon/AddressSpaceStream.cpp
+++ b/system/OpenglSystemCommon/AddressSpaceStream.cpp
@@ -298,6 +298,9 @@
 }
 
 AddressSpaceStream::~AddressSpaceStream() {
+    flush();
+    ensureType3Finished();
+    ensureType1Finished();
     if (!m_virtioMode) {
         m_ops.unmap(m_context.to_host, sizeof(struct asg_ring_storage));
         m_ops.unmap(m_context.buffer, m_writeBufferSize);
diff --git a/system/OpenglSystemCommon/HostConnection.cpp b/system/OpenglSystemCommon/HostConnection.cpp
index 540f6c4..7420e3e 100644
--- a/system/OpenglSystemCommon/HostConnection.cpp
+++ b/system/OpenglSystemCommon/HostConnection.cpp
@@ -358,6 +358,7 @@
 static GoldfishProcessPipe m_goldfishProcessPipe;
 
 HostConnection::HostConnection() :
+    exitUncleanly(false),
     m_checksumHelper(),
     m_glExtensions(),
     m_grallocOnly(true),
@@ -373,9 +374,10 @@
 {
     // round-trip to ensure that queued commands have been processed
     // before process pipe closure is detected.
-    if (m_rcEnc) {
+    if (m_rcEnc && !exitUncleanly) {
         (void)m_rcEnc->rcGetRendererVersion(m_rcEnc.get());
     }
+
     if (m_grallocType == GRALLOC_TYPE_MINIGBM) {
         delete m_grallocHelper;
     }
@@ -587,6 +589,16 @@
     tinfo->hostConn.reset();
 }
 
+void HostConnection::exitUnclean() {
+    EGLThreadInfo *tinfo = getEGLThreadInfo();
+    if (!tinfo) {
+        return;
+    }
+
+    tinfo->hostConn->exitUncleanly = true;
+    tinfo->hostConn.reset();
+}
+
 // static
 std::unique_ptr<HostConnection> HostConnection::createUnique() {
     ALOGD("%s: call\n", __func__);
diff --git a/system/OpenglSystemCommon/HostConnection.h b/system/OpenglSystemCommon/HostConnection.h
index 1220628..46a17f9 100644
--- a/system/OpenglSystemCommon/HostConnection.h
+++ b/system/OpenglSystemCommon/HostConnection.h
@@ -143,6 +143,7 @@
     static HostConnection *get();
     static HostConnection *getWithThreadInfo(EGLThreadInfo* tInfo);
     static void exit();
+    static void exitUnclean(); // for testing purposes
 
     static std::unique_ptr<HostConnection> createUnique();
     HostConnection(const HostConnection&) = delete;
@@ -188,6 +189,8 @@
 #pragma clang diagnostic pop
 #endif
 
+    bool exitUncleanly; // for testing purposes
+
 private:
     // If the connection failed, |conn| is deleted.
     // Returns NULL if connection failed.
diff --git a/system/OpenglSystemCommon/ProcessPipe.cpp b/system/OpenglSystemCommon/ProcessPipe.cpp
index bca6388..e6e5c4b 100644
--- a/system/OpenglSystemCommon/ProcessPipe.cpp
+++ b/system/OpenglSystemCommon/ProcessPipe.cpp
@@ -212,3 +212,43 @@
 uint64_t getPuid() {
     return sProcUID;
 }
+
+void processPipeRestart() {
+    ALOGW("%s: restarting process pipe\n", __func__);
+    bool isPipe = false;
+
+    switch (sConnType) {
+        // TODO: Move those over too
+        case HOST_CONNECTION_QEMU_PIPE:
+        case HOST_CONNECTION_ADDRESS_SPACE:
+        case HOST_CONNECTION_TCP:
+        case HOST_CONNECTION_VIRTIO_GPU:
+            isPipe = true;
+            break;
+        case HOST_CONNECTION_VIRTIO_GPU_PIPE:
+        case HOST_CONNECTION_VIRTIO_GPU_ADDRESS_SPACE: {
+            isPipe = false;
+            break;
+        }
+    }
+
+    sProcUID = 0;
+
+    if (isPipe) {
+        if (qemu_pipe_valid(sProcPipe)) {
+            qemu_pipe_close(sProcPipe);
+            sProcPipe = 0;
+        }
+    } else {
+        delete sVirtioGpuPipeStream;
+        sVirtioGpuPipeStream = nullptr;
+    }
+
+    processPipeInitOnce();
+};
+
+void refreshHostConnection() {
+    HostConnection* hostConn = HostConnection::get();
+    ExtendedRCEncoderContext* rcEnc = hostConn->rcEncoder();
+    rcEnc->rcSetPuid(rcEnc, sProcUID);
+}
diff --git a/system/OpenglSystemCommon/ProcessPipe.h b/system/OpenglSystemCommon/ProcessPipe.h
index 20f7c9f..4f61d8f 100644
--- a/system/OpenglSystemCommon/ProcessPipe.h
+++ b/system/OpenglSystemCommon/ProcessPipe.h
@@ -35,3 +35,8 @@
 
 extern bool processPipeInit(HostConnectionType connType, renderControl_encoder_context_t *rcEnc);
 extern uint64_t getPuid();
+
+// For testing purposes; this will close the current process pipe if opened, reset the state to initial,
+// and open it again with the same parameters.
+extern void processPipeRestart();
+extern void refreshHostConnection();
diff --git a/system/gralloc/gralloc_old.cpp b/system/gralloc/gralloc_old.cpp
index 40f6b6c..b8f610b 100644
--- a/system/gralloc/gralloc_old.cpp
+++ b/system/gralloc/gralloc_old.cpp
@@ -997,6 +997,11 @@
         }
 
         delete d;
+
+        if (sHostCon) {
+            delete sHostCon;
+            sHostCon = nullptr;
+        }
     }
     return 0;
 }