Skip to content

fix(guest-bin): align user memory allocations #753

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

andreiltd
Copy link
Member

The guest allocation wrapper aligned only the total memory block but did not guarantee that the pointer returned to the user was itself properly aligned. This patch attempts to fix that.

It also adds alloc_aligned function which would be tricky to implement outside the guest but will make implementing other posix like allocation API much easier.

@andreiltd andreiltd force-pushed the fix-guest-align-mem branch from 7aee0e0 to b81669d Compare July 31, 2025 14:39
@andreiltd andreiltd added the kind/enhancement For PRs adding features, improving functionality, docs, tests, etc. label Jul 31, 2025
@andreiltd andreiltd marked this pull request as draft July 31, 2025 14:45
@andreiltd andreiltd force-pushed the fix-guest-align-mem branch from b81669d to f3efbb5 Compare July 31, 2025 15:57
The guest allocation wrapper aligned only the total memory block but did
not guarantee that the pointer returned to the user was itself properly
aligned. This patch attempts to fix that.

It also adds alloc_aligned function which would be tricky to implement
outside the guest but will make implementing other posix like allocation
API much easier.

Signed-off-by: Tomasz Andrzejak <[email protected]>
@andreiltd andreiltd force-pushed the fix-guest-align-mem branch from f3efbb5 to cc20014 Compare July 31, 2025 15:58
@andreiltd andreiltd force-pushed the fix-guest-align-mem branch from 0e3f72e to c623979 Compare July 31, 2025 16:18
@andreiltd andreiltd marked this pull request as ready for review July 31, 2025 16:38
/// # Safety
/// The returned pointer must be freed with `memory::free` when it is no longer needed, otherwise memory will leak.
unsafe fn alloc_helper(size: usize, zero: bool) -> *mut c_void {
unsafe fn alloc_helper(size: usize, alignment: usize, zero: bool) -> *mut c_void {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests we should be adding to verify this new behavior?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is tricky. This crate default target is baremetal and cargo doesn't allow different default targets for build and test: rust-lang/cargo#6784 so cargo test won't work.

It really only make sense to write the tests within this module so we have an access to internal memory layout with header and whatnot. So we should probably either use custom test harness or unstable test_runner features?

https://doc.rust-lang.org/beta/unstable-book/language-features/custom-test-frameworks.html

cc: @jprendes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement For PRs adding features, improving functionality, docs, tests, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants