Skip to content

Conversation

@simonrozsival
Copy link
Member

@simonrozsival simonrozsival commented Dec 14, 2021

  • On (old) Android we need to load getifaddrs and freeifaddrs manually because the ifaddrs.h header is missing in older Android releases (before API 24) but the functions are availabe in libc.
  • I had to change the cmake configuration. check_symbol_exists wasn't reliable for wasm builds so I changed it to check_function_exists which we already use to detect getifaddrs in src/mono/cmake/configure.cmake

The code is based on the Xamarin Android implementation: https://github.com/xamarin/xamarin-android/blob/main/src/monodroid/jni/xamarin_getifaddrs.cc

This PR re-enables some tests that were disabled on Android in #50567
Ref #51303

@ghost ghost added the area-System.Net label Dec 14, 2021
@ghost
Copy link

ghost commented Dec 14, 2021

Tagging subscribers to this area: @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Issue Details

This PR is still WIP.

On (old) Android we need to load getifaddrs and freeifaddrs dynamically because the ifaddrs.h header is missing in older Android releases (before API 24).

The code is based on the Xamarin Android implementation: https://github.com/xamarin/xamarin-android/blob/main/src/monodroid/jni/xamarin_getifaddrs.cc

This PR re-enables some tests that were disabled on Android in #50567
Ref #51303

Author: simonrozsival
Assignees: -
Labels:

area-System.Net

Milestone: -

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival
Copy link
Member Author

All the failing tests are unrelated to this PR.

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

static atomic_bool initialized = false;
static atomic_bool access_guard = true;

while (!atomic_load(&initialized))
Copy link
Member

@jkotas jkotas Jan 7, 2022

Choose a reason for hiding this comment

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

Is pthread_once available on Android? It would be better to use it here instead of the busy waiting loop.

If two threads happen to try to initialize this at the same time, one of them may end up busy waiting for multiple miliseconds here in this implementation that's not efficient. pthread_once does not have this problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

pthread_once is available and it also uses atomic_load_explicit in a loop + futex syscalls, so it will most likely be heavier than this simple loop. The likelihood of two threads hitting this code at exactly the same time is so low that, IMO, it's not worth worrying about.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, pthread_once has a loop similar to this one, except that it also handles the case that I have pointed out. The extra cost of pthread_once is negligible given what this whole method does.

The #1 rule for lock-free programming: Do not write lock-free algorithm yourselves unless you absolutely have to.

@simonrozsival
Copy link
Member Author

/azp run runtime-manual

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@simonrozsival
Copy link
Member Author

The failing runtime-manual tests are unrelated to this PR.

@simonrozsival simonrozsival merged commit 8c47d32 into dotnet:main Jan 11, 2022
@simonrozsival
Copy link
Member Author

/backport to release/6.0-maui

@github-actions
Copy link
Contributor

Started backporting to release/6.0-maui: https://github.com/dotnet/runtime/actions/runs/1683046431

@github-actions
Copy link
Contributor

@simonrozsival backporting to release/6.0-maui failed, the patch most likely resulted in conflicts:

$ git am --3way --ignore-whitespace --keep-non-patch changes.patch

Applying: Re-enable tests
Applying: Dynamically load getifaddrs if necessary
Using index info to reconstruct a base tree...
A	src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Prevent redeclaration of the ifaddrs struct
Using index info to reconstruct a base tree...
M	src/mono/cmake/config.h.in
A	src/native/libs/Common/pal_config.h.in
A	src/native/libs/System.Native/pal_interfaceaddresses.c
A	src/native/libs/configure.cmake
Falling back to patching base and 3-way merge...
Auto-merging src/mono/cmake/config.h.in
Auto-merging src/libraries/Native/Unix/configure.cmake
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Auto-merging src/libraries/Native/Unix/Common/pal_config.h.in
Applying: Fix typo
Using index info to reconstruct a base tree...
A	src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Do not close the dynamic library
Using index info to reconstruct a base tree...
A	src/native/libs/System.Native/pal_interfaceaddresses.c
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/Native/Unix/System.Native/pal_interfaceaddresses.c
Applying: Enable the fixed functional tests in CI
error: sha1 information is lacking or useless (src/libraries/System.Net.NetworkInformation/tests/FunctionalTests/IPGlobalPropertiesTest.cs).
error: could not build fake ancestor
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0006 Enable the fixed functional tests in CI
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

