Skip to content

[BUG] Assertion fail on RP2040 with heap_4 #1187

Closed
@ldursw

Description

@ldursw

When using heap_4 and pvPortMalloc is called from a critical section, an assertion fails.

pvPortMalloc calls vTaskSuspendAll here which in turn asserts that it is not in a critical section here.

The Pull Request #982 says

As an added bonus, it also allows the SMP implementation of vTaskSuspendAll() to be simplified (as we can assume the vTaskSuspendAll() is now never called in a critical section). As such, an extra assert has been added to vTaskSuspendAll() to ensure that it is never called in a critical section.

which may have caused the issue with pvPortMalloc.

I don't see why a memory allocation can't happen in a critical section. So I reverted part of the PR and it seems to work just fine.

@@ -3853,9 +3853,6 @@ void vTaskSuspendAll( void )
              * uxSchedulerSuspended since that will prevent context switches. */
             ulState = portSET_INTERRUPT_MASK();
 
-            /* This must never be called from inside a critical section. */
-            configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
-
             /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
              * do not otherwise exhibit real time behaviour. */
             portSOFTWARE_BARRIER();
@@ -3867,7 +3864,14 @@ void vTaskSuspendAll( void )
              * it. */
             if( uxSchedulerSuspended == 0U )
             {
-                prvCheckForRunStateChange();
+                if( portGET_CRITICAL_NESTING_COUNT() == 0U )
+                {
+                    prvCheckForRunStateChange();
+                }
+                else
+                {
+                    mtCOVERAGE_TEST_MARKER();
+                }
             }
             else
             {

Target

  • Development board: Raspberry Pi Pico
  • Instruction Set Architecture: Dual Core ARM Cortex M0+
  • IDE and version: Visual Studio Code with PlatformIO
  • Toolchain and version: arm-none-eabi (xPack GNU Arm Embedded GCC x86_64) 12.3.1 20230626

To Reproduce
Configure a project with heap_4 and try to allocate memory in a critical section.

vTaskEnterCritical();
void *ptr = pvPortMalloc(16); // will assert here
vPortFree(ptr);
vTaskExitCritical();

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions