diff options
author | Philipp Wollermann <philwo@google.com> | 2016-09-23 09:59:26 +0000 |
---|---|---|
committer | Laszlo Csomor <laszlocsomor@google.com> | 2016-09-23 12:28:41 +0000 |
commit | f399a2144b9c01ff8d45dafd64bb59574157ea34 (patch) | |
tree | 53548d3f118c2f425de4deba60229ae7be1b9788 /src/main/java/com/google | |
parent | 33ad37612ab7edc32d5e82c3912acebacaef42dc (diff) |
Add a SandboxedSpawnActionContext interface and implementations for our sandboxed execution strategies.
--
MOS_MIGRATED_REVID=134054610
Diffstat (limited to 'src/main/java/com/google')
4 files changed, 98 insertions, 10 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/actions/SandboxedSpawnActionContext.java b/src/main/java/com/google/devtools/build/lib/actions/SandboxedSpawnActionContext.java new file mode 100644 index 0000000000..9d9cdb439e --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/actions/SandboxedSpawnActionContext.java @@ -0,0 +1,51 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.devtools.build.lib.actions; + +import java.util.concurrent.atomic.AtomicReference; + +/** + * A context that allows execution of {@link Spawn} instances similar to {@link SpawnActionContext}, + * but with the additional restriction, that during execution the {@link Spawn} must not be allowed + * to modify the current execution root of the build. Instead, the {@link Spawn} should be executed + * in a sandbox or on a remote system and its output files only be moved to the execution root, if + * the implementation is able to {@code compareAndSet} the {@link AtomicReference} that is passed to + * the {@link #exec} method to its own class object (e.g. LinuxSandboxedStrategy.class). + * + * <p>If the {@code compareAndSet} fails, the Spawn strategy should abandon the output of its + * execution and throw an {@link InterruptedException} from its {@code exec} method. + */ +public interface SandboxedSpawnActionContext extends SpawnActionContext { + + /** + * Executes the given spawn. + * + * <p>When the {@link SpawnActionContext} is about to move the output files of the spawn out of + * the sandbox into the execroot, it has to first verify that the {@link AtomicReference} is still + * null or already set to a value uniquely identifying the current {@link SpawnActionContext} + * (e.g. the class object of the strategy). This is to ensure that in case multiple {@link + * SandboxedSpawnActionContext} instances are processing the {@link Spawn} in parallel that only + * one strategy actually generates the output files. + * + * <p>If the {@link AtomicReference} is not null (thus {@code #compareAndSet} fails) and not set + * to the unique reference of the strategy, the {@link SandboxedSpawnActionContext} should abandon + * all results and raise {@link InterruptedException}. + */ + void exec( + Spawn spawn, + ActionExecutionContext actionExecutionContext, + AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) + throws ExecException, InterruptedException; +} diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java index 8363ffb36b..dac9e58924 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/DarwinSandboxedStrategy.java @@ -54,6 +54,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; /** Strategy that uses sandboxing to execute a process, for Darwin */ @ExecutionStrategy( @@ -147,7 +148,16 @@ public class DarwinSandboxedStrategy extends SandboxStrategy { @Override public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) - throws ExecException { + throws ExecException, InterruptedException { + exec(spawn, actionExecutionContext, null); + } + + @Override + public void exec( + Spawn spawn, + ActionExecutionContext actionExecutionContext, + AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) + throws ExecException, InterruptedException { Executor executor = actionExecutionContext.getExecutor(); // Certain actions can't run remotely or in a sandbox - pass them on to the standalone strategy. @@ -210,7 +220,12 @@ public class DarwinSandboxedStrategy extends SandboxStrategy { Spawns.getTimeoutSeconds(spawn), SandboxHelpers.shouldAllowNetwork(buildRequest, spawn)); } finally { - hardlinkedExecRoot.copyOutputs(execRoot, outputs); + if (writeOutputFiles != null + && !writeOutputFiles.compareAndSet(null, DarwinSandboxedStrategy.class)) { + Thread.currentThread().interrupt(); + } else { + hardlinkedExecRoot.copyOutputs(execRoot, outputs); + } if (!sandboxDebug) { SandboxHelpers.lazyCleanup(backgroundWorkers, runner); } @@ -218,6 +233,10 @@ public class DarwinSandboxedStrategy extends SandboxStrategy { } catch (IOException e) { throw new UserExecException("I/O error during sandboxed execution", e); } + + if (Thread.interrupted()) { + throw new InterruptedException(); + } } @Override diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java index 808da2fa22..d07943a469 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedStrategy.java @@ -34,6 +34,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ExecutorService; import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; /** Strategy that uses sandboxing to execute a process. */ @ExecutionStrategy( @@ -81,12 +82,19 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { this.fullySupported = fullySupported; } - /** - * Executes the given {@code spawn}. - */ + /** Executes the given {@code spawn}. */ @Override public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext) - throws ExecException { + throws ExecException, InterruptedException { + exec(spawn, actionExecutionContext, null); + } + + @Override + public void exec( + Spawn spawn, + ActionExecutionContext actionExecutionContext, + AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) + throws ExecException, InterruptedException { Executor executor = actionExecutionContext.getExecutor(); // Certain actions can't run remotely or in a sandbox - pass them on to the standalone strategy. @@ -129,6 +137,7 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { } else { runner = new ProcessWrapperRunner(execRoot, sandboxPath, sandboxExecRoot, verboseFailures); } + try { runner.run( spawn.getArguments(), @@ -137,7 +146,13 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { Spawns.getTimeoutSeconds(spawn), SandboxHelpers.shouldAllowNetwork(buildRequest, spawn)); } finally { - symlinkedExecRoot.copyOutputs(execRoot, outputs); + if (writeOutputFiles != null + && !writeOutputFiles.compareAndSet(null, LinuxSandboxedStrategy.class)) { + Thread.currentThread().interrupt(); + } else { + symlinkedExecRoot.copyOutputs(execRoot, outputs); + } + if (!sandboxOptions.sandboxDebug) { SandboxHelpers.lazyCleanup(backgroundWorkers, runner); } @@ -145,6 +160,10 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { } catch (IOException e) { throw new UserExecException("I/O error during sandboxed execution", e); } + + if (Thread.interrupted()) { + throw new InterruptedException(); + } } private ImmutableSet<Path> getBindMounts(BlazeDirectories blazeDirs) { @@ -159,5 +178,4 @@ public class LinuxSandboxedStrategy extends SandboxStrategy { } return bindMounts.build(); } - } diff --git a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java index 7a12e9b600..6b5c3ed64b 100644 --- a/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java +++ b/src/main/java/com/google/devtools/build/lib/sandbox/SandboxStrategy.java @@ -19,8 +19,8 @@ import com.google.common.collect.ImmutableSet.Builder; import com.google.devtools.build.lib.actions.ActionExecutionContext; import com.google.devtools.build.lib.actions.EnvironmentalExecException; import com.google.devtools.build.lib.actions.ExecException; +import com.google.devtools.build.lib.actions.SandboxedSpawnActionContext; import com.google.devtools.build.lib.actions.Spawn; -import com.google.devtools.build.lib.actions.SpawnActionContext; import com.google.devtools.build.lib.analysis.BlazeDirectories; import com.google.devtools.build.lib.vfs.Path; import com.google.devtools.build.lib.vfs.PathFragment; @@ -28,7 +28,7 @@ import java.io.IOException; import java.util.Map; /** Abstract common ancestor for sandbox strategies implementing the common parts. */ -abstract class SandboxStrategy implements SpawnActionContext { +abstract class SandboxStrategy implements SandboxedSpawnActionContext { private final BlazeDirectories blazeDirs; private final boolean verboseFailures; |