[compiler-rt] 8fdd821 - [Sanitizers] intercept netent, protoent and mincore on FreeBSD.

David Carlier via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 10 11:24:58 PDT 2021


Author: David Carlier
Date: 2021-09-10T19:24:51+01:00
New Revision: 8fdd821aef366c7d37407993dc5debec296cdc2b

URL: https://github.com/llvm/llvm-project/commit/8fdd821aef366c7d37407993dc5debec296cdc2b
DIFF: https://github.com/llvm/llvm-project/commit/8fdd821aef366c7d37407993dc5debec296cdc2b.diff

LOG: [Sanitizers] intercept netent, protoent and mincore on FreeBSD.
netent on Linux in addition as well.

Reviewd By: vitalybuka

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

Added: 
    compiler-rt/test/sanitizer_common/TestCases/Linux/netent.cpp

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
    compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
    compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h
    compiler-rt/test/sanitizer_common/TestCases/Linux/protoent.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 659b36a3007d0..535f1e3a0d410 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -446,7 +446,8 @@
 #define SANITIZER_INTERCEPT_SEM \
   (SI_LINUX || SI_FREEBSD || SI_NETBSD || SI_SOLARIS)
 #define SANITIZER_INTERCEPT_PTHREAD_SETCANCEL SI_POSIX
-#define SANITIZER_INTERCEPT_MINCORE (SI_LINUX || SI_NETBSD || SI_SOLARIS)
+#define SANITIZER_INTERCEPT_MINCORE \
+  (SI_LINUX || SI_NETBSD || SI_FREEBSD || SI_SOLARIS)
 #define SANITIZER_INTERCEPT_PROCESS_VM_READV SI_LINUX
 #define SANITIZER_INTERCEPT_CTERMID \
   (SI_LINUX || SI_MAC || SI_FREEBSD || SI_NETBSD || SI_SOLARIS)
@@ -519,9 +520,9 @@
 #define SANITIZER_INTERCEPT_FGETLN (SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_STRMODE (SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_TTYENT SI_NETBSD
-#define SANITIZER_INTERCEPT_PROTOENT (SI_NETBSD || SI_LINUX)
+#define SANITIZER_INTERCEPT_PROTOENT (SI_LINUX || SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_PROTOENT_R SI_GLIBC
-#define SANITIZER_INTERCEPT_NETENT SI_NETBSD
+#define SANITIZER_INTERCEPT_NETENT (SI_LINUX || SI_NETBSD || SI_FREEBSD)
 #define SANITIZER_INTERCEPT_SETVBUF \
   (SI_NETBSD || SI_FREEBSD || SI_LINUX || SI_MAC)
 #define SANITIZER_INTERCEPT_GETMNTINFO (SI_NETBSD || SI_FREEBSD || SI_MAC)

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
index 72a1810298b6d..e8c3f1a85a557 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
@@ -16,26 +16,26 @@
 
 #if SANITIZER_FREEBSD
 
-#include "sanitizer_internal_defs.h"
-#include "sanitizer_platform.h"
-#include "sanitizer_platform_limits_posix.h"
+#  include "sanitizer_internal_defs.h"
+#  include "sanitizer_platform.h"
+#  include "sanitizer_platform_limits_posix.h"
 
 // Get sys/_types.h, because that tells us whether 64-bit inodes are
 // used in struct dirent below.
-#include <sys/_types.h>
+#  include <sys/_types.h>
 
 namespace __sanitizer {
 void *__sanitizer_get_link_map_by_dlopen_handle(void *handle);
-#define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
-  (link_map *)__sanitizer_get_link_map_by_dlopen_handle(handle)
+#  define GET_LINK_MAP_BY_DLOPEN_HANDLE(handle) \
+    (link_map *)__sanitizer_get_link_map_by_dlopen_handle(handle)
 
 extern unsigned struct_utsname_sz;
 extern unsigned struct_stat_sz;
-#if defined(__powerpc64__)
+#  if defined(__powerpc64__)
 const unsigned struct___old_kernel_stat_sz = 0;
-#else
+#  else
 const unsigned struct___old_kernel_stat_sz = 32;
-#endif
+#  endif
 extern unsigned struct_rusage_sz;
 extern unsigned siginfo_t_sz;
 extern unsigned struct_itimerval_sz;
@@ -114,11 +114,24 @@ struct __sanitizer_ipc_perm {
   long key;
 };
 
-#if !defined(__i386__)
+struct __sanitizer_protoent {
+  char *p_name;
+  char **p_aliases;
+  int p_proto;
+};
+
+struct __sanitizer_netent {
+  char *n_name;
+  char **n_aliases;
+  int n_addrtype;
+  u32 n_net;
+};
+
+#  if !defined(__i386__)
 typedef long long __sanitizer_time_t;
-#else
+#  else
 typedef long __sanitizer_time_t;
-#endif
+#  endif
 
 struct __sanitizer_shmid_ds {
   __sanitizer_ipc_perm shm_perm;
@@ -147,7 +160,7 @@ struct __sanitizer_ifaddrs {
   unsigned int ifa_flags;
   void *ifa_addr;     // (struct sockaddr *)
   void *ifa_netmask;  // (struct sockaddr *)
-#undef ifa_dstaddr
+#  undef ifa_dstaddr
   void *ifa_dstaddr;  // (struct sockaddr *)
   void *ifa_data;
 };
@@ -229,12 +242,12 @@ struct __sanitizer_cmsghdr {
 };
 
 struct __sanitizer_dirent {
-#if defined(__INO64)
+#  if defined(__INO64)
   unsigned long long d_fileno;
   unsigned long long d_off;
-#else
+#  else
   unsigned int d_fileno;
-#endif
+#  endif
   unsigned short d_reclen;
   // more fields that we don't care about
 };
@@ -243,23 +256,23 @@ struct __sanitizer_dirent {
 typedef int __sanitizer_clock_t;
 typedef int __sanitizer_clockid_t;
 
-#if defined(_LP64) || defined(__x86_64__) || defined(__powerpc__) || \
-    defined(__mips__)
+#  if defined(_LP64) || defined(__x86_64__) || defined(__powerpc__) || \
+      defined(__mips__)
 typedef unsigned __sanitizer___kernel_uid_t;
 typedef unsigned __sanitizer___kernel_gid_t;
-#else
+#  else
 typedef unsigned short __sanitizer___kernel_uid_t;
 typedef unsigned short __sanitizer___kernel_gid_t;
-#endif
+#  endif
 typedef long long __sanitizer___kernel_off_t;
 
-#if defined(__powerpc__) || defined(__mips__)
+#  if defined(__powerpc__) || defined(__mips__)
 typedef unsigned int __sanitizer___kernel_old_uid_t;
 typedef unsigned int __sanitizer___kernel_old_gid_t;
-#else
+#  else
 typedef unsigned short __sanitizer___kernel_old_uid_t;
 typedef unsigned short __sanitizer___kernel_old_gid_t;
-#endif
+#  endif
 
 typedef long long __sanitizer___kernel_loff_t;
 typedef struct {
@@ -399,39 +412,39 @@ struct __sanitizer_ifconf {
   } ifc_ifcu;
 };
 
-#define IOC_NRBITS 8
-#define IOC_TYPEBITS 8
-#if defined(__powerpc__) || defined(__powerpc64__) || defined(__mips__)
-#define IOC_SIZEBITS 13
-#define IOC_DIRBITS 3
-#define IOC_NONE 1U
-#define IOC_WRITE 4U
-#define IOC_READ 2U
-#else
-#define IOC_SIZEBITS 14
-#define IOC_DIRBITS 2
-#define IOC_NONE 0U
-#define IOC_WRITE 1U
-#define IOC_READ 2U
-#endif
-#define IOC_NRMASK ((1 << IOC_NRBITS) - 1)
-#define IOC_TYPEMASK ((1 << IOC_TYPEBITS) - 1)
-#define IOC_SIZEMASK ((1 << IOC_SIZEBITS) - 1)
-#if defined(IOC_DIRMASK)
-#undef IOC_DIRMASK
-#endif
-#define IOC_DIRMASK ((1 << IOC_DIRBITS) - 1)
-#define IOC_NRSHIFT 0
-#define IOC_TYPESHIFT (IOC_NRSHIFT + IOC_NRBITS)
-#define IOC_SIZESHIFT (IOC_TYPESHIFT + IOC_TYPEBITS)
-#define IOC_DIRSHIFT (IOC_SIZESHIFT + IOC_SIZEBITS)
-#define EVIOC_EV_MAX 0x1f
-#define EVIOC_ABS_MAX 0x3f
-
-#define IOC_DIR(nr) (((nr) >> IOC_DIRSHIFT) & IOC_DIRMASK)
-#define IOC_TYPE(nr) (((nr) >> IOC_TYPESHIFT) & IOC_TYPEMASK)
-#define IOC_NR(nr) (((nr) >> IOC_NRSHIFT) & IOC_NRMASK)
-#define IOC_SIZE(nr) (((nr) >> IOC_SIZESHIFT) & IOC_SIZEMASK)
+#  define IOC_NRBITS 8
+#  define IOC_TYPEBITS 8
+#  if defined(__powerpc__) || defined(__powerpc64__) || defined(__mips__)
+#    define IOC_SIZEBITS 13
+#    define IOC_DIRBITS 3
+#    define IOC_NONE 1U
+#    define IOC_WRITE 4U
+#    define IOC_READ 2U
+#  else
+#    define IOC_SIZEBITS 14
+#    define IOC_DIRBITS 2
+#    define IOC_NONE 0U
+#    define IOC_WRITE 1U
+#    define IOC_READ 2U
+#  endif
+#  define IOC_NRMASK ((1 << IOC_NRBITS) - 1)
+#  define IOC_TYPEMASK ((1 << IOC_TYPEBITS) - 1)
+#  define IOC_SIZEMASK ((1 << IOC_SIZEBITS) - 1)
+#  if defined(IOC_DIRMASK)
+#    undef IOC_DIRMASK
+#  endif
+#  define IOC_DIRMASK ((1 << IOC_DIRBITS) - 1)
+#  define IOC_NRSHIFT 0
+#  define IOC_TYPESHIFT (IOC_NRSHIFT + IOC_NRBITS)
+#  define IOC_SIZESHIFT (IOC_TYPESHIFT + IOC_TYPEBITS)
+#  define IOC_DIRSHIFT (IOC_SIZESHIFT + IOC_SIZEBITS)
+#  define EVIOC_EV_MAX 0x1f
+#  define EVIOC_ABS_MAX 0x3f
+
+#  define IOC_DIR(nr) (((nr) >> IOC_DIRSHIFT) & IOC_DIRMASK)
+#  define IOC_TYPE(nr) (((nr) >> IOC_TYPESHIFT) & IOC_TYPEMASK)
+#  define IOC_NR(nr) (((nr) >> IOC_NRSHIFT) & IOC_NRMASK)
+#  define IOC_SIZE(nr) (((nr) >> IOC_SIZESHIFT) & IOC_SIZEMASK)
 
 extern unsigned struct_ifreq_sz;
 extern unsigned struct_termios_sz;
@@ -633,24 +646,24 @@ extern unsigned struct_fstab_sz;
 extern unsigned struct_StringList_sz;
 }  // namespace __sanitizer
 
-#define CHECK_TYPE_SIZE(TYPE) \
-  COMPILER_CHECK(sizeof(__sanitizer_##TYPE) == sizeof(TYPE))
+#  define CHECK_TYPE_SIZE(TYPE) \
+    COMPILER_CHECK(sizeof(__sanitizer_##TYPE) == sizeof(TYPE))
 
-#define CHECK_SIZE_AND_OFFSET(CLASS, MEMBER)                      \
-  COMPILER_CHECK(sizeof(((__sanitizer_##CLASS *)NULL)->MEMBER) == \
-                 sizeof(((CLASS *)NULL)->MEMBER));                \
-  COMPILER_CHECK(offsetof(__sanitizer_##CLASS, MEMBER) ==         \
-                 offsetof(CLASS, MEMBER))
+#  define CHECK_SIZE_AND_OFFSET(CLASS, MEMBER)                      \
+    COMPILER_CHECK(sizeof(((__sanitizer_##CLASS *)NULL)->MEMBER) == \
+                   sizeof(((CLASS *)NULL)->MEMBER));                \
+    COMPILER_CHECK(offsetof(__sanitizer_##CLASS, MEMBER) ==         \
+                   offsetof(CLASS, MEMBER))
 
 // For sigaction, which is a function and struct at the same time,
 // and thus requires explicit "struct" in sizeof() expression.
-#define CHECK_STRUCT_SIZE_AND_OFFSET(CLASS, MEMBER)                      \
-  COMPILER_CHECK(sizeof(((struct __sanitizer_##CLASS *)NULL)->MEMBER) == \
-                 sizeof(((struct CLASS *)NULL)->MEMBER));                \
-  COMPILER_CHECK(offsetof(struct __sanitizer_##CLASS, MEMBER) ==         \
-                 offsetof(struct CLASS, MEMBER))
+#  define CHECK_STRUCT_SIZE_AND_OFFSET(CLASS, MEMBER)                      \
+    COMPILER_CHECK(sizeof(((struct __sanitizer_##CLASS *)NULL)->MEMBER) == \
+                   sizeof(((struct CLASS *)NULL)->MEMBER));                \
+    COMPILER_CHECK(offsetof(struct __sanitizer_##CLASS, MEMBER) ==         \
+                   offsetof(struct CLASS, MEMBER))
 
-#define SIGACTION_SYMNAME sigaction
+#  define SIGACTION_SYMNAME sigaction
 
 #endif
 

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h
index 5657f33dc66ab..d69b344dd613d 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_posix.h
@@ -715,6 +715,13 @@ struct __sanitizer_protoent {
   int p_proto;
 };
 
+struct __sanitizer_netent {
+  char *n_name;
+  char **n_aliases;
+  int n_addrtype;
+  u32 n_net;
+};
+
 struct __sanitizer_addrinfo {
   int ai_flags;
   int ai_family;

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/netent.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/netent.cpp
new file mode 100644
index 0000000000000..b18fc79330ec5
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/netent.cpp
@@ -0,0 +1,63 @@
+// RUN: %clangxx -O0 -g %s -o %t
+//
+// REQUIRES: linux, freebsd
+
+#include <inttypes.h>
+#include <netdb.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#if defined(__linux__)
+#define LOOPBACK "loopback"
+#else
+#define LOOPBACK "your-net"
+#endif
+
+void test1() {
+  struct netent *ntp = getnetent();
+  assert(ntp && ntp->n_name);
+  assert(ntp->n_addrtype == 2);
+  assert(ntp->n_net == 127);
+  endnetent();
+}
+
+void test2() {
+  struct netent *ntp = getnetbyname(LOOPBACK);
+  assert(ntp && ntp->n_name);
+  assert(ntp->n_addrtype == 2);
+  assert(ntp->n_net == 127);
+  endnetent();
+}
+
+void test3() {
+  struct netent *lb = getnetbyname(LOOPBACK);
+  assert(lb);
+  struct netent *ntp = getnetbyaddr(lb->n_net, lb->n_addrtype);
+  assert(ntp && ntp->n_name);
+  assert(ntp->n_addrtype == 2);
+  assert(ntp->n_net == 127);
+  endnetent();
+}
+
+void test4() {
+  setnetent(1);
+
+  struct netent *ntp = getnetent();
+  assert(ntp && ntp->n_name);
+  assert(ntp->n_addrtype == 2);
+  assert(ntp->n_net == 127);
+  endnetent();
+}
+
+int main(void) {
+  printf("netent\n");
+
+  test1();
+  test2();
+  test3();
+  test4();
+
+  return 0;
+}

diff  --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/protoent.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/protoent.cpp
index 003790067d1b1..0d51d80fc7680 100644
--- a/compiler-rt/test/sanitizer_common/TestCases/Linux/protoent.cpp
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/protoent.cpp
@@ -1,69 +1,50 @@
-// RUN: %clangxx -std=c++11 -O0 -g %s -o %t
-// RUN: %clangxx -fno-sanitize=all -std=c++11 -O0 -g %s -o %t.nosan
-// RUN: 
diff  <(%run %t 2>&1) <(%run %t.nosan 2>&1)
-// REQUIRES: !android
+// RUN: %clangxx -O0 -g %s -o %t
+//
+// REQUIRES: linux, freebsd
 
-#include <assert.h>
-#include <errno.h>
 #include <netdb.h>
 #include <stdio.h>
-#include <string>
-
-std::string any_name;
-int total_count;
-
-void print_protoent(protoent *curr_entry) {
-  fprintf(stderr, "%s (%d)\n", curr_entry->p_name, curr_entry->p_proto);
+#include <stdlib.h>
+#include <assert.h>
 
-  char **aliases = curr_entry->p_aliases;
-  while (char *alias = *aliases++) {
-    fprintf(stderr, "  alias %s\n", alias);
-  }
+void test1() {
+  struct protoent *ptp = getprotoent();
+  assert(ptp && ptp->p_name);
+  assert(ptp->p_proto == 0);
+  endprotoent();
 }
 
-void print_all_protoent() {
-  protoent entry;
-  char buf[1024];
-  protoent *curr_entry;
-
-  while (getprotoent_r(&entry, buf, sizeof(buf), &curr_entry) != ENOENT && curr_entry) {
-    ++total_count;
-    any_name = curr_entry->p_name;
-    print_protoent(curr_entry);
-  }
+void test2() {
+  struct protoent *ptp = getprotobyname("tcp");
+  assert(ptp && ptp->p_name);
+  assert(ptp->p_proto == 6);
+  endprotoent();
 }
 
-void print_protoent_by_name(const char *name) {
-  protoent entry;
-  char buf[1024];
-  protoent *curr_entry;
-
-  int res = getprotobyname_r(name, &entry, buf, sizeof(buf), &curr_entry);
-  assert(!res && curr_entry);
-  print_protoent(curr_entry);
+void test3() {
+  struct protoent *ptp = getprotobynumber(1);
+  assert(ptp && ptp->p_name);
+  assert(ptp->p_proto == 1);
+  endprotoent();
 }
 
-void print_protoent_by_num(int num) {
-  protoent entry;
-  char buf[1024];
-  protoent *curr_entry;
+void test4() {
+  setprotoent(1);
+  struct protoent *ptp = getprotobynumber(1);
 
-  int res = getprotobynumber_r(num, &entry, buf, sizeof(buf), &curr_entry);
-  assert(!res && curr_entry);
-  print_protoent(curr_entry);
+  ptp = getprotobynumber(2);
+  assert(ptp && ptp->p_name);
+  assert(ptp->p_proto == 2);
+  endprotoent();
 }
 
-int main() {
-  fprintf(stderr, "All protoent\n");
-  print_all_protoent();
-
-  if (!total_count)
-    return 0;
+int main(void) {
+  printf("protoent\n");
 
-  fprintf(stderr, "Protoent by name\n");
-  print_protoent_by_name(any_name.c_str());
+  test1();
+  test2();
+  test3();
+  test4();
 
-  fprintf(stderr, "Protoent by num\n");
-  print_protoent_by_num(total_count / 2);
   return 0;
 }


        


More information about the llvm-commits mailing list