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

rebis-dev: Fix Heap::compute_pstr_size being off by one cell #2759

Open
wants to merge 2 commits into
base: rebis-dev
Choose a base branch
from

Conversation

adri326
Copy link
Contributor

@adri326 adri326 commented Jan 7, 2025

So it looks like my fix for segfault around heap and PStr had an off-by-one error in the logic for compute_pstr_size, which this PR fixes.
This off-by-one error was compounded by a second off-by-one error within the logic of build_functor!() (some parts did not account for the [] atom appended to PStrs), which happened to cancel out with the previous, invalid implementation of compute_pstr_size.

This does not address #2757 (and I would prefer to fix that issue in a separate PR, so that we can get the unit tests of rebis-dev back in order), though the logic for compute_pstr_size should coincide with the corrected behavior of #2757. The test cases I've added here ought to be tweaked once the correct behavior is set in stone.

Now Heap::compute_pstr_size accurately measures the number of bytes needed by Heap::allocate_pstr (except in the instances of #2757).
I've also simplified the logic a bit, and cleaned up the affected tests of build_functor :)

src/machine/heap.rs Outdated Show resolved Hide resolved
@adri326
Copy link
Contributor Author

adri326 commented Jan 8, 2025

It turns out that I had missed one instance of the issue spotted in #2729: scan_slice_to_str wrongly assumed that Heap.inner.ptr was aligned to Heap::heap_cell_alignment().

I've rewritten it (and renamed it to the more accurate scan_pstr_at) to not rely on this incorrect assumption anymore, and to reduce the number of unsafe code needed in it and around it, by expressing it in terms of HeapView instead.

To make it easy to use it in the implementations of SizedHeap, I've introduced a small AsHeapView trait, currently only local to this module.

- Fix Heap::compute_pstr_size being off at len=0, len=n*align and null bytes
  (compute_pstr_size now more accurately reflects the behavior of allocate_pstr).
- Add debug assertion for the behaviour of compute_pstr_size
- Fix off-by-one errors in build_functor
…t to deduplicate unsafe code

scan_slice_to_str was close to being safe already, but the code using it needed to be unsafe,
and the safety of scan_slice_to_str was hinging on the correctness of the caller unsafe block.

It also wrongly assumed that Heap.inner.ptr was aligned to Heap::heap_cell_alignment().

Instead, I have rewritten `scan_slice_to_str` (and renamed it to the more accurate name
`scan_pstr_at`), to fix the incorrect computation caused by this wrong assumption,
and to reduce the unsafe blocks to their minimum, whose safety I could then prove.

The invariants of Heap remain to be fully vetted, as some of the code in this module remain
~wildly unsafe~ :)
@adri326 adri326 force-pushed the fix-compute_pstr_size branch from 8c856bd to 5e08d4d Compare January 12, 2025 11:07
@adri326 adri326 changed the title Fix Heap::compute_pstr_size being off by one cell rebis-dev: Fix Heap::compute_pstr_size being off by one cell Feb 4, 2025
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

Successfully merging this pull request may close these issues.

2 participants