-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[Android] Prevent race condition by synchronizing buffer access #117950
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
[Android] Prevent race condition by synchronizing buffer access #117950
Conversation
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines will not run the associated pipelines, because the pull request was updated after the run command was issued. Review the pull request again and issue a new run command. |
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines successfully started running 2 pipeline(s). |
Tagging subscribers to 'arch-android': @vitek-karas, @simonrozsival, @steveisok, @akoeplinger |
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.
Pull Request Overview
This PR fixes a race condition in Android SSL implementation where GCHandle
references were being freed while native callbacks could still access them, causing InvalidCastException
or NullReferenceException
. The fix extends the GCHandle
lifetime to match the SafeDeleteSslContext
object and adds proper synchronization for buffer operations.
Key changes:
- Replace weak
GCHandle
allocation with persistent handle that lives for the full object lifetime - Add comprehensive locking around all buffer operations to prevent race conditions
- Implement null/disposed checks in native callbacks with graceful error handling
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
src/libraries/tests.proj | Re-enables System.Net.Http.Functional.Tests for Android and adds it to smoke tests |
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs | Implements persistent GCHandle, thread-safe buffer access, and defensive callback handling |
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
…nection methods to use WeakGCHandle
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines successfully started running 2 pipeline(s). |
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Outdated
Show resolved
Hide resolved
…Android/SafeDeleteSslContext.cs Co-authored-by: Jan Kotas <jkotas@microsoft.com>
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines successfully started running 2 pipeline(s). |
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines successfully started running 2 pipeline(s). |
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.
All the failing tests are known and unrelated to these changes.
src/native/libs/System.Security.Cryptography.Native.Android/pal_sslstream.c
Outdated
Show resolved
Hide resolved
src/libraries/System.Net.Security/src/System/Net/Security/Pal.Android/SafeDeleteSslContext.cs
Show resolved
Hide resolved
/azp run runtime-android,runtime-androidemulator |
Azure Pipelines successfully started running 2 pipeline(s). |
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.
All the failing tests are known and unrelated to these changes.
What's the known issue for the wasm failure?
|
I have opened #118244 on the wasm test failure. All CI failure - even the ones unrelated to the change - should have tracking issue: https://github.com/dotnet/runtime/blob/main/docs/workflow/ci/failure-analysis.md#what-to-do-if-you-determine-the-failure-is-unrelated |
Thanks Jan — I merged the PR on the second CI attempt and the failure occurred on the first attempt, which is why it let me merge without an escape comment. |
Problem
Android SSL callbacks
WriteToConnection
/ReadFromConnection
receive an IntPtr that should reference a GCHandle toSafeDeleteSslContext
. The handle was freed while native code could still invoke the callbacks with the IntPtr referencing a different object. This led to a NullReferenceException or InvalidCastException:Description
This PR synchronizes all buffer mutations behind a shared
_lock
, and updates the managed callbacks to verify that the handle is still allocated and its target is aSafeDeleteSslContext
before using it.Changes
Testing
The
System.Net.Http.Functional.Tests
now pass without the crash on Android.Fixes #117045