xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/examples/sum/sum.c (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
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 //     https://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 #include <stdio.h>
16 #include <stdlib.h>
17 #include <sys/ptrace.h>
18 #include <unistd.h>
19 
20 int sumsymbol = 5;
21 
22 typedef struct sum_params_s {
23   int a;
24   int b;
25   int ret;
26 } sum_params;
27 
ftest(FILE * f)28 extern int ftest(FILE *f) {
29   return f->_offset;
30 }
31 
sum(int a,int b)32 extern int sum(int a, int b) {
33   return a + b;
34 }
35 
sums(sum_params * params)36 extern void sums(sum_params* params) {
37   params->ret =  params->a + params->b;
38 }
39 
addf(float a,double b,long double c)40 extern long double addf(float a, double b, long double c) {
41   return a + b + c;
42 }
43 
sub(int a,int b)44 extern int sub(int a, int b) {
45   return a - b;
46 }
47 
mul(int a,int b)48 extern int mul(int a, int b) {
49   return a * b;
50 }
51 
divs(int a,int b)52 extern int divs(int a, int b) {
53   return a / b;
54 }
55 
muld(double a,float b)56 extern double muld(double a, float b) {
57   return a * b;
58 }
59 
crash(void)60 extern void crash(void) {
61   void(*die)() = (void(*)())(0x0000dead);
62   die();
63 }
64 
violate(void)65 extern void violate(void) {
66   // Issue a PTRACE_CONT that will always fail, since we are not in stopped
67   // state. The actual call should be caught by the sandbox policy.
68   ptrace(PTRACE_CONT, 0, NULL, NULL);
69 }
70 
sumarr(int * input,size_t nelem)71 extern int sumarr(int* input, size_t nelem) {
72   int s = 0, i;
73   for (i = 0; i < nelem; i++) {
74     s += input[i];
75   }
76   return s;
77 }
78 
testptr(void * ptr)79 extern void testptr(void *ptr) {
80   if (ptr) {
81     puts("Is Not a NULL-ptr");
82   } else {
83     puts("Is a NULL-ptr");
84   }
85 }
86 
read_int(int fd)87 extern int read_int(int fd) {
88   char buf[10] = {0};
89   int ret = read(fd, buf, sizeof(buf) - 1);
90   if(ret > 0) {
91     ret = atoi(buf);
92   }
93   return ret;
94 }
95 
sleep_for_sec(int sec)96 extern void sleep_for_sec(int sec) {
97   sleep(sec);
98 }
99