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 access violation issue in libgpiod v1 driver #2311

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

pgrawehr
Copy link
Contributor

@pgrawehr pgrawehr commented May 5, 2024

Fixes #1849

After the cause is finally verified, this removes the SafeHandle base class from SafeLineHandle and changes it to a normal class. The line(s) are always disposed together with their chips, and thus do not need to be explicitly disposed. Lines (individual pins) must not be disposed after the chip is disposed.

This also adds various tests to make sure exceptions are thrown when attempting to use disposed drivers.

Microsoft Reviewers: Open in CodeFlow

/// </summary>
public void ReleaseLock()
{
ReleaseHandle();
// Contrary to intuition, this does not invalidate the handle (see comment on declaration)
Interop.LibgpiodV1.gpiod_line_release(_handle);
Copy link
Member

Choose a reason for hiding this comment

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

Assuming this will make it so that IsInvalid method will return true? Does it set the handle to IntPtr.Zero or to InvalidHandleValue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, see comment. This just internally decreases some kind of reference counter and does not invalidate the handle. IIRC this is needed to change the mode of a pin without closing it.

{
if (_handle != IntPtr.Zero)
{
Interop.LibgpiodV1.gpiod_line_release(_handle);
Copy link
Member

Choose a reason for hiding this comment

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

Whould this call ReleaseLock instead? And should ReleaseLock also set _handle to IntPtr.Zero?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

They're separate because they serve different purposes. Here, we really want to close the line (from an user point of view), the other method is a special case because gpiod_line_release needs to be called before a change of the pin mode is possible. Technically, the handle is never invalidated until the owning chip is closed.

@@ -13,6 +13,12 @@
<Left>lib/netstandard2.0/System.Device.Gpio.dll</Left>
<Right>lib/net6.0-windows10.0.17763/System.Device.Gpio.dll</Right>
</Suppression>
<Suppression>
<DiagnosticId>CP0002</DiagnosticId>
Copy link
Member

Choose a reason for hiding this comment

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

I wouldn't expect this to show up as we should have the finalizer in both places. Can you please check why this is 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.

I have no clue, but looks like a bug of the tool to me. We have added a finalizer, so why should there be a warning that it's now missing? I can't see why this would be a breaking change, anyway.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I verified the generated IL. The finalize method is also present in the netstandard2.0 assembly.

@joperezr joperezr added this to the v3.2.0 milestone May 23, 2024
@joperezr
Copy link
Member

/azp run dotnet.iot

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

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.

Segfault in finalization with LibGpiodDriver
3 participants