[compiler-rt] r341181 - Add internal_sysctl() used by FreeBSD, NetBSD, OpenBSD and MacOSX

Kamil Rytarowski via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 31 01:10:06 PDT 2018


Author: kamil
Date: Fri Aug 31 01:10:06 2018
New Revision: 341181

URL: http://llvm.org/viewvc/llvm-project?rev=341181&view=rev
Log:
Add internal_sysctl() used by FreeBSD, NetBSD, OpenBSD and MacOSX

Summary:
Switch local sysctl(2) calls to internal_sysctl().

This is a preparation for introduction of interceptors for
the sysctl*() family of functions and switching `internal_sysctl*()`
to libc calls bypassing interceptors.

No functional change intended with this revision.

Reviewers: vitalybuka, joerg, kcc

Reviewed By: vitalybuka

Subscribers: kubamracek, llvm-commits, #sanitizers

Tags: #sanitizers

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

Modified:
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_openbsd.cc
    compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h

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=341181&r1=341180&r2=341181&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux.cc Fri Aug 31 01:10:06 2018
@@ -648,10 +648,10 @@ void ReExec() {
     CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
   };
   char path[400];
-  size_t len;
+  uptr len;
 
   len = sizeof(path);
-  if (sysctl(name, ARRAY_SIZE(name), path, &len, NULL, 0) != -1)
+  if (internal_sysctl(name, ARRAY_SIZE(name), path, &len, NULL, 0) != -1)
     pathname = path;
 #elif SANITIZER_SOLARIS
   pathname = getexecname();
@@ -815,6 +815,13 @@ int internal_fork() {
 #endif
 }
 
+#if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
+int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
+                    uptr *oldlenp, const void *newp, uptr newlen) {
+  return sysctl(name, namelen, oldp, oldlenp, newp, newlen);
+}
+#endif
+
 #if SANITIZER_LINUX
 #define SA_RESTORER 0x04000000
 // Doesn't set sa_restorer if the caller did not set it, so use with caution
@@ -1114,8 +1121,9 @@ uptr ReadBinaryName(/*out*/char *buf, up
   const int Mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
 #endif
   const char *default_module_name = "kern.proc.pathname";
-  size_t Size = buf_len;
-  bool IsErr = (sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
+  uptr Size = buf_len;
+  bool IsErr =
+      (internal_sysctl(Mib, ARRAY_SIZE(Mib), buf, &Size, NULL, 0) != 0);
   int readlink_error = IsErr ? errno : 0;
   uptr module_name_len = Size;
 #else
@@ -1980,13 +1988,13 @@ void CheckASLR() {
 #if SANITIZER_NETBSD
   int mib[3];
   int paxflags;
-  size_t len = sizeof(paxflags);
+  uptr len = sizeof(paxflags);
 
   mib[0] = CTL_PROC;
   mib[1] = internal_getpid();
   mib[2] = PROC_PID_PAXFLAGS;
 
-  if (UNLIKELY(sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
+  if (UNLIKELY(internal_sysctl(mib, 3, &paxflags, &len, NULL, 0) == -1)) {
     Printf("sysctl failed\n");
     Die();
   }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc?rev=341181&r1=341180&r2=341181&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_linux_libcdep.cc Fri Aug 31 01:10:06 2018
@@ -652,10 +652,10 @@ u32 GetNumberOfCPUs() {
 #if SANITIZER_FREEBSD || SANITIZER_NETBSD || SANITIZER_OPENBSD
   u32 ncpu;
   int req[2];
-  size_t len = sizeof(ncpu);
+  uptr len = sizeof(ncpu);
   req[0] = CTL_HW;
   req[1] = HW_NCPU;
-  CHECK_EQ(sysctl(req, 2, &ncpu, &len, NULL, 0), 0);
+  CHECK_EQ(internal_sysctl(req, 2, &ncpu, &len, NULL, 0), 0);
   return ncpu;
 #elif SANITIZER_ANDROID && !defined(CPU_COUNT) && !defined(__aarch64__)
   // Fall back to /sys/devices/system/cpu on Android when cpu_set_t doesn't

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=341181&r1=341180&r2=341181&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_mac.cc Fri Aug 31 01:10:06 2018
@@ -213,6 +213,11 @@ int internal_fork() {
   return fork();
 }
 
+int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
+                    uptr *oldlenp, const void *newp, uptr newlen) {
+  return sysctl(name, namelen, oldp, oldlenp, newp, newlen);
+}
+
 int internal_forkpty(int *amaster) {
   int master, slave;
   if (openpty(&master, &slave, nullptr, nullptr, nullptr) == -1) return -1;
@@ -499,9 +504,9 @@ MacosVersion GetMacosVersionInternal() {
   uptr len = 0, maxlen = sizeof(version) / sizeof(version[0]);
   for (uptr i = 0; i < maxlen; i++) version[i] = '\0';
   // Get the version length.
-  CHECK_NE(sysctl(mib, 2, 0, &len, 0, 0), -1);
+  CHECK_NE(internal_sysctl(mib, 2, 0, &len, 0, 0), -1);
   CHECK_LT(len, maxlen);
-  CHECK_NE(sysctl(mib, 2, version, &len, 0, 0), -1);
+  CHECK_NE(internal_sysctl(mib, 2, version, &len, 0, 0), -1);
   switch (version[0]) {
     case '9': return MACOS_VERSION_LEOPARD;
     case '1': {

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_openbsd.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_openbsd.cc?rev=341181&r1=341180&r2=341181&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_openbsd.cc (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_openbsd.cc Fri Aug 31 01:10:06 2018
@@ -54,9 +54,9 @@ int internal_mprotect(void *addr, uptr l
 uptr ReadBinaryName(/*out*/char *buf, uptr buf_len) {
   // On OpenBSD we cannot get the full path
   struct kinfo_proc kp;
-  size_t kl;
+  uptr kl;
   const int Mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()};
-  if (sysctl(Mib, ARRAY_SIZE(Mib), &kp, &kl, NULL, 0) != -1)
+  if (internal_sysctl(Mib, ARRAY_SIZE(Mib), &kp, &kl, NULL, 0) != -1)
     return internal_snprintf(buf,
                              (KI_MAXCOMLEN < buf_len ? KI_MAXCOMLEN : buf_len),
                              "%s", kp.p_comm);
@@ -64,23 +64,23 @@ uptr ReadBinaryName(/*out*/char *buf, up
 }
 
 static void GetArgsAndEnv(char ***argv, char ***envp) {
-  size_t nargv;
-  size_t nenv;
+  uptr nargv;
+  uptr nenv;
   int argvmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV};
   int envmib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ENV};
-  if (sysctl(argvmib, 4, NULL, &nargv, NULL, 0) == -1) {
+  if (internal_sysctl(argvmib, 4, NULL, &nargv, NULL, 0) == -1) {
     Printf("sysctl KERN_PROC_NARGV failed\n");
     Die();
   }
-  if (sysctl(envmib, 4, NULL, &nenv, NULL, 0) == -1) {
+  if (internal_sysctl(envmib, 4, NULL, &nenv, NULL, 0) == -1) {
     Printf("sysctl KERN_PROC_NENV failed\n");
     Die();
   }
-  if (sysctl(argvmib, 4, &argv, &nargv, NULL, 0) == -1) {
+  if (internal_sysctl(argvmib, 4, &argv, &nargv, NULL, 0) == -1) {
     Printf("sysctl KERN_PROC_ARGV failed\n");
     Die();
   }
-  if (sysctl(envmib, 4, &envp, &nenv, NULL, 0) == -1) {
+  if (internal_sysctl(envmib, 4, &envp, &nenv, NULL, 0) == -1) {
     Printf("sysctl KERN_PROC_ENV failed\n");
     Die();
   }

Modified: compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h?rev=341181&r1=341180&r2=341181&view=diff
==============================================================================
--- compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h (original)
+++ compiler-rt/trunk/lib/sanitizer_common/sanitizer_posix.h Fri Aug 31 01:10:06 2018
@@ -60,6 +60,9 @@ uptr internal_waitpid(int pid, int *stat
 int internal_fork();
 int internal_forkpty(int *amaster);
 
+int internal_sysctl(const int *name, unsigned int namelen, void *oldp,
+                    uptr *oldlenp, const void *newp, uptr newlen);
+
 // These functions call appropriate pthread_ functions directly, bypassing
 // the interceptor. They are weak and may not be present in some tools.
 SANITIZER_WEAK_ATTRIBUTE




More information about the llvm-commits mailing list