From b44e6f8a16ccee4bd81deb0922eabd4075a226b1 Mon Sep 17 00:00:00 2001 From: Russ Butler Date: Wed, 24 May 2017 15:12:52 -0500 Subject: [PATCH] Assert that file IO is not used in ISRs Trigger an assert if a file is read from or written to from an interrupt handler or critical section. This includes using printf from an interrupt handler or critical section. This makes failures due to use in incorrect context deterministic and easier to locate. This feature is enabled by defining MBED_TRAP_ERRORS_ENABLED to 1 or by using the debug profile. --- platform/mbed_retarget.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/platform/mbed_retarget.cpp b/platform/mbed_retarget.cpp index 739d6aa717..b4328b14c5 100644 --- a/platform/mbed_retarget.cpp +++ b/platform/mbed_retarget.cpp @@ -23,6 +23,7 @@ #include "platform/PlatformMutex.h" #include "platform/mbed_error.h" #include "platform/mbed_stats.h" +#include "platform/mbed_critical.h" #if MBED_CONF_FILESYSTEM_PRESENT #include "filesystem/FileSystem.h" #include "filesystem/File.h" @@ -309,6 +310,12 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign #endif int n; // n is the number of bytes written +#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED + if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) { + error("Error - writing to a file in an ISR or critical section\r\n"); + } +#endif + if (fh < 3) { #if DEVICE_SERIAL if (!stdio_uart_inited) init_serial(); @@ -353,6 +360,12 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int #endif int n; // n is the number of bytes read +#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED + if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) { + error("Error - reading from a file in an ISR or critical section\r\n"); + } +#endif + if (fh < 3) { // only read a character at a time from stdin #if DEVICE_SERIAL