• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
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 TEST_SUPPORT_CHECK_ASSERTION_H
10 #define TEST_SUPPORT_CHECK_ASSERTION_H
11 
12 #include <cassert>
13 #include <cstdarg>
14 #include <cstddef>
15 #include <cstdio>
16 #include <cstdlib>
17 #include <exception>
18 #include <string>
19 #include <string_view>
20 #include <utility>
21 
22 #include <unistd.h>
23 #include <errno.h>
24 #include <sys/wait.h>
25 #include "test_macros.h"
26 #include "test_allocator.h"
27 
28 #if TEST_STD_VER < 11
29 # error "C++11 or greater is required to use this header"
30 #endif
31 
32 struct AssertionInfoMatcher {
33   static const int any_line = -1;
34   static constexpr const char* any_file = "*";
35   static constexpr const char* any_msg = "*";
36 
AssertionInfoMatcherAssertionInfoMatcher37   constexpr AssertionInfoMatcher() : is_empty_(true), msg_(any_msg, __builtin_strlen(any_msg)), file_(any_file, __builtin_strlen(any_file)), line_(any_line) { }
38   constexpr AssertionInfoMatcher(const char* msg, const char* file = any_file, int line = any_line)
is_empty_AssertionInfoMatcher39     : is_empty_(false), msg_(msg, __builtin_strlen(msg)), file_(file, __builtin_strlen(file)), line_(line) {}
40 
MatchesAssertionInfoMatcher41   bool Matches(char const* file, int line, char const* message) const {
42     assert(!empty() && "empty matcher");
43 
44     if (CheckLineMatches(line) && CheckFileMatches(file) && CheckMessageMatches(message))
45         return true;
46     // Write to stdout because that's the file descriptor captured by the parent
47     // process.
48     std::printf("Failed to match assertion info!\n%s\nVS\n%s:%d (%s)\n", ToString().data(), file, line, message);
49     return false;
50   }
51 
ToStringAssertionInfoMatcher52   std::string ToString() const {
53     std::string result = "msg = \""; result += msg_; result += "\"\n";
54     result += "line = " + (line_ == any_line ? "'*'" : std::to_string(line_)) + "\n";
55     result += "file = " + (file_ == any_file ? "'*'" : std::string(file_));
56     return result;
57   }
58 
emptyAssertionInfoMatcher59   bool empty() const { return is_empty_; }
60 private:
CheckLineMatchesAssertionInfoMatcher61   bool CheckLineMatches(int got_line) const {
62     if (line_ == any_line)
63       return true;
64     return got_line == line_;
65   }
66 
CheckFileMatchesAssertionInfoMatcher67   bool CheckFileMatches(std::string_view got_file) const {
68     assert(!empty() && "empty matcher");
69     if (file_ == any_file)
70       return true;
71     std::size_t found_at = got_file.find(file_);
72     if (found_at == std::string_view::npos)
73       return false;
74     // require the match start at the beginning of the file or immediately after
75     // a directory separator.
76     if (found_at != 0) {
77       char last_char = got_file[found_at - 1];
78       if (last_char != '/' && last_char != '\\')
79         return false;
80     }
81     // require the match goes until the end of the string.
82     return got_file.substr(found_at) == file_;
83   }
84 
CheckMessageMatchesAssertionInfoMatcher85   bool CheckMessageMatches(std::string_view got_msg) const {
86     assert(!empty() && "empty matcher");
87     if (msg_ == any_msg)
88       return true;
89     std::size_t found_at = got_msg.find(msg_);
90     if (found_at == std::string_view::npos)
91       return false;
92     // Allow any match
93     return true;
94   }
95 private:
96   bool is_empty_;
97   std::string_view msg_;
98   std::string_view file_;
99   int line_;
100 };
101 
102 static constexpr AssertionInfoMatcher AnyMatcher(AssertionInfoMatcher::any_msg);
103 
GlobalMatcher()104 inline AssertionInfoMatcher& GlobalMatcher() {
105   static AssertionInfoMatcher GMatch;
106   return GMatch;
107 }
108 
109 struct DeathTest {
110   enum ResultKind {
111     RK_DidNotDie, RK_MatchFound, RK_MatchFailure, RK_Terminate, RK_SetupFailure, RK_Unknown
112   };
113 
ResultKindToStringDeathTest114   static const char* ResultKindToString(ResultKind RK) {
115 #define CASE(K) case K: return #K
116     switch (RK) {
117     CASE(RK_MatchFailure);
118     CASE(RK_DidNotDie);
119     CASE(RK_SetupFailure);
120     CASE(RK_MatchFound);
121     CASE(RK_Unknown);
122     CASE(RK_Terminate);
123     }
124     return "not a result kind";
125   }
126 
IsValidResultKindDeathTest127   static bool IsValidResultKind(int val) {
128     return val >= RK_DidNotDie && val <= RK_Unknown;
129   }
130 
DeathTestDeathTest131   DeathTest(AssertionInfoMatcher const& Matcher) : matcher_(Matcher) {}
132 
133   template <class Func>
RunDeathTest134   ResultKind Run(Func&& f) {
135     int pipe_res = pipe(stdout_pipe_fd_);
136     assert(pipe_res != -1 && "failed to create pipe");
137     pipe_res = pipe(stderr_pipe_fd_);
138     assert(pipe_res != -1 && "failed to create pipe");
139     pid_t child_pid = fork();
140     assert(child_pid != -1 &&
141         "failed to fork a process to perform a death test");
142     child_pid_ = child_pid;
143     if (child_pid_ == 0) {
144       RunForChild(std::forward<Func>(f));
145       assert(false && "unreachable");
146     }
147     return RunForParent();
148   }
149 
getChildExitCodeDeathTest150   int getChildExitCode() const { return exit_code_; }
getChildStdOutDeathTest151   std::string const& getChildStdOut() const { return stdout_from_child_; }
getChildStdErrDeathTest152   std::string const& getChildStdErr() const { return stderr_from_child_; }
153 private:
154   template <class Func>
RunForChildDeathTest155   TEST_NORETURN void RunForChild(Func&& f) {
156     close(GetStdOutReadFD()); // don't need to read from the pipe in the child.
157     close(GetStdErrReadFD());
158     auto DupFD = [](int DestFD, int TargetFD) {
159       int dup_result = dup2(DestFD, TargetFD);
160       if (dup_result == -1)
161         std::exit(RK_SetupFailure);
162     };
163     DupFD(GetStdOutWriteFD(), STDOUT_FILENO);
164     DupFD(GetStdErrWriteFD(), STDERR_FILENO);
165 
166     GlobalMatcher() = matcher_;
167     f();
168     std::exit(RK_DidNotDie);
169   }
170 
ReadChildIOUntilEndDeathTest171   static std::string ReadChildIOUntilEnd(int FD) {
172     std::string error_msg;
173     char buffer[256];
174     int num_read;
175     do {
176       while ((num_read = read(FD, buffer, 255)) > 0) {
177         buffer[num_read] = '\0';
178         error_msg += buffer;
179       }
180     } while (num_read == -1 && errno == EINTR);
181     return error_msg;
182   }
183 
CaptureIOFromChildDeathTest184   void CaptureIOFromChild() {
185     close(GetStdOutWriteFD()); // no need to write from the parent process
186     close(GetStdErrWriteFD());
187     stdout_from_child_ = ReadChildIOUntilEnd(GetStdOutReadFD());
188     stderr_from_child_ = ReadChildIOUntilEnd(GetStdErrReadFD());
189     close(GetStdOutReadFD());
190     close(GetStdErrReadFD());
191   }
192 
RunForParentDeathTest193   ResultKind RunForParent() {
194     CaptureIOFromChild();
195 
196     int status_value;
197     pid_t result = waitpid(child_pid_, &status_value, 0);
198     assert(result != -1 && "there is no child process to wait for");
199 
200     if (WIFEXITED(status_value)) {
201       exit_code_ = WEXITSTATUS(status_value);
202       if (!IsValidResultKind(exit_code_))
203         return RK_Unknown;
204       return static_cast<ResultKind>(exit_code_);
205     }
206     return RK_Unknown;
207   }
208 
209   DeathTest(DeathTest const&) = delete;
210   DeathTest& operator=(DeathTest const&) = delete;
211 
GetStdOutReadFDDeathTest212   int GetStdOutReadFD() const {
213     return stdout_pipe_fd_[0];
214   }
215 
GetStdOutWriteFDDeathTest216   int GetStdOutWriteFD() const {
217     return stdout_pipe_fd_[1];
218   }
219 
GetStdErrReadFDDeathTest220   int GetStdErrReadFD() const {
221     return stderr_pipe_fd_[0];
222   }
223 
GetStdErrWriteFDDeathTest224   int GetStdErrWriteFD() const {
225     return stderr_pipe_fd_[1];
226   }
227 private:
228   AssertionInfoMatcher matcher_;
229   pid_t child_pid_ = -1;
230   int exit_code_ = -1;
231   int stdout_pipe_fd_[2];
232   int stderr_pipe_fd_[2];
233   std::string stdout_from_child_;
234   std::string stderr_from_child_;
235 };
236 
237 #ifdef _LIBCPP_VERSION
__libcpp_verbose_abort(char const * format,...)238 void std::__libcpp_verbose_abort(char const* format, ...) {
239   // Extract information from the error message. This has to stay synchronized with
240   // how we format assertions in the library.
241   va_list list;
242   va_start(list, format);
243   char const* file = va_arg(list, char const*);
244   int line = va_arg(list, int);
245   char const* expression = va_arg(list, char const*); (void)expression;
246   char const* message = va_arg(list, char const*);
247   va_end(list);
248 
249   if (GlobalMatcher().Matches(file, line, message)) {
250     std::exit(DeathTest::RK_MatchFound);
251   }
252   std::exit(DeathTest::RK_MatchFailure);
253 }
254 #endif // _LIBCPP_VERSION
255 
terminate_handler()256 [[noreturn]] inline void terminate_handler() {
257   std::exit(DeathTest::RK_Terminate);
258 }
259 
260 template <class Func>
ExpectDeath(const char * stmt,Func && func,AssertionInfoMatcher Matcher)261 inline bool ExpectDeath(const char* stmt, Func&& func, AssertionInfoMatcher Matcher) {
262   std::set_terminate(terminate_handler);
263   DeathTest DT(Matcher);
264   DeathTest::ResultKind RK = DT.Run(func);
265   auto OnFailure = [&](const char* msg) {
266     std::fprintf(stderr, "EXPECT_DEATH( %s ) failed! (%s)\n\n", stmt, msg);
267     if (RK != DeathTest::RK_Unknown) {
268       std::fprintf(stderr, "child exit code: %d\n", DT.getChildExitCode());
269     }
270     if (!DT.getChildStdErr().empty()) {
271       std::fprintf(stderr, "---------- standard err ----------\n%s\n", DT.getChildStdErr().c_str());
272     }
273     if (!DT.getChildStdOut().empty()) {
274       std::fprintf(stderr, "---------- standard out ----------\n%s\n", DT.getChildStdOut().c_str());
275     }
276     return false;
277   };
278   switch (RK) {
279   case DeathTest::RK_MatchFound:
280   case DeathTest::RK_Terminate:
281     return true;
282   case DeathTest::RK_SetupFailure:
283     return OnFailure("child failed to setup test environment");
284   case DeathTest::RK_Unknown:
285       return OnFailure("reason unknown");
286   case DeathTest::RK_DidNotDie:
287       return OnFailure("child did not die");
288   case DeathTest::RK_MatchFailure:
289       return OnFailure("matcher failed");
290   }
291   assert(false && "unreachable");
292 }
293 
294 template <class Func>
ExpectDeath(const char * stmt,Func && func)295 inline bool ExpectDeath(const char* stmt, Func&& func) {
296   return ExpectDeath(stmt, func, AnyMatcher);
297 }
298 
299 /// Assert that the specified expression throws a libc++ debug exception.
300 #define EXPECT_DEATH(...) assert((ExpectDeath(#__VA_ARGS__, [&]() { __VA_ARGS__; } )))
301 
302 #define EXPECT_STD_TERMINATE(...) assert(ExpectDeath(#__VA_ARGS__, __VA_ARGS__))
303 
304 #define EXPECT_DEATH_MATCHES(Matcher, ...) assert((ExpectDeath(#__VA_ARGS__, [&]() { __VA_ARGS__; }, Matcher)))
305 
306 #define TEST_LIBCPP_ASSERT_FAILURE(expr, message) assert((ExpectDeath(#expr, [&]() { (void)(expr); }, AssertionInfoMatcher(message))))
307 
308 #endif // TEST_SUPPORT_CHECK_ASSERTION_H
309