Skip to content

Commit 5f9f141

Browse files
lberkiiancha1992
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 bazelbuild#20753. RELNOTES: None. PiperOrigin-RevId: 596566851 Change-Id: Ifb56a3016a521b6a0cd4b5700172951d6feabddf
1 parent f00da30 commit 5f9f141

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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
@@ -14,6 +14,7 @@
1414

1515
package com.google.devtools.build.lib.sandbox;
1616

17+
import static com.google.common.collect.ImmutableSet.toImmutableSet;
1718
import static com.google.devtools.build.lib.sandbox.LinuxSandboxCommandLineBuilder.NetworkNamespace.NETNS_WITH_LOOPBACK;
1819
import static com.google.devtools.build.lib.sandbox.LinuxSandboxCommandLineBuilder.NetworkNamespace.NO_NETNS;
1920

@@ -61,6 +62,8 @@
6162
import java.util.Map;
6263
import java.util.Optional;
6364
import java.util.SortedMap;
65+
import java.util.Set;
66+
import java.util.TreeSet;
6467
import java.util.concurrent.atomic.AtomicBoolean;
6568
import javax.annotation.Nullable;
6669

@@ -390,7 +393,7 @@ public String getName() {
390393
protected ImmutableSet<Path> getWritableDirs(
391394
Path sandboxExecRoot, Path withinSandboxExecRoot, Map<String, String> env)
392395
throws IOException {
393-
ImmutableSet.Builder<Path> writableDirs = ImmutableSet.builder();
396+
Set<Path> writableDirs = new TreeSet<>();
394397
writableDirs.addAll(super.getWritableDirs(sandboxExecRoot, withinSandboxExecRoot, env));
395398
if (getSandboxOptions().memoryLimitMb > 0) {
396399
CgroupsInfo cgroupsInfo = CgroupsInfo.getInstance();
@@ -400,7 +403,23 @@ protected ImmutableSet<Path> getWritableDirs(
400403
writableDirs.add(fs.getPath("/dev/shm").resolveSymbolicLinks());
401404
writableDirs.add(fs.getPath("/tmp"));
402405

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

406425
private ImmutableList<BindMount> getBindMounts(

0 commit comments

Comments
 (0)