[compiler-rt] r174316 - [Sanitizer] extend internal libc with stat/fstat/lstat functions
Alexey Samsonov
samsonov at google.com
Mon Feb 4 02:16:51 PST 2013
Author: samsonov
Date: Mon Feb 4 04:16:50 2013
New Revision: 174316
URL: http://llvm.org/viewvc/llvm-project?rev=174316&view=rev
Log:
[Sanitizer] extend internal libc with stat/fstat/lstat functions
Modified:
compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc
compiler-rt/trunk/lib/tsan/tests/unit/tsan_platform_test.cc
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_libc.h Mon Feb 4 04:16:50 2013
@@ -71,6 +71,10 @@ fd_t internal_open(const char *filename,
uptr internal_read(fd_t fd, void *buf, uptr count);
uptr internal_write(fd_t fd, const void *buf, uptr count);
uptr internal_filesize(fd_t fd); // -1 on error.
+int internal_stat(const char *path, void *buf);
+int internal_lstat(const char *path, void *buf);
+int internal_fstat(fd_t fd, void *buf);
+
int internal_dup2(int oldfd, int newfd);
uptr internal_readlink(const char *path, char *buf, uptr bufsize);
int internal_snprintf(char *buffer, uptr length, const char *format, ...);
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc Mon Feb 4 04:16:50 2013
@@ -93,16 +93,38 @@ uptr internal_write(fd_t fd, const void
return res;
}
+int internal_stat(const char *path, void *buf) {
+#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
+ return syscall(__NR_stat, path, buf);
+#else
+ return syscall(__NR_stat64, path, buf);
+#endif
+}
+
+int internal_lstat(const char *path, void *buf) {
+#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
+ return syscall(__NR_lstat, path, buf);
+#else
+ return syscall(__NR_lstat64, path, buf);
+#endif
+}
+
+int internal_fstat(fd_t fd, void *buf) {
+#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
+ return syscall(__NR_fstat, fd, buf);
+#else
+ return syscall(__NR_fstat64, fd, buf);
+#endif
+}
+
uptr internal_filesize(fd_t fd) {
#if SANITIZER_LINUX_USES_64BIT_SYSCALLS
struct stat st;
- if (syscall(__NR_fstat, fd, &st))
- return -1;
#else
struct stat64 st;
- if (syscall(__NR_fstat64, fd, &st))
- return -1;
#endif
+ if (internal_fstat(fd, &st))
+ return -1;
return (uptr)st.st_size;
}
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc Mon Feb 4 04:16:50 2013
@@ -69,9 +69,21 @@ uptr internal_write(fd_t fd, const void
return write(fd, buf, count);
}
+int internal_stat(const char *path, void *buf) {
+ return stat(path, buf);
+}
+
+int internal_lstat(const char *path, void *buf) {
+ return lstat(path, buf);
+}
+
+int internal_fstat(fd_t fd, void *buf) {
+ return fstat(fd, buf);
+}
+
uptr internal_filesize(fd_t fd) {
struct stat st;
- if (fstat(fd, &st))
+ if (internal_fstat(fd, &st))
return -1;
return (uptr)st.st_size;
}
Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_win.cc Mon Feb 4 04:16:50 2013
@@ -219,6 +219,18 @@ uptr internal_write(fd_t fd, const void
return ret;
}
+int internal_stat(const char *path, void *buf) {
+ UNIMPLEMENTED();
+}
+
+int internal_lstat(const char *path, void *buf) {
+ UNIMPLEMENTED();
+}
+
+int internal_fstat(fd_t fd, void *buf) {
+ UNIMPLEMENTED();
+}
+
uptr internal_filesize(fd_t fd) {
UNIMPLEMENTED();
}
Modified: compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/tests/sanitizer_libc_test.cc Mon Feb 4 04:16:50 2013
@@ -9,9 +9,17 @@
// Tests for sanitizer_libc.h.
//===----------------------------------------------------------------------===//
+#include "sanitizer_common/sanitizer_common.h"
#include "sanitizer_common/sanitizer_libc.h"
#include "gtest/gtest.h"
+#if defined(__linux__) || defined(__APPLE__)
+# define SANITIZER_TEST_HAS_STAT_H 1
+# include <sys/stat.h>
+#else
+# define SANITIZER_TEST_HAS_STAT_H 0
+#endif
+
// A regression test for internal_memmove() implementation.
TEST(SanitizerCommon, InternalMemmoveRegression) {
char src[] = "Hello World";
@@ -40,3 +48,39 @@ TEST(SanitizerCommon, mem_is_zero) {
}
delete [] x;
}
+
+TEST(SanitizerCommon, FileOps) {
+ const char *str1 = "qwerty";
+ uptr len1 = internal_strlen(str1);
+ const char *str2 = "zxcv";
+ uptr len2 = internal_strlen(str2);
+
+ const char kTempFileName[] = "/tmp/sanitizer_common.tmp";
+ fd_t fd = OpenFile(kTempFileName, true);
+ EXPECT_NE(fd, kInvalidFd);
+ EXPECT_EQ(len1, internal_write(fd, str1, len1));
+ EXPECT_EQ(len2, internal_write(fd, str2, len2));
+ internal_close(fd);
+
+ fd = OpenFile(kTempFileName, false);
+ EXPECT_NE(fd, kInvalidFd);
+ uptr fsize = internal_filesize(fd);
+ EXPECT_EQ(len1 + len2, fsize);
+
+#if SANITIZER_TEST_HAS_STAT_H
+ struct stat st1, st2, st3;
+ EXPECT_EQ(0, internal_stat(kTempFileName, &st1));
+ EXPECT_EQ(0, internal_lstat(kTempFileName, &st2));
+ EXPECT_EQ(0, internal_fstat(fd, &st3));
+ EXPECT_EQ(fsize, (uptr)st3.st_size);
+#endif
+
+ char buf[64] = {};
+ EXPECT_EQ(len1, internal_read(fd, buf, len1));
+ EXPECT_EQ(0, internal_memcmp(buf, str1, len1));
+ EXPECT_EQ((char)0, buf[len1 + 1]);
+ internal_memset(buf, 0, len1);
+ EXPECT_EQ(len2, internal_read(fd, buf, len2));
+ EXPECT_EQ(0, internal_memcmp(buf, str2, len2));
+ internal_close(fd);
+}
Modified: compiler-rt/trunk/lib/tsan/tests/unit/tsan_platform_test.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/tests/unit/tsan_platform_test.cc?rev=174316&r1=174315&r2=174316&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/tests/unit/tsan_platform_test.cc (original)
+++ compiler-rt/trunk/lib/tsan/tests/unit/tsan_platform_test.cc Mon Feb 4 04:16:50 2013
@@ -61,29 +61,4 @@ TEST(Platform, ThreadInfoWorker) {
pthread_join(t, 0);
}
-TEST(Platform, FileOps) {
- const char *str1 = "qwerty";
- uptr len1 = internal_strlen(str1);
- const char *str2 = "zxcv";
- uptr len2 = internal_strlen(str2);
-
- fd_t fd = OpenFile("./tsan_test.tmp", true);
- EXPECT_NE(fd, kInvalidFd);
- EXPECT_EQ(len1, internal_write(fd, str1, len1));
- EXPECT_EQ(len2, internal_write(fd, str2, len2));
- internal_close(fd);
-
- fd = OpenFile("./tsan_test.tmp", false);
- EXPECT_NE(fd, kInvalidFd);
- EXPECT_EQ(len1 + len2, internal_filesize(fd));
- char buf[64] = {};
- EXPECT_EQ(len1, internal_read(fd, buf, len1));
- EXPECT_EQ(0, internal_memcmp(buf, str1, len1));
- EXPECT_EQ((char)0, buf[len1 + 1]);
- internal_memset(buf, 0, len1);
- EXPECT_EQ(len2, internal_read(fd, buf, len2));
- EXPECT_EQ(0, internal_memcmp(buf, str2, len2));
- internal_close(fd);
-}
-
} // namespace __tsan
More information about the llvm-commits
mailing list