Fix 32-bit ARM SIGBUS: align elastic allocator payload for coded_lists::buf#262
Merged
aous72 merged 2 commits intoaous72:masterfrom Mar 19, 2026
Merged
Fix 32-bit ARM SIGBUS: align elastic allocator payload for coded_lists::buf#262aous72 merged 2 commits intoaous72:masterfrom
aous72 merged 2 commits intoaous72:masterfrom
Conversation
…s::buf
Problem:
OpenJPH’s mem_elastic_allocator lays out memory as:
[ stores_list header ][ payload... ]
Payload holds placement-new `coded_lists` instances via get_buffer():
p = new (cur_store->data) coded_lists(needed_bytes);
Each `coded_lists` sets its bitstream pointer as:
buf = (ui8*)this + sizeof(coded_lists)
So the address of `buf` is:
slab + offset(stores_list → data) + sizeof(coded_lists)
Previously, `data` was set to immediately after the `stores_list` object
(i.e. offset sizeof(stores_list) from the slab start). The compiler-chosen
size of `stores_list` is not guaranteed to be a multiple of 8 or 16. For
some layouts, the first `coded_lists` header therefore landed at an offset
such that `buf` ended up at an address congruent to 4 (mod 8): 4-byte
aligned but not 8-byte aligned.
On strict 32-bit ARM, libc memcpy used when copying or flushing these
bitstreams (e.g. wide loads such as LDRD) can require 8-byte
alignment. Misaligned `buf` then triggers SIGBUS. This showed up in
practice when OpenJPH was used from OpenEXR (Huffman-table /
compressed data flush paths) on armv6 and armv7 machines.
Root cause:
The failure is not in `coded_lists` itself but in where the payload
region begins relative to the slab. Padding only inside `coded_lists` or
rounding the *user* allocation size is insufficient if the *first* byte
of the payload region is still at a bad offset from the malloc base.
Solution
--------
1. stores_list: Introduce offset16() = round_up(sizeof(stores_list), 16).
Construct with:
data = orig_data = (ui8*)this + offset16()
so the first byte available for `coded_lists` is 16-byte aligned from
the start of the slab. eval_store_bytes() adds this offset so malloc
allocates enough space for header + padding + payload.
2. get_buffer: Round (needed_bytes + sizeof(coded_lists)) up to a multiple
of 16 so successive `coded_lists` regions in the same store keep
consistent alignment as `data` advances.
Together, `coded_lists` headers and their `buf` pointers stay suitably
aligned for memcpy/fwrite on 32-bit ARM while preserving existing
allocator behavior on other platforms.
Analysis and solution made with the help of Cursor / Claude Opus 4.5
Signed-off-by: Cary Phillips <cary@ilm.com>
Merged
Changed function name from offset16() to the more descriptive name stores_list_size16().
Owner
|
Thank you Cary for this very helpful commit. |
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.
Problem:
OpenJPH’s
mem_elastic_allocatorlays out memory as:[ stores_list header ][ payload... ]Payload holds placement-new
coded_listsinstances via get_buffer():p = new (cur_store->data) coded_lists(needed_bytes);Each
coded_listssets its bitstream pointer as:buf = (ui8*)this + sizeof(coded_lists)So the address of
bufis:slab + offset(stores_list → data) + sizeof(coded_lists)Previously,
datawas set to immediately after thestores_listobject (i.e. offset sizeof(stores_list) from the slab start). The compiler-chosen size ofstores_listis not guaranteed to be a multiple of 8 or 16. For some layouts, the firstcoded_listsheader therefore landed at an offset such thatbufended up at an address congruent to 4 (mod 8): 4-byte aligned but not 8-byte aligned.On strict 32-bit ARM, libc memcpy used when copying or flushing these bitstreams (e.g. wide loads such as LDRD) can require 8-byte alignment. Misaligned
bufthen triggers SIGBUS. This showed up in practice when OpenJPH was used from OpenEXR (Huffman-table / compressed data flush paths) on armv6 and armv7 machines.Root cause:
The failure is not in
coded_listsitself but in where the payload region begins relative to the slab. Padding only insidecoded_listsor rounding the user allocation size is insufficient if the first byte of the payload region is still at a bad offset from the malloc base.Solution
stores_list: Introduceoffset16() = round_up(sizeof(stores_list), 16). Construct with:data = orig_data = (ui8*)this + offset16()so the first byte available forcoded_listsis 16-byte aligned from the start of the slab. eval_store_bytes() adds this offset so malloc allocates enough space for header + padding + payload.get_buffer: Round (needed_bytes + sizeof(coded_lists)) up to a multiple of 16 so successivecoded_listsregions in the same store keep consistent alignment asdataadvances. Together,coded_listsheaders and theirbufpointers stay suitably aligned for memcpy/fwrite on 32-bit ARM while preserving existing allocator behavior on other platforms.This resolves AcademySoftwareFoundation/openexr#2134
Analysis and solution made with the help of Cursor / Claude Opus 4.5