Testing: add subprocess resource limits (M-TESTING-05)
This commit is contained in:
1 parent
80e6ed9afc
commit
404bf4ce2f
4 files changed
+71
No files matched your search
@@ -944,6 +944,14 @@ namespace Void::Testing
|
||||
std::this_thread::sleep_for(std::chrono::seconds{5});
|
||||
return 0;
|
||||
}
|
||||
if (config.selftest_child == "alloc") {
|
||||
// Touch a large buffer so an RLIMIT_AS cap is exercised.
|
||||
constexpr std::size_t kAllocBytes = 256ULL * 1024ULL * 1024ULL;
|
||||
std::vector<char> big(kAllocBytes, 'x');
|
||||
volatile char sink = big[kAllocBytes - 1];
|
||||
(void)sink;
|
||||
return 0;
|
||||
}
|
||||
if (config.selftest_child == "flood") {
|
||||
const std::string out_chunk(131072, 'O');
|
||||
const std::string err_chunk(131072, 'E');
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
#ifndef _WIN32
|
||||
#include <fcntl.h>
|
||||
#include <poll.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <unistd.h>
|
||||
@@ -156,6 +157,35 @@ ProcessTestResult run_isolated_subprocess(
|
||||
(void)::dup2(err_pipe[1], STDERR_FILENO);
|
||||
close_fd_safe(out_pipe[1]);
|
||||
close_fd_safe(err_pipe[1]);
|
||||
|
||||
const auto limit = [](int resource, std::uint64_t soft,
|
||||
std::uint64_t hard) {
|
||||
const struct rlimit value{static_cast<rlim_t>(soft),
|
||||
static_cast<rlim_t>(hard)};
|
||||
(void)::setrlimit(resource, &value);
|
||||
};
|
||||
const auto& res_limits = options.resources;
|
||||
if (res_limits.disable_core_dumps) {
|
||||
limit(RLIMIT_CORE, 0, 0);
|
||||
}
|
||||
if (res_limits.cpu_seconds) {
|
||||
// Soft limit delivers SIGXCPU; the hard limit is the backstop.
|
||||
limit(RLIMIT_CPU, *res_limits.cpu_seconds,
|
||||
*res_limits.cpu_seconds + 1);
|
||||
}
|
||||
if (res_limits.address_space_bytes) {
|
||||
limit(RLIMIT_AS, *res_limits.address_space_bytes,
|
||||
*res_limits.address_space_bytes);
|
||||
}
|
||||
if (res_limits.file_size_bytes) {
|
||||
limit(RLIMIT_FSIZE, *res_limits.file_size_bytes,
|
||||
*res_limits.file_size_bytes);
|
||||
}
|
||||
if (res_limits.open_files) {
|
||||
limit(RLIMIT_NOFILE, *res_limits.open_files,
|
||||
*res_limits.open_files);
|
||||
}
|
||||
|
||||
::execv(exe_str.c_str(), argv_ptrs.data());
|
||||
::_exit(127);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user