@karelz karelz added this to the 7.0.0 milestone Jan 11, 2022
simonrozsival added a commit to simonrozsival/runtime that referenced this pull request Jan 11, 2022
* Re-enable tests

* Dynamically load getifaddrs if necessary

* Prevent redeclaration of the ifaddrs struct

* Fix typo

* Do not close the dynamic library

* Enable the fixed functional tests in CI

* Reduce code duplication for obtaining libc file name

* Simplify usage of the function pointers

* Move typedefs

* Rename the _ensure_ function

* Remove fptr typedefs

* Update comment

* Add missing include

* Update comment

* Remove unnecessary comment

* Move static variable

* Remove unnecessary change

* Move LIBC_FILENAME to the utilities header

* Avoid error if constant is undefined

* Conditionally include ifaddrs

* Try to fix cmake_symbol_exists issue for browser

* Minor tweaks

* Try different way of detecting getifaddrs

* Use the hack only for Android builds

* Revert "Move LIBC_FILENAME to the utilities header"

This reverts commit 4e67687.

* Revert "Reduce code duplication for obtaining libc file name"

This reverts commit aca15d1.

* Simplify opening libc

* Update code style

* Fix race condition

* Prevent race condition

* Switch locking implementation for a lock-free implementation

* Enable unit test for Android

* Try using weak symbols

* Fix function name

* Revert "Fix function name"

This reverts commit f927aae.

* Revert "Try using weak symbols"

This reverts commit 46d3ede.

* Refactor code to use pthread_once
safern pushed a commit that referenced this pull request Jan 18, 2022
…tion (#63628)

* [Android] Fix accessing network interfaces information (#62780)

* Re-enable tests

* Dynamically load getifaddrs if necessary

* Prevent redeclaration of the ifaddrs struct

* Fix typo

* Do not close the dynamic library

* Enable the fixed functional tests in CI

* Reduce code duplication for obtaining libc file name

* Simplify usage of the function pointers

* Move typedefs

* Rename the _ensure_ function

* Remove fptr typedefs

* Update comment

* Add missing include

* Update comment

* Remove unnecessary comment

* Move static variable

* Remove unnecessary change

* Move LIBC_FILENAME to the utilities header

* Avoid error if constant is undefined

* Conditionally include ifaddrs

* Try to fix cmake_symbol_exists issue for browser

* Minor tweaks

* Try different way of detecting getifaddrs

* Use the hack only for Android builds

* Revert "Move LIBC_FILENAME to the utilities header"

This reverts commit 4e67687.

* Revert "Reduce code duplication for obtaining libc file name"

This reverts commit aca15d1.

* Simplify opening libc

* Update code style

* Fix race condition

* Prevent race condition

* Switch locking implementation for a lock-free implementation

* Enable unit test for Android

* Try using weak symbols

* Fix function name

* Revert "Fix function name"

This reverts commit f927aae.

* Revert "Try using weak symbols"

This reverts commit 46d3ede.

* Refactor code to use pthread_once

* [Android] Throw PNSE for unavailable network information (#63633)

* Update tests

* Add android specific implementation

* Add UnsupportedOSPlatform attributes

* Fix typo

* Remove unnecessary file reference

* Clean-up code

* Minor code clean-up

* Remove dictionary

* Refactoring

* Revert comment change

* Fix usage of Interop.Sys.GetNetworkInterfaces
@ghost ghost locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants