FuzzerUtilLinux.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. //===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // Misc utils for Linux.
  9. //===----------------------------------------------------------------------===//
  10. #include "FuzzerPlatform.h"
  11. #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD || \
  12. LIBFUZZER_EMSCRIPTEN
  13. #include "FuzzerCommand.h"
  14. #include "FuzzerInternal.h"
  15. #include <signal.h>
  16. #include <stdlib.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include <unistd.h>
  20. namespace fuzzer {
  21. int ExecuteCommand(const Command &Cmd) {
  22. std::string CmdLine = Cmd.toString();
  23. int exit_code = system(CmdLine.c_str());
  24. if (WIFEXITED(exit_code))
  25. return WEXITSTATUS(exit_code);
  26. if (WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGINT)
  27. return Fuzzer::InterruptExitCode();
  28. return exit_code;
  29. }
  30. void DiscardOutput(int Fd) {
  31. FILE* Temp = fopen("/dev/null", "w");
  32. if (!Temp)
  33. return;
  34. dup2(fileno(Temp), Fd);
  35. fclose(Temp);
  36. }
  37. void SetThreadName(std::thread &thread, const std::string &name) {
  38. #if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
  39. (void)pthread_setname_np(thread.native_handle(), name.c_str());
  40. #elif LIBFUZZER_NETBSD
  41. (void)pthread_setname_np(thread.native_handle(), "%s", const_cast<char *>(name.c_str()));
  42. #endif
  43. }
  44. } // namespace fuzzer
  45. #endif