[libc-commits] [libc] [llvm] [libc] Use a syscall wrapper for clock_gettime (PR #238737)

Connector Switch via libc-commits libc-commits at lists.llvm.org
Wed Nov 4 02:46:54 PST 2026


https://github.com/c8ef created https://github.com/llvm/llvm-project/pull/238737

This PR move clock_gettime behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.


>From 00648217d97bcda6f6124a4a10726cc7be0748db Mon Sep 17 00:00:00 2001
From: c8ef <c8ef at outlook.com>
Date: Wed, 4 Nov 2026 18:43:03 +0800
Subject: [PATCH] [libc] Use a syscall wrapper for clock_gettime

This PR move clock_gettime behind the centralized syscall wrappers, following libc/docs/dev/syscall_wrapper_refactor.md.
---
 .../linux/syscall_wrappers/CMakeLists.txt     | 14 +++++
 .../linux/syscall_wrappers/clock_gettime.h    | 53 +++++++++++++++++++
 libc/src/__support/time/linux/CMakeLists.txt  |  7 +--
 .../__support/time/linux/clock_gettime.cpp    | 23 +++-----
 .../llvm-project-overlay/libc/BUILD.bazel     | 19 ++++++-
 5 files changed, 97 insertions(+), 19 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index d69d6f15455f11..21300c4d5b630b 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1902,6 +1902,20 @@ add_header_library(
     libc.src.__support.macros.config
 )
 
+add_header_library(
+  clock_gettime
+  HDRS
+    clock_gettime.h
+  DEPENDS
+    libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.include.sys_syscall
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+)
+
 add_header_library(
   getrusage
   HDRS
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h
new file mode 100644
index 00000000000000..b868012e826c73
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h
@@ -0,0 +1,53 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header for clock_gettime syscall wrapper.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_GETTIME_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_GETTIME_H
+
+#include "hdr/types/clockid_t.h"
+#include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall.h" // syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h>
+
+#if defined(SYS_clock_gettime64)
+#include <linux/time_types.h>
+#endif
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+#if defined(SYS_clock_gettime64)
+LIBC_INLINE ErrorOr<int> clock_gettime(clockid_t clockid,
+                                       __kernel_timespec *ts) {
+  return syscall_checked<int>(SYS_clock_gettime64, clockid, ts);
+}
+#elif defined(SYS_clock_gettime)
+LIBC_INLINE ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
+  static_assert(
+      sizeof(timespec::tv_nsec) == sizeof(long),
+      "This legacy syscall fallback is only safe on platforms where tv_nsec "
+      "matches the register size (long). It is unsafe on 32-bit platforms "
+      "with 64-bit tv_nsec.");
+  return syscall_checked<int>(SYS_clock_gettime, clockid, ts);
+}
+#else
+#error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
+#endif
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_SYSCALL_WRAPPERS_CLOCK_GETTIME_H
diff --git a/libc/src/__support/time/linux/CMakeLists.txt b/libc/src/__support/time/linux/CMakeLists.txt
index a7bdb7b36be491..4a8be1d2db9243 100644
--- a/libc/src/__support/time/linux/CMakeLists.txt
+++ b/libc/src/__support/time/linux/CMakeLists.txt
@@ -6,12 +6,13 @@ add_object_library(
     clock_gettime.cpp
   DEPENDS
     libc.include.sys_syscall
-    libc.hdr.types.struct_timespec
     libc.hdr.types.clockid_t
+    libc.hdr.types.struct_timespec
+    libc.src.__support.OSUtil.linux.syscall_wrappers.clock_gettime
+    libc.src.__support.OSUtil.linux.vdso
     libc.src.__support.common
     libc.src.__support.error_or
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.OSUtil.linux.vdso
+    libc.src.__support.macros.config
 )
 
 add_object_library(
diff --git a/libc/src/__support/time/linux/clock_gettime.cpp b/libc/src/__support/time/linux/clock_gettime.cpp
index ac77f11bb16075..29e3c599df655e 100644
--- a/libc/src/__support/time/linux/clock_gettime.cpp
+++ b/libc/src/__support/time/linux/clock_gettime.cpp
@@ -9,8 +9,8 @@
 #include "src/__support/time/clock_gettime.h"
 #include "hdr/types/clockid_t.h"
 #include "hdr/types/struct_timespec.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h"
 #include "src/__support/OSUtil/linux/vdso.h"
-#include "src/__support/OSUtil/syscall.h"
 #include "src/__support/common.h"
 #include "src/__support/error_or.h"
 #include "src/__support/macros/config.h"
@@ -31,9 +31,8 @@ ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
     if (LIBC_LIKELY(clock_gettime64 != nullptr))
       ret = clock_gettime64(clockid, reinterpret_cast<__kernel_timespec *>(ts));
     else
-      ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64,
-                                              static_cast<long>(clockid),
-                                              reinterpret_cast<long>(ts));
+      return linux_syscalls::clock_gettime(
+          clockid, reinterpret_cast<__kernel_timespec *>(ts));
   } else {
     __kernel_timespec ts64{};
     __kernel_timespec *ts64_ptr = nullptr;
@@ -42,9 +41,10 @@ ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
     if (ts64_ptr != nullptr && LIBC_LIKELY(clock_gettime64 != nullptr)) {
       ret = clock_gettime64(clockid, ts64_ptr);
     } else {
-      ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime64,
-                                              static_cast<long>(clockid),
-                                              reinterpret_cast<long>(ts64_ptr));
+      auto result = linux_syscalls::clock_gettime(clockid, ts64_ptr);
+      if (!result)
+        return Error(result.error());
+      ret = result.value();
     }
     if (ret == 0 && ts != nullptr) {
       ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec);
