From: "Erik Schilling" <erik.schilling@linaro.org>
To: "Viresh Kumar" <viresh.kumar@linaro.org>
Cc: "Linux-GPIO" <linux-gpio@vger.kernel.org>,
"Manos Pitsidianakis" <manos.pitsidianakis@linaro.org>
Subject: Re: [libgpiod][PATCH 1/3] bindings: rust: fix soundness of line_info modeling
Date: Thu, 28 Sep 2023 14:27:35 +0200 [thread overview]
Message-ID: <CVUJTBQZYN6B.17WXH28G8MKZ2@ablu-work> (raw)
In-Reply-To: <20230928112733.nkzirzdcdirmxr3w@vireshk-i7>
On Thu Sep 28, 2023 at 1:27 PM CEST, Viresh Kumar wrote:
> On 27-09-23, 18:29, Erik Schilling wrote:
> > diff --git a/bindings/rust/libgpiod/src/chip.rs b/bindings/rust/libgpiod/src/chip.rs
> > index 81e1be6..02265fc 100644
> > --- a/bindings/rust/libgpiod/src/chip.rs
> > +++ b/bindings/rust/libgpiod/src/chip.rs
> > @@ -95,7 +95,7 @@ impl Chip {
> > }
> >
> > /// Get a snapshot of information about the line.
> > - pub fn line_info(&self, offset: Offset) -> Result<line::Info> {
> > + pub fn line_info(&self, offset: Offset) -> Result<line::InfoOwned> {
> > // SAFETY: The `gpiod_line_info` returned by libgpiod is guaranteed to live as long
> > // as the `struct Info`.
> > let info = unsafe { gpiod::gpiod_chip_get_line_info(self.ichip.chip, offset) };
> > @@ -107,12 +107,16 @@ impl Chip {
> > ));
> > }
> >
> > - line::Info::new(info)
> > + // SAFETY: We verified that the pointer is valid. We own the pointer and
> > + // no longer use it after converting it into a InfoOwned instance.
> > + let line_info = unsafe { line::InfoOwned::from_raw_owned(info) };
> > +
> > + Ok(line_info)
>
> Maybe get rid of the extra `line_info` variable and return directly ?
Will fix in v2
>
> > }
> >
> > /// Get the current snapshot of information about the line at given offset and start watching
> > /// it for future changes.
> > - pub fn watch_line_info(&self, offset: Offset) -> Result<line::Info> {
> > + pub fn watch_line_info(&self, offset: Offset) -> Result<line::InfoOwned> {
> > // SAFETY: `gpiod_line_info` is guaranteed to be valid here.
> > let info = unsafe { gpiod::gpiod_chip_watch_line_info(self.ichip.chip, offset) };
> >
> > @@ -123,7 +127,11 @@ impl Chip {
> > ));
> > }
> >
> > - line::Info::new_watch(info)
> > + // SAFETY: We verified that the pointer is valid. We own the instance and
> > + // no longer use it after converting it into a InfoOwned instance.
> > + let line_info = unsafe { line::InfoOwned::from_raw_owned(info) };
> > +
> > + Ok(line_info)
>
> Same here ?
>
> > diff --git a/bindings/rust/libgpiod/src/info_event.rs b/bindings/rust/libgpiod/src/info_event.rs
> > index db60600..e88dd72 100644
> > --- a/bindings/rust/libgpiod/src/info_event.rs
> > +++ b/bindings/rust/libgpiod/src/info_event.rs
> > @@ -44,7 +44,7 @@ impl Event {
> > }
> >
> > /// Get the line-info object associated with the event.
> > - pub fn line_info(&self) -> Result<line::Info> {
> > + pub fn line_info(&self) -> Result<&line::Info> {
> > // SAFETY: `gpiod_line_info` is guaranteed to be valid here.
> > let info = unsafe { gpiod::gpiod_info_event_get_line_info(self.event) };
> >
> > @@ -55,7 +55,9 @@ impl Event {
> > ));
> > }
> >
> > - line::Info::new_from_event(info)
> > + let line_info = unsafe { line::Info::from_raw_non_owning(info) };
>
> SAFETY comment ?
Good catch. Forgot that the lint is not enabled by default... Will fix
in v2.
>
> > +
> > + Ok(line_info)
> > }
> > }
> >
> > diff --git a/bindings/rust/libgpiod/src/line_info.rs b/bindings/rust/libgpiod/src/line_info.rs
> > impl Info {
> > - fn new_internal(info: *mut gpiod::gpiod_line_info, contained: bool) -> Result<Self> {
> > - Ok(Self { info, contained })
> > - }
> > -
> > - /// Get a snapshot of information about the line.
> > - pub(crate) fn new(info: *mut gpiod::gpiod_line_info) -> Result<Self> {
> > - Info::new_internal(info, false)
> > - }
> > -
> > - /// Get a snapshot of information about the line and start watching it for changes.
> > - pub(crate) fn new_watch(info: *mut gpiod::gpiod_line_info) -> Result<Self> {
> > - Info::new_internal(info, false)
> > + /// Converts a non-owning pointer to a wrapper reference of a specific
> > + /// lifetime
> > + ///
> > + /// No ownership will be assumed, the pointer must be free'd by the original
> > + /// owner.
> > + ///
> > + /// SAFETY: The pointer must point to an instance that is valid for the
> > + /// entire lifetime 'a. The instance must be owned by an object that is
> > + /// owned by the thread invoking this method. The owning object may not be
> > + /// moved to another thread for the entire lifetime 'a.
> > + pub(crate) unsafe fn from_raw_non_owning<'a>(info: *mut gpiod::gpiod_line_info) -> &'a Info {
>
> I think we can get rid of _non_owning, and _owned later on, from functions since
> the parent structure already says so.
>
> Info::from_raw()
> InfoOwned::from_raw()
>
> should be good enough ?
I got no strong feelings here. I first started with `from_raw`, but switched to
the added suffix since `Info::from_raw` sounded ambigous to me.
>
> > - /// Get the Line info object associated with an event.
> > - pub(crate) fn new_from_event(info: *mut gpiod::gpiod_line_info) -> Result<Self> {
> > - Info::new_internal(info, true)
> > + fn as_raw_ptr(&self) -> *mut gpiod::gpiod_line_info {
> > + self as *const _ as *mut _
>
> What's wrong with keeping `_info` as `info` in the structure and using it
> directly instead of this, since this is private anyway ?
We would still need to cast it the same way. One _could_ write:
fn as_raw_ptr(&self) -> *mut gpiod::gpiod_line_info {
&self.info as *const _ as *mut _
}
But the cast dance is still required since we need a *mut, but start
with a readonly reference.
This is required since libgpiod's C lib keeps the struct internals
opaque and does not make guarantees about immutable datastructures for
any API calls.
Technically, the 1:1 mapping of this to Rust would be to restrict the
entire API to `&mut self`. One could do that - it would probably allow
us to advertise the structs as `Sync` - but it would require consumers
to declare all libgpiod-related variables as `mut`.
- Erik
next prev parent reply other threads:[~2023-09-28 12:27 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-27 16:29 [libgpiod][PATCH 0/3] bindings: rust: fix modeling of line_info lifetimes Erik Schilling
2023-09-27 16:29 ` [libgpiod][PATCH 1/3] bindings: rust: fix soundness of line_info modeling Erik Schilling
2023-09-28 11:27 ` Viresh Kumar
2023-09-28 12:27 ` Erik Schilling [this message]
2023-09-29 10:39 ` Viresh Kumar
2023-09-29 10:58 ` Erik Schilling
2023-09-29 11:02 ` Viresh Kumar
2023-09-28 13:24 ` Erik Schilling
2023-09-29 10:39 ` Viresh Kumar
2023-09-29 11:06 ` Erik Schilling
2023-09-29 10:50 ` Manos Pitsidianakis
2023-09-27 16:29 ` [libgpiod][PATCH 2/3] bindings: rust: allow cloning line::Info -> line::OwnedInfo Erik Schilling
2023-09-28 12:52 ` Erik Schilling
2023-09-29 10:50 ` Viresh Kumar
2023-09-29 11:05 ` Erik Schilling
2023-09-27 16:29 ` [libgpiod][PATCH 3/3] bindings: rust: bump major for libgpiod crate Erik Schilling
2023-09-29 12:43 ` Bartosz Golaszewski
2023-09-29 12:45 ` Erik Schilling
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CVUJTBQZYN6B.17WXH28G8MKZ2@ablu-work \
--to=erik.schilling@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=manos.pitsidianakis@linaro.org \
--cc=viresh.kumar@linaro.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox