Imported Upstream version 1.3.1
[platform/upstream/libunwind.git] / tests / Ltest-mem-validate.c
1 /* libunwind - a platform-independent unwind library
2    Copyright (C) 2003-2004 Hewlett-Packard Co
3         Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of libunwind.
6
7 Copyright (c) 2003 Hewlett-Packard Co.
8
9 Permission is hereby granted, free of charge, to any person obtaining
10 a copy of this software and associated documentation files (the
11 "Software"), to deal in the Software without restriction, including
12 without limitation the rights to use, copy, modify, merge, publish,
13 distribute, sublicense, and/or sell copies of the Software, and to
14 permit persons to whom the Software is furnished to do so, subject to
15 the following conditions:
16
17 The above copyright notice and this permission notice shall be
18 included in all copies or substantial portions of the Software.
19
20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */
27
28 #include "compiler.h"
29
30 #include <libunwind.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 #include <sys/resource.h>
37 #include <sys/mman.h>
38
39 #define panic(args...)                          \
40         { fprintf (stderr, args); exit (-1); }
41
42 void * stack_start;
43
44 #define PAGE_SIZE 4096
45
46 void do_backtrace (void)
47 {
48   void* buffer[1024];
49   int size = 1024;
50   mprotect((void*)((uintptr_t)stack_start & ~(PAGE_SIZE - 1)),
51            PAGE_SIZE, PROT_NONE);
52
53   unw_cursor_t cursor;
54   unw_word_t ip, sp;
55   unw_context_t uc;
56   int ret;
57   int steps = 0;
58
59   unw_getcontext (&uc);
60   if (unw_init_local (&cursor, &uc) < 0)
61     panic ("unw_init_local failed!\n");
62
63   do
64     {
65       unw_get_reg (&cursor, UNW_REG_IP, &ip);
66       unw_get_reg (&cursor, UNW_REG_SP, &sp);
67
68       ret = unw_step (&cursor);
69       if (ret < 0)
70         {
71           unw_get_reg (&cursor, UNW_REG_IP, &ip);
72         }
73       steps ++;
74     }
75   while (ret > 0);
76
77   if (steps < 5)
78     {
79       exit(-1);
80     }
81
82   mprotect((void*)((uintptr_t)stack_start & ~(PAGE_SIZE - 1)),
83            PAGE_SIZE, PROT_READ|PROT_WRITE);
84 }
85
86 void consume_and_run (int depth)
87 {
88   unw_cursor_t cursor;
89   unw_context_t uc;
90   char string[1024];
91
92   sprintf (string, "hello %p %p\n", &cursor, &uc);
93   if (depth == 0) {
94     do_backtrace();
95   } else {
96     consume_and_run(depth - 1);
97   }
98 }
99
100 int
101 main (int argc, char **argv UNUSED)
102 {
103   int start;
104   unw_context_t uc;
105   unw_cursor_t cursor;
106
107   stack_start = &start;
108
109   // Initialize pipe mem validate check, opens file descriptors
110   unw_getcontext(&uc);
111   if (unw_init_local (&cursor, &uc) < 0)
112     panic ("unw_init_local failed!\n");
113
114   int i;
115   for (i = 3; i < 10; i++)
116     {
117
118       pid_t childpid = fork();
119       if (!childpid)
120         {
121           /* Close fds and make sure we still work */
122           int ret = close(i);
123         }
124
125       int status;
126       if (childpid)
127         {
128           wait(&status);
129           if (WIFEXITED(status))
130               return WEXITSTATUS(status);
131           else
132             return -1;
133         }
134       else
135         {
136           consume_and_run (10);
137
138           return 0;
139         }
140     }
141
142   return 0;
143 }