Skip to content

Figure out rules for minimal alignment of system allocator #812

@RalfJung

Description

@RalfJung
Member

libstd assumes that the system allocator (which does not have an explicitly alignment parameter) still provides some minimal alignment, given by MIN_ALIGN:

// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values.
#[cfg(all(any(target_arch = "x86",
              target_arch = "arm",
              target_arch = "mips",
              target_arch = "powerpc",
              target_arch = "powerpc64",
              target_arch = "asmjs",
              target_arch = "wasm32")))]
pub const MIN_ALIGN: usize = 8;
#[cfg(all(any(target_arch = "x86_64",
              target_arch = "aarch64",
              target_arch = "mips64",
              target_arch = "s390x",
              target_arch = "sparc64")))]
pub const MIN_ALIGN: usize = 16;

However, at least on Unix, MIN_ALIGN is only exploited for allocations where the size is at least as big as the alignment. On Windows, OTOH, MIN_ALIGN is exploited regardless of the size of the allocation.

Does that mean that we can reduce the alignment we provide for small allocations?

Activity

RalfJung

RalfJung commented on Jun 30, 2019

@RalfJung
MemberAuthor

This behavior on Unix platforms was introduced by rust-lang/rust@21d8992 to fix rust-lang/rust#45955 (I totally forgot I reported this...). Curiously, the Windows allocator was left unchanged.

Cc @SimonSapin

RalfJung

RalfJung commented on Jun 30, 2019

@RalfJung
MemberAuthor

https://support.microsoft.com/en-us/help/286470/how-to-use-pageheap-exe-in-windows-xp-windows-2000-and-windows-server seems to indicate that Windows indeed guarantees alignment regardless of size:

The Windows heap managers (all versions) have always guaranteed that the heap allocations have a start address that is 8-byte aligned (on 64-bit platforms the alignment is 16-bytes).

it doesn't say anything about size.
So maybe we should make Miri give out more liberally aligned small allocations everywhere except on Windows.

added
A-intptrcastArea: affects int2ptr and ptr2int casts
A-shimsArea: This affects the external function shims
C-bugCategory: This is a bug.
on Jun 30, 2019
SimonSapin

SimonSapin commented on Jul 1, 2019

@SimonSapin

It’s been a while, but not changing Windows may have been an oversight. I did not look at all into the guarantees provided by HeapAlloc like I did for malloc.

RalfJung

RalfJung commented on Jul 2, 2019

@RalfJung
MemberAuthor
added a commit that references this issue on Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-intptrcastArea: affects int2ptr and ptr2int castsA-shimsArea: This affects the external function shimsC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @SimonSapin@RalfJung

      Issue actions

        Figure out rules for minimal alignment of system allocator · Issue #812 · rust-lang/miri