Skip to content

Commit 09a07a4

Browse files
larsrc-googlecopybara-github
authored andcommitted
Change --experimental_sandbox_memory_limit to take a number in mebibytes, renaming it to --experimental_sandbox_memory_limit_mb.
Also allows using HOST_RAM. An int value can only handle up to 2GB, which is not enough. PiperOrigin-RevId: 509171311 Change-Id: I8e45c0309b72b498e87b707fd1a4143c45a91a93
1 parent dfcb44a commit 09a07a4

File tree

7 files changed

+29
-18
lines changed

7 files changed

+29
-18
lines changed

src/main/java/com/google/devtools/build/lib/sandbox/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ java_library(
3535
deps = [
3636
"//src/main/java/com/google/devtools/build/lib/actions:localhost_capacity",
3737
"//src/main/java/com/google/devtools/build/lib/util",
38+
"//src/main/java/com/google/devtools/build/lib/util:ram_resource_converter",
3839
"//src/main/java/com/google/devtools/build/lib/util:resource_converter",
3940
"//src/main/java/com/google/devtools/build/lib/vfs",
4041
"//src/main/java/com/google/devtools/build/lib/vfs:pathfragment",

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public static CgroupsInfo getInstance() throws IOException {
291291
/**
292292
* Creates a cgroups directory with the given memory limit.
293293
*
294-
* @param memoryLimit Memory limit in bytes.
294+
* @param memoryLimit Memory limit in megabytes (MiB).
295295
* @param dirName Base name of the directory created. In cgroups v2, <code>.scope</code> gets
296296
* appended.
297297
*/
@@ -304,13 +304,13 @@ public String createMemoryLimitCgroupDir(String dirName, int memoryLimit) throws
304304
// In cgroups v2, we need to propagate the controllers into new subdirs.
305305
Files.asCharSink(new File(cgroupsDir, "memory.oom.group"), UTF_8).write("1\n");
306306
Files.asCharSink(new File(cgroupsDir, "memory.max"), UTF_8)
307-
.write(Integer.toString(memoryLimit));
307+
.write(Long.toString(memoryLimit * 1024L * 1024L));
308308
} else {
309309
cgroupsDir = new File(getBlazeDir(), dirName);
310310
cgroupsDir.mkdirs();
311311
cgroupsDir.deleteOnExit();
312312
Files.asCharSink(new File(cgroupsDir, "memory.limit_in_bytes"), UTF_8)
313-
.write(Integer.toString(memoryLimit));
313+
.write(Long.toString(memoryLimit * 1024L * 1024L));
314314
}
315315
return cgroupsDir.toString();
316316
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,13 @@ protected SandboxedSpawn prepareSpawn(Spawn spawn, SpawnExecutionContext context
331331
.setUseDebugMode(sandboxOptions.sandboxDebug)
332332
.setKillDelay(timeoutKillDelay);
333333

334-
if (sandboxOptions.memoryLimit > 0) {
334+
if (sandboxOptions.memoryLimitMb > 0) {
335335
CgroupsInfo cgroupsInfo = CgroupsInfo.getInstance();
336336
// We put the sandbox inside a unique subdirectory using the context's ID. This ID is
337337
// unique per spawn run by this spawn runner.
338338
cgroupsDir =
339339
cgroupsInfo.createMemoryLimitCgroupDir(
340-
"sandbox_" + context.getId(), sandboxOptions.memoryLimit);
340+
"sandbox_" + context.getId(), sandboxOptions.memoryLimitMb);
341341
commandLineBuilder.setCgroupsDir(cgroupsDir);
342342
}
343343

@@ -410,7 +410,7 @@ protected ImmutableSet<Path> getWritableDirs(
410410
throws IOException {
411411
ImmutableSet.Builder<Path> writableDirs = ImmutableSet.builder();
412412
writableDirs.addAll(super.getWritableDirs(sandboxExecRoot, withinSandboxExecRoot, env));
413-
if (getSandboxOptions().memoryLimit > 0) {
413+
if (getSandboxOptions().memoryLimitMb > 0) {
414414
CgroupsInfo cgroupsInfo = CgroupsInfo.getInstance();
415415
writableDirs.add(fileSystem.getPath(cgroupsInfo.getMountPoint().getAbsolutePath()));
416416
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.Maps;
2121
import com.google.devtools.build.lib.actions.LocalHostCapacity;
2222
import com.google.devtools.build.lib.util.OptionsUtils;
23+
import com.google.devtools.build.lib.util.RamResourceConverter;
2324
import com.google.devtools.build.lib.util.ResourceConverter;
2425
import com.google.devtools.build.lib.vfs.FileSystem;
2526
import com.google.devtools.build.lib.vfs.Path;
@@ -396,14 +397,15 @@ public ImmutableSet<Path> getInaccessiblePaths(FileSystem fs) {
396397
public boolean sandboxHermeticTmp;
397398

398399
@Option(
399-
name = "experimental_sandbox_memory_limit",
400+
name = "experimental_sandbox_memory_limit_mb",
400401
defaultValue = "0",
401402
documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
402403
effectTags = {OptionEffectTag.EXECUTION},
404+
converter = RamResourceConverter.class,
403405
help =
404-
"If set to true, each Linux sandbox will be limited to the given amount of memory."
405-
+ " Requires cgroups v2 and permissions for the users to the cgroups dir.")
406-
public int memoryLimit;
406+
"If > 0, each Linux sandbox will be limited to the given amount of memory (in MB)."
407+
+ " Requires cgroups v1 or v2 and permissions for the users to the cgroups dir.")
408+
public int memoryLimitMb;
407409

408410
/** Converter for the number of threads used for asynchronous tree deletion. */
409411
public static final class AsyncTreeDeletesConverter extends ResourceConverter {

src/main/java/com/google/devtools/build/lib/util/RamResourceConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public RamResourceConverter() {
3232

3333
@Override
3434
public String getTypeDescription() {
35-
return "an integer, or \"HOST_RAM\", optionally followed by [-|*]<float>.";
35+
return "an integer number of MBs, or \"HOST_RAM\", optionally followed by [-|*]<float>.";
3636
}
3737
}

src/main/java/com/google/devtools/build/lib/worker/WorkerModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void buildStarting(BuildStartingEvent event) {
100100
sandboxOptions.sandboxDebug,
101101
ImmutableList.copyOf(sandboxOptions.sandboxTmpfsPath),
102102
ImmutableList.copyOf(sandboxOptions.sandboxWritablePath),
103-
sandboxOptions.memoryLimit,
103+
sandboxOptions.memoryLimitMb,
104104
sandboxOptions.getInaccessiblePaths(env.getRuntime().getFileSystem()));
105105
} else {
106106
workerSandboxOptions = null;

src/test/shell/integration/sandboxing_test.sh

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,16 +269,20 @@ function test_sandbox_hardening_with_cgroups_v1() {
269269

270270
mkdir pkg
271271
cat >pkg/BUILD <<EOF
272-
genrule(name = "pkg", outs = ["pkg.out"], cmd = "pwd; echo >\$@")
272+
genrule(
273+
name = "pkg",
274+
outs = ["pkg.out"],
275+
cmd = "pwd; hexdump -C -n 250000 < /dev/random | sort > /dev/null 2>\$@"
276+
)
273277
EOF
274278
local genfiles_base="$(bazel info ${PRODUCT_NAME}-genfiles)"
275279

276280
bazel build --genrule_strategy=linux-sandbox \
277-
--experimental_sandbox_memory_limit=1000000 //pkg \
281+
--experimental_sandbox_memory_limit_mb=1000 //pkg \
278282
>"${TEST_log}" 2>&1 || fail "Expected build to succeed"
279283
rm -f ${genfiles_base}/pkg/pkg.out
280284
bazel build --genrule_strategy=linux-sandbox \
281-
--experimental_sandbox_memory_limit=100 //pkg \
285+
--experimental_sandbox_memory_limit_mb=1 //pkg \
282286
>"${TEST_log}" 2>&1 && fail "Expected build to fail" || true
283287
}
284288

@@ -298,22 +302,26 @@ function test_sandbox_hardening_with_cgroups_v2() {
298302

299303
mkdir pkg
300304
cat >pkg/BUILD <<EOF
301-
genrule(name = "pkg", outs = ["pkg.out"], cmd = "pwd; echo >\$@")
305+
genrule(
306+
name = "pkg",
307+
outs = ["pkg.out"],
308+
cmd = "pwd; hexdump -C -n 250000 < /dev/random | sort > /dev/null 2>\$@"
309+
)
302310
EOF
303311
local genfiles_base="$(bazel info ${PRODUCT_NAME}-genfiles)"
304312
# Need to make sure the bazel server runs under systemd, too.
305313
bazel shutdown
306314

307315
XDG_RUNTIME_DIR=/run/user/$( id -u ) systemd-run --user --scope \
308316
bazel build --genrule_strategy=linux-sandbox \
309-
--experimental_sandbox_memory_limit=1000000 //pkg \
317+
--experimental_sandbox_memory_limit_mb=1000 //pkg \
310318
>"${TEST_log}" 2>&1 || fail "Expected build to succeed"
311319
rm -f ${genfiles_base}/pkg/pkg.out
312320

313321
bazel shutdown
314322
XDG_RUNTIME_DIR=/run/user/$( id -u ) systemd-run --user --scope \
315323
bazel build --genrule_strategy=linux-sandbox \
316-
--experimental_sandbox_memory_limit=100 //pkg \
324+
--experimental_sandbox_memory_limit_mb=1 //pkg \
317325
>"${TEST_log}" 2>&1 && fail "Expected build to fail" || true
318326
}
319327

0 commit comments

Comments
 (0)