[compiler-rt] r326162 - Add new interceptors: getprotoent(3) family

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


Author: kamil
Date: Mon Feb 26 18:32:04 2018
New Revision: 326162

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

Summary:
getprotoent, getprotobynumber, getprotobyname - get protocol entry

Reuse them on NetBSD.

Sponsored by <The NetBSD Foundation>

Reviewers: joerg, vitalybuka

Reviewed By: vitalybuka

Subscribers: kubamracek, llvm-commits, #sanitizers

Tags: #sanitizers

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

Added:
    compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.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.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=326162&r1=326161&r2=326162&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:32:04 2018
@@ -6885,6 +6885,76 @@ INTERCEPTOR(int, setttyentpath, char *pa
 #define INIT_TTYENT
 #endif
 
+#if SANITIZER_INTERCEPT_PROTOENT
+INTERCEPTOR(struct __sanitizer_protoent *, getprotoent) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, getprotoent);
+  struct __sanitizer_protoent *p = REAL(getprotoent)();
+  if (p) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
+
+    SIZE_T pp_size = 1; // One handles the trailing \0
+
+    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
+       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
+                                   pp_size * sizeof(char **));
+  }
+  return p;
+}
+
+INTERCEPTOR(struct __sanitizer_protoent *, getprotobyname, const char *name) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, getprotobyname, name);
+  if (name)
+    COMMON_INTERCEPTOR_READ_RANGE(ctx, name, REAL(strlen)(name) + 1);
+  struct __sanitizer_protoent *p = REAL(getprotobyname)(name);
+  if (p) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
+
+    SIZE_T pp_size = 1; // One handles the trailing \0
+
+    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
+       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
+                                   pp_size * sizeof(char **));
+  }
+  return p;
+}
+
+INTERCEPTOR(struct __sanitizer_protoent *, getprotobynumber, int proto) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, getprotobynumber, proto);
+  struct __sanitizer_protoent *p = REAL(getprotobynumber)(proto);
+  if (p) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p, sizeof(*p));
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_name, REAL(strlen)(p->p_name) + 1);
+
+    SIZE_T pp_size = 1; // One handles the trailing \0
+
+    for (char **pp = p->p_aliases; *pp; ++pp, ++pp_size)
+       COMMON_INTERCEPTOR_WRITE_RANGE(ctx, *pp, REAL(strlen)(*pp) + 1);
+
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, p->p_aliases,
+                                   pp_size * sizeof(char **));
+  }
+  return p;
+}
+#define INIT_PROTOENT \
+  COMMON_INTERCEPT_FUNCTION(getprotoent); \
+  COMMON_INTERCEPT_FUNCTION(getprotobyname); \
+  COMMON_INTERCEPT_FUNCTION(getprotobynumber)
+#else
+#define INIT_PROTOENT
+#endif
+
 static void InitializeCommonInterceptors() {
   static u64 metadata_mem[sizeof(MetadataHashMap) / sizeof(u64) + 1];
   interceptor_metadata_map = new((void *)&metadata_mem) MetadataHashMap();
@@ -7116,6 +7186,7 @@ static void InitializeCommonInterceptors
   INIT_FGETLN;
   INIT_STRMODE;
   INIT_TTYENT;
+  INIT_PROTOENT;
 
 #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=326162&r1=326161&r2=326162&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:32:04 2018
@@ -462,5 +462,6 @@
 #define SANITIZER_INTERCEPT_FGETLN SI_NETBSD
 #define SANITIZER_INTERCEPT_STRMODE SI_NETBSD
 #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
+#define SANITIZER_INTERCEPT_PROTOENT SI_NETBSD
 
 #endif  // #ifndef SANITIZER_PLATFORM_INTERCEPTORS_H

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=326162&r1=326161&r2=326162&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:32:04 2018
@@ -113,6 +113,12 @@ struct __sanitizer_shmid_ds {
   void *_shm_internal;
 };
 
+struct __sanitizer_protoent {
+  char *p_name;
+  char **p_aliases;
+  int p_proto;
+};
+
 extern unsigned struct_msqid_ds_sz;
 extern unsigned struct_mq_attr_sz;
 extern unsigned struct_timex_sz;

Added: compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc?rev=326162&view=auto
==============================================================================
--- compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc (added)
+++ compiler-rt/trunk/test/sanitizer_common/TestCases/NetBSD/protoent.cc Mon Feb 26 18:32:04 2018
@@ -0,0 +1,89 @@
+// RUN: %clangxx -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
+
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#define STRING_OR_NULL(x) ((x) ? (x) : "null")
+
+void test1() {
+  struct protoent *ptp = getprotoent();
+
+  printf("%s ", STRING_OR_NULL(ptp->p_name));
+
+  for (char **cp = ptp->p_aliases; *cp != NULL; cp++)
+    printf("%s ", STRING_OR_NULL(*cp));
+
+  printf("%d\n", ptp->p_proto);
+  endprotoent();
+}
+
+void test2() {
+  struct protoent *ptp = getprotobyname("icmp");
+
+  printf("%s ", STRING_OR_NULL(ptp->p_name));
+
+  for (char **cp = ptp->p_aliases; *cp != NULL; cp++)
+    printf("%s ", STRING_OR_NULL(*cp));
+
+  printf("%d\n", ptp->p_proto);
+  endprotoent();
+}
+
+void test3() {
+  struct protoent *ptp = getprotobynumber(1);
+
+  printf("%s ", STRING_OR_NULL(ptp->p_name));
+
+  for (char **cp = ptp->p_aliases; *cp != NULL; cp++)
+    printf("%s ", STRING_OR_NULL(*cp));
+
+  printf("%d\n", ptp->p_proto);
+  endprotoent();
+}
+
+void test4() {
+  setprotoent(1);
+  struct protoent *ptp = getprotobynumber(1);
+
+  ptp = getprotobynumber(2);
+
+  printf("%s ", STRING_OR_NULL(ptp->p_name));
+
+  for (char **cp = ptp->p_aliases; *cp != NULL; cp++)
+    printf("%s ", STRING_OR_NULL(*cp));
+
+  printf("%d\n", ptp->p_proto);
+  endprotoent();
+}
+
+void test5() {
+  struct protoent *ptp = getprotobyname("ttp");
+
+  printf("%s ", STRING_OR_NULL(ptp->p_name));
+
+  for (char **cp = ptp->p_aliases; *cp != NULL; cp++)
+    printf("%s ", STRING_OR_NULL(*cp));
+
+  printf("%d\n", ptp->p_proto);
+  endprotoent();
+}
+
+int main(void) {
+  printf("protoent\n");
+
+  test1();
+  test2();
+  test3();
+  test4();
+  test5();
+
+  // CHECK: protoent
+  // CHECK: hopopt HOPOPT 0
+  // CHECK: icmp ICMP 1
+  // CHECK: icmp ICMP 1
+  // CHECK: igmp IGMP 2
+  // CHECK: ttp TTP iptm IPTM 84
+
+  return 0;
+}




More information about the llvm-commits mailing list