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

wasi: round up the size for aligned_alloc #115254

Merged
merged 1 commit into from
Aug 28, 2023

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Aug 26, 2023

C11 aligned_alloc requires that the size be a multiple of the
alignment. This is enforced in the wasi-libc emmalloc implementation,
which always returns NULL if the size is not a multiple.
(The default MALLOC_IMPL=dlmalloc does not currently check this.)

C11 `aligned_alloc` requires that the size be a multiple of the
alignment. This is enforced in the wasi-libc emmalloc implementation,
which always returns NULL if the size is not a multiple.
(The default `MALLOC_IMPL=dlmalloc` does not currently check this.)
@rustbot
Copy link
Collaborator

rustbot commented Aug 26, 2023

r? @thomcc

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 26, 2023
@cuviper cuviper added the O-wasi Operating system: Wasi, Webassembly System Interface label Aug 26, 2023
@thomcc
Copy link
Member

thomcc commented Aug 27, 2023

@bors r+

@bors
Copy link
Contributor

bors commented Aug 27, 2023

📌 Commit 1c6d867 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 27, 2023
@thomcc
Copy link
Member

thomcc commented Aug 27, 2023

Note that this behavior has been proposed to change, so we should consider reverting this in the future if https://open-std.org/JTC1/SC22/WG14/www/docs/n2072.htm is standardized.

GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 27, 2023
wasi: round up the size for `aligned_alloc`

C11 `aligned_alloc` requires that the size be a multiple of the
alignment. This is enforced in the wasi-libc emmalloc implementation,
which always returns NULL if the size is not a multiple.
(The default `MALLOC_IMPL=dlmalloc` does not currently check this.)
@cuviper
Copy link
Member Author

cuviper commented Aug 27, 2023

we should consider reverting this in the future if n2072 is standardized.

We could... but I wouldn't bother unless there's some evidence that it's useful, and not just the allocator rounding up on your behalf or otherwise wasting that padding.

@thomcc
Copy link
Member

thomcc commented Aug 27, 2023

It's definitely useful, to the point where it's surprising this restriction exists.

Consider allocating 1 byte at alignment 1024 from a bump nursery (this is true for other types of allocators as well but less easy to show). You'd do something like (curptr + 1023) & !1023 to get the allocation base, then add another byte to curptr to account for the size, this will waste between 0 and 1023 bytes. However, if you rounded the size up to 1024 even though you only need 1 byte, you'd be wasting between 1023 and 2047 bytes.

To put it another way, if I'm an allocator, I only need to allocate size + align bytes to satisfy a given layout, so in the previous example, at most 1025 bytes in order to be guaranteed to find 1 byte which is aligned to 1024. Rounding up size to alignment means that I now have to allocate up to 2048 bytes.

(Note: if I have an off-by-one anywhere in here, I blame the fact that I have the flu)

@bors
Copy link
Contributor

bors commented Aug 28, 2023

⌛ Testing commit 1c6d867 with merge 1baf77a...

@bors
Copy link
Contributor

bors commented Aug 28, 2023

☀️ Test successful - checks-actions
Approved by: thomcc
Pushing 1baf77a to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 28, 2023
@bors bors merged commit 1baf77a into rust-lang:master Aug 28, 2023
11 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Aug 28, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (1baf77a): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.1% [1.2%, 3.9%] 4
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 630.803s -> 631.998s (0.19%)
Artifact size: 316.08 MiB -> 316.03 MiB (-0.02%)

@cuviper cuviper deleted the aligned_alloc-size branch September 2, 2023 03:17
@RalfJung
Copy link
Member

RalfJung commented May 11, 2024

C11 aligned_alloc requires that the size be a multiple of the
alignment.

FWIW, I can't find this requirement in C18 or C23 any more, so wasi may have to be updated here.

EDIT: #125003

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. O-unix Operating system: Unix-like O-wasi Operating system: Wasi, Webassembly System Interface S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants