2020-04-08 10:16:30 -07:00
|
|
|
//===-- ExecuteFunction implementation for Unix-like Systems --------------===//
|
2020-02-24 17:53:43 -05:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
#include "ExecuteFunction.h"
|
2024-07-12 09:28:41 -07:00
|
|
|
#include "src/__support/macros/config.h"
|
2025-01-09 13:40:54 -08:00
|
|
|
#include "test/UnitTest/ExecuteFunction.h" // FunctionCaller
|
|
|
|
|
#include <assert.h>
|
2020-03-11 23:57:20 -04:00
|
|
|
#include <poll.h>
|
2020-02-24 17:53:43 -05:00
|
|
|
#include <signal.h>
|
2025-01-09 13:40:54 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2020-02-24 17:53:43 -05:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2024-07-12 09:28:41 -07:00
|
|
|
namespace LIBC_NAMESPACE_DECL {
|
2020-02-24 17:53:43 -05:00
|
|
|
namespace testutils {
|
|
|
|
|
|
2023-01-25 16:49:10 +00:00
|
|
|
bool ProcessStatus::exited_normally() { return WIFEXITED(platform_defined); }
|
2020-02-24 17:53:43 -05:00
|
|
|
|
2023-01-25 16:49:10 +00:00
|
|
|
int ProcessStatus::get_exit_code() {
|
2021-12-21 13:12:45 -08:00
|
|
|
assert(exited_normally() && "Abnormal termination, no exit code");
|
|
|
|
|
return WEXITSTATUS(platform_defined);
|
2020-02-24 17:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2023-01-25 16:49:10 +00:00
|
|
|
int ProcessStatus::get_fatal_signal() {
|
2021-12-21 13:12:45 -08:00
|
|
|
if (exited_normally())
|
2020-02-24 17:53:43 -05:00
|
|
|
return 0;
|
2021-12-21 13:12:45 -08:00
|
|
|
return WTERMSIG(platform_defined);
|
2020-02-24 17:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2025-03-10 11:57:09 -04:00
|
|
|
ProcessStatus invoke_in_subprocess(FunctionCaller *func, int timeout_ms) {
|
2022-03-16 03:34:54 +00:00
|
|
|
int pipe_fds[2];
|
2025-01-09 14:15:31 -08:00
|
|
|
if (::pipe(pipe_fds) == -1) {
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2021-12-21 13:12:45 -08:00
|
|
|
return ProcessStatus::error("pipe(2) failed");
|
2025-01-09 14:15:31 -08:00
|
|
|
}
|
2020-03-11 23:57:20 -04:00
|
|
|
|
2020-02-24 17:53:43 -05:00
|
|
|
// Don't copy the buffers into the child process and print twice.
|
2025-01-09 13:40:54 -08:00
|
|
|
::fflush(stderr);
|
|
|
|
|
::fflush(stdout);
|
2022-03-16 03:34:54 +00:00
|
|
|
pid_t pid = ::fork();
|
2025-01-09 14:15:31 -08:00
|
|
|
if (pid == -1) {
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2021-12-21 13:12:45 -08:00
|
|
|
return ProcessStatus::error("fork(2) failed");
|
2025-01-09 14:15:31 -08:00
|
|
|
}
|
2020-03-11 23:57:20 -04:00
|
|
|
|
2022-03-16 03:34:54 +00:00
|
|
|
if (!pid) {
|
|
|
|
|
(*func)();
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2025-01-09 13:40:54 -08:00
|
|
|
::exit(0);
|
2020-02-24 17:53:43 -05:00
|
|
|
}
|
2022-03-16 03:34:54 +00:00
|
|
|
::close(pipe_fds[1]);
|
2020-03-11 23:57:20 -04:00
|
|
|
|
2025-11-28 19:32:29 +05:30
|
|
|
pollfd poll_fd{pipe_fds[0], POLLIN, 0};
|
2020-03-11 23:57:20 -04:00
|
|
|
// No events requested so this call will only return after the timeout or if
|
|
|
|
|
// the pipes peer was closed, signaling the process exited.
|
2025-01-09 14:15:31 -08:00
|
|
|
if (::poll(&poll_fd, 1, timeout_ms) == -1) {
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2021-12-21 13:12:45 -08:00
|
|
|
return ProcessStatus::error("poll(2) failed");
|
2025-01-09 14:15:31 -08:00
|
|
|
}
|
2020-03-11 23:57:20 -04:00
|
|
|
// If the pipe wasn't closed by the child yet then timeout has expired.
|
2022-03-16 03:34:54 +00:00
|
|
|
if (!(poll_fd.revents & POLLHUP)) {
|
|
|
|
|
::kill(pid, SIGKILL);
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2021-12-21 13:12:45 -08:00
|
|
|
return ProcessStatus::timed_out_ps();
|
2020-03-11 23:57:20 -04:00
|
|
|
}
|
2020-02-24 17:53:43 -05:00
|
|
|
|
2022-03-16 03:34:54 +00:00
|
|
|
int wstatus = 0;
|
2020-03-17 12:59:21 -07:00
|
|
|
// Wait on the pid of the subprocess here so it gets collected by the system
|
|
|
|
|
// and doesn't turn into a zombie.
|
2022-03-16 03:34:54 +00:00
|
|
|
pid_t status = ::waitpid(pid, &wstatus, 0);
|
2025-01-09 14:15:31 -08:00
|
|
|
if (status == -1) {
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2021-12-21 13:12:45 -08:00
|
|
|
return ProcessStatus::error("waitpid(2) failed");
|
2025-01-09 14:15:31 -08:00
|
|
|
}
|
2022-03-16 03:34:54 +00:00
|
|
|
assert(status == pid);
|
2025-01-09 14:46:52 -08:00
|
|
|
delete func;
|
2022-03-16 03:34:54 +00:00
|
|
|
return {wstatus};
|
2020-02-24 17:53:43 -05:00
|
|
|
}
|
|
|
|
|
|
2022-03-16 03:34:54 +00:00
|
|
|
const char *signal_as_string(int signum) { return ::strsignal(signum); }
|
2020-02-24 17:53:43 -05:00
|
|
|
|
|
|
|
|
} // namespace testutils
|
2024-07-12 09:28:41 -07:00
|
|
|
} // namespace LIBC_NAMESPACE_DECL
|