Skip to content

Commit bc1d9d3

Browse files
lberkicopybara-github
authored andcommitted
Rewrite paths of writable directories that are under the execroot.
This is necessary because that paths of those directories are different when seen by Bazel and by the processes within the sandbox and the sandbox interprets paths to writable directories as within the sandbox. This is notably the case for $TEST_TMPDIR. The reason why this worked at all is that the $TEST_TMPDIR that Bazel passes to the test is relative to the working directory (it's absolutized in the test wrapper script) Progress on #20753. RELNOTES: None. PiperOrigin-RevId: 596566851 Change-Id: Ifb56a3016a521b6a0cd4b5700172951d6feabddf
1 parent 161ba07 commit bc1d9d3

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

src/main/java/com/google/devtools/build/lib/sandbox/LinuxSandboxedSpawnRunner.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.devtools.build.lib.sandbox;
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
18+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1819
import static com.google.devtools.build.lib.sandbox.LinuxSandboxCommandLineBuilder.NetworkNamespace.NETNS_WITH_LOOPBACK;
1920
import static com.google.devtools.build.lib.sandbox.LinuxSandboxCommandLineBuilder.NetworkNamespace.NO_NETNS;
2021

@@ -59,8 +60,10 @@
5960
import java.time.Duration;
6061
import java.util.HashMap;
6162
import java.util.Map;
63+
import java.util.Set;
6264
import java.util.SortedMap;
6365
import java.util.TreeMap;
66+
import java.util.TreeSet;
6467
import java.util.concurrent.atomic.AtomicBoolean;
6568
import javax.annotation.Nullable;
6669

@@ -384,7 +387,7 @@ public String getName() {
384387
protected ImmutableSet<Path> getWritableDirs(
385388
Path sandboxExecRoot, Path withinSandboxExecRoot, Map<String, String> env)
386389
throws IOException {
387-
ImmutableSet.Builder<Path> writableDirs = ImmutableSet.builder();
390+
Set<Path> writableDirs = new TreeSet<>();
388391
writableDirs.addAll(super.getWritableDirs(sandboxExecRoot, withinSandboxExecRoot, env));
389392
if (getSandboxOptions().memoryLimitMb > 0) {
390393
CgroupsInfo cgroupsInfo = CgroupsInfo.getInstance();
@@ -394,7 +397,23 @@ protected ImmutableSet<Path> getWritableDirs(
394397
writableDirs.add(fs.getPath("/dev/shm").resolveSymbolicLinks());
395398
writableDirs.add(fs.getPath("/tmp"));
396399

397-
return writableDirs.build();
400+
if (sandboxExecRoot.equals(withinSandboxExecRoot)) {
401+
return ImmutableSet.copyOf(writableDirs);
402+
}
403+
404+
// If a writable directory is under the sandbox exec root, transform it so that its path will
405+
// be the one that it will be available at after processing the bind mounts (this is how the
406+
// sandbox interprets the corresponding arguments)
407+
//
408+
// Notably, this is usually the case for $TEST_TMPDIR because its default value is under the
409+
// execroot.
410+
return writableDirs.stream()
411+
.map(
412+
d ->
413+
d.startsWith(sandboxExecRoot)
414+
? withinSandboxExecRoot.getRelative(d.relativeTo(sandboxExecRoot))
415+
: d)
416+
.collect(toImmutableSet());
398417
}
399418

400419
private ImmutableList<BindMount> prepareAndGetBindMounts(

0 commit comments

Comments
 (0)