[Sanitizers] Make OpenFile more portable

llvm-svn: 234410
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
index cfd9e7e..5223557 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
@@ -63,7 +63,7 @@
 // dump current memory layout to another file.
 
 static bool cov_sandboxed = false;
-static int cov_fd = kInvalidFd;
+static fd_t cov_fd = kInvalidFd;
 static unsigned int cov_max_block_size = 0;
 static bool coverage_enabled = false;
 static const char *coverage_dir;
@@ -124,7 +124,7 @@
   // Current file mapped size of the pc array.
   uptr pc_array_mapped_size;
   // Descriptor of the file mapped pc array.
-  int pc_fd;
+  fd_t pc_fd;
 
   // Vector of coverage guard arrays, protected by mu.
   InternalMmapVectorNoCtor<s32*> guard_array_vec;
@@ -177,7 +177,7 @@
   internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.raw",
                     coverage_dir, internal_getpid());
   pc_fd = OpenFile(path.data(), RdWr);
-  if (internal_iserror(pc_fd)) {
+  if (pc_fd == kInvalidFd) {
     Report("Coverage: failed to open %s for reading/writing\n", path.data());
     Die();
   }
@@ -516,7 +516,7 @@
 
 static void CovWritePacked(int pid, const char *module, const void *blob,
                            unsigned int blob_size) {
-  if (cov_fd < 0) return;
+  if (cov_fd == kInvalidFd) return;
   unsigned module_name_length = internal_strlen(module);
   CovHeader header = {pid, module_name_length, blob_size};
 
@@ -557,7 +557,7 @@
 // If packed = true and name == 0: <pid>.<sancov>.<packed>.
 // If packed = true and name != 0: <name>.<sancov>.<packed> (name is
 // user-supplied).
-static int CovOpenFile(InternalScopedString *path, bool packed,
+static fd_t CovOpenFile(InternalScopedString *path, bool packed,
                        const char *name, const char *extension = "sancov") {
   path->clear();
   if (!packed) {
@@ -571,11 +571,9 @@
     else
       path->append("%s/%s.%s.packed", coverage_dir, name, extension);
   }
-  uptr fd = OpenFile(path->data(), WrOnly);
-  if (internal_iserror(fd)) {
+  fd_t fd = OpenFile(path->data(), WrOnly);
+  if (fd == kInvalidFd)
     Report("SanitizerCoverage: failed to open %s for writing\n", path->data());
-    return -1;
-  }
   return fd;
 }
 
@@ -595,13 +593,13 @@
     out.append("%s 0x%zx\n", module_name, module_address);
   }
   InternalScopedString path(kMaxPathLength);
-  int fd = CovOpenFile(&path, false, "trace-points");
-  if (fd < 0) return;
+  fd_t fd = CovOpenFile(&path, false, "trace-points");
+  if (fd == kInvalidFd) return;
   internal_write(fd, out.data(), out.length());
   internal_close(fd);
 
   fd = CovOpenFile(&path, false, "trace-compunits");
-  if (fd < 0) return;
+  if (fd == kInvalidFd) return;
   out.clear();
   for (uptr i = 0; i < comp_unit_name_vec.size(); i++)
     out.append("%s\n", comp_unit_name_vec[i].copied_module_name);
@@ -609,7 +607,7 @@
   internal_close(fd);
 
   fd = CovOpenFile(&path, false, "trace-events");
-  if (fd < 0) return;
+  if (fd == kInvalidFd) return;
   uptr bytes_to_write = max_idx * sizeof(tr_event_array[0]);
   u8 *event_bytes = reinterpret_cast<u8*>(tr_event_array);
   // The trace file could be huge, and may not be written with a single syscall.
@@ -660,8 +658,8 @@
     }
   }
   InternalScopedString path(kMaxPathLength);
-  int fd = CovOpenFile(&path, false, "caller-callee");
-  if (fd < 0) return;
+  fd_t fd = CovOpenFile(&path, false, "caller-callee");
+  if (fd == kInvalidFd) return;
   internal_write(fd, out.data(), out.length());
   internal_close(fd);
   VReport(1, " CovDump: %zd caller-callee pairs written\n", total);
@@ -694,9 +692,9 @@
     CHECK_LE(r.beg, r.end);
     CHECK_LE(r.end, size());
     const char *base_name = StripModuleName(r.copied_module_name);
-    int fd =
+    fd_t fd =
         CovOpenFile(&path, /* packed */ false, base_name, "counters-sancov");
-    if (fd < 0) return;
+    if (fd == kInvalidFd) return;
     internal_write(fd, bitset.data() + r.beg, r.end - r.beg);
     internal_close(fd);
     VReport(1, " CovDump: %zd counters written for '%s'\n", r.end - r.beg,
@@ -722,8 +720,8 @@
         n_set_bits++;
     }
     const char *base_name = StripModuleName(r.copied_module_name);
-    int fd = CovOpenFile(&path, /* packed */ false, base_name, "bitset-sancov");
-    if (fd < 0) return;
+    fd_t fd = CovOpenFile(&path, /* packed */false, base_name, "bitset-sancov");
+    if (fd == kInvalidFd) return;
     internal_write(fd, out.data() + r.beg, r.end - r.beg);
     internal_close(fd);
     VReport(1,
@@ -770,21 +768,21 @@
 
     const char *module_name = StripModuleName(r.copied_module_name);
     if (cov_sandboxed) {
-      if (cov_fd >= 0) {
+      if (cov_fd != kInvalidFd) {
         CovWritePacked(internal_getpid(), module_name, offsets.data(),
                        offsets.size() * sizeof(offsets[0]));
         VReport(1, " CovDump: %zd PCs written to packed file\n", num_offsets);
       }
     } else {
       // One file per module per process.
-      int fd = CovOpenFile(&path, false /* packed */, module_name);
-      if (fd < 0) continue;
+      fd_t fd = CovOpenFile(&path, false /* packed */, module_name);
+      if (fd == kInvalidFd) continue;
       internal_write(fd, offsets.data(), offsets.size() * sizeof(offsets[0]));
       internal_close(fd);
       VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), num_offsets);
     }
   }
-  if (cov_fd >= 0)
+  if (cov_fd != kInvalidFd)
     internal_close(cov_fd);
 }
 
@@ -804,16 +802,17 @@
   if (!coverage_enabled) return;
   cov_sandboxed = args->coverage_sandboxed;
   if (!cov_sandboxed) return;
-  cov_fd = args->coverage_fd;
   cov_max_block_size = args->coverage_max_block_size;
-  if (cov_fd < 0) {
+  if (args->coverage_fd >= 0) {
+    cov_fd = args->coverage_fd;
+  } else {
     InternalScopedString path(kMaxPathLength);
     // Pre-open the file now. The sandbox won't allow us to do it later.
     cov_fd = CovOpenFile(&path, true /* packed */, 0);
   }
 }
 
-int MaybeOpenCovFile(const char *name) {
+fd_t MaybeOpenCovFile(const char *name) {
   CHECK(name);
   if (!coverage_enabled) return -1;
   InternalScopedString path(kMaxPathLength);
@@ -890,7 +889,7 @@
 }
 SANITIZER_INTERFACE_ATTRIBUTE
 sptr __sanitizer_maybe_open_cov_file(const char *name) {
-  return MaybeOpenCovFile(name);
+  return (sptr)MaybeOpenCovFile(name);
 }
 SANITIZER_INTERFACE_ATTRIBUTE
 uptr __sanitizer_get_total_unique_coverage() {