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

Add Handle that own pointer and call C fn to free a pointer #260

Closed
wants to merge 1 commit into from

Conversation

Stargateur
Copy link
Contributor

@Stargateur Stargateur commented Aug 16, 2022

That an idea to avoid fall in trap of Drop impl like in #244.

The purpose is to limit at maximum the risk of double free, with this Handle that own pointer it's very unlucky that a double free happen.

There is still one Drop impl of BpfProgram but this is a little more difficult since the free function ask for a struct not a pointer, this struct use a lot of unsafe, see #261, if we are sure we can malloc to make the clone so we can use free yourself, meaning we could use Handle::new(ptr, free);.

With this PR #244 should not introduce UB.

Copy link
Collaborator

@Wojtek242 Wojtek242 left a comment

Choose a reason for hiding this comment

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

Looks good. Left a few comments to improve consistency with the code base.

@@ -0,0 +1,36 @@
#![allow(dead_code)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why allow dead code? If as_ref and as_mut are not needed, they're not needed so I'd remove them. They can be added later when needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sendqueue.rs use them, I could change the code of sendqueue or add cfg feature gate just for them, I didn't like any of the solution.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah okay, leave a comment explaining this then.


/// MUST NOT IMPLEMENT COPY or CLONE
/// This struct OWN the pointer
/// and have a F implementation that will call F to free the pointer.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Rewrite comment to be more formal and clearer. I suggest.

`Handle` MUST NOT implement `Copy` or `Clone` because it assumes it's the sole owner of the pointer and it will free it upon `Drop`ing.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh and it should be a normal comment (i.e. //) and not a documentation comment (i.e. ///).

@@ -1520,6 +1512,8 @@ fn test_struct_size() {

#[repr(transparent)]
pub struct BpfInstruction(raw::bpf_insn);

// Be aware of the Drop impl
Copy link
Collaborator

Choose a reason for hiding this comment

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

As per #261, please submit a PR to remove this, then rebase and we won't need this comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unfortunately I can't use Handle for remove the BpfProgram Drop impl. I will see what I can do.

@@ -19,7 +20,7 @@ impl SendQueue {
let squeue = unsafe { raw::pcap_sendqueue_alloc(memsize) };
let squeue = NonNull::new(squeue).ok_or(Error::InsufficientMemory)?;

Ok(Self(squeue))
Ok(Self(Handle::new(squeue, raw::pcap_sendqueue_destroy)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

For consistency with Capture and Savefile, please implement a From<NonNull<raw::pcap_send_queue>> trait and use that.

Copy link
Collaborator

@Wojtek242 Wojtek242 left a comment

Choose a reason for hiding this comment

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

Oh and since we should start working towards more comprehensive testing, please add unit tests for Handle.

@Wojtek242
Copy link
Collaborator

Due to significant changes on main since this was proposed, I will close it. If you get around to addressing the comments and adapting to the current code base, I'll be happy to review it.

@Wojtek242 Wojtek242 closed this Feb 25, 2024
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