Fix report_basic test
[platform/core/system/crash-worker.git] / tests / test1.c
1 #include <libgen.h>
2 #include <stdint.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/mman.h>
7 #include <sys/types.h>
8 #include <unistd.h>
9
10 int do_sleep=0;
11 int do_ill=0;
12
13 #if defined(__arm__)
14 char ill_buf[] = { 0xf0, 0x00, 0x00, 0xf0 };
15 #elif defined(__aarch64__)
16 char ill_buf[] = { 0x22, 0x00, 0x00, 0xf9 };
17 #elif defined(__i386__) || defined(__x86_64__)
18 char ill_buf[] = { 0x06, 0x00, 0x00, 0x00 };
19 #else
20 #error "Unsupported architecture"
21 #endif
22
23 int main(int ac, char *av[]);
24
25 void(*baz)(void);
26
27 int bar(int a, int b){
28         int *c=(int*)0;
29         if (do_ill)
30                 baz();
31         if (do_sleep)
32                 sleep(-1);
33         return a + b + *c;
34 }
35
36 int foo(int c){
37         int a=1;
38         int b=2;
39         return bar(a+c,b+c);
40 }
41
42 int *memory;
43
44 int allocate(size_t amount) {
45         size_t i;
46         memory = malloc(amount);
47
48         if (memory == NULL)
49                 exit(1);
50
51         for (i=0; i < amount/sizeof(size_t); i++) {
52                 memory[i]=i;
53         }
54         return 0;
55 }
56
57 int str_ends_with(const char *str, const char *suffix)
58 {
59     if (!str || !suffix)
60         return 0;
61     size_t lenstr = strlen(str);
62     size_t lensuffix = strlen(suffix);
63     if (lensuffix > lenstr)
64         return 0;
65     return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
66 }
67
68 int main(int ac, char* av[])
69 {
70         if (str_ends_with(av[0], "-sleep"))
71                 do_sleep = 1;
72         else if (str_ends_with(av[0], "-ill"))
73                 do_ill = 1;
74         baz = mmap(0, sizeof(ill_buf), PROT_READ|PROT_WRITE|PROT_EXEC,
75                    MAP_PRIVATE|MAP_ANON, -1, 0);
76         memcpy(baz, ill_buf, sizeof(ill_buf));
77         allocate(256*1024*1024);
78         return foo(2);
79 }