40b144080b6e2d824c9d162480d29cf833d481cc
[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 f2 (clib_longjmp_t * env)
51 {
52   i++;
53   clib_longjmp (env, 1);
54 }
55
56 static never_inline void f1 (clib_longjmp_t * env)
57 {
58   i++;
59   f2 (env);
60 }
61
62 int test_longjmp_main (unformat_input_t * input)
63 {
64   clib_longjmp_t env;
65
66   i = 0;
67   if (clib_setjmp (&env, 0) == 0)
68     {
69       if_verbose ("calling long jumper %d", i);
70       f1 (&env);
71     }
72   if_verbose ("back from long jump %d", i);
73
74   test_calljmp (input);
75
76   return 0;
77 }
78
79 static uword f3 (uword arg)
80 {
81   uword i, j, array[10];
82
83   for (i = 0; i < ARRAY_LEN (array); i++)
84     array[i] = arg + i;
85
86   j = 0;
87   for (i = 0; i < ARRAY_LEN (array); i++)
88     j ^= array[i];
89
90   return j;
91 }
92
93 static void test_calljmp (unformat_input_t * input)
94 {
95   static u8 stack[32*1024];
96   uword v;
97
98   v = clib_calljmp (f3, 0, stack + sizeof (stack));
99   ASSERT (v == f3 (0));
100   if_verbose ("calljump ok");
101 }
102
103 #ifdef CLIB_UNIX
104 int main (int argc, char * argv [])
105 {
106   unformat_input_t i;
107   int res;
108
109   verbose = (argc > 1);
110   unformat_init_command_line (&i, argv);
111   res = test_longjmp_main (&i);
112   unformat_free (&i);
113   return res;
114 }
115 #endif