Introduce libc independent assert fail handler
Introduce assert_fail_handler function definition as a libc independent
interface for handling assert failures.
Signed-off-by: Imre Kis <imre.kis@arm.com>
Change-Id: I80207b0158e44e697c8a0b8366c56dfbdc9b7c37
diff --git a/components/common/libc/include/assert_fail_handler.h b/components/common/libc/include/assert_fail_handler.h
new file mode 100644
index 0000000..7f36f25
--- /dev/null
+++ b/components/common/libc/include/assert_fail_handler.h
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#ifndef ASSERT_FAIL_HANDLER_H_
+#define ASSERT_FAIL_HANDLER_H_
+
+#include "compiler.h"
+
+/*
+ * Generic assert fail handler function definition. Should be implemented by the environment.
+ */
+void __noreturn assert_fail_handler(const char *file, int line,
+ const char *func, const char *failedexpr);
+
+#endif /* ASSERT_FAIL_HANDLER_H_ */
diff --git a/environments/opteesp/component.cmake b/environments/opteesp/component.cmake
index 86b1d19..72eef78 100644
--- a/environments/opteesp/component.cmake
+++ b/environments/opteesp/component.cmake
@@ -11,6 +11,7 @@
target_sources(${TGT} PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/optee_sp_header.c"
+ "${CMAKE_CURRENT_LIST_DIR}/sp_assert.c"
"${CMAKE_CURRENT_LIST_DIR}/sp_entry.c"
"${CMAKE_CURRENT_LIST_DIR}/sp_trace.c"
)
diff --git a/environments/opteesp/sp_assert.c b/environments/opteesp/sp_assert.c
new file mode 100644
index 0000000..8f336e7
--- /dev/null
+++ b/environments/opteesp/sp_assert.c
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: BSD-3-Clause
+/*
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
+ */
+
+#include "assert_fail_handler.h"
+#include "compiler.h"
+#include "trace.h"
+
+/*
+ * The generic trace function called on assert fail.
+ */
+void __noreturn assert_fail_handler(const char *file, int line,
+ const char *func, const char *failedexpr)
+{
+#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
+ trace_printf(func, line, TRACE_LEVEL_ERROR, "assertion %s failed", failedexpr);
+#endif /* TRACE_LEVEL */
+
+ while (1)
+ ;
+}
diff --git a/external/newlib/newlib_sp_assert.c b/external/newlib/newlib_sp_assert.c
index 5a2d428..f386bb4 100644
--- a/external/newlib/newlib_sp_assert.c
+++ b/external/newlib/newlib_sp_assert.c
@@ -1,21 +1,21 @@
// SPDX-License-Identifier: BSD-3-Clause
/*
- * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited and Contributors. All rights reserved.
*/
-#include <assert.h>
+#include "assert_fail_handler.h"
#include "compiler.h"
-#include "trace.h"
+#include <assert.h>
/*
- * The generic trace function called on assert fail.
+ * This function implements newlib's assert fail handler function by calling the
+ * generic assert fail handler function that should be implemented by the
+ * environment.
*/
-void __noreturn __assert_func(const char *file, int line, const char *func, const char *failedexpr)
+void __noreturn __assert_func(const char *file, int line, const char *func,
+ const char *failedexpr)
{
-#if TRACE_LEVEL >= TRACE_LEVEL_ERROR
- trace_printf(func, line, TRACE_LEVEL_ERROR, "assertion %s failed", failedexpr);
-#endif /* TRACE_LEVEL */
-
+ assert_fail_handler(file, line, func, failedexpr);
while (1)
;
}