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

Fix free after clone Pt. 2 #7

Conversation

mkatychev
Copy link
Contributor

@mkatychev mkatychev commented Aug 1, 2024

  • Removed bit masking ownership resolution in favour of reference counting:
    owners: Option<Arc<AtomicUsize>>,

Addresses #5 further

src/lib.rs Outdated
Comment on lines 68 to 74
pub const fn borrowed(&self) -> MownStr {
MownStr {
addr: self.addr,
xlen: self.xlen & LEN_MASK,
_phd: PhantomData,
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pchampin this method was particularly finicky in triggering use after free UB behaviour in a lot of sophia term methods

I strongly recommend removing it altogether and rely on Clone + Arc semantics to conserve on string allocation

src/lib.rs Show resolved Hide resolved
@mkatychev mkatychev marked this pull request as ready for review August 1, 2024 23:15
@jkleinknox jkleinknox mentioned this pull request Aug 2, 2024
src/lib.rs Outdated Show resolved Hide resolved
@mkatychev mkatychev force-pushed the mkatychev-jklin/fix-free-after-clone branch from e9ecd6e to 2921d05 Compare August 2, 2024 18:46
Copy link
Owner

@pchampin pchampin left a comment

Choose a reason for hiding this comment

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

Thanks a lot for investigating #5 and proposing this PR. However, the proposed PR negates the whole purpose of MownStr, so I would rather wait until we better understand #5 before merging any change.

Comment on lines +54 to +56
len: other.len(),
_phd: PhantomData,
owners: None,
Copy link
Owner

Choose a reason for hiding this comment

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

This change goes against the aim of MownStr, which is to cost no more space than a regular &str or Box<str>. If we go for this cost, we might as well use Cow<str> instead of MownStr.

Comment on lines +123 to +132
fn clone(&self) -> Self {
self.owners
.as_ref()
.map(|o| o.fetch_add(1, Ordering::Relaxed));

MownStr {
addr: self.addr,
len: self.len,
_phd: self._phd,
owners: self.owners.clone(),
Copy link
Owner

Choose a reason for hiding this comment

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

This radically changes the way MownStr was supposed to work. Namely, when cloning an owned MownStr, the text data should be duplicated, just like when cloning a Box<str>. That's why, IMO, there should not be any double-free even after a clone.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@pchampin Totally agree, the intent was simply to satisfy miri

@mkatychev
Copy link
Contributor Author

Closed in favour of #8

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