xref: /llvm-project/compiler-rt/lib/fuzzer/FuzzerUtilLinux.cpp (revision e371ada409b225ea990b5ac0d5cafea26a6046e1)
110ab2aceSGeorge Karpenkov //===- FuzzerUtilLinux.cpp - Misc utils for Linux. ------------------------===//
210ab2aceSGeorge Karpenkov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
610ab2aceSGeorge Karpenkov //
710ab2aceSGeorge Karpenkov //===----------------------------------------------------------------------===//
810ab2aceSGeorge Karpenkov // Misc utils for Linux.
910ab2aceSGeorge Karpenkov //===----------------------------------------------------------------------===//
10226866e1SDokyung Song #include "FuzzerPlatform.h"
119802089eSVitaly Buka #if LIBFUZZER_LINUX || LIBFUZZER_NETBSD || LIBFUZZER_FREEBSD ||                \
1253065c54SDavid Carlier     LIBFUZZER_EMSCRIPTEN
1304304d12SMatt Morehouse #include "FuzzerCommand.h"
14aa0e9046SMaxim Schessler #include "FuzzerInternal.h"
1510ab2aceSGeorge Karpenkov 
1698c2754aSKrzysztof Parzyszek #include <signal.h>
1710ab2aceSGeorge Karpenkov #include <stdlib.h>
1863f48717SKostya Serebryany #include <sys/types.h>
1963f48717SKostya Serebryany #include <sys/wait.h>
20e5b603a4SMarco Vanotti #include <unistd.h>
2163f48717SKostya Serebryany 
2210ab2aceSGeorge Karpenkov 
2310ab2aceSGeorge Karpenkov namespace fuzzer {
2410ab2aceSGeorge Karpenkov 
ExecuteCommand(const Command & Cmd)2504304d12SMatt Morehouse int ExecuteCommand(const Command &Cmd) {
2604304d12SMatt Morehouse   std::string CmdLine = Cmd.toString();
2763f48717SKostya Serebryany   int exit_code = system(CmdLine.c_str());
2863f48717SKostya Serebryany   if (WIFEXITED(exit_code))
2963f48717SKostya Serebryany     return WEXITSTATUS(exit_code);
30aa0e9046SMaxim Schessler   if (WIFSIGNALED(exit_code) && WTERMSIG(exit_code) == SIGINT)
31aa0e9046SMaxim Schessler     return Fuzzer::InterruptExitCode();
3263f48717SKostya Serebryany   return exit_code;
3310ab2aceSGeorge Karpenkov }
3410ab2aceSGeorge Karpenkov 
DiscardOutput(int Fd)35e5b603a4SMarco Vanotti void DiscardOutput(int Fd) {
36e5b603a4SMarco Vanotti   FILE* Temp = fopen("/dev/null", "w");
37e5b603a4SMarco Vanotti   if (!Temp)
38e5b603a4SMarco Vanotti     return;
39e5b603a4SMarco Vanotti   dup2(fileno(Temp), Fd);
40e5b603a4SMarco Vanotti   fclose(Temp);
41e5b603a4SMarco Vanotti }
42e5b603a4SMarco Vanotti 
SetThreadName(std::thread & thread,const std::string & name)43b2a25385SDavid CARLIER void SetThreadName(std::thread &thread, const std::string &name) {
44b2a25385SDavid CARLIER #if LIBFUZZER_LINUX || LIBFUZZER_FREEBSD
45b2a25385SDavid CARLIER   (void)pthread_setname_np(thread.native_handle(), name.c_str());
46b2a25385SDavid CARLIER #elif LIBFUZZER_NETBSD
47*e371ada4SDavid CARLIER   (void)pthread_setname_np(thread.native_handle(), "%s", const_cast<char *>(name.c_str()));
48b2a25385SDavid CARLIER #endif
49b2a25385SDavid CARLIER }
50b2a25385SDavid CARLIER 
5110ab2aceSGeorge Karpenkov } // namespace fuzzer
5210ab2aceSGeorge Karpenkov 
539802089eSVitaly Buka #endif
54