[compiler-rt] [compiler-rt] intercept freebsd's cpuset_getdomain api. (PR #76851)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 3 14:33:18 PST 2024


https://github.com/devnexen updated https://github.com/llvm/llvm-project/pull/76851

>From 792b1396d1aad6de65654453324df530843e1d7f Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Wed, 3 Jan 2024 19:26:31 +0000
Subject: [PATCH 1/2] [compiler-rt] intercept freebsd's cpuset_getdomain api.

---
 .../sanitizer_common_interceptors.inc         | 19 +++++++++++++++++++
 .../sanitizer_platform_interceptors.h         |  1 +
 .../sanitizer_platform_limits_freebsd.cpp     |  2 ++
 .../sanitizer_platform_limits_freebsd.h       |  7 +++++++
 4 files changed, 29 insertions(+)

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 77fa1b4965a7a4..2723423938068b 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -10258,6 +10258,24 @@ INTERCEPTOR(int, cpuset_getaffinity, int level, int which, __int64_t id, SIZE_T
 #define INIT_CPUSET_GETAFFINITY
 #endif
 
+#if SANITIZER_INTERCEPT_CPUSET_GETDOMAIN
+INTERCEPTOR(int, cpuset_getdomain, int level, int which, __int64 id,
+            SIZE_T domainsetsize, __sanitizer_domainset_t *mask, int *policy) {
+  void *ctx;
+  COMMON_INTERCEPTOR_ENTER(ctx, cpuset_getdomain, level, which, id, domainsetsize,
+                           mask, policy);
+  int res = REAL(cpuset_getdomain)(level, which, id, domainsetsize, mask, policy);
+  if (mask && !res) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, mask, domainsetsize);
+    if (policy) COMMON_INTERCEPTOR_WRITE_RANGE(ctx, policy, sizeof(*policy));
+  }
+  return res;
+}
+#define INIT_CPUSET_GETDOMAIN COMMON_INTERCEPT_FUNCTION(cpuset_getdomain);
+#else
+#define INIT_CPUSET_GETDOMAIN
+#endif
+
 #include "sanitizer_common_interceptors_netbsd_compat.inc"
 
 namespace __sanitizer {
@@ -10578,6 +10596,7 @@ static void InitializeCommonInterceptors() {
   INIT_HEXDUMP;
   INIT_ARGP_PARSE;
   INIT_CPUSET_GETAFFINITY;
+  INIT_CPUSET_GETDOMAIN;
 
   INIT___PRINTF_CHK;
 }
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 0ce4e9351bc1da..02d4fbd970ef45 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -599,6 +599,7 @@
 #define SANITIZER_INTERCEPT_HEXDUMP SI_FREEBSD
 #define SANITIZER_INTERCEPT_ARGP_PARSE SI_GLIBC
 #define SANITIZER_INTERCEPT_CPUSET_GETAFFINITY SI_FREEBSD
+#define SANITIZER_INTERCEPT CPUSET_GETDOMAIN SI_FREEBSD
 
 // This macro gives a way for downstream users to override the above
 // interceptor macros irrespective of the platform they are on. They have
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
index 38f968d533b147..27c53c3ec82322 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.cpp
@@ -18,6 +18,7 @@
 #include <sys/capsicum.h>
 #include <sys/consio.h>
 #include <sys/cpuset.h>
+#include <sys/domainset.h>
 #include <sys/filio.h>
 #include <sys/ipc.h>
 #include <sys/kbio.h>
@@ -105,6 +106,7 @@ void *__sanitizer_get_link_map_by_dlopen_handle(void *handle) {
 }
 
 unsigned struct_cpuset_sz = sizeof(cpuset_t);
+unsigned struct_domainset_sz = sizeof(domainset_t);
 unsigned struct_cap_rights_sz = sizeof(cap_rights_t);
 unsigned struct_utsname_sz = sizeof(struct utsname);
 unsigned struct_stat_sz = sizeof(struct stat);
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 43b8a38f39be18..6a5fa5e422fe42 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_limits_freebsd.h
@@ -724,9 +724,16 @@ struct __sanitizer_cpuset {
 #endif
 };
 
+struct __sanitizer_domainset {
+  long __bits[(256 + (sizeof(long) * 8) - 1) / (sizeof(long) * 8)];
+};
+
 typedef struct __sanitizer_cpuset __sanitizer_cpuset_t;
 extern unsigned struct_cpuset_sz;
 
+typedef struct __sanitizer_domainset __sanitizer_domainset_t;
+extern unsigned struct_domainset_sz;
+
 typedef unsigned long long __sanitizer_eventfd_t;
 }  // namespace __sanitizer
 

>From f6c6db6bdd289258f72cd0efa2c4acfbd4bb6e51 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Wed, 3 Jan 2024 22:31:58 +0000
Subject: [PATCH 2/2] adding test

---
 .../sanitizer_platform_interceptors.h            |  2 +-
 .../TestCases/FreeBSD/cpuset_getcomain.cpp       | 16 ++++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/FreeBSD/cpuset_getcomain.cpp

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
index 02d4fbd970ef45..b3ba89e7d3c9ad 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_platform_interceptors.h
@@ -599,7 +599,7 @@
 #define SANITIZER_INTERCEPT_HEXDUMP SI_FREEBSD
 #define SANITIZER_INTERCEPT_ARGP_PARSE SI_GLIBC
 #define SANITIZER_INTERCEPT_CPUSET_GETAFFINITY SI_FREEBSD
-#define SANITIZER_INTERCEPT CPUSET_GETDOMAIN SI_FREEBSD
+#define SANITIZER_INTERCEPT_CPUSET_GETDOMAIN SI_FREEBSD
 
 // This macro gives a way for downstream users to override the above
 // interceptor macros irrespective of the platform they are on. They have
diff --git a/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/cpuset_getcomain.cpp b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/cpuset_getcomain.cpp
new file mode 100644
index 00000000000000..2fa7dbea77bb8f
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/FreeBSD/cpuset_getcomain.cpp
@@ -0,0 +1,16 @@
+// RUN: %clangxx -O0 %s -o %t && %run %t
+
+#include <sys/types.h>
+#include <sys/cpuset.h>
+#include <sys/domainset.h>
+#include <assert.h>
+
+int main() {
+   domainset_t ds;
+   int pc;
+
+   int res = cpuset_getdomain(CPU_LEVEL_ROOT, CPU_WHICH_PID, -1, sizeof(ds), &ds, &pc);
+   assert(res == 0);
+   assert(pc != DOMAINSET_POLICY_INVALID);
+   return 0;
+}



More information about the llvm-commits mailing list