• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Implementation of libc death test executors -----------------------===//
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 #include "LibcTest.h"
10 
11 #include "test/UnitTest/ExecuteFunction.h"
12 #include "test/UnitTest/TestLogger.h"
13 
14 #include <cassert>
15 
16 namespace LIBC_NAMESPACE {
17 namespace testing {
18 
testProcessKilled(testutils::FunctionCaller * Func,int Signal,const char * LHSStr,const char * RHSStr,internal::Location Loc)19 bool Test::testProcessKilled(testutils::FunctionCaller *Func, int Signal,
20                              const char *LHSStr, const char *RHSStr,
21                              internal::Location Loc) {
22   testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 1000);
23 
24   if (const char *error = Result.get_error()) {
25     Ctx->markFail();
26     tlog << Loc;
27     tlog << error << '\n';
28     return false;
29   }
30 
31   if (Result.timed_out()) {
32     Ctx->markFail();
33     tlog << Loc;
34     tlog << "Process timed out after " << 1000 << " milliseconds.\n";
35     return false;
36   }
37 
38   if (Result.exited_normally()) {
39     Ctx->markFail();
40     tlog << Loc;
41     tlog << "Expected " << LHSStr
42          << " to be killed by a signal\nBut it exited normally!\n";
43     return false;
44   }
45 
46   int KilledBy = Result.get_fatal_signal();
47   assert(KilledBy != 0 && "Not killed by any signal");
48   if (Signal == -1 || KilledBy == Signal)
49     return true;
50 
51   using testutils::signal_as_string;
52   Ctx->markFail();
53   tlog << Loc;
54   tlog << "              Expected: " << LHSStr << '\n'
55        << "To be killed by signal: " << Signal << '\n'
56        << "              Which is: " << signal_as_string(Signal) << '\n'
57        << "  But it was killed by: " << KilledBy << '\n'
58        << "              Which is: " << signal_as_string(KilledBy) << '\n';
59   return false;
60 }
61 
testProcessExits(testutils::FunctionCaller * Func,int ExitCode,const char * LHSStr,const char * RHSStr,internal::Location Loc)62 bool Test::testProcessExits(testutils::FunctionCaller *Func, int ExitCode,
63                             const char *LHSStr, const char *RHSStr,
64                             internal::Location Loc) {
65   testutils::ProcessStatus Result = testutils::invoke_in_subprocess(Func, 1000);
66 
67   if (const char *error = Result.get_error()) {
68     Ctx->markFail();
69     tlog << Loc;
70     tlog << error << '\n';
71     return false;
72   }
73 
74   if (Result.timed_out()) {
75     Ctx->markFail();
76     tlog << Loc;
77     tlog << "Process timed out after " << 1000 << " milliseconds.\n";
78     return false;
79   }
80 
81   if (!Result.exited_normally()) {
82     Ctx->markFail();
83     tlog << Loc;
84     tlog << "Expected " << LHSStr << '\n'
85          << "to exit with exit code " << ExitCode << '\n'
86          << "But it exited abnormally!\n";
87     return false;
88   }
89 
90   int ActualExit = Result.get_exit_code();
91   if (ActualExit == ExitCode)
92     return true;
93 
94   Ctx->markFail();
95   tlog << Loc;
96   tlog << "Expected exit code of: " << LHSStr << '\n'
97        << "             Which is: " << ActualExit << '\n'
98        << "       To be equal to: " << RHSStr << '\n'
99        << "             Which is: " << ExitCode << '\n';
100   return false;
101 }
102 
103 } // namespace testing
104 } // namespace LIBC_NAMESPACE
105