[compiler-rt] r348743 - Add new interceptors for FILE repositioning stream

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 10 00:56:14 PST 2018


Author: kamil
Date: Mon Dec 10 00:56:14 2018
New Revision: 348743

URL: http://llvm.org/viewvc/llvm-project?rev=348743&view=rev
Log:
Add new interceptors for FILE repositioning stream

Summary:
Add new interceptors for a set of functions to reposition a stream:
fgetpos, fseek, fseeko, fsetpos, ftell, ftello, rewind .

Add a dedicated test.

Enable this interface on NetBSD.

Reviewers: joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: kubamracek, llvm-commits, mgorny, #sanitizers

Tags: #sanitizers

Differential Revision: https://reviews.llvm.org/D55471

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/fseek.cc
Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc?rev=348743&r1=348742&r2=348743&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Mon Dec 10 00:56:14 2018
@@ -77,6 +77,8 @@
 #define ctime __ctime50
 #define ctime_r __ctime_r50
 #define devname __devname50
+#define fgetpos __fgetpos50
+#define fsetpos __fsetpos50
 #define fts_children __fts_children60
 #define fts_close __fts_close60
 #define fts_open __fts_open60
@@ -8228,6 +8230,59 @@ INTERCEPTOR(char *, MD5Data, const unsig
 #define INIT_MD5
 #endif
 
+#if SANITIZER_INTERCEPT_FSEEK
+INTERCEPTOR(int, fseek, __sanitizer_FILE *stream, long int offset, int whence) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, fseek, stream, offset, whence);
+  return REAL(fseek)(stream, offset, whence);
+}
+INTERCEPTOR(int, fseeko, __sanitizer_FILE *stream, OFF_T offset, int whence) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, fseeko, stream, offset, whence);
+  return REAL(fseeko)(stream, offset, whence);
+}
+INTERCEPTOR(long int, ftell, __sanitizer_FILE *stream) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, ftell, stream);
+  return REAL(ftell)(stream);
+}
+INTERCEPTOR(OFF_T, ftello, __sanitizer_FILE *stream) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, ftello, stream);
+  return REAL(ftello)(stream);
+}
+INTERCEPTOR(void, rewind, __sanitizer_FILE *stream) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, rewind, stream);
+  return REAL(rewind)(stream);
+}
+INTERCEPTOR(int, fgetpos, __sanitizer_FILE *stream, void *pos) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, fgetpos, stream, pos);
+  int ret = REAL(fgetpos)(stream, pos);
+  if (pos && !ret)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, pos, fpos_t_sz);
+  return ret;
+}
+INTERCEPTOR(int, fsetpos, __sanitizer_FILE *stream, const void *pos) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, fsetpos, stream, pos);
+  if (pos)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, pos, fpos_t_sz);
+  return REAL(fsetpos)(stream, pos);
+}
+#define INIT_FSEEK \
+  COMMON_INTERCEPT_FUNCTION(fseek); \
+  COMMON_INTERCEPT_FUNCTION(fseeko); \
+  COMMON_INTERCEPT_FUNCTION(ftell); \
+  COMMON_INTERCEPT_FUNCTION(ftello); \
+  COMMON_INTERCEPT_FUNCTION(rewind); \
+  COMMON_INTERCEPT_FUNCTION(fgetpos); \
+  COMMON_INTERCEPT_FUNCTION(fsetpos)
+#else
+#define INIT_FSEEK
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map =
@@ -8502,6 +8557,7 @@ static void InitializeCommonInterceptors
   INIT_MD4;
   INIT_RMD160;
   INIT_MD5;
+  INIT_FSEEK;
 
   INIT___PRINTF_CHK;
 }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h?rev=348743&r1=348742&r2=348743&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Mon Dec 10 00:56:14 2018
@@ -539,5 +539,6 @@
 #define SANITIZER_INTERCEPT_MD4 SI_NETBSD
 #define SANITIZER_INTERCEPT_RMD160 SI_NETBSD
 #define SANITIZER_INTERCEPT_MD5 SI_NETBSD
+#define SANITIZER_INTERCEPT_FSEEK SI_NETBSD
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc?rev=348743&r1=348742&r2=348743&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.cc Mon Dec 10 00:56:14 2018
@@ -2113,6 +2113,8 @@ const unsigned RMD160_return_length = RM
 
 const unsigned MD5_CTX_sz = sizeof(MD5_CTX);
 const unsigned MD5_return_length = MD5_DIGEST_STRING_LENGTH;
+
+const unsigned fpos_t_sz = sizeof(fpos_t);
 }  // namespace __sanitizer
 
 using namespace __sanitizer;

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h?rev=348743&r1=348742&r2=348743&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_limits_netbsd.h Mon Dec 10 00:56:14 2018
@@ -2235,6 +2235,8 @@ extern const unsigned RMD160_return_leng
 
 extern const unsigned MD5_CTX_sz;
 extern const unsigned MD5_return_length;
+
+extern const unsigned fpos_t_sz;
 }  // namespace __sanitizer
 
 #define CHECK_TYPE_SIZE(TYPE) \

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/fseek.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/fseek.cc?rev=348743&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/fseek.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/fseek.cc Mon Dec 10 00:56:14 2018
@@ -0,0 +1,51 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <assert.h>
+#include <inttypes.h>
+#include <stdio.h>
+
+int main(void) {
+  printf("fseek\n");
+
+  FILE *fp = fopen("/etc/fstab", "r");
+  assert(fp);
+
+  int rv = fseek(fp, 10, SEEK_SET);
+  assert(!rv);
+
+  printf("position: %ld\n", ftell(fp));
+
+  rewind(fp);
+
+  printf("position: %ld\n", ftell(fp));
+
+  rv = fseeko(fp, 15, SEEK_SET);
+  assert(!rv);
+
+  printf("position: %" PRIuMAX "\n", (uintmax_t)ftello(fp));
+
+  fpos_t pos;
+  rv = fgetpos(fp, &pos);
+  assert(!rv);
+
+  rewind(fp);
+
+  printf("position: %" PRIuMAX "\n", (uintmax_t)ftello(fp));
+
+  rv = fsetpos(fp, &pos);
+  assert(!rv);
+
+  printf("position: %" PRIuMAX "\n", (uintmax_t)ftello(fp));
+
+  rv = fclose(fp);
+  assert(!rv);
+
+  // CHECK: fseek
+  // CHECK: position: 10
+  // CHECK: position: 0
+  // CHECK: position: 15
+  // CHECK: position: 0
+  // CHECK: position: 15
+
+  return 0;
+}




More information about the llvm-commits mailing list