@@ -52,18 +52,11 @@ ErrorOr<int> clock_gettime(clockid_t clockid, timespec *ts) {
     }
   }
 #elif defined(SYS_clock_gettime)
-  static_assert(
-      sizeof(timespec::tv_nsec) == sizeof(long),
-      "This legacy syscall fallback is only safe on platforms where tv_nsec "
-      "matches the register size (long). It is unsafe on 32-bit platforms "
-      "with 64-bit tv_nsec.");
   TypedSymbol<VDSOSym::ClockGetTime> clock_gettime;
   if (LIBC_LIKELY(clock_gettime != nullptr))
     ret = clock_gettime(clockid, ts);
   else
-    ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_gettime,
-                                            static_cast<long>(clockid),
-                                            reinterpret_cast<long>(ts));
+    return linux_syscalls::clock_gettime(clockid, ts);
 #else
 #error "SYS_clock_gettime and SYS_clock_gettime64 syscalls not available."
 #endif
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index c59f23190dbcca..cb35dc10dd84cf 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -4358,6 +4358,23 @@ libc_support_library(
     ],
 )
 
+libc_support_library(
+    name = "__support_osutil_linux_syscall_wrappers_clock_gettime",
+    hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/clock_gettime.h"],
+    target_compatible_with = select({
+        "@platforms//os:linux": [],
+        "//conditions:default": ["@platforms//:incompatible"],
+    }),
+    deps = [
+        ":__support_common",
+        ":__support_error_or",
+        ":__support_macros_config",
+        ":__support_osutil_syscall",
+        ":types_clockid_t",
+        ":types_struct_timespec",
+    ],
+)
+
 libc_support_library(
     name = "__support_osutil_linux_syscall_wrappers_close",
     hdrs = ["src/__support/OSUtil/linux/syscall_wrappers/close.h"],
@@ -5401,7 +5418,7 @@ libc_support_library(
         ":__support_common",
         ":__support_error_or",
         ":__support_macros_config",
-        ":__support_osutil_syscall",
+        ":__support_osutil_linux_syscall_wrappers_clock_gettime",
         ":__support_osutil_vdso",
         ":types_clockid_t",
         ":types_struct_timespec",



More information about the libc-commits mailing list