[compiler-rt] r234487 - Use WriteToFile instead of internal_write in non-POSIX code

Timur Iskhodzhanov timurrrr at google.com
Thu Apr 9 07:11:26 PDT 2015


Author: timurrrr
Date: Thu Apr  9 09:11:25 2015
New Revision: 234487

URL: http://llvm.org/viewvc/llvm-project?rev=234487&view=rev
Log:
Use WriteToFile instead of internal_write in non-POSIX code

Modified:
    compiler-rt/trunk/lib/asan/asan_posix.cc
    compiler-rt/trunk/lib/dfsan/dfsan.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
    compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc

Modified: compiler-rt/trunk/lib/asan/asan_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_posix.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_posix.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_posix.cc Thu Apr  9 09:11:25 2015
@@ -21,6 +21,7 @@
 #include "asan_report.h"
 #include "asan_stack.h"
 #include "sanitizer_common/sanitizer_libc.h"
+#include "sanitizer_common/sanitizer_posix.h"
 #include "sanitizer_common/sanitizer_procmaps.h"
 
 #include <pthread.h>

Modified: compiler-rt/trunk/lib/dfsan/dfsan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/dfsan/dfsan.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/dfsan/dfsan.cc (original)
+++ compiler-rt/trunk/lib/dfsan/dfsan.cc Thu Apr  9 09:11:25 2015
@@ -309,12 +309,12 @@ dfsan_dump_labels(int fd) {
     char buf[64];
     internal_snprintf(buf, sizeof(buf), "%u %u %u ", l,
                       __dfsan_label_info[l].l1, __dfsan_label_info[l].l2);
-    internal_write(fd, buf, internal_strlen(buf));
+    WriteToFile(fd, buf, internal_strlen(buf));
     if (__dfsan_label_info[l].l1 == 0 && __dfsan_label_info[l].desc) {
-      internal_write(fd, __dfsan_label_info[l].desc,
-                     internal_strlen(__dfsan_label_info[l].desc));
+      WriteToFile(fd, __dfsan_label_info[l].desc,
+                  internal_strlen(__dfsan_label_info[l].desc));
     }
-    internal_write(fd, "\n", 1);
+    WriteToFile(fd, "\n", 1);
   }
 }
 

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.cc Thu Apr  9 09:11:25 2015
@@ -61,8 +61,8 @@ void ReportFile::ReopenIfNecessary() {
   fd = OpenFile(full_path, WrOnly);
   if (fd == kInvalidFd) {
     const char *ErrorMsgPrefix = "ERROR: Can't open file: ";
-    internal_write(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
-    internal_write(kStderrFd, full_path, internal_strlen(full_path));
+    WriteToFile(kStderrFd, ErrorMsgPrefix, internal_strlen(ErrorMsgPrefix));
+    WriteToFile(kStderrFd, full_path, internal_strlen(full_path));
     Die();
   }
   fd_pid = pid;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Thu Apr  9 09:11:25 2015
@@ -200,9 +200,13 @@ enum FileAccessMode {
 fd_t OpenFile(const char *filename, FileAccessMode mode,
               error_t *errno_p = nullptr);
 void CloseFile(fd_t);
-// Returns true on success, false on error.
+
+// Return true on success, false on error.
 bool ReadFromFile(fd_t fd, void *buff, uptr buff_size,
                   uptr *bytes_read = nullptr, error_t *error_p = nullptr);
+bool WriteToFile(fd_t fd, const void *buff, uptr buff_size,
+                 uptr *bytes_written = nullptr, error_t *error_p = nullptr);
+
 bool SupportsColoredOutput(fd_t fd);
 
 // Opens the file 'file_name" and reads up to 'max_len' bytes.

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_libcdep.cc Thu Apr  9 09:11:25 2015
@@ -522,9 +522,9 @@ static void CovWritePacked(int pid, cons
 
   if (cov_max_block_size == 0) {
     // Writing to a file. Just go ahead.
-    internal_write(cov_fd, &header, sizeof(header));
-    internal_write(cov_fd, module, module_name_length);
-    internal_write(cov_fd, blob, blob_size);
+    WriteToFile(cov_fd, &header, sizeof(header));
+    WriteToFile(cov_fd, module, module_name_length);
+    WriteToFile(cov_fd, blob, blob_size);
   } else {
     // Writing to a socket. We want to split the data into appropriately sized
     // blocks.
@@ -547,8 +547,7 @@ static void CovWritePacked(int pid, cons
       internal_memcpy(block_data_begin, blob_pos, payload_size);
       blob_pos += payload_size;
       ((CovHeader *)block.data())->data_length = payload_size;
-      internal_write(cov_fd, block.data(),
-                     header_size_with_module + payload_size);
+      WriteToFile(cov_fd, block.data(), header_size_with_module + payload_size);
     }
   }
 }
@@ -595,7 +594,7 @@ void CoverageData::DumpTrace() {
   InternalScopedString path(kMaxPathLength);
   fd_t fd = CovOpenFile(&path, false, "trace-points");
   if (fd == kInvalidFd) return;
-  internal_write(fd, out.data(), out.length());
+  WriteToFile(fd, out.data(), out.length());
   CloseFile(fd);
 
   fd = CovOpenFile(&path, false, "trace-compunits");
@@ -603,7 +602,7 @@ void CoverageData::DumpTrace() {
   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);
-  internal_write(fd, out.data(), out.length());
+  WriteToFile(fd, out.data(), out.length());
   CloseFile(fd);
 
   fd = CovOpenFile(&path, false, "trace-events");
@@ -612,8 +611,9 @@ void CoverageData::DumpTrace() {
   u8 *event_bytes = reinterpret_cast<u8*>(tr_event_array);
   // The trace file could be huge, and may not be written with a single syscall.
   while (bytes_to_write) {
-    uptr actually_written = internal_write(fd, event_bytes, bytes_to_write);
-    if (actually_written <= bytes_to_write) {
+    uptr actually_written;
+    if (WriteToFile(fd, event_bytes, bytes_to_write, &actually_written) &&
+        actually_written <= bytes_to_write) {
       bytes_to_write -= actually_written;
       event_bytes += actually_written;
     } else {
@@ -660,7 +660,7 @@ void CoverageData::DumpCallerCalleePairs
   InternalScopedString path(kMaxPathLength);
   fd_t fd = CovOpenFile(&path, false, "caller-callee");
   if (fd == kInvalidFd) return;
-  internal_write(fd, out.data(), out.length());
+  WriteToFile(fd, out.data(), out.length());
   CloseFile(fd);
   VReport(1, " CovDump: %zd caller-callee pairs written\n", total);
 }
@@ -695,7 +695,7 @@ void CoverageData::DumpCounters() {
     fd_t fd =
         CovOpenFile(&path, /* packed */ false, base_name, "counters-sancov");
     if (fd == kInvalidFd) return;
-    internal_write(fd, bitset.data() + r.beg, r.end - r.beg);
+    WriteToFile(fd, bitset.data() + r.beg, r.end - r.beg);
     CloseFile(fd);
     VReport(1, " CovDump: %zd counters written for '%s'\n", r.end - r.beg,
             base_name);
@@ -722,7 +722,7 @@ void CoverageData::DumpAsBitSet() {
     const char *base_name = StripModuleName(r.copied_module_name);
     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);
+    WriteToFile(fd, out.data() + r.beg, r.end - r.beg);
     CloseFile(fd);
     VReport(1,
             " CovDump: bitset of %zd bits written for '%s', %zd bits are set\n",
@@ -777,7 +777,7 @@ void CoverageData::DumpOffsets() {
       // One file per module per process.
       fd_t fd = CovOpenFile(&path, false /* packed */, module_name);
       if (fd == kInvalidFd) continue;
-      internal_write(fd, offsets.data(), offsets.size() * sizeof(offsets[0]));
+      WriteToFile(fd, offsets.data(), offsets.size() * sizeof(offsets[0]));
       CloseFile(fd);
       VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), num_offsets);
     }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc Thu Apr  9 09:11:25 2015
@@ -108,8 +108,7 @@ void CovUpdateMapping(const char *covera
     Die();
   }
 
-  res = internal_write(map_fd, text.data(), text.length());
-  if (internal_iserror(res, &err)) {
+  if (!WriteToFile(map_fd, text.data(), text.length(), nullptr, &err)) {
     Printf("sancov.map write failed: %d\n", err);
     Die();
   }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h Thu Apr  9 09:11:25 2015
@@ -63,7 +63,6 @@ const fd_t kStdinFd = 0;
 const fd_t kStdoutFd = 1;
 const fd_t kStderrFd = 2;
 
-uptr internal_write(fd_t fd, const void *buf, uptr count);
 uptr internal_ftruncate(fd_t fd, uptr size);
 
 // OS

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.cc Thu Apr  9 09:11:25 2015
@@ -233,6 +233,16 @@ bool ReadFromFile(fd_t fd, void *buff, u
   return true;
 }
 
+bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
+                 error_t *error_p) {
+  uptr res = internal_write(fd, buff, buff_size);
+  if (internal_iserror(res, error_p))
+    return false;
+  if (bytes_written)
+    *bytes_written = res;
+  return true;
+}
+
 void *MapFileToMemory(const char *file_name, uptr *buff_size) {
   fd_t fd = OpenFile(file_name, RdOnly);
   CHECK(fd != kInvalidFd);

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h Thu Apr  9 09:11:25 2015
@@ -31,6 +31,7 @@ uptr internal_open(const char *filename,
 uptr internal_close(fd_t fd);
 
 uptr internal_read(fd_t fd, void *buf, uptr count);
+uptr internal_write(fd_t fd, const void *buf, uptr count);
 
 // Memory
 uptr internal_mmap(void *addr, uptr length, int prot, int flags,

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Thu Apr  9 09:11:25 2015
@@ -413,14 +413,17 @@ bool SupportsColoredOutput(fd_t fd) {
   return false;
 }
 
-uptr internal_write(fd_t fd, const void *buf, uptr count) {
+bool WriteToFile(fd_t fd, const void *buff, uptr buff_size, uptr *bytes_written,
+                 error_t *error_p) {
   if (fd != kStderrFd)
     UNIMPLEMENTED();
 
   static HANDLE output_stream = 0;
   // Abort immediately if we know printing is not possible.
-  if (output_stream == INVALID_HANDLE_VALUE)
-    return 0;
+  if (output_stream == INVALID_HANDLE_VALUE) {
+    if (error_p) *error_p = ERROR_INVALID_HANDLE;
+    return false;
+  }
 
   // If called for the first time, try to use stderr to output stuff,
   // falling back to stdout if anything goes wrong.
@@ -436,8 +439,10 @@ uptr internal_write(fd_t fd, const void
       output_stream = GetStdHandle(STD_OUTPUT_HANDLE);
       if (output_stream == 0)
         output_stream = INVALID_HANDLE_VALUE;
-      if (output_stream == INVALID_HANDLE_VALUE)
-        return 0;
+      if (output_stream == INVALID_HANDLE_VALUE) {
+        if (error_p) *error_p = ERROR_INVALID_HANDLE;
+        return false;
+      }
     } else {
       // Successfully got an stderr handle.  However, if WriteFile() fails,
       // we can still try to fallback to stdout.
@@ -445,9 +450,11 @@ uptr internal_write(fd_t fd, const void
     }
   }
 
-  DWORD ret;
-  if (WriteFile(output_stream, buf, count, &ret, 0))
-    return ret;
+  DWORD internal_bytes_written;
+  if (WriteFile(output_stream, buff, buff_size, &internal_bytes_written, 0)) {
+    if (bytes_written) *bytes_written = internal_bytes_written;
+    return true;
+  }
 
   // Re-try with stdout if using a valid stderr handle fails.
   if (fallback_to_stdout) {
@@ -455,9 +462,11 @@ uptr internal_write(fd_t fd, const void
     if (output_stream == 0)
       output_stream = INVALID_HANDLE_VALUE;
     if (output_stream != INVALID_HANDLE_VALUE)
-      return internal_write(fd, buf, count);
+      return WriteToFile(fd, buff, buff_size, bytes_written, error_p);
   }
-  return 0;
+
+  if (error_p) *error_p = GetLastError();
+  return false;
 }
 
 uptr internal_sched_yield() {
@@ -600,7 +609,7 @@ void BufferedStackTrace::SlowUnwindStack
 void ReportFile::Write(const char *buffer, uptr length) {
   SpinMutexLock l(mu);
   ReopenIfNecessary();
-  if (length != internal_write(fd, buffer, length)) {
+  if (!WriteToFile(fd, buffer, length)) {
     // stderr may be closed, but we may be able to print to the debugger
     // instead.  This is the case when launching a program from Visual Studio,
     // and the following routine should write to its console.

Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc?rev=234487&r1=234486&r2=234487&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl.cc Thu Apr  9 09:11:25 2015
@@ -133,7 +133,7 @@ static void MemoryProfiler(Context *ctx,
   ctx->thread_registry->GetNumberOfThreads(&n_threads, &n_running_threads);
   InternalScopedBuffer<char> buf(4096);
   WriteMemoryProfile(buf.data(), buf.size(), n_threads, n_running_threads);
-  internal_write(fd, buf.data(), internal_strlen(buf.data()));
+  WriteToFile(fd, buf.data(), internal_strlen(buf.data()));
 }
 
 static void BackgroundThread(void *arg) {





More information about the llvm-commits mailing list