Skip to content

Moving the rest of Unix libraries to the 'overrider' mechanism when linked into static host. - #45995

Merged
VSadov merged 21 commits into
dotnet:masterfrom
VSadov:moreLibs
Dec 18, 2020
Merged

VSadov merged 21 commits into
dotnet:masterfrom
VSadov:moreLibs

Conversation

@VSadov

@VSadov VSadov commented Dec 12, 2020 •

Copy link
Copy Markdown
Member

Resolves:#43699

  • System.Native
  • System.Net.Security.Native
  • System.Security.Cryptography.Native.OpenSsl
  • System.Security.Cryptography.Native.Apple
  • Added a postbuild script to validate that entrypoints.c is consistent with the exports from a corresponding .so/.dylib

There were some changes in the libraries themselves. That was mostly motivated by fixing discrepancies between exported APIs depending on available OS features. Since at a library level we have a single dispatch table, we need all the methods to be present, even if containing trivial error-returning implementation.

NOTE: The same API unification was not done for IOS, TVOS, Android. There are significant differences in exported APIs on mobile.
It can be resolved in the same way - by adding a lot of stubs that do nothing and return errors, but it would be unnecessary churn right now, since we do not do superhost on those platforms.

@ghost

ghost commented Dec 12, 2020

Copy link
Copy Markdown

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

Issue Details
Author: VSadov
Assignees: -
Labels:

area-System.Net

Milestone: -

@VSadov
VSadov marked this pull request as draft December 12, 2020 02:36
@ghost

ghost commented Dec 12, 2020

Copy link
Copy Markdown

Tagging subscribers to this area: @vitek-karas, @agocke
See info in area-owners.md if you want to be subscribed.

Issue Details
  • System.Native
  • System.Net.Security.Native
  • System.Security.Cryptography.Native.OpenSsl
Author: VSadov
Assignees: -
Labels:

area-Host

Milestone: -

@am11

am11 commented Dec 13, 2020

Copy link
Copy Markdown
Member

As far as I know, HAVE_INOTIFY on FreeBSD must be true as it is listed as a prerequisite, so we can simplify it by unconditionally linking it to fix the build failure in installer (instead of duplicating or extracting the related checks in another cmake file):

if (HAVE_INOTIFY)
find_library(INOTIFY_LIBRARY inotify HINTS /usr/local/lib)
list(APPEND ${NativeLibsExtra} ${INOTIFY_LIBRARY})
endif ()

@VSadov

VSadov commented Dec 13, 2020

Copy link
Copy Markdown
Member Author

@am11 - What worries me is that the INOTIFY check in extra_libs.cmake is sufficient when we build .so version of the library, but not enough when linking static host. I wonder why we see the failure on FreeBSD at all.

@am11

am11 commented Dec 13, 2020

Copy link
Copy Markdown
Member

I think because in installer partition, we do not have introspection for HAVE_INOTIFY, which is why that condition in extra_libs.cmake is failing and we are not linking libinotify. Unconditionally linking libinotofy for FreeBSD is reasonable, since an FX lib; FileSystemWatcher, does not function without libinotify on FreeBSD, so it is mandatory.

@VSadov

VSadov commented Dec 13, 2020

Copy link
Copy Markdown
Member Author

@am11 right, clr and libs partitions check for HAVE_INOTIFY, since that impacts which headers to include.
Also if the library is not present, the check will fail the build on FreeBSD.

I think including the libinotify unconditionally is indeed a safe and simple solution.

@VSadov

VSadov commented Dec 16, 2020

Copy link
Copy Markdown
Member Author

@@ -0,0 +1,50 @@
#!/usr/bin/env python

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have been discussing removal of python usage from our build system recently. So adding a new python script doesn't sound like a good thing. Since this is Unix only, a shell script would be ideal. Could you please convert this to a shell script? It should be pretty straightforward. I'll be happy to help with that if you want.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'd really appreciate help here. It would likely take me days to write the same in shell. :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We have CMAKE_NM available, we can probably live even without cmake script calling shell script and just do the checking inside cmake script itself.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, let me put together a shell version.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@am11 our responses have crossed. I don't think we can check that in cmake script, as it is executed at build files generation time and we need to execute it after the actual build.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Sure, let me put together a shell version.

Thanks! I am not using shell scripts often and this is well beyond my expertise.

TBH, I am not an expert in python either, but it is a much more approachable language, so it was my choice over bash or awk.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@janvorli, ok, so it is for the post-build target. We can still pass CMAKE_NM as argument which is located with proper mechanism for the current toolchain, rather than assuming it is available in PATH.

locate_toolchain_exec(nm CMAKE_NM)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sure, that would make sense.

#else
void SystemNative_ReadEvents(int32_t sock, NetworkChangeEvent onNetworkChange)
{
// NOTSUPPORTEDAPI

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think "not supported" and "unreachable" should be treated the same. Is this function a dummy simply to help the linker or is this needed? Basically if it is really "unreachable" it shouldn't be able to ever be executed if this function shouldn't ever be execute and we want a hard failure I think FAILFAST is more appropriate. My concern here is how does one debug this failure?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

NOTSUPPORTEDAPI was just a marker for myself to find all these at the end - in case if I get a better idea what should be in the bodies. I forgot to remove these.

These are methods that were previously excluded on some platforms via conditional compilation due to underlying OS not having said functionality. The stubs that I have put instead are primarily to make compiler happy. They are not expected to be called, since previously they were not even there.
In most cases there is a way to fail gracefully with ENOSUP, which I think is the right way to implement these. However a few cases do not provide such option.

For the cases that cannot naturally fail, my preference would be to have a "release assert" to prevent accidental use of unimplemented API. __builtin_unreachable is one way to achieve that which is also cross-platform.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I could also use abort(). I will switch to that as a more obvious failfast case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I could also use abort(). I will switch to that as a more obvious failfast case.

That would be my preference here. Thanks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In most cases there is a way to fail gracefully with ENOSUP, which I think is the right way to implement these.

There is a quite a bit of prior art in the libraries PAL where we handle this case by returning errno = ENOTSUP. I do not think we need to bother with making distinction between not supported and unreachable for the libraries PAL.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, I went through all of these (had to remove // NOTSUPPORTEDAPI marker anyways) and all looks like using errno = ENOTSUP pattern.

The only one that needs a failfast is void SystemNative_ReadEvents(int32_t sock, NetworkChangeEvent onNetworkChange)

DllImportEntry(BrotliSetDictionaryData)
DllImportEntry(BrotliDecoderGetErrorCode)
DllImportEntry(BrotliEncoderTakeOutput)
DllImportEntry(BrotliEncoderVersion)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need to export these? They do not seem to be used anywhere in the .cs files.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They are exported and the diffing script found them. I am not sure why we export them.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

They are BROTLI_COMMON_API and that ends up __declspec(dllexport) because we set
add_definitions(-DBROTLI_SHARED_COMPILATION)

It looks intentional. Like for use from other native stuff.

I can try not setting BROTLI_SHARED_COMPILATION.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We should not be exporting them if we are not using them. It just keeps dead unused code around.

@@ -14,7 +14,6 @@ set(NATIVECOMPRESSION_SOURCES
)

if (NOT CLR_CMAKE_TARGET_BROWSER)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you please also delete the unused methods from src\libraries\Native\Windows\System.IO.Compression.Native\System.IO.Compression.Native.def ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Looks like there are also actual BROTLI_DEC_API/BROTLI_ENC_API which we are not using.

Ex:
BROTLI_DEC_API BROTLI_BOOL BrotliDecoderHasMoreOutput(...)
BROTLI_ENC_API size_t BrotliEncoderMaxCompressedSize(size_t input_size);

I will assume that in our use patterns we have no need for these and will remove the decorations.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Or better - replace with some noop like BROTLI_INTERNAL, - so that we could find them easily if needed

@am11 am11 Dec 17, 2020 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Changing upstream code is not ideal, we have tried so far to keep them (brotli, zlib, libunwind and rapidjson) drop-in for next version update as much as possible. Maybe we can configure fvisibility and friends to achieve the desired result? Or strip those symbols after target is built.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is tempting to set exports externally via version-scripts. That is known for causing portability issues in the past, but maybe, since this is a simple leaf dll case, it would work here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I made the Unix exports cpecified externally via version-scripts. We will set BROTLI_SHARED_COMPILATION so that all stuff intended to be public is public, as before.
But then what is actually exported will be reduced to what we use by passing explicit list of exports to the linker.

@VSadov VSadov Dec 17, 2020 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

So, now System.IO.Compression.Native DSO exports exactly the number entries that is used by managed part. That is on all platforms - Windows, Unix, OSX, IOS.
Perhaps that will allow for some bits to be trimmed off.

@janvorli

Copy link
Copy Markdown
Member

@am11 I've pushed in the change from .py to .sh, but I haven't passed in the nm path as you've suggested yet. I'll look into that tomorrow with another fix I need to do to the script for OSX.

#!/usr/bin/env bash

dllList=$(nm $1 | awk '/^[0-9a-fA-F]+ T \S+/ { if ($3 != "_init" && $3 != "_fini") print $3 }')
entriesList=$(awk 'match($0, /^\s+DllImportEntry\((\S+)\)/, m) { print m[1]}' $2)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This expression only worked for me on macOS but failed on Linux, FreeBSD and illumos. The following variant works on all (on illumos/OpenIndiana, worked with nawk but not with the default; oawk):

entriesList=$(awk -F'DllImportEntry\\(|\\)' '/DllImportEntry/{ print $2 }' $2)

alternatively, we can use pure bash to split the strings to get the consistent results.

@janvorli

Copy link
Copy Markdown
Member

@am11, @VSadov I've pushed a commit that changes the verification script to not to use awk and also to handle the symbol naming on macOS (leading underscore)

add_definitions(-DTARGET_UNIX)
endif(CLR_CMAKE_TARGET_UNIX)

# Ensure that awk is present

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is needed to make version-scripts helpers to work in libs partition.
We should consider switching libs to use the configurecompiler.cmake. Not in this PR though.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

find_program(MC mc)
if (MC STREQUAL "MC-NOTFOUND")
message(FATAL_ERROR "MC not found")
endif()
else (CLR_CMAKE_HOST_WIN32)
enable_language(ASM)
# Ensure that awk is present
find_program(AWK awk)
if (AWK STREQUAL "AWK-NOTFOUND")
message(FATAL_ERROR "AWK not found")
endif()
# detect linker
set(ldVersion ${CMAKE_C_COMPILER};-Wl,--version)
execute_process(COMMAND ${ldVersion}
ERROR_QUIET
OUTPUT_VARIABLE ldVersionOutput)
if("${ldVersionOutput}" MATCHES "GNU ld" OR "${ldVersionOutput}" MATCHES "GNU gold" OR "${ldVersionOutput}" MATCHES "GNU linkers")
set(LD_GNU 1)
elseif("${ldVersionOutput}" MATCHES "Solaris Link")
set(LD_SOLARIS 1)
else(CLR_CMAKE_HOST_OSX)
set(LD_OSX 1)
endif()

I think these lines belong in eng/native/configuretools.cmake, which is in use by libraries partition. That will prevent us from re-duplicating the code, which was previously de-duplicated. 🙂

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@am11 right, configuretools.cmake is a better place. Thanks!

@am11

am11 commented Dec 17, 2020

Copy link
Copy Markdown
Member

@janvorli, thanks, I agree that getting rid of python dependency is good, but getting rid of a few awk scripts in favor of pure bash script; as you have done, is actually more advantageous in terms of maintenance. The reason is AWK implementation on each Unix-like platform is different, it could be gawk,mawk,nawk,oawk or something else. Cross platform support of consistent syntax by version has not been a problem with python, which makes it a reliable choice in many toolings.

@VSadov

VSadov commented Dec 17, 2020

Copy link
Copy Markdown
Member Author

I think I have addressed all the concerns. If there are no more, may I merge this?

@VSadov

VSadov commented Dec 18, 2020

Copy link
Copy Markdown
Member Author

Thanks everyone!!!

@VSadov
VSadov merged commit e98fb61 into dotnet:master Dec 18, 2020
@VSadov
VSadov deleted the moreLibs branch December 18, 2020 04:43
akoeplinger added a commit to akoeplinger/runtime that referenced this pull request Dec 18, 2020
…obile

dotnet#45995 added this and it relies on this code in configuretools.cmake to detect which linker is used: https://github.com/dotnet/runtime/blob/4bc323242d43dda85d490c0630c4637ffc092469/eng/native/configuretools.cmake#L66-L86

However passing `-Wl,--version` to the compiler doesn't seem to work when using the clang provided by the Android NDK, it still uses the system linker in /usr/bin/ld.
On OSX that one doesn't support the `--version` option so we don't detect LD_GNU which we'd need for providing the correct `-Wl,--version-script` compiler option.

It looks like this only passed on the PR because we build Android on Linux there and the system linker _is_ GNU there so the check was successful.

Disable the generation of the exports file on mobile platforms since we don't use it there anyway.
akoeplinger added a commit that referenced this pull request Dec 18, 2020
…obile (#46222)

#45995 added this and it relies on this code in configuretools.cmake to detect which linker is used: https://github.com/dotnet/runtime/blob/4bc323242d43dda85d490c0630c4637ffc092469/eng/native/configuretools.cmake#L66-L86

However passing `-Wl,--version` to the compiler doesn't seem to work when using the clang provided by the Android NDK, it still uses the system linker in /usr/bin/ld.
On OSX that one doesn't support the `--version` option so we don't detect LD_GNU which we'd need for providing the correct `-Wl,--version-script` compiler option.

It looks like this only passed on the PR because we build Android on Linux there and the system linker _is_ GNU there so the check was successful.

Disable the generation of the exports file on mobile platforms since we don't use it there anyway.
@ghost ghost locked as resolved and limited conversation to collaborators Jan 17, 2021
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.

6 participants