[sanitizer] Read file to InternalMmapVectorNoCtor

llvm-svn: 331791
This commit is contained in:
Vitaly Buka 2018-05-08 18:35:11 +00:00
parent 6364288dba
commit e21fbe5af2
2 changed files with 52 additions and 9 deletions

View File

@ -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) {

View File

@ -131,6 +131,43 @@ bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
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) {