[compiler-rt] r326161 - Add new interceptors: getttyent(3) family

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 26 18:30:16 PST 2018


Author: kamil
Date: Mon Feb 26 18:30:16 2018
New Revision: 326161

URL: http://llvm.org/viewvc/llvm-project?rev=326161&view=rev
Log:
Add new interceptors: getttyent(3) family

Summary:
getttyent, getttynam, setttyentpath - get ttys file entry

Reuse them on NetBSD.

Sponsored by <The NetBSD Foundation>

Reviewers: vitalybuka, joerg

Reviewed By: vitalybuka

Subscribers: llvm-commits, kubamracek, #sanitizers

Tags: #sanitizers

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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/ttyent.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=326161&r1=326160&r2=326161&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_common_interceptors.inc Mon Feb 26 18:30:16 2018
@@ -6851,6 +6851,40 @@ INTERCEPTOR(void, strmode, u32 mode, cha
 #define INIT_STRMODE
 #endif
 
+#if SANITIZER_INTERCEPT_TTYENT
+INTERCEPTOR(struct __sanitizer_ttyent *, getttyent, void) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, getttyent);
+  struct __sanitizer_ttyent *ttyent = REAL(getttyent)();
+  if (ttyent)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
+  return ttyent;
+}
+INTERCEPTOR(struct __sanitizer_ttyent *, getttynam, char *name) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, getttynam, name);
+  if (name)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
+  struct __sanitizer_ttyent *ttyent = REAL(getttynam)(name);
+  if (ttyent)
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, ttyent, struct_ttyent_sz);
+  return ttyent;
+}
+INTERCEPTOR(int, setttyentpath, char *path) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, setttyentpath, path);
+  if (path)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, path, REAL(strlen)(path) + 1);
+  return REAL(setttyentpath)(path);
+}
+#define INIT_TTYENT \
+  COMMON_INTERCEPT_FUNCTION(getttyent); \
+  COMMON_INTERCEPT_FUNCTION(getttynam); \
+  COMMON_INTERCEPT_FUNCTION(setttyentpath)
+#else
+#define INIT_TTYENT
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@@ -7081,6 +7115,7 @@ static void InitializeCommonInterceptors
   INIT_DEVNAME_R;
   INIT_FGETLN;
   INIT_STRMODE;
+  INIT_TTYENT;
 
 #if SANITIZER_NETBSD
   COMMON_INTERCEPT_FUNCTION(__libc_mutex_lock);

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=326161&r1=326160&r2=326161&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_platform_interceptors.h Mon Feb 26 18:30:16 2018
@@ -461,5 +461,6 @@
 #define SANITIZER_INTERCEPT_DEVNAME_R SI_NETBSD
 #define SANITIZER_INTERCEPT_FGETLN SI_NETBSD
 #define SANITIZER_INTERCEPT_STRMODE SI_NETBSD
+#define SANITIZER_INTERCEPT_TTYENT 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=326161&r1=326160&r2=326161&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 Feb 26 18:30:16 2018
@@ -198,6 +198,7 @@
 #include <term.h>
 #include <termios.h>
 #include <time.h>
+#include <ttyent.h>
 #include <utime.h>
 #include <utmp.h>
 #include <utmpx.h>
@@ -338,6 +339,8 @@ int glob_altdirfunc = GLOB_ALTDIRFUNC;
 
 unsigned path_max = PATH_MAX;
 
+int struct_ttyent_sz = sizeof(struct ttyent);
+
 // ioctl arguments
 unsigned struct_altqreq_sz = sizeof(altqreq);
 unsigned struct_amr_user_ioctl_sz = sizeof(amr_user_ioctl);

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=326161&r1=326160&r2=326161&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 Feb 26 18:30:16 2018
@@ -362,6 +362,8 @@ extern int glob_altdirfunc;
 
 extern unsigned path_max;
 
+extern int struct_ttyent_sz;
+
 extern int ptrace_pt_io;
 extern int ptrace_pt_lwpinfo;
 extern int ptrace_pt_set_event_mask;
@@ -437,6 +439,16 @@ struct __sanitizer_ifconf {
   } ifc_ifcu;
 };
 
+struct __sanitizer_ttyent {
+  char *ty_name;
+  char *ty_getty;
+  char *ty_type;
+  int ty_status;
+  char *ty_window;
+  char *ty_comment;
+  char *ty_class;
+};
+
 #define IOC_NRBITS 8
 #define IOC_TYPEBITS 8
 #define IOC_SIZEBITS 14

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/ttyent.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/ttyent.cc?rev=326161&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/ttyent.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/ttyent.cc Mon Feb 26 18:30:16 2018
@@ -0,0 +1,70 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <ttyent.h>
+
+#define STRING_OR_NULL(x) ((x) ? (x) : "null")
+
+void test1() {
+  struct ttyent *typ = getttyent();
+
+  printf("%s %s %s %d %s %s %s\n", STRING_OR_NULL(typ->ty_name),
+         STRING_OR_NULL(typ->ty_getty), STRING_OR_NULL(typ->ty_type),
+         typ->ty_status, STRING_OR_NULL(typ->ty_window),
+         STRING_OR_NULL(typ->ty_comment), STRING_OR_NULL(typ->ty_class));
+
+  endttyent();
+}
+
+void test2() {
+  struct ttyent *typ = getttynam("console");
+
+  printf("%s %s %s %d %s %s %s\n", STRING_OR_NULL(typ->ty_name),
+         STRING_OR_NULL(typ->ty_getty), STRING_OR_NULL(typ->ty_type),
+         typ->ty_status, STRING_OR_NULL(typ->ty_window),
+         STRING_OR_NULL(typ->ty_comment), STRING_OR_NULL(typ->ty_class));
+
+  endttyent();
+}
+
+void test3() {
+  if (!setttyent())
+    exit(1);
+
+  struct ttyent *typ = getttyent();
+
+  printf("%s %s %s %d %s %s %s\n", STRING_OR_NULL(typ->ty_name),
+         STRING_OR_NULL(typ->ty_getty), STRING_OR_NULL(typ->ty_type),
+         typ->ty_status, STRING_OR_NULL(typ->ty_window),
+         STRING_OR_NULL(typ->ty_comment), STRING_OR_NULL(typ->ty_class));
+
+  endttyent();
+}
+
+void test4() {
+  if (!setttyentpath(_PATH_TTYS))
+    exit(1);
+
+  struct ttyent *typ = getttyent();
+
+  printf("%s %s %s %d %s %s %s\n", STRING_OR_NULL(typ->ty_name),
+         STRING_OR_NULL(typ->ty_getty), STRING_OR_NULL(typ->ty_type),
+         typ->ty_status, STRING_OR_NULL(typ->ty_window),
+         STRING_OR_NULL(typ->ty_comment), STRING_OR_NULL(typ->ty_class));
+
+  endttyent();
+}
+
+int main(void) {
+  printf("ttyent\n");
+
+  test1();
+  test2();
+  test3();
+  test4();
+
+  // CHECK: ttyent
+
+  return 0;
+}




More information about the llvm-commits mailing list