ART: Sync oat file to disk before patching

Bug: 15567083, 17439961, 17510489, 17478752

(cherry picked from commit 035592c51fee2995a3d90a9043f91a7a3128df2b)

Change-Id: I828dc6775044b5050c2520eb097abe6a920fd3ee
diff --git a/dex2oat/dex2oat.cc b/dex2oat/dex2oat.cc
index be6f097..d97144e 100644
--- a/dex2oat/dex2oat.cc
+++ b/dex2oat/dex2oat.cc
@@ -435,6 +435,11 @@
       return nullptr;
     }
 
+    // Flush result to disk. Patching code will re-open the file (mmap), so ensure that our view
+    // of the file already made it there and won't be re-ordered with writes from PatchOat or
+    // image patching.
+    oat_file->Flush();
+
     if (!driver->IsImage() && driver->GetCompilerOptions().GetIncludePatchInformation()) {
       t2.NewTiming("Patching ELF");
       std::string error_msg;