Skip to content

[libc] Fix alarm layout mismatch on 32-bit time64#201276

Merged
kaladron merged 1 commit into
llvm:mainfrom
kaladron:yocto
Jun 3, 2026
Merged

[libc] Fix alarm layout mismatch on 32-bit time64#201276
kaladron merged 1 commit into
llvm:mainfrom
kaladron:yocto

Conversation

@kaladron
Copy link
Copy Markdown
Member

@kaladron kaladron commented Jun 3, 2026

Fixed alarm implementation on 32-bit architectures with 64-bit time_t (like RISC-V 32-bit). The SYS_setitimer syscall on these platforms expects the legacy 32-bit struct itimerval (with 32-bit tv_sec and tv_usec). Convert the arguments to this layout before invoking the syscall to avoid the kernel misinterpreting the timeout.

Assisted-by: Automated tooling, human reviewed.

Fixed alarm implementation on 32-bit architectures with 64-bit time_t
(like RISC-V 32-bit). The SYS_setitimer syscall on these platforms
expects the legacy 32-bit struct itimerval (with 32-bit tv_sec and
tv_usec). Convert the arguments to this layout before invoking the
syscall to avoid the kernel misinterpreting the timeout.

Assisted-by: Automated tooling, human reviewed.
@kaladron kaladron marked this pull request as ready for review June 3, 2026 06:58
@kaladron kaladron requested a review from a team as a code owner June 3, 2026 06:58
@llvmorg-github-actions
Copy link
Copy Markdown

@llvm/pr-subscribers-libc

Author: Jeff Bailey (kaladron)

Changes

Fixed alarm implementation on 32-bit architectures with 64-bit time_t (like RISC-V 32-bit). The SYS_setitimer syscall on these platforms expects the legacy 32-bit struct itimerval (with 32-bit tv_sec and tv_usec). Convert the arguments to this layout before invoking the syscall to avoid the kernel misinterpreting the timeout.

Assisted-by: Automated tooling, human reviewed.


Full diff: https://github.com/llvm/llvm-project/pull/201276.diff

1 Files Affected:

  • (modified) libc/src/unistd/linux/alarm.cpp (+23-10)
diff --git a/libc/src/unistd/linux/alarm.cpp b/libc/src/unistd/linux/alarm.cpp
index b4c80bb484c71..f849e487a3f96 100644
--- a/libc/src/unistd/linux/alarm.cpp
+++ b/libc/src/unistd/linux/alarm.cpp
@@ -30,16 +30,29 @@ LLVM_LIBC_FUNCTION(unsigned int, alarm, (unsigned int seconds)) {
   return static_cast<unsigned int>(
       LIBC_NAMESPACE::syscall_impl<long>(SYS_alarm, seconds));
 #elif defined(SYS_setitimer)
-  struct itimerval itv, old_itv;
-  itv.it_interval.tv_sec = 0;
-  itv.it_interval.tv_usec = 0;
-  itv.it_value.tv_sec = seconds;
-  itv.it_value.tv_usec = 0;
-  if (LIBC_NAMESPACE::syscall_impl<int>(SYS_setitimer, 0 /* ITIMER_REAL */,
-                                        &itv, &old_itv) < 0)
-    return 0;
-  return static_cast<unsigned int>(old_itv.it_value.tv_sec +
-                                   (old_itv.it_value.tv_usec > 0 ? 1 : 0));
+  // On 32-bit architectures with 64-bit time_t, SYS_setitimer still expects
+  // 32-bit fields. We must convert itimerval to use 32-bit fields.
+  if constexpr (sizeof(time_t) > sizeof(long)) {
+    long itv32[4] = {0, 0, static_cast<long>(seconds), 0};
+    long old_itv32[4];
+    long ret = LIBC_NAMESPACE::syscall_impl<long>(
+        SYS_setitimer, 0 /* ITIMER_REAL */, itv32, old_itv32);
+    if (ret < 0)
+      return 0;
+    return static_cast<unsigned int>(old_itv32[2] + (old_itv32[3] > 0 ? 1 : 0));
+  } else {
+    struct itimerval itv, old_itv;
+    itv.it_interval.tv_sec = 0;
+    itv.it_interval.tv_usec = 0;
+    itv.it_value.tv_sec = seconds;
+    itv.it_value.tv_usec = 0;
+    long ret = LIBC_NAMESPACE::syscall_impl<long>(
+        SYS_setitimer, 0 /* ITIMER_REAL */, &itv, &old_itv);
+    if (ret < 0)
+      return 0;
+    return static_cast<unsigned int>(old_itv.it_value.tv_sec +
+                                     (old_itv.it_value.tv_usec > 0 ? 1 : 0));
+  }
 #else
 #error "alarm implementation not available for this architecture"
 #endif

@kaladron kaladron merged commit 57b0939 into llvm:main Jun 3, 2026
43 checks passed
@kaladron kaladron deleted the yocto branch June 3, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants