blob: 50dc24b48b0900a87f820f45edbce80f3d663213 [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{
Damjan Marion97475fd2021-10-31 19:06:20 +010085 return (uword) __builtin_frame_address (0);
Ed Warnickecb9cada2015-12-08 15:45:58 -070086}
87
Dave Barachc3799992016-08-15 11:12:27 -040088static void
89test_calljmp (unformat_input_t * input)
Ed Warnickecb9cada2015-12-08 15:45:58 -070090{
Damjan Marion97475fd2021-10-31 19:06:20 +010091 u8 stack[4096] __attribute__ ((aligned (16))) = {};
92 uword start, end, v;
93
94 start = pointer_to_uword (stack);
95 end = start + ARRAY_LEN (stack);
96
97 v = f3 (0);
98 if (!(v < start || v > end))
99 clib_panic ("something went wrong in the calljmp test");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700100
101 v = clib_calljmp (f3, 0, stack + sizeof (stack));
Damjan Marion97475fd2021-10-31 19:06:20 +0100102 if_verbose ("calljump %s",
103 v >= start && v < (end - sizeof (uword)) ? "ok" : "fail");
Ed Warnickecb9cada2015-12-08 15:45:58 -0700104}
105
106#ifdef CLIB_UNIX
Dave Barachc3799992016-08-15 11:12:27 -0400107int
108main (int argc, char *argv[])
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109{
110 unformat_input_t i;
111 int res;
112
Damjan Marion4dffd1c2018-09-03 12:30:36 +0200113 clib_mem_init (0, 64 << 20);
114
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115 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
Dave Barachc3799992016-08-15 11:12:27 -0400122
123/*
124 * fd.io coding-style-patch-verification: ON
125 *
126 * Local Variables:
127 * eval: (c-set-style "gnu")
128 * End:
129 */