Skip to content

tinyusb_msc: heap_caps_aligned_calloc() allocates 4x needed memory (DMA OOM) (IEC-486) #397

@pztrick

Description

@pztrick

Answers checklist.

  • I have read the component documentation ESP-IDF Components and the issue is not addressed there.
  • I am using target and esp-idf version as defined in component's idf_component.yml
  • I have searched the issue tracker for a similar issue and not found any related issue.

Which component are you using? If you choose Other, provide details in More Information.

device/esp_tinyusb

ESP-IDF version.

v6.1-dev-1323-gf6aaf22278

Development Kit.

custom esp32-s3

Used Component version.

2.0.1

More Information.

Summary

msc_storage_new() calls heap_caps_aligned_calloc() with incorrect arguments, resulting in allocation of:

sizeof(msc_storage_obj_t) * sizeof(uint32_t)

instead of the intended:

sizeof(msc_storage_obj_t)

This causes ~4x overallocation in MALLOC_CAP_DMA memory and can lead to ESP_ERR_NO_MEM on systems with constrained or fragmented DMA heap.


Current Code

heap_caps_aligned_calloc(MSC_STORAGE_MEM_ALIGN,
                         sizeof(msc_storage_obj_t),
                         sizeof(uint32_t),
                         MALLOC_CAP_DMA);

Per the API:

heap_caps_aligned_calloc(alignment, n, size, caps)

The above allocates:

sizeof(msc_storage_obj_t) * sizeof(uint32_t)

// evaluates to roughly: CONFIG_TINYUSB_MSC_BUFSIZE * 4

Correct Usage

heap_caps_aligned_calloc(MSC_STORAGE_MEM_ALIGN,
                         1,
                         sizeof(msc_storage_obj_t),
                         MALLOC_CAP_DMA);

Impact

  • Causes unnecessary 4× DMA allocation
  • Fails when available DMA memory exceeded
  • Appears intermittent depending on heap fragmentation
  • CONFIG_TINYUSB_MSC_BUFSIZE=8192 would result in ~32K being sought for allocation

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions