|
| 1 | +/******************************************************************************* |
| 2 | + * (c) 2018 - 2024 Zondax AG |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + ********************************************************************************/ |
| 16 | + |
| 17 | +#include <stdbool.h> |
| 18 | +#include <stdint.h> |
| 19 | + |
| 20 | +#include "parser_common.h" |
| 21 | +#include "zxmacros.h" |
| 22 | + |
| 23 | +#if defined(LEDGER_SPECIFIC) |
| 24 | +#define STACK_SHIFT 0x20 |
| 25 | +#define MINIMUM_STACK 0x400 |
| 26 | +#else |
| 27 | +static int16_t recursionDepthCounter = 0; |
| 28 | +#define MAX_RECURSION_DEPTH 50 |
| 29 | +#endif |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief Checks the available stack space to prevent stack overflow. |
| 33 | + * |
| 34 | + * @return parser_error_t Returns parser_running_out_of_stack if stack space is insufficient, otherwise parser_ok. |
| 35 | + */ |
| 36 | +parser_error_t checkStack() { |
| 37 | +#if defined(LEDGER_SPECIFIC) |
| 38 | + // NOLINTNEXTLINE(readability-identifier-length): here `p` is fine |
| 39 | + void *p = NULL; |
| 40 | + const uint32_t availableStack = (uint32_t)((void *)&p) + STACK_SHIFT - (uint32_t)&app_stack_canary; |
| 41 | + ZEMU_LOGF(50, "Check: available stack: %d\n", availableStack) |
| 42 | + if (availableStack <= MINIMUM_STACK) { |
| 43 | + return parser_running_out_of_stack; |
| 44 | + } |
| 45 | +#else |
| 46 | + if (recursionDepthCounter >= MAX_RECURSION_DEPTH) { |
| 47 | + return parser_running_out_of_stack; |
| 48 | + } |
| 49 | + recursionDepthCounter++; |
| 50 | +#endif |
| 51 | + return parser_ok; |
| 52 | +} |
| 53 | + |
| 54 | +/** |
| 55 | + * @brief Frees the stack space by decrementing the recursion depth counter. |
| 56 | + * |
| 57 | + * @return parser_error_t Always returns parser_ok. |
| 58 | + */ |
| 59 | +parser_error_t freeStack(uint8_t depth) { |
| 60 | +#if !defined(LEDGER_SPECIFIC) |
| 61 | + if (recursionDepthCounter > 0) { |
| 62 | + recursionDepthCounter -= depth; |
| 63 | + } |
| 64 | +#else |
| 65 | + (void)depth; |
| 66 | + void *p = NULL; |
| 67 | + const uint32_t availableStack = (uint32_t)((void *)&p) - (uint32_t)&app_stack_canary; |
| 68 | + ZEMU_LOGF(50, "Free: available stack: %d\n", availableStack) |
| 69 | + if (availableStack <= MINIMUM_STACK) { |
| 70 | + return parser_running_out_of_stack; |
| 71 | + } |
| 72 | +#endif |
| 73 | + return parser_ok; |
| 74 | +} |
0 commit comments