Add heap alignment workaround for ESP32-S3 operator new#42
Open
ltowarek wants to merge 1 commit into
Open
Conversation
ESP-IDF's heap allocator returns 4-byte-aligned blocks on 32-bit Xtensa, but the ESP32-S3 toolchain reports alignof(max_align_t)=8. Non-conforming blocks crash protobuf's TaggedAllocationPolicyPtr via InstrFetchProhibited at PC=0x00010000. Replaces global operator new/delete with versions backed by heap_caps_aligned_alloc(alignof(max_align_t), ...). Tracked in issue #32. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
e345fe4 to
2b5e2a2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/esp_heap_align.cpp— replaces the six standard replaceable allocation operators (operator new,operator new[], and theirdeletecounterparts) with versions backed byheap_caps_aligned_alloc(alignof(max_align_t), ...), makingoperator newconforming on ESP32-S3Kconfigwith a newESP_OPENTELEMETRY_HEAP_ALIGN_WORKAROUNDbool option (defaulty)CMakeLists.txtto conditionally compileesp_heap_align.cppand pull it onto the link line via-Wl,--undefined=_Znwjtest/:heap_align_idf— pure ESP-IDF alignment survey (no external libs); shows which allocations violate the C/C++ standardheap_align_protobuf— uses protobuf'sArenawith the exactArenaOptionsfromOtlpHttpExporter; reproduces theInstrFetchProhibitedcrash atPC=0x00010000without the workaroundBackground
ESP-IDF's
multi_heap.cusessizeof(void*)=4as its alignment granularity. The ESP32-S3 toolchain reportsalignof(std::max_align_t)==8. Protobuf'sTaggedAllocationPolicyPtrstores control flags in the low 3 bits of a pointer (kPtrMask = ~7), requiring 8-byte alignment. Whenoperator newreturns a 4-byte-aligned block,get()computesptr & ~7 = ptr - 4, readingmax_block_size(0x00010000) as theblock_allocfunction pointer — crashing withInstrFetchProhibitedatPC=0x00010000. Tracked in issue #32.Test plan
test/heap_align_protobufwithCONFIG_ESP_OPENTELEMETRY_HEAP_ALIGN_WORKAROUND=y(default) — confirm it printsPASS: Arena survived second-block allocationCONFIG_ESP_OPENTELEMETRY_HEAP_ALIGN_WORKAROUND=n— confirm it crashes withInstrFetchProhibitedatPC=0x00010000test/heap_align_idf— confirmNon-conforming: 0 / 16with workaround enabled