Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 38 | #include <vppinfra/clib.h> |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 50 | static never_inline void |
| 51 | f2 (clib_longjmp_t * env) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 52 | { |
| 53 | i++; |
| 54 | clib_longjmp (env, 1); |
| 55 | } |
| 56 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 57 | static never_inline void |
| 58 | f1 (clib_longjmp_t * env) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
| 60 | i++; |
| 61 | f2 (env); |
| 62 | } |
| 63 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 64 | int |
| 65 | test_longjmp_main (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 82 | static uword |
| 83 | f3 (uword arg) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | { |
Damjan Marion | 97475fd | 2021-10-31 19:06:20 +0100 | [diff] [blame^] | 85 | return (uword) __builtin_frame_address (0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 88 | static void |
| 89 | test_calljmp (unformat_input_t * input) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | { |
Damjan Marion | 97475fd | 2021-10-31 19:06:20 +0100 | [diff] [blame^] | 91 | 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 Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 100 | |
| 101 | v = clib_calljmp (f3, 0, stack + sizeof (stack)); |
Damjan Marion | 97475fd | 2021-10-31 19:06:20 +0100 | [diff] [blame^] | 102 | if_verbose ("calljump %s", |
| 103 | v >= start && v < (end - sizeof (uword)) ? "ok" : "fail"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | #ifdef CLIB_UNIX |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 107 | int |
| 108 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | { |
| 110 | unformat_input_t i; |
| 111 | int res; |
| 112 | |
Damjan Marion | 4dffd1c | 2018-09-03 12:30:36 +0200 | [diff] [blame] | 113 | clib_mem_init (0, 64 << 20); |
| 114 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * fd.io coding-style-patch-verification: ON |
| 125 | * |
| 126 | * Local Variables: |
| 127 | * eval: (c-set-style "gnu") |
| 128 | * End: |
| 129 | */ |