New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
8 bytes size class in tcmalloc and jemalloc breaks ABI on x86_64 #724
Comments
|
Reported by Florian Weimer in https://bugzilla.redhat.com/show_bug.cgi?id=1237260 |
|
thanks for reporting it. This is interesting. My understanding is that it's pointless to align less than 16 bytes allocations on 16 bytes. And that is seemingly position taken by prior maintainers of tcmalloc. Size class of 8 bytes (which is used for all allocations equal or smaller than 8 bytes) is aligned on 8 bytes. And all other sizes classes are aligned on 16 bytes or more. |
|
And this is actually tested by this line: gperftools/src/tests/tcmalloc_unittest.cc Line 766 in ea0b1d3
kMinAlign is defined here: Line 74 in ea0b1d3
|
|
So perhaps there is indeed "de jure" requirement of x86-64 ABI to always align even small allocations on 16 bytes. But in practice both tcmalloc and jemalloc (I've just tested your program on jemalloc too) do not align smallest allocations on 16 bytes and that seems 100% reasonably. Can you, please, point me to "de jure" thing in ABI document anywhere? |
|
So i've went through http://www.x86-64.org/documentation_folder/abi.pdf searching for places that mention alignment and only requirement that I found that affects malloc is that struct or array alignments are max alignments of elements. Given that no element smaller than 16 bytes has natural alignment of 16 bytes, I believe both tcmalloc and jemalloc are perfectly ABI compliant to align smaller allocations on 8 bytes. With that closing as 'working as intended'. Feel free to reopen if you have arguments against this. |
|
The de jure ruling is this: The alignment of the type On x86_64, |
|
Hi. I disagree about "must align to 16 bytes, even for small allocations". I read this as follows:
|
|
There are basically two ways out: Reduce the alignment of |
|
Indeed that statement in standard about malloc could be interpreted as "malloc has to align to 16 bytes". But do note that your example from link above produces struct S that both requires 16 bytes alignment and has sizeof(struct S) == 16. So malloc(sizeof(struct S)) still works correctly. I'm not a language lawyer, but it seems unreasonable and weird for standard to support something like that: So perhaps some other statements in standard effectively forbid anything that could get smaller alignment from malloc and not be OK with it. |
|
Also I think ultimate intention of standard is something like: "malloc works for malloc(sizeof(T)) of all types" and status quo supports that as far as I can see. |
|
Correction: "malloc works for malloc(sizeof(T)) for all types that don't have extended alignment". |
|
You are correct that example I posted is invalid, However, It's possible to create a type which shows this problem with a GCC extension (alignment specifier on a typedef). |
|
On Mon, Oct 12, 2015 at 4:27 AM, Florian Weimer notifications@github.com
For this particular case I think use of gcc extension invalidates |
This test program:
prints:
max_align_t alignment: 16
malloc: 0x23b6010
malloc: 0x23b6018
malloc: 0x23b6020
malloc: 0x23b6028
malloc: 0x23b6030
malloc: 0x23b6038
malloc: 0x23b6040
malloc: 0x23b6048
malloc: 0x23b6050
malloc: 0x23b6058
This means that not all allocations are aligned to 16 bytes, as required by the
x86_64 ABI.
The text was updated successfully, but these errors were encountered: