Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 12, 2024

Conversation

minxuanz
Copy link
Contributor

@minxuanz minxuanz commented Sep 27, 2024

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

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Sep 27, 2024

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

Author: Min (MinxuanZ)

Changes

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


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

1 Files Affected:

  • (modified) compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp (+8-3)
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);

@minxuanz
Copy link
Contributor Author

minxuanz commented Nov 9, 2024

Ping , could anyone review

Copy link

github-actions bot commented Nov 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@fmayer
Copy link
Contributor

fmayer commented Nov 9, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
View the diff from clang-format here.

Please fix

@minxuanz
Copy link
Contributor Author

minxuanz commented Nov 9, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:

View the diff from clang-format here.

Please fix

fixed

@fmayer
Copy link
Contributor

fmayer commented Nov 11, 2024

Thanks!

@fmayer fmayer merged commit 3b29a8a into llvm:main Nov 12, 2024
7 checks passed
Copy link

@minxuanz Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 12, 2024

LLVM Buildbot has detected a new failure on builder llvm-clang-x86_64-gcc-ubuntu running on sie-linux-worker3 while building compiler-rt at step 6 "test-build-unified-tree-check-all".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/174/builds/8274

Here is the relevant piece of the build log for the reference
Step 6 (test-build-unified-tree-check-all) failure: test (failure)
...
0.034 [2/6/4] Building CXX object compiler-rt/lib/memprof/CMakeFiles/RTMemprof_dynamic_version_script_dummy.x86_64.dir/dummy.cpp.o
0.034 [1/6/5] Building CXX object compiler-rt/lib/nsan/CMakeFiles/RTNsan_dynamic_version_script_dummy.x86_64.dir/dummy.cpp.o
0.078 [0/6/6] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.ubsan_standalone.so
0.080 [0/5/7] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.nsan.so
0.086 [0/4/8] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.hwasan_aliases.so
0.091 [0/3/9] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.hwasan.so
0.093 [0/2/10] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.memprof.so
0.103 [0/1/11] Linking CXX shared library /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/lib/clang/20/lib/x86_64-unknown-linux-gnu/libclang_rt.asan.so
1.814 [3/1/14] cd /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins && /usr/bin/cmake/bin/cmake --build /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/ --target runtimes-test-depends --config Release
0.382 [0/2/1] Generating Fuzzer-x86_64-Test
FAILED: compiler-rt/lib/fuzzer/tests/Fuzzer-x86_64-Test /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/fuzzer/tests/Fuzzer-x86_64-Test 
cd /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/fuzzer/tests && /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./bin/clang++ FuzzerTestObjects.FuzzerUnittest.cpp.x86_64.o FuzzerTestObjects.gtest-all.cc.x86_64.o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/fuzzer/tests/libRTFuzzerTest.x86_64.a -o /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/compiler-rt/lib/fuzzer/tests/./Fuzzer-x86_64-Test -fuse-ld=gold -Wthread-safety -Wthread-safety-reference -Wthread-safety-beta -resource-dir=/home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/./lib/../lib/clang/20 --driver-mode=g++ -lpthread -m64
/usr/bin/ld.gold: internal error in read_header_prolog, at ../../gold/dwarf_reader.cc:1678
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
9.513 [0/1/2] Checking TSan Go runtime...
ninja: build stopped: subcommand failed.
FAILED: runtimes/CMakeFiles/runtimes-test-depends /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/CMakeFiles/runtimes-test-depends 
cd /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins && /usr/bin/cmake/bin/cmake --build /home/buildbot/buildbot-root/llvm-clang-x86_64-gcc-ubuntu/build/runtimes/runtimes-bins/ --target runtimes-test-depends --config Release
ninja: build stopped: subcommand failed.

@llvm-ci
Copy link
Collaborator

llvm-ci commented Nov 12, 2024

LLVM Buildbot has detected a new failure on builder sanitizer-windows running on sanitizer-windows while building compiler-rt at step 4 "annotate".

Full details are available at: https://lab.llvm.org/buildbot/#/builders/107/builds/4881

Here is the relevant piece of the build log for the reference
Step 4 (annotate) failure: 'python ../llvm-zorg/zorg/buildbot/builders/annotated/sanitizer-windows.py ...' (failure)
...
Total Discovered Tests: 680
  Unsupported      : 237 (34.85%)
  Passed           : 430 (63.24%)
  Expectedly Failed:  13 (1.91%)

1 warning(s) in tests
[124/127] Running the AddressSanitizer tests with dynamic runtime
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\llvm\utils\lit\lit\llvm\config.py:57: note: using lit tools: C:\gnuwin32\bin
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\b\slave\sanitizer-windows\build\stage1\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/b/slave/sanitizer-windows/build/stage1/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-print-runtime-dir'].
-- Testing: 540 tests, 16 workers --
command timed out: 1200 seconds without output running ['python', '../llvm-zorg/zorg/buildbot/builders/annotated/sanitizer-windows.py', '--jobs=16'], attempting to kill
program finished with exit code 1
elapsedTime=2067.439000
Testing:  0.. 10.. 20.. 30.. 40..
Step 8 (stage 1 check) failure: stage 1 check (failure)
...

Testing Time: 22.23s

Total Discovered Tests: 827
  Unsupported: 478 (57.80%)
  Passed     : 349 (42.20%)

2 warning(s) in tests
[117/127] Generating Fuzzer-x86_64-Test.exe
   Creating library C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\lib\fuzzer\tests\.\Fuzzer-x86_64-Test.lib and object C:\b\slave\sanitizer-windows\build\stage1\projects\compiler-rt\lib\fuzzer\tests\.\Fuzzer-x86_64-Test.exp

[121/127] Generating X86_64WindowsDynamicConfig/Asan-x86_64-calls-Dynamic-Test.exe
[122/127] Running libFuzzer tests
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\llvm\utils\lit\lit\llvm\config.py:57: note: using lit tools: C:\gnuwin32\bin
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\b\slave\sanitizer-windows\build\stage1\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/b/slave/sanitizer-windows/build/stage1/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-print-runtime-dir'].
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\fuzzer\lit.cfg.py:34: note: lsan feature unavailable
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\fuzzer\lit.cfg.py:43: note: msan feature unavailable
-- Testing: 176 tests, 16 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90.. 

Testing Time: 453.72s

Total Discovered Tests: 196
  Unsupported:  48 (24.49%)
  Passed     : 148 (75.51%)

1 warning(s) in tests
[123/127] Running the AddressSanitizer tests
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\llvm\utils\lit\lit\llvm\config.py:57: note: using lit tools: C:\gnuwin32\bin
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\b\slave\sanitizer-windows\build\stage1\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/b/slave/sanitizer-windows/build/stage1/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-print-runtime-dir'].
-- Testing: 572 tests, 16 workers --
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..

Testing Time: 96.44s

Total Discovered Tests: 680
  Unsupported      : 237 (34.85%)
  Passed           : 430 (63.24%)
  Expectedly Failed:  13 (1.91%)

1 warning(s) in tests
[124/127] Running the AddressSanitizer tests with dynamic runtime
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\llvm\utils\lit\lit\llvm\config.py:57: note: using lit tools: C:\gnuwin32\bin
llvm-lit.py: C:\b\slave\sanitizer-windows\llvm-project\compiler-rt\test\lit.common.cfg.py:60: warning: Path reported by clang does not exist: "C:\b\slave\sanitizer-windows\build\stage1\lib\clang\20\lib\x86_64-pc-windows-msvc". This path was found by running ['C:/b/slave/sanitizer-windows/build/stage1/./bin/clang.exe', '--target=x86_64-pc-windows-msvc', '-print-runtime-dir'].
-- Testing: 540 tests, 16 workers --

command timed out: 1200 seconds without output running ['python', '../llvm-zorg/zorg/buildbot/builders/annotated/sanitizer-windows.py', '--jobs=16'], attempting to kill
program finished with exit code 1
elapsedTime=2067.439000
Testing:  0.. 10.. 20.. 30.. 40..

fmayer added a commit that referenced this pull request Nov 12, 2024
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…#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)
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
@minxuanz
Copy link
Contributor Author

minxuanz commented Dec 9, 2024

Build failure maybe due to missing link options -lrt and time.h

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants