2415c4f061c7ca783ced1bd6a46479ae05f3dcfd
[vpp.git] / vppinfra / vppinfra / test_longjmp.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
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  *     http://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 /*
16   Copyright (c) 2005 Eliot Dresselhaus
17
18   Permission is hereby granted, free of charge, to any person obtaining
19   a copy of this software and associated documentation files (the
20   "Software"), to deal in the Software without restriction, including
21   without limitation the rights to use, copy, modify, merge, publish,
22   distribute, sublicense, and/or sell copies of the Software, and to
23   permit persons to whom the Software is furnished to do so, subject to
24   the following conditions:
25
26   The above copyright notice and this permission notice shall be
27   included in all copies or substantial portions of the Software.
28
29   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33   LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34   OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35   WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 */
37
38 #include <vppinfra/clib.h>
39 #include <vppinfra/longjmp.h>
40 #include <vppinfra/format.h>
41
42 static void test_calljmp (unformat_input_t * input);
43
44 static int i;
45
46 static int verbose;
47 #define if_verbose(format,args...) \
48   if (verbose) { clib_warning(format, ## args); }
49
50 static never_inline void
51 f2 (clib_longjmp_t * env)
52 {
53   i++;
54   clib_longjmp (env, 1);
55 }
56
57 static never_inline void
58 f1 (clib_longjmp_t * env)
59 {
60   i++;
61   f2 (env);
62 }
63
64 int
65 test_longjmp_main (unformat_input_t * input)
66 {
67   clib_longjmp_t env;
68
69   i = 0;
70   if (clib_setjmp (&env, 0) == 0)
71     {
72       if_verbose ("calling long jumper %d", i);
73       f1 (&env);
74     }
75   if_verbose ("back from long jump %d", i);
76
77   test_calljmp (input);
78
79   return 0;
80 }
81
82 static uword
83 f3 (uword arg)
84 {
85   uword i, j, array[10];
86
87   for (i = 0; i < ARRAY_LEN (array); i++)
88     array[i] = arg + i;
89
90   j = 0;
91   for (i = 0; i < ARRAY_LEN (array); i++)
92     j ^= array[i];
93
94   return j;
95 }
96
97 static void
98 test_calljmp (unformat_input_t * input)
99 {
100   static u8 stack[32 * 1024] __attribute__ ((aligned (16)));
101   uword v;
102
103   v = clib_calljmp (f3, 0, stack + sizeof (stack));
104   ASSERT (v == f3 (0));
105   if_verbose ("calljump ok");
106 }
107
108 #ifdef CLIB_UNIX
109 int
110 main (int argc, char *argv[])
111 {
112   unformat_input_t i;
113   int res;
114
115   verbose = (argc > 1);
116   unformat_init_command_line (&i, argv);
117   res = test_longjmp_main (&i);
118   unformat_free (&i);
119   return res;
120 }
121 #endif
122
123 /*
124  * fd.io coding-style-patch-verification: ON
125  *
126  * Local Variables:
127  * eval: (c-set-style "gnu")
128  * End:
129  */