Internal change

PiperOrigin-RevId: 282945153
Change-Id: I26d4a9d21574fad2751708fe4bb9b38ecdd8131f
This commit is contained in:
Sandboxed API Team 2019-11-28 08:06:37 -08:00 committed by Copybara-Service
parent e7eb1f97a3
commit 44443779bc
3 changed files with 16 additions and 6 deletions

View File

@ -21,6 +21,7 @@
#include <string> #include <string>
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/time/time.h"
#include "sandboxed_api/sandbox2/monitor.h" #include "sandboxed_api/sandbox2/monitor.h"
#include "sandboxed_api/sandbox2/result.h" #include "sandboxed_api/sandbox2/result.h"
#include "sandboxed_api/util/canonical_errors.h" #include "sandboxed_api/util/canonical_errors.h"
@ -99,14 +100,16 @@ bool Sandbox2::IsTerminated() const {
} }
void Sandbox2::SetWallTimeLimit(time_t limit) const { void Sandbox2::SetWallTimeLimit(time_t limit) const {
CHECK(monitor_ != nullptr) << "Sandbox was not launched yet"; set_walltime_limit(absl::Seconds(limit));
}
if (limit == 0) { void Sandbox2::set_walltime_limit(absl::Duration limit) const {
if (limit == absl::ZeroDuration()) {
VLOG(1) << "Disarming walltime timer to "; VLOG(1) << "Disarming walltime timer to ";
monitor_->deadline_millis_.store(0, std::memory_order_relaxed); monitor_->deadline_millis_.store(0, std::memory_order_relaxed);
} else { } else {
VLOG(1) << "Will set the walltime timer to " << limit << " seconds"; VLOG(1) << "Will set the walltime timer to " << limit;
auto deadline = absl::Now() + absl::Seconds(limit); absl::Time deadline = absl::Now() + limit;
monitor_->deadline_millis_.store(absl::ToUnixMillis(deadline), monitor_->deadline_millis_.store(absl::ToUnixMillis(deadline),
std::memory_order_relaxed); std::memory_order_relaxed);
} }

View File

@ -94,8 +94,15 @@ class Sandbox2 final {
// This can be useful in a persistent sandbox scenario, to impose a deadline // This can be useful in a persistent sandbox scenario, to impose a deadline
// for responses after each request and reset the deadline in between. // for responses after each request and reset the deadline in between.
// Sandboxed API can be used to implement persistent sandboxes. // Sandboxed API can be used to implement persistent sandboxes.
ABSL_DEPRECATED("Use set_walltime_limit() instead")
void SetWallTimeLimit(time_t limit) const; void SetWallTimeLimit(time_t limit) const;
// Sets a wall time limit on a running sandboxee, absl::ZeroDuration() to
// disarm. This can be useful in a persistent sandbox scenario, to impose a
// deadline for responses after each request and reset the deadline in
// between. Sandboxed API can be used to implement persistent sandboxes.
void set_walltime_limit(absl::Duration limit) const;
// Gets the pid inside the executor. // Gets the pid inside the executor.
pid_t GetPid() { pid_t GetPid() {
if (monitor_ != nullptr) { if (monitor_ != nullptr) {

View File

@ -150,7 +150,7 @@ TEST(RunAsyncTest, SandboxeeTimeoutWithStacktraces) {
.TryBuild()); .TryBuild());
Sandbox2 sandbox(std::move(executor), std::move(policy)); Sandbox2 sandbox(std::move(executor), std::move(policy));
ASSERT_TRUE(sandbox.RunAsync()); ASSERT_TRUE(sandbox.RunAsync());
sandbox.SetWallTimeLimit(1); sandbox.set_walltime_limit(absl::Seconds(1));
auto result = sandbox.AwaitResult(); auto result = sandbox.AwaitResult();
EXPECT_EQ(result.final_status(), Result::TIMEOUT); EXPECT_EQ(result.final_status(), Result::TIMEOUT);
EXPECT_THAT(result.GetStackTrace(), HasSubstr("sleep")); EXPECT_THAT(result.GetStackTrace(), HasSubstr("sleep"));
@ -171,7 +171,7 @@ TEST(RunAsyncTest, SandboxeeTimeoutDisabledStacktraces) {
.TryBuild()); .TryBuild());
Sandbox2 sandbox(std::move(executor), std::move(policy)); Sandbox2 sandbox(std::move(executor), std::move(policy));
ASSERT_TRUE(sandbox.RunAsync()); ASSERT_TRUE(sandbox.RunAsync());
sandbox.SetWallTimeLimit(1); sandbox.set_walltime_limit(absl::Seconds(1));
auto result = sandbox.AwaitResult(); auto result = sandbox.AwaitResult();
EXPECT_EQ(result.final_status(), Result::TIMEOUT); EXPECT_EQ(result.final_status(), Result::TIMEOUT);
EXPECT_THAT(result.GetStackTrace(), IsEmpty()); EXPECT_THAT(result.GetStackTrace(), IsEmpty());