]> git.lizzy.rs Git - rust.git/commit
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955
authorSimon Sapin <simon.sapin@exyr.org>
Mon, 20 Nov 2017 14:30:04 +0000 (15:30 +0100)
committerSimon Sapin <simon.sapin@exyr.org>
Mon, 20 Nov 2017 14:56:53 +0000 (15:56 +0100)
commit21d899272a7fb39a497424e3260ddab773af7983
treeca082d56ca602fafd323932925df299a5dfe5803
parent41e03c3c469d1c89735fa518a9af4eb3df8b0728
alloc_system: don’t assume MIN_ALIGN for small sizes, fix #45955

The GNU C library (glibc) is documented to always allocate with an alignment
of at least 8 or 16 bytes, on 32-bit or 64-bit platforms:
https://www.gnu.org/software/libc/manual/html_node/Aligned-Memory-Blocks.html

This matches our use of `MIN_ALIGN` before this commit.
However, even when libc is glibc, the program might be linked
with another allocator that redefines the `malloc` symbol and friends.
(The `alloc_jemalloc` crate does, in some cases.)

So `alloc_system` doesn’t know which allocator it calls,
and needs to be conservative in assumptions it makes.

The C standard says:

https://port70.net/%7Ensz/c/c11/n1570.html#7.22.3
> The pointer returned if the allocation succeeds is suitably aligned
> so that it may be assigned to a pointer to any type of object
> with a fundamental alignment requirement

https://port70.net/~nsz/c/c11/n1570.html#6.2.8p2
> A fundamental alignment is represented by an alignment less than
> or equal to the greatest alignment supported by the implementation
> in all contexts, which is equal to `_Alignof (max_align_t)`.

`_Alignof (max_align_t)` depends on the ABI and doesn’t seem to have
a clear definition, but it seems to match our `MIN_ALIGN` in practice.

However, the size of objects is rounded up to the next multiple
of their alignment (since that size is also the stride used in arrays).
Conversely, the alignment of a non-zero-size object is at most its size.
So for example it seems ot be legal for `malloc(8)` to return a pointer
that’s only 8-bytes-aligned, even if `_Alignof (max_align_t)` is 16.
src/liballoc/tests/heap.rs [new file with mode: 0644]
src/liballoc/tests/lib.rs
src/liballoc_system/lib.rs