[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)
This commit is contained in:
Min 2024-11-12 11:12:30 +08:00 committed by GitHub
parent aad256598d
commit 3b29a8a008
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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) {