Skip to content

unix/coverage: zero the MP3 decoder state when allocating it - #12047

Open
YORROY123 wants to merge 1 commit into
adafruit:mainfrom
YORROY123:mp3-unix-calloc
Open

YORROY123 wants to merge 1 commit into
adafruit:mainfrom
YORROY123:mp3-unix-calloc

Conversation

@YORROY123

Copy link
Copy Markdown

The unix coverage variant allocates the helix MP3 decoder with plain malloc(), but MP3Decode() expects the decoder state to start out zeroed. For example, mainDataBytes is only ever added to. The espressif allocator already clears its memory for this reason (c7807cf), and the default mp3_alloc() uses m_malloc_maybe(), which also returns cleared memory (MICROPY_GC_CONSERVATIVE_CLEAR).

This change switches the coverage variant to calloc().

How it shows up

I built the coverage variant with AddressSanitizer, using the flags from CI_UNIX_OPTS_SANITIZE_ADDRESS in tools/ci.sh. ASan's malloc() fills new memory with 0xbe, and tests/circuitpython/issue9705.py stops on the first get_buffer():

ERROR: AddressSanitizer: negative-size-param: (size=-1094795586)
    #1 memcpy
    #2 MP3Decode ../../lib/mp3/src/mp3dec.c:359
    #3 audiomp3_mp3file_get_buffer ../../shared-module/audiomp3/MP3Decoder.c:516

-1094795586 is 0xbebebebe. The normal build passes only because the memory it gets back happens to be zero.

Testing

  • With the ASan build, the error is gone after this change. The decoded output matches issue9705.py.exp, apart from UBSan's reports about left shifts of negative values in the fixed-point DSP code, which this change doesn't touch.
  • With the ASan build, the whole run-tests.py suite gives 1056 passed. The only failure is issue9705.py, and that is only because of those UBSan lines.
  • With the normal coverage build, run-tests.py -d circuitpython still passes.

Found and tested with Claude Code (Claude Opus 5.5); I reviewed the change.

The coverage variant allocates the helix MP3 decoder with plain
malloc(). MP3Decode() relies on the decoder state starting out zeroed,
for example mainDataBytes, which is only ever added to. The espressif
allocator already clears its memory for this reason (c7807cf), and
the default m_malloc path returns cleared memory too.

With an AddressSanitizer build, whose malloc() fills new memory with
0xbe, tests/circuitpython/issue9705.py stops in MP3Decode() with
"negative-size-param" because mainDataBytes starts as 0xbebebebe. A
normal build only passes because the memory it gets happens to be zero.

Use calloc() so the coverage build matches the other allocators.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant