diff options
author | Timur Iskhodzhanov <timurrrr@google.com> | 2015-04-09 14:11:25 +0000 |
---|---|---|
committer | Timur Iskhodzhanov <timurrrr@google.com> | 2015-04-09 14:11:25 +0000 |
commit | 2f2def2a56cebf5bc2a77c5305cfe58158c50294 (patch) | |
tree | 4983a40ae1d802521c95c940860434c69c5c5535 /lib | |
parent | 422e4c2f5016dd46c53b14f3793b6257c01d928a (diff) | |
download | compiler-rt-2f2def2a56cebf5bc2a77c5305cfe58158c50294.tar.gz |
Use WriteToFile instead of internal_write in non-POSIX code
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@234487 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/asan/asan_posix.cc | 1 | ||||
-rw-r--r-- | lib/dfsan/dfsan.cc | 8 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_common.cc | 4 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_common.h | 6 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_coverage_libcdep.cc | 26 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc | 3 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_libc.h | 1 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_posix.cc | 10 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_posix.h | 1 | ||||
-rw-r--r-- | lib/sanitizer_common/sanitizer_win.cc | 31 | ||||
-rw-r--r-- | lib/tsan/rtl/tsan_rtl.cc | 2 |
11 files changed, 58 insertions, 35 deletions
diff --git a/lib/asan/asan_posix.cc b/lib/asan/asan_posix.cc index 4652ddee0..2e857f6f6 100644 --- a/lib/asan/asan_posix.cc +++ b/lib/asan/asan_posix.cc @@ -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> diff --git a/lib/dfsan/dfsan.cc b/lib/dfsan/dfsan.cc index 3ee904ca8..cd81b0bac 100644 --- a/lib/dfsan/dfsan.cc +++ b/lib/dfsan/dfsan.cc @@ -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); } } diff --git a/lib/sanitizer_common/sanitizer_common.cc b/lib/sanitizer_common/sanitizer_common.cc index 9a7e8b572..8185c81c2 100644 --- a/lib/sanitizer_common/sanitizer_common.cc +++ b/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/lib/sanitizer_common/sanitizer_common.h b/lib/sanitizer_common/sanitizer_common.h index a5770da2f..85f0dd68a 100644 --- a/lib/sanitizer_common/sanitizer_common.h +++ b/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/lib/sanitizer_common/sanitizer_coverage_libcdep.cc b/lib/sanitizer_common/sanitizer_coverage_libcdep.cc index b335838ad..ae3c08075 100644 --- a/lib/sanitizer_common/sanitizer_coverage_libcdep.cc +++ b/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/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc b/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc index efb6d6269..bed9614dc 100644 --- a/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc +++ b/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/lib/sanitizer_common/sanitizer_libc.h b/lib/sanitizer_common/sanitizer_libc.h index d3ac597f5..84bcfa494 100644 --- a/lib/sanitizer_common/sanitizer_libc.h +++ b/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/lib/sanitizer_common/sanitizer_posix.cc b/lib/sanitizer_common/sanitizer_posix.cc index b55ad3a86..1b0f25a88 100644 --- a/lib/sanitizer_common/sanitizer_posix.cc +++ b/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/lib/sanitizer_common/sanitizer_posix.h b/lib/sanitizer_common/sanitizer_posix.h index 1c7a87251..de01bce5b 100644 --- a/lib/sanitizer_common/sanitizer_posix.h +++ b/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/lib/sanitizer_common/sanitizer_win.cc b/lib/sanitizer_common/sanitizer_win.cc index c4adba8b1..c2bae0997 100644 --- a/lib/sanitizer_common/sanitizer_win.cc +++ b/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. diff --git a/lib/tsan/rtl/tsan_rtl.cc b/lib/tsan/rtl/tsan_rtl.cc index 5f17c712e..ee279a381 100644 --- a/lib/tsan/rtl/tsan_rtl.cc +++ b/lib/tsan/rtl/tsan_rtl.cc @@ -133,7 +133,7 @@ static void MemoryProfiler(Context *ctx, fd_t fd, int i) { 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) { |