[sw/ottf] Add malloc failure and stack overflow hooks.
The OTTF uses FreeRTOS to enable running concurrent tasks within a test.
When a new task is created, memory for the TCB and stack are allocated
using FreeRTOS's malloc implementation. Currently the size of the heap
is fixed to 0x800u bytes. This commit adds FreeRTOS hooks that display
a useful message to test developers and abort the test when FreeRTOS
detects a malloc failure or stack overflow.
Signed-off-by: Timothy Trippel <ttrippel@google.com>
diff --git a/sw/device/lib/testing/test_framework/FreeRTOSConfig.h b/sw/device/lib/testing/test_framework/FreeRTOSConfig.h
index 78c9734..bfadbc7 100644
--- a/sw/device/lib/testing/test_framework/FreeRTOSConfig.h
+++ b/sw/device/lib/testing/test_framework/FreeRTOSConfig.h
@@ -24,10 +24,11 @@
#define configUSE_TICK_HOOK 0
// Memory
-#define configCHECK_FOR_STACK_OVERFLOW 0
+#define configCHECK_FOR_STACK_OVERFLOW 1
#define configMINIMAL_STACK_SIZE 256 // in words
-#define configTOTAL_HEAP_SIZE ((size_t)0x1500u)
#define configSTACK_DEPTH_TYPE uint16_t
+#define configTOTAL_HEAP_SIZE ((size_t)0x800u)
+#define configUSE_MALLOC_FAILED_HOOK 1
// Other
#define configENABLE_BACKWARD_COMPATIBILITY 0
diff --git a/sw/device/lib/testing/test_framework/freertos_hooks.c b/sw/device/lib/testing/test_framework/freertos_hooks.c
index 0f7f6c6..d99adb7 100644
--- a/sw/device/lib/testing/test_framework/freertos_hooks.c
+++ b/sw/device/lib/testing/test_framework/freertos_hooks.c
@@ -2,18 +2,31 @@
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0
-#include "sw/device/lib/testing/test_framework/freertos_hooks.h"
-
+#include "sw/device/lib/irq.h"
#include "sw/device/lib/runtime/hart.h"
#include "sw/device/lib/runtime/log.h"
#include "sw/vendor/freertos_freertos_kernel/include/FreeRTOS.h"
+#include "sw/vendor/freertos_freertos_kernel/include/task.h"
// NOTE: the function names below do NOT, and cannot, conform to the style
// guide, since they are specific implementations of FreeRTOS defined functions.
+/**
+ * This is called if configUSE_MALLOC_FAILED_HOOK is set to 1 in
+ * FreeRTOSConfig.h, and a call to pvPortMalloc() fails.
+ */
void vApplicationMallocFailedHook(void) {
- // TODO: communicate this event back to the host.
- LOG_INFO("Malloc Failed.");
- taskDISABLE_INTERRUPTS();
+ LOG_INFO("FreeRTOS malloc failed. Increase heap size in FreeRTOSConfig.h");
+ irq_global_ctrl(false);
+ abort();
+}
+
+/**
+ * This is called if configCHECK_FOR_STACK_OVERFLOW is set to 1 or 2 in
+ * FreeRTOSConfig.h, and a task detects a stack overflow.
+ */
+void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName) {
+ LOG_INFO("FreeRTOS stack overflow. Increase stack size of task: %s");
+ irq_global_ctrl(false);
abort();
}
diff --git a/sw/device/lib/testing/test_framework/freertos_hooks.h b/sw/device/lib/testing/test_framework/freertos_hooks.h
deleted file mode 100644
index 4b8cb8a..0000000
--- a/sw/device/lib/testing/test_framework/freertos_hooks.h
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright lowRISC contributors.
-// Licensed under the Apache License, Version 2.0, see LICENSE for details.
-// SPDX-License-Identifier: Apache-2.0
-
-#ifndef OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_FREERTOS_HOOKS_H_
-#define OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_FREERTOS_HOOKS_H_
-
-#include "sw/vendor/freertos_freertos_kernel/include/FreeRTOS.h"
-#include "sw/vendor/freertos_freertos_kernel/include/task.h"
-
-// NOTE: the function names below do NOT, and cannot, conform to the style
-// guide, since they are specific implementations of FreeRTOS defined functions.
-
-/**
- * This is called if configUSE_MALLOC_FAILED_HOOK is set to 1 in
- * FreeRTOSConfig.h, and a call to pvPortMalloc() fails.
- */
-void vApplicationMallocFailedHook(void);
-
-#endif // OPENTITAN_SW_DEVICE_LIB_TESTING_TEST_FRAMEWORK_FREERTOS_HOOKS_H_