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

Always use mmap instead of posix_memalign in cranelift-jit #4986

Open
bjorn3 opened this issue Sep 30, 2022 · 4 comments
Open

Always use mmap instead of posix_memalign in cranelift-jit #4986

bjorn3 opened this issue Sep 30, 2022 · 4 comments

Comments

@bjorn3
Copy link
Contributor

bjorn3 commented Sep 30, 2022

This currently behind the selinux-fix feature flag. SELinux doesn't allow making parts of the heap executable. In addition it seems that there is an issue in multithreaded environments causing permission issues leading to crashes when using posix_memalign instead of mmap. (see #4980) I don't know why using posix_memalign was chosen initially. Maybe there is some advantage to it that I don't know of. If not, I don't see any reason to stay with posix_memalign as default. @sunfishcode do you remember why you used posix_memalign?

@sunfishcode
Copy link
Member

It's std::alloc::alloc now, rather than posix_memalign, but the same question applies.

I think the reason was that alloc/posix_memalign can be faster, because it can reuse free memory that the allocator has, rather than always requiring an mmap system call to allocate and a munmap system call to free each time. However, I don't have any data on whether it's actually faster.

@bjorn3
Copy link
Contributor Author

bjorn3 commented Sep 30, 2022

Maybe we could make a simple memory allocator ourself? Unlike system allocators it doesn't have to deal with tiny allocations or allocations of fixed sizes, so complexity from that doesn't have to exist and it could be made aware of which page has which memory protection applied so that once a page is made executable it will only consider it allocatable once every function stored in it has been deallocated. This way it is also no longer necessary to allocate multiples of the page size.

@bjorn3
Copy link
Contributor Author

bjorn3 commented Sep 30, 2022

Another advantage of that would be that it allows keeping all code within 2GB even when allocating a lot of memory in between compiling functions. This is necessary to avoid the panics at

let pcrel = i32::try_from((what as isize) - (at as isize)).unwrap();
and elsewhere during applying of relocations.

@Mrmaxmeier
Copy link
Contributor

I'm using a simple mmap-based allocator to work around #4000 (^) here: https://github.com/Mrmaxmeier/wasmtime/commits/cranelift-jit-reserve-vmem-area
The branch is mostly a hack though and made for easy rebasing, but feel free to benchmark the mmap overhead with it ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants