xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/testcases/limits.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li // A binary to test sandbox2 limits.
16*ec63e07aSXin Li // Per setrlimit(2): exceeding RLIMIT_AS with mmap, brk or mremap do not
17*ec63e07aSXin Li // kill but fail with ENOMEM. However if we trigger automatic stack
18*ec63e07aSXin Li // expansion, for instance with a large stack allocation with alloca(3),
19*ec63e07aSXin Li // and we have no alternate stack, then we are killed with SIGSEGV.
20*ec63e07aSXin Li 
21*ec63e07aSXin Li #include <alloca.h>
22*ec63e07aSXin Li #include <sys/mman.h>
23*ec63e07aSXin Li 
24*ec63e07aSXin Li #include <cerrno>
25*ec63e07aSXin Li #include <cstdio>
26*ec63e07aSXin Li #include <cstdlib>
27*ec63e07aSXin Li 
TestMmapUnderLimit(void)28*ec63e07aSXin Li int TestMmapUnderLimit(void) {
29*ec63e07aSXin Li   // mmap should work
30*ec63e07aSXin Li   void* ptr = mmap(0, 1ULL << 20 /* 1 MiB */, PROT_READ | PROT_WRITE,
31*ec63e07aSXin Li                    MAP_ANONYMOUS | MAP_SHARED, -1, 0);
32*ec63e07aSXin Li   if (ptr == MAP_FAILED) {
33*ec63e07aSXin Li     return EXIT_FAILURE;
34*ec63e07aSXin Li   }
35*ec63e07aSXin Li   return EXIT_SUCCESS;
36*ec63e07aSXin Li }
37*ec63e07aSXin Li 
TestMmapAboveLimit(void)38*ec63e07aSXin Li int TestMmapAboveLimit(void) {
39*ec63e07aSXin Li   // mmap should fail with ENOMEM
40*ec63e07aSXin Li   void* ptr = mmap(0, 100ULL << 20 /* 100 MiB */, PROT_READ | PROT_WRITE,
41*ec63e07aSXin Li                    MAP_ANONYMOUS | MAP_SHARED, -1, 0);
42*ec63e07aSXin Li   if (ptr != MAP_FAILED || errno != ENOMEM) {
43*ec63e07aSXin Li     return EXIT_FAILURE;
44*ec63e07aSXin Li   }
45*ec63e07aSXin Li   return EXIT_SUCCESS;
46*ec63e07aSXin Li }
47*ec63e07aSXin Li 
48*ec63e07aSXin Li // Tests using alloca are marked noinline because clang in optimized mode tries
49*ec63e07aSXin Li // to inline the test function, and then "optimizes" it by moving the alloca
50*ec63e07aSXin Li // stack allocation to the beginning of main() and merging it with main()'s
51*ec63e07aSXin Li // local variable allocation. This is specially inconvenient for TestAllocaBig*
52*ec63e07aSXin Li // functions below, because they make an allocation big enough to kill the
53*ec63e07aSXin Li // process, and with inlining they get to kill the process every time.
54*ec63e07aSXin Li //
55*ec63e07aSXin Li // This workaround makes sure the stack allocation is only done when the test
56*ec63e07aSXin Li // function is actually called.
57*ec63e07aSXin Li 
TestAllocaSmallUnderLimit()58*ec63e07aSXin Li __attribute__((noinline)) int TestAllocaSmallUnderLimit() {
59*ec63e07aSXin Li   void* ptr = alloca(1ULL << 20 /* 1 MiB */);
60*ec63e07aSXin Li   printf("alloca worked (ptr=%p)\n", ptr);
61*ec63e07aSXin Li   return EXIT_SUCCESS;
62*ec63e07aSXin Li }
63*ec63e07aSXin Li 
TestAllocaBigUnderLimit()64*ec63e07aSXin Li __attribute__((noinline)) int TestAllocaBigUnderLimit() {
65*ec63e07aSXin Li   void* ptr = alloca(8ULL << 20 /* 8 MiB */);
66*ec63e07aSXin Li   printf("We should have been killed by now (ptr=%p)\n", ptr);
67*ec63e07aSXin Li   return EXIT_FAILURE;
68*ec63e07aSXin Li }
69*ec63e07aSXin Li 
TestAllocaBigAboveLimit()70*ec63e07aSXin Li __attribute__((noinline)) int TestAllocaBigAboveLimit() {
71*ec63e07aSXin Li   void* ptr = alloca(100ULL << 20 /* 100 MiB */);
72*ec63e07aSXin Li   printf("We should have been killed by now (ptr=%p)\n", ptr);
73*ec63e07aSXin Li   return EXIT_FAILURE;
74*ec63e07aSXin Li }
75*ec63e07aSXin Li 
main(int argc,char * argv[])76*ec63e07aSXin Li int main(int argc, char* argv[]) {
77*ec63e07aSXin Li   // Disable buffering.
78*ec63e07aSXin Li   setbuf(stdin, nullptr);
79*ec63e07aSXin Li   setbuf(stdout, nullptr);
80*ec63e07aSXin Li   setbuf(stderr, nullptr);
81*ec63e07aSXin Li 
82*ec63e07aSXin Li   if (argc < 2) {
83*ec63e07aSXin Li     printf("argc < 2\n");
84*ec63e07aSXin Li     return EXIT_FAILURE;
85*ec63e07aSXin Li   }
86*ec63e07aSXin Li 
87*ec63e07aSXin Li   int testno = atoi(argv[1]);  // NOLINT
88*ec63e07aSXin Li   switch (testno) {
89*ec63e07aSXin Li     case 1:
90*ec63e07aSXin Li       return TestMmapUnderLimit();
91*ec63e07aSXin Li     case 2:
92*ec63e07aSXin Li       return TestMmapAboveLimit();
93*ec63e07aSXin Li     case 3:
94*ec63e07aSXin Li       return TestAllocaSmallUnderLimit();
95*ec63e07aSXin Li     case 4:
96*ec63e07aSXin Li       return TestAllocaBigUnderLimit();
97*ec63e07aSXin Li     case 5:
98*ec63e07aSXin Li       return TestAllocaBigAboveLimit();
99*ec63e07aSXin Li     default:
100*ec63e07aSXin Li       printf("Unknown test: %d\n", testno);
101*ec63e07aSXin Li       return EXIT_FAILURE;
102*ec63e07aSXin Li   }
103*ec63e07aSXin Li 
104*ec63e07aSXin Li   return EXIT_SUCCESS;
105*ec63e07aSXin Li }
106