Skip to content

Implement DataNotification for rust #6543

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

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

rbran
Copy link
Contributor

@rbran rbran commented Apr 1, 2025

No description provided.

@emesare
Copy link
Member

emesare commented Apr 1, 2025

The solution for only registering the notifications is fine with me, I will be able to review this more once testing is done for the upcoming 5.0 release. Thank you!

@emesare emesare self-requested a review April 1, 2025 15:07
@emesare emesare added this to the H milestone Apr 1, 2025
@emesare emesare added the Component: Rust API Issue needs changes to the Rust API label Apr 1, 2025
@emesare emesare linked an issue Apr 17, 2025 that may be closed by this pull request
@emesare emesare self-assigned this Apr 18, 2025
@emesare
Copy link
Member

emesare commented Apr 25, 2025

When constructing the data notification it is very important that we not copy/clone the BNBinaryDataNotification otherwise when we go to unregister the notifications we will be keying off a different object. I understand this is more of an issue with the FFI layer keying off something such as the pointer value but we should still support unregistering it. In the branch below I just wrap the BNBinaryDataNotification constructed in register_data_notification with a Box and move the boxed value into the returned DataNotificationHandle.

let handle = BNBinaryDataNotification {
context: leak_notify as *mut _ as *mut c_void,
$($ffi_param_name: triggers.$fun_name.then_some($fun_name::<H>)),*
};
// Box it to prevent a copy being returned in `DataNotificationHandle`.
let mut boxed_handle = Box::new(handle);
unsafe { BNRegisterDataNotification(view.handle, boxed_handle.as_mut()) };
DataNotificationHandle {
bv: view.to_owned(),
handle: boxed_handle,
_life: std::marker::PhantomData,
}

@greaka
Copy link

greaka commented May 2, 2025

If I understand your comment correctly, then you are doing a pointer equality check during deregister? In that case a Box is not enough, you need a Pin:

- let mut boxed_handle = Box::new(handle); 
+ let mut boxed_handle = Box::pin(handle); 

register_data_notification already introduced the lifetime 'a and binds both H and the output to it. Why do you want to require owning self too in this case? By changing the function signature to something more like the following, you could allow to still use the original struct instance outside of notifications.

    fn register_data_notification<'a, H: CustomDataNotification + 'a>(
        view: &BinaryView,
        notify: &'a H,
        triggers: DataNotificationTriggers,
    ) -> DataNotificationHandle<'a, H> {}

    fn register<'a>(
        &'a self,
        view: &BinaryView,
        triggers: DataNotificationTriggers,
    ) -> DataNotificationHandle<'a, Self> where Self: 'a + Sized {
        register_data_notification(view, self, triggers)
    }

if you require Pin:

    fn register_data_notification<'a, H: CustomDataNotification + 'a>(
        view: &BinaryView,
        notify: Pin<&'a H>,
        triggers: DataNotificationTriggers,
    ) -> DataNotificationHandle<'a, H> {}

    fn register<'a>(
        self: Pin<&'a Self>,
        view: &BinaryView,
        triggers: DataNotificationTriggers,
    ) -> DataNotificationHandle<'a, Self> where Self: 'a + Sized {
        register_data_notification(view, self, triggers)
    }

With a few extra lines you could then also support ?Sized implementers. You need to support saving the original fat pointer to make that work.

One alternative would be to add support for fetching the original Self via a getter on DataNotificationHandle, I'm not sold on that idea yet though.

@emesare
Copy link
Member

emesare commented May 5, 2025

If I understand your comment correctly, then you are doing a pointer equality check during deregister? In that case a Box is not enough, you need a Pin:

Yea thanks for the correction, we want to pin the boxed value. Will make sure to swap out the call before merging.

@rbran
Copy link
Contributor Author

rbran commented May 5, 2025

If I understand your comment correctly, then you are doing a pointer equality check during deregister? In that case a Box is not enough, you need a Pin:

I can't see how the value could be moved leaving the original pointer invalid, specially because the Box is leaked immediately and only the type implementation itself will operate on the data ref. Let me know of an example where that can happen if you know some.

register_data_notification already introduced the lifetime 'a and binds both H and the output to it. Why do you want to require owning self too in this case?

That's a really good idea, I did that in fact, the only difference is that CustomDataNotification::register is the easy-to-use function for Unpin (99% of the cases) impl types and register_data_notification for anything else that needs Pin.

With a few extra lines you could then also support ?Sized implementers. You need to support saving the original fat pointer to make that work.

That will require a significant amount of complexity, I think that's better left for the user to implement manually, aka put the type in a Box, mostly because is very unlikely we will see a UnSized type used here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Rust API Issue needs changes to the Rust API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Map out data notifications in the rust api
3 participants