[compiler-rt] [libfuzzer] use timer_create() instead of setitimer() for linux (PR #110274)

via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 27 07:28:54 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Min (MinxuanZ)

<details>
<summary>Changes</summary>

SetTimer() now uses getitimer() to sending SIGALRM every ` UnitTimeoutSec/2 + 1` s
Set UnitTimeoutSec with the `-timeout=` option

 "POSIX.1-2008 marks getitimer() and setitimer() obsolete" and also has some issues regarding accuracy of the timers under load . See https://linux.die.net/man/2/setitimer.
I propose using timer_create() and sigaction() ,See http://man7.org/linux/man-pages/man2/timer_create.2.html

# test result on my x86_64 linux
`make check-fuzzer`
![image](https://github.com/user-attachments/assets/19b4e073-16a5-4daa-95ed-2cf4830c042f)


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


1 Files Affected:

- (modified) compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp (+8-3) 


``````````diff
diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index 392c1e5be4eea3..469e4ed0661908 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -104,11 +104,16 @@ bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
 }
 
 void SetTimer(int Seconds) {
-  struct itimerval T {
+  timer_t timerid;
+  struct itimerspec T {
     {Seconds, 0}, { Seconds, 0 }
   };
-  if (setitimer(ITIMER_REAL, &T, nullptr)) {
-    Printf("libFuzzer: setitimer failed with %d\n", errno);
+  if (timer_create(CLOCK_REALTIME, NULL, &timerid) == -1) {
+    Printf("libFuzzer: timer_create failed with %d\n", errno);
+    exit(1);
+  }
+  if (timer_settime(timerid, 0, &T, NULL) == -1) {
+    Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
   }
   SetSigaction(SIGALRM, AlarmHandler);

``````````

</details>


https://github.com/llvm/llvm-project/pull/110274


More information about the llvm-commits mailing list