Skip to content

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

Closed
adri326 wants to merge 13 commits intomthom:rebis-devfrom
adri326:fix-compute_pstr_size
Closed

rebis-dev: Fix Heap::compute_pstr_size being off by one cell#2759
adri326 wants to merge 13 commits intomthom:rebis-devfrom
adri326:fix-compute_pstr_size

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 :)

mthom and others added 11 commits October 26, 2024 12:44
This one was a toughie: it turns out that using `ptr::align_of()`` was
a bad idea, since the buffer in `Heap` itself is not aligned to
`Heap::heap_cell_alignment()`, so `ptr::align_of()` would sometimes
return lower values than expected.

That made for an heisenbug: if the alignment of the heap happened to be 4,
then the bug wouldn't trigger.
rebis-dev: Fix UB detected by miri in tests
@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
@triska
Copy link
Contributor

triska commented Feb 17, 2025

A few CI tests apparently fail, I hope at least the formatting would be easy to resolve.

@adri326
Copy link
Contributor Author

adri326 commented Feb 18, 2025

The formatting issue is omnipresent on rebis-dev, running carfo fmt gives me a few thousands of lines of diff (I made that mistake multiple times). I would prefer if @mthom pushed a cargo fmt commit on rebis-dev and I can then work through the rebase of my branches

@triska
Copy link
Contributor

triska commented Feb 25, 2025

Does 823b74c address the same issue as eb16e54?

@mthom
Copy link
Owner

mthom commented Mar 1, 2025

@adri326 Just pushed rebis-dev with a cargo fmt commit.

@mthom mthom force-pushed the rebis-dev branch 3 times, most recently from c2b1261 to 3549588 Compare April 12, 2025 07:22
@triska
Copy link
Contributor

triska commented Apr 30, 2025

@adri326: If you have time, could you please rebase the commits on top of the current rebis-dev branch? Thank you a lot!

@adri326
Copy link
Contributor Author

adri326 commented May 1, 2025

The original issue has been fixed by 2cf3b57 in the meantime

@adri326 adri326 closed this May 1, 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.

3 participants