Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.sandbox; |
| 16 | |
| 17 | import com.google.common.collect.ImmutableSet; |
| 18 | import com.google.common.collect.ImmutableSet.Builder; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.actions.EnvironmentalExecException; |
| 21 | import com.google.devtools.build.lib.actions.ExecException; |
Philipp Wollermann | f399a21 | 2016-09-23 09:59:26 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.SandboxedSpawnActionContext; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.Spawn; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.SpawnActionContext; |
| 25 | import com.google.devtools.build.lib.actions.Spawns; |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.UserExecException; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.buildtool.BuildRequest; |
| 29 | import com.google.devtools.build.lib.events.Event; |
Philipp Wollermann | a7827c8 | 2016-09-26 16:21:22 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.events.EventHandler; |
Yue Gan | 400f82e | 2017-01-12 05:08:02 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.util.io.OutErr; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.vfs.Path; |
| 33 | import com.google.devtools.build.lib.vfs.PathFragment; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 34 | import java.io.IOException; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 35 | import java.util.Map; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 36 | import java.util.Set; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 37 | import java.util.concurrent.atomic.AtomicReference; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 38 | |
| 39 | /** Abstract common ancestor for sandbox strategies implementing the common parts. */ |
Philipp Wollermann | f399a21 | 2016-09-23 09:59:26 +0000 | [diff] [blame] | 40 | abstract class SandboxStrategy implements SandboxedSpawnActionContext { |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 41 | |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 42 | private final BuildRequest buildRequest; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 43 | private final BlazeDirectories blazeDirs; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 44 | private final Path execRoot; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 45 | private final boolean verboseFailures; |
| 46 | private final SandboxOptions sandboxOptions; |
Philipp Wollermann | 809df53 | 2016-09-08 12:53:39 +0000 | [diff] [blame] | 47 | private final SpawnHelpers spawnHelpers; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 48 | |
| 49 | public SandboxStrategy( |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 50 | BuildRequest buildRequest, |
| 51 | BlazeDirectories blazeDirs, |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 52 | boolean verboseFailures, |
| 53 | SandboxOptions sandboxOptions) { |
| 54 | this.buildRequest = buildRequest; |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 55 | this.blazeDirs = blazeDirs; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 56 | this.execRoot = blazeDirs.getExecRoot(); |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 57 | this.verboseFailures = verboseFailures; |
| 58 | this.sandboxOptions = sandboxOptions; |
Philipp Wollermann | 809df53 | 2016-09-08 12:53:39 +0000 | [diff] [blame] | 59 | this.spawnHelpers = new SpawnHelpers(blazeDirs.getExecRoot()); |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 62 | protected void runSpawn( |
| 63 | Spawn spawn, |
| 64 | ActionExecutionContext actionExecutionContext, |
| 65 | Map<String, String> spawnEnvironment, |
| 66 | SandboxExecRoot sandboxExecRoot, |
| 67 | Set<PathFragment> outputs, |
| 68 | SandboxRunner runner, |
| 69 | AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) |
| 70 | throws ExecException, InterruptedException { |
Philipp Wollermann | a7827c8 | 2016-09-26 16:21:22 +0000 | [diff] [blame] | 71 | EventHandler eventHandler = actionExecutionContext.getExecutor().getEventHandler(); |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 72 | ExecException execException = null; |
Yue Gan | 400f82e | 2017-01-12 05:08:02 +0000 | [diff] [blame] | 73 | OutErr outErr = actionExecutionContext.getFileOutErr(); |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 74 | try { |
| 75 | runner.run( |
| 76 | spawn.getArguments(), |
| 77 | spawnEnvironment, |
Yue Gan | 400f82e | 2017-01-12 05:08:02 +0000 | [diff] [blame] | 78 | outErr, |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 79 | Spawns.getTimeoutSeconds(spawn), |
Yue Gan | 400f82e | 2017-01-12 05:08:02 +0000 | [diff] [blame] | 80 | SandboxHelpers.shouldAllowNetwork(buildRequest, spawn), |
Philipp Wollermann | 28d9bd3 | 2017-02-01 16:05:02 +0000 | [diff] [blame] | 81 | sandboxOptions.sandboxDebug, |
| 82 | sandboxOptions.sandboxFakeHostname); |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 83 | } catch (ExecException e) { |
| 84 | execException = e; |
| 85 | } |
| 86 | |
| 87 | if (writeOutputFiles != null && !writeOutputFiles.compareAndSet(null, SandboxStrategy.class)) { |
| 88 | throw new InterruptedException(); |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | // We copy the outputs even when the command failed, otherwise StandaloneTestStrategy |
| 93 | // won't be able to get the test logs of a failed test. (We should probably do this in |
| 94 | // some better way.) |
| 95 | sandboxExecRoot.copyOutputs(execRoot, outputs); |
| 96 | } catch (IOException e) { |
| 97 | if (execException == null) { |
| 98 | throw new UserExecException("Could not move output artifacts from sandboxed execution", e); |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 99 | } else { |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 100 | // Catch the IOException and turn it into an error message, otherwise this might hide an |
| 101 | // exception thrown during runner.run earlier. |
| 102 | eventHandler.handle( |
| 103 | Event.error( |
| 104 | "I/O exception while extracting output artifacts from sandboxed execution: " + e)); |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 105 | } |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 108 | if (execException != null) { |
Yue Gan | 400f82e | 2017-01-12 05:08:02 +0000 | [diff] [blame] | 109 | outErr.printErr( |
| 110 | "Use --strategy=" |
| 111 | + spawn.getMnemonic() |
| 112 | + "=standalone to disable sandboxing for the failing actions.\n"); |
Philipp Wollermann | 238839c | 2016-10-27 08:55:07 +0000 | [diff] [blame] | 113 | throw execException; |
Philipp Wollermann | 9e21b41 | 2016-09-26 14:08:28 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 117 | /** Gets the list of directories that the spawn will assume to be writable. */ |
Kristina Chodorow | 4b73e97 | 2017-02-16 17:00:53 +0000 | [diff] [blame^] | 118 | protected ImmutableSet<Path> getWritableDirs(Path sandboxExecRoot, Map<String, String> env, |
| 119 | ImmutableSet<PathFragment> outputs) { |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 120 | Builder<Path> writableDirs = ImmutableSet.builder(); |
| 121 | // We have to make the TEST_TMPDIR directory writable if it is specified. |
| 122 | if (env.containsKey("TEST_TMPDIR")) { |
| 123 | writableDirs.add(sandboxExecRoot.getRelative(env.get("TEST_TMPDIR"))); |
| 124 | } |
Kristina Chodorow | 4b73e97 | 2017-02-16 17:00:53 +0000 | [diff] [blame^] | 125 | for (PathFragment output : outputs) { |
| 126 | writableDirs.add(sandboxExecRoot.getRelative(output).getParentDirectory()); |
| 127 | } |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 128 | return writableDirs.build(); |
| 129 | } |
| 130 | |
| 131 | protected ImmutableSet<Path> getInaccessiblePaths() { |
| 132 | ImmutableSet.Builder<Path> inaccessiblePaths = ImmutableSet.builder(); |
| 133 | for (String path : sandboxOptions.sandboxBlockPath) { |
| 134 | inaccessiblePaths.add(blazeDirs.getFileSystem().getPath(path)); |
| 135 | } |
| 136 | return inaccessiblePaths.build(); |
| 137 | } |
| 138 | |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 139 | @Override |
| 140 | public boolean willExecuteRemotely(boolean remotable) { |
| 141 | return false; |
| 142 | } |
| 143 | |
| 144 | @Override |
| 145 | public String toString() { |
| 146 | return "sandboxed"; |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public boolean shouldPropagateExecException() { |
| 151 | return verboseFailures && sandboxOptions.sandboxDebug; |
| 152 | } |
Philipp Wollermann | 809df53 | 2016-09-08 12:53:39 +0000 | [diff] [blame] | 153 | |
| 154 | public Map<PathFragment, Path> getMounts(Spawn spawn, ActionExecutionContext executionContext) |
| 155 | throws ExecException { |
| 156 | try { |
| 157 | return spawnHelpers.getMounts(spawn, executionContext); |
| 158 | } catch (IOException e) { |
| 159 | throw new EnvironmentalExecException("Could not prepare mounts for sandbox execution", e); |
| 160 | } |
| 161 | } |
| 162 | |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 163 | } |