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
@@ -2,6 +2,8 @@
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
@@ -9,9 +11,21 @@
|
||||
namespace Void::Testing
|
||||
{
|
||||
|
||||
/// Per-child resource limits applied before exec. Unset values are left at
|
||||
/// the parent's limits.
|
||||
struct ProcessResourceLimits {
|
||||
std::optional<std::uint64_t> cpu_seconds{};
|
||||
std::optional<std::uint64_t> address_space_bytes{};
|
||||
std::optional<std::uint64_t> file_size_bytes{};
|
||||
std::optional<std::uint64_t> open_files{};
|
||||
/// Prevent a crashing child from writing a core file.
|
||||
bool disable_core_dumps{true};
|
||||
};
|
||||
|
||||
struct ProcessTestOptions {
|
||||
std::chrono::milliseconds timeout{60000};
|
||||
std::size_t max_output_bytes{1024ULL * 1024ULL};
|
||||
ProcessResourceLimits resources{};
|
||||
};
|
||||
|
||||
enum class ProcessOutcome {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -287,4 +287,23 @@ Void::Testing::Test testing_subprocess_isolation_exit_crash_timeout_and_flood()
|
||||
expect_eq(res.stdout_captured.size(), std::size_t{4096});
|
||||
expect_eq(res.stderr_captured.size(), std::size_t{4096});
|
||||
}
|
||||
|
||||
// 7. Resource limits: a large allocation fails under a tight address-space
|
||||
// cap instead of succeeding.
|
||||
{
|
||||
const std::vector<std::string> args{"--selftest-child", "alloc"};
|
||||
const auto uncapped = run_isolated_subprocess(exe, args);
|
||||
expect(uncapped.outcome == ProcessOutcome::passed);
|
||||
|
||||
const auto capped = run_isolated_subprocess(
|
||||
exe, args,
|
||||
ProcessTestOptions{
|
||||
.timeout = std::chrono::milliseconds{5000},
|
||||
.max_output_bytes = 4096,
|
||||
.resources = ProcessResourceLimits{
|
||||
.address_space_bytes = 64ULL * 1024ULL * 1024ULL},
|
||||
});
|
||||
expect(capped.outcome == ProcessOutcome::crashed ||
|
||||
capped.outcome == ProcessOutcome::failed);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user