blob: 01debe2ac3738c2900212bdc318c46559f14828d [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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
Dave Barachc3799992016-08-15 11:12:27 -040038#include <vppinfra/clib.h>
Ed Warnickecb9cada2015-12-08 15:45:58 -070039#include <vppinfra/longjmp.h>
40#include <vppinfra/format.h>
41
42static void test_calljmp (unformat_input_t * input);
43
44static int i;
45
46static int verbose;
47#define if_verbose(format,args...) \
48 if (verbose) { clib_warning(format, ## args); }
49
Dave Barachc3799992016-08-15 11:12:27 -040050static never_inline void
51f2 (clib_longjmp_t * env)
Ed Warnickecb9cada2015-12-08 15:45:58 -070052{
53 i++;
54 clib_longjmp (env, 1);
55}
56
Dave Barachc3799992016-08-15 11:12:27 -040057static never_inline void
58f1 (clib_longjmp_t * env)
Ed Warnickecb9cada2015-12-08 15:45:58 -070059{
60 i++;
61 f2 (env);
62}
63
Dave Barachc3799992016-08-15 11:12:27 -040064int
65test_longjmp_main (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070066{
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
Dave Barachc3799992016-08-15 11:12:27 -040082static uword
83f3 (uword arg)
Ed Warnickecb9cada2015-12-08 15:45:58 -070084{
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
Dave Barachc3799992016-08-15 11:12:27 -040097static void
98test_calljmp (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070099{
Dave Barachc3799992016-08-15 11:12:27 -0400100 static u8 stack[32 * 1024] __attribute__ ((aligned (16)));
Ed Warnickecb9cada2015-12-08 15:45:58 -0700101 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
Dave Barachc3799992016-08-15 11:12:27 -0400109int
110main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700111{
112 unformat_input_t i;
113 int res;
114
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200115 clib_mem_init (0, 64 << 20);
116
Ed Warnickecb9cada2015-12-08 15:45:58 -0700117 verbose = (argc > 1);
118 unformat_init_command_line (&i, argv);
119 res = test_longjmp_main (&i);
120 unformat_free (&i);
121 return res;
122}
123#endif
Dave Barachc3799992016-08-15 11:12:27 -0400124
125/*
126 * fd.io coding-style-patch-verification: ON
127 *
128 * Local Variables:
129 * eval: (c-set-style "gnu")
130 * End:
131 */