1 /*
2 * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <string.h>
8 #include <sys/param.h>
9 #include "esp_system.h"
10 #include "spi_flash_mmap.h"
11 #include "soc/soc_memory_layout.h"
12 #include "esp_private/cache_utils.h"
13
14 #define ASSERT_STR "assert failed: "
15 #define CACHE_DISABLED_STR "<cached disabled>"
16
17 #if __XTENSA__
18 #define INST_LEN 3
19 #elif __riscv
20 #define INST_LEN 4
21 #endif
22
ra_to_str(char * addr)23 static inline void ra_to_str(char *addr)
24 {
25 addr[0] = '0';
26 addr[1] = 'x';
27 itoa((uint32_t)(__builtin_return_address(0) - INST_LEN), addr + 2, 16);
28 }
29
30 /* Overriding assert function so that whenever assert is called from critical section,
31 * it does not lead to a crash of its own.
32 */
__assert_func(const char * file,int line,const char * func,const char * expr)33 void __attribute__((noreturn)) __assert_func(const char *file, int line, const char *func, const char *expr)
34 {
35 #if CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT
36 char buff[sizeof(ASSERT_STR) + 11 + 1] = ASSERT_STR;
37
38 ra_to_str(&buff[sizeof(ASSERT_STR) - 1]);
39
40 esp_system_abort(buff);
41 #else
42 char addr[11] = { 0 };
43 char buff[200];
44 char lbuf[5];
45 uint32_t rem_len = sizeof(buff) - 1;
46 uint32_t off = 0;
47
48 itoa(line, lbuf, 10);
49
50 #if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
51 if (!spi_flash_cache_enabled())
52 #endif
53 {
54 if (esp_ptr_in_drom(file)) {
55 file = CACHE_DISABLED_STR;
56 }
57
58 if (esp_ptr_in_drom(func)) {
59 ra_to_str(addr);
60 func = addr;
61 }
62
63 if (esp_ptr_in_drom(expr)) {
64 expr = CACHE_DISABLED_STR;
65 }
66 }
67
68 const char *str[] = {ASSERT_STR, func ? func : "\b", " ", file, ":", lbuf, " (", expr, ")"};
69
70 for (int i = 0; i < sizeof(str) / sizeof(str[0]); i++) {
71 uint32_t len = strlen(str[i]);
72 uint32_t cpy_len = MIN(len, rem_len);
73 memcpy(buff + off, str[i], cpy_len);
74 rem_len -= cpy_len;
75 off += cpy_len;
76 if (rem_len == 0) {
77 break;
78 }
79 }
80 buff[off] = '\0';
81 esp_system_abort(buff);
82 #endif /* CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT */
83 }
84
__assert(const char * file,int line,const char * failedexpr)85 void __attribute__((noreturn)) __assert(const char *file, int line, const char *failedexpr)
86 {
87 __assert_func(file, line, NULL, failedexpr);
88 }
89
90 /* No-op function, used to force linker to include these changes */
newlib_include_assert_impl(void)91 void newlib_include_assert_impl(void)
92 {
93 }
94