[libc-commits] [libc] [libc] Add sanitizer syscall hook macros (PR #244940)

Aiden Grossman via libc-commits libc-commits at lists.llvm.org
Sat Nov 21 13:24:42 PST 2026


https://github.com/boomanaiden154 created https://github.com/llvm/llvm-project/pull/244940

Adds some infrastructure (primarily macros) that will be useful for
adding sanitizer instrument around syscalls using the standard
__sanitizer_syscall_pre_<name> and __sanitizer_syscall_pre_impl_<name>
hooks.


>From 230d64a399c3be0356dfe53bbf3fe928f6dddb82 Mon Sep 17 00:00:00 2001
From: Aiden Grossman <aidengrossman at google.com>
Date: Sat, 21 Nov 2026 21:24:29 +0000
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.7
---
 .../src/__support/OSUtil/linux/CMakeLists.txt |  1 +
 libc/src/__support/OSUtil/linux/syscall.h     |  7 +++++
 libc/src/__support/macros/sanitizer.h         | 27 +++++++++++++++++++
 3 files changed, 35 insertions(+)

diff --git a/libc/src/__support/OSUtil/linux/CMakeLists.txt b/libc/src/__support/OSUtil/linux/CMakeLists.txt
index 895621614e0dde..67ae0f61b11061 100644
--- a/libc/src/__support/OSUtil/linux/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/CMakeLists.txt
@@ -17,6 +17,7 @@ add_object_library(
     .${LIBC_TARGET_ARCHITECTURE}.linux_${LIBC_TARGET_ARCHITECTURE}_util
     libc.src.__support.common
     libc.src.__support.CPP.string_view
+    libc.src.__support.macros.sanitizer
     libc.include.sys_syscall
     libc.src.__support.OSUtil.linux.syscall_wrappers.exit_group
 )
diff --git a/libc/src/__support/OSUtil/linux/syscall.h b/libc/src/__support/OSUtil/linux/syscall.h
index 5e73aa27f56fcc..41a01ab514c848 100644
--- a/libc/src/__support/OSUtil/linux/syscall.h
+++ b/libc/src/__support/OSUtil/linux/syscall.h
@@ -15,6 +15,7 @@
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/optimization.h"
 #include "src/__support/macros/properties/architectures.h"
+#include "src/__support/macros/sanitizer.h"
 
 #ifdef LIBC_TARGET_ARCH_IS_X86_32
 #include "i386/syscall.h"
@@ -55,6 +56,12 @@ template <typename R, typename... Ts>
     return Error(static_cast<int>(-ret));
   return cpp::bit_or_static_cast<R>(ret);
 }
+
+template <typename R>
+LIBC_INLINE long syscall_result_to_long(const ErrorOr<R> &res) {
+  return res.has_value() ? cpp::bit_or_static_cast<long>(res.value())
+                         : -static_cast<long>(res.error());
+}
 } // namespace linux_syscalls
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/__support/macros/sanitizer.h b/libc/src/__support/macros/sanitizer.h
index 6218705198fcd4..d3bb37837a08d3 100644
--- a/libc/src/__support/macros/sanitizer.h
+++ b/libc/src/__support/macros/sanitizer.h
@@ -67,4 +67,31 @@
   ((void)(addr), (void)(size))
 #endif
 
+//-----------------------------------------------------------------------------
+// Linux syscall sanitizer hooks
+//-----------------------------------------------------------------------------
+
+#if (defined(LIBC_HAS_ADDRESS_SANITIZER) ||                                    \
+     defined(LIBC_HAS_MEMORY_SANITIZER) ||                                     \
+     LIBC_HAS_FEATURE(hwaddress_sanitizer) ||                                  \
+     LIBC_HAS_FEATURE(thread_sanitizer) || defined(__SANITIZE_THREAD__)) &&    \
+    defined(__linux__) && __has_include(<sanitizer/linux_syscall_hooks.h>)
+#include <sanitizer/linux_syscall_hooks.h>
+#define LIBC_SANITIZER_SYSCALL_PRE(name, ...)                                  \
+  __sanitizer_syscall_pre_##name(__VA_ARGS__)
+#define LIBC_SANITIZER_SYSCALL_PRE_0(name) __sanitizer_syscall_pre_##name()
+#define LIBC_SANITIZER_SYSCALL_POST(name, ...)                                 \
+  __sanitizer_syscall_post_##name(__VA_ARGS__)
+#else
+#define LIBC_SANITIZER_SYSCALL_PRE(name, ...)                                  \
+  do {                                                                         \
+  } while (0)
+#define LIBC_SANITIZER_SYSCALL_PRE_0(name)                                     \
+  do {                                                                         \
+  } while (0)
+#define LIBC_SANITIZER_SYSCALL_POST(name, ...)                                 \
+  do {                                                                         \
+  } while (0)
+#endif
+
 #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_SANITIZER_H



More information about the libc-commits mailing list