[compiler-rt] r331791 - [sanitizer] Read file to InternalMmapVectorNoCtor

Vitaly Buka via llvm-commits llvm-commits at lists.llvm.org
Tue May 8 11:35:12 PDT 2018


Author: vitalybuka
Date: Tue May  8 11:35:11 2018
New Revision: 331791

URL: http://llvm.org/viewvc/llvm-project?rev=331791&view=rev
Log:
[sanitizer] Read file to InternalMmapVectorNoCtor

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_file.cc

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=331791&r1=331790&r2=331791&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common.h Tue May  8 11:35:11 2018
@@ -198,15 +198,6 @@ class ScopedErrorReportLock {
 extern uptr stoptheworld_tracer_pid;
 extern uptr stoptheworld_tracer_ppid;
 
-// Opens the file 'file_name" and reads up to 'max_len' bytes.
-// The resulting buffer is mmaped and stored in '*buff'.
-// The size of the mmaped region is stored in '*buff_size'.
-// The total number of read bytes is stored in '*read_len'.
-// Returns true if file was successfully opened and read.
-bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
-                      uptr *read_len, uptr max_len = 1 << 26,
-                      error_t *errno_p = nullptr);
-
 bool IsAccessibleMemoryRange(uptr beg, uptr size);
 
 // Error report formatting.
@@ -645,6 +636,21 @@ enum ModuleArch {
   kModuleArchARM64
 };
 
+// Opens the file 'file_name" and reads up to 'max_len' bytes.
+// The resulting buffer is mmaped and stored in '*buff'.
+// The size of the mmaped region is stored in '*buff_size'.
+// The total number of read bytes is stored in '*read_len'.
+// Returns true if file was successfully opened and read.
+bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
+                      uptr *read_len, uptr max_len = 1 << 26,
+                      error_t *errno_p = nullptr);
+// Opens the file 'file_name" and reads up to 'max_len' bytes.
+// The resulting buffer is mmaped and stored in '*buff'.
+// Returns true if file was successfully opened and read.
+bool ReadFileToBuffer(const char *file_name,
+                      InternalMmapVectorNoCtor<char> *buff,
+                      uptr max_len = 1 << 26, error_t *errno_p = nullptr);
+
 // When adding a new architecture, don't forget to also update
 // script/asan_symbolize.py and sanitizer_symbolizer_libcdep.cc.
 inline const char *ModuleArchToString(ModuleArch arch) {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_file.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_file.cc?rev=331791&r1=331790&r2=331791&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_file.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_file.cc Tue May  8 11:35:11 2018
@@ -131,6 +131,43 @@ bool ReadFileToBuffer(const char *file_n
   return true;
 }
 
+bool ReadFileToBuffer(const char *file_name,
+                      InternalMmapVectorNoCtor<char> *buff, uptr max_len,
+                      error_t *errno_p) {
+  uptr PageSize = GetPageSizeCached();
+  buff->clear();
+  // The files we usually open are not seekable, so try different buffer sizes.
+  for (uptr size = Max(PageSize, buff->capacity()); size <= max_len;
+       size *= 2) {
+    buff->resize(size);
+    fd_t fd = OpenFile(file_name, RdOnly, errno_p);
+    if (fd == kInvalidFd)
+      return false;
+    uptr read_len = 0;
+    // Read up to one page at a time.
+    bool reached_eof = false;
+    while (read_len + PageSize <= buff->size()) {
+      uptr just_read;
+      if (!ReadFromFile(fd, buff->data() + read_len, PageSize, &just_read,
+                        errno_p)) {
+        CloseFile(fd);
+        return false;
+      }
+      if (!just_read) {
+        reached_eof = true;
+        break;
+      }
+      read_len += just_read;
+    }
+    CloseFile(fd);
+    if (reached_eof) {  // We've read the whole file.
+      buff->resize(read_len);
+      break;
+    }
+  }
+  return true;
+}
+
 static const char kPathSeparator = SANITIZER_WINDOWS ? ';' : ':';
 
 char *FindPathToBinary(const char *name) {




More information about the llvm-commits mailing list