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

Shouldn't div_ceil be used to convert from size to pages? #47

Closed
daniel-wong-dfinity-org opened this issue Feb 10, 2025 · 3 comments
Closed

Comments

@daniel-wong-dfinity-org
Copy link
Contributor

Specifically, here in wasm.rs, it says

let pages = size / self.page_size();

IIUC, alloc promises to allocate AT LEAST size bytes.

To do that, the number of pages would need to be rounded UP. Whereas, the current code rounds DOWN. For example, the code could instead say this:

let pages = size.div_ceil(self.page_size());

(Ofc, the behavior is the same when size HAPPENS TO BE divisible by page_size.)

Am I missing something? If I'm right, seems like someone would have HAD to have noticed this by now. Unless everyone is always passing a size that is divisible by self.page_size(), anyone who is relying on the "at least size" guarantee would have been "short changed", which I imagine would very often cause unrecoverable problems...

@alexcrichton
Copy link
Owner

I believe you're correct, thanks for the sharp eye!

The reason this hasn't come up before I think is that the only call to system_allocator.alloc is preceded by rounding up the alignment size where DEFAULT_GRANULARITY is already the wasm page size. Basically "we got lucky".

I think it'd be reasonable to change this to using div_ceil!

@daniel-wong-dfinity-org
Copy link
Contributor Author

Thank you for checking! I'll send you a pull request soon.

@daniel-wong-dfinity-org
Copy link
Contributor Author

Can you please review this pull request?

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

2 participants