ART: Fix android-cloexec warnings

Use the DupCloexec helper wherever possible. Add O_CLOEXEC to open
and fopen calls.

Bug: 32619234
Test: WITH_TIDY=1 mmma art
Change-Id: I0afb1beea53ab8f68ab85d1762aff999903060fe
diff --git a/runtime/hprof/hprof.cc b/runtime/hprof/hprof.cc
index f696e25..e8d283e 100644
--- a/runtime/hprof/hprof.cc
+++ b/runtime/hprof/hprof.cc
@@ -41,6 +41,7 @@
 #include "art_field-inl.h"
 #include "art_method-inl.h"
 #include "base/array_ref.h"
+#include "base/file_utils.h"
 #include "base/globals.h"
 #include "base/macros.h"
 #include "base/mutex.h"
@@ -761,13 +762,13 @@
     // Where exactly are we writing to?
     int out_fd;
     if (fd_ >= 0) {
-      out_fd = dup(fd_);
+      out_fd = DupCloexec(fd_);
       if (out_fd < 0) {
         ThrowRuntimeException("Couldn't dump heap; dup(%d) failed: %s", fd_, strerror(errno));
         return false;
       }
     } else {
-      out_fd = open(filename_.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0644);
+      out_fd = open(filename_.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
       if (out_fd < 0) {
         ThrowRuntimeException("Couldn't dump heap; open(\"%s\") failed: %s", filename_.c_str(),
                               strerror(errno));
diff --git a/runtime/monitor_android.cc b/runtime/monitor_android.cc
index 74623da..19e1f3d 100644
--- a/runtime/monitor_android.cc
+++ b/runtime/monitor_android.cc
@@ -43,7 +43,7 @@
 
   // Emit the process name, <= 37 bytes.
   {
-    int fd = open("/proc/self/cmdline", O_RDONLY);
+    int fd = open("/proc/self/cmdline", O_RDONLY  | O_CLOEXEC);
     char procName[33];
     memset(procName, 0, sizeof(procName));
     read(fd, procName, sizeof(procName) - 1);
diff --git a/runtime/native/dalvik_system_VMDebug.cc b/runtime/native/dalvik_system_VMDebug.cc
index 6f98a6d..96f15de 100644
--- a/runtime/native/dalvik_system_VMDebug.cc
+++ b/runtime/native/dalvik_system_VMDebug.cc
@@ -23,6 +23,7 @@
 
 #include "nativehelper/jni_macros.h"
 
+#include "base/file_utils.h"
 #include "base/histogram-inl.h"
 #include "base/time_utils.h"
 #include "class_linker.h"
@@ -113,7 +114,7 @@
     return;
   }
 
-  int fd = dup(originalFd);
+  int fd = DupCloexec(originalFd);
   if (fd < 0) {
     ScopedObjectAccess soa(env);
     soa.Self()->ThrowNewExceptionF("Ljava/lang/RuntimeException;",
diff --git a/runtime/oat_file_assistant_test.cc b/runtime/oat_file_assistant_test.cc
index 3a974df..aba2eae 100644
--- a/runtime/oat_file_assistant_test.cc
+++ b/runtime/oat_file_assistant_test.cc
@@ -338,9 +338,9 @@
                      CompilerFilter::kSpeed,
                      /* with_alternate_image */ false);
 
-  android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
-  android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
-  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+  android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
+  android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
+  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
 
   OatFileAssistant oat_file_assistant(dex_location.c_str(),
                                       kRuntimeISA,
@@ -377,8 +377,8 @@
                      CompilerFilter::kSpeed,
                      /* with_alternate_image */ false);
 
-  android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY));
-  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+  android::base::unique_fd vdex_fd(open(vdex_location.c_str(), O_RDONLY | O_CLOEXEC));
+  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
 
   OatFileAssistant oat_file_assistant(dex_location.c_str(),
                                       kRuntimeISA,
@@ -410,8 +410,8 @@
                      CompilerFilter::kSpeed,
                      /* with_alternate_image */ false);
 
-  android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY));
-  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+  android::base::unique_fd odex_fd(open(odex_location.c_str(), O_RDONLY | O_CLOEXEC));
+  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
 
   OatFileAssistant oat_file_assistant(dex_location.c_str(),
                                       kRuntimeISA,
@@ -436,7 +436,7 @@
 
   Copy(GetDexSrc1(), dex_location);
 
-  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY));
+  android::base::unique_fd zip_fd(open(dex_location.c_str(), O_RDONLY | O_CLOEXEC));
   OatFileAssistant oat_file_assistant(dex_location.c_str(),
                                       kRuntimeISA,
                                       false,