-
Notifications
You must be signed in to change notification settings - Fork 571
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
base: main
Are you sure you want to change the base?
Conversation
The handle is "owned" by the chip and is freed with that one. Freeing them in the wrong order causes an access violation
src/System.Device.Gpio/System/Device/Gpio/Drivers/Libgpiod/LibGpiodDriver.cs
Outdated
Show resolved
Hide resolved
src/System.Device.Gpio/Interop/Unix/libgpiod/V1/SafeLineHandle.cs
Outdated
Show resolved
Hide resolved
/// </summary> | ||
public void ReleaseLock() | ||
{ | ||
ReleaseHandle(); | ||
// Contrary to intuition, this does not invalidate the handle (see comment on declaration) | ||
Interop.LibgpiodV1.gpiod_line_release(_handle); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
/azp run dotnet.iot |
Azure Pipelines successfully started running 1 pipeline(s). |
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