1 //===-- ExecuteFunction.h ---------------------------------------*- C++ -*-===//
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 
9 #ifndef LLVM_LIBC_UTILS_TESTUTILS_EXECUTEFUNCTION_H
10 #define LLVM_LIBC_UTILS_TESTUTILS_EXECUTEFUNCTION_H
11 
12 #include <stdint.h>
13 
14 namespace __llvm_libc {
15 namespace testutils {
16 
17 class FunctionCaller {
18 public:
~FunctionCaller()19   virtual ~FunctionCaller() {}
20   virtual void operator()() = 0;
21 };
22 
23 struct ProcessStatus {
24   int platform_defined;
25   const char *failure = nullptr;
26 
27   static constexpr unsigned TIMEOUT = ~0U;
28 
errorProcessStatus29   static ProcessStatus error(const char *error) { return {0, error}; }
timed_out_psProcessStatus30   static ProcessStatus timed_out_ps() {
31     return {0, reinterpret_cast<const char *>(TIMEOUT)};
32   }
33 
timed_outProcessStatus34   bool timed_out() const {
35     return failure == reinterpret_cast<const char *>(TIMEOUT);
36   }
get_errorProcessStatus37   const char *get_error() const { return timed_out() ? nullptr : failure; }
38   bool exited_normally() const;
39   int get_exit_code() const;
40   int get_fatal_signal() const;
41 };
42 
43 ProcessStatus invoke_in_subprocess(FunctionCaller *func,
44                                    unsigned timeout_ms = ProcessStatus::TIMEOUT);
45 
46 const char *signal_as_string(int signum);
47 
48 } // namespace testutils
49 } // namespace __llvm_libc
50 
51 #endif // LLVM_LIBC_UTILS_TESTUTILS_EXECUTEFUNCTION_H
52