From 864308a78de8d1478386f49c7d87636bf6ccb61b Mon Sep 17 00:00:00 2001 From: Timur Iskhodzhanov Date: Thu, 9 Apr 2015 12:37:05 +0000 Subject: [PATCH] Introduce CloseFile to be used instead of internal_close on non-POSIX llvm-svn: 234481 --- compiler-rt/lib/dfsan/dfsan.cc | 2 +- compiler-rt/lib/sanitizer_common/sanitizer_common.cc | 6 +++--- compiler-rt/lib/sanitizer_common/sanitizer_common.h | 1 + .../lib/sanitizer_common/sanitizer_coverage_libcdep.cc | 18 +++++++++--------- .../sanitizer_coverage_mapping_libcdep.cc | 2 +- compiler-rt/lib/sanitizer_common/sanitizer_libc.h | 1 - compiler-rt/lib/sanitizer_common/sanitizer_posix.cc | 4 ++++ compiler-rt/lib/sanitizer_common/sanitizer_posix.h | 1 + compiler-rt/lib/sanitizer_common/sanitizer_win.cc | 4 ++-- .../lib/sanitizer_common/tests/sanitizer_libc_test.cc | 6 +++--- 10 files changed, 25 insertions(+), 20 deletions(-) diff --git a/compiler-rt/lib/dfsan/dfsan.cc b/compiler-rt/lib/dfsan/dfsan.cc index 1b0dc8f..3ee904c 100644 --- a/compiler-rt/lib/dfsan/dfsan.cc +++ b/compiler-rt/lib/dfsan/dfsan.cc @@ -350,7 +350,7 @@ static void dfsan_fini() { Report("INFO: DataFlowSanitizer: dumping labels to %s\n", flags().dump_labels_at_exit); dfsan_dump_labels(fd); - internal_close(fd); + CloseFile(fd); } } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc index e499de0..d273056 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.cc @@ -54,7 +54,7 @@ void ReportFile::ReopenIfNecessary() { if (fd_pid == pid) return; else - internal_close(fd); + CloseFile(fd); } internal_snprintf(full_path, kMaxPathLength, "%s.%zu", path_prefix, pid); @@ -81,7 +81,7 @@ void ReportFile::SetReportPath(const char *path) { SpinMutexLock l(mu); if (fd != kStdoutFd && fd != kStderrFd && fd != kInvalidFd) - internal_close(fd); + CloseFile(fd); fd = kInvalidFd; if (internal_strcmp(path, "stdout") == 0) { fd = kStdoutFd; @@ -163,7 +163,7 @@ uptr ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size, } read_len += just_read; } - internal_close(fd); + CloseFile(fd); if (reached_eof) // We've read the whole file. break; } diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common.h b/compiler-rt/lib/sanitizer_common/sanitizer_common.h index 3e72882..e46ea81 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_common.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_common.h @@ -199,6 +199,7 @@ enum FileAccessMode { // Returns kInvalidFd on error. fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p = nullptr); +void CloseFile(fd_t); 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 64b7e4e..b335838 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_libcdep.cc @@ -245,7 +245,7 @@ void CoverageData::Disable() { tr_event_pointer = nullptr; } if (pc_fd != kInvalidFd) { - internal_close(pc_fd); + CloseFile(pc_fd); pc_fd = kInvalidFd; } } @@ -596,7 +596,7 @@ void CoverageData::DumpTrace() { fd_t fd = CovOpenFile(&path, false, "trace-points"); if (fd == kInvalidFd) return; internal_write(fd, out.data(), out.length()); - internal_close(fd); + CloseFile(fd); fd = CovOpenFile(&path, false, "trace-compunits"); if (fd == kInvalidFd) return; @@ -604,7 +604,7 @@ void CoverageData::DumpTrace() { 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()); - internal_close(fd); + CloseFile(fd); fd = CovOpenFile(&path, false, "trace-events"); if (fd == kInvalidFd) return; @@ -620,7 +620,7 @@ void CoverageData::DumpTrace() { break; } } - internal_close(fd); + CloseFile(fd); VReport(1, " CovDump: Trace: %zd PCs written\n", size()); VReport(1, " CovDump: Trace: %zd Events written\n", max_idx); } @@ -661,7 +661,7 @@ void CoverageData::DumpCallerCalleePairs() { fd_t fd = CovOpenFile(&path, false, "caller-callee"); if (fd == kInvalidFd) return; internal_write(fd, out.data(), out.length()); - internal_close(fd); + CloseFile(fd); VReport(1, " CovDump: %zd caller-callee pairs written\n", total); } @@ -696,7 +696,7 @@ void CoverageData::DumpCounters() { CovOpenFile(&path, /* packed */ false, base_name, "counters-sancov"); if (fd == kInvalidFd) return; internal_write(fd, bitset.data() + r.beg, r.end - r.beg); - internal_close(fd); + CloseFile(fd); VReport(1, " CovDump: %zd counters written for '%s'\n", r.end - r.beg, base_name); } @@ -723,7 +723,7 @@ void CoverageData::DumpAsBitSet() { 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); - internal_close(fd); + CloseFile(fd); VReport(1, " CovDump: bitset of %zd bits written for '%s', %zd bits are set\n", r.end - r.beg, base_name, n_set_bits); @@ -778,12 +778,12 @@ void CoverageData::DumpOffsets() { fd_t fd = CovOpenFile(&path, false /* packed */, module_name); if (fd == kInvalidFd) continue; internal_write(fd, offsets.data(), offsets.size() * sizeof(offsets[0])); - internal_close(fd); + CloseFile(fd); VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), num_offsets); } } if (cov_fd != kInvalidFd) - internal_close(cov_fd); + CloseFile(cov_fd); } void CoverageData::DumpAll() { 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 96244fe..efb6d62 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc @@ -113,7 +113,7 @@ void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) { Printf("sancov.map write failed: %d\n", err); Die(); } - internal_close(map_fd); + CloseFile(map_fd); InternalScopedString path(64 + internal_strlen(coverage_dir)); res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map", diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h index 6a05ba6..edd92fe 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_libc.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_libc.h @@ -62,7 +62,6 @@ const fd_t kInvalidFd = -1; const fd_t kStdinFd = 0; const fd_t kStdoutFd = 1; const fd_t kStderrFd = 2; -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); diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc index 77db0da..e0638e8 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.cc @@ -219,6 +219,10 @@ fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *errno_p) { return res; } +void CloseFile(fd_t fd) { + internal_close(fd); +} + 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 31bbb97..63d5f9e 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_posix.h +++ b/compiler-rt/lib/sanitizer_common/sanitizer_posix.h @@ -28,6 +28,7 @@ namespace __sanitizer { // Don't use directly, use __sanitizer::OpenFile() instead. uptr internal_open(const char *filename, int flags); uptr internal_open(const char *filename, int flags, u32 mode); +uptr internal_close(fd_t fd); // 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 3a129e3..7f1b934 100644 --- a/compiler-rt/lib/sanitizer_common/sanitizer_win.cc +++ b/compiler-rt/lib/sanitizer_common/sanitizer_win.cc @@ -395,11 +395,11 @@ static __declspec(allocate(".CRT$XID")) int (*__run_atexit)() = RunAtexit; #endif // ------------------ sanitizer_libc.h -uptr internal_close(fd_t fd) { +fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *last_error) { UNIMPLEMENTED(); } -fd_t OpenFile(const char *filename, FileAccessMode mode, error_t *last_error) { +void CloseFile(fd_t fd) { UNIMPLEMENTED(); } diff --git a/compiler-rt/lib/sanitizer_common/tests/sanitizer_libc_test.cc b/compiler-rt/lib/sanitizer_common/tests/sanitizer_libc_test.cc index bf3a93e..14b63fc 100644 --- a/compiler-rt/lib/sanitizer_common/tests/sanitizer_libc_test.cc +++ b/compiler-rt/lib/sanitizer_common/tests/sanitizer_libc_test.cc @@ -82,7 +82,7 @@ TEST(SanitizerCommon, FileOps) { ASSERT_NE(fd, kInvalidFd); EXPECT_EQ(len1, internal_write(fd, str1, len1)); EXPECT_EQ(len2, internal_write(fd, str2, len2)); - internal_close(fd); + CloseFile(fd); fd = OpenFile(tmpfile, RdOnly); ASSERT_NE(fd, kInvalidFd); @@ -113,7 +113,7 @@ TEST(SanitizerCommon, FileOps) { internal_memset(buf, 0, len1); EXPECT_EQ(len2, internal_read(fd, buf, len2)); EXPECT_EQ(0, internal_memcmp(buf, str2, len2)); - internal_close(fd); + CloseFile(fd); internal_unlink(tmpfile); } #endif @@ -151,7 +151,7 @@ TEST(SanitizerCommon, InternalMmapWithOffset) { ASSERT_EQ('A', p[0]); ASSERT_EQ('B', p[1]); - internal_close(fd); + CloseFile(fd); UnmapOrDie(p, page_size); internal_unlink(tmpfile); } -- 2.7.4