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

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 11 19:12:33 PST 2024


Author: Min
Date: 2024-11-11T19:12:30-08:00
New Revision: 3b29a8a00809e868e3df7e687695670ff5077fbd

URL: https://github.com/llvm/llvm-project/commit/3b29a8a00809e868e3df7e687695670ff5077fbd
DIFF: https://github.com/llvm/llvm-project/commit/3b29a8a00809e868e3df7e687695670ff5077fbd.diff

LOG: [libfuzzer] use timer_create() instead of setitimer() for linux (#110274)

SetTimer() now uses setitimer() 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)

Added: 
    

Modified: 
    compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index 392c1e5be4eea3..36a1122c9f7cc8 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -104,14 +104,19 @@ 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);
+  SetSigaction(SIGALRM, AlarmHandler);
+  if (timer_create(CLOCK_REALTIME, nullptr, &TimerId) == -1) {
+    Printf("libFuzzer: timer_create failed with %d\n", errno);
+    exit(1);
+  }
+  if (timer_settime(TimerId, 0, &T, nullptr) == -1) {
+    Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
   }
-  SetSigaction(SIGALRM, AlarmHandler);
 }
 
 void SetSignalHandler(const FuzzingOptions& Options) {


        


More information about the llvm-commits mailing list