From 0713053e4a3f08fc0a8408400ee5c4d20a188765 Mon Sep 17 00:00:00 2001
From: Kevin Athey <kda@google.com>
Date: Thu, 7 Apr 2022 09:40:49 -0700
Subject: [PATCH] [MSAN] extend prctl interceptor to support PR_SCHED_CORE

Reviewed By: eugenis

Differential Revision: https://reviews.llvm.org/D122851
---
 .../sanitizer_common_interceptors.inc         |  4 +++
 .../TestCases/Linux/prctl.cpp                 | 26 +++++++++++++++++++
 2 files changed, 30 insertions(+)
 create mode 100644 compiler-rt/test/sanitizer_common/TestCases/Linux/prctl.cpp

diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
index 18b023078441..9b98261b0d0f 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors.inc
@@ -1365,12 +1365,16 @@ INTERCEPTOR(int, prctl, int option, unsigned long arg2, unsigned long arg3,
   void *ctx;
   COMMON_INTERCEPTOR_ENTER(ctx, prctl, option, arg2, arg3, arg4, arg5);
   static const int PR_SET_NAME = 15;
+  static const int PR_SCHED_CORE = 62;
+  static const int PR_SCHED_CORE_GET = 0;
   int res = REAL(prctl(option, arg2, arg3, arg4, arg5));
   if (option == PR_SET_NAME) {
     char buff[16];
     internal_strncpy(buff, (char *)arg2, 15);
     buff[15] = 0;
     COMMON_INTERCEPTOR_SET_THREAD_NAME(ctx, buff);
+  } else if (res != -1 && option == PR_SCHED_CORE && arg2 == PR_SCHED_CORE_GET) {
+    COMMON_INTERCEPTOR_WRITE_RANGE(ctx, (u64*)(arg5), sizeof(u64));
   }
   return res;
 }
diff --git a/compiler-rt/test/sanitizer_common/TestCases/Linux/prctl.cpp b/compiler-rt/test/sanitizer_common/TestCases/Linux/prctl.cpp
new file mode 100644
index 000000000000..f1d19b3e7e6e
--- /dev/null
+++ b/compiler-rt/test/sanitizer_common/TestCases/Linux/prctl.cpp
@@ -0,0 +1,26 @@
+// RUN: %clangxx %s -o %t && %run %t %p
+
+#include <assert.h>
+#include <errno.h>
+#include <stdint.h>
+#include <sys/prctl.h>
+
+int main() {
+
+  int res;
+  res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, 0, 0);
+  if (res < 0) {
+    assert(errno == EINVAL);
+    return 0;
+  }
+
+  uint64_t cookie = 0;
+  res = prctl(PR_SCHED_CORE, PR_SCHED_CORE_GET, 0, 0, &cookie);
+  if (res < 0) {
+    assert(errno == EINVAL);
+  } else {
+    assert(cookie != 0);
+  }
+
+  return 0;
+}
-- 
GitLab