summaryrefslogtreecommitdiffstats
path: root/compiler-rt/lib/sanitizer_common
diff options
context:
space:
mode:
Diffstat (limited to 'compiler-rt/lib/sanitizer_common')
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_common.cc4
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_common.h6
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc26
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc3
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_libc.h1
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_posix.cc10
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_posix.h1
-rw-r--r--compiler-rt/lib/sanitizer_common/sanitizer_win.cc31
8 files changed, 52 insertions, 30 deletions
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
index 9a7e8b57206..8185c81c254 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
@@ -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;
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index a5770da2f81..85f0dd68ad2 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -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.
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
index b335838ad3b..ae3c08075b4 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc
@@ -522,9 +522,9 @@ static void CovWritePacked(int pid, const char *module, const void *blob,
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, const char *module, const void *blob,
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);
}
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
index efb6d626985..bed9614dce3 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
@@ -108,8 +108,7 @@ void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) {
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();
}
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
index d3ac597f583..84bcfa49451 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h
@@ -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
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
index b55ad3a8644..1b0f25a88f1 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc
@@ -233,6 +233,16 @@ bool ReadFromFile(fd_t fd, void *buff, uptr buff_size, uptr *bytes_read,
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);
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
index 1c7a87251bb..de01bce5bc0 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h
@@ -31,6 +31,7 @@ uptr internal_open(const char *filename, int flags, u32 mode);
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,
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc
index c4adba8b1e2..c2bae09972a 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc
@@ -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 *buf, uptr count) {
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 *buf, uptr count) {
}
}
- 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 *buf, uptr count) {
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::SlowUnwindStackWithContext(uptr pc, void *context,
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.
OpenPOWER on IntegriCloud