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

via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 11 16:57:12 PST 2024


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

>From c2293659408f6147ecf6d25a700baeb771d2bcfa Mon Sep 17 00:00:00 2001
From: magus <manypotatoes1 at gmail.com>
Date: Fri, 27 Sep 2024 17:29:39 +0800
Subject: [PATCH 1/6] [libfuzzer] use timer_create() instead of setitimer() for
 linux

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

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);

>From 001d59b1ddf3dc3a4624d40c272fc5d87b3de9e6 Mon Sep 17 00:00:00 2001
From: Min <45393763+MinxuanZ at users.noreply.github.com>
Date: Sat, 9 Nov 2024 11:43:33 +0800
Subject: [PATCH 2/6] move SetSigaction above  timer_settime

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index 469e4ed0661908..6e6413ff5dd567 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -108,6 +108,7 @@ void SetTimer(int Seconds) {
   struct itimerspec T {
     {Seconds, 0}, { Seconds, 0 }
   };
+  SetSigaction(SIGALRM, AlarmHandler);
   if (timer_create(CLOCK_REALTIME, NULL, &timerid) == -1) {
     Printf("libFuzzer: timer_create failed with %d\n", errno);
     exit(1);
@@ -115,8 +116,7 @@ void SetTimer(int Seconds) {
   if (timer_settime(timerid, 0, &T, NULL) == -1) {
     Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
-  }
-  SetSigaction(SIGALRM, AlarmHandler);
+  }  
 }
 
 void SetSignalHandler(const FuzzingOptions& Options) {

>From cca60bb411a51a338bea58a3b659af5a113d619e Mon Sep 17 00:00:00 2001
From: Min <45393763+MinxuanZ at users.noreply.github.com>
Date: Sat, 9 Nov 2024 12:48:50 +0800
Subject: [PATCH 3/6] fix format

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index 6e6413ff5dd567..ef07d556f84e00 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -116,7 +116,7 @@ void SetTimer(int Seconds) {
   if (timer_settime(timerid, 0, &T, NULL) == -1) {
     Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
-  }  
+  }
 }
 
 void SetSignalHandler(const FuzzingOptions& Options) {

>From 4e9f3063b70abae89e0cd81a86327a6ff3edc53a Mon Sep 17 00:00:00 2001
From: Min <45393763+MinxuanZ at users.noreply.github.com>
Date: Mon, 11 Nov 2024 16:00:32 +0800
Subject: [PATCH 4/6] use nullptr

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index ef07d556f84e00..daca295339e458 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -109,11 +109,11 @@ void SetTimer(int Seconds) {
     {Seconds, 0}, { Seconds, 0 }
   };
   SetSigaction(SIGALRM, AlarmHandler);
-  if (timer_create(CLOCK_REALTIME, NULL, &timerid) == -1) {
+  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, NULL) == -1) {
+  if (timer_settime(timerid, 0, &T, nullptr) == -1) {
     Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
   }

>From 06ed01d32e86a08766d6b5d2769b80472a2bebd9 Mon Sep 17 00:00:00 2001
From: Min <45393763+MinxuanZ at users.noreply.github.com>
Date: Mon, 11 Nov 2024 17:07:30 +0800
Subject: [PATCH 5/6] fix format

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index daca295339e458..46f2c6837f0d72 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -104,16 +104,16 @@ bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
 }
 
 void SetTimer(int Seconds) {
-  timer_t timerid;
+  timer_t TimerId;
   struct itimerspec T {
-    {Seconds, 0}, { Seconds, 0 }
+    { Seconds, 0 }, { Seconds, 0 }
   };
   SetSigaction(SIGALRM, AlarmHandler);
-  if (timer_create(CLOCK_REALTIME, nullptr, &timerid) == -1) {
+  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) {
+  if (timer_settime(TimerId, 0, &T, nullptr) == -1) {
     Printf("libFuzzer: timer_settime failed with %d\n", errno);
     exit(1);
   }

>From 133a5932ab0057dadfa78f7a71216feae031ccab Mon Sep 17 00:00:00 2001
From: Min <45393763+MinxuanZ at users.noreply.github.com>
Date: Tue, 12 Nov 2024 08:57:01 +0800
Subject: [PATCH 6/6] fix format

---
 compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
index 46f2c6837f0d72..36a1122c9f7cc8 100644
--- a/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
+++ b/compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp
@@ -106,7 +106,7 @@ bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
 void SetTimer(int Seconds) {
   timer_t TimerId;
   struct itimerspec T {
-    { Seconds, 0 }, { Seconds, 0 }
+    {Seconds, 0}, { Seconds, 0 }
   };
   SetSigaction(SIGALRM, AlarmHandler);
   if (timer_create(CLOCK_REALTIME, nullptr, &TimerId) == -1) {



More information about the llvm-commits mailing list