RFR: 8371637: allocateNativeInternal sometimes return incorrectly aligned memory [v3]
Harald Eilertsen
haraldei at openjdk.org
Fri Nov 14 13:13:00 UTC 2025
> `jdk.internal.foreign.SegmentFactories::allocateNativeInternal` assumes that the underlying implementation of malloc aligns allocations on 16 byte boundaries for 64 bit platforms, and 8 byte boundaries on 32 bit platforms. So for any allocation where the requested alignment is less than or equal to this default alignment it makes no adjustment.
>
> However, this assumption does not hold for all allocators. Specifically jemallc, used by libc on FreeBSD will align small allocations on 8 or 4 byte boundaries, respectively. This causes allocateNativeInternal to sometimes return memory that is not properly aligned when the requested alignment is exactly 16 bytes.
>
> To make sure we honour the requested alignment when it exaclty matches the quantum as defined by MAX_MALLOC_ALIGN, this patch ensures that we adjust the alignment also in this case.
>
> This should make no difference for platforms where malloc allready aligns on the quantum, except for a few unnecessary trivial calculations.
>
> This work was sponsored by: The FreeBSD Foundation
Harald Eilertsen has updated the pull request incrementally with one additional commit since the last revision:
OS agnostic fix for alignment of native segments
Only align up the requested memory if the requested alignment is larget
than max alignment provided by malloc, or if the requested size is not a
multiple of the alignment size.
This work was sponsored by: The FreeBSD Foundation
Co-authored-by: mcimadamore
-------------
Changes:
- all: https://git.openjdk.org/jdk/pull/28235/files
- new: https://git.openjdk.org/jdk/pull/28235/files/2b8266f6..15daa51b
Webrevs:
- full: https://webrevs.openjdk.org/?repo=jdk&pr=28235&range=02
- incr: https://webrevs.openjdk.org/?repo=jdk&pr=28235&range=01-02
Stats: 17 lines in 1 file changed: 4 ins; 11 del; 2 mod
Patch: https://git.openjdk.org/jdk/pull/28235.diff
Fetch: git fetch https://git.openjdk.org/jdk.git pull/28235/head:pull/28235
PR: https://git.openjdk.org/jdk/pull/28235
More information about the core-libs-dev
mailing list