Skip to content
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

Figure out rules for minimal alignment of system allocator #812

Closed
RalfJung opened this issue Jun 30, 2019 · 4 comments · Fixed by #817
Closed

Figure out rules for minimal alignment of system allocator #812

RalfJung opened this issue Jun 30, 2019 · 4 comments · Fixed by #817
Labels
A-intptrcast Area: affects int2ptr and ptr2int casts A-shims Area: This affects the external function shims C-bug Category: This is a bug.

Comments

@RalfJung
Copy link
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?

@RalfJung
Copy link
Member Author

RalfJung commented Jun 30, 2019

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
Copy link
Member Author

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.

@RalfJung RalfJung added A-intptrcast Area: affects int2ptr and ptr2int casts A-shims Area: This affects the external function shims C-bug Category: This is a bug. labels Jun 30, 2019
@SimonSapin
Copy link

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
Copy link
Member Author

RalfJung commented Jul 2, 2019

See jemalloc/jemalloc#1533.

bors added a commit that referenced this issue Jul 5, 2019
align small malloc-allocations even less, and test that we do

Needs rust-lang/rust#62295 to land.

Fixes #812.
@bors bors closed this as completed in #817 Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intptrcast Area: affects int2ptr and ptr2int casts A-shims Area: This affects the external function shims C